2020-12-29 17:15:51 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2013, 2014, 2015 Pieter Wuille, Gregory Maxwell *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2015-10-26 14:54:21 +00:00
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
#ifndef SECP256K1_ECMULT_GEN_IMPL_H
|
|
|
|
#define SECP256K1_ECMULT_GEN_IMPL_H
|
2015-10-26 14:54:21 +00:00
|
|
|
|
2019-05-28 12:23:28 +00:00
|
|
|
#include "util.h"
|
2015-10-26 14:54:21 +00:00
|
|
|
#include "scalar.h"
|
|
|
|
#include "group.h"
|
|
|
|
#include "ecmult_gen.h"
|
|
|
|
#include "hash_impl.h"
|
2022-03-08 19:45:41 +00:00
|
|
|
#include "precomputed_ecmult_gen.h"
|
2019-05-28 12:23:28 +00:00
|
|
|
|
2022-10-08 15:43:43 +00:00
|
|
|
static void rustsecp256k1_v0_6_1_ecmult_gen_context_build(rustsecp256k1_v0_6_1_ecmult_gen_context *ctx) {
|
|
|
|
rustsecp256k1_v0_6_1_ecmult_gen_blind(ctx, NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
ctx->built = 1;
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 15:43:43 +00:00
|
|
|
static int rustsecp256k1_v0_6_1_ecmult_gen_context_is_built(const rustsecp256k1_v0_6_1_ecmult_gen_context* ctx) {
|
2022-03-08 19:45:41 +00:00
|
|
|
return ctx->built;
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 15:43:43 +00:00
|
|
|
static void rustsecp256k1_v0_6_1_ecmult_gen_context_clear(rustsecp256k1_v0_6_1_ecmult_gen_context *ctx) {
|
2022-03-08 19:45:41 +00:00
|
|
|
ctx->built = 0;
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_scalar_clear(&ctx->blind);
|
|
|
|
rustsecp256k1_v0_6_1_gej_clear(&ctx->initial);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
/* For accelerating the computation of a*G:
|
|
|
|
* To harden against timing attacks, use the following mechanism:
|
|
|
|
* * Break up the multiplicand into groups of PREC_BITS bits, called n_0, n_1, n_2, ..., n_(PREC_N-1).
|
|
|
|
* * Compute sum(n_i * (PREC_G)^i * G + U_i, i=0 ... PREC_N-1), where:
|
|
|
|
* * U_i = U * 2^i, for i=0 ... PREC_N-2
|
|
|
|
* * U_i = U * (1-2^(PREC_N-1)), for i=PREC_N-1
|
|
|
|
* where U is a point with no known corresponding scalar. Note that sum(U_i, i=0 ... PREC_N-1) = 0.
|
|
|
|
* For each i, and each of the PREC_G possible values of n_i, (n_i * (PREC_G)^i * G + U_i) is
|
|
|
|
* precomputed (call it prec(i, n_i)). The formula now becomes sum(prec(i, n_i), i=0 ... PREC_N-1).
|
|
|
|
* None of the resulting prec group elements have a known scalar, and neither do any of
|
|
|
|
* the intermediate sums while computing a*G.
|
2022-10-08 15:43:43 +00:00
|
|
|
* The prec values are stored in rustsecp256k1_v0_6_1_ecmult_gen_prec_table[i][n_i] = n_i * (PREC_G)^i * G + U_i.
|
2022-03-08 19:45:41 +00:00
|
|
|
*/
|
2022-10-08 15:43:43 +00:00
|
|
|
static void rustsecp256k1_v0_6_1_ecmult_gen(const rustsecp256k1_v0_6_1_ecmult_gen_context *ctx, rustsecp256k1_v0_6_1_gej *r, const rustsecp256k1_v0_6_1_scalar *gn) {
|
2022-03-08 19:45:41 +00:00
|
|
|
int bits = ECMULT_GEN_PREC_BITS;
|
|
|
|
int g = ECMULT_GEN_PREC_G(bits);
|
|
|
|
int n = ECMULT_GEN_PREC_N(bits);
|
2015-10-26 14:54:21 +00:00
|
|
|
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_ge add;
|
|
|
|
rustsecp256k1_v0_6_1_ge_storage adds;
|
|
|
|
rustsecp256k1_v0_6_1_scalar gnb;
|
2022-03-08 19:45:41 +00:00
|
|
|
int i, j, n_i;
|
|
|
|
|
2015-10-26 14:54:21 +00:00
|
|
|
memset(&adds, 0, sizeof(adds));
|
|
|
|
*r = ctx->initial;
|
|
|
|
/* Blind scalar/point multiplication by computing (n-b)G + bG instead of nG. */
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_scalar_add(&gnb, gn, &ctx->blind);
|
2015-10-26 14:54:21 +00:00
|
|
|
add.infinity = 0;
|
2022-03-08 19:45:41 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2022-10-08 15:43:43 +00:00
|
|
|
n_i = rustsecp256k1_v0_6_1_scalar_get_bits(&gnb, i * bits, bits);
|
2022-03-08 19:45:41 +00:00
|
|
|
for (j = 0; j < g; j++) {
|
2015-10-26 14:54:21 +00:00
|
|
|
/** This uses a conditional move to avoid any secret data in array indexes.
|
|
|
|
* _Any_ use of secret indexes has been demonstrated to result in timing
|
|
|
|
* sidechannels, even when the cache-line access patterns are uniform.
|
|
|
|
* See also:
|
|
|
|
* "A word of warning", CHES 2013 Rump Session, by Daniel J. Bernstein and Peter Schwabe
|
|
|
|
* (https://cryptojedi.org/peter/data/chesrump-20130822.pdf) and
|
|
|
|
* "Cache Attacks and Countermeasures: the Case of AES", RSA 2006,
|
|
|
|
* by Dag Arne Osvik, Adi Shamir, and Eran Tromer
|
2020-12-29 17:15:51 +00:00
|
|
|
* (https://www.tau.ac.il/~tromer/papers/cache.pdf)
|
2015-10-26 14:54:21 +00:00
|
|
|
*/
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_ge_storage_cmov(&adds, &rustsecp256k1_v0_6_1_ecmult_gen_prec_table[i][j], j == n_i);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_ge_from_storage(&add, &adds);
|
|
|
|
rustsecp256k1_v0_6_1_gej_add_ge(r, r, &add);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
n_i = 0;
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_ge_clear(&add);
|
|
|
|
rustsecp256k1_v0_6_1_scalar_clear(&gnb);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 15:43:43 +00:00
|
|
|
/* Setup blinding values for rustsecp256k1_v0_6_1_ecmult_gen. */
|
|
|
|
static void rustsecp256k1_v0_6_1_ecmult_gen_blind(rustsecp256k1_v0_6_1_ecmult_gen_context *ctx, const unsigned char *seed32) {
|
|
|
|
rustsecp256k1_v0_6_1_scalar b;
|
|
|
|
rustsecp256k1_v0_6_1_gej gb;
|
|
|
|
rustsecp256k1_v0_6_1_fe s;
|
2015-10-26 14:54:21 +00:00
|
|
|
unsigned char nonce32[32];
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_rfc6979_hmac_sha256 rng;
|
2020-08-26 17:35:27 +00:00
|
|
|
int overflow;
|
2015-10-26 14:54:21 +00:00
|
|
|
unsigned char keydata[64] = {0};
|
|
|
|
if (seed32 == NULL) {
|
|
|
|
/* When seed is NULL, reset the initial point and blinding value. */
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_gej_set_ge(&ctx->initial, &rustsecp256k1_v0_6_1_ge_const_g);
|
|
|
|
rustsecp256k1_v0_6_1_gej_neg(&ctx->initial, &ctx->initial);
|
|
|
|
rustsecp256k1_v0_6_1_scalar_set_int(&ctx->blind, 1);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
/* The prior blinding value (if not reset) is chained forward by including it in the hash. */
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_scalar_get_b32(nonce32, &ctx->blind);
|
2015-10-26 14:54:21 +00:00
|
|
|
/** Using a CSPRNG allows a failure free interface, avoids needing large amounts of random data,
|
|
|
|
* and guards against weak or adversarial seeds. This is a simpler and safer interface than
|
|
|
|
* asking the caller for blinding values directly and expecting them to retry on failure.
|
|
|
|
*/
|
|
|
|
memcpy(keydata, nonce32, 32);
|
|
|
|
if (seed32 != NULL) {
|
|
|
|
memcpy(keydata + 32, seed32, 32);
|
|
|
|
}
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
|
2015-10-26 14:54:21 +00:00
|
|
|
memset(keydata, 0, sizeof(keydata));
|
2020-08-26 17:35:27 +00:00
|
|
|
/* Accept unobservably small non-uniformity. */
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
|
|
|
|
overflow = !rustsecp256k1_v0_6_1_fe_set_b32(&s, nonce32);
|
|
|
|
overflow |= rustsecp256k1_v0_6_1_fe_is_zero(&s);
|
|
|
|
rustsecp256k1_v0_6_1_fe_cmov(&s, &rustsecp256k1_v0_6_1_fe_one, overflow);
|
2015-10-26 14:54:21 +00:00
|
|
|
/* Randomize the projection to defend against multiplier sidechannels. */
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_gej_rescale(&ctx->initial, &s);
|
|
|
|
rustsecp256k1_v0_6_1_fe_clear(&s);
|
|
|
|
rustsecp256k1_v0_6_1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
|
|
|
|
rustsecp256k1_v0_6_1_scalar_set_b32(&b, nonce32, NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
/* A blinding value of 0 works, but would undermine the projection hardening. */
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_scalar_cmov(&b, &rustsecp256k1_v0_6_1_scalar_one, rustsecp256k1_v0_6_1_scalar_is_zero(&b));
|
|
|
|
rustsecp256k1_v0_6_1_rfc6979_hmac_sha256_finalize(&rng);
|
2015-10-26 14:54:21 +00:00
|
|
|
memset(nonce32, 0, 32);
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_ecmult_gen(ctx, &gb, &b);
|
|
|
|
rustsecp256k1_v0_6_1_scalar_negate(&b, &b);
|
2015-10-26 14:54:21 +00:00
|
|
|
ctx->blind = b;
|
|
|
|
ctx->initial = gb;
|
2022-10-08 15:43:43 +00:00
|
|
|
rustsecp256k1_v0_6_1_scalar_clear(&b);
|
|
|
|
rustsecp256k1_v0_6_1_gej_clear(&gb);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
#endif /* SECP256K1_ECMULT_GEN_IMPL_H */
|