55
66#define MAX_TERMS 10
77
8+ /*
9+ todo: separate tests of batch_add_* from (create, destroy, verify)
10+ since, compiling libsecp with batch module and without
11+ schnorrsig, won't compile add functions. So, can't use them in tests
12+ */
813
914/* Tests for the equality of two sha256 structs. This function only produces a
1015 * correct result if an integer multiple of 64 many bytes have been written
1116 * into the hash functions. */
12- void batch_test_sha256_eq (const secp256k1_sha256 * sha1 , const secp256k1_sha256 * sha2 ) {
17+ void test_batch_sha256_eq (const secp256k1_sha256 * sha1 , const secp256k1_sha256 * sha2 ) {
1318 /* Is buffer fully consumed? */
1419 CHECK ((sha1 -> bytes & 0x3F ) == 0 );
1520
@@ -26,7 +31,67 @@ void test_batch_sha256_tagged(void) {
2631
2732 secp256k1_sha256_initialize_tagged (& sha , (unsigned char * ) tag , sizeof (tag ));
2833 secp256k1_batch_sha256_tagged (& sha_optimized );
29- batch_test_sha256_eq (& sha , & sha_optimized );
34+ test_batch_sha256_eq (& sha , & sha_optimized );
35+ }
36+
37+ /* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
38+ * bytes) changes the hash function
39+ */
40+ void batch_schnorrsig_randomizer_gen_bitflip (secp256k1_sha256 * sha , unsigned char * * args , size_t n_flip , size_t n_bytes , size_t msglen ) {
41+ unsigned char randomizers [2 ][32 ];
42+ secp256k1_sha256 sha_cpy ;
43+ sha_cpy = * sha ;
44+ CHECK (secp256k1_batch_schnorrsig_randomizer_gen (randomizers [0 ], & sha_cpy , args [0 ], args [1 ], msglen , args [2 ]) == 1 );
45+ secp256k1_testrand_flip (args [n_flip ], n_bytes );
46+ sha_cpy = * sha ;
47+ CHECK (secp256k1_batch_schnorrsig_randomizer_gen (randomizers [1 ], & sha_cpy , args [0 ], args [1 ], msglen , args [2 ]) == 1 );
48+ CHECK (secp256k1_memcmp_var (randomizers [0 ], randomizers [1 ], 32 ) != 0 );
49+ }
50+
51+ /*todo: make n_sigs var global macro? then, wouldn't it affects n_sigs api tests?*/
52+ void run_batch_schnorrsig_randomizer_gen_tests (void ) {
53+ secp256k1_sha256 sha ;
54+ size_t n_sigs = 20 ;
55+ unsigned char msg [32 ];
56+ size_t msglen = sizeof (msg [0 ]);
57+ unsigned char sig [64 ];
58+ unsigned char compressed_pk [33 ];
59+ unsigned char * args [3 ];
60+ uint8_t rand ;
61+ size_t i ; /* loops through n_sigs */
62+ int j ; /* loops through count */
63+
64+ secp256k1_batch_sha256_tagged (& sha );
65+
66+ for (i = 0 ; i < n_sigs ; i ++ ) {
67+ /* generate i-th schnorrsig verify data */
68+ secp256k1_testrand256 (msg );
69+ secp256k1_testrand256 (& sig [0 ]);
70+ secp256k1_testrand256 (& sig [32 ]);
71+ secp256k1_testrand256 (& compressed_pk [1 ]);
72+ rand = secp256k1_testrand_int (2 ) + 2 ; /* randomly choose 2 or 3 */
73+ compressed_pk [0 ] = (unsigned char )rand ;
74+
75+ /* check that bitflip in an argument results in different nonces */
76+ args [0 ] = sig ;
77+ args [1 ] = msg ;
78+ args [2 ] = compressed_pk ;
79+
80+ for (j = 0 ; j < count ; j ++ ) {
81+ batch_schnorrsig_randomizer_gen_bitflip (& sha , args , 0 , 64 , msglen );
82+ batch_schnorrsig_randomizer_gen_bitflip (& sha , args , 1 , 32 , msglen );
83+ batch_schnorrsig_randomizer_gen_bitflip (& sha , args , 2 , 33 , msglen );
84+ }
85+
86+ /* write i-th schnorrsig verify data to the sha object
87+ * this is required for generating the next randomizer */
88+ secp256k1_sha256_write (& sha , sig , 64 );
89+ secp256k1_sha256_write (& sha , msg , msglen );
90+ secp256k1_sha256_write (& sha , compressed_pk , 33 );
91+
92+ }
93+
94+ /* todo: msglen difference test?? */
3095}
3196
3297void test_batch_api (void ) {
@@ -113,6 +178,7 @@ void test_batch_api(void) {
113178void run_batch_tests (void ) {
114179 test_batch_api ();
115180 test_batch_sha256_tagged ();
181+ run_batch_schnorrsig_randomizer_gen_tests ();
116182}
117183
118184#endif /* SECP256K1_MODULE_BATCH_TESTS_H */
0 commit comments