langsmith 0.4.11 → 0.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.cjs +5 -2
- package/dist/client.d.ts +5 -1
- package/dist/client.js +5 -2
- package/dist/evaluation/evaluate_comparative.cjs +5 -0
- package/dist/evaluation/evaluate_comparative.js +5 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/run_trees.cjs +7 -6
- package/dist/run_trees.js +8 -7
- package/dist/schemas.d.ts +1 -0
- package/dist/utils/_uuid.cjs +113 -0
- package/dist/utils/_uuid.d.ts +26 -0
- package/dist/utils/_uuid.js +112 -0
- package/dist/utils/xxhash/xxhash.cjs +331 -0
- package/dist/utils/xxhash/xxhash.d.ts +15 -0
- package/dist/utils/xxhash/xxhash.js +327 -0
- package/dist/wrappers/anthropic.cjs +17 -4
- package/dist/wrappers/anthropic.js +17 -4
- package/dist/wrappers/gemini.cjs +16 -5
- package/dist/wrappers/gemini.js +16 -5
- package/dist/wrappers/openai.cjs +27 -12
- package/dist/wrappers/openai.js +27 -12
- package/package.json +1 -1
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.XXH3_128 = XXH3_128;
|
|
4
|
+
exports.xxh128ToBytes = xxh128ToBytes;
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
6
|
+
// @ts-nocheck
|
|
7
|
+
// Original from: https://github.com/i404788/xxh3-ts
|
|
8
|
+
// Vendored for compatibility (remove Buffer use in favor of Uint8Array)
|
|
9
|
+
const n = (n) => BigInt(n);
|
|
10
|
+
const PRIME32_1 = n("0x9E3779B1"); // 0b10011110001101110111100110110001
|
|
11
|
+
const PRIME32_2 = n("0x85EBCA77"); // 0b10000101111010111100101001110111
|
|
12
|
+
const PRIME32_3 = n("0xC2B2AE3D"); // 0b11000010101100101010111000111101
|
|
13
|
+
const PRIME64_1 = n("0x9E3779B185EBCA87"); // 0b1001111000110111011110011011000110000101111010111100101010000111
|
|
14
|
+
const PRIME64_2 = n("0xC2B2AE3D27D4EB4F"); // 0b1100001010110010101011100011110100100111110101001110101101001111
|
|
15
|
+
const PRIME64_3 = n("0x165667B19E3779F9"); // 0b0001011001010110011001111011000110011110001101110111100111111001
|
|
16
|
+
const PRIME64_4 = n("0x85EBCA77C2B2AE63"); // 0b1000010111101011110010100111011111000010101100101010111001100011
|
|
17
|
+
const PRIME64_5 = n("0x27D4EB2F165667C5"); // 0b0010011111010100111010110010111100010110010101100110011111000101
|
|
18
|
+
const PRIME_MX1 = n("0x165667919E3779F9"); // 0b0001011001010110011001111001000110011110001101110111100111111001
|
|
19
|
+
const PRIME_MX2 = n("0x9FB21C651E98DF25"); // 0b1001111110110010000111000110010100011110100110001101111100100101
|
|
20
|
+
// Convert hex string to Uint8Array
|
|
21
|
+
function hexToBytes(hex) {
|
|
22
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
23
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
24
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
25
|
+
}
|
|
26
|
+
return bytes;
|
|
27
|
+
}
|
|
28
|
+
const kkey = hexToBytes("b8fe6c3923a44bbe7c01812cf721ad1cded46de9839097db7240a4a4b7b3671fcb79e64eccc0e578825ad07dccff7221b8084674f743248ee03590e6813a264c3c2852bb91c300cb88d0658b1b532ea371644897a20df94e3819ef46a9deacd8a8fa763fe39c343ff9dcbbc7c70b4f1d8a51e04bcdb45931c89f7ec9d9787364eac5ac8334d3ebc3c581a0fffa1363eb170ddd51b7f0da49d316552629d4689e2b16be587d47a1fc8ff8b8d17ad031ce45cb3a8f95160428afd7fbcabb4b407e");
|
|
29
|
+
const mask128 = (n(1) << n(128)) - n(1);
|
|
30
|
+
const mask64 = (n(1) << n(64)) - n(1);
|
|
31
|
+
const mask32 = (n(1) << n(32)) - n(1);
|
|
32
|
+
const STRIPE_LEN = 64;
|
|
33
|
+
const ACC_NB = STRIPE_LEN / 8;
|
|
34
|
+
const _U64 = 8;
|
|
35
|
+
const _U32 = 4;
|
|
36
|
+
// Create a view into a Uint8Array at an offset
|
|
37
|
+
function getView(buf, offset = 0) {
|
|
38
|
+
return new Uint8Array(buf.buffer, buf.byteOffset + offset, buf.length - offset);
|
|
39
|
+
}
|
|
40
|
+
// Read BigInt from Uint8Array in little-endian
|
|
41
|
+
function readBigUInt64LE(buf, offset = 0) {
|
|
42
|
+
const view = new DataView(buf.buffer, buf.byteOffset + offset);
|
|
43
|
+
return view.getBigUint64(0, true);
|
|
44
|
+
}
|
|
45
|
+
// Read UInt32 from Uint8Array in little-endian
|
|
46
|
+
function readUInt32LE(buf, offset = 0) {
|
|
47
|
+
const view = new DataView(buf.buffer, buf.byteOffset + offset);
|
|
48
|
+
return view.getUint32(0, true);
|
|
49
|
+
}
|
|
50
|
+
// Read UInt8 from Uint8Array
|
|
51
|
+
function readUInt8(buf, offset = 0) {
|
|
52
|
+
return buf[offset];
|
|
53
|
+
}
|
|
54
|
+
const bswap64 = (a) => {
|
|
55
|
+
return (((a & n(0xff)) << n(56)) |
|
|
56
|
+
((a & n(0xff00)) << n(40)) |
|
|
57
|
+
((a & n(0xff0000)) << n(24)) |
|
|
58
|
+
((a & n(0xff000000)) << n(8)) |
|
|
59
|
+
((a & n(0xff00000000)) >> n(8)) |
|
|
60
|
+
((a & n(0xff0000000000)) >> n(24)) |
|
|
61
|
+
((a & n(0xff000000000000)) >> n(40)) |
|
|
62
|
+
((a & n(0xff00000000000000)) >> n(56)));
|
|
63
|
+
};
|
|
64
|
+
const bswap32 = (a) => {
|
|
65
|
+
a = ((a & n(0x0000ffff)) << n(16)) | ((a & n(0xffff0000)) >> n(16));
|
|
66
|
+
a = ((a & n(0x00ff00ff)) << n(8)) | ((a & n(0xff00ff00)) >> n(8));
|
|
67
|
+
return a;
|
|
68
|
+
};
|
|
69
|
+
const XXH_mult32to64 = (a, b) => ((a & mask32) * (b & mask32)) & mask64;
|
|
70
|
+
const assert = (a) => {
|
|
71
|
+
if (!a)
|
|
72
|
+
throw new Error("Assert failed");
|
|
73
|
+
};
|
|
74
|
+
function rotl32(a, b) {
|
|
75
|
+
return ((a << b) | (a >> (n(32) - b))) & mask32;
|
|
76
|
+
}
|
|
77
|
+
function XXH3_accumulate_512(acc, data, key) {
|
|
78
|
+
for (let i = 0; i < ACC_NB; i++) {
|
|
79
|
+
const data_val = readBigUInt64LE(data, i * 8);
|
|
80
|
+
const data_key = data_val ^ readBigUInt64LE(key, i * 8);
|
|
81
|
+
acc[i ^ 1] += data_val;
|
|
82
|
+
acc[i] += XXH_mult32to64(data_key, data_key >> n(32));
|
|
83
|
+
}
|
|
84
|
+
return acc;
|
|
85
|
+
}
|
|
86
|
+
function XXH3_accumulate(acc, data, key, nbStripes) {
|
|
87
|
+
for (let n = 0; n < nbStripes; n++) {
|
|
88
|
+
XXH3_accumulate_512(acc, getView(data, n * STRIPE_LEN), getView(key, n * 8));
|
|
89
|
+
}
|
|
90
|
+
return acc;
|
|
91
|
+
}
|
|
92
|
+
function XXH3_scrambleAcc(acc, key) {
|
|
93
|
+
for (let i = 0; i < ACC_NB; i++) {
|
|
94
|
+
const key64 = readBigUInt64LE(key, i * 8);
|
|
95
|
+
let acc64 = acc[i];
|
|
96
|
+
acc64 = xorshift64(acc64, n(47));
|
|
97
|
+
acc64 ^= key64;
|
|
98
|
+
acc64 *= PRIME32_1;
|
|
99
|
+
acc[i] = acc64 & mask64;
|
|
100
|
+
}
|
|
101
|
+
return acc;
|
|
102
|
+
}
|
|
103
|
+
function XXH3_mix2Accs(acc, key) {
|
|
104
|
+
return XXH3_mul128_fold64(acc[0] ^ readBigUInt64LE(key, 0), acc[1] ^ readBigUInt64LE(key, _U64));
|
|
105
|
+
}
|
|
106
|
+
function XXH3_mergeAccs(acc, key, start) {
|
|
107
|
+
let result64 = start;
|
|
108
|
+
result64 += XXH3_mix2Accs(acc.slice(0), getView(key, 0 * _U32));
|
|
109
|
+
result64 += XXH3_mix2Accs(acc.slice(2), getView(key, 4 * _U32));
|
|
110
|
+
result64 += XXH3_mix2Accs(acc.slice(4), getView(key, 8 * _U32));
|
|
111
|
+
result64 += XXH3_mix2Accs(acc.slice(6), getView(key, 12 * _U32));
|
|
112
|
+
return XXH3_avalanche(result64 & mask64);
|
|
113
|
+
}
|
|
114
|
+
function XXH3_hashLong(acc, data, secret, f_acc, f_scramble) {
|
|
115
|
+
const nbStripesPerBlock = Math.floor((secret.byteLength - STRIPE_LEN) / 8);
|
|
116
|
+
const block_len = STRIPE_LEN * nbStripesPerBlock;
|
|
117
|
+
const nb_blocks = Math.floor((data.byteLength - 1) / block_len);
|
|
118
|
+
for (let n = 0; n < nb_blocks; n++) {
|
|
119
|
+
acc = XXH3_accumulate(acc, getView(data, n * block_len), secret, nbStripesPerBlock);
|
|
120
|
+
acc = f_scramble(acc, getView(secret, secret.byteLength - STRIPE_LEN));
|
|
121
|
+
}
|
|
122
|
+
{
|
|
123
|
+
// Partial block
|
|
124
|
+
const nbStripes = Math.floor((data.byteLength - 1 - block_len * nb_blocks) / STRIPE_LEN);
|
|
125
|
+
acc = XXH3_accumulate(acc, getView(data, nb_blocks * block_len), secret, nbStripes);
|
|
126
|
+
// Last Stripe
|
|
127
|
+
acc = f_acc(acc, getView(data, data.byteLength - STRIPE_LEN), getView(secret, secret.byteLength - STRIPE_LEN - 7));
|
|
128
|
+
}
|
|
129
|
+
return acc;
|
|
130
|
+
}
|
|
131
|
+
function XXH3_hashLong_128b(data, secret, seed) {
|
|
132
|
+
let acc = new BigUint64Array([
|
|
133
|
+
PRIME32_3,
|
|
134
|
+
PRIME64_1,
|
|
135
|
+
PRIME64_2,
|
|
136
|
+
PRIME64_3,
|
|
137
|
+
PRIME64_4,
|
|
138
|
+
PRIME32_2,
|
|
139
|
+
PRIME64_5,
|
|
140
|
+
PRIME32_1,
|
|
141
|
+
]);
|
|
142
|
+
assert(data.length > 128);
|
|
143
|
+
acc = XXH3_hashLong(acc, data, secret, XXH3_accumulate_512, XXH3_scrambleAcc);
|
|
144
|
+
/* converge into final hash */
|
|
145
|
+
assert(acc.length * 8 == 64);
|
|
146
|
+
{
|
|
147
|
+
const low64 = XXH3_mergeAccs(acc, getView(secret, 11), (n(data.byteLength) * PRIME64_1) & mask64);
|
|
148
|
+
const high64 = XXH3_mergeAccs(acc, getView(secret, secret.byteLength - STRIPE_LEN - 11), ~(n(data.byteLength) * PRIME64_2) & mask64);
|
|
149
|
+
return (high64 << n(64)) | low64;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function XXH3_mul128_fold64(a, b) {
|
|
153
|
+
const lll = (a * b) & mask128;
|
|
154
|
+
return (lll & mask64) ^ (lll >> n(64));
|
|
155
|
+
}
|
|
156
|
+
function XXH3_mix16B(data, key, seed) {
|
|
157
|
+
return XXH3_mul128_fold64((readBigUInt64LE(data, 0) ^ (readBigUInt64LE(key, 0) + seed)) & mask64, (readBigUInt64LE(data, 8) ^ (readBigUInt64LE(key, 8) - seed)) & mask64);
|
|
158
|
+
}
|
|
159
|
+
function XXH3_mix32B(acc, data1, data2, key, seed) {
|
|
160
|
+
let accl = acc & mask64;
|
|
161
|
+
let acch = (acc >> n(64)) & mask64;
|
|
162
|
+
accl += XXH3_mix16B(data1, key, seed);
|
|
163
|
+
accl ^= readBigUInt64LE(data2, 0) + readBigUInt64LE(data2, 8);
|
|
164
|
+
accl &= mask64;
|
|
165
|
+
acch += XXH3_mix16B(data2, getView(key, 16), seed);
|
|
166
|
+
acch ^= readBigUInt64LE(data1, 0) + readBigUInt64LE(data1, 8);
|
|
167
|
+
acch &= mask64;
|
|
168
|
+
return (acch << n(64)) | accl;
|
|
169
|
+
}
|
|
170
|
+
function XXH3_avalanche(h64) {
|
|
171
|
+
h64 ^= h64 >> n(37);
|
|
172
|
+
h64 *= PRIME_MX1;
|
|
173
|
+
h64 &= mask64;
|
|
174
|
+
h64 ^= h64 >> n(32);
|
|
175
|
+
return h64;
|
|
176
|
+
}
|
|
177
|
+
function XXH3_avalanche64(h64) {
|
|
178
|
+
h64 ^= h64 >> n(33);
|
|
179
|
+
h64 *= PRIME64_2;
|
|
180
|
+
h64 &= mask64;
|
|
181
|
+
h64 ^= h64 >> n(29);
|
|
182
|
+
h64 *= PRIME64_3;
|
|
183
|
+
h64 &= mask64;
|
|
184
|
+
h64 ^= h64 >> n(32);
|
|
185
|
+
return h64;
|
|
186
|
+
}
|
|
187
|
+
function XXH3_len_1to3_128b(data, key32, seed) {
|
|
188
|
+
const len = data.byteLength;
|
|
189
|
+
assert(len > 0 && len <= 3);
|
|
190
|
+
const combined = n(readUInt8(data, len - 1)) |
|
|
191
|
+
n(len << 8) |
|
|
192
|
+
n(readUInt8(data, 0) << 16) |
|
|
193
|
+
n(readUInt8(data, len >> 1) << 24);
|
|
194
|
+
const blow = (n(readUInt32LE(key32, 0)) ^ n(readUInt32LE(key32, 4))) + seed;
|
|
195
|
+
const low = (combined ^ blow) & mask64;
|
|
196
|
+
const bhigh = (n(readUInt32LE(key32, 8)) ^ n(readUInt32LE(key32, 12))) - seed;
|
|
197
|
+
const high = (rotl32(bswap32(combined), n(13)) ^ bhigh) & mask64;
|
|
198
|
+
return ((XXH3_avalanche64(high) & mask64) << n(64)) | XXH3_avalanche64(low);
|
|
199
|
+
}
|
|
200
|
+
function xorshift64(b, shift) {
|
|
201
|
+
return b ^ (b >> shift);
|
|
202
|
+
}
|
|
203
|
+
function XXH3_len_4to8_128b(data, key32, seed) {
|
|
204
|
+
const len = data.byteLength;
|
|
205
|
+
assert(len >= 4 && len <= 8);
|
|
206
|
+
{
|
|
207
|
+
const l1 = readUInt32LE(data, 0);
|
|
208
|
+
const l2 = readUInt32LE(data, len - 4);
|
|
209
|
+
const l64 = n(l1) | (n(l2) << n(32));
|
|
210
|
+
const bitflip = ((readBigUInt64LE(key32, 16) ^ readBigUInt64LE(key32, 24)) + seed) &
|
|
211
|
+
mask64;
|
|
212
|
+
const keyed = l64 ^ bitflip;
|
|
213
|
+
let m128 = (keyed * (PRIME64_1 + (n(len) << n(2)))) & mask128;
|
|
214
|
+
m128 += (m128 & mask64) << n(65);
|
|
215
|
+
m128 &= mask128;
|
|
216
|
+
m128 ^= m128 >> n(67);
|
|
217
|
+
return (xorshift64((xorshift64(m128 & mask64, n(35)) * PRIME_MX2) & mask64, n(28)) |
|
|
218
|
+
(XXH3_avalanche(m128 >> n(64)) << n(64)));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function XXH3_len_9to16_128b(data, key64, seed) {
|
|
222
|
+
const len = data.byteLength;
|
|
223
|
+
assert(len >= 9 && len <= 16);
|
|
224
|
+
{
|
|
225
|
+
const bitflipl = ((readBigUInt64LE(key64, 32) ^ readBigUInt64LE(key64, 40)) + seed) &
|
|
226
|
+
mask64;
|
|
227
|
+
const bitfliph = ((readBigUInt64LE(key64, 48) ^ readBigUInt64LE(key64, 56)) - seed) &
|
|
228
|
+
mask64;
|
|
229
|
+
const ll1 = readBigUInt64LE(data);
|
|
230
|
+
let ll2 = readBigUInt64LE(data, len - 8);
|
|
231
|
+
let m128 = (ll1 ^ ll2 ^ bitflipl) * PRIME64_1;
|
|
232
|
+
const m128_l = (m128 & mask64) + (n(len - 1) << n(54));
|
|
233
|
+
m128 = (m128 & (mask128 ^ mask64)) | m128_l; // eqv. to adding only to lower 64b
|
|
234
|
+
ll2 ^= bitfliph;
|
|
235
|
+
m128 += (ll2 + (ll2 & mask32) * (PRIME32_2 - n(1))) << n(64);
|
|
236
|
+
m128 &= mask128;
|
|
237
|
+
m128 ^= bswap64(m128 >> n(64));
|
|
238
|
+
let h128 = (m128 & mask64) * PRIME64_2;
|
|
239
|
+
h128 += ((m128 >> n(64)) * PRIME64_2) << n(64);
|
|
240
|
+
h128 &= mask128;
|
|
241
|
+
return (XXH3_avalanche(h128 & mask64) | (XXH3_avalanche(h128 >> n(64)) << n(64)));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function XXH3_len_0to16_128b(data, seed) {
|
|
245
|
+
const len = data.byteLength;
|
|
246
|
+
assert(len <= 16);
|
|
247
|
+
if (len > 8)
|
|
248
|
+
return XXH3_len_9to16_128b(data, kkey, seed);
|
|
249
|
+
if (len >= 4)
|
|
250
|
+
return XXH3_len_4to8_128b(data, kkey, seed);
|
|
251
|
+
if (len > 0)
|
|
252
|
+
return XXH3_len_1to3_128b(data, kkey, seed);
|
|
253
|
+
return (XXH3_avalanche64(seed ^ readBigUInt64LE(kkey, 64) ^ readBigUInt64LE(kkey, 72)) |
|
|
254
|
+
(XXH3_avalanche64(seed ^ readBigUInt64LE(kkey, 80) ^ readBigUInt64LE(kkey, 88)) <<
|
|
255
|
+
n(64)));
|
|
256
|
+
}
|
|
257
|
+
function inv64(x) {
|
|
258
|
+
// NOTE: `AND` fixes signedness (but because of 2's complement we need to re-add 1)
|
|
259
|
+
return (~x + n(1)) & mask64;
|
|
260
|
+
}
|
|
261
|
+
function XXH3_len_17to128_128b(data, secret, seed) {
|
|
262
|
+
let acc = (n(data.byteLength) * PRIME64_1) & mask64;
|
|
263
|
+
let i = n(data.byteLength - 1) / n(32);
|
|
264
|
+
while (i >= 0) {
|
|
265
|
+
const ni = Number(i);
|
|
266
|
+
acc = XXH3_mix32B(acc, getView(data, 16 * ni), getView(data, data.byteLength - 16 * (ni + 1)), getView(secret, 32 * ni), seed);
|
|
267
|
+
i--;
|
|
268
|
+
}
|
|
269
|
+
let h128l = (acc + (acc >> n(64))) & mask64;
|
|
270
|
+
h128l = XXH3_avalanche(h128l);
|
|
271
|
+
let h128h = (acc & mask64) * PRIME64_1 +
|
|
272
|
+
(acc >> n(64)) * PRIME64_4 +
|
|
273
|
+
((n(data.byteLength) - seed) & mask64) * PRIME64_2;
|
|
274
|
+
h128h &= mask64;
|
|
275
|
+
h128h = inv64(XXH3_avalanche(h128h));
|
|
276
|
+
return h128l | (h128h << n(64));
|
|
277
|
+
}
|
|
278
|
+
function XXH3_len_129to240_128b(data, secret, seed) {
|
|
279
|
+
let acc = (n(data.byteLength) * PRIME64_1) & mask64;
|
|
280
|
+
for (let i = 32; i < 160; i += 32) {
|
|
281
|
+
acc = XXH3_mix32B(acc, getView(data, i - 32), getView(data, i - 16), getView(secret, i - 32), seed);
|
|
282
|
+
}
|
|
283
|
+
acc = XXH3_avalanche(acc & mask64) | (XXH3_avalanche(acc >> n(64)) << n(64));
|
|
284
|
+
for (let i = 160; i <= data.byteLength; i += 32) {
|
|
285
|
+
acc = XXH3_mix32B(acc, getView(data, i - 32), getView(data, i - 16), getView(secret, 3 + i - 160), seed);
|
|
286
|
+
}
|
|
287
|
+
acc = XXH3_mix32B(acc, getView(data, data.byteLength - 16), getView(data, data.byteLength - 32), getView(secret, 136 - 17 - 16), inv64(seed));
|
|
288
|
+
let h128l = (acc + (acc >> n(64))) & mask64;
|
|
289
|
+
h128l = XXH3_avalanche(h128l);
|
|
290
|
+
let h128h = (acc & mask64) * PRIME64_1 +
|
|
291
|
+
(acc >> n(64)) * PRIME64_4 +
|
|
292
|
+
((n(data.byteLength) - seed) & mask64) * PRIME64_2;
|
|
293
|
+
h128h &= mask64;
|
|
294
|
+
h128h = inv64(XXH3_avalanche(h128h));
|
|
295
|
+
return h128l | (h128h << n(64));
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Compute XXH3 128-bit hash of the input data.
|
|
299
|
+
*
|
|
300
|
+
* @param data - Input data as Uint8Array
|
|
301
|
+
* @param seed - Optional seed value (default: 0)
|
|
302
|
+
* @returns 128-bit hash as a single BigInt (high 64 bits << 64 | low 64 bits)
|
|
303
|
+
*/
|
|
304
|
+
function XXH3_128(data, seed = n(0)) {
|
|
305
|
+
const len = data.byteLength;
|
|
306
|
+
if (len <= 16)
|
|
307
|
+
return XXH3_len_0to16_128b(data, seed);
|
|
308
|
+
if (len <= 128)
|
|
309
|
+
return XXH3_len_17to128_128b(data, kkey, seed);
|
|
310
|
+
if (len <= 240)
|
|
311
|
+
return XXH3_len_129to240_128b(data, kkey, seed);
|
|
312
|
+
return XXH3_hashLong_128b(data, kkey, seed);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Convert a 128-bit hash (BigInt) to a 16-byte Uint8Array.
|
|
316
|
+
*
|
|
317
|
+
* @param hash128 - 128-bit hash as BigInt
|
|
318
|
+
* @returns 16-byte Uint8Array in little-endian byte order
|
|
319
|
+
*/
|
|
320
|
+
function xxh128ToBytes(hash128) {
|
|
321
|
+
const result = new Uint8Array(16);
|
|
322
|
+
const view = new DataView(result.buffer);
|
|
323
|
+
// Extract low and high 64-bit values
|
|
324
|
+
const low64 = hash128 & mask64;
|
|
325
|
+
const high64 = hash128 >> n(64);
|
|
326
|
+
// Write in big-endian order to match Python's xxhash.digest() output
|
|
327
|
+
// Python outputs: [high64 bytes][low64 bytes], each in big-endian
|
|
328
|
+
view.setBigUint64(0, high64, false); // high 64 bits, big-endian
|
|
329
|
+
view.setBigUint64(8, low64, false); // low 64 bits, big-endian
|
|
330
|
+
return result;
|
|
331
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute XXH3 128-bit hash of the input data.
|
|
3
|
+
*
|
|
4
|
+
* @param data - Input data as Uint8Array
|
|
5
|
+
* @param seed - Optional seed value (default: 0)
|
|
6
|
+
* @returns 128-bit hash as a single BigInt (high 64 bits << 64 | low 64 bits)
|
|
7
|
+
*/
|
|
8
|
+
export declare function XXH3_128(data: Uint8Array, seed?: bigint): bigint;
|
|
9
|
+
/**
|
|
10
|
+
* Convert a 128-bit hash (BigInt) to a 16-byte Uint8Array.
|
|
11
|
+
*
|
|
12
|
+
* @param hash128 - 128-bit hash as BigInt
|
|
13
|
+
* @returns 16-byte Uint8Array in little-endian byte order
|
|
14
|
+
*/
|
|
15
|
+
export declare function xxh128ToBytes(hash128: bigint): Uint8Array;
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
// Original from: https://github.com/i404788/xxh3-ts
|
|
4
|
+
// Vendored for compatibility (remove Buffer use in favor of Uint8Array)
|
|
5
|
+
const n = (n) => BigInt(n);
|
|
6
|
+
const PRIME32_1 = n("0x9E3779B1"); // 0b10011110001101110111100110110001
|
|
7
|
+
const PRIME32_2 = n("0x85EBCA77"); // 0b10000101111010111100101001110111
|
|
8
|
+
const PRIME32_3 = n("0xC2B2AE3D"); // 0b11000010101100101010111000111101
|
|
9
|
+
const PRIME64_1 = n("0x9E3779B185EBCA87"); // 0b1001111000110111011110011011000110000101111010111100101010000111
|
|
10
|
+
const PRIME64_2 = n("0xC2B2AE3D27D4EB4F"); // 0b1100001010110010101011100011110100100111110101001110101101001111
|
|
11
|
+
const PRIME64_3 = n("0x165667B19E3779F9"); // 0b0001011001010110011001111011000110011110001101110111100111111001
|
|
12
|
+
const PRIME64_4 = n("0x85EBCA77C2B2AE63"); // 0b1000010111101011110010100111011111000010101100101010111001100011
|
|
13
|
+
const PRIME64_5 = n("0x27D4EB2F165667C5"); // 0b0010011111010100111010110010111100010110010101100110011111000101
|
|
14
|
+
const PRIME_MX1 = n("0x165667919E3779F9"); // 0b0001011001010110011001111001000110011110001101110111100111111001
|
|
15
|
+
const PRIME_MX2 = n("0x9FB21C651E98DF25"); // 0b1001111110110010000111000110010100011110100110001101111100100101
|
|
16
|
+
// Convert hex string to Uint8Array
|
|
17
|
+
function hexToBytes(hex) {
|
|
18
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
19
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
20
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
21
|
+
}
|
|
22
|
+
return bytes;
|
|
23
|
+
}
|
|
24
|
+
const kkey = hexToBytes("b8fe6c3923a44bbe7c01812cf721ad1cded46de9839097db7240a4a4b7b3671fcb79e64eccc0e578825ad07dccff7221b8084674f743248ee03590e6813a264c3c2852bb91c300cb88d0658b1b532ea371644897a20df94e3819ef46a9deacd8a8fa763fe39c343ff9dcbbc7c70b4f1d8a51e04bcdb45931c89f7ec9d9787364eac5ac8334d3ebc3c581a0fffa1363eb170ddd51b7f0da49d316552629d4689e2b16be587d47a1fc8ff8b8d17ad031ce45cb3a8f95160428afd7fbcabb4b407e");
|
|
25
|
+
const mask128 = (n(1) << n(128)) - n(1);
|
|
26
|
+
const mask64 = (n(1) << n(64)) - n(1);
|
|
27
|
+
const mask32 = (n(1) << n(32)) - n(1);
|
|
28
|
+
const STRIPE_LEN = 64;
|
|
29
|
+
const ACC_NB = STRIPE_LEN / 8;
|
|
30
|
+
const _U64 = 8;
|
|
31
|
+
const _U32 = 4;
|
|
32
|
+
// Create a view into a Uint8Array at an offset
|
|
33
|
+
function getView(buf, offset = 0) {
|
|
34
|
+
return new Uint8Array(buf.buffer, buf.byteOffset + offset, buf.length - offset);
|
|
35
|
+
}
|
|
36
|
+
// Read BigInt from Uint8Array in little-endian
|
|
37
|
+
function readBigUInt64LE(buf, offset = 0) {
|
|
38
|
+
const view = new DataView(buf.buffer, buf.byteOffset + offset);
|
|
39
|
+
return view.getBigUint64(0, true);
|
|
40
|
+
}
|
|
41
|
+
// Read UInt32 from Uint8Array in little-endian
|
|
42
|
+
function readUInt32LE(buf, offset = 0) {
|
|
43
|
+
const view = new DataView(buf.buffer, buf.byteOffset + offset);
|
|
44
|
+
return view.getUint32(0, true);
|
|
45
|
+
}
|
|
46
|
+
// Read UInt8 from Uint8Array
|
|
47
|
+
function readUInt8(buf, offset = 0) {
|
|
48
|
+
return buf[offset];
|
|
49
|
+
}
|
|
50
|
+
const bswap64 = (a) => {
|
|
51
|
+
return (((a & n(0xff)) << n(56)) |
|
|
52
|
+
((a & n(0xff00)) << n(40)) |
|
|
53
|
+
((a & n(0xff0000)) << n(24)) |
|
|
54
|
+
((a & n(0xff000000)) << n(8)) |
|
|
55
|
+
((a & n(0xff00000000)) >> n(8)) |
|
|
56
|
+
((a & n(0xff0000000000)) >> n(24)) |
|
|
57
|
+
((a & n(0xff000000000000)) >> n(40)) |
|
|
58
|
+
((a & n(0xff00000000000000)) >> n(56)));
|
|
59
|
+
};
|
|
60
|
+
const bswap32 = (a) => {
|
|
61
|
+
a = ((a & n(0x0000ffff)) << n(16)) | ((a & n(0xffff0000)) >> n(16));
|
|
62
|
+
a = ((a & n(0x00ff00ff)) << n(8)) | ((a & n(0xff00ff00)) >> n(8));
|
|
63
|
+
return a;
|
|
64
|
+
};
|
|
65
|
+
const XXH_mult32to64 = (a, b) => ((a & mask32) * (b & mask32)) & mask64;
|
|
66
|
+
const assert = (a) => {
|
|
67
|
+
if (!a)
|
|
68
|
+
throw new Error("Assert failed");
|
|
69
|
+
};
|
|
70
|
+
function rotl32(a, b) {
|
|
71
|
+
return ((a << b) | (a >> (n(32) - b))) & mask32;
|
|
72
|
+
}
|
|
73
|
+
function XXH3_accumulate_512(acc, data, key) {
|
|
74
|
+
for (let i = 0; i < ACC_NB; i++) {
|
|
75
|
+
const data_val = readBigUInt64LE(data, i * 8);
|
|
76
|
+
const data_key = data_val ^ readBigUInt64LE(key, i * 8);
|
|
77
|
+
acc[i ^ 1] += data_val;
|
|
78
|
+
acc[i] += XXH_mult32to64(data_key, data_key >> n(32));
|
|
79
|
+
}
|
|
80
|
+
return acc;
|
|
81
|
+
}
|
|
82
|
+
function XXH3_accumulate(acc, data, key, nbStripes) {
|
|
83
|
+
for (let n = 0; n < nbStripes; n++) {
|
|
84
|
+
XXH3_accumulate_512(acc, getView(data, n * STRIPE_LEN), getView(key, n * 8));
|
|
85
|
+
}
|
|
86
|
+
return acc;
|
|
87
|
+
}
|
|
88
|
+
function XXH3_scrambleAcc(acc, key) {
|
|
89
|
+
for (let i = 0; i < ACC_NB; i++) {
|
|
90
|
+
const key64 = readBigUInt64LE(key, i * 8);
|
|
91
|
+
let acc64 = acc[i];
|
|
92
|
+
acc64 = xorshift64(acc64, n(47));
|
|
93
|
+
acc64 ^= key64;
|
|
94
|
+
acc64 *= PRIME32_1;
|
|
95
|
+
acc[i] = acc64 & mask64;
|
|
96
|
+
}
|
|
97
|
+
return acc;
|
|
98
|
+
}
|
|
99
|
+
function XXH3_mix2Accs(acc, key) {
|
|
100
|
+
return XXH3_mul128_fold64(acc[0] ^ readBigUInt64LE(key, 0), acc[1] ^ readBigUInt64LE(key, _U64));
|
|
101
|
+
}
|
|
102
|
+
function XXH3_mergeAccs(acc, key, start) {
|
|
103
|
+
let result64 = start;
|
|
104
|
+
result64 += XXH3_mix2Accs(acc.slice(0), getView(key, 0 * _U32));
|
|
105
|
+
result64 += XXH3_mix2Accs(acc.slice(2), getView(key, 4 * _U32));
|
|
106
|
+
result64 += XXH3_mix2Accs(acc.slice(4), getView(key, 8 * _U32));
|
|
107
|
+
result64 += XXH3_mix2Accs(acc.slice(6), getView(key, 12 * _U32));
|
|
108
|
+
return XXH3_avalanche(result64 & mask64);
|
|
109
|
+
}
|
|
110
|
+
function XXH3_hashLong(acc, data, secret, f_acc, f_scramble) {
|
|
111
|
+
const nbStripesPerBlock = Math.floor((secret.byteLength - STRIPE_LEN) / 8);
|
|
112
|
+
const block_len = STRIPE_LEN * nbStripesPerBlock;
|
|
113
|
+
const nb_blocks = Math.floor((data.byteLength - 1) / block_len);
|
|
114
|
+
for (let n = 0; n < nb_blocks; n++) {
|
|
115
|
+
acc = XXH3_accumulate(acc, getView(data, n * block_len), secret, nbStripesPerBlock);
|
|
116
|
+
acc = f_scramble(acc, getView(secret, secret.byteLength - STRIPE_LEN));
|
|
117
|
+
}
|
|
118
|
+
{
|
|
119
|
+
// Partial block
|
|
120
|
+
const nbStripes = Math.floor((data.byteLength - 1 - block_len * nb_blocks) / STRIPE_LEN);
|
|
121
|
+
acc = XXH3_accumulate(acc, getView(data, nb_blocks * block_len), secret, nbStripes);
|
|
122
|
+
// Last Stripe
|
|
123
|
+
acc = f_acc(acc, getView(data, data.byteLength - STRIPE_LEN), getView(secret, secret.byteLength - STRIPE_LEN - 7));
|
|
124
|
+
}
|
|
125
|
+
return acc;
|
|
126
|
+
}
|
|
127
|
+
function XXH3_hashLong_128b(data, secret, seed) {
|
|
128
|
+
let acc = new BigUint64Array([
|
|
129
|
+
PRIME32_3,
|
|
130
|
+
PRIME64_1,
|
|
131
|
+
PRIME64_2,
|
|
132
|
+
PRIME64_3,
|
|
133
|
+
PRIME64_4,
|
|
134
|
+
PRIME32_2,
|
|
135
|
+
PRIME64_5,
|
|
136
|
+
PRIME32_1,
|
|
137
|
+
]);
|
|
138
|
+
assert(data.length > 128);
|
|
139
|
+
acc = XXH3_hashLong(acc, data, secret, XXH3_accumulate_512, XXH3_scrambleAcc);
|
|
140
|
+
/* converge into final hash */
|
|
141
|
+
assert(acc.length * 8 == 64);
|
|
142
|
+
{
|
|
143
|
+
const low64 = XXH3_mergeAccs(acc, getView(secret, 11), (n(data.byteLength) * PRIME64_1) & mask64);
|
|
144
|
+
const high64 = XXH3_mergeAccs(acc, getView(secret, secret.byteLength - STRIPE_LEN - 11), ~(n(data.byteLength) * PRIME64_2) & mask64);
|
|
145
|
+
return (high64 << n(64)) | low64;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function XXH3_mul128_fold64(a, b) {
|
|
149
|
+
const lll = (a * b) & mask128;
|
|
150
|
+
return (lll & mask64) ^ (lll >> n(64));
|
|
151
|
+
}
|
|
152
|
+
function XXH3_mix16B(data, key, seed) {
|
|
153
|
+
return XXH3_mul128_fold64((readBigUInt64LE(data, 0) ^ (readBigUInt64LE(key, 0) + seed)) & mask64, (readBigUInt64LE(data, 8) ^ (readBigUInt64LE(key, 8) - seed)) & mask64);
|
|
154
|
+
}
|
|
155
|
+
function XXH3_mix32B(acc, data1, data2, key, seed) {
|
|
156
|
+
let accl = acc & mask64;
|
|
157
|
+
let acch = (acc >> n(64)) & mask64;
|
|
158
|
+
accl += XXH3_mix16B(data1, key, seed);
|
|
159
|
+
accl ^= readBigUInt64LE(data2, 0) + readBigUInt64LE(data2, 8);
|
|
160
|
+
accl &= mask64;
|
|
161
|
+
acch += XXH3_mix16B(data2, getView(key, 16), seed);
|
|
162
|
+
acch ^= readBigUInt64LE(data1, 0) + readBigUInt64LE(data1, 8);
|
|
163
|
+
acch &= mask64;
|
|
164
|
+
return (acch << n(64)) | accl;
|
|
165
|
+
}
|
|
166
|
+
function XXH3_avalanche(h64) {
|
|
167
|
+
h64 ^= h64 >> n(37);
|
|
168
|
+
h64 *= PRIME_MX1;
|
|
169
|
+
h64 &= mask64;
|
|
170
|
+
h64 ^= h64 >> n(32);
|
|
171
|
+
return h64;
|
|
172
|
+
}
|
|
173
|
+
function XXH3_avalanche64(h64) {
|
|
174
|
+
h64 ^= h64 >> n(33);
|
|
175
|
+
h64 *= PRIME64_2;
|
|
176
|
+
h64 &= mask64;
|
|
177
|
+
h64 ^= h64 >> n(29);
|
|
178
|
+
h64 *= PRIME64_3;
|
|
179
|
+
h64 &= mask64;
|
|
180
|
+
h64 ^= h64 >> n(32);
|
|
181
|
+
return h64;
|
|
182
|
+
}
|
|
183
|
+
function XXH3_len_1to3_128b(data, key32, seed) {
|
|
184
|
+
const len = data.byteLength;
|
|
185
|
+
assert(len > 0 && len <= 3);
|
|
186
|
+
const combined = n(readUInt8(data, len - 1)) |
|
|
187
|
+
n(len << 8) |
|
|
188
|
+
n(readUInt8(data, 0) << 16) |
|
|
189
|
+
n(readUInt8(data, len >> 1) << 24);
|
|
190
|
+
const blow = (n(readUInt32LE(key32, 0)) ^ n(readUInt32LE(key32, 4))) + seed;
|
|
191
|
+
const low = (combined ^ blow) & mask64;
|
|
192
|
+
const bhigh = (n(readUInt32LE(key32, 8)) ^ n(readUInt32LE(key32, 12))) - seed;
|
|
193
|
+
const high = (rotl32(bswap32(combined), n(13)) ^ bhigh) & mask64;
|
|
194
|
+
return ((XXH3_avalanche64(high) & mask64) << n(64)) | XXH3_avalanche64(low);
|
|
195
|
+
}
|
|
196
|
+
function xorshift64(b, shift) {
|
|
197
|
+
return b ^ (b >> shift);
|
|
198
|
+
}
|
|
199
|
+
function XXH3_len_4to8_128b(data, key32, seed) {
|
|
200
|
+
const len = data.byteLength;
|
|
201
|
+
assert(len >= 4 && len <= 8);
|
|
202
|
+
{
|
|
203
|
+
const l1 = readUInt32LE(data, 0);
|
|
204
|
+
const l2 = readUInt32LE(data, len - 4);
|
|
205
|
+
const l64 = n(l1) | (n(l2) << n(32));
|
|
206
|
+
const bitflip = ((readBigUInt64LE(key32, 16) ^ readBigUInt64LE(key32, 24)) + seed) &
|
|
207
|
+
mask64;
|
|
208
|
+
const keyed = l64 ^ bitflip;
|
|
209
|
+
let m128 = (keyed * (PRIME64_1 + (n(len) << n(2)))) & mask128;
|
|
210
|
+
m128 += (m128 & mask64) << n(65);
|
|
211
|
+
m128 &= mask128;
|
|
212
|
+
m128 ^= m128 >> n(67);
|
|
213
|
+
return (xorshift64((xorshift64(m128 & mask64, n(35)) * PRIME_MX2) & mask64, n(28)) |
|
|
214
|
+
(XXH3_avalanche(m128 >> n(64)) << n(64)));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function XXH3_len_9to16_128b(data, key64, seed) {
|
|
218
|
+
const len = data.byteLength;
|
|
219
|
+
assert(len >= 9 && len <= 16);
|
|
220
|
+
{
|
|
221
|
+
const bitflipl = ((readBigUInt64LE(key64, 32) ^ readBigUInt64LE(key64, 40)) + seed) &
|
|
222
|
+
mask64;
|
|
223
|
+
const bitfliph = ((readBigUInt64LE(key64, 48) ^ readBigUInt64LE(key64, 56)) - seed) &
|
|
224
|
+
mask64;
|
|
225
|
+
const ll1 = readBigUInt64LE(data);
|
|
226
|
+
let ll2 = readBigUInt64LE(data, len - 8);
|
|
227
|
+
let m128 = (ll1 ^ ll2 ^ bitflipl) * PRIME64_1;
|
|
228
|
+
const m128_l = (m128 & mask64) + (n(len - 1) << n(54));
|
|
229
|
+
m128 = (m128 & (mask128 ^ mask64)) | m128_l; // eqv. to adding only to lower 64b
|
|
230
|
+
ll2 ^= bitfliph;
|
|
231
|
+
m128 += (ll2 + (ll2 & mask32) * (PRIME32_2 - n(1))) << n(64);
|
|
232
|
+
m128 &= mask128;
|
|
233
|
+
m128 ^= bswap64(m128 >> n(64));
|
|
234
|
+
let h128 = (m128 & mask64) * PRIME64_2;
|
|
235
|
+
h128 += ((m128 >> n(64)) * PRIME64_2) << n(64);
|
|
236
|
+
h128 &= mask128;
|
|
237
|
+
return (XXH3_avalanche(h128 & mask64) | (XXH3_avalanche(h128 >> n(64)) << n(64)));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function XXH3_len_0to16_128b(data, seed) {
|
|
241
|
+
const len = data.byteLength;
|
|
242
|
+
assert(len <= 16);
|
|
243
|
+
if (len > 8)
|
|
244
|
+
return XXH3_len_9to16_128b(data, kkey, seed);
|
|
245
|
+
if (len >= 4)
|
|
246
|
+
return XXH3_len_4to8_128b(data, kkey, seed);
|
|
247
|
+
if (len > 0)
|
|
248
|
+
return XXH3_len_1to3_128b(data, kkey, seed);
|
|
249
|
+
return (XXH3_avalanche64(seed ^ readBigUInt64LE(kkey, 64) ^ readBigUInt64LE(kkey, 72)) |
|
|
250
|
+
(XXH3_avalanche64(seed ^ readBigUInt64LE(kkey, 80) ^ readBigUInt64LE(kkey, 88)) <<
|
|
251
|
+
n(64)));
|
|
252
|
+
}
|
|
253
|
+
function inv64(x) {
|
|
254
|
+
// NOTE: `AND` fixes signedness (but because of 2's complement we need to re-add 1)
|
|
255
|
+
return (~x + n(1)) & mask64;
|
|
256
|
+
}
|
|
257
|
+
function XXH3_len_17to128_128b(data, secret, seed) {
|
|
258
|
+
let acc = (n(data.byteLength) * PRIME64_1) & mask64;
|
|
259
|
+
let i = n(data.byteLength - 1) / n(32);
|
|
260
|
+
while (i >= 0) {
|
|
261
|
+
const ni = Number(i);
|
|
262
|
+
acc = XXH3_mix32B(acc, getView(data, 16 * ni), getView(data, data.byteLength - 16 * (ni + 1)), getView(secret, 32 * ni), seed);
|
|
263
|
+
i--;
|
|
264
|
+
}
|
|
265
|
+
let h128l = (acc + (acc >> n(64))) & mask64;
|
|
266
|
+
h128l = XXH3_avalanche(h128l);
|
|
267
|
+
let h128h = (acc & mask64) * PRIME64_1 +
|
|
268
|
+
(acc >> n(64)) * PRIME64_4 +
|
|
269
|
+
((n(data.byteLength) - seed) & mask64) * PRIME64_2;
|
|
270
|
+
h128h &= mask64;
|
|
271
|
+
h128h = inv64(XXH3_avalanche(h128h));
|
|
272
|
+
return h128l | (h128h << n(64));
|
|
273
|
+
}
|
|
274
|
+
function XXH3_len_129to240_128b(data, secret, seed) {
|
|
275
|
+
let acc = (n(data.byteLength) * PRIME64_1) & mask64;
|
|
276
|
+
for (let i = 32; i < 160; i += 32) {
|
|
277
|
+
acc = XXH3_mix32B(acc, getView(data, i - 32), getView(data, i - 16), getView(secret, i - 32), seed);
|
|
278
|
+
}
|
|
279
|
+
acc = XXH3_avalanche(acc & mask64) | (XXH3_avalanche(acc >> n(64)) << n(64));
|
|
280
|
+
for (let i = 160; i <= data.byteLength; i += 32) {
|
|
281
|
+
acc = XXH3_mix32B(acc, getView(data, i - 32), getView(data, i - 16), getView(secret, 3 + i - 160), seed);
|
|
282
|
+
}
|
|
283
|
+
acc = XXH3_mix32B(acc, getView(data, data.byteLength - 16), getView(data, data.byteLength - 32), getView(secret, 136 - 17 - 16), inv64(seed));
|
|
284
|
+
let h128l = (acc + (acc >> n(64))) & mask64;
|
|
285
|
+
h128l = XXH3_avalanche(h128l);
|
|
286
|
+
let h128h = (acc & mask64) * PRIME64_1 +
|
|
287
|
+
(acc >> n(64)) * PRIME64_4 +
|
|
288
|
+
((n(data.byteLength) - seed) & mask64) * PRIME64_2;
|
|
289
|
+
h128h &= mask64;
|
|
290
|
+
h128h = inv64(XXH3_avalanche(h128h));
|
|
291
|
+
return h128l | (h128h << n(64));
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Compute XXH3 128-bit hash of the input data.
|
|
295
|
+
*
|
|
296
|
+
* @param data - Input data as Uint8Array
|
|
297
|
+
* @param seed - Optional seed value (default: 0)
|
|
298
|
+
* @returns 128-bit hash as a single BigInt (high 64 bits << 64 | low 64 bits)
|
|
299
|
+
*/
|
|
300
|
+
export function XXH3_128(data, seed = n(0)) {
|
|
301
|
+
const len = data.byteLength;
|
|
302
|
+
if (len <= 16)
|
|
303
|
+
return XXH3_len_0to16_128b(data, seed);
|
|
304
|
+
if (len <= 128)
|
|
305
|
+
return XXH3_len_17to128_128b(data, kkey, seed);
|
|
306
|
+
if (len <= 240)
|
|
307
|
+
return XXH3_len_129to240_128b(data, kkey, seed);
|
|
308
|
+
return XXH3_hashLong_128b(data, kkey, seed);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Convert a 128-bit hash (BigInt) to a 16-byte Uint8Array.
|
|
312
|
+
*
|
|
313
|
+
* @param hash128 - 128-bit hash as BigInt
|
|
314
|
+
* @returns 16-byte Uint8Array in little-endian byte order
|
|
315
|
+
*/
|
|
316
|
+
export function xxh128ToBytes(hash128) {
|
|
317
|
+
const result = new Uint8Array(16);
|
|
318
|
+
const view = new DataView(result.buffer);
|
|
319
|
+
// Extract low and high 64-bit values
|
|
320
|
+
const low64 = hash128 & mask64;
|
|
321
|
+
const high64 = hash128 >> n(64);
|
|
322
|
+
// Write in big-endian order to match Python's xxhash.digest() output
|
|
323
|
+
// Python outputs: [high64 bytes][low64 bytes], each in big-endian
|
|
324
|
+
view.setBigUint64(0, high64, false); // high 64 bits, big-endian
|
|
325
|
+
view.setBigUint64(8, low64, false); // low 64 bits, big-endian
|
|
326
|
+
return result;
|
|
327
|
+
}
|