protected function calculateRoot() : string
{
$size = \count($this->nodes);
if ($size < 1) {
return '';
}
$hash = [];
// Population (Use self::MERKLE_LEAF as a prefix)
for ($i = 0; $i < $size; ++$i) {
$hash[$i] = self::MERKLE_LEAF . $this->personalization . $this->nodes[$i]->getHash(true, $this->outputSize, $this->personalization);
}
// Calculation (Use self::MERKLE_BRANCH as a prefix)
do {
$tmp = [];
$j = 0;
for ($i = 0; $i < $size; $i += 2) {
if (empty($hash[$i + 1])) {
$tmp[$j] = $hash[$i];
} else {
$tmp[$j] = Util::raw_hash(self::MERKLE_BRANCH . $this->personalization . $hash[$i] . $hash[$i + 1], $this->outputSize);
}
++$j;
}
$hash = $tmp;
$size >>= 1;
} while ($size > 1);
// We should only have one value left:
$this->rootCalculated = true;
return \array_shift($hash);
}