1212/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
1313 * bytes) changes the hash function
1414 */
15- void nonce_function_bip340_bitflip (unsigned char * * args , size_t n_flip , size_t n_bytes ) {
15+ void nonce_function_bip340_bitflip (unsigned char * * args , size_t n_flip , size_t n_bytes , size_t algolen ) {
1616 unsigned char nonces [2 ][32 ];
17- CHECK (nonce_function_bip340 (nonces [0 ], args [0 ], args [1 ], args [2 ], args [3 ], args [4 ]) == 1 );
17+ CHECK (nonce_function_bip340 (nonces [0 ], args [0 ], args [1 ], args [2 ], args [3 ], algolen , args [4 ]) == 1 );
1818 secp256k1_testrand_flip (args [n_flip ], n_bytes );
19- CHECK (nonce_function_bip340 (nonces [1 ], args [0 ], args [1 ], args [2 ], args [3 ], args [4 ]) == 1 );
19+ CHECK (nonce_function_bip340 (nonces [1 ], args [0 ], args [1 ], args [2 ], args [3 ], algolen , args [4 ]) == 1 );
2020 CHECK (secp256k1_memcmp_var (nonces [0 ], nonces [1 ], 32 ) != 0 );
2121}
2222
@@ -34,7 +34,8 @@ void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256 *sha2)
3434void run_nonce_function_bip340_tests (void ) {
3535 unsigned char tag [13 ] = "BIP0340/nonce" ;
3636 unsigned char aux_tag [11 ] = "BIP0340/aux" ;
37- unsigned char algo16 [16 ] = "BIP0340/nonce\0\0\0" ;
37+ unsigned char algo [13 ] = "BIP0340/nonce" ;
38+ size_t algolen = sizeof (algo );
3839 secp256k1_sha256 sha ;
3940 secp256k1_sha256 sha_optimized ;
4041 unsigned char nonce [32 ];
@@ -68,33 +69,37 @@ void run_nonce_function_bip340_tests(void) {
6869 args [0 ] = msg ;
6970 args [1 ] = key ;
7071 args [2 ] = pk ;
71- args [3 ] = algo16 ;
72+ args [3 ] = algo ;
7273 args [4 ] = aux_rand ;
7374 for (i = 0 ; i < count ; i ++ ) {
74- nonce_function_bip340_bitflip (args , 0 , 32 );
75- nonce_function_bip340_bitflip (args , 1 , 32 );
76- nonce_function_bip340_bitflip (args , 2 , 32 );
77- /* Flip algo16 special case "BIP0340/nonce" */
78- nonce_function_bip340_bitflip (args , 3 , 16 );
79- /* Flip algo16 again */
80- nonce_function_bip340_bitflip (args , 3 , 16 );
81- nonce_function_bip340_bitflip (args , 4 , 32 );
75+ nonce_function_bip340_bitflip (args , 0 , 32 , algolen );
76+ nonce_function_bip340_bitflip (args , 1 , 32 , algolen );
77+ nonce_function_bip340_bitflip (args , 2 , 32 , algolen );
78+ /* Flip algo special case "BIP0340/nonce" */
79+ nonce_function_bip340_bitflip (args , 3 , algolen , algolen );
80+ /* Flip algo again */
81+ nonce_function_bip340_bitflip (args , 3 , algolen , algolen );
82+ nonce_function_bip340_bitflip (args , 4 , 32 , algolen );
8283 }
8384
84- /* NULL algo16 is disallowed */
85- CHECK (nonce_function_bip340 (nonce , msg , key , pk , NULL , NULL ) == 0 );
86- /* Empty algo16 is fine */
87- memset (algo16 , 0x00 , 16 );
88- CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo16 , NULL ) == 1 );
89- /* algo16 with terminating null bytes is fine */
90- algo16 [1 ] = 65 ;
91- CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo16 , NULL ) == 1 );
92- /* Other algo16 is fine */
93- memset (algo16 , 0xFF , 16 );
94- CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo16 , NULL ) == 1 );
85+ /* NULL algo is disallowed */
86+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , NULL , 0 , NULL ) == 0 );
87+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo , algolen , NULL ) == 1 );
88+ /* Other algo is fine */
89+ secp256k1_rfc6979_hmac_sha256_generate (& secp256k1_test_rng , algo , algolen );
90+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo , algolen , NULL ) == 1 );
91+
92+ for (i = 0 ; i < count ; i ++ ) {
93+ unsigned char nonce2 [32 ];
94+ /* Different algolen gives different nonce */
95+ uint32_t offset = secp256k1_testrand_int (algolen - 1 );
96+ size_t algolen_tmp = (algolen + offset ) % algolen ;
97+ CHECK (nonce_function_bip340 (nonce2 , msg , key , pk , algo , algolen_tmp , NULL ) == 1 );
98+ CHECK (secp256k1_memcmp_var (nonce , nonce2 , 32 ) != 0 );
99+ }
95100
96101 /* NULL aux_rand argument is allowed. */
97- CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo16 , NULL ) == 1 );
102+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo , algolen , NULL ) == 1 );
98103}
99104
100105void test_schnorrsig_api (void ) {
@@ -634,34 +639,37 @@ void test_schnorrsig_bip_vectors(void) {
634639}
635640
636641/* Nonce function that returns constant 0 */
637- static int nonce_function_failing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * xonly_pk32 , const unsigned char * algo16 , void * data ) {
642+ static int nonce_function_failing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * xonly_pk32 , const unsigned char * algo , size_t algolen , void * data ) {
638643 (void ) msg32 ;
639644 (void ) key32 ;
640645 (void ) xonly_pk32 ;
641- (void ) algo16 ;
646+ (void ) algo ;
647+ (void ) algolen ;
642648 (void ) data ;
643649 (void ) nonce32 ;
644650 return 0 ;
645651}
646652
647653/* Nonce function that sets nonce to 0 */
648- static int nonce_function_0 (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * xonly_pk32 , const unsigned char * algo16 , void * data ) {
654+ static int nonce_function_0 (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * xonly_pk32 , const unsigned char * algo , size_t algolen , void * data ) {
649655 (void ) msg32 ;
650656 (void ) key32 ;
651657 (void ) xonly_pk32 ;
652- (void ) algo16 ;
658+ (void ) algo ;
659+ (void ) algolen ;
653660 (void ) data ;
654661
655662 memset (nonce32 , 0 , 32 );
656663 return 1 ;
657664}
658665
659666/* Nonce function that sets nonce to 0xFF...0xFF */
660- static int nonce_function_overflowing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * xonly_pk32 , const unsigned char * algo16 , void * data ) {
667+ static int nonce_function_overflowing (unsigned char * nonce32 , const unsigned char * msg32 , const unsigned char * key32 , const unsigned char * xonly_pk32 , const unsigned char * algo , size_t algolen , void * data ) {
661668 (void ) msg32 ;
662669 (void ) key32 ;
663670 (void ) xonly_pk32 ;
664- (void ) algo16 ;
671+ (void ) algo ;
672+ (void ) algolen ;
665673 (void ) data ;
666674
667675 memset (nonce32 , 0xFF , 32 );
0 commit comments