Skip to content

feat: update Poseidon2 to match upstream P3#905

Open
Nashtare wants to merge 3 commits into0xMiden:nextfrom
Nashtare:robin/update_poseidon2
Open

feat: update Poseidon2 to match upstream P3#905
Nashtare wants to merge 3 commits into0xMiden:nextfrom
Nashtare:robin/update_poseidon2

Conversation

@Nashtare
Copy link

@Nashtare Nashtare commented Mar 19, 2026

Describe your changes

Update Poseidon2 implementation to match upstream Plonky3's for Goldilocks and support SIMD acceleration.
Most of the changes are related to updated hardcoded data (commit a6211e5)

closes #904

Checklist before requesting a review

  • Repo forked and branch created from next according to naming convention.
  • Commit messages and codestyle follow conventions.
  • Relevant issues are linked in the PR description.
  • Tests added for new functionality.
  • Documentation/comments updated according to changes.

@adr1anh adr1anh self-requested a review March 19, 2026 15:02
Copy link
Contributor

@adr1anh adr1anh left a comment

Choose a reason for hiding this comment

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

Thanks for this!

I didn't realize our constants were different, which might affect a lot of our downstream repos. We've already done it with the Rpo transition, but we should check how complicated it would be to do again. Moreover, given the current Poseidon2 discussion, we might want to hold off for a few weeks until to make sure we don't need to switch again.

In the case of benchmarks of the VM, we could try just importing Poseidon2 from p3 directly. This would give us an idea of the performance improvement when SIMD is available.

pub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE;

/// Matrix used for computing the linear layers of internal rounds.
pub const MAT_DIAG: [Felt; STATE_WIDTH] = MAT_DIAG;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should keep these exports for the VM constrains.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we probably do need to re-export the constants and other relevant data as it would be used in the constraint definitions in Miden VM.

mod test;

#[cfg(feature = "std")]
static P3_POSEIDON2: std::sync::LazyLock<p3_goldilocks::Poseidon2Goldilocks<12>> =
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if there's a way we could change the way Plonky's treats its constants to avoid the Vec storage entirely. I wonder what situations need custom constants.

@Nashtare
Copy link
Author

I didn't realize our constants were different, which might affect a lot of our downstream repos. We've already done it with the Rpo transition, but we should check how complicated it would be to do again.

I had to do a couple tweaks on the miden-vm side to test with the official benchmarks but nothing crazy, mostly API related stuff

Moreover, given the current Poseidon2 discussion, we might want to hold off for a few weeks until to make sure we don't need to switch again.

Yeah this definitely makes sense

Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

OFC, this moves quite a bit off the public API of miden-crypto, but I assume that's the point. A bit worried by the no-std adjustments, see inline.

Comment on lines -369 to 373
/// Applies Poseidon2 permutation to the provided state.
///
/// This delegates to the Poseidon2 implementation.
#[inline(always)]
pub fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: is there a reason to delete this comment?

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.

Optimize Poseidon2

4 participants