|
| 1 | +# Poseidon2 Implementation for Zig |
| 2 | + |
| 3 | +This module exposes the KoalaBear Poseidon2 instances that match Plonky3's parameters while reusing the shared `poseidon2/poseidon2.zig` core and the existing field arithmetic in `fields/koalabear/montgomery.zig`. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Plonky3 Compatible**: Re-exports the KoalaBear Poseidon2 instances and round constants used by Plonky3 |
| 8 | +- **Shared Core**: Delegates to the generic `poseidon2` engine that powers the rest of this repository |
| 9 | +- **KoalaBear Field**: Uses the standard Montgomery formulation from `fields/koalabear/montgomery.zig` |
| 10 | +- **Poseidon2-16 and Poseidon2-24**: Provides ready-to-use 16- and 24-width permutations |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +```zig |
| 15 | +const poseidon2 = @import("plonky3_poseidon2/root.zig"); |
| 16 | +
|
| 17 | +// Create Montgomery field elements (KoalaBear) |
| 18 | +var state16: [16]poseidon2.Field.MontFieldElem = undefined; |
| 19 | +// initialise state16... |
| 20 | +poseidon2.poseidon2_16(&state16); |
| 21 | +
|
| 22 | +var state24: [24]poseidon2.Field.MontFieldElem = undefined; |
| 23 | +// initialise state24... |
| 24 | +poseidon2.poseidon2_24(&state24); |
| 25 | +``` |
| 26 | + |
| 27 | +## API Reference |
| 28 | + |
| 29 | +### Field Alias |
| 30 | +- `Field`: Alias of `fields/koalabear/montgomery.zig`'s `MontgomeryField` |
| 31 | + |
| 32 | +### Poseidon2 Types |
| 33 | +- `Poseidon2KoalaBear16Plonky3`: The width-16 Poseidon2 instance (Plonky3 parameters) |
| 34 | +- `Poseidon2KoalaBear24Plonky3`: The width-24 Poseidon2 instance (Plonky3 parameters) |
| 35 | + |
| 36 | +### Convenience Functions |
| 37 | +- `poseidon2_16(state: *[16]Field.MontFieldElem)`: In-place width-16 permutation |
| 38 | +- `poseidon2_24(state: *[24]Field.MontFieldElem)`: In-place width-24 permutation |
| 39 | + |
| 40 | +## Implementation Details |
| 41 | + |
| 42 | +Both instances are generated by feeding the Plonky3 round constants into the shared `poseidon2.Poseidon2` template. This keeps the implementation identical to the other Poseidon variants in this repository while guaranteeing constant parity with Plonky3. |
| 43 | + |
| 44 | +## Testing |
| 45 | + |
| 46 | +The implementation has been tested against Plonky3's Poseidon2 implementation to ensure compatibility. All field operations, round constants, and permutation steps match exactly. |
| 47 | + |
| 48 | +## Files |
| 49 | + |
| 50 | +- `root.zig`: Main module exports and thin wrappers over shared instances |
| 51 | + |
0 commit comments