Skip to content

Commit 589c671

Browse files
committed
Changing the variables name and the dependency depending on the future_snark feature.
1 parent d04640d commit 589c671

File tree

5 files changed

+64
-58
lines changed

5 files changed

+64
-58
lines changed

mithril-stm/Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ 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 = [] # For activating snark features
22+
future_snark = ["ff", "midnight-circuits", "midnight-curves", "sha2", "group", "num-traits"] # For activating snark features
2323

2424
[dependencies]
25-
anyhow.workspace = true
25+
anyhow = { workspace = true }
2626
blake2 = "0.10.6"
2727
# Enforce blst portable feature for runtime detection of Intel ADX instruction set.
2828
blst = { version = "0.3.16", features = ["portable"] }
2929
digest = { workspace = true }
30-
ff = "0.13.1"
31-
group = "0.13.0"
32-
midnight-circuits = { git = "https://github.com/midnightntwrk/midnight-zk", rev = "c88a50c2169f060120a52ad0980de90f08bc9535" }
33-
midnight-curves = { git = "https://github.com/midnightntwrk/midnight-zk", rev = "c88a50c2169f060120a52ad0980de90f08bc9535" }
34-
num-traits = "0.2.19"
30+
ff = {version = "0.13.1", optional = true}
31+
group = {version = "0.13.0", optional = true }
32+
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}
3535
rand_core = { workspace = true }
3636
rayon = { workspace = true }
3737
serde = { workspace = true }
38-
sha2 = "0.10.9"
38+
sha2 = {version = "0.10.9", optional = true }
3939
thiserror = { workspace = true }
4040

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

mithril-stm/src/schnorr_signature/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// TODO: Remove
22
#![allow(dead_code)]
33

4+
mod signature;
5+
mod signing_key;
6+
mod verification_key;
7+
8+
49
use midnight_circuits::{
510
ecc::{hash_to_curve::HashToCurveGadget, native::EccChip},
611
hash::poseidon::PoseidonChip,
@@ -13,20 +18,17 @@ use sha2::{Digest, Sha256};
1318

1419
use anyhow::{Result, anyhow};
1520

16-
mod signature;
17-
mod signing_key;
18-
mod verification_key;
19-
2021
use signature::*;
21-
// use signing_key::*;
2222
use verification_key::*;
2323

24+
25+
2426
/// A DST to distinguish between use of Poseidon hash
2527
pub const DST_SIGNATURE: JubjubBase = JubjubBase::from_raw([0u64, 0, 0, 0]);
2628
pub const DST_LOTTERY: JubjubBase = JubjubBase::from_raw([1u64, 0, 0, 0]);
2729

2830
/// Defining a type for the CPU hash to curve gadget
29-
type JubjubHashToCurve = HashToCurveGadget<
31+
pub(crate) type JubjubHashToCurve = HashToCurveGadget<
3032
JubjubBase,
3133
JubjubExtended,
3234
AssignedNative<JubjubBase>,
@@ -36,7 +38,6 @@ type JubjubHashToCurve = HashToCurveGadget<
3638

3739
/// Convert an arbitrary array of bytes into a Jubjub scalar field element
3840
/// First hash the message to 256 bits use Sha256 then perform the conversion
39-
/// TODO: Handle the unwrap properly
4041
pub(crate) fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
4142
let mut hash = Sha256::new();
4243
hash.update(msg);
@@ -50,6 +51,7 @@ pub(crate) fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
5051
"Hash of the message does not have the correct lenght."
5152
));
5253
}
54+
5355
Ok(JubjubBase::from_raw([
5456
u64::from_le_bytes(output[0..8].try_into()?),
5557
u64::from_le_bytes(output[8..16].try_into()?),
@@ -65,13 +67,15 @@ pub(crate) fn get_coordinates(point: JubjubSubgroup) -> (JubjubBase, JubjubBase)
6567
let affine = JubjubAffine::from(extended); // Convert to JubjubAffine (affine coordinates)
6668
let x = affine.get_u(); // Get x-coordinate
6769
let y = affine.get_v(); // Get y-coordinate
70+
6871
(x, y)
6972
}
7073

7174
/// Convert an element of the BLS12-381 base field to
7275
/// one of the Jubjub base field
7376
pub(crate) fn jubjub_base_to_scalar(x: &JubjubBase) -> Result<JubjubScalar> {
7477
let bytes = x.to_bytes_le();
78+
7579
Ok(JubjubScalar::from_raw([
7680
u64::from_le_bytes(bytes[0..8].try_into()?),
7781
u64::from_le_bytes(bytes[8..16].try_into()?),
@@ -121,7 +125,7 @@ mod tests {
121125
// Testing conversion from BLS12-381 base field to Jubjub base field
122126
// TODO: Add randomness to val
123127
#[test]
124-
fn test_jubjub_ase_to_scalar() {
128+
fn test_jubjub_base_to_scalar() {
125129
let val = vec![0, 0, 0, 1];
126130
let jjbase = JubjubBase::from_raw(val.clone().try_into().unwrap());
127131
let jjscalar = JubjubScalar::from_raw(val.try_into().unwrap());
@@ -142,7 +146,6 @@ mod tests {
142146
sig.verify(&msg, &vk).unwrap();
143147
}
144148

145-
// TODO: Change the errors
146149
#[test]
147150
fn test_invalid_sig() {
148151
let msg = vec![0, 0, 0, 1];

mithril-stm/src/schnorr_signature/signature.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::{
2121
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2222
pub(crate) struct SchnorrSignature {
2323
pub(crate) sigma: JubjubSubgroup,
24-
pub(crate) s: JubjubScalar,
25-
pub(crate) c: JubjubBase,
24+
pub(crate) signature: JubjubScalar,
25+
pub(crate) challenge: JubjubBase,
2626
}
2727

2828
impl SchnorrSignature {
@@ -60,13 +60,13 @@ impl SchnorrSignature {
6060
let hash = JubjubHashToCurve::hash_to_curve(&[hash_msg_to_jubjubbase(msg)?]);
6161

6262
// Computing R1 = H(msg) * s + sigma * c
63-
let c_scalar = jubjub_base_to_scalar(&self.c)?;
64-
let h_s = hash * self.s;
63+
let c_scalar = jubjub_base_to_scalar(&self.challenge)?;
64+
let h_s = hash * self.signature;
6565
let sigma_c = self.sigma * c_scalar;
6666
let r1_tilde = h_s + sigma_c;
6767

6868
// Computing R2 = g * s + vk * c
69-
let g_s = g * self.s;
69+
let g_s = g * self.signature;
7070
let vk_c = vk.0 * c_scalar;
7171
let r2_tilde = g_s + vk_c;
7272

@@ -90,7 +90,7 @@ impl SchnorrSignature {
9090
r2y,
9191
]);
9292

93-
if c_tilde != self.c {
93+
if c_tilde != self.challenge {
9494
// TODO: Wrong error for now, need to change that once the errors are added
9595
return Err(anyhow!("Signature failed to verify."));
9696
}
@@ -111,15 +111,17 @@ impl SchnorrSignature {
111111
let (sigmax, sigmay) = get_coordinates(self.sigma);
112112
let ev =
113113
PoseidonChip::<JubjubBase>::hash(&[DST_LOTTERY, hashx, hashy, idx, sigmax, sigmay]);
114+
114115
Ok(ev.to_bytes_le())
115116
}
116117

117118
/// Convert an `SchnorrSignature` to a byte representation.
118119
pub(crate) fn to_bytes(self) -> [u8; 96] {
119120
let mut out = [0; 96];
120121
out[0..32].copy_from_slice(&self.sigma.to_bytes());
121-
out[32..64].copy_from_slice(&self.s.to_bytes());
122-
out[64..96].copy_from_slice(&self.c.to_bytes_le());
122+
out[32..64].copy_from_slice(&self.signature.to_bytes());
123+
out[64..96].copy_from_slice(&self.challenge.to_bytes_le());
124+
123125
out
124126
}
125127

@@ -133,12 +135,13 @@ impl SchnorrSignature {
133135
let sigma = JubjubSubgroup::from_bytes(&bytes[0..32].try_into()?)
134136
.into_option()
135137
.ok_or(anyhow!("Unable to convert bytes into a sigma value."))?;
136-
let s = JubjubScalar::from_bytes(&bytes[32..64].try_into()?)
138+
let signature = JubjubScalar::from_bytes(&bytes[32..64].try_into()?)
137139
.into_option()
138140
.ok_or(anyhow!("Unable to convert bytes into an s value."))?;
139-
let c = JubjubBase::from_bytes_le(&bytes[64..96].try_into()?)
141+
let challenge = JubjubBase::from_bytes_le(&bytes[64..96].try_into()?)
140142
.into_option()
141143
.ok_or(anyhow!("Unable to convert bytes into a c value."))?;
142-
Ok(Self { sigma, s, c })
144+
145+
Ok(Self { sigma, signature, challenge })
143146
}
144147
}

mithril-stm/src/schnorr_signature/signing_key.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use midnight_circuits::instructions::HashToCurveCPU;
99

1010
use group::Group;
1111

12-
use crate::schnorr_signature::{
12+
pub(crate) use crate::schnorr_signature::{
1313
DST_SIGNATURE, JubjubHashToCurve, get_coordinates, hash_msg_to_jubjubbase,
1414
jubjub_base_to_scalar,
1515
};
@@ -67,53 +67,52 @@ impl SchnorrSigningKey {
6767
rng: &mut (impl RngCore + CryptoRng),
6868
) -> Result<SchnorrSignature> {
6969
// Use the subgroup generator to compute the curve points
70-
let g = JubjubSubgroup::generator();
71-
let vk = SchnorrVerificationKey::from(self);
70+
let generator = JubjubSubgroup::generator();
71+
let verification_key = SchnorrVerificationKey::from(self);
7272

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

7676
// sigma = H(Sha256(msg)) * sk
77-
let sigma = hash * self.0;
77+
let sigma = hash_msg * self.0;
7878

7979
// Compute the random part of the signature with
8080
// r1 = H(msg) * r
8181
// r2 = g * r
82-
let r = JubjubScalar::random(rng);
83-
let r1 = hash * r;
84-
let r2 = g * r;
82+
let random_scalar = JubjubScalar::random(rng);
83+
let random_value_1 = hash_msg * random_scalar;
84+
let random_value_2 = generator * random_scalar;
8585

8686
// Since the hash function takes as input scalar elements
8787
// We need to convert the EC points to their coordinates
8888
// I use gx and gy for now but maybe we can replace them by a DST?
89-
let (hashx, hashy) = get_coordinates(hash);
90-
let (vkx, vky) = get_coordinates(vk.0);
91-
let (sigmax, sigmay) = get_coordinates(sigma);
92-
let (r1x, r1y) = get_coordinates(r1);
93-
let (r2x, r2y) = get_coordinates(r2);
89+
let (hash_msg_x, hash_msg_y) = get_coordinates(hash_msg);
90+
let (verification_key_x, verification_key_y) = get_coordinates(verification_key.0);
91+
let (sigma_x, sigma_y) = get_coordinates(sigma);
92+
let (random_value_1_x, random_value_1_y) = get_coordinates(random_value_1);
93+
let (random_value_2_x, random_value_2_y) = get_coordinates(random_value_2);
9494

95-
let c = PoseidonChip::<JubjubBase>::hash(&[
95+
let challenge = PoseidonChip::<JubjubBase>::hash(&[
9696
DST_SIGNATURE,
97-
hashx,
98-
hashy,
99-
vkx,
100-
vky,
101-
sigmax,
102-
sigmay,
103-
r1x,
104-
r1y,
105-
r2x,
106-
r2y,
97+
hash_msg_x,
98+
hash_msg_y,
99+
verification_key_x,
100+
verification_key_y,
101+
sigma_x,
102+
sigma_y,
103+
random_value_1_x,
104+
random_value_1_y,
105+
random_value_2_x,
106+
random_value_2_y,
107107
]);
108108

109109
// We want to use the from_raw function because the result of
110110
// the poseidon hash might not fit into the smaller modulus
111111
// the Fr scalar field
112-
// TODO: Refactor this
113-
let c_scalar = jubjub_base_to_scalar(&c)?;
114-
let s = r - c_scalar * self.0;
112+
let challenge_scalar = jubjub_base_to_scalar(&challenge)?;
113+
let signature = random_scalar - challenge_scalar * self.0;
115114

116-
Ok(SchnorrSignature { sigma, s, c })
115+
Ok(SchnorrSignature { sigma, signature, challenge })
117116
}
118117

119118
pub(crate) fn to_bytes(&self) -> [u8; 32] {
@@ -130,6 +129,7 @@ impl SchnorrSigningKey {
130129
.get(..32)
131130
.ok_or(anyhow!("Not enough bytes to create a signing key."))?
132131
.try_into()?;
132+
133133
// Jubjub returs a CtChoice so I convert it to an option that looses the const time property
134134
match JubjubScalar::from_bytes(bytes).into_option() {
135135
Some(sk) => Ok(Self(sk)),
@@ -144,7 +144,7 @@ impl SchnorrSigningKey {
144144
//
145145
#[cfg(test)]
146146
mod tests {
147-
use super::*;
147+
pub(crate) use super::*;
148148
use rand_chacha::ChaCha20Rng;
149149
use rand_core::SeedableRng;
150150

mithril-stm/src/schnorr_signature/verification_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ impl SchnorrVerificationKey {
2222
}
2323

2424
/// Do we really need to separate the coordinates?
25-
/// TODO: Make sure this is correct with some tests
2625
pub(crate) fn from_bytes(bytes: &[u8]) -> Result<Self> {
2726
let bytes = bytes
2827
.get(0..32)
@@ -42,6 +41,7 @@ impl From<&SchnorrSigningKey> for SchnorrVerificationKey {
4241
/// of the subgroup and sk is the schnorr secret key
4342
fn from(sk: &SchnorrSigningKey) -> Self {
4443
let g = JubjubSubgroup::generator();
44+
4545
SchnorrVerificationKey(g * sk.0)
4646
}
4747
}

0 commit comments

Comments
 (0)