1 /**********************************************************************
2 * Copyright (c) 2013, 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 **********************************************************************/
7 #ifndef _SECP256K1_ECMULT_IMPL_H_
8 #define _SECP256K1_ECMULT_IMPL_H_
14 /* optimal for 128-bit and 256-bit exponents. */
17 /** larger numbers may result in slightly better performance, at the cost of
18 exponentially larger precomputed tables. */
19 #ifdef USE_ENDOMORPHISM
20 /** Two tables for window size 15: 1.375 MiB. */
23 /** One table for window size 16: 1.375 MiB. */
27 /** The number of entries a table with precomputed multiples needs to have. */
28 #define ECMULT_TABLE_SIZE(w) (1 << ((w)-2))
30 /** Fill a table 'prej' with precomputed odd multiples of a. Prej will contain
31 * the values [1*a,3*a,...,(2*n-1)*a], so it space for n values. zr[0] will
32 * contain prej[0].z / a.z. The other zr[i] values = prej[i].z / prej[i-1].z.
33 * Prej's Z values are undefined, except for the last value.
35 static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej_t *prej, secp256k1_fe_t *zr, const secp256k1_gej_t *a) {
37 secp256k1_ge_t a_ge, d_ge;
40 VERIFY_CHECK(!a->infinity);
42 secp256k1_gej_double_var(&d, a, NULL);
45 * Perform the additions on an isomorphism where 'd' is affine: drop the z coordinate
46 * of 'd', and scale the 1P starting value's x/y coordinates without changing its z.
52 secp256k1_ge_set_gej_zinv(&a_ge, a, &d.z);
59 for (i = 1; i < n; i++) {
60 secp256k1_gej_add_ge_var(&prej[i], &prej[i-1], &d_ge, &zr[i]);
64 * Each point in 'prej' has a z coordinate too small by a factor of 'd.z'. Only
65 * the final point's z coordinate is actually used though, so just update that.
67 secp256k1_fe_mul(&prej[n-1].z, &prej[n-1].z, &d.z);
70 /** Fill a table 'pre' with precomputed odd multiples of a.
72 * There are two versions of this function:
73 * - secp256k1_ecmult_odd_multiples_table_globalz_windowa which brings its
74 * resulting point set to a single constant Z denominator, stores the X and Y
75 * coordinates as ge_storage points in pre, and stores the global Z in rz.
76 * It only operates on tables sized for WINDOW_A wnaf multiples.
77 * - secp256k1_ecmult_odd_multiples_table_storage_var, which converts its
78 * resulting point set to actually affine points, and stores those in pre.
79 * It operates on tables of any size, but uses heap-allocated temporaries.
81 * To compute a*P + b*G, we compute a table for P using the first function,
82 * and for G using the second (which requires an inverse, but it only needs to
85 static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge_t *pre, secp256k1_fe_t *globalz, const secp256k1_gej_t *a) {
86 secp256k1_gej_t prej[ECMULT_TABLE_SIZE(WINDOW_A)];
87 secp256k1_fe_t zr[ECMULT_TABLE_SIZE(WINDOW_A)];
89 /* Compute the odd multiples in Jacobian form. */
90 secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), prej, zr, a);
91 /* Bring them to the same Z denominator. */
92 secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A), pre, globalz, prej, zr);
95 static void secp256k1_ecmult_odd_multiples_table_storage_var(int n, secp256k1_ge_storage_t *pre, const secp256k1_gej_t *a) {
96 secp256k1_gej_t *prej = checked_malloc(sizeof(secp256k1_gej_t) * n);
97 secp256k1_ge_t *prea = checked_malloc(sizeof(secp256k1_ge_t) * n);
98 secp256k1_fe_t *zr = checked_malloc(sizeof(secp256k1_fe_t) * n);
101 /* Compute the odd multiples in Jacobian form. */
102 secp256k1_ecmult_odd_multiples_table(n, prej, zr, a);
103 /* Convert them in batch to affine coordinates. */
104 secp256k1_ge_set_table_gej_var(n, prea, prej, zr);
105 /* Convert them to compact storage form. */
106 for (i = 0; i < n; i++) {
107 secp256k1_ge_to_storage(&pre[i], &prea[i]);
115 /** The following two macro retrieves a particular odd multiple from a table
116 * of precomputed multiples. */
117 #define ECMULT_TABLE_GET_GE(r,pre,n,w) do { \
118 VERIFY_CHECK(((n) & 1) == 1); \
119 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
120 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
122 *(r) = (pre)[((n)-1)/2]; \
124 secp256k1_ge_neg((r), &(pre)[(-(n)-1)/2]); \
128 #define ECMULT_TABLE_GET_GE_STORAGE(r,pre,n,w) do { \
129 VERIFY_CHECK(((n) & 1) == 1); \
130 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
131 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
133 secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \
135 secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \
136 secp256k1_ge_neg((r), (r)); \
140 static void secp256k1_ecmult_context_init(secp256k1_ecmult_context_t *ctx) {
142 #ifdef USE_ENDOMORPHISM
143 ctx->pre_g_128 = NULL;
147 static void secp256k1_ecmult_context_build(secp256k1_ecmult_context_t *ctx) {
150 if (ctx->pre_g != NULL) {
154 /* get the generator */
155 secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
157 ctx->pre_g = (secp256k1_ge_storage_t (*)[])checked_malloc(sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
159 /* precompute the tables with odd multiples */
160 secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g, &gj);
162 #ifdef USE_ENDOMORPHISM
164 secp256k1_gej_t g_128j;
167 ctx->pre_g_128 = (secp256k1_ge_storage_t (*)[])checked_malloc(sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
169 /* calculate 2^128*generator */
171 for (i = 0; i < 128; i++) {
172 secp256k1_gej_double_var(&g_128j, &g_128j, NULL);
174 secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g_128, &g_128j);
179 static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context_t *dst,
180 const secp256k1_ecmult_context_t *src) {
181 if (src->pre_g == NULL) {
184 size_t size = sizeof((*dst->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
185 dst->pre_g = (secp256k1_ge_storage_t (*)[])checked_malloc(size);
186 memcpy(dst->pre_g, src->pre_g, size);
188 #ifdef USE_ENDOMORPHISM
189 if (src->pre_g_128 == NULL) {
190 dst->pre_g_128 = NULL;
192 size_t size = sizeof((*dst->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
193 dst->pre_g_128 = (secp256k1_ge_storage_t (*)[])checked_malloc(size);
194 memcpy(dst->pre_g_128, src->pre_g_128, size);
199 static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context_t *ctx) {
200 return ctx->pre_g != NULL;
203 static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context_t *ctx) {
205 #ifdef USE_ENDOMORPHISM
206 free(ctx->pre_g_128);
208 secp256k1_ecmult_context_init(ctx);
211 /** Convert a number to WNAF notation. The number becomes represented by sum(2^i * wnaf[i], i=0..bits),
212 * with the following guarantees:
213 * - each wnaf[i] is either 0, or an odd integer between -(1<<(w-1) - 1) and (1<<(w-1) - 1)
214 * - two non-zero entries in wnaf are separated by at least w-1 zeroes.
215 * - the number of set values in wnaf is returned. This number is at most 256, and at most one more
216 * - than the number of bits in the (absolute value) of the input.
218 static int secp256k1_ecmult_wnaf(int *wnaf, const secp256k1_scalar_t *a, int w) {
219 secp256k1_scalar_t s = *a;
225 if (secp256k1_scalar_get_bits(&s, 255, 1)) {
226 secp256k1_scalar_negate(&s, &s);
233 if (secp256k1_scalar_get_bits(&s, bit, 1) == (unsigned int)carry) {
239 if (now > 256 - bit) {
243 word = secp256k1_scalar_get_bits_var(&s, bit, now) + carry;
245 carry = (word >> (w-1)) & 1;
248 while (set_bits < bit) {
249 wnaf[set_bits++] = 0;
251 wnaf[set_bits++] = sign * word;
254 VERIFY_CHECK(carry == 0);
258 static void secp256k1_ecmult(const secp256k1_ecmult_context_t *ctx, secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_scalar_t *na, const secp256k1_scalar_t *ng) {
259 secp256k1_ge_t pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
262 #ifdef USE_ENDOMORPHISM
263 secp256k1_ge_t pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
264 secp256k1_scalar_t na_1, na_lam;
265 /* Splitted G factors. */
266 secp256k1_scalar_t ng_1, ng_128;
268 int wnaf_na_lam[130];
273 int wnaf_ng_128[129];
284 #ifdef USE_ENDOMORPHISM
285 /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
286 secp256k1_scalar_split_lambda_var(&na_1, &na_lam, na);
288 /* build wnaf representation for na_1 and na_lam. */
289 bits_na_1 = secp256k1_ecmult_wnaf(wnaf_na_1, &na_1, WINDOW_A);
290 bits_na_lam = secp256k1_ecmult_wnaf(wnaf_na_lam, &na_lam, WINDOW_A);
291 VERIFY_CHECK(bits_na_1 <= 130);
292 VERIFY_CHECK(bits_na_lam <= 130);
294 if (bits_na_lam > bits) {
298 /* build wnaf representation for na. */
299 bits_na = secp256k1_ecmult_wnaf(wnaf_na, na, WINDOW_A);
303 /* Calculate odd multiples of a.
304 * All multiples are brought to the same Z 'denominator', which is stored
305 * in Z. Due to secp256k1' isomorphism we can do all operations pretending
306 * that the Z coordinate was 1, use affine addition formulae, and correct
307 * the Z coordinate of the result once at the end.
308 * The exception is the precomputed G table points, which are actually
309 * affine. Compared to the base used for other points, they have a Z ratio
310 * of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
311 * isomorphism to efficiently add with a known Z inverse.
313 secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, a);
315 #ifdef USE_ENDOMORPHISM
316 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
317 secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
320 /* split ng into ng_1 and ng_128 (where gn = gn_1 + gn_128*2^128, and gn_1 and gn_128 are ~128 bit) */
321 secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
323 /* Build wnaf representation for ng_1 and ng_128 */
324 bits_ng_1 = secp256k1_ecmult_wnaf(wnaf_ng_1, &ng_1, WINDOW_G);
325 bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, &ng_128, WINDOW_G);
326 if (bits_ng_1 > bits) {
329 if (bits_ng_128 > bits) {
333 bits_ng = secp256k1_ecmult_wnaf(wnaf_ng, ng, WINDOW_G);
334 if (bits_ng > bits) {
339 secp256k1_gej_set_infinity(r);
341 for (i = bits - 1; i >= 0; i--) {
343 secp256k1_gej_double_var(r, r, NULL);
344 #ifdef USE_ENDOMORPHISM
345 if (i < bits_na_1 && (n = wnaf_na_1[i])) {
346 ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
347 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
349 if (i < bits_na_lam && (n = wnaf_na_lam[i])) {
350 ECMULT_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
351 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
353 if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
354 ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
355 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
357 if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
358 ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g_128, n, WINDOW_G);
359 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
362 if (i < bits_na && (n = wnaf_na[i])) {
363 ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
364 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
366 if (i < bits_ng && (n = wnaf_ng[i])) {
367 ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
368 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
374 secp256k1_fe_mul(&r->z, &r->z, &Z);