Skip to content
This repository was archived by the owner on Oct 25, 2025. It is now read-only.
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ debug/
target/
target-tarpaulin/
target-custom/
venv/

# These are backup files generated by rustfmt
**/*.rs.bk
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# the part of this workspace

[workspace]
exclude = ["sdk"]
exclude = ["sdk", "ws2riscvtest"]
members = [
"circuits",
"cli",
Expand All @@ -32,6 +32,7 @@ resolver = "2"
[profile.dev]
lto = "thin"
# We are running our tests with optimizations turned on to make them faster.

# Please turn optimizations off, when you want accurate stack traces for debugging.
opt-level = 2

Expand Down
10 changes: 10 additions & 0 deletions ws2riscvtest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
edition = "2021"
name = "ws2riscvtest"
version = "0.1.0"

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

[dependencies]
wasm-bindgen = "0.2"
27 changes: 27 additions & 0 deletions ws2riscvtest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# What
This directory aims at testing if we can convert a WASM binary to RISC-V target and make it run well on `mozak-vm`. We simulate a naive implementation of Fibonacci for this.

## Building and testing a WASM module
We need `wasm-pack` via
```
cargo install wasm-pack
```

Inside the directory `wasmtest`, we build a WASM module via:
```
wasm-pack build --target web
```
This builds binary for the target `wasm-unknown-unknown`

This would build artifacts accessible in the directory `./pkg`. Most importantly, these files would be generated:
1. `pkg/ws2riscvtest_bg.wasm`, the WASM binary file.
2. `pkg/ws2riscvtest.js`, the JS file required to run WASM code in browser

Run a dummy testing server (on port 8000 by default) via:
```
python3 -m http.server
```
and load up `http://localhost:8000/` in the browser. The correct run should output:
```
WASM TESTER! Function result: 34
```
9 changes: 9 additions & 0 deletions ws2riscvtest/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>WASM Tester</title>
<script type="module" src="index.js"></script>
</head>
<body></body>
</html>
15 changes: 15 additions & 0 deletions ws2riscvtest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Import our outputted wasm ES6 module
// Which, export default's, an initialization function
import init from "./pkg/ws2riscvtest.js";

const runWasm = async () => {
// Instantiate our wasm module
const module = await init("./pkg/ws2riscvtest_bg.wasm");

// Call the function exported from wasm, save the result
const funcResult = module.fibonacci(BigInt("8"));

// Set the result onto the body
document.body.textContent = `WASM TESTER! Function result: ${funcResult}`;
};
runWasm();
14 changes: 14 additions & 0 deletions ws2riscvtest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// The wasm-pack uses wasm-bindgen to build and generate JavaScript binding file.
// Import the wasm-bindgen crate.
use wasm_bindgen::prelude::*;

// Our fibonacci function
// wasm-pack requires "exported" functions
// to include #[wasm_bindgen]
#[wasm_bindgen]
pub fn fibonacci(a: u64) -> u64 {
if a == 0 || a == 1 {
return 1;
}
return fibonacci(a-1) + fibonacci(a - 2);
}