1+ pub use midnight_curves:: {
2+ Bls12 , EDWARDS_D , Fq as JubjubBase , Fq as BlsScalar , Fr as JubjubScalar ,
3+ G1Affine as BlstG1Affine , G1Projective as BlstG1 , G2Affine as BlstG2Affine , JubjubAffine ,
4+ JubjubExtended as Jubjub , JubjubExtended , JubjubSubgroup , MODULUS ,
5+ } ;
6+
7+ use midnight_circuits:: {
8+ ecc:: {
9+ hash_to_curve:: HashToCurveGadget ,
10+ native:: EccChip ,
11+ } ,
12+ hash:: poseidon:: PoseidonChip ,
13+ instructions:: {
14+ HashToCurveCPU ,
15+ hash:: HashCPU ,
16+ } ,
17+ types:: AssignedNative ,
18+ } ;
19+
20+ use ff:: { Field } ;
21+ use group:: Group ;
22+
23+ use subtle:: { Choice , ConstantTimeEq } ;
24+ use thiserror:: Error ;
25+
26+ pub mod helper;
27+ mod signature;
28+ mod signing_key;
29+ mod verification_key;
30+
31+ pub use signature:: * ;
32+ pub use signing_key:: * ;
33+ pub use verification_key:: * ;
34+
35+
36+
37+ type JubjubHashToCurve = HashToCurveGadget <
38+ JubjubBase ,
39+ Jubjub ,
40+ AssignedNative < JubjubBase > ,
41+ PoseidonChip < JubjubBase > ,
42+ EccChip < Jubjub > ,
43+ > ;
44+
45+ type PoseidonHash = PoseidonChip < JubjubBase > ;
46+
47+ pub const DST_SIGNATURE : JubjubBase = JubjubBase :: from_raw ( [ 2u64 , 0 , 0 , 0 ] ) ;
48+
49+
50+ #[ derive( Debug , Error ) ]
51+ pub enum SignatureError {
52+ #[ error( "Verification failed: Signature is invalid." ) ]
53+ VerificationFailed ,
54+ /// This error occurs when the serialization of the raw bytes failed
55+ #[ error( "Invalid bytes" ) ]
56+ SerializationError ,
57+ }
58+
59+
60+
61+
62+ #[ cfg( test) ]
63+ mod tests {
64+ use super :: * ;
65+ use rand_core:: OsRng ;
66+
67+ /// Test signing functionality.
68+ #[ test]
69+ fn test_signature_verification_valid ( ) {
70+ let mut rng = OsRng ;
71+ let sk = SigningKey :: generate ( & mut rng) ;
72+ let msg = JubjubBase :: random ( & mut rng) ;
73+
74+ // Sign the message
75+ let signature = sk. sign ( msg, & mut rng) ;
76+
77+ // Ensure the components of the signature are non-default values
78+ assert_ne ! (
79+ signature. sigma,
80+ JubjubSubgroup :: identity( ) ,
81+ "Signature sigma should not be the identity element."
82+ ) ;
83+ assert_ne ! (
84+ signature. s,
85+ JubjubScalar :: ZERO ,
86+ "Signature s component should not be zero."
87+ ) ;
88+ assert_ne ! (
89+ signature. c,
90+ JubjubBase :: ZERO ,
91+ "Signature c component should not be zero."
92+ ) ;
93+
94+ signature. verify ( msg, & VerificationKey :: from ( & sk) ) . unwrap ( ) ;
95+ }
96+
97+ #[ test]
98+ fn test_signature_verification_invalid_signature ( ) {
99+ let mut rng = OsRng ;
100+ let sk = SigningKey :: generate ( & mut rng) ;
101+ let msg = JubjubBase :: random ( & mut rng) ;
102+ let vk: VerificationKey = ( & sk) . into ( ) ;
103+
104+ // Generate signature and tamper with it
105+ let mut signature = sk. sign ( msg, & mut rng) ;
106+ signature. s = JubjubScalar :: random ( & mut rng) ; // Modify `s` component
107+
108+ // Verify the modified signature
109+ let result = signature. verify ( msg, & vk) ;
110+ assert ! (
111+ result. is_err( ) ,
112+ "Invalid signature should fail verification, but it passed."
113+ ) ;
114+ }
115+
116+ }
0 commit comments