Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (wip): wasm proving for plume sig
Browse files Browse the repository at this point in the history
shreyas-londhe committed Mar 7, 2024
1 parent 2e8c8c9 commit c237b0e
Showing 6 changed files with 138 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"}
18 changes: 17 additions & 1 deletion circuits/halo2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
43 changes: 43 additions & 0 deletions circuits/halo2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Fr>,
builder: Rc<RefCell<BaseCircuitBuilder<Fr>>>,
}

#[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);
}
}
49 changes: 49 additions & 0 deletions circuits/halo2/wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>hello-wasm example</title>
</head>
<body>
<script type="module">
import init, {
initThreadPool,
initPanicHook,
Halo2Wasm,
MyCircuit,
} from "../pkg/plume_halo2.js";
import path from "path";
import fs from "fs";
init().then(async () => {
initPanicHook();
// await initThreadPool(2);
console.log("WASM initialized");
const halo2wasm = new Halo2Wasm();
console.log("Halo2Wasm initialized");
const myCircuit = new MyCircuit(halo2wasm);
console.log("MyCircuit initialized");
await myCircuit.run();
console.log("MyCircuit run");
const paramsPath = path.join(__dirname, "data", "params.bin");
console.log("paramsPath", paramsPath);
let params = fs.readFile(filePath, (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}

const byteArray = Buffer.from(data);
console.log("Read file:", byteArray);
return byteArray;
});
// await halo2wasm.loadParams();
// await halo2wasm.genVk();
// const vk = await halo2wasm.getVk();
// console.log("MyCircuit getVk", vk);
// await halo2wasm.genPk();
// const pk = await halo2wasm.getPk();
// console.log("MyCircuit getPk", pk);
});
</script>
</body>
</html>
20 changes: 20 additions & 0 deletions circuits/halo2/wasm/wasm_build.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c237b0e

Please sign in to comment.