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