Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bench for emulated key-aggregation on Bn254 #525

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions plonk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ name = "plonk-benches"
path = "benches/bench.rs"
harness = false

[[bench]]
name = "key-aggregation-benches"
path = "benches/emulated_key_aggregation.rs"
harness = false
required-features = ["test-srs", "print-trace"]

[features]
default = ["parallel"]
std = [
Expand Down Expand Up @@ -77,6 +83,7 @@ parallel = [
"dep:rayon",
]
test-srs = []
print-trace = ["ark-std/print-trace"]

[[example]]
name = "proof-of-exp"
Expand Down
95 changes: 95 additions & 0 deletions plonk/benches/emulated_key_aggregation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) 2022 Espresso Systems (espressosys.com)
// This file is part of the Jellyfish library.

// You should have received a copy of the MIT License
// along with the Jellyfish library. If not, see <https://mit-license.org/>.

// For benchmark, run:
// RAYON_NUM_THREADS=N cargo bench
// where N is the number of threads you want to use (N = 1 for single-thread).

use ark_bn254::{g1::Config as Param254, Bn254, Fq as Fq254, Fr as Fr254};
use ark_ec::{
short_weierstrass::{Projective, SWCurveConfig},
CurveGroup,
};
use ark_ff::{UniformRand as _, Zero};
use jf_plonk::{
errors::PlonkError,
proof_system::{PlonkKzgSnark, UniversalSNARK},
transcript::StandardTranscript,
};
use jf_relation::{
gadgets::{ecc::SWToTEConParam, EmulationConfig},
Circuit, PlonkCircuit,
};
use jf_utils::test_rng;

const NUM_NODES: usize = 200;

fn gen_emulated_key_aggregation_circuit<E, P>(
num_nodes: usize,
) -> Result<PlonkCircuit<Fr254>, PlonkError>
where
E: EmulationConfig<Fr254> + SWToTEConParam,
P: SWCurveConfig<BaseField = E>,
{
let mut rng = test_rng();
let mut points = vec![];
let mut result = Projective::<P>::zero();
for _ in 0..num_nodes {
let p = Projective::<P>::rand(&mut rng);
result += p;
points.push(p.into_affine());
}
let mut circuit = PlonkCircuit::<Fr254>::new_ultra_plonk(20);
let mut point_vars = vec![];
for p in points {
point_vars.push(circuit.create_emulated_sw_point_variable(p.into())?);
}
let neutral = Projective::<P>::zero().into_affine();
let mut acc_point_var = circuit.create_constant_emulated_sw_point_variable(neutral.into())?;
let result_var =
circuit.create_public_emulated_sw_point_variable(result.into_affine().into())?;

for p in point_vars {
acc_point_var = circuit.emulated_sw_ecc_add(&acc_point_var, &p, E::from(0u64))?;
}
circuit.is_emulated_sw_point_equal(&acc_point_var, &result_var)?;

circuit.finalize_for_arithmetization()?;
Ok(circuit)
}

#[cfg(any(test, feature = "test-srs"))]
fn bench_emulated_key_aggregation<E, P>(num_nodes: usize)
where
E: EmulationConfig<Fr254> + SWToTEConParam,
P: SWCurveConfig<BaseField = E>,
{
use ark_std::{end_timer, start_timer};

let mut rng = test_rng();

let circuit_time = start_timer!(|| format!("Building circuit for {} nodes ", num_nodes));
let circuit = gen_emulated_key_aggregation_circuit::<E, P>(num_nodes).unwrap();
end_timer!(circuit_time);

println!("Num of gates: {}", circuit.num_gates());

let max_degree = circuit.num_gates() + 2;
let srs = PlonkKzgSnark::<Bn254>::universal_setup_for_testing(max_degree, &mut rng).unwrap();

let (pk, _) = PlonkKzgSnark::<Bn254>::preprocess(&srs, &circuit).unwrap();

let proof_time = start_timer!(|| format!("Generating proof for {} nodes ", num_nodes));
let _ =
PlonkKzgSnark::<Bn254>::prove::<_, _, StandardTranscript>(&mut rng, &circuit, &pk, None)
.unwrap();
end_timer!(proof_time);
}

#[cfg(any(test, feature = "test-srs"))]
fn main() {
bench_emulated_key_aggregation::<Fq254, Param254>(NUM_NODES);
}
Loading