Skip to content

Commit 63a3565

Browse files
Merge #1120: ecmult_gen: Skip RNG when creating blinding if no seed is available
55f8bc9 ecmult_gen: Improve comments about projective blinding (Tim Ruffing) 7a86955 ecmult_gen: Simplify code (no observable change) (Tim Ruffing) 4cc0b1b ecmult_gen: Skip RNG when creating blinding if no seed is available (Tim Ruffing) Pull request description: Running the RNG is pointless if no seed is available because the key will be fixed. The computation just wastes time. Previously, users could avoid this computation at least by asking for a context without signing capabilities. But since 3b0c218 we always build an ecmult_gen context, ignoring the context flags. Moreover, users could never avoid this pointless computation when asking for the creation of a signing context. This fixes one item in #1065. ACKs for top commit: sipa: ACK 55f8bc9 apoelstra: ACK 55f8bc9 Tree-SHA512: 5ccba56041f94fa8f40a8a56ce505369ff2e0ed20cd7f0bfc3fdfffa5fa7bf826a93602b9b2455a352865a9548ab4928e858c19bb5af7ec221594a3bf25c4f3d
2 parents af65d30 + 55f8bc9 commit 63a3565

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/ecmult_gen_impl.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,31 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
8888
unsigned char nonce32[32];
8989
secp256k1_rfc6979_hmac_sha256 rng;
9090
int overflow;
91-
unsigned char keydata[64] = {0};
91+
unsigned char keydata[64];
9292
if (seed32 == NULL) {
9393
/* When seed is NULL, reset the initial point and blinding value. */
9494
secp256k1_gej_set_ge(&ctx->initial, &secp256k1_ge_const_g);
9595
secp256k1_gej_neg(&ctx->initial, &ctx->initial);
9696
secp256k1_scalar_set_int(&ctx->blind, 1);
97+
return;
9798
}
9899
/* The prior blinding value (if not reset) is chained forward by including it in the hash. */
99-
secp256k1_scalar_get_b32(nonce32, &ctx->blind);
100+
secp256k1_scalar_get_b32(keydata, &ctx->blind);
100101
/** Using a CSPRNG allows a failure free interface, avoids needing large amounts of random data,
101102
* and guards against weak or adversarial seeds. This is a simpler and safer interface than
102103
* asking the caller for blinding values directly and expecting them to retry on failure.
103104
*/
104-
memcpy(keydata, nonce32, 32);
105-
if (seed32 != NULL) {
106-
memcpy(keydata + 32, seed32, 32);
107-
}
108-
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
105+
VERIFY_CHECK(seed32 != NULL);
106+
memcpy(keydata + 32, seed32, 32);
107+
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, 64);
109108
memset(keydata, 0, sizeof(keydata));
110109
/* Accept unobservably small non-uniformity. */
111110
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
112111
overflow = !secp256k1_fe_set_b32(&s, nonce32);
113112
overflow |= secp256k1_fe_is_zero(&s);
114113
secp256k1_fe_cmov(&s, &secp256k1_fe_one, overflow);
115-
/* Randomize the projection to defend against multiplier sidechannels. */
114+
/* Randomize the projection to defend against multiplier sidechannels.
115+
Do this before our own call to secp256k1_ecmult_gen below. */
116116
secp256k1_gej_rescale(&ctx->initial, &s);
117117
secp256k1_fe_clear(&s);
118118
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
@@ -121,6 +121,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
121121
secp256k1_scalar_cmov(&b, &secp256k1_scalar_one, secp256k1_scalar_is_zero(&b));
122122
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
123123
memset(nonce32, 0, 32);
124+
/* The random projection in ctx->initial ensures that gb will have a random projection. */
124125
secp256k1_ecmult_gen(ctx, &gb, &b);
125126
secp256k1_scalar_negate(&b, &b);
126127
ctx->blind = b;

0 commit comments

Comments
 (0)