|
1 | | -use ethereum_hashing::{ZERO_HASHES, hash, hash32_concat}; |
| 1 | +use ethereum_hashing::{ZERO_HASHES, hash32_concat}; |
2 | 2 | use safe_arith::ArithError; |
3 | 3 | use std::sync::LazyLock; |
4 | 4 |
|
@@ -382,20 +382,19 @@ pub fn verify_merkle_proof( |
382 | 382 | pub fn merkle_root_from_branch(leaf: H256, branch: &[H256], depth: usize, index: usize) -> H256 { |
383 | 383 | assert_eq!(branch.len(), depth, "proof length should equal depth"); |
384 | 384 |
|
385 | | - let mut merkle_root = leaf.as_slice().to_vec(); |
| 385 | + let mut merkle_root = leaf.0; |
386 | 386 |
|
387 | | - for (i, leaf) in branch.iter().enumerate().take(depth) { |
| 387 | + for (i, branch_node) in branch.iter().enumerate().take(depth) { |
388 | 388 | let ith_bit = (index >> i) & 0x01; |
389 | | - if ith_bit == 1 { |
390 | | - merkle_root = hash32_concat(leaf.as_slice(), &merkle_root)[..].to_vec(); |
| 389 | + let (left, right) = if ith_bit == 1 { |
| 390 | + (branch_node.as_slice(), merkle_root.as_slice()) |
391 | 391 | } else { |
392 | | - let mut input = merkle_root; |
393 | | - input.extend_from_slice(leaf.as_slice()); |
394 | | - merkle_root = hash(&input); |
395 | | - } |
| 392 | + (merkle_root.as_slice(), branch_node.as_slice()) |
| 393 | + }; |
| 394 | + merkle_root = hash32_concat(left, right); |
396 | 395 | } |
397 | 396 |
|
398 | | - H256::from_slice(&merkle_root) |
| 397 | + H256::from(merkle_root) |
399 | 398 | } |
400 | 399 |
|
401 | 400 | impl From<ArithError> for MerkleTreeError { |
|
0 commit comments