Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat!: Replace js feature with wasm32 target (#202)
Browse files Browse the repository at this point in the history
feat!: Replace `js` feature with wasm32 target
  • Loading branch information
phated authored May 23, 2023
1 parent 4a8099a commit 86b99c8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
40 changes: 24 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
acvm = { version = "0.12.0", features = ["bn254"] }
bincode = "1.3.3"
Expand All @@ -18,22 +21,38 @@ serde = { version = "1.0.136", features = ["derive"] }
serde-big-array = "0.5.1"
thiserror = "1.0.21"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# Native
barretenberg-sys = { version = "0.1.2", optional = true }

# Wasm
wasmer = { version = "2.3", optional = true, default-features = false }
getrandom = { version = "0.2", optional = true }
rust-embed = { version = "6.6.0", optional = true, features = [
"debug-embed",
"interpolate-folder-path",
"include-exclude",
] }
getrandom = { version = "0.2", optional = true }
wasmer = { version = "2.3", optional = true, default-features = false, features = [
"sys-default",
"cranelift",
"default-compiler",
"default-cranelift",
"default-universal"
] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = [ "js" ] }
rust-embed = { version = "6.6.0", features = [
"debug-embed",
"interpolate-folder-path",
"include-exclude",
] }
wasmer = { version = "2.3", default-features = false, features = [ "js-default" ] }

[build-dependencies]
pkg-config = "0.3"

[dev-dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.0", features = [ "macros" ] }

[features]
Expand All @@ -42,20 +61,9 @@ native = [
"dep:barretenberg-sys"
]
wasm = [
"wasmer",
"dep:rust-embed",
"dep:getrandom",
"wasmer/sys-default",
"wasmer/cranelift",
"wasmer/default-compiler",
"wasmer/default-cranelift",
"wasmer/default-universal"
]
js = [
"wasmer",
"dep:wasmer",
"dep:rust-embed",
"dep:getrandom",
"wasmer/js-default"
"dep:getrandom"
]

[patch.crates-io]
Expand Down
28 changes: 16 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
// `acvm-backend-barretenberg` can either interact with the Barretenberg backend through a static library
// or through an embedded wasm binary. It does not make sense to include both of these backends at the same time.
// We then throw a compilation error if both flags are set.
// TODO: handle JS target.
#[cfg(all(feature = "native", feature = "wasm"))]
compile_error!("feature \"native\" and feature \"wasm\" cannot be enabled at the same time");

#[cfg(all(feature = "native", target_arch = "wasm32"))]
compile_error!("feature \"native\" cannot be enabled for a \"wasm32\" target");

#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
compile_error!("feature \"wasm\" cannot be enabled for a \"wasm32\" target");

mod acvm_interop;
mod barretenberg_structures;
mod composer;
#[cfg(any(feature = "native", feature = "wasm"))]
mod crs;
mod pedersen;
mod pippenger;
Expand Down Expand Up @@ -120,9 +124,9 @@ const FIELD_BYTES: usize = 32;

#[derive(Debug)]
pub struct Barretenberg {
#[cfg(feature = "wasm")]
#[cfg(not(feature = "native"))]
memory: wasmer::Memory,
#[cfg(feature = "wasm")]
#[cfg(not(feature = "native"))]
instance: wasmer::Instance,
}

Expand Down Expand Up @@ -276,15 +280,15 @@ mod wasm {
pub(super) fn transfer_to_heap(&self, arr: &[u8], offset: usize) {
let memory = &self.memory;

#[cfg(feature = "js")]
#[cfg(target_arch = "wasm32")]
{
let view = memory.uint8view();
for (byte_id, cell_id) in (offset..(offset + arr.len())).enumerate() {
view.set_index(cell_id as u32, arr[byte_id])
}
}

#[cfg(not(feature = "js"))]
#[cfg(not(target_arch = "wasm32"))]
{
for (byte_id, cell) in memory.uint8view()[offset..(offset + arr.len())]
.iter()
Expand All @@ -306,13 +310,13 @@ mod wasm {
let memory = &self.memory;
let end = start + length;

#[cfg(feature = "js")]
#[cfg(target_arch = "wasm32")]
return memory
.uint8view()
.subarray(start as u32, end as u32)
.to_vec();

#[cfg(not(feature = "js"))]
#[cfg(not(target_arch = "wasm32"))]
return memory.view()[start..end]
.iter()
.map(|cell| cell.get())
Expand Down Expand Up @@ -431,7 +435,7 @@ mod wasm {
let mut ptr_end = 0;
let byte_view = env.memory.uint8view();

#[cfg(feature = "js")]
#[cfg(target_arch = "wasm32")]
for (i, cell) in byte_view.to_vec()[ptr as usize..].iter().enumerate() {
if cell != &0_u8 {
ptr_end = i;
Expand All @@ -440,7 +444,7 @@ mod wasm {
}
}

#[cfg(not(feature = "js"))]
#[cfg(not(target_arch = "wasm32"))]
for (i, cell) in byte_view[ptr as usize..].iter().enumerate() {
if cell.get() != 0 {
ptr_end = i;
Expand All @@ -449,11 +453,11 @@ mod wasm {
}
}

#[cfg(feature = "js")]
#[cfg(target_arch = "wasm32")]
let str_vec: Vec<_> =
byte_view.to_vec()[ptr as usize..=(ptr + ptr_end as i32) as usize].to_vec();

#[cfg(not(feature = "js"))]
#[cfg(not(target_arch = "wasm32"))]
let str_vec: Vec<_> = byte_view[ptr as usize..=(ptr + ptr_end as i32) as usize]
.iter()
.cloned()
Expand Down

0 comments on commit 86b99c8

Please sign in to comment.