Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decider circuit RelaxedR1CS #21

Merged
merged 1 commit into from
Sep 7, 2023
Merged

Conversation

arnaucube
Copy link
Collaborator

@arnaucube arnaucube commented Sep 5, 2023

This PR implements the approach that @nalinbhardwaj proposed (#19), in order to avoid needing to modify an existing SNARK prover to work with Relaxed R1CS. The main idea is to have a R1CS circuit that checks the Relaxed R1CS relation (Az∘Bz - uCz-E=0), and is this circuit the one which is then proved with the SNARK prover.

As an example, for an original circuit of 1k constraints, it takes ~3k constraints for the RelaxedR1CS checker circuit.

The following table provides some approximate results to have a bit of intuition on the numbers:

original circuit description num constr original circuit num constr RelaxedR1CS checker circuit with R1CS matrices as witness num constr RelaxedR1CS checker circuit with R1CS matrices as constraints
y=x^3+x+5 3 19 8
sha256 40k 304k 109k
2x sha256 79.7k 605.6k 217k
CustomTestCircuit 40k 240k 120k
CustomTestCircuit 79k 474k 237k
CustomTestCircuit 1k 6k 3k
CustomTestCircuit 10k 60k 30k
CustomTestCircuit 1M 6M 3M

(CustomTestCircuit logic is just a loop of multiplications in order to generate the number of constraints desired, it's logic can be found here)

Note that the CustomTestCircuit is very sparse, that's why although original circuits with similar number of constraints for sha256 & CustomTestCircuit, they will have a bit different number of constraints for the RelaxedR1CS checker circuit.

For the circuit implementation I'm using sparse matrices shape, which I'm not 100% sure that is the proper way to handle the circuit matrices in terms of constraints. If needed I could update it implementing it with dense matrices.

Commit msg:
Add decider circuit RelaxedR1CS

  • Add naive decider circuit RelaxedR1CSGadget, which in-circuit checks that the given z satisfies the given RelaxedR1CS instance
  • Add method to relax the R1CS instance
  • Add check_relation (for testing only) to R1CS & RelaxedR1CS
  • Migrate from own SparseMatrix to use ark_relations::r1cs::Matrix
  • Add frontend helper to use arkworks circuits

resolves #19

Edit1: I've requested also review from @nalinbhardwaj (through chat as I can not put him as reviewer through the GitHub interface), since this approach was originally proposed by him.
Edit2: updated the table with the most-right column.

Copy link
Collaborator

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Only have a question on the decider usecase.

src/decider/circuit.rs Show resolved Hide resolved
@arnaucube arnaucube force-pushed the feature/decider-circuit-rel-r1cs branch from 14d49dc to 029829a Compare September 6, 2023 15:39
@arnaucube
Copy link
Collaborator Author

Update: @han0110 made a great point here: #21 (comment) 🙌
I've updated the code to use the R1CS matrices as constants instead of witnesses (here the small diff) as @han0110 suggested, and the blowup factor of number of constraints for the circuit got from 6x to 3x!
Updated also the table from the PR message adding another column with the new numbers of constraints.

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Left some nits to be addressed optionally.

src/ccs/r1cs.rs Show resolved Hide resolved
src/ccs/r1cs.rs Show resolved Hide resolved
@arnaucube arnaucube force-pushed the feature/decider-circuit-rel-r1cs branch from 5aab2be to 30bb79b Compare September 7, 2023 17:13
- Add naive decider circuit `RelaxedR1CSGadget`, which in-circuit checks
  that the given z satisfies the given RelaxedR1CS instance
- Add method to relax the R1CS instance
- Add check_relation (for testing only) to R1CS & RelaxedR1CS
- Migrate from own SparseMatrix to use ark_relations::r1cs::Matrix
- Add frontend helper to use arkworks circuits
@arnaucube arnaucube force-pushed the feature/decider-circuit-rel-r1cs branch from 30bb79b to fafbe82 Compare September 7, 2023 17:19
@arnaucube arnaucube added this pull request to the merge queue Sep 7, 2023
Merged via the queue into main with commit ac913ab Sep 7, 2023
3 checks passed
@pnyda
Copy link

pnyda commented Sep 16, 2023

May I ask how we're gonna use Sha256Gadget?

@arnaucube
Copy link
Collaborator Author

May I ask how we're gonna use Sha256Gadget?

The Sha256Gadget only was used only for the test to have some numbers about the constraints of a 'real circuit' passed through the relaxed-r1cs verifier circuit. No plans (at least for now) to use it in the library itself (other than in the mentioned test).

@arnaucube arnaucube deleted the feature/decider-circuit-rel-r1cs branch November 7, 2023 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
circuits decider decider/compressed snark Nova
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Decider's RelaxedR1CS in-circuit circuit
4 participants