From f9d3e5f5e7ed7b229e5e01890eb44868066ee6cf Mon Sep 17 00:00:00 2001 From: timofey Date: Mon, 11 Mar 2024 14:39:36 +0100 Subject: [PATCH] Fix log2 precision loss (#75) --- lightclient-circuits/src/ssz_merkle.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lightclient-circuits/src/ssz_merkle.rs b/lightclient-circuits/src/ssz_merkle.rs index 026a51d..5d272dc 100644 --- a/lightclient-circuits/src/ssz_merkle.rs +++ b/lightclient-circuits/src/ssz_merkle.rs @@ -37,8 +37,12 @@ pub fn ssz_merkleize_chunks>( MAX_INPUT_LEAFS_NOT_POW2 ); - let len_even = chunks.len() + chunks.len() % 2; - let height = (len_even as f64).log2().ceil() as usize; + let height = if chunks.len() == 1 { + 1 + } else { + chunks.len().next_power_of_two().ilog2() as usize + }; + for depth in 0..height { // Pad to even length using 32 zero bytes assigned as constants. let len_even = chunks.len() + chunks.len() % 2;