@@ -6,8 +6,159 @@ mod utils;
66mod verification_key;
77
88pub use error:: * ;
9- pub use jubjub_wrapper:: * ;
9+ pub ( crate ) use jubjub_wrapper:: * ;
1010pub use signature:: * ;
1111pub use signing_key:: * ;
1212pub ( crate ) use utils:: * ;
1313pub use verification_key:: * ;
14+
15+ #[ cfg( test) ]
16+ mod tests {
17+ use proptest:: prelude:: * ;
18+ use rand_chacha:: ChaCha20Rng ;
19+ use rand_core:: SeedableRng ;
20+
21+ use crate :: { SchnorrSignature , signature_scheme:: SchnorrSignatureError } ;
22+
23+ use super :: { SchnorrSigningKey , SchnorrVerificationKey } ;
24+
25+ proptest ! {
26+ #![ proptest_config( ProptestConfig :: with_cases( 10 ) ) ]
27+
28+ #[ test]
29+ fn valid_signing_verification(
30+ msg in prop:: collection:: vec( any:: <u8 >( ) , 1 ..128 ) ,
31+ seed in any:: <[ u8 ; 32 ] >( ) ,
32+ ) {
33+ let sk_result = SchnorrSigningKey :: generate( & mut ChaCha20Rng :: from_seed( seed) ) ;
34+ assert!( sk_result. is_ok( ) , "Secret ket generation failed" ) ;
35+ let sk = sk_result. unwrap( ) ;
36+
37+ let vk = SchnorrVerificationKey :: new_from_signing_key( sk. clone( ) ) . unwrap( ) ;
38+
39+ let sig_result = sk. sign( & msg, & mut ChaCha20Rng :: from_seed( seed) ) ;
40+ assert!( sig_result. is_ok( ) , "Signature generation failed" ) ;
41+
42+ let sig = sig_result. unwrap( ) ;
43+
44+ assert!( sig. verify( & msg, & vk) . is_ok( ) , "Verification failed." ) ;
45+ }
46+
47+ #[ test]
48+ fn invalid_signature( msg in prop:: collection:: vec( any:: <u8 >( ) , 1 ..128 ) , seed in any:: <[ u8 ; 32 ] >( ) ) {
49+ let mut rng = ChaCha20Rng :: from_seed( seed) ;
50+ let sk1 = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
51+ let vk1 = SchnorrVerificationKey :: new_from_signing_key( sk1) . unwrap( ) ;
52+ let sk2 = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
53+ let fake_sig = sk2. sign( & msg, & mut rng) . unwrap( ) ;
54+
55+ let error = fake_sig. verify( & msg, & vk1) . expect_err( "Fake signature should not be verified" ) ;
56+
57+ assert!(
58+ matches!(
59+ error. downcast_ref:: <SchnorrSignatureError >( ) ,
60+ Some ( SchnorrSignatureError :: SignatureInvalid ( _) )
61+ ) ,
62+ "Unexpected error: {error:?}"
63+ ) ;
64+ }
65+
66+ #[ test]
67+ fn signing_key_to_from_bytes( seed in any:: <[ u8 ; 32 ] >( ) ) {
68+ let mut rng = ChaCha20Rng :: from_seed( seed) ;
69+ let sk = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
70+
71+ let mut sk_bytes = sk. to_bytes( ) ;
72+ let recovered_sk = SchnorrSigningKey :: from_bytes( & sk_bytes) . unwrap( ) ;
73+ assert_eq!( sk. 0 , recovered_sk. 0 , "Recovered signing key does not match with the original!" ) ;
74+
75+ sk_bytes[ 31 ] |= 0xff ;
76+
77+ let result = SchnorrSigningKey :: from_bytes( & sk_bytes) . expect_err( "From bytes conversion of signing key should fail" ) ;
78+
79+ assert!(
80+ matches!(
81+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
82+ Some ( SchnorrSignatureError :: ScalarFieldElementSerializationError )
83+ ) ,
84+ "Unexpected error: {result:?}"
85+ ) ;
86+ }
87+
88+ #[ test]
89+ fn verification_key_to_from_bytes( seed in any:: <[ u8 ; 32 ] >( ) ) {
90+ let mut rng = ChaCha20Rng :: from_seed( seed) ;
91+ let sk = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
92+ let vk = SchnorrVerificationKey :: new_from_signing_key( sk. clone( ) ) . unwrap( ) ;
93+
94+ let mut vk_bytes = vk. to_bytes( ) ;
95+ let recovered_vk = SchnorrVerificationKey :: from_bytes( & vk_bytes) . unwrap( ) ;
96+ assert_eq!( vk. 0 , recovered_vk. 0 , "Recovered verification key does not match with the original!" ) ;
97+
98+ vk_bytes[ 31 ] |= 0xff ;
99+
100+ let result = SchnorrVerificationKey :: from_bytes( & vk_bytes) . expect_err( "From bytes conversion of verification key should fail" ) ;
101+
102+ assert!(
103+ matches!(
104+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
105+ Some ( SchnorrSignatureError :: PrimeOrderProjectivePointSerializationError )
106+ ) ,
107+ "Unexpected error: {result:?}"
108+ ) ;
109+ }
110+
111+ #[ test]
112+ fn signature_to_from_bytes( msg in prop:: collection:: vec( any:: <u8 >( ) , 1 ..128 ) , seed in any:: <[ u8 ; 32 ] >( ) ) {
113+ let mut rng = ChaCha20Rng :: from_seed( seed) ;
114+
115+ let sk = SchnorrSigningKey :: generate( & mut rng) . unwrap( ) ;
116+ let signature = sk. sign( & msg, & mut ChaCha20Rng :: from_seed( seed) ) . unwrap( ) ;
117+
118+ let signature_bytes = signature. to_bytes( ) ;
119+ let recovered_signature = SchnorrSignature :: from_bytes( & signature_bytes) . unwrap( ) ;
120+ assert_eq!( signature, recovered_signature, "Recovered signature does not match with the original!" ) ;
121+
122+ // Test invalid `sigma`
123+ let mut corrupted_bytes = signature_bytes. clone( ) ;
124+ corrupted_bytes[ 31 ] |= 0xff ;
125+
126+ let result = SchnorrSignature :: from_bytes( & corrupted_bytes) . expect_err( "From bytes conversion of signature should fail" ) ;
127+
128+ assert!(
129+ matches!(
130+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
131+ Some ( SchnorrSignatureError :: ProjectivePointSerializationError )
132+ ) ,
133+ "Unexpected error: {result:?}"
134+ ) ;
135+
136+ // Test invalid `signature`
137+ let mut corrupted_bytes = signature_bytes. clone( ) ;
138+ corrupted_bytes[ 63 ] |= 0xff ;
139+
140+ let result = SchnorrSignature :: from_bytes( & corrupted_bytes) . expect_err( "From bytes conversion should fail" ) ;
141+
142+ assert!(
143+ matches!(
144+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
145+ Some ( SchnorrSignatureError :: ScalarFieldElementSerializationError )
146+ ) ,
147+ "Unexpected error: {result:?}"
148+ ) ;
149+ // Test invalid `challenge`
150+ let mut corrupted_bytes = signature_bytes. clone( ) ;
151+ corrupted_bytes[ 95 ] |= 0xff ;
152+
153+ let result = SchnorrSignature :: from_bytes( & corrupted_bytes) . expect_err( "From bytes conversion should fail" ) ;
154+
155+ assert!(
156+ matches!(
157+ result. downcast_ref:: <SchnorrSignatureError >( ) ,
158+ Some ( SchnorrSignatureError :: ScalarFieldElementSerializationError )
159+ ) ,
160+ "Unexpected error: {result:?}"
161+ ) ;
162+ }
163+ }
164+ }
0 commit comments