Skip to content

Commit bc2ce93

Browse files
committed
Added utils module for utility functions.
1 parent 589c671 commit bc2ce93

File tree

4 files changed

+71
-62
lines changed

4 files changed

+71
-62
lines changed

mithril-stm/src/schnorr_signature/mod.rs

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33

44
mod signature;
55
mod signing_key;
6+
mod utils;
67
mod verification_key;
78

8-
99
use 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

2116
use signature::*;
17+
use utils::*;
2218
use 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
2721
pub const DST_SIGNATURE: JubjubBase = JubjubBase::from_raw([0u64, 0, 0, 0]);
2822
pub 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)]
8834
mod 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

mithril-stm/src/schnorr_signature/signature.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ impl SchnorrSignature {
142142
.into_option()
143143
.ok_or(anyhow!("Unable to convert bytes into a c value."))?;
144144

145-
Ok(Self { sigma, signature, challenge })
145+
Ok(Self {
146+
sigma,
147+
signature,
148+
challenge,
149+
})
146150
}
147151
}

mithril-stm/src/schnorr_signature/signing_key.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use midnight_circuits::instructions::HashToCurveCPU;
1010
use group::Group;
1111

1212
pub(crate) use crate::schnorr_signature::{
13-
DST_SIGNATURE, JubjubHashToCurve, get_coordinates, hash_msg_to_jubjubbase,
14-
jubjub_base_to_scalar,
13+
DST_SIGNATURE, JubjubHashToCurve,
14+
utils::{get_coordinates, hash_msg_to_jubjubbase, jubjub_base_to_scalar},
1515
};
1616
use crate::schnorr_signature::{SchnorrSignature, SchnorrVerificationKey};
1717

@@ -112,7 +112,11 @@ impl SchnorrSigningKey {
112112
let challenge_scalar = jubjub_base_to_scalar(&challenge)?;
113113
let signature = random_scalar - challenge_scalar * self.0;
114114

115-
Ok(SchnorrSignature { sigma, signature, challenge })
115+
Ok(SchnorrSignature {
116+
sigma,
117+
signature,
118+
challenge,
119+
})
116120
}
117121

118122
pub(crate) fn to_bytes(&self) -> [u8; 32] {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use midnight_curves::{
2+
Fq as JubjubBase, Fr as JubjubScalar, JubjubAffine, JubjubExtended, JubjubSubgroup,
3+
};
4+
use sha2::{Digest, Sha256};
5+
6+
use anyhow::{Result, anyhow};
7+
8+
/// Convert an arbitrary array of bytes into a Jubjub scalar field element
9+
/// First hash the message to 256 bits use Sha256 then perform the conversion
10+
pub(crate) fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
11+
let mut hash = Sha256::new();
12+
hash.update(msg);
13+
let hmsg = hash.finalize();
14+
let mut output = [0u8; 32];
15+
// Adding a check here but this
16+
if hmsg.len() == output.len() {
17+
output.copy_from_slice(&hmsg);
18+
} else {
19+
return Err(anyhow!(
20+
"Hash of the message does not have the correct lenght."
21+
));
22+
}
23+
24+
Ok(JubjubBase::from_raw([
25+
u64::from_le_bytes(output[0..8].try_into()?),
26+
u64::from_le_bytes(output[8..16].try_into()?),
27+
u64::from_le_bytes(output[16..24].try_into()?),
28+
u64::from_le_bytes(output[24..32].try_into()?),
29+
]))
30+
}
31+
32+
/// Extract the coordinates of a given point
33+
/// This is mainly use to feed the Poseidon hash function
34+
pub(crate) fn get_coordinates(point: JubjubSubgroup) -> (JubjubBase, JubjubBase) {
35+
let extended = JubjubExtended::from(point); // Convert to JubjubExtended
36+
let affine = JubjubAffine::from(extended); // Convert to JubjubAffine (affine coordinates)
37+
let x = affine.get_u(); // Get x-coordinate
38+
let y = affine.get_v(); // Get y-coordinate
39+
40+
(x, y)
41+
}
42+
43+
/// Convert an element of the BLS12-381 base field to
44+
/// one of the Jubjub base field
45+
pub(crate) fn jubjub_base_to_scalar(x: &JubjubBase) -> Result<JubjubScalar> {
46+
let bytes = x.to_bytes_le();
47+
48+
Ok(JubjubScalar::from_raw([
49+
u64::from_le_bytes(bytes[0..8].try_into()?),
50+
u64::from_le_bytes(bytes[8..16].try_into()?),
51+
u64::from_le_bytes(bytes[16..24].try_into()?),
52+
u64::from_le_bytes(bytes[24..32].try_into()?),
53+
]))
54+
}

0 commit comments

Comments
 (0)