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

Commit

Permalink
fix: Implement random_get for wasm backend (#102)
Browse files Browse the repository at this point in the history
* fix: Implement random_get for wasm backend

* cspell
  • Loading branch information
phated committed Apr 6, 2023
1 parent 880bcc8 commit 9c0f06e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions barretenberg_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rust-version.workspace = true
wasmer = { version = "*", default-features = false }
common = { path = "../common", default-features = false }
rust-embed = { version = "6.6.0", features = ["debug-embed", "interpolate-folder-path", "include-exclude"] }
getrandom = "0.2"

[build-dependencies]
pkg-config = "0.3"
Expand Down
27 changes: 23 additions & 4 deletions barretenberg_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ fn instance_load() -> (Instance, Memory) {
"fd_close" => Function::new_native(&store, fd_close),
"proc_exit" => Function::new_native(&store, proc_exit),
"fd_fdstat_get" => Function::new_native(&store, fd_fdstat_get),
"random_get" => Function::new_native(&store, random_get),
"random_get" => Function::new_native_with_env(
&store,
Env {
memory: memory.clone(),
},
random_get
),
"fd_seek" => Function::new_native(&store, fd_seek),
"fd_write" => Function::new_native(&store, fd_write),
"environ_sizes_get" => Function::new_native(&store, environ_sizes_get),
Expand Down Expand Up @@ -204,9 +210,22 @@ fn logstr(env: &Env, ptr: i32) {
println!("{string}");
}

// TODO: Implement randomization function https://github.com/noir-lang/aztec_backend/issues/101
fn random_get(_: i32, _: i32) -> i32 {
0_i32
// Based on https://github.com/wasmerio/wasmer/blob/2.3.0/lib/wasi/src/syscalls/mod.rs#L2537
fn random_get(env: &Env, buf: i32, buf_len: i32) -> i32 {
let mut u8_buffer = vec![0; buf_len as usize];
let res = getrandom::getrandom(&mut u8_buffer);
match res {
Ok(()) => {
unsafe {
env.memory
.uint8view()
.subarray(buf as u32, buf as u32 + buf_len as u32)
.copy_from(&u8_buffer);
}
0_i32 // __WASI_ESUCCESS
}
Err(_) => 29_i32, // __WASI_EIO
}
}

fn proc_exit(_: i32) {
Expand Down
5 changes: 4 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"TURBOVERIFIER",
"vals",
"wasi",
"subarray",
"ESUCCESS",
// In Solidity
//
"addmod",
Expand All @@ -65,6 +67,7 @@
"indicatif",
"tempdir",
"tempfile",
"wasmer"
"wasmer",
"getrandom"
]
}

0 comments on commit 9c0f06e

Please sign in to comment.