Skip to content

Commit b0246be

Browse files
Add remaining available hash functionality
1 parent 69b07f0 commit b0246be

File tree

12 files changed

+258
-45
lines changed

12 files changed

+258
-45
lines changed

wasm/src/algorithms/bhp/bhp1024.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,52 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::{Field, Scalar, from_js_typed_array, types::native::BHP1024Native};
18-
use snarkvm_console::algorithms::{Commit, Hash};
17+
use crate::{Field, Group, Scalar, from_js_typed_array, types::native::BHP1024Native};
18+
use snarkvm_console::algorithms::{Commit, CommitUncompressed, Hash};
1919

2020
use js_sys::Array;
21+
use snarkvm_console::account::HashUncompressed;
2122
use wasm_bindgen::prelude::*;
2223

2324
#[wasm_bindgen]
2425
pub struct BHP1024(BHP1024Native);
2526

2627
#[wasm_bindgen]
2728
impl BHP1024 {
29+
/// Create a BHP hasher with an input size of 1024 bits.
2830
#[wasm_bindgen(constructor)]
2931
pub fn new() -> Self {
3032
Self(BHP1024Native::setup("AleoBHP1024").expect("Failed to set up BHP1024"))
3133
}
3234

33-
/// Hash an array of booleans.
35+
/// Create a BHP hasher with an input size of 1024 bits with a custom domain separator.
36+
pub fn setup(domain_separator: &str) -> Result<Self, String> {
37+
BHP1024Native::setup(domain_separator)
38+
.map(|native| Self(native))
39+
.map_err(|e| format!("Failed to set up BHP1024 with domain separator {}: {}", domain_separator, e))
40+
}
41+
42+
/// Returns the BHP hash with an input hasher of 1024 bits.
3443
pub fn hash(&self, input: Array) -> Result<Field, String> {
3544
let input = from_js_typed_array!(input, as_bool, "boolean")?;
3645
self.0.hash(&input).map(|field| Field::from(field)).map_err(|e| e.to_string())
3746
}
3847

39-
/// Commit to an array of booleans.
48+
/// Returns a BHP hash with an input hasher of 1024 bits.
49+
pub fn hash_to_group(&self, input: Array) -> Result<Group, String> {
50+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
51+
self.0.hash_uncompressed(&input).map(|group| Group::from(group)).map_err(|e| e.to_string())
52+
}
53+
54+
/// Returns a BHP commitment with an input hasher of 1024 bits and randomizer.
4055
pub fn commit(&self, input: Array, randomizer: Scalar) -> Result<Field, String> {
4156
let input = from_js_typed_array!(input, as_bool, "boolean")?;
4257
self.0.commit(&input, &randomizer).map(|field| Field::from(field)).map_err(|e| e.to_string())
4358
}
59+
60+
/// Returns a BHP commitment with an input hasher of 1024 bits and randomizer.
61+
pub fn commit_to_group(&self, input: Array, randomizer: Scalar) -> Result<Group, String> {
62+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
63+
self.0.commit_uncompressed(&input, &randomizer).map(|field| Group::from(field)).map_err(|e| e.to_string())
64+
}
4465
}

wasm/src/algorithms/bhp/bhp256.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::{Field, Scalar, from_js_typed_array, types::native::BHP256Native};
18-
use snarkvm_console::algorithms::{Commit, Hash};
17+
use crate::{Field, Group, Scalar, from_js_typed_array, types::native::BHP256Native};
18+
use snarkvm_console::algorithms::{Commit, CommitUncompressed, Hash, HashUncompressed};
1919

2020
use js_sys::Array;
2121
use wasm_bindgen::prelude::*;
@@ -25,20 +25,40 @@ pub struct BHP256(BHP256Native);
2525

2626
#[wasm_bindgen]
2727
impl BHP256 {
28+
/// Create a BHP hasher with an input size of 256 bits
2829
#[wasm_bindgen(constructor)]
2930
pub fn new() -> Self {
3031
Self(BHP256Native::setup("AleoBHP256").expect("Failed to set up BHP256"))
3132
}
3233

33-
/// Hash an array of booleans.
34+
/// Create a BHP hasher with an input size of 256 bits with a custom domain separator.
35+
pub fn setup(domain_separator: &str) -> Result<Self, String> {
36+
BHP256Native::setup(domain_separator)
37+
.map(|native| Self(native))
38+
.map_err(|e| format!("Failed to set up BHP256 with domain separator {}: {}", domain_separator, e))
39+
}
40+
41+
/// Returns the BHP hash with an input hasher of 256 bits.
3442
pub fn hash(&self, input: Array) -> Result<Field, String> {
3543
let input = from_js_typed_array!(input, as_bool, "boolean")?;
3644
self.0.hash(&input).map(|field| Field::from(field)).map_err(|e| e.to_string())
3745
}
3846

39-
/// Commit to an array of booleans.
47+
/// Returns a BHP hash with an input hasher of 256 bits.
48+
pub fn hash_to_group(&self, input: Array) -> Result<Group, String> {
49+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
50+
self.0.hash_uncompressed(&input).map(|group| Group::from(group)).map_err(|e| e.to_string())
51+
}
52+
53+
/// Returns a BHP commitment with an input hasher of 256 bits and randomizer.
4054
pub fn commit(&self, input: Array, randomizer: Scalar) -> Result<Field, String> {
4155
let input = from_js_typed_array!(input, as_bool, "boolean")?;
4256
self.0.commit(&input, &randomizer).map(|field| Field::from(field)).map_err(|e| e.to_string())
4357
}
58+
59+
/// Returns a BHP commitment with an input hasher of 256 bits and randomizer.
60+
pub fn commit_to_group(&self, input: Array, randomizer: Scalar) -> Result<Group, String> {
61+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
62+
self.0.commit_uncompressed(&input, &randomizer).map(|group| Group::from(group)).map_err(|e| e.to_string())
63+
}
4464
}

wasm/src/algorithms/bhp/bhp512.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,52 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::{Field, Scalar, from_js_typed_array, types::native::BHP512Native};
18-
use snarkvm_console::algorithms::{Commit, Hash};
17+
use crate::{Field, Group, Scalar, from_js_typed_array, types::native::BHP512Native};
18+
use snarkvm_console::algorithms::{Commit, CommitUncompressed, Hash};
1919

2020
use js_sys::Array;
21+
use snarkvm_console::account::HashUncompressed;
2122
use wasm_bindgen::prelude::*;
2223

2324
#[wasm_bindgen]
2425
pub struct BHP512(BHP512Native);
2526

2627
#[wasm_bindgen]
2728
impl BHP512 {
29+
/// Create a BHP hasher with an input size of 512 bits.
2830
#[wasm_bindgen(constructor)]
2931
pub fn new() -> Self {
3032
Self(BHP512Native::setup("AleoBHP512").expect("Failed to set up BHP512"))
3133
}
3234

33-
/// Hash an array of booleans.
35+
/// Create a BHP hasher with an input size of 512 bits with a custom domain separator.
36+
pub fn setup(domain_separator: &str) -> Result<Self, String> {
37+
BHP512Native::setup(domain_separator)
38+
.map(|native| Self(native))
39+
.map_err(|e| format!("Failed to set up BHP512 with domain separator {}: {}", domain_separator, e))
40+
}
41+
42+
/// Returns the BHP hash with an input hasher of 512 bits.
3443
pub fn hash(&self, input: Array) -> Result<Field, String> {
3544
let input = from_js_typed_array!(input, as_bool, "boolean")?;
3645
self.0.hash(&input).map(|field| Field::from(field)).map_err(|e| e.to_string())
3746
}
3847

39-
/// Commit to an array of booleans.
48+
/// Returns a BHP hash with an input hasher of 512 bits.
49+
pub fn hash_to_group(&self, input: Array) -> Result<Group, String> {
50+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
51+
self.0.hash_uncompressed(&input).map(|group| Group::from(group)).map_err(|e| e.to_string())
52+
}
53+
54+
/// Returns a BHP commitment with an input hasher of 512 bits and randomizer.
4055
pub fn commit(&self, input: Array, randomizer: Scalar) -> Result<Field, String> {
4156
let input = from_js_typed_array!(input, as_bool, "boolean")?;
4257
self.0.commit(&input, &randomizer).map(|field| Field::from(field)).map_err(|e| e.to_string())
4358
}
59+
60+
/// Returns a BHP commitment with an input hasher of 512 bits and randomizer.
61+
pub fn commit_to_group(&self, input: Array, randomizer: Scalar) -> Result<Group, String> {
62+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
63+
self.0.commit_uncompressed(&input, &randomizer).map(|group| Group::from(group)).map_err(|e| e.to_string())
64+
}
4465
}

wasm/src/algorithms/bhp/bhp768.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,52 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::{Field, Scalar, from_js_typed_array, types::native::BHP768Native};
18-
use snarkvm_console::algorithms::{Commit, Hash};
17+
use crate::{Field, Group, Scalar, from_js_typed_array, types::native::BHP768Native};
18+
use snarkvm_console::algorithms::{Commit, CommitUncompressed, Hash};
1919

2020
use js_sys::Array;
21+
use snarkvm_console::account::HashUncompressed;
2122
use wasm_bindgen::prelude::*;
2223

2324
#[wasm_bindgen]
2425
pub struct BHP768(BHP768Native);
2526

2627
#[wasm_bindgen]
2728
impl BHP768 {
29+
/// Create a BHP hasher with an input size of 768 bits.
2830
#[wasm_bindgen(constructor)]
2931
pub fn new() -> Self {
3032
Self(BHP768Native::setup("AleoBHP768").expect("Failed to set up BHP768"))
3133
}
3234

33-
/// Hash an array of booleans.
35+
/// Create a BHP hasher with an input size of 768 bits with a custom domain separator.
36+
pub fn setup(domain_separator: &str) -> Result<Self, String> {
37+
BHP768Native::setup(domain_separator)
38+
.map(|native| Self(native))
39+
.map_err(|e| format!("Failed to set up BHP768 with domain separator {}: {}", domain_separator, e))
40+
}
41+
42+
/// Returns the BHP hash with an input hasher of 768 bits.
3443
pub fn hash(&self, input: Array) -> Result<Field, String> {
3544
let input = from_js_typed_array!(input, as_bool, "boolean")?;
3645
self.0.hash(&input).map(|field| Field::from(field)).map_err(|e| e.to_string())
3746
}
3847

39-
/// Commit to an array of booleans.
48+
/// Returns a BHP hash with an input hasher of 768 bits.
49+
pub fn hash_to_group(&self, input: Array) -> Result<Group, String> {
50+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
51+
self.0.hash_uncompressed(&input).map(|group| Group::from(group)).map_err(|e| e.to_string())
52+
}
53+
54+
/// Returns a BHP commitment with an input hasher of 768 bits and randomizer.
4055
pub fn commit(&self, input: Array, randomizer: Scalar) -> Result<Field, String> {
4156
let input = from_js_typed_array!(input, as_bool, "boolean")?;
4257
self.0.commit(&input, &randomizer).map(|field| Field::from(field)).map_err(|e| e.to_string())
4358
}
59+
60+
/// Returns a BHP commitment with an input hasher of 768 bits and randomizer.
61+
pub fn commit_to_group(&self, input: Array, randomizer: Scalar) -> Result<Group, String> {
62+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
63+
self.0.commit_uncompressed(&input, &randomizer).map(|field| Group::from(field)).map_err(|e| e.to_string())
64+
}
4465
}

wasm/src/algorithms/bhp/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ mod tests {
3131
use super::*;
3232
use crate::{
3333
Field,
34+
Group,
3435
Scalar,
3536
test::create_native_field_vector,
3637
types::native::{BHP256Native, BHP512Native, BHP768Native, BHP1024Native},
3738
};
38-
use snarkvm_console::algorithms::{Commit, Hash, ToBits};
39+
use snarkvm_console::algorithms::{Commit, CommitUncompressed, Hash, HashUncompressed, ToBits};
3940

4041
use js_sys::Array;
4142
use wasm_bindgen::JsValue;
@@ -76,30 +77,54 @@ mod tests {
7677
let hash_512 = bhp512.hash(bit_array.clone()).unwrap();
7778
let hash_768 = bhp768.hash(bit_array.clone()).unwrap();
7879
let hash_1024 = bhp1024.hash(bit_array.clone()).unwrap();
80+
let hash_to_group_256 = bhp256.hash_to_group(bit_array.clone()).unwrap();
81+
let hash_to_group_512 = bhp512.hash_to_group(bit_array.clone()).unwrap();
82+
let hash_to_group_768 = bhp768.hash_to_group(bit_array.clone()).unwrap();
83+
let hash_to_group_1024 = bhp1024.hash_to_group(bit_array.clone()).unwrap();
7984
let commit_256 = bhp256.commit(bit_array.clone(), scalar.clone()).unwrap();
8085
let commit_512 = bhp512.commit(bit_array.clone(), scalar.clone()).unwrap();
8186
let commit_768 = bhp768.commit(bit_array.clone(), scalar.clone()).unwrap();
8287
let commit_1024 = bhp1024.commit(bit_array.clone(), scalar.clone()).unwrap();
88+
let commit_to_group_256 = bhp256.commit_to_group(bit_array.clone(), scalar.clone()).unwrap();
89+
let commit_to_group_512 = bhp512.commit_to_group(bit_array.clone(), scalar.clone()).unwrap();
90+
let commit_to_group_768 = bhp768.commit_to_group(bit_array.clone(), scalar.clone()).unwrap();
91+
let commit_to_group_1024 = bhp1024.commit_to_group(bit_array.clone(), scalar.clone()).unwrap();
8392

8493
// Hash and commit to the field element using all native BHP hasher instances.
8594
let native_hash_256 = native_bhp256.hash(&bit_vector).unwrap();
8695
let native_hash_512 = native_bhp512.hash(&bit_vector).unwrap();
8796
let native_hash_768 = native_bhp768.hash(&bit_vector).unwrap();
8897
let native_hash_1024 = native_bhp1024.hash(&bit_vector).unwrap();
98+
let native_hash_to_group_256 = native_bhp256.hash_uncompressed(&bit_vector).unwrap();
99+
let native_hash_to_group_512 = native_bhp512.hash_uncompressed(&bit_vector).unwrap();
100+
let native_hash_to_group_768 = native_bhp768.hash_uncompressed(&bit_vector).unwrap();
101+
let native_hash_to_group_1024 = native_bhp1024.hash_uncompressed(&bit_vector).unwrap();
89102
let native_commit_256 = native_bhp256.commit(&bit_vector, &scalar).unwrap();
90103
let native_commit_512 = native_bhp512.commit(&bit_vector, &scalar).unwrap();
91104
let native_commit_768 = native_bhp768.commit(&bit_vector, &scalar).unwrap();
92105
let native_commit_1024 = native_bhp1024.commit(&bit_vector, &scalar).unwrap();
106+
let native_commit_to_group_256 = native_bhp256.commit_uncompressed(&bit_vector, &scalar).unwrap();
107+
let native_commit_to_group_512 = native_bhp512.commit_uncompressed(&bit_vector, &scalar).unwrap();
108+
let native_commit_to_group_768 = native_bhp768.commit_uncompressed(&bit_vector, &scalar).unwrap();
109+
let native_commit_to_group_1024 = native_bhp1024.commit_uncompressed(&bit_vector, &scalar).unwrap();
93110

94111
// Assert native and exported results are equal.
95112
assert_eq!(hash_256, Field::from(native_hash_256));
96113
assert_eq!(hash_512, Field::from(native_hash_512));
97114
assert_eq!(hash_768, Field::from(native_hash_768));
98115
assert_eq!(hash_1024, Field::from(native_hash_1024));
116+
assert_eq!(hash_to_group_256, Group::from(native_hash_to_group_256));
117+
assert_eq!(hash_to_group_512, Group::from(native_hash_to_group_512));
118+
assert_eq!(hash_to_group_768, Group::from(native_hash_to_group_768));
119+
assert_eq!(hash_to_group_1024, Group::from(native_hash_to_group_1024));
99120
assert_eq!(commit_256, Field::from(native_commit_256));
100121
assert_eq!(commit_512, Field::from(native_commit_512));
101122
assert_eq!(commit_768, Field::from(native_commit_768));
102123
assert_eq!(commit_1024, Field::from(native_commit_1024));
124+
assert_eq!(commit_to_group_256, Group::from(native_commit_to_group_256));
125+
assert_eq!(commit_to_group_512, Group::from(native_commit_to_group_512));
126+
assert_eq!(commit_to_group_768, Group::from(native_commit_to_group_768));
127+
assert_eq!(commit_to_group_1024, Group::from(native_commit_to_group_1024));
103128
}
104129
}
105130
}

wasm/src/algorithms/pedersen/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ mod tests {
2525
use super::*;
2626
use crate::{
2727
Field,
28+
Group,
2829
Scalar,
29-
test::create_native_field_vector,
3030
types::native::{Pedersen64Native, Pedersen128Native},
3131
};
32-
use snarkvm_console::algorithms::{Commit, Hash, ToBits};
32+
use snarkvm_console::algorithms::{Commit, CommitUncompressed, Hash, ToBits};
3333

3434
use crate::types::native::CurrentNetwork;
3535
use js_sys::Array;
@@ -68,18 +68,24 @@ mod tests {
6868
let hash_128 = pedersen128.hash(bit_array.clone()).unwrap();
6969
let commit_64 = pedersen64.commit(bit_array.clone(), scalar.clone()).unwrap();
7070
let commit_128 = pedersen128.commit(bit_array.clone(), scalar.clone()).unwrap();
71+
let commit_to_group_64 = pedersen64.commit_to_group(bit_array.clone(), scalar.clone()).unwrap();
72+
let commit_to_group_128 = pedersen128.commit_to_group(bit_array.clone(), scalar.clone()).unwrap();
7173

7274
// Hash and commit to the field element using all native Pedersen hasher instances.
7375
let native_hash_64 = native_pedersen64.hash(&bit_vector).unwrap();
7476
let native_hash_128 = native_pedersen128.hash(&bit_vector).unwrap();
7577
let native_commit_64 = native_pedersen64.commit(&bit_vector, &scalar).unwrap();
7678
let native_commit_128 = native_pedersen128.commit(&bit_vector, &scalar).unwrap();
79+
let native_commit_to_group_64 = native_pedersen64.commit_uncompressed(&bit_vector, &scalar).unwrap();
80+
let native_commit_to_group_128 = native_pedersen128.commit_uncompressed(&bit_vector, &scalar).unwrap();
7781

7882
// Assert native and exported results are equal.
7983
assert_eq!(hash_64, Field::from(native_hash_64));
8084
assert_eq!(hash_128, Field::from(native_hash_128));
8185
assert_eq!(commit_64, Field::from(native_commit_64));
8286
assert_eq!(commit_128, Field::from(native_commit_128));
87+
assert_eq!(commit_to_group_64, Group::from(native_commit_to_group_64));
88+
assert_eq!(commit_to_group_128, Group::from(native_commit_to_group_128));
8389
}
8490
}
8591
}

wasm/src/algorithms/pedersen/pedersen128.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::{Field, Scalar, from_js_typed_array, types::native::Pedersen128Native};
18-
use snarkvm_console::algorithms::{Commit, Hash};
17+
use crate::{Field, Group, Scalar, from_js_typed_array, types::native::Pedersen128Native};
18+
use snarkvm_console::algorithms::{Commit, CommitUncompressed, Hash};
1919

2020
use js_sys::Array;
2121
use wasm_bindgen::prelude::*;
@@ -25,20 +25,32 @@ pub struct Pedersen128(Pedersen128Native);
2525

2626
#[wasm_bindgen]
2727
impl Pedersen128 {
28+
/// Create a Pedersen64 hasher for a given (up to) 128-bit input.
2829
#[wasm_bindgen(constructor)]
2930
pub fn new() -> Self {
3031
Self(Pedersen128Native::setup("AleoPedersen128"))
3132
}
3233

33-
/// Hash an array of fields.
34+
/// Create a Pedersen64 hasher for a given (up to) 128-bit input with a custom domain separator.
35+
pub fn setup(domain_separator: &str) -> Self {
36+
Self(Pedersen128Native::setup(domain_separator))
37+
}
38+
39+
/// Returns the Pedersen hash for a given (up to) 64-bit input.
3440
pub fn hash(&self, input: Array) -> Result<Field, String> {
3541
let input = from_js_typed_array!(input, as_bool, "boolean")?;
3642
self.0.hash(&input).map(|field| Field::from(field)).map_err(|e| e.to_string())
3743
}
3844

39-
/// Commit to an array of booleans.
45+
/// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
4046
pub fn commit(&self, input: Array, randomizer: Scalar) -> Result<Field, String> {
4147
let input = from_js_typed_array!(input, as_bool, "boolean")?;
4248
self.0.commit(&input, &randomizer).map(|field| Field::from(field)).map_err(|e| e.to_string())
4349
}
50+
51+
/// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
52+
pub fn commit_to_group(&self, input: Array, randomizer: Scalar) -> Result<Group, String> {
53+
let input = from_js_typed_array!(input, as_bool, "boolean")?;
54+
self.0.commit_uncompressed(&input, &randomizer).map(|field| Group::from(field)).map_err(|e| e.to_string())
55+
}
4456
}

0 commit comments

Comments
 (0)