Skip to content

Commit 070e772

Browse files
committed
Faster fixed-input ecmult tests
1 parent b39d431 commit 070e772

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

src/tests.c

+78-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "modinv64_impl.h"
2929
#endif
3030

31+
#define CONDITIONAL_TEST(cnt, nam) if (count < (cnt)) { printf("Skipping %s (iteration count too low)\n", nam); } else
32+
3133
static int count = 64;
3234
static secp256k1_context *ctx = NULL;
3335

@@ -4712,8 +4714,8 @@ void test_ecmult_accumulate(secp256k1_sha256* acc, const secp256k1_scalar* x, se
47124714
}
47134715
}
47144716

4715-
void test_ecmult_constants(void) {
4716-
/* Test ecmult_gen for:
4717+
void test_ecmult_constants_2bit(void) {
4718+
/* Using test_ecmult_accumulate, test ecmult for:
47174719
* - For i in 0..36:
47184720
* - Key i
47194721
* - Key -i
@@ -4756,8 +4758,81 @@ void test_ecmult_constants(void) {
47564758
secp256k1_scratch_space_destroy(ctx, scratch);
47574759
}
47584760

4761+
void test_ecmult_constants_sha(uint32_t prefix, size_t iter, const unsigned char* expected32) {
4762+
/* Using test_ecmult_accumulate, test ecmult for:
4763+
* - Key 0
4764+
* - Key 1
4765+
* - Key -1
4766+
* - For i in range(iter):
4767+
* - Key SHA256(LE32(prefix) || LE16(i))
4768+
*/
4769+
secp256k1_scalar x;
4770+
secp256k1_sha256 acc;
4771+
unsigned char b32[32];
4772+
unsigned char inp[6];
4773+
size_t i;
4774+
secp256k1_scratch_space *scratch = secp256k1_scratch_space_create(ctx, 65536);
4775+
4776+
inp[0] = prefix & 0xFF;
4777+
inp[1] = (prefix >> 8) & 0xFF;
4778+
inp[2] = (prefix >> 16) & 0xFF;
4779+
inp[3] = (prefix >> 24) & 0xFF;
4780+
secp256k1_sha256_initialize(&acc);
4781+
secp256k1_scalar_set_int(&x, 0);
4782+
test_ecmult_accumulate(&acc, &x, scratch);
4783+
secp256k1_scalar_set_int(&x, 1);
4784+
test_ecmult_accumulate(&acc, &x, scratch);
4785+
secp256k1_scalar_negate(&x, &x);
4786+
test_ecmult_accumulate(&acc, &x, scratch);
4787+
4788+
for (i = 0; i < iter; ++i) {
4789+
secp256k1_sha256 gen;
4790+
inp[4] = i & 0xff;
4791+
inp[5] = (i >> 8) & 0xff;
4792+
secp256k1_sha256_initialize(&gen);
4793+
secp256k1_sha256_write(&gen, inp, sizeof(inp));
4794+
secp256k1_sha256_finalize(&gen, b32);
4795+
secp256k1_scalar_set_b32(&x, b32, NULL);
4796+
test_ecmult_accumulate(&acc, &x, scratch);
4797+
}
4798+
secp256k1_sha256_finalize(&acc, b32);
4799+
CHECK(secp256k1_memcmp_var(b32, expected32, 32) == 0);
4800+
4801+
secp256k1_scratch_space_destroy(ctx, scratch);
4802+
}
4803+
47594804
void run_ecmult_constants(void) {
4760-
test_ecmult_constants();
4805+
/* Expected hashes of all points in the tests below. Computed using an
4806+
* independent implementation. */
4807+
static const unsigned char expected32_6bit20[32] = {
4808+
0x68, 0xb6, 0xed, 0x6f, 0x28, 0xca, 0xc9, 0x7f,
4809+
0x8e, 0x8b, 0xd6, 0xc0, 0x61, 0x79, 0x34, 0x6e,
4810+
0x5a, 0x8f, 0x2b, 0xbc, 0x3e, 0x1f, 0xc5, 0x2e,
4811+
0x2a, 0xd0, 0x45, 0x67, 0x7f, 0x95, 0x95, 0x8e
4812+
};
4813+
static const unsigned char expected32_8bit8[32] = {
4814+
0x8b, 0x65, 0x8e, 0xea, 0x86, 0xae, 0x3c, 0x95,
4815+
0x90, 0xb6, 0x77, 0xa4, 0x8c, 0x76, 0xd9, 0xec,
4816+
0xf5, 0xab, 0x8a, 0x2f, 0xfd, 0xdb, 0x19, 0x12,
4817+
0x1a, 0xee, 0xe6, 0xb7, 0x6e, 0x05, 0x3f, 0xc6
4818+
};
4819+
/* For every combination of 6 bit positions out of 256, restricted to
4820+
* 20-bit windows (i.e., the first and last bit position are no more than
4821+
* 19 bits apart), all 64 bit patterns occur in the input scalars used in
4822+
* this test. */
4823+
CONDITIONAL_TEST(1, "test_ecmult_constants_sha 1024") {
4824+
test_ecmult_constants_sha(4808378u, 1024, expected32_6bit20);
4825+
}
4826+
4827+
/* For every combination of 8 consecutive bit positions, all 256 bit
4828+
* patterns occur in the input scalars used in this test. */
4829+
CONDITIONAL_TEST(3, "test_ecmult_constants_sha 2048") {
4830+
test_ecmult_constants_sha(1607366309u, 2048, expected32_8bit8);
4831+
}
4832+
4833+
CONDITIONAL_TEST(35, "test_ecmult_constants_2bit") {
4834+
test_ecmult_constants_2bit();
4835+
}
47614836
}
47624837

47634838
void test_ecmult_gen_blind(void) {

0 commit comments

Comments
 (0)