]> Git Repo - secp256k1.git/blame - src/ecmult_const_impl.h
Add constant-time multiply `secp256k1_ecmult_const` for ECDH
[secp256k1.git] / src / ecmult_const_impl.h
CommitLineData
44015000
AP
1/**********************************************************************
2 * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
6
7#ifndef _SECP256K1_ECMULT_CONST_IMPL_
8#define _SECP256K1_ECMULT_CONST_IMPL_
9
10#include "scalar.h"
11#include "group.h"
12#include "ecmult_const.h"
13#include "ecmult_impl.h"
14
15#define WNAF_BITS 256
16#define WNAF_SIZE(w) ((WNAF_BITS + (w) - 1) / (w))
17
18/* This is like `ECMULT_TABLE_GET_GE` but is constant time */
19#define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \
20 int m; \
21 int abs_n = (n) * (((n) > 0) * 2 - 1); \
22 secp256k1_fe_t neg_y; \
23 VERIFY_CHECK(((n) & 1) == 1); \
24 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
25 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
26 for (m = 1; m < (1 << ((w) - 1)); m += 2) { \
27 /* This loop is used to avoid secret data in array indices. See
28 * the comment in ecmult_gen_impl.h for rationale. */ \
29 secp256k1_fe_cmov(&(r)->x, &(pre)[(m - 1) / 2].x, m == abs_n); \
30 secp256k1_fe_cmov(&(r)->y, &(pre)[(m - 1) / 2].y, m == abs_n); \
31 } \
32 (r)->infinity = 0; \
33 secp256k1_fe_normalize_weak(&(r)->x); \
34 secp256k1_fe_normalize_weak(&(r)->y); \
35 secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
36 secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
37} while(0)
38
39
40/** Convert a number to WNAF notation. The number becomes represented by sum(2^{wi} * wnaf[i], i=0..return_val)
41 * with the following guarantees:
42 * - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
43 * - each wnaf[i] is nonzero
44 * - the number of words set is returned; this is always (WNAF_BITS + w - 1) / w
45 *
46 * Adapted from `The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar
47 * Multiplications Secure against Side Channel Attacks`, Okeya and Tagaki. M. Joye (Ed.)
48 * CT-RSA 2003, LNCS 2612, pp. 328-443, 2003. Springer-Verlagy Berlin Heidelberg 2003
49 *
50 * Numbers reference steps of `Algorithm SPA-resistant Width-w NAF with Odd Scalar` on pp. 335
51 */
52static void secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar_t *a, int w) {
53 secp256k1_scalar_t s = *a;
54 /* Negate to force oddness */
55 int is_even = secp256k1_scalar_is_even(&s);
56 int global_sign = secp256k1_scalar_cond_negate(&s, is_even);
57
58 int word = 0;
59 /* 1 2 3 */
60 int u_last = secp256k1_scalar_shr_int(&s, w);
61 int u;
62 /* 4 */
63 while (word * w < WNAF_BITS) {
64 int sign;
65 int even;
66
67 /* 4.1 4.4 */
68 u = secp256k1_scalar_shr_int(&s, w);
69 /* 4.2 */
70 even = ((u & 1) == 0);
71 sign = 2 * (u_last > 0) - 1;
72 u += sign * even;
73 u_last -= sign * even * (1 << w);
74
75 /* 4.3, adapted for global sign change */
76 wnaf[word++] = u_last * global_sign;
77
78 u_last = u;
79 }
80 wnaf[word] = u * global_sign;
81
82 VERIFY_CHECK(secp256k1_scalar_is_zero(&s));
83 VERIFY_CHECK(word == WNAF_SIZE(w));
84}
85
86
87static void secp256k1_ecmult_const(secp256k1_gej_t *r, const secp256k1_ge_t *a, const secp256k1_scalar_t *scalar) {
88 secp256k1_ge_t pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
89 secp256k1_ge_t tmpa;
90 secp256k1_fe_t Z;
91
92 int wnaf[1 + WNAF_SIZE(WINDOW_A - 1)];
93
94 int i;
95 int is_zero = secp256k1_scalar_is_zero(scalar);
96 secp256k1_scalar_t sc = *scalar;
97 /* the wNAF ladder cannot handle zero, so bump this to one .. we will
98 * correct the result after the fact */
99 sc.d[0] += is_zero;
100
101 /* build wnaf representation for q. */
102 secp256k1_wnaf_const(wnaf, &sc, WINDOW_A - 1);
103
104 /* Calculate odd multiples of a.
105 * All multiples are brought to the same Z 'denominator', which is stored
106 * in Z. Due to secp256k1' isomorphism we can do all operations pretending
107 * that the Z coordinate was 1, use affine addition formulae, and correct
108 * the Z coordinate of the result once at the end.
109 */
110 secp256k1_gej_set_ge(r, a);
111 secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, r);
112
113 /* first loop iteration (separated out so we can directly set r, rather
114 * than having it start at infinity, get doubled several times, then have
115 * its new value added to it) */
116 i = wnaf[WNAF_SIZE(WINDOW_A - 1)];
117 VERIFY_CHECK(i != 0);
118 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
119 secp256k1_gej_set_ge(r, &tmpa);
120 /* remaining loop iterations */
121 for (i = WNAF_SIZE(WINDOW_A - 1) - 1; i >= 0; i--) {
122 int n;
123 int j;
124 for (j = 0; j < WINDOW_A - 1; ++j) {
125 secp256k1_gej_double_nonzero(r, r, NULL);
126 }
127 n = wnaf[i];
128 VERIFY_CHECK(n != 0);
129 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
130 secp256k1_gej_add_ge(r, r, &tmpa);
131 }
132
133 secp256k1_fe_mul(&r->z, &r->z, &Z);
134
135 /* correct for zero */
136 r->infinity |= is_zero;
137}
138
139#endif
This page took 0.034875 seconds and 4 git commands to generate.