Coverting MMR size to leaf count is a bit more complicated than the other way around, because unlike leaf count, MMR size doesn't immediately tell us how the tree looks. In case of leaf count, every tree had leaves, but when it comes to MMR size, every tree has nodes. That means that we can't simply look at binary representation of MMR size and tell something about the tree structure.
So to solve this problem we will use the following approach. Find the largest , so that fits in MMR size. Then subtract that number from MMR size and add to leaf count (variable that starts at and accumulates total number of leaves). Repeat until MMR size is .
When it comes to actual implementation, instead of finding the largest as described above, we will just start with and at each step check if it fits in MMR size, then do subtraction and decrement .
Here are implementations of that exact algorithm in some languages
function bit_length(n: number) {
return n.toString(2).length;
}
function mmr_size_to_leaf_count(mmr_size: number) {
let leaf_count = 0;
for (let height = bit_length(mmr_size); height >= 0, height--) {
const tree_leaf_count = 1 << height;
const tree_size = 2 * tree_leaf_count - 1;
if (tree_size <= mmr_size) {
leaf_count += tree_leaf_count;
mmr_size -= tree_size;
}
}
if (mmr_size > 0) {
throw new Error(`Invalid elements count`);
}
return leaf_count;
}
But how can we know that this algorithms is correct? We need to do a proper mathematical proof.
So what we have to proof is that for every valid MMR size, there exists only one set (set of tree heights) so that
You may ask how it could be possible that two sets represent the same MMR size. Indeed that's not possible, but if we take a look at the following example, we'll understand why proper proof is needed.
Let's define another set in a similar way as but with one difference.Now letAnd when we calculate value for those sets we getAs you can see, for defined as above, it's not the case that there exists one set so that the equation holds.
So going back to our proper equation for and MMR size, how do we show that there is only one such set for a given MMR size? We need to proof that if there are two sets that give the same mmr size, then those sets are equal.
We want to proof thatWe'll do a proof by contradiction. Assume that there are two sets and that give the same MMR size, but they are not equal.That means that there is at least one element that is in one set but not in the other. Without loss of generality, let's assume that , also we can assume that is the greatest of such elements. Therefore, we can write the following equationsSince we assumed those sums are equal, we can writeBecause is the greatest element on which and differThen we can cancel first sums on both sides of the equationIntuitively, we see that the greatest power of is on the left hand site, so we want to maximize the sum on the right hand side and minimize the sum on the left hand side. If that results in left hand site still being greater than right, we have a contradiction.Since So we get a contradiction and therefore we proved that for a given MMR size there is only one set so thatBecause of that, if we iterate over that set starting from the greatest element, we will get the correct result.
Notice that this proof wouldn't work in example with , because in that case there would be instead of in the equation and the inequality wouldn't hold.