Skip to content

Commit 8d782a3

Browse files
committed
Correction on variable names and started adding bench for schnorr_signature.
1 parent b47dfad commit 8d782a3

File tree

7 files changed

+89
-40
lines changed

7 files changed

+89
-40
lines changed

mithril-stm/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ name = "multi_sig"
7272
harness = false
7373
required-features = ["benchmark-internals"]
7474

75+
[[bench]]
76+
name = "schnorr_sig"
77+
harness = false
78+
required-features = ["future_snark"]
79+
7580
[[bench]]
7681
name = "stm"
7782
harness = false

mithril-stm/benches/schnorr_sig.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
2+
use mithril_stm::{SchnorrSigningKey, SchnorrVerificationKey};
3+
use rand_chacha::ChaCha20Rng;
4+
use rand_core::{RngCore, SeedableRng};
5+
6+
fn sign_and_verify(c: &mut Criterion, nr_sigs: usize) {
7+
let mut group = c.benchmark_group("Schnorr".to_string());
8+
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
9+
let mut rng_sig = ChaCha20Rng::from_seed([1u8; 32]);
10+
11+
let mut msg = [0u8; 32];
12+
rng.fill_bytes(&mut msg);
13+
let mut mvks = Vec::new();
14+
let mut sigs = Vec::new();
15+
for _ in 0..nr_sigs {
16+
let sk = SchnorrSigningKey::generate(&mut rng);
17+
let vk = SchnorrVerificationKey::from(&sk);
18+
let sig = sk.sign(&msg, &mut rng_sig).unwrap();
19+
sigs.push(sig);
20+
mvks.push(vk);
21+
}
22+
23+
group.bench_function(BenchmarkId::new("Individual verif", nr_sigs), |b| {
24+
b.iter(|| {
25+
for (vk, sig) in mvks.iter().zip(sigs.iter()) {
26+
assert!(sig.verify(&msg, vk).is_ok());
27+
}
28+
})
29+
});
30+
}
31+
32+
fn schnorr_benches(c: &mut Criterion) {
33+
sign_and_verify(c, 1000);
34+
}
35+
36+
criterion_group!(name = benches;
37+
config = Criterion::default().nresamples(10);
38+
targets =
39+
schnorr_benches
40+
);
41+
criterion_main!(benches);

mithril-stm/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ pub use bls_multi_signature::{
140140
BlsVerificationKeyProofOfPossession,
141141
};
142142

143+
#[cfg(feature = "future_snark")]
144+
pub use schnorr_signature::{SchnorrSignature, SchnorrSigningKey, SchnorrVerificationKey};
145+
143146
/// The quantity of stake held by a party, represented as a `u64`.
144147
pub type Stake = u64;
145148

mithril-stm/src/schnorr_signature/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub(super) mod utils;
77
mod verification_key;
88

99
pub use signature::*;
10+
pub use signing_key::*;
1011
pub use utils::*;
1112
pub use verification_key::*;
1213

@@ -24,9 +25,7 @@ mod tests {
2425
use rand_chacha::ChaCha20Rng;
2526
use rand_core::SeedableRng;
2627

27-
use crate::schnorr_signature::{
28-
signing_key::SchnorrSigningKey, verification_key::SchnorrVerificationKey,
29-
};
28+
use crate::schnorr_signature::{SchnorrSigningKey, SchnorrVerificationKey};
3029

3130
#[test]
3231
fn test_hash_msg_to_jubjubbase() {

mithril-stm/src/schnorr_signature/signature.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,40 @@ impl SchnorrSignature {
5959
let generator = JubjubSubgroup::generator();
6060

6161
// First hashing the message to a scalar then hashing it to a curve point
62-
let hash_msg = JubjubExtended::hash_to_point(msg);
62+
let msg_hash = JubjubExtended::hash_to_point(msg);
6363

6464
// Computing R1 = H(msg) * s + sigma * c
65-
let hash_msg_times_sig = hash_msg * self.signature;
65+
let msg_hash_times_signature = msg_hash * self.signature;
6666
let sigma_times_challenge = self.sigma * self.challenge;
67-
let random_value_1_recomputed = hash_msg_times_sig + sigma_times_challenge;
67+
let random_point_1_recomputed = msg_hash_times_signature + sigma_times_challenge;
6868

6969
// Computing R2 = g * s + vk * c
70-
let generator_times_s = generator * self.signature;
70+
let generator_times_signature = generator * self.signature;
7171
let vk_times_challenge = verification_key.0 * self.challenge;
72-
let random_value_2_recomputed = generator_times_s + vk_times_challenge;
72+
let random_point_2_recomputed = generator_times_signature + vk_times_challenge;
7373

74-
let (hash_msg_x, hash_msg_y) = get_coordinates_extended(hash_msg);
74+
let (msg_hash_x, msg_hash_y) = get_coordinates_extended(msg_hash);
7575
let (verification_key_x, verification_key_y) = get_coordinates_subgroup(verification_key.0);
7676
let (sigma_x, sigma_y) = get_coordinates_extended(self.sigma);
77-
let (random_value_1_recomputed_x, random_value_1_recomputed_y) =
78-
get_coordinates_extended(random_value_1_recomputed);
79-
let (random_value_2_recomputed_x, random_value_2_recomputed_y) =
80-
get_coordinates_subgroup(random_value_2_recomputed);
77+
let (random_point_1_recomputed_x, random_point_1_recomputed_y) =
78+
get_coordinates_extended(random_point_1_recomputed);
79+
let (random_point_2_recomputed_x, random_point_2_recomputed_y) =
80+
get_coordinates_subgroup(random_point_2_recomputed);
81+
8182
let challenge_recomputed = Hash::digest_truncated(
8283
Domain::Other,
8384
&[
8485
DST_SIGNATURE,
85-
hash_msg_x,
86-
hash_msg_y,
86+
msg_hash_x,
87+
msg_hash_y,
8788
verification_key_x,
8889
verification_key_y,
8990
sigma_x,
9091
sigma_y,
91-
random_value_1_recomputed_x,
92-
random_value_1_recomputed_y,
93-
random_value_2_recomputed_x,
94-
random_value_2_recomputed_y,
92+
random_point_1_recomputed_x,
93+
random_point_1_recomputed_y,
94+
random_point_2_recomputed_x,
95+
random_point_2_recomputed_y,
9596
],
9697
)[0];
9798

mithril-stm/src/schnorr_signature/signing_key.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ use rand_core::{CryptoRng, RngCore};
99
use group::Group;
1010

1111
use crate::schnorr_signature::{
12-
DST_SIGNATURE, SchnorrSignature, SchnorrVerificationKey,
13-
utils::{get_coordinates_extended, get_coordinates_subgroup},
12+
DST_SIGNATURE, SchnorrSignature, SchnorrVerificationKey, get_coordinates_extended,
13+
get_coordinates_subgroup,
1414
};
1515

1616
/// Schnorr Signing key, it is essentially a random scalar of the Jubjub scalar field
1717
#[derive(Debug, Clone)]
18-
pub struct SchnorrSigningKey(pub(crate) JubjubScalar);
18+
pub struct SchnorrSigningKey(pub JubjubScalar);
1919

2020
impl SchnorrSigningKey {
21-
pub(crate) fn generate(rng: &mut (impl RngCore + CryptoRng)) -> Self {
21+
pub fn generate(rng: &mut (impl RngCore + CryptoRng)) -> Self {
2222
SchnorrSigningKey(JubjubScalar::random(rng))
2323
}
2424

@@ -61,7 +61,7 @@ impl SchnorrSigningKey {
6161
/// details in the implementation of the SchnorrSignature.
6262
///
6363
// TODO: Check if we want the sign function to handle the randomness by itself
64-
pub(crate) fn sign(
64+
pub fn sign(
6565
&self,
6666
msg: &[u8],
6767
rng: &mut (impl RngCore + CryptoRng),
@@ -71,40 +71,40 @@ impl SchnorrSigningKey {
7171
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_msg = JubjubExtended::hash_to_point(msg);
74+
let msg_hash = JubjubExtended::hash_to_point(msg);
7575

76-
let sigma = hash_msg * self.0;
76+
let sigma = msg_hash * self.0;
7777

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

8585
// Since the hash function takes as input scalar elements
8686
// We need to convert the EC points to their coordinates
8787
// I use gx and gy for now but maybe we can replace them by a DST?
88-
let (hash_msg_x, hash_msg_y) = get_coordinates_extended(hash_msg);
88+
let (msg_hash_x, msg_hash_y) = get_coordinates_extended(msg_hash);
8989
let (verification_key_x, verification_key_y) = get_coordinates_subgroup(verification_key.0);
9090
let (sigma_x, sigma_y) = get_coordinates_extended(sigma);
91-
let (random_value_1_x, random_value_1_y) = get_coordinates_extended(random_value_1);
92-
let (random_value_2_x, random_value_2_y) = get_coordinates_subgroup(random_value_2);
91+
let (random_point_1_x, random_point_1_y) = get_coordinates_extended(random_point_1);
92+
let (random_point_2_x, random_point_2_y) = get_coordinates_subgroup(random_point_2);
9393

9494
let challenge = Hash::digest_truncated(
9595
Domain::Other,
9696
&[
9797
DST_SIGNATURE,
98-
hash_msg_x,
99-
hash_msg_y,
98+
msg_hash_x,
99+
msg_hash_y,
100100
verification_key_x,
101101
verification_key_y,
102102
sigma_x,
103103
sigma_y,
104-
random_value_1_x,
105-
random_value_1_y,
106-
random_value_2_x,
107-
random_value_2_y,
104+
random_point_1_x,
105+
random_point_1_y,
106+
random_point_2_x,
107+
random_point_2_y,
108108
],
109109
)[0];
110110

mithril-stm/src/schnorr_signature/verification_key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ impl From<&SchnorrSigningKey> for SchnorrVerificationKey {
3232
///
3333
/// This is done by computing `vk = g * sk` where g is the generator
3434
/// of the subgroup and sk is the schnorr secret key
35-
fn from(sk: &SchnorrSigningKey) -> Self {
36-
let g = JubjubSubgroup::generator();
35+
fn from(signing_key: &SchnorrSigningKey) -> Self {
36+
let generator = JubjubSubgroup::generator();
3737

38-
SchnorrVerificationKey(g * sk.0)
38+
SchnorrVerificationKey(generator * signing_key.0)
3939
}
4040
}

0 commit comments

Comments
 (0)