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

Refactor away from PairingEngine + misc type changes #61

Merged
merged 33 commits into from
Jan 11, 2022

Conversation

ghost
Copy link

@ghost ghost commented Dec 16, 2021

Summary of changes:

  1. Refactor anything that is only dependent on the scalar field to not depend on PairingEngine (unfortunately this means there's a lot of E::Fr -> F changes in the diff)
  2. Move dependence on TEModelParams to other places and not Circuit/StandardComposer. In practice this means (I think) the implementation of the fixed-base ECC gadget is determined later, and the variable-base ECC gadget depends on the params but not the rest of the circuit.
  3. Suggested change to remove the TranscriptWrapper type
  4. Minor change to Permutation generic types (can be reversed)
  5. Some other minor tweaks to types (can be reversed). I was mostly just experimenting to see what might be slightly cleaner.

In summary this PR is not really ready to be merged, but open to input and feedback.

@bhgomes
Copy link
Collaborator

bhgomes commented Dec 16, 2021

Thanks for the PR! For the type parameters, can you use a where clause instead of declaring the type constraints inline at the declaration site, it’s easier to read/change this way and keeps it consistent with the rest of the library.

@bhgomes
Copy link
Collaborator

bhgomes commented Dec 16, 2021

Also, if you think it's not ready for review you can mark it as draft so we know it's not ready from the PR list.

@ghost ghost changed the title Refactor away from PairingEngine + misc type changes Draft: Refactor away from PairingEngine + misc type changes Dec 16, 2021
@CPerezz
Copy link
Contributor

CPerezz commented Dec 16, 2021

Hey @joebebel this looks really promising! Willing to see it completed!

Also, as @bhgomes suggested, if you mark it as Draft for now while the PR is not ready would be nice! :)

@CPerezz
Copy link
Contributor

CPerezz commented Dec 16, 2021

@joebebel
Since FFtField does not imply PrimeField, how do we plan to ensure that the field is prime and also extract lots of the associated trait methods info for them?

Also, going this way will probably imply gate-featured verification & prover for IPA and KZG verifications. As now the verification always uses pairings and you don't want to enforce the PairingEngine.

@CPerezz CPerezz marked this pull request as draft December 22, 2021 08:24
@ghost
Copy link
Author

ghost commented Dec 28, 2021

Since FFtField does not imply PrimeField, how do we plan to ensure that the field is prime and also extract lots of the associated trait methods info for them?

we should just bind to FftField when we need those interfaces, and PrimeField when we need that. For practical purposes, I don't think any circuit will be over a non prime field. There is some theoretical possibility where non-fft fields might be desirable, but that seems like an extra complication for now.

Also, going this way will probably imply gate-featured verification & prover for IPA and KZG verifications. As now the verification always uses pairings and you don't want to enforce the PairingEngine.

As long as we're generic over the field and the PolynomialCommitment I think this should work. But it requires some moving of the Rust generics.

@ghost
Copy link
Author

ghost commented Dec 31, 2021

OK, this is getting closer. There are some problems:

  1. For some reason, in SonicKZG and MarlinKZG the open operation on multiple polynomials is not as efficient as the random linear combination before. So more powers of tau in the setup are required. This regression is unfortunate, might require some further attention. I had to increase the setup size to get the tests to pass.
  2. Rust type errors when using the ? operator, as it is unable to convert ark_poly_commit errors to ark_plonk errors implicitly. I tried several ways, I can't figure out how to get it to work, because the ark_poly_commit error is an associated type of PolynomialCommitment it's difficult to debug. For now I just .unwrap() instead, but this isn't a great long term solution.
  3. I added back TEModelParameters as a generic for StandardComposer and other circuit related structs. It would be ideal to use ModelParameters instead (so that short weierstrass inner curves can be used) but after spending some hours trying to figure this out, it's difficult to do in a fully generic way.

I think other than the regression in the efficiency of the polynomial commitment and the error handling, it's ready to review for merge.

@bhgomes
Copy link
Collaborator

bhgomes commented Dec 31, 2021

  1. Rust type errors when using the ? operator, as it is unable to convert ark_poly_commit errors to ark_plonk errors implicitly. I tried several ways, I can't figure out how to get it to work, because the ark_poly_commit error is an associated type of PolynomialCommitment it's difficult to debug. For now I just .unwrap() instead, but this isn't a great long term solution.

Whenever an error type is an associated type of a trait, you cannot write a From impl since it would actually be generic over every type (since the compiler cannot prove that only some types can inhabit the associated type). So instead, I usually use this pattern:

let value = fn_returning_ark_poly_commit_error(x, y, z).map_err(ark_plonk::Error::CustomVariant)?;

It would be nice if the error could be converted with an Into<ark_plonk::Error> impl to remove the map_err but as I said above, this is not possible generally (again, this is only because the error we want to map is an associated type, otherwise this does work).

  1. I added back TEModelParameters as a generic for StandardComposer and other circuit related structs. It would be ideal to use ModelParameters instead (so that short weierstrass inner curves can be used) but after spending some hours trying to figure this out, it's difficult to do in a fully generic way.

I think that the inner curve abstractions in arkworks are lacking here, and in general, there should be a trait that defines the addition and scalar multiplication laws for the curves directly, and the StandardComposer just queries that trait for implementing the circuit. Then, when constructing the circuit, you specify which instance of this trait you want, and the circuit is populated with the correct constraints. The arkworks abstractions are too low level to be leveraged to do this, since they are more about specifying the kind of curve (and performing the direct native computation), than specifying the algorithm itself at compile-time.

Copy link
Contributor

@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.

Just went over the surface and highlighted a few possible leftovers.

Will go in depth as soon as all is cleared! :)

benches/plonk.rs Outdated Show resolved Hide resolved
Comment on lines 293 to 305
// assert_eq!(
// self.variables[&a].into_repr()
// & (E::Fr::from(2u64).pow(&[(num_bits) as u64, 0, 0, 0])
// - E::Fr::one())
// & (F::from(2u64).pow(&[(num_bits) as u64, 0, 0, 0])
// - F::one())
// .into_repr(),
// self.variables[&self.w_l[self.n - 1]]
// );
// assert_eq!(
// self.variables[&b]
// & (E::Fr::from(2u64).pow(&[(num_bits) as u64, 0, 0, 0])
// - E::Fr::one()),
// & (F::from(2u64).pow(&[(num_bits) as u64, 0, 0, 0])
// - F::one()),
// self.variables[&self.w_r[self.n - 1]]
// );
Copy link
Contributor

Choose a reason for hiding this comment

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

We should file an issue for this.
(Not relevant to the PR)

Comment on lines 89 to 95
/*
impl<F, P> GateConstraint<F> for FixedBaseScalarMul<F, P>
where
F: Field,
P: SWModelParameters<BaseField = F>,
{
}*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Leftover?

Copy link
Author

Choose a reason for hiding this comment

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

We need a better way of abstracting the embedded curve type

plonk-core/src/proof_system/widget/mod.rs Outdated Show resolved Hide resolved
plonk-core/src/proof_system/widget/mod.rs Outdated Show resolved Hide resolved
plonk-core/src/circuit.rs Outdated Show resolved Hide resolved
@Pratyush
Copy link

Pratyush commented Jan 5, 2022

For some reason, in SonicKZG and MarlinKZG the open operation on multiple polynomials is not as efficient as the random linear combination before. So more powers of tau in the setup are required. This regression is unfortunate, might require some further attention. I had to increase the setup size to get the tests to pass.

This should not be the case unless you're using hiding and/or strict degree bounds (which plonk doesn't need, AFAIK). I can hop on a call to help debug. btw you can turn on the print-trace feature on ark-poly-commit; that should give you a sense of why opening requires a larger SRS.

@ghost
Copy link
Author

ghost commented Jan 6, 2022

This should not be the case unless you're using hiding and/or strict degree bounds (which plonk doesn't need, AFAIK). I can hop on a call to help debug. btw you can turn on the print-trace feature on ark-poly-commit; that should give you a sense of why opening requires a larger SRS.

I don't know what I changed to fix it, but now it functions correctly. It wasn't a problem with the aggregation, though; the error was TooManyCoefficients { num_coefficients: 80, num_powers: 23 }. In this case the largest degree of the aggregated polynomials, t_4_poly was correctly 80, but only a degree 23 setup was generated. Strangely, t_4_poly was degree 23 in the previous proof in the same test. So somehow the CRS was not correctly sized.

@ghost ghost changed the title Draft: Refactor away from PairingEngine + misc type changes Refactor away from PairingEngine + misc type changes Jan 6, 2022
@LukePearson1
Copy link
Contributor

When this ready for a review, even if still in draft, could you let us know and I'll give a first round? 😄 @joebebel

@ghost
Copy link
Author

ghost commented Jan 6, 2022

I believe this branch is now non-inferior (passes all tests, etc) to the main branch, so it is "ready for review".

There is one main deficiency, which is that it performs two PC::check operations (whereas the original effectively just did one). So slightly worse performance.

Thanks @Pratyush, I figured out what you meant about the trait bounding and it seems to work well. Also thanks @bhgomes, the error handling is better now.

@ghost ghost marked this pull request as ready for review January 6, 2022 11:15
Copy link
Collaborator

@davidnevadoc davidnevadoc left a comment

Choose a reason for hiding this comment

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

What an amazing PR!

Just highlighted a couple of nits but overall I love the improvements introduced. Great job! :)

plonk-core/src/constraint_system/boolean.rs Outdated Show resolved Hide resolved
plonk-core/src/circuit.rs Show resolved Hide resolved
plonk-core/src/constraint_system/arithmetic.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@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.

Some nits and questions.

Aside from that, this is a brilliant work @joebebel.
Congratulations and big thanks for this huge improvement! 🥇

plonk-core/src/error.rs Show resolved Hide resolved
plonk-core/src/proof_system/preprocess.rs Outdated Show resolved Hide resolved
plonk-core/src/proof_system/preprocess.rs Show resolved Hide resolved
plonk-core/src/proof_system/proof.rs Outdated Show resolved Hide resolved
plonk-core/src/proof_system/proof.rs Outdated Show resolved Hide resolved
plonk-core/src/proof_system/proof.rs Outdated Show resolved Hide resolved
plonk-core/src/proof_system/prover.rs Show resolved Hide resolved
plonk-core/src/test.rs Outdated Show resolved Hide resolved
plonk-core/src/transcript.rs Show resolved Hide resolved
plonk-core/src/proof_system/preprocess.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@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.

A couple unused things in proof.rs
Once fixed the CI should pass.

Once it passes, feel free to merge!
Awesome job with this PR @joebebel !!!!!

@ghost
Copy link
Author

ghost commented Jan 11, 2022

OK, ready to merge

@LukePearson1 LukePearson1 requested review from LukePearson1 and removed request for LukePearson1 January 11, 2022 11:32
@CPerezz CPerezz merged commit 632afc7 into ZK-Garage:master Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants