Skip to content

Add comprehensive Poseidon/Poseidon2 support for BN254 and BLS12-381#1663

Merged
jayz22 merged 6 commits intostellar:release/v25-previewfrom
jayz22:more-poseidon
Jan 7, 2026
Merged

Add comprehensive Poseidon/Poseidon2 support for BN254 and BLS12-381#1663
jayz22 merged 6 commits intostellar:release/v25-previewfrom
jayz22:more-poseidon

Conversation

@jayz22
Copy link
Copy Markdown
Contributor

@jayz22 jayz22 commented Jan 3, 2026

Summary

This PR provides comprehensive Poseidon and Poseidon2 hash function implementations for both BN254 and BLS12-381 curves, with extensive parameter coverage and test validation against external reference implementations.

Changes

Convenience Hash Methods

Poseidon Parameters (poseidon_params.rs)

  • BN254: MDS matrix and round constants for t=2, t=3, t=4 (validated against circomlib)
  • BLS12-381: MDS matrix and round constants for t=2, t=3, t=4 (validated against reference Sage script and poseidon-bls12381-circom)

Poseidon2 Parameters (poseidon2_params.rs)

  • BN254: Diagonal matrix (MAT_DIAG) and round constants for t=2, t=3, t=4
  • BLS12-381: Diagonal matrix and round constants for t=2, t=3, t=4
  • Parameters generated using reference Sage script and validated against reference test vectors (generated by the script)

Sponge Implementations

  • PoseidonSponge and Poseidon2Sponge with configurable parameters via PoseidonConfig and Poseidon2Config
  • Proper capacity/rate handling matching reference implementations

Test Coverage

  • Poseidon (BN254) - hash_n validated against circomlib
  • Poseidon (BLS12-381) - hash_n validated against poseidon-bls12381-circom
  • Poseidon2 (BN254) - hash validated against barretenberg, permutation validated against reference test vectors
  • Poseidon2 (BLS12-381) - permutation validated against reference test vectors

  - Add poseidon_hash<N>() matching circom implementation
  - Add poseidon2_hash<N>() matching noir/barretenberg implementation
  - Add Poseidon parameters (MDS, RC) for BN254 t=3, BLS12-381 t=2,3,4
  - Add Poseidon2 parameters (MAT_DIAG, RC) for BN254/BLS12-381 t=2,3,4
  - Add PoseidonConfig/Poseidon2Config for flexible parameter selection
  - Add comprehensive test coverage validated against:
    - circomlib (BN254 Poseidon)
    - poseidon-bls12381-circom (BLS12-381 Poseidon)
    - barretenberg (BN254 Poseidon2)
    - Reference Sage scripts for parameter generation
Copy link
Copy Markdown
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

Reviewing the top-level arrangement of the exported fns and that interface, it looks good, there's just one piece of feedback I mention inline to remove the generics that don't appear to be necessary.

Defer to @sisuresh for a review of the crypto internals.

Comment thread soroban-sdk/src/crypto.rs Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds comprehensive Poseidon and Poseidon2 hash function support for BN254 and BLS12-381 elliptic curves, providing cryptographic primitives validated against multiple external reference implementations (circomlib, barretenberg, noir, and poseidon-bls12381-circom).

Key Changes:

  • Added convenience hash methods poseidon_hash<N>() and poseidon2_hash<N>() with configurable field types (BN254/BLS12-381)
  • Introduced PoseidonConfig and Poseidon2Config structs to encapsulate hash parameters (rate, capacity, rounds, matrices)
  • Changed API signatures from Vec<U256> to fixed-size arrays [U256; N] for improved type safety
  • Added extensive test coverage with validation against circomlib (Poseidon BN254), poseidon-bls12381-circom (Poseidon BLS12-381), and barretenberg (Poseidon2 BN254)

Reviewed changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 7 comments.

File Description
soroban-sdk/src/tests/crypto_poseidon.rs Reorganized tests into Poseidon and Poseidon2 sections; added comprehensive test cases for both BN254 and BLS12-381 with t=2, t=3, t=4 configurations; updated to use array-based API
soroban-sdk/src/crypto/poseidon_sponge.rs Introduced PoseidonConfig struct; refactored sponge to use configuration-based initialization; updated absorb() to accept Vec instead of single elements; added hash() convenience function
soroban-sdk/src/crypto/poseidon2_sponge.rs Introduced Poseidon2Config struct; parallel refactoring to Poseidon sponge with BLS12-381 support; added hash() convenience function with rate=3
soroban-sdk/src/crypto.rs Updated public API signatures to use fixed-size arrays and field_type parameter; added documentation linking to reference implementations; exported new Config structs

Comment thread soroban-sdk/src/crypto/poseidon2_sponge.rs Outdated
Comment thread soroban-sdk/src/crypto/poseidon_sponge.rs Outdated
Comment thread soroban-sdk/src/crypto/poseidon_sponge.rs Outdated
Comment thread soroban-sdk/src/crypto/poseidon_sponge.rs Outdated
Comment thread soroban-sdk/src/crypto.rs Outdated
Comment thread soroban-sdk/src/crypto.rs Outdated
Comment thread soroban-sdk/src/crypto/poseidon_sponge.rs Outdated
Comment thread soroban-sdk/src/crypto/poseidon_params.rs Outdated
Comment thread soroban-sdk/src/crypto/poseidon_sponge.rs
Comment thread soroban-sdk/src/crypto/poseidon_sponge.rs
Comment thread soroban-sdk/src/crypto/poseidon_sponge.rs
@jayz22 jayz22 merged commit 984f63e into stellar:release/v25-preview Jan 7, 2026
90 of 91 checks passed
@jayz22 jayz22 deleted the more-poseidon branch January 7, 2026 23:16
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