Skip to content

Commit b254ad2

Browse files
committed
Added more test and helper function to convert msg to base field.
1 parent 3f332a8 commit b254ad2

File tree

8 files changed

+100
-90
lines changed

8 files changed

+100
-90
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-stm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ff = "0.13.1"
3737
group = "0.13.0"
3838
num-traits = "0.2.19"
3939
subtle = "2.6.1"
40+
sha2 = "0.10.9"
4041

4142
[target.'cfg(any(target_family = "wasm", windows))'.dependencies]
4243
# WASM and Windows don't support rug backend, fallback to num-integer only

mithril-stm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ mod key_registration;
117117
mod merkle_tree;
118118
mod parameters;
119119
mod participant;
120-
mod single_signature;
121120
#[cfg(feature = "future_snark")]
122121
mod schnorr_signatures;
122+
mod single_signature;
123123

124124
pub use aggregate_signature::{
125125
AggregateSignature, AggregateSignatureType, AggregateVerificationKey, BasicVerifier, Clerk,

mithril-stm/src/schnorr_signatures/helper.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
pub use midnight_curves::{
2-
EDWARDS_D, Fq as JubjubBase, Fr as JubjubScalar,
3-
JubjubAffine, JubjubExtended, JubjubSubgroup,
2+
EDWARDS_D, Fq as JubjubBase, Fr as JubjubScalar, JubjubAffine, JubjubExtended, JubjubSubgroup,
43
};
54

65
use ff::Field;
6+
use sha2::{Digest, Sha256};
77
use subtle::{Choice, ConstantTimeEq};
88

99
use std::slice;
1010

11-
1211
pub fn get_coordinates(point: JubjubSubgroup) -> (JubjubBase, JubjubBase) {
1312
let extended: JubjubExtended = point.into(); // Convert to JubjubExtended
1413
let affine: JubjubAffine = extended.into(); // Convert to JubjubAffine (affine coordinates)
@@ -27,7 +26,6 @@ pub fn jubjub_base_to_scalar(x: JubjubBase) -> JubjubScalar {
2726
])
2827
}
2928

30-
3129
pub fn is_on_curve(u: JubjubBase, v: JubjubBase) -> Choice {
3230
let u2 = u.square();
3331
let v2 = v.square();
@@ -41,3 +39,15 @@ pub fn is_on_curve(u: JubjubBase, v: JubjubBase) -> Choice {
4139
// Compare in constant time
4240
lhs.ct_eq(&rhs)
4341
}
42+
43+
pub fn hash_msg_to_base(msg: &[u8]) -> JubjubBase {
44+
let mut hash = Sha256::new();
45+
hash.update(msg);
46+
let hmsg = hash.finalize();
47+
let mut output = [0u8; 32];
48+
output.copy_from_slice(hmsg.as_slice());
49+
50+
output[31] &= 0x0f;
51+
52+
JubjubBase::from_bytes_le(&output).unwrap()
53+
}

mithril-stm/src/schnorr_signatures/mod.rs

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@ pub use midnight_curves::{
55
};
66

77
use 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;
2115
use group::Group;
16+
use sha2::{Digest, Sha256};
2217

2318
use subtle::{Choice, ConstantTimeEq};
2419
use thiserror::Error;
@@ -28,12 +23,11 @@ mod signature;
2823
mod signing_key;
2924
mod verification_key;
3025

26+
pub use helper::*;
3127
pub use signature::*;
3228
pub use signing_key::*;
3329
pub use verification_key::*;
3430

35-
36-
3731
type JubjubHashToCurve = HashToCurveGadget<
3832
JubjubBase,
3933
Jubjub,
@@ -46,7 +40,6 @@ type PoseidonHash = PoseidonChip<JubjubBase>;
4640

4741
pub(crate) const DST_SIGNATURE: JubjubBase = JubjubBase::from_raw([2u64, 0, 0, 0]);
4842

49-
5043
#[derive(Debug, Error)]
5144
pub 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)]
6262
mod 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+
}

mithril-stm/src/schnorr_signatures/signature.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1+
use midnight_circuits::instructions::{HashToCurveCPU, hash::HashCPU};
12

2-
use midnight_circuits::{
3-
instructions::{
4-
HashToCurveCPU,
5-
hash::HashCPU,
6-
},
7-
};
8-
9-
pub use midnight_curves::{Fq as JubjubBase, Fr as JubjubScalar,
10-
JubjubSubgroup,
11-
};
12-
3+
pub use midnight_curves::{Fq as JubjubBase, Fr as JubjubScalar, JubjubSubgroup};
134

145
use group::Group;
156

16-
use crate::schnorr_signatures::helper::{get_coordinates, jubjub_base_to_scalar, is_on_curve};
7+
use crate::schnorr_signatures::helper::{get_coordinates, is_on_curve, jubjub_base_to_scalar};
178
use crate::schnorr_signatures::verification_key::*;
18-
use crate::schnorr_signatures::{JubjubHashToCurve, SignatureError, PoseidonHash, DST_SIGNATURE};
19-
20-
9+
use crate::schnorr_signatures::{DST_SIGNATURE, JubjubHashToCurve, PoseidonHash, SignatureError};
2110

2211
/// Schnorr signature including the value sigma used for the lottery
2312
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -29,7 +18,11 @@ pub struct SchnorrSignature {
2918

3019
impl SchnorrSignature {
3120
/// Verify a signature against a verification key.
32-
pub fn verify(&self, msg: JubjubBase, vk: &SchnorrVerificationKey) -> Result<(), SignatureError> {
21+
pub fn verify(
22+
&self,
23+
msg: JubjubBase,
24+
vk: &SchnorrVerificationKey,
25+
) -> Result<(), SignatureError> {
3326
let g = JubjubSubgroup::generator();
3427
let hash = JubjubHashToCurve::hash_to_curve(&[msg]);
3528
let c_scalar = jubjub_base_to_scalar(self.c);
@@ -67,4 +60,3 @@ impl SchnorrSignature {
6760
(x, y)
6861
}
6962
}
70-

mithril-stm/src/schnorr_signatures/signing_key.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
2-
pub use midnight_curves::{Fq as JubjubBase, Fr as JubjubScalar,
3-
JubjubExtended as Jubjub, JubjubExtended, JubjubSubgroup
4-
};
5-
use midnight_circuits::{
6-
instructions::{
7-
HashToCurveCPU,
8-
hash::HashCPU,
9-
},
1+
use midnight_circuits::instructions::{HashToCurveCPU, hash::HashCPU};
2+
pub use midnight_curves::{
3+
Fq as JubjubBase, Fr as JubjubScalar, JubjubExtended as Jubjub, JubjubExtended, JubjubSubgroup,
104
};
115

126
use ff::Field;
@@ -15,13 +9,14 @@ use rand_core::{CryptoRng, RngCore};
159
use subtle::CtOption;
1610
use thiserror::Error;
1711

18-
use crate::{error::MultiSignatureError, schnorr_signatures::helper::{get_coordinates, is_on_curve, jubjub_base_to_scalar}};
19-
use crate::schnorr_signatures::verification_key::*;
2012
use crate::schnorr_signatures::signature::*;
13+
use crate::schnorr_signatures::verification_key::*;
14+
use crate::{
15+
error::MultiSignatureError,
16+
schnorr_signatures::helper::{get_coordinates, is_on_curve, jubjub_base_to_scalar},
17+
};
2118

22-
use crate::schnorr_signatures::{JubjubHashToCurve, SignatureError, PoseidonHash, DST_SIGNATURE};
23-
24-
19+
use crate::schnorr_signatures::{DST_SIGNATURE, JubjubHashToCurve, PoseidonHash, SignatureError};
2520

2621
/// The signing key is a scalar from the Jubjub scalar field
2722
#[derive(Debug, Clone)]
@@ -35,7 +30,7 @@ impl SchnorrSigningKey {
3530
}
3631

3732
/// A slightly modified version of the regular Schnorr signature (I think)
38-
/// We include the computation of sigma, a value depending only on the msg
33+
/// We include the computation of sigma, a value depending only on the msg
3934
/// and the secret key as it is used for the lottery process
4035
pub fn sign(&self, msg: JubjubBase, rng: &mut (impl RngCore + CryptoRng)) -> SchnorrSignature {
4136
let g = JubjubSubgroup::generator();
@@ -84,15 +79,21 @@ impl SchnorrSigningKey {
8479
/// Fails if the byte string represents a scalar larger than the group order.
8580
pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> {
8681
// This is a bit ugly, I'll try to find a better way to do it
87-
let bytes = bytes.get(..32).ok_or(MultiSignatureError::SerializationError)?.try_into().unwrap();
82+
let bytes = bytes
83+
.get(..32)
84+
.ok_or(MultiSignatureError::SerializationError)?
85+
.try_into()
86+
.unwrap();
8887
// Jubjub returs a CtChoice so I convert it to an option that looses the const time property
89-
match JubjubScalar::from_bytes(bytes).into_option().ok_or(MultiSignatureError::SerializationError) {
88+
match JubjubScalar::from_bytes(bytes)
89+
.into_option()
90+
.ok_or(MultiSignatureError::SerializationError)
91+
{
9092
Ok(sk) => Ok(Self(sk)),
9193
// the error should be updated
92-
Err(e) => Err(e)
94+
Err(e) => Err(e),
9395
}
9496
}
95-
9697
}
9798

9899
// Should we have this implementation?
@@ -102,4 +103,4 @@ impl From<&SchnorrSigningKey> for SchnorrVerificationKey {
102103
let vk = &g * &sk.0;
103104
SchnorrVerificationKey(vk)
104105
}
105-
}
106+
}

mithril-stm/src/schnorr_signatures/verification_key.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
use midnight_circuits::{
2-
ecc::{
3-
hash_to_curve::HashToCurveGadget,
4-
native::EccChip,
5-
},
2+
ecc::{hash_to_curve::HashToCurveGadget, native::EccChip},
63
hash::poseidon::PoseidonChip,
7-
instructions::{
8-
HashToCurveCPU,
9-
hash::HashCPU,
10-
},
4+
instructions::{HashToCurveCPU, hash::HashCPU},
115
types::AssignedNative,
126
};
137

14-
pub use midnight_curves::{Fq as JubjubBase, Fr as JubjubScalar,
15-
JubjubExtended as Jubjub, JubjubExtended, JubjubSubgroup,
8+
pub use midnight_curves::{
9+
Fq as JubjubBase, Fr as JubjubScalar, JubjubExtended as Jubjub, JubjubExtended, JubjubSubgroup,
1610
};
1711

18-
1912
use ff::Field;
2013
use group::Group;
2114
use rand_core::{CryptoRng, RngCore};
2215
use thiserror::Error;
2316

24-
use crate::schnorr_signatures::helper::{get_coordinates, jubjub_base_to_scalar, is_on_curve};
25-
use crate::schnorr_signatures::{JubjubHashToCurve, SignatureError, PoseidonHash, DST_SIGNATURE};
26-
27-
17+
use crate::schnorr_signatures::helper::{get_coordinates, is_on_curve, jubjub_base_to_scalar};
18+
use crate::schnorr_signatures::{DST_SIGNATURE, JubjubHashToCurve, PoseidonHash, SignatureError};
2819

2920
#[derive(Debug, Clone, Copy, Default)]
3021
pub struct SchnorrVerificationKey(pub JubjubSubgroup);
3122

32-
3323
impl SchnorrVerificationKey {
3424
pub fn to_field(&self) -> [JubjubBase; 2] {
3525
let (x, y) = get_coordinates(self.0);
@@ -68,4 +58,4 @@ impl SchnorrVerificationKey {
6858

6959
Ok(SchnorrVerificationKey(point))
7060
}
71-
}
61+
}

0 commit comments

Comments
 (0)