snarkvm_circuit_algorithms/pedersen/
mod.rs

1// Copyright 2024-2025 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod commit;
17mod commit_uncompressed;
18mod hash;
19mod hash_uncompressed;
20
21#[cfg(all(test, feature = "console"))]
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
27/// Pedersen64 is an *additively-homomorphic* collision-resistant hash function that takes up to a 64-bit input.
28pub type Pedersen64<E> = Pedersen<E, 64>;
29/// Pedersen128 is an *additively-homomorphic* collision-resistant hash function that takes up to a 128-bit input.
30pub type Pedersen128<E> = Pedersen<E, 128>;
31
32/// Pedersen is a collision-resistant hash function that takes a variable-length input.
33/// The Pedersen hash function does *not* behave like a random oracle, see Poseidon for one.
34pub struct Pedersen<E: Environment, const NUM_BITS: u8> {
35    /// The base window for the Pedersen hash.
36    base_window: Vec<Group<E>>,
37    /// The random base window for the Pedersen commitment.
38    random_base: Vec<Group<E>>,
39}
40
41#[cfg(feature = "console")]
42impl<E: Environment, const NUM_BITS: u8> Inject for Pedersen<E, NUM_BITS> {
43    type Primitive = console::Pedersen<E::Network, NUM_BITS>;
44
45    /// Initializes a new instance of Pedersen with the given Pedersen variant.
46    fn new(_mode: Mode, pedersen: Self::Primitive) -> Self {
47        // Initialize the base window.
48        let base_window = Vec::constant(pedersen.base_window().iter().copied().collect());
49        assert_eq!(base_window.len(), NUM_BITS as usize);
50
51        // Initialize the random base.
52        let random_base = Vec::constant(pedersen.random_base_window().iter().copied().collect());
53        assert_eq!(random_base.len(), E::ScalarField::size_in_bits());
54
55        Self { base_window, random_base }
56    }
57}
58
59#[cfg(all(test, feature = "console"))]
60mod tests {
61    use super::*;
62    use snarkvm_circuit_types::environment::Circuit;
63
64    const ITERATIONS: u64 = 10;
65    const MESSAGE: &str = "PedersenCircuit0";
66    const NUM_BITS_MULTIPLIER: u8 = 8;
67
68    fn check_setup<const NUM_BITS: u8>(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) {
69        for _ in 0..ITERATIONS {
70            // Initialize the native Pedersen hash.
71            let native = console::Pedersen::<<Circuit as Environment>::Network, NUM_BITS>::setup(MESSAGE);
72
73            Circuit::scope("Pedersen::setup", || {
74                // Perform the setup operation.
75                let circuit = Pedersen::<Circuit, NUM_BITS>::constant(native.clone());
76                assert_scope!(num_constants, num_public, num_private, num_constraints);
77
78                // Check for equivalency of the bases.
79                native.base_window().iter().zip_eq(circuit.base_window.iter()).for_each(|(expected, candidate)| {
80                    assert_eq!(*expected, candidate.eject_value());
81                });
82
83                // Check for equality of the random base.
84                native.random_base_window().iter().zip_eq(circuit.random_base.iter()).for_each(
85                    |(expected, candidate)| {
86                        assert_eq!(*expected, candidate.eject_value());
87                    },
88                );
89            });
90        }
91    }
92
93    #[test]
94    fn test_setup_constant() {
95        // Set the number of windows, and modulate the window size.
96        check_setup::<NUM_BITS_MULTIPLIER>(2590, 0, 0, 0);
97        check_setup::<{ 2 * NUM_BITS_MULTIPLIER }>(2670, 0, 0, 0);
98        check_setup::<{ 3 * NUM_BITS_MULTIPLIER }>(2750, 0, 0, 0);
99        check_setup::<{ 4 * NUM_BITS_MULTIPLIER }>(2830, 0, 0, 0);
100        check_setup::<{ 5 * NUM_BITS_MULTIPLIER }>(2910, 0, 0, 0);
101    }
102}