/ console / algorithms / src / bhp / hash_uncompressed.rs
hash_uncompressed.rs
  1  // Copyright (c) 2025-2026 ACDC Network
  2  // This file is part of the alphavm library.
  3  //
  4  // Alpha Chain | Delta Chain Protocol
  5  // International Monetary Graphite.
  6  //
  7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
  8  // They built world-class ZK infrastructure. We installed the EASY button.
  9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
 10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
 11  //
 12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
 13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
 14  // No rights reserved. No permission required. No warranty. No refunds.
 15  //
 16  // https://creativecommons.org/publicdomain/zero/1.0/
 17  // SPDX-License-Identifier: CC0-1.0
 18  
 19  use super::*;
 20  
 21  impl<E: Environment, const NUM_WINDOWS: u8, const WINDOW_SIZE: u8> HashUncompressed
 22      for BHP<E, NUM_WINDOWS, WINDOW_SIZE>
 23  {
 24      type Input = bool;
 25      type Output = Group<E>;
 26  
 27      /// Returns the BHP hash of the given input as an affine group element.
 28      ///
 29      /// This uncompressed variant of the BHP hash function is provided to support
 30      /// the BHP commitment scheme, as it is typically not used by applications.
 31      fn hash_uncompressed(&self, input: &[Self::Input]) -> Result<Self::Output> {
 32          // The number of hasher bits to fit.
 33          let num_hasher_bits = NUM_WINDOWS as usize * WINDOW_SIZE as usize * BHP_CHUNK_SIZE;
 34          // The number of data bits in the output.
 35          let num_data_bits = Field::<E>::size_in_data_bits();
 36          // The maximum number of input bits per iteration.
 37          let max_input_bits_per_iteration = num_hasher_bits - num_data_bits;
 38  
 39          debug_assert!(num_data_bits < num_hasher_bits);
 40          debug_assert_eq!(num_data_bits - 64, self.domain.len());
 41  
 42          // Initialize a variable to store the hash from the current iteration.
 43          let mut digest = Group::<E>::zero();
 44  
 45          // Prepare a reusable vector for the preimage.
 46          let mut preimage = Vec::with_capacity(num_hasher_bits);
 47  
 48          // Compute the hash of the input.
 49          for (i, input_bits) in input.chunks(max_input_bits_per_iteration).enumerate() {
 50              // Determine if this is the first iteration.
 51              match i == 0 {
 52                  // Construct the first iteration as: [ 0...0 || DOMAIN || LENGTH(INPUT) || INPUT[0..BLOCK_SIZE] ].
 53                  true => {
 54                      // Initialize a vector for the hash preimage.
 55                      preimage.extend(&self.domain);
 56                      (input.len() as u64).write_bits_le(&mut preimage);
 57                      preimage.extend(input_bits);
 58                  }
 59                  // Construct the subsequent iterations as: [ PREVIOUS_HASH[0..DATA_BITS] || INPUT[I * BLOCK_SIZE..(I + 1) * BLOCK_SIZE] ].
 60                  false => {
 61                      // Initialize a vector for the hash preimage.
 62                      digest.to_x_coordinate().write_bits_le(&mut preimage);
 63                      preimage.truncate(num_data_bits);
 64                      preimage.extend(input_bits);
 65                  }
 66              }
 67              // Hash the preimage for this iteration.
 68              digest = self.hasher.hash_uncompressed(&preimage)?;
 69              preimage.clear();
 70          }
 71  
 72          Ok(digest)
 73      }
 74  }
 75  
 76  #[cfg(test)]
 77  mod tests {
 78      use super::*;
 79      use alphavm_console_types::environment::Console;
 80  
 81      type CurrentEnvironment = Console;
 82  
 83      const ITERATIONS: u64 = 1000;
 84  
 85      #[test]
 86      fn test_bhp256_input_size() -> Result<()> {
 87          let bhp = BHP256::<CurrentEnvironment>::setup("BHPTest")?;
 88  
 89          let mut rng = TestRng::default();
 90  
 91          for i in 0..ITERATIONS {
 92              let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut rng)).collect::<Vec<_>>();
 93              bhp.hash_uncompressed(&input)?;
 94          }
 95          Ok(())
 96      }
 97  
 98      #[test]
 99      fn test_bhp512_input_size() -> Result<()> {
100          let bhp = BHP512::<CurrentEnvironment>::setup("BHPTest")?;
101  
102          let mut rng = TestRng::default();
103  
104          for i in 0..ITERATIONS {
105              let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut rng)).collect::<Vec<_>>();
106              bhp.hash_uncompressed(&input)?;
107          }
108          Ok(())
109      }
110  
111      #[test]
112      fn test_bhp768_input_size() -> Result<()> {
113          let bhp = BHP768::<CurrentEnvironment>::setup("BHPTest")?;
114  
115          let mut rng = TestRng::default();
116  
117          for i in 0..ITERATIONS {
118              let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut rng)).collect::<Vec<_>>();
119              bhp.hash_uncompressed(&input)?;
120          }
121          Ok(())
122      }
123  
124      #[test]
125      fn test_bhp1024_input_size() -> Result<()> {
126          let bhp = BHP1024::<CurrentEnvironment>::setup("BHPTest")?;
127  
128          let mut rng = TestRng::default();
129  
130          for i in 0..ITERATIONS {
131              let input = (0..bhp.window_size() as u64 + i).map(|_| bool::rand(&mut rng)).collect::<Vec<_>>();
132              bhp.hash_uncompressed(&input)?;
133          }
134          Ok(())
135      }
136  }