Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

feat: wasm bindgens #7

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
86 changes: 86 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ fn main() {

```

#### Compile to WASM
Build the wasm module with the following command:
```bash
cd verifier
wasm-pack build --target nodejs --dev # adjust the target to your liking
```

Then run the example in the `examples/wasm_example` directory:
```bash
cd examples/wasm_example
pnpm install
pnpm run test
```

## Features

- Verification of Groth16 and PlonK proofs generated using `gnark` or `sp1` on the `Bn254` curve.
Expand Down
40 changes: 28 additions & 12 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Examples
This is a simple example of how to use the `snark-bn254-verifier` crate to verify a proof generated by the `SP1` library. The cycle count for running the programs varies depending on the proof system used:

| Program | Plonk Proof | Groth16 Proof |
|----------------|----------------|---------------|
| Fibonacci | 8,078,761 | 9,656,158 |
| Is-Prime | 8,050,250 | 9,651,061 |
| Tendermint | 8,044,765 | 9,653,857 |
| SHA-2 | 8,078,621 | 9,637,958 |
| Program | Plonk Proof | Groth16 Proof |
| ---------- | ----------- | ------------- |
| Fibonacci | 8,078,761 | 9,656,158 |
| Is-Prime | 8,050,250 | 9,651,061 |
| Tendermint | 8,044,765 | 9,653,857 |
| SHA-2 | 8,078,621 | 9,637,958 |

To run the example, you can use the following ELF options:

| ELF Option | Description |
|----------------|------------------------------------|
| fibonacci | Fibonacci sequence program |
| is-prime | Prime number checking program |
| sha2 | SHA-2 hashing program |
| tendermint | Tendermint consensus program |
| ELF Option | Description |
| ---------- | ----------------------------- |
| fibonacci | Fibonacci sequence program |
| is-prime | Prime number checking program |
| sha2 | SHA-2 hashing program |
| tendermint | Tendermint consensus program |

To run the example, run the following command:
```bash
Expand All @@ -26,5 +26,21 @@ cargo prove build --bin groth16

# Run the example
cd ../script
cargo build # Installs the circuit artifacts
RUST_LOG=info cargo run --release -- --elf fibonacci --mode plonk
```

### Wasm Example

To build the JSON proofs for the wasm example, run the following command:
```bash
cd script
cargo build
```

To run the wasm example, run the following command:
```bash
cd wasm_example
pnpm install
pnpm run test
```
4 changes: 3 additions & 1 deletion examples/script/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion examples/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ strum_macros = "0.25"

[build-dependencies]
sp1-helper = "2.0.0"
sp1-prover = "2.0.0"
sp1-sdk = "2.0.0"
serde_json = "1.0.128"

[dev-dependencies]
snark-bn254-verifier = { path = "../../verifier" }
substrate-bn = { git = "https://github.com/sp1-patches/bn", branch = "patch-v0.7.0" }
27 changes: 27 additions & 0 deletions examples/script/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use sp1_sdk::install::try_install_circuit_artifacts;
use sp1_sdk::SP1ProofWithPublicValues;
use std::fs;
use std::path::Path;

fn main() {
// Install the Groth16 and Plonk circuit artifacts
try_install_circuit_artifacts();

let binaries_dir = Path::new("../binaries");
let output_dir = Path::new("../wasm_example/data");

// Convert binary proofs to JSON
fs::read_dir(binaries_dir)
.expect("Failed to read binaries directory")
.filter_map(Result::ok)
.filter(|entry| entry.path().extension().and_then(|s| s.to_str()) == Some("bin"))
.for_each(|entry| {
let path = entry.path();
let proof = SP1ProofWithPublicValues::load(&path).expect("Failed to load proof");
let json_proof = serde_json::to_string(&proof).expect("Failed to serialize proof");
let json_path = output_dir
.join(path.file_stem().unwrap())
.with_extension("json");
fs::write(json_path, json_proof).expect("Failed to write JSON proof");
});
}
3 changes: 1 addition & 2 deletions examples/script/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ mod tests {

use super::*;

use snark_bn254_verifier::{Groth16Verifier, PlonkVerifier};
use snark_bn254_verifier::{Fr, Groth16Verifier, PlonkVerifier};
use strum::IntoEnumIterator;
use substrate_bn::Fr;

const PLONK_VK_BYTES: &[u8] = include_bytes!("../../../../.sp1/circuits/v2.0.0/plonk_vk.bin");
const GROTH16_VK_BYTES: &[u8] =
Expand Down
1 change: 1 addition & 0 deletions examples/wasm_example/data/fibonacci_groth16_proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"proof":{"Groth16":{"public_inputs":["338694232377558529823153776359654126084495984661914661651706714220115668510","6835433473072582537735779005252378178401920886001391083506222100041177144720"],"encoded_proof":"073862c903a173089485c6916e7d55d31826104842e002548028443932b13021100cdab28507e0d1415a29853f9e930f477c1af99e60c1f758fa9faf316a9e8b07d1e42c7d4f44b3f042caae815840be2e31aad122ce512c17a17b79f954bf9f0736236acb8c29084781c26fac901f0573927af9b99f94cc0b2344cbad12c0d406b24aeaf4c3eb34e96e9e11544da04e2f950d13923c8e3002fc6022a99419db0787dc86be057118d5ed953377b49120040d6514450462c04d22d637bfa1d84d19bb0e4e33a31f09313c1a9f7548ea66e09dbd36af5329dc2c43c73de9314b9a203338ae4a62d5e8eb6932bac4eaa09ff2381f9aa459b7748b35d3c714a5ec6e","raw_proof":"073862c903a173089485c6916e7d55d31826104842e002548028443932b13021100cdab28507e0d1415a29853f9e930f477c1af99e60c1f758fa9faf316a9e8b07d1e42c7d4f44b3f042caae815840be2e31aad122ce512c17a17b79f954bf9f0736236acb8c29084781c26fac901f0573927af9b99f94cc0b2344cbad12c0d406b24aeaf4c3eb34e96e9e11544da04e2f950d13923c8e3002fc6022a99419db0787dc86be057118d5ed953377b49120040d6514450462c04d22d637bfa1d84d19bb0e4e33a31f09313c1a9f7548ea66e09dbd36af5329dc2c43c73de9314b9a203338ae4a62d5e8eb6932bac4eaa09ff2381f9aa459b7748b35d3c714a5ec6e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","groth16_vkey_hash":[106,41,6,172,110,196,97,200,202,205,91,53,87,21,127,89,49,123,213,114,229,123,200,37,57,117,170,245,197,172,180,136]}},"stdin":{"buffer":[[20,0,0,0]],"ptr":0,"proofs":[]},"public_values":{"buffer":{"data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,109,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,42,194]}},"sp1_version":"v2.0.0"}
1 change: 1 addition & 0 deletions examples/wasm_example/data/fibonacci_plonk_proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"proof":{"Plonk":{"public_inputs":["338694232377558529823153776359654126084495984661914661651706714220115668510","6835433473072582537735779005252378178401920886001391083506222100041177144720"],"encoded_proof":"2448be9e53902e783637964863ddb1eba8c718091b9d4c6cc88c6b7e11f926d62754b8254411fcbb1a7f8e3c05de5c85aef24e3ab1091da1c90694557711df080ba315037d36a917d61ceca92465e51ee7489ac51115fa78fc5ec426a21c4f7715b1d6f3ee87e32c9cdf212159ca4276a7e018c1d9982ef60888a34c0bc9e34a045c6a62e1cdeac6b7f74774e909c9fdc96cad62700348f7c0965d447cd47041187a282676b9a3fcb08be36a9ae3cd05827420f69fc560319e931514777ed8891852b342a17af9b235939b7a4dc1658fcfcf44bf0b57db59066b513e168064742cbc8e537ffc7790f9ba77c4dd1898490ee067a5c18f38735b2f451f1a4e23f5053dcfefeadec87c9bc93be1f84a88f9435b9d4b114fd3a5bc9b9eb266f73b87211c8f184eafcc4986f6292652edc268157d24e3d50587638151b9766dd0be962d021e48ebc6ae02563f867817504e334f495e715d7a940824544d706bd52c4c17e88621d787978cc3862b91536b2daf3b1f7ff61b5e276a022d8e4ce70362251c6b31a7417b62984ccde65319ae59723d07c2e8d72e1f5c568a7b684bf57f001e2eea86a60d6b07db097993227de30fa7da8f8535afb504abe761ddda97f1271a2e3725fce3b0ad91cf9301ac7ae97c1cdff9b964bba65b4746c1c1794be02a14caa2e3ab32aca1362c923f6d37eaa9ab7fe142c9956f73fb55e6d569f213f51ac1c24da2227d47d28ef3b9b9e44e7a69e8ad53b1aeb109661813dfbd315f56143c0cb8201bcde2ae44302884470a952d62a17d9ef6778f0484b534f1634da11a2aaa8dba0524f16fb177f773f74f8379ac155af2318f8b4120c6ad1658dd0522419736f346c47a0c4570e77841abf6cdca65747b68896df6c68d3bba4a00341970fbd41886242b172b38cd0ceb510cdbd789fa9c5540c82c23dfbb5ae339e82fe1939520387479e175d0b36df61bee9ec15dd1efccb00c51e88aa51e7c1bfd1abcc37c0019a580bcf29bf2080665394de1f86536f19aa2fa224b8a87b86c2523c446901aa09b6206f030b51f58cb371605df4e3e9260776d145b15acba332708c490a519d108c323a75785a4c6a17610e114c5e8dda1d39b19784ab0fca84b00e5612ef73c019f0cbc6c568657da4e4dd9677767d72848c68b5445ff96c9fa2c17675989378ce516467bc5914225c4fe862a8ac83e71c72aaab604dd77a6e1","raw_proof":"2448be9e53902e783637964863ddb1eba8c718091b9d4c6cc88c6b7e11f926d62754b8254411fcbb1a7f8e3c05de5c85aef24e3ab1091da1c90694557711df080ba315037d36a917d61ceca92465e51ee7489ac51115fa78fc5ec426a21c4f7715b1d6f3ee87e32c9cdf212159ca4276a7e018c1d9982ef60888a34c0bc9e34a045c6a62e1cdeac6b7f74774e909c9fdc96cad62700348f7c0965d447cd47041187a282676b9a3fcb08be36a9ae3cd05827420f69fc560319e931514777ed889143c0cb8201bcde2ae44302884470a952d62a17d9ef6778f0484b534f1634da11a2aaa8dba0524f16fb177f773f74f8379ac155af2318f8b4120c6ad1658dd051852b342a17af9b235939b7a4dc1658fcfcf44bf0b57db59066b513e168064742cbc8e537ffc7790f9ba77c4dd1898490ee067a5c18f38735b2f451f1a4e23f5053dcfefeadec87c9bc93be1f84a88f9435b9d4b114fd3a5bc9b9eb266f73b87211c8f184eafcc4986f6292652edc268157d24e3d50587638151b9766dd0be962d021e48ebc6ae02563f867817504e334f495e715d7a940824544d706bd52c4c17e88621d787978cc3862b91536b2daf3b1f7ff61b5e276a022d8e4ce70362251970fbd41886242b172b38cd0ceb510cdbd789fa9c5540c82c23dfbb5ae339e82fe1939520387479e175d0b36df61bee9ec15dd1efccb00c51e88aa51e7c1bfd0000000712567003ecc599076053a3bd44d8dd9a623926b97d5758fbf93d7dad65859bc11c6b31a7417b62984ccde65319ae59723d07c2e8d72e1f5c568a7b684bf57f001e2eea86a60d6b07db097993227de30fa7da8f8535afb504abe761ddda97f1271a2e3725fce3b0ad91cf9301ac7ae97c1cdff9b964bba65b4746c1c1794be02a14caa2e3ab32aca1362c923f6d37eaa9ab7fe142c9956f73fb55e6d569f213f51ac1c24da2227d47d28ef3b9b9e44e7a69e8ad53b1aeb109661813dfbd315f5608c490a519d108c323a75785a4c6a17610e114c5e8dda1d39b19784ab0fca84b1abcc37c0019a580bcf29bf2080665394de1f86536f19aa2fa224b8a87b86c2523c446901aa09b6206f030b51f58cb371605df4e3e9260776d145b15acba332722419736f346c47a0c4570e77841abf6cdca65747b68896df6c68d3bba4a00340000000100e5612ef73c019f0cbc6c568657da4e4dd9677767d72848c68b5445ff96c9fa2c17675989378ce516467bc5914225c4fe862a8ac83e71c72aaab604dd77a6e1","plonk_vkey_hash":[74,202,36,10,62,82,150,230,165,101,249,141,199,40,198,244,143,141,228,121,42,143,163,101,3,140,59,134,149,33,118,245]}},"stdin":{"buffer":[[20,0,0,0]],"ptr":0,"proofs":[]},"public_values":{"buffer":{"data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,109,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,42,194]}},"sp1_version":"v2.0.0"}
1 change: 1 addition & 0 deletions examples/wasm_example/data/is-prime_groth16_proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"proof":{"Groth16":{"public_inputs":["79974752685130133349989020271128338092086594955138314388631360057283056061","5408444369343130360314198614562428875014335023761526637751134529685417182618"],"encoded_proof":"06cbb8db6d4579709f5dea5016e34b1d14e931a709add4af2cf2d43bff57791e2d14247e8518a002a9b504c2bce69a25a26ab61948dcbec8789d358f46ce56010aa81d641f5a96fff0fa5cafd1b1d459c3972f90aa5dfaf6569f471c3113d65c187ae3a30bc5ce5c483a9a06fc1736990d4e03705d5a96a1b5103965ca7cb6a00c95bdf8e88c9dc1f2326c95af236f1f68146de2c7cd589a40e91a5b626b5061000b7c6d9acdccba01f8aa7cad17576315f031d9c3050a409c0354c3d90a170727aa69b8b70d6e30089b3152bbc1c4924b981cb06718afcd48e6111e94772545133a5e8ab0a3772d78635e169a3e91d7c8c0389523e7f15089714818326d708c","raw_proof":"06cbb8db6d4579709f5dea5016e34b1d14e931a709add4af2cf2d43bff57791e2d14247e8518a002a9b504c2bce69a25a26ab61948dcbec8789d358f46ce56010aa81d641f5a96fff0fa5cafd1b1d459c3972f90aa5dfaf6569f471c3113d65c187ae3a30bc5ce5c483a9a06fc1736990d4e03705d5a96a1b5103965ca7cb6a00c95bdf8e88c9dc1f2326c95af236f1f68146de2c7cd589a40e91a5b626b5061000b7c6d9acdccba01f8aa7cad17576315f031d9c3050a409c0354c3d90a170727aa69b8b70d6e30089b3152bbc1c4924b981cb06718afcd48e6111e94772545133a5e8ab0a3772d78635e169a3e91d7c8c0389523e7f15089714818326d708c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","groth16_vkey_hash":[106,41,6,172,110,196,97,200,202,205,91,53,87,21,127,89,49,123,213,114,229,123,200,37,57,117,170,245,197,172,180,136]}},"stdin":{"buffer":[[11,0,0,0,0,0,0,0]],"ptr":0,"proofs":[]},"public_values":{"buffer":{"data":[1]}},"sp1_version":"v2.0.0"}
1 change: 1 addition & 0 deletions examples/wasm_example/data/is-prime_plonk_proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"proof":{"Plonk":{"public_inputs":["79974752685130133349989020271128338092086594955138314388631360057283056061","5408444369343130360314198614562428875014335023761526637751134529685417182618"],"encoded_proof":"0973da201577ae244b4f27a74e7303f20b779e2994fd15a20ecf35c7eb44824f039aea4f9eabcbbade4fbde0b2868994d80ff8bcb7c4303588594ee4b2ec844406f4e87b8e8fd35694b5c33b94c915ba5e66b590abf886168130763e2ee3f413152910a9b571f1195980344cac50a17ba3692f33edf6dbb61af4bfa7771ac3cd2fd22e672f4d0eb9921817a9d68cd5b312a58e1ee4bdfa9030010c7c86d9bf0c0ea56195721ae59442f72bbf9579a45a5f0a449713b66956fbb57ba44aa63e1007373a317932303728e8d0c1da55239420f9e9be45bf701dc568877ca46adddc2b2abc8e6ebb632dad0250edc2f3b8fcb7ee76650b903533f4f8e2c70a7302551cdc7852d8a8f23986b97463ca00dbaf15bbde95acaaabb1529be85d04f425470b2a05aedbbe2840bef213cf1b361bbc8ddc5af1afa0dd7b9d8f98856fdf32c30ed59b449f1f3e7fd379bdc527b1997f2c65df2ceb1fe716b1f9a689fceb814400682c94c78067a529593b30965bfbeccc095fd5b1eea72750098a3eaf7983692619aea64181292b9dce108d33374fa7b60e27328abad7407546dc20b095c08b22a957227bb13b726bb3ea2faff99fe5c78c0d3b130eadf7fe06cac70029d2d621747afa6679ca83dc6a17e9801ddc899232e4a6f931b816df144471e898df5f1f85cfd1d5f2569dcb3c78fff45244d276e54b6bce08cae1decc8ba81d615ecf12e9acb09dcb760c8a9d4dc3504a5f1e67f54eeb7465e074dd4942c80ce8aade04e30c01ee14cc0ceae2cc00f9d171489dfc0fb0da8fd4d187104c7322ba7e5d1fb1218bc264eac64a36e3a8244dc4806d845f11c055d7fb2f8d35877a366aa422fb7a8dac0c734383286433288803b43b694e2a3ede3cf74a08df523f094e3129521f97bf34d2e84a24d6683b9faee91895c448dfd0af250b70971b184f7af206299aa455c3ce64a181d0d86030fbd7ba2600c33e02ccd239a1401dbcc4ea572a275e0ff36074956319cb64e3483bad98193ae2ea95821c406ee9617790fe570d496b062be351566e4e723e4d0697c1559daa6b9f159bf53da66a2a508633e4103cd86778387d0623a3138dfe1596eb141de042e281c3f5aaee1154ff8345530e3202f68ac1b88836e32656411334f3e1668523ad3b810d8ead2335adb77e7805996ea733fee9c8359ac62e0c7194b7b3ec204c5b989b0d19c27255c72310ca","raw_proof":"0973da201577ae244b4f27a74e7303f20b779e2994fd15a20ecf35c7eb44824f039aea4f9eabcbbade4fbde0b2868994d80ff8bcb7c4303588594ee4b2ec844406f4e87b8e8fd35694b5c33b94c915ba5e66b590abf886168130763e2ee3f413152910a9b571f1195980344cac50a17ba3692f33edf6dbb61af4bfa7771ac3cd2fd22e672f4d0eb9921817a9d68cd5b312a58e1ee4bdfa9030010c7c86d9bf0c0ea56195721ae59442f72bbf9579a45a5f0a449713b66956fbb57ba44aa63e1004e30c01ee14cc0ceae2cc00f9d171489dfc0fb0da8fd4d187104c7322ba7e5d1fb1218bc264eac64a36e3a8244dc4806d845f11c055d7fb2f8d35877a366aa407373a317932303728e8d0c1da55239420f9e9be45bf701dc568877ca46adddc2b2abc8e6ebb632dad0250edc2f3b8fcb7ee76650b903533f4f8e2c70a7302551cdc7852d8a8f23986b97463ca00dbaf15bbde95acaaabb1529be85d04f425470b2a05aedbbe2840bef213cf1b361bbc8ddc5af1afa0dd7b9d8f98856fdf32c30ed59b449f1f3e7fd379bdc527b1997f2c65df2ceb1fe716b1f9a689fceb814400682c94c78067a529593b30965bfbeccc095fd5b1eea72750098a3eaf79836929521f97bf34d2e84a24d6683b9faee91895c448dfd0af250b70971b184f7af206299aa455c3ce64a181d0d86030fbd7ba2600c33e02ccd239a1401dbcc4ea57000000071e25d230403568dfc46145bb41153c4936bf7392dae70bdb98678571b4b739322619aea64181292b9dce108d33374fa7b60e27328abad7407546dc20b095c08b22a957227bb13b726bb3ea2faff99fe5c78c0d3b130eadf7fe06cac70029d2d621747afa6679ca83dc6a17e9801ddc899232e4a6f931b816df144471e898df5f1f85cfd1d5f2569dcb3c78fff45244d276e54b6bce08cae1decc8ba81d615ecf12e9acb09dcb760c8a9d4dc3504a5f1e67f54eeb7465e074dd4942c80ce8aade103cd86778387d0623a3138dfe1596eb141de042e281c3f5aaee1154ff8345532a275e0ff36074956319cb64e3483bad98193ae2ea95821c406ee9617790fe570d496b062be351566e4e723e4d0697c1559daa6b9f159bf53da66a2a508633e422fb7a8dac0c734383286433288803b43b694e2a3ede3cf74a08df523f094e31000000010e3202f68ac1b88836e32656411334f3e1668523ad3b810d8ead2335adb77e7805996ea733fee9c8359ac62e0c7194b7b3ec204c5b989b0d19c27255c72310ca","plonk_vkey_hash":[74,202,36,10,62,82,150,230,165,101,249,141,199,40,198,244,143,141,228,121,42,143,163,101,3,140,59,134,149,33,118,245]}},"stdin":{"buffer":[[11,0,0,0,0,0,0,0]],"ptr":0,"proofs":[]},"public_values":{"buffer":{"data":[1]}},"sp1_version":"v2.0.0"}
Loading