12
12
/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
13
13
* bytes) changes the hash function
14
14
*/
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 ) {
16
16
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 );
18
18
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 );
20
20
CHECK (secp256k1_memcmp_var (nonces [0 ], nonces [1 ], 32 ) != 0 );
21
21
}
22
22
@@ -34,7 +34,8 @@ void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256 *sha2)
34
34
void run_nonce_function_bip340_tests (void ) {
35
35
unsigned char tag [13 ] = "BIP0340/nonce" ;
36
36
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 );
38
39
secp256k1_sha256 sha ;
39
40
secp256k1_sha256 sha_optimized ;
40
41
unsigned char nonce [32 ];
@@ -68,33 +69,37 @@ void run_nonce_function_bip340_tests(void) {
68
69
args [0 ] = msg ;
69
70
args [1 ] = key ;
70
71
args [2 ] = pk ;
71
- args [3 ] = algo16 ;
72
+ args [3 ] = algo ;
72
73
args [4 ] = aux_rand ;
73
74
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 );
82
83
}
83
84
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
+ }
95
100
96
101
/* 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 );
98
103
}
99
104
100
105
void test_schnorrsig_api (void ) {
@@ -634,34 +639,37 @@ void test_schnorrsig_bip_vectors(void) {
634
639
}
635
640
636
641
/* 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 ) {
638
643
(void ) msg32 ;
639
644
(void ) key32 ;
640
645
(void ) xonly_pk32 ;
641
- (void ) algo16 ;
646
+ (void ) algo ;
647
+ (void ) algolen ;
642
648
(void ) data ;
643
649
(void ) nonce32 ;
644
650
return 0 ;
645
651
}
646
652
647
653
/* 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 ) {
649
655
(void ) msg32 ;
650
656
(void ) key32 ;
651
657
(void ) xonly_pk32 ;
652
- (void ) algo16 ;
658
+ (void ) algo ;
659
+ (void ) algolen ;
653
660
(void ) data ;
654
661
655
662
memset (nonce32 , 0 , 32 );
656
663
return 1 ;
657
664
}
658
665
659
666
/* 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 ) {
661
668
(void ) msg32 ;
662
669
(void ) key32 ;
663
670
(void ) xonly_pk32 ;
664
- (void ) algo16 ;
671
+ (void ) algo ;
672
+ (void ) algolen ;
665
673
(void ) data ;
666
674
667
675
memset (nonce32 , 0xFF , 32 );
0 commit comments