Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

cli: feat: add skip and padding arguments #277

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@ impl ArgBuilder<bool> for MockTestArg {
}
}

struct SkipArg;
impl ArgBuilder<usize> for SkipArg {
fn builder() -> Arg<'static> {
arg!(--skip [SKIP_SIZE] "Skip first SKIP_SIZE slice(s) proving")
.default_value("0")
.value_parser(value_parser!(usize))
.multiple_values(false)
}

fn parse(matches: &ArgMatches) -> usize {
matches.get_one("skip").cloned().unwrap_or_default()
}
}

struct PaddingArg;
impl ArgBuilder<Option<usize>> for PaddingArg {
fn builder() -> Arg<'static> {
arg!(--padding [AT_LEAST_N] "Insert trivial slices so that the number of proofs is at least AT_LEAST_N")
.value_parser(value_parser!(usize))
.multiple_values(false)
}

fn parse(matches: &ArgMatches) -> Option<usize> {
matches.get_one("padding").copied()
}
}

fn setup_command() -> Command<'static> {
let command = Command::new("setup")
.about("Setup a new zkWasm circuit for provided Wasm image")
Expand Down Expand Up @@ -180,7 +207,7 @@ fn dry_run_command() -> Command<'static> {
}

fn prove_command() -> Command<'static> {
Command::new("prove")
let command = Command::new("prove")
.about("Execute the Wasm image and generate a proof")
.arg(WasmImageArg::builder())
.arg(PublicInputsArg::builder())
Expand All @@ -189,7 +216,13 @@ fn prove_command() -> Command<'static> {
.arg(ContextOutputArg::builder())
.arg(OutputDirArg::builder())
.arg(MockTestArg::builder())
.arg(FileBackendArg::builder())
.arg(FileBackendArg::builder());

if cfg!(feature = "continuation") {
command.arg(SkipArg::builder()).arg(PaddingArg::builder())
} else {
command
}
}

fn verify_command() -> Command<'static> {
Expand Down Expand Up @@ -259,6 +292,8 @@ impl From<&ArgMatches> for ProveArg {
running_arg: val.into(),
mock_test: MockTestArg::parse(val),
file_backend: FileBackendArg::parse(val),
skip: SkipArg::parse(val),
padding: PaddingArg::parse(val),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ pub(crate) struct ProveArg {
pub(crate) running_arg: RunningArg,
pub(crate) mock_test: bool,
pub(crate) file_backend: bool,
// skip first n slice(s) proving.
pub(crate) skip: usize,
// add trivial circuits to padding
pub(crate) padding: Option<usize>,
}

/// Verify the proof.
Expand Down
18 changes: 16 additions & 2 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ impl Config {
context_output_filename: Option<String>,
mock_test: bool,
table_backend: TraceBackend,
skip: usize,
padding: Option<usize>,
) -> anyhow::Result<()> {
let mut cached_proving_key = None;

Expand Down Expand Up @@ -319,9 +321,21 @@ impl Config {
let mut proof_load_info =
ProofGenerationInfo::new(&self.name, self.k as usize, HashType::Poseidon);

let progress_bar = ProgressBar::new(tables.execution_tables.etable.len() as u64);
let progress_bar = ProgressBar::new(if let Some(padding) = padding {
usize::max(tables.execution_tables.etable.len(), padding) as u64
} else {
tables.execution_tables.etable.len() as u64
});

let mut slices = Slices::new(self.k, tables)?.enumerate().peekable();
if skip != 0 {
progress_bar.inc(skip as u64);
println!("skip first {} slice(s)", skip);
}

let mut slices = Slices::new(self.k, tables, padding)?
.enumerate()
.skip(skip)
.peekable();
while let Some((index, circuit)) = slices.next() {
let circuit = circuit?;

Expand Down
2 changes: 2 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ fn main() -> Result<()> {
arg.running_arg.context_output,
arg.mock_test,
trace_backend,
arg.skip,
arg.padding,
)?;
}
Subcommands::Verify(arg) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/playground/examples/binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> Result<()> {
let result = loader.run(runner, &mut monitor)?;
let instances = result.public_inputs_and_outputs::<Fr>();

let slices = Slices::new(K, monitor.into_tables())?;
let slices = Slices::new(K, monitor.into_tables(), None)?;
slices.mock_test_all(instances)?;

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/playground/examples/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> Result<()> {
let runner = loader.compile(&module, &mut monitor)?;
let result = loader.run(runner, &mut monitor)?;

let slices: Slices<Fr> = Slices::new(K, monitor.into_tables())?;
let slices: Slices<Fr> = Slices::new(K, monitor.into_tables(), None)?;
slices.mock_test_all(result.public_inputs_and_outputs())?;

result.context_outputs
Expand All @@ -53,7 +53,7 @@ fn main() -> Result<()> {
let runner = loader.compile(&module, &mut monitor)?;
let result = loader.run(runner, &mut monitor)?;

let slices: Slices<Fr> = Slices::new(K, monitor.into_tables())?;
let slices: Slices<Fr> = Slices::new(K, monitor.into_tables(), None)?;
slices.mock_test_all(result.public_inputs_and_outputs())?;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/playground/examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> Result<()> {
let result = loader.run(runner, &mut monitor)?;
let instances = result.public_inputs_and_outputs::<Fr>();

let slices = Slices::new(K, monitor.into_tables())?;
let slices = Slices::new(K, monitor.into_tables(), None)?;
slices.mock_test_all(instances)?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/playground/examples/phantom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> Result<()> {
let result = loader.run(runner, &mut monitor)?;
let instances = result.public_inputs_and_outputs::<Fr>();

let slices = Slices::new(K, monitor.into_tables())?;
let slices = Slices::new(K, monitor.into_tables(), None)?;
slices.mock_test_all(instances)?;

Ok(())
Expand Down
1 change: 1 addition & 0 deletions crates/zkwasm/src/circuits/etable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ impl<F: FieldExt> EventTableConfig<F> {
vec![
(encode_instruction_table_entry(fid_cell.expr(meta), iid_cell.expr(meta), opcode)
- itable_lookup_cell.curr_expr(meta))
* enabled_cell.curr_expr(meta)
* fixed_curr!(meta, step_sel),
]
});
Expand Down
66 changes: 59 additions & 7 deletions crates/zkwasm/src/loader/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use specs::configure_table::ConfigureTable;
use specs::etable::EventTable;
use specs::imtable::InitMemoryTable;
use specs::itable::InstructionTable;
use specs::jtable::CalledFrameTable;
use specs::jtable::FrameTable;
use specs::jtable::InheritedFrameTable;
use specs::slice::FrameTableSlice;
use specs::slice::Slice;
use specs::state::InitializationState;
use specs::TableBackend;
Expand All @@ -22,6 +24,9 @@ use crate::runtime::state::UpdateInitializationState;
pub struct Slices<F: FieldExt> {
k: u32,

// The number of trivial circuits left.
padding: usize,

itable: Arc<InstructionTable>,
br_table: Arc<BrTable>,
elem_table: Arc<ElemTable>,
Expand All @@ -37,18 +42,27 @@ pub struct Slices<F: FieldExt> {
}

impl<F: FieldExt> Slices<F> {
pub fn new(k: u32, tables: Tables) -> Result<Self, BuildingCircuitError> {
if cfg!(not(feature = "continuation")) {
let slices = tables.execution_tables.etable.len();

if slices != 1 {
return Err(BuildingCircuitError::MultiSlicesNotSupport(slices));
}
/*
* padding: Insert trivial slices so that the number of proofs is at least padding.
*/
pub fn new(
k: u32,
tables: Tables,
padding: Option<usize>,
) -> Result<Self, BuildingCircuitError> {
let slices_len = tables.execution_tables.etable.len();

if cfg!(not(feature = "continuation")) && slices_len != 1 {
return Err(BuildingCircuitError::MultiSlicesNotSupport(slices_len));
}

let padding = padding.map_or(0, |padding| padding.saturating_sub(slices_len));

Ok(Self {
k,

padding,

itable: tables.compilation_tables.itable,
br_table: tables.compilation_tables.br_table,
elem_table: tables.compilation_tables.elem_table,
Expand Down Expand Up @@ -89,6 +103,40 @@ impl<F: FieldExt> Slices<F> {
}
}

impl<F: FieldExt> Slices<F> {
// create a circuit slice with all entries disabled.
fn trivial_slice(&mut self) -> Result<ZkWasmCircuit<F>, BuildingCircuitError> {
self.padding -= 1;

let frame_table = Arc::new(FrameTableSlice {
inherited: self.initial_frame_table.clone(),
called: CalledFrameTable::default(),
});

let slice = Slice {
itable: self.itable.clone(),
br_table: self.br_table.clone(),
elem_table: self.elem_table.clone(),
configure_table: self.configure_table.clone(),
initial_frame_table: self.initial_frame_table.clone(),

frame_table,
post_inherited_frame_table: self.initial_frame_table.clone(),

imtable: self.imtable.clone(),
post_imtable: self.imtable.clone(),

initialization_state: self.initialization_state.clone(),
post_initialization_state: self.initialization_state.clone(),

etable: Arc::new(EventTable::new(vec![])),
is_last_slice: false,
};

ZkWasmCircuit::new(self.k, slice)
}
}

impl<F: FieldExt> Iterator for Slices<F> {
type Item = Result<ZkWasmCircuit<F>, BuildingCircuitError>;

Expand All @@ -97,6 +145,10 @@ impl<F: FieldExt> Iterator for Slices<F> {
return None;
}

if self.padding > 0 {
return Some(self.trivial_slice());
}

let etable = match self.etables.pop_front().unwrap() {
TableBackend::Memory(etable) => etable,
TableBackend::Json(path) => EventTable::read(&path).unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion crates/zkwasm/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn test_circuit_with_env(
let execution_result = loader.run(runner, &mut monitor)?;
let instances: Vec<Fr> = execution_result.public_inputs_and_outputs();

Slices::new(k, monitor.into_tables())?.mock_test_all(instances)?;
Slices::new(k, monitor.into_tables(), None)?.mock_test_all(instances)?;

Ok(())
}
Expand Down
Loading