Skip to content
This repository has been archived by the owner on Aug 5, 2019. It is now read-only.

Commit

Permalink
merkleHash: add zeroed chunks to leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Feb 16, 2019
1 parent 0b125be commit 4ffca55
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,21 @@ function merkleHash (list) {
chunkz = list
}

// Tree-hash
while (chunkz.length > 1) {
if (chunkz.length % 2 === 1) {
chunkz.push(Buffer.alloc(SSZ_CHUNK_SIZE))
const bitLength = (x) => {
let numBits = 0
while (x !== 0) {
x = x >> 1
numBits++
}
return numBits
}
const nextPowerOf2 = (x) => x === 0 ? 1 : Math.pow(2, bitLength(x - 1))
// Add zeroed chunks as leaf nodes to create full binary tree
chunkz = chunkz.concat(
Array.from({ length: nextPowerOf2(chunkz.length) - chunkz.length },
() => Buffer.alloc(SSZ_CHUNK_SIZE)))
// Merkleise
while (chunkz.length > 1) {
const chunkz2 = []
for (let i = 0; i < chunkz.length; i += 2) {
chunkz2.push(hash(Buffer.concat([chunkz[i], chunkz[i + 1]])))
Expand Down

0 comments on commit 4ffca55

Please sign in to comment.