11use anyhow:: { Context , anyhow} ;
22use dusk_jubjub:: {
3- ExtendedPoint as JubjubExtended , Fr as JubjubScalar , SubgroupPoint as JubjubSubgroup ,
3+ ExtendedPoint as JubjubExtended , Fq as JubjubBase , Fr as JubjubScalar ,
4+ SubgroupPoint as JubjubSubgroup ,
45} ;
56use dusk_poseidon:: { Domain , Hash } ;
67use group:: { Group , GroupEncoding } ;
78
89use crate :: {
910 StmResult ,
10- error:: SchnorrSignatureError ,
1111 schnorr_signature:: {
12- DST_SIGNATURE , SchnorrVerificationKey , get_coordinates_several_points, is_on_curve,
12+ DST_SIGNATURE , SchnorrSignatureError , SchnorrVerificationKey ,
13+ get_coordinates_several_points, is_on_curve,
1314 } ,
1415} ;
1516
@@ -25,7 +26,6 @@ pub struct SchnorrSignature {
2526 /// Part of the Schnorr signature depending on the secret key
2627 pub ( crate ) signature : JubjubScalar ,
2728 /// Part of the Schnorr signature NOT depending on the secret key
28- // pub(crate) challenge: JubjubBase,
2929 pub ( crate ) challenge : JubjubScalar ,
3030}
3131
@@ -48,7 +48,7 @@ impl SchnorrSignature {
4848 /// || sigma || random_point_1_recomputed || random_point_2_recomputed)
4949 ///
5050 /// Check: challenge == challenge_recomputed
51- ///
51+ ///
5252 pub fn verify ( & self , msg : & [ u8 ] , verification_key : & SchnorrVerificationKey ) -> StmResult < ( ) > {
5353 // Check that the verification key is on the curve
5454 if !is_on_curve ( verification_key. 0 . into ( ) ) {
@@ -83,7 +83,12 @@ impl SchnorrSignature {
8383 ] ) ;
8484
8585 let mut poseidon_input = vec ! [ DST_SIGNATURE ] ;
86- poseidon_input. extend ( points_coordinates) ;
86+ poseidon_input. extend (
87+ points_coordinates
88+ . into_iter ( )
89+ . flat_map ( |( x, y) | [ x, y] )
90+ . collect :: < Vec < JubjubBase > > ( ) ,
91+ ) ;
8792
8893 let challenge_recomputed = Hash :: digest_truncated ( Domain :: Other , & poseidon_input) [ 0 ] ;
8994
@@ -96,7 +101,7 @@ impl SchnorrSignature {
96101 Ok ( ( ) )
97102 }
98103
99- /// Convert an `SchnorrSignature` to a byte representation .
104+ /// Convert an `SchnorrSignature` into bytes .
100105 pub fn to_bytes ( self ) -> [ u8 ; 96 ] {
101106 let mut out = [ 0 ; 96 ] ;
102107 out[ 0 ..32 ] . copy_from_slice ( & self . sigma . to_bytes ( ) ) ;
@@ -106,9 +111,7 @@ impl SchnorrSignature {
106111 out
107112 }
108113
109- /// Convert a string of bytes into a `SchnorrSignature`.
110- ///
111- /// Not sure the sigma, s and c creation can fail if the 96 bytes are correctly extracted.
114+ /// Convert bytes into a `SchnorrSignature`.
112115 pub fn from_bytes ( bytes : & [ u8 ] ) -> StmResult < Self > {
113116 if bytes. len ( ) < 96 {
114117 return Err ( anyhow ! ( SchnorrSignatureError :: SerializationError ) )
@@ -151,18 +154,41 @@ impl SchnorrSignature {
151154}
152155
153156#[ cfg( test) ]
154- mod test {
157+ mod tests {
155158
156- use crate :: { SchnorrSignature , SchnorrSigningKey } ;
159+ use crate :: { SchnorrSignature , SchnorrSigningKey , SchnorrVerificationKey } ;
157160 use rand_chacha:: ChaCha20Rng ;
158161 use rand_core:: SeedableRng ;
159162
163+ #[ test]
164+ fn invalid_sig ( ) {
165+ let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
166+ let msg2 = vec ! [ 0 , 0 , 0 , 2 ] ;
167+ let seed = [ 0u8 ; 32 ] ;
168+ let mut rng = ChaCha20Rng :: from_seed ( seed) ;
169+ let sk = SchnorrSigningKey :: try_generate ( & mut rng) . unwrap ( ) ;
170+ let vk = SchnorrVerificationKey :: from ( & sk) ;
171+ let sk2 = SchnorrSigningKey :: try_generate ( & mut rng) . unwrap ( ) ;
172+ let vk2 = SchnorrVerificationKey :: from ( & sk2) ;
173+
174+ let sig = sk. sign ( & msg, & mut rng) . unwrap ( ) ;
175+ let sig2 = sk. sign ( & msg2, & mut rng) . unwrap ( ) ;
176+
177+ // Wrong verification key is used
178+ let result1 = sig. verify ( & msg, & vk2) ;
179+ let result2 = sig2. verify ( & msg, & vk) ;
180+
181+ result1. expect_err ( "Wrong verification key used, test should fail." ) ;
182+ // Wrong message is verified
183+ result2. expect_err ( "Wrong message used, test should fail." ) ;
184+ }
185+
160186 #[ test]
161187 fn serialize_deserialize_signature ( ) {
162188 let mut rng = ChaCha20Rng :: from_seed ( [ 0u8 ; 32 ] ) ;
163189
164190 let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
165- let sk = SchnorrSigningKey :: generate ( & mut rng) . unwrap ( ) ;
191+ let sk = SchnorrSigningKey :: try_generate ( & mut rng) . unwrap ( ) ;
166192
167193 let sig = sk. sign ( & msg, & mut rng) . unwrap ( ) ;
168194 let sig_bytes: [ u8 ; 96 ] = sig. to_bytes ( ) ;
@@ -177,6 +203,41 @@ mod test {
177203
178204 let result = SchnorrSignature :: from_bytes ( & msg) ;
179205
180- assert ! ( result. is_err( ) ) ;
206+ result. expect_err ( "Not enough bytes." ) ;
207+ }
208+
209+ mod golden {
210+
211+ use rand_chacha:: ChaCha20Rng ;
212+ use rand_core:: SeedableRng ;
213+
214+ use crate :: schnorr_signature:: { SchnorrSignature , SchnorrSigningKey } ;
215+
216+ const GOLDEN_BYTES : & [ u8 ; 96 ] = & [
217+ 143 , 53 , 198 , 62 , 178 , 1 , 88 , 253 , 21 , 92 , 100 , 13 , 72 , 180 , 198 , 127 , 39 , 175 , 102 ,
218+ 69 , 147 , 249 , 244 , 224 , 122 , 121 , 248 , 68 , 217 , 242 , 158 , 113 , 94 , 57 , 200 , 241 , 208 ,
219+ 145 , 251 , 8 , 92 , 119 , 163 , 38 , 81 , 85 , 54 , 36 , 193 , 221 , 254 , 242 , 21 , 129 , 110 , 161 ,
220+ 142 , 184 , 107 , 156 , 100 , 34 , 190 , 9 , 200 , 20 , 178 , 142 , 61 , 253 , 193 , 11 , 5 , 180 , 97 ,
221+ 73 , 125 , 88 , 162 , 36 , 30 , 177 , 225 , 52 , 136 , 21 , 138 , 93 , 81 , 23 , 19 , 64 , 82 , 78 , 229 ,
222+ 3 ,
223+ ] ;
224+
225+ fn golden_value ( ) -> SchnorrSignature {
226+ let mut rng = ChaCha20Rng :: from_seed ( [ 0u8 ; 32 ] ) ;
227+ let sk = SchnorrSigningKey :: try_generate ( & mut rng) . unwrap ( ) ;
228+ let msg = [ 0u8 ; 32 ] ;
229+ sk. sign ( & msg, & mut rng) . unwrap ( )
230+ }
231+
232+ #[ test]
233+ fn golden_conversions ( ) {
234+ let value = SchnorrSignature :: from_bytes ( GOLDEN_BYTES )
235+ . expect ( "This from bytes should not fail" ) ;
236+ assert_eq ! ( golden_value( ) , value) ;
237+
238+ let serialized = SchnorrSignature :: to_bytes ( value) ;
239+ let golden_serialized = SchnorrSignature :: to_bytes ( golden_value ( ) ) ;
240+ assert_eq ! ( golden_serialized, serialized) ;
241+ }
181242 }
182243}
0 commit comments