Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
wasm support
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Jan 15, 2024
1 parent df265ae commit 318b757
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 25 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions bus-mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ serde = {version = "1.0.130", features = ["derive"] }
serde_json = "1.0.66"
strum = "0.24"
strum_macros = "0.24"
revm-precompile = {version = "=2.2.0", default-features=false}

revm-precompile = { version = "=2.2.0", default-features = false, optional = true }
[dev-dependencies]
hex = "0.4.3"
pretty_assertions = "1.0.0"
tokio = { version = "1.13", features = ["macros"] }
url = "2.2.2"
mock = { path = "../mock" }

[build-dependencies]
cfg_aliases = "0.1.0"

[features]
default = ["notwasm"]
wasm = []
notwasm = ["revm-precompile"]
test = ["mock"]
7 changes: 7 additions & 0 deletions bus-mapping/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cfg_aliases::cfg_aliases;

fn main() {
cfg_aliases! {
wasm: { target_arch = "wasm32" },
}
}
5 changes: 5 additions & 0 deletions bus-mapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@
#![allow(clippy::upper_case_acronyms)] // Too pedantic
#![feature(type_changing_struct_update)]

#[cfg(all(feature = "wasm", feature = "notwasm"))]
compile_error!("bus-mapping: both wasm & notwasm are enabled, just one of them must be enabled");

Check failure on line 227 in bus-mapping/src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux Build

bus-mapping: both wasm & notwasm are enabled, just one of them must be enabled

Check failure on line 227 in bus-mapping/src/lib.rs

View workflow job for this annotation

GitHub Actions / Light unit tests

bus-mapping: both wasm & notwasm are enabled, just one of them must be enabled

Check failure on line 227 in bus-mapping/src/lib.rs

View workflow job for this annotation

GitHub Actions / Heavy unit tests

bus-mapping: both wasm & notwasm are enabled, just one of them must be enabled

Check failure on line 227 in bus-mapping/src/lib.rs

View workflow job for this annotation

GitHub Actions / Various lints

bus-mapping: both wasm & notwasm are enabled, just one of them must be enabled
#[cfg(all(not(feature = "wasm"), not(feature = "notwasm")))]
compile_error!("bus-mapping: none of wasm & notwasm are enabled, one of them must be enabled");

extern crate alloc;
extern crate core;

Expand Down
49 changes: 30 additions & 19 deletions bus-mapping/src/precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,50 @@ use eth_types::{
evm_types::{GasCost, OpcodeId},
Address, Bytecode, Word,
};
#[cfg(not(wasm))]
use revm_precompile::{Precompile, PrecompileError, Precompiles};

#[allow(unused_variables)]
/// Check if address is a precompiled or not.
pub fn is_precompiled(address: &Address) -> bool {
Precompiles::berlin()
#[cfg(not(wasm))]
return Precompiles::berlin()
.get(address.as_fixed_bytes())
.is_some()
.is_some();
#[cfg(wasm)]
unreachable!()
}

pub(crate) fn execute_precompiled(
address: &Address,
input: &[u8],
gas: u64,
) -> (Vec<u8>, u64, bool) {
let Some(Precompile::Standard(precompile_fn)) = Precompiles::berlin()
#[cfg(wasm)]
return (vec![], 0, false);

#[cfg(not(wasm))]
{
let Some(Precompile::Standard(precompile_fn)) = Precompiles::berlin()
.get(address.as_fixed_bytes()) else {
panic!("calling non-exist precompiled contract address")
};
let (return_data, gas_cost, is_oog, is_ok) = match precompile_fn(input, gas) {
Ok((gas_cost, return_value)) => {
// Some Revm behavior for invalid inputs might be overridden.
(return_value, gas_cost, false, true)
}
Err(err) => match err {
PrecompileError::OutOfGas => (vec![], gas, true, false),
_ => {
log::warn!("unknown precompile err {err:?}");
(vec![], gas, false, false)
panic!("calling non-exist precompiled contract address")
};
let (return_data, gas_cost, is_oog, is_ok) = match precompile_fn(input, gas) {
Ok((gas_cost, return_value)) => {
// Some Revm behavior for invalid inputs might be overridden.
(return_value, gas_cost, false, true)
}
},
};
log::trace!("called precompile with is_ok {is_ok} is_oog {is_oog}, gas_cost {gas_cost}, return_data len {}", return_data.len());
(return_data, gas_cost, is_oog)
Err(err) => match err {
PrecompileError::OutOfGas => (vec![], gas, true, false),
_ => {
log::warn!("unknown precompile err {err:?}");
(vec![], gas, false, false)
}
},
};
log::trace!("called precompile with is_ok {is_ok} is_oog {is_oog}, gas_cost {gas_cost}, return_data len {}", return_data.len());
(return_data, gas_cost, is_oog)
}
}

/// Addresses of the precompiled contracts.
Expand Down
16 changes: 12 additions & 4 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.gi
num = "0.4"
sha3 = "0.10"
array-init = "2.0.0"
bus-mapping = { path = "../bus-mapping" }
bus-mapping = { path = "../bus-mapping", default-features = false }
eth-types = { path = "../eth-types" }
gadgets = { path = "../gadgets" }
ethers-core = "=2.0.10"
Expand All @@ -33,24 +33,32 @@ integer = { git = "https://github.com/privacy-scaling-explorations/halo2wrong"
libsecp256k1 = "0.7"
num-bigint = { version = "0.4" }
rand_chacha = "0.3"
snark-verifier = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", tag = "v2023_04_20", default-features = false, features = ["loader_halo2", "system_halo2", "loader_evm"] }
snark-verifier = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", tag = "v2023_04_20", default-features = false, features = ["loader_halo2", "system_halo2", "loader_evm"], optional = true }
cli-table = { version = "0.4", optional = true }
num_enum = "0.5.7"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.78"
thiserror = "1.0"
hex = {version = "0.4.3", features = ["serde"]}

[target.'cfg(wasm)'.dependencies]
getrandom = { version = "0.2", features = ["js"] }

[dev-dependencies]
bus-mapping = { path = "../bus-mapping", features = ["test"] }
bus-mapping = { path = "../bus-mapping", default-features = false }
ctor = "0.1.22"
ethers-signers = "=2.0.10"
itertools = "0.10.1"
mock = { path = "../mock" }
pretty_assertions = "1.0.0"

[build-dependencies]
cfg_aliases = "0.1.0"

[features]
default = []
default = ["notwasm"]
wasm = ["bus-mapping/wasm"]
notwasm = [ "bus-mapping/notwasm", "snark-verifier"]
# We export some test circuits for other crates to consume
test-circuits = []
# Test utilities for testool crate to consume
Expand Down
7 changes: 7 additions & 0 deletions zkevm-circuits/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cfg_aliases::cfg_aliases;

fn main() {
cfg_aliases! {
wasm: { target_arch = "wasm32" },
}
}
7 changes: 7 additions & 0 deletions zkevm-circuits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#![deny(unsafe_code)]
#![deny(clippy::debug_assert_with_mut_call)]

#[cfg(all(feature = "wasm", feature = "notwasm"))]
compile_error!("zkevm-circuits: both wasm & notwasm are enabled, just one of them must be enabled");
#[cfg(all(not(feature = "wasm"), not(feature = "notwasm")))]
compile_error!("zkevm-circuits: none of wasm & notwasm are enabled, one of them must be enabled");

pub mod bytecode_circuit;
#[allow(dead_code, reason = "under active development")]
pub mod circuit_tools;
Expand All @@ -27,12 +32,14 @@ pub mod keccak_circuit;
#[allow(dead_code, reason = "under active development")]
pub mod mpt_circuit;
pub mod pi_circuit;
#[cfg(not(wasm))]
pub mod root_circuit;
pub mod state_circuit;
pub mod super_circuit;
pub mod table;

#[cfg(any(test, feature = "test-util"))]
#[cfg(not(wasm))]
pub mod test_util;

pub mod instance;
Expand Down

0 comments on commit 318b757

Please sign in to comment.