Crates.io | dusk-poseidon |
lib.rs | dusk-poseidon |
version | 0.40.0 |
source | src |
created_at | 2021-02-11 16:21:19.954044 |
updated_at | 2024-08-14 19:17:54.852909 |
description | Implementation of Poseidon hash algorithm over the Bls12-381 Scalar field. |
homepage | |
repository | https://github.com/dusk-network/poseidon252 |
max_upload_size | |
id | 353841 |
size | 135,644 |
Reference implementation for the Poseidon Hashing algorithm.
Reference: Starkad and Poseidon: New Hash Functions for Zero Knowledge Proof Systems
This repository has been created so there's a unique library that holds the tools & functions required to perform Poseidon Hashes on field elements of the bls12-381 elliptic curve.
The hash uses the Hades design for its inner permutation and the SAFE framework for contstructing the sponge.
The library provides the two hashing techniques of Poseidon:
BlsScalar
.use rand::rngs::StdRng;
use rand::SeedableRng;
use dusk_poseidon::{Domain, Hash};
use dusk_bls12_381::BlsScalar;
use ff::Field;
// generate random input
let mut rng = StdRng::seed_from_u64(0xbeef);
let mut input = [BlsScalar::zero(); 42];
for scalar in input.iter_mut() {
*scalar = BlsScalar::random(&mut rng);
}
// digest the input all at once
let hash = Hash::digest(Domain::Other, &input);
// update the input gradually
let mut hasher = Hash::new(Domain::Other);
hasher.update(&input[..3]);
hasher.update(&input[3..]);
assert_eq!(hash, hasher.finalize());
// create a hash used for merkle tree hashing with arity = 4
let merkle_hash = Hash::digest(Domain::Merkle4, &input[..4]);
// which is different when another domain is used
assert_ne!(merkle_hash, Hash::digest(Domain::Other, &input[..4]));
There are benchmarks for hashing, encrypting and decrypting in their native form, operating on Scalar
, and for a zero-knowledge circuit proof generation and verification.
To run all benchmarks on your machine, run
cargo bench --features=zk,encryption
in the repository.
This code is licensed under Mozilla Public License Version 2.0 (MPL-2.0). Please see LICENSE for further info.
Implementation designed by the dusk team.