@@ -25,9 +25,12 @@ void test_schnorrsig_api(secp256k1_scratch_space *scratch) {
25
25
unsigned char sk2 [32 ];
26
26
unsigned char sk3 [32 ];
27
27
unsigned char msg [32 ];
28
+ unsigned char rand32 [32 ];
29
+ unsigned char rand_commitment32 [32 ];
28
30
unsigned char sig64 [64 ];
29
31
secp256k1_pubkey pk [3 ];
30
32
secp256k1_schnorrsig sig ;
33
+ secp256k1_s2c_commit_context s2c_ctx ;
31
34
const secp256k1_schnorrsig * sigptr = & sig ;
32
35
const unsigned char * msgptr = msg ;
33
36
const secp256k1_pubkey * pkptr = & pk [0 ];
@@ -88,6 +91,50 @@ void test_schnorrsig_api(secp256k1_scratch_space *scratch) {
88
91
CHECK (secp256k1_schnorrsig_parse (none , & sig , NULL ) == 0 );
89
92
CHECK (ecount == 4 );
90
93
94
+ secp256k1_rand256 (rand32 );
95
+ ecount = 0 ;
96
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (none , rand_commitment32 , rand32 ) == 1 );
97
+ CHECK (ecount == 0 );
98
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (none , NULL , rand32 ) == 0 );
99
+ CHECK (ecount == 1 );
100
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (none , rand_commitment32 , NULL ) == 0 );
101
+ CHECK (ecount == 2 );
102
+
103
+ ecount = 0 ;
104
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & s2c_ctx , msg , sk1 , rand_commitment32 ) == 1 );
105
+ CHECK (ecount == 0 );
106
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (none , & s2c_ctx , msg , sk1 , rand_commitment32 ) == 0 );
107
+ CHECK (ecount == 1 );
108
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , NULL , msg , sk1 , rand_commitment32 ) == 0 );
109
+ CHECK (ecount == 2 );
110
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & s2c_ctx , NULL , sk1 , rand_commitment32 ) == 0 );
111
+ CHECK (ecount == 3 );
112
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & s2c_ctx , msg , NULL , rand_commitment32 ) == 0 );
113
+ CHECK (ecount == 4 );
114
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (sign , & s2c_ctx , msg , sk1 , NULL ) == 0 );
115
+ CHECK (ecount == 5 );
116
+
117
+ ecount = 0 ;
118
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_setrand (none , & s2c_ctx , rand32 ) == 1 );
119
+ CHECK (ecount == 0 );
120
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_setrand (none , NULL , rand32 ) == 0 );
121
+ CHECK (ecount == 1 );
122
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_setrand (none , & s2c_ctx , NULL ) == 0 );
123
+ CHECK (ecount == 2 );
124
+
125
+ CHECK (secp256k1_schnorrsig_sign (sign , & sig , & negated_nonce , msg , sk1 , NULL , & s2c_ctx ) == 1 );
126
+ ecount = 0 ;
127
+ CHECK (secp256k1_schnorrsig_verify_s2c_commit (none , & sig , rand32 , & s2c_ctx .original_pubnonce , negated_nonce ) == 0 );
128
+ CHECK (ecount == 1 );
129
+ CHECK (secp256k1_schnorrsig_verify_s2c_commit (vrfy , & sig , rand32 , & s2c_ctx .original_pubnonce , negated_nonce ) == 1 );
130
+ CHECK (ecount == 1 );
131
+ CHECK (secp256k1_schnorrsig_verify_s2c_commit (vrfy , NULL , rand32 , & s2c_ctx .original_pubnonce , negated_nonce ) == 0 );
132
+ CHECK (ecount == 2 );
133
+ CHECK (secp256k1_schnorrsig_verify_s2c_commit (vrfy , & sig , NULL , & s2c_ctx .original_pubnonce , negated_nonce ) == 0 );
134
+ CHECK (ecount == 3 );
135
+ CHECK (secp256k1_schnorrsig_verify_s2c_commit (vrfy , & sig , rand32 , NULL , negated_nonce ) == 0 );
136
+ CHECK (ecount == 4 );
137
+
91
138
ecount = 0 ;
92
139
CHECK (secp256k1_schnorrsig_verify (none , & sig , msg , & pk [0 ]) == 0 );
93
140
CHECK (ecount == 1 );
@@ -656,6 +703,38 @@ void test_schnorrsig_sign_verify(secp256k1_scratch_space *scratch) {
656
703
}
657
704
#undef N_SIGS
658
705
706
+ void test_schnorrsig_anti_nonce_sidechannel (void ) {
707
+ unsigned char msg32 [32 ];
708
+ unsigned char key32 [32 ];
709
+ unsigned char algo16 [16 ];
710
+ unsigned char rand32 [32 ];
711
+ unsigned char rand_commitment32 [32 ];
712
+ secp256k1_s2c_commit_context s2c_ctx ;
713
+ secp256k1_pubkey s2c_opening ;
714
+ secp256k1_schnorrsig sig ;
715
+ int negated_nonce ;
716
+
717
+ secp256k1_rand256 (msg32 );
718
+ secp256k1_rand256 (key32 );
719
+ secp256k1_rand256 (rand32 );
720
+ memset (algo16 , 23 , sizeof (algo16 ));
721
+
722
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_host_commit (ctx , rand_commitment32 , rand32 ) == 1 );
723
+
724
+ /* Host sends rand_commitment32 to client. */
725
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_commit (ctx , & s2c_ctx , msg32 , key32 , rand_commitment32 ) == 1 );
726
+
727
+ /* Client sends s2c opening. Host replies with rand32. */
728
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_setrand (ctx , & s2c_ctx , rand32 ) == 1 );
729
+ /* Providing wrong data results in an error. */
730
+ CHECK (secp256k1_schnorrsig_anti_nonce_sidechan_client_setrand (ctx , & s2c_ctx , rand_commitment32 ) == 0 );
731
+ CHECK (secp256k1_s2c_commit_get_opening (ctx , & s2c_opening , & s2c_ctx ) == 1 );
732
+ CHECK (secp256k1_schnorrsig_sign (ctx , & sig , & negated_nonce , msg32 , key32 , NULL , & s2c_ctx ) == 1 );
733
+
734
+ /* Client sends signature to host. */
735
+ CHECK (secp256k1_schnorrsig_verify_s2c_commit (ctx , & sig , rand32 , & s2c_opening , negated_nonce ) == 1 );
736
+ }
737
+
659
738
void run_schnorrsig_tests (void ) {
660
739
secp256k1_scratch_space * scratch = secp256k1_scratch_space_create (ctx , 1024 * 1024 );
661
740
@@ -664,6 +743,7 @@ void run_schnorrsig_tests(void) {
664
743
test_schnorrsig_bip_vectors (scratch );
665
744
test_schnorrsig_sign ();
666
745
test_schnorrsig_sign_verify (scratch );
746
+ test_schnorrsig_anti_nonce_sidechannel ();
667
747
668
748
secp256k1_scratch_space_destroy (scratch );
669
749
}
0 commit comments