]> Git Repo - secp256k1.git/blame - src/scalar_impl.h
Merge #408: Add `secp256k1_ec_pubkey_negate` and `secp256k1_ec_privkey_negate`
[secp256k1.git] / src / scalar_impl.h
CommitLineData
71712b27
GM
1/**********************************************************************
2 * Copyright (c) 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 **********************************************************************/
a9f5c8b8
PW
6
7#ifndef _SECP256K1_SCALAR_IMPL_H_
8#define _SECP256K1_SCALAR_IMPL_H_
9
d1502eb4 10#include "group.h"
a9f5c8b8
PW
11#include "scalar.h"
12
1d52a8b1
PW
13#if defined HAVE_CONFIG_H
14#include "libsecp256k1-config.h"
15#endif
79359302 16
83836a95
AP
17#if defined(EXHAUSTIVE_TEST_ORDER)
18#include "scalar_low_impl.h"
19#elif defined(USE_SCALAR_4X64)
1d52a8b1
PW
20#include "scalar_4x64_impl.h"
21#elif defined(USE_SCALAR_8X32)
22#include "scalar_8x32_impl.h"
23#else
24#error "Please select scalar implementation"
25#endif
a9f5c8b8 26
597128d3 27#ifndef USE_NUM_NONE
dd891e0e 28static void secp256k1_scalar_get_num(secp256k1_num *r, const secp256k1_scalar *a) {
a9f5c8b8 29 unsigned char c[32];
1d52a8b1 30 secp256k1_scalar_get_b32(c, a);
a9f5c8b8
PW
31 secp256k1_num_set_bin(r, c, 32);
32}
33
6efd6e77 34/** secp256k1 curve order, see secp256k1_ecdsa_const_order_as_fe in ecdsa_impl.h */
dd891e0e 35static void secp256k1_scalar_order_get_num(secp256k1_num *r) {
83836a95
AP
36#if defined(EXHAUSTIVE_TEST_ORDER)
37 static const unsigned char order[32] = {
38 0,0,0,0,0,0,0,0,
39 0,0,0,0,0,0,0,0,
40 0,0,0,0,0,0,0,0,
41 0,0,0,0,0,0,0,EXHAUSTIVE_TEST_ORDER
42 };
43#else
f1ebfe39
PW
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
49 };
83836a95 50#endif
f1ebfe39 51 secp256k1_num_set_bin(r, order, 32);
659b554d 52}
597128d3 53#endif
1d52a8b1 54
dd891e0e 55static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x) {
83836a95
AP
56#if defined(EXHAUSTIVE_TEST_ORDER)
57 int i;
58 *r = 0;
59 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++)
60 if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1)
61 *r = i;
62 /* If this VERIFY_CHECK triggers we were given a noninvertible scalar (and thus
63 * have a composite group order; fix it in exhaustive_tests.c). */
64 VERIFY_CHECK(*r != 0);
65}
66#else
dd891e0e 67 secp256k1_scalar *t;
d9543c90 68 int i;
71712b27 69 /* First compute x ^ (2^N - 1) for some values of N. */
dd891e0e 70 secp256k1_scalar x2, x3, x4, x6, x7, x8, x15, x30, x60, x120, x127;
1d52a8b1
PW
71
72 secp256k1_scalar_sqr(&x2, x);
73 secp256k1_scalar_mul(&x2, &x2, x);
74
75 secp256k1_scalar_sqr(&x3, &x2);
76 secp256k1_scalar_mul(&x3, &x3, x);
77
78 secp256k1_scalar_sqr(&x4, &x3);
79 secp256k1_scalar_mul(&x4, &x4, x);
80
81 secp256k1_scalar_sqr(&x6, &x4);
82 secp256k1_scalar_sqr(&x6, &x6);
83 secp256k1_scalar_mul(&x6, &x6, &x2);
84
85 secp256k1_scalar_sqr(&x7, &x6);
86 secp256k1_scalar_mul(&x7, &x7, x);
87
88 secp256k1_scalar_sqr(&x8, &x7);
89 secp256k1_scalar_mul(&x8, &x8, x);
90
91 secp256k1_scalar_sqr(&x15, &x8);
26320197 92 for (i = 0; i < 6; i++) {
1d52a8b1 93 secp256k1_scalar_sqr(&x15, &x15);
26320197 94 }
1d52a8b1
PW
95 secp256k1_scalar_mul(&x15, &x15, &x7);
96
97 secp256k1_scalar_sqr(&x30, &x15);
26320197 98 for (i = 0; i < 14; i++) {
1d52a8b1 99 secp256k1_scalar_sqr(&x30, &x30);
26320197 100 }
1d52a8b1
PW
101 secp256k1_scalar_mul(&x30, &x30, &x15);
102
103 secp256k1_scalar_sqr(&x60, &x30);
26320197 104 for (i = 0; i < 29; i++) {
1d52a8b1 105 secp256k1_scalar_sqr(&x60, &x60);
26320197 106 }
1d52a8b1
PW
107 secp256k1_scalar_mul(&x60, &x60, &x30);
108
109 secp256k1_scalar_sqr(&x120, &x60);
26320197 110 for (i = 0; i < 59; i++) {
1d52a8b1 111 secp256k1_scalar_sqr(&x120, &x120);
26320197 112 }
1d52a8b1
PW
113 secp256k1_scalar_mul(&x120, &x120, &x60);
114
115 secp256k1_scalar_sqr(&x127, &x120);
26320197 116 for (i = 0; i < 6; i++) {
1d52a8b1 117 secp256k1_scalar_sqr(&x127, &x127);
26320197 118 }
1d52a8b1
PW
119 secp256k1_scalar_mul(&x127, &x127, &x7);
120
71712b27 121 /* Then accumulate the final result (t starts at x127). */
d9543c90 122 t = &x127;
26320197 123 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 124 secp256k1_scalar_sqr(t, t);
26320197 125 }
71712b27 126 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 127 for (i = 0; i < 4; i++) { /* 0 */
1d52a8b1 128 secp256k1_scalar_sqr(t, t);
26320197 129 }
71712b27 130 secp256k1_scalar_mul(t, t, &x3); /* 111 */
26320197 131 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 132 secp256k1_scalar_sqr(t, t);
26320197 133 }
71712b27 134 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 135 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 136 secp256k1_scalar_sqr(t, t);
26320197 137 }
71712b27 138 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 139 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 140 secp256k1_scalar_sqr(t, t);
26320197 141 }
71712b27 142 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 143 for (i = 0; i < 4; i++) { /* 0 */
1d52a8b1 144 secp256k1_scalar_sqr(t, t);
26320197 145 }
71712b27 146 secp256k1_scalar_mul(t, t, &x3); /* 111 */
26320197 147 for (i = 0; i < 3; i++) { /* 0 */
1d52a8b1 148 secp256k1_scalar_sqr(t, t);
26320197 149 }
71712b27 150 secp256k1_scalar_mul(t, t, &x2); /* 11 */
26320197 151 for (i = 0; i < 4; i++) { /* 0 */
1d52a8b1 152 secp256k1_scalar_sqr(t, t);
26320197 153 }
71712b27 154 secp256k1_scalar_mul(t, t, &x3); /* 111 */
26320197 155 for (i = 0; i < 5; i++) { /* 00 */
1d52a8b1 156 secp256k1_scalar_sqr(t, t);
26320197 157 }
71712b27 158 secp256k1_scalar_mul(t, t, &x3); /* 111 */
26320197 159 for (i = 0; i < 4; i++) { /* 00 */
1d52a8b1 160 secp256k1_scalar_sqr(t, t);
26320197 161 }
71712b27 162 secp256k1_scalar_mul(t, t, &x2); /* 11 */
26320197 163 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 164 secp256k1_scalar_sqr(t, t);
26320197 165 }
71712b27 166 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 167 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 168 secp256k1_scalar_sqr(t, t);
26320197 169 }
71712b27 170 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 171 for (i = 0; i < 5; i++) { /* 0 */
1d52a8b1 172 secp256k1_scalar_sqr(t, t);
26320197 173 }
71712b27 174 secp256k1_scalar_mul(t, t, &x4); /* 1111 */
26320197 175 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 176 secp256k1_scalar_sqr(t, t);
26320197 177 }
71712b27 178 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 179 for (i = 0; i < 3; i++) { /* 00 */
1d52a8b1 180 secp256k1_scalar_sqr(t, t);
26320197 181 }
71712b27 182 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 183 for (i = 0; i < 4; i++) { /* 000 */
1d52a8b1 184 secp256k1_scalar_sqr(t, t);
26320197 185 }
71712b27 186 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 187 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 188 secp256k1_scalar_sqr(t, t);
26320197 189 }
71712b27 190 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 191 for (i = 0; i < 10; i++) { /* 0000000 */
1d52a8b1 192 secp256k1_scalar_sqr(t, t);
26320197 193 }
71712b27 194 secp256k1_scalar_mul(t, t, &x3); /* 111 */
26320197 195 for (i = 0; i < 4; i++) { /* 0 */
1d52a8b1 196 secp256k1_scalar_sqr(t, t);
26320197 197 }
71712b27 198 secp256k1_scalar_mul(t, t, &x3); /* 111 */
26320197 199 for (i = 0; i < 9; i++) { /* 0 */
1d52a8b1 200 secp256k1_scalar_sqr(t, t);
26320197 201 }
71712b27 202 secp256k1_scalar_mul(t, t, &x8); /* 11111111 */
26320197 203 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 204 secp256k1_scalar_sqr(t, t);
26320197 205 }
71712b27 206 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 207 for (i = 0; i < 3; i++) { /* 00 */
1d52a8b1 208 secp256k1_scalar_sqr(t, t);
26320197 209 }
71712b27 210 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 211 for (i = 0; i < 3; i++) { /* 00 */
1d52a8b1 212 secp256k1_scalar_sqr(t, t);
26320197 213 }
71712b27 214 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 215 for (i = 0; i < 5; i++) { /* 0 */
1d52a8b1 216 secp256k1_scalar_sqr(t, t);
26320197 217 }
71712b27 218 secp256k1_scalar_mul(t, t, &x4); /* 1111 */
26320197 219 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 220 secp256k1_scalar_sqr(t, t);
26320197 221 }
71712b27 222 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 223 for (i = 0; i < 5; i++) { /* 000 */
1d52a8b1 224 secp256k1_scalar_sqr(t, t);
26320197 225 }
71712b27 226 secp256k1_scalar_mul(t, t, &x2); /* 11 */
26320197 227 for (i = 0; i < 4; i++) { /* 00 */
1d52a8b1 228 secp256k1_scalar_sqr(t, t);
26320197 229 }
71712b27 230 secp256k1_scalar_mul(t, t, &x2); /* 11 */
26320197 231 for (i = 0; i < 2; i++) { /* 0 */
1d52a8b1 232 secp256k1_scalar_sqr(t, t);
26320197 233 }
71712b27 234 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 235 for (i = 0; i < 8; i++) { /* 000000 */
1d52a8b1 236 secp256k1_scalar_sqr(t, t);
26320197 237 }
71712b27 238 secp256k1_scalar_mul(t, t, &x2); /* 11 */
26320197 239 for (i = 0; i < 3; i++) { /* 0 */
1d52a8b1 240 secp256k1_scalar_sqr(t, t);
26320197 241 }
71712b27 242 secp256k1_scalar_mul(t, t, &x2); /* 11 */
26320197 243 for (i = 0; i < 3; i++) { /* 00 */
1d52a8b1 244 secp256k1_scalar_sqr(t, t);
26320197 245 }
71712b27 246 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 247 for (i = 0; i < 6; i++) { /* 00000 */
1d52a8b1 248 secp256k1_scalar_sqr(t, t);
26320197 249 }
71712b27 250 secp256k1_scalar_mul(t, t, x); /* 1 */
26320197 251 for (i = 0; i < 8; i++) { /* 00 */
1d52a8b1 252 secp256k1_scalar_sqr(t, t);
26320197 253 }
71712b27 254 secp256k1_scalar_mul(r, t, &x6); /* 111111 */
1d52a8b1
PW
255}
256
dd891e0e 257SECP256K1_INLINE static int secp256k1_scalar_is_even(const secp256k1_scalar *a) {
44015000
AP
258 return !(a->d[0] & 1);
259}
83836a95 260#endif
44015000 261
dd891e0e 262static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x) {
d1502eb4
PW
263#if defined(USE_SCALAR_INV_BUILTIN)
264 secp256k1_scalar_inverse(r, x);
265#elif defined(USE_SCALAR_INV_NUM)
266 unsigned char b[32];
dd891e0e
PW
267 secp256k1_num n, m;
268 secp256k1_scalar t = *x;
36b305a8 269 secp256k1_scalar_get_b32(b, &t);
d1502eb4 270 secp256k1_num_set_bin(&n, b, 32);
f1ebfe39
PW
271 secp256k1_scalar_order_get_num(&m);
272 secp256k1_num_mod_inverse(&n, &n, &m);
d1502eb4
PW
273 secp256k1_num_get_bin(b, 32, &n);
274 secp256k1_scalar_set_b32(r, b, NULL);
36b305a8
PW
275 /* Verify that the inverse was computed correctly, without GMP code. */
276 secp256k1_scalar_mul(&t, &t, r);
277 CHECK(secp256k1_scalar_is_one(&t));
d1502eb4
PW
278#else
279#error "Please select scalar inverse implementation"
280#endif
281}
282
6794be60 283#ifdef USE_ENDOMORPHISM
83836a95
AP
284#if defined(EXHAUSTIVE_TEST_ORDER)
285/**
286 * Find k1 and k2 given k, such that k1 + k2 * lambda == k mod n; unlike in the
287 * full case we don't bother making k1 and k2 be small, we just want them to be
288 * nontrivial to get full test coverage for the exhaustive tests. We therefore
289 * (arbitrarily) set k2 = k + 5 and k1 = k - k2 * lambda.
290 */
291static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a) {
292 *r2 = (*a + 5) % EXHAUSTIVE_TEST_ORDER;
293 *r1 = (*a + (EXHAUSTIVE_TEST_ORDER - *r2) * EXHAUSTIVE_TEST_LAMBDA) % EXHAUSTIVE_TEST_ORDER;
294}
295#else
f1ebfe39
PW
296/**
297 * The Secp256k1 curve has an endomorphism, where lambda * (x, y) = (beta * x, y), where
298 * lambda is {0x53,0x63,0xad,0x4c,0xc0,0x5c,0x30,0xe0,0xa5,0x26,0x1c,0x02,0x88,0x12,0x64,0x5a,
299 * 0x12,0x2e,0x22,0xea,0x20,0x81,0x66,0x78,0xdf,0x02,0x96,0x7c,0x1b,0x23,0xbd,0x72}
300 *
301 * "Guide to Elliptic Curve Cryptography" (Hankerson, Menezes, Vanstone) gives an algorithm
302 * (algorithm 3.74) to find k1 and k2 given k, such that k1 + k2 * lambda == k mod n, and k1
303 * and k2 have a small size.
304 * It relies on constants a1, b1, a2, b2. These constants for the value of lambda above are:
305 *
306 * - a1 = {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
307 * - b1 = -{0xe4,0x43,0x7e,0xd6,0x01,0x0e,0x88,0x28,0x6f,0x54,0x7f,0xa9,0x0a,0xbf,0xe4,0xc3}
308 * - a2 = {0x01,0x14,0xca,0x50,0xf7,0xa8,0xe2,0xf3,0xf6,0x57,0xc1,0x10,0x8d,0x9d,0x44,0xcf,0xd8}
309 * - b2 = {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
310 *
311 * The algorithm then computes c1 = round(b1 * k / n) and c2 = round(b2 * k / n), and gives
312 * k1 = k - (c1*a1 + c2*a2) and k2 = -(c1*b1 + c2*b2). Instead, we use modular arithmetic, and
313 * compute k1 as k - k2 * lambda, avoiding the need for constants a1 and a2.
314 *
315 * g1, g2 are precomputed constants used to replace division with a rounded multiplication
316 * when decomposing the scalar for an endomorphism-based point multiplication.
317 *
318 * The possibility of using precomputed estimates is mentioned in "Guide to Elliptic Curve
319 * Cryptography" (Hankerson, Menezes, Vanstone) in section 3.5.
320 *
321 * The derivation is described in the paper "Efficient Software Implementation of Public-Key
322 * Cryptography on Sensor Networks Using the MSP430X Microcontroller" (Gouvea, Oliveira, Lopez),
323 * Section 4.3 (here we use a somewhat higher-precision estimate):
324 * d = a1*b2 - b1*a2
325 * g1 = round((2^272)*b2/d)
326 * g2 = round((2^272)*b1/d)
327 *
328 * (Note that 'd' is also equal to the curve order here because [a1,b1] and [a2,b2] are found
329 * as outputs of the Extended Euclidean Algorithm on inputs 'order' and 'lambda').
330 *
331 * The function below splits a in r1 and r2, such that r1 + lambda * r2 == a (mod order).
332 */
333
dd891e0e
PW
334static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a) {
335 secp256k1_scalar c1, c2;
336 static const secp256k1_scalar minus_lambda = SECP256K1_SCALAR_CONST(
f1ebfe39
PW
337 0xAC9C52B3UL, 0x3FA3CF1FUL, 0x5AD9E3FDUL, 0x77ED9BA4UL,
338 0xA880B9FCUL, 0x8EC739C2UL, 0xE0CFC810UL, 0xB51283CFUL
339 );
dd891e0e 340 static const secp256k1_scalar minus_b1 = SECP256K1_SCALAR_CONST(
f1ebfe39
PW
341 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL,
342 0xE4437ED6UL, 0x010E8828UL, 0x6F547FA9UL, 0x0ABFE4C3UL
343 );
dd891e0e 344 static const secp256k1_scalar minus_b2 = SECP256K1_SCALAR_CONST(
f1ebfe39
PW
345 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
346 0x8A280AC5UL, 0x0774346DUL, 0xD765CDA8UL, 0x3DB1562CUL
347 );
dd891e0e 348 static const secp256k1_scalar g1 = SECP256K1_SCALAR_CONST(
f1ebfe39
PW
349 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00003086UL,
350 0xD221A7D4UL, 0x6BCDE86CUL, 0x90E49284UL, 0xEB153DABUL
351 );
dd891e0e 352 static const secp256k1_scalar g2 = SECP256K1_SCALAR_CONST(
f1ebfe39
PW
353 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x0000E443UL,
354 0x7ED6010EUL, 0x88286F54UL, 0x7FA90ABFUL, 0xE4C42212UL
355 );
c35ff1ea
PW
356 VERIFY_CHECK(r1 != a);
357 VERIFY_CHECK(r2 != a);
ed35d43a 358 /* these _var calls are constant time since the shift amount is constant */
f1ebfe39
PW
359 secp256k1_scalar_mul_shift_var(&c1, a, &g1, 272);
360 secp256k1_scalar_mul_shift_var(&c2, a, &g2, 272);
361 secp256k1_scalar_mul(&c1, &c1, &minus_b1);
362 secp256k1_scalar_mul(&c2, &c2, &minus_b2);
c35ff1ea 363 secp256k1_scalar_add(r2, &c1, &c2);
f1ebfe39 364 secp256k1_scalar_mul(r1, r2, &minus_lambda);
c35ff1ea 365 secp256k1_scalar_add(r1, r1, a);
6794be60
PW
366}
367#endif
83836a95 368#endif
6794be60 369
a9f5c8b8 370#endif
This page took 0.08539 seconds and 4 git commands to generate.