7 #ifndef SECP256K1_ECMULT_GEN_IMPL_H 8 #define SECP256K1_ECMULT_GEN_IMPL_H 14 #ifdef USE_ECMULT_STATIC_PRECOMPUTATION 22 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION 29 if (ctx->
prec != NULL) {
32 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION 36 secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
40 static const unsigned char nums_b32[33] =
"The scalar for this x is unknown";
44 r = secp256k1_fe_set_b32(&nums_x, nums_b32);
47 r = secp256k1_ge_set_xo_var(&nums_ge, &nums_x, 0);
50 secp256k1_gej_set_ge(&nums_gej, &nums_ge);
52 secp256k1_gej_add_ge_var(&nums_gej, &nums_gej, &secp256k1_ge_const_g, NULL);
62 for (j = 0; j < 64; j++) {
64 precj[j*16] = numsbase;
65 for (i = 1; i < 16; i++) {
66 secp256k1_gej_add_var(&precj[j*16 + i], &precj[j*16 + i - 1], &gbase, NULL);
69 for (i = 0; i < 4; i++) {
70 secp256k1_gej_double_var(&gbase, &gbase, NULL);
73 secp256k1_gej_double_var(&numsbase, &numsbase, NULL);
76 secp256k1_gej_neg(&numsbase, &numsbase);
77 secp256k1_gej_add_var(&numsbase, &numsbase, &nums_gej, NULL);
80 secp256k1_ge_set_all_gej_var(prec, precj, 1024, cb);
82 for (j = 0; j < 64; j++) {
83 for (i = 0; i < 16; i++) {
84 secp256k1_ge_to_storage(&(*ctx->
prec)[j][i], &prec[j*16 + i]);
91 secp256k1_ecmult_gen_blind(ctx, NULL);
95 return ctx->
prec != NULL;
100 if (src->
prec == NULL) {
103 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION 116 #ifndef USE_ECMULT_STATIC_PRECOMPUTATION 119 secp256k1_scalar_clear(&ctx->
blind);
120 secp256k1_gej_clear(&ctx->
initial);
130 memset(&adds, 0,
sizeof(adds));
133 secp256k1_scalar_add(&gnb, gn, &ctx->
blind);
135 for (j = 0; j < 64; j++) {
136 bits = secp256k1_scalar_get_bits(&gnb, j * 4, 4);
137 for (i = 0; i < 16; i++) {
148 secp256k1_ge_storage_cmov(&adds, &(*ctx->
prec)[j][i], i == bits);
150 secp256k1_ge_from_storage(&add, &adds);
151 secp256k1_gej_add_ge(r, r, &add);
154 secp256k1_ge_clear(&add);
155 secp256k1_scalar_clear(&gnb);
163 unsigned char nonce32[32];
166 unsigned char keydata[64] = {0};
167 if (seed32 == NULL) {
169 secp256k1_gej_set_ge(&ctx->
initial, &secp256k1_ge_const_g);
171 secp256k1_scalar_set_int(&ctx->
blind, 1);
174 secp256k1_scalar_get_b32(nonce32, &ctx->
blind);
179 memcpy(keydata, nonce32, 32);
180 if (seed32 != NULL) {
181 memcpy(keydata + 32, seed32, 32);
183 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
184 memset(keydata, 0,
sizeof(keydata));
187 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
188 retry = !secp256k1_fe_set_b32(&s, nonce32);
189 retry |= secp256k1_fe_is_zero(&s);
192 secp256k1_gej_rescale(&ctx->
initial, &s);
193 secp256k1_fe_clear(&s);
195 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
196 secp256k1_scalar_set_b32(&b, nonce32, &retry);
198 retry |= secp256k1_scalar_is_zero(&b);
200 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
201 memset(nonce32, 0, 32);
202 secp256k1_ecmult_gen(ctx, &gb, &b);
203 secp256k1_scalar_negate(&b, &b);
206 secp256k1_scalar_clear(&b);
207 secp256k1_gej_clear(&gb);
#define VERIFY_CHECK(cond)
A group element of the secp256k1 curve, in jacobian coordinates.
A group element of the secp256k1 curve, in affine coordinates.
A scalar modulo the group order of the secp256k1 curve.
void * memcpy(void *a, const void *b, size_t c)
secp256k1_ge_storage(* prec)[64][16]