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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use super::*;
impl<E: Environment, const NUM_WINDOWS: u8, const WINDOW_SIZE: u8> HashUncompressed
for BHP<E, NUM_WINDOWS, WINDOW_SIZE>
{
type Input = bool;
type Output = Group<E>;
fn hash_uncompressed(&self, input: &[Self::Input]) -> Result<Self::Output> {
let num_hasher_bits = NUM_WINDOWS as usize * WINDOW_SIZE as usize * BHP_CHUNK_SIZE;
let num_data_bits = Field::<E>::size_in_data_bits();
let max_input_bits_per_iteration = num_hasher_bits - num_data_bits;
let mut digest = Group::<E>::zero();
for (i, input_bits) in input.chunks(max_input_bits_per_iteration).enumerate() {
let mut preimage = Vec::with_capacity(num_hasher_bits);
match i == 0 {
true => {
preimage.extend(&self.domain);
preimage.extend((input.len() as u64).to_bits_le());
preimage.extend(input_bits);
}
false => {
preimage.extend(digest.to_x_coordinate().to_bits_le().iter().take(num_data_bits));
preimage.extend(input_bits);
}
}
digest = self.hasher.hash_uncompressed(&preimage)?;
}
Ok(digest)
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_types::environment::Console;
type CurrentEnvironment = Console;
const ITERATIONS: u64 = 1000;
#[test]
fn test_bhp256_input_size() -> Result<()> {
let bhp = BHP256::<CurrentEnvironment>::setup("BHPTest")?;
for i in 0..ITERATIONS {
let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut test_rng())).collect::<Vec<_>>();
bhp.hash_uncompressed(&input)?;
}
Ok(())
}
#[test]
fn test_bhp512_input_size() -> Result<()> {
let bhp = BHP512::<CurrentEnvironment>::setup("BHPTest")?;
for i in 0..ITERATIONS {
let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut test_rng())).collect::<Vec<_>>();
bhp.hash_uncompressed(&input)?;
}
Ok(())
}
#[test]
fn test_bhp768_input_size() -> Result<()> {
let bhp = BHP768::<CurrentEnvironment>::setup("BHPTest")?;
for i in 0..ITERATIONS {
let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut test_rng())).collect::<Vec<_>>();
bhp.hash_uncompressed(&input)?;
}
Ok(())
}
#[test]
fn test_bhp1024_input_size() -> Result<()> {
let bhp = BHP1024::<CurrentEnvironment>::setup("BHPTest")?;
for i in 0..ITERATIONS {
let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut test_rng())).collect::<Vec<_>>();
bhp.hash_uncompressed(&input)?;
}
Ok(())
}
}