@typeberry/trie
Advanced tools
Comparing version 0.0.1-9949ea3 to 0.0.1-9fbd0e8
166
index.d.ts
@@ -21,3 +21,4 @@ /** | ||
declare const __OPAQUE_TYPE__: unique symbol; | ||
// TODO [MaSi]: it should be "unique symbol" but in debugger adapter we have opaque types from different packages and it is problematic. | ||
declare const __OPAQUE_TYPE__ = "__INTERNAL_OPAQUE_ID__"; | ||
@@ -37,7 +38,7 @@ type WithOpaque<Token extends string> = { | ||
declare class BytesBlob { | ||
readonly buffer: Uint8Array = new Uint8Array([]); | ||
readonly raw: Uint8Array; | ||
readonly length: number = 0; | ||
protected constructor(data: Uint8Array) { | ||
this.buffer = data; | ||
this.raw = data; | ||
this.length = data.byteLength; | ||
@@ -50,3 +51,3 @@ } | ||
toString() { | ||
return bytesToHexString(this.buffer); | ||
return bytesToHexString(this.raw); | ||
} | ||
@@ -57,3 +58,3 @@ | ||
const decoder = new TextDecoder(); | ||
return decoder.decode(this.buffer); | ||
return decoder.decode(this.raw); | ||
} | ||
@@ -72,4 +73,17 @@ | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.buffer[i] !== other.buffer[i]) { | ||
return u8ArraySameLengthEqual(this.raw, other.raw); | ||
} | ||
/** Compare the sequence to another one lexicographically. */ | ||
isLessThan(other: BytesBlob): boolean { | ||
const min = Math.min(this.length, other.length); | ||
const thisRaw = this.raw; | ||
const otherRaw = other.raw; | ||
for (let i = 0; i < min; i++) { | ||
if (thisRaw[i] < otherRaw[i]) { | ||
return true; | ||
} | ||
if (thisRaw[i] > otherRaw[i]) { | ||
return false; | ||
@@ -79,13 +93,13 @@ } | ||
return true; | ||
return this.length < other.length; | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
static fromString(v: string): BytesBlob { | ||
static blobFromString(v: string): BytesBlob { | ||
const encoder = new TextEncoder(); | ||
return BytesBlob.from(encoder.encode(v)); | ||
return BytesBlob.blobFrom(encoder.encode(v)); | ||
} | ||
/** Create a new [`BytesBlob`] from existing [`Uint8Array`]. */ | ||
static from(v: Uint8Array): BytesBlob { | ||
static blobFrom(v: Uint8Array): BytesBlob { | ||
return new BytesBlob(v); | ||
@@ -95,3 +109,3 @@ } | ||
/** Create a new [`BytesBlob`] by concatenating data from multiple `Uint8Array`s. */ | ||
static copyFromBlobs(v: Uint8Array, ...rest: Uint8Array[]) { | ||
static blobFromParts(v: Uint8Array, ...rest: Uint8Array[]) { | ||
const totalLength = v.length + rest.reduce((a, v) => a + v.length, 0); | ||
@@ -109,4 +123,4 @@ const buffer = new Uint8Array(totalLength); | ||
/** Create a new [`BytesBlob`] from an array of bytes. */ | ||
static fromNumbers(v: number[]): BytesBlob { | ||
check(v.find((x) => (x & 0xff) !== x) === undefined, "BytesBlob.fromNumbers used with non-byte number array."); | ||
static blobFromNumbers(v: number[]): BytesBlob { | ||
check(v.find((x) => (x & 0xff) !== x) === undefined, "BytesBlob.blobFromNumbers used with non-byte number array."); | ||
const arr = new Uint8Array(v); | ||
@@ -154,7 +168,2 @@ return new BytesBlob(arr); | ||
/** Raw bytes array. */ | ||
get raw(): Uint8Array { | ||
return this.buffer; | ||
} | ||
/** Create new [`Bytes<X>`] given a backing buffer and it's length. */ | ||
@@ -165,2 +174,9 @@ static fromBlob<X extends number>(v: Uint8Array, len: X): Bytes<X> { | ||
/** Create new [`Bytes<X>`] given an array of bytes and it's length. */ | ||
static fromNumbers<X extends number>(v: number[], len: X): Bytes<X> { | ||
check(v.find((x) => (x & 0xff) !== x) === undefined, "Bytes.fromNumbers used with non-byte number array."); | ||
const x = new Uint8Array(v); | ||
return new Bytes(x, len); | ||
} | ||
/** Create an empty [`Bytes<X>`] of given length. */ | ||
@@ -186,3 +202,3 @@ static zero<X extends number>(len: X): Bytes<X> { | ||
const blob = BytesBlob.parseBlobNoPrefix(v); | ||
return new Bytes(blob.buffer, len); | ||
return new Bytes(blob.raw, len); | ||
} | ||
@@ -197,4 +213,10 @@ | ||
const blob = BytesBlob.parseBlob(v); | ||
return new Bytes(blob.buffer, len); | ||
return new Bytes(blob.raw, len); | ||
} | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other: Bytes<T>): boolean { | ||
check(this.length === other.length, "Comparing incorrectly typed bytes!"); | ||
return u8ArraySameLengthEqual(this.raw, other.raw); | ||
} | ||
} | ||
@@ -240,3 +262,3 @@ | ||
/** Return a raw in-memory representation of this [`BitVec`]. */ | ||
raw(): Uint8Array { | ||
get raw(): Uint8Array { | ||
return this.data.subarray(0, this.byteLength); | ||
@@ -276,3 +298,3 @@ } | ||
* | ||
* https://graypaper.fluffylabs.dev/#/387103d/071401071f01 | ||
* https://graypaper.fluffylabs.dev/#/579bd12/073101073c01 | ||
* | ||
@@ -288,7 +310,10 @@ */ | ||
type StateKey = Opaque<OpaqueHash, "stateKey">; | ||
type TruncatedStateKey = Opaque<Bytes<31>, "stateKey">; | ||
type TruncatedStateKey = Opaque<Bytes<TRUNCATED_KEY_BYTES>, "stateKey">; | ||
type InputKey = StateKey | TruncatedStateKey; | ||
/** | ||
* A state commitment. | ||
* | ||
* https://graypaper.fluffylabs.dev/#/387103d/0cb0000cb400 | ||
* https://graypaper.fluffylabs.dev/#/579bd12/0c1f010c2301 | ||
*/ | ||
@@ -298,2 +323,13 @@ type TrieHash = Opaque<OpaqueHash, "trie">; | ||
/** Value nodes have the key truncated to 31 bytes. */ | ||
declare const TRUNCATED_KEY_BYTES = 31; | ||
type TRUNCATED_KEY_BYTES = 31; | ||
declare function parseInputKey(v: string): InputKey { | ||
if (v.length === HASH_SIZE * 2) { | ||
return Bytes.parseBytesNoPrefix(v, HASH_SIZE).asOpaque(); | ||
} | ||
return Bytes.parseBytesNoPrefix(v, TRUNCATED_KEY_BYTES).asOpaque(); | ||
} | ||
/** | ||
@@ -341,11 +377,11 @@ * The kind of the trie node. | ||
getNodeType(): NodeType { | ||
if ((this.data[0] & 0b1) === 0b0) { | ||
if ((this.data[0] & FIRST_BIT_SET) === 0) { | ||
return NodeType.Branch; | ||
} | ||
if ((this.data[0] & 0b11) === 0b11) { | ||
return NodeType.EmbedLeaf; | ||
if ((this.data[0] & FIRST_TWO_BITS_SET) === FIRST_TWO_BITS_SET) { | ||
return NodeType.Leaf; | ||
} | ||
return NodeType.Leaf; | ||
return NodeType.EmbedLeaf; | ||
} | ||
@@ -385,6 +421,6 @@ | ||
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) | ||
node.data[0] &= 0b1111_1110; | ||
node.data[0] &= FIRST_BIT_SET_NEG; | ||
@@ -396,3 +432,3 @@ return new BranchNode(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(); | ||
} | ||
@@ -402,3 +438,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(); | ||
} | ||
@@ -433,14 +469,13 @@ } | ||
static fromValue(key: StateKey, value: BytesBlob, valueHash: TrieHash): LeafNode { | ||
static fromValue(key: InputKey, value: BytesBlob, valueHash: TrieHash): LeafNode { | ||
const node = new TrieNode(); | ||
// The value will fit in the leaf itself. | ||
if (value.length <= HASH_BYTES) { | ||
node.data[0] = value.length << 2; | ||
node.data[0] |= 0b01; | ||
if (value.length <= HASH_SIZE) { | ||
node.data[0] = FIRST_BIT_SET | value.length; | ||
// truncate & copy the key | ||
node.data.set(key.raw.subarray(0, TRUNCATED_KEY_BYTES), 1); | ||
// copy the value | ||
node.data.set(value.buffer, TRUNCATED_KEY_BYTES + 1); | ||
node.data.set(value.raw, TRUNCATED_KEY_BYTES + 1); | ||
} else { | ||
node.data[0] = 0b11; | ||
node.data[0] = FIRST_TWO_BITS_SET; | ||
// truncate & copy the key | ||
@@ -467,4 +502,5 @@ node.data.set(key.raw.subarray(0, TRUNCATED_KEY_BYTES), 1); | ||
getValueLength(): number { | ||
const firstByte = this.node.data[0] >> 2; | ||
return firstByte; | ||
const firstByte = this.node.data[0]; | ||
// 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 +516,3 @@ | ||
const len = this.getValueLength(); | ||
return BytesBlob.from(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
return BytesBlob.blobFrom(this.node.data.subarray(HASH_SIZE, HASH_SIZE + len)); | ||
} | ||
@@ -491,3 +527,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(); | ||
} | ||
@@ -501,3 +537,3 @@ } | ||
// This has to be benchmarked and re-written to a custom map most likely. | ||
private readonly map = new Map<string, V>(); | ||
private readonly map = new Map<string, [K, V]>(); | ||
@@ -516,3 +552,3 @@ /** Return number of items in the dictionary. */ | ||
get(key: K): V | undefined { | ||
return this.map.get(key.toString()); | ||
return this.map.get(key.toString())?.[1]; | ||
} | ||
@@ -522,9 +558,20 @@ | ||
set(key: K, value: V) { | ||
this.map.set(key.toString(), value); | ||
this.map.set(key.toString(), [key, value]); | ||
} | ||
/** 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()); | ||
} | ||
/** it allows to use HashDictionary in for-of loop */ | ||
*[Symbol.iterator]() { | ||
for (const value of this.map.values()) { | ||
yield value; | ||
} | ||
} | ||
} | ||
@@ -572,3 +619,3 @@ | ||
const prevValue = hash.raw[0]; | ||
hash.raw[0] &= 0b1111_1110; | ||
hash.raw[0] &= FIRST_BIT_SET_NEG; | ||
const returnValue = exe(hash); | ||
@@ -602,4 +649,6 @@ // restore the original byte, so that we have correct value in case it | ||
declare class InMemoryTrie { | ||
// Exposed for trie-visualiser | ||
public readonly nodes: WriteableNodesDb; | ||
// TODO [ToDr] Consider using HashDictionary? | ||
private readonly flat: Map<string, BytesBlob> = new Map(); | ||
private readonly nodes: WriteableNodesDb; | ||
private root: TrieNode | null = null; | ||
@@ -615,5 +664,6 @@ | ||
set(key: StateKey, value: BytesBlob, maybeValueHash?: TrieHash) { | ||
this.flat.set(key.toString(), value); | ||
const valueHash = maybeValueHash ?? this.nodes.hasher.hashConcat(value.buffer); | ||
set(key: InputKey, value: BytesBlob, maybeValueHash?: TrieHash) { | ||
const truncatedKey = Bytes.fromBlob(key.raw.subarray(0, TRUNCATED_KEY_BYTES), TRUNCATED_KEY_BYTES); | ||
this.flat.set(truncatedKey.toString(), value); | ||
const valueHash = maybeValueHash ?? this.nodes.hasher.hashConcat(value.raw); | ||
const leafNode = LeafNode.fromValue(key, value, valueHash); | ||
@@ -628,5 +678,9 @@ this.root = trieInsert(this.root, this.nodes, leafNode); | ||
getRoot(): TrieHash { | ||
getRootNode(): TrieNode | null { | ||
return this.root; | ||
} | ||
getRootHash(): TrieHash { | ||
if (this.root === null) { | ||
return Bytes.zero(HASH_BYTES).asOpaque(); | ||
return Bytes.zero(HASH_SIZE).asOpaque(); | ||
} | ||
@@ -642,2 +696,2 @@ | ||
export { BitVec, Bytes, BytesBlob, InMemoryTrie, type StateKey, type TrieHash }; | ||
export { BitVec, BranchNode, Bytes, BytesBlob, InMemoryTrie, type InputKey, LeafNode, NodeType, NodesDb, type StateKey, type TrieHash, type TrieHasher, TrieNode, type TruncatedStateKey, WriteableNodesDb, parseInputKey }; |
858
index.js
@@ -45,5 +45,4 @@ 'use strict'; | ||
constructor(data) { | ||
this.buffer = new Uint8Array([]); | ||
this.length = 0; | ||
this.buffer = data; | ||
this.raw = data; | ||
this.length = data.byteLength; | ||
@@ -55,3 +54,3 @@ } | ||
toString() { | ||
return bytesToHexString(this.buffer); | ||
return bytesToHexString(this.raw); | ||
} | ||
@@ -61,3 +60,3 @@ /** Decode contained bytes as string. */ | ||
const decoder = new TextDecoder(); | ||
return decoder.decode(this.buffer); | ||
return decoder.decode(this.raw); | ||
} | ||
@@ -73,20 +72,30 @@ /** Converts current type into some opaque extension. */ | ||
} | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.buffer[i] !== other.buffer[i]) { | ||
return u8ArraySameLengthEqual(this.raw, other.raw); | ||
} | ||
/** Compare the sequence to another one lexicographically. */ | ||
isLessThan(other) { | ||
const min = Math.min(this.length, other.length); | ||
const thisRaw = this.raw; | ||
const otherRaw = other.raw; | ||
for (let i = 0; i < min; i++) { | ||
if (thisRaw[i] < otherRaw[i]) { | ||
return true; | ||
} | ||
if (thisRaw[i] > otherRaw[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
return this.length < other.length; | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
static fromString(v) { | ||
static blobFromString(v) { | ||
const encoder = new TextEncoder(); | ||
return BytesBlob.from(encoder.encode(v)); | ||
return BytesBlob.blobFrom(encoder.encode(v)); | ||
} | ||
/** Create a new [`BytesBlob`] from existing [`Uint8Array`]. */ | ||
static from(v) { | ||
static blobFrom(v) { | ||
return new BytesBlob(v); | ||
} | ||
/** Create a new [`BytesBlob`] by concatenating data from multiple `Uint8Array`s. */ | ||
static copyFromBlobs(v, ...rest) { | ||
static blobFromParts(v, ...rest) { | ||
const totalLength = v.length + rest.reduce((a, v) => a + v.length, 0); | ||
@@ -103,4 +112,4 @@ const buffer = new Uint8Array(totalLength); | ||
/** Create a new [`BytesBlob`] from an array of bytes. */ | ||
static fromNumbers(v) { | ||
check(v.find((x) => (x & 0xff) !== x) === undefined, "BytesBlob.fromNumbers used with non-byte number array."); | ||
static blobFromNumbers(v) { | ||
check(v.find((x) => (x & 0xff) !== x) === undefined, "BytesBlob.blobFromNumbers used with non-byte number array."); | ||
const arr = new Uint8Array(v); | ||
@@ -140,6 +149,2 @@ return new BytesBlob(arr); | ||
} | ||
/** Raw bytes array. */ | ||
get raw() { | ||
return this.buffer; | ||
} | ||
/** Create new [`Bytes<X>`] given a backing buffer and it's length. */ | ||
@@ -149,2 +154,8 @@ static fromBlob(v, len) { | ||
} | ||
/** Create new [`Bytes<X>`] given an array of bytes and it's length. */ | ||
static fromNumbers(v, len) { | ||
check(v.find((x) => (x & 0xff) !== x) === undefined, "Bytes.fromNumbers used with non-byte number array."); | ||
const x = new Uint8Array(v); | ||
return new Bytes(x, len); | ||
} | ||
/** Create an empty [`Bytes<X>`] of given length. */ | ||
@@ -167,3 +178,3 @@ static zero(len) { | ||
const blob = BytesBlob.parseBlobNoPrefix(v); | ||
return new Bytes(blob.buffer, len); | ||
return new Bytes(blob.raw, len); | ||
} | ||
@@ -176,4 +187,9 @@ /** Parse a hex-encoded fixed-length bytes with `0x` prefix. */ | ||
const blob = BytesBlob.parseBlob(v); | ||
return new Bytes(blob.buffer, len); | ||
return new Bytes(blob.raw, len); | ||
} | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other) { | ||
check(this.length === other.length, "Comparing incorrectly typed bytes!"); | ||
return u8ArraySameLengthEqual(this.raw, other.raw); | ||
} | ||
} | ||
@@ -219,2 +235,10 @@ function byteFromString(s) { | ||
} | ||
function u8ArraySameLengthEqual(self, other) { | ||
for (let i = 0; i < self.length; i += 1) { | ||
if (self[i] !== other[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -248,3 +272,3 @@ /** | ||
/** Return a raw in-memory representation of this [`BitVec`]. */ | ||
raw() { | ||
get raw() { | ||
return this.data.subarray(0, this.byteLength); | ||
@@ -279,11 +303,704 @@ } | ||
/** Regular hash length */ | ||
const HASH_BYTES = 32; | ||
/** | ||
* 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/AGABfwADBQQAAQICBQUBAQroBwdNBQZtZW1vcnkCAAxibGFrZTJiX2luaXQAAA5ibGFrZTJiX3VwZGF0ZQABDWJsYWtlMmJfZmluYWwAAhBibGFrZTJiX2NvbXByZXNzAAMKvz8EwAIAIABCADcDACAAQgA3AwggAEIANwMQIABCADcDGCAAQgA3AyAgAEIANwMoIABCADcDMCAAQgA3AzggAEIANwNAIABCADcDSCAAQgA3A1AgAEIANwNYIABCADcDYCAAQgA3A2ggAEIANwNwIABCADcDeCAAQoi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whAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgGXx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBp8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByAgfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggHnx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIBd8fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiASfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgHXx8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBF8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByATfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggHHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBh8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAWfHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFHx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgISAhKQMAIAEgCYWFNwMAICIgIikDACACIAqFhTcDACAjICMpAwAgAyALhYU3AwAgJCAkKQMAIAQgDIWFNwMAICUgJSkDACAFIA2FhTcDACAmICYpAwAgBiAOhYU3AwAgJyAnKQMAIAcgD4WFNwMAICggKCkDACAIIBCFhTcDAAs="); | ||
} | ||
}); | ||
// 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; | ||
const FIRST_BIT_SET_NEG = 0b01_11_11_11; | ||
const FIRST_TWO_BITS_SET = 0b11_00_00_00; | ||
const FIRST_TWO_BITS_SET_NEG = 0b00_11_11_11; | ||
/** Value nodes have the key truncated to 31 bytes. */ | ||
const TRUNCATED_KEY_BYTES = 31; | ||
const TRUNCATED_KEY_BITS = TRUNCATED_KEY_BYTES * 8; | ||
function parseInputKey(v) { | ||
if (v.length === HASH_SIZE * 2) { | ||
return Bytes.parseBytesNoPrefix(v, HASH_SIZE).asOpaque(); | ||
} | ||
return Bytes.parseBytesNoPrefix(v, TRUNCATED_KEY_BYTES).asOpaque(); | ||
} | ||
/** | ||
* The kind of the trie node. | ||
*/ | ||
var NodeType; | ||
exports.NodeType = void 0; | ||
(function (NodeType) { | ||
@@ -296,3 +1013,3 @@ /** Branch node (left & right subtree hashes) */ | ||
NodeType[NodeType["EmbedLeaf"] = 2] = "EmbedLeaf"; | ||
})(NodeType || (NodeType = {})); | ||
})(exports.NodeType || (exports.NodeType = {})); | ||
/** | ||
@@ -328,13 +1045,13 @@ * A representation of an unidentified raw trie node. | ||
getNodeType() { | ||
if ((this.data[0] & 0b1) === 0b0) { | ||
return NodeType.Branch; | ||
if ((this.data[0] & FIRST_BIT_SET) === 0) { | ||
return exports.NodeType.Branch; | ||
} | ||
if ((this.data[0] & 0b11) === 0b11) { | ||
return NodeType.EmbedLeaf; | ||
if ((this.data[0] & FIRST_TWO_BITS_SET) === FIRST_TWO_BITS_SET) { | ||
return exports.NodeType.Leaf; | ||
} | ||
return NodeType.Leaf; | ||
return exports.NodeType.EmbedLeaf; | ||
} | ||
/** View this node as a branch node */ | ||
asBranchNode() { | ||
check(this.getNodeType() === NodeType.Branch); | ||
check(this.getNodeType() === exports.NodeType.Branch); | ||
return new BranchNode(this); | ||
@@ -344,3 +1061,3 @@ } | ||
asLeafNode() { | ||
check(this.getNodeType() !== NodeType.Branch); | ||
check(this.getNodeType() !== exports.NodeType.Branch); | ||
return new LeafNode(this); | ||
@@ -369,5 +1086,5 @@ } | ||
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) | ||
node.data[0] &= 0b1111_1110; | ||
node.data[0] &= FIRST_BIT_SET_NEG; | ||
return new BranchNode(node); | ||
@@ -377,7 +1094,7 @@ } | ||
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(); | ||
} | ||
@@ -410,12 +1127,11 @@ } | ||
// The value will fit in the leaf itself. | ||
if (value.length <= HASH_BYTES) { | ||
node.data[0] = value.length << 2; | ||
node.data[0] |= 0b01; | ||
if (value.length <= HASH_SIZE) { | ||
node.data[0] = FIRST_BIT_SET | value.length; | ||
// truncate & copy the key | ||
node.data.set(key.raw.subarray(0, TRUNCATED_KEY_BYTES), 1); | ||
// copy the value | ||
node.data.set(value.buffer, TRUNCATED_KEY_BYTES + 1); | ||
node.data.set(value.raw, TRUNCATED_KEY_BYTES + 1); | ||
} | ||
else { | ||
node.data[0] = 0b11; | ||
node.data[0] = FIRST_TWO_BITS_SET; | ||
// truncate & copy the key | ||
@@ -439,4 +1155,5 @@ node.data.set(key.raw.subarray(0, TRUNCATED_KEY_BYTES), 1); | ||
getValueLength() { | ||
const firstByte = this.node.data[0] >> 2; | ||
return firstByte; | ||
const firstByte = this.node.data[0]; | ||
// we only store values up to `HASH_SIZE`, so they fit on the last 6 bits. | ||
return firstByte & FIRST_TWO_BITS_SET_NEG; | ||
} | ||
@@ -451,3 +1168,3 @@ /** | ||
const len = this.getValueLength(); | ||
return BytesBlob.from(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
return BytesBlob.blobFrom(this.node.data.subarray(HASH_SIZE, HASH_SIZE + len)); | ||
} | ||
@@ -461,3 +1178,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(); | ||
} | ||
@@ -484,12 +1201,22 @@ } | ||
get(key) { | ||
return this.map.get(key.toString()); | ||
return this.map.get(key.toString())?.[1]; | ||
} | ||
/** Insert/overwrite the value at given key. */ | ||
set(key, value) { | ||
this.map.set(key.toString(), value); | ||
this.map.set(key.toString(), [key, value]); | ||
} | ||
/** 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()); | ||
} | ||
/** it allows to use HashDictionary in for-of loop */ | ||
*[Symbol.iterator]() { | ||
for (const value of this.map.values()) { | ||
yield value; | ||
} | ||
} | ||
} | ||
@@ -534,3 +1261,3 @@ | ||
const prevValue = hash.raw[0]; | ||
hash.raw[0] &= 0b1111_1110; | ||
hash.raw[0] &= FIRST_BIT_SET_NEG; | ||
const returnValue = exe(hash); | ||
@@ -566,2 +1293,3 @@ // restore the original byte, so that we have correct value in case it | ||
constructor(nodes) { | ||
// TODO [ToDr] Consider using HashDictionary? | ||
this.flat = new Map(); | ||
@@ -572,4 +1300,5 @@ this.root = null; | ||
set(key, value, maybeValueHash) { | ||
this.flat.set(key.toString(), value); | ||
const valueHash = maybeValueHash ?? this.nodes.hasher.hashConcat(value.buffer); | ||
const truncatedKey = Bytes.fromBlob(key.raw.subarray(0, TRUNCATED_KEY_BYTES), TRUNCATED_KEY_BYTES); | ||
this.flat.set(truncatedKey.toString(), value); | ||
const valueHash = maybeValueHash ?? this.nodes.hasher.hashConcat(value.raw); | ||
const leafNode = LeafNode.fromValue(key, value, valueHash); | ||
@@ -582,5 +1311,8 @@ this.root = trieInsert(this.root, this.nodes, leafNode); | ||
} | ||
getRoot() { | ||
getRootNode() { | ||
return this.root; | ||
} | ||
getRootHash() { | ||
if (this.root === null) { | ||
return Bytes.zero(HASH_BYTES).asOpaque(); | ||
return Bytes.zero(HASH_SIZE).asOpaque(); | ||
} | ||
@@ -654,3 +1386,3 @@ return this.nodes.hashNode(this.root); | ||
const kind = currentNode.getNodeType(); | ||
if (kind !== NodeType.Branch) { | ||
if (kind !== exports.NodeType.Branch) { | ||
// we found a leaf that needs to be merged with the one being inserted. | ||
@@ -668,3 +1400,3 @@ const leaf = currentNode.asLeafNode(); | ||
if (nextNode === null) { | ||
if (nextHash.isEqualTo(Bytes.zero(HASH_BYTES))) { | ||
if (nextHash.isEqualTo(Bytes.zero(HASH_SIZE))) { | ||
return traversedPath; | ||
@@ -716,3 +1448,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). | ||
@@ -749,7 +1481,7 @@ // TODO [ToDr] [opti] might be better to store the original bit value instead of recalculating. | ||
check(bitIndex < TRUNCATED_KEY_BITS); | ||
const byte = bitIndex >> 3; | ||
const byte = bitIndex >>> 3; | ||
const bit = bitIndex - (byte << 3); | ||
const mask = 1 << bit; | ||
const mask = 0b10_00_00_00 >>> bit; | ||
const val = key.raw[byte] & mask; | ||
return val > 0; | ||
return val !== 0; | ||
} | ||
@@ -761,3 +1493,3 @@ function trieStringify(root, nodes) { | ||
const kind = root.getNodeType(); | ||
if (kind === NodeType.Branch) { | ||
if (kind === exports.NodeType.Branch) { | ||
const branch = root.asBranchNode(); | ||
@@ -784,4 +1516,10 @@ const leftHash = branch.getLeft(); | ||
exports.BitVec = BitVec; | ||
exports.BranchNode = BranchNode; | ||
exports.Bytes = Bytes; | ||
exports.BytesBlob = BytesBlob; | ||
exports.InMemoryTrie = InMemoryTrie; | ||
exports.LeafNode = LeafNode; | ||
exports.NodesDb = NodesDb; | ||
exports.TrieNode = TrieNode; | ||
exports.WriteableNodesDb = WriteableNodesDb; | ||
exports.parseInputKey = parseInputKey; |
{ | ||
"name": "@typeberry/trie", | ||
"version": "0.0.1-9949ea3", | ||
"version": "0.0.1-9fbd0e8", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": "Fluffy Labs", |
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
80445
1977
0