Skip to content

Commit bb159a7

Browse files
committed
Modifications according to reviews.
1 parent 44714f9 commit bb159a7

File tree

6 files changed

+56
-44
lines changed

6 files changed

+56
-44
lines changed

mithril-stm/Cargo.toml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,30 @@ rug-backend = ["rug/default"]
1919
num-integer-backend = ["num-bigint", "num-rational", "num-traits"]
2020
benchmark-internals = [] # For benchmarking multi_sig
2121
future_proof_system = [] # For activating future proof systems
22-
future_snark = ["ff", "midnight-circuits", "midnight-curves", "sha2", "group", "num-traits"] # For activating snark features
22+
future_snark = [
23+
"ff",
24+
"midnight-circuits",
25+
"midnight-curves",
26+
"sha2",
27+
"group",
28+
"num-traits",
29+
] # For activating snark features
2330

2431
[dependencies]
2532
anyhow = { workspace = true }
2633
blake2 = "0.10.6"
2734
# Enforce blst portable feature for runtime detection of Intel ADX instruction set.
2835
blst = { version = "0.3.16", features = ["portable"] }
2936
digest = { workspace = true }
30-
ff = {version = "0.13.1", optional = true}
31-
group = {version = "0.13.0", optional = true }
37+
ff = { version = "0.13.1", optional = true }
38+
group = { version = "0.13.0", optional = true }
3239
midnight-circuits = { git = "https://github.com/midnightntwrk/midnight-zk", rev = "c88a50c2169f060120a52ad0980de90f08bc9535", optional = true }
33-
midnight-curves = { git = "https://github.com/midnightntwrk/midnight-zk", rev = "c88a50c2169f060120a52ad0980de90f08bc9535", optional = true }
34-
num-traits = {version = "0.2.19", optional = true}
40+
midnight-curves = { git = "https://github.com/midnightntwrk/midnight-zk", rev = "c88a50c2169f060120a52ad0980de90f08bc9535", optional = true }
41+
num-traits = { version = "0.2.19", optional = true }
3542
rand_core = { workspace = true }
3643
rayon = { workspace = true }
3744
serde = { workspace = true }
38-
sha2 = {version = "0.10.9", optional = true }
45+
sha2 = { version = "0.10.9", optional = true }
3946
thiserror = { workspace = true }
4047

4148
[target.'cfg(any(target_family = "wasm", target_env = "musl", windows))'.dependencies]

mithril-stm/src/schnorr_signature/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33

44
mod signature;
55
mod signing_key;
6-
mod utils;
6+
pub(super) mod utils;
77
mod verification_key;
88

9+
pub use signature::*;
10+
pub use utils::*;
11+
pub use verification_key::*;
12+
913
use midnight_circuits::{
1014
ecc::{hash_to_curve::HashToCurveGadget, native::EccChip},
1115
hash::poseidon::PoseidonChip,
1216
types::AssignedNative,
1317
};
1418
use midnight_curves::{Fq as JubjubBase, JubjubExtended};
1519

16-
use signature::*;
17-
use utils::*;
18-
use verification_key::*;
19-
2020
/// A DST (Domain Separation Tag) to distinguish between use of Poseidon hash
21-
pub const DST_SIGNATURE: JubjubBase = JubjubBase::from_raw([0u64, 0, 0, 0]);
22-
pub const DST_LOTTERY: JubjubBase = JubjubBase::from_raw([1u64, 0, 0, 0]);
21+
pub(crate) const DST_SIGNATURE: JubjubBase = JubjubBase::from_raw([0u64, 0, 0, 0]);
22+
pub(crate) const DST_LOTTERY: JubjubBase = JubjubBase::from_raw([1u64, 0, 0, 0]);
2323

2424
/// Defining a type for the CPU hash to curve gadget
2525
pub(crate) type JubjubHashToCurve = HashToCurveGadget<
@@ -151,9 +151,18 @@ mod tests {
151151
let sk = SchnorrSigningKey::generate(&mut rng);
152152

153153
let sig = sk.sign(&msg, &mut rng).unwrap();
154-
let sig_bytes: [u8; 96] = sig.clone().to_bytes();
154+
let sig_bytes: [u8; 96] = sig.to_bytes();
155155
let sig2 = SchnorrSignature::from_bytes(&sig_bytes).unwrap();
156156

157157
assert_eq!(sig, sig2);
158158
}
159+
160+
#[test]
161+
fn test_from_bytes_signature_too_many_bytes() {
162+
let msg = vec![0u8; 97];
163+
164+
let result = SchnorrSignature::from_bytes(&msg);
165+
166+
assert!(result.is_err());
167+
}
159168
}

mithril-stm/src/schnorr_signature/signature.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
/// the message and the signing key.
2121
/// This value is used in the lottery process to determine the correct indices.
2222
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23-
pub(crate) struct SchnorrSignature {
23+
pub struct SchnorrSignature {
2424
/// Deterministic value depending on the message and secret key
2525
pub(crate) sigma: JubjubSubgroup,
2626
/// Part of the Schnorr signature depending on the secret key
@@ -58,24 +58,24 @@ impl SchnorrSignature {
5858
/// to their coordinates representation to feed them to the hash function.
5959
/// - Check: c == c_tilde
6060
///
61-
pub(crate) fn verify(&self, msg: &[u8], vk: &SchnorrVerificationKey) -> Result<()> {
62-
let g = JubjubSubgroup::generator();
61+
pub fn verify(&self, msg: &[u8], vk: &SchnorrVerificationKey) -> Result<()> {
62+
let generator = JubjubSubgroup::generator();
6363

6464
// First hashing the message to a scalar then hashing it to a curve point
65-
let hash = JubjubHashToCurve::hash_to_curve(&[hash_msg_to_jubjubbase(msg)?]);
65+
let hash_msg = JubjubHashToCurve::hash_to_curve(&[hash_msg_to_jubjubbase(msg)?]);
6666

6767
// Computing R1 = H(msg) * s + sigma * c
68-
let c_scalar = jubjub_base_to_scalar(&self.challenge)?;
69-
let h_s = hash * self.signature;
70-
let sigma_c = self.sigma * c_scalar;
68+
let challenge_scalar = jubjub_base_to_scalar(&self.challenge)?;
69+
let h_s = hash_msg * self.signature;
70+
let sigma_c = self.sigma * challenge_scalar;
7171
let r1_tilde = h_s + sigma_c;
7272

7373
// Computing R2 = g * s + vk * c
74-
let g_s = g * self.signature;
75-
let vk_c = vk.0 * c_scalar;
74+
let g_s = generator * self.signature;
75+
let vk_c = vk.0 * challenge_scalar;
7676
let r2_tilde = g_s + vk_c;
7777

78-
let (hashx, hashy) = get_coordinates(hash);
78+
let (hashx, hashy) = get_coordinates(hash_msg);
7979
let (vkx, vky) = get_coordinates(vk.0);
8080
let (sigmax, sigmay) = get_coordinates(self.sigma);
8181
let (r1x, r1y) = get_coordinates(r1_tilde);
@@ -108,7 +108,7 @@ impl SchnorrSignature {
108108
/// We need to convert the inputs to fit in a Poseidon hash.
109109
/// The order of the hash input must be the same as the one in the SNARK circuit
110110
/// `ev = H(DST || msg || index || σ) <- MSP.Eval(msg,index,σ)` given in paper.
111-
fn evaluate_dense_mapping(&self, msg: &[u8], index: Index) -> Result<[u8; 32]> {
111+
pub(crate) fn evaluate_dense_mapping(&self, msg: &[u8], index: Index) -> Result<[u8; 32]> {
112112
let hash = JubjubHashToCurve::hash_to_curve(&[hash_msg_to_jubjubbase(msg)?]);
113113
let (hashx, hashy) = get_coordinates(hash);
114114
// TODO: Check if this is the correct way to add the index
@@ -121,7 +121,7 @@ impl SchnorrSignature {
121121
}
122122

123123
/// Convert an `SchnorrSignature` to a byte representation.
124-
pub(crate) fn to_bytes(self) -> [u8; 96] {
124+
pub fn to_bytes(self) -> [u8; 96] {
125125
let mut out = [0; 96];
126126
out[0..32].copy_from_slice(&self.sigma.to_bytes());
127127
out[32..64].copy_from_slice(&self.signature.to_bytes());
@@ -133,11 +133,8 @@ impl SchnorrSignature {
133133
/// Convert a string of bytes into a `SchnorrSignature`.
134134
///
135135
/// Not sure the sigma, s and c creation can fail if the 96 bytes are correctly extracted.
136-
/// TODO: Do we want to fail conversion if there are more than 96 bytes?
137-
pub(crate) fn from_bytes(bytes: &[u8]) -> Result<Self> {
138-
let bytes = bytes
139-
.get(..96)
140-
.ok_or(anyhow!("Not enough bytes to create a signature."))?;
136+
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
137+
let bytes: [u8; 96] = bytes.try_into()?;
141138
let sigma = JubjubSubgroup::from_bytes(&bytes[0..32].try_into()?)
142139
.into_option()
143140
.ok_or(anyhow!("Unable to convert bytes into a sigma value."))?;

mithril-stm/src/schnorr_signature/signing_key.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
use anyhow::{Result, anyhow};
22
use ff::Field;
3-
use midnight_circuits::hash::poseidon::PoseidonChip;
4-
use midnight_circuits::instructions::hash::HashCPU;
3+
use midnight_circuits::{
4+
hash::poseidon::PoseidonChip,
5+
instructions::{HashToCurveCPU, hash::HashCPU},
6+
};
57
use midnight_curves::{Fq as JubjubBase, Fr as JubjubScalar, JubjubSubgroup};
68
use rand_core::{CryptoRng, RngCore};
79

8-
use midnight_circuits::instructions::HashToCurveCPU;
9-
1010
use group::Group;
1111

12-
pub(crate) use crate::schnorr_signature::{
13-
DST_SIGNATURE, JubjubHashToCurve,
12+
use crate::schnorr_signature::{
13+
DST_SIGNATURE, JubjubHashToCurve, SchnorrSignature, SchnorrVerificationKey,
1414
utils::{get_coordinates, hash_msg_to_jubjubbase, jubjub_base_to_scalar},
1515
};
16-
use crate::schnorr_signature::{SchnorrSignature, SchnorrVerificationKey};
1716

1817
/// Schnorr Signing key, it is essentially a random scalar of the Jubjub scalar field
1918
#[derive(Debug, Clone)]
20-
pub(crate) struct SchnorrSigningKey(pub(crate) JubjubScalar);
19+
pub struct SchnorrSigningKey(pub(crate) JubjubScalar);
2120

2221
impl SchnorrSigningKey {
2322
pub(crate) fn generate(rng: &mut (impl RngCore + CryptoRng)) -> Self {

mithril-stm/src/schnorr_signature/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use anyhow::{Result, anyhow};
88
/// Convert an arbitrary array of bytes into a Jubjub scalar field element
99
///
1010
/// First hash the message to 256 bits use Sha256 then perform the conversion
11-
pub(crate) fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
11+
pub fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
1212
let mut hash = Sha256::new();
1313
hash.update(msg);
1414
let hmsg = hash.finalize();
@@ -32,7 +32,7 @@ pub(crate) fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
3232
/// Extract the coordinates of a given point
3333
///
3434
/// This is mainly use to feed the Poseidon hash function
35-
pub(crate) fn get_coordinates(point: JubjubSubgroup) -> (JubjubBase, JubjubBase) {
35+
pub fn get_coordinates(point: JubjubSubgroup) -> (JubjubBase, JubjubBase) {
3636
let point_extended_representation = JubjubExtended::from(point);
3737
let point_affine_representation = JubjubAffine::from(point_extended_representation);
3838
let x_coordinate = point_affine_representation.get_u();
@@ -42,7 +42,7 @@ pub(crate) fn get_coordinates(point: JubjubSubgroup) -> (JubjubBase, JubjubBase)
4242
}
4343

4444
/// Convert an element of the BLS12-381 base field to one of the Jubjub base field
45-
pub(crate) fn jubjub_base_to_scalar(x: &JubjubBase) -> Result<JubjubScalar> {
45+
pub fn jubjub_base_to_scalar(x: &JubjubBase) -> Result<JubjubScalar> {
4646
let bytes = x.to_bytes_le();
4747

4848
Ok(JubjubScalar::from_raw([

mithril-stm/src/schnorr_signature/verification_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use anyhow::{Result, anyhow};
22
use group::{Group, GroupEncoding};
33
pub use midnight_curves::JubjubSubgroup;
44

5-
use crate::schnorr_signature::signing_key::SchnorrSigningKey;
5+
pub(crate) use crate::schnorr_signature::signing_key::SchnorrSigningKey;
66

77
/// Schnorr verification key, it consists of a point on the Jubjub curve
88
/// vk = g * sk, where g is a generator
99
#[derive(Debug, Clone, Copy, Default)]
10-
pub(crate) struct SchnorrVerificationKey(pub(crate) JubjubSubgroup);
10+
pub struct SchnorrVerificationKey(pub(crate) JubjubSubgroup);
1111

1212
impl SchnorrVerificationKey {
1313
/// TODO: Make sure this is correct as the previous implementation is

0 commit comments

Comments
 (0)