1 /**********************************************************************
2 * Copyright (c) 2013, 2014, 2015 Pieter Wuille, Gregory Maxwell *
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
13 #include "ecmult_gen.h"
14 #include "hash_impl.h"
15 #ifdef USE_ECMULT_STATIC_PRECOMPUTATION
16 #include "ecmult_static_context.h"
19 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
20 static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = ROUND_TO_ALIGN(sizeof(*((secp256k1_ecmult_gen_context*) NULL)->prec));
22 static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = 0;
25 static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx) {
29 static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, void **prealloc) {
30 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
31 secp256k1_ge prec[ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G];
33 secp256k1_gej nums_gej;
35 size_t const prealloc_size = SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
36 void* const base = *prealloc;
39 if (ctx->prec != NULL) {
42 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
43 ctx->prec = (secp256k1_ge_storage (*)[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G])manual_alloc(prealloc, prealloc_size, base, prealloc_size);
45 /* get the generator */
46 secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
48 /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
50 static const unsigned char nums_b32[33] = "The scalar for this x is unknown";
54 r = secp256k1_fe_set_b32(&nums_x, nums_b32);
57 r = secp256k1_ge_set_xo_var(&nums_ge, &nums_x, 0);
60 secp256k1_gej_set_ge(&nums_gej, &nums_ge);
61 /* Add G to make the bits in x uniformly distributed. */
62 secp256k1_gej_add_ge_var(&nums_gej, &nums_gej, &secp256k1_ge_const_g, NULL);
67 secp256k1_gej precj[ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G]; /* Jacobian versions of prec. */
69 secp256k1_gej numsbase;
70 gbase = gj; /* PREC_G^j * G */
71 numsbase = nums_gej; /* 2^j * nums. */
72 for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
73 /* Set precj[j*PREC_G .. j*PREC_G+(PREC_G-1)] to (numsbase, numsbase + gbase, ..., numsbase + (PREC_G-1)*gbase). */
74 precj[j*ECMULT_GEN_PREC_G] = numsbase;
75 for (i = 1; i < ECMULT_GEN_PREC_G; i++) {
76 secp256k1_gej_add_var(&precj[j*ECMULT_GEN_PREC_G + i], &precj[j*ECMULT_GEN_PREC_G + i - 1], &gbase, NULL);
78 /* Multiply gbase by PREC_G. */
79 for (i = 0; i < ECMULT_GEN_PREC_B; i++) {
80 secp256k1_gej_double_var(&gbase, &gbase, NULL);
82 /* Multiply numbase by 2. */
83 secp256k1_gej_double_var(&numsbase, &numsbase, NULL);
84 if (j == ECMULT_GEN_PREC_N - 2) {
85 /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
86 secp256k1_gej_neg(&numsbase, &numsbase);
87 secp256k1_gej_add_var(&numsbase, &numsbase, &nums_gej, NULL);
90 secp256k1_ge_set_all_gej_var(prec, precj, ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G);
92 for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
93 for (i = 0; i < ECMULT_GEN_PREC_G; i++) {
94 secp256k1_ge_to_storage(&(*ctx->prec)[j][i], &prec[j*ECMULT_GEN_PREC_G + i]);
99 ctx->prec = (secp256k1_ge_storage (*)[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G])secp256k1_ecmult_static_context;
101 secp256k1_ecmult_gen_blind(ctx, NULL);
104 static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context* ctx) {
105 return ctx->prec != NULL;
108 static void secp256k1_ecmult_gen_context_finalize_memcpy(secp256k1_ecmult_gen_context *dst, const secp256k1_ecmult_gen_context *src) {
109 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
110 if (src->prec != NULL) {
111 /* We cast to void* first to suppress a -Wcast-align warning. */
112 dst->prec = (secp256k1_ge_storage (*)[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G])(void*)((unsigned char*)dst + ((unsigned char*)src->prec - (unsigned char*)src));
115 (void)dst, (void)src;
119 static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx) {
120 secp256k1_scalar_clear(&ctx->blind);
121 secp256k1_gej_clear(&ctx->initial);
125 static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *gn) {
127 secp256k1_ge_storage adds;
128 secp256k1_scalar gnb;
131 memset(&adds, 0, sizeof(adds));
133 /* Blind scalar/point multiplication by computing (n-b)G + bG instead of nG. */
134 secp256k1_scalar_add(&gnb, gn, &ctx->blind);
136 for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
137 bits = secp256k1_scalar_get_bits(&gnb, j * ECMULT_GEN_PREC_B, ECMULT_GEN_PREC_B);
138 for (i = 0; i < ECMULT_GEN_PREC_G; i++) {
139 /** This uses a conditional move to avoid any secret data in array indexes.
140 * _Any_ use of secret indexes has been demonstrated to result in timing
141 * sidechannels, even when the cache-line access patterns are uniform.
143 * "A word of warning", CHES 2013 Rump Session, by Daniel J. Bernstein and Peter Schwabe
144 * (https://cryptojedi.org/peter/data/chesrump-20130822.pdf) and
145 * "Cache Attacks and Countermeasures: the Case of AES", RSA 2006,
146 * by Dag Arne Osvik, Adi Shamir, and Eran Tromer
147 * (http://www.tau.ac.il/~tromer/papers/cache.pdf)
149 secp256k1_ge_storage_cmov(&adds, &(*ctx->prec)[j][i], i == bits);
151 secp256k1_ge_from_storage(&add, &adds);
152 secp256k1_gej_add_ge(r, r, &add);
155 secp256k1_ge_clear(&add);
156 secp256k1_scalar_clear(&gnb);
159 /* Setup blinding values for secp256k1_ecmult_gen. */
160 static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const unsigned char *seed32) {
164 unsigned char nonce32[32];
165 secp256k1_rfc6979_hmac_sha256 rng;
167 unsigned char keydata[64] = {0};
168 if (seed32 == NULL) {
169 /* When seed is NULL, reset the initial point and blinding value. */
170 secp256k1_gej_set_ge(&ctx->initial, &secp256k1_ge_const_g);
171 secp256k1_gej_neg(&ctx->initial, &ctx->initial);
172 secp256k1_scalar_set_int(&ctx->blind, 1);
174 /* The prior blinding value (if not reset) is chained forward by including it in the hash. */
175 secp256k1_scalar_get_b32(nonce32, &ctx->blind);
176 /** Using a CSPRNG allows a failure free interface, avoids needing large amounts of random data,
177 * and guards against weak or adversarial seeds. This is a simpler and safer interface than
178 * asking the caller for blinding values directly and expecting them to retry on failure.
180 memcpy(keydata, nonce32, 32);
181 if (seed32 != NULL) {
182 memcpy(keydata + 32, seed32, 32);
184 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
185 memset(keydata, 0, sizeof(keydata));
186 /* Retry for out of range results to achieve uniformity. */
188 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
189 retry = !secp256k1_fe_set_b32(&s, nonce32);
190 retry = retry || secp256k1_fe_is_zero(&s);
191 } while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > Fp. */
192 /* Randomize the projection to defend against multiplier sidechannels. */
193 secp256k1_gej_rescale(&ctx->initial, &s);
194 secp256k1_fe_clear(&s);
196 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
197 secp256k1_scalar_set_b32(&b, nonce32, &retry);
198 /* A blinding value of 0 works, but would undermine the projection hardening. */
199 retry = retry || secp256k1_scalar_is_zero(&b);
200 } while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > order. */
201 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
202 memset(nonce32, 0, 32);
203 secp256k1_ecmult_gen(ctx, &gb, &b);
204 secp256k1_scalar_negate(&b, &b);
207 secp256k1_scalar_clear(&b);
208 secp256k1_gej_clear(&gb);
211 #endif /* SECP256K1_ECMULT_GEN_IMPL_H */