@@ -18,13 +18,38 @@ mod tests {
1818 use rand_chacha:: ChaCha20Rng ;
1919 use rand_core:: SeedableRng ;
2020
21- use crate :: { SchnorrSignature , signature_scheme:: SchnorrSignatureError } ;
21+ use crate :: {
22+ SchnorrSignature ,
23+ signature_scheme:: { PrimeOrderProjectivePoint , ScalarFieldElement , SchnorrSignatureError } ,
24+ } ;
2225
2326 use super :: { SchnorrSigningKey , SchnorrVerificationKey } ;
2427
2528 proptest ! {
2629 #![ proptest_config( ProptestConfig :: with_cases( 10 ) ) ]
2730
31+ #[ test]
32+ fn verification_key( seed in any:: <[ u8 ; 32 ] >( ) ) {
33+ // Valid generation check
34+ let sk = SchnorrSigningKey :: generate( & mut ChaCha20Rng :: from_seed( seed) ) . unwrap( ) ;
35+ let g = PrimeOrderProjectivePoint :: create_generator( ) ;
36+ let vk = g. scalar_multiplication( & sk. 0 ) ;
37+ let vk_from_sk = SchnorrVerificationKey :: new_from_signing_key( sk) . unwrap( ) ;
38+ assert_eq!( vk, vk_from_sk. 0 ) ;
39+
40+ // Check if sk is 0
41+ let mut bytes = [ 0u8 ; 32 ] ;
42+ let sk = SchnorrSigningKey ( ScalarFieldElement :: from_bytes( & bytes) . unwrap( ) ) ;
43+ SchnorrVerificationKey :: new_from_signing_key( sk)
44+ . expect_err( "Verification key should not be generated from zero signing key" ) ;
45+
46+ // Check if sk is 1
47+ bytes[ 0 ] = 1 ;
48+ let sk = SchnorrSigningKey ( ScalarFieldElement :: from_bytes( & bytes) . unwrap( ) ) ;
49+ SchnorrVerificationKey :: new_from_signing_key( sk)
50+ . expect_err( "Verification key should not be generated from one signing key" ) ;
51+ }
52+
2853 #[ test]
2954 fn valid_signing_verification(
3055 msg in prop:: collection:: vec( any:: <u8 >( ) , 1 ..128 ) ,
@@ -67,15 +92,27 @@ mod tests {
6792 fn signing_key_to_from_bytes( seed in any:: <[ u8 ; 32 ] >( ) ) {
6893 let mut rng = ChaCha20Rng :: from_seed( seed) ;
6994 let sk = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
70-
7195 let mut sk_bytes = sk. to_bytes( ) ;
96+
97+ // Valid conversion
7298 let recovered_sk = SchnorrSigningKey :: from_bytes( & sk_bytes) . unwrap( ) ;
7399 assert_eq!( sk. 0 , recovered_sk. 0 , "Recovered signing key does not match with the original!" ) ;
74100
75- sk_bytes[ 31 ] |= 0xff ;
101+ // Not enough bytes
102+ let mut short_bytes = [ 0u8 ; 31 ] ;
103+ short_bytes. copy_from_slice( sk_bytes. get( ..31 ) . unwrap( ) ) ;
104+ let result = SchnorrSigningKey :: from_bytes( & short_bytes) . expect_err( "From bytes conversion of signing key should fail" ) ;
105+ assert!(
106+ matches!(
107+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
108+ Some ( SchnorrSignatureError :: SerializationError )
109+ ) ,
110+ "Unexpected error: {result:?}"
111+ ) ;
76112
113+ // Invalid bytes
114+ sk_bytes[ 31 ] |= 0xff ;
77115 let result = SchnorrSigningKey :: from_bytes( & sk_bytes) . expect_err( "From bytes conversion of signing key should fail" ) ;
78-
79116 assert!(
80117 matches!(
81118 result. downcast_ref:: <SchnorrSignatureError >( ) ,
@@ -90,15 +127,27 @@ mod tests {
90127 let mut rng = ChaCha20Rng :: from_seed( seed) ;
91128 let sk = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
92129 let vk = SchnorrVerificationKey :: new_from_signing_key( sk. clone( ) ) . unwrap( ) ;
93-
94130 let mut vk_bytes = vk. to_bytes( ) ;
131+
132+ // Valid conversion
95133 let recovered_vk = SchnorrVerificationKey :: from_bytes( & vk_bytes) . unwrap( ) ;
96134 assert_eq!( vk. 0 , recovered_vk. 0 , "Recovered verification key does not match with the original!" ) ;
97135
98- vk_bytes[ 31 ] |= 0xff ;
136+ // Not enough bytes
137+ let mut short_bytes = [ 0u8 ; 31 ] ;
138+ short_bytes. copy_from_slice( vk_bytes. get( ..31 ) . unwrap( ) ) ;
139+ let result = SchnorrVerificationKey :: from_bytes( & short_bytes) . expect_err( "From bytes conversion of verification key should fail" ) ;
140+ assert!(
141+ matches!(
142+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
143+ Some ( SchnorrSignatureError :: SerializationError )
144+ ) ,
145+ "Unexpected error: {result:?}"
146+ ) ;
99147
148+ // Invalid bytes
149+ vk_bytes[ 31 ] |= 0xff ;
100150 let result = SchnorrVerificationKey :: from_bytes( & vk_bytes) . expect_err( "From bytes conversion of verification key should fail" ) ;
101-
102151 assert!(
103152 matches!(
104153 result. downcast_ref:: <SchnorrSignatureError >( ) ,
@@ -111,20 +160,18 @@ mod tests {
111160 #[ test]
112161 fn signature_to_from_bytes( msg in prop:: collection:: vec( any:: <u8 >( ) , 1 ..128 ) , seed in any:: <[ u8 ; 32 ] >( ) ) {
113162 let mut rng = ChaCha20Rng :: from_seed( seed) ;
114-
115163 let sk = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
116164 let signature = sk. sign( & msg, & mut ChaCha20Rng :: from_seed( seed) ) . unwrap( ) ;
117-
118165 let signature_bytes = signature. to_bytes( ) ;
166+
167+ // Valid conversion
119168 let recovered_signature = SchnorrSignature :: from_bytes( & signature_bytes) . unwrap( ) ;
120169 assert_eq!( signature, recovered_signature, "Recovered signature does not match with the original!" ) ;
121170
122171 // Test invalid `sigma`
123172 let mut corrupted_bytes = signature_bytes. clone( ) ;
124173 corrupted_bytes[ 31 ] |= 0xff ;
125-
126174 let result = SchnorrSignature :: from_bytes( & corrupted_bytes) . expect_err( "From bytes conversion of signature should fail" ) ;
127-
128175 assert!(
129176 matches!(
130177 result. downcast_ref:: <SchnorrSignatureError >( ) ,
@@ -136,29 +183,38 @@ mod tests {
136183 // Test invalid `signature`
137184 let mut corrupted_bytes = signature_bytes. clone( ) ;
138185 corrupted_bytes[ 63 ] |= 0xff ;
139-
140186 let result = SchnorrSignature :: from_bytes( & corrupted_bytes) . expect_err( "From bytes conversion should fail" ) ;
141-
142187 assert!(
143188 matches!(
144189 result. downcast_ref:: <SchnorrSignatureError >( ) ,
145190 Some ( SchnorrSignatureError :: ScalarFieldElementSerializationError )
146191 ) ,
147192 "Unexpected error: {result:?}"
148193 ) ;
194+
149195 // Test invalid `challenge`
150196 let mut corrupted_bytes = signature_bytes. clone( ) ;
151197 corrupted_bytes[ 95 ] |= 0xff ;
152-
153198 let result = SchnorrSignature :: from_bytes( & corrupted_bytes) . expect_err( "From bytes conversion should fail" ) ;
154-
155199 assert!(
156200 matches!(
157201 result. downcast_ref:: <SchnorrSignatureError >( ) ,
158202 Some ( SchnorrSignatureError :: ScalarFieldElementSerializationError )
159203 ) ,
160204 "Unexpected error: {result:?}"
161205 ) ;
206+
207+ // Not enough bytes
208+ let mut short_bytes = [ 0u8 ; 95 ] ;
209+ short_bytes. copy_from_slice( signature_bytes. get( ..95 ) . unwrap( ) ) ;
210+ let result = SchnorrSignature :: from_bytes( & short_bytes) . expect_err( "From bytes conversion of signature should fail" ) ;
211+ assert!(
212+ matches!(
213+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
214+ Some ( SchnorrSignatureError :: SerializationError )
215+ ) ,
216+ "Unexpected error: {result:?}"
217+ ) ;
162218 }
163219 }
164220}
0 commit comments