snarkvm_circuit_algorithms/bhp/
commit.rs1use 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 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 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 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 let input = (0..num_input_bits).map(|_| bool::rand(&mut rng)).collect::<Vec<bool>>();
60 let randomizer = Uniform::rand(&mut rng);
62 let expected = native.commit(&input, &randomizer).expect("Failed to commit native input");
64 let circuit_input: Vec<Boolean<_>> = Inject::new(mode, input);
66 let circuit_randomizer: Scalar<_> = Inject::new(mode, randomizer);
68
69 Circuit::scope(format!("BHP {mode} {i}"), || {
70 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}