antouhou / rs-merkle

The most advanced Merkle tree library for Rust
MIT License
168 stars 45 forks source link

incorrect tree_depth #32

Open themighty1 opened 1 year ago

themighty1 commented 1 year ago

currently tree depth is incorrectly calculated for power-of-two leaf count. it is +1 than what it should be.

pub fn tree_depth(leaves_count: usize) -> usize {
    8 * core::mem::size_of::<usize>() - leaves_count.leading_zeros() as usize
}

e.g. for leaves_count == 4 this will return 3, whereas it should be 2.

This bug doesn't seem to open any exploitable vectors, it only causes to allocate memory for a non-existent layer. It is still a good idea to fix this.