Skip to content

Commit 05cda97

Browse files
committed
review fixes
1 parent 980c5f0 commit 05cda97

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

networks/monero/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl Block {
126126
transactions.push(self.miner_transaction.hash());
127127
transactions.extend_from_slice(&self.transactions);
128128

129-
blob.extend_from_slice(&merkle_root(&transactions));
129+
blob.extend_from_slice(&merkle_root(transactions));
130130
write_varint(&(1 + self.transactions.len()), &mut blob).unwrap();
131131
blob
132132
}

networks/monero/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use monero_io as io;
77
pub use monero_generators as generators;
88
pub use monero_primitives as primitives;
99

10-
/// Merkel tree functionality.
10+
/// Merkle tree functionality.
1111
pub mod merkle;
1212

1313
/// Ring Signature structs and functionality.

networks/monero/src/merkle.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@ use std_shims::vec::Vec;
22

33
use crate::primitives::keccak256;
44

5-
/// Calculates the merkel root of the given tree. Equivalent to `tree_hash` in monero-core.
6-
pub fn merkle_root(leafs: &[[u8; 32]]) -> [u8; 32] {
5+
/// Calculates the Merkle root of the given tree. Equivalent to `tree_hash` in monero-core:
6+
/// https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a
7+
/// /src/crypto/tree-hash.c#L62
8+
///
9+
/// # Panics
10+
/// This function will panic if the tree is empty.
11+
pub fn merkle_root(mut leafs: Vec<[u8; 32]>) -> [u8; 32] {
712
match leafs.len() {
13+
0 => panic!("Can't compute Merkle root for empty tree"),
814
1 => leafs[0],
915
2 => keccak256([leafs[0], leafs[1]].concat()),
1016
_ => {
11-
let mut hashes = leafs.to_vec();
12-
1317
// Monero preprocess this so the length is a power of 2
1418
let mut high_pow_2 = 4; // 4 is the lowest value this can be
15-
while high_pow_2 < hashes.len() {
19+
while high_pow_2 < leafs.len() {
1620
high_pow_2 *= 2;
1721
}
1822
let low_pow_2 = high_pow_2 / 2;
1923

2024
// Merge right-most hashes until we're at the low_pow_2
2125
{
22-
let overage = hashes.len() - low_pow_2;
23-
let mut rightmost = hashes.drain((low_pow_2 - overage) ..);
26+
let overage = leafs.len() - low_pow_2;
27+
let mut rightmost = leafs.drain((low_pow_2 - overage) ..);
2428
// This is true since we took overage from beneath and above low_pow_2, taking twice as
2529
// many elements as overage
2630
debug_assert_eq!(rightmost.len() % 2, 0);
@@ -32,23 +36,23 @@ pub fn merkle_root(leafs: &[[u8; 32]]) -> [u8; 32] {
3236
}
3337
drop(rightmost);
3438

35-
hashes.extend(paired_hashes);
36-
assert_eq!(hashes.len(), low_pow_2);
39+
leafs.extend(paired_hashes);
40+
assert_eq!(leafs.len(), low_pow_2);
3741
}
3842

3943
// Do a traditional pairing off
40-
let mut new_hashes = Vec::with_capacity(hashes.len() / 2);
41-
while hashes.len() > 1 {
44+
let mut new_hashes = Vec::with_capacity(leafs.len() / 2);
45+
while leafs.len() > 1 {
4246
let mut i = 0;
43-
while i < hashes.len() {
44-
new_hashes.push(keccak256([hashes[i], hashes[i + 1]].concat()));
47+
while i < leafs.len() {
48+
new_hashes.push(keccak256([leafs[i], leafs[i + 1]].concat()));
4549
i += 2;
4650
}
4751

48-
hashes = new_hashes;
49-
new_hashes = Vec::with_capacity(hashes.len() / 2);
52+
leafs = new_hashes;
53+
new_hashes = Vec::with_capacity(leafs.len() / 2);
5054
}
51-
hashes[0]
55+
leafs[0]
5256
}
5357
}
5458
}

0 commit comments

Comments
 (0)