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

feat: Poseidon2 implementation and availabe as Merkle Hash #708

Merged
merged 6 commits into from
Dec 12, 2024

Conversation

alxiong
Copy link
Contributor

@alxiong alxiong commented Dec 11, 2024

closes: #704

When we merge, I'd like to merge with commits (leave intermediate steps in the git history) if possible, as I try to make each commit self-contained in scope.

This PR:

  • Add struct Poseidon2 and its permutation ::permute() and permute_mut()
  • Add trait Poseidon2Params whose generic parameter is much cleaner than plonky3, while sufficient for our purposes
  • Add concrete parameters (2 instances for Bls12-381, 1 instance for Bn254) in poseidon2/constants/ and add test vectors (generated by reference impl by zkhash)
  • Implement HasherAlgorithm for Poseidon2 so that it can be used in Merkle tree as the hash function candidate

Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (main)
  • Linked to GitHub issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • Wrote unit tests
  • Updated relevant documentation in the code
  • Added relevant changelog entries to the CHANGELOG.md of touched crates.
  • Re-reviewed Files changed in the GitHub PR explorer

@alxiong alxiong requested a review from mrain as a code owner December 11, 2024 15:51
@alxiong alxiong changed the title Add Poseidon2 implementation and availabe as Merkle Hash feat: Poseidon2 implementation and availabe as Merkle Hash Dec 11, 2024
@@ -52,6 +54,36 @@ pub type RescueLightWeightMerkleTree<F> = LightWeightMerkleTree<F, RescueHash<F>
/// Example instantiation of a SparseMerkleTree indexed by I
pub type RescueSparseMerkleTree<I, F> = UniversalMerkleTree<F, RescueHash<F>, I, 3, F>;

// TODO: (alex) move this compression to CRHF and wrap with better API?
Copy link
Contributor

Choose a reason for hiding this comment

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

Agree that we should have a CRHF impl (and maybe sponge as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, let's add in a separate PR. This is already getting big

mrain
mrain previously approved these changes Dec 11, 2024
Copy link
Contributor

@mrain mrain left a comment

Choose a reason for hiding this comment

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

Generally LGTM with some comments.

  • Should we also have a native benchmark?
  • Circuit impl could be in a separate PR.

@alxiong
Copy link
Contributor Author

alxiong commented Dec 12, 2024

Benchmark

Poseidon2 over (Bls12_381::Fr, t=2)/1k iter
                        time:   [12.217 ms 12.254 ms 12.324 ms]
                        change: [-0.4638% +0.0421% +0.7025%] (p = 0.69 > 0.05)
                        No change in performance detected.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high severe

Poseidon2 over (Bls12_381::Fr, t=2)/100k iter
                        time:   [1.2291 s 1.2487 s 1.2811 s]
                        change: [-0.1380% +1.5566% +4.0473%] (p = 0.11 > 0.05)
                        No change in performance detected.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high severe

Poseidon2 over (Bls12_381::Fr, t=3)/1k iter
                        time:   [14.579 ms 14.650 ms 14.713 ms]
                        change: [-0.2505% +0.2707% +0.8794%] (p = 0.22 > 0.05)
                        No change in performance detected.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high mild

Poseidon2 over (Bls12_381::Fr, t=3)/100k iter
                        time:   [1.4549 s 1.4633 s 1.4767 s]
Found 2 outliers among 10 measurements (20.00%)
  1 (10.00%) high mild
  1 (10.00%) high severe

Poseidon2 over (Bn254::Fr, t=3)/1k iter
                        time:   [14.828 ms 14.891 ms 15.010 ms]
Found 2 outliers among 10 measurements (20.00%)
  1 (10.00%) high mild
  1 (10.00%) high severe

Poseidon2 over (Bn254::Fr, t=3)/100k iter
                        time:   [1.4809 s 1.4891 s 1.5012 s]
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high severe

Across all 3 instances,
⚠️ <1.5 sec for 100k permutations is expected (without any AVX acceleration)

@alxiong alxiong merged commit e62dca6 into main Dec 12, 2024
8 checks passed
@alxiong alxiong deleted the ax/poseidon2 branch December 12, 2024 14:03
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.

Feature request: poseidon 2 implementation
2 participants