diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..43d6b86 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,8 @@ +[target.wasm32-unknown-unknown] +rustflags = ["-C", "target-feature=+atomics,+bulk-memory,+mutable-globals", "-C", "link-arg=--max-memory=4294967296"] + +[unstable] +build-std = ["panic_abort", "std"] + +[build] +target = "aarch64-apple-darwin" diff --git a/Cargo.toml b/Cargo.toml index 4e4bf6f..4cec13a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ ark-ff = { git = "https://github.com/FindoraNetwork/ark-algebra" } ark-serialize = { git = "https://github.com/FindoraNetwork/ark-algebra" } ark-algebra-test-templates = { git = "https://github.com/FindoraNetwork/ark-algebra" } ark-std = { git = "https://github.com/FindoraNetwork/ark-std" } +halo2-wasm = { path = "../../open-source-repos/halo2-browser/halo2-wasm"} diff --git a/circuits/halo2/Cargo.toml b/circuits/halo2/Cargo.toml index b382d5f..566d1c5 100644 --- a/circuits/halo2/Cargo.toml +++ b/circuits/halo2/Cargo.toml @@ -3,7 +3,8 @@ name = "plume-halo2" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib"] [dependencies] halo2-base = { git = "https://github.com/shreyas-londhe/halo2-lib.git", branch = "feat/secp256k1-hash2curve", default-features = false, features = ["halo2-axiom", "test-utils"] } @@ -13,4 +14,19 @@ num-bigint = "0.4.4" num-traits = "0.2.18" rand = "0.8.5" +wasm-bindgen = { version = "0.2", features = ["serde-serialize"]} +serde-wasm-bindgen = "0.6" +halo2-wasm = "0.3.4" +[target.'cfg(target_family = "wasm")'.dependencies] +getrandom = { version = "0.2", features = ["js"]} +console_error_panic_hook = "0.1.7" +rayon = "1.9" +wasm-bindgen-rayon = { version = "1.1.3"} +web-sys = { version = "0.3", features = ["Request", "Window", "Response"] } +wasm-bindgen-futures = "0.4" +js-sys = "0.3" + +[features] +default=["rayon"] +rayon=["halo2-wasm/rayon"] diff --git a/circuits/halo2/src/lib.rs b/circuits/halo2/src/lib.rs index fb77b0b..98ce92f 100644 --- a/circuits/halo2/src/lib.rs +++ b/circuits/halo2/src/lib.rs @@ -1 +1,44 @@ pub mod plume; + +use std::{cell::RefCell, rc::Rc}; + +use halo2_base::{ + gates::{circuit::builder::BaseCircuitBuilder, GateChip, GateInstructions}, + halo2_proofs::halo2curves::bn256::Fr, +}; +use halo2_wasm::Halo2Wasm; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct MyCircuit { + // Add whatever other chips you need here + gate: GateChip, + builder: Rc>>, +} + +#[wasm_bindgen] +impl MyCircuit { + #[wasm_bindgen(constructor)] + pub fn new(circuit: &Halo2Wasm) -> Self { + let gate = GateChip::new(); + MyCircuit { + gate, + builder: Rc::clone(&circuit.circuit), + } + } + + pub fn run(&mut self) { + // Replace with your circuit, making sure to use `self.builder` + let a = self + .builder + .borrow_mut() + .main(0) + .load_witness(Fr::from(1u64)); + let b = self + .builder + .borrow_mut() + .main(0) + .load_witness(Fr::from(2u64)); + self.gate.add(self.builder.borrow_mut().main(0), a, b); + } +} diff --git a/circuits/halo2/wasm/index.html b/circuits/halo2/wasm/index.html new file mode 100644 index 0000000..a611029 --- /dev/null +++ b/circuits/halo2/wasm/index.html @@ -0,0 +1,49 @@ + + + + + hello-wasm example + + + + + diff --git a/circuits/halo2/wasm/wasm_build.sh b/circuits/halo2/wasm/wasm_build.sh new file mode 100755 index 0000000..12405c2 --- /dev/null +++ b/circuits/halo2/wasm/wasm_build.sh @@ -0,0 +1,20 @@ +#!/bin/bash +set -e + +rm -rf pkg + +TARGET=$1 + +if [ "$TARGET" = "nodejs" ]; then + wasm-pack build --release --target nodejs --out-dir pkg --no-default-features +elif [ "$TARGET" = "web" ]; then + wasm-pack build --release --target web --out-dir pkg +else + echo "Target must be either 'web' or 'nodejs'" + exit 1 +fi + + +if [ "$TARGET" == "nodejs" ]; then + sed -i '' "s/require('env')/{memory: new WebAssembly.Memory({initial: 100,maximum: 65536,shared: true,})}/g" pkg/index.js +fi