Skip to content

Commit 926c0f7

Browse files
Merge branch 'main' into main
2 parents 31f73ea + 7fe890e commit 926c0f7

File tree

14 files changed

+452
-403
lines changed

14 files changed

+452
-403
lines changed

Diff for: crates/provers/groth16/circom-adapter/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ repository.workspace = true
1111
lambdaworks-math.workspace = true
1212
lambdaworks-groth16.workspace = true
1313

14-
serde = { version = "1.0" }
14+
serde = { version = "1.0", features = ["derive"] }
1515
serde_json = "1"

Diff for: crates/provers/groth16/circom-adapter/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Circom - Lambdaworks Groth16 Adapter
2+
3+
This package allows one to perform trusted setup, prove, and verify constraints generated by [SnarkJS](https://github.com/iden3/snarkjs) from a [Circom](https://github.com/iden3/circom) circuit over [BLS12-381](https://github.com/lambdaclass/lambdaworks/tree/main/crates/math/src/elliptic_curve/short_weierstrass/curves/bls12_381).
4+
5+
## Setup
6+
7+
1. Install [Circom](https://github.com/iden3/circom) and [SnarkJS](https://github.com/iden3/snarkjs).
8+
9+
2. Compile your circuit with `circom`, e.g. for a circuit named `test.circom` you would do:
10+
11+
```bash
12+
circom test.circom --r1cs --wasm -p bls12381
13+
```
14+
15+
> [!IMPORTANT]
16+
> Note that `-p bls12381` is important as that is the field supported by Lambdaworks Circom Adapter.
17+
18+
3. Compiling a circuit like above will create a `test_js` directory, and a `test.r1cs` file. Now, we will create a witness for an input that is saved within `input.json`:
19+
20+
```bash
21+
node test_js/generate_witness.js test_js/test.wasm input.json witness.wtns
22+
```
23+
24+
This will generate a **witness.wtns** file.
25+
26+
4. For the Circom Adapter we need to export the witness and R1CS files as JSON:
27+
28+
```bash
29+
snarkjs wtns export json witness.wtns witness.wtns.json
30+
snarkjs r1cs export json test.r1cs test.r1cs.json
31+
```
32+
33+
To do these steps all at once, you can copy-paste the following snippet to your terminal in the same directory as your circuit, using your own circuit name instead of `test` here:
34+
35+
```bash
36+
circom test.circom --r1cs --wasm -p bls12381;
37+
38+
node test_js/generate_witness.js test_js/test.wasm input.json witness.wtns;
39+
40+
snarkjs wtns export json witness.wtns;
41+
snarkjs r1cs export json test.r1cs test.r1cs.json;
42+
```
43+
44+
## Usage
45+
46+
This crate exposes a `circom_to_lambda` function along with readers for R1CS and witness files. `circom_to_lambda` accepts a Witness and R1CS.
47+
48+
```rust
49+
let circom_r1cs = read_circom_r1cs("test.r1cs.json").expect("could not read r1cs");
50+
let circom_wtns = read_circom_witness("witness.json").expect("could not read witness");
51+
52+
let (qap, wtns, pubs) = circom_to_lambda(circom_r1cs, circom_wtns);
53+
```
54+
55+
This function returns a Lambdaworks-compatible QAP, the witness assignments and public signals. Then one should perform setup, prove, and verify. Here's the complete procedure:
56+
57+
```rust
58+
fn poseidon_parse_prove_verify() {
59+
let (qap, wtns, pubs) = circom_to_lambda(
60+
&fs::read_to_string("test.r1cs.json").expect("Error reading file"),
61+
&fs::read_to_string("witness.json").expect("Error reading file"),
62+
);
63+
64+
let (pk, vk) = setup(&qap);
65+
let proof = Prover::prove(&wtns, &qap, &pk);
66+
let accept = verify(&vk, &proof, &pubs);
67+
assert!(accept);
68+
}
69+
```
70+
71+
## Examples
72+
73+
There are a few examples within the [tests](./tests/) folder:
74+
75+
- [`poseidon_test.rs`](./tests/poseidon_test.rs): Poseidon hash of $100$ is proven and verified.
76+
- [`vitalik_test.rs`](./tests/vitalik_test.rs): Here we demonstrate the example from [Vitalik's Medium post](https://medium.com/@VitalikButerin/quadratic-arithmetic-programs-from-zero-to-hero-f6d558cea649) where $x^3 + x + 5 = 35$ is zk-proven.

Diff for: crates/provers/groth16/circom-adapter/src/README.md

-83
This file was deleted.

Diff for: crates/provers/groth16/circom-adapter/src/integration_tests.rs

-104
This file was deleted.

0 commit comments

Comments
 (0)