2023-07-28 22:10:14 +02:00
|
|
|
Nim implementation of Poseidon2
|
2023-10-25 10:26:32 +02:00
|
|
|
===============================
|
|
|
|
|
|
|
|
Experimental implementation of the [Poseidon 2][1] cryptographic hash function,
|
|
|
|
specialized to BN254 and t=3. Uses the [constantine][2] library for
|
|
|
|
cryptographic primitives. Neither completeness nor correctness are guaranteed at
|
|
|
|
this moment in time.
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
2023-07-28 22:10:14 +02:00
|
|
|
|
2023-10-25 10:26:32 +02:00
|
|
|
Use the [Nimble][3] package manager to add `poseidon2` to an existing
|
|
|
|
project. Add the following to its .nimble file:
|
2023-07-28 22:10:14 +02:00
|
|
|
|
2023-10-25 10:26:32 +02:00
|
|
|
```nim
|
|
|
|
requires "poseidon2 >= 0.1.0 & < 0.2.0"
|
|
|
|
```
|
2023-07-28 22:10:14 +02:00
|
|
|
|
2023-11-09 14:44:38 +01:00
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
Hashing bytes into a field element with the sponge construction:
|
|
|
|
```nim
|
|
|
|
import poseidon2
|
|
|
|
|
|
|
|
let input = [1'u8, 2'u8, 3'u8] # some bytes that you want to hash
|
|
|
|
let digest: F = Sponge.digest(input) # a field element
|
|
|
|
```
|
|
|
|
|
|
|
|
Converting a field element into bytes:
|
|
|
|
```nim
|
|
|
|
let output: array[32, byte] = digest.toBytes
|
|
|
|
```
|
|
|
|
|
|
|
|
Combining field elements, useful for constructing a binary Merkle tree:
|
|
|
|
```nim
|
|
|
|
let left = Sponge.digest([1'u8, 2'u8, 3'u8])
|
|
|
|
let right = Sponge.digest([4'u8, 5'u8, 6'u8])
|
|
|
|
let combination = compress(left, right)
|
|
|
|
```
|
|
|
|
|
2023-10-25 10:26:32 +02:00
|
|
|
[1]: https://eprint.iacr.org/2023/323.pdf
|
|
|
|
[2]: https://github.com/mratsim/constantine
|
|
|
|
[3]: https://github.com/nim-lang/nimble
|