This time, the task is a bit unusual: you must implement a simple Rank-1 Constraint System (R1CS) in Rust. For that reason, consider a pretty simple problem: the prover
For that reason, we construct the circuit of the following form:
Here, we need only two constraints to check the correctness of the prover's statement:
-
$r_1 = x \times x$ . -
$r_2 = x \times r_1 - y$ .
Therefore, the solution vector becomes
-
Implement the basic Linear Algebra operations for R1CS in Rust.
-
Implement the R1CS satisfiability check.
-
Construct the matrices
$L$ ,$R$ ,$O$ to check the satisfiability of the given solution vector$\mathbf{w}$ (checking the cubic root of given$y$ ).
Download Rust (in case you do not have one), clone/fork the repository, and verify that everything compiles (just that, the code does not work yet). In case you are confused, the project is structured as follows:
Path | Description |
---|---|
src/main.rs |
The entrypoint where you can test your implementation. |
src/finite_field.rs |
Contains the |
src/linear_algebra.rs |
Basic Linear Algebra operations (with vectors and matrices) you need to implement. |
src/r1cs.rs |
R1CS implementation where you also would need to implement a piece of functionality. |
In case you have installed everything correctly, the project compiles, you have successfully completed this task!
Now, recall that our ultimate goal is to construct the matrices
where
For that reason, we need to have the Hadamard product (element-wise multiplication) and inner (dot) product of two vectors and the matrix-vector product. For that reason, implement the following functions in the linear_algebra.rs
module:
-
Vector::dot(&self, other: &Self) -> Fp
: the inner product of two vectors. -
Vector::hadamard_product(&self, other: &Self) -> Self
: the Hadamard (elementwise) product$\mathbf{v} \odot \mathbf{u}$ of two vectors. -
Matrix::hadamard_product(&self, other: &Self) -> Self
: the Hadamard (elementwise) product$A \odot B$ of two matrices. -
Matrix::vector_product(&self, other: &Vector) -> Vector
: the matrix-vector product$A\mathbf{v}$ .
To test the correctness of your implementation, run
cargo test linear_algebra
Now, we need to implement the R1CS satisfiability check. For that reason, implement the following functions in the r1cs.rs
module:
-
R1CS::is_satisfied(&self, witness: &Vector<WITNESS_SIZE>) -> bool
: the function that checks the satisfiability of the given solution vector$\mathbf{w}$ . -
R1CS::is_constraint_satisfied(&self, witness: &Vector<WITNESS_SIZE>, j: usize) -> bool
: the function that checks whether the$j$ -th constraint is satisfied.
To test the correctness of your implementation, run
cargo test r1cs
Now, as the final step, construct the matrices main.rs
file. This file will automatically:
- Generate a random valid witness.
- Construct the R1CS with the given matrices
$L$ ,$R$ ,$O$ . - Check the satisfiability of the given solution vector.
Note
In the lecture, we considered a bit more complicated circuit r1cs.rs
file in the tests
module and adapt it to the cubic root problem.