Bitcoin ABC 0.32.12
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#include "util.h"
11#include "field.h"
12#include "modinv64_impl.h"
13
14#if defined(USE_ASM_X86_64)
15#include "field_5x52_asm_impl.h"
16#else
18#endif
19
35#ifdef VERIFY
36static void secp256k1_fe_verify(const secp256k1_fe *a) {
37 const uint64_t *d = a->n;
38 int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
39 /* secp256k1 'p' value defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
40 r &= (d[0] <= 0xFFFFFFFFFFFFFULL * m);
41 r &= (d[1] <= 0xFFFFFFFFFFFFFULL * m);
42 r &= (d[2] <= 0xFFFFFFFFFFFFFULL * m);
43 r &= (d[3] <= 0xFFFFFFFFFFFFFULL * m);
44 r &= (d[4] <= 0x0FFFFFFFFFFFFULL * m);
45 r &= (a->magnitude >= 0);
46 r &= (a->magnitude <= 2048);
47 if (a->normalized) {
48 r &= (a->magnitude <= 1);
49 if (r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
50 r &= (d[0] < 0xFFFFEFFFFFC2FULL);
51 }
52 }
53 VERIFY_CHECK(r == 1);
54}
55#endif
56
57static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m) {
58 VERIFY_CHECK(m >= 0);
59 VERIFY_CHECK(m <= 2048);
60 r->n[0] = 0xFFFFFFFFFFFFFULL * 2 * m;
61 r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * m;
62 r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * m;
63 r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * m;
64 r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * m;
65#ifdef VERIFY
66 r->magnitude = m;
67 r->normalized = (m == 0);
68 secp256k1_fe_verify(r);
69#endif
70}
71
73 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
74
75 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
76 uint64_t m;
77 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
78
79 /* The first pass ensures the magnitude is 1, ... */
80 t0 += x * 0x1000003D1ULL;
81 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
82 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
83 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
84 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
85
86 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
87 VERIFY_CHECK(t4 >> 49 == 0);
88
89 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
90 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
91 & (t0 >= 0xFFFFEFFFFFC2FULL));
92
93 /* Apply the final reduction (for constant-time behaviour, we do it always) */
94 t0 += x * 0x1000003D1ULL;
95 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
96 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
97 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
98 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
99
100 /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
101 VERIFY_CHECK(t4 >> 48 == x);
102
103 /* Mask off the possible multiple of 2^256 from the final reduction */
104 t4 &= 0x0FFFFFFFFFFFFULL;
105
106 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
107
108#ifdef VERIFY
109 r->magnitude = 1;
110 r->normalized = 1;
111 secp256k1_fe_verify(r);
112#endif
113}
114
116 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
117
118 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
119 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
120
121 /* The first pass ensures the magnitude is 1, ... */
122 t0 += x * 0x1000003D1ULL;
123 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
124 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
125 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
126 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
127
128 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
129 VERIFY_CHECK(t4 >> 49 == 0);
130
131 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
132
133#ifdef VERIFY
134 r->magnitude = 1;
135 secp256k1_fe_verify(r);
136#endif
137}
138
140 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
141
142 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
143 uint64_t m;
144 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
145
146 /* The first pass ensures the magnitude is 1, ... */
147 t0 += x * 0x1000003D1ULL;
148 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
149 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
150 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
151 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
152
153 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
154 VERIFY_CHECK(t4 >> 49 == 0);
155
156 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
157 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
158 & (t0 >= 0xFFFFEFFFFFC2FULL));
159
160 if (x) {
161 t0 += 0x1000003D1ULL;
162 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
163 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
164 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
165 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
166
167 /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
168 VERIFY_CHECK(t4 >> 48 == x);
169
170 /* Mask off the possible multiple of 2^256 from the final reduction */
171 t4 &= 0x0FFFFFFFFFFFFULL;
172 }
173
174 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
175
176#ifdef VERIFY
177 r->magnitude = 1;
178 r->normalized = 1;
179 secp256k1_fe_verify(r);
180#endif
181}
182
184 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
185
186 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
187 uint64_t z0, z1;
188
189 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
190 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
191
192 /* The first pass ensures the magnitude is 1, ... */
193 t0 += x * 0x1000003D1ULL;
194 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
195 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
196 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
197 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
198 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
199
200 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
201 VERIFY_CHECK(t4 >> 49 == 0);
202
203 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
204}
205
207 uint64_t t0, t1, t2, t3, t4;
208 uint64_t z0, z1;
209 uint64_t x;
210
211 t0 = r->n[0];
212 t4 = r->n[4];
213
214 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
215 x = t4 >> 48;
216
217 /* The first pass ensures the magnitude is 1, ... */
218 t0 += x * 0x1000003D1ULL;
219
220 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
221 z0 = t0 & 0xFFFFFFFFFFFFFULL;
222 z1 = z0 ^ 0x1000003D0ULL;
223
224 /* Fast return path should catch the majority of cases */
225 if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
226 return 0;
227 }
228
229 t1 = r->n[1];
230 t2 = r->n[2];
231 t3 = r->n[3];
232
233 t4 &= 0x0FFFFFFFFFFFFULL;
234
235 t1 += (t0 >> 52);
236 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
237 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
238 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
239 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
240
241 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
242 VERIFY_CHECK(t4 >> 49 == 0);
243
244 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
245}
246
248 VERIFY_CHECK(0 <= a && a <= 0x7FFF);
249 r->n[0] = a;
250 r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
251#ifdef VERIFY
252 r->magnitude = (a != 0);
253 r->normalized = 1;
254 secp256k1_fe_verify(r);
255#endif
256}
257
259 const uint64_t *t = a->n;
260#ifdef VERIFY
261 VERIFY_CHECK(a->normalized);
262 secp256k1_fe_verify(a);
263#endif
264 return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
265}
266
268#ifdef VERIFY
269 VERIFY_CHECK(a->normalized);
270 secp256k1_fe_verify(a);
271#endif
272 return a->n[0] & 1;
273}
274
276 int i;
277#ifdef VERIFY
278 a->magnitude = 0;
279 a->normalized = 1;
280#endif
281 for (i=0; i<5; i++) {
282 a->n[i] = 0;
283 }
284}
285
286static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
287 int i;
288#ifdef VERIFY
289 VERIFY_CHECK(a->normalized);
290 VERIFY_CHECK(b->normalized);
291 secp256k1_fe_verify(a);
292 secp256k1_fe_verify(b);
293#endif
294 for (i = 4; i >= 0; i--) {
295 if (a->n[i] > b->n[i]) {
296 return 1;
297 }
298 if (a->n[i] < b->n[i]) {
299 return -1;
300 }
301 }
302 return 0;
303}
304
305static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a) {
306 int ret;
307 r->n[0] = (uint64_t)a[31]
308 | ((uint64_t)a[30] << 8)
309 | ((uint64_t)a[29] << 16)
310 | ((uint64_t)a[28] << 24)
311 | ((uint64_t)a[27] << 32)
312 | ((uint64_t)a[26] << 40)
313 | ((uint64_t)(a[25] & 0xF) << 48);
314 r->n[1] = (uint64_t)((a[25] >> 4) & 0xF)
315 | ((uint64_t)a[24] << 4)
316 | ((uint64_t)a[23] << 12)
317 | ((uint64_t)a[22] << 20)
318 | ((uint64_t)a[21] << 28)
319 | ((uint64_t)a[20] << 36)
320 | ((uint64_t)a[19] << 44);
321 r->n[2] = (uint64_t)a[18]
322 | ((uint64_t)a[17] << 8)
323 | ((uint64_t)a[16] << 16)
324 | ((uint64_t)a[15] << 24)
325 | ((uint64_t)a[14] << 32)
326 | ((uint64_t)a[13] << 40)
327 | ((uint64_t)(a[12] & 0xF) << 48);
328 r->n[3] = (uint64_t)((a[12] >> 4) & 0xF)
329 | ((uint64_t)a[11] << 4)
330 | ((uint64_t)a[10] << 12)
331 | ((uint64_t)a[9] << 20)
332 | ((uint64_t)a[8] << 28)
333 | ((uint64_t)a[7] << 36)
334 | ((uint64_t)a[6] << 44);
335 r->n[4] = (uint64_t)a[5]
336 | ((uint64_t)a[4] << 8)
337 | ((uint64_t)a[3] << 16)
338 | ((uint64_t)a[2] << 24)
339 | ((uint64_t)a[1] << 32)
340 | ((uint64_t)a[0] << 40);
341 ret = !((r->n[4] == 0x0FFFFFFFFFFFFULL) & ((r->n[3] & r->n[2] & r->n[1]) == 0xFFFFFFFFFFFFFULL) & (r->n[0] >= 0xFFFFEFFFFFC2FULL));
342#ifdef VERIFY
343 r->magnitude = 1;
344 if (ret) {
345 r->normalized = 1;
346 secp256k1_fe_verify(r);
347 } else {
348 r->normalized = 0;
349 }
350#endif
351 return ret;
352}
353
355static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
356#ifdef VERIFY
357 VERIFY_CHECK(a->normalized);
358 secp256k1_fe_verify(a);
359#endif
360 r[0] = (a->n[4] >> 40) & 0xFF;
361 r[1] = (a->n[4] >> 32) & 0xFF;
362 r[2] = (a->n[4] >> 24) & 0xFF;
363 r[3] = (a->n[4] >> 16) & 0xFF;
364 r[4] = (a->n[4] >> 8) & 0xFF;
365 r[5] = a->n[4] & 0xFF;
366 r[6] = (a->n[3] >> 44) & 0xFF;
367 r[7] = (a->n[3] >> 36) & 0xFF;
368 r[8] = (a->n[3] >> 28) & 0xFF;
369 r[9] = (a->n[3] >> 20) & 0xFF;
370 r[10] = (a->n[3] >> 12) & 0xFF;
371 r[11] = (a->n[3] >> 4) & 0xFF;
372 r[12] = ((a->n[2] >> 48) & 0xF) | ((a->n[3] & 0xF) << 4);
373 r[13] = (a->n[2] >> 40) & 0xFF;
374 r[14] = (a->n[2] >> 32) & 0xFF;
375 r[15] = (a->n[2] >> 24) & 0xFF;
376 r[16] = (a->n[2] >> 16) & 0xFF;
377 r[17] = (a->n[2] >> 8) & 0xFF;
378 r[18] = a->n[2] & 0xFF;
379 r[19] = (a->n[1] >> 44) & 0xFF;
380 r[20] = (a->n[1] >> 36) & 0xFF;
381 r[21] = (a->n[1] >> 28) & 0xFF;
382 r[22] = (a->n[1] >> 20) & 0xFF;
383 r[23] = (a->n[1] >> 12) & 0xFF;
384 r[24] = (a->n[1] >> 4) & 0xFF;
385 r[25] = ((a->n[0] >> 48) & 0xF) | ((a->n[1] & 0xF) << 4);
386 r[26] = (a->n[0] >> 40) & 0xFF;
387 r[27] = (a->n[0] >> 32) & 0xFF;
388 r[28] = (a->n[0] >> 24) & 0xFF;
389 r[29] = (a->n[0] >> 16) & 0xFF;
390 r[30] = (a->n[0] >> 8) & 0xFF;
391 r[31] = a->n[0] & 0xFF;
392}
393
395#ifdef VERIFY
396 VERIFY_CHECK(a->magnitude <= m);
397 secp256k1_fe_verify(a);
398 VERIFY_CHECK(0xFFFFEFFFFFC2FULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
399 VERIFY_CHECK(0xFFFFFFFFFFFFFULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
400 VERIFY_CHECK(0x0FFFFFFFFFFFFULL * 2 * (m + 1) >= 0x0FFFFFFFFFFFFULL * 2 * m);
401#endif
402 r->n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) - a->n[0];
403 r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[1];
404 r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[2];
405 r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[3];
406 r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->n[4];
407#ifdef VERIFY
408 r->magnitude = m + 1;
409 r->normalized = 0;
410 secp256k1_fe_verify(r);
411#endif
412}
413
415 r->n[0] *= a;
416 r->n[1] *= a;
417 r->n[2] *= a;
418 r->n[3] *= a;
419 r->n[4] *= a;
420#ifdef VERIFY
421 r->magnitude *= a;
422 r->normalized = 0;
423 secp256k1_fe_verify(r);
424#endif
425}
426
428#ifdef VERIFY
429 secp256k1_fe_verify(a);
430#endif
431 r->n[0] += a->n[0];
432 r->n[1] += a->n[1];
433 r->n[2] += a->n[2];
434 r->n[3] += a->n[3];
435 r->n[4] += a->n[4];
436#ifdef VERIFY
437 r->magnitude += a->magnitude;
438 r->normalized = 0;
439 secp256k1_fe_verify(r);
440#endif
441}
442
444#ifdef VERIFY
445 VERIFY_CHECK(a->magnitude <= 8);
446 VERIFY_CHECK(b->magnitude <= 8);
447 secp256k1_fe_verify(a);
448 secp256k1_fe_verify(b);
449 VERIFY_CHECK(r != b);
450 VERIFY_CHECK(a != b);
451#endif
452 secp256k1_fe_mul_inner(r->n, a->n, b->n);
453#ifdef VERIFY
454 r->magnitude = 1;
455 r->normalized = 0;
456 secp256k1_fe_verify(r);
457#endif
458}
459
460static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
461#ifdef VERIFY
462 VERIFY_CHECK(a->magnitude <= 8);
463 secp256k1_fe_verify(a);
464#endif
465 secp256k1_fe_sqr_inner(r->n, a->n);
466#ifdef VERIFY
467 r->magnitude = 1;
468 r->normalized = 0;
469 secp256k1_fe_verify(r);
470#endif
471}
472
473static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
474 uint64_t mask0, mask1;
475 volatile int vflag = flag;
476 VG_CHECK_VERIFY(r->n, sizeof(r->n));
477 mask0 = vflag + ~((uint64_t)0);
478 mask1 = ~mask0;
479 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
480 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
481 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
482 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
483 r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
484#ifdef VERIFY
485 if (flag) {
486 r->magnitude = a->magnitude;
487 r->normalized = a->normalized;
488 }
489#endif
490}
491
493 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
494 uint64_t one = (uint64_t)1;
495 uint64_t mask = -(t0 & one) >> 12;
496
497#ifdef VERIFY
498 secp256k1_fe_verify(r);
499 VERIFY_CHECK(r->magnitude < 32);
500#endif
501
502 /* Bounds analysis (over the rationals).
503 *
504 * Let m = r->magnitude
505 * C = 0xFFFFFFFFFFFFFULL * 2
506 * D = 0x0FFFFFFFFFFFFULL * 2
507 *
508 * Initial bounds: t0..t3 <= C * m
509 * t4 <= D * m
510 */
511
512 t0 += 0xFFFFEFFFFFC2FULL & mask;
513 t1 += mask;
514 t2 += mask;
515 t3 += mask;
516 t4 += mask >> 4;
517
518 VERIFY_CHECK((t0 & one) == 0);
519
520 /* t0..t3: added <= C/2
521 * t4: added <= D/2
522 *
523 * Current bounds: t0..t3 <= C * (m + 1/2)
524 * t4 <= D * (m + 1/2)
525 */
526
527 r->n[0] = (t0 >> 1) + ((t1 & one) << 51);
528 r->n[1] = (t1 >> 1) + ((t2 & one) << 51);
529 r->n[2] = (t2 >> 1) + ((t3 & one) << 51);
530 r->n[3] = (t3 >> 1) + ((t4 & one) << 51);
531 r->n[4] = (t4 >> 1);
532
533 /* t0..t3: shifted right and added <= C/4 + 1/2
534 * t4: shifted right
535 *
536 * Current bounds: t0..t3 <= C * (m/2 + 1/2)
537 * t4 <= D * (m/2 + 1/4)
538 */
539
540#ifdef VERIFY
541 /* Therefore the output magnitude (M) has to be set such that:
542 * t0..t3: C * M >= C * (m/2 + 1/2)
543 * t4: D * M >= D * (m/2 + 1/4)
544 *
545 * It suffices for all limbs that, for any input magnitude m:
546 * M >= m/2 + 1/2
547 *
548 * and since we want the smallest such integer value for M:
549 * M == floor(m/2) + 1
550 */
551 r->magnitude = (r->magnitude >> 1) + 1;
552 r->normalized = 0;
553 secp256k1_fe_verify(r);
554#endif
555}
556
558 uint64_t mask0, mask1;
559 volatile int vflag = flag;
560 VG_CHECK_VERIFY(r->n, sizeof(r->n));
561 mask0 = vflag + ~((uint64_t)0);
562 mask1 = ~mask0;
563 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
564 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
565 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
566 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
567}
568
570#ifdef VERIFY
571 VERIFY_CHECK(a->normalized);
572#endif
573 r->n[0] = a->n[0] | a->n[1] << 52;
574 r->n[1] = a->n[1] >> 12 | a->n[2] << 40;
575 r->n[2] = a->n[2] >> 24 | a->n[3] << 28;
576 r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
577}
578
580 r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
581 r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
582 r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
583 r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
584 r->n[4] = a->n[3] >> 16;
585#ifdef VERIFY
586 r->magnitude = 1;
587 r->normalized = 1;
588 secp256k1_fe_verify(r);
589#endif
590}
591
593 const uint64_t M52 = UINT64_MAX >> 12;
594 const uint64_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4];
595
596 /* The output from secp256k1_modinv64{_var} should be normalized to range [0,modulus), and
597 * have limbs in [0,2^62). The modulus is < 2^256, so the top limb must be below 2^(256-62*4).
598 */
599 VERIFY_CHECK(a0 >> 62 == 0);
600 VERIFY_CHECK(a1 >> 62 == 0);
601 VERIFY_CHECK(a2 >> 62 == 0);
602 VERIFY_CHECK(a3 >> 62 == 0);
603 VERIFY_CHECK(a4 >> 8 == 0);
604
605 r->n[0] = a0 & M52;
606 r->n[1] = (a0 >> 52 | a1 << 10) & M52;
607 r->n[2] = (a1 >> 42 | a2 << 20) & M52;
608 r->n[3] = (a2 >> 32 | a3 << 30) & M52;
609 r->n[4] = (a3 >> 22 | a4 << 40);
610
611#ifdef VERIFY
612 r->magnitude = 1;
613 r->normalized = 1;
614 secp256k1_fe_verify(r);
615#endif
616}
617
619 const uint64_t M62 = UINT64_MAX >> 2;
620 const uint64_t a0 = a->n[0], a1 = a->n[1], a2 = a->n[2], a3 = a->n[3], a4 = a->n[4];
621
622#ifdef VERIFY
623 VERIFY_CHECK(a->normalized);
624#endif
625
626 r->v[0] = (a0 | a1 << 52) & M62;
627 r->v[1] = (a1 >> 10 | a2 << 42) & M62;
628 r->v[2] = (a2 >> 20 | a3 << 32) & M62;
629 r->v[3] = (a3 >> 30 | a4 << 22) & M62;
630 r->v[4] = a4 >> 40;
631}
632
634 {{-0x1000003D1LL, 0, 0, 0, 256}},
635 0x27C7F6E22DDACACFLL
636};
637
638static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x) {
639 secp256k1_fe tmp;
641
642 tmp = *x;
644 secp256k1_fe_to_signed62(&s, &tmp);
647
648#ifdef VERIFY
650#endif
651}
652
654 secp256k1_fe tmp;
656
657 tmp = *x;
659 secp256k1_fe_to_signed62(&s, &tmp);
662
663#ifdef VERIFY
665#endif
666}
667
668#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_get_bounds(secp256k1_fe *r, int m)
Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,...
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)
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_half(secp256k1_fe *r)
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:116
#define VERIFY_CHECK(cond)
Definition: util.h:96
#define SECP256K1_RESTRICT
Definition: util.h:156
#define SECP256K1_INLINE
Definition: secp256k1.h:131
uint32_t n[10]
Definition: field_10x26.h:16