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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use super::*;
impl<E: Environment, const NUM_BITS: u8> CommitUncompressed for Pedersen<E, NUM_BITS> {
type Input = Boolean<E>;
type Output = Group<E>;
type Randomizer = Scalar<E>;
fn commit_uncompressed(&self, input: &[Self::Input], randomizer: &Self::Randomizer) -> Self::Output {
let hash = self.hash_uncompressed(input);
randomizer
.to_bits_le()
.iter()
.zip_eq(&self.random_base)
.map(|(bit, power)| Group::ternary(bit, power, &Group::zero()))
.fold(hash, |acc, x| acc + x)
}
}
impl<E: Environment, const NUM_BITS: u8>
Metrics<dyn CommitUncompressed<Input = Boolean<E>, Output = Group<E>, Randomizer = Scalar<E>>>
for Pedersen<E, NUM_BITS>
{
type Case = (Vec<Mode>, Vec<Mode>);
fn count(case: &Self::Case) -> Count {
let (input_modes, randomizer_modes) = case;
let uncompressed_count =
count!(Pedersen<E, NUM_BITS>, HashUncompressed<Input = Boolean<E>, Output = Group<E>>, input_modes);
let uncompressed_mode =
output_mode!(Pedersen<E, NUM_BITS>, HashUncompressed<Input = Boolean<E>, Output = Group<E>>, input_modes);
let group_initialize_count = randomizer_modes
.iter()
.map(|mode| {
count!(
Group<E>,
Ternary<Boolean = Boolean<E>, Output = Group<E>>,
&(*mode, Mode::Constant, Mode::Constant)
)
})
.fold(Count::zero(), |cumulative, count| cumulative + count);
let randomizer_to_bits_count =
match Mode::combine(randomizer_modes[0], randomizer_modes.iter().copied()).is_constant() {
true => Count::is(251, 0, 0, 0),
false => Count::is(0, 0, 251, 252),
};
let modes = randomizer_modes.iter().map(|mode| {
match mode.is_constant() {
true => Mode::Constant,
false => Mode::Private,
}
});
let (_, summation_count) =
modes.fold((uncompressed_mode, Count::zero()), |(prev_mode, cumulative), curr_mode| {
let mode = output_mode!(Group<E>, Add<Group<E>, Output = Group<E>>, &(prev_mode, curr_mode));
let sum_count = count!(Group<E>, Add<Group<E>, Output = Group<E>>, &(prev_mode, curr_mode));
(mode, cumulative + sum_count)
});
uncompressed_count + group_initialize_count + randomizer_to_bits_count + summation_count
}
}
impl<E: Environment, const NUM_BITS: u8>
OutputMode<dyn CommitUncompressed<Input = Boolean<E>, Output = Group<E>, Randomizer = Scalar<E>>>
for Pedersen<E, NUM_BITS>
{
type Case = (Vec<Mode>, Vec<Mode>);
fn output_mode(parameters: &Self::Case) -> Mode {
let (input_modes, randomizer_modes) = parameters;
match input_modes.iter().all(|m| *m == Mode::Constant) && randomizer_modes.iter().all(|m| *m == Mode::Constant)
{
true => Mode::Constant,
false => Mode::Private,
}
}
}
#[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_commit_uncompressed<const NUM_BITS: u8>(mode: Mode, rng: &mut TestRng) {
use console::CommitUncompressed as C;
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 randomizer = Uniform::rand(rng);
let expected = native.commit_uncompressed(&input, &randomizer).expect("Failed to commit native input");
let circuit_input: Vec<Boolean<_>> = Inject::new(mode, input);
let circuit_randomizer: Scalar<_> = Inject::new(mode, randomizer);
Circuit::scope(format!("Pedersen {mode} {i}"), || {
let candidate = circuit.commit_uncompressed(&circuit_input, &circuit_randomizer);
assert_eq!(expected, candidate.eject_value());
let input_modes = circuit_input.iter().map(|b| b.eject_mode()).collect::<Vec<_>>();
let randomizer_modes =
circuit_randomizer.to_bits_le().iter().map(|b| b.eject_mode()).collect::<Vec<_>>();
assert_count!(
Pedersen<Circuit, NUM_BITS>,
CommitUncompressed<Input = Boolean<Circuit>, Output = Group<Circuit>, Randomizer = Scalar<Circuit>>,
&(input_modes.clone(), randomizer_modes.clone())
);
assert_output_mode!(
Pedersen<Circuit, NUM_BITS>,
CommitUncompressed<Input = Boolean<Circuit>, Output = Group<Circuit>, Randomizer = Scalar<Circuit>>,
&(input_modes, randomizer_modes),
candidate
);
});
}
}
fn check_homomorphic_addition<C: Display + Eject + Add<Output = C> + ToBits<Boolean = Boolean<Circuit>>>(
pedersen: &impl CommitUncompressed<Input = Boolean<Circuit>, Randomizer = Scalar<Circuit>, Output = Group<Circuit>>,
first: C,
second: C,
rng: &mut TestRng,
) {
println!("Checking homomorphic addition on {} + {}", first, second);
let first_randomizer: Scalar<_> = Inject::new(Mode::Private, Uniform::rand(rng));
let second_randomizer: Scalar<_> = Inject::new(Mode::Private, Uniform::rand(rng));
let a = pedersen.commit_uncompressed(&first.to_bits_le(), &first_randomizer);
let b = pedersen.commit_uncompressed(&second.to_bits_le(), &second_randomizer);
let expected = a + b;
let combined_randomizer = first_randomizer + second_randomizer;
let candidate = pedersen.commit_uncompressed(&(first + second).to_bits_le(), &combined_randomizer);
assert_eq!(expected.eject(), candidate.eject());
assert!(Circuit::is_satisfied());
}
#[test]
fn test_commit_uncompressed_constant() {
let mut rng = TestRng::default();
check_commit_uncompressed::<NUM_BITS_MULTIPLIER>(Mode::Constant, &mut rng);
check_commit_uncompressed::<{ 2 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
check_commit_uncompressed::<{ 3 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
check_commit_uncompressed::<{ 4 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
check_commit_uncompressed::<{ 5 * NUM_BITS_MULTIPLIER }>(Mode::Constant, &mut rng);
}
#[test]
fn test_commit_uncompressed_public() {
let mut rng = TestRng::default();
check_commit_uncompressed::<NUM_BITS_MULTIPLIER>(Mode::Public, &mut rng);
check_commit_uncompressed::<{ 2 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
check_commit_uncompressed::<{ 3 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
check_commit_uncompressed::<{ 4 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
check_commit_uncompressed::<{ 5 * NUM_BITS_MULTIPLIER }>(Mode::Public, &mut rng);
}
#[test]
fn test_commit_uncompressed_private() {
let mut rng = TestRng::default();
check_commit_uncompressed::<NUM_BITS_MULTIPLIER>(Mode::Private, &mut rng);
check_commit_uncompressed::<{ 2 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
check_commit_uncompressed::<{ 3 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
check_commit_uncompressed::<{ 4 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
check_commit_uncompressed::<{ 5 * NUM_BITS_MULTIPLIER }>(Mode::Private, &mut rng);
}
#[test]
fn test_pedersen64_homomorphism_private() {
let pedersen = Pedersen64::constant(console::Pedersen64::setup("Pedersen64HomomorphismTest"));
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let first = U8::<Circuit>::new(Mode::Private, console::U8::new(u8::rand(&mut rng) >> 1));
let second = U8::new(Mode::Private, console::U8::new(u8::rand(&mut rng) >> 1));
check_homomorphic_addition(&pedersen, first, second, &mut rng);
let first = U16::<Circuit>::new(Mode::Private, console::U16::new(u16::rand(&mut rng) >> 1));
let second = U16::new(Mode::Private, console::U16::new(u16::rand(&mut rng) >> 1));
check_homomorphic_addition(&pedersen, first, second, &mut rng);
let first = U32::<Circuit>::new(Mode::Private, console::U32::new(u32::rand(&mut rng) >> 1));
let second = U32::new(Mode::Private, console::U32::new(u32::rand(&mut rng) >> 1));
check_homomorphic_addition(&pedersen, first, second, &mut rng);
let first = U64::<Circuit>::new(Mode::Private, console::U64::new(u64::rand(&mut rng) >> 1));
let second = U64::new(Mode::Private, console::U64::new(u64::rand(&mut rng) >> 1));
check_homomorphic_addition(&pedersen, first, second, &mut rng);
}
}
#[test]
fn test_pedersen_homomorphism_private() {
fn check_pedersen_homomorphism(
pedersen: &impl CommitUncompressed<
Input = Boolean<Circuit>,
Randomizer = Scalar<Circuit>,
Output = Group<Circuit>,
>,
) {
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let first = U8::<Circuit>::new(Mode::Private, console::U8::new(u8::rand(&mut rng) >> 1));
let second = U8::new(Mode::Private, console::U8::new(u8::rand(&mut rng) >> 1));
check_homomorphic_addition(pedersen, first, second, &mut rng);
let first = U16::<Circuit>::new(Mode::Private, console::U16::new(u16::rand(&mut rng) >> 1));
let second = U16::new(Mode::Private, console::U16::new(u16::rand(&mut rng) >> 1));
check_homomorphic_addition(pedersen, first, second, &mut rng);
let first = U32::<Circuit>::new(Mode::Private, console::U32::new(u32::rand(&mut rng) >> 1));
let second = U32::new(Mode::Private, console::U32::new(u32::rand(&mut rng) >> 1));
check_homomorphic_addition(pedersen, first, second, &mut rng);
let first = U64::<Circuit>::new(Mode::Private, console::U64::new(u64::rand(&mut rng) >> 1));
let second = U64::new(Mode::Private, console::U64::new(u64::rand(&mut rng) >> 1));
check_homomorphic_addition(pedersen, first, second, &mut rng);
let first = U128::<Circuit>::new(Mode::Private, console::U128::new(u128::rand(&mut rng) >> 1));
let second = U128::new(Mode::Private, console::U128::new(u128::rand(&mut rng) >> 1));
check_homomorphic_addition(pedersen, first, second, &mut rng);
}
}
let pedersen128 = Pedersen128::constant(console::Pedersen128::setup("Pedersen128HomomorphismTest"));
check_pedersen_homomorphism(&pedersen128);
}
}