Skip to content

Commit 09ad452

Browse files
committed
Moved test out of proptest.
1 parent c6bb7d6 commit 09ad452

File tree

1 file changed

+110
-53
lines changed
  • mithril-stm/src/schnorr_signatures

1 file changed

+110
-53
lines changed

mithril-stm/src/schnorr_signatures/mod.rs

Lines changed: 110 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ mod tests {
6969
use crate::error::{MultiSignatureError, RegisterError};
7070
use crate::key_registration::KeyRegistration;
7171

72+
use blake2::{Blake2b, Blake2s256,Blake2b512, digest::{Digest, FixedOutput, consts::U32}};
73+
74+
type Blake2b256 = Blake2b<U32>;
75+
7276
use super::*;
7377

7478
impl PartialEq for SchnorrSigningKey {
@@ -77,61 +81,114 @@ mod tests {
7781
}
7882
}
7983

80-
// impl Eq for SchnorrSigningKey {}
81-
82-
proptest! {
83-
#![proptest_config(ProptestConfig::with_cases(1000))]
84-
85-
/// Test signing functionality.
86-
#[test]
87-
fn test_signature_verification_valid(seed in any::<u64>()) {
88-
let mut rng = OsRng;
89-
let sk = SchnorrSigningKey::generate(&mut rng);
90-
let msg = JubjubBase::random(&mut rng);
91-
92-
// Sign the message
93-
let signature = sk.sign(msg, &mut rng);
94-
95-
// Ensure the components of the signature are non-default values
96-
assert_ne!(
97-
signature.sigma,
98-
JubjubSubgroup::identity(),
99-
"Signature sigma should not be the identity element."
100-
);
101-
assert_ne!(
102-
signature.s,
103-
JubjubScalar::ZERO,
104-
"Signature s component should not be zero."
105-
);
106-
assert_ne!(
107-
signature.c,
108-
JubjubBase::ZERO,
109-
"Signature c component should not be zero."
110-
);
111-
112-
signature.verify(msg, &SchnorrVerificationKey::from(&sk)).unwrap();
84+
impl PartialEq for SchnorrVerificationKey {
85+
fn eq(&self, other: &Self) -> bool {
86+
self.to_bytes() == other.to_bytes()
11387
}
88+
}
11489

115-
#[test]
116-
fn test_signature_verification_invalid_signature(seed in any::<u64>()) {
117-
let mut rng = OsRng;
118-
let sk = SchnorrSigningKey::generate(&mut rng);
119-
let msg = JubjubBase::random(&mut rng);
120-
let vk: SchnorrVerificationKey = (&sk).into();
121-
122-
// Generate signature and tamper with it
123-
let mut signature = sk.sign(msg, &mut rng);
124-
signature.s = JubjubScalar::random(&mut rng); // Modify `s` component
125-
126-
// Verify the modified signature
127-
let result = signature.verify(msg, &vk);
128-
assert!(
129-
result.is_err(),
130-
"Invalid signature should fail verification, but it passed."
131-
);
132-
}
133-
134-
90+
impl Eq for SchnorrSigningKey {}
91+
92+
#[test]
93+
fn test_sig(
94+
) {
95+
96+
let msg = vec![0,0,0,1];
97+
98+
let mut rng = OsRng;
99+
100+
let sk = SchnorrSigningKey::generate(&mut ChaCha20Rng::from_entropy());
101+
let vk = SchnorrVerificationKey::from(&sk);
102+
103+
let mut hash = Blake2b256::new();
104+
hash.update(msg);
105+
let hmsg = hash.finalize();
106+
let mut output = [0u8; 32];
107+
output.copy_from_slice(hmsg.as_slice());
108+
109+
let msg = JubjubBase::from_bytes_be(&output).unwrap();
110+
111+
let sig = sk.sign(msg, &mut rng);
112+
113+
sig.verify(msg, &vk).unwrap();
114+
}
115+
116+
/// Test signing functionality.
117+
#[test]
118+
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();
126+
127+
let mut rng = OsRng;
128+
let sk = SchnorrSigningKey::generate(&mut rng);
129+
// let msg = JubjubBase::random(&mut rng);
130+
131+
// Sign the message
132+
let signature = sk.sign(msg, &mut rng);
133+
134+
// Ensure the components of the signature are non-default values
135+
assert_ne!(
136+
signature.sigma,
137+
JubjubSubgroup::identity(),
138+
"Signature sigma should not be the identity element."
139+
);
140+
assert_ne!(
141+
signature.s,
142+
JubjubScalar::ZERO,
143+
"Signature s component should not be zero."
144+
);
145+
assert_ne!(
146+
signature.c,
147+
JubjubBase::ZERO,
148+
"Signature c component should not be zero."
149+
);
150+
151+
signature.verify(msg, &SchnorrVerificationKey::from(&sk)).unwrap();
152+
}
153+
154+
#[test]
155+
fn test_signature_verification_invalid_signature() {
156+
let mut rng = OsRng;
157+
let sk = SchnorrSigningKey::generate(&mut rng);
158+
let msg = JubjubBase::random(&mut rng);
159+
let vk: SchnorrVerificationKey = (&sk).into();
160+
161+
// Generate signature and tamper with it
162+
let mut signature = sk.sign(msg, &mut rng);
163+
signature.s = JubjubScalar::random(&mut rng); // Modify `s` component
164+
165+
// Verify the modified signature
166+
let result = signature.verify(msg, &vk);
167+
assert!(
168+
result.is_err(),
169+
"Invalid signature should fail verification, but it passed."
170+
);
171+
}
172+
173+
#[test]
174+
fn serialize_deserialize_vk() {
175+
let seed = 0;
176+
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(seed);
177+
let sk = SchnorrSigningKey::generate(&mut rng);
178+
let vk = SchnorrVerificationKey::from(&sk);
179+
let vk_bytes = vk.to_bytes();
180+
let vk2 = SchnorrVerificationKey::from_bytes(&vk_bytes).unwrap();
181+
assert_eq!(vk, vk2);
182+
}
183+
184+
#[test]
185+
fn serialize_deserialize_sk() {
186+
let seed = 0;
187+
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(seed);
188+
let sk = SchnorrSigningKey::generate(&mut rng);
189+
let sk_bytes: [u8; 32] = sk.to_bytes();
190+
let sk2 = SchnorrSigningKey::from_bytes(&sk_bytes).unwrap();
191+
assert_eq!(sk, sk2);
135192
}
136193

137194

0 commit comments

Comments
 (0)