Skip to content

Commit 65dc230

Browse files
Add initial BHP and Poseidon hash function impls
1 parent 055820a commit 65dc230

File tree

12 files changed

+616
-0
lines changed

12 files changed

+616
-0
lines changed

wasm/src/algorithms/bhp/bhp1024.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

wasm/src/algorithms/bhp/bhp256.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
BHP256Native,
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 BHP256(BHP256Native);
27+
28+
#[wasm_bindgen]
29+
impl BHP256 {
30+
#[wasm_bindgen(constructor)]
31+
pub fn new() -> Self {
32+
Self(BHP256Native::setup("AleoBHP256").expect("Failed to set up BHP256"))
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+
}

wasm/src/algorithms/bhp/bhp512.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
BHP512Native,
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 BHP512(BHP512Native);
27+
28+
#[wasm_bindgen]
29+
impl BHP512 {
30+
#[wasm_bindgen(constructor)]
31+
pub fn new() -> Self {
32+
Self(BHP512Native::setup("AleoBHP512").expect("Failed to set up BHP512"))
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+
}

wasm/src/algorithms/bhp/bhp768.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
BHP768Native,
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 BHP768(BHP768Native);
27+
28+
#[wasm_bindgen]
29+
impl BHP768 {
30+
#[wasm_bindgen(constructor)]
31+
pub fn new() -> Self {
32+
Self(BHP768Native::setup("AleoBHP768").expect("Failed to set up BHP768"))
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+
}

wasm/src/algorithms/bhp/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
pub mod bhp256;
18+
pub mod bhp512;
19+
pub mod bhp768;
20+
pub mod bhp1024;

wasm/src/algorithms/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
pub mod bhp;
18+
pub use bhp::*;
19+
20+
pub mod poseidon;
21+
pub use poseidon::*;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
pub mod poseidon2;
18+
pub use poseidon2::*;
19+
20+
pub mod poseidon4;
21+
pub use poseidon4::*;
22+
23+
pub mod poseidon8;
24+
pub use poseidon8::*;

0 commit comments

Comments
 (0)