Skip to content

Commit

Permalink
Merge branch 'add_circom_example' of https://github.com/lambdaclass/l…
Browse files Browse the repository at this point in the history
…ambdaworks into add_circom_example
  • Loading branch information
jotabulacios committed Oct 23, 2024
2 parents 73357bb + 97f6c4e commit 2940b73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Below is a list of examples to understand lambdaworks and learn what you can bui
- [BabySNARK](./examples/baby-snark/)
- [Pinocchio](./examples/pinocchio/)
- [Using Circom with lambdaworks's Groth16](./provers/groth16/circom-adapter/src/README.md)
- [Proving Fibonacci using Circom and lambdaworks](./examples/prove-verify-circom/circom_lambdaworks_tutorial.md)

- You can use Circom to generate circuits and use lambdaworks's capabilities to prove the execution with [Groth16](./provers/groth16/README.md).
- You can use the [Stark prover](./provers/stark/src/) to define an algebraic intermediate representation (AIR) and prove the execution of a program
Expand Down
46 changes: 22 additions & 24 deletions examples/prove-verify-circom/circom_lambdaworks_tutorial.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Groth16 from Circom to Lambdaworks tutorial

In this small tutorial we will perform all the steps needed to prove a computation using Groth16
In this small tutorial, we will perform all the steps needed to prove a computation using Groth16.

As said above, our goal is to prove a computation, in this case we want to prove the Fibonacci sequence.
As said above, our goal is to prove a computation. In this case we want to prove the Fibonacci sequence.

If you don´t know what Groth16 does, you can read our [post](https://blog.lambdaclass.com/groth16/)
If you don´t know what Groth16 does, you can read our [post](https://blog.lambdaclass.com/groth16/).

Let's begin by creating the circuit and the input for the program using [Circom2](https://docs.circom.io/getting-started/installation/)
Let's begin by creating the circuit and the input for the program using [Circom2](https://docs.circom.io/getting-started/installation/).


> [!IMPORTANT]
> This tutorial will work within `lambdaworks/provers/groth16` to make it easier to follow along. If you prefer, you can create a new rust project, but you will need to import all the crates from the library
> This tutorial will work within `lambdaworks/provers/groth16` to make it easier to follow along. If you prefer, you can create a new rust project, but you will need to import all the crates from the library.
As we want to cover all the steps from creating a program to prove it using Groth 16, the first thing to do is to create the the folder for our program.
As we want to cover all the steps from creating a program to prove it using Groth 16, the first thing to do is to create the folder for our program.

Create a file named `fibonacci` inside `test_files`.This is where our program will be.
Create a file named `fibonacci` inside `test_files`.This is where our program will be.

The directory should look like this:
```
Expand Down Expand Up @@ -67,7 +67,7 @@ template Fibonacci(n) {
// Instantiate Fibonacci as the main circuit with a specific n value
component main = Fibonacci(10);
```
in our case this will be named `fibonacci.circom`
In our case this will be named `fibonacci.circom`.


In this circuit:
Expand All @@ -77,7 +77,7 @@ In this circuit:

### Creating inputs for the Circuit

Once we have defined the circuit, the next step is to provide the inputs needed for the proving procces. In this case, our inputs consists of the starting numbers for the Fibonacci sequence
Once we have defined the circuit, the next step is to provide the inputs needed for the proving process. In this case, our inputs consists of the starting numbers for the Fibonacci sequence.

As we want to use F(0) = 1 and F(1) = 1, the input JSON file will look like this;

Expand All @@ -89,7 +89,7 @@ As we want to use F(0) = 1 and F(1) = 1, the input JSON file will look like this

The file should be named `input.json`.

Inside the same directory where those file are run
Inside the same directory where those files are run

```bash
circom fibonacci.circom --r1cs --wasm -p bls12381
Expand All @@ -102,7 +102,7 @@ Here
That will create a `fibonacci_js` directory and a `fibonacci_r1cs `file

> [!WARNING]
> Do not skip the -p bls12381 flag as this is the only field supported by the adapter right now. If not specified the default is bn128 and the proving will not be possible
> Do not skip the -p bls12381 flag, as this is the only field supported by the adapter right now. If not specified, the default is bn128 and the proving will not be possible

To compute the `witness` execute
Expand Down Expand Up @@ -137,7 +137,7 @@ fibonacci/
└── witness.wtns
```

We only need `fibonacci.r1cs.json` and `witness.json` so if you want, yo can delete the unnesesary files by running:
We only need `fibonacci.r1cs.json` and `witness.json` so, if you want, you can delete the unnecessary files by running:
```
rm -rf fibonacci_js fibonacci.circom fibonacci.r1cs witness.wtns input.json
```
Expand All @@ -154,33 +154,31 @@ rm -rf fibonacci_js fibonacci.circom fibonacci.r1cs witness.wtns input.json; # D

## Using Lambdaworks Circom Adapter

As mentioned in the blog post, the main goal of Groth16 (or any other prover) is to prove computations.
The function `circom_to_lambda` will take the `fibonacci.r1cs.json` and the `witness.json` data and translate it into the format needed for Lambdaworks.


As mentioned in the blog post, the main goal of Groth16 (or any other prover) is to prove computation.
The function `circom_to_lambda` will take the `fibonacci.r1cs.json` and the `witness.json` data and translate it into the format needed for Lambdaworks.

The `circom_to_lambda` function is responsible for converting the R1CS constraints and witness generated by Circom into a format that Lambdaworks can use to construct a Quadratic Arithmetic Program (QAP). The QAP representation is essential for Groth16 proof generation, as it represents the arithmetic circuit in terms of polynomials.

### How `circom_to_lambda` Works
### How `circom_to_lambda` works

The function `circom_to_lambda` does the following:

1. Parse JSON Data: It parses the R1CS constraints and witness files from JSON format into Rust data structures using serde_json
1. Parse JSON Data: It parses the R1CS constraints and witness files from JSON format into Rust data structures using serde_json.

2. Build LRO Matrices: It extracts the L,R,O matrices which represent the variables involved in each constraint.

3. Adjust Witness: It adjusts the order of inputs and outputs in the witness to match Lambdaworks's format. Circom and Lambdaworks have different conventions for witness ordering, so this adjustment ensures compatibility
3. Adjust Witness: It adjusts the order of inputs and outputs in the witness to match Lambdaworks's format. Circom and Lambdaworks have different conventions for witness ordering, so this adjustment ensures compatibility.

4. Construct QAP: It uses the L,R,O matrices to build a Quadratic Arithmetic Program (QAP). This QAP is used in the Groth16 proving process.


## Generating the Proof with Groth16
## Generating the proof with Groth16

After converting the data using `circom_to_lambda`, you can use the resulting QAP and witness to generate a Groth16 proof. This process involves two key steps: creating the proving key and then using it to generate the proof based on the QAP. Here’s a detailed explanation of the process:

We will put all together inside `integration_test`
We will put all together inside `integration_test`.

Step 1: Read the R1CS and Witness Files
Step 1: Read the R1CS and Witness Files.
First, you need to read the R1CS (Rank-1 Constraint System) file and the witness file generated by Circom.

```rust
Expand All @@ -198,7 +196,7 @@ let (qap, w) = circom_to_lambda(

`circom_to_lambda` : Converts the content of the R1CS and witness files into a format compatible with Lambdaworks. This function parses the JSON content and produces a QAP and a corresponding witness vector.

Step 2: Generate the Proving and Verifying Keys
Step 2: Generate the Proving and Verifying Keys:

```rust
let (pk, vk) = setup(&qap);
Expand Down Expand Up @@ -280,7 +278,7 @@ If everything is set up correctly, you should see a success message confirming t
Proof verification succeeded. All steps completed.
```

# Conclusion
## Summary

Congratulations! You have successfully set up a Groth16 proof for a Fibonacci circuit using Circom and Lambdaworks. This tutorial walked you through the entire process—from defining the circuit in Circom, generating the R1CS and witness, converting the data for use in Rust, and finally, creating and verifying the proof using Lambdaworks.

0 comments on commit 2940b73

Please sign in to comment.