Skip to content

Commit

Permalink
Merge pull request anoma#34 from nucypher/benchmarks-pr-compare
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec authored Jan 19, 2023
2 parents 939f913 + 0a24d30 commit 185822b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 86 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,22 @@ jobs:
- uses: actions/cache@v3
continue-on-error: false
with:
# Not caching ~/.cargo/bin/ because it breaks `cargo install critcmp` in `boa-dev/criterion-compare-action@v3`
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Run benchmarks
uses: boa-dev/criterion-compare-action@v3
if: github.event_name == 'pull_request'
with:
cwd: ${{ matrix.component }}
branchName: ${{ github.base_ref }}

# The next steps have been adapted from https://raw.githubusercontent.com/unicode-org/icu4x/main/.github/workflows/build-test.yml

# Benchmarking & dashboards job > Run benchmark.
Expand All @@ -116,8 +123,8 @@ jobs:
mkdir -p $OUTPUT_PATH;
touch $OUTPUT_PATH/output.txt;
# Currently fails to extract benchmark information https://github.com/rhysd/github-action-benchmark/issues/39
# That's why '::' is replaced with ' '
cargo bench --bench ${{ matrix.component }} -- --output-format bencher | sed 's/::/__/' | tee $OUTPUT_PATH/output.txt;
# Thats why '::' is replaced with ' '
cargo bench -- --output-format bencher | sed 's/::/__/' | tee $OUTPUT_PATH/output.txt;
echo "Saved output to $OUTPUT_PATH/output.txt";
popd
Expand Down
45 changes: 0 additions & 45 deletions .gitlab-ci.yml

This file was deleted.

7 changes: 6 additions & 1 deletion ferveo/src/vss/pvss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ impl<E: PairingEngine, T> PubliclyVerifiableSS<E, T> {
);
phi.coeffs[0] = *s;
let evals = phi.evaluate_over_domain_by_ref(dkg.domain);
// commitment to coeffs
// commitment to coeffs, F_i
let coeffs = fast_multiexp(&phi.coeffs, dkg.pvss_params.g);
let shares = dkg
.validators
.iter()
.map(|val| {
// ek_{i}^{eval_i}, i = validator index
fast_multiexp(
// &evals.evals[i..i] = &evals.evals[i]
&evals.evals[val.share_start..val.share_end],
val.validator.public_key.encryption_key.into_projective(),
)
Expand Down Expand Up @@ -144,6 +146,9 @@ impl<E: PairingEngine, T> PubliclyVerifiableSS<E, T> {
a += a_i.mul(powers_of_alpha.into_repr());
powers_of_alpha *= alpha;
}
// Y = \sum_i y_i \alpha^i
// A = \sum_i a_i \alpha^i
// e(G,Y) = e(A, ek)
E::pairing(dkg.pvss_params.g, y) == E::pairing(a, ek)
},
)
Expand Down
2 changes: 1 addition & 1 deletion tpke-wasm/js-benches/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import init, {
encrypt,
ParticipantPayload,
SharedSecretBuilder,
} from "./pkg/tpke_wasm.js";
} from "../pkg/tpke_wasm.js";

const BENCHMARK_TRIALS = 10;

Expand Down
1 change: 0 additions & 1 deletion tpke-wasm/js-benches/pkg

This file was deleted.

58 changes: 47 additions & 11 deletions tpke/benches/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use ark_bls12_381::{Bls12_381, Fr};
use ark_ec::{
prepare_g1, prepare_g2, AffineCurve, PairingEngine, ProjectiveCurve,
};
use ark_ff::{BigInteger256, Field, UniformRand, Zero};
use ark_ff::{BigInteger256, Field, One, UniformRand, Zero};
use criterion::{
black_box, criterion_group, criterion_main, BenchmarkId, Criterion,
};
use group_threshold_cryptography::{
make_random_ark_polynomial_at, make_random_polynomial_at,
};

use group_threshold_cryptography::make_random_polynomial_at;
use itertools::izip;
use rand::prelude::StdRng;
use rand_core::SeedableRng;
use rand_core::{RngCore, SeedableRng};

type E = Bls12_381;
type G1Projective = ark_ec::bls12::G1Projective<ark_bls12_381::Parameters>;
Expand Down Expand Up @@ -202,25 +201,62 @@ pub fn bench_product_of_pairings(c: &mut Criterion) {
}

pub fn bench_random_poly(c: &mut Criterion) {
let mut group = c.benchmark_group("RandomPoly");
let mut group = c.benchmark_group("random_polynomial_evaluation");
group.sample_size(10);

fn evaluate_polynomial<E: PairingEngine>(
polynomial: &[E::Fr],
x: &E::Fr,
) -> E::Fr {
let mut result = E::Fr::zero();
let mut x_power = E::Fr::one();
for coeff in polynomial {
result += *coeff * x_power;
x_power *= x;
}
result
}

pub fn naive_make_random_polynomial_at<E: PairingEngine>(
threshold: usize,
root: &E::Fr,
rng: &mut impl RngCore,
) -> Vec<E::Fr> {
// [][threshold-1]
let mut d_i = (0..threshold - 1)
.map(|_| E::Fr::rand(rng))
.collect::<Vec<_>>();
// [0..][threshold]
d_i.insert(0, E::Fr::zero());

// Now, we calculate d_i_0
// This is the term that will "zero out" the polynomial at x_r, d_i(x_r) = 0
let d_i_0 = E::Fr::zero() - evaluate_polynomial::<E>(&d_i, root);
d_i[0] = d_i_0;
assert_eq!(evaluate_polynomial::<E>(&d_i, root), E::Fr::zero());

debug_assert!(d_i.len() == threshold);
debug_assert!(evaluate_polynomial::<E>(&d_i, root) == E::Fr::zero());
d_i
}

// Skipping t=1, because it results in a random polynomial with t-1=0 coefficients
for threshold in [2, 4, 8, 16, 32, 64] {
let rng = &mut StdRng::seed_from_u64(0);
let mut ark = {
let mut rng = rng.clone();
move || {
black_box(make_random_ark_polynomial_at::<E>(
black_box(make_random_polynomial_at::<E>(
threshold,
&Fr::zero(),
&mut rng,
))
}
};
let mut vec = {
let mut naive = {
let mut rng = rng.clone();
move || {
black_box(make_random_polynomial_at::<E>(
black_box(naive_make_random_polynomial_at::<E>(
threshold,
&Fr::zero(),
&mut rng,
Expand All @@ -235,10 +271,10 @@ pub fn bench_random_poly(c: &mut Criterion) {
},
);
group.bench_function(
BenchmarkId::new("random_polynomial_vec", threshold),
BenchmarkId::new("random_polynomial_naive", threshold),
|b| {
#[allow(clippy::redundant_closure)]
b.iter(|| vec())
b.iter(|| naive())
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion tpke/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ mod tests {
let msg: &[u8] = "abc".as_bytes();
let aad: &[u8] = "my-aad".as_bytes();

let (pubkey, _privkey, contexts) =
let (pubkey, _, contexts) =
setup_fast::<E>(threshold, shares_num, &mut rng);
let ciphertext = encrypt::<_, E>(msg, aad, &pubkey, rng);

Expand Down
29 changes: 6 additions & 23 deletions tpke/src/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{lagrange_basis_at, PrivateDecryptionContextSimple};
use ark_ec::{PairingEngine, ProjectiveCurve};
use ark_ff::{PrimeField, Zero};
use ark_poly::{univariate::DensePolynomial, Polynomial, UVPolynomial};
use ark_std::UniformRand;
use itertools::zip_eq;
use rand::prelude::StdRng;
use rand_core::RngCore;
Expand Down Expand Up @@ -91,36 +90,20 @@ pub fn make_random_polynomial_at<E: PairingEngine>(
rng: &mut impl RngCore,
) -> DensePolynomial<E::Fr> {
// [][threshold-1]
let mut d_i = (0..threshold - 1)
.map(|_| E::Fr::rand(rng))
.collect::<Vec<_>>();
let mut threshold_poly = DensePolynomial::<E::Fr>::rand(threshold - 1, rng);

// [0..][threshold]
d_i.insert(0, E::Fr::zero());
let mut d_i = DensePolynomial::from_coefficients_vec(d_i);
threshold_poly[0] = E::Fr::zero();

// Now, we calculate d_i_0
// This is the term that will "zero out" the polynomial at x_r, d_i(x_r) = 0
let d_i_0 = E::Fr::zero() - d_i.evaluate(root);
d_i[0] = d_i_0;

debug_assert!(d_i.evaluate(root) == E::Fr::zero());
debug_assert!(d_i.len() == threshold);

d_i
}

pub fn make_random_ark_polynomial_at<E: PairingEngine>(
threshold: usize,
root: &E::Fr,
rng: &mut impl RngCore,
) -> Vec<E::Fr> {
let mut threshold_poly = DensePolynomial::<E::Fr>::rand(threshold - 1, rng);
threshold_poly[0] = E::Fr::zero();
let d_i_0 = E::Fr::zero() - threshold_poly.evaluate(root);
threshold_poly[0] = d_i_0;

debug_assert!(threshold_poly.evaluate(root) == E::Fr::zero());
debug_assert!(threshold_poly.coeffs.len() == threshold);
threshold_poly.coeffs

threshold_poly
}

fn prepare_share_updates_for_refreshing<E: PairingEngine>(
Expand Down

0 comments on commit 185822b

Please sign in to comment.