snarkvm_circuit_algorithms/bhp/
hash.rs

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