@@ -443,6 +443,98 @@ void run_sha256_tests(void) {
443443 }
444444}
445445
446+ /* Tests for the equality of two sha256 structs. This function only produces a
447+ * correct result if an integer multiple of 64 many bytes have been written
448+ * into the hash functions. */
449+ void test_sha256_eq (secp256k1_sha256 * sha1 , secp256k1_sha256 * sha2 ) {
450+ unsigned char buf [32 ] = { 0 };
451+ unsigned char buf2 [32 ];
452+
453+ /* Is buffer fully consumed? */
454+ CHECK ((sha1 -> bytes & 0x3F ) == 0 );
455+
456+ /* Compare the struct excluding the buffer, because it may be
457+ * uninitialized or already included in the state. */
458+ CHECK (sha1 -> bytes == sha2 -> bytes );
459+ CHECK (memcmp (sha1 -> s , sha2 -> s , sizeof (sha1 -> s )) == 0 );
460+
461+ /* Compare the output */
462+ secp256k1_sha256_write (sha1 , buf , 32 );
463+ secp256k1_sha256_write (sha2 , buf , 32 );
464+ secp256k1_sha256_finalize (sha1 , buf );
465+ secp256k1_sha256_finalize (sha2 , buf2 );
466+ CHECK (memcmp (buf , buf2 , 32 ) == 0 );
467+ }
468+
469+ /* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
470+ * bytes) changes the hash function
471+ */
472+ void nonce_function_bip340_bitflip (unsigned char * * args , size_t n_flip , size_t n_bytes ) {
473+ unsigned char nonces [2 ][32 ];
474+ CHECK (nonce_function_bip340 (nonces [0 ], args [0 ], args [1 ], args [2 ], args [3 ], args [4 ], 0 ) == 1 );
475+ secp256k1_rand_flip (args [n_flip ], n_bytes );
476+ CHECK (nonce_function_bip340 (nonces [1 ], args [0 ], args [1 ], args [2 ], args [3 ], args [4 ], 0 ) == 1 );
477+ CHECK (memcmp (nonces [0 ], nonces [1 ], 32 ) != 0 );
478+ }
479+
480+ void run_nonce_function_bip340_tests (void ) {
481+ unsigned char tag [12 ] = "BIP340/nonce" ;
482+ unsigned char aux_tag [10 ] = "BIP340/aux" ;
483+ unsigned char algo16 [16 ] = "BIP340/nonce0000" ;
484+ secp256k1_sha256 sha ;
485+ secp256k1_sha256 sha_optimized ;
486+ unsigned char nonce [32 ];
487+ unsigned char msg [32 ];
488+ unsigned char key [32 ];
489+ unsigned char pk [32 ];
490+ unsigned char aux_rand [32 ];
491+ unsigned char * args [5 ];
492+
493+ /* Check that hash initialized by
494+ * secp256k1_nonce_function_bip340_sha256_tagged has the expected
495+ * state. */
496+ secp256k1_sha256_initialize_tagged (& sha , tag , sizeof (tag ));
497+ secp256k1_nonce_function_bip340_sha256_tagged (& sha_optimized );
498+ test_sha256_eq (& sha , & sha_optimized );
499+
500+ /* Check that hash initialized by
501+ * secp256k1_nonce_function_bip340_sha256_tagged_aux has the expected
502+ * state. */
503+ secp256k1_sha256_initialize_tagged (& sha , aux_tag , sizeof (aux_tag ));
504+ secp256k1_nonce_function_bip340_sha256_tagged_aux (& sha_optimized );
505+ test_sha256_eq (& sha , & sha_optimized );
506+
507+ secp256k1_rand256 (msg );
508+ secp256k1_rand256 (key );
509+ secp256k1_rand256 (pk );
510+ secp256k1_rand256 (aux_rand );
511+
512+ /* Check that a bitflip in an argument results in different nonces. */
513+ args [0 ] = msg ;
514+ args [1 ] = key ;
515+ args [2 ] = pk ;
516+ args [3 ] = algo16 ;
517+ args [4 ] = aux_rand ;
518+ nonce_function_bip340_bitflip (args , 0 , 32 );
519+ nonce_function_bip340_bitflip (args , 1 , 32 );
520+ nonce_function_bip340_bitflip (args , 2 , 32 );
521+ /* Flip algo16 special case "BIP340/nonce0000" */
522+ nonce_function_bip340_bitflip (args , 3 , 16 );
523+ /* Flip algo16 again */
524+ nonce_function_bip340_bitflip (args , 3 , 16 );
525+ nonce_function_bip340_bitflip (args , 4 , 32 );
526+
527+ /* NULL algo16 is disallowed */
528+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , NULL , NULL , 0 ) == 0 );
529+
530+ /* NULL aux_rand argument is allowed. */
531+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo16 , NULL , 0 ) == 1 );
532+
533+ /* Check that counter != 0 makes nonce function fail. */
534+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo16 , NULL , 0 ) == 1 );
535+ CHECK (nonce_function_bip340 (nonce , msg , key , pk , algo16 , NULL , 1 ) == 0 );
536+ }
537+
446538void run_hmac_sha256_tests (void ) {
447539 static const char * keys [6 ] = {
448540 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b" ,
0 commit comments