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

perf(merkle): optimize Poseidon hash using direct hades_permutation #1280

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/merkle_tree/src/hashes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use core::hash::HashStateTrait;
use core::pedersen::PedersenTrait;
use core::poseidon::PoseidonTrait;
use core::poseidon::hades_permutation;
use core::traits::PartialOrd;

/// Computes a commutative hash of a sorted pair of felt252 values.
Expand Down Expand Up @@ -33,14 +34,14 @@ pub impl PedersenCHasher of CommutativeHasher {

/// Computes the Poseidon commutative hash of a sorted pair of felt252 values.
pub impl PoseidonCHasher of CommutativeHasher {
/// Computes the Poseidon hash of the concatenation of two values, sorting the pair first.
/// Computes the Poseidon hash of two values, sorting the pair first.
fn commutative_hash(a: felt252, b: felt252) -> felt252 {
let hash_state = PoseidonTrait::new();
if a < b {
hash_state.update(a).update(b).finalize()
let (result, _, _) = if a < b {
hades_permutation(a, b, 2)
} else {
hash_state.update(b).update(a).finalize()
}
hades_permutation(b, a, 2)
};
result
}
}

Expand Down
Loading