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"
16 #if defined(USE_FIELD_10X26)
17 #include "field_10x26_impl.h"
18 #elif defined(USE_FIELD_5X52)
19 #include "field_5x52_impl.h"
21 #error "Please select field implementation"
24 SECP256K1_INLINE
static int secp256k1_fe_equal(const secp256k1_fe
*a
, const secp256k1_fe
*b
) {
26 secp256k1_fe_negate(&na
, a
, 1);
27 secp256k1_fe_add(&na
, b
);
28 return secp256k1_fe_normalizes_to_zero(&na
);
31 SECP256K1_INLINE
static int secp256k1_fe_equal_var(const secp256k1_fe
*a
, const secp256k1_fe
*b
) {
33 secp256k1_fe_negate(&na
, a
, 1);
34 secp256k1_fe_add(&na
, b
);
35 return secp256k1_fe_normalizes_to_zero_var(&na
);
38 static int secp256k1_fe_sqrt(secp256k1_fe
*r
, const secp256k1_fe
*a
) {
39 /** Given that p is congruent to 3 mod 4, we can compute the square root of
40 * a mod p as the (p+1)/4'th power of a.
42 * As (p+1)/4 is an even number, it will have the same result for a and for
43 * (-a). Only one of these two numbers actually has a square root however,
44 * so we test at the end by squaring and comparing to the input.
45 * Also because (p+1)/4 is an even number, the computed square root is
46 * itself always a square (a ** ((p+1)/4) is the square of a ** ((p+1)/8)).
48 secp256k1_fe x2
, x3
, x6
, x9
, x11
, x22
, x44
, x88
, x176
, x220
, x223
, t1
;
51 /** The binary representation of (p + 1)/4 has 3 blocks of 1s, with lengths in
52 * { 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
53 * 1, [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
56 secp256k1_fe_sqr(&x2
, a
);
57 secp256k1_fe_mul(&x2
, &x2
, a
);
59 secp256k1_fe_sqr(&x3
, &x2
);
60 secp256k1_fe_mul(&x3
, &x3
, a
);
64 secp256k1_fe_sqr(&x6
, &x6
);
66 secp256k1_fe_mul(&x6
, &x6
, &x3
);
70 secp256k1_fe_sqr(&x9
, &x9
);
72 secp256k1_fe_mul(&x9
, &x9
, &x3
);
76 secp256k1_fe_sqr(&x11
, &x11
);
78 secp256k1_fe_mul(&x11
, &x11
, &x2
);
81 for (j
=0; j
<11; j
++) {
82 secp256k1_fe_sqr(&x22
, &x22
);
84 secp256k1_fe_mul(&x22
, &x22
, &x11
);
87 for (j
=0; j
<22; j
++) {
88 secp256k1_fe_sqr(&x44
, &x44
);
90 secp256k1_fe_mul(&x44
, &x44
, &x22
);
93 for (j
=0; j
<44; j
++) {
94 secp256k1_fe_sqr(&x88
, &x88
);
96 secp256k1_fe_mul(&x88
, &x88
, &x44
);
99 for (j
=0; j
<88; j
++) {
100 secp256k1_fe_sqr(&x176
, &x176
);
102 secp256k1_fe_mul(&x176
, &x176
, &x88
);
105 for (j
=0; j
<44; j
++) {
106 secp256k1_fe_sqr(&x220
, &x220
);
108 secp256k1_fe_mul(&x220
, &x220
, &x44
);
111 for (j
=0; j
<3; j
++) {
112 secp256k1_fe_sqr(&x223
, &x223
);
114 secp256k1_fe_mul(&x223
, &x223
, &x3
);
116 /* The final result is then assembled using a sliding window over the blocks. */
119 for (j
=0; j
<23; j
++) {
120 secp256k1_fe_sqr(&t1
, &t1
);
122 secp256k1_fe_mul(&t1
, &t1
, &x22
);
123 for (j
=0; j
<6; j
++) {
124 secp256k1_fe_sqr(&t1
, &t1
);
126 secp256k1_fe_mul(&t1
, &t1
, &x2
);
127 secp256k1_fe_sqr(&t1
, &t1
);
128 secp256k1_fe_sqr(r
, &t1
);
130 /* Check that a square root was actually calculated */
132 secp256k1_fe_sqr(&t1
, r
);
133 return secp256k1_fe_equal(&t1
, a
);
136 static void secp256k1_fe_inv(secp256k1_fe
*r
, const secp256k1_fe
*a
) {
137 secp256k1_fe x2
, x3
, x6
, x9
, x11
, x22
, x44
, x88
, x176
, x220
, x223
, t1
;
140 /** The binary representation of (p - 2) has 5 blocks of 1s, with lengths in
141 * { 1, 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
142 * [1], [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
145 secp256k1_fe_sqr(&x2
, a
);
146 secp256k1_fe_mul(&x2
, &x2
, a
);
148 secp256k1_fe_sqr(&x3
, &x2
);
149 secp256k1_fe_mul(&x3
, &x3
, a
);
152 for (j
=0; j
<3; j
++) {
153 secp256k1_fe_sqr(&x6
, &x6
);
155 secp256k1_fe_mul(&x6
, &x6
, &x3
);
158 for (j
=0; j
<3; j
++) {
159 secp256k1_fe_sqr(&x9
, &x9
);
161 secp256k1_fe_mul(&x9
, &x9
, &x3
);
164 for (j
=0; j
<2; j
++) {
165 secp256k1_fe_sqr(&x11
, &x11
);
167 secp256k1_fe_mul(&x11
, &x11
, &x2
);
170 for (j
=0; j
<11; j
++) {
171 secp256k1_fe_sqr(&x22
, &x22
);
173 secp256k1_fe_mul(&x22
, &x22
, &x11
);
176 for (j
=0; j
<22; j
++) {
177 secp256k1_fe_sqr(&x44
, &x44
);
179 secp256k1_fe_mul(&x44
, &x44
, &x22
);
182 for (j
=0; j
<44; j
++) {
183 secp256k1_fe_sqr(&x88
, &x88
);
185 secp256k1_fe_mul(&x88
, &x88
, &x44
);
188 for (j
=0; j
<88; j
++) {
189 secp256k1_fe_sqr(&x176
, &x176
);
191 secp256k1_fe_mul(&x176
, &x176
, &x88
);
194 for (j
=0; j
<44; j
++) {
195 secp256k1_fe_sqr(&x220
, &x220
);
197 secp256k1_fe_mul(&x220
, &x220
, &x44
);
200 for (j
=0; j
<3; j
++) {
201 secp256k1_fe_sqr(&x223
, &x223
);
203 secp256k1_fe_mul(&x223
, &x223
, &x3
);
205 /* The final result is then assembled using a sliding window over the blocks. */
208 for (j
=0; j
<23; j
++) {
209 secp256k1_fe_sqr(&t1
, &t1
);
211 secp256k1_fe_mul(&t1
, &t1
, &x22
);
212 for (j
=0; j
<5; j
++) {
213 secp256k1_fe_sqr(&t1
, &t1
);
215 secp256k1_fe_mul(&t1
, &t1
, a
);
216 for (j
=0; j
<3; j
++) {
217 secp256k1_fe_sqr(&t1
, &t1
);
219 secp256k1_fe_mul(&t1
, &t1
, &x2
);
220 for (j
=0; j
<2; j
++) {
221 secp256k1_fe_sqr(&t1
, &t1
);
223 secp256k1_fe_mul(r
, a
, &t1
);
226 static void secp256k1_fe_inv_var(secp256k1_fe
*r
, const secp256k1_fe
*a
) {
227 #if defined(USE_FIELD_INV_BUILTIN)
228 secp256k1_fe_inv(r
, a
);
229 #elif defined(USE_FIELD_INV_NUM)
231 static const secp256k1_fe negone
= SECP256K1_FE_CONST(
232 0xFFFFFFFFUL
, 0xFFFFFFFFUL
, 0xFFFFFFFFUL
, 0xFFFFFFFFUL
,
233 0xFFFFFFFFUL
, 0xFFFFFFFFUL
, 0xFFFFFFFEUL
, 0xFFFFFC2EUL
235 /* secp256k1 field prime, value p defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
236 static const unsigned char prime
[32] = {
237 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
238 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
239 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
240 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F
245 secp256k1_fe_normalize_var(&c
);
246 secp256k1_fe_get_b32(b
, &c
);
247 secp256k1_num_set_bin(&n
, b
, 32);
248 secp256k1_num_set_bin(&m
, prime
, 32);
249 secp256k1_num_mod_inverse(&n
, &n
, &m
);
250 secp256k1_num_get_bin(b
, 32, &n
);
251 res
= secp256k1_fe_set_b32(r
, b
);
254 /* Verify the result is the (unique) valid inverse using non-GMP code. */
255 secp256k1_fe_mul(&c
, &c
, r
);
256 secp256k1_fe_add(&c
, &negone
);
257 CHECK(secp256k1_fe_normalizes_to_zero_var(&c
));
259 #error "Please select field inverse implementation"
263 static void secp256k1_fe_inv_all_var(secp256k1_fe
*r
, const secp256k1_fe
*a
, size_t len
) {
270 VERIFY_CHECK((r
+ len
<= a
) || (a
+ len
<= r
));
276 secp256k1_fe_mul(&r
[i
], &r
[i
- 1], &a
[i
]);
279 secp256k1_fe_inv_var(&u
, &r
[--i
]);
283 secp256k1_fe_mul(&r
[j
], &r
[i
], &u
);
284 secp256k1_fe_mul(&u
, &u
, &a
[j
]);
290 static int secp256k1_fe_is_quad_var(const secp256k1_fe
*a
) {
295 /* secp256k1 field prime, value p defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
296 static const unsigned char prime
[32] = {
297 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
298 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
299 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
300 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F
304 secp256k1_fe_normalize_var(&c
);
305 secp256k1_fe_get_b32(b
, &c
);
306 secp256k1_num_set_bin(&n
, b
, 32);
307 secp256k1_num_set_bin(&m
, prime
, 32);
308 return secp256k1_num_jacobi(&n
, &m
) >= 0;
311 return secp256k1_fe_sqrt(&r
, a
);
315 #endif /* SECP256K1_FIELD_IMPL_H */