From 882a88b8de7a378f6f7ccbd499f81868fd4587c3 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 10:39:41 +0800 Subject: [PATCH 01/15] Separate out clap derive stuff --- Cargo.lock | 23 ++++++++++ Cargo.toml | 1 + args/Cargo.toml | 38 +++++++++++++++++ args/src/bench_args.rs | 31 ++++++++++++++ args/src/lib.rs | 76 +++++++++++++++++++++++++++++++++ cli/Cargo.toml | 1 + cli/src/cli_benches/benches.rs | 50 ++++------------------ cli/src/main.rs | 77 +++------------------------------- 8 files changed, 185 insertions(+), 112 deletions(-) create mode 100644 args/Cargo.toml create mode 100644 args/src/bench_args.rs create mode 100644 args/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index af572e579..88ccc2f3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -962,6 +962,7 @@ dependencies = [ "itertools 0.12.1", "log", "mozak-circuits", + "mozak-cli-args", "mozak-examples", "mozak-node", "mozak-runner", @@ -977,6 +978,28 @@ dependencies = [ "tempfile", ] +[[package]] +name = "mozak-cli-args" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "clap-verbosity-flag", + "clap_derive", + "clio", + "criterion", + "env_logger", + "itertools 0.12.1", + "log", + "proptest", + "rkyv", + "rkyv_derive", + "serde", + "serde_json", + "starky", + "tempfile", +] + [[package]] name = "mozak-examples" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 20151305e..d28d26c84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ [workspace] exclude = ["sdk"] members = [ + "args", "circuits", "cli", "examples-builder", diff --git a/args/Cargo.toml b/args/Cargo.toml new file mode 100644 index 000000000..5415320c6 --- /dev/null +++ b/args/Cargo.toml @@ -0,0 +1,38 @@ +[package] +categories = ["cryptography"] +description = "MozakVM cli" +edition = "2021" +keywords = ["crypto", "zero-knowledge", "vm"] +license = "All rights reserved" +name = "mozak-cli-args" +readme = "README.md" +repository = "https://github.com/0xmozak/mozak-vm" +version = "0.1.0" + +[dependencies] +clap = { version = "4.5", features = [ + "derive", + "cargo", + "env", + "unicode", +] } +# TODO(Matthias): implement shell completion for CLI via clap_complete +# clap_complete = "4.3" +anyhow = "1.0" +clap-verbosity-flag = "2.2" +clap_derive = "4.5" +clio = { version = "0.3", features = ["clap-parse"] } +env_logger = "0.11" +itertools = "0.12" +log = "0.4" +rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } +rkyv_derive = "=0.8.0-alpha.1" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +starky = { workspace = true, default-features = false } +tempfile = "3" + +[dev-dependencies] +# Only as a workaround to be able to control criterion's rayon feature and optional dependency. +criterion = { workspace = true, default-features = false } +proptest = "1.4" diff --git a/args/src/bench_args.rs b/args/src/bench_args.rs new file mode 100644 index 000000000..42f943bb7 --- /dev/null +++ b/args/src/bench_args.rs @@ -0,0 +1,31 @@ +use clap::{Args as Args_, Subcommand}; + +#[derive(Debug, Args_, Clone)] +#[command(args_conflicts_with_subcommands = true)] +pub struct BenchArgs { + #[command(subcommand)] + pub function: BenchFunction, +} + +#[derive(PartialEq, Debug, Subcommand, Clone)] +pub enum BenchFunction { + XorBench { + iterations: u32, + }, + NopBench { + iterations: u32, + }, + Poseidon2Bench { + input_len: u32, + }, + /// Benchmarks (almost) every instruction. + OmniBench { + iterations: u32, + }, + SortBench { + n: u32, + }, + SortBenchRecursive { + n: u32, + }, +} diff --git a/args/src/lib.rs b/args/src/lib.rs new file mode 100644 index 000000000..3d1981a16 --- /dev/null +++ b/args/src/lib.rs @@ -0,0 +1,76 @@ +#![deny(clippy::pedantic)] +#![deny(clippy::cargo)] +#![allow(clippy::multiple_crate_versions)] +pub mod bench_args; + +use bench_args::BenchArgs; +use clap::{Parser, Subcommand}; +use clap_derive::Args; +use clio::{Input, Output}; + +#[derive(Parser, Debug, Clone)] +#[command(author, version, about, long_about = None)] +pub struct Cli { + #[clap(flatten)] + pub verbose: clap_verbosity_flag::Verbosity, + #[command(subcommand)] + pub command: Command, + /// Debug API, default is OFF, currently only `prove` command is supported + #[arg(short, long)] + pub debug: bool, +} + +#[derive(Clone, Debug, Args)] +pub struct RunArgs { + pub elf: Input, + #[arg(long)] + pub system_tape: Option, + #[arg(long)] + pub self_prog_id: Option, +} + +#[derive(Clone, Debug, Args)] +pub struct ProveArgs { + pub elf: Input, + pub proof: Output, + #[arg(long)] + pub system_tape: Option, + #[arg(long)] + pub self_prog_id: Option, + pub recursive_proof: Option, +} + +#[derive(Clone, Debug, Subcommand)] +pub enum Command { + /// Decode a given ELF and prints the program + Decode { elf: Input }, + /// Decode and execute a given ELF. Prints the final state of + /// the registers + Run(RunArgs), + /// Prove and verify the execution of a given ELF + ProveAndVerify(RunArgs), + /// Prove the execution of given ELF and write proof to file. + Prove(ProveArgs), + /// Verify the given proof from file. + Verify { proof: Input }, + /// Verify the given recursive proof from file. + VerifyRecursiveProof { proof: Input, verifier_key: Input }, + /// Builds a transaction bundle. + BundleTransaction { + /// System tape generated from native execution. + #[arg(long, required = true)] + system_tape: Input, + /// Output file path of the serialized bundle. + #[arg(long, default_value = "bundle")] + bundle: Output, + }, + /// Compute the Program Rom Hash of the given ELF. + ProgramRomHash { elf: Input }, + /// Compute the Memory Init Hash of the given ELF. + MemoryInitHash { elf: Input }, + /// Bench the function with given parameters + Bench(BenchArgs), +} + +#[allow(non_upper_case_globals)] +pub const parse: fn() -> Cli = Cli::parse; diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ef5bda002..ad7e13e08 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -29,6 +29,7 @@ clio = { version = "0.3", features = ["clap-parse"] } env_logger = "0.11" itertools = "0.12" log = "0.4" +mozak-cli-args = { path = "../args" } mozak-examples = { path = "../examples-builder", features = ["mozak-sort"] } plonky2 = { workspace = true, default-features = false } plonky2_maybe_rayon = { workspace = true, default-features = false } diff --git a/cli/src/cli_benches/benches.rs b/cli/src/cli_benches/benches.rs index dfe75d120..303d42b67 100644 --- a/cli/src/cli_benches/benches.rs +++ b/cli/src/cli_benches/benches.rs @@ -1,7 +1,7 @@ use std::time::Duration; use anyhow::Result; -use clap::{Args as Args_, Subcommand}; +pub use mozak_cli_args::bench_args::{BenchArgs, BenchFunction}; use super::nop::NopBench; use super::omni::OmniBench; @@ -9,13 +9,6 @@ use super::poseidon2::Poseidon2Bench; use super::sort::{SortBench, SortBenchRecursive}; use super::xor::XorBench; -#[derive(Debug, Args_, Clone)] -#[command(args_conflicts_with_subcommands = true)] -pub struct BenchArgs { - #[command(subcommand)] - pub function: BenchFunction, -} - pub(crate) trait Bench { type Args; type Prepared; @@ -37,38 +30,13 @@ pub(crate) trait Bench { } } -#[derive(PartialEq, Debug, Subcommand, Clone)] -pub enum BenchFunction { - XorBench { - iterations: u32, - }, - NopBench { - iterations: u32, - }, - Poseidon2Bench { - input_len: u32, - }, - /// Benchmarks (almost) every instruction. - OmniBench { - iterations: u32, - }, - SortBench { - n: u32, - }, - SortBenchRecursive { - n: u32, - }, -} - -impl BenchArgs { - pub fn bench(&self) -> Result { - match &self.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), - } +pub fn bench(args: &BenchArgs) -> Result { + 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), } } diff --git a/cli/src/main.rs b/cli/src/main.rs index 55bbf90c3..7862b711d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -8,9 +8,7 @@ use std::io::{Read, Write}; use std::path::PathBuf; use anyhow::Result; -use clap::{Parser, Subcommand}; -use clap_derive::Args; -use clio::{Input, Output}; +use clio::Input; use itertools::Itertools; use log::debug; use mozak_circuits::generation::memoryinit::generate_elf_memory_init_trace; @@ -27,8 +25,9 @@ use mozak_circuits::stark::recursive_verifier::{ use mozak_circuits::stark::utils::trace_rows_to_poly_values; use mozak_circuits::stark::verifier::verify_proof; use mozak_circuits::test_utils::{prove_and_verify_mozak_stark, C, D, F, S}; -use mozak_cli::cli_benches::benches::BenchArgs; +use mozak_cli::cli_benches::benches::bench; use mozak_cli::runner::{deserialize_system_tape, load_program, raw_tapes_from_system_tape}; +use mozak_cli_args::{parse, Command, ProveArgs, RunArgs}; use mozak_node::types::{Attestation, Transaction}; use mozak_runner::state::{RawTapes, State}; use mozak_runner::vm::step; @@ -42,75 +41,11 @@ use starky::config::StarkConfig; const PROGRAMS_MAP_JSON: &str = "examples/programs_map.json"; -#[derive(Parser, Debug, Clone)] -#[command(author, version, about, long_about = None)] -struct Cli { - #[clap(flatten)] - verbose: clap_verbosity_flag::Verbosity, - #[command(subcommand)] - command: Command, - /// Debug API, default is OFF, currently only `prove` command is supported - #[arg(short, long)] - debug: bool, -} - -#[derive(Clone, Debug, Args)] -pub struct RunArgs { - elf: Input, - #[arg(long)] - system_tape: Option, - #[arg(long)] - self_prog_id: Option, -} - -#[derive(Clone, Debug, Args)] -pub struct ProveArgs { - elf: Input, - proof: Output, - #[arg(long)] - system_tape: Option, - #[arg(long)] - self_prog_id: Option, - recursive_proof: Option, -} - -#[derive(Clone, Debug, Subcommand)] -enum Command { - /// Decode a given ELF and prints the program - Decode { elf: Input }, - /// Decode and execute a given ELF. Prints the final state of - /// the registers - Run(RunArgs), - /// Prove and verify the execution of a given ELF - ProveAndVerify(RunArgs), - /// Prove the execution of given ELF and write proof to file. - Prove(ProveArgs), - /// Verify the given proof from file. - Verify { proof: Input }, - /// Verify the given recursive proof from file. - VerifyRecursiveProof { proof: Input, verifier_key: Input }, - /// Builds a transaction bundle. - BundleTransaction { - /// System tape generated from native execution. - #[arg(long, required = true)] - system_tape: Input, - /// Output file path of the serialized bundle. - #[arg(long, default_value = "bundle")] - bundle: Output, - }, - /// Compute the Program Rom Hash of the given ELF. - ProgramRomHash { elf: Input }, - /// Compute the Memory Init Hash of the given ELF. - MemoryInitHash { elf: Input }, - /// Bench the function with given parameters - Bench(BenchArgs), -} - /// Run me eg like `cargo run -- -vvv run vm/tests/testdata/rv32ui-p-addi /// iotape.txt` #[allow(clippy::too_many_lines)] fn main() -> Result<()> { - let cli = Cli::parse(); + let cli = parse(); let config = StarkConfig::standard_fast_config(); env_logger::Builder::new() .filter_level(cli.verbose.log_level_filter()) @@ -415,8 +350,8 @@ fn main() -> Result<()> { let trace_cap = trace_commitment.merkle_tree.cap; println!("{trace_cap:?}"); } - Command::Bench(bench) => { - let time_taken = bench.bench()?.as_secs_f64(); + Command::Bench(args) => { + let time_taken = bench(&args)?.as_secs_f64(); println!("{time_taken}"); } } From 15f8e1009ad435422e48ee3b504424cafcd049fd Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 10:42:15 +0800 Subject: [PATCH 02/15] Clean up --- Cargo.lock | 11 ----------- args/Cargo.toml | 8 -------- cli/Cargo.toml | 20 +++++--------------- 3 files changed, 5 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88ccc2f3a..0b7b15459 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -953,9 +953,6 @@ name = "mozak-cli" version = "0.1.0" dependencies = [ "anyhow", - "clap", - "clap-verbosity-flag", - "clap_derive", "clio", "criterion", "env_logger", @@ -982,21 +979,13 @@ dependencies = [ name = "mozak-cli-args" version = "0.1.0" dependencies = [ - "anyhow", "clap", "clap-verbosity-flag", "clap_derive", "clio", "criterion", - "env_logger", - "itertools 0.12.1", "log", "proptest", - "rkyv", - "rkyv_derive", - "serde", - "serde_json", - "starky", "tempfile", ] diff --git a/args/Cargo.toml b/args/Cargo.toml index 5415320c6..5b2d3eb94 100644 --- a/args/Cargo.toml +++ b/args/Cargo.toml @@ -18,18 +18,10 @@ clap = { version = "4.5", features = [ ] } # TODO(Matthias): implement shell completion for CLI via clap_complete # clap_complete = "4.3" -anyhow = "1.0" clap-verbosity-flag = "2.2" clap_derive = "4.5" clio = { version = "0.3", features = ["clap-parse"] } -env_logger = "0.11" -itertools = "0.12" log = "0.4" -rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } -rkyv_derive = "=0.8.0-alpha.1" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -starky = { workspace = true, default-features = false } tempfile = "3" [dev-dependencies] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ad7e13e08..01fc6d636 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -10,27 +10,17 @@ repository = "https://github.com/0xmozak/mozak-vm" version = "0.1.0" [dependencies] -clap = { version = "4.5", features = [ - "derive", - "cargo", - "env", - "unicode", -] } -mozak-circuits = { path = "../circuits", features = ["test"] } -mozak-node = { path = "../node", features = ["std"] } -mozak-runner = { path = "../runner", features = ["test"] } -mozak-sdk = { path = "../sdk", features = ["std"] } -# TODO(Matthias): implement shell completion for CLI via clap_complete -# clap_complete = "4.3" anyhow = "1.0" -clap-verbosity-flag = "2.2" -clap_derive = "4.5" -clio = { version = "0.3", features = ["clap-parse"] } +clio = { version = "0.3" } env_logger = "0.11" itertools = "0.12" log = "0.4" +mozak-circuits = { path = "../circuits", features = ["test"] } mozak-cli-args = { path = "../args" } mozak-examples = { path = "../examples-builder", features = ["mozak-sort"] } +mozak-node = { path = "../node", features = ["std"] } +mozak-runner = { path = "../runner", features = ["test"] } +mozak-sdk = { path = "../sdk", features = ["std"] } plonky2 = { workspace = true, default-features = false } plonky2_maybe_rayon = { workspace = true, default-features = false } rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } From 6bda1ada4b1080b0698055013d04ab45e85398b7 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 12:09:34 +0800 Subject: [PATCH 03/15] Move stuff --- Cargo.lock | 2 + circuits/Cargo.toml | 2 + {cli => circuits}/src/cli_benches/benches.rs | 0 {cli => circuits}/src/cli_benches/mod.rs | 0 {cli => circuits}/src/cli_benches/nop.rs | 3 +- {cli => circuits}/src/cli_benches/omni.rs | 2 +- .../src/cli_benches/poseidon2.rs | 4 +- {cli => circuits}/src/cli_benches/sort.rs | 14 ++-- {cli => circuits}/src/cli_benches/xor.rs | 2 +- circuits/src/expr.rs | 2 + circuits/src/lib.rs | 2 + circuits/tests/riscv_tests.rs | 72 ++----------------- cli/src/lib.rs | 1 - cli/src/main.rs | 2 +- examples-builder/src/lib.rs | 61 ++++++++++++++++ 15 files changed, 88 insertions(+), 81 deletions(-) rename {cli => circuits}/src/cli_benches/benches.rs (100%) rename {cli => circuits}/src/cli_benches/mod.rs (100%) rename {cli => circuits}/src/cli_benches/nop.rs (95%) rename {cli => circuits}/src/cli_benches/omni.rs (99%) rename {cli => circuits}/src/cli_benches/poseidon2.rs (91%) rename {cli => circuits}/src/cli_benches/sort.rs (90%) rename {cli => circuits}/src/cli_benches/xor.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index 0b7b15459..8c00d05ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -924,6 +924,8 @@ dependencies = [ "itertools 0.12.1", "log", "mozak-circuits-derive", + "mozak-cli-args", + "mozak-examples", "mozak-runner", "mozak-sdk", "plonky2", diff --git a/circuits/Cargo.toml b/circuits/Cargo.toml index 74efdb20d..8f3203996 100644 --- a/circuits/Cargo.toml +++ b/circuits/Cargo.toml @@ -17,7 +17,9 @@ derive_more = { version = "1.0.0-beta.6", features = ["full"] } expr = { path = "../expr" } itertools = "0.12" log = "0.4" +mozak-cli-args = { path = "../args" } mozak-circuits-derive = { path = "./derive" } +mozak-examples = { path = "../examples-builder" } mozak-runner = { path = "../runner" } mozak-sdk = { path = "../sdk" } plonky2 = { workspace = true, default-features = false } diff --git a/cli/src/cli_benches/benches.rs b/circuits/src/cli_benches/benches.rs similarity index 100% rename from cli/src/cli_benches/benches.rs rename to circuits/src/cli_benches/benches.rs diff --git a/cli/src/cli_benches/mod.rs b/circuits/src/cli_benches/mod.rs similarity index 100% rename from cli/src/cli_benches/mod.rs rename to circuits/src/cli_benches/mod.rs diff --git a/cli/src/cli_benches/nop.rs b/circuits/src/cli_benches/nop.rs similarity index 95% rename from cli/src/cli_benches/nop.rs rename to circuits/src/cli_benches/nop.rs index 8d60587db..0a9787b68 100644 --- a/cli/src/cli_benches/nop.rs +++ b/circuits/src/cli_benches/nop.rs @@ -1,4 +1,3 @@ -use mozak_circuits::test_utils::{prove_and_verify_mozak_stark, F}; use mozak_runner::code; use mozak_runner::elf::Program; use mozak_runner::instruction::{Args, Instruction, Op, NOP}; @@ -6,11 +5,13 @@ use mozak_runner::vm::ExecutionRecord; use starky::config::StarkConfig; use super::benches::Bench; +use crate::test_utils::{prove_and_verify_mozak_stark, F}; #[allow(clippy::module_name_repetitions)] pub fn nop_execute((program, record): (Program, ExecutionRecord)) -> Result<(), anyhow::Error> { prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) } + pub fn nop_prepare(iterations: u32) -> (Program, ExecutionRecord) { let instructions = [ Instruction { diff --git a/cli/src/cli_benches/omni.rs b/circuits/src/cli_benches/omni.rs similarity index 99% rename from cli/src/cli_benches/omni.rs rename to circuits/src/cli_benches/omni.rs index 3573abd43..40525602a 100644 --- a/cli/src/cli_benches/omni.rs +++ b/circuits/src/cli_benches/omni.rs @@ -1,4 +1,3 @@ -use mozak_circuits::test_utils::{prove_and_verify_mozak_stark, F}; use mozak_runner::code; use mozak_runner::elf::Program; use mozak_runner::instruction::{Args, Instruction, Op}; @@ -6,6 +5,7 @@ use mozak_runner::vm::ExecutionRecord; use starky::config::StarkConfig; use super::benches::Bench; +use crate::test_utils::{prove_and_verify_mozak_stark, F}; pub fn omni_execute((program, record): (Program, ExecutionRecord)) -> Result<(), anyhow::Error> { prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) diff --git a/cli/src/cli_benches/poseidon2.rs b/circuits/src/cli_benches/poseidon2.rs similarity index 91% rename from cli/src/cli_benches/poseidon2.rs rename to circuits/src/cli_benches/poseidon2.rs index 32fb3ef0c..807de5406 100644 --- a/cli/src/cli_benches/poseidon2.rs +++ b/circuits/src/cli_benches/poseidon2.rs @@ -1,11 +1,9 @@ -use mozak_circuits::test_utils::{ - create_poseidon2_test, prove_and_verify_mozak_stark, Poseidon2Test, F, -}; use mozak_runner::elf::Program; use mozak_runner::vm::ExecutionRecord; use starky::config::StarkConfig; use super::benches::Bench; +use crate::test_utils::{create_poseidon2_test, prove_and_verify_mozak_stark, Poseidon2Test, F}; pub fn poseidon2_execute( (program, record): (Program, ExecutionRecord), diff --git a/cli/src/cli_benches/sort.rs b/circuits/src/cli_benches/sort.rs similarity index 90% rename from cli/src/cli_benches/sort.rs rename to circuits/src/cli_benches/sort.rs index 7104b1c45..44df40251 100644 --- a/cli/src/cli_benches/sort.rs +++ b/circuits/src/cli_benches/sort.rs @@ -1,12 +1,4 @@ use anyhow::Result; -use mozak_circuits::stark::mozak_stark::{MozakStark, PublicInputs}; -use mozak_circuits::stark::proof::AllProof; -use mozak_circuits::stark::prover::prove; -use mozak_circuits::stark::recursive_verifier::{ - recursive_mozak_stark_circuit, MozakStarkVerifierCircuit, -}; -use mozak_circuits::stark::verifier::verify_proof; -use mozak_circuits::test_utils::{prove_and_verify_mozak_stark, C, D, F}; use mozak_examples::MOZAK_SORT_ELF; use mozak_runner::elf::Program; use mozak_runner::state::{RawTapes, State}; @@ -17,6 +9,12 @@ use plonky2::util::timing::TimingTree; use starky::config::StarkConfig; use super::benches::Bench; +use crate::stark::mozak_stark::{MozakStark, PublicInputs}; +use crate::stark::proof::AllProof; +use crate::stark::prover::prove; +use crate::stark::recursive_verifier::{recursive_mozak_stark_circuit, MozakStarkVerifierCircuit}; +use crate::stark::verifier::verify_proof; +use crate::test_utils::{prove_and_verify_mozak_stark, C, D, F}; pub fn sort_execute(result: Result<(Program, ExecutionRecord)>) -> Result<()> { let (program, record) = result?; diff --git a/cli/src/cli_benches/xor.rs b/circuits/src/cli_benches/xor.rs similarity index 96% rename from cli/src/cli_benches/xor.rs rename to circuits/src/cli_benches/xor.rs index cc4d3af84..51d2c6748 100644 --- a/cli/src/cli_benches/xor.rs +++ b/circuits/src/cli_benches/xor.rs @@ -1,4 +1,3 @@ -use mozak_circuits::test_utils::{prove_and_verify_mozak_stark, F}; use mozak_runner::code; use mozak_runner::elf::Program; use mozak_runner::instruction::{Args, Instruction, Op}; @@ -6,6 +5,7 @@ use mozak_runner::vm::ExecutionRecord; use starky::config::StarkConfig; use super::benches::Bench; +use crate::test_utils::{prove_and_verify_mozak_stark, F}; #[allow(clippy::module_name_repetitions)] pub fn xor_execute((program, record): (Program, ExecutionRecord)) -> Result<(), anyhow::Error> { diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index c1b3bc17d..8b1f64953 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -10,6 +10,8 @@ use plonky2::iop::ext_target::ExtensionTarget; use plonky2::plonk::circuit_builder::CircuitBuilder; use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; +pub const Food: i32 = 7; + struct CircuitBuilderEvaluator<'a, F, const D: usize> where F: RichField, diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index 59fbeb163..115b644ee 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -4,9 +4,11 @@ // exceptions: #![allow(clippy::missing_panics_doc)] #![allow(clippy::missing_errors_doc)] +#![allow(clippy::multiple_crate_versions)] #![feature(const_trait_impl)] pub mod bitshift; +pub mod cli_benches; pub mod columns_view; pub mod cpu; pub mod cross_table_lookup; diff --git a/circuits/tests/riscv_tests.rs b/circuits/tests/riscv_tests.rs index dd79f3978..6cf64e6f8 100644 --- a/circuits/tests/riscv_tests.rs +++ b/circuits/tests/riscv_tests.rs @@ -1,11 +1,10 @@ -use std::include_bytes; - -use anyhow::Result; +use anyhow::{Ok, Result}; use mozak_circuits::test_utils::prove_and_verify_mozak_stark; use mozak_runner::elf::Program; use mozak_runner::state::State; use mozak_runner::vm::step; use plonky2::field::goldilocks_field::GoldilocksField; +use plonky2_maybe_rayon::*; use starky::config::StarkConfig; /// This function takes the contents of a compiled ELF and runs it through the @@ -38,66 +37,9 @@ fn run_test(elf: &[u8]) -> Result<()> { Ok(()) } -/// This macro takes in an identifier as the test name and the file name of a -/// compiled ELF, and sets up a `run_test` for it. -macro_rules! test_elf { - ($test_name:ident, $file_name:tt) => { - #[test] - fn $test_name() -> Result<()> { - run_test(include_bytes!(concat!( - "../../riscv-testdata/testdata/", - $file_name - ))) - } - }; +#[test] +fn riscv_tests() { + mozak_examples::riscv_tests.into_par_iter().for_each(|elf| { + run_test(elf).unwrap(); + }) } - -// Base instruction set -test_elf!(add, "rv32ui-p-add"); -test_elf!(addi, "rv32ui-p-addi"); -test_elf!(and, "rv32ui-p-and"); -test_elf!(andi, "rv32ui-p-andi"); -test_elf!(auipc, "rv32ui-p-auipc"); -test_elf!(beq, "rv32ui-p-beq"); -test_elf!(bge, "rv32ui-p-bge"); -test_elf!(bgeu, "rv32ui-p-bgeu"); -test_elf!(blt, "rv32ui-p-blt"); -test_elf!(bltu, "rv32ui-p-bltu"); -test_elf!(bne, "rv32ui-p-bne"); -test_elf!(jal, "rv32ui-p-jal"); -test_elf!(jalr, "rv32ui-p-jalr"); -test_elf!(lb, "rv32ui-p-lb"); -test_elf!(lbu, "rv32ui-p-lbu"); -test_elf!(lh, "rv32ui-p-lh"); -test_elf!(lhu, "rv32ui-p-lhu"); -test_elf!(lui, "rv32ui-p-lui"); -test_elf!(lw, "rv32ui-p-lw"); -test_elf!(or, "rv32ui-p-or"); -test_elf!(ori, "rv32ui-p-ori"); -test_elf!(sb, "rv32ui-p-sb"); -test_elf!(sh, "rv32ui-p-sh"); -test_elf!(simple, "rv32ui-p-simple"); -test_elf!(sll, "rv32ui-p-sll"); -test_elf!(slli, "rv32ui-p-slli"); -test_elf!(slt, "rv32ui-p-slt"); -test_elf!(slti, "rv32ui-p-slti"); -test_elf!(sltiu, "rv32ui-p-sltiu"); -test_elf!(sltu, "rv32ui-p-sltu"); -test_elf!(sra, "rv32ui-p-sra"); -test_elf!(srai, "rv32ui-p-srai"); -test_elf!(srl, "rv32ui-p-srl"); -test_elf!(srli, "rv32ui-p-srli"); -test_elf!(sub, "rv32ui-p-sub"); -test_elf!(sw, "rv32ui-p-sw"); -test_elf!(xor, "rv32ui-p-xor"); -test_elf!(xori, "rv32ui-p-xori"); - -// M extension -test_elf!(div, "rv32um-p-div"); -test_elf!(divu, "rv32um-p-divu"); -test_elf!(mul, "rv32um-p-mul"); -test_elf!(mulh, "rv32um-p-mulh"); -test_elf!(mulhsu, "rv32um-p-mulhsu"); -test_elf!(mulhu, "rv32um-p-mulhu"); -test_elf!(rem, "rv32um-p-rem"); -test_elf!(remu, "rv32um-p-remu"); diff --git a/cli/src/lib.rs b/cli/src/lib.rs index c30e7ac46..e174d82fc 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1,4 +1,3 @@ -pub mod cli_benches; pub mod runner; #[cfg(test)] mod tests; diff --git a/cli/src/main.rs b/cli/src/main.rs index 7862b711d..49b67c624 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -11,6 +11,7 @@ use anyhow::Result; use clio::Input; use itertools::Itertools; use log::debug; +use mozak_circuits::cli_benches::benches::bench; use mozak_circuits::generation::memoryinit::generate_elf_memory_init_trace; use mozak_circuits::generation::storage_device::generate_call_tape_trace; use mozak_circuits::program::generation::generate_program_rom_trace; @@ -25,7 +26,6 @@ use mozak_circuits::stark::recursive_verifier::{ use mozak_circuits::stark::utils::trace_rows_to_poly_values; use mozak_circuits::stark::verifier::verify_proof; use mozak_circuits::test_utils::{prove_and_verify_mozak_stark, C, D, F, S}; -use mozak_cli::cli_benches::benches::bench; use mozak_cli::runner::{deserialize_system_tape, load_program, raw_tapes_from_system_tape}; use mozak_cli_args::{parse, Command, ProveArgs, RunArgs}; use mozak_node::types::{Attestation, Transaction}; diff --git a/examples-builder/src/lib.rs b/examples-builder/src/lib.rs index fdb344ff9..7e0a50958 100644 --- a/examples-builder/src/lib.rs +++ b/examples-builder/src/lib.rs @@ -1 +1,62 @@ include!(concat!(env!("OUT_DIR"), "/vars.rs")); + +/// This macro takes in an identifier as the test name and the file name of a +/// compiled ELF, and sets up a `run_test` for it. +macro_rules! load_test_elf { + ($file_name:tt) => { + include_bytes!(concat!("../../riscv-testdata/testdata/", $file_name)) + }; +} + +// TODO: fix the macro. +#[allow(non_upper_case_globals)] +pub static riscv_tests: &[&[u8]] = &[ + // Base instruction set + load_test_elf!("rv32ui-p-add"), + load_test_elf!("rv32ui-p-addi"), + load_test_elf!("rv32ui-p-and"), + load_test_elf!("rv32ui-p-andi"), + load_test_elf!("rv32ui-p-auipc"), + load_test_elf!("rv32ui-p-beq"), + load_test_elf!("rv32ui-p-bge"), + load_test_elf!("rv32ui-p-bgeu"), + load_test_elf!("rv32ui-p-blt"), + load_test_elf!("rv32ui-p-bltu"), + load_test_elf!("rv32ui-p-bne"), + load_test_elf!("rv32ui-p-jal"), + load_test_elf!("rv32ui-p-jalr"), + load_test_elf!("rv32ui-p-lb"), + load_test_elf!("rv32ui-p-lbu"), + load_test_elf!("rv32ui-p-lh"), + load_test_elf!("rv32ui-p-lhu"), + load_test_elf!("rv32ui-p-lui"), + load_test_elf!("rv32ui-p-lw"), + load_test_elf!("rv32ui-p-or"), + load_test_elf!("rv32ui-p-ori"), + load_test_elf!("rv32ui-p-sb"), + load_test_elf!("rv32ui-p-sh"), + load_test_elf!("rv32ui-p-simple"), + load_test_elf!("rv32ui-p-sll"), + load_test_elf!("rv32ui-p-slli"), + load_test_elf!("rv32ui-p-slt"), + load_test_elf!("rv32ui-p-slti"), + load_test_elf!("rv32ui-p-sltiu"), + load_test_elf!("rv32ui-p-sltu"), + load_test_elf!("rv32ui-p-sra"), + load_test_elf!("rv32ui-p-srai"), + load_test_elf!("rv32ui-p-srl"), + load_test_elf!("rv32ui-p-srli"), + load_test_elf!("rv32ui-p-sub"), + load_test_elf!("rv32ui-p-sw"), + load_test_elf!("rv32ui-p-xor"), + load_test_elf!("rv32ui-p-xori"), + // M extension + load_test_elf!("rv32um-p-div"), + load_test_elf!("rv32um-p-divu"), + load_test_elf!("rv32um-p-mul"), + load_test_elf!("rv32um-p-mulh"), + load_test_elf!("rv32um-p-mulhsu"), + load_test_elf!("rv32um-p-mulhu"), + load_test_elf!("rv32um-p-rem"), + load_test_elf!("rv32um-p-remu"), +]; From 6399fec4501ca574ca99c837e843a3f83a7d07a1 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 12:14:14 +0800 Subject: [PATCH 04/15] Move benchs --- circuits/src/{cli_benches => benches}/benches.rs | 0 circuits/src/{cli_benches => benches}/mod.rs | 0 circuits/src/{cli_benches => benches}/nop.rs | 2 ++ circuits/src/{cli_benches => benches}/omni.rs | 0 circuits/src/{cli_benches => benches}/poseidon2.rs | 0 circuits/src/{cli_benches => benches}/sort.rs | 0 circuits/src/{cli_benches => benches}/xor.rs | 0 circuits/src/expr.rs | 2 -- circuits/src/lib.rs | 2 +- cli/src/main.rs | 2 +- 10 files changed, 4 insertions(+), 4 deletions(-) rename circuits/src/{cli_benches => benches}/benches.rs (100%) rename circuits/src/{cli_benches => benches}/mod.rs (100%) rename circuits/src/{cli_benches => benches}/nop.rs (96%) rename circuits/src/{cli_benches => benches}/omni.rs (100%) rename circuits/src/{cli_benches => benches}/poseidon2.rs (100%) rename circuits/src/{cli_benches => benches}/sort.rs (100%) rename circuits/src/{cli_benches => benches}/xor.rs (100%) diff --git a/circuits/src/cli_benches/benches.rs b/circuits/src/benches/benches.rs similarity index 100% rename from circuits/src/cli_benches/benches.rs rename to circuits/src/benches/benches.rs diff --git a/circuits/src/cli_benches/mod.rs b/circuits/src/benches/mod.rs similarity index 100% rename from circuits/src/cli_benches/mod.rs rename to circuits/src/benches/mod.rs diff --git a/circuits/src/cli_benches/nop.rs b/circuits/src/benches/nop.rs similarity index 96% rename from circuits/src/cli_benches/nop.rs rename to circuits/src/benches/nop.rs index 0a9787b68..1d0155995 100644 --- a/circuits/src/cli_benches/nop.rs +++ b/circuits/src/benches/nop.rs @@ -12,6 +12,8 @@ pub fn nop_execute((program, record): (Program, ExecutionRecord)) -> Result<( 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) { let instructions = [ Instruction { diff --git a/circuits/src/cli_benches/omni.rs b/circuits/src/benches/omni.rs similarity index 100% rename from circuits/src/cli_benches/omni.rs rename to circuits/src/benches/omni.rs diff --git a/circuits/src/cli_benches/poseidon2.rs b/circuits/src/benches/poseidon2.rs similarity index 100% rename from circuits/src/cli_benches/poseidon2.rs rename to circuits/src/benches/poseidon2.rs diff --git a/circuits/src/cli_benches/sort.rs b/circuits/src/benches/sort.rs similarity index 100% rename from circuits/src/cli_benches/sort.rs rename to circuits/src/benches/sort.rs diff --git a/circuits/src/cli_benches/xor.rs b/circuits/src/benches/xor.rs similarity index 100% rename from circuits/src/cli_benches/xor.rs rename to circuits/src/benches/xor.rs diff --git a/circuits/src/expr.rs b/circuits/src/expr.rs index 8b1f64953..c1b3bc17d 100644 --- a/circuits/src/expr.rs +++ b/circuits/src/expr.rs @@ -10,8 +10,6 @@ use plonky2::iop::ext_target::ExtensionTarget; use plonky2::plonk::circuit_builder::CircuitBuilder; use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -pub const Food: i32 = 7; - struct CircuitBuilderEvaluator<'a, F, const D: usize> where F: RichField, diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index 115b644ee..9bff5c436 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -7,8 +7,8 @@ #![allow(clippy::multiple_crate_versions)] #![feature(const_trait_impl)] +pub mod benches; pub mod bitshift; -pub mod cli_benches; pub mod columns_view; pub mod cpu; pub mod cross_table_lookup; diff --git a/cli/src/main.rs b/cli/src/main.rs index 49b67c624..3d2526043 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -11,7 +11,7 @@ use anyhow::Result; use clio::Input; use itertools::Itertools; use log::debug; -use mozak_circuits::cli_benches::benches::bench; +use mozak_circuits::benches::benches::bench; use mozak_circuits::generation::memoryinit::generate_elf_memory_init_trace; use mozak_circuits::generation::storage_device::generate_call_tape_trace; use mozak_circuits::program::generation::generate_program_rom_trace; From 0caf013e70d98c1272554140797a5e06c1b4684b Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 12:34:43 +0800 Subject: [PATCH 05/15] Make clippy happy --- circuits/src/benches/benches.rs | 42 --- circuits/src/benches/mod.rs | 43 ++- circuits/src/benches/nop.rs | 71 ++--- circuits/src/benches/omni.rs | 486 +++++++++++++++--------------- circuits/src/benches/poseidon2.rs | 35 +-- circuits/src/benches/sort.rs | 124 ++++---- circuits/src/benches/xor.rs | 85 +++--- cli/src/main.rs | 2 +- 8 files changed, 423 insertions(+), 465 deletions(-) delete mode 100644 circuits/src/benches/benches.rs diff --git a/circuits/src/benches/benches.rs b/circuits/src/benches/benches.rs deleted file mode 100644 index 303d42b67..000000000 --- a/circuits/src/benches/benches.rs +++ /dev/null @@ -1,42 +0,0 @@ -use std::time::Duration; - -use anyhow::Result; -pub use mozak_cli_args::bench_args::{BenchArgs, BenchFunction}; - -use super::nop::NopBench; -use super::omni::OmniBench; -use super::poseidon2::Poseidon2Bench; -use super::sort::{SortBench, SortBenchRecursive}; -use super::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 { - let prepared = self.prepare(args); - let start = std::time::Instant::now(); - self.execute(prepared)?; - Ok(start.elapsed()) - } -} - -pub fn bench(args: &BenchArgs) -> Result { - 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), - } -} diff --git a/circuits/src/benches/mod.rs b/circuits/src/benches/mod.rs index 354332982..7938eb37c 100644 --- a/circuits/src/benches/mod.rs +++ b/circuits/src/benches/mod.rs @@ -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 { + let prepared = self.prepare(args); + let start = std::time::Instant::now(); + self.execute(prepared)?; + Ok(start.elapsed()) + } +} + +pub fn bench(args: &BenchArgs) -> Result { + 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), + } +} diff --git a/circuits/src/benches/nop.rs b/circuits/src/benches/nop.rs index 1d0155995..2322f5c13 100644 --- a/circuits/src/benches/nop.rs +++ b/circuits/src/benches/nop.rs @@ -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)) -> 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) { - 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); - 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)) -> 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)) } } diff --git a/circuits/src/benches/omni.rs b/circuits/src/benches/omni.rs index 40525602a..e5747cf4c 100644 --- a/circuits/src/benches/omni.rs +++ b/circuits/src/benches/omni.rs @@ -4,287 +4,281 @@ use mozak_runner::instruction::{Args, Instruction, Op}; 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}; -pub fn omni_execute((program, record): (Program, ExecutionRecord)) -> Result<(), anyhow::Error> { - prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) -} +pub(crate) struct OmniBench; -#[allow(clippy::module_name_repetitions)] -/// Benchmark almost every instruction. -/// -/// Important: when extending, don't mess with register 1, because we need it as -/// the loop variable. -pub fn omni_prepare(iterations: u32) -> (Program, ExecutionRecord) { - let instructions = [ - Instruction { - op: Op::ADD, - args: Args { - rd: 2, - rs1: 1, - imm: 1_u32.wrapping_neg(), - ..Args::default() +impl Bench for OmniBench { + type Args = u32; + type Prepared = (Program, ExecutionRecord); + + /// Benchmark almost every instruction. + /// + /// Important: when extending, don't mess with register 1, because we need + /// it as the loop variable. + #[allow(clippy::too_many_lines)] + fn prepare(&self, &iterations: &u32) -> Self::Prepared { + let instructions = [ + Instruction { + op: Op::ADD, + args: Args { + rd: 2, + rs1: 1, + imm: 1_u32.wrapping_neg(), + ..Args::default() + }, }, - }, - Instruction { - op: Op::SUB, - args: Args { - rd: 3, - rs1: 1, - imm: 1_u32.wrapping_neg(), - ..Args::default() + Instruction { + op: Op::SUB, + args: Args { + rd: 3, + rs1: 1, + imm: 1_u32.wrapping_neg(), + ..Args::default() + }, }, - }, - Instruction { - op: Op::XOR, - args: Args { - rd: 3, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::XOR, + args: Args { + rd: 3, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::OR, - args: Args { - rd: 3, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::OR, + args: Args { + rd: 3, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::AND, - args: Args { - rd: 3, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::AND, + args: Args { + rd: 3, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SLL, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SLL, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SRL, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SRL, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SRA, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SRA, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SLT, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SLT, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SLTU, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SLTU, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::LB, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::LB, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::LH, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::LH, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::LW, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::LW, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::LBU, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::LBU, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SB, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SB, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SH, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SH, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::SW, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::SW, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - // TODO(Matthias): add branches, jumps and ecalls later. - Instruction { - op: Op::MUL, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + // TODO(Matthias): add branches, jumps and ecalls later. + Instruction { + op: Op::MUL, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::MULH, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::MULH, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::MULHU, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::MULHU, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::MULHSU, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::MULHSU, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::DIV, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::DIV, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::DIVU, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::DIVU, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::REM, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::REM, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - Instruction { - op: Op::REMU, - args: Args { - rd: 4, - rs1: 1, - rs2: 2, - ..Args::default() + Instruction { + op: Op::REMU, + args: Args { + rd: 4, + rs1: 1, + rs2: 2, + ..Args::default() + }, }, - }, - // The instructions for the loop: count-down and a branch back to the start. - Instruction { - op: Op::ADD, - args: Args { - rd: 1, - rs1: 1, - imm: 1_u32.wrapping_neg(), - ..Args::default() + // The instructions for the loop: count-down and a branch back to the start. + Instruction { + op: Op::ADD, + args: Args { + rd: 1, + rs1: 1, + imm: 1_u32.wrapping_neg(), + ..Args::default() + }, }, - }, - Instruction { - op: Op::BLT, - args: Args { - rs1: 0, - rs2: 1, - imm: 0, - ..Args::default() + Instruction { + op: Op::BLT, + args: Args { + rs1: 0, + rs2: 1, + imm: 0, + ..Args::default() + }, }, - }, - ]; - code::execute(instructions, &[], &[(1, iterations)]) -} - -pub(crate) struct OmniBench; - -impl Bench for OmniBench { - type Args = u32; - type Prepared = (Program, ExecutionRecord); - - fn prepare(&self, args: &Self::Args) -> Self::Prepared { omni_prepare(*args) } + ]; + code::execute(instructions, &[], &[(1, iterations)]) + } - fn execute(&self, prepared: Self::Prepared) -> anyhow::Result<()> { omni_execute(prepared) } + fn execute(&self, (program, record): (Program, ExecutionRecord)) -> anyhow::Result<()> { + prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) + } } #[cfg(test)] mod tests { - use super::{omni_execute, omni_prepare}; + use super::OmniBench; + use crate::benches::Bench; #[test] - fn test_omni_bench() -> anyhow::Result<()> { - let iterations = 10; - omni_execute(omni_prepare(iterations)) - } + fn test_omni_bench() -> anyhow::Result<()> { OmniBench {}.execute(OmniBench {}.prepare(&10)) } } diff --git a/circuits/src/benches/poseidon2.rs b/circuits/src/benches/poseidon2.rs index 807de5406..e63145175 100644 --- a/circuits/src/benches/poseidon2.rs +++ b/circuits/src/benches/poseidon2.rs @@ -2,43 +2,36 @@ use mozak_runner::elf::Program; use mozak_runner::vm::ExecutionRecord; use starky::config::StarkConfig; -use super::benches::Bench; +use super::Bench; use crate::test_utils::{create_poseidon2_test, prove_and_verify_mozak_stark, Poseidon2Test, F}; -pub fn poseidon2_execute( - (program, record): (Program, ExecutionRecord), -) -> Result<(), anyhow::Error> { - prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) -} -pub fn poseidon2_prepare(input_len: u32) -> (Program, ExecutionRecord) { - let s: String = "dead_beef_feed_c0de".repeat(input_len as usize); - create_poseidon2_test(&[Poseidon2Test { - data: s, - input_start_addr: 1024, - output_start_addr: 1024 + input_len, - }]) -} - pub(crate) struct Poseidon2Bench; impl Bench for Poseidon2Bench { type Args = u32; type Prepared = (Program, ExecutionRecord); - fn prepare(&self, args: &Self::Args) -> Self::Prepared { poseidon2_prepare(*args) } + fn prepare(&self, &input_len: &u32) -> (Program, ExecutionRecord) { + let s: String = "dead_beef_feed_c0de".repeat(input_len as usize); + create_poseidon2_test(&[Poseidon2Test { + data: s, + input_start_addr: 1024, + output_start_addr: 1024 + input_len, + }]) + } - fn execute(&self, prepared: Self::Prepared) -> anyhow::Result<()> { - poseidon2_execute(prepared) + fn execute(&self, (program, record): (Program, ExecutionRecord)) -> anyhow::Result<()> { + prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) } } #[cfg(test)] mod tests { - use super::{poseidon2_execute, poseidon2_prepare}; + use super::Poseidon2Bench; + use crate::benches::Bench; #[test] fn test_poseidon2_bench() -> anyhow::Result<()> { - let input_len = 10; - poseidon2_execute(poseidon2_prepare(input_len)) + Poseidon2Bench {}.execute(Poseidon2Bench {}.prepare(&10)) } } diff --git a/circuits/src/benches/sort.rs b/circuits/src/benches/sort.rs index 44df40251..206f8e927 100644 --- a/circuits/src/benches/sort.rs +++ b/circuits/src/benches/sort.rs @@ -8,7 +8,7 @@ use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::util::timing::TimingTree; use starky::config::StarkConfig; -use super::benches::Bench; +use super::Bench; use crate::stark::mozak_stark::{MozakStark, PublicInputs}; use crate::stark::proof::AllProof; use crate::stark::prover::prove; @@ -16,71 +16,27 @@ use crate::stark::recursive_verifier::{recursive_mozak_stark_circuit, MozakStark use crate::stark::verifier::verify_proof; use crate::test_utils::{prove_and_verify_mozak_stark, C, D, F}; -pub fn sort_execute(result: Result<(Program, ExecutionRecord)>) -> Result<()> { - let (program, record) = result?; - prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) -} - -pub fn sort_prepare(n: u32) -> Result<(Program, ExecutionRecord)> { - let program = Program::vanilla_load_elf(MOZAK_SORT_ELF)?; - let raw_tapes = RawTapes { - public_tape: n.to_le_bytes().to_vec(), - ..Default::default() - }; - let state = State::new(program.clone(), raw_tapes); - let record = step(&program, state)?; - Ok((program, record)) -} - -/// Returns the stark proof for `MOZAK_SORT_ELF`, and its corresponding -/// `RecursiveVerifierCircuit`. -pub fn sort_recursive_prepare( - n: u32, -) -> Result<(MozakStarkVerifierCircuit, AllProof)> { - let mozak_stark = MozakStark::default(); - let stark_config = StarkConfig::standard_fast_config(); - let (program, record) = sort_prepare(n)?; - let public_inputs = PublicInputs { - entry_point: F::from_canonical_u32(program.entry_point), - }; - let mozak_proof = prove::( - &program, - &record, - &mozak_stark, - &stark_config, - public_inputs, - &mut TimingTree::default(), - )?; - verify_proof(&mozak_stark, mozak_proof.clone(), &stark_config)?; - let circuit_config = CircuitConfig::standard_recursion_config(); - let mozak_stark_circuit = recursive_mozak_stark_circuit::( - &mozak_stark, - &mozak_proof.degree_bits(&stark_config), - &circuit_config, - &stark_config, - ); - Ok((mozak_stark_circuit, mozak_proof)) -} - -/// Recursively verifies the stark proof for `MOZAK_SORT_ELF`, with -/// its `MozakStarkVerifierCircuit` -pub fn sort_recursive_execute( - circuit_with_proof: Result<(MozakStarkVerifierCircuit, AllProof)>, -) -> Result<()> { - let (mozak_stark_circuit, mozak_proof) = circuit_with_proof?; - let recursive_proof = mozak_stark_circuit.prove(&mozak_proof)?; - mozak_stark_circuit.circuit.verify(recursive_proof) -} - pub(crate) struct SortBench; impl Bench for SortBench { type Args = u32; type Prepared = Result<(Program, ExecutionRecord)>; - fn prepare(&self, args: &Self::Args) -> Self::Prepared { sort_prepare(*args) } + fn prepare(&self, &n: &u32) -> Self::Prepared { + let program = Program::vanilla_load_elf(MOZAK_SORT_ELF)?; + let raw_tapes = RawTapes { + public_tape: n.to_le_bytes().to_vec(), + ..Default::default() + }; + let state = State::new(program.clone(), raw_tapes); + let record = step(&program, state)?; + Ok((program, record)) + } - fn execute(&self, prepared: Self::Prepared) -> Result<()> { sort_execute(prepared) } + fn execute(&self, result: Result<(Program, ExecutionRecord)>) -> Result<()> { + let (program, record) = result?; + prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) + } } pub(crate) struct SortBenchRecursive; @@ -89,24 +45,56 @@ impl Bench for SortBenchRecursive { type Args = u32; type Prepared = Result<(MozakStarkVerifierCircuit, AllProof)>; - fn prepare(&self, args: &Self::Args) -> Self::Prepared { sort_recursive_prepare(*args) } + /// Returns the stark proof for `MOZAK_SORT_ELF`, and its corresponding + /// `RecursiveVerifierCircuit`. + fn prepare(&self, &n: &u32) -> Self::Prepared { + let mozak_stark = MozakStark::default(); + let stark_config = StarkConfig::standard_fast_config(); + let (program, record) = SortBench {}.prepare(&n)?; + let public_inputs = PublicInputs { + entry_point: F::from_canonical_u32(program.entry_point), + }; + let mozak_proof = prove::( + &program, + &record, + &mozak_stark, + &stark_config, + public_inputs, + &mut TimingTree::default(), + )?; + verify_proof(&mozak_stark, mozak_proof.clone(), &stark_config)?; + let circuit_config = CircuitConfig::standard_recursion_config(); + let mozak_stark_circuit = recursive_mozak_stark_circuit::( + &mozak_stark, + &mozak_proof.degree_bits(&stark_config), + &circuit_config, + &stark_config, + ); + Ok((mozak_stark_circuit, mozak_proof)) + } - fn execute(&self, prepared: Self::Prepared) -> Result<()> { sort_recursive_execute(prepared) } + /// Recursively verifies the stark proof for `MOZAK_SORT_ELF`, with + /// its `MozakStarkVerifierCircuit` + fn execute( + &self, + circuit_with_proof: Result<(MozakStarkVerifierCircuit, AllProof)>, + ) -> Result<()> { + let (mozak_stark_circuit, mozak_proof) = circuit_with_proof?; + let recursive_proof = mozak_stark_circuit.prove(&mozak_proof)?; + mozak_stark_circuit.circuit.verify(recursive_proof) + } } #[cfg(test)] mod tests { use anyhow::Result; - use super::{sort_execute, sort_prepare, sort_recursive_execute, sort_recursive_prepare}; + use super::{SortBench, SortBenchRecursive}; + use crate::benches::Bench; #[test] - fn test_sort_bench() -> Result<()> { - let n = 10; - sort_execute(sort_prepare(n)) - } + fn test_sort_bench() -> Result<()> { SortBench {}.execute(SortBench {}.prepare(&10)) } #[test] fn test_recursive_sort_bench() -> Result<()> { - let n = 10; - sort_recursive_execute(sort_recursive_prepare(n)) + SortBenchRecursive {}.execute(SortBenchRecursive {}.prepare(&10)) } } diff --git a/circuits/src/benches/xor.rs b/circuits/src/benches/xor.rs index 51d2c6748..7e1c3ffdb 100644 --- a/circuits/src/benches/xor.rs +++ b/circuits/src/benches/xor.rs @@ -4,64 +4,57 @@ use mozak_runner::instruction::{Args, Instruction, Op}; 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 xor_execute((program, record): (Program, ExecutionRecord)) -> Result<(), anyhow::Error> { - prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) -} - -pub fn xor_prepare(iterations: u32) -> (Program, ExecutionRecord) { - let instructions = [ - Instruction { - op: Op::ADD, - args: Args { - rd: 1, - rs1: 1, - imm: 1_u32.wrapping_neg(), - ..Args::default() - }, - }, - Instruction { - op: Op::XOR, - args: Args { - rd: 2, - rs1: 1, - imm: 0xDEAD_BEEF, - ..Args::default() - }, - }, - Instruction { - op: Op::BLT, - args: Args { - rs1: 0, - rs2: 1, - imm: 0, - ..Args::default() - }, - }, - ]; - code::execute(instructions, &[], &[(1, iterations)]) -} - pub(crate) struct XorBench; impl Bench for XorBench { type Args = u32; type Prepared = (Program, ExecutionRecord); - fn prepare(&self, args: &Self::Args) -> Self::Prepared { xor_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() + }, + }, + Instruction { + op: Op::XOR, + args: Args { + rd: 2, + rs1: 1, + imm: 0xDEAD_BEEF, + ..Args::default() + }, + }, + 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<()> { xor_execute(prepared) } + fn execute(&self, (program, record): (Program, ExecutionRecord)) -> anyhow::Result<()> { + prove_and_verify_mozak_stark(&program, &record, &StarkConfig::standard_fast_config()) + } } #[cfg(test)] mod tests { - use super::{xor_execute, xor_prepare}; + use super::XorBench; + use crate::benches::Bench; #[test] - fn test_xor_bench() -> anyhow::Result<()> { - let iterations = 10; - xor_execute(xor_prepare(iterations)) - } + fn test_xor_bench() -> anyhow::Result<()> { XorBench {}.execute(XorBench {}.prepare(&10)) } } diff --git a/cli/src/main.rs b/cli/src/main.rs index 3d2526043..bcfc7b118 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -11,7 +11,7 @@ use anyhow::Result; use clio::Input; use itertools::Itertools; use log::debug; -use mozak_circuits::benches::benches::bench; +use mozak_circuits::benches::bench; use mozak_circuits::generation::memoryinit::generate_elf_memory_init_trace; use mozak_circuits::generation::storage_device::generate_call_tape_trace; use mozak_circuits::program::generation::generate_program_rom_trace; From 974845142e79392f3cb906033d2f47b104ec8255 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 12:36:05 +0800 Subject: [PATCH 06/15] CLean up --- Cargo.lock | 39 ++++++++++++++++++--------------------- args/Cargo.toml | 5 ----- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99a71e1fc..318fd3ab6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -607,9 +607,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", "windows-sys 0.52.0", @@ -985,9 +985,7 @@ dependencies = [ "clap-verbosity-flag", "clap_derive", "clio", - "criterion", "log", - "proptest", "tempfile", ] @@ -1071,9 +1069,9 @@ checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" [[package]] name = "num" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3135b08af27d103b0a51f2ae0f8632117b7b185ccf931445affa8df530576a41" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" dependencies = [ "num-bigint", "num-complex", @@ -1096,9 +1094,9 @@ dependencies = [ [[package]] name = "num-complex" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", "rand", @@ -1132,11 +1130,10 @@ dependencies = [ [[package]] name = "num-rational" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" dependencies = [ - "autocfg", "num-bigint", "num-integer", "num-traits", @@ -1551,9 +1548,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.200" +version = "1.0.201" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f" +checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c" dependencies = [ "serde_derive", ] @@ -1571,9 +1568,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.200" +version = "1.0.201" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb" +checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865" dependencies = [ "proc-macro2", "quote", @@ -1582,9 +1579,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.116" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" dependencies = [ "itoa", "ryu", @@ -2257,18 +2254,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.33" +version = "0.7.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "087eca3c1eaf8c47b94d02790dd086cd594b912d2043d4de4bfdd466b3befb7c" +checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.33" +version = "0.7.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f4b6c273f496d8fd4eaf18853e6b448760225dc030ff2c485a786859aea6393" +checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b" dependencies = [ "proc-macro2", "quote", diff --git a/args/Cargo.toml b/args/Cargo.toml index 5b2d3eb94..85a9e1d40 100644 --- a/args/Cargo.toml +++ b/args/Cargo.toml @@ -23,8 +23,3 @@ clap_derive = "4.5" clio = { version = "0.3", features = ["clap-parse"] } log = "0.4" tempfile = "3" - -[dev-dependencies] -# Only as a workaround to be able to control criterion's rayon feature and optional dependency. -criterion = { workspace = true, default-features = false } -proptest = "1.4" From 6b18afcfb74bb6592a97ea1c53dd60f4908bb59d Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 12:36:42 +0800 Subject: [PATCH 07/15] Slim down --- Cargo.lock | 2 -- args/Cargo.toml | 2 -- circuits/Cargo.toml | 2 +- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 318fd3ab6..2db09fce5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -985,8 +985,6 @@ dependencies = [ "clap-verbosity-flag", "clap_derive", "clio", - "log", - "tempfile", ] [[package]] diff --git a/args/Cargo.toml b/args/Cargo.toml index 85a9e1d40..c9aff02a8 100644 --- a/args/Cargo.toml +++ b/args/Cargo.toml @@ -21,5 +21,3 @@ clap = { version = "4.5", features = [ clap-verbosity-flag = "2.2" clap_derive = "4.5" clio = { version = "0.3", features = ["clap-parse"] } -log = "0.4" -tempfile = "3" diff --git a/circuits/Cargo.toml b/circuits/Cargo.toml index 8f3203996..5e24ecc00 100644 --- a/circuits/Cargo.toml +++ b/circuits/Cargo.toml @@ -17,8 +17,8 @@ derive_more = { version = "1.0.0-beta.6", features = ["full"] } expr = { path = "../expr" } itertools = "0.12" log = "0.4" -mozak-cli-args = { path = "../args" } mozak-circuits-derive = { path = "./derive" } +mozak-cli-args = { path = "../args" } mozak-examples = { path = "../examples-builder" } mozak-runner = { path = "../runner" } mozak-sdk = { path = "../sdk" } From dc923c516763437b5220e43b50721ecf41aa0622 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 12:38:32 +0800 Subject: [PATCH 08/15] Format --- circuits/src/benches/sort.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/circuits/src/benches/sort.rs b/circuits/src/benches/sort.rs index 206f8e927..7920fd63f 100644 --- a/circuits/src/benches/sort.rs +++ b/circuits/src/benches/sort.rs @@ -93,6 +93,7 @@ mod tests { #[test] fn test_sort_bench() -> Result<()> { SortBench {}.execute(SortBench {}.prepare(&10)) } + #[test] fn test_recursive_sort_bench() -> Result<()> { SortBenchRecursive {}.execute(SortBenchRecursive {}.prepare(&10)) From 76932240bdde46537f76651d7dfb0305cb7a75c6 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 13:04:55 +0800 Subject: [PATCH 09/15] Upgrade clio --- Cargo.lock | 90 ++++++++------------------------------------- Cargo.toml | 1 + circuits/src/lib.rs | 1 - cli/src/main.rs | 4 +- 4 files changed, 18 insertions(+), 78 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2db09fce5..4d354e7b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,7 +87,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -97,7 +97,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -318,8 +318,7 @@ checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" [[package]] name = "clio" version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7fc6734af48458f72f5a3fa7b840903606427d98a710256e808f76a965047d9" +source = "git+https://github.com/0xmozak/clio.git#a5a47f19470ee6991cf7200751eae8d2e3ac07af" dependencies = [ "cfg-if", "clap", @@ -327,7 +326,7 @@ dependencies = [ "libc", "tempfile", "walkdir", - "windows-sys 0.42.0", + "windows-sys", ] [[package]] @@ -612,7 +611,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -790,7 +789,7 @@ checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ "hermit-abi", "libc", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1514,7 +1513,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -1742,7 +1741,7 @@ dependencies = [ "cfg-if", "fastrand", "rustix", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -2090,7 +2089,7 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -2102,21 +2101,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "windows-sys" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" -dependencies = [ - "windows_aarch64_gnullvm 0.42.2", - "windows_aarch64_msvc 0.42.2", - "windows_i686_gnu 0.42.2", - "windows_i686_msvc 0.42.2", - "windows_x86_64_gnu 0.42.2", - "windows_x86_64_gnullvm 0.42.2", - "windows_x86_64_msvc 0.42.2", -] - [[package]] name = "windows-sys" version = "0.52.0" @@ -2132,46 +2116,28 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" -[[package]] -name = "windows_aarch64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" - [[package]] name = "windows_aarch64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" -[[package]] -name = "windows_i686_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" - [[package]] name = "windows_i686_gnu" version = "0.52.5" @@ -2184,48 +2150,24 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" -[[package]] -name = "windows_i686_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" - [[package]] name = "windows_i686_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" - [[package]] name = "windows_x86_64_gnu" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" -[[package]] -name = "windows_x86_64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" - [[package]] name = "windows_x86_64_msvc" version = "0.52.5" diff --git a/Cargo.toml b/Cargo.toml index d28d26c84..16ccb30b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ plonky2_crypto = { git = "https://github.com/0xmozak/plonky2-crypto.git" } criterion = { version = "0.5", default-features = false, features = ["html_reports", "plotters", "cargo_bench_support"] } [patch.crates-io] +clio = { git = "https://github.com/0xmozak/clio.git" } plonky2 = { git = "https://github.com/0xmozak/plonky2.git" } plonky2_maybe_rayon = { git = "https://github.com/0xmozak/plonky2.git" } starky = { git = "https://github.com/0xmozak/plonky2.git" } diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index 9bff5c436..c5c917df8 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -4,7 +4,6 @@ // exceptions: #![allow(clippy::missing_panics_doc)] #![allow(clippy::missing_errors_doc)] -#![allow(clippy::multiple_crate_versions)] #![feature(const_trait_impl)] pub mod benches; diff --git a/cli/src/main.rs b/cli/src/main.rs index bcfc7b118..179b1d494 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,8 +1,6 @@ #![deny(clippy::pedantic)] #![deny(clippy::cargo)] -// TODO(bing): `clio` uses an older `windows-sys` vs other dependencies. -// Remove when `clio` updates, or if `clio` is no longer needed. -#![allow(clippy::multiple_crate_versions)] + use std::collections::HashMap; use std::io::{Read, Write}; use std::path::PathBuf; From 9604a44741ec5b9066cae378b5ac7d2f20f21e0c Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 13:21:11 +0800 Subject: [PATCH 10/15] Shuffle tests --- circuits/Cargo.toml | 4 --- circuits/src/lib.rs | 2 ++ circuits/src/riscv_tests.rs | 49 +++++++++++++++++++++++++++++++++++ circuits/tests/riscv_tests.rs | 45 -------------------------------- 4 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 circuits/src/riscv_tests.rs delete mode 100644 circuits/tests/riscv_tests.rs diff --git a/circuits/Cargo.toml b/circuits/Cargo.toml index 5e24ecc00..836781abd 100644 --- a/circuits/Cargo.toml +++ b/circuits/Cargo.toml @@ -44,10 +44,6 @@ parallel = ["plonky2/parallel", "starky/parallel", "plonky2_maybe_rayon/parallel test = [] timing = ["plonky2/timing", "starky/timing"] -[[test]] -name = "riscv_tests" -required-features = ["test"] - [[bench]] harness = false name = "simple_prover" diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index c5c917df8..0d9cc4b48 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -29,6 +29,8 @@ pub mod public_sub_table; pub mod rangecheck; pub mod rangecheck_u8; pub mod register; +#[cfg(any(feature = "test", test))] +pub mod riscv_tests; pub mod stark; pub mod storage_device; pub mod tape_commitments; diff --git a/circuits/src/riscv_tests.rs b/circuits/src/riscv_tests.rs new file mode 100644 index 000000000..7c9e712ac --- /dev/null +++ b/circuits/src/riscv_tests.rs @@ -0,0 +1,49 @@ +#[cfg(test)] +mod tests { + use anyhow::{Ok, Result}; + use mozak_runner::elf::Program; + use mozak_runner::state::State; + use mozak_runner::vm::step; + use plonky2::field::goldilocks_field::GoldilocksField; + use plonky2_maybe_rayon::*; + use starky::config::StarkConfig; + + use crate::test_utils::prove_and_verify_mozak_stark; + + /// This function takes the contents of a compiled ELF and runs it through + /// the Mozak VM runner to ensure correctness of the base RISC-V + /// implementation. Afterwards, we prove and verify the execution. + /// + /// Below, we use a set of test files compiled from . + /// specifically the rv32ui and rv32um tests. + /// + /// These files are generated on the first `cargo build` using Docker which + /// downloads the RISC-V toolchain and compiles these test files into ELFs. + /// + /// To use these tests, this function specifically asserts that the value of + /// x10 == 0 at the end of a run, as defined by `RVTEST_PASS` here: + /// Custom tests may be added as long as the assertion is respected. + fn run_test(elf: &[u8]) -> Result<()> { + let _ = env_logger::try_init(); + let program = Program::vanilla_load_elf(elf)?; + let state = State::::from(program.clone()); + let record = step(&program, state)?; + let state = record.last_state.clone(); + // At the end of every test, + // register a0(x10) is set to 0 before an ECALL if it passes + assert_eq!(state.get_register_value(10), 0); + assert_eq!(state.get_register_value(17), 93); + assert!(state.has_halted()); + + let config = StarkConfig::standard_fast_config(); + prove_and_verify_mozak_stark(&program, &record, &config)?; + Ok(()) + } + + #[test] + fn riscv_tests() { + mozak_examples::riscv_tests.into_par_iter().for_each(|elf| { + run_test(elf).unwrap(); + }); + } +} diff --git a/circuits/tests/riscv_tests.rs b/circuits/tests/riscv_tests.rs deleted file mode 100644 index 6cf64e6f8..000000000 --- a/circuits/tests/riscv_tests.rs +++ /dev/null @@ -1,45 +0,0 @@ -use anyhow::{Ok, Result}; -use mozak_circuits::test_utils::prove_and_verify_mozak_stark; -use mozak_runner::elf::Program; -use mozak_runner::state::State; -use mozak_runner::vm::step; -use plonky2::field::goldilocks_field::GoldilocksField; -use plonky2_maybe_rayon::*; -use starky::config::StarkConfig; - -/// This function takes the contents of a compiled ELF and runs it through the -/// Mozak VM runner to ensure correctness of the base RISC-V implementation. -/// Afterwards, we prove and verify the execution. -/// -/// Below, we use a set of test files compiled from https://github.com/riscv-software-src/riscv-tests, -/// specifically the rv32ui and rv32um tests. -/// -/// These files are generated on the first `cargo build` using Docker which -/// downloads the RISC-V toolchain and compiles these test files into ELFs. -/// -/// To use these tests, this function specifically asserts that the value of -/// x10 == 0 at the end of a run, as defined by RVTEST_PASS here: https://github.com/riscv/riscv-test-env/blob/4fabfb4e0d3eacc1dc791da70e342e4b68ea7e46/p/riscv_test.h#L247-L252 -/// Custom tests may be added as long as the assertion is respected. -fn run_test(elf: &[u8]) -> Result<()> { - let _ = env_logger::try_init(); - let program = Program::vanilla_load_elf(elf)?; - let state = State::::from(program.clone()); - let record = step(&program, state)?; - let state = record.last_state.clone(); - // At the end of every test, - // register a0(x10) is set to 0 before an ECALL if it passes - assert_eq!(state.get_register_value(10), 0); - assert_eq!(state.get_register_value(17), 93); - assert!(state.has_halted()); - - let config = StarkConfig::standard_fast_config(); - prove_and_verify_mozak_stark(&program, &record, &config)?; - Ok(()) -} - -#[test] -fn riscv_tests() { - mozak_examples::riscv_tests.into_par_iter().for_each(|elf| { - run_test(elf).unwrap(); - }) -} From 4c81c6a2401c2aa2bf4ea6e72204e6255c585cd4 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 13:30:16 +0800 Subject: [PATCH 11/15] Restore test --- circuits/Cargo.toml | 4 +++ circuits/src/lib.rs | 2 -- circuits/src/riscv_tests.rs | 49 ----------------------------------- circuits/tests/riscv_tests.rs | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 51 deletions(-) delete mode 100644 circuits/src/riscv_tests.rs create mode 100644 circuits/tests/riscv_tests.rs diff --git a/circuits/Cargo.toml b/circuits/Cargo.toml index 836781abd..5e24ecc00 100644 --- a/circuits/Cargo.toml +++ b/circuits/Cargo.toml @@ -44,6 +44,10 @@ parallel = ["plonky2/parallel", "starky/parallel", "plonky2_maybe_rayon/parallel test = [] timing = ["plonky2/timing", "starky/timing"] +[[test]] +name = "riscv_tests" +required-features = ["test"] + [[bench]] harness = false name = "simple_prover" diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index 0d9cc4b48..c5c917df8 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -29,8 +29,6 @@ pub mod public_sub_table; pub mod rangecheck; pub mod rangecheck_u8; pub mod register; -#[cfg(any(feature = "test", test))] -pub mod riscv_tests; pub mod stark; pub mod storage_device; pub mod tape_commitments; diff --git a/circuits/src/riscv_tests.rs b/circuits/src/riscv_tests.rs deleted file mode 100644 index 7c9e712ac..000000000 --- a/circuits/src/riscv_tests.rs +++ /dev/null @@ -1,49 +0,0 @@ -#[cfg(test)] -mod tests { - use anyhow::{Ok, Result}; - use mozak_runner::elf::Program; - use mozak_runner::state::State; - use mozak_runner::vm::step; - use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2_maybe_rayon::*; - use starky::config::StarkConfig; - - use crate::test_utils::prove_and_verify_mozak_stark; - - /// This function takes the contents of a compiled ELF and runs it through - /// the Mozak VM runner to ensure correctness of the base RISC-V - /// implementation. Afterwards, we prove and verify the execution. - /// - /// Below, we use a set of test files compiled from . - /// specifically the rv32ui and rv32um tests. - /// - /// These files are generated on the first `cargo build` using Docker which - /// downloads the RISC-V toolchain and compiles these test files into ELFs. - /// - /// To use these tests, this function specifically asserts that the value of - /// x10 == 0 at the end of a run, as defined by `RVTEST_PASS` here: - /// Custom tests may be added as long as the assertion is respected. - fn run_test(elf: &[u8]) -> Result<()> { - let _ = env_logger::try_init(); - let program = Program::vanilla_load_elf(elf)?; - let state = State::::from(program.clone()); - let record = step(&program, state)?; - let state = record.last_state.clone(); - // At the end of every test, - // register a0(x10) is set to 0 before an ECALL if it passes - assert_eq!(state.get_register_value(10), 0); - assert_eq!(state.get_register_value(17), 93); - assert!(state.has_halted()); - - let config = StarkConfig::standard_fast_config(); - prove_and_verify_mozak_stark(&program, &record, &config)?; - Ok(()) - } - - #[test] - fn riscv_tests() { - mozak_examples::riscv_tests.into_par_iter().for_each(|elf| { - run_test(elf).unwrap(); - }); - } -} diff --git a/circuits/tests/riscv_tests.rs b/circuits/tests/riscv_tests.rs new file mode 100644 index 000000000..f423bc9ff --- /dev/null +++ b/circuits/tests/riscv_tests.rs @@ -0,0 +1,45 @@ +use anyhow::{Ok, Result}; +use mozak_circuits::test_utils::prove_and_verify_mozak_stark; +use mozak_runner::elf::Program; +use mozak_runner::state::State; +use mozak_runner::vm::step; +use plonky2::field::goldilocks_field::GoldilocksField; +use plonky2_maybe_rayon::*; +use starky::config::StarkConfig; + +/// This function takes the contents of a compiled ELF and runs it through +/// the Mozak VM runner to ensure correctness of the base RISC-V +/// implementation. Afterwards, we prove and verify the execution. +/// +/// Below, we use a set of test files compiled from . +/// specifically the rv32ui and rv32um tests. +/// +/// These files are generated on the first `cargo build` using Docker which +/// downloads the RISC-V toolchain and compiles these test files into ELFs. +/// +/// To use these tests, this function specifically asserts that the value of +/// x10 == 0 at the end of a run, as defined by `RVTEST_PASS` here: +/// Custom tests may be added as long as the assertion is respected. +fn run_test(elf: &[u8]) -> Result<()> { + let _ = env_logger::try_init(); + let program = Program::vanilla_load_elf(elf)?; + let state = State::::from(program.clone()); + let record = step(&program, state)?; + let state = record.last_state.clone(); + // At the end of every test, + // register a0(x10) is set to 0 before an ECALL if it passes + assert_eq!(state.get_register_value(10), 0); + assert_eq!(state.get_register_value(17), 93); + assert!(state.has_halted()); + + let config = StarkConfig::standard_fast_config(); + prove_and_verify_mozak_stark(&program, &record, &config)?; + Ok(()) +} + +#[test] +fn riscv_tests() { + mozak_examples::riscv_tests.into_par_iter().for_each(|elf| { + run_test(elf).unwrap(); + }); +} From 19c59c664c175e91780934c4a9ef04712cbb50f6 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 13:58:10 +0800 Subject: [PATCH 12/15] Read at runtime --- circuits/tests/riscv_tests.rs | 69 ++++++++++++++++++++++++++++++++--- examples-builder/src/lib.rs | 61 ------------------------------- 2 files changed, 63 insertions(+), 67 deletions(-) diff --git a/circuits/tests/riscv_tests.rs b/circuits/tests/riscv_tests.rs index f423bc9ff..43d46a1ff 100644 --- a/circuits/tests/riscv_tests.rs +++ b/circuits/tests/riscv_tests.rs @@ -1,10 +1,11 @@ +use std::fs; + use anyhow::{Ok, Result}; use mozak_circuits::test_utils::prove_and_verify_mozak_stark; use mozak_runner::elf::Program; use mozak_runner::state::State; use mozak_runner::vm::step; use plonky2::field::goldilocks_field::GoldilocksField; -use plonky2_maybe_rayon::*; use starky::config::StarkConfig; /// This function takes the contents of a compiled ELF and runs it through @@ -37,9 +38,65 @@ fn run_test(elf: &[u8]) -> Result<()> { Ok(()) } -#[test] -fn riscv_tests() { - mozak_examples::riscv_tests.into_par_iter().for_each(|elf| { - run_test(elf).unwrap(); - }); +/// This macro takes in an identifier as the test name and the file name of a +/// compiled ELF, and sets up a `run_test` for it. +macro_rules! test_elf { + ($test_name:ident, $file_name:tt) => { + #[test] + fn $test_name() -> Result<()> { + let elf = fs::read(concat!("riscv-testdata/testdata/", $file_name)) + .expect("Should have been able to read the file"); + run_test(&elf) + } + }; } + +// Base instruction set +test_elf!(add, "rv32ui-p-add"); +test_elf!(addi, "rv32ui-p-addi"); +test_elf!(and, "rv32ui-p-and"); +test_elf!(andi, "rv32ui-p-andi"); +test_elf!(auipc, "rv32ui-p-auipc"); +test_elf!(beq, "rv32ui-p-beq"); +test_elf!(bge, "rv32ui-p-bge"); +test_elf!(bgeu, "rv32ui-p-bgeu"); +test_elf!(blt, "rv32ui-p-blt"); +test_elf!(bltu, "rv32ui-p-bltu"); +test_elf!(bne, "rv32ui-p-bne"); +test_elf!(jal, "rv32ui-p-jal"); +test_elf!(jalr, "rv32ui-p-jalr"); +test_elf!(lb, "rv32ui-p-lb"); +test_elf!(lbu, "rv32ui-p-lbu"); +test_elf!(lh, "rv32ui-p-lh"); +test_elf!(lhu, "rv32ui-p-lhu"); +test_elf!(lui, "rv32ui-p-lui"); +test_elf!(lw, "rv32ui-p-lw"); +test_elf!(or, "rv32ui-p-or"); +test_elf!(ori, "rv32ui-p-ori"); +test_elf!(sb, "rv32ui-p-sb"); +test_elf!(sh, "rv32ui-p-sh"); +test_elf!(simple, "rv32ui-p-simple"); +test_elf!(sll, "rv32ui-p-sll"); +test_elf!(slli, "rv32ui-p-slli"); +test_elf!(slt, "rv32ui-p-slt"); +test_elf!(slti, "rv32ui-p-slti"); +test_elf!(sltiu, "rv32ui-p-sltiu"); +test_elf!(sltu, "rv32ui-p-sltu"); +test_elf!(sra, "rv32ui-p-sra"); +test_elf!(srai, "rv32ui-p-srai"); +test_elf!(srl, "rv32ui-p-srl"); +test_elf!(srli, "rv32ui-p-srli"); +test_elf!(sub, "rv32ui-p-sub"); +test_elf!(sw, "rv32ui-p-sw"); +test_elf!(xor, "rv32ui-p-xor"); +test_elf!(xori, "rv32ui-p-xori"); + +// M extension +test_elf!(div, "rv32um-p-div"); +test_elf!(divu, "rv32um-p-divu"); +test_elf!(mul, "rv32um-p-mul"); +test_elf!(mulh, "rv32um-p-mulh"); +test_elf!(mulhsu, "rv32um-p-mulhsu"); +test_elf!(mulhu, "rv32um-p-mulhu"); +test_elf!(rem, "rv32um-p-rem"); +test_elf!(remu, "rv32um-p-remu"); diff --git a/examples-builder/src/lib.rs b/examples-builder/src/lib.rs index 7e0a50958..fdb344ff9 100644 --- a/examples-builder/src/lib.rs +++ b/examples-builder/src/lib.rs @@ -1,62 +1 @@ include!(concat!(env!("OUT_DIR"), "/vars.rs")); - -/// This macro takes in an identifier as the test name and the file name of a -/// compiled ELF, and sets up a `run_test` for it. -macro_rules! load_test_elf { - ($file_name:tt) => { - include_bytes!(concat!("../../riscv-testdata/testdata/", $file_name)) - }; -} - -// TODO: fix the macro. -#[allow(non_upper_case_globals)] -pub static riscv_tests: &[&[u8]] = &[ - // Base instruction set - load_test_elf!("rv32ui-p-add"), - load_test_elf!("rv32ui-p-addi"), - load_test_elf!("rv32ui-p-and"), - load_test_elf!("rv32ui-p-andi"), - load_test_elf!("rv32ui-p-auipc"), - load_test_elf!("rv32ui-p-beq"), - load_test_elf!("rv32ui-p-bge"), - load_test_elf!("rv32ui-p-bgeu"), - load_test_elf!("rv32ui-p-blt"), - load_test_elf!("rv32ui-p-bltu"), - load_test_elf!("rv32ui-p-bne"), - load_test_elf!("rv32ui-p-jal"), - load_test_elf!("rv32ui-p-jalr"), - load_test_elf!("rv32ui-p-lb"), - load_test_elf!("rv32ui-p-lbu"), - load_test_elf!("rv32ui-p-lh"), - load_test_elf!("rv32ui-p-lhu"), - load_test_elf!("rv32ui-p-lui"), - load_test_elf!("rv32ui-p-lw"), - load_test_elf!("rv32ui-p-or"), - load_test_elf!("rv32ui-p-ori"), - load_test_elf!("rv32ui-p-sb"), - load_test_elf!("rv32ui-p-sh"), - load_test_elf!("rv32ui-p-simple"), - load_test_elf!("rv32ui-p-sll"), - load_test_elf!("rv32ui-p-slli"), - load_test_elf!("rv32ui-p-slt"), - load_test_elf!("rv32ui-p-slti"), - load_test_elf!("rv32ui-p-sltiu"), - load_test_elf!("rv32ui-p-sltu"), - load_test_elf!("rv32ui-p-sra"), - load_test_elf!("rv32ui-p-srai"), - load_test_elf!("rv32ui-p-srl"), - load_test_elf!("rv32ui-p-srli"), - load_test_elf!("rv32ui-p-sub"), - load_test_elf!("rv32ui-p-sw"), - load_test_elf!("rv32ui-p-xor"), - load_test_elf!("rv32ui-p-xori"), - // M extension - load_test_elf!("rv32um-p-div"), - load_test_elf!("rv32um-p-divu"), - load_test_elf!("rv32um-p-mul"), - load_test_elf!("rv32um-p-mulh"), - load_test_elf!("rv32um-p-mulhsu"), - load_test_elf!("rv32um-p-mulhu"), - load_test_elf!("rv32um-p-rem"), - load_test_elf!("rv32um-p-remu"), -]; From 57a450716b71edabf047f39ff757fcf725fef47d Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 14:30:18 +0800 Subject: [PATCH 13/15] Read at compile time --- circuits/tests/riscv_tests.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/circuits/tests/riscv_tests.rs b/circuits/tests/riscv_tests.rs index 43d46a1ff..6d5c95caf 100644 --- a/circuits/tests/riscv_tests.rs +++ b/circuits/tests/riscv_tests.rs @@ -44,9 +44,10 @@ macro_rules! test_elf { ($test_name:ident, $file_name:tt) => { #[test] fn $test_name() -> Result<()> { - let elf = fs::read(concat!("riscv-testdata/testdata/", $file_name)) - .expect("Should have been able to read the file"); - run_test(&elf) + run_test(include_bytes!(concat!( + "../../riscv-testdata/testdata/", + $file_name + ))) } }; } From 9d5253c3bdcef0a4094ec1b398390c6528a43215 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 14:30:58 +0800 Subject: [PATCH 14/15] Clippy --- circuits/tests/riscv_tests.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/circuits/tests/riscv_tests.rs b/circuits/tests/riscv_tests.rs index 6d5c95caf..34716d14b 100644 --- a/circuits/tests/riscv_tests.rs +++ b/circuits/tests/riscv_tests.rs @@ -1,5 +1,3 @@ -use std::fs; - use anyhow::{Ok, Result}; use mozak_circuits::test_utils::prove_and_verify_mozak_stark; use mozak_runner::elf::Program; From ba18db84f1d8b07c0109921d562f708fe355d4e0 Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 9 May 2024 14:32:18 +0800 Subject: [PATCH 15/15] Fix --- circuits/tests/riscv_tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/circuits/tests/riscv_tests.rs b/circuits/tests/riscv_tests.rs index 34716d14b..6e825fd5c 100644 --- a/circuits/tests/riscv_tests.rs +++ b/circuits/tests/riscv_tests.rs @@ -1,3 +1,5 @@ +use std::include_bytes; + use anyhow::{Ok, Result}; use mozak_circuits::test_utils::prove_and_verify_mozak_stark; use mozak_runner::elf::Program;