snarkvm_circuit_algorithms/pedersen/
mod.rs1mod commit;
17mod commit_uncompressed;
18mod hash;
19mod hash_uncompressed;
20
21#[cfg(test)]
22use snarkvm_circuit_types::environment::{assert_count, assert_output_mode, assert_scope};
23
24use crate::{Commit, CommitUncompressed, Hash, HashUncompressed};
25use snarkvm_circuit_types::prelude::*;
26
27pub type Pedersen64<E> = Pedersen<E, 64>;
29pub type Pedersen128<E> = Pedersen<E, 128>;
31
32pub struct Pedersen<E: Environment, const NUM_BITS: u8> {
35 base_window: Vec<Group<E>>,
37 random_base: Vec<Group<E>>,
39}
40
41impl<E: Environment, const NUM_BITS: u8> Inject for Pedersen<E, NUM_BITS> {
42 type Primitive = console::Pedersen<E::Network, NUM_BITS>;
43
44 fn new(_mode: Mode, pedersen: Self::Primitive) -> Self {
46 let base_window = Vec::constant(pedersen.base_window().iter().copied().collect());
48 assert_eq!(base_window.len(), NUM_BITS as usize);
49
50 let random_base = Vec::constant(pedersen.random_base_window().iter().copied().collect());
52 assert_eq!(random_base.len(), E::ScalarField::size_in_bits());
53
54 Self { base_window, random_base }
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use snarkvm_circuit_types::environment::Circuit;
62
63 const ITERATIONS: u64 = 10;
64 const MESSAGE: &str = "PedersenCircuit0";
65 const NUM_BITS_MULTIPLIER: u8 = 8;
66
67 fn check_setup<const NUM_BITS: u8>(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) {
68 for _ in 0..ITERATIONS {
69 let native = console::Pedersen::<<Circuit as Environment>::Network, NUM_BITS>::setup(MESSAGE);
71
72 Circuit::scope("Pedersen::setup", || {
73 let circuit = Pedersen::<Circuit, NUM_BITS>::constant(native.clone());
75 assert_scope!(num_constants, num_public, num_private, num_constraints);
76
77 native.base_window().iter().zip_eq(circuit.base_window.iter()).for_each(|(expected, candidate)| {
79 assert_eq!(*expected, candidate.eject_value());
80 });
81
82 native.random_base_window().iter().zip_eq(circuit.random_base.iter()).for_each(
84 |(expected, candidate)| {
85 assert_eq!(*expected, candidate.eject_value());
86 },
87 );
88 });
89 }
90 }
91
92 #[test]
93 fn test_setup_constant() {
94 check_setup::<NUM_BITS_MULTIPLIER>(2590, 0, 0, 0);
96 check_setup::<{ 2 * NUM_BITS_MULTIPLIER }>(2670, 0, 0, 0);
97 check_setup::<{ 3 * NUM_BITS_MULTIPLIER }>(2750, 0, 0, 0);
98 check_setup::<{ 4 * NUM_BITS_MULTIPLIER }>(2830, 0, 0, 0);
99 check_setup::<{ 5 * NUM_BITS_MULTIPLIER }>(2910, 0, 0, 0);
100 }
101}