Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@guildofweavers/merkle

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@guildofweavers/merkle - npm Package Compare versions

Comparing version 0.3.6 to 0.3.7

11

lib/hash/JsHash.js

@@ -98,4 +98,15 @@ "use strict";

}
digestValues(values, valueSize) {
const elementCount = values.byteLength / valueSize;
if (!Number.isInteger(elementCount)) {
throw new Error('Values buffer cannot contain partial number of elements');
}
const result = new Array(elementCount);
for (let i = 0, offset = 0; i < elementCount; i++, offset += valueSize) {
result[i] = this.digest(values.slice(offset, offset + valueSize));
}
return new JsVector_1.JsVector(result);
}
}
exports.JsHash = JsHash;
//# sourceMappingURL=JsHash.js.map

34

lib/hash/WasmBlake2s.js

@@ -64,3 +64,3 @@ "use strict";

let lBuffer = leaves.toBuffer(), lRef = lBuffer.byteOffset, releaseLeaves = false;
if (lBuffer.buffer !== wasm.U8.buffer) {
if (lBuffer.buffer !== wasm.memory.buffer) {
// if the leaves buffer belongs to some other WASM memory, copy it into local memory

@@ -119,3 +119,3 @@ lRef = wasm.newArray(lBuffer.byteLength);

let buffer = vectors[i].toBuffer();
if (buffer.buffer === this.wasm.U8.buffer) {
if (buffer.buffer === this.wasm.memory.buffer) {
// if the vector is already in WASM memory, just cache the reference to it

@@ -132,3 +132,3 @@ vRef = buffer.byteOffset;

}
const resRef = this.wasm.newArray(elementCount * this.digestSize);
const resRef = this.wasm.newArray(elementCount * DIGEST_SIZE);
this.wasm.mergeArrayElements(vRefs, resRef, vectors.length, elementCount, elementSize);

@@ -141,6 +141,32 @@ // release all memory that was used up during the operation

// build and return a vector with hashes
return new WasmVector_1.WasmVector(this.wasm.memory, resRef, elementCount, this.digestSize);
return new WasmVector_1.WasmVector(this.wasm.memory, resRef, elementCount, DIGEST_SIZE);
}
digestValues(values, valueSize) {
const elementCount = values.byteLength / valueSize;
if (!Number.isInteger(elementCount)) {
throw new Error('Values buffer cannot contain partial number of elements');
}
let vRef, releaseValues;
if (this.wasm.memory.buffer === values.buffer) {
// if the vector is already in WASM memory, just cache the reference to it
vRef = values.byteOffset;
releaseValues = false;
}
else {
// otherwise, copy the vector into WASM memory
vRef = this.wasm.newArray(values.byteLength);
this.wasm.U8.set(values, vRef);
releaseValues = true;
}
// allocate memory to hold the results and hash the values
const resRef = this.wasm.newArray(elementCount * DIGEST_SIZE);
this.wasm.hashValues1(vRef, resRef, valueSize, elementCount);
// if the values were copied into WASM memory during the operation, free the memory
if (releaseValues) {
this.wasm.__release(vRef);
}
return new WasmVector_1.WasmVector(this.wasm.memory, resRef, elementCount, DIGEST_SIZE);
}
}
exports.WasmBlake2s = WasmBlake2s;
//# sourceMappingURL=WasmBlake2s.js.map

33

lib/MerkleTree.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const JsVector_1 = require("./vectors/JsVector");
const WasmVector_1 = require("./vectors/WasmVector");
// CLASS DEFINITION

@@ -10,32 +9,8 @@ // ================================================================================================

// --------------------------------------------------------------------------------------------
static async createAsync(values, hashOrValueSize, hash) {
static async createAsync(values, hash) {
// FUTURE: implement asynchronous instantiation
return MerkleTree.create(values, hashOrValueSize, hash);
return MerkleTree.create(values, hash);
}
static create(values, hashOrValueSize, hash) {
let leaves;
if (Array.isArray(values)) {
if (typeof hashOrValueSize !== 'object')
throw new TypeError('Hash object is invalid');
leaves = new JsVector_1.JsVector(values);
hash = hashOrValueSize;
}
else if (Buffer.isBuffer(values)) {
if (typeof hashOrValueSize !== 'number')
throw new TypeError('Value size is invalid');
if (!hash)
throw new TypeError('Hash object is undefined');
if (hash.wasm) {
leaves = WasmVector_1.WasmVector.fromBuffer(hash.wasm, values, hashOrValueSize);
}
else {
leaves = JsVector_1.JsVector.fromBuffer(values, hashOrValueSize);
}
}
else {
if (typeof hashOrValueSize !== 'object')
throw new TypeError('Hash object is invalid');
leaves = values;
hash = hashOrValueSize;
}
static create(values, hash) {
const leaves = Array.isArray(values) ? new JsVector_1.JsVector(values) : values;
const depth = Math.ceil(Math.log2(leaves.length));

@@ -42,0 +17,0 @@ const nodes = hash.buildMerkleNodes(depth, leaves);

@@ -10,13 +10,2 @@ "use strict";

}
static fromBuffer(values, valueSize) {
const elementCount = values.byteLength / valueSize;
if (!Number.isInteger(elementCount)) {
throw new Error('Value buffer cannot contain partial number of elements');
}
const result = new Array(elementCount);
for (let i = 0, offset = 0; i < elementCount; i++, offset += valueSize) {
result[i] = values.slice(offset, offset + valueSize);
}
return new JsVector(result);
}
get byteLength() {

@@ -23,0 +12,0 @@ return this.values.length * this.elementSize;

@@ -6,4 +6,4 @@ "use strict";

class WasmVector {
constructor(wasm, base, length, elementSize) {
this.wasm = wasm;
constructor(memory, base, length, elementSize) {
this.memory = memory;
this.base = base;

@@ -13,16 +13,2 @@ this.length = length;

}
static fromBuffer(wasm, values, valueSize) {
const elementCount = values.byteLength / valueSize;
if (!Number.isInteger(elementCount)) {
throw new Error('Value buffer cannot contain partial number of elements');
}
if (wasm.memory.buffer === values.buffer) {
return new WasmVector(wasm, values.byteOffset, elementCount, valueSize);
}
else {
const base = wasm.newArray(values.byteLength);
wasm.U8.set(values, base);
return new WasmVector(wasm, base, elementCount, valueSize);
}
}
get byteLength() {

@@ -32,3 +18,3 @@ return this.length * this.elementSize;

copyValue(index, destination, offset) {
const value = Buffer.from(this.wasm.memory.buffer, this.base + index * this.elementSize, this.elementSize);
const value = Buffer.from(this.memory.buffer, this.base + index * this.elementSize, this.elementSize);
value.copy(destination, offset);

@@ -46,3 +32,3 @@ return this.elementSize;

}
return Buffer.from(this.wasm.memory.buffer, offset, length);
return Buffer.from(this.memory.buffer, offset, length);
}

@@ -49,0 +35,0 @@ }

@@ -29,3 +29,2 @@ declare module '@guildofweavers/merkle' {

readonly isOptimized: boolean;
readonly wasm? : WasmModule;

@@ -41,2 +40,4 @@ /** Hashes the provided value */

mergeVectorRows(vectors: Vector[]): Vector;
digestValues(values: Buffer, valueSize: number): Vector;
}

@@ -59,10 +60,2 @@

/**
* Returns a Merkle tree created from the specified values
* @param values A buffer containing serialized values for the tree
* @param valueSize Size (in bytes) of each value
* @param hash Hash object to use for hashing of internal nodes
*/
static create(values: Buffer, valueSize: number, hash: Hash): MerkleTree;
/**
* Returns a Promise for a Merkle tree created from the specified values

@@ -74,10 +67,2 @@ * @param values Values that form the leaves of the tree

/**
* Returns a Promise for a Merkle tree created from the specified values
* @param values A buffer containing serialized values for the tree
* @param valueSize Size (in bytes) of each value
* @param hash Hash object to use for hashing of internal nodes
*/
static create(values: Buffer, valueSize: number, hash: Hash): MerkleTree;
/** Root of the tree */

@@ -139,7 +124,2 @@ readonly root: Buffer;

export interface WasmModule {
readonly memory : WebAssembly.Memory;
readonly U8 : Uint8Array;
newArray(length: number): number;
}
}
{
"name": "@guildofweavers/merkle",
"version": "0.3.6",
"version": "0.3.7",
"description": "Merkle tree and other data structures",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -43,7 +43,2 @@ # Merkle

Values can also be passed in as a single buffer like so:
* static **create**(values: `Buffer`, valueSize: `number`, hash: `Hash`): `MerkleTree`
* static **createAsync**(values: `Buffer`, valueSize: `number`, hash: `Hash`): `Promise<MerkleTree>`
The meaning of the parameters is as follows:

@@ -50,0 +45,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc