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