Skip to content

Commit

Permalink
benches: Update benchmarks to be generic over curve choice
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Oct 10, 2023
1 parent 344ab6f commit c26bb6c
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 26 deletions.
5 changes: 2 additions & 3 deletions benches/circuit_msm_throughput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use std::time::{Duration, Instant};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use itertools::Itertools;
use mpc_stark::{
algebra::authenticated_stark_point::AuthenticatedStarkPointResult,
test_helpers::execute_mock_mpc,
algebra::authenticated_curve::AuthenticatedPointResult, test_helpers::execute_mock_mpc,
};
use tokio::runtime::Builder as RuntimeBuilder;

Expand Down Expand Up @@ -36,7 +35,7 @@ pub fn bench_msm_throughput(c: &mut Criterion) {

let start_time = Instant::now();

let res = AuthenticatedStarkPointResult::msm(&scalars, &points);
let res = AuthenticatedPointResult::msm(&scalars, &points);
black_box(res.open().await);

start_time.elapsed()
Expand Down
2 changes: 1 addition & 1 deletion benches/circuit_mul_throughput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn bench_mul_throughput(c: &mut Criterion) {
let mut total_time = Duration::from_millis(0);
for _ in 0..n_iters {
let (elapsed1, elapsed2) = execute_mock_mpc(|fabric| async move {
let mut res = fabric.share_scalar(1, PARTY0);
let mut res = fabric.share_scalar(1u8, PARTY0);

let start_time = Instant::now();
for _ in 0..circuit_size {
Expand Down
7 changes: 4 additions & 3 deletions benches/gate_throughput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use criterion::{
Criterion, Throughput,
};
use mpc_stark::{
algebra::scalar::Scalar, beaver::PartyIDBeaverSource, network::NoRecvNetwork, MpcFabric, PARTY0,
algebra::scalar::Scalar, beaver::PartyIDBeaverSource, network::NoRecvNetwork,
test_helpers::TestCurve, MpcFabric, PARTY0,
};
use rand::{rngs::OsRng, thread_rng};
use tokio::runtime::Builder as RuntimeBuilder;
Expand Down Expand Up @@ -41,7 +42,7 @@ pub fn config() -> Criterion {
}

/// Create a mock fabric for testing
pub fn mock_fabric(size_hint: usize) -> MpcFabric {
pub fn mock_fabric(size_hint: usize) -> MpcFabric<TestCurve> {
let network = NoRecvNetwork::default();
let beaver_source = PartyIDBeaverSource::new(PARTY0);
MpcFabric::new_with_size_hint(size_hint, network, beaver_source)
Expand All @@ -54,7 +55,7 @@ pub fn mock_fabric(size_hint: usize) -> MpcFabric {
/// Measures the raw throughput of scalar addition
pub fn scalar_addition(c: &mut Criterion) {
let mut rng = thread_rng();
let mut res = Scalar::random(&mut rng);
let mut res = Scalar::<TestCurve>::random(&mut rng);

let mut group = c.benchmark_group("raw_scalar_addition");

Expand Down
5 changes: 3 additions & 2 deletions benches/gate_throughput_traced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use clap::Parser;
use cpuprofiler::PROFILER;
use gperftools::HEAP_PROFILER;
use mpc_stark::{
algebra::scalar::Scalar, beaver::PartyIDBeaverSource, network::NoRecvNetwork, MpcFabric, PARTY0,
algebra::scalar::Scalar, beaver::PartyIDBeaverSource, network::NoRecvNetwork,
test_helpers::TestCurve, MpcFabric, PARTY0,
};
use rand::thread_rng;

Expand All @@ -19,7 +20,7 @@ use rand::thread_rng;
const NUM_GATES: usize = 10_000_000;

/// Create a mock fabric for testing
pub fn mock_fabric(size_hint: usize) -> MpcFabric {
pub fn mock_fabric(size_hint: usize) -> MpcFabric<TestCurve> {
let network = NoRecvNetwork::default();
let beaver_source = PartyIDBeaverSource::new(PARTY0);
MpcFabric::new_with_size_hint(size_hint, network, beaver_source)
Expand Down
4 changes: 2 additions & 2 deletions benches/growable_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use mpc_stark::{algebra::scalar::Scalar, buffer::GrowableBuffer};
use mpc_stark::{algebra::scalar::Scalar, buffer::GrowableBuffer, test_helpers::TestCurve};

// --------------
// | Benchmarks |
Expand All @@ -11,7 +11,7 @@ pub fn buffer_read__sequential(c: &mut Criterion) {
let mut group = c.benchmark_group("buffer_read__sequential");

for buffer_size in [1_000, 10_000, 100_000, 1_000_000] {
let buffer: GrowableBuffer<Scalar> = GrowableBuffer::new(buffer_size);
let buffer: GrowableBuffer<Scalar<TestCurve>> = GrowableBuffer::new(buffer_size);

group.throughput(Throughput::Elements(buffer_size as u64));
group.bench_function(BenchmarkId::from_parameter(buffer_size), |b| {
Expand Down
5 changes: 3 additions & 2 deletions benches/native_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use itertools::Itertools;
use mpc_stark::{
algebra::{scalar::Scalar, stark_curve::StarkPoint},
algebra::{curve::CurvePoint, scalar::Scalar},
random_point,
test_helpers::TestCurve,
};
use rand::thread_rng;

Expand All @@ -22,7 +23,7 @@ pub fn bench_native_msm(c: &mut Criterion) {
let scalars = (0..n_elems).map(|_| Scalar::random(&mut rng)).collect_vec();
let points = (0..n_elems).map(|_| random_point()).collect_vec();
b.iter(|| {
black_box(StarkPoint::msm(&scalars, &points));
black_box(CurvePoint::<TestCurve>::msm(&scalars, &points));
})
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/algebra/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ impl<C: CurveGroup> CurvePointResult<C> {
mod test {
use rand::thread_rng;

use crate::{algebra::test_helper::TestCurve, test_helpers::mock_fabric};
use crate::{test_helpers::mock_fabric, test_helpers::TestCurve};

use super::*;

Expand Down
9 changes: 0 additions & 9 deletions src/algebra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,3 @@ pub mod macros;
pub mod mpc_curve;
pub mod mpc_scalar;
pub mod scalar;

/// Helpers useful for testing throughout the `algebra` module
#[cfg(any(test, feature = "test_helpers"))]
pub(crate) mod test_helper {
use ark_curve25519::EdwardsProjective as Curve25519Projective;

/// A curve used for testing algebra implementations, set to curve25519
pub type TestCurve = Curve25519Projective;
}
2 changes: 0 additions & 2 deletions src/algebra/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ impl<C: CurveGroup> Scalar<C> {
}

/// Sample a random field element
///
/// TODO: Validate that this gives a uniform distribution over the field
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
Self(C::ScalarField::rand(rng))
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ pub mod test_helpers {
use futures::Future;

use crate::{
algebra::test_helper::TestCurve,
beaver::PartyIDBeaverSource,
network::{MockNetwork, NoRecvNetwork, UnboundedDuplexStream},
MpcFabric, PARTY0, PARTY1,
};

use ark_curve25519::EdwardsProjective as Curve25519Projective;

/// A curve used for testing algebra implementations, set to curve25519
pub type TestCurve = Curve25519Projective;

/// Create a mock fabric
pub fn mock_fabric() -> MpcFabric<TestCurve> {
let network = NoRecvNetwork::default();
Expand Down

0 comments on commit c26bb6c

Please sign in to comment.