1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::errors::CRHError;
use snarkvm_utilities::{FromBytes, ToBytes};
use snarkvm_fields::PrimeField;
use std::{
fmt::{Debug, Display},
hash::Hash,
};
pub trait CRH: Clone + Debug + ToBytes + FromBytes + Send + Sync + From<<Self as CRH>::Parameters> {
type Output: Copy + Clone + Debug + Display + ToBytes + FromBytes + Eq + Hash + Default + Send + Sync;
type Parameters: Clone + Debug + Eq;
fn setup(message: &str) -> Self;
fn hash(&self, input: &[u8]) -> Result<Self::Output, CRHError> {
let bits = input
.iter()
.flat_map(|&byte| (0..8).map(move |i| (byte >> i) & 1u8 == 1u8))
.collect::<Vec<bool>>();
self.hash_bits(&bits)
}
fn hash_bits(&self, input_bits: &[bool]) -> Result<Self::Output, CRHError>;
fn hash_field_elements<F: PrimeField>(&self, input: &[F]) -> Result<Self::Output, CRHError> {
let mut input_bytes = vec![];
for elem in input.iter() {
input_bytes.extend_from_slice(&elem.to_bytes_le()?);
}
self.hash(&input_bytes)
}
fn parameters(&self) -> &Self::Parameters;
}