@typeberry/trie
Advanced tools
Comparing version 0.0.1-dd62eff to 0.0.1-f8aea68
@@ -31,2 +31,10 @@ /** | ||
/** | ||
* Utilities for tests. | ||
*/ | ||
/** Unique symbol that can be added to a class to have it be compared by strings instead of defaults. */ | ||
declare const TEST_COMPARE_VIA_STRING: unique symbol = Symbol("compare via string"); | ||
/** | ||
* A variable-length blob of bytes with a concise text representation. | ||
@@ -38,2 +46,4 @@ * | ||
declare class BytesBlob { | ||
[TEST_COMPARE_VIA_STRING] = true; | ||
readonly raw: Uint8Array; | ||
@@ -74,4 +84,7 @@ readonly length: number = 0; | ||
/** Compare the sequence to another one lexicographically. */ | ||
isLessThan(other: BytesBlob): boolean { | ||
/** Compare the sequence to another one lexicographically. | ||
* Returns true if "this" blob is less than (or equal to) "other" | ||
* https://graypaper.fluffylabs.dev/#/5f542d7/07c40007c400 | ||
*/ | ||
private cmp(other: BytesBlob, { orEqual = false } = {}): boolean { | ||
const min = Math.min(this.length, other.length); | ||
@@ -91,5 +104,16 @@ const thisRaw = this.raw; | ||
if (orEqual) { | ||
return this.length <= other.length; | ||
} | ||
return this.length < other.length; | ||
} | ||
isLessThan(other: BytesBlob): boolean { | ||
return this.cmp(other, { orEqual: false }); | ||
} | ||
isLessThanOrEqualTo(other: BytesBlob): boolean { | ||
return this.cmp(other, { orEqual: true }); | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
@@ -254,6 +278,19 @@ static blobFromString(v: string): BytesBlob { | ||
/** Return a raw in-memory representation of this [`BitVec`]. */ | ||
raw(): Uint8Array { | ||
get raw(): Uint8Array { | ||
return this.data.subarray(0, this.byteLength); | ||
} | ||
/** Perform OR operation on all bits in place. */ | ||
sumWith(other: BitVec) { | ||
check( | ||
other.bitLength === this.bitLength, | ||
`Invalid bit length for sumWith: ${other.bitLength} vs ${this.bitLength}`, | ||
); | ||
const otherRaw = other.raw; | ||
for (let i = 0; i < this.byteLength; i++) { | ||
this.data[i] |= otherRaw[i]; | ||
} | ||
} | ||
/** | ||
@@ -285,2 +322,18 @@ * Set the bit at index `idx` to value `val`. | ||
} | ||
/** | ||
* Iterate over indices of bits that are set. | ||
*/ | ||
*indicesOfSetBits() { | ||
for (let b = 0; b < this.bitLength; b++) { | ||
const byteIndex = b >> 3; | ||
const bitIndex = b - (byteIndex << 3); | ||
const byte = this.data[byteIndex]; | ||
const bitValue = byte >> bitIndex; | ||
if ((bitValue & 0b1) === 0b1) { | ||
yield b; | ||
} | ||
} | ||
} | ||
} | ||
@@ -291,3 +344,3 @@ | ||
* | ||
* https://graypaper.fluffylabs.dev/#/387103d/071401071f01 | ||
* https://graypaper.fluffylabs.dev/#/579bd12/073101073c01 | ||
* | ||
@@ -310,6 +363,7 @@ */ | ||
* | ||
* https://graypaper.fluffylabs.dev/#/387103d/0cb0000cb400 | ||
* https://graypaper.fluffylabs.dev/#/579bd12/0c1f010c2301 | ||
*/ | ||
type TrieHash = Opaque<OpaqueHash, "trie">; | ||
type ValueHash = Opaque<OpaqueHash, "trieValue">; | ||
/** Value nodes have the key truncated to 31 bytes. */ | ||
@@ -320,4 +374,4 @@ declare const TRUNCATED_KEY_BYTES = 31; | ||
declare function parseInputKey(v: string): InputKey { | ||
if (v.length === HASH_BYTES * 2) { | ||
return Bytes.parseBytesNoPrefix(v, HASH_BYTES).asOpaque(); | ||
if (v.length === HASH_SIZE * 2) { | ||
return Bytes.parseBytesNoPrefix(v, HASH_SIZE).asOpaque(); | ||
} | ||
@@ -412,3 +466,3 @@ return Bytes.parseBytesNoPrefix(v, TRUNCATED_KEY_BYTES).asOpaque(); | ||
node.data.set(left.raw, 0); | ||
node.data.set(right.raw, HASH_BYTES); | ||
node.data.set(right.raw, HASH_SIZE); | ||
@@ -423,3 +477,3 @@ // set the first bit to 0 (branch node) | ||
getLeft(): TrieHash { | ||
return Bytes.fromBlob(this.node.data.subarray(0, HASH_BYTES), HASH_BYTES).asOpaque(); | ||
return Bytes.fromBlob(this.node.data.subarray(0, HASH_SIZE), HASH_SIZE).asOpaque(); | ||
} | ||
@@ -429,3 +483,3 @@ | ||
getRight(): TrieHash { | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_BYTES), HASH_BYTES).asOpaque(); | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_SIZE), HASH_SIZE).asOpaque(); | ||
} | ||
@@ -463,3 +517,3 @@ } | ||
// The value will fit in the leaf itself. | ||
if (value.length <= HASH_BYTES) { | ||
if (value.length <= HASH_SIZE) { | ||
node.data[0] = FIRST_BIT_SET | value.length; | ||
@@ -494,3 +548,3 @@ // truncate & copy the key | ||
const firstByte = this.node.data[0]; | ||
// we only store values up to `HASH_BYTES`, so they fit on the last 6 bits. | ||
// we only store values up to `HASH_SIZE`, so they fit on the last 6 bits. | ||
return firstByte & FIRST_TWO_BITS_SET_NEG; | ||
@@ -507,3 +561,3 @@ } | ||
const len = this.getValueLength(); | ||
return BytesBlob.blobFrom(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
return BytesBlob.blobFrom(this.node.data.subarray(HASH_SIZE, HASH_SIZE + len)); | ||
} | ||
@@ -518,3 +572,3 @@ | ||
getValueHash(): ValueHash { | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_BYTES), HASH_BYTES).asOpaque(); | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_SIZE), HASH_SIZE).asOpaque(); | ||
} | ||
@@ -550,5 +604,9 @@ } | ||
/** Remove the key and any value from the dictionary. */ | ||
/** | ||
* Remove the key and any value from the dictionary. | ||
* | ||
* Returns `true` if element existed and was removed, `false` otherwise. | ||
*/ | ||
delete(key: K) { | ||
this.map.delete(key.toString()); | ||
return this.map.delete(key.toString()); | ||
} | ||
@@ -666,3 +724,3 @@ | ||
if (this.root === null) { | ||
return Bytes.zero(HASH_BYTES).asOpaque(); | ||
return Bytes.zero(HASH_SIZE).asOpaque(); | ||
} | ||
@@ -669,0 +727,0 @@ |
769
index.js
'use strict'; | ||
require('node:assert'); | ||
/** | ||
@@ -38,2 +40,9 @@ * A function to perform runtime assertions. | ||
/** | ||
* Utilities for tests. | ||
*/ | ||
/** Unique symbol that can be added to a class to have it be compared by strings instead of defaults. */ | ||
const TEST_COMPARE_VIA_STRING = Symbol("compare via string"); | ||
var _a; | ||
/** | ||
* A variable-length blob of bytes with a concise text representation. | ||
@@ -46,2 +55,3 @@ * | ||
constructor(data) { | ||
this[_a] = true; | ||
this.length = 0; | ||
@@ -73,4 +83,7 @@ this.raw = data; | ||
} | ||
/** Compare the sequence to another one lexicographically. */ | ||
isLessThan(other) { | ||
/** Compare the sequence to another one lexicographically. | ||
* Returns true if "this" blob is less than (or equal to) "other" | ||
* https://graypaper.fluffylabs.dev/#/5f542d7/07c40007c400 | ||
*/ | ||
cmp(other, { orEqual = false } = {}) { | ||
const min = Math.min(this.length, other.length); | ||
@@ -87,4 +100,13 @@ const thisRaw = this.raw; | ||
} | ||
if (orEqual) { | ||
return this.length <= other.length; | ||
} | ||
return this.length < other.length; | ||
} | ||
isLessThan(other) { | ||
return this.cmp(other, { orEqual: false }); | ||
} | ||
isLessThanOrEqualTo(other) { | ||
return this.cmp(other, { orEqual: true }); | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
@@ -139,2 +161,3 @@ static blobFromString(v) { | ||
} | ||
_a = TEST_COMPARE_VIA_STRING; | ||
/** | ||
@@ -267,5 +290,13 @@ * A convenience wrapper for a fix-length sequence of bytes. | ||
/** Return a raw in-memory representation of this [`BitVec`]. */ | ||
raw() { | ||
get raw() { | ||
return this.data.subarray(0, this.byteLength); | ||
} | ||
/** Perform OR operation on all bits in place. */ | ||
sumWith(other) { | ||
check(other.bitLength === this.bitLength, `Invalid bit length for sumWith: ${other.bitLength} vs ${this.bitLength}`); | ||
const otherRaw = other.raw; | ||
for (let i = 0; i < this.byteLength; i++) { | ||
this.data[i] |= otherRaw[i]; | ||
} | ||
} | ||
/** | ||
@@ -296,4 +327,702 @@ * Set the bit at index `idx` to value `val`. | ||
} | ||
/** | ||
* Iterate over indices of bits that are set. | ||
*/ | ||
*indicesOfSetBits() { | ||
for (let b = 0; b < this.bitLength; b++) { | ||
const byteIndex = b >> 3; | ||
const bitIndex = b - (byteIndex << 3); | ||
const byte = this.data[byteIndex]; | ||
const bitValue = byte >> bitIndex; | ||
if ((bitValue & 0b1) === 0b1) { | ||
yield b; | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Size of the output of the hash functions. | ||
* | ||
* https://graypaper.fluffylabs.dev/#/579bd12/073101073c01 | ||
* | ||
*/ | ||
const HASH_SIZE = 32; | ||
var blake2b$1 = {exports: {}}; | ||
var nanoassert = assert$2; | ||
class AssertionError extends Error {} | ||
AssertionError.prototype.name = 'AssertionError'; | ||
/** | ||
* Minimal assert function | ||
* @param {any} t Value to check if falsy | ||
* @param {string=} m Optional assertion error message | ||
* @throws {AssertionError} | ||
*/ | ||
function assert$2 (t, m) { | ||
if (!t) { | ||
var err = new AssertionError(m); | ||
if (Error.captureStackTrace) Error.captureStackTrace(err, assert$2); | ||
throw err | ||
} | ||
} | ||
var blake2bWasm = {exports: {}}; | ||
function isBuffer (value) { | ||
return Buffer.isBuffer(value) || value instanceof Uint8Array | ||
} | ||
function isEncoding (encoding) { | ||
return Buffer.isEncoding(encoding) | ||
} | ||
function alloc (size, fill, encoding) { | ||
return Buffer.alloc(size, fill, encoding) | ||
} | ||
function allocUnsafe (size) { | ||
return Buffer.allocUnsafe(size) | ||
} | ||
function allocUnsafeSlow (size) { | ||
return Buffer.allocUnsafeSlow(size) | ||
} | ||
function byteLength (string, encoding) { | ||
return Buffer.byteLength(string, encoding) | ||
} | ||
function compare (a, b) { | ||
return Buffer.compare(a, b) | ||
} | ||
function concat (buffers, totalLength) { | ||
return Buffer.concat(buffers, totalLength) | ||
} | ||
function copy (source, target, targetStart, start, end) { | ||
return toBuffer(source).copy(target, targetStart, start, end) | ||
} | ||
function equals (a, b) { | ||
return toBuffer(a).equals(b) | ||
} | ||
function fill (buffer, value, offset, end, encoding) { | ||
return toBuffer(buffer).fill(value, offset, end, encoding) | ||
} | ||
function from (value, encodingOrOffset, length) { | ||
return Buffer.from(value, encodingOrOffset, length) | ||
} | ||
function includes (buffer, value, byteOffset, encoding) { | ||
return toBuffer(buffer).includes(value, byteOffset, encoding) | ||
} | ||
function indexOf (buffer, value, byfeOffset, encoding) { | ||
return toBuffer(buffer).indexOf(value, byfeOffset, encoding) | ||
} | ||
function lastIndexOf (buffer, value, byteOffset, encoding) { | ||
return toBuffer(buffer).lastIndexOf(value, byteOffset, encoding) | ||
} | ||
function swap16 (buffer) { | ||
return toBuffer(buffer).swap16() | ||
} | ||
function swap32 (buffer) { | ||
return toBuffer(buffer).swap32() | ||
} | ||
function swap64 (buffer) { | ||
return toBuffer(buffer).swap64() | ||
} | ||
function toBuffer (buffer) { | ||
if (Buffer.isBuffer(buffer)) return buffer | ||
return Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength) | ||
} | ||
function toString (buffer, encoding, start, end) { | ||
return toBuffer(buffer).toString(encoding, start, end) | ||
} | ||
function write (buffer, string, offset, length, encoding) { | ||
return toBuffer(buffer).write(string, offset, length, encoding) | ||
} | ||
function writeDoubleLE (buffer, value, offset) { | ||
return toBuffer(buffer).writeDoubleLE(value, offset) | ||
} | ||
function writeFloatLE (buffer, value, offset) { | ||
return toBuffer(buffer).writeFloatLE(value, offset) | ||
} | ||
function writeUInt32LE (buffer, value, offset) { | ||
return toBuffer(buffer).writeUInt32LE(value, offset) | ||
} | ||
function writeInt32LE (buffer, value, offset) { | ||
return toBuffer(buffer).writeInt32LE(value, offset) | ||
} | ||
function readDoubleLE (buffer, offset) { | ||
return toBuffer(buffer).readDoubleLE(offset) | ||
} | ||
function readFloatLE (buffer, offset) { | ||
return toBuffer(buffer).readFloatLE(offset) | ||
} | ||
function readUInt32LE (buffer, offset) { | ||
return toBuffer(buffer).readUInt32LE(offset) | ||
} | ||
function readInt32LE (buffer, offset) { | ||
return toBuffer(buffer).readInt32LE(offset) | ||
} | ||
var b4a$1 = { | ||
isBuffer, | ||
isEncoding, | ||
alloc, | ||
allocUnsafe, | ||
allocUnsafeSlow, | ||
byteLength, | ||
compare, | ||
concat, | ||
copy, | ||
equals, | ||
fill, | ||
from, | ||
includes, | ||
indexOf, | ||
lastIndexOf, | ||
swap16, | ||
swap32, | ||
swap64, | ||
toBuffer, | ||
toString, | ||
write, | ||
writeDoubleLE, | ||
writeFloatLE, | ||
writeUInt32LE, | ||
writeInt32LE, | ||
readDoubleLE, | ||
readFloatLE, | ||
readUInt32LE, | ||
readInt32LE | ||
}; | ||
var blake2b; | ||
var hasRequiredBlake2b; | ||
function requireBlake2b () { | ||
if (hasRequiredBlake2b) return blake2b; | ||
hasRequiredBlake2b = 1; | ||
var __commonJS = (cb, mod) => function __require() { | ||
return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
}; | ||
var __toBinary = /* @__PURE__ */ (() => { | ||
var table = new Uint8Array(128); | ||
for (var i = 0; i < 64; i++) | ||
table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i; | ||
return (base64) => { | ||
var n = base64.length, bytes2 = new Uint8Array((n - (base64[n - 1] == "=") - (base64[n - 2] == "=")) * 3 / 4 | 0); | ||
for (var i2 = 0, j = 0; i2 < n; ) { | ||
var c0 = table[base64.charCodeAt(i2++)], c1 = table[base64.charCodeAt(i2++)]; | ||
var c2 = table[base64.charCodeAt(i2++)], c3 = table[base64.charCodeAt(i2++)]; | ||
bytes2[j++] = c0 << 2 | c1 >> 4; | ||
bytes2[j++] = c1 << 4 | c2 >> 2; | ||
bytes2[j++] = c2 << 6 | c3; | ||
} | ||
return bytes2; | ||
}; | ||
})(); | ||
// wasm-binary:./blake2b.wat | ||
var require_blake2b = __commonJS({ | ||
"wasm-binary:./blake2b.wat"(exports2, module2) { | ||
module2.exports = __toBinary("AGFzbQEAAAABEANgAn9/AGADf39/AGABfwADBQQAAQIC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D4WFNwMAICggKCkDACAIIBCFhTcDAAs="); | ||
} | ||
}); | ||
// wasm-module:./blake2b.wat | ||
var bytes = require_blake2b(); | ||
var compiled = WebAssembly.compile(bytes); | ||
blake2b = async (imports) => { | ||
const instance = await WebAssembly.instantiate(await compiled, imports); | ||
return instance.exports; | ||
}; | ||
return blake2b; | ||
} | ||
var assert$1 = nanoassert; | ||
var b4a = b4a$1; | ||
var wasm = null; | ||
var wasmPromise = typeof WebAssembly !== "undefined" && requireBlake2b()().then(mod => { | ||
wasm = mod; | ||
}); | ||
var head = 64; | ||
var freeList = []; | ||
blake2bWasm.exports = Blake2b$1; | ||
var BYTES_MIN$1 = blake2bWasm.exports.BYTES_MIN = 16; | ||
var BYTES_MAX$1 = blake2bWasm.exports.BYTES_MAX = 64; | ||
blake2bWasm.exports.BYTES = 32; | ||
var KEYBYTES_MIN$1 = blake2bWasm.exports.KEYBYTES_MIN = 16; | ||
var KEYBYTES_MAX$1 = blake2bWasm.exports.KEYBYTES_MAX = 64; | ||
blake2bWasm.exports.KEYBYTES = 32; | ||
var SALTBYTES$1 = blake2bWasm.exports.SALTBYTES = 16; | ||
var PERSONALBYTES$1 = blake2bWasm.exports.PERSONALBYTES = 16; | ||
function Blake2b$1 (digestLength, key, salt, personal, noAssert) { | ||
if (!(this instanceof Blake2b$1)) return new Blake2b$1(digestLength, key, salt, personal, noAssert) | ||
if (!wasm) throw new Error('WASM not loaded. Wait for Blake2b.ready(cb)') | ||
if (!digestLength) digestLength = 32; | ||
if (noAssert !== true) { | ||
assert$1(digestLength >= BYTES_MIN$1, 'digestLength must be at least ' + BYTES_MIN$1 + ', was given ' + digestLength); | ||
assert$1(digestLength <= BYTES_MAX$1, 'digestLength must be at most ' + BYTES_MAX$1 + ', was given ' + digestLength); | ||
if (key != null) { | ||
assert$1(key instanceof Uint8Array, 'key must be Uint8Array or Buffer'); | ||
assert$1(key.length >= KEYBYTES_MIN$1, 'key must be at least ' + KEYBYTES_MIN$1 + ', was given ' + key.length); | ||
assert$1(key.length <= KEYBYTES_MAX$1, 'key must be at least ' + KEYBYTES_MAX$1 + ', was given ' + key.length); | ||
} | ||
if (salt != null) { | ||
assert$1(salt instanceof Uint8Array, 'salt must be Uint8Array or Buffer'); | ||
assert$1(salt.length === SALTBYTES$1, 'salt must be exactly ' + SALTBYTES$1 + ', was given ' + salt.length); | ||
} | ||
if (personal != null) { | ||
assert$1(personal instanceof Uint8Array, 'personal must be Uint8Array or Buffer'); | ||
assert$1(personal.length === PERSONALBYTES$1, 'personal must be exactly ' + PERSONALBYTES$1 + ', was given ' + personal.length); | ||
} | ||
} | ||
if (!freeList.length) { | ||
freeList.push(head); | ||
head += 216; | ||
} | ||
this.digestLength = digestLength; | ||
this.finalized = false; | ||
this.pointer = freeList.pop(); | ||
this._memory = new Uint8Array(wasm.memory.buffer); | ||
this._memory.fill(0, 0, 64); | ||
this._memory[0] = this.digestLength; | ||
this._memory[1] = key ? key.length : 0; | ||
this._memory[2] = 1; // fanout | ||
this._memory[3] = 1; // depth | ||
if (salt) this._memory.set(salt, 32); | ||
if (personal) this._memory.set(personal, 48); | ||
if (this.pointer + 216 > this._memory.length) this._realloc(this.pointer + 216); // we need 216 bytes for the state | ||
wasm.blake2b_init(this.pointer, this.digestLength); | ||
if (key) { | ||
this.update(key); | ||
this._memory.fill(0, head, head + key.length); // whiteout key | ||
this._memory[this.pointer + 200] = 128; | ||
} | ||
} | ||
Blake2b$1.prototype._realloc = function (size) { | ||
wasm.memory.grow(Math.max(0, Math.ceil(Math.abs(size - this._memory.length) / 65536))); | ||
this._memory = new Uint8Array(wasm.memory.buffer); | ||
}; | ||
Blake2b$1.prototype.update = function (input) { | ||
assert$1(this.finalized === false, 'Hash instance finalized'); | ||
assert$1(input instanceof Uint8Array, 'input must be Uint8Array or Buffer'); | ||
if (head + input.length > this._memory.length) this._realloc(head + input.length); | ||
this._memory.set(input, head); | ||
wasm.blake2b_update(this.pointer, head, head + input.length); | ||
return this | ||
}; | ||
Blake2b$1.prototype.digest = function (enc) { | ||
assert$1(this.finalized === false, 'Hash instance finalized'); | ||
this.finalized = true; | ||
freeList.push(this.pointer); | ||
wasm.blake2b_final(this.pointer); | ||
if (!enc || enc === 'binary') { | ||
return this._memory.slice(this.pointer + 128, this.pointer + 128 + this.digestLength) | ||
} | ||
if (typeof enc === 'string') { | ||
return b4a.toString(this._memory, enc, this.pointer + 128, this.pointer + 128 + this.digestLength) | ||
} | ||
assert$1(enc instanceof Uint8Array && enc.length >= this.digestLength, 'input must be Uint8Array or Buffer'); | ||
for (var i = 0; i < this.digestLength; i++) { | ||
enc[i] = this._memory[this.pointer + 128 + i]; | ||
} | ||
return enc | ||
}; | ||
// libsodium compat | ||
Blake2b$1.prototype.final = Blake2b$1.prototype.digest; | ||
Blake2b$1.WASM = wasm; | ||
Blake2b$1.SUPPORTED = typeof WebAssembly !== 'undefined'; | ||
Blake2b$1.ready = function (cb) { | ||
if (!cb) cb = noop; | ||
if (!wasmPromise) return cb(new Error('WebAssembly not supported')) | ||
return wasmPromise.then(() => cb(), cb) | ||
}; | ||
Blake2b$1.prototype.ready = Blake2b$1.ready; | ||
Blake2b$1.prototype.getPartialHash = function () { | ||
return this._memory.slice(this.pointer, this.pointer + 216); | ||
}; | ||
Blake2b$1.prototype.setPartialHash = function (ph) { | ||
this._memory.set(ph, this.pointer); | ||
}; | ||
function noop () {} | ||
var blake2bWasmExports = blake2bWasm.exports; | ||
var assert = nanoassert; | ||
var b2wasm = blake2bWasmExports; | ||
// 64-bit unsigned addition | ||
// Sets v[a,a+1] += v[b,b+1] | ||
// v should be a Uint32Array | ||
function ADD64AA (v, a, b) { | ||
var o0 = v[a] + v[b]; | ||
var o1 = v[a + 1] + v[b + 1]; | ||
if (o0 >= 0x100000000) { | ||
o1++; | ||
} | ||
v[a] = o0; | ||
v[a + 1] = o1; | ||
} | ||
// 64-bit unsigned addition | ||
// Sets v[a,a+1] += b | ||
// b0 is the low 32 bits of b, b1 represents the high 32 bits | ||
function ADD64AC (v, a, b0, b1) { | ||
var o0 = v[a] + b0; | ||
if (b0 < 0) { | ||
o0 += 0x100000000; | ||
} | ||
var o1 = v[a + 1] + b1; | ||
if (o0 >= 0x100000000) { | ||
o1++; | ||
} | ||
v[a] = o0; | ||
v[a + 1] = o1; | ||
} | ||
// Little-endian byte access | ||
function B2B_GET32 (arr, i) { | ||
return (arr[i] ^ | ||
(arr[i + 1] << 8) ^ | ||
(arr[i + 2] << 16) ^ | ||
(arr[i + 3] << 24)) | ||
} | ||
// G Mixing function | ||
// The ROTRs are inlined for speed | ||
function B2B_G (a, b, c, d, ix, iy) { | ||
var x0 = m[ix]; | ||
var x1 = m[ix + 1]; | ||
var y0 = m[iy]; | ||
var y1 = m[iy + 1]; | ||
ADD64AA(v, a, b); // v[a,a+1] += v[b,b+1] ... in JS we must store a uint64 as two uint32s | ||
ADD64AC(v, a, x0, x1); // v[a, a+1] += x ... x0 is the low 32 bits of x, x1 is the high 32 bits | ||
// v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated to the right by 32 bits | ||
var xor0 = v[d] ^ v[a]; | ||
var xor1 = v[d + 1] ^ v[a + 1]; | ||
v[d] = xor1; | ||
v[d + 1] = xor0; | ||
ADD64AA(v, c, d); | ||
// v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 24 bits | ||
xor0 = v[b] ^ v[c]; | ||
xor1 = v[b + 1] ^ v[c + 1]; | ||
v[b] = (xor0 >>> 24) ^ (xor1 << 8); | ||
v[b + 1] = (xor1 >>> 24) ^ (xor0 << 8); | ||
ADD64AA(v, a, b); | ||
ADD64AC(v, a, y0, y1); | ||
// v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated right by 16 bits | ||
xor0 = v[d] ^ v[a]; | ||
xor1 = v[d + 1] ^ v[a + 1]; | ||
v[d] = (xor0 >>> 16) ^ (xor1 << 16); | ||
v[d + 1] = (xor1 >>> 16) ^ (xor0 << 16); | ||
ADD64AA(v, c, d); | ||
// v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 63 bits | ||
xor0 = v[b] ^ v[c]; | ||
xor1 = v[b + 1] ^ v[c + 1]; | ||
v[b] = (xor1 >>> 31) ^ (xor0 << 1); | ||
v[b + 1] = (xor0 >>> 31) ^ (xor1 << 1); | ||
} | ||
// Initialization Vector | ||
var BLAKE2B_IV32 = new Uint32Array([ | ||
0xF3BCC908, 0x6A09E667, 0x84CAA73B, 0xBB67AE85, | ||
0xFE94F82B, 0x3C6EF372, 0x5F1D36F1, 0xA54FF53A, | ||
0xADE682D1, 0x510E527F, 0x2B3E6C1F, 0x9B05688C, | ||
0xFB41BD6B, 0x1F83D9AB, 0x137E2179, 0x5BE0CD19 | ||
]); | ||
var SIGMA8 = [ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | ||
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, | ||
11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, | ||
7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, | ||
9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, | ||
2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9, | ||
12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11, | ||
13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10, | ||
6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5, | ||
10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | ||
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 | ||
]; | ||
// These are offsets into a uint64 buffer. | ||
// Multiply them all by 2 to make them offsets into a uint32 buffer, | ||
// because this is Javascript and we don't have uint64s | ||
var SIGMA82 = new Uint8Array(SIGMA8.map(function (x) { return x * 2 })); | ||
// Compression function. 'last' flag indicates last block. | ||
// Note we're representing 16 uint64s as 32 uint32s | ||
var v = new Uint32Array(32); | ||
var m = new Uint32Array(32); | ||
function blake2bCompress (ctx, last) { | ||
var i = 0; | ||
// init work variables | ||
for (i = 0; i < 16; i++) { | ||
v[i] = ctx.h[i]; | ||
v[i + 16] = BLAKE2B_IV32[i]; | ||
} | ||
// low 64 bits of offset | ||
v[24] = v[24] ^ ctx.t; | ||
v[25] = v[25] ^ (ctx.t / 0x100000000); | ||
// high 64 bits not supported, offset may not be higher than 2**53-1 | ||
// last block flag set ? | ||
if (last) { | ||
v[28] = ~v[28]; | ||
v[29] = ~v[29]; | ||
} | ||
// get little-endian words | ||
for (i = 0; i < 32; i++) { | ||
m[i] = B2B_GET32(ctx.b, 4 * i); | ||
} | ||
// twelve rounds of mixing | ||
for (i = 0; i < 12; i++) { | ||
B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]); | ||
B2B_G(2, 10, 18, 26, SIGMA82[i * 16 + 2], SIGMA82[i * 16 + 3]); | ||
B2B_G(4, 12, 20, 28, SIGMA82[i * 16 + 4], SIGMA82[i * 16 + 5]); | ||
B2B_G(6, 14, 22, 30, SIGMA82[i * 16 + 6], SIGMA82[i * 16 + 7]); | ||
B2B_G(0, 10, 20, 30, SIGMA82[i * 16 + 8], SIGMA82[i * 16 + 9]); | ||
B2B_G(2, 12, 22, 24, SIGMA82[i * 16 + 10], SIGMA82[i * 16 + 11]); | ||
B2B_G(4, 14, 16, 26, SIGMA82[i * 16 + 12], SIGMA82[i * 16 + 13]); | ||
B2B_G(6, 8, 18, 28, SIGMA82[i * 16 + 14], SIGMA82[i * 16 + 15]); | ||
} | ||
for (i = 0; i < 16; i++) { | ||
ctx.h[i] = ctx.h[i] ^ v[i] ^ v[i + 16]; | ||
} | ||
} | ||
// reusable parameter_block | ||
var parameter_block = new Uint8Array([ | ||
0, 0, 0, 0, // 0: outlen, keylen, fanout, depth | ||
0, 0, 0, 0, // 4: leaf length, sequential mode | ||
0, 0, 0, 0, // 8: node offset | ||
0, 0, 0, 0, // 12: node offset | ||
0, 0, 0, 0, // 16: node depth, inner length, rfu | ||
0, 0, 0, 0, // 20: rfu | ||
0, 0, 0, 0, // 24: rfu | ||
0, 0, 0, 0, // 28: rfu | ||
0, 0, 0, 0, // 32: salt | ||
0, 0, 0, 0, // 36: salt | ||
0, 0, 0, 0, // 40: salt | ||
0, 0, 0, 0, // 44: salt | ||
0, 0, 0, 0, // 48: personal | ||
0, 0, 0, 0, // 52: personal | ||
0, 0, 0, 0, // 56: personal | ||
0, 0, 0, 0 // 60: personal | ||
]); | ||
// Creates a BLAKE2b hashing context | ||
// Requires an output length between 1 and 64 bytes | ||
// Takes an optional Uint8Array key | ||
function Blake2b (outlen, key, salt, personal) { | ||
// zero out parameter_block before usage | ||
parameter_block.fill(0); | ||
// state, 'param block' | ||
this.b = new Uint8Array(128); | ||
this.h = new Uint32Array(16); | ||
this.t = 0; // input count | ||
this.c = 0; // pointer within buffer | ||
this.outlen = outlen; // output length in bytes | ||
parameter_block[0] = outlen; | ||
if (key) parameter_block[1] = key.length; | ||
parameter_block[2] = 1; // fanout | ||
parameter_block[3] = 1; // depth | ||
if (salt) parameter_block.set(salt, 32); | ||
if (personal) parameter_block.set(personal, 48); | ||
// initialize hash state | ||
for (var i = 0; i < 16; i++) { | ||
this.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameter_block, i * 4); | ||
} | ||
// key the hash, if applicable | ||
if (key) { | ||
blake2bUpdate(this, key); | ||
// at the end | ||
this.c = 128; | ||
} | ||
} | ||
Blake2b.prototype.update = function (input) { | ||
assert(input instanceof Uint8Array, 'input must be Uint8Array or Buffer'); | ||
blake2bUpdate(this, input); | ||
return this | ||
}; | ||
Blake2b.prototype.digest = function (out) { | ||
var buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(this.outlen) : out; | ||
assert(buf instanceof Uint8Array, 'out must be "binary", "hex", Uint8Array, or Buffer'); | ||
assert(buf.length >= this.outlen, 'out must have at least outlen bytes of space'); | ||
blake2bFinal(this, buf); | ||
if (out === 'hex') return hexSlice(buf) | ||
return buf | ||
}; | ||
Blake2b.prototype.final = Blake2b.prototype.digest; | ||
Blake2b.ready = function (cb) { | ||
b2wasm.ready(function () { | ||
cb(); // ignore the error | ||
}); | ||
}; | ||
// Updates a BLAKE2b streaming hash | ||
// Requires hash context and Uint8Array (byte array) | ||
function blake2bUpdate (ctx, input) { | ||
for (var i = 0; i < input.length; i++) { | ||
if (ctx.c === 128) { // buffer full ? | ||
ctx.t += ctx.c; // add counters | ||
blake2bCompress(ctx, false); // compress (not last) | ||
ctx.c = 0; // counter to zero | ||
} | ||
ctx.b[ctx.c++] = input[i]; | ||
} | ||
} | ||
// Completes a BLAKE2b streaming hash | ||
// Returns a Uint8Array containing the message digest | ||
function blake2bFinal (ctx, out) { | ||
ctx.t += ctx.c; // mark last block offset | ||
while (ctx.c < 128) { // fill up with zeros | ||
ctx.b[ctx.c++] = 0; | ||
} | ||
blake2bCompress(ctx, true); // final block flag = 1 | ||
for (var i = 0; i < ctx.outlen; i++) { | ||
out[i] = ctx.h[i >> 2] >> (8 * (i & 3)); | ||
} | ||
return out | ||
} | ||
function hexSlice (buf) { | ||
var str = ''; | ||
for (var i = 0; i < buf.length; i++) str += toHex(buf[i]); | ||
return str | ||
} | ||
function toHex (n) { | ||
if (n < 16) return '0' + n.toString(16) | ||
return n.toString(16) | ||
} | ||
var Proto = Blake2b; | ||
blake2b$1.exports = function createHash (outlen, key, salt, personal, noAssert) { | ||
if (noAssert !== true) { | ||
assert(outlen >= BYTES_MIN, 'outlen must be at least ' + BYTES_MIN + ', was given ' + outlen); | ||
assert(outlen <= BYTES_MAX, 'outlen must be at most ' + BYTES_MAX + ', was given ' + outlen); | ||
if (key != null) { | ||
assert(key instanceof Uint8Array, 'key must be Uint8Array or Buffer'); | ||
assert(key.length >= KEYBYTES_MIN, 'key must be at least ' + KEYBYTES_MIN + ', was given ' + key.length); | ||
assert(key.length <= KEYBYTES_MAX, 'key must be at most ' + KEYBYTES_MAX + ', was given ' + key.length); | ||
} | ||
if (salt != null) { | ||
assert(salt instanceof Uint8Array, 'salt must be Uint8Array or Buffer'); | ||
assert(salt.length === SALTBYTES, 'salt must be exactly ' + SALTBYTES + ', was given ' + salt.length); | ||
} | ||
if (personal != null) { | ||
assert(personal instanceof Uint8Array, 'personal must be Uint8Array or Buffer'); | ||
assert(personal.length === PERSONALBYTES, 'personal must be exactly ' + PERSONALBYTES + ', was given ' + personal.length); | ||
} | ||
} | ||
return new Proto(outlen, key, salt, personal) | ||
}; | ||
blake2b$1.exports.ready = function (cb) { | ||
b2wasm.ready(function () { // ignore errors | ||
cb(); | ||
}); | ||
}; | ||
blake2b$1.exports.WASM_SUPPORTED = b2wasm.SUPPORTED; | ||
blake2b$1.exports.WASM_LOADED = false; | ||
var BYTES_MIN = blake2b$1.exports.BYTES_MIN = 16; | ||
var BYTES_MAX = blake2b$1.exports.BYTES_MAX = 64; | ||
blake2b$1.exports.BYTES = 32; | ||
var KEYBYTES_MIN = blake2b$1.exports.KEYBYTES_MIN = 16; | ||
var KEYBYTES_MAX = blake2b$1.exports.KEYBYTES_MAX = 64; | ||
blake2b$1.exports.KEYBYTES = 32; | ||
var SALTBYTES = blake2b$1.exports.SALTBYTES = 16; | ||
var PERSONALBYTES = blake2b$1.exports.PERSONALBYTES = 16; | ||
b2wasm.ready(function (err) { | ||
if (!err) { | ||
blake2b$1.exports.WASM_LOADED = true; | ||
blake2b$1.exports = b2wasm; | ||
} | ||
}); | ||
const FIRST_BIT_SET = 0b10_00_00_00; | ||
@@ -304,4 +1033,2 @@ const FIRST_BIT_SET_NEG = 0b01_11_11_11; | ||
/** Regular hash length */ | ||
const HASH_BYTES = 32; | ||
/** Value nodes have the key truncated to 31 bytes. */ | ||
@@ -311,4 +1038,4 @@ const TRUNCATED_KEY_BYTES = 31; | ||
function parseInputKey(v) { | ||
if (v.length === HASH_BYTES * 2) { | ||
return Bytes.parseBytesNoPrefix(v, HASH_BYTES).asOpaque(); | ||
if (v.length === HASH_SIZE * 2) { | ||
return Bytes.parseBytesNoPrefix(v, HASH_SIZE).asOpaque(); | ||
} | ||
@@ -398,3 +1125,3 @@ return Bytes.parseBytesNoPrefix(v, TRUNCATED_KEY_BYTES).asOpaque(); | ||
node.data.set(left.raw, 0); | ||
node.data.set(right.raw, HASH_BYTES); | ||
node.data.set(right.raw, HASH_SIZE); | ||
// set the first bit to 0 (branch node) | ||
@@ -406,7 +1133,7 @@ node.data[0] &= FIRST_BIT_SET_NEG; | ||
getLeft() { | ||
return Bytes.fromBlob(this.node.data.subarray(0, HASH_BYTES), HASH_BYTES).asOpaque(); | ||
return Bytes.fromBlob(this.node.data.subarray(0, HASH_SIZE), HASH_SIZE).asOpaque(); | ||
} | ||
/** Get the hash of the right sub-trie. */ | ||
getRight() { | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_BYTES), HASH_BYTES).asOpaque(); | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_SIZE), HASH_SIZE).asOpaque(); | ||
} | ||
@@ -439,3 +1166,3 @@ } | ||
// The value will fit in the leaf itself. | ||
if (value.length <= HASH_BYTES) { | ||
if (value.length <= HASH_SIZE) { | ||
node.data[0] = FIRST_BIT_SET | value.length; | ||
@@ -468,3 +1195,3 @@ // truncate & copy the key | ||
const firstByte = this.node.data[0]; | ||
// we only store values up to `HASH_BYTES`, so they fit on the last 6 bits. | ||
// we only store values up to `HASH_SIZE`, so they fit on the last 6 bits. | ||
return firstByte & FIRST_TWO_BITS_SET_NEG; | ||
@@ -480,3 +1207,3 @@ } | ||
const len = this.getValueLength(); | ||
return BytesBlob.blobFrom(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
return BytesBlob.blobFrom(this.node.data.subarray(HASH_SIZE, HASH_SIZE + len)); | ||
} | ||
@@ -490,3 +1217,3 @@ /** | ||
getValueHash() { | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_BYTES), HASH_BYTES).asOpaque(); | ||
return Bytes.fromBlob(this.node.data.subarray(HASH_SIZE), HASH_SIZE).asOpaque(); | ||
} | ||
@@ -519,5 +1246,9 @@ } | ||
} | ||
/** Remove the key and any value from the dictionary. */ | ||
/** | ||
* Remove the key and any value from the dictionary. | ||
* | ||
* Returns `true` if element existed and was removed, `false` otherwise. | ||
*/ | ||
delete(key) { | ||
this.map.delete(key.toString()); | ||
return this.map.delete(key.toString()); | ||
} | ||
@@ -621,3 +1352,3 @@ /** it allows to use HashDictionary in for-of loop */ | ||
if (this.root === null) { | ||
return Bytes.zero(HASH_BYTES).asOpaque(); | ||
return Bytes.zero(HASH_SIZE).asOpaque(); | ||
} | ||
@@ -704,3 +1435,3 @@ return this.nodes.hashNode(this.root); | ||
if (nextNode === null) { | ||
if (nextHash.isEqualTo(Bytes.zero(HASH_BYTES))) { | ||
if (nextHash.isEqualTo(Bytes.zero(HASH_SIZE))) { | ||
return traversedPath; | ||
@@ -752,3 +1483,3 @@ } | ||
// Now construct the common branches, and insert zero hash in place of other sub-trees. | ||
const zero = Bytes.zero(HASH_BYTES).asOpaque(); | ||
const zero = Bytes.zero(HASH_SIZE).asOpaque(); | ||
// In case we move the leaf from left to right it's hash needs to be re-calculated (missing bit). | ||
@@ -755,0 +1486,0 @@ // TODO [ToDr] [opti] might be better to store the original bit value instead of recalculating. |
{ | ||
"name": "@typeberry/trie", | ||
"version": "0.0.1-dd62eff", | ||
"version": "0.0.1-f8aea68", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": "Fluffy Labs", |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
83316
2063
0
2