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_IMPL_H
8 #define SECP256K1_FIELD_IMPL_H
10 #if defined HAVE_CONFIG_H
11 #include "libsecp256k1-config.h"
17 #if defined(USE_FIELD_10X26)
18 #include "field_10x26_impl.h"
19 #elif defined(USE_FIELD_5X52)
20 #include "field_5x52_impl.h"
22 #error "Please select field implementation"
25 SECP256K1_INLINE static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b) {
27 secp256k1_fe_negate(&na, a, 1);
28 secp256k1_fe_add(&na, b);
29 return secp256k1_fe_normalizes_to_zero(&na);
32 SECP256K1_INLINE static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b) {
34 secp256k1_fe_negate(&na, a, 1);
35 secp256k1_fe_add(&na, b);
36 return secp256k1_fe_normalizes_to_zero_var(&na);
39 static int secp256k1_fe_sqrt(secp256k1_fe *r, const secp256k1_fe *a) {
40 /** Given that p is congruent to 3 mod 4, we can compute the square root of
41 * a mod p as the (p+1)/4'th power of a.
43 * As (p+1)/4 is an even number, it will have the same result for a and for
44 * (-a). Only one of these two numbers actually has a square root however,
45 * so we test at the end by squaring and comparing to the input.
46 * Also because (p+1)/4 is an even number, the computed square root is
47 * itself always a square (a ** ((p+1)/4) is the square of a ** ((p+1)/8)).
49 secp256k1_fe x2, x3, x6, x9, x11, x22, x44, x88, x176, x220, x223, t1;
54 /** The binary representation of (p + 1)/4 has 3 blocks of 1s, with lengths in
55 * { 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
56 * 1, [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
59 secp256k1_fe_sqr(&x2, a);
60 secp256k1_fe_mul(&x2, &x2, a);
62 secp256k1_fe_sqr(&x3, &x2);
63 secp256k1_fe_mul(&x3, &x3, a);
67 secp256k1_fe_sqr(&x6, &x6);
69 secp256k1_fe_mul(&x6, &x6, &x3);
73 secp256k1_fe_sqr(&x9, &x9);
75 secp256k1_fe_mul(&x9, &x9, &x3);
79 secp256k1_fe_sqr(&x11, &x11);
81 secp256k1_fe_mul(&x11, &x11, &x2);
84 for (j=0; j<11; j++) {
85 secp256k1_fe_sqr(&x22, &x22);
87 secp256k1_fe_mul(&x22, &x22, &x11);
90 for (j=0; j<22; j++) {
91 secp256k1_fe_sqr(&x44, &x44);
93 secp256k1_fe_mul(&x44, &x44, &x22);
96 for (j=0; j<44; j++) {
97 secp256k1_fe_sqr(&x88, &x88);
99 secp256k1_fe_mul(&x88, &x88, &x44);
102 for (j=0; j<88; j++) {
103 secp256k1_fe_sqr(&x176, &x176);
105 secp256k1_fe_mul(&x176, &x176, &x88);
108 for (j=0; j<44; j++) {
109 secp256k1_fe_sqr(&x220, &x220);
111 secp256k1_fe_mul(&x220, &x220, &x44);
114 for (j=0; j<3; j++) {
115 secp256k1_fe_sqr(&x223, &x223);
117 secp256k1_fe_mul(&x223, &x223, &x3);
119 /* The final result is then assembled using a sliding window over the blocks. */
122 for (j=0; j<23; j++) {
123 secp256k1_fe_sqr(&t1, &t1);
125 secp256k1_fe_mul(&t1, &t1, &x22);
126 for (j=0; j<6; j++) {
127 secp256k1_fe_sqr(&t1, &t1);
129 secp256k1_fe_mul(&t1, &t1, &x2);
130 secp256k1_fe_sqr(&t1, &t1);
131 secp256k1_fe_sqr(r, &t1);
133 /* Check that a square root was actually calculated */
135 secp256k1_fe_sqr(&t1, r);
136 return secp256k1_fe_equal(&t1, a);
139 static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a) {
140 secp256k1_fe x2, x3, x6, x9, x11, x22, x44, x88, x176, x220, x223, t1;
143 /** The binary representation of (p - 2) has 5 blocks of 1s, with lengths in
144 * { 1, 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
145 * [1], [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
148 secp256k1_fe_sqr(&x2, a);
149 secp256k1_fe_mul(&x2, &x2, a);
151 secp256k1_fe_sqr(&x3, &x2);
152 secp256k1_fe_mul(&x3, &x3, a);
155 for (j=0; j<3; j++) {
156 secp256k1_fe_sqr(&x6, &x6);
158 secp256k1_fe_mul(&x6, &x6, &x3);
161 for (j=0; j<3; j++) {
162 secp256k1_fe_sqr(&x9, &x9);
164 secp256k1_fe_mul(&x9, &x9, &x3);
167 for (j=0; j<2; j++) {
168 secp256k1_fe_sqr(&x11, &x11);
170 secp256k1_fe_mul(&x11, &x11, &x2);
173 for (j=0; j<11; j++) {
174 secp256k1_fe_sqr(&x22, &x22);
176 secp256k1_fe_mul(&x22, &x22, &x11);
179 for (j=0; j<22; j++) {
180 secp256k1_fe_sqr(&x44, &x44);
182 secp256k1_fe_mul(&x44, &x44, &x22);
185 for (j=0; j<44; j++) {
186 secp256k1_fe_sqr(&x88, &x88);
188 secp256k1_fe_mul(&x88, &x88, &x44);
191 for (j=0; j<88; j++) {
192 secp256k1_fe_sqr(&x176, &x176);
194 secp256k1_fe_mul(&x176, &x176, &x88);
197 for (j=0; j<44; j++) {
198 secp256k1_fe_sqr(&x220, &x220);
200 secp256k1_fe_mul(&x220, &x220, &x44);
203 for (j=0; j<3; j++) {
204 secp256k1_fe_sqr(&x223, &x223);
206 secp256k1_fe_mul(&x223, &x223, &x3);
208 /* The final result is then assembled using a sliding window over the blocks. */
211 for (j=0; j<23; j++) {
212 secp256k1_fe_sqr(&t1, &t1);
214 secp256k1_fe_mul(&t1, &t1, &x22);
215 for (j=0; j<5; j++) {
216 secp256k1_fe_sqr(&t1, &t1);
218 secp256k1_fe_mul(&t1, &t1, a);
219 for (j=0; j<3; j++) {
220 secp256k1_fe_sqr(&t1, &t1);
222 secp256k1_fe_mul(&t1, &t1, &x2);
223 for (j=0; j<2; j++) {
224 secp256k1_fe_sqr(&t1, &t1);
226 secp256k1_fe_mul(r, a, &t1);
229 static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a) {
230 #if defined(USE_FIELD_INV_BUILTIN)
231 secp256k1_fe_inv(r, a);
232 #elif defined(USE_FIELD_INV_NUM)
234 static const secp256k1_fe negone = SECP256K1_FE_CONST(
235 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
236 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL, 0xFFFFFC2EUL
238 /* secp256k1 field prime, value p defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
239 static const unsigned char prime[32] = {
240 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
241 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
242 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
243 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F
248 secp256k1_fe_normalize_var(&c);
249 secp256k1_fe_get_b32(b, &c);
250 secp256k1_num_set_bin(&n, b, 32);
251 secp256k1_num_set_bin(&m, prime, 32);
252 secp256k1_num_mod_inverse(&n, &n, &m);
253 secp256k1_num_get_bin(b, 32, &n);
254 res = secp256k1_fe_set_b32(r, b);
257 /* Verify the result is the (unique) valid inverse using non-GMP code. */
258 secp256k1_fe_mul(&c, &c, r);
259 secp256k1_fe_add(&c, &negone);
260 CHECK(secp256k1_fe_normalizes_to_zero_var(&c));
262 #error "Please select field inverse implementation"
266 static void secp256k1_fe_inv_all_var(secp256k1_fe *r, const secp256k1_fe *a, size_t len) {
273 VERIFY_CHECK((r + len <= a) || (a + len <= r));
279 secp256k1_fe_mul(&r[i], &r[i - 1], &a[i]);
282 secp256k1_fe_inv_var(&u, &r[--i]);
286 secp256k1_fe_mul(&r[j], &r[i], &u);
287 secp256k1_fe_mul(&u, &u, &a[j]);
293 static int secp256k1_fe_is_quad_var(const secp256k1_fe *a) {
298 /* secp256k1 field prime, value p defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
299 static const unsigned char prime[32] = {
300 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
301 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
302 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
303 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F
307 secp256k1_fe_normalize_var(&c);
308 secp256k1_fe_get_b32(b, &c);
309 secp256k1_num_set_bin(&n, b, 32);
310 secp256k1_num_set_bin(&m, prime, 32);
311 return secp256k1_num_jacobi(&n, &m) >= 0;
314 return secp256k1_fe_sqrt(&r, a);
318 #endif /* SECP256K1_FIELD_IMPL_H */