From 6fad4b8409c4105a5334b7be6aac1981dc6c0275 Mon Sep 17 00:00:00 2001 From: 0x471 <0x471@protonmail.com> Date: Wed, 1 Jan 2025 18:45:56 +0000 Subject: [PATCH 1/3] perf(merkle): optimize Poseidon hash using direct hades_permutation --- packages/merkle_tree/src/hashes.cairo | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/merkle_tree/src/hashes.cairo b/packages/merkle_tree/src/hashes.cairo index 0ad9921c2..b2834c8bb 100644 --- a/packages/merkle_tree/src/hashes.cairo +++ b/packages/merkle_tree/src/hashes.cairo @@ -5,6 +5,7 @@ use core::hash::HashStateTrait; use core::pedersen::PedersenTrait; use core::poseidon::PoseidonTrait; use core::traits::PartialOrd; +use core::poseidon::hades_permutation; /// Computes a commutative hash of a sorted pair of felt252 values. /// @@ -33,14 +34,15 @@ 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 by directly using hades_permutation with the two values + /// and a length indicator, 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 } } From 730467408f5fae4d3e37e6fde1bb356aa30dd118 Mon Sep 17 00:00:00 2001 From: 0x471 <0x471@protonmail.com> Date: Sat, 4 Jan 2025 14:48:30 +0000 Subject: [PATCH 2/3] docs: clarify sorting behavior in PoseidonCHasher comment --- packages/merkle_tree/src/hashes.cairo | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/merkle_tree/src/hashes.cairo b/packages/merkle_tree/src/hashes.cairo index b2834c8bb..3a681e681 100644 --- a/packages/merkle_tree/src/hashes.cairo +++ b/packages/merkle_tree/src/hashes.cairo @@ -34,8 +34,7 @@ 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 by directly using hades_permutation with the two values - /// and a length indicator, sorting the pair first. + /// Computes the Poseidon hash of two values, sorting the pair first. fn commutative_hash(a: felt252, b: felt252) -> felt252 { let (result, _, _) = if a < b { hades_permutation(a, b, 2) From eaa98c7e872227a953f5541137fdb6e5ad788a72 Mon Sep 17 00:00:00 2001 From: 0x471 <0x471@protonmail.com> Date: Sat, 4 Jan 2025 14:54:34 +0000 Subject: [PATCH 3/3] style: format merkle-tree/hashes.cairo --- packages/merkle_tree/src/hashes.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/merkle_tree/src/hashes.cairo b/packages/merkle_tree/src/hashes.cairo index 3a681e681..31cb11677 100644 --- a/packages/merkle_tree/src/hashes.cairo +++ b/packages/merkle_tree/src/hashes.cairo @@ -4,8 +4,8 @@ use core::hash::HashStateTrait; use core::pedersen::PedersenTrait; use core::poseidon::PoseidonTrait; -use core::traits::PartialOrd; use core::poseidon::hades_permutation; +use core::traits::PartialOrd; /// Computes a commutative hash of a sorted pair of felt252 values. ///