Skip to content

Commit a8cfaf7

Browse files
committed
Test ecmult_gen with all 2^i+2^j
1 parent b3039a8 commit a8cfaf7

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

src/tests.c

+50-25
Original file line numberDiff line numberDiff line change
@@ -3690,37 +3690,62 @@ 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, serialize it uncompressed, and feed it into acc. */
3695+
secp256k1_gej rj;
3696+
secp256k1_ge r;
3697+
unsigned char bytes[65];
3698+
size_t size = 65;
3699+
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &rj, x);
3700+
secp256k1_ge_set_gej_var(&r, &rj);
3701+
secp256k1_eckey_pubkey_serialize(&r, bytes, &size, 0);
3702+
CHECK(size == 65);
3703+
secp256k1_sha256_write(acc, bytes, size);
3704+
}
3705+
36933706
void test_ecmult_constants(void) {
3694-
/* Test ecmult_gen() for [0..36) and [order-36..0). */
3707+
/* Test ecmult_gen for:
3708+
* - Numbers 1..35
3709+
* - Numbers -1..-35
3710+
* - Numbers 2^i (with i=0..255)
3711+
* - Numbers 2^i + 2^j (with i=0..255, j=i+1..255)
3712+
*/
36953713
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++ ) {
3714+
secp256k1_sha256 acc;
3715+
unsigned char b32[32];
3716+
int i, j;
3717+
/* Expected hash of all the computed points; created with an independent
3718+
* implementation. */
3719+
static const unsigned char expected32[32] = {
3720+
0xed, 0x49, 0x88, 0x52, 0x06, 0xb2, 0x26, 0x0f,
3721+
0x3f, 0xdc, 0x11, 0x9b, 0x8f, 0x56, 0xa1, 0x50,
3722+
0xbb, 0xc7, 0xe9, 0xaf, 0xd8, 0x7d, 0x18, 0x4f,
3723+
0xb3, 0x88, 0x57, 0xbe, 0xa3, 0x3c, 0xab, 0xdd
3724+
};
3725+
secp256k1_sha256_initialize(&acc);
3726+
for (i = 1; i < 36; ++i) {
37133727
secp256k1_scalar_set_int(&x, i);
3728+
test_ecmult_accumulate(&acc, &x);
37143729
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);
3730+
test_ecmult_accumulate(&acc, &x);
3731+
};
3732+
for (i = 0; i < 256; ++i) {
3733+
memset(b32, 0, 32);
3734+
b32[31 - (i >> 3)] = (1 << (i & 7));
3735+
secp256k1_scalar_set_b32(&x, b32, NULL);
3736+
test_ecmult_accumulate(&acc, &x);
3737+
}
3738+
for (i = 0; i < 256; ++i) {
3739+
for (j = i+1; j < 256; ++j) {
3740+
memset(b32, 0, 32);
3741+
b32[31 - (i >> 3)] = (1 << (i & 7));
3742+
b32[31 - (j >> 3)] |= (1 << (j & 7));
3743+
secp256k1_scalar_set_b32(&x, b32, NULL);
3744+
test_ecmult_accumulate(&acc, &x);
37213745
}
3722-
CHECK(secp256k1_gej_is_infinity(&r));
37233746
}
3747+
secp256k1_sha256_finalize(&acc, b32);
3748+
CHECK(secp256k1_memcmp_var(b32, expected32, 32) == 0);
37243749
}
37253750

37263751
void run_ecmult_constants(void) {

0 commit comments

Comments
 (0)