7 #ifndef SECP256K1_SCALAR_IMPL_H 8 #define SECP256K1_SCALAR_IMPL_H 13 #if defined HAVE_CONFIG_H 17 #if defined(EXHAUSTIVE_TEST_ORDER) 19 #elif defined(USE_SCALAR_4X64) 21 #elif defined(USE_SCALAR_8X32) 24 #error "Please select scalar implementation" 30 secp256k1_scalar_get_b32(c, a);
31 secp256k1_num_set_bin(r, c, 32);
35 static void secp256k1_scalar_order_get_num(
secp256k1_num *r) {
36 #if defined(EXHAUSTIVE_TEST_ORDER) 37 static const unsigned char order[32] = {
44 static const unsigned char order[32] = {
45 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
46 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
47 0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
48 0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
51 secp256k1_num_set_bin(r, order, 32);
56 #if defined(EXHAUSTIVE_TEST_ORDER) 60 if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1)
74 secp256k1_scalar_sqr(&u2, x);
75 secp256k1_scalar_mul(&x2, &u2, x);
76 secp256k1_scalar_mul(&u5, &u2, &x2);
77 secp256k1_scalar_mul(&x3, &u5, &u2);
78 secp256k1_scalar_mul(&u9, &x3, &u2);
79 secp256k1_scalar_mul(&u11, &u9, &u2);
80 secp256k1_scalar_mul(&u13, &u11, &u2);
82 secp256k1_scalar_sqr(&x6, &u13);
83 secp256k1_scalar_sqr(&x6, &x6);
84 secp256k1_scalar_mul(&x6, &x6, &u11);
86 secp256k1_scalar_sqr(&x8, &x6);
87 secp256k1_scalar_sqr(&x8, &x8);
88 secp256k1_scalar_mul(&x8, &x8, &x2);
90 secp256k1_scalar_sqr(&x14, &x8);
91 for (i = 0; i < 5; i++) {
92 secp256k1_scalar_sqr(&x14, &x14);
94 secp256k1_scalar_mul(&x14, &x14, &x6);
96 secp256k1_scalar_sqr(&x28, &x14);
97 for (i = 0; i < 13; i++) {
98 secp256k1_scalar_sqr(&x28, &x28);
100 secp256k1_scalar_mul(&x28, &x28, &x14);
102 secp256k1_scalar_sqr(&x56, &x28);
103 for (i = 0; i < 27; i++) {
104 secp256k1_scalar_sqr(&x56, &x56);
106 secp256k1_scalar_mul(&x56, &x56, &x28);
108 secp256k1_scalar_sqr(&x112, &x56);
109 for (i = 0; i < 55; i++) {
110 secp256k1_scalar_sqr(&x112, &x112);
112 secp256k1_scalar_mul(&x112, &x112, &x56);
114 secp256k1_scalar_sqr(&x126, &x112);
115 for (i = 0; i < 13; i++) {
116 secp256k1_scalar_sqr(&x126, &x126);
118 secp256k1_scalar_mul(&x126, &x126, &x14);
122 for (i = 0; i < 3; i++) {
123 secp256k1_scalar_sqr(t, t);
125 secp256k1_scalar_mul(t, t, &u5);
126 for (i = 0; i < 4; i++) {
127 secp256k1_scalar_sqr(t, t);
129 secp256k1_scalar_mul(t, t, &x3);
130 for (i = 0; i < 4; i++) {
131 secp256k1_scalar_sqr(t, t);
133 secp256k1_scalar_mul(t, t, &u5);
134 for (i = 0; i < 5; i++) {
135 secp256k1_scalar_sqr(t, t);
137 secp256k1_scalar_mul(t, t, &u11);
138 for (i = 0; i < 4; i++) {
139 secp256k1_scalar_sqr(t, t);
141 secp256k1_scalar_mul(t, t, &u11);
142 for (i = 0; i < 4; i++) {
143 secp256k1_scalar_sqr(t, t);
145 secp256k1_scalar_mul(t, t, &x3);
146 for (i = 0; i < 5; i++) {
147 secp256k1_scalar_sqr(t, t);
149 secp256k1_scalar_mul(t, t, &x3);
150 for (i = 0; i < 6; i++) {
151 secp256k1_scalar_sqr(t, t);
153 secp256k1_scalar_mul(t, t, &u13);
154 for (i = 0; i < 4; i++) {
155 secp256k1_scalar_sqr(t, t);
157 secp256k1_scalar_mul(t, t, &u5);
158 for (i = 0; i < 3; i++) {
159 secp256k1_scalar_sqr(t, t);
161 secp256k1_scalar_mul(t, t, &x3);
162 for (i = 0; i < 5; i++) {
163 secp256k1_scalar_sqr(t, t);
165 secp256k1_scalar_mul(t, t, &u9);
166 for (i = 0; i < 6; i++) {
167 secp256k1_scalar_sqr(t, t);
169 secp256k1_scalar_mul(t, t, &u5);
170 for (i = 0; i < 10; i++) {
171 secp256k1_scalar_sqr(t, t);
173 secp256k1_scalar_mul(t, t, &x3);
174 for (i = 0; i < 4; i++) {
175 secp256k1_scalar_sqr(t, t);
177 secp256k1_scalar_mul(t, t, &x3);
178 for (i = 0; i < 9; i++) {
179 secp256k1_scalar_sqr(t, t);
181 secp256k1_scalar_mul(t, t, &x8);
182 for (i = 0; i < 5; i++) {
183 secp256k1_scalar_sqr(t, t);
185 secp256k1_scalar_mul(t, t, &u9);
186 for (i = 0; i < 6; i++) {
187 secp256k1_scalar_sqr(t, t);
189 secp256k1_scalar_mul(t, t, &u11);
190 for (i = 0; i < 4; i++) {
191 secp256k1_scalar_sqr(t, t);
193 secp256k1_scalar_mul(t, t, &u13);
194 for (i = 0; i < 5; i++) {
195 secp256k1_scalar_sqr(t, t);
197 secp256k1_scalar_mul(t, t, &x2);
198 for (i = 0; i < 6; i++) {
199 secp256k1_scalar_sqr(t, t);
201 secp256k1_scalar_mul(t, t, &u13);
202 for (i = 0; i < 10; i++) {
203 secp256k1_scalar_sqr(t, t);
205 secp256k1_scalar_mul(t, t, &u13);
206 for (i = 0; i < 4; i++) {
207 secp256k1_scalar_sqr(t, t);
209 secp256k1_scalar_mul(t, t, &u9);
210 for (i = 0; i < 6; i++) {
211 secp256k1_scalar_sqr(t, t);
213 secp256k1_scalar_mul(t, t, x);
214 for (i = 0; i < 8; i++) {
215 secp256k1_scalar_sqr(t, t);
217 secp256k1_scalar_mul(r, t, &x6);
221 return !(a->
d[0] & 1);
226 #if defined(USE_SCALAR_INV_BUILTIN) 227 secp256k1_scalar_inverse(r, x);
228 #elif defined(USE_SCALAR_INV_NUM) 232 secp256k1_scalar_get_b32(b, &t);
233 secp256k1_num_set_bin(&n, b, 32);
234 secp256k1_scalar_order_get_num(&m);
235 secp256k1_num_mod_inverse(&n, &n, &m);
236 secp256k1_num_get_bin(b, 32, &n);
237 secp256k1_scalar_set_b32(r, b, NULL);
239 secp256k1_scalar_mul(&t, &t, r);
240 CHECK(secp256k1_scalar_is_one(&t));
242 #error "Please select scalar inverse implementation" 246 #ifdef USE_ENDOMORPHISM 247 #if defined(EXHAUSTIVE_TEST_ORDER) 255 *r2 = (*a + 5) % EXHAUSTIVE_TEST_ORDER;
300 0xAC9C52B3UL, 0x3FA3CF1FUL, 0x5AD9E3FDUL, 0x77ED9BA4UL,
301 0xA880B9FCUL, 0x8EC739C2UL, 0xE0CFC810UL, 0xB51283CFUL
304 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL,
305 0xE4437ED6UL, 0x010E8828UL, 0x6F547FA9UL, 0x0ABFE4C3UL
308 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
309 0x8A280AC5UL, 0x0774346DUL, 0xD765CDA8UL, 0x3DB1562CUL
312 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00003086UL,
313 0xD221A7D4UL, 0x6BCDE86CUL, 0x90E49284UL, 0xEB153DABUL
316 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x0000E443UL,
317 0x7ED6010EUL, 0x88286F54UL, 0x7FA90ABFUL, 0xE4C42212UL
322 secp256k1_scalar_mul_shift_var(&c1, a, &g1, 272);
323 secp256k1_scalar_mul_shift_var(&c2, a, &g2, 272);
324 secp256k1_scalar_mul(&c1, &c1, &minus_b1);
325 secp256k1_scalar_mul(&c2, &c2, &minus_b2);
326 secp256k1_scalar_add(r2, &c1, &c2);
327 secp256k1_scalar_mul(r1, r2, &minus_lambda);
328 secp256k1_scalar_add(r1, r1, a);
#define VERIFY_CHECK(cond)
#define EXHAUSTIVE_TEST_LAMBDA
#define SECP256K1_SCALAR_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
A scalar modulo the group order of the secp256k1 curve.
#define EXHAUSTIVE_TEST_ORDER