]> Git Repo - secp256k1.git/blame - src/scalar_impl.h
Merge pull request #196
[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
10#include <string.h>
11
d1502eb4 12#include "group.h"
a9f5c8b8
PW
13#include "scalar.h"
14
1d52a8b1
PW
15#if defined HAVE_CONFIG_H
16#include "libsecp256k1-config.h"
17#endif
79359302 18
1d52a8b1
PW
19#if defined(USE_SCALAR_4X64)
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
a4a43d75 28static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_t *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
659b554d 34static void secp256k1_scalar_order_get_num(secp256k1_num_t *r) {
f1ebfe39
PW
35 static const unsigned char order[32] = {
36 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
37 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
38 0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
39 0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
40 };
41 secp256k1_num_set_bin(r, order, 32);
659b554d 42}
597128d3 43#endif
1d52a8b1 44
a4a43d75 45static void secp256k1_scalar_inverse(secp256k1_scalar_t *r, const secp256k1_scalar_t *x) {
71712b27 46 /* First compute x ^ (2^N - 1) for some values of N. */
1d52a8b1
PW
47 secp256k1_scalar_t x2, x3, x4, x6, x7, x8, x15, x30, x60, x120, x127;
48
49 secp256k1_scalar_sqr(&x2, x);
50 secp256k1_scalar_mul(&x2, &x2, x);
51
52 secp256k1_scalar_sqr(&x3, &x2);
53 secp256k1_scalar_mul(&x3, &x3, x);
54
55 secp256k1_scalar_sqr(&x4, &x3);
56 secp256k1_scalar_mul(&x4, &x4, x);
57
58 secp256k1_scalar_sqr(&x6, &x4);
59 secp256k1_scalar_sqr(&x6, &x6);
60 secp256k1_scalar_mul(&x6, &x6, &x2);
61
62 secp256k1_scalar_sqr(&x7, &x6);
63 secp256k1_scalar_mul(&x7, &x7, x);
64
65 secp256k1_scalar_sqr(&x8, &x7);
66 secp256k1_scalar_mul(&x8, &x8, x);
67
68 secp256k1_scalar_sqr(&x15, &x8);
69 for (int i=0; i<6; i++)
70 secp256k1_scalar_sqr(&x15, &x15);
71 secp256k1_scalar_mul(&x15, &x15, &x7);
72
73 secp256k1_scalar_sqr(&x30, &x15);
74 for (int i=0; i<14; i++)
75 secp256k1_scalar_sqr(&x30, &x30);
76 secp256k1_scalar_mul(&x30, &x30, &x15);
77
78 secp256k1_scalar_sqr(&x60, &x30);
79 for (int i=0; i<29; i++)
80 secp256k1_scalar_sqr(&x60, &x60);
81 secp256k1_scalar_mul(&x60, &x60, &x30);
82
83 secp256k1_scalar_sqr(&x120, &x60);
84 for (int i=0; i<59; i++)
85 secp256k1_scalar_sqr(&x120, &x120);
86 secp256k1_scalar_mul(&x120, &x120, &x60);
87
88 secp256k1_scalar_sqr(&x127, &x120);
89 for (int i=0; i<6; i++)
90 secp256k1_scalar_sqr(&x127, &x127);
91 secp256k1_scalar_mul(&x127, &x127, &x7);
92
71712b27 93 /* Then accumulate the final result (t starts at x127). */
1d52a8b1 94 secp256k1_scalar_t *t = &x127;
71712b27 95 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 96 secp256k1_scalar_sqr(t, t);
71712b27
GM
97 secp256k1_scalar_mul(t, t, x); /* 1 */
98 for (int i=0; i<4; i++) /* 0 */
1d52a8b1 99 secp256k1_scalar_sqr(t, t);
71712b27
GM
100 secp256k1_scalar_mul(t, t, &x3); /* 111 */
101 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 102 secp256k1_scalar_sqr(t, t);
71712b27
GM
103 secp256k1_scalar_mul(t, t, x); /* 1 */
104 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 105 secp256k1_scalar_sqr(t, t);
71712b27
GM
106 secp256k1_scalar_mul(t, t, x); /* 1 */
107 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 108 secp256k1_scalar_sqr(t, t);
71712b27
GM
109 secp256k1_scalar_mul(t, t, x); /* 1 */
110 for (int i=0; i<4; i++) /* 0 */
1d52a8b1 111 secp256k1_scalar_sqr(t, t);
71712b27
GM
112 secp256k1_scalar_mul(t, t, &x3); /* 111 */
113 for (int i=0; i<3; i++) /* 0 */
1d52a8b1 114 secp256k1_scalar_sqr(t, t);
71712b27
GM
115 secp256k1_scalar_mul(t, t, &x2); /* 11 */
116 for (int i=0; i<4; i++) /* 0 */
1d52a8b1 117 secp256k1_scalar_sqr(t, t);
71712b27
GM
118 secp256k1_scalar_mul(t, t, &x3); /* 111 */
119 for (int i=0; i<5; i++) /* 00 */
1d52a8b1 120 secp256k1_scalar_sqr(t, t);
71712b27
GM
121 secp256k1_scalar_mul(t, t, &x3); /* 111 */
122 for (int i=0; i<4; i++) /* 00 */
1d52a8b1 123 secp256k1_scalar_sqr(t, t);
71712b27
GM
124 secp256k1_scalar_mul(t, t, &x2); /* 11 */
125 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 126 secp256k1_scalar_sqr(t, t);
71712b27
GM
127 secp256k1_scalar_mul(t, t, x); /* 1 */
128 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 129 secp256k1_scalar_sqr(t, t);
71712b27
GM
130 secp256k1_scalar_mul(t, t, x); /* 1 */
131 for (int i=0; i<5; i++) /* 0 */
1d52a8b1 132 secp256k1_scalar_sqr(t, t);
71712b27
GM
133 secp256k1_scalar_mul(t, t, &x4); /* 1111 */
134 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 135 secp256k1_scalar_sqr(t, t);
71712b27
GM
136 secp256k1_scalar_mul(t, t, x); /* 1 */
137 for (int i=0; i<3; i++) /* 00 */
1d52a8b1 138 secp256k1_scalar_sqr(t, t);
71712b27
GM
139 secp256k1_scalar_mul(t, t, x); /* 1 */
140 for (int i=0; i<4; i++) /* 000 */
1d52a8b1 141 secp256k1_scalar_sqr(t, t);
71712b27
GM
142 secp256k1_scalar_mul(t, t, x); /* 1 */
143 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 144 secp256k1_scalar_sqr(t, t);
71712b27
GM
145 secp256k1_scalar_mul(t, t, x); /* 1 */
146 for (int i=0; i<10; i++) /* 0000000 */
1d52a8b1 147 secp256k1_scalar_sqr(t, t);
71712b27
GM
148 secp256k1_scalar_mul(t, t, &x3); /* 111 */
149 for (int i=0; i<4; i++) /* 0 */
1d52a8b1 150 secp256k1_scalar_sqr(t, t);
71712b27
GM
151 secp256k1_scalar_mul(t, t, &x3); /* 111 */
152 for (int i=0; i<9; i++) /* 0 */
1d52a8b1 153 secp256k1_scalar_sqr(t, t);
71712b27
GM
154 secp256k1_scalar_mul(t, t, &x8); /* 11111111 */
155 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 156 secp256k1_scalar_sqr(t, t);
71712b27
GM
157 secp256k1_scalar_mul(t, t, x); /* 1 */
158 for (int i=0; i<3; i++) /* 00 */
1d52a8b1 159 secp256k1_scalar_sqr(t, t);
71712b27
GM
160 secp256k1_scalar_mul(t, t, x); /* 1 */
161 for (int i=0; i<3; i++) /* 00 */
1d52a8b1 162 secp256k1_scalar_sqr(t, t);
71712b27
GM
163 secp256k1_scalar_mul(t, t, x); /* 1 */
164 for (int i=0; i<5; i++) /* 0 */
1d52a8b1 165 secp256k1_scalar_sqr(t, t);
71712b27
GM
166 secp256k1_scalar_mul(t, t, &x4); /* 1111 */
167 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 168 secp256k1_scalar_sqr(t, t);
71712b27
GM
169 secp256k1_scalar_mul(t, t, x); /* 1 */
170 for (int i=0; i<5; i++) /* 000 */
1d52a8b1 171 secp256k1_scalar_sqr(t, t);
71712b27
GM
172 secp256k1_scalar_mul(t, t, &x2); /* 11 */
173 for (int i=0; i<4; i++) /* 00 */
1d52a8b1 174 secp256k1_scalar_sqr(t, t);
71712b27
GM
175 secp256k1_scalar_mul(t, t, &x2); /* 11 */
176 for (int i=0; i<2; i++) /* 0 */
1d52a8b1 177 secp256k1_scalar_sqr(t, t);
71712b27
GM
178 secp256k1_scalar_mul(t, t, x); /* 1 */
179 for (int i=0; i<8; i++) /* 000000 */
1d52a8b1 180 secp256k1_scalar_sqr(t, t);
71712b27
GM
181 secp256k1_scalar_mul(t, t, &x2); /* 11 */
182 for (int i=0; i<3; i++) /* 0 */
1d52a8b1 183 secp256k1_scalar_sqr(t, t);
71712b27
GM
184 secp256k1_scalar_mul(t, t, &x2); /* 11 */
185 for (int i=0; i<3; i++) /* 00 */
1d52a8b1 186 secp256k1_scalar_sqr(t, t);
71712b27
GM
187 secp256k1_scalar_mul(t, t, x); /* 1 */
188 for (int i=0; i<6; i++) /* 00000 */
1d52a8b1 189 secp256k1_scalar_sqr(t, t);
71712b27
GM
190 secp256k1_scalar_mul(t, t, x); /* 1 */
191 for (int i=0; i<8; i++) /* 00 */
1d52a8b1 192 secp256k1_scalar_sqr(t, t);
71712b27 193 secp256k1_scalar_mul(r, t, &x6); /* 111111 */
1d52a8b1
PW
194}
195
d1502eb4
PW
196static void secp256k1_scalar_inverse_var(secp256k1_scalar_t *r, const secp256k1_scalar_t *x) {
197#if defined(USE_SCALAR_INV_BUILTIN)
198 secp256k1_scalar_inverse(r, x);
199#elif defined(USE_SCALAR_INV_NUM)
200 unsigned char b[32];
201 secp256k1_scalar_get_b32(b, x);
f1ebfe39 202 secp256k1_num_t n, m;
d1502eb4 203 secp256k1_num_set_bin(&n, b, 32);
f1ebfe39
PW
204 secp256k1_scalar_order_get_num(&m);
205 secp256k1_num_mod_inverse(&n, &n, &m);
d1502eb4
PW
206 secp256k1_num_get_bin(b, 32, &n);
207 secp256k1_scalar_set_b32(r, b, NULL);
208#else
209#error "Please select scalar inverse implementation"
210#endif
211}
212
6794be60 213#ifdef USE_ENDOMORPHISM
f1ebfe39
PW
214/**
215 * The Secp256k1 curve has an endomorphism, where lambda * (x, y) = (beta * x, y), where
216 * lambda is {0x53,0x63,0xad,0x4c,0xc0,0x5c,0x30,0xe0,0xa5,0x26,0x1c,0x02,0x88,0x12,0x64,0x5a,
217 * 0x12,0x2e,0x22,0xea,0x20,0x81,0x66,0x78,0xdf,0x02,0x96,0x7c,0x1b,0x23,0xbd,0x72}
218 *
219 * "Guide to Elliptic Curve Cryptography" (Hankerson, Menezes, Vanstone) gives an algorithm
220 * (algorithm 3.74) to find k1 and k2 given k, such that k1 + k2 * lambda == k mod n, and k1
221 * and k2 have a small size.
222 * It relies on constants a1, b1, a2, b2. These constants for the value of lambda above are:
223 *
224 * - a1 = {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
225 * - b1 = -{0xe4,0x43,0x7e,0xd6,0x01,0x0e,0x88,0x28,0x6f,0x54,0x7f,0xa9,0x0a,0xbf,0xe4,0xc3}
226 * - a2 = {0x01,0x14,0xca,0x50,0xf7,0xa8,0xe2,0xf3,0xf6,0x57,0xc1,0x10,0x8d,0x9d,0x44,0xcf,0xd8}
227 * - b2 = {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
228 *
229 * The algorithm then computes c1 = round(b1 * k / n) and c2 = round(b2 * k / n), and gives
230 * k1 = k - (c1*a1 + c2*a2) and k2 = -(c1*b1 + c2*b2). Instead, we use modular arithmetic, and
231 * compute k1 as k - k2 * lambda, avoiding the need for constants a1 and a2.
232 *
233 * g1, g2 are precomputed constants used to replace division with a rounded multiplication
234 * when decomposing the scalar for an endomorphism-based point multiplication.
235 *
236 * The possibility of using precomputed estimates is mentioned in "Guide to Elliptic Curve
237 * Cryptography" (Hankerson, Menezes, Vanstone) in section 3.5.
238 *
239 * The derivation is described in the paper "Efficient Software Implementation of Public-Key
240 * Cryptography on Sensor Networks Using the MSP430X Microcontroller" (Gouvea, Oliveira, Lopez),
241 * Section 4.3 (here we use a somewhat higher-precision estimate):
242 * d = a1*b2 - b1*a2
243 * g1 = round((2^272)*b2/d)
244 * g2 = round((2^272)*b1/d)
245 *
246 * (Note that 'd' is also equal to the curve order here because [a1,b1] and [a2,b2] are found
247 * as outputs of the Extended Euclidean Algorithm on inputs 'order' and 'lambda').
248 *
249 * The function below splits a in r1 and r2, such that r1 + lambda * r2 == a (mod order).
250 */
251
6794be60 252static void secp256k1_scalar_split_lambda_var(secp256k1_scalar_t *r1, secp256k1_scalar_t *r2, const secp256k1_scalar_t *a) {
f1ebfe39
PW
253 static const secp256k1_scalar_t minus_lambda = SECP256K1_SCALAR_CONST(
254 0xAC9C52B3UL, 0x3FA3CF1FUL, 0x5AD9E3FDUL, 0x77ED9BA4UL,
255 0xA880B9FCUL, 0x8EC739C2UL, 0xE0CFC810UL, 0xB51283CFUL
256 );
257 static const secp256k1_scalar_t minus_b1 = SECP256K1_SCALAR_CONST(
258 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL,
259 0xE4437ED6UL, 0x010E8828UL, 0x6F547FA9UL, 0x0ABFE4C3UL
260 );
261 static const secp256k1_scalar_t minus_b2 = SECP256K1_SCALAR_CONST(
262 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
263 0x8A280AC5UL, 0x0774346DUL, 0xD765CDA8UL, 0x3DB1562CUL
264 );
265 static const secp256k1_scalar_t g1 = SECP256K1_SCALAR_CONST(
266 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00003086UL,
267 0xD221A7D4UL, 0x6BCDE86CUL, 0x90E49284UL, 0xEB153DABUL
268 );
269 static const secp256k1_scalar_t g2 = SECP256K1_SCALAR_CONST(
270 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x0000E443UL,
271 0x7ED6010EUL, 0x88286F54UL, 0x7FA90ABFUL, 0xE4C42212UL
272 );
c35ff1ea
PW
273 VERIFY_CHECK(r1 != a);
274 VERIFY_CHECK(r2 != a);
275 secp256k1_scalar_t c1, c2;
f1ebfe39
PW
276 secp256k1_scalar_mul_shift_var(&c1, a, &g1, 272);
277 secp256k1_scalar_mul_shift_var(&c2, a, &g2, 272);
278 secp256k1_scalar_mul(&c1, &c1, &minus_b1);
279 secp256k1_scalar_mul(&c2, &c2, &minus_b2);
c35ff1ea 280 secp256k1_scalar_add(r2, &c1, &c2);
f1ebfe39 281 secp256k1_scalar_mul(r1, r2, &minus_lambda);
c35ff1ea 282 secp256k1_scalar_add(r1, r1, a);
6794be60
PW
283}
284#endif
285
a9f5c8b8 286#endif
This page took 0.082732 seconds and 4 git commands to generate.