33
44mod signature;
55mod signing_key;
6+ mod utils;
67mod verification_key;
78
8-
99use midnight_circuits:: {
1010 ecc:: { hash_to_curve:: HashToCurveGadget , native:: EccChip } ,
1111 hash:: poseidon:: PoseidonChip ,
1212 types:: AssignedNative ,
1313} ;
14- use midnight_curves:: {
15- Fq as JubjubBase , Fr as JubjubScalar , JubjubAffine , JubjubExtended , JubjubSubgroup ,
16- } ;
17- use sha2:: { Digest , Sha256 } ;
18-
19- use anyhow:: { Result , anyhow} ;
14+ use midnight_curves:: { Fq as JubjubBase , JubjubExtended } ;
2015
2116use signature:: * ;
17+ use utils:: * ;
2218use verification_key:: * ;
2319
24-
25-
26- /// A DST to distinguish between use of Poseidon hash
20+ /// A DST (Domain Separation Tag) to distinguish between use of Poseidon hash
2721pub const DST_SIGNATURE : JubjubBase = JubjubBase :: from_raw ( [ 0u64 , 0 , 0 , 0 ] ) ;
2822pub const DST_LOTTERY : JubjubBase = JubjubBase :: from_raw ( [ 1u64 , 0 , 0 , 0 ] ) ;
2923
@@ -36,59 +30,12 @@ pub(crate) type JubjubHashToCurve = HashToCurveGadget<
3630 EccChip < JubjubExtended > ,
3731> ;
3832
39- /// Convert an arbitrary array of bytes into a Jubjub scalar field element
40- /// First hash the message to 256 bits use Sha256 then perform the conversion
41- pub ( crate ) fn hash_msg_to_jubjubbase ( msg : & [ u8 ] ) -> Result < JubjubBase > {
42- let mut hash = Sha256 :: new ( ) ;
43- hash. update ( msg) ;
44- let hmsg = hash. finalize ( ) ;
45- let mut output = [ 0u8 ; 32 ] ;
46- // Adding a check here but this
47- if hmsg. len ( ) == output. len ( ) {
48- output. copy_from_slice ( & hmsg) ;
49- } else {
50- return Err ( anyhow ! (
51- "Hash of the message does not have the correct lenght."
52- ) ) ;
53- }
54-
55- Ok ( JubjubBase :: from_raw ( [
56- u64:: from_le_bytes ( output[ 0 ..8 ] . try_into ( ) ?) ,
57- u64:: from_le_bytes ( output[ 8 ..16 ] . try_into ( ) ?) ,
58- u64:: from_le_bytes ( output[ 16 ..24 ] . try_into ( ) ?) ,
59- u64:: from_le_bytes ( output[ 24 ..32 ] . try_into ( ) ?) ,
60- ] ) )
61- }
62-
63- /// Extract the coordinates of a given point
64- /// This is mainly use to feed the Poseidon hash function
65- pub ( crate ) fn get_coordinates ( point : JubjubSubgroup ) -> ( JubjubBase , JubjubBase ) {
66- let extended = JubjubExtended :: from ( point) ; // Convert to JubjubExtended
67- let affine = JubjubAffine :: from ( extended) ; // Convert to JubjubAffine (affine coordinates)
68- let x = affine. get_u ( ) ; // Get x-coordinate
69- let y = affine. get_v ( ) ; // Get y-coordinate
70-
71- ( x, y)
72- }
73-
74- /// Convert an element of the BLS12-381 base field to
75- /// one of the Jubjub base field
76- pub ( crate ) fn jubjub_base_to_scalar ( x : & JubjubBase ) -> Result < JubjubScalar > {
77- let bytes = x. to_bytes_le ( ) ;
78-
79- Ok ( JubjubScalar :: from_raw ( [
80- u64:: from_le_bytes ( bytes[ 0 ..8 ] . try_into ( ) ?) ,
81- u64:: from_le_bytes ( bytes[ 8 ..16 ] . try_into ( ) ?) ,
82- u64:: from_le_bytes ( bytes[ 16 ..24 ] . try_into ( ) ?) ,
83- u64:: from_le_bytes ( bytes[ 24 ..32 ] . try_into ( ) ?) ,
84- ] ) )
85- }
86-
8733#[ cfg( test) ]
8834mod tests {
8935
9036 use super :: * ;
9137 use group:: Group ;
38+ use midnight_curves:: Fr as JubjubScalar ;
9239 use rand_chacha:: ChaCha20Rng ;
9340 use rand_core:: SeedableRng ;
9441
0 commit comments