Skip to content

Commit

Permalink
Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgoergens committed May 9, 2024
1 parent 6399fec commit ef99615
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 464 deletions.
42 changes: 0 additions & 42 deletions circuits/src/benches/benches.rs

This file was deleted.

43 changes: 42 additions & 1 deletion circuits/src/benches/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
pub mod benches;
pub mod nop;
pub mod omni;
pub mod poseidon2;
pub mod sort;
pub mod xor;

use std::time::Duration;

use anyhow::Result;
pub use mozak_cli_args::bench_args::{BenchArgs, BenchFunction};
use nop::NopBench;
use omni::OmniBench;
use poseidon2::Poseidon2Bench;
use sort::{SortBench, SortBenchRecursive};
use xor::XorBench;

pub(crate) trait Bench {
type Args;
type Prepared;

/// method to be executed to prepare the benchmark
fn prepare(&self, args: &Self::Args) -> Self::Prepared;

/// actual benchmark function, whose execution time is
/// to be measured
fn execute(&self, prepared: Self::Prepared) -> Result<()>;

/// benchmark the `execute` function implemented through the
/// trait `Bench`
fn bench(&self, args: &Self::Args) -> Result<Duration> {
let prepared = self.prepare(args);
let start = std::time::Instant::now();
self.execute(prepared)?;
Ok(start.elapsed())
}
}

pub fn bench(args: &BenchArgs) -> Result<Duration> {
match &args.function {
BenchFunction::XorBench { iterations } => XorBench.bench(iterations),
BenchFunction::NopBench { iterations } => NopBench.bench(iterations),
BenchFunction::OmniBench { iterations } => OmniBench.bench(iterations),
BenchFunction::Poseidon2Bench { input_len } => Poseidon2Bench.bench(input_len),
BenchFunction::SortBench { n } => SortBench.bench(n),
BenchFunction::SortBenchRecursive { n } => SortBenchRecursive.bench(n),
}
}
71 changes: 31 additions & 40 deletions circuits/src/benches/nop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,49 @@ use mozak_runner::instruction::{Args, Instruction, Op, NOP};
use mozak_runner::vm::ExecutionRecord;
use starky::config::StarkConfig;

use super::benches::Bench;
use super::Bench;
use crate::test_utils::{prove_and_verify_mozak_stark, F};

#[allow(clippy::module_name_repetitions)]
pub fn nop_execute((program, record): (Program, ExecutionRecord<F>)) -> Result<(), anyhow::Error> {
prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config())
}

#[allow(clippy::module_name_repetitions)]
#[must_use]
pub fn nop_prepare(iterations: u32) -> (Program, ExecutionRecord<F>) {
let instructions = [
Instruction {
op: Op::ADD,
args: Args {
rd: 1,
rs1: 1,
imm: 1_u32.wrapping_neg(),
..Args::default()
},
},
NOP,
Instruction {
op: Op::BLT,
args: Args {
rs1: 0,
rs2: 1,
imm: 0,
..Args::default()
},
},
];
code::execute(instructions, &[], &[(1, iterations)])
}

pub(crate) struct NopBench;

impl Bench for NopBench {
type Args = u32;
type Prepared = (Program, ExecutionRecord<F>);

fn prepare(&self, args: &Self::Args) -> Self::Prepared { nop_prepare(*args) }
fn prepare(&self, &iterations: &u32) -> Self::Prepared {
let instructions = [
Instruction {
op: Op::ADD,
args: Args {
rd: 1,
rs1: 1,
imm: 1_u32.wrapping_neg(),
..Args::default()
},
},
NOP,
Instruction {
op: Op::BLT,
args: Args {
rs1: 0,
rs2: 1,
imm: 0,
..Args::default()
},
},
];
code::execute(instructions, &[], &[(1, iterations)])
}

fn execute(&self, prepared: Self::Prepared) -> anyhow::Result<()> { nop_execute(prepared) }
fn execute(&self, (program, record): (Program, ExecutionRecord<F>)) -> anyhow::Result<()> {
prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config())
}
}
#[cfg(test)]
mod tests {
use super::{nop_execute, nop_prepare};
use super::NopBench;
use crate::benches::Bench;

#[test]
fn test_nop_bench() -> anyhow::Result<()> {
let iterations = 10;
nop_execute(nop_prepare(iterations))
}
fn test_nop_bench() -> anyhow::Result<()> { NopBench {}.execute(NopBench {}.prepare(&10)) }
}
Loading

0 comments on commit ef99615

Please sign in to comment.