Skip to content

Poseidon hash function#1777

Open
OlivierBBB wants to merge 1 commit into
mainfrom
1744-poseidon-hash-function
Open

Poseidon hash function#1777
OlivierBBB wants to merge 1 commit into
mainfrom
1744-poseidon-hash-function

Conversation

@OlivierBBB
Copy link
Copy Markdown
Contributor

@OlivierBBB OlivierBBB commented May 14, 2026

Note

Medium Risk
Compiler and constant-evaluation changes for 𝔽 casts and memory layout affect correctness of generated code; the large Poseidon surface is mostly testdata but increases CI runtime and maintenance burden.

Overview
Adds a Poseidon sponge hash in ZKC (Koalabear, width 16) with parallel u32 and native 𝔽 implementations, static MDS/round constants, and extensive bench/unit/invalid coverage under testdata/zkc/bench/poseidon/.

Strengthens field-element handling: casts and constants are checked against the field modulus in typing and constant evaluation; memory codegen uses native registers for 𝔽 data lines; IncrementalCompiler defaults to KOALABEAR_16; tests gain KOALABEAR_CONFIG plus felt memory/casting and overflow invalid cases.

Renames FlatternFlatten across Corset source maps, ZKC types/codegen/lowering, and IR term simplifiers (mechanical API spelling fix). Small codegen/parser fixes (e.g. unwritable memory panic text, function AST comment).

Reviewed by Cursor Bugbot for commit 24b460a. Bugbot is set up for automated code reviews on this repo. Configure here.

@OlivierBBB OlivierBBB linked an issue May 14, 2026 that may be closed by this pull request
@OlivierBBB OlivierBBB marked this pull request as ready for review May 28, 2026 12:54
Copilot AI review requested due to automatic review settings May 28, 2026 12:54
Comment thread pkg/zkc/compiler/codegen/evaluate.go 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 a Poseidon hash benchmark/test suite for the zkc DSL, including u32-emulated and native felt implementations, and updates compiler support needed for felt-typed memory/static constants. It also performs a repository-wide spelling rename from flattern to flatten.

Changes:

  • Adds Poseidon constants, MDS matrices, result tables, implementations, documentation, and zkc benchmark tests.
  • Adds felt memory unit tests and compiler/codegen changes for felt memory and constants.
  • Renames flattern identifiers/comments to flatten across compiler, IR, and corset packages.

Reviewed changes

Copilot reviewed 95 out of 95 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
pkg/zkc/compiler/codegen/evaluate.go Adds field-aware constant evaluation paths.
pkg/zkc/compiler/codegen/statement.go Allows felt data lines for writable memories.
pkg/zkc/compiler/codegen/flatten.go Renames codegen flatten helper.
pkg/zkc/compiler/codegen/compile.go Updates callers to renamed flatten helper.
pkg/zkc/compiler/incremental_compiler.go Defaults incremental compiler field.
pkg/zkc/compiler/parser/parser.go Fixes error propagation and comment spelling.
pkg/test/zkc_bench_test.go Wires Poseidon benchmark tests.
pkg/test/zkc_unit_test.go Wires felt memory unit tests.
testdata/zkc/unit/*felt_memory* Adds felt memory/static table test cases.
testdata/zkc/bench/poseidon/** Adds Poseidon implementations, constants, docs, and test fixtures.
pkg/corset/**, pkg/ir/**, pkg/zkc/vm/**, pkg/asm/** Applies flatternflatten spelling/identifier cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread testdata/zkc/bench/poseidon/test/felt/utils_05.zkc Outdated
Comment thread pkg/zkc/compiler/codegen/evaluate.go Outdated
Comment on lines +194 to +195
case *expr.Cast[symbol.Resolved]:
return p.Eval(e.Expr, definition)
Comment thread testdata/zkc/bench/poseidon/README.md Outdated
Comment thread testdata/zkc/bench/poseidon/README.md Outdated
Comment thread pkg/zkc/compiler/parser/parser.go Outdated
Comment thread pkg/test/zkc_bench_test.go
Comment thread testdata/zkc/bench/poseidon/impl/u32/_mds_matrix.zkc Outdated
Comment thread testdata/zkc/bench/poseidon/impl/felt/_mds_matrix.zkc Outdated
Comment thread testdata/zkc/bench/poseidon/impl/felt/poseidon.zkc Outdated
@OlivierBBB OlivierBBB force-pushed the 1744-poseidon-hash-function branch from d61d2e9 to f7a9222 Compare May 28, 2026 13:12
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit faba60a. Configure here.

Comment thread pkg/zkc/compiler/codegen/evaluate.go
@DavePearce DavePearce force-pushed the 1744-poseidon-hash-function branch 2 times, most recently from 8261f8b to a11a4fa Compare June 3, 2026 22:34
Copy link
Copy Markdown
Contributor

@DavePearce DavePearce left a comment

Choose a reason for hiding this comment

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

Ok, there are some problems here. We do not want 70 odd files for a single benchmark!! You need to tidy this up and remove all of the various debugging / test harness stuff. I want these files only:

testdata/zkc/benchmark/poseidon_felt.zkc   <-- contains complete benchmark for felt
testdata/zkc/benchmark/poseidon_felt.accepts
testdata/zkc/benchmark/poseidon_u32.zkc.   <-- contains complete benchmark for u32
testdata/zkc/benchmark/poseidon_u32.accepts

The fact that a README.md is needed to explain what is going on tells me that something is seriously off.

Poseidon of

- range(k) with k = 7, 16, 256
- [0]*k, with k = 1, 16, 256

more tests (65k u32 and 65k felt)

added large test (262k felt and u32 Poseidon)

Signed-off-by: DavePearce <dave01001110@gmail.com>
@DavePearce DavePearce force-pushed the 1744-poseidon-hash-function branch from a11a4fa to c35200f Compare June 3, 2026 22:54
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.

Poseidon hash function

3 participants