Skip to content

Commit ec6dff0

Browse files
committed
Test ecmult functions with all 2^i+2^j combinations
1 parent 1e5d50f commit ec6dff0

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

src/tests.c

+65-25
Original file line numberDiff line numberDiff line change
@@ -3690,37 +3690,77 @@ void run_wnaf(void) {
36903690
CHECK(secp256k1_scalar_is_zero(&n));
36913691
}
36923692

3693+
void test_ecmult_accumulate(secp256k1_sha256* acc, const secp256k1_scalar* x) {
3694+
/* Compute x*G in 4 different ways, serialize it uncompressed, and feed it into acc. */
3695+
secp256k1_gej rj1, rj2, rj3, rj4, gj, infj;
3696+
secp256k1_ge r;
3697+
const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
3698+
unsigned char bytes[65];
3699+
size_t size = 65;
3700+
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
3701+
secp256k1_gej_set_infinity(&infj);
3702+
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &rj1, x);
3703+
secp256k1_ecmult(&ctx->ecmult_ctx, &rj2, &gj, x, &zero);
3704+
secp256k1_ecmult(&ctx->ecmult_ctx, &rj3, &infj, &zero, x);
3705+
secp256k1_ecmult_const(&rj4, &secp256k1_ge_const_g, x, 256);
3706+
secp256k1_ge_set_gej_var(&r, &rj1);
3707+
ge_equals_gej(&r, &rj2);
3708+
ge_equals_gej(&r, &rj3);
3709+
ge_equals_gej(&r, &rj4);
3710+
if (secp256k1_ge_is_infinity(&r)) {
3711+
/* Store infinity as 0x00 */
3712+
const unsigned char zerobyte[1] = {0};
3713+
secp256k1_sha256_write(acc, zerobyte, 1);
3714+
} else {
3715+
/* Store other points using their uncompressed serialization. */
3716+
secp256k1_eckey_pubkey_serialize(&r, bytes, &size, 0);
3717+
CHECK(size == 65);
3718+
secp256k1_sha256_write(acc, bytes, size);
3719+
}
3720+
}
3721+
36933722
void test_ecmult_constants(void) {
3694-
/* Test ecmult_gen() for [0..36) and [order-36..0). */
3723+
/* Test ecmult_gen for:
3724+
* - Numbers 0..36 and their negations
3725+
* - Numbers 2^i (with i=0..255)
3726+
* - Numbers 2^i + 2^j (with i=0..255, j=i+1..255)
3727+
*/
36953728
secp256k1_scalar x;
3696-
secp256k1_gej r;
3697-
secp256k1_ge ng;
3698-
int i;
3699-
int j;
3700-
secp256k1_ge_neg(&ng, &secp256k1_ge_const_g);
3701-
for (i = 0; i < 36; i++ ) {
3702-
secp256k1_scalar_set_int(&x, i);
3703-
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &r, &x);
3704-
for (j = 0; j < i; j++) {
3705-
if (j == i - 1) {
3706-
ge_equals_gej(&secp256k1_ge_const_g, &r);
3707-
}
3708-
secp256k1_gej_add_ge(&r, &r, &ng);
3709-
}
3710-
CHECK(secp256k1_gej_is_infinity(&r));
3711-
}
3712-
for (i = 1; i <= 36; i++ ) {
3729+
secp256k1_sha256 acc;
3730+
unsigned char b32[32];
3731+
int i, j;
3732+
/* Expected hash of all the computed points; created with an independent
3733+
* implementation. */
3734+
static const unsigned char expected32[32] = {
3735+
0xf4, 0x8f, 0xe4, 0xea, 0xb8, 0x42, 0x43, 0x89,
3736+
0xb1, 0x8b, 0x92, 0x4d, 0xdb, 0x2d, 0x63, 0x3c,
3737+
0x10, 0x7d, 0x4a, 0x37, 0xff, 0x35, 0x42, 0x7f,
3738+
0x2e, 0x07, 0x1b, 0xec, 0xf0, 0x72, 0x15, 0xd9
3739+
};
3740+
secp256k1_sha256_initialize(&acc);
3741+
for (i = 0; i <= 36; ++i) {
37133742
secp256k1_scalar_set_int(&x, i);
3743+
test_ecmult_accumulate(&acc, &x);
37143744
secp256k1_scalar_negate(&x, &x);
3715-
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &r, &x);
3716-
for (j = 0; j < i; j++) {
3717-
if (j == i - 1) {
3718-
ge_equals_gej(&ng, &r);
3719-
}
3720-
secp256k1_gej_add_ge(&r, &r, &secp256k1_ge_const_g);
3745+
test_ecmult_accumulate(&acc, &x);
3746+
};
3747+
for (i = 0; i < 256; ++i) {
3748+
memset(b32, 0, 32);
3749+
b32[31 - (i >> 3)] = (1 << (i & 7));
3750+
secp256k1_scalar_set_b32(&x, b32, NULL);
3751+
test_ecmult_accumulate(&acc, &x);
3752+
}
3753+
for (i = 0; i < 256; ++i) {
3754+
for (j = i+1; j < 256; ++j) {
3755+
memset(b32, 0, 32);
3756+
b32[31 - (i >> 3)] = (1 << (i & 7));
3757+
b32[31 - (j >> 3)] |= (1 << (j & 7));
3758+
secp256k1_scalar_set_b32(&x, b32, NULL);
3759+
test_ecmult_accumulate(&acc, &x);
37213760
}
3722-
CHECK(secp256k1_gej_is_infinity(&r));
37233761
}
3762+
secp256k1_sha256_finalize(&acc, b32);
3763+
CHECK(secp256k1_memcmp_var(b32, expected32, 32) == 0);
37243764
}
37253765

37263766
void run_ecmult_constants(void) {

0 commit comments

Comments
 (0)