]> Git Repo - secp256k1.git/blob - src/ecmult_gen_impl.h
1188a21367683f3eb4ed75b8a811535353b5711d
[secp256k1.git] / src / ecmult_gen_impl.h
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  **********************************************************************/
6
7 #ifndef SECP256K1_ECMULT_GEN_IMPL_H
8 #define SECP256K1_ECMULT_GEN_IMPL_H
9
10 #include "util.h"
11 #include "scalar.h"
12 #include "group.h"
13 #include "ecmult_gen.h"
14 #include "hash_impl.h"
15 #ifdef USE_ECMULT_STATIC_PRECOMPUTATION
16 #include "ecmult_static_context.h"
17 #endif
18
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));
21 #else
22     static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = 0;
23 #endif
24
25 static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx) {
26     ctx->prec = NULL;
27 }
28
29 static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, const secp256k1_callback* cb) {
30 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
31     secp256k1_ge prec[1024];
32     secp256k1_gej gj;
33     secp256k1_gej nums_gej;
34     int i, j;
35 #endif
36
37     if (ctx->prec != NULL) {
38         return;
39     }
40 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
41     ctx->prec = (secp256k1_ge_storage (*)[64][16])checked_malloc(cb, sizeof(*ctx->prec));
42
43     /* get the generator */
44     secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
45
46     /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
47     {
48         static const unsigned char nums_b32[33] = "The scalar for this x is unknown";
49         secp256k1_fe nums_x;
50         secp256k1_ge nums_ge;
51         int r;
52         r = secp256k1_fe_set_b32(&nums_x, nums_b32);
53         (void)r;
54         VERIFY_CHECK(r);
55         r = secp256k1_ge_set_xo_var(&nums_ge, &nums_x, 0);
56         (void)r;
57         VERIFY_CHECK(r);
58         secp256k1_gej_set_ge(&nums_gej, &nums_ge);
59         /* Add G to make the bits in x uniformly distributed. */
60         secp256k1_gej_add_ge_var(&nums_gej, &nums_gej, &secp256k1_ge_const_g, NULL);
61     }
62
63     /* compute prec. */
64     {
65         secp256k1_gej precj[1024]; /* Jacobian versions of prec. */
66         secp256k1_gej gbase;
67         secp256k1_gej numsbase;
68         gbase = gj; /* 16^j * G */
69         numsbase = nums_gej; /* 2^j * nums. */
70         for (j = 0; j < 64; j++) {
71             /* Set precj[j*16 .. j*16+15] to (numsbase, numsbase + gbase, ..., numsbase + 15*gbase). */
72             precj[j*16] = numsbase;
73             for (i = 1; i < 16; i++) {
74                 secp256k1_gej_add_var(&precj[j*16 + i], &precj[j*16 + i - 1], &gbase, NULL);
75             }
76             /* Multiply gbase by 16. */
77             for (i = 0; i < 4; i++) {
78                 secp256k1_gej_double_var(&gbase, &gbase, NULL);
79             }
80             /* Multiply numbase by 2. */
81             secp256k1_gej_double_var(&numsbase, &numsbase, NULL);
82             if (j == 62) {
83                 /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
84                 secp256k1_gej_neg(&numsbase, &numsbase);
85                 secp256k1_gej_add_var(&numsbase, &numsbase, &nums_gej, NULL);
86             }
87         }
88         secp256k1_ge_set_all_gej_var(prec, precj, 1024);
89     }
90     for (j = 0; j < 64; j++) {
91         for (i = 0; i < 16; i++) {
92             secp256k1_ge_to_storage(&(*ctx->prec)[j][i], &prec[j*16 + i]);
93         }
94     }
95 #else
96     (void)cb;
97     ctx->prec = (secp256k1_ge_storage (*)[64][16])secp256k1_ecmult_static_context;
98 #endif
99     secp256k1_ecmult_gen_blind(ctx, NULL);
100 }
101
102 static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context* ctx) {
103     return ctx->prec != NULL;
104 }
105
106 static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context *dst,
107                                                const secp256k1_ecmult_gen_context *src, const secp256k1_callback* cb) {
108     if (src->prec == NULL) {
109         dst->prec = NULL;
110     } else {
111 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
112         dst->prec = (secp256k1_ge_storage (*)[64][16])checked_malloc(cb, sizeof(*dst->prec));
113         memcpy(dst->prec, src->prec, sizeof(*dst->prec));
114 #else
115         (void)cb;
116         dst->prec = src->prec;
117 #endif
118         dst->initial = src->initial;
119         dst->blind = src->blind;
120     }
121 }
122
123 static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx) {
124 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION
125     free(ctx->prec);
126 #endif
127     secp256k1_scalar_clear(&ctx->blind);
128     secp256k1_gej_clear(&ctx->initial);
129     ctx->prec = NULL;
130 }
131
132 static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *gn) {
133     secp256k1_ge add;
134     secp256k1_ge_storage adds;
135     secp256k1_scalar gnb;
136     int bits;
137     int i, j;
138     memset(&adds, 0, sizeof(adds));
139     *r = ctx->initial;
140     /* Blind scalar/point multiplication by computing (n-b)G + bG instead of nG. */
141     secp256k1_scalar_add(&gnb, gn, &ctx->blind);
142     add.infinity = 0;
143     for (j = 0; j < 64; j++) {
144         bits = secp256k1_scalar_get_bits(&gnb, j * 4, 4);
145         for (i = 0; i < 16; i++) {
146             /** This uses a conditional move to avoid any secret data in array indexes.
147              *   _Any_ use of secret indexes has been demonstrated to result in timing
148              *   sidechannels, even when the cache-line access patterns are uniform.
149              *  See also:
150              *   "A word of warning", CHES 2013 Rump Session, by Daniel J. Bernstein and Peter Schwabe
151              *    (https://cryptojedi.org/peter/data/chesrump-20130822.pdf) and
152              *   "Cache Attacks and Countermeasures: the Case of AES", RSA 2006,
153              *    by Dag Arne Osvik, Adi Shamir, and Eran Tromer
154              *    (http://www.tau.ac.il/~tromer/papers/cache.pdf)
155              */
156             secp256k1_ge_storage_cmov(&adds, &(*ctx->prec)[j][i], i == bits);
157         }
158         secp256k1_ge_from_storage(&add, &adds);
159         secp256k1_gej_add_ge(r, r, &add);
160     }
161     bits = 0;
162     secp256k1_ge_clear(&add);
163     secp256k1_scalar_clear(&gnb);
164 }
165
166 /* Setup blinding values for secp256k1_ecmult_gen. */
167 static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const unsigned char *seed32) {
168     secp256k1_scalar b;
169     secp256k1_gej gb;
170     secp256k1_fe s;
171     unsigned char nonce32[32];
172     secp256k1_rfc6979_hmac_sha256 rng;
173     int retry;
174     unsigned char keydata[64] = {0};
175     if (seed32 == NULL) {
176         /* When seed is NULL, reset the initial point and blinding value. */
177         secp256k1_gej_set_ge(&ctx->initial, &secp256k1_ge_const_g);
178         secp256k1_gej_neg(&ctx->initial, &ctx->initial);
179         secp256k1_scalar_set_int(&ctx->blind, 1);
180     }
181     /* The prior blinding value (if not reset) is chained forward by including it in the hash. */
182     secp256k1_scalar_get_b32(nonce32, &ctx->blind);
183     /** Using a CSPRNG allows a failure free interface, avoids needing large amounts of random data,
184      *   and guards against weak or adversarial seeds.  This is a simpler and safer interface than
185      *   asking the caller for blinding values directly and expecting them to retry on failure.
186      */
187     memcpy(keydata, nonce32, 32);
188     if (seed32 != NULL) {
189         memcpy(keydata + 32, seed32, 32);
190     }
191     secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
192     memset(keydata, 0, sizeof(keydata));
193     /* Retry for out of range results to achieve uniformity. */
194     do {
195         secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
196         retry = !secp256k1_fe_set_b32(&s, nonce32);
197         retry |= secp256k1_fe_is_zero(&s);
198     } while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > Fp. */
199     /* Randomize the projection to defend against multiplier sidechannels. */
200     secp256k1_gej_rescale(&ctx->initial, &s);
201     secp256k1_fe_clear(&s);
202     do {
203         secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
204         secp256k1_scalar_set_b32(&b, nonce32, &retry);
205         /* A blinding value of 0 works, but would undermine the projection hardening. */
206         retry |= secp256k1_scalar_is_zero(&b);
207     } while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > order. */
208     secp256k1_rfc6979_hmac_sha256_finalize(&rng);
209     memset(nonce32, 0, 32);
210     secp256k1_ecmult_gen(ctx, &gb, &b);
211     secp256k1_scalar_negate(&b, &b);
212     ctx->blind = b;
213     ctx->initial = gb;
214     secp256k1_scalar_clear(&b);
215     secp256k1_gej_clear(&gb);
216 }
217
218 #endif /* SECP256K1_ECMULT_GEN_IMPL_H */
This page took 0.029877 seconds and 2 git commands to generate.