Skip to content

Commit 106ab4a

Browse files
authored
adding keccak and benchmark (#23)
* adding keccak and benchmark * keccak added * safe init logging
1 parent 9e5c87a commit 106ab4a

File tree

4 files changed

+526
-77
lines changed

4 files changed

+526
-77
lines changed

Diff for: src/benches/mod.rs

+89-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::env;
2-
31
use log::{log_enabled, Level, LevelFilter};
2+
use std::env;
43
use std::io::Write;
54
mod array_access;
65
#[cfg(test)]
@@ -11,9 +10,95 @@ mod recursion;
1110
pub(crate) fn init_logging() {
1211
if !log_enabled!(Level::Debug) {
1312
env::set_var("RUST_LOG", "debug");
14-
env_logger::builder()
13+
let _ = env_logger::builder()
1514
.format(|buf, record| writeln!(buf, " {}", record.args()))
16-
.init();
15+
.try_init();
1716
log::set_max_level(LevelFilter::Debug);
1817
}
1918
}
19+
20+
#[cfg(test)]
21+
mod test {
22+
use plonky2::{
23+
field::extension::Extendable,
24+
hash::hash_types::RichField,
25+
iop::witness::PartialWitness,
26+
plonk::{
27+
circuit_builder::CircuitBuilder, circuit_data::CircuitConfig, config::GenericConfig,
28+
},
29+
};
30+
use serde::Serialize;
31+
use std::time;
32+
33+
use crate::{circuit::UserCircuit, utils::verify_proof_tuple};
34+
35+
#[derive(Serialize, Clone, Debug)]
36+
pub(crate) struct BenchResult {
37+
pub circuit: String,
38+
// n is circuit dependent
39+
pub n: usize,
40+
// arity is 0 when it's not recursive, 1 when ivc and more for PCD
41+
pub arity: usize,
42+
pub gate_count: usize,
43+
pub building: u64,
44+
pub proving: u64,
45+
pub lde: usize,
46+
pub verifying: u64,
47+
}
48+
49+
pub fn run_benchs(fname: String, benches: Vec<Box<dyn FnOnce() -> BenchResult>>) {
50+
let mut writer = csv::Writer::from_path(fname).unwrap();
51+
for bench in benches {
52+
let result = bench();
53+
writer.serialize(result).unwrap();
54+
writer.flush().unwrap();
55+
}
56+
}
57+
58+
pub trait Benchable {
59+
// returns the relevant information depending on the circuit being benchmarked
60+
// i.e. n can be the number of times we hash some fixed length data
61+
fn n(&self) -> usize {
62+
0
63+
}
64+
}
65+
66+
pub fn bench_simple_circuit<
67+
F,
68+
const D: usize,
69+
C: GenericConfig<D, F = F>,
70+
U: UserCircuit<F, D> + Benchable,
71+
>(
72+
tname: String,
73+
u: U,
74+
) -> BenchResult
75+
where
76+
F: RichField + Extendable<D>,
77+
{
78+
let mut b = CircuitBuilder::new(CircuitConfig::standard_recursion_config());
79+
let mut pw = PartialWitness::new();
80+
let now = time::Instant::now();
81+
let wires = U::build(&mut b);
82+
let gate_count = b.num_gates();
83+
let circuit_data = b.build::<C>();
84+
let building_time = now.elapsed();
85+
let now = time::Instant::now();
86+
u.prove(&mut pw, &wires);
87+
let proof = circuit_data.prove(pw).expect("invalid proof");
88+
let proving_time = now.elapsed();
89+
let lde = circuit_data.common.lde_size();
90+
let now = time::Instant::now();
91+
verify_proof_tuple(&(proof, circuit_data.verifier_only, circuit_data.common)).unwrap();
92+
let verifying_time = now.elapsed();
93+
BenchResult {
94+
circuit: tname,
95+
gate_count,
96+
n: u.n(),
97+
arity: 0,
98+
lde,
99+
building: building_time.as_millis() as u64,
100+
proving: proving_time.as_millis() as u64,
101+
verifying: verifying_time.as_millis() as u64,
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)