@guildofweavers/merkle
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -6,2 +6,3 @@ "use strict"; | ||
const crypto = require("crypto"); | ||
const JsVector_1 = require("../vectors/JsVector"); | ||
// MODULE VARIABLES | ||
@@ -84,4 +85,18 @@ // ================================================================================================ | ||
} | ||
mergeVectorRows(vectors) { | ||
const elementCount = vectors[0].length; | ||
const elementSize = vectors[0].elementSize; | ||
const result = new Array(elementCount); | ||
const buffer = Buffer.allocUnsafe(vectors.length * elementSize); | ||
for (let i = 0; i < elementCount; i++) { | ||
let offset = 0; | ||
for (let j = 0; j < vectors.length; j++) { | ||
offset += vectors[j].copyValue(i, buffer, offset); | ||
} | ||
result[i] = this.digest(buffer); | ||
} | ||
return new JsVector_1.JsVector(result); | ||
} | ||
} | ||
exports.JsHash = JsHash; | ||
//# sourceMappingURL=JsHash.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const assembly_1 = require("../assembly"); | ||
const WasmVector_1 = require("../vectors/WasmVector"); | ||
// MODULE VARIABLES | ||
@@ -101,4 +102,42 @@ // ================================================================================================ | ||
} | ||
mergeVectorRows(vectors) { | ||
const elementCount = vectors[0].length; | ||
const elementSize = vectors[0].elementSize; | ||
if (elementSize > 64) { | ||
throw new Error(`Cannot merge vector rows: vector element size must be smaller than 64 bytes`); | ||
} | ||
else if (64 % elementSize !== 0) { | ||
throw new Error(`Cannot merge vector rows: vector element size must be a divisor of 64`); | ||
} | ||
const vRefs = this.wasm.newArray(vectors.length * 8); | ||
const vIdx = vRefs >>> 3; | ||
const refsToRelease = new Set(); | ||
// build array of references to vectors | ||
let vRef; | ||
for (let i = 0; i < vectors.length; i++) { | ||
let buffer = vectors[i].toBuffer(); | ||
if (buffer.buffer === this.wasm.U8.buffer) { | ||
// if the vector is already in WASM memory, just cache the reference to it | ||
vRef = buffer.byteOffset; | ||
} | ||
else { | ||
// otherwise, copy the vector into WASM memory | ||
vRef = this.wasm.newArray(buffer.byteLength); | ||
this.wasm.U8.set(vectors[i].toBuffer(), vRef); | ||
refsToRelease.add(vRef); | ||
} | ||
this.wasm.U64[vIdx + i] = BigInt(vRef); | ||
} | ||
const resRef = this.wasm.newArray(elementCount * this.digestSize); | ||
this.wasm.mergeArrayElements(vRefs, resRef, vectors.length, elementCount, elementSize); | ||
// release all memory that was used up during the operation | ||
this.wasm.__release(vRefs); | ||
for (let vRef of refsToRelease) { | ||
this.wasm.__release(vRef); | ||
} | ||
// build and return a vector with hashes | ||
return new WasmVector_1.WasmVector(this.wasm.memory, resRef, elementCount, this.digestSize); | ||
} | ||
} | ||
exports.WasmBlake2s = WasmBlake2s; | ||
//# sourceMappingURL=WasmBlake2s.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const JsVector_1 = require("./JsVector"); | ||
const JsVector_1 = require("./vectors/JsVector"); | ||
// CLASS DEFINITION | ||
@@ -5,0 +5,0 @@ // ================================================================================================ |
@@ -37,2 +37,4 @@ declare module '@guildofweavers/merkle' { | ||
buildMerkleNodes(depth: number, leaves: Vector): ArrayBuffer; | ||
mergeVectorRows(vectors: Vector[]): Vector; | ||
} | ||
@@ -113,4 +115,5 @@ | ||
copyValue(index: number, destination: Buffer, offset: number): number; | ||
toBuffer(startIdx?: number, elementCount?: number): Buffer; | ||
} | ||
} |
{ | ||
"name": "@guildofweavers/merkle", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Merkle tree and other data structures", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45930
12
745