1
+ // Copyright (C) 2019-2025 Provable Inc.
2
+ // This file is part of the Aleo SDK library.
3
+
4
+ // The Aleo SDK library is free software: you can redistribute it and/or modify
5
+ // it under the terms of the GNU General Public License as published by
6
+ // the Free Software Foundation, either version 3 of the License, or
7
+ // (at your option) any later version.
8
+
9
+ // The Aleo SDK library is distributed in the hope that it will be useful,
10
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ // GNU General Public License for more details.
13
+
14
+ // You should have received a copy of the GNU General Public License
15
+ // along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+ use crate :: { Field , types:: native:: {
18
+ BHP1024Native ,
19
+ } , Scalar } ;
20
+ use snarkvm_console:: algorithms:: { Commit , Hash } ;
21
+
22
+ use js_sys:: Array ;
23
+ use wasm_bindgen:: prelude:: * ;
24
+
25
+ #[ wasm_bindgen]
26
+ pub struct BHP1024 ( BHP1024Native ) ;
27
+
28
+ #[ wasm_bindgen]
29
+ impl BHP1024 {
30
+ #[ wasm_bindgen( constructor) ]
31
+ pub fn new ( ) -> Self {
32
+ Self ( BHP1024Native :: setup ( "AleoBHP1024" ) . expect ( "Failed to set up BHP1024" ) )
33
+ }
34
+
35
+ /// Hash an array of booleans.
36
+ pub fn hash ( & self , input : Array ) -> Result < Field , String > {
37
+ // Convert an array of booleans to a vector of booleans, failing if any values aren't booleans.
38
+ let input = input. to_vec ( )
39
+ . iter ( )
40
+ . map ( |x| x
41
+ . as_bool ( )
42
+ . ok_or_else ( || "Input must be a boolean array" . to_string ( ) )
43
+ )
44
+ . collect :: < Result < Vec < bool > , String > > ( ) ?;
45
+
46
+
47
+ self . 0 . hash ( & input)
48
+ . map ( |field| Field :: from ( field) )
49
+ . map_err ( |e| e. to_string ( ) )
50
+ }
51
+
52
+ /// Commit to an array of booleans.
53
+ pub fn commit ( & self , input : Array , randomizer : Scalar ) -> Result < Field , String > {
54
+ // Convert an array of booleans to a vector of booleans, failing if any values aren't booleans.
55
+ let input = input. to_vec ( )
56
+ . iter ( )
57
+ . map ( |x| x
58
+ . as_bool ( )
59
+ . ok_or_else ( || "Input must be a boolean array" . to_string ( ) )
60
+ )
61
+ . collect :: < Result < Vec < bool > , String > > ( ) ?;
62
+
63
+ self . 0 . commit ( & input, & randomizer)
64
+ . map ( |field| Field :: from ( field) )
65
+ . map_err ( |e| e. to_string ( ) )
66
+ }
67
+ }
0 commit comments