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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use super::*;
impl<E: Environment, const NUM_BITS: u8> Hash for Pedersen<E, NUM_BITS> {
type Input = Boolean<E>;
type Output = Field<E>;
fn hash(&self, input: &[Self::Input]) -> Self::Output {
self.hash_uncompressed(input).to_x_coordinate()
}
}
impl<E: Environment, const NUM_BITS: u8> Metrics<dyn Hash<Input = Boolean<E>, Output = Field<E>>>
for Pedersen<E, NUM_BITS>
{
type Case = Vec<Mode>;
#[inline]
fn count(case: &Self::Case) -> Count {
count!(Pedersen<E, NUM_BITS>, HashUncompressed<Input = Boolean<E>, Output = Group<E>>, case)
}
}
impl<E: Environment, const NUM_BITS: u8> OutputMode<dyn Hash<Input = Boolean<E>, Output = Field<E>>>
for Pedersen<E, NUM_BITS>
{
type Case = Vec<Mode>;
#[inline]
fn output_mode(parameter: &Self::Case) -> Mode {
output_mode!(Pedersen<E, NUM_BITS>, HashUncompressed<Input = Boolean<E>, Output = Group<E>>, parameter)
}
}
#[cfg(all(test, console))]
mod tests {
use super::*;
use snarkvm_circuit_types::environment::Circuit;
use snarkvm_utilities::{TestRng, Uniform};
const ITERATIONS: u64 = 10;
const MESSAGE: &str = "PedersenCircuit0";
const NUM_BITS_MULTIPLIER: u8 = 8;
fn check_hash<const NUM_BITS: u8>(mode: Mode, rng: &mut TestRng) {
use console::Hash as H;
let native = console::Pedersen::<<Circuit as Environment>::Network, NUM_BITS>::setup(MESSAGE);
let circuit = Pedersen::<Circuit, NUM_BITS>::constant(native.clone());
for i in 0..ITERATIONS {
let input = (0..NUM_BITS).map(|_| bool::rand(rng)).collect::<Vec<bool>>();
let expected = native.hash(&input).expect("Failed to hash native input");
let circuit_input: Vec<Boolean<_>> = Inject::new(mode, input);
Circuit::scope(format!("Pedersen {mode} {i}"), || {
let candidate = circuit.hash(&circuit_input);
assert_eq!(expected, candidate.eject_value());
let modes = circuit_input.iter().map(|b| b.eject_mode()).collect::<Vec<_>>();
assert_count!(
Pedersen<Circuit, NUM_BITS>,
HashUncompressed<Input = Boolean<Circuit>, Output = Group<Circuit>>,
&modes
);
assert_output_mode!(
Pedersen<Circuit, NUM_BITS>,
HashUncompressed<Input = Boolean<Circuit>, Output = Group<Circuit>>,
&modes,
candidate
);
});
}
}
#[test]
fn test_hash_constant() {
let mut rng = TestRng::default();
check_hash::<NUM_BITS_MULTIPLIER>(Mode::Constant, &mut rng);
check_hash::<{ 2 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
check_hash::<{ 3 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
check_hash::<{ 4 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
check_hash::<{ 5 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
}
#[test]
fn test_hash_public() {
let mut rng = TestRng::default();
check_hash::<NUM_BITS_MULTIPLIER>(Mode::Public, &mut rng);
check_hash::<{ 2 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
check_hash::<{ 3 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
check_hash::<{ 4 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
check_hash::<{ 5 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
}
#[test]
fn test_hash_private() {
let mut rng = TestRng::default();
check_hash::<NUM_BITS_MULTIPLIER>(Mode::Private, &mut rng);
check_hash::<{ 2 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
check_hash::<{ 3 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
check_hash::<{ 4 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
check_hash::<{ 5 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
}
}