2 releases

0.3.4 Jan 19, 2024
0.3.3 Jan 18, 2024

#487 in WebAssembly

Download history 9/week @ 2024-01-16 1/week @ 2024-02-13 7/week @ 2024-02-20 16/week @ 2024-02-27 10/week @ 2024-03-12 2/week @ 2024-03-19 10/week @ 2024-03-26 45/week @ 2024-04-02 1/week @ 2024-04-16

58 downloads per month

MIT license

155KB
2K SLoC

Rust 2K SLoC // 0.0% comments TypeScript 300 SLoC // 0.0% comments JavaScript 54 SLoC // 0.1% comments Shell 26 SLoC // 0.3% comments

halo2-wasm

This repository aims to streamline the process of building WASM modules from zero-knowledge proof circuits written on top of halo2-lib. To discuss or collaborate, join our community on Telegram.

Getting Started

For a brief overview on writing halo2-lib circuits, see this doc. In addition to the halo2-lib setup, you will need wasm-pack installed:

curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

The template folder includes everything you need to turn your circuit into a WASM bundle. In particular, template/src/lib.rs is an example of a simple circuit that uses the context of a Halo2Wasm struct. You can then build your WASM bundle (from the template subdirectory) with:

./scripts/build.sh <PLATFORM>

where <PLATFORM> is either nodejs or web.

Multithreading

Halo2 uses Rayon for multithreading, and we use wasm-bindgen-rayon to support this in the browser. It does not work outside the browser, however, so when nodejs is the compilation target, Rayon will be turned off (it is enabled using the rayon feature flag).

Setting up the WASM module in JS

Web

import {
  init,
  initThreadPool,
  initPanicHook,
  Halo2Wasm,
  MyCircuit,
} from "<IMPORT PATH>";

const main = async () => {
  //setup Halo2Wasm and MyCircuit
  await init();
  initPanicHook();
  await initThreadPool(numThreads);
  const halo2wasm = new Halo2Wasm();
  const myCircuit = new MyCircuit(halo2wasm);
};

Node.js

import { Halo2Wasm, initPanicHook, MyCircuit } from "<IMPORT PATH>";

const main = async () => {
  //setup Halo2Wasm and MyCircuit
  initPanicHook();
  const halo2wasm = new Halo2Wasm();
  const myCircuit = new MyCircuit(halo2wasm);
};

You can now run MyCircuit witness generation with myCircuit.run() (following the example in the template subdirectory). You can then call any of the Halo2Wasm operations (mock, keygen, prove, etc.),

halo2-js

halo2-js is a Typescript wrapper for easily using the halo2-wasm module's functions (ie. proving, keygen, etc). Check out the repo for more info.

Benchmarks

Coming soon!

Projects built with halo2-wasm

Acknowledgements

This work would not be possible without Nalin's guide on using raw halo2 in WASM. Check out his guide here.

Dependencies

~26MB
~448K SLoC