This is a simple implementation of an algorithm that shows how to construct an "almost" ZK circuit using R1CS. By "almost", it means that the implementation enforces the correct computation of constraints but lacks full zero-knowledge properties since the witness is exposed and it does not provide privacy guarantees of ZK proofs.
As highlighted by the material from RareSkills, this algorithm is academic and should not be used in any form of production.
This example is built using Rust, and is based on the second module of RareSkill's Book of Zero Knowledge, chapter 3: Building a Zero Knowledge Proof from an R1CS.
- Elliptic Curves over Finite Fields
- Bilinear Pairings (which also means knowing Group Theory and Homomorphism)
- Rank 1 Constraint Systems
We want to prove the claim that we know the x
and y
values that satisfy:
For the above polynomial, we can break it down to the following set of constraints:
The system of equations satisfy the requirements of an R1CS. That is, every constraint has a single non-constant multiplication, and our system of equations are in the form:
Where
The witness vector is:
And
The multiplication of each matrix with
In R1CS, all operations are performed in modular arithmetic.
Given our witness vector, we want to prove that it satisfies the R1CS without us having to directly reveal it during the verification step. While this example is not fully zero-knowledge, we multiply each term in the witness vector with
Our "encrypted" witness would look something like this:
Where
The Bn254
curve used by Ethereum supports the pairing:
During the verification step, we want to check:
And this works because by bilinearity, both sides yield elements in
And this holds if and only if
To verify the R1CS constraints, we want to lift each linear combination (the dot-product of each matrix with the witness) onto the elliptic curve (i.e. as elliptic curve points). This means we compute the following:
Since all linear combinations (scalars) are now lifted to the elliptic curve, we can use bilinear pairings to compare them constraint-by-constraint.
That is for every row
Each pairing on both sides of the equality evaluates to an element in the target group
RareSkills Book of ZK, where all the good stuff is.