snarkvm_circuit_algorithms/bhp/
commit.rs

1// Copyright (c) 2019-2025 Provable Inc.
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
16use super::*;
17
18impl<E: Environment, const NUM_WINDOWS: u8, const WINDOW_SIZE: u8> Commit for BHP<E, NUM_WINDOWS, WINDOW_SIZE> {
19    type Input = Boolean<E>;
20    type Output = Field<E>;
21    type Randomizer = Scalar<E>;
22
23    /// Returns the BHP commitment of the given input and randomizer as a field element.
24    fn commit(&self, input: &[Self::Input], randomizer: &Self::Randomizer) -> Self::Output {
25        self.commit_uncompressed(input, randomizer).to_x_coordinate()
26    }
27}
28
29#[cfg(all(test, feature = "console"))]
30mod tests {
31    use super::*;
32    use snarkvm_circuit_types::environment::Circuit;
33    use snarkvm_utilities::{TestRng, Uniform};
34
35    use anyhow::Result;
36
37    const ITERATIONS: u64 = 100;
38    const DOMAIN: &str = "BHPCircuit0";
39
40    fn check_commit<const NUM_WINDOWS: u8, const WINDOW_SIZE: u8>(
41        mode: Mode,
42        num_constants: u64,
43        num_public: u64,
44        num_private: u64,
45        num_constraints: u64,
46    ) -> Result<()> {
47        use console::Commit as C;
48
49        // Initialize BHP.
50        let native = console::BHP::<<Circuit as Environment>::Network, NUM_WINDOWS, WINDOW_SIZE>::setup(DOMAIN)?;
51        let circuit = BHP::<Circuit, NUM_WINDOWS, WINDOW_SIZE>::new(Mode::Constant, native.clone());
52        // Determine the number of inputs.
53        let num_input_bits = NUM_WINDOWS as usize * WINDOW_SIZE as usize * BHP_CHUNK_SIZE;
54
55        let mut rng = TestRng::default();
56
57        for i in 0..ITERATIONS {
58            // Sample a random input.
59            let input = (0..num_input_bits).map(|_| bool::rand(&mut rng)).collect::<Vec<bool>>();
60            // Sample a randomizer.
61            let randomizer = Uniform::rand(&mut rng);
62            // Compute the expected commitment.
63            let expected = native.commit(&input, &randomizer).expect("Failed to commit native input");
64            // Prepare the circuit input.
65            let circuit_input: Vec<Boolean<_>> = Inject::new(mode, input);
66            // Prepare the circuit randomizer.
67            let circuit_randomizer: Scalar<_> = Inject::new(mode, randomizer);
68
69            Circuit::scope(format!("BHP {mode} {i}"), || {
70                // Perform the hash operation.
71                let candidate = circuit.commit(&circuit_input, &circuit_randomizer);
72                assert_scope!(<=num_constants, num_public, num_private, num_constraints);
73                assert_eq!(expected, candidate.eject_value());
74            });
75            Circuit::reset();
76        }
77        Ok(())
78    }
79
80    #[test]
81    fn test_commit_constant() -> Result<()> {
82        check_commit::<32, 48>(Mode::Constant, 8250, 0, 0, 0)
83    }
84
85    #[test]
86    fn test_commit_public() -> Result<()> {
87        check_commit::<32, 48>(Mode::Public, 1044, 0, 10781, 10785)
88    }
89
90    #[test]
91    fn test_commit_private() -> Result<()> {
92        check_commit::<32, 48>(Mode::Private, 1044, 0, 10781, 10785)
93    }
94}