Metadata-Version: 2.5
Name: zeropx-poseidon2-goldilocks
Version: 0.2.0
Summary: Pure-Python Poseidon2 permutation and hash over the Goldilocks field (2^64 - 2^32 + 1). Byte-for-byte compatible with the Plonky2 / qp-poseidon-core reference. Zero runtime dependencies.
Project-URL: Homepage, https://github.com/NixEngine/zeropx-poseidon2-goldilocks
Project-URL: Repository, https://github.com/NixEngine/zeropx-poseidon2-goldilocks
Project-URL: Issues, https://github.com/NixEngine/zeropx-poseidon2-goldilocks/issues
Author-email: "Dr. Vander Geraldo Rodrigues da Cunha Júnior" <vgrcjr@gmail.com>
License: MIT
License-File: LICENSE
Keywords: cryptography,goldilocks,hash,plonky2,poseidon,poseidon2,zero-knowledge
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Security :: Cryptography
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == 'dev'
Description-Content-Type: text/markdown

# zeropx-poseidon2-goldilocks

Pure-Python Poseidon2 permutation and hash over the Goldilocks field
(`p = 2^64 - 2^32 + 1`). **Byte-for-byte compatible** with the
[qp-poseidon-core](https://crates.io/crates/qp-poseidon-core)
reference used by [Plonky2](https://github.com/mir-protocol/plonky2)
and the [Quantus Substrate runtime](https://github.com/Quantus-Network/chain).

**Zero runtime dependencies.** Python 3.10+, standard library only.

## What is this for

- **ZK-circuit debugging.** You have a Rust prover emitting Poseidon2
  hashes and you want to sanity-check a witness or a public input in
  Python without pulling a Rust toolchain.
- **Audit cross-check.** Someone hands you a Poseidon2 digest allegedly
  computed by Plonky2 — you can verify it here without trusting their
  build.
- **Teaching / reproducing.** The Poseidon2 paper spec (eprint 2023/323)
  maps to concrete code in `permutation.py`, next to the round-count
  and matrix definitions from the same paper.
- **Small tools** that need Poseidon2 without a heavyweight FFI stack.

**Not a production prover backend.** Pure Python is 100x-1000x slower
than an optimised Rust/C implementation. Use it to verify someone
else's output, not to prove your own.

## Install

```bash
pip install zeropx-poseidon2-goldilocks
```
*(Python import module: `poseidon2_goldilocks`)*

## Use

```python
from poseidon2_goldilocks import (
    permute, hash_no_pad, hash_bytes,
    GOLDILOCKS_PRIME, SPONGE_WIDTH, POSEIDON2_OUTPUT,
)

# 12-element field state -> 12-element field state
out = permute([0] * SPONGE_WIDTH)

# Variable-length field-element input -> 4-element digest
digest = hash_no_pad([1, 2, 3, 4, 5])

# Arbitrary bytes -> 32-byte digest (8 bytes/felt, little-endian).
# This is the same composition Quantus uses for header preimages.
d32 = hash_bytes(b"hello world")
```

### Binary Merkle tree (v0.2+)

```python
from poseidon2_goldilocks import (
    hash_no_pad, merkle_root, merkle_proof, verify_merkle_proof,
)

# Each leaf is a 4-element digest — hash your data first
leaves = [tuple(hash_no_pad([i])) for i in range(5)]
root   = merkle_root(leaves)

# Produce and verify an inclusion proof for leaf index 2
proof   = merkle_proof(leaves, 2)
assert verify_merkle_proof(root, leaves[2], proof, 2)

# Tamper detection: a proof for index 2 will not verify at index 3
assert not verify_merkle_proof(root, leaves[2], proof, 3)
```

Trees are padded to the next power of two with `EMPTY_DIGEST` (all
zeros). A proof is a list of `(sibling_digest, "L"|"R")` pairs. The
verifier cross-checks each `L`/`R` label against the corresponding
bit of the leaf index, so a proof cannot be replayed at the wrong
position even if it accidentally hashes to the same root.

## Compatibility

This package produces identical output to `qp-poseidon-core v3.1.0`
for its default configuration (width=12, rate=8, capacity=4, S-box
d=7, RF=8, RP=22). The round constants and matrix-diagonal values are
copied verbatim from that reference (originally generated by
`qp-poseidon-constants` from seed `0x3141592653589793`).

Test vectors from the reference implementation's own test suite are
in `tests/test_permutation.py` — the `P3_ZERO_RESULT` and
`P3_SEQ_RESULT` constants are the expected output of
`permute([0]*12)` and `permute([0,1,2,...,11])` respectively.

## Non-canonical input caveat

`hash_bytes` reads its input 8 bytes per field element as
little-endian u64. If any of those chunks decode to a value ≥
GOLDILOCKS_PRIME, it will be silently reduced. This matches the
reference `bytes_to_digest`, but it means the bytes→hash mapping is
not injective at the field boundary. Guards at higher layers (e.g.
`BytesDigest::try_from` in `qp-wormhole-inputs`) reject non-canonical
input before calling this. If you need injectivity, do the same
check yourself.

## Related packages

- **[qp-poseidon-core](https://crates.io/crates/qp-poseidon-core)** —
  the Rust reference this package tracks.
- **[poseidon-hash](https://pypi.org/project/poseidon-hash/)** —
  reference implementation of the *original* Poseidon (not Poseidon2)
  by Ingonyama. Requires `galois` and `numpy`. Different algorithm,
  different constants.
- **[Poseidon Cryptanalysis Initiative](https://www.poseidon-initiative.info/)** —
  Ethereum Foundation bounty program (2024-2026) targeting round-reduced
  Poseidon and Poseidon2 instances, including this exact `Poseidon-64
  over Goldilocks (d=7, t=8)` family.

## Author & Provenance

**Sole Author:** Dr. Vander Geraldo Rodrigues da Cunha Júnior (`vgrcjr@gmail.com`).

*Technical & AI-Assisted Provenance Disclosure:* Auxiliary computational/LLM tooling was used strictly for boilerplate and test-harness scaffolding under 1:1 human architectural design, code review, deterministic reference-vector verification (`qp-poseidon-core` / `Plonky2`), and sole human authorship. Zero runtime third-party or AI dependencies.

## License

MIT.
