@@ -27,13 +27,16 @@ void test_schnorrsig_api(secp256k1_scratch_space *scratch) {
2727 unsigned char msg [32 ];
2828 unsigned char data32 [32 ];
2929 unsigned char s2c_data32 [32 ];
30+ unsigned char rand32 [32 ];
31+ unsigned char rand_commitment32 [32 ];
3032 unsigned char sig64 [64 ];
3133 secp256k1_pubkey pk [3 ];
3234 secp256k1_schnorrsig sig ;
3335 const secp256k1_schnorrsig * sigptr = & sig ;
3436 const unsigned char * msgptr = msg ;
3537 const secp256k1_pubkey * pkptr = & pk [0 ];
3638 secp256k1_s2c_opening s2c_opening ;
39+ secp256k1_pubkey client_commit ;
3740 unsigned char ones [32 ];
3841
3942 /** setup **/
@@ -124,6 +127,48 @@ void test_schnorrsig_api(secp256k1_scratch_space *scratch) {
124127 CHECK (ecount == 5 );
125128 }
126129
130+ secp256k1_rand256 (rand32 );
131+ ecount = 0 ;
132+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (none , rand_commitment32 , rand32 ) == 1 );
133+ CHECK (ecount == 0 );
134+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (none , NULL , rand32 ) == 0 );
135+ CHECK (ecount == 1 );
136+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (none , rand_commitment32 , NULL ) == 0 );
137+ CHECK (ecount == 2 );
138+
139+ ecount = 0 ;
140+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & client_commit , msg , sk1 , rand_commitment32 ) == 1 );
141+ CHECK (ecount == 0 );
142+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (none , & client_commit , msg , sk1 , rand_commitment32 ) == 0 );
143+ CHECK (ecount == 1 );
144+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , NULL , msg , sk1 , rand_commitment32 ) == 0 );
145+ CHECK (ecount == 2 );
146+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & client_commit , NULL , sk1 , rand_commitment32 ) == 0 );
147+ CHECK (ecount == 3 );
148+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & client_commit , msg , NULL , rand_commitment32 ) == 0 );
149+ CHECK (ecount == 4 );
150+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & client_commit , msg , sk1 , NULL ) == 0 );
151+ CHECK (ecount == 5 );
152+
153+ CHECK (secp256k1_schnorrsig_sign (sign , & sig , & s2c_opening , msg , sk1 , rand32 , NULL , NULL ) == 1 );
154+
155+ ecount = 0 ;
156+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (ctx , & sig , rand32 , & s2c_opening , & client_commit ) == 1 );
157+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (none , & sig , rand32 , & s2c_opening , & client_commit ) == 0 );
158+ CHECK (ecount == 1 );
159+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (sign , & sig , rand32 , & s2c_opening , & client_commit ) == 0 );
160+ CHECK (ecount == 2 );
161+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (vrfy , & sig , rand32 , & s2c_opening , & client_commit ) == 1 );
162+ CHECK (ecount == 2 );
163+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (vrfy , NULL , rand32 , & s2c_opening , & client_commit ) == 0 );
164+ CHECK (ecount == 3 );
165+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (vrfy , & sig , NULL , & s2c_opening , & client_commit ) == 0 );
166+ CHECK (ecount == 4 );
167+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (vrfy , & sig , rand32 , NULL , & client_commit ) == 0 );
168+ CHECK (ecount == 5 );
169+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (vrfy , & sig , rand32 , & s2c_opening , NULL ) == 0 );
170+ CHECK (ecount == 6 );
171+
127172 ecount = 0 ;
128173 CHECK (secp256k1_schnorrsig_verify (none , & sig , msg , & pk [0 ]) == 0 );
129174 CHECK (ecount == 1 );
@@ -807,6 +852,45 @@ void test_schnorrsig_s2c_commit_verify(void) {
807852 }
808853}
809854
855+ void test_schnorrsig_anti_nonce_sidechannel (void ) {
856+ unsigned char msg32 [32 ];
857+ unsigned char key32 [32 ];
858+ unsigned char rand32 [32 ];
859+ unsigned char rand_commitment32 [32 ];
860+ secp256k1_s2c_opening s2c_opening ;
861+ secp256k1_pubkey client_commit ;
862+ secp256k1_schnorrsig sig ;
863+
864+ secp256k1_rand256 (msg32 );
865+ secp256k1_rand256 (key32 );
866+ secp256k1_rand256 (rand32 );
867+
868+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (ctx , rand_commitment32 , rand32 ) == 1 );
869+
870+ /* Host sends rand_commitment32 to client. */
871+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (ctx , & client_commit , msg32 , key32 , rand_commitment32 ) == 1 );
872+ /* Client sends client_commit to host. Host replies with rand32. */
873+ CHECK (secp256k1_schnorrsig_sign (ctx , & sig , & s2c_opening , msg32 , key32 , rand32 , NULL , NULL ) == 1 );
874+ /* Client sends signature to host. */
875+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (ctx , & sig , rand32 , & s2c_opening , & client_commit ) == 1 );
876+
877+ {
878+ /* Signature without commitment to randomness fails verification */
879+ secp256k1_schnorrsig sig_tmp ;
880+ CHECK (secp256k1_schnorrsig_sign (ctx , & sig_tmp , NULL , msg32 , key32 , NULL , NULL , NULL ) == 1 );
881+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (ctx , & sig_tmp , rand32 , & s2c_opening , & client_commit ) == 0 );
882+ }
883+ {
884+ /* If sign-to-contract opening doesn't match commitment, verification fails */
885+ secp256k1_schnorrsig sig_tmp ;
886+ secp256k1_s2c_opening s2c_opening_tmp ;
887+ unsigned char rand32_tmp [32 ];
888+ secp256k1_rand256 (rand32_tmp );
889+ CHECK (secp256k1_schnorrsig_sign (ctx , & sig_tmp , & s2c_opening_tmp , msg32 , key32 , rand32_tmp , NULL , NULL ) == 1 );
890+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_verify (ctx , & sig_tmp , rand32 , & s2c_opening_tmp , & client_commit ) == 0 );
891+ }
892+ }
893+
810894void run_schnorrsig_tests (void ) {
811895 int i ;
812896 secp256k1_scratch_space * scratch = secp256k1_scratch_space_create (ctx , 1024 * 1024 );
@@ -821,6 +905,8 @@ void run_schnorrsig_tests(void) {
821905 * a test. */
822906 test_schnorrsig_s2c_commit_verify ();
823907 }
908+ test_schnorrsig_anti_nonce_sidechannel ();
909+
824910 secp256k1_scratch_space_destroy (scratch );
825911}
826912
0 commit comments