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_GEN_IMPL_H_
8 #define _SECP256K1_ECMULT_GEN_IMPL_H_
12 #include "ecmult_gen.h"
15 /* For accelerating the computation of a*G:
16 * To harden against timing attacks, use the following mechanism:
17 * * Break up the multiplicand into groups of 4 bits, called n_0, n_1, n_2, ..., n_63.
18 * * Compute sum(n_i * 16^i * G + U_i, i=0..63), where:
19 * * U_i = U * 2^i (for i=0..62)
20 * * U_i = U * (1-2^63) (for i=63)
21 * where U is a point with no known corresponding scalar. Note that sum(U_i, i=0..63) = 0.
22 * For each i, and each of the 16 possible values of n_i, (n_i * 16^i * G + U_i) is
23 * precomputed (call it prec(i, n_i)). The formula now becomes sum(prec(i, n_i), i=0..63).
24 * None of the resulting prec group elements have a known scalar, and neither do any of
25 * the intermediate sums while computing a*G.
27 secp256k1_fe_t prec[64][16][2]; /* prec[j][i] = (16^j * i * G + U_i).{x,y} */
28 } secp256k1_ecmult_gen_consts_t;
30 static const secp256k1_ecmult_gen_consts_t *secp256k1_ecmult_gen_consts = NULL;
32 static void secp256k1_ecmult_gen_start(void) {
33 if (secp256k1_ecmult_gen_consts != NULL)
36 /* Allocate the precomputation table. */
37 secp256k1_ecmult_gen_consts_t *ret = (secp256k1_ecmult_gen_consts_t*)checked_malloc(sizeof(secp256k1_ecmult_gen_consts_t));
39 /* get the generator */
40 const secp256k1_ge_t *g = &secp256k1_ge_consts->g;
41 secp256k1_gej_t gj; secp256k1_gej_set_ge(&gj, g);
43 /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
44 secp256k1_gej_t nums_gej;
46 static const unsigned char nums_b32[32] = "The scalar for this x is unknown";
47 secp256k1_fe_t nums_x;
48 VERIFY_CHECK(secp256k1_fe_set_b32(&nums_x, nums_b32));
49 secp256k1_ge_t nums_ge;
50 VERIFY_CHECK(secp256k1_ge_set_xo_var(&nums_ge, &nums_x, 0));
51 secp256k1_gej_set_ge(&nums_gej, &nums_ge);
52 /* Add G to make the bits in x uniformly distributed. */
53 secp256k1_gej_add_ge_var(&nums_gej, &nums_gej, g);
57 secp256k1_ge_t prec[1024];
59 secp256k1_gej_t precj[1024]; /* Jacobian versions of prec. */
60 secp256k1_gej_t gbase; gbase = gj; /* 16^j * G */
61 secp256k1_gej_t numsbase; numsbase = nums_gej; /* 2^j * nums. */
62 for (int j=0; j<64; j++) {
63 /* Set precj[j*16 .. j*16+15] to (numsbase, numsbase + gbase, ..., numsbase + 15*gbase). */
64 precj[j*16] = numsbase;
65 for (int i=1; i<16; i++) {
66 secp256k1_gej_add_var(&precj[j*16 + i], &precj[j*16 + i - 1], &gbase);
68 /* Multiply gbase by 16. */
69 for (int i=0; i<4; i++) {
70 secp256k1_gej_double_var(&gbase, &gbase);
72 /* Multiply numbase by 2. */
73 secp256k1_gej_double_var(&numsbase, &numsbase);
75 /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
76 secp256k1_gej_neg(&numsbase, &numsbase);
77 secp256k1_gej_add_var(&numsbase, &numsbase, &nums_gej);
80 secp256k1_ge_set_all_gej_var(1024, prec, precj);
82 for (int j=0; j<64; j++) {
83 for (int i=0; i<16; i++) {
84 VERIFY_CHECK(!secp256k1_ge_is_infinity(&prec[j*16 + i]));
85 ret->prec[j][i][0] = prec[j*16 + i].x;
86 ret->prec[j][i][1] = prec[j*16 + i].y;
90 /* Set the global pointer to the precomputation table. */
91 secp256k1_ecmult_gen_consts = ret;
94 static void secp256k1_ecmult_gen_stop(void) {
95 if (secp256k1_ecmult_gen_consts == NULL)
98 secp256k1_ecmult_gen_consts_t *c = (secp256k1_ecmult_gen_consts_t*)secp256k1_ecmult_gen_consts;
99 secp256k1_ecmult_gen_consts = NULL;
103 static void secp256k1_ecmult_gen(secp256k1_gej_t *r, const secp256k1_scalar_t *gn) {
104 const secp256k1_ecmult_gen_consts_t *c = secp256k1_ecmult_gen_consts;
105 secp256k1_gej_set_infinity(r);
109 for (int j=0; j<64; j++) {
110 bits = secp256k1_scalar_get_bits(gn, j * 4, 4);
111 for (int i=0; i<16; i++) {
112 secp256k1_fe_cmov(&add.x, &c->prec[j][i][0], i == bits);
113 secp256k1_fe_cmov(&add.y, &c->prec[j][i][1], i == bits);
115 secp256k1_gej_add_ge(r, r, &add);
118 secp256k1_ge_clear(&add);