Skip to content

Commit

Permalink
lib/{merkle,mrkl}: rename createBranch to createPath. improve mrkl.de…
Browse files Browse the repository at this point in the history
…riveRoot.
  • Loading branch information
chjj committed Nov 30, 2023
1 parent b1c4d94 commit 579318c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
28 changes: 16 additions & 12 deletions lib/merkle.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ function createRoot(alg, leaves) {
}

/**
* Collect a merkle branch from vector index.
* Collect a merkle path from leaf index.
* @param {Object} alg
* @param {Number} index
* @param {Buffer[]} leaves
* @returns {Buffer[]} branch
* @returns {Buffer[]} path
*/

function createBranch(alg, index, leaves) {
function createPath(alg, index, leaves) {
assert(alg && typeof alg.root === 'function');
assert((index >>> 0) === index);
assert(Array.isArray(leaves));
Expand All @@ -171,14 +171,14 @@ function createBranch(alg, index, leaves) {
let size = leaves.length;

const [nodes] = createTree(alg, leaves);
const branch = [];
const path = [];

let i = 0;

while (size > 1) {
const j = Math.min(index ^ 1, size - 1);

branch.push(nodes[i + j]);
path.push(nodes[i + j]);

index >>>= 1;

Expand All @@ -187,27 +187,27 @@ function createBranch(alg, index, leaves) {
size = (size + 1) >>> 1;
}

return branch;
return path;
}

/**
* Derive merkle root from branch.
* Derive merkle root from path.
* @param {Object} alg
* @param {Buffer} hash
* @param {Buffer[]} branch
* @param {Buffer[]} path
* @param {Number} index
* @returns {Buffer} root
*/

function deriveRoot(alg, hash, branch, index) {
function deriveRoot(alg, hash, path, index) {
assert(alg && typeof alg.root === 'function');
assert(Buffer.isBuffer(hash));
assert(Array.isArray(branch));
assert(Array.isArray(path));
assert((index >>> 0) === index);

let root = hash;

for (const hash of branch) {
for (const hash of path) {
if ((index & 1) && hash.equals(root))
return alg.zero;

Expand All @@ -219,6 +219,9 @@ function deriveRoot(alg, hash, branch, index) {
index >>>= 1;
}

if (index !== 0)
return alg.zero;

return root;
}

Expand All @@ -228,5 +231,6 @@ function deriveRoot(alg, hash, branch, index) {

exports.createTree = createTree;
exports.createRoot = createRoot;
exports.createBranch = createBranch;
exports.createPath = createPath;
exports.createBranch = createPath;
exports.deriveRoot = deriveRoot;
35 changes: 22 additions & 13 deletions lib/mrkl.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ function createRoot(alg, leaves) {
}

/**
* Collect a merkle branch from vector index.
* Collect a merkle path from leaf index.
* @param {Object} alg
* @param {Number} index
* @param {Buffer[]} leaves
* @returns {Buffer[]} branch
* @returns {Buffer[]} path
*/

function createBranch(alg, index, leaves) {
function createPath(alg, index, leaves) {
assert((index >>> 0) === index);

const nodes = createTree(alg, leaves);
const sentinel = hashEmpty(alg);
const branch = [];
const path = [];

let size = leaves.length;
let i = 0;
Expand All @@ -105,9 +105,9 @@ function createBranch(alg, index, leaves) {
const j = index ^ 1;

if (j < size)
branch.push(nodes[i + j]);
path.push(nodes[i + j]);
else
branch.push(sentinel);
path.push(sentinel);

index >>>= 1;

Expand All @@ -116,27 +116,32 @@ function createBranch(alg, index, leaves) {
size = (size + 1) >>> 1;
}

return branch;
return path;
}

/**
* Derive merkle root from branch.
* Derive merkle root from path.
* @param {Object} alg
* @param {Buffer} leaf
* @param {Buffer[]} branch
* @param {Buffer[]} path
* @param {Number} index
* @returns {Buffer} root
*/

function deriveRoot(alg, leaf, branch, index) {
function deriveRoot(alg, leaf, path, index) {
assert(alg && typeof alg.multi === 'function');
assert(Buffer.isBuffer(leaf));
assert(Array.isArray(branch));
assert(Array.isArray(path));
assert((index >>> 0) === index);

const sentinel = hashEmpty(alg);

let root = hashLeaf(alg, leaf);

for (const hash of branch) {
for (const hash of path) {
if ((index & 1) && hash.equals(sentinel))
return alg.zero;

if (index & 1)
root = hashInternal(alg, hash, root);
else
Expand All @@ -145,6 +150,9 @@ function deriveRoot(alg, leaf, branch, index) {
index >>>= 1;
}

if (index !== 0)
return alg.zero;

return root;
}

Expand Down Expand Up @@ -188,7 +196,8 @@ function hashInternal(alg, left, right) {

exports.createTree = createTree;
exports.createRoot = createRoot;
exports.createBranch = createBranch;
exports.createPath = createPath;
exports.createBranch = createPath;
exports.deriveRoot = deriveRoot;
exports.hashEmpty = hashEmpty;
exports.hashLeaf = hashLeaf;
Expand Down

0 comments on commit 579318c

Please sign in to comment.