Bitcoin ABC 0.32.7
P2P Digital Currency
field_5x52_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#ifndef SECP256K1_FIELD_REPR_IMPL_H
8#define SECP256K1_FIELD_REPR_IMPL_H
9
10#if defined HAVE_CONFIG_H
11#include "libsecp256k1-config.h"
12#endif
13
14#include "util.h"
15#include "field.h"
16#include "modinv64_impl.h"
17
18#if defined(USE_ASM_X86_64)
19#include "field_5x52_asm_impl.h"
20#else
22#endif
23
39#ifdef VERIFY
40static void secp256k1_fe_verify(const secp256k1_fe *a) {
41 const uint64_t *d = a->n;
42 int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
43 /* secp256k1 'p' value defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
44 r &= (d[0] <= 0xFFFFFFFFFFFFFULL * m);
45 r &= (d[1] <= 0xFFFFFFFFFFFFFULL * m);
46 r &= (d[2] <= 0xFFFFFFFFFFFFFULL * m);
47 r &= (d[3] <= 0xFFFFFFFFFFFFFULL * m);
48 r &= (d[4] <= 0x0FFFFFFFFFFFFULL * m);
49 r &= (a->magnitude >= 0);
50 r &= (a->magnitude <= 2048);
51 if (a->normalized) {
52 r &= (a->magnitude <= 1);
53 if (r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
54 r &= (d[0] < 0xFFFFEFFFFFC2FULL);
55 }
56 }
57 VERIFY_CHECK(r == 1);
58}
59#endif
60
62 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
63
64 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
65 uint64_t m;
66 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
67
68 /* The first pass ensures the magnitude is 1, ... */
69 t0 += x * 0x1000003D1ULL;
70 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
71 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
72 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
73 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
74
75 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
76 VERIFY_CHECK(t4 >> 49 == 0);
77
78 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
79 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
80 & (t0 >= 0xFFFFEFFFFFC2FULL));
81
82 /* Apply the final reduction (for constant-time behaviour, we do it always) */
83 t0 += x * 0x1000003D1ULL;
84 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
85 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
86 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
87 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
88
89 /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
90 VERIFY_CHECK(t4 >> 48 == x);
91
92 /* Mask off the possible multiple of 2^256 from the final reduction */
93 t4 &= 0x0FFFFFFFFFFFFULL;
94
95 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
96
97#ifdef VERIFY
98 r->magnitude = 1;
99 r->normalized = 1;
100 secp256k1_fe_verify(r);
101#endif
102}
103
105 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
106
107 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
108 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
109
110 /* The first pass ensures the magnitude is 1, ... */
111 t0 += x * 0x1000003D1ULL;
112 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
113 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
114 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
115 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
116
117 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
118 VERIFY_CHECK(t4 >> 49 == 0);
119
120 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
121
122#ifdef VERIFY
123 r->magnitude = 1;
124 secp256k1_fe_verify(r);
125#endif
126}
127
129 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
130
131 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
132 uint64_t m;
133 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
134
135 /* The first pass ensures the magnitude is 1, ... */
136 t0 += x * 0x1000003D1ULL;
137 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
138 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
139 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
140 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
141
142 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
143 VERIFY_CHECK(t4 >> 49 == 0);
144
145 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
146 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
147 & (t0 >= 0xFFFFEFFFFFC2FULL));
148
149 if (x) {
150 t0 += 0x1000003D1ULL;
151 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
152 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
153 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
154 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
155
156 /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
157 VERIFY_CHECK(t4 >> 48 == x);
158
159 /* Mask off the possible multiple of 2^256 from the final reduction */
160 t4 &= 0x0FFFFFFFFFFFFULL;
161 }
162
163 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
164
165#ifdef VERIFY
166 r->magnitude = 1;
167 r->normalized = 1;
168 secp256k1_fe_verify(r);
169#endif
170}
171
173 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
174
175 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
176 uint64_t z0, z1;
177
178 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
179 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
180
181 /* The first pass ensures the magnitude is 1, ... */
182 t0 += x * 0x1000003D1ULL;
183 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
184 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
185 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
186 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
187 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
188
189 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
190 VERIFY_CHECK(t4 >> 49 == 0);
191
192 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
193}
194
196 uint64_t t0, t1, t2, t3, t4;
197 uint64_t z0, z1;
198 uint64_t x;
199
200 t0 = r->n[0];
201 t4 = r->n[4];
202
203 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
204 x = t4 >> 48;
205
206 /* The first pass ensures the magnitude is 1, ... */
207 t0 += x * 0x1000003D1ULL;
208
209 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
210 z0 = t0 & 0xFFFFFFFFFFFFFULL;
211 z1 = z0 ^ 0x1000003D0ULL;
212
213 /* Fast return path should catch the majority of cases */
214 if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
215 return 0;
216 }
217
218 t1 = r->n[1];
219 t2 = r->n[2];
220 t3 = r->n[3];
221
222 t4 &= 0x0FFFFFFFFFFFFULL;
223
224 t1 += (t0 >> 52);
225 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
226 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
227 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
228 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
229
230 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
231 VERIFY_CHECK(t4 >> 49 == 0);
232
233 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
234}
235
237 VERIFY_CHECK(0 <= a && a <= 0x7FFF);
238 r->n[0] = a;
239 r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
240#ifdef VERIFY
241 r->magnitude = (a != 0);
242 r->normalized = 1;
243 secp256k1_fe_verify(r);
244#endif
245}
246
248 const uint64_t *t = a->n;
249#ifdef VERIFY
250 VERIFY_CHECK(a->normalized);
251 secp256k1_fe_verify(a);
252#endif
253 return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
254}
255
257#ifdef VERIFY
258 VERIFY_CHECK(a->normalized);
259 secp256k1_fe_verify(a);
260#endif
261 return a->n[0] & 1;
262}
263
265 int i;
266#ifdef VERIFY
267 a->magnitude = 0;
268 a->normalized = 1;
269#endif
270 for (i=0; i<5; i++) {
271 a->n[i] = 0;
272 }
273}
274
275static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
276 int i;
277#ifdef VERIFY
278 VERIFY_CHECK(a->normalized);
279 VERIFY_CHECK(b->normalized);
280 secp256k1_fe_verify(a);
281 secp256k1_fe_verify(b);
282#endif
283 for (i = 4; i >= 0; i--) {
284 if (a->n[i] > b->n[i]) {
285 return 1;
286 }
287 if (a->n[i] < b->n[i]) {
288 return -1;
289 }
290 }
291 return 0;
292}
293
294static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a) {
295 int ret;
296 r->n[0] = (uint64_t)a[31]
297 | ((uint64_t)a[30] << 8)
298 | ((uint64_t)a[29] << 16)
299 | ((uint64_t)a[28] << 24)
300 | ((uint64_t)a[27] << 32)
301 | ((uint64_t)a[26] << 40)
302 | ((uint64_t)(a[25] & 0xF) << 48);
303 r->n[1] = (uint64_t)((a[25] >> 4) & 0xF)
304 | ((uint64_t)a[24] << 4)
305 | ((uint64_t)a[23] << 12)
306 | ((uint64_t)a[22] << 20)
307 | ((uint64_t)a[21] << 28)
308 | ((uint64_t)a[20] << 36)
309 | ((uint64_t)a[19] << 44);
310 r->n[2] = (uint64_t)a[18]
311 | ((uint64_t)a[17] << 8)
312 | ((uint64_t)a[16] << 16)
313 | ((uint64_t)a[15] << 24)
314 | ((uint64_t)a[14] << 32)
315 | ((uint64_t)a[13] << 40)
316 | ((uint64_t)(a[12] & 0xF) << 48);
317 r->n[3] = (uint64_t)((a[12] >> 4) & 0xF)
318 | ((uint64_t)a[11] << 4)
319 | ((uint64_t)a[10] << 12)
320 | ((uint64_t)a[9] << 20)
321 | ((uint64_t)a[8] << 28)
322 | ((uint64_t)a[7] << 36)
323 | ((uint64_t)a[6] << 44);
324 r->n[4] = (uint64_t)a[5]
325 | ((uint64_t)a[4] << 8)
326 | ((uint64_t)a[3] << 16)
327 | ((uint64_t)a[2] << 24)
328 | ((uint64_t)a[1] << 32)
329 | ((uint64_t)a[0] << 40);
330 ret = !((r->n[4] == 0x0FFFFFFFFFFFFULL) & ((r->n[3] & r->n[2] & r->n[1]) == 0xFFFFFFFFFFFFFULL) & (r->n[0] >= 0xFFFFEFFFFFC2FULL));
331#ifdef VERIFY
332 r->magnitude = 1;
333 if (ret) {
334 r->normalized = 1;
335 secp256k1_fe_verify(r);
336 } else {
337 r->normalized = 0;
338 }
339#endif
340 return ret;
341}
342
344static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
345#ifdef VERIFY
346 VERIFY_CHECK(a->normalized);
347 secp256k1_fe_verify(a);
348#endif
349 r[0] = (a->n[4] >> 40) & 0xFF;
350 r[1] = (a->n[4] >> 32) & 0xFF;
351 r[2] = (a->n[4] >> 24) & 0xFF;
352 r[3] = (a->n[4] >> 16) & 0xFF;
353 r[4] = (a->n[4] >> 8) & 0xFF;
354 r[5] = a->n[4] & 0xFF;
355 r[6] = (a->n[3] >> 44) & 0xFF;
356 r[7] = (a->n[3] >> 36) & 0xFF;
357 r[8] = (a->n[3] >> 28) & 0xFF;
358 r[9] = (a->n[3] >> 20) & 0xFF;
359 r[10] = (a->n[3] >> 12) & 0xFF;
360 r[11] = (a->n[3] >> 4) & 0xFF;
361 r[12] = ((a->n[2] >> 48) & 0xF) | ((a->n[3] & 0xF) << 4);
362 r[13] = (a->n[2] >> 40) & 0xFF;
363 r[14] = (a->n[2] >> 32) & 0xFF;
364 r[15] = (a->n[2] >> 24) & 0xFF;
365 r[16] = (a->n[2] >> 16) & 0xFF;
366 r[17] = (a->n[2] >> 8) & 0xFF;
367 r[18] = a->n[2] & 0xFF;
368 r[19] = (a->n[1] >> 44) & 0xFF;
369 r[20] = (a->n[1] >> 36) & 0xFF;
370 r[21] = (a->n[1] >> 28) & 0xFF;
371 r[22] = (a->n[1] >> 20) & 0xFF;
372 r[23] = (a->n[1] >> 12) & 0xFF;
373 r[24] = (a->n[1] >> 4) & 0xFF;
374 r[25] = ((a->n[0] >> 48) & 0xF) | ((a->n[1] & 0xF) << 4);
375 r[26] = (a->n[0] >> 40) & 0xFF;
376 r[27] = (a->n[0] >> 32) & 0xFF;
377 r[28] = (a->n[0] >> 24) & 0xFF;
378 r[29] = (a->n[0] >> 16) & 0xFF;
379 r[30] = (a->n[0] >> 8) & 0xFF;
380 r[31] = a->n[0] & 0xFF;
381}
382
384#ifdef VERIFY
385 VERIFY_CHECK(a->magnitude <= m);
386 secp256k1_fe_verify(a);
387 VERIFY_CHECK(0xFFFFEFFFFFC2FULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
388 VERIFY_CHECK(0xFFFFFFFFFFFFFULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
389 VERIFY_CHECK(0x0FFFFFFFFFFFFULL * 2 * (m + 1) >= 0x0FFFFFFFFFFFFULL * 2 * m);
390#endif
391 r->n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) - a->n[0];
392 r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[1];
393 r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[2];
394 r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[3];
395 r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->n[4];
396#ifdef VERIFY
397 r->magnitude = m + 1;
398 r->normalized = 0;
399 secp256k1_fe_verify(r);
400#endif
401}
402
404 r->n[0] *= a;
405 r->n[1] *= a;
406 r->n[2] *= a;
407 r->n[3] *= a;
408 r->n[4] *= a;
409#ifdef VERIFY
410 r->magnitude *= a;
411 r->normalized = 0;
412 secp256k1_fe_verify(r);
413#endif
414}
415
417#ifdef VERIFY
418 secp256k1_fe_verify(a);
419#endif
420 r->n[0] += a->n[0];
421 r->n[1] += a->n[1];
422 r->n[2] += a->n[2];
423 r->n[3] += a->n[3];
424 r->n[4] += a->n[4];
425#ifdef VERIFY
426 r->magnitude += a->magnitude;
427 r->normalized = 0;
428 secp256k1_fe_verify(r);
429#endif
430}
431
433#ifdef VERIFY
434 VERIFY_CHECK(a->magnitude <= 8);
435 VERIFY_CHECK(b->magnitude <= 8);
436 secp256k1_fe_verify(a);
437 secp256k1_fe_verify(b);
438 VERIFY_CHECK(r != b);
439 VERIFY_CHECK(a != b);
440#endif
441 secp256k1_fe_mul_inner(r->n, a->n, b->n);
442#ifdef VERIFY
443 r->magnitude = 1;
444 r->normalized = 0;
445 secp256k1_fe_verify(r);
446#endif
447}
448
449static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
450#ifdef VERIFY
451 VERIFY_CHECK(a->magnitude <= 8);
452 secp256k1_fe_verify(a);
453#endif
454 secp256k1_fe_sqr_inner(r->n, a->n);
455#ifdef VERIFY
456 r->magnitude = 1;
457 r->normalized = 0;
458 secp256k1_fe_verify(r);
459#endif
460}
461
462static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
463 uint64_t mask0, mask1;
464 volatile int vflag = flag;
465 VG_CHECK_VERIFY(r->n, sizeof(r->n));
466 mask0 = vflag + ~((uint64_t)0);
467 mask1 = ~mask0;
468 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
469 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
470 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
471 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
472 r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
473#ifdef VERIFY
474 if (flag) {
475 r->magnitude = a->magnitude;
476 r->normalized = a->normalized;
477 }
478#endif
479}
480
482 uint64_t mask0, mask1;
483 volatile int vflag = flag;
484 VG_CHECK_VERIFY(r->n, sizeof(r->n));
485 mask0 = vflag + ~((uint64_t)0);
486 mask1 = ~mask0;
487 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
488 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
489 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
490 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
491}
492
494#ifdef VERIFY
495 VERIFY_CHECK(a->normalized);
496#endif
497 r->n[0] = a->n[0] | a->n[1] << 52;
498 r->n[1] = a->n[1] >> 12 | a->n[2] << 40;
499 r->n[2] = a->n[2] >> 24 | a->n[3] << 28;
500 r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
501}
502
504 r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
505 r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
506 r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
507 r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
508 r->n[4] = a->n[3] >> 16;
509#ifdef VERIFY
510 r->magnitude = 1;
511 r->normalized = 1;
512 secp256k1_fe_verify(r);
513#endif
514}
515
517 const uint64_t M52 = UINT64_MAX >> 12;
518 const uint64_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4];
519
520 /* The output from secp256k1_modinv64{_var} should be normalized to range [0,modulus), and
521 * have limbs in [0,2^62). The modulus is < 2^256, so the top limb must be below 2^(256-62*4).
522 */
523 VERIFY_CHECK(a0 >> 62 == 0);
524 VERIFY_CHECK(a1 >> 62 == 0);
525 VERIFY_CHECK(a2 >> 62 == 0);
526 VERIFY_CHECK(a3 >> 62 == 0);
527 VERIFY_CHECK(a4 >> 8 == 0);
528
529 r->n[0] = a0 & M52;
530 r->n[1] = (a0 >> 52 | a1 << 10) & M52;
531 r->n[2] = (a1 >> 42 | a2 << 20) & M52;
532 r->n[3] = (a2 >> 32 | a3 << 30) & M52;
533 r->n[4] = (a3 >> 22 | a4 << 40);
534
535#ifdef VERIFY
536 r->magnitude = 1;
537 r->normalized = 1;
538 secp256k1_fe_verify(r);
539#endif
540}
541
543 const uint64_t M62 = UINT64_MAX >> 2;
544 const uint64_t a0 = a->n[0], a1 = a->n[1], a2 = a->n[2], a3 = a->n[3], a4 = a->n[4];
545
546#ifdef VERIFY
547 VERIFY_CHECK(a->normalized);
548#endif
549
550 r->v[0] = (a0 | a1 << 52) & M62;
551 r->v[1] = (a1 >> 10 | a2 << 42) & M62;
552 r->v[2] = (a2 >> 20 | a3 << 32) & M62;
553 r->v[3] = (a3 >> 30 | a4 << 22) & M62;
554 r->v[4] = a4 >> 40;
555}
556
558 {{-0x1000003D1LL, 0, 0, 0, 256}},
559 0x27C7F6E22DDACACFLL
560};
561
562static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x) {
563 secp256k1_fe tmp;
565
566 tmp = *x;
568 secp256k1_fe_to_signed62(&s, &tmp);
571
572#ifdef VERIFY
574#endif
575}
576
578 secp256k1_fe tmp;
580
581 tmp = *x;
583 secp256k1_fe_to_signed62(&s, &tmp);
586
587#ifdef VERIFY
589#endif
590}
591
592#endif /* SECP256K1_FIELD_REPR_IMPL_H */
static SECP256K1_INLINE void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t *a)
static SECP256K1_INLINE void secp256k1_fe_mul_inner(uint32_t *r, const uint32_t *a, const uint32_t *SECP256K1_RESTRICT b)
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_set_int(secp256k1_fe *r, int a)
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
static SECP256K1_INLINE int secp256k1_fe_is_zero(const secp256k1_fe *a)
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_mul_int(secp256k1_fe *r, int a)
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag)
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
static void secp256k1_fe_to_signed62(secp256k1_modinv64_signed62 *r, const secp256k1_fe *a)
static void secp256k1_fe_normalize(secp256k1_fe *r)
Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,...
static SECP256K1_INLINE int secp256k1_fe_is_odd(const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_clear(secp256k1_fe *a)
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a)
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
Convert a field element to a 32-byte big endian value.
static SECP256K1_INLINE void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_fe_from_signed62(secp256k1_fe *r, const secp256k1_modinv64_signed62 *a)
static const secp256k1_modinv64_modinfo secp256k1_const_modinfo_fe
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b)
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x)
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *x)
static void secp256k1_modinv64(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static void secp256k1_modinv64_var(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
#define VG_CHECK_VERIFY(x, y)
Definition: util.h:115
#define VERIFY_CHECK(cond)
Definition: util.h:95
#define SECP256K1_RESTRICT
Definition: util.h:155
#define SECP256K1_INLINE
Definition: secp256k1.h:127
uint32_t n[10]
Definition: field_10x26.h:16