Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The current system to convert a cs to a sparseR1CS doesn't take in account duplicate constraints, factorisation, etc, and a lot of constraints coming from a QAP are converted multiple times, ending up with a blow up of the number of constraints. This PR fixes this issue.
Description
Example
This is an example of a series of Double() called on a G1Affine point, expressed as a QAP, each block corresponds to a single double, reusing the previous result.
We see that each block starts with a square, reusing a linear expression from the previous block. Moreover this linear expression grows, and can be factored using an already computed sub linear expression. The current system doesn't leverage that, and the longer the linear expression the greater the number of corresponding plonk constraints.
Fix
To fix it,
toSparseR1CS
now uses a record of sorted linear expressions encountered. Each time we encounter a linear expression to split, it is now turned into a "primitive" linear expression, which means that the linear expression becomesprimitive_linearExpression = 1/gcd(coefs) * linearExpression
(the sign of the gcd changes according to the sign of the first coef to ensure non ambiguity). Theprimitive_linearEpression
is recursively split, yielding a single term that is equal to the linear expression, but only plonk constraints are used to reach it. It is then multiplied by the original gcd to retrieve the correct value.The
split
function now checks if the leftmost part of a linear expressionl[:i]
for i from len(l)-1 to 0 is in the record, and the rightmost part is recursively split. This strategy covers a lot of sub linear expression, however in order to work all the time, one should in fact check all the possible sub linear expressions (2 to the len(l) possiblities). But since the record stores the sorted linear expression, only very specific patterns are not caught by the fix (I can describe it if needed, briefly it happens only when added terms to a recorded linear expression are in between consecutive terms of the recorded expression after sorting it).Results
The
TestScalarMulG1
circuit took ~430k constraints due to the blow up, now it's at ~7k constraints.Tests
The regression tests fail, however it's due to the fact that now the plonk circuits take less constraints.