|
28 | 28 | #include "modinv64_impl.h"
|
29 | 29 | #endif
|
30 | 30 |
|
| 31 | +#define CONDITIONAL_TEST(cnt, nam) if (count < (cnt)) { printf("Skipping %s (iteration count too low)\n", nam); } else |
| 32 | + |
31 | 33 | static int count = 64;
|
32 | 34 | static secp256k1_context *ctx = NULL;
|
33 | 35 |
|
@@ -4752,8 +4754,8 @@ void test_ecmult_accumulate(secp256k1_sha256* acc, const secp256k1_scalar* x, se
|
4752 | 4754 | }
|
4753 | 4755 | }
|
4754 | 4756 |
|
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: |
4757 | 4759 | * - For i in 0..36:
|
4758 | 4760 | * - Key i
|
4759 | 4761 | * - Key -i
|
@@ -4796,8 +4798,81 @@ void test_ecmult_constants(void) {
|
4796 | 4798 | secp256k1_scratch_space_destroy(ctx, scratch);
|
4797 | 4799 | }
|
4798 | 4800 |
|
| 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 | + |
4799 | 4844 | 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 | + } |
4801 | 4876 | }
|
4802 | 4877 |
|
4803 | 4878 | void test_ecmult_gen_blind(void) {
|
|
0 commit comments