2025-01-25 12:35:54 +01:00
|
|
|
|
|
|
|
|
use ark_ff::prelude::{Zero};
|
|
|
|
|
use ark_bn254::Fr as F;
|
|
|
|
|
|
|
|
|
|
use crate::state::*;
|
2025-01-28 14:12:59 +01:00
|
|
|
use crate::{poseidon2, skyscraper};
|
2025-01-25 12:35:54 +01:00
|
|
|
use crate::griffin;
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
pub enum Hash {
|
|
|
|
|
Poseidon2,
|
|
|
|
|
Griffin,
|
2025-02-11 13:54:21 +01:00
|
|
|
// Skyscraper
|
2025-01-25 12:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
pub fn permute(h: Hash, s: State) -> State {
|
|
|
|
|
match h {
|
|
|
|
|
Hash::Poseidon2 => poseidon2::permutation::permute(s),
|
|
|
|
|
Hash::Griffin => griffin::permutation::permute(s),
|
2025-02-11 13:54:21 +01:00
|
|
|
// Hash::Skyscraper => skyscraper::permutation::permute_state(s),
|
2025-01-25 12:35:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn permute_inplace(h: Hash, s: &mut State){
|
|
|
|
|
match h {
|
|
|
|
|
Hash::Poseidon2 => poseidon2::permutation::permute_inplace(s),
|
|
|
|
|
Hash::Griffin => griffin::permutation::permute_inplace(s),
|
2025-02-11 13:54:21 +01:00
|
|
|
// Hash::Skyscraper => skyscraper::permutation::permute_state_inplace(s),
|
2025-01-25 12:35:54 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
pub fn compress(h: Hash, x: F, y: F) -> F {
|
|
|
|
|
let mut u = State { x: x, y: y, z: F::zero() };
|
|
|
|
|
permute_inplace(h, &mut u);
|
|
|
|
|
u.x
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn keyed_compress(h: Hash, key: u64, x: F, y: F) -> F {
|
|
|
|
|
let mut u = State { x: x, y: y, z: F::from(key) };
|
|
|
|
|
permute_inplace(h, &mut u);
|
|
|
|
|
u.x
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|