Bitcoin ABC 0.32.7
P2P Digital Currency
ecmult_const_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#ifndef SECP256K1_ECMULT_CONST_IMPL_H
8#define SECP256K1_ECMULT_CONST_IMPL_H
9
10#include "scalar.h"
11#include "group.h"
12#include "ecmult_const.h"
13#include "ecmult_impl.h"
14
24
25 /* Compute the odd multiples in Jacobian form. */
27 /* Bring them to the same Z denominator. */
29}
30
31/* This is like `ECMULT_TABLE_GET_GE` but is constant time */
32#define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \
33 int m = 0; \
34 /* Extract the sign-bit for a constant time absolute-value. */ \
35 int volatile mask = (n) >> (sizeof(n) * CHAR_BIT - 1); \
36 int abs_n = ((n) + mask) ^ mask; \
37 int idx_n = abs_n >> 1; \
38 secp256k1_fe neg_y; \
39 VERIFY_CHECK(((n) & 1) == 1); \
40 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
41 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
42 VERIFY_SETUP(secp256k1_fe_clear(&(r)->x)); \
43 VERIFY_SETUP(secp256k1_fe_clear(&(r)->y)); \
44 /* Unconditionally set r->x = (pre)[m].x. r->y = (pre)[m].y. because it's either the correct one \
45 * or will get replaced in the later iterations, this is needed to make sure `r` is initialized. */ \
46 (r)->x = (pre)[m].x; \
47 (r)->y = (pre)[m].y; \
48 for (m = 1; m < ECMULT_TABLE_SIZE(w); m++) { \
49 /* This loop is used to avoid secret data in array indices. See
50 * the comment in ecmult_gen_impl.h for rationale. */ \
51 secp256k1_fe_cmov(&(r)->x, &(pre)[m].x, m == idx_n); \
52 secp256k1_fe_cmov(&(r)->y, &(pre)[m].y, m == idx_n); \
53 } \
54 (r)->infinity = 0; \
55 secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
56 secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
57} while(0)
58
59
73static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size) {
74 int global_sign;
75 int skew = 0;
76 int word = 0;
77
78 /* 1 2 3 */
79 int u_last;
80 int u;
81
82 int flip;
83 int bit;
85 int not_neg_one;
86
87 VERIFY_CHECK(w > 0);
88 VERIFY_CHECK(size > 0);
89
90 /* Note that we cannot handle even numbers by negating them to be odd, as is
91 * done in other implementations, since if our scalars were specified to have
92 * width < 256 for performance reasons, their negations would have width 256
93 * and we'd lose any performance benefit. Instead, we use a technique from
94 * Section 4.2 of the Okeya/Tagaki paper, which is to add either 1 (for even)
95 * or 2 (for odd) to the number we are encoding, returning a skew value indicating
96 * this, and having the caller compensate after doing the multiplication.
97 *
98 * In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
99 * particular, to ensure that the outputs from the endomorphism-split fit into
100 * 128 bits). If we negate, the parity of our number flips, inverting which of
101 * {1, 2} we want to add to the scalar when ensuring that it's odd. Further
102 * complicating things, -1 interacts badly with `secp256k1_scalar_cadd_bit` and
103 * we need to special-case it in this logic. */
104 flip = secp256k1_scalar_is_high(scalar);
105 /* We add 1 to even numbers, 2 to odd ones, noting that negation flips parity */
106 bit = flip ^ !secp256k1_scalar_is_even(scalar);
107 /* We check for negative one, since adding 2 to it will cause an overflow */
108 secp256k1_scalar_negate(&s, scalar);
109 not_neg_one = !secp256k1_scalar_is_one(&s);
110 s = *scalar;
111 secp256k1_scalar_cadd_bit(&s, bit, not_neg_one);
112 /* If we had negative one, flip == 1, s.d[0] == 0, bit == 1, so caller expects
113 * that we added two to it and flipped it. In fact for -1 these operations are
114 * identical. We only flipped, but since skewing is required (in the sense that
115 * the skew must be 1 or 2, never zero) and flipping is not, we need to change
116 * our flags to claim that we only skewed. */
117 global_sign = secp256k1_scalar_cond_negate(&s, flip);
118 global_sign *= not_neg_one * 2 - 1;
119 skew = 1 << bit;
120
121 /* 4 */
122 u_last = secp256k1_scalar_shr_int(&s, w);
123 do {
124 int even;
125
126 /* 4.1 4.4 */
127 u = secp256k1_scalar_shr_int(&s, w);
128 /* 4.2 */
129 even = ((u & 1) == 0);
130 /* In contrast to the original algorithm, u_last is always > 0 and
131 * therefore we do not need to check its sign. In particular, it's easy
132 * to see that u_last is never < 0 because u is never < 0. Moreover,
133 * u_last is never = 0 because u is never even after a loop
134 * iteration. The same holds analogously for the initial value of
135 * u_last (in the first loop iteration). */
136 VERIFY_CHECK(u_last > 0);
137 VERIFY_CHECK((u_last & 1) == 1);
138 u += even;
139 u_last -= even * (1 << w);
140
141 /* 4.3, adapted for global sign change */
142 wnaf[word++] = u_last * global_sign;
143
144 u_last = u;
145 } while (word * w < size);
146 wnaf[word] = u * global_sign;
147
149 VERIFY_CHECK(word == WNAF_SIZE_BITS(size, w));
150 return skew;
152
153static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar, int size) {
155 secp256k1_ge tmpa;
156 secp256k1_fe Z;
157
158 int skew_1;
160 int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)];
161 int skew_lam;
162 secp256k1_scalar q_1, q_lam;
163 int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)];
164
165 int i;
166
167 /* build wnaf representation for q. */
168 int rsize = size;
169 if (size > 128) {
170 rsize = 128;
171 /* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */
172 secp256k1_scalar_split_lambda(&q_1, &q_lam, scalar);
173 skew_1 = secp256k1_wnaf_const(wnaf_1, &q_1, WINDOW_A - 1, 128);
174 skew_lam = secp256k1_wnaf_const(wnaf_lam, &q_lam, WINDOW_A - 1, 128);
175 } else
176 {
177 skew_1 = secp256k1_wnaf_const(wnaf_1, scalar, WINDOW_A - 1, size);
178 skew_lam = 0;
179 }
180
181 /* Calculate odd multiples of a.
182 * All multiples are brought to the same Z 'denominator', which is stored
183 * in Z. Due to secp256k1' isomorphism we can do all operations pretending
184 * that the Z coordinate was 1, use affine addition formulae, and correct
185 * the Z coordinate of the result once at the end.
186 */
190 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
191 secp256k1_fe_normalize_weak(&pre_a[i].y);
192 }
193 if (size > 128) {
194 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
195 secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
196 }
197
198 }
199
200 /* first loop iteration (separated out so we can directly set r, rather
201 * than having it start at infinity, get doubled several times, then have
202 * its new value added to it) */
203 i = wnaf_1[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
204 VERIFY_CHECK(i != 0);
205 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
206 secp256k1_gej_set_ge(r, &tmpa);
207 if (size > 128) {
208 i = wnaf_lam[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
209 VERIFY_CHECK(i != 0);
210 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A);
211 secp256k1_gej_add_ge(r, r, &tmpa);
212 }
213 /* remaining loop iterations */
214 for (i = WNAF_SIZE_BITS(rsize, WINDOW_A - 1) - 1; i >= 0; i--) {
215 int n;
216 int j;
217 for (j = 0; j < WINDOW_A - 1; ++j) {
219 }
220
221 n = wnaf_1[i];
222 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
223 VERIFY_CHECK(n != 0);
224 secp256k1_gej_add_ge(r, r, &tmpa);
225 if (size > 128) {
226 n = wnaf_lam[i];
227 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
228 VERIFY_CHECK(n != 0);
229 secp256k1_gej_add_ge(r, r, &tmpa);
230 }
231 }
232
233 secp256k1_fe_mul(&r->z, &r->z, &Z);
234
235 {
236 /* Correct for wNAF skew */
237 secp256k1_ge correction = *a;
238 secp256k1_ge_storage correction_1_stor;
239 secp256k1_ge_storage correction_lam_stor;
240 secp256k1_ge_storage a2_stor;
241 secp256k1_gej tmpj;
242 secp256k1_gej_set_ge(&tmpj, &correction);
243 secp256k1_gej_double_var(&tmpj, &tmpj, NULL);
244 secp256k1_ge_set_gej(&correction, &tmpj);
245 secp256k1_ge_to_storage(&correction_1_stor, a);
246 if (size > 128) {
247 secp256k1_ge_to_storage(&correction_lam_stor, a);
248 }
249 secp256k1_ge_to_storage(&a2_stor, &correction);
250
251 /* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */
252 secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2);
253 if (size > 128) {
254 secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2);
255 }
256
257 /* Apply the correction */
258 secp256k1_ge_from_storage(&correction, &correction_1_stor);
259 secp256k1_ge_neg(&correction, &correction);
260 secp256k1_gej_add_ge(r, r, &correction);
261
262 if (size > 128) {
263 secp256k1_ge_from_storage(&correction, &correction_lam_stor);
264 secp256k1_ge_neg(&correction, &correction);
265 secp256k1_ge_mul_lambda(&correction, &correction);
266 secp256k1_gej_add_ge(r, r, &correction);
267 }
268 }
269}
270
271#endif /* SECP256K1_ECMULT_CONST_IMPL_H */
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
Definition: ecmult.h:33
static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a)
Fill a table 'pre' with precomputed odd multiples of a.
#define ECMULT_CONST_TABLE_GET_GE(r, pre, n, w)
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar, int size)
static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size)
Convert a number to WNAF notation.
#define WNAF_SIZE(w)
Definition: ecmult_impl.h:46
static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, secp256k1_fe *zr, const secp256k1_gej *a)
Fill a table 'prej' with precomputed odd multiples of a.
Definition: ecmult_impl.h:64
#define WINDOW_A
Definition: ecmult_impl.h:32
#define WNAF_SIZE_BITS(bits, w)
Definition: ecmult_impl.h:45
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
Weakly normalize a field element: reduce its magnitude to 1, but don't fully normalize.
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
Sets a field element to be the product of two others.
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast.
static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr)
Bring a batch inputs given in jacobian coordinates (with known z-ratios) to the same global z "denomi...
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity).
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Convert a group element back from the storage type.
static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the double of a.
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Convert a group element to the storage type.
static int secp256k1_scalar_is_even(const secp256k1_scalar *a)
Check whether a scalar, considered as an nonnegative integer, is even.
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
static int secp256k1_scalar_cond_negate(secp256k1_scalar *a, int flag)
Conditionally negate a number, in constant time.
static int secp256k1_scalar_is_one(const secp256k1_scalar *a)
Check whether a scalar equals one.
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
Check whether a scalar is higher than the group order divided by 2.
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag)
Conditionally add a power of two to a scalar.
static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k)
Find r1 and r2 such that r1+r2*lambda = k, where r1 and r2 or their negations are maximum 128 bits lo...
static int secp256k1_scalar_shr_int(secp256k1_scalar *r, int n)
Shift a scalar right by some amount strictly between 0 and 16, returning the low bits that were shift...
#define VERIFY_CHECK(cond)
Definition: util.h:95
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
int infinity
Definition: group.h:16
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:23
secp256k1_fe z
Definition: group.h:26
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13