Skip to content

Commit

Permalink
feat: support for inserting trivial slices in the header
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed Jul 4, 2024
1 parent 85d6412 commit 91bfc4f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 11 deletions.
16 changes: 15 additions & 1 deletion crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ impl ArgBuilder<usize> for SkipArg {
}
}

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 @@ -206,7 +219,7 @@ fn prove_command() -> Command<'static> {
.arg(FileBackendArg::builder());

if cfg!(feature = "continuation") {
command.arg(SkipArg::builder())
command.arg(SkipArg::builder()).arg(PaddingArg::builder())
} else {
command
}
Expand Down Expand Up @@ -280,6 +293,7 @@ impl From<&ArgMatches> for ProveArg {
mock_test: MockTestArg::parse(val),
file_backend: FileBackendArg::parse(val),
skip: SkipArg::parse(val),
padding: PaddingArg::parse(val),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ pub(crate) struct ProveArg {
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
9 changes: 7 additions & 2 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl Config {
mock_test: bool,
table_backend: TraceBackend,
skip: usize,
padding: Option<usize>,
) -> anyhow::Result<()> {
let mut cached_proving_key = None;

Expand Down Expand Up @@ -320,14 +321,18 @@ 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
});

if skip != 0 {
progress_bar.inc(skip as u64);
println!("skip first {} slice(s)", skip);
}

let mut slices = Slices::new(self.k, tables)?
let mut slices = Slices::new(self.k, tables, padding)?
.enumerate()
.skip(skip)
.peekable();
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ fn main() -> Result<()> {
arg.mock_test,
trace_backend,
arg.skip,
arg.padding,
)?;
}
Subcommands::Verify(arg) => {
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

0 comments on commit 91bfc4f

Please sign in to comment.