spongefish_anemoi/
lib.rs

1//! Work-in-progress (but working) implementation of the Anemoi hash function.
2//!
3//! The main reason for this code not being deployed is that [anemoi](https://anemoi-hash.github.io/)'s Rust implementation
4//! is not published as a crate and thus `spongefish` cannot publish it along with a new release.
5use ark_ff::{Field, PrimeField};
6use spongefish::duplex_sponge::Permutation;
7use zeroize::Zeroize;
8
9#[derive(Clone, Zeroize)]
10pub struct AnemoiState<F: Field, const R: usize, const N: usize>([F; N]);
11
12impl<F: Field, const N: usize, const R: usize> Default for AnemoiState<F, R, N> {
13    fn default() -> Self {
14        Self([F::zero(); N])
15    }
16}
17
18impl<F: Field, const R: usize, const N: usize> AsRef<[F]> for AnemoiState<F, R, N> {
19    fn as_ref(&self) -> &[F] {
20        &self.0
21    }
22}
23
24impl<F: Field, const R: usize, const N: usize> AsMut<[F]> for AnemoiState<F, R, N> {
25    fn as_mut(&mut self) -> &mut [F] {
26        &mut self.0
27    }
28}
29
30pub type AnemoiBls12_381_2_1 = AnemoiState<anemoi::bls12_381::Felt, 2, 1>;
31use anemoi::{bls12_381::anemoi_2_1::AnemoiBls12_381_2_1 as _AnemoiBls12_381_2_1, Anemoi};
32
33impl Permutation
34    for AnemoiState<
35        anemoi::bls12_381::Felt,
36        { _AnemoiBls12_381_2_1::RATE },
37        { _AnemoiBls12_381_2_1::WIDTH },
38    >
39{
40    type U = anemoi::bls12_381::Felt;
41
42    const N: usize = _AnemoiBls12_381_2_1::WIDTH;
43
44    const R: usize = _AnemoiBls12_381_2_1::RATE;
45
46    fn new(iv: [u8; 32]) -> Self {
47        let mut state = Self::default();
48        state.as_mut()[Self::R] = anemoi::bls12_381::Felt::from_le_bytes_mod_order(&iv);
49        state
50    }
51
52    fn permute(&mut self) {
53        _AnemoiBls12_381_2_1::permutation(&mut self.0);
54    }
55}