-
Notifications
You must be signed in to change notification settings - Fork 55
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
Onchain decider circuit for Protogalaxy #145
Onchain decider circuit for Protogalaxy #145
Conversation
80e991a
to
c447906
Compare
5dc7e5f
to
a8f3449
Compare
Together with the existing `MatrixGadget` and `VectorGadget`, we can now use the same logic for checking R1CS satisfiability of `R1CSVar` both natively and non-natively.
… folding commitments
9ad522f
to
74a08d5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work!!!! This is really great! 🙌
Big improvements with the abstractions! Great job!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!!!
What a PR. This one might be one of the best ones I've seen in quite some time. Really well documented and with a nice abstraction.
Congrats on this work!!! 🔥
sponge: &T, | ||
pp_hash: FpVar<CF2<C>>, // public params hash | ||
) -> Result<(FpVar<CF2<C>>, Vec<FpVar<CF2<C>>>), SynthesisError> { | ||
let mut sponge = sponge.clone(); | ||
let U_vec = self.to_native_sponge_field_elements()?; | ||
sponge.absorb(&pp_hash)?; | ||
sponge.absorb(&U_vec)?; | ||
Ok((sponge.squeeze_field_elements(1)?.pop().unwrap(), U_vec)) | ||
Ok(( | ||
// `unwrap` is safe because the sponge is guaranteed to return a single element |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love that you added these types of justifications! <3
/// `U_vec` is `U` expressed as a vector of `FpVar`s, which can be reused | ||
/// before or after calling this function to save constraints. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I follow exactly how do we have these constraints. COuld you briefly explain? Or refer me to somewhere to get the intuition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@winderica can explain better the specifics in this call, but for context: this comes from Nova impl. The idea was that in the U.hash()
call, internally the vector of field elements that represents all the parameters (including points coordinates) of U
is computed, and later in other parts of the logic of the decider circuit this vector is needed again. So instead of computing it 2 times (inside the circuit), it is computed once and returned so that it can be reused without needing more constraints later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @arnaucube! The use of U_vec
here is exactly inspired by CommittedInstanceVarOps::hash
.
The reason why reusing U_vec
saves constraints, or why recalculating U_vec
requires constraints, is due to the implementation of NonNativeUintVar::to_constraint_field
.
sonobe/folding-schemes/src/folding/circuits/nonnative/uint.rs
Lines 509 to 521 in 6d8f297
impl<F: PrimeField> ToConstraintFieldGadget<F> for NonNativeUintVar<F> { | |
fn to_constraint_field(&self) -> Result<Vec<FpVar<F>>, SynthesisError> { | |
let bits_per_limb = F::MODULUS_BIT_SIZE as usize - 1; | |
let limbs = self | |
.to_bits_le()? | |
.chunks(bits_per_limb) | |
.map(Boolean::le_bits_to_fp_var) | |
.collect::<Result<Vec<_>, _>>()?; | |
Ok(limbs) | |
} | |
} |
In a NonNativeUintVar
, there are many limbs, and each limb currently stores a FpVar
that has a small number of bits (much smaller than the field order) for efficiency of integer operations like addition and multiplication. However, a small number of bits per limb will result in more limbs, and consequently, hashing a NonNativeUintVar
will require more rounds of permutations and thus more constraints.
To save the costs of hashing, we convert the limbs of NonNativeUintVar
to a compact form in NonNativeUintVar::to_constraint_field
, such that each new limb now stores as many bits as possible (i.e., F::MODULUS_BIT_SIZE as usize - 1
), and the number of limbs becomes as small as possible. This process introduces constraints because it decomposes all limbs into bits and then recomposes the bits back to FpVar
s.
By reusing U_vec
, we no longer need to call NonNativeUintVar::to_constraint_field
multiple times, thus saving constraints.
Having said that, I think it is possible to avoid constraints in NonNativeUintVar::to_constraint_field
while producing short hash preimages. To this end, we do not decompose and recompose limbs. Instead, we directly compose several limbs into one via linear combination, which does not introduce any constraints. Although the number of bits in each new limb is unlikely to be the optimal value F::MODULUS_BIT_SIZE as usize - 1
, the number of limbs should still be pretty short. In this way, converting U
to FpVar
s becomes nearly free, and we may remove U_vec
in favor of a user friendlier API.
pub randomness: D::Randomness, | ||
|
||
/// CycleFold running instance | ||
pub cf_U_i: CycleFoldCommittedInstance<C2>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This forces us to always use Cyclefold I assume right? Not that I'm against it. But would be nice to maybe mention somewhere that "vanilla Nova" isn't supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been like this since Sonobe was started, always Nova+CycleFold (in all combinations (onchain&offchain deciders, also in the IVC without deciders)), also for HyperNova & ProtoGalaxy, both using CycleFold.
I mean, is not that this specific new line forces to use CycleFold instead of 'vanilla Nova', but that the entire project is around CycleFold.
Not sure if adding a comment in this line would make it clearer, it appears in the docs & readme, and in many comments along the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it is a good idea to rename the circuit to something like CycleFoldBasedOnchainDeciderCircuit
? The point is, when someone propose a new paradigm for converting folding schemes to IVC, we can implement the corresponding decider circuit for all folding schemes, and the user can choose the best decider circuit for their use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe once that new paradigm appears and it is implemented we can then rename the current method if needed, but in general I try to not abstract in advance, like for the moment this struct is the only possibility in the code, and it's mentioned in the comments that it is cyclefold-based. On the current code we would be only making the name of the struct longer while not distinguishing from another struct since it would not exist yet.
If at some point we add another alternative to the OnchainDeciderCircuit that is not based on the CycleFold approach, then agree that renaming it into this name is what would make sense.
How do you see it? maybe I'm missing some part of the point, and at the end making the name longer would not be a problem neither, so if you think it makes more sense I'm happy with it too, but with the info at hand seems that it would not make things clearer or help to distinguish from other cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed! I am neither a big fan of premature abstraction, so let's keep the name as is until a new scheme is proposed :)
b812dd6
Following #157 and #162, this PR implements generic on-chain and off-chain decider circuits
GenericOnchainDeciderCircuit
andGenericOffchainDeciderCircuit{1, 2}
that can handle all folding schemes (Nova, HyperNova, and Protogalaxy, hopefully even Mova and Ova in the future).The abstraction in #157 and #162 provide a unified yet flexible usage of these objects, leading to highly reuseable decider circuits. As an example, we can take a look at the definition and internal logic of
GenericOnchainDeciderCircuit
below:sonobe/folding-schemes/src/folding/circuits/decider/on_chain.rs
Lines 64 to 110 in 7add907
sonobe/folding-schemes/src/folding/circuits/decider/on_chain.rs
Lines 232 to 321 in 7add907
With
GenericOnchainDeciderCircuit
, creating the on-chain decider circuit for Protogalaxy is as simple as giving a type alias and implementing theTryFrom
trait:sonobe/folding-schemes/src/folding/protogalaxy/decider_eth_circuit.rs
Lines 74 to 150 in 7add907
Below I list several additional modifications made in this PR:
EquivalenceGadget
that can check the equivalence (equality for native field elements, congruence for non-native field elements) between two in-circuit variables.DeciderEthCircuit
of HyperNova, which verifies the field operations in NIFS.V in-circuit. (Note that in c447906, I mistakenly ignored the group operations in NIFS.V. In fact, these operations are expensive in-circuit and should be handled by the verifier, which is added in 5dc7e5f .)u
andx
in Nova) are no longer necessary to be public. They are thus removed from public inputs to reduce the verifier's costs. The smart contract for on-chain verification is also updated.P.S.
While this PR should already be ready for review, please focus on what you are working on now (such as #133). I don't want to bother the other contributors with merge conflicts due to this huge PR and am happy to rebase at my end after the pending PRs get merged :)Conflicts resolved :)