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

Plonky2 proofs verification #4393

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft

Plonky2 proofs verification #4393

wants to merge 7 commits into from

Conversation

ekovalev
Copy link
Member

@ekovalev ekovalev commented Dec 10, 2024

Plonky2 proofs onchain verification.

The change implements a framework for zk-related apps. Specifically, it allows to verify Plonky2 proofs for arbitrary circuits.
A general flow might look like so:

+------------+                          +-------------------------------+
|            |           proof          |     Application Contract      |
|   Prover   | -----------------------> |                               |
|            |                          |    (verifier_circuit_data)    |
+------------+                          +-------------------------------+
                                                        |
                                                        | concatenate( verifier_circuit_data, 
                                                        |              proof )
                                                        v
                                        +-------------------------------+
                                        |       Verifier Contract       |
                                        |                               |
                                        |       plonky2::verify()       |
                                        +-------------------------------+

Application contract is supposed to store circuit-specific data, and prepend with it a proof it wants verified.
The Verifier contract imports the original plonky2 code for verification; however, the underlying primitives (Goldilocks field and Poseidon hasher) are custom-implemented in such a way that heavy operations are offloaded to host (through a dedicated syscall).

Specifically, the change includes:

  • A syscall for Poseidon permutation computation;
  • Host call for efficient Poseidon permutation computation;
  • Example verifier contract with a custom Goldilocks field implementation to plug into the out-of-the-box plonky2 verifier;
  • Crypto primitives on the host (client) side: necessary Goldilocks field arithmetic and Poseidon hashing primitives to back up the Poseidon permutation host call.

Note: To make plonky2 compile for wasm target we need to use our fork with a different feature enabled for the getrandom crate.

Hardware acceleration of field arithmetics and hashing

  • original plonky2 provides specialisations for the field ops and Poseidon hashing over the Goldilocks field. However, currently SIMD parallelisation of Poseidon only works on the aarch64 architecture; it has been disabled for x86_64 after the MDS matrix constants update in plonky2 and haven't been aligned with those changes ever since.
  • field arithmetics on x86_64 allows SIMD with avx2 or avx512 target features enabled. The avx512 has been completely removed in our implementation because it requires unstable language features to compile. As for the avx2 support, it is there but, as mentioned, not used in Poseidon. We keep it in the codebase though in case we want to benefit from polynomials evaluation on the host some day.

That being said, on an x86_u64 machine we won't have any parallelisation yet. Nevertheless, even as is it gives about 4.5-5x speed-up on Poseidon permute operation with respect to fully wasm-based solution.

@ekovalev ekovalev added A1-inprogress Issue is in progress or PR draft is not ready to be reviewed C1-feature Feature request labels Dec 10, 2024
@ekovalev ekovalev requested a review from mertwole December 10, 2024 19:49
Copy link
Member

@mertwole mertwole left a comment

Choose a reason for hiding this comment

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

Looks great, just 2 things:

  • Do bls12_381 changes belong to this PR?
  • I think gr_permute is not very descriptive, maybe gr_poseidon_permute will be better

use plonky2::plonk::verifier::verify;

#[gstd::async_main]
async fn main() {
Copy link
Member

Choose a reason for hiding this comment

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

Have you measured how much gas will it take for a recursive proof verification? It's interesting to know

Copy link
Member Author

Choose a reason for hiding this comment

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

Provided the benchmarks of the syscall/hostcall combo are correct, a recursive proof verification would take slightly above 100 bln gas. Or roughly ~15% of a block gas allowance.

@ekovalev ekovalev force-pushed the ek-plonky2-verification branch 6 times, most recently from d6390a0 to d79aefa Compare December 19, 2024 08:12
@ekovalev ekovalev force-pushed the ek-plonky2-verification branch from d79aefa to 018068e Compare December 26, 2024 13:36
@ekovalev ekovalev force-pushed the ek-plonky2-verification branch from b3dc57b to 0abd6eb Compare December 27, 2024 15:19
@ekovalev ekovalev requested review from gshep and breathx December 29, 2024 23:38
@ekovalev ekovalev added A0-pleasereview PR is ready to be reviewed by the team D2-node Gear Node and removed A1-inprogress Issue is in progress or PR draft is not ready to be reviewed labels Dec 29, 2024
@ekovalev ekovalev marked this pull request as ready for review December 29, 2024 23:39
@ekovalev ekovalev marked this pull request as draft January 15, 2025 19:03
@ekovalev ekovalev added A5-dontmerge PR should not be merged yet and removed A0-pleasereview PR is ready to be reviewed by the team labels Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A5-dontmerge PR should not be merged yet C1-feature Feature request D2-node Gear Node
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants