Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add BHP, Pedersen, and Poseidon hash function implementations to the Provable SDK. #989

Open
wants to merge 9 commits into
base: mainnet
Choose a base branch
from

Conversation

iamalwaysuncomfortable
Copy link
Collaborator

@iamalwaysuncomfortable iamalwaysuncomfortable commented Mar 7, 2025

Motivation

This PR adds BHP, Pedersen and Poseidon hash function impls to Provable SDK. This proposed functionality enables JS developers to use the Aleo implementations of these hash functions within their dapps.

These hash exports are useful (but not limited to) the following use-cases.

  • Deriving record and ciphertext view keys for private key owners: Some apps such as wallet impls may want to be able to view plaintexts of ciphertexts on chain on behalf of a private key holder in order to execute critical functionality based on that hidden value.
  • Implementing client side cryptography: Apps may want to be able to implement hash-function based cryptography client-side compatible with their private dapps such as creating private randomness, decoding elements that have been encoded within a circuit or finalize block, etc.
  • Research: Exporting hash functions for researchers who desire to experiment with BHP, Pedersen or Poseidon hash functions.

Changelog

  • Implements individual hashing objects BHP256, BHP512, BHP768, BHP1024, Pedersen64, Pedersen128, Poseidon2, Poseidon4, and Poseidon8. The current rationale behind this is to enable different domain separators to ensure dapps implementing these hash functions within offchain protocols in their dapp do not share a global domain (which may lead to vulnerabilities such as witness discovery).
  • Adds a utilities module that provides macros for converting js arrays of protocol objects to vectors of corresponding objects in rust & wasm
  • Implements convenience methods on the Ciphertext object to enable decryption of ciphertexts from transition inputs or outputs by the key ViewKey holder.

Test Plan

  • Adds tests within the wasm SDK to ensure the exported wasm implementations match the native hashing impls.
  • Adds test vectors within the JS SDK to ensure hashing within javascript matches expected outputs

@iamalwaysuncomfortable iamalwaysuncomfortable changed the title [Feature] Add BHP and Poseidon hash function impls to the aleo-wasm package. [Feature] Add BHP, Poseidon, and Pedersen hash function impls to the aleo-wasm package. Mar 11, 2025
@iamalwaysuncomfortable iamalwaysuncomfortable marked this pull request as ready for review March 11, 2025 23:28
@iamalwaysuncomfortable iamalwaysuncomfortable changed the title [Feature] Add BHP, Poseidon, and Pedersen hash function impls to the aleo-wasm package. [Feature] Add BHP, Poseidon, and Pedersen hash function impls to the wasm package. Mar 12, 2025
@iamalwaysuncomfortable iamalwaysuncomfortable changed the title [Feature] Add BHP, Poseidon, and Pedersen hash function impls to the wasm package. [Feature] Add BHP, Poseidon, and Pedersen hash function implementations to the Provable SDK. Mar 12, 2025
@iamalwaysuncomfortable iamalwaysuncomfortable changed the title [Feature] Add BHP, Poseidon, and Pedersen hash function implementations to the Provable SDK. [Feature] Add BHP, Pedersen, and Poseidon hash function implementations to the Provable SDK. Mar 12, 2025
Copy link
Collaborator

@jaketarnow jaketarnow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@Pauan Pauan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor comments

macro_rules! from_wasm_object_array {
($input:expr, $wasm_type:ident) => {{
$input
.to_vec()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can use .iter() instead of .to_vec().into_iter()

macro_rules! from_js_typed_array {
($input:expr, $method:ident, $_type:expr) => {{
$input
.to_vec()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can use .iter() instead of .to_vec().iter()

#[macro_export]
macro_rules! to_bits_array_le {
($self:expr) => {{
let array = js_sys::Array::new();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use .collect::<js_sys::Array> instead of manually pushing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants