Skip to content

Commit

Permalink
set the number of frame table entry always 2
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed Dec 14, 2023
1 parent 0a6b45d commit b54a506
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 102 deletions.
4 changes: 4 additions & 0 deletions crates/specs/src/jtable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::itable::InstructionTableEntry;
use serde::Serialize;

// 1. jumps to zkmain
// 2. jumps to start(if exists)
pub const STATIC_FRAME_ENTRY_NUMBER: usize = 2;

#[derive(Default, Serialize, Debug, Clone)]
pub struct StaticFrameEntry {
pub enable: bool,
Expand Down
3 changes: 2 additions & 1 deletion crates/specs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use imtable::InitMemoryTable;
use itable::InstructionTable;
use jtable::JumpTable;
use jtable::StaticFrameEntry;
use jtable::STATIC_FRAME_ENTRY_NUMBER;
use mtable::AccessType;
use mtable::LocationType;
use mtable::MTable;
Expand Down Expand Up @@ -50,7 +51,7 @@ pub struct CompilationTable {
pub br_table: Arc<BrTable>,
pub elem_table: Arc<ElemTable>,
pub configure_table: Arc<ConfigureTable>,
pub static_jtable: Arc<Vec<StaticFrameEntry>>,
pub static_jtable: Arc<[StaticFrameEntry; STATIC_FRAME_ENTRY_NUMBER]>,
pub initialization_state: InitializationState<u32>,
}

Expand Down
50 changes: 27 additions & 23 deletions crates/zkwasm/src/circuits/image_table/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use halo2_proofs::arithmetic::FieldExt;
use halo2_proofs::circuit::AssignedCell;
use halo2_proofs::circuit::Layouter;
use halo2_proofs::plonk::Error;
use specs::jtable::STATIC_FRAME_ENTRY_NUMBER;

use super::ImageTableChip;
use crate::circuits::utils::image_table::ImageTableAssigner;
Expand Down Expand Up @@ -66,33 +67,36 @@ impl<F: FieldExt> ImageTableChip<F> {
let static_frame_entries_handler = |base_offset| {
ctx.borrow_mut().offset = base_offset;

permutation_cells
.static_frame_entries
.iter()
.map(|(enable, entry)| {
let offset = ctx.borrow().offset;
let mut cells = vec![];

enable.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.col,
offset,
)?;
ctx.borrow_mut().next();
for (enable, entry) in &permutation_cells.static_frame_entries {
let offset = ctx.borrow().offset;

let offset = ctx.borrow().offset;
enable.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.col,
offset,
)?;
ctx.borrow_mut().next();

entry.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.col,
offset,
)?;
ctx.borrow_mut().next();
let offset = ctx.borrow().offset;

Ok((enable.clone(), entry.clone()))
})
.collect::<Result<Vec<_>, Error>>()
entry.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.col,
offset,
)?;
ctx.borrow_mut().next();

cells.push((enable.clone(), entry.clone()));
}

Ok(cells.try_into().expect(&format!(
"The number of static frame entries should be {}",
STATIC_FRAME_ENTRY_NUMBER
)))
};

let instruction_handler = |base_offset| {
Expand Down
30 changes: 17 additions & 13 deletions crates/zkwasm/src/circuits/image_table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,39 @@ use halo2_proofs::plonk::Expression;
use halo2_proofs::plonk::Fixed;
use halo2_proofs::plonk::VirtualCells;
use std::marker::PhantomData;
use wasmi::DEFAULT_VALUE_STACK_LIMIT;

use crate::curr;

use super::test_circuit::RESERVE_ROWS;
use super::utils::image_table::GLOBAL_CAPABILITY;
use super::utils::image_table::INIT_MEMORY_ENTRIES_OFFSET;
use super::utils::image_table::STACK_CAPABILITY;

mod assign;
mod configure;

pub const IMAGE_COL_NAME: &str = "img_col";
/*
* 8192: 64 * 1024 / 8
* A page is 64KB, an entry is 8B
*/
pub const PAGE_ENTRIES: u32 = 8192;

pub const PAGE_SIZE: u32 = 64 * 1024;
// A block is 8 bytes
pub const PAGE_ENTRIES: u32 = PAGE_SIZE / 8;

/// Compute maximal number of pages supported by the circuit.
/// circuit size - reserved rows for blind - initialization_state/static frame entries/instructions/br_table
/// circuit size - reserved rows for blind - init memory entries base offset
/// - stack entries - global entries
pub fn compute_maximal_pages(k: u32) -> u32 {
let bytes: u32 =
((1usize << k) - RESERVE_ROWS - INIT_MEMORY_ENTRIES_OFFSET - DEFAULT_VALUE_STACK_LIMIT * 2)
.try_into()
.unwrap();
let rows: u32 = ((1usize << k)
- RESERVE_ROWS
- INIT_MEMORY_ENTRIES_OFFSET
- STACK_CAPABILITY
- GLOBAL_CAPABILITY)
.try_into()
.unwrap();

let pages = bytes / PAGE_ENTRIES;
// A block is 8 bytes.
let bytes = rows * 8;

pages
bytes / PAGE_SIZE
}

#[derive(Clone)]
Expand Down
29 changes: 9 additions & 20 deletions crates/zkwasm/src/circuits/jtable/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use halo2_proofs::circuit::AssignedCell;
use halo2_proofs::plonk::Error;
use specs::jtable::JumpTable;
use specs::jtable::StaticFrameEntry;
use specs::jtable::STATIC_FRAME_ENTRY_NUMBER;

use super::JtableOffset;
use super::JumpTableChip;
use super::STATIC_FRAME_ENTRY_NUMBER;
use crate::circuits::utils::bn_to_field;
use crate::circuits::utils::Context;

Expand Down Expand Up @@ -66,24 +66,10 @@ impl<F: FieldExt> JumpTableChip<F> {
&self,
ctx: &mut Context<'_, F>,
rest_jops: &mut u64,
static_entries: &Vec<StaticFrameEntry>,
) -> Result<Vec<(AssignedCell<F, F>, AssignedCell<F, F>)>, Error> {
let mut static_entries = static_entries.clone();

static_entries: &[StaticFrameEntry; STATIC_FRAME_ENTRY_NUMBER],
) -> Result<[(AssignedCell<F, F>, AssignedCell<F, F>); STATIC_FRAME_ENTRY_NUMBER], Error> {
let mut cells = vec![];

static_entries.resize(
STATIC_FRAME_ENTRY_NUMBER,
StaticFrameEntry {
enable: false,
frame_id: 0,
next_frame_id: 0,
callee_fid: 0,
fid: 0,
iid: 0,
},
);

for entry in static_entries {
ctx.region.assign_fixed(
|| "jtable start entries",
Expand Down Expand Up @@ -123,7 +109,10 @@ impl<F: FieldExt> JumpTableChip<F> {
}
}

Ok(cells)
Ok(cells.try_into().expect(&format!(
"The number of static frame entries should be {}",
STATIC_FRAME_ENTRY_NUMBER
)))
}

fn assign_jtable_entries(
Expand Down Expand Up @@ -197,8 +186,8 @@ impl<F: FieldExt> JumpTableChip<F> {
ctx: &mut Context<'_, F>,
jtable: &JumpTable,
etable_jops_cell: &Option<AssignedCell<F, F>>,
static_entries: &Vec<StaticFrameEntry>,
) -> Result<Vec<(AssignedCell<F, F>, AssignedCell<F, F>)>, Error> {
static_entries: &[StaticFrameEntry; STATIC_FRAME_ENTRY_NUMBER],
) -> Result<[(AssignedCell<F, F>, AssignedCell<F, F>); STATIC_FRAME_ENTRY_NUMBER], Error> {
if etable_jops_cell.is_some() {
self.constraint_to_etable_jops(ctx, etable_jops_cell.as_ref().unwrap())?;
}
Expand Down
5 changes: 1 addition & 4 deletions crates/zkwasm/src/circuits/jtable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ use halo2_proofs::plonk::Advice;
use halo2_proofs::plonk::Column;
use halo2_proofs::plonk::ConstraintSystem;
use halo2_proofs::plonk::Fixed;
use specs::jtable::STATIC_FRAME_ENTRY_NUMBER;
use std::marker::PhantomData;

mod assign;
mod configure;
pub(crate) mod expression;

// 1. jumps to zkmain
// 2. jumps to start(if exists)
pub(crate) const STATIC_FRAME_ENTRY_NUMBER: usize = 2;

// enable and data should encode in image table
pub(crate) const STATIC_FRAME_ENTRY_IMAGE_TABLE_ENTRY: usize = STATIC_FRAME_ENTRY_NUMBER * 2;

Expand Down
50 changes: 27 additions & 23 deletions crates/zkwasm/src/circuits/post_image_table/continuation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use halo2_proofs::plonk::Error;
use halo2_proofs::plonk::Fixed;
use num_bigint::BigUint;
use specs::encode::init_memory_table::encode_init_memory_table_address;
use specs::jtable::STATIC_FRAME_ENTRY_NUMBER;
use specs::mtable::LocationType;

use crate::circuits::image_table::ImageTableConfig;
Expand Down Expand Up @@ -143,33 +144,36 @@ impl<F: FieldExt> PostImageTableChipTrait<F, ContinuationPostImageTableConfig<F>
let static_frame_entries_handler = |base_offset| {
ctx.borrow_mut().offset = base_offset;

permutation_cells
.static_frame_entries
.iter()
.map(|(enable, entry)| {
let offset = ctx.borrow().offset;
let mut cells = vec![];

enable.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.post_image_table,
offset,
)?;
ctx.borrow_mut().next();
for (enable, entry) in &permutation_cells.static_frame_entries {
let offset = ctx.borrow().offset;

let offset = ctx.borrow().offset;
enable.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.post_image_table,
offset,
)?;
ctx.borrow_mut().next();

entry.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.post_image_table,
offset,
)?;
ctx.borrow_mut().next();
let offset = ctx.borrow().offset;

Ok((enable.clone(), entry.clone()))
})
.collect::<Result<Vec<_>, Error>>()
entry.copy_advice(
|| "image table: static frame entry",
&mut ctx.borrow_mut().region,
self.config.post_image_table,
offset,
)?;
ctx.borrow_mut().next();

cells.push((enable.clone(), entry.clone()));
}

Ok(cells.try_into().expect(&format!(
"The number of static frame entries should be {}",
STATIC_FRAME_ENTRY_NUMBER
)))
};

let instruction_handler = |base_offset| {
Expand Down
8 changes: 1 addition & 7 deletions crates/zkwasm/src/circuits/test_circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,7 @@ impl<F: FieldExt> Circuit<F> for TestCircuit<F> {
let mut image_table_assigner = ImageTableAssigner::new(
// Add one for default lookup value
self.tables.compilation_tables.itable.entries().len() + 1,
// FIXME: avoid compute
self.tables
.compilation_tables
.itable
.create_brtable()
.entries()
.len()
self.tables.compilation_tables.br_table.entries().len()
+ self.tables.compilation_tables.elem_table.entries().len()
+ 1,
config.circuit_maximal_pages,
Expand Down
33 changes: 23 additions & 10 deletions crates/zkwasm/src/circuits/utils/image_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use halo2_proofs::arithmetic::FieldExt;
use num_bigint::BigUint;
use specs::encode::image_table::ImageTableEncoder;
use specs::imtable::InitMemoryTableEntry;
use specs::jtable::STATIC_FRAME_ENTRY_NUMBER;
use specs::mtable::LocationType;
use specs::mtable::VarType;
use specs::state::InitializationState;
Expand All @@ -13,7 +14,6 @@ use crate::circuits::config::zkwasm_k;
use crate::circuits::image_table::compute_maximal_pages;
use crate::circuits::image_table::PAGE_ENTRIES;
use crate::circuits::jtable::STATIC_FRAME_ENTRY_IMAGE_TABLE_ENTRY;
use crate::circuits::jtable::STATIC_FRAME_ENTRY_NUMBER;
use crate::circuits::utils::bn_to_field;

pub const STACK_CAPABILITY: usize = DEFAULT_VALUE_STACK_LIMIT;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl InitMemoryLayouter {
#[allow(dead_code)]
pub(crate) struct ImageTableLayouter<T> {
pub(crate) initialization_state: InitializationState<T>,
pub(crate) static_frame_entries: Vec<(T, T)>,
pub(crate) static_frame_entries: [(T, T); STATIC_FRAME_ENTRY_NUMBER],
pub(crate) instructions: Vec<T>,
pub(crate) br_table_entires: Vec<T>,
// NOTE: unused instructions and br_table entries.
Expand Down Expand Up @@ -114,8 +114,13 @@ impl ImageTableAssigner {

pub fn exec_static_frame_entries<T, Error>(
&mut self,
mut static_frame_entries_handler: impl FnMut(usize) -> Result<Vec<(T, T)>, Error>,
) -> Result<Vec<(T, T)>, Error> {
mut static_frame_entries_handler: impl FnMut(
usize,
) -> Result<
[(T, T); STATIC_FRAME_ENTRY_NUMBER],
Error,
>,
) -> Result<[(T, T); STATIC_FRAME_ENTRY_NUMBER], Error> {
static_frame_entries_handler(self.static_frame_entries_offset)
}

Expand Down Expand Up @@ -150,7 +155,10 @@ impl ImageTableAssigner {
pub fn exec<T, Error>(
&mut self,
initialization_state_handler: impl FnMut(usize) -> Result<InitializationState<T>, Error>,
static_frame_entries_handler: impl FnMut(usize) -> Result<Vec<(T, T)>, Error>,
static_frame_entries_handler: impl FnMut(
usize,
)
-> Result<[(T, T); STATIC_FRAME_ENTRY_NUMBER], Error>,
instruction_handler: impl FnMut(usize) -> Result<Vec<T>, Error>,
br_table_handler: impl FnMut(usize) -> Result<Vec<T>, Error>,
padding_handler: impl FnMut(usize, usize) -> Result<Vec<T>, Error>,
Expand Down Expand Up @@ -190,11 +198,16 @@ impl<F: FieldExt> EncodeCompilationTableValues<F> for CompilationTable {
// Encode disabled static frame entry in image table
assert_eq!(self.static_jtable.len(), STATIC_FRAME_ENTRY_NUMBER);

Ok(self
.static_jtable
.iter()
.map(|entry| (F::from(entry.enable as u64), bn_to_field(&entry.encode())))
.collect())
let mut cells = vec![];

for entry in self.static_jtable.as_ref() {
cells.push((F::from(entry.enable as u64), bn_to_field(&entry.encode())));
}

Ok(cells.try_into().expect(&format!(
"The number of static frame entries should be {}",
STATIC_FRAME_ENTRY_NUMBER
)))
};

let instruction_handler = |_| {
Expand Down
Loading

0 comments on commit b54a506

Please sign in to comment.