Skip to content

Commit 44714f9

Browse files
committed
Small modifications based on JP feedback.
1 parent bc2ce93 commit 44714f9

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

mithril-stm/src/schnorr_signature/mod.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ mod tests {
4343
signing_key::SchnorrSigningKey, verification_key::SchnorrVerificationKey,
4444
};
4545

46-
// Testing conversion from arbitrary message to scalar field element
4746
#[test]
4847
fn test_hash_msg_to_jubjubbase() {
4948
let msg = vec![0, 0, 0, 1];
@@ -53,23 +52,21 @@ mod tests {
5352
179, 7, 17, 168, 141, 112, 57, 117, 112, 92, 169, 56, 36, 70, 1, 217, 9, 13, 255, 42,
5453
100, 207, 166, 110, 188, 47, 35, 211, 35, 168, 100, 25,
5554
];
55+
5656
let field_elem = JubjubBase::from_bytes_le(&bytes_le).unwrap();
57+
5758
assert_eq!(h, field_elem)
5859
}
5960

60-
// Testing conversion from EC point to scalar coordinates
61-
// For now only printing, next step is to try to generate a point
62-
// from x and y values to check if they match with the result of the function
6361
#[test]
6462
fn test_get_coordinates() {
6563
let seed = [0u8; 32];
6664
let mut rng = ChaCha20Rng::from_seed(seed);
6765
let point = JubjubSubgroup::random(&mut rng);
66+
6867
let (_x, _y) = get_coordinates(point);
69-
// println!("{:?}", (x, y));
7068
}
7169

72-
// Testing conversion from BLS12-381 base field to Jubjub base field
7370
// TODO: Add randomness to val
7471
#[test]
7572
fn test_jubjub_base_to_scalar() {
@@ -89,7 +86,9 @@ mod tests {
8986
let mut rng = ChaCha20Rng::from_seed(seed);
9087
let sk = SchnorrSigningKey::generate(&mut rng);
9188
let vk = SchnorrVerificationKey::from(&sk);
89+
9290
let sig = sk.sign(&msg, &mut rng).unwrap();
91+
9392
sig.verify(&msg, &vk).unwrap();
9493
}
9594

@@ -99,26 +98,24 @@ mod tests {
9998
let msg2 = vec![0, 0, 0, 2];
10099
let seed = [0u8; 32];
101100
let mut rng = ChaCha20Rng::from_seed(seed);
102-
103101
let sk = SchnorrSigningKey::generate(&mut rng);
104102
let vk = SchnorrVerificationKey::from(&sk);
105-
106103
let sk2 = SchnorrSigningKey::generate(&mut rng);
107104
let vk2 = SchnorrVerificationKey::from(&sk2);
108105

109106
let sig = sk.sign(&msg, &mut rng).unwrap();
110107
let sig2 = sk.sign(&msg2, &mut rng).unwrap();
111108

112109
// Wrong verification key is used
113-
let result = sig.verify(&msg, &vk2);
110+
let result1 = sig.verify(&msg, &vk2);
111+
let result2 = sig2.verify(&msg, &vk);
112+
114113
assert!(
115-
result.is_err(),
114+
result1.is_err(),
116115
"Wrong verfication key used, test should fail."
117116
);
118-
119117
// Wrong message is verified
120-
let result = sig2.verify(&msg, &vk);
121-
assert!(result.is_err(), "Wrong message used, test should fail.");
118+
assert!(result2.is_err(), "Wrong message used, test should fail.");
122119
}
123120

124121
#[test]
@@ -127,8 +124,10 @@ mod tests {
127124
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(seed);
128125
let sk = SchnorrSigningKey::generate(&mut rng);
129126
let vk = SchnorrVerificationKey::from(&sk);
127+
130128
let vk_bytes = vk.to_bytes();
131129
let vk2 = SchnorrVerificationKey::from_bytes(&vk_bytes).unwrap();
130+
132131
assert_eq!(vk.0, vk2.0);
133132
}
134133

@@ -137,8 +136,10 @@ mod tests {
137136
let seed = 0;
138137
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(seed);
139138
let sk = SchnorrSigningKey::generate(&mut rng);
139+
140140
let sk_bytes: [u8; 32] = sk.to_bytes();
141141
let sk2 = SchnorrSigningKey::from_bytes(&sk_bytes).unwrap();
142+
142143
assert_eq!(sk, sk2);
143144
}
144145

@@ -150,9 +151,9 @@ mod tests {
150151
let sk = SchnorrSigningKey::generate(&mut rng);
151152

152153
let sig = sk.sign(&msg, &mut rng).unwrap();
153-
154154
let sig_bytes: [u8; 96] = sig.clone().to_bytes();
155155
let sig2 = SchnorrSignature::from_bytes(&sig_bytes).unwrap();
156+
156157
assert_eq!(sig, sig2);
157158
}
158159
}

mithril-stm/src/schnorr_signature/signature.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@ use crate::{
1515
};
1616

1717
/// Structure of the Schnorr signature to use with the SNARK
18+
///
1819
/// This signature includes a value `sigma` which depends only on
1920
/// the message and the signing key.
2021
/// This value is used in the lottery process to determine the correct indices.
2122
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2223
pub(crate) struct SchnorrSignature {
24+
/// Deterministic value depending on the message and secret key
2325
pub(crate) sigma: JubjubSubgroup,
26+
/// Part of the Schnorr signature depending on the secret key
2427
pub(crate) signature: JubjubScalar,
28+
/// Part of the Schnorr signature NOT depending on the secret key
2529
pub(crate) challenge: JubjubBase,
2630
}
2731

2832
impl SchnorrSignature {
2933
/// Description of the verification for Schnorr
34+
///
3035
/// This function performs the verification of a Schnorr signature given the signature, the signed message
3136
/// and a verification key derived from the secret key used to sign.
3237
///
@@ -98,8 +103,8 @@ impl SchnorrSignature {
98103
Ok(())
99104
}
100105

101-
/// Dense mapping function indexed by the index to be evaluated
102-
/// adapted to the Schnorr signature.
106+
/// Dense mapping function indexed by the index to be evaluated adapted to the Schnorr signature.
107+
///
103108
/// We need to convert the inputs to fit in a Poseidon hash.
104109
/// The order of the hash input must be the same as the one in the SNARK circuit
105110
/// `ev = H(DST || msg || index || σ) <- MSP.Eval(msg,index,σ)` given in paper.
@@ -126,6 +131,7 @@ impl SchnorrSignature {
126131
}
127132

128133
/// Convert a string of bytes into a `SchnorrSignature`.
134+
///
129135
/// Not sure the sigma, s and c creation can fail if the 96 bytes are correctly extracted.
130136
/// TODO: Do we want to fail conversion if there are more than 96 bytes?
131137
pub(crate) fn from_bytes(bytes: &[u8]) -> Result<Self> {

mithril-stm/src/schnorr_signature/signing_key.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ impl SchnorrSigningKey {
2424
SchnorrSigningKey(JubjubScalar::random(rng))
2525
}
2626

27-
// TODO: Check if we want the sign function to handle the randomness by itself
2827
/// This function is an adapted version of the Schnorr signature scheme
2928
/// and works with the Jubjub elliptic curve and the Poseidon hash function.
29+
///
3030
/// The scheme works as follows:
3131
/// Input:
3232
/// - a message: some bytes
@@ -61,6 +61,8 @@ impl SchnorrSigningKey {
6161
/// The verification algorithm consists in recomputing the challenge from the signature value and
6262
/// checking it matches the challenge value in the Schnorr signature. It is described in more
6363
/// details in the implementation of the SchnorrSignature.
64+
///
65+
// TODO: Check if we want the sign function to handle the randomness by itself
6466
pub(crate) fn sign(
6567
&self,
6668
msg: &[u8],
@@ -124,6 +126,7 @@ impl SchnorrSigningKey {
124126
}
125127

126128
/// Convert a string of bytes into a `SchnorrSigningKey`.
129+
///
127130
/// The bytes must represent a Jubjub scalar or the conversion will fail
128131
// TODO: Maybe rework this function, do we want to allow any bytes representation
129132
// to be convertible to a sk?
@@ -168,8 +171,10 @@ mod tests {
168171
fn test_to_from_bytes() {
169172
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
170173
let sk = SchnorrSigningKey::generate(&mut rng);
174+
171175
let bytes = sk.to_bytes();
172176
let recovered_sk = SchnorrSigningKey::from_bytes(&bytes).unwrap();
177+
173178
assert_eq!(sk, recovered_sk);
174179
}
175180

@@ -182,6 +187,7 @@ mod tests {
182187
rng.fill_bytes(&mut sk);
183188
// Setting the msb to 1 to make sk bigger than the modulus
184189
sk[0] |= 0xff;
190+
185191
let result = SchnorrSigningKey::from_bytes(&sk);
186192

187193
assert!(

mithril-stm/src/schnorr_signature/utils.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use sha2::{Digest, Sha256};
66
use anyhow::{Result, anyhow};
77

88
/// Convert an arbitrary array of bytes into a Jubjub scalar field element
9+
///
910
/// First hash the message to 256 bits use Sha256 then perform the conversion
1011
pub(crate) fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
1112
let mut hash = Sha256::new();
1213
hash.update(msg);
1314
let hmsg = hash.finalize();
1415
let mut output = [0u8; 32];
15-
// Adding a check here but this
1616
if hmsg.len() == output.len() {
1717
output.copy_from_slice(&hmsg);
1818
} else {
@@ -30,18 +30,18 @@ pub(crate) fn hash_msg_to_jubjubbase(msg: &[u8]) -> Result<JubjubBase> {
3030
}
3131

3232
/// Extract the coordinates of a given point
33+
///
3334
/// This is mainly use to feed the Poseidon hash function
3435
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
36+
let point_extended_representation = JubjubExtended::from(point);
37+
let point_affine_representation = JubjubAffine::from(point_extended_representation);
38+
let x_coordinate = point_affine_representation.get_u();
39+
let y_coordinate = point_affine_representation.get_v();
3940

40-
(x, y)
41+
(x_coordinate, y_coordinate)
4142
}
4243

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

0 commit comments

Comments
 (0)