Skip to content

Commit

Permalink
fix: sort indices correctly (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: themighty1 <[email protected]>
  • Loading branch information
themighty1 and themighty1 authored Jan 11, 2024
1 parent 6cf1616 commit bc42846
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/merkle_proof.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use crate::{
error::Error,
partial_tree::PartialTree,
prelude::*,
proof_serializers::{DirectHashesOrder, MerkleProofSerializer},
utils, Hasher,
};
Expand Down Expand Up @@ -222,8 +222,10 @@ impl<T: Hasher> MerkleProof<T> {
// Sorting leaves by indexes in case they weren't sorted already
leaf_tuples.sort_by(|(a, _), (b, _)| a.cmp(b));
// Getting back _sorted_ indices
let (sorted_indices, _): (Vec<_>, Vec<_>) = leaf_tuples.iter().cloned().unzip();

let proof_indices_by_layers =
utils::indices::proof_indices_by_layers(leaf_indices, total_leaves_count);
utils::indices::proof_indices_by_layers(&sorted_indices, total_leaves_count);

// The next lines copy hashes from proof hashes and group them by layer index
let mut proof_layers: Vec<Vec<(usize, T::Hash)>> = Vec::with_capacity(tree_depth + 1);
Expand Down
29 changes: 29 additions & 0 deletions tests/merkle_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ pub mod root {
"Should return error not_enough_hashes_to_calculate_root"
);
}

// Expect to calculate the correct root even though the indices are unsorted
#[test]
fn should_sort_indices() {
let test_data = common::setup();
let leaf_hashes = &test_data.leaf_hashes;
let expected_root = test_data.expected_root_hex.clone();
let indices_to_prove = vec![0, 3];

let merkle_tree = MerkleTree::<Sha256>::from_leaves(&test_data.leaf_hashes);
let proof = merkle_tree.proof(&indices_to_prove);

// make indices unsorted
let indices_to_compute_root = vec![3, 0];
let leaves_to_compute_root: Vec<[u8; 32]> = indices_to_compute_root
.iter()
.map(|i| *leaf_hashes.get(*i).unwrap())
.collect();

let extracted_root = proof
.root_hex(
&indices_to_compute_root,
&leaves_to_compute_root,
test_data.leaf_values.len(),
)
.unwrap();

assert_eq!(extracted_root, expected_root);
}
}

pub mod to_bytes {
Expand Down

0 comments on commit bc42846

Please sign in to comment.