|
| 1 | +/// Investigation of Issue #11: Duplicate leaves behavior |
| 2 | +/// |
| 3 | +/// This test investigates what happens when there are duplicate leaves in the tree. |
| 4 | +/// Questions to answer: |
| 5 | +/// 1. How are the proofs generated? |
| 6 | +/// 2. How is the root generated? |
| 7 | +/// 3. What happens when we try to make a proof for a duplicate leaf? |
| 8 | +
|
| 9 | +use merkletreers::tree::MerkleTree; |
| 10 | +use merkletreers::utils::hash_it; |
| 11 | + |
| 12 | +#[cfg(test)] |
| 13 | +mod tests { |
| 14 | + use super::*; |
| 15 | + |
| 16 | + #[test] |
| 17 | + fn test_duplicate_leaves_root_generation() { |
| 18 | + // Example from issue #11: ["m","e","r","k","l","e","t","r","e","e","r","s"] |
| 19 | + // Note: "e" appears 4 times, "r" appears 3 times |
| 20 | + let data = ["m", "e", "r", "k", "l", "e", "t", "r", "e", "e", "r", "s"]; |
| 21 | + |
| 22 | + let leaves = data |
| 23 | + .iter() |
| 24 | + .map(|d| { |
| 25 | + let mut buffer = [0u8; 32]; |
| 26 | + hash_it(d.as_bytes(), &mut buffer); |
| 27 | + buffer |
| 28 | + }) |
| 29 | + .collect::<Vec<[u8; 32]>>(); |
| 30 | + |
| 31 | + // Create tree with duplicate leaves |
| 32 | + let tree = MerkleTree::new(leaves.clone()); |
| 33 | + |
| 34 | + // Root should be generated successfully |
| 35 | + println!("Root with duplicates: {:?}", tree.root); |
| 36 | + assert_ne!(tree.root, [0u8; 32], "Root should not be zero"); |
| 37 | + |
| 38 | + // Let's also test with a smaller example for clarity |
| 39 | + let simple_duplicates = ["a", "b", "a", "c"]; |
| 40 | + let simple_leaves = simple_duplicates |
| 41 | + .iter() |
| 42 | + .map(|d| { |
| 43 | + let mut buffer = [0u8; 32]; |
| 44 | + hash_it(d.as_bytes(), &mut buffer); |
| 45 | + buffer |
| 46 | + }) |
| 47 | + .collect::<Vec<[u8; 32]>>(); |
| 48 | + |
| 49 | + let simple_tree = MerkleTree::new(simple_leaves); |
| 50 | + println!("Simple root with duplicates: {:?}", simple_tree.root); |
| 51 | + assert_ne!(simple_tree.root, [0u8; 32], "Simple root should not be zero"); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn test_duplicate_leaves_proof_generation() { |
| 56 | + // When we have duplicates, the proof is generated for the FIRST occurrence |
| 57 | + let data = ["a", "b", "a", "c"]; |
| 58 | + |
| 59 | + let leaves = data |
| 60 | + .iter() |
| 61 | + .map(|d| { |
| 62 | + let mut buffer = [0u8; 32]; |
| 63 | + hash_it(d.as_bytes(), &mut buffer); |
| 64 | + buffer |
| 65 | + }) |
| 66 | + .collect::<Vec<[u8; 32]>>(); |
| 67 | + |
| 68 | + let tree = MerkleTree::new(leaves.clone()); |
| 69 | + |
| 70 | + // Hash of "a" |
| 71 | + let mut leaf_a = [0u8; 32]; |
| 72 | + hash_it("a".as_bytes(), &mut leaf_a); |
| 73 | + |
| 74 | + // Make proof for "a" - this will find the FIRST occurrence at index 0 |
| 75 | + let proof = tree.make_proof(leaf_a); |
| 76 | + |
| 77 | + println!("Proof for duplicate 'a': {:?}", proof); |
| 78 | + |
| 79 | + // Save root before check_proof consumes tree |
| 80 | + let expected_root = tree.root; |
| 81 | + |
| 82 | + // Verify the proof |
| 83 | + let computed_root = tree.check_proof(proof, leaf_a); |
| 84 | + println!("Computed root: {:?}", computed_root); |
| 85 | + println!("Expected root: {:?}", expected_root); |
| 86 | + |
| 87 | + // The proof is valid because it proves the FIRST occurrence |
| 88 | + assert_eq!( |
| 89 | + computed_root, expected_root, |
| 90 | + "Proof verification should succeed for first occurrence" |
| 91 | + ); |
| 92 | + |
| 93 | + // Important note: We cannot distinguish between different positions of the same leaf value |
| 94 | + // The proof will always be for the FIRST occurrence found by iter().position() |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_duplicate_leaves_multiple_proofs() { |
| 99 | + // Test what happens when we try to make proofs for all instances of a duplicate |
| 100 | + let data = ["a", "b", "c", "a", "d", "a"]; |
| 101 | + |
| 102 | + let leaves = data |
| 103 | + .iter() |
| 104 | + .map(|d| { |
| 105 | + let mut buffer = [0u8; 32]; |
| 106 | + hash_it(d.as_bytes(), &mut buffer); |
| 107 | + buffer |
| 108 | + }) |
| 109 | + .collect::<Vec<[u8; 32]>>(); |
| 110 | + |
| 111 | + let tree = MerkleTree::new(leaves.clone()); |
| 112 | + |
| 113 | + // Hash of "a" |
| 114 | + let mut leaf_a = [0u8; 32]; |
| 115 | + hash_it("a".as_bytes(), &mut leaf_a); |
| 116 | + |
| 117 | + // Try to make proof for "a" |
| 118 | + // The current implementation will find the FIRST occurrence at index 0 |
| 119 | + let proof = tree.make_proof(leaf_a); |
| 120 | + |
| 121 | + println!("Number of proof nodes: {}", proof.len()); |
| 122 | + println!("Proof for first 'a': {:?}", proof); |
| 123 | + |
| 124 | + // Save root before check_proof consumes tree |
| 125 | + let expected_root = tree.root; |
| 126 | + |
| 127 | + // Verify the proof |
| 128 | + let computed_root = tree.check_proof(proof.clone(), leaf_a); |
| 129 | + println!("Computed root: {:?}", computed_root); |
| 130 | + println!("Expected root: {:?}", expected_root); |
| 131 | + |
| 132 | + assert_eq!( |
| 133 | + computed_root, expected_root, |
| 134 | + "Proof verification should succeed for first occurrence" |
| 135 | + ); |
| 136 | + |
| 137 | + // The issue is: we cannot distinguish between different positions of the same leaf value |
| 138 | + // The proof will always be for the FIRST occurrence |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_unique_leaves_vs_duplicate_leaves() { |
| 143 | + // Compare behavior with unique vs duplicate leaves |
| 144 | + |
| 145 | + // Unique leaves |
| 146 | + let unique_data = ["a", "b", "c", "d"]; |
| 147 | + let unique_leaves = unique_data |
| 148 | + .iter() |
| 149 | + .map(|d| { |
| 150 | + let mut buffer = [0u8; 32]; |
| 151 | + hash_it(d.as_bytes(), &mut buffer); |
| 152 | + buffer |
| 153 | + }) |
| 154 | + .collect::<Vec<[u8; 32]>>(); |
| 155 | + |
| 156 | + let unique_tree = MerkleTree::new(unique_leaves.clone()); |
| 157 | + |
| 158 | + // Duplicate leaves (same data but "a" appears twice) |
| 159 | + let duplicate_data = ["a", "b", "a", "d"]; |
| 160 | + let duplicate_leaves = duplicate_data |
| 161 | + .iter() |
| 162 | + .map(|d| { |
| 163 | + let mut buffer = [0u8; 32]; |
| 164 | + hash_it(d.as_bytes(), &mut buffer); |
| 165 | + buffer |
| 166 | + }) |
| 167 | + .collect::<Vec<[u8; 32]>>(); |
| 168 | + |
| 169 | + let duplicate_tree = MerkleTree::new(duplicate_leaves.clone()); |
| 170 | + |
| 171 | + // Roots should be different because the tree structure is different |
| 172 | + assert_ne!( |
| 173 | + unique_tree.root, duplicate_tree.root, |
| 174 | + "Different leaf arrangements should produce different roots" |
| 175 | + ); |
| 176 | + |
| 177 | + println!("Unique tree root: {:?}", unique_tree.root); |
| 178 | + println!("Duplicate tree root: {:?}", duplicate_tree.root); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_issue_11_exact_example() { |
| 183 | + // Exact example from issue #11 |
| 184 | + let data = ["m", "e", "r", "k", "l", "e", "t", "r", "e", "e", "r", "s"]; |
| 185 | + |
| 186 | + let leaves = data |
| 187 | + .iter() |
| 188 | + .map(|d| { |
| 189 | + let mut buffer = [0u8; 32]; |
| 190 | + hash_it(d.as_bytes(), &mut buffer); |
| 191 | + buffer |
| 192 | + }) |
| 193 | + .collect::<Vec<[u8; 32]>>(); |
| 194 | + |
| 195 | + println!("Number of leaves: {}", leaves.len()); |
| 196 | + |
| 197 | + let tree = MerkleTree::new(leaves.clone()); |
| 198 | + |
| 199 | + println!("Root: {:?}", tree.root); |
| 200 | + let expected_root = tree.root; |
| 201 | + |
| 202 | + // Try to make proof for each unique letter |
| 203 | + let unique_letters = ["m", "e", "r", "k", "l", "t", "s"]; |
| 204 | + |
| 205 | + for letter in unique_letters.iter() { |
| 206 | + let mut leaf = [0u8; 32]; |
| 207 | + hash_it(letter.as_bytes(), &mut leaf); |
| 208 | + |
| 209 | + let tree_for_proof = MerkleTree::new(leaves.clone()); |
| 210 | + let proof = tree_for_proof.make_proof(leaf); |
| 211 | + |
| 212 | + let tree_for_check = MerkleTree::new(leaves.clone()); |
| 213 | + let computed_root = tree_for_check.check_proof(proof.clone(), leaf); |
| 214 | + |
| 215 | + println!( |
| 216 | + "Letter '{}': proof length = {}, verification = {}", |
| 217 | + letter, |
| 218 | + proof.len(), |
| 219 | + computed_root == expected_root |
| 220 | + ); |
| 221 | + |
| 222 | + assert_eq!( |
| 223 | + computed_root, expected_root, |
| 224 | + "Proof verification should succeed for '{}'", |
| 225 | + letter |
| 226 | + ); |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments