snarkvm_console_algorithms/bhp/hasher/
mod.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
16mod hash_uncompressed;
17
18use crate::Blake2Xs;
19use snarkvm_console_types::prelude::*;
20use snarkvm_utilities::BigInteger;
21
22use std::sync::Arc;
23
24/// The BHP chunk size (this implementation is for a 3-bit BHP).
25pub(super) const BHP_CHUNK_SIZE: usize = 3;
26pub(super) const BHP_LOOKUP_SIZE: usize = 1 << BHP_CHUNK_SIZE;
27
28/// BHP is a collision-resistant hash function that takes a variable-length input.
29/// The BHP hasher is used to process one internal iteration of the BHP hash function.
30#[derive(Clone, Debug, PartialEq)]
31pub struct BHPHasher<E: Environment, const NUM_WINDOWS: u8, const WINDOW_SIZE: u8> {
32    /// The bases for the BHP hash.
33    bases: Arc<Vec<Vec<Group<E>>>>,
34    /// The bases lookup table for the BHP hash.
35    bases_lookup: Arc<Vec<Vec<[Group<E>; BHP_LOOKUP_SIZE]>>>,
36    /// The random base for the BHP commitment.
37    random_base: Arc<Vec<Group<E>>>,
38}
39
40impl<E: Environment, const NUM_WINDOWS: u8, const WINDOW_SIZE: u8> BHPHasher<E, NUM_WINDOWS, WINDOW_SIZE> {
41    /// The maximum number of input bits.
42    const MAX_BITS: usize = NUM_WINDOWS as usize * WINDOW_SIZE as usize * BHP_CHUNK_SIZE;
43    /// The minimum number of input bits (at least one window).
44    const MIN_BITS: usize = WINDOW_SIZE as usize * BHP_CHUNK_SIZE;
45
46    /// Initializes a new instance of BHP with the given domain.
47    pub fn setup(domain: &str) -> Result<Self> {
48        // Calculate the maximum window size.
49        let mut maximum_window_size = 0;
50        let mut range = E::BigInteger::from(2_u64);
51        while range < E::Scalar::modulus_minus_one_div_two() {
52            // range < (p-1)/2
53            range.muln(4); // range * 2^4
54            maximum_window_size += 1;
55        }
56        ensure!(WINDOW_SIZE <= maximum_window_size, "The maximum BHP window size is {maximum_window_size}");
57
58        // Compute the bases.
59        let bases = (0..NUM_WINDOWS)
60            .map(|index| {
61                // Construct an indexed message to attempt to sample a base.
62                let (generator, _, _) = Blake2Xs::hash_to_curve::<E::Affine>(&format!(
63                    "Aleo.BHP.{NUM_WINDOWS}.{WINDOW_SIZE}.{domain}.{index}"
64                ));
65                let mut base = Group::<E>::new(generator);
66                // Compute the generators for the sampled base.
67                let mut powers = Vec::with_capacity(WINDOW_SIZE as usize);
68                for _ in 0..WINDOW_SIZE {
69                    powers.push(base);
70                    for _ in 0..4 {
71                        base = base.double();
72                    }
73                }
74                powers
75            })
76            .collect::<Vec<Vec<Group<E>>>>();
77        ensure!(bases.len() == NUM_WINDOWS as usize, "Incorrect number of BHP windows ({})", bases.len());
78        for window in &bases {
79            ensure!(window.len() == WINDOW_SIZE as usize, "Incorrect BHP window size ({})", window.len());
80        }
81
82        // Compute the bases lookup.
83        let bases_lookup = bases
84            .iter()
85            .map(|x| {
86                x.iter()
87                    .map(|g| {
88                        let mut lookup = [Group::<E>::zero(); BHP_LOOKUP_SIZE];
89                        for (i, element) in lookup.iter_mut().enumerate().take(BHP_LOOKUP_SIZE) {
90                            *element = *g;
91                            if (i & 0x01) != 0 {
92                                *element += g;
93                            }
94                            if (i & 0x02) != 0 {
95                                *element += g.double();
96                            }
97                            if (i & 0x04) != 0 {
98                                *element = element.neg();
99                            }
100                        }
101                        lookup
102                    })
103                    .collect()
104            })
105            .collect::<Vec<Vec<[Group<E>; BHP_LOOKUP_SIZE]>>>();
106        ensure!(bases_lookup.len() == NUM_WINDOWS as usize, "Incorrect number of BHP lookups ({})", bases_lookup.len());
107        for window in &bases_lookup {
108            ensure!(window.len() == WINDOW_SIZE as usize, "Incorrect BHP lookup window size ({})", window.len());
109        }
110
111        // Next, compute the random base.
112        let (generator, _, _) =
113            Blake2Xs::hash_to_curve::<E::Affine>(&format!("Aleo.BHP.{NUM_WINDOWS}.{WINDOW_SIZE}.{domain}.Randomizer"));
114        let mut base_power = Group::<E>::new(generator);
115        let mut random_base = Vec::with_capacity(Scalar::<E>::size_in_bits());
116        for _ in 0..Scalar::<E>::size_in_bits() {
117            random_base.push(base_power);
118            base_power = base_power.double();
119        }
120        ensure!(
121            random_base.len() == Scalar::<E>::size_in_bits(),
122            "Incorrect number of BHP random base powers ({})",
123            random_base.len()
124        );
125
126        Ok(Self { bases: Arc::new(bases), bases_lookup: Arc::new(bases_lookup), random_base: Arc::new(random_base) })
127    }
128
129    /// Returns the bases.
130    pub fn bases(&self) -> &Arc<Vec<Vec<Group<E>>>> {
131        &self.bases
132    }
133
134    /// Returns the random base window.
135    pub fn random_base(&self) -> &Arc<Vec<Group<E>>> {
136        &self.random_base
137    }
138}