#poseidon-hash #zero-knowledge-proofs #scalar-field #elliptic-curve #zero-knowledge

no-std dusk-poseidon

Implementation of Poseidon hash algorithm over the Bls12-381 Scalar field

40 releases (22 breaking)

0.40.0 Aug 14, 2024
0.39.0 May 8, 2024
0.38.0 Apr 24, 2024
0.37.0 Mar 27, 2024
0.20.0-pre.1 Mar 31, 2021

#164 in Cryptography

Download history 41/week @ 2024-06-24 14/week @ 2024-07-08 48/week @ 2024-07-15 26/week @ 2024-07-22 340/week @ 2024-07-29 36/week @ 2024-08-05 206/week @ 2024-08-12 149/week @ 2024-08-19 39/week @ 2024-08-26 184/week @ 2024-09-02 302/week @ 2024-09-09 66/week @ 2024-09-16 289/week @ 2024-09-23 239/week @ 2024-09-30 613/week @ 2024-10-07

1,211 downloads per month
Used in 14 crates (12 directly)

MPL-2.0 license

67KB
754 lines

Build Status Repository Documentation

Dusk-Poseidon

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:

  • The 'normal' hashing functionalities operating on BlsScalar.
  • The 'gadget' hashing functionalities that build a circuit which outputs the hash.

Example

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]));

Benchmarks

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.

Licensing

This code is licensed under Mozilla Public License Version 2.0 (MPL-2.0). Please see LICENSE for further info.

About

Implementation designed by the dusk team.

Contributing

  • If you want to contribute to this repository/project please, check CONTRIBUTING.md
  • If you want to report a bug or request a new feature addition, please open an issue on this repository.

Dependencies

~4MB
~91K SLoC