@@ -5,20 +5,15 @@ pub use midnight_curves::{
55} ;
66
77use midnight_circuits:: {
8- ecc:: {
9- hash_to_curve:: HashToCurveGadget ,
10- native:: EccChip ,
11- } ,
8+ ecc:: { hash_to_curve:: HashToCurveGadget , native:: EccChip } ,
129 hash:: poseidon:: PoseidonChip ,
13- instructions:: {
14- HashToCurveCPU ,
15- hash:: HashCPU ,
16- } ,
10+ instructions:: { HashToCurveCPU , hash:: HashCPU } ,
1711 types:: AssignedNative ,
1812} ;
1913
20- use ff:: { Field } ;
14+ use ff:: Field ;
2115use group:: Group ;
16+ use sha2:: { Digest , Sha256 } ;
2217
2318use subtle:: { Choice , ConstantTimeEq } ;
2419use thiserror:: Error ;
@@ -28,12 +23,11 @@ mod signature;
2823mod signing_key;
2924mod verification_key;
3025
26+ pub use helper:: * ;
3127pub use signature:: * ;
3228pub use signing_key:: * ;
3329pub use verification_key:: * ;
3430
35-
36-
3731type JubjubHashToCurve = HashToCurveGadget <
3832 JubjubBase ,
3933 Jubjub ,
@@ -46,7 +40,6 @@ type PoseidonHash = PoseidonChip<JubjubBase>;
4640
4741pub ( crate ) const DST_SIGNATURE : JubjubBase = JubjubBase :: from_raw ( [ 2u64 , 0 , 0 , 0 ] ) ;
4842
49-
5043#[ derive( Debug , Error ) ]
5144pub enum SignatureError {
5245 #[ error( "Verification failed: Signature is invalid." ) ]
@@ -56,20 +49,30 @@ pub enum SignatureError {
5649 SerializationError ,
5750}
5851
59-
52+ fn u64s_from_bytes ( bytes : & [ u8 ; 32 ] ) -> [ u64 ; 4 ] {
53+ [
54+ u64:: from_le_bytes ( bytes[ 0 ..8 ] . try_into ( ) . unwrap ( ) ) ,
55+ u64:: from_le_bytes ( bytes[ 8 ..16 ] . try_into ( ) . unwrap ( ) ) ,
56+ u64:: from_le_bytes ( bytes[ 16 ..24 ] . try_into ( ) . unwrap ( ) ) ,
57+ u64:: from_le_bytes ( bytes[ 24 ..32 ] . try_into ( ) . unwrap ( ) ) ,
58+ ]
59+ }
6060
6161#[ cfg( test) ]
6262mod tests {
6363 // use blst::{blst_p1, blst_p2};
6464 use proptest:: prelude:: * ;
6565 use rand_chacha:: ChaCha20Rng ;
66- use rand_core:: { RngCore , SeedableRng , OsRng } ;
66+ use rand_core:: { OsRng , RngCore , SeedableRng } ;
6767
6868 // use crate::bls_multi_signature::helper::unsafe_helpers::{p1_affine_to_sig, p2_affine_to_vk};
6969 use crate :: error:: { MultiSignatureError , RegisterError } ;
7070 use crate :: key_registration:: KeyRegistration ;
7171
72- use blake2:: { Blake2b , Blake2s256 , Blake2b512 , digest:: { Digest , FixedOutput , consts:: U32 } } ;
72+ use blake2:: {
73+ Blake2b , Blake2b512 , Blake2s256 ,
74+ digest:: { Digest , FixedOutput , consts:: U32 } ,
75+ } ;
7376
7477 type Blake2b256 = Blake2b < U32 > ;
7578
@@ -89,40 +92,54 @@ mod tests {
8992
9093 impl Eq for SchnorrSigningKey { }
9194
95+ // Testing conversion from arbitrary message to base field element
9296 #[ test]
93- fn test_sig (
94- ) {
95-
96- let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
97+ fn test_hash_msg_to_bas ( ) {
98+ let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
99+ let h = hash_msg_to_base ( & msg) ;
100+ println ! ( "{:?}" , h) ;
101+ }
97102
103+ // Testing basic signature using Sha256 to hash the message
104+ #[ test]
105+ fn test_sig ( ) {
106+ let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
98107 let mut rng = OsRng ;
99108
100109 let sk = SchnorrSigningKey :: generate ( & mut ChaCha20Rng :: from_entropy ( ) ) ;
101110 let vk = SchnorrVerificationKey :: from ( & sk) ;
102111
112+ let msg = hash_msg_to_base ( & msg) ;
113+
114+ let sig = sk. sign ( msg, & mut rng) ;
115+
116+ sig. verify ( msg, & vk) . unwrap ( ) ;
117+ }
118+
119+ // Testing basic signature using Blake2b256 to hash the message
120+ #[ test]
121+ fn test_sig_blake ( ) {
122+ let mut rng = OsRng ;
123+ let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
124+ let sk = SchnorrSigningKey :: generate ( & mut ChaCha20Rng :: from_entropy ( ) ) ;
125+ let vk = SchnorrVerificationKey :: from ( & sk) ;
126+
103127 let mut hash = Blake2b256 :: new ( ) ;
104128 hash. update ( msg) ;
105129 let hmsg = hash. finalize ( ) ;
106130 let mut output = [ 0u8 ; 32 ] ;
107131 output. copy_from_slice ( hmsg. as_slice ( ) ) ;
108132
109133 let msg = JubjubBase :: from_bytes_be ( & output) . unwrap ( ) ;
110-
111134 let sig = sk. sign ( msg, & mut rng) ;
112-
113135 sig. verify ( msg, & vk) . unwrap ( ) ;
114136 }
115137
116138 /// Test signing functionality.
117139 #[ test]
118140 fn test_signature_verification_valid ( ) {
119- let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
120- let mut hash = Blake2b256 :: new ( ) ;
121- hash. update ( msg) ;
122- let hmsg = hash. finalize ( ) ;
123- let mut output = [ 0u8 ; 32 ] ;
124- output. copy_from_slice ( hmsg. as_slice ( ) ) ;
125- let msg = JubjubBase :: from_bytes_be ( & output) . unwrap ( ) ;
141+ let msg = vec ! [ 0 , 0 , 0 , 1 ] ;
142+ let msg = hash_msg_to_base ( & msg) ;
126143
127144 let mut rng = OsRng ;
128145 let sk = SchnorrSigningKey :: generate ( & mut rng) ;
@@ -190,6 +207,4 @@ mod tests {
190207 let sk2 = SchnorrSigningKey :: from_bytes ( & sk_bytes) . unwrap ( ) ;
191208 assert_eq ! ( sk, sk2) ;
192209 }
193-
194-
195- }
210+ }
0 commit comments