Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mopro wasm halo2 #254

Merged
merged 22 commits into from
Dec 5, 2024
Merged

Mopro wasm halo2 #254

merged 22 commits into from
Dec 5, 2024

Conversation

sifnoc
Copy link
Collaborator

@sifnoc sifnoc commented Nov 7, 2024

To enable multithreading in WASM, wasm-bindgen-rayon must be used.

Usage with WebAssembly
By default, when building to WebAssembly, Rayon will treat it as any other platform without multithreading support and will fall back to sequential iteration. This allows existing code to compile and run successfully with no changes necessary, but it will run slower as it will only use a single CPU core.

from: Rayon - github

Currently, wasm-bindgen-rayon version 1.2.1 can only be compiled with the nightly rust, specifically nightly-2024-07-18

Related issue: #202

@vivianjeng
Copy link
Collaborator

vivianjeng commented Nov 26, 2024

I followed the wasm-pack tutorial
and found that wasm-bindgen can be done with stable toolchain
see this commit and CI result
(the feature flags should be fixed)

but the commit is built for nodejs, not sure if we can have both nodejs and web targets

Nov 27th update:
In order to support wasm-bindgen-rayon, we should keep mopro-wasm folder and only have web target

Copy link

cloudflare-workers-and-pages bot commented Nov 27, 2024

Deploying mopro with  Cloudflare Pages  Cloudflare Pages

Latest commit: 63e90b9
Status: ✅  Deploy successful!
Preview URL: https://90c087f1.mopro.pages.dev
Branch Preview URL: https://mopro-wasm.mopro.pages.dev

View logs

@sifnoc sifnoc marked this pull request as ready for review December 2, 2024 08:46
@sifnoc sifnoc requested a review from vivianjeng December 2, 2024 08:46
Comment on lines 15 to 17
mopro-wasm $ wasm-pack build --target web --out-dir ../test-e2e/web/halo2-plonk-fibonacci -- --features plonk
mopro-wasm $ wasm-pack build --target web --out-dir ../test-e2e/web/halo2-hyperplonk-fibonacci -- --features hyperplonk
mopro-wasm $ wasm-pack build --target web --out-dir ../test-e2e/web/halo2-gemini-fibonacci -- --features gemini
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking can this be

wasm-pack build --target web --out-dir ../test-e2e/web/mopro -- --features plonk --features hyperplonk --features gemini

so the users can simply use

import * as mopro_wasm from './mopro/mopro_wasm.js';

and call each function with

mopro_wasm.generate_plonk_proof(...)
mopro_wasm.generate_hyperplonk_proof(...)
mopro_wasm.generate_gemini_proof(...)
// and could be others in the future

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that the user would eventually use only one type of backend—Plonk, HyperPlonk, or Gemini in Halo2—even for testing purposes. However, if you think it's better to have a single Wasm package in test-e2e/web, such as test-e2e/web/halo2-wasm-circuit, I can modify it to follow your suggestion

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe even not specify halo2?
I hope it can be more general for applications
so like test-e2e/web/mopro or test-e2e/web/mopro-pkg
to let users know the package is generated by mopro and in the documentation we can guide them to use

import * as mopro_wasm from './mopro/mopro_wasm.js';

to import and use functions

Comment on lines 23 to 33
```bash
wasm-pack test --chrome --headless -- --features plonk
```

```bash
wasm-pack test --chrome --headless -- --features hyperplonk
```

```bash
wasm-pack test --chrome --headless -- --features gemini
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here
if you rename the function like

generate_plonk_proof(...)
generate_hyperplonk_proof(...)
generate_gemini_proof(...)

you can test the functions with

 wasm-pack test --chrome --headless -- --all-features

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this doc is simply copied from mopro-wasm during the compilation of the wasm code with wasm-pack. It will be updated

const PROVING_KEY = await fetchBinaryFile('./halo2-hyperplonk-fibonacci/parameters/hyperplonk_fibonacci_pk.bin');
const VERIFYING_KEY = await fetchBinaryFile('./halo2-hyperplonk-fibonacci/parameters/hyperplonk_fibonacci_vk.bin');

const result = await halo2_hyperplonk_wasm.generate_proof(SRS_KEY, PROVING_KEY, input);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also thinking to print out (or show on screen) the execution time of generate_proof

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I had added a feature that displayed the proof generation time alongside the test results. However, I eventually removed it because showing the generation time without any additional metadata—such as device information or circuit configuration—could lead to unmanaged benchmarking results.

If you think it's not a significant issue, I would be happy to add the feature back, either by logging the proof generation time to the console or showing it alongside the test results

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is interesting to benchmark different backends or different circuits
and we can update the result here https://zkmopro.org/docs/performance#halo2
(but update here is optional)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update halo2 wasm benchmark result in another PR. I want to add managing number thread feature in test-e2e/web

test-e2e/web/README.md Show resolved Hide resolved

#[cfg(feature = "plonk")]
#[wasm_bindgen]
pub fn prove_and_verify_plonk_proof() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what is the function for?
there is generate_proof and verify_proof in each file (plonk, hyperplonk, and gemini)
and there is also tests in tests/wasm.rs
maybe this file can be removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. This file is left-over, I will remove this.

@sifnoc sifnoc requested a review from vivianjeng December 3, 2024 15:12
Copy link
Collaborator

@vivianjeng vivianjeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works well for me
just need to change halo2 to more general term like mopro supported proving systems
(But when mentioning rayon, we can still mention halo2)

docs/docs/mopro-wasm.md Outdated Show resolved Hide resolved
docs/docs/mopro-wasm.md Outdated Show resolved Hide resolved
mopro-wasm/README.md Outdated Show resolved Hide resolved
test-e2e/web/README.md Outdated Show resolved Hide resolved
test-e2e/web/README.md Outdated Show resolved Hide resolved
test-e2e/web/index.html Outdated Show resolved Hide resolved
test-e2e/web/index.html Outdated Show resolved Hide resolved
test-e2e/web/README.md Show resolved Hide resolved
test-e2e/web/README.md Outdated Show resolved Hide resolved
@sifnoc
Copy link
Collaborator Author

sifnoc commented Dec 4, 2024

You can see how many web-worker spawned in browser from dev tool like one below.
image

It's updated at 63e90b9

@sifnoc sifnoc requested a review from vivianjeng December 4, 2024 14:27
Copy link
Collaborator

@vivianjeng vivianjeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thank you!
It is such a big progress of mopro :)

@vivianjeng vivianjeng merged commit 5b1c721 into main Dec 5, 2024
33 checks passed
@vivianjeng vivianjeng deleted the mopro-wasm branch December 5, 2024 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants