2024-09-24 13:19:16 +02:00

1.1 KiB

Nim/C implementation of Poseidon2 over the Goldilocks field

Experimental implementation of the Poseidon2 cryptographic hash function, specialized to the Goldilocks field p=2^64-2^32+1 and t=12. Uses a C implementation internally.

Installation

Use the Nimble package manager to add poseidon2-goldilocks to an existing project. Add the following to its .nimble file:

requires "poseidon2-goldilocks >= 0.0.1 & < 0.0.1"

Usage

Hashing bytes into a field element with the sponge construction:

import poseidon2_goldilocks

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 hash digest (4 field elements) into bytes:

let output: array[32, byte] = digest.toBytes

Combining field elements, useful for constructing a binary Merkle tree:

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)