Skip to content

Commit

Permalink
DKG: Use u16 everywhere + other small fixes (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
benr-ml authored Mar 21, 2024
1 parent 503f864 commit f658d44
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 237 deletions.
10 changes: 5 additions & 5 deletions fastcrypto-tbls/benches/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn gen_ecies_keys(n: u16) -> Vec<(PartyId, ecies::PrivateKey<EG>, ecies::PublicK

pub fn setup_party(
id: PartyId,
threshold: u32,
threshold: u16,
weight: u16,
keys: &[(PartyId, ecies::PrivateKey<EG>, ecies::PublicKey<EG>)],
) -> Party<G, EG> {
Expand Down Expand Up @@ -58,7 +58,7 @@ mod dkg_benches {
let mut create: BenchmarkGroup<_> = c.benchmark_group("DKG create");
for (n, total_w) in iproduct!(SIZES.iter(), TOTAL_WEIGHTS.iter()) {
let w = total_w / n;
let t = (total_w / 3) as u32;
let t = total_w / 3;
let keys = gen_ecies_keys(*n);
let d0 = setup_party(0, t, w, &keys);

Expand All @@ -67,7 +67,7 @@ mod dkg_benches {
|b| b.iter(|| d0.create_message(&mut thread_rng())),
);

let message = d0.create_message(&mut thread_rng());
let message = d0.create_message(&mut thread_rng()).unwrap();
println!(
"Message size for n={}, t={}: {}",
n,
Expand All @@ -81,11 +81,11 @@ mod dkg_benches {
let mut verify: BenchmarkGroup<_> = c.benchmark_group("DKG message processing");
for (n, total_w) in iproduct!(SIZES.iter(), TOTAL_WEIGHTS.iter()) {
let w = total_w / n;
let t = (total_w / 3) as u32;
let t = total_w / 3;
let keys = gen_ecies_keys(*n);
let d0 = setup_party(0, t, w, &keys);
let d1 = setup_party(1, t, w, &keys);
let message = d0.create_message(&mut thread_rng());
let message = d0.create_message(&mut thread_rng()).unwrap();

verify.bench_function(
format!("n={}, total_weight={}, t={}, w={}", n, total_w, t, w).as_str(),
Expand Down
18 changes: 9 additions & 9 deletions fastcrypto-tbls/benches/nidkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn gen_ecies_keys(n: u16) -> Vec<(u16, ecies::PrivateKey<G>, ecies::PublicKey<G>

pub fn setup_party(
id: usize,
threshold: u32,
threshold: u16,
keys: &[(u16, ecies::PrivateKey<G>, ecies::PublicKey<G>)],
) -> Party<G> {
let nodes = keys
Expand Down Expand Up @@ -54,7 +54,7 @@ mod nidkg_benches {
{
let mut create: BenchmarkGroup<_> = c.benchmark_group("NI-DKG create");
for n in SIZES {
let t = (n / 2) as u32;
let t = (n / 2) as u16;
let keys = gen_ecies_keys(n);
let d0 = setup_party(0, t, &keys);

Expand All @@ -67,11 +67,11 @@ mod nidkg_benches {
{
let mut verify: BenchmarkGroup<_> = c.benchmark_group("NI-DKG message verification");
for n in SIZES {
let t = (n / 2) as u32;
let t = (n / 2) as u16;
let keys = gen_ecies_keys(n);
let d0 = setup_party(0, t, &keys);
let d1 = setup_party(1, t, &keys);
let m = d0.create_message(&mut thread_rng());
let m = d0.create_message(&mut thread_rng()).unwrap();
println!("Message size: {}", bcs::to_bytes(&m).unwrap().len());

verify.bench_function(format!("n={}, t={}", n, t).as_str(), |b| {
Expand All @@ -84,11 +84,11 @@ mod nidkg_benches {
let mut verify: BenchmarkGroup<_> =
c.benchmark_group("NI-DKG message processing for one share");
for n in SIZES {
let t = (n / 2) as u32;
let t = (n / 2) as u16;
let keys = gen_ecies_keys(n);
let d0 = setup_party(0, t, &keys);
let d1 = setup_party(1, t, &keys);
let m = d0.create_message(&mut thread_rng());
let m = d0.create_message(&mut thread_rng()).unwrap();

verify.bench_function(format!("n={}, t={}", n, t).as_str(), |b| {
b.iter(|| d1.process_message(&m, &mut thread_rng()))
Expand All @@ -101,7 +101,7 @@ mod nidkg_benches {
let mut verify: BenchmarkGroup<_> =
c.benchmark_group("NI-DKG generate partial pks in g2");
for n in SIZES {
let t = (n / 2) as u32;
let t = (n / 2) as u16;
let keys = gen_ecies_keys(n);
let d1 = setup_party(1, t, &keys);

Expand All @@ -114,15 +114,15 @@ mod nidkg_benches {
let mut verify: BenchmarkGroup<_> =
c.benchmark_group("NI-DKG verify partial pks in g2");
for n in SIZES {
let t = (n / 2) as u32;
let t = (n / 2) as u16;
let keys = gen_ecies_keys(n);
let d0 = setup_party(0, t, &keys);
let pks_in_g2 = d0.create_partial_pks_in_g2();
println!(
"pks_in_g2 size: {}",
bcs::to_bytes(&pks_in_g2).unwrap().len()
);
let m = d0.create_message(&mut thread_rng());
let m = d0.create_message(&mut thread_rng()).unwrap();

verify.bench_function(format!("n={}, t={}", n, t).as_str(), |b| {
b.iter(|| {
Expand Down
18 changes: 9 additions & 9 deletions fastcrypto-tbls/benches/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion};
use fastcrypto::groups::bls12381;
use fastcrypto_tbls::polynomial::Poly;
use rand::thread_rng;
use std::num::NonZeroU32;
use std::num::NonZeroU16;

mod polynomial_benches {
use super::*;
Expand All @@ -19,7 +19,7 @@ mod polynomial_benches {
for n in SIZES {
let t = n / 3;
vss_sk_gen.bench_function(format!("n={}, t={}", n, t).as_str(), |b| {
b.iter(|| Poly::<bls12381::Scalar>::rand(t as u32, &mut thread_rng()))
b.iter(|| Poly::<bls12381::Scalar>::rand(t as u16, &mut thread_rng()))
});
}
}
Expand All @@ -28,7 +28,7 @@ mod polynomial_benches {
let mut vss_pk_gen: BenchmarkGroup<_> = c.benchmark_group("VSS public key generation");
for n in SIZES {
let t = n / 3;
let vss_sk = Poly::<bls12381::Scalar>::rand(t as u32, &mut thread_rng());
let vss_sk = Poly::<bls12381::Scalar>::rand(t as u16, &mut thread_rng());
vss_pk_gen.bench_function(format!("n={}, t={}", n, t).as_str(), |b| {
b.iter(|| vss_sk.commit::<G>())
});
Expand All @@ -39,11 +39,11 @@ mod polynomial_benches {
let mut shares_gen: BenchmarkGroup<_> = c.benchmark_group("Shares generation");
for n in SIZES {
let t = n / 3;
let vss_sk = Poly::<bls12381::Scalar>::rand(t as u32, &mut thread_rng());
let vss_sk = Poly::<bls12381::Scalar>::rand(t as u16, &mut thread_rng());
shares_gen.bench_function(format!("n={}, t={}", n, t).as_str(), |b| {
b.iter(|| {
(1u32..=(n as u32)).for_each(|i| {
vss_sk.eval(NonZeroU32::new(i).unwrap());
(1u16..=(n as u16)).for_each(|i| {
vss_sk.eval(NonZeroU16::new(i).unwrap());
})
})
});
Expand All @@ -58,14 +58,14 @@ mod polynomial_benches {
for n in SIZES {
let t = n / 3;
let k = n / 10;
let vss_sk = Poly::<bls12381::Scalar>::rand(t as u32, &mut thread_rng());
let vss_sk = Poly::<bls12381::Scalar>::rand(t as u16, &mut thread_rng());
let vss_pk = vss_sk.commit::<G>();
shares_verification.bench_function(
format!("n={}, t={}, k={}", n, t, k).as_str(),
|b| {
b.iter(|| {
(1u32..=(k as u32)).for_each(|i| {
vss_pk.eval(NonZeroU32::new(i).unwrap());
(1u16..=(k as u16)).for_each(|i| {
vss_pk.eval(NonZeroU16::new(i).unwrap());
})
})
},
Expand Down
10 changes: 5 additions & 5 deletions fastcrypto-tbls/benches/tbls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion};
use fastcrypto::groups::bls12381;
use rand::thread_rng;
use std::num::NonZeroU32;
use std::num::NonZeroU16;

mod tbls_benches {
use super::*;
Expand All @@ -21,7 +21,7 @@ mod tbls_benches {
const WEIGHTS: [usize; 5] = [10, 20, 30, 40, 50];
for w in WEIGHTS {
let shares = (1..=w)
.map(|i| private_poly.eval(NonZeroU32::new(i as u32).unwrap()))
.map(|i| private_poly.eval(NonZeroU16::new(i as u16).unwrap()))
.collect::<Vec<_>>();

create.bench_function(format!("w={}", w).as_str(), |b| {
Expand All @@ -34,15 +34,15 @@ mod tbls_benches {
let mut create: BenchmarkGroup<_> = c.benchmark_group("Recover full signature");
const TOTAL_WEIGHTS: [usize; 4] = [666, 833, 1111, 1666];
for w in TOTAL_WEIGHTS {
let private_poly = Poly::<bls12381::Scalar>::rand(w as u32, &mut thread_rng());
let private_poly = Poly::<bls12381::Scalar>::rand(w as u16, &mut thread_rng());
let shares = (1..=w)
.map(|i| private_poly.eval(NonZeroU32::new(i as u32).unwrap()))
.map(|i| private_poly.eval(NonZeroU16::new(i as u16).unwrap()))
.collect::<Vec<_>>();

let sigs = ThresholdBls12381MinSig::partial_sign_batch(shares.iter(), msg);

create.bench_function(format!("w={}", w).as_str(), |b| {
b.iter(|| ThresholdBls12381MinSig::aggregate(w as u32, sigs.iter()).unwrap())
b.iter(|| ThresholdBls12381MinSig::aggregate(w as u16, sigs.iter()).unwrap())
});
}
}
Expand Down
Loading

0 comments on commit f658d44

Please sign in to comment.