@guildofweavers/merkle
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -33,2 +33,3 @@ "use strict"; | ||
digest(value) { | ||
// TODO: investigate checking if the buffer comes from shared memory | ||
if (value.byteLength < 4096) { | ||
@@ -35,0 +36,0 @@ this.wasm.U8.set(value, this.iRef); |
@@ -21,3 +21,3 @@ "use strict"; | ||
this.depth = depth; | ||
this.nodes = nodes; | ||
this.nodes = Buffer.from(nodes); | ||
this.nodeSize = nodeSize; | ||
@@ -29,11 +29,14 @@ this.values = leaves; | ||
get root() { | ||
return Buffer.from(this.nodes, this.nodeSize, this.nodeSize); | ||
// makes a copy of a node at position 1 | ||
return this.nodes.slice(this.nodeSize, this.nodeSize + this.nodeSize); | ||
} | ||
getLeaf(index) { | ||
return this.values.toBuffer(index, 1); | ||
// makes a copy of the leaf value | ||
return Buffer.from(this.values.toBuffer(index, 1)); | ||
} | ||
getLeaves() { | ||
// makes a deep copy of all leaves | ||
const leaves = new Array(this.values.length); | ||
for (let i = 0; i < leaves.length; i++) { | ||
leaves[i] = this.values.toBuffer(i, 1); | ||
leaves[i] = Buffer.from(this.values.toBuffer(i, 1)); | ||
} | ||
@@ -53,8 +56,9 @@ return leaves; | ||
const nodeCount = this.nodes.byteLength / nodeSize; | ||
const value1 = this.values.toBuffer(index, 1); | ||
const value2 = this.values.toBuffer(index ^ 1, 1); | ||
const value1 = this.getLeaf(index); | ||
const value2 = this.getLeaf(index ^ 1); | ||
const proof = [value1, value2]; | ||
index = (index + nodeCount) >> 1; | ||
while (index > 1) { | ||
let sibling = Buffer.from(this.nodes, (index ^ 1) * nodeSize, nodeSize); | ||
let siblingOffset = (index ^ 1) * nodeSize; | ||
let sibling = this.nodes.slice(siblingOffset, siblingOffset + nodeSize); | ||
proof.push(sibling); | ||
@@ -79,4 +83,4 @@ index = index >> 1; | ||
let index = indexes[i]; | ||
let v1 = this.values.toBuffer(index, 1); | ||
let v2 = this.values.toBuffer(index + 1, 1); | ||
let v1 = this.getLeaf(index); | ||
let v2 = this.getLeaf(index + 1); | ||
// only values for indexes that were explicitly requested are included in values array | ||
@@ -112,3 +116,4 @@ const inputIndex1 = indexMap.get(index); | ||
else { | ||
let sibling = Buffer.from(this.nodes, siblingIndex * nodeSize, nodeSize); | ||
let siblingOffset = siblingIndex * nodeSize; | ||
let sibling = this.nodes.slice(siblingOffset, siblingOffset + nodeSize); | ||
proof.nodes[i].push(sibling); | ||
@@ -115,0 +120,0 @@ } |
{ | ||
"name": "@guildofweavers/merkle", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "Merkle tree and other data structures", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
41650
652