|
14 | 14 | // You should have received a copy of the GNU General Public License
|
15 | 15 | // along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
|
16 | 16 |
|
17 |
| -pub mod bhp1024; |
18 | 17 | pub mod bhp256;
|
| 18 | +pub use bhp256::BHP256; |
| 19 | + |
19 | 20 | pub mod bhp512;
|
| 21 | +pub use bhp512::BHP512; |
| 22 | + |
20 | 23 | pub mod bhp768;
|
| 24 | +pub use bhp768::BHP768; |
| 25 | + |
| 26 | +pub mod bhp1024; |
| 27 | +pub use bhp1024::BHP1024; |
| 28 | + |
| 29 | +#[cfg(test)] |
| 30 | +mod tests { |
| 31 | + use super::*; |
| 32 | + use crate::{ |
| 33 | + Field, |
| 34 | + Scalar, |
| 35 | + test::create_native_field_vector, |
| 36 | + types::native::{BHP256Native, BHP512Native, BHP768Native, BHP1024Native}, |
| 37 | + }; |
| 38 | + use snarkvm_console::algorithms::{Commit, Hash, ToBits}; |
| 39 | + |
| 40 | + use js_sys::Array; |
| 41 | + use wasm_bindgen::JsValue; |
| 42 | + use wasm_bindgen_test::*; |
| 43 | + |
| 44 | + const SCALAR_FIELD_ELEMENT: &str = |
| 45 | + "1774157567936692047646837016039369013254365378639847034769080448564598011047scalar"; |
| 46 | + |
| 47 | + #[wasm_bindgen_test] |
| 48 | + fn test_wasm_bhp_hashes_equal_native_hashes() { |
| 49 | + // Create a field element and a scalar element. |
| 50 | + let scalar = Scalar::from_string(SCALAR_FIELD_ELEMENT).unwrap(); |
| 51 | + |
| 52 | + // Create all exported BHP hasher instances. |
| 53 | + let bhp256 = BHP256::new(); |
| 54 | + let bhp512 = BHP512::new(); |
| 55 | + let bhp768 = BHP768::new(); |
| 56 | + let bhp1024 = BHP1024::new(); |
| 57 | + |
| 58 | + // Create all native BHP hasher instances. |
| 59 | + let native_bhp256 = BHP256Native::setup("AleoBHP256").unwrap(); |
| 60 | + let native_bhp512 = BHP512Native::setup("AleoBHP512").unwrap(); |
| 61 | + let native_bhp768 = BHP768Native::setup("AleoBHP768").unwrap(); |
| 62 | + let native_bhp1024 = BHP1024Native::setup("AleoBHP1024").unwrap(); |
| 63 | + |
| 64 | + for count in [1, 2, 4, 8, 16] { |
| 65 | + // Create a field vector. |
| 66 | + let fields = create_native_field_vector(Some(count)); |
| 67 | + |
| 68 | + // Create both a boolean vector and boolean array. |
| 69 | + let bit_vector = fields.iter() |
| 70 | + .flat_map(|item| item.to_bits_le()) // Flatten all bit representations |
| 71 | + .collect::<Vec<bool>>(); |
| 72 | + let bit_array = bit_vector.iter().map(|item| JsValue::from(*item)).collect::<Array>(); |
| 73 | + |
| 74 | + // Hash and commit to the field element using all BHP hasher instances. |
| 75 | + let hash_256 = bhp256.hash(bit_array.clone()).unwrap(); |
| 76 | + let hash_512 = bhp512.hash(bit_array.clone()).unwrap(); |
| 77 | + let hash_768 = bhp768.hash(bit_array.clone()).unwrap(); |
| 78 | + let hash_1024 = bhp1024.hash(bit_array.clone()).unwrap(); |
| 79 | + let commit_256 = bhp256.commit(bit_array.clone(), scalar.clone()).unwrap(); |
| 80 | + let commit_512 = bhp512.commit(bit_array.clone(), scalar.clone()).unwrap(); |
| 81 | + let commit_768 = bhp768.commit(bit_array.clone(), scalar.clone()).unwrap(); |
| 82 | + let commit_1024 = bhp1024.commit(bit_array.clone(), scalar.clone()).unwrap(); |
| 83 | + |
| 84 | + // Hash and commit to the field element using all native BHP hasher instances. |
| 85 | + let native_hash_256 = native_bhp256.hash(&bit_vector).unwrap(); |
| 86 | + let native_hash_512 = native_bhp512.hash(&bit_vector).unwrap(); |
| 87 | + let native_hash_768 = native_bhp768.hash(&bit_vector).unwrap(); |
| 88 | + let native_hash_1024 = native_bhp1024.hash(&bit_vector).unwrap(); |
| 89 | + let native_commit_256 = native_bhp256.commit(&bit_vector, &scalar).unwrap(); |
| 90 | + let native_commit_512 = native_bhp512.commit(&bit_vector, &scalar).unwrap(); |
| 91 | + let native_commit_768 = native_bhp768.commit(&bit_vector, &scalar).unwrap(); |
| 92 | + let native_commit_1024 = native_bhp1024.commit(&bit_vector, &scalar).unwrap(); |
| 93 | + |
| 94 | + // Assert native and exported results are equal. |
| 95 | + assert_eq!(hash_256, Field::from(native_hash_256)); |
| 96 | + assert_eq!(hash_512, Field::from(native_hash_512)); |
| 97 | + assert_eq!(hash_768, Field::from(native_hash_768)); |
| 98 | + assert_eq!(hash_1024, Field::from(native_hash_1024)); |
| 99 | + assert_eq!(commit_256, Field::from(native_commit_256)); |
| 100 | + assert_eq!(commit_512, Field::from(native_commit_512)); |
| 101 | + assert_eq!(commit_768, Field::from(native_commit_768)); |
| 102 | + assert_eq!(commit_1024, Field::from(native_commit_1024)); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments