Skip to content

Commit 0a40a48

Browse files
Merge #1049: Faster fixed-input ecmult tests
070e772 Faster fixed-input ecmult tests (Pieter Wuille) Pull request description: Given how much #920 slowed down the tests with low iteration count, replace it with 3 different similar test: * count >= 1: a test with 1024 multiplies that tests any pattern of 6 bits in windows not more than 20 bits wide * count >= 3: a test with 2048 multiplies that tests any pattern of 8 consecutive bits * count >= 35: the old test (which effectively tests all 2-bit patterns) ACKs for top commit: robot-dreams: ACK 070e772, the addition of the `CONDITIONAL_TEST` macro is nice. real-or-random: ACK 070e772 Tree-SHA512: b4ccca42c71fcd1baa7143f73d1c3ac9d012c296485164a03341dbeee02e4ba9f7c7ad6b441923a5fe0286c97eff60815033adb4e1d30b3ef08bcb79590327ff
2 parents c8aa516 + 070e772 commit 0a40a48

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

@@ -4752,8 +4754,8 @@ void test_ecmult_accumulate(secp256k1_sha256* acc, const secp256k1_scalar* x, se
47524754
}
47534755
}
47544756

4755-
void test_ecmult_constants(void) {
4756-
/* Test ecmult_gen for:
4757+
void test_ecmult_constants_2bit(void) {
4758+
/* Using test_ecmult_accumulate, test ecmult for:
47574759
* - For i in 0..36:
47584760
* - Key i
47594761
* - Key -i
@@ -4796,8 +4798,81 @@ void test_ecmult_constants(void) {
47964798
secp256k1_scratch_space_destroy(ctx, scratch);
47974799
}
47984800

4801+
void test_ecmult_constants_sha(uint32_t prefix, size_t iter, const unsigned char* expected32) {
4802+
/* Using test_ecmult_accumulate, test ecmult for:
4803+
* - Key 0
4804+
* - Key 1
4805+
* - Key -1
4806+
* - For i in range(iter):
4807+
* - Key SHA256(LE32(prefix) || LE16(i))
4808+
*/
4809+
secp256k1_scalar x;
4810+
secp256k1_sha256 acc;
4811+
unsigned char b32[32];
4812+
unsigned char inp[6];
4813+
size_t i;
4814+
secp256k1_scratch_space *scratch = secp256k1_scratch_space_create(ctx, 65536);
4815+
4816+
inp[0] = prefix & 0xFF;
4817+
inp[1] = (prefix >> 8) & 0xFF;
4818+
inp[2] = (prefix >> 16) & 0xFF;
4819+
inp[3] = (prefix >> 24) & 0xFF;
4820+
secp256k1_sha256_initialize(&acc);
4821+
secp256k1_scalar_set_int(&x, 0);
4822+
test_ecmult_accumulate(&acc, &x, scratch);
4823+
secp256k1_scalar_set_int(&x, 1);
4824+
test_ecmult_accumulate(&acc, &x, scratch);
4825+
secp256k1_scalar_negate(&x, &x);
4826+
test_ecmult_accumulate(&acc, &x, scratch);
4827+
4828+
for (i = 0; i < iter; ++i) {
4829+
secp256k1_sha256 gen;
4830+
inp[4] = i & 0xff;
4831+
inp[5] = (i >> 8) & 0xff;
4832+
secp256k1_sha256_initialize(&gen);
4833+
secp256k1_sha256_write(&gen, inp, sizeof(inp));
4834+
secp256k1_sha256_finalize(&gen, b32);
4835+
secp256k1_scalar_set_b32(&x, b32, NULL);
4836+
test_ecmult_accumulate(&acc, &x, scratch);
4837+
}
4838+
secp256k1_sha256_finalize(&acc, b32);
4839+
CHECK(secp256k1_memcmp_var(b32, expected32, 32) == 0);
4840+
4841+
secp256k1_scratch_space_destroy(ctx, scratch);
4842+
}
4843+
47994844
void run_ecmult_constants(void) {
4800-
test_ecmult_constants();
4845+
/* Expected hashes of all points in the tests below. Computed using an
4846+
* independent implementation. */
4847+
static const unsigned char expected32_6bit20[32] = {
4848+
0x68, 0xb6, 0xed, 0x6f, 0x28, 0xca, 0xc9, 0x7f,
4849+
0x8e, 0x8b, 0xd6, 0xc0, 0x61, 0x79, 0x34, 0x6e,
4850+
0x5a, 0x8f, 0x2b, 0xbc, 0x3e, 0x1f, 0xc5, 0x2e,
4851+
0x2a, 0xd0, 0x45, 0x67, 0x7f, 0x95, 0x95, 0x8e
4852+
};
4853+
static const unsigned char expected32_8bit8[32] = {
4854+
0x8b, 0x65, 0x8e, 0xea, 0x86, 0xae, 0x3c, 0x95,
4855+
0x90, 0xb6, 0x77, 0xa4, 0x8c, 0x76, 0xd9, 0xec,
4856+
0xf5, 0xab, 0x8a, 0x2f, 0xfd, 0xdb, 0x19, 0x12,
4857+
0x1a, 0xee, 0xe6, 0xb7, 0x6e, 0x05, 0x3f, 0xc6
4858+
};
4859+
/* For every combination of 6 bit positions out of 256, restricted to
4860+
* 20-bit windows (i.e., the first and last bit position are no more than
4861+
* 19 bits apart), all 64 bit patterns occur in the input scalars used in
4862+
* this test. */
4863+
CONDITIONAL_TEST(1, "test_ecmult_constants_sha 1024") {
4864+
test_ecmult_constants_sha(4808378u, 1024, expected32_6bit20);
4865+
}
4866+
4867+
/* For every combination of 8 consecutive bit positions, all 256 bit
4868+
* patterns occur in the input scalars used in this test. */
4869+
CONDITIONAL_TEST(3, "test_ecmult_constants_sha 2048") {
4870+
test_ecmult_constants_sha(1607366309u, 2048, expected32_8bit8);
4871+
}
4872+
4873+
CONDITIONAL_TEST(35, "test_ecmult_constants_2bit") {
4874+
test_ecmult_constants_2bit();
4875+
}
48014876
}
48024877

48034878
void test_ecmult_gen_blind(void) {

0 commit comments

Comments
 (0)