|
| 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. |
0 commit comments