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

chore: make WASM compatible #27

Merged
merged 2 commits into from
Jan 28, 2025
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
6 changes: 6 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ jobs:

- name: Run cargo clippy
run: cargo clippy --verbose --all-targets --all-features -- -D warnings

- name: Install wasm32 target
run: rustup target add wasm32-unknown-unknown

- name: Build for WASM
run: cargo build --target wasm32-unknown-unknown
2 changes: 1 addition & 1 deletion src/aggregation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
logging::init_logger,
macros::{start_timer, stop_timer},
},
wasm_ctx::{WASMArgsBuilder, WASMCtx, WasiWASMCtx, ZKWASMCtx},
wasm_ctx::{wasi::WasiWASMCtx, WASMArgsBuilder, WASMCtx, ZKWASMCtx},
wasm_snark::{StepSize, WASMPublicParams, WasmSNARK, ZKWASMInstance},
};
use nova::{
Expand Down
2 changes: 1 addition & 1 deletion src/sharding/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
macros::{start_timer, stop_timer},
tracing::estimate_wasm,
},
wasm_ctx::{TraceSliceValues, WASMArgsBuilder, WasiWASMCtx, ZKWASMCtx},
wasm_ctx::{wasi::WasiWASMCtx, TraceSliceValues, WASMArgsBuilder, ZKWASMCtx},
wasm_snark::{StepSize, WASMPublicParams, WasmSNARK, ZKWASMInstance},
};
use nova::{
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
error::ZKWASMError,
wasm_ctx::{TraceSliceValues, WASMArgsBuilder, WASMCtx, WasiWASMCtx, ZKWASMCtx},
wasm_ctx::{wasi::WasiWASMCtx, TraceSliceValues, WASMArgsBuilder, WASMCtx, ZKWASMCtx},
wasm_snark::{StepSize, WasmSNARK},
};
use crate::utils::{
Expand Down
2 changes: 1 addition & 1 deletion src/utils/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) fn split_vector<T>(mut vec: Vec<T>, split_index: usize) -> (Vec<T>, V
mod test {
use wasmi::{BCGlobalIdx, WitnessVM};

use crate::wasm_ctx::{WASMArgsBuilder, WASMCtx, WasiWASMCtx, ZKWASMCtx};
use crate::wasm_ctx::{wasi::WasiWASMCtx, WASMArgsBuilder, WASMCtx, ZKWASMCtx};
use std::{collections::HashMap, path::PathBuf};

/// Count how many time an opcode gets used. Uses the J index of the opcode
Expand Down
66 changes: 37 additions & 29 deletions src/wasm_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use crate::utils::{
tracing::unwrap_rc_refcell,
wasm::{decode_func_args, prepare_func_results, read_wasm_or_wat},
};
use rand::{rngs::StdRng, RngCore, SeedableRng};
use serde::{Deserialize, Serialize};
use std::{cell::RefCell, cmp, num::NonZeroUsize, path::PathBuf, rc::Rc};
use wasmi::{Tracer, WitnessVM};
use wasmi_wasi::{clocks_ctx, sched_ctx, Table, WasiCtx};

/// Builder for [`WASMArgs`]. Constructs the arguments needed to construct a WASM execution context
/// that will be used for proving.
Expand Down Expand Up @@ -290,41 +288,51 @@ impl ZKWASMCtx for WASMCtx {
}
}

/// Wasm execution context
#[derive(Debug, Clone)]
pub struct WasiWASMCtx {
args: WASMArgs,
}
#[cfg(not(target_arch = "wasm32"))]
/// Implementation of WASM execution context for zkVM using wasmi-wasi
pub mod wasi {
use crate::error::ZKWASMError;

impl WasiWASMCtx {
/// Create a new instance of [`WasiWASMCtx`]
pub fn new(args: WASMArgs) -> Self {
Self { args }
}
}

impl ZKWASMCtx for WasiWASMCtx {
type T = WasiCtx;
use super::{WASMArgs, ZKWASMCtx};
use rand::{rngs::StdRng, RngCore, SeedableRng};
use wasmi_wasi::{clocks_ctx, sched_ctx, Table, WasiCtx};

fn args(&self) -> &WASMArgs {
&self.args
/// Wasm execution context
#[derive(Debug, Clone)]
pub struct WasiWASMCtx {
args: WASMArgs,
}

fn create_store(engine: &wasmi::Engine) -> wasmi::Store<Self::T> {
let wasi = WasiCtx::new(zkvm_random_ctx(), clocks_ctx(), sched_ctx(), Table::new());
wasmi::Store::new(engine, wasi)
impl WasiWASMCtx {
/// Create a new instance of [`WasiWASMCtx`]
pub fn new(args: WASMArgs) -> Self {
Self { args }
}
}

fn create_linker(engine: &wasmi::Engine) -> Result<wasmi::Linker<Self::T>, ZKWASMError> {
let mut linker = <wasmi::Linker<WasiCtx>>::new(engine);
wasmi_wasi::add_to_linker(&mut linker, |ctx| ctx)?;
Ok(linker)
impl ZKWASMCtx for WasiWASMCtx {
type T = WasiCtx;

fn args(&self) -> &WASMArgs {
&self.args
}

fn create_store(engine: &wasmi::Engine) -> wasmi::Store<Self::T> {
let wasi = WasiCtx::new(zkvm_random_ctx(), clocks_ctx(), sched_ctx(), Table::new());
wasmi::Store::new(engine, wasi)
}

fn create_linker(engine: &wasmi::Engine) -> Result<wasmi::Linker<Self::T>, ZKWASMError> {
let mut linker = <wasmi::Linker<WasiCtx>>::new(engine);
wasmi_wasi::add_to_linker(&mut linker, |ctx| ctx)?;
Ok(linker)
}
}
}

/// zkvm uses a seed to generate random numbers.
pub fn zkvm_random_ctx() -> Box<dyn RngCore + Send + Sync> {
Box::new(StdRng::from_seed([0; 32]))
/// zkvm uses a seed to generate random numbers.
pub fn zkvm_random_ctx() -> Box<dyn RngCore + Send + Sync> {
Box::new(StdRng::from_seed([0; 32]))
}
}

/// # Initial Set (IS) Memory Sizes.
Expand Down
2 changes: 1 addition & 1 deletion src/wasm_snark/mcc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::{rngs::StdRng, RngCore, SeedableRng};
use crate::{
error::ZKWASMError,
utils::logging::init_logger,
wasm_ctx::{WASMArgsBuilder, WASMCtx, WasiWASMCtx, ZKWASMCtx},
wasm_ctx::{wasi::WasiWASMCtx, WASMArgsBuilder, WASMCtx, ZKWASMCtx},
wasm_snark::mcc::multiset_ops::step_RS_WS,
};

Expand Down
Loading