1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #ifndef _SECP256K1_FIELD_REPR_IMPL_H_
8 #define _SECP256K1_FIELD_REPR_IMPL_H_
10 #if defined HAVE_CONFIG_H
11 #include "libsecp256k1-config.h"
19 #if defined(USE_ASM_X86_64)
20 #include "field_5x52_asm_impl.h"
22 #include "field_5x52_int128_impl.h"
25 /** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,
26 * represented as 5 uint64_t's in base 2^52. The values are allowed to contain >52 each. In particular,
27 * each FieldElem has a 'magnitude' associated with it. Internally, a magnitude M means each element
28 * is at most M*(2^53-1), except the most significant one, which is limited to M*(2^49-1). All operations
29 * accept any input with magnitude at most M, and have different rules for propagating magnitude to their
34 static void secp256k1_fe_verify(const secp256k1_fe_t *a) {
35 const uint64_t *d = a->n;
36 int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
37 /* secp256k1 'p' value defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
38 r &= (d[0] <= 0xFFFFFFFFFFFFFULL * m);
39 r &= (d[1] <= 0xFFFFFFFFFFFFFULL * m);
40 r &= (d[2] <= 0xFFFFFFFFFFFFFULL * m);
41 r &= (d[3] <= 0xFFFFFFFFFFFFFULL * m);
42 r &= (d[4] <= 0x0FFFFFFFFFFFFULL * m);
43 r &= (a->magnitude >= 0);
44 r &= (a->magnitude <= 2048);
46 r &= (a->magnitude <= 1);
47 if (r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
48 r &= (d[0] < 0xFFFFEFFFFFC2FULL);
54 static void secp256k1_fe_verify(const secp256k1_fe_t *a) {
59 static void secp256k1_fe_normalize(secp256k1_fe_t *r) {
60 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
62 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
64 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
66 /* The first pass ensures the magnitude is 1, ... */
67 t0 += x * 0x1000003D1ULL;
68 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
69 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
70 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
71 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
73 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
74 VERIFY_CHECK(t4 >> 49 == 0);
76 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
77 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
78 & (t0 >= 0xFFFFEFFFFFC2FULL));
80 /* Apply the final reduction (for constant-time behaviour, we do it always) */
81 t0 += x * 0x1000003D1ULL;
82 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
83 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
84 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
85 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
87 /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
88 VERIFY_CHECK(t4 >> 48 == x);
90 /* Mask off the possible multiple of 2^256 from the final reduction */
91 t4 &= 0x0FFFFFFFFFFFFULL;
93 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
98 secp256k1_fe_verify(r);
102 static void secp256k1_fe_normalize_weak(secp256k1_fe_t *r) {
103 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
105 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
106 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
108 /* The first pass ensures the magnitude is 1, ... */
109 t0 += x * 0x1000003D1ULL;
110 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
111 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
112 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
113 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
115 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
116 VERIFY_CHECK(t4 >> 49 == 0);
118 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
122 secp256k1_fe_verify(r);
126 static void secp256k1_fe_normalize_var(secp256k1_fe_t *r) {
127 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
129 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
131 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
133 /* The first pass ensures the magnitude is 1, ... */
134 t0 += x * 0x1000003D1ULL;
135 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
136 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
137 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
138 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
140 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
141 VERIFY_CHECK(t4 >> 49 == 0);
143 /* At most a single final reduction is needed; check if the value is >= the field characteristic */
144 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
145 & (t0 >= 0xFFFFEFFFFFC2FULL));
148 t0 += 0x1000003D1ULL;
149 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
150 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
151 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
152 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
154 /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
155 VERIFY_CHECK(t4 >> 48 == x);
157 /* Mask off the possible multiple of 2^256 from the final reduction */
158 t4 &= 0x0FFFFFFFFFFFFULL;
161 r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
166 secp256k1_fe_verify(r);
170 static int secp256k1_fe_normalizes_to_zero(secp256k1_fe_t *r) {
171 uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
173 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
176 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
177 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
179 /* The first pass ensures the magnitude is 1, ... */
180 t0 += x * 0x1000003D1ULL;
181 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
182 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
183 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
184 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
185 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
187 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
188 VERIFY_CHECK(t4 >> 49 == 0);
190 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
193 static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe_t *r) {
194 uint64_t t0, t1, t2, t3, t4;
201 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
204 /* The first pass ensures the magnitude is 1, ... */
205 t0 += x * 0x1000003D1ULL;
207 /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
208 z0 = t0 & 0xFFFFFFFFFFFFFULL;
209 z1 = z0 ^ 0x1000003D0ULL;
211 /* Fast return path should catch the majority of cases */
212 if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
220 t4 &= 0x0FFFFFFFFFFFFULL;
222 t1 += (t0 >> 52); t0 = z0;
223 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
224 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
225 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
226 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
228 /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
229 VERIFY_CHECK(t4 >> 49 == 0);
231 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
234 SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe_t *r, int a) {
236 r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
240 secp256k1_fe_verify(r);
244 SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe_t *a) {
245 const uint64_t *t = a->n;
247 VERIFY_CHECK(a->normalized);
248 secp256k1_fe_verify(a);
250 return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
253 SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe_t *a) {
255 VERIFY_CHECK(a->normalized);
256 secp256k1_fe_verify(a);
261 SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe_t *a) {
267 for (i=0; i<5; i++) {
272 static int secp256k1_fe_cmp_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
275 VERIFY_CHECK(a->normalized);
276 VERIFY_CHECK(b->normalized);
277 secp256k1_fe_verify(a);
278 secp256k1_fe_verify(b);
280 for (i = 4; i >= 0; i--) {
281 if (a->n[i] > b->n[i]) {
284 if (a->n[i] < b->n[i]) {
291 static int secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a) {
293 r->n[0] = r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
294 for (i=0; i<32; i++) {
296 for (j=0; j<2; j++) {
297 int limb = (8*i+4*j)/52;
298 int shift = (8*i+4*j)%52;
299 r->n[limb] |= (uint64_t)((a[31-i] >> (4*j)) & 0xF) << shift;
302 if (r->n[4] == 0x0FFFFFFFFFFFFULL && (r->n[3] & r->n[2] & r->n[1]) == 0xFFFFFFFFFFFFFULL && r->n[0] >= 0xFFFFEFFFFFC2FULL) {
308 secp256k1_fe_verify(r);
313 /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
314 static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a) {
317 VERIFY_CHECK(a->normalized);
318 secp256k1_fe_verify(a);
320 for (i=0; i<32; i++) {
323 for (j=0; j<2; j++) {
324 int limb = (8*i+4*j)/52;
325 int shift = (8*i+4*j)%52;
326 c |= ((a->n[limb] >> shift) & 0xF) << (4 * j);
332 SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m) {
334 VERIFY_CHECK(a->magnitude <= m);
335 secp256k1_fe_verify(a);
337 r->n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) - a->n[0];
338 r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[1];
339 r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[2];
340 r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[3];
341 r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->n[4];
343 r->magnitude = m + 1;
345 secp256k1_fe_verify(r);
349 SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe_t *r, int a) {
358 secp256k1_fe_verify(r);
362 SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
364 secp256k1_fe_verify(a);
372 r->magnitude += a->magnitude;
374 secp256k1_fe_verify(r);
378 static void secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t * SECP256K1_RESTRICT b) {
380 VERIFY_CHECK(a->magnitude <= 8);
381 VERIFY_CHECK(b->magnitude <= 8);
382 secp256k1_fe_verify(a);
383 secp256k1_fe_verify(b);
384 VERIFY_CHECK(r != b);
386 secp256k1_fe_mul_inner(r->n, a->n, b->n);
390 secp256k1_fe_verify(r);
394 static void secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
396 VERIFY_CHECK(a->magnitude <= 8);
397 secp256k1_fe_verify(a);
399 secp256k1_fe_sqr_inner(r->n, a->n);
403 secp256k1_fe_verify(r);
407 static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag) {
408 uint64_t mask0, mask1;
409 mask0 = flag + ~((uint64_t)0);
411 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
412 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
413 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
414 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
415 r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
417 if (a->magnitude > r->magnitude) {
418 r->magnitude = a->magnitude;
420 r->normalized &= a->normalized;
424 static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage_t *r, const secp256k1_fe_storage_t *a, int flag) {
425 uint64_t mask0, mask1;
426 mask0 = flag + ~((uint64_t)0);
428 r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
429 r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
430 r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
431 r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
434 static void secp256k1_fe_to_storage(secp256k1_fe_storage_t *r, const secp256k1_fe_t *a) {
436 VERIFY_CHECK(a->normalized);
438 r->n[0] = a->n[0] | a->n[1] << 52;
439 r->n[1] = a->n[1] >> 12 | a->n[2] << 40;
440 r->n[2] = a->n[2] >> 24 | a->n[3] << 28;
441 r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
444 static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe_t *r, const secp256k1_fe_storage_t *a) {
445 r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
446 r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
447 r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
448 r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
449 r->n[4] = a->n[3] >> 16;