feat(poseidon2): support BN254 widths t=4,8,12,16; fix matMulExternal indexing#1743
Merged
ivokub merged 1 commit intoConsensys:masterfrom Mar 31, 2026
Merged
Conversation
… matMulExternalInPlace default; updated code generation; addressing: Consensys#1741 (comment)
Contributor
Author
|
@ivokub here is the PR, only the changes, with the |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
ivokub
approved these changes
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds support for BN254 Poseidon2 permutation widths t=4, 8, 12, and 16 in the circuit-side implementation, now that gnark-crypto provides precomputed constants (DiagM1 + round keys) for these widths via gnark-crypto#783.
Previously,
matMulInternalInPlacepanicked for any width beyond 2 or 3:This PR:
DiagM1to the circuitParametersstruct and propagates it from gnark-crypto for BN254DiagM1diagonal, mirroring gnark-crypto'smatMulInternalInPlacematMulExternalInPlacefor thedefault(width = 4k) branch:nil-initialized viamake([]frontend.Variable, 4)instead of zero-valuedinput[4*i+1],input[4*i+2],input[4*i+3]were all incorrectly reading frominput[4*i]Type of change
How has this been tested?
TestPoseidon2_BN254_Widths: table-driven test covering t=4, 8, 12, 16 with valid witnesses (circuit output matches gnark-crypto native permutation) and invalid witnesses (raw input as expected output is correctly rejected), across both Groth16 and PLONK backendsgo test ./std/permutation/poseidon2/How has this been benchmarked?
Checklist:
golangci-lintdoes not output errors locallyNote
Medium Risk
Touches Poseidon2 permutation arithmetic used in circuits and changes matrix multiplication behavior for wider states, which can impact proof correctness if wrong. Coverage is improved with new width-specific and aliasing tests, reducing but not eliminating risk.
Overview
Adds Poseidon2 circuit support for BN254 widths
t=4,8,12,16by extending parameter handling withParameters.DiagM1and implementing the width>=4internal matrix multiplication path (previously panicked).Fixes a correctness bug in
matMulExternalInPlacefor widths4k(bad accumulator init and wrong indexing), bumpsgithub.com/consensys/gnark-cryptotov0.20.1, and adds regression tests:TestPoseidon2_BN254_WidthsplusTestVectorAliasingto ensure vector ops are safe when result aliases inputs.Written by Cursor Bugbot for commit 5d800c7. This will update automatically on new commits. Configure here.