@certivu/sdk 1.0.0 → 1.4.1

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/index.js CHANGED
@@ -12,1249 +12,6 @@ async function getAuditLog(baseUrl, apiKey, options = {}) {
12
12
  return res.json();
13
13
  }
14
14
 
15
- // ../../node_modules/.bun/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/_u64.js
16
- var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
17
- var _32n = /* @__PURE__ */ BigInt(32);
18
- function fromBig(n, le = false) {
19
- if (le)
20
- return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
21
- return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
22
- }
23
- function split(lst, le = false) {
24
- const len = lst.length;
25
- let Ah = new Uint32Array(len);
26
- let Al = new Uint32Array(len);
27
- for (let i = 0;i < len; i++) {
28
- const { h, l } = fromBig(lst[i], le);
29
- [Ah[i], Al[i]] = [h, l];
30
- }
31
- return [Ah, Al];
32
- }
33
- var rotlSH = (h, l, s) => h << s | l >>> 32 - s;
34
- var rotlSL = (h, l, s) => l << s | h >>> 32 - s;
35
- var rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
36
- var rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
37
-
38
- // ../../node_modules/.bun/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/utils.js
39
- /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
40
- function isBytes(a) {
41
- return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
42
- }
43
- function anumber(n) {
44
- if (!Number.isSafeInteger(n) || n < 0)
45
- throw new Error("positive integer expected, got " + n);
46
- }
47
- function abytes(b, ...lengths) {
48
- if (!isBytes(b))
49
- throw new Error("Uint8Array expected");
50
- if (lengths.length > 0 && !lengths.includes(b.length))
51
- throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
52
- }
53
- function aexists(instance, checkFinished = true) {
54
- if (instance.destroyed)
55
- throw new Error("Hash instance has been destroyed");
56
- if (checkFinished && instance.finished)
57
- throw new Error("Hash#digest() has already been called");
58
- }
59
- function aoutput(out, instance) {
60
- abytes(out);
61
- const min = instance.outputLen;
62
- if (out.length < min) {
63
- throw new Error("digestInto() expects output buffer of length at least " + min);
64
- }
65
- }
66
- function u32(arr) {
67
- return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
68
- }
69
- function clean(...arrays) {
70
- for (let i = 0;i < arrays.length; i++) {
71
- arrays[i].fill(0);
72
- }
73
- }
74
- var isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
75
- function byteSwap(word) {
76
- return word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
77
- }
78
- function byteSwap32(arr) {
79
- for (let i = 0;i < arr.length; i++) {
80
- arr[i] = byteSwap(arr[i]);
81
- }
82
- return arr;
83
- }
84
- var swap32IfBE = isLE ? (u) => u : byteSwap32;
85
- var hasHexBuiltin = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function")();
86
- var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
87
- function bytesToHex(bytes) {
88
- abytes(bytes);
89
- if (hasHexBuiltin)
90
- return bytes.toHex();
91
- let hex = "";
92
- for (let i = 0;i < bytes.length; i++) {
93
- hex += hexes[bytes[i]];
94
- }
95
- return hex;
96
- }
97
- function utf8ToBytes(str) {
98
- if (typeof str !== "string")
99
- throw new Error("string expected");
100
- return new Uint8Array(new TextEncoder().encode(str));
101
- }
102
- function toBytes(data) {
103
- if (typeof data === "string")
104
- data = utf8ToBytes(data);
105
- abytes(data);
106
- return data;
107
- }
108
- class Hash {
109
- }
110
- function createHasher(hashCons) {
111
- const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
112
- const tmp = hashCons();
113
- hashC.outputLen = tmp.outputLen;
114
- hashC.blockLen = tmp.blockLen;
115
- hashC.create = () => hashCons();
116
- return hashC;
117
- }
118
-
119
- // ../../node_modules/.bun/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/sha3.js
120
- var _0n = BigInt(0);
121
- var _1n = BigInt(1);
122
- var _2n = BigInt(2);
123
- var _7n = BigInt(7);
124
- var _256n = BigInt(256);
125
- var _0x71n = BigInt(113);
126
- var SHA3_PI = [];
127
- var SHA3_ROTL = [];
128
- var _SHA3_IOTA = [];
129
- for (let round = 0, R = _1n, x = 1, y = 0;round < 24; round++) {
130
- [x, y] = [y, (2 * x + 3 * y) % 5];
131
- SHA3_PI.push(2 * (5 * y + x));
132
- SHA3_ROTL.push((round + 1) * (round + 2) / 2 % 64);
133
- let t = _0n;
134
- for (let j = 0;j < 7; j++) {
135
- R = (R << _1n ^ (R >> _7n) * _0x71n) % _256n;
136
- if (R & _2n)
137
- t ^= _1n << (_1n << /* @__PURE__ */ BigInt(j)) - _1n;
138
- }
139
- _SHA3_IOTA.push(t);
140
- }
141
- var IOTAS = split(_SHA3_IOTA, true);
142
- var SHA3_IOTA_H = IOTAS[0];
143
- var SHA3_IOTA_L = IOTAS[1];
144
- var rotlH = (h, l, s) => s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s);
145
- var rotlL = (h, l, s) => s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s);
146
- function keccakP(s, rounds = 24) {
147
- const B = new Uint32Array(5 * 2);
148
- for (let round = 24 - rounds;round < 24; round++) {
149
- for (let x = 0;x < 10; x++)
150
- B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
151
- for (let x = 0;x < 10; x += 2) {
152
- const idx1 = (x + 8) % 10;
153
- const idx0 = (x + 2) % 10;
154
- const B0 = B[idx0];
155
- const B1 = B[idx0 + 1];
156
- const Th = rotlH(B0, B1, 1) ^ B[idx1];
157
- const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
158
- for (let y = 0;y < 50; y += 10) {
159
- s[x + y] ^= Th;
160
- s[x + y + 1] ^= Tl;
161
- }
162
- }
163
- let curH = s[2];
164
- let curL = s[3];
165
- for (let t = 0;t < 24; t++) {
166
- const shift = SHA3_ROTL[t];
167
- const Th = rotlH(curH, curL, shift);
168
- const Tl = rotlL(curH, curL, shift);
169
- const PI = SHA3_PI[t];
170
- curH = s[PI];
171
- curL = s[PI + 1];
172
- s[PI] = Th;
173
- s[PI + 1] = Tl;
174
- }
175
- for (let y = 0;y < 50; y += 10) {
176
- for (let x = 0;x < 10; x++)
177
- B[x] = s[y + x];
178
- for (let x = 0;x < 10; x++)
179
- s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
180
- }
181
- s[0] ^= SHA3_IOTA_H[round];
182
- s[1] ^= SHA3_IOTA_L[round];
183
- }
184
- clean(B);
185
- }
186
-
187
- class Keccak extends Hash {
188
- constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
189
- super();
190
- this.pos = 0;
191
- this.posOut = 0;
192
- this.finished = false;
193
- this.destroyed = false;
194
- this.enableXOF = false;
195
- this.blockLen = blockLen;
196
- this.suffix = suffix;
197
- this.outputLen = outputLen;
198
- this.enableXOF = enableXOF;
199
- this.rounds = rounds;
200
- anumber(outputLen);
201
- if (!(0 < blockLen && blockLen < 200))
202
- throw new Error("only keccak-f1600 function is supported");
203
- this.state = new Uint8Array(200);
204
- this.state32 = u32(this.state);
205
- }
206
- clone() {
207
- return this._cloneInto();
208
- }
209
- keccak() {
210
- swap32IfBE(this.state32);
211
- keccakP(this.state32, this.rounds);
212
- swap32IfBE(this.state32);
213
- this.posOut = 0;
214
- this.pos = 0;
215
- }
216
- update(data) {
217
- aexists(this);
218
- data = toBytes(data);
219
- abytes(data);
220
- const { blockLen, state } = this;
221
- const len = data.length;
222
- for (let pos = 0;pos < len; ) {
223
- const take = Math.min(blockLen - this.pos, len - pos);
224
- for (let i = 0;i < take; i++)
225
- state[this.pos++] ^= data[pos++];
226
- if (this.pos === blockLen)
227
- this.keccak();
228
- }
229
- return this;
230
- }
231
- finish() {
232
- if (this.finished)
233
- return;
234
- this.finished = true;
235
- const { state, suffix, pos, blockLen } = this;
236
- state[pos] ^= suffix;
237
- if ((suffix & 128) !== 0 && pos === blockLen - 1)
238
- this.keccak();
239
- state[blockLen - 1] ^= 128;
240
- this.keccak();
241
- }
242
- writeInto(out) {
243
- aexists(this, false);
244
- abytes(out);
245
- this.finish();
246
- const bufferOut = this.state;
247
- const { blockLen } = this;
248
- for (let pos = 0, len = out.length;pos < len; ) {
249
- if (this.posOut >= blockLen)
250
- this.keccak();
251
- const take = Math.min(blockLen - this.posOut, len - pos);
252
- out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
253
- this.posOut += take;
254
- pos += take;
255
- }
256
- return out;
257
- }
258
- xofInto(out) {
259
- if (!this.enableXOF)
260
- throw new Error("XOF is not possible for this instance");
261
- return this.writeInto(out);
262
- }
263
- xof(bytes) {
264
- anumber(bytes);
265
- return this.xofInto(new Uint8Array(bytes));
266
- }
267
- digestInto(out) {
268
- aoutput(out, this);
269
- if (this.finished)
270
- throw new Error("digest() was already called");
271
- this.writeInto(out);
272
- this.destroy();
273
- return out;
274
- }
275
- digest() {
276
- return this.digestInto(new Uint8Array(this.outputLen));
277
- }
278
- destroy() {
279
- this.destroyed = true;
280
- clean(this.state);
281
- }
282
- _cloneInto(to) {
283
- const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
284
- to || (to = new Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
285
- to.state32.set(this.state32);
286
- to.pos = this.pos;
287
- to.posOut = this.posOut;
288
- to.finished = this.finished;
289
- to.rounds = rounds;
290
- to.suffix = suffix;
291
- to.outputLen = outputLen;
292
- to.enableXOF = enableXOF;
293
- to.destroyed = this.destroyed;
294
- return to;
295
- }
296
- }
297
- var gen = (suffix, blockLen, outputLen) => createHasher(() => new Keccak(blockLen, suffix, outputLen));
298
- var sha3_256 = /* @__PURE__ */ (() => gen(6, 136, 256 / 8))();
299
-
300
- // ../crypto/src/hash.ts
301
- function hash(content) {
302
- const bytes = typeof content === "string" ? new TextEncoder().encode(content) : content;
303
- return `sha3-256:${bytesToHex(sha3_256(bytes))}`;
304
- }
305
- function canonicalJson(obj) {
306
- return JSON.stringify(obj, Object.keys(obj).sort());
307
- }
308
- // ../../node_modules/.bun/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_assert.js
309
- function anumber2(n) {
310
- if (!Number.isSafeInteger(n) || n < 0)
311
- throw new Error("positive integer expected, got " + n);
312
- }
313
- function isBytes2(a) {
314
- return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
315
- }
316
- function abytes2(b, ...lengths) {
317
- if (!isBytes2(b))
318
- throw new Error("Uint8Array expected");
319
- if (lengths.length > 0 && !lengths.includes(b.length))
320
- throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
321
- }
322
- function aexists2(instance, checkFinished = true) {
323
- if (instance.destroyed)
324
- throw new Error("Hash instance has been destroyed");
325
- if (checkFinished && instance.finished)
326
- throw new Error("Hash#digest() has already been called");
327
- }
328
- function aoutput2(out, instance) {
329
- abytes2(out);
330
- const min = instance.outputLen;
331
- if (out.length < min) {
332
- throw new Error("digestInto() expects output buffer of length at least " + min);
333
- }
334
- }
335
-
336
- // ../../node_modules/.bun/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_u64.js
337
- var U32_MASK642 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
338
- var _32n2 = /* @__PURE__ */ BigInt(32);
339
- function fromBig2(n, le = false) {
340
- if (le)
341
- return { h: Number(n & U32_MASK642), l: Number(n >> _32n2 & U32_MASK642) };
342
- return { h: Number(n >> _32n2 & U32_MASK642) | 0, l: Number(n & U32_MASK642) | 0 };
343
- }
344
- function split2(lst, le = false) {
345
- let Ah = new Uint32Array(lst.length);
346
- let Al = new Uint32Array(lst.length);
347
- for (let i = 0;i < lst.length; i++) {
348
- const { h, l } = fromBig2(lst[i], le);
349
- [Ah[i], Al[i]] = [h, l];
350
- }
351
- return [Ah, Al];
352
- }
353
- var rotlSH2 = (h, l, s) => h << s | l >>> 32 - s;
354
- var rotlSL2 = (h, l, s) => l << s | h >>> 32 - s;
355
- var rotlBH2 = (h, l, s) => l << s - 32 | h >>> 64 - s;
356
- var rotlBL2 = (h, l, s) => h << s - 32 | l >>> 64 - s;
357
-
358
- // ../../node_modules/.bun/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/cryptoNode.js
359
- import * as nc from "node:crypto";
360
- var crypto2 = nc && typeof nc === "object" && "webcrypto" in nc ? nc.webcrypto : nc && typeof nc === "object" && ("randomBytes" in nc) ? nc : undefined;
361
-
362
- // ../../node_modules/.bun/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/utils.js
363
- /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
364
- var u322 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
365
- var isLE2 = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
366
- var byteSwap2 = (word) => word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
367
- function byteSwap322(arr) {
368
- for (let i = 0;i < arr.length; i++) {
369
- arr[i] = byteSwap2(arr[i]);
370
- }
371
- }
372
- function utf8ToBytes2(str) {
373
- if (typeof str !== "string")
374
- throw new Error("utf8ToBytes expected string, got " + typeof str);
375
- return new Uint8Array(new TextEncoder().encode(str));
376
- }
377
- function toBytes2(data) {
378
- if (typeof data === "string")
379
- data = utf8ToBytes2(data);
380
- abytes2(data);
381
- return data;
382
- }
383
- function concatBytes(...arrays) {
384
- let sum = 0;
385
- for (let i = 0;i < arrays.length; i++) {
386
- const a = arrays[i];
387
- abytes2(a);
388
- sum += a.length;
389
- }
390
- const res = new Uint8Array(sum);
391
- for (let i = 0, pad = 0;i < arrays.length; i++) {
392
- const a = arrays[i];
393
- res.set(a, pad);
394
- pad += a.length;
395
- }
396
- return res;
397
- }
398
-
399
- class Hash2 {
400
- clone() {
401
- return this._cloneInto();
402
- }
403
- }
404
- function wrapConstructor(hashCons) {
405
- const hashC = (msg) => hashCons().update(toBytes2(msg)).digest();
406
- const tmp = hashCons();
407
- hashC.outputLen = tmp.outputLen;
408
- hashC.blockLen = tmp.blockLen;
409
- hashC.create = () => hashCons();
410
- return hashC;
411
- }
412
- function wrapXOFConstructorWithOpts(hashCons) {
413
- const hashC = (msg, opts) => hashCons(opts).update(toBytes2(msg)).digest();
414
- const tmp = hashCons({});
415
- hashC.outputLen = tmp.outputLen;
416
- hashC.blockLen = tmp.blockLen;
417
- hashC.create = (opts) => hashCons(opts);
418
- return hashC;
419
- }
420
- function randomBytes(bytesLength = 32) {
421
- if (crypto2 && typeof crypto2.getRandomValues === "function") {
422
- return crypto2.getRandomValues(new Uint8Array(bytesLength));
423
- }
424
- if (crypto2 && typeof crypto2.randomBytes === "function") {
425
- return crypto2.randomBytes(bytesLength);
426
- }
427
- throw new Error("crypto.getRandomValues must be defined");
428
- }
429
-
430
- // ../../node_modules/.bun/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/sha3.js
431
- var SHA3_PI2 = [];
432
- var SHA3_ROTL2 = [];
433
- var _SHA3_IOTA2 = [];
434
- var _0n2 = /* @__PURE__ */ BigInt(0);
435
- var _1n2 = /* @__PURE__ */ BigInt(1);
436
- var _2n2 = /* @__PURE__ */ BigInt(2);
437
- var _7n2 = /* @__PURE__ */ BigInt(7);
438
- var _256n2 = /* @__PURE__ */ BigInt(256);
439
- var _0x71n2 = /* @__PURE__ */ BigInt(113);
440
- for (let round = 0, R = _1n2, x = 1, y = 0;round < 24; round++) {
441
- [x, y] = [y, (2 * x + 3 * y) % 5];
442
- SHA3_PI2.push(2 * (5 * y + x));
443
- SHA3_ROTL2.push((round + 1) * (round + 2) / 2 % 64);
444
- let t = _0n2;
445
- for (let j = 0;j < 7; j++) {
446
- R = (R << _1n2 ^ (R >> _7n2) * _0x71n2) % _256n2;
447
- if (R & _2n2)
448
- t ^= _1n2 << (_1n2 << /* @__PURE__ */ BigInt(j)) - _1n2;
449
- }
450
- _SHA3_IOTA2.push(t);
451
- }
452
- var [SHA3_IOTA_H2, SHA3_IOTA_L2] = /* @__PURE__ */ split2(_SHA3_IOTA2, true);
453
- var rotlH2 = (h, l, s) => s > 32 ? rotlBH2(h, l, s) : rotlSH2(h, l, s);
454
- var rotlL2 = (h, l, s) => s > 32 ? rotlBL2(h, l, s) : rotlSL2(h, l, s);
455
- function keccakP2(s, rounds = 24) {
456
- const B = new Uint32Array(5 * 2);
457
- for (let round = 24 - rounds;round < 24; round++) {
458
- for (let x = 0;x < 10; x++)
459
- B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
460
- for (let x = 0;x < 10; x += 2) {
461
- const idx1 = (x + 8) % 10;
462
- const idx0 = (x + 2) % 10;
463
- const B0 = B[idx0];
464
- const B1 = B[idx0 + 1];
465
- const Th = rotlH2(B0, B1, 1) ^ B[idx1];
466
- const Tl = rotlL2(B0, B1, 1) ^ B[idx1 + 1];
467
- for (let y = 0;y < 50; y += 10) {
468
- s[x + y] ^= Th;
469
- s[x + y + 1] ^= Tl;
470
- }
471
- }
472
- let curH = s[2];
473
- let curL = s[3];
474
- for (let t = 0;t < 24; t++) {
475
- const shift = SHA3_ROTL2[t];
476
- const Th = rotlH2(curH, curL, shift);
477
- const Tl = rotlL2(curH, curL, shift);
478
- const PI = SHA3_PI2[t];
479
- curH = s[PI];
480
- curL = s[PI + 1];
481
- s[PI] = Th;
482
- s[PI + 1] = Tl;
483
- }
484
- for (let y = 0;y < 50; y += 10) {
485
- for (let x = 0;x < 10; x++)
486
- B[x] = s[y + x];
487
- for (let x = 0;x < 10; x++)
488
- s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
489
- }
490
- s[0] ^= SHA3_IOTA_H2[round];
491
- s[1] ^= SHA3_IOTA_L2[round];
492
- }
493
- B.fill(0);
494
- }
495
-
496
- class Keccak2 extends Hash2 {
497
- constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
498
- super();
499
- this.blockLen = blockLen;
500
- this.suffix = suffix;
501
- this.outputLen = outputLen;
502
- this.enableXOF = enableXOF;
503
- this.rounds = rounds;
504
- this.pos = 0;
505
- this.posOut = 0;
506
- this.finished = false;
507
- this.destroyed = false;
508
- anumber2(outputLen);
509
- if (0 >= this.blockLen || this.blockLen >= 200)
510
- throw new Error("Sha3 supports only keccak-f1600 function");
511
- this.state = new Uint8Array(200);
512
- this.state32 = u322(this.state);
513
- }
514
- keccak() {
515
- if (!isLE2)
516
- byteSwap322(this.state32);
517
- keccakP2(this.state32, this.rounds);
518
- if (!isLE2)
519
- byteSwap322(this.state32);
520
- this.posOut = 0;
521
- this.pos = 0;
522
- }
523
- update(data) {
524
- aexists2(this);
525
- const { blockLen, state } = this;
526
- data = toBytes2(data);
527
- const len = data.length;
528
- for (let pos = 0;pos < len; ) {
529
- const take = Math.min(blockLen - this.pos, len - pos);
530
- for (let i = 0;i < take; i++)
531
- state[this.pos++] ^= data[pos++];
532
- if (this.pos === blockLen)
533
- this.keccak();
534
- }
535
- return this;
536
- }
537
- finish() {
538
- if (this.finished)
539
- return;
540
- this.finished = true;
541
- const { state, suffix, pos, blockLen } = this;
542
- state[pos] ^= suffix;
543
- if ((suffix & 128) !== 0 && pos === blockLen - 1)
544
- this.keccak();
545
- state[blockLen - 1] ^= 128;
546
- this.keccak();
547
- }
548
- writeInto(out) {
549
- aexists2(this, false);
550
- abytes2(out);
551
- this.finish();
552
- const bufferOut = this.state;
553
- const { blockLen } = this;
554
- for (let pos = 0, len = out.length;pos < len; ) {
555
- if (this.posOut >= blockLen)
556
- this.keccak();
557
- const take = Math.min(blockLen - this.posOut, len - pos);
558
- out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
559
- this.posOut += take;
560
- pos += take;
561
- }
562
- return out;
563
- }
564
- xofInto(out) {
565
- if (!this.enableXOF)
566
- throw new Error("XOF is not possible for this instance");
567
- return this.writeInto(out);
568
- }
569
- xof(bytes) {
570
- anumber2(bytes);
571
- return this.xofInto(new Uint8Array(bytes));
572
- }
573
- digestInto(out) {
574
- aoutput2(out, this);
575
- if (this.finished)
576
- throw new Error("digest() was already called");
577
- this.writeInto(out);
578
- this.destroy();
579
- return out;
580
- }
581
- digest() {
582
- return this.digestInto(new Uint8Array(this.outputLen));
583
- }
584
- destroy() {
585
- this.destroyed = true;
586
- this.state.fill(0);
587
- }
588
- _cloneInto(to) {
589
- const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
590
- to || (to = new Keccak2(blockLen, suffix, outputLen, enableXOF, rounds));
591
- to.state32.set(this.state32);
592
- to.pos = this.pos;
593
- to.posOut = this.posOut;
594
- to.finished = this.finished;
595
- to.rounds = rounds;
596
- to.suffix = suffix;
597
- to.outputLen = outputLen;
598
- to.enableXOF = enableXOF;
599
- to.destroyed = this.destroyed;
600
- return to;
601
- }
602
- }
603
- var gen2 = (suffix, blockLen, outputLen) => wrapConstructor(() => new Keccak2(blockLen, suffix, outputLen));
604
- var sha3_224 = /* @__PURE__ */ gen2(6, 144, 224 / 8);
605
- var sha3_2562 = /* @__PURE__ */ gen2(6, 136, 256 / 8);
606
- var sha3_384 = /* @__PURE__ */ gen2(6, 104, 384 / 8);
607
- var sha3_512 = /* @__PURE__ */ gen2(6, 72, 512 / 8);
608
- var keccak_224 = /* @__PURE__ */ gen2(1, 144, 224 / 8);
609
- var keccak_256 = /* @__PURE__ */ gen2(1, 136, 256 / 8);
610
- var keccak_384 = /* @__PURE__ */ gen2(1, 104, 384 / 8);
611
- var keccak_512 = /* @__PURE__ */ gen2(1, 72, 512 / 8);
612
- var genShake = (suffix, blockLen, outputLen) => wrapXOFConstructorWithOpts((opts = {}) => new Keccak2(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true));
613
- var shake128 = /* @__PURE__ */ genShake(31, 168, 128 / 8);
614
- var shake256 = /* @__PURE__ */ genShake(31, 136, 256 / 8);
615
-
616
- // ../../node_modules/.bun/@noble+post-quantum@0.2.1/node_modules/@noble/post-quantum/esm/utils.js
617
- /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
618
- var ensureBytes = abytes2;
619
- var randomBytes2 = randomBytes;
620
- function equalBytes(a, b) {
621
- if (a.length !== b.length)
622
- return false;
623
- let diff = 0;
624
- for (let i = 0;i < a.length; i++)
625
- diff |= a[i] ^ b[i];
626
- return diff === 0;
627
- }
628
- function splitCoder(...lengths) {
629
- const getLength = (c) => typeof c === "number" ? c : c.bytesLen;
630
- const bytesLen = lengths.reduce((sum, a) => sum + getLength(a), 0);
631
- return {
632
- bytesLen,
633
- encode: (bufs) => {
634
- const res = new Uint8Array(bytesLen);
635
- for (let i = 0, pos = 0;i < lengths.length; i++) {
636
- const c = lengths[i];
637
- const l = getLength(c);
638
- const b = typeof c === "number" ? bufs[i] : c.encode(bufs[i]);
639
- ensureBytes(b, l);
640
- res.set(b, pos);
641
- if (typeof c !== "number")
642
- b.fill(0);
643
- pos += l;
644
- }
645
- return res;
646
- },
647
- decode: (buf) => {
648
- ensureBytes(buf, bytesLen);
649
- const res = [];
650
- for (const c of lengths) {
651
- const l = getLength(c);
652
- const b = buf.subarray(0, l);
653
- res.push(typeof c === "number" ? b : c.decode(b));
654
- buf = buf.subarray(l);
655
- }
656
- return res;
657
- }
658
- };
659
- }
660
- function vecCoder(c, vecLen) {
661
- const bytesLen = vecLen * c.bytesLen;
662
- return {
663
- bytesLen,
664
- encode: (u) => {
665
- if (u.length !== vecLen)
666
- throw new Error(`vecCoder.encode: wrong length=${u.length}. Expected: ${vecLen}`);
667
- const res = new Uint8Array(bytesLen);
668
- for (let i = 0, pos = 0;i < u.length; i++) {
669
- const b = c.encode(u[i]);
670
- res.set(b, pos);
671
- b.fill(0);
672
- pos += b.length;
673
- }
674
- return res;
675
- },
676
- decode: (a) => {
677
- ensureBytes(a, bytesLen);
678
- const r = [];
679
- for (let i = 0;i < a.length; i += c.bytesLen)
680
- r.push(c.decode(a.subarray(i, i + c.bytesLen)));
681
- return r;
682
- }
683
- };
684
- }
685
- function cleanBytes(...list) {
686
- for (const t of list) {
687
- if (Array.isArray(t))
688
- for (const b of t)
689
- b.fill(0);
690
- else
691
- t.fill(0);
692
- }
693
- }
694
- function getMask(bits) {
695
- return (1 << bits) - 1;
696
- }
697
-
698
- // ../../node_modules/.bun/@noble+post-quantum@0.2.1/node_modules/@noble/post-quantum/esm/_crystals.js
699
- /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
700
- function bitReversal(n, bits = 8) {
701
- const padded = n.toString(2).padStart(8, "0");
702
- const sliced = padded.slice(-bits).padStart(7, "0");
703
- const revrsd = sliced.split("").reverse().join("");
704
- return Number.parseInt(revrsd, 2);
705
- }
706
- var genCrystals = (opts) => {
707
- const { newPoly, N, Q, F, ROOT_OF_UNITY, brvBits, isKyber } = opts;
708
- const mod = (a, modulo = Q) => {
709
- const result = a % modulo | 0;
710
- return (result >= 0 ? result | 0 : modulo + result | 0) | 0;
711
- };
712
- const smod = (a, modulo = Q) => {
713
- const r = mod(a, modulo) | 0;
714
- return (r > modulo >> 1 ? r - modulo | 0 : r) | 0;
715
- };
716
- function getZettas() {
717
- const out = newPoly(N);
718
- for (let i = 0;i < N; i++) {
719
- const b = bitReversal(i, brvBits);
720
- const p = BigInt(ROOT_OF_UNITY) ** BigInt(b) % BigInt(Q);
721
- out[i] = Number(p) | 0;
722
- }
723
- return out;
724
- }
725
- const nttZetas = getZettas();
726
- const LEN1 = isKyber ? 128 : N;
727
- const LEN2 = isKyber ? 1 : 0;
728
- const NTT = {
729
- encode: (r) => {
730
- for (let k = 1, len = 128;len > LEN2; len >>= 1) {
731
- for (let start = 0;start < N; start += 2 * len) {
732
- const zeta = nttZetas[k++];
733
- for (let j = start;j < start + len; j++) {
734
- const t = mod(zeta * r[j + len]);
735
- r[j + len] = mod(r[j] - t) | 0;
736
- r[j] = mod(r[j] + t) | 0;
737
- }
738
- }
739
- }
740
- return r;
741
- },
742
- decode: (r) => {
743
- for (let k = LEN1 - 1, len = 1 + LEN2;len < LEN1 + LEN2; len <<= 1) {
744
- for (let start = 0;start < N; start += 2 * len) {
745
- const zeta = nttZetas[k--];
746
- for (let j = start;j < start + len; j++) {
747
- const t = r[j];
748
- r[j] = mod(t + r[j + len]);
749
- r[j + len] = mod(zeta * (r[j + len] - t));
750
- }
751
- }
752
- }
753
- for (let i = 0;i < r.length; i++)
754
- r[i] = mod(F * r[i]);
755
- return r;
756
- }
757
- };
758
- const bitsCoder = (d, c) => {
759
- const mask = getMask(d);
760
- const bytesLen = d * (N / 8);
761
- return {
762
- bytesLen,
763
- encode: (poly) => {
764
- const r = new Uint8Array(bytesLen);
765
- for (let i = 0, buf = 0, bufLen = 0, pos = 0;i < poly.length; i++) {
766
- buf |= (c.encode(poly[i]) & mask) << bufLen;
767
- bufLen += d;
768
- for (;bufLen >= 8; bufLen -= 8, buf >>= 8)
769
- r[pos++] = buf & getMask(bufLen);
770
- }
771
- return r;
772
- },
773
- decode: (bytes) => {
774
- const r = newPoly(N);
775
- for (let i = 0, buf = 0, bufLen = 0, pos = 0;i < bytes.length; i++) {
776
- buf |= bytes[i] << bufLen;
777
- bufLen += 8;
778
- for (;bufLen >= d; bufLen -= d, buf >>= d)
779
- r[pos++] = c.decode(buf & mask);
780
- }
781
- return r;
782
- }
783
- };
784
- };
785
- return { mod, smod, nttZetas, NTT, bitsCoder };
786
- };
787
- var createXofShake = (shake) => (seed, blockLen) => {
788
- if (!blockLen)
789
- blockLen = shake.blockLen;
790
- const _seed = new Uint8Array(seed.length + 2);
791
- _seed.set(seed);
792
- const seedLen = seed.length;
793
- const buf = new Uint8Array(blockLen);
794
- let h = shake.create({});
795
- let calls = 0;
796
- let xofs = 0;
797
- return {
798
- stats: () => ({ calls, xofs }),
799
- get: (x, y) => {
800
- _seed[seedLen + 0] = x;
801
- _seed[seedLen + 1] = y;
802
- h.destroy();
803
- h = shake.create({}).update(_seed);
804
- calls++;
805
- return () => {
806
- xofs++;
807
- return h.xofInto(buf);
808
- };
809
- },
810
- clean: () => {
811
- h.destroy();
812
- buf.fill(0);
813
- _seed.fill(0);
814
- }
815
- };
816
- };
817
- var XOF128 = /* @__PURE__ */ createXofShake(shake128);
818
- var XOF256 = /* @__PURE__ */ createXofShake(shake256);
819
-
820
- // ../../node_modules/.bun/@noble+post-quantum@0.2.1/node_modules/@noble/post-quantum/esm/ml-dsa.js
821
- /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
822
- var N = 256;
823
- var Q = 8380417;
824
- var ROOT_OF_UNITY = 1753;
825
- var F = 8347681;
826
- var D = 13;
827
- var GAMMA2_1 = Math.floor((Q - 1) / 88) | 0;
828
- var GAMMA2_2 = Math.floor((Q - 1) / 32) | 0;
829
- var PARAMS = {
830
- 2: { K: 4, L: 4, D, GAMMA1: 2 ** 17, GAMMA2: GAMMA2_1, TAU: 39, ETA: 2, OMEGA: 80 },
831
- 3: { K: 6, L: 5, D, GAMMA1: 2 ** 19, GAMMA2: GAMMA2_2, TAU: 49, ETA: 4, OMEGA: 55 },
832
- 5: { K: 8, L: 7, D, GAMMA1: 2 ** 19, GAMMA2: GAMMA2_2, TAU: 60, ETA: 2, OMEGA: 75 }
833
- };
834
- var newPoly = (n) => new Int32Array(n);
835
- var { mod, smod, NTT, bitsCoder } = genCrystals({
836
- N,
837
- Q,
838
- F,
839
- ROOT_OF_UNITY,
840
- newPoly,
841
- isKyber: false,
842
- brvBits: 8
843
- });
844
- var id = (n) => n;
845
- var polyCoder = (d, compress = id, verify = id) => bitsCoder(d, {
846
- encode: (i) => compress(verify(i)),
847
- decode: (i) => verify(compress(i))
848
- });
849
- var polyAdd = (a, b) => {
850
- for (let i = 0;i < a.length; i++)
851
- a[i] = mod(a[i] + b[i]);
852
- return a;
853
- };
854
- var polySub = (a, b) => {
855
- for (let i = 0;i < a.length; i++)
856
- a[i] = mod(a[i] - b[i]);
857
- return a;
858
- };
859
- var polyShiftl = (p) => {
860
- for (let i = 0;i < N; i++)
861
- p[i] <<= D;
862
- return p;
863
- };
864
- var polyChknorm = (p, B) => {
865
- for (let i = 0;i < N; i++)
866
- if (Math.abs(smod(p[i])) >= B)
867
- return true;
868
- return false;
869
- };
870
- var MultiplyNTTs = (a, b) => {
871
- const c = newPoly(N);
872
- for (let i = 0;i < a.length; i++)
873
- c[i] = mod(a[i] * b[i]);
874
- return c;
875
- };
876
- function RejNTTPoly(xof) {
877
- const r = newPoly(N);
878
- for (let j = 0;j < N; ) {
879
- const b = xof();
880
- if (b.length % 3)
881
- throw new Error("RejNTTPoly: unaligned block");
882
- for (let i = 0;j < N && i <= b.length - 3; i += 3) {
883
- const t = (b[i + 0] | b[i + 1] << 8 | b[i + 2] << 16) & 8388607;
884
- if (t < Q)
885
- r[j++] = t;
886
- }
887
- }
888
- return r;
889
- }
890
- var EMPTY = new Uint8Array(0);
891
- function getDilithium(opts) {
892
- const { K, L, GAMMA1, GAMMA2, TAU, ETA, OMEGA } = opts;
893
- const { CRH_BYTES, TR_BYTES, C_TILDE_BYTES, XOF128: XOF1282, XOF256: XOF2562 } = opts;
894
- if (![2, 4].includes(ETA))
895
- throw new Error("Wrong ETA");
896
- if (![1 << 17, 1 << 19].includes(GAMMA1))
897
- throw new Error("Wrong GAMMA1");
898
- if (![GAMMA2_1, GAMMA2_2].includes(GAMMA2))
899
- throw new Error("Wrong GAMMA2");
900
- const BETA = TAU * ETA;
901
- const decompose = (r) => {
902
- const rPlus = mod(r);
903
- const r0 = smod(rPlus, 2 * GAMMA2) | 0;
904
- if (rPlus - r0 === Q - 1)
905
- return { r1: 0 | 0, r0: r0 - 1 | 0 };
906
- const r1 = Math.floor((rPlus - r0) / (2 * GAMMA2)) | 0;
907
- return { r1, r0 };
908
- };
909
- const HighBits = (r) => decompose(r).r1;
910
- const LowBits = (r) => decompose(r).r0;
911
- const MakeHint = (z, r) => {
912
- const res0 = z <= GAMMA2 || z > Q - GAMMA2 || z === Q - GAMMA2 && r === 0 ? 0 : 1;
913
- return res0;
914
- };
915
- const UseHint = (h, r) => {
916
- const m = Math.floor((Q - 1) / (2 * GAMMA2));
917
- const { r1, r0 } = decompose(r);
918
- if (h === 1)
919
- return r0 > 0 ? mod(r1 + 1, m) | 0 : mod(r1 - 1, m) | 0;
920
- return r1 | 0;
921
- };
922
- const Power2Round = (r) => {
923
- const rPlus = mod(r);
924
- const r0 = smod(rPlus, 2 ** D) | 0;
925
- return { r1: Math.floor((rPlus - r0) / 2 ** D) | 0, r0 };
926
- };
927
- const hintCoder = {
928
- bytesLen: OMEGA + K,
929
- encode: (h) => {
930
- if (h === false)
931
- throw new Error("hint.encode: hint is false");
932
- const res = new Uint8Array(OMEGA + K);
933
- for (let i = 0, k = 0;i < K; i++) {
934
- for (let j = 0;j < N; j++)
935
- if (h[i][j] !== 0)
936
- res[k++] = j;
937
- res[OMEGA + i] = k;
938
- }
939
- return res;
940
- },
941
- decode: (buf) => {
942
- const h = [];
943
- let k = 0;
944
- for (let i = 0;i < K; i++) {
945
- const hi = newPoly(N);
946
- if (buf[OMEGA + i] < k || buf[OMEGA + i] > OMEGA)
947
- return false;
948
- for (let j = k;j < buf[OMEGA + i]; j++) {
949
- if (j > k && buf[j] <= buf[j - 1])
950
- return false;
951
- hi[buf[j]] = 1;
952
- }
953
- k = buf[OMEGA + i];
954
- h.push(hi);
955
- }
956
- for (let j = k;j < OMEGA; j++)
957
- if (buf[j] !== 0)
958
- return false;
959
- return h;
960
- }
961
- };
962
- const ETACoder = polyCoder(ETA === 2 ? 3 : 4, (i) => ETA - i, (i) => {
963
- if (!(-ETA <= i && i <= ETA))
964
- throw new Error(`malformed key s1/s3 ${i} outside of ETA range [${-ETA}, ${ETA}]`);
965
- return i;
966
- });
967
- const T0Coder = polyCoder(13, (i) => (1 << D - 1) - i);
968
- const T1Coder = polyCoder(10);
969
- const ZCoder = polyCoder(GAMMA1 === 1 << 17 ? 18 : 20, (i) => smod(GAMMA1 - i));
970
- const W1Coder = polyCoder(GAMMA2 === GAMMA2_1 ? 6 : 4);
971
- const W1Vec = vecCoder(W1Coder, K);
972
- const publicCoder = splitCoder(32, vecCoder(T1Coder, K));
973
- const secretCoder = splitCoder(32, 32, TR_BYTES, vecCoder(ETACoder, L), vecCoder(ETACoder, K), vecCoder(T0Coder, K));
974
- const sigCoder = splitCoder(C_TILDE_BYTES, vecCoder(ZCoder, L), hintCoder);
975
- const CoefFromHalfByte = ETA === 2 ? (n) => n < 15 ? 2 - n % 5 : false : (n) => n < 9 ? 4 - n : false;
976
- function RejBoundedPoly(xof) {
977
- const r = newPoly(N);
978
- for (let j = 0;j < N; ) {
979
- const b = xof();
980
- for (let i = 0;j < N && i < b.length; i += 1) {
981
- const d1 = CoefFromHalfByte(b[i] & 15);
982
- const d2 = CoefFromHalfByte(b[i] >> 4 & 15);
983
- if (d1 !== false)
984
- r[j++] = d1;
985
- if (j < N && d2 !== false)
986
- r[j++] = d2;
987
- }
988
- }
989
- return r;
990
- }
991
- const SampleInBall = (seed) => {
992
- const pre = newPoly(N);
993
- const s = shake256.create({}).update(seed);
994
- const buf = new Uint8Array(shake256.blockLen);
995
- s.xofInto(buf);
996
- const masks = buf.slice(0, 8);
997
- for (let i = N - TAU, pos = 8, maskPos = 0, maskBit = 0;i < N; i++) {
998
- let b = i + 1;
999
- for (;b > i; ) {
1000
- b = buf[pos++];
1001
- if (pos < shake256.blockLen)
1002
- continue;
1003
- s.xofInto(buf);
1004
- pos = 0;
1005
- }
1006
- pre[i] = pre[b];
1007
- pre[b] = 1 - ((masks[maskPos] >> maskBit++ & 1) << 1);
1008
- if (maskBit >= 8) {
1009
- maskPos++;
1010
- maskBit = 0;
1011
- }
1012
- }
1013
- return pre;
1014
- };
1015
- const polyPowerRound = (p) => {
1016
- const res0 = newPoly(N);
1017
- const res1 = newPoly(N);
1018
- for (let i = 0;i < p.length; i++) {
1019
- const { r0, r1 } = Power2Round(p[i]);
1020
- res0[i] = r0;
1021
- res1[i] = r1;
1022
- }
1023
- return { r0: res0, r1: res1 };
1024
- };
1025
- const polyUseHint = (u, h) => {
1026
- for (let i = 0;i < N; i++)
1027
- u[i] = UseHint(h[i], u[i]);
1028
- return u;
1029
- };
1030
- const polyMakeHint = (a, b) => {
1031
- const v = newPoly(N);
1032
- let cnt = 0;
1033
- for (let i = 0;i < N; i++) {
1034
- const h = MakeHint(a[i], b[i]);
1035
- v[i] = h;
1036
- cnt += h;
1037
- }
1038
- return { v, cnt };
1039
- };
1040
- const signRandBytes = 32;
1041
- const seedCoder = splitCoder(32, 64, 32);
1042
- const internal = {
1043
- signRandBytes,
1044
- keygen: (seed = randomBytes2(32)) => {
1045
- const seedDst = new Uint8Array(32 + 2);
1046
- seedDst.set(seed);
1047
- seedDst[32] = K;
1048
- seedDst[33] = L;
1049
- const [rho, rhoPrime, K_] = seedCoder.decode(shake256(seedDst, { dkLen: seedCoder.bytesLen }));
1050
- const xofPrime = XOF2562(rhoPrime);
1051
- const s1 = [];
1052
- for (let i = 0;i < L; i++)
1053
- s1.push(RejBoundedPoly(xofPrime.get(i & 255, i >> 8 & 255)));
1054
- const s2 = [];
1055
- for (let i = L;i < L + K; i++)
1056
- s2.push(RejBoundedPoly(xofPrime.get(i & 255, i >> 8 & 255)));
1057
- const s1Hat = s1.map((i) => NTT.encode(i.slice()));
1058
- const t0 = [];
1059
- const t1 = [];
1060
- const xof = XOF1282(rho);
1061
- const t = newPoly(N);
1062
- for (let i = 0;i < K; i++) {
1063
- t.fill(0);
1064
- for (let j = 0;j < L; j++) {
1065
- const aij = RejNTTPoly(xof.get(j, i));
1066
- polyAdd(t, MultiplyNTTs(aij, s1Hat[j]));
1067
- }
1068
- NTT.decode(t);
1069
- const { r0, r1 } = polyPowerRound(polyAdd(t, s2[i]));
1070
- t0.push(r0);
1071
- t1.push(r1);
1072
- }
1073
- const publicKey = publicCoder.encode([rho, t1]);
1074
- const tr = shake256(publicKey, { dkLen: TR_BYTES });
1075
- const secretKey = secretCoder.encode([rho, K_, tr, s1, s2, t0]);
1076
- xof.clean();
1077
- xofPrime.clean();
1078
- cleanBytes(rho, rhoPrime, K_, s1, s2, s1Hat, t, t0, t1, tr, seedDst);
1079
- return { publicKey, secretKey };
1080
- },
1081
- sign: (secretKey, msg, random) => {
1082
- const [rho, _K, tr, s1, s2, t0] = secretCoder.decode(secretKey);
1083
- const A = [];
1084
- const xof = XOF1282(rho);
1085
- for (let i = 0;i < K; i++) {
1086
- const pv = [];
1087
- for (let j = 0;j < L; j++)
1088
- pv.push(RejNTTPoly(xof.get(j, i)));
1089
- A.push(pv);
1090
- }
1091
- xof.clean();
1092
- for (let i = 0;i < L; i++)
1093
- NTT.encode(s1[i]);
1094
- for (let i = 0;i < K; i++) {
1095
- NTT.encode(s2[i]);
1096
- NTT.encode(t0[i]);
1097
- }
1098
- const mu = shake256.create({ dkLen: CRH_BYTES }).update(tr).update(msg).digest();
1099
- const rnd = random ? random : new Uint8Array(32);
1100
- ensureBytes(rnd);
1101
- const rhoprime = shake256.create({ dkLen: CRH_BYTES }).update(_K).update(rnd).update(mu).digest();
1102
- ensureBytes(rhoprime, CRH_BYTES);
1103
- const x256 = XOF2562(rhoprime, ZCoder.bytesLen);
1104
- main_loop:
1105
- for (let kappa = 0;; ) {
1106
- const y = [];
1107
- for (let i = 0;i < L; i++, kappa++)
1108
- y.push(ZCoder.decode(x256.get(kappa & 255, kappa >> 8)()));
1109
- const z = y.map((i) => NTT.encode(i.slice()));
1110
- const w = [];
1111
- for (let i = 0;i < K; i++) {
1112
- const wi = newPoly(N);
1113
- for (let j = 0;j < L; j++)
1114
- polyAdd(wi, MultiplyNTTs(A[i][j], z[j]));
1115
- NTT.decode(wi);
1116
- w.push(wi);
1117
- }
1118
- const w1 = w.map((j) => j.map(HighBits));
1119
- const cTilde = shake256.create({ dkLen: C_TILDE_BYTES }).update(mu).update(W1Vec.encode(w1)).digest();
1120
- const cHat = NTT.encode(SampleInBall(cTilde));
1121
- const cs1 = s1.map((i) => MultiplyNTTs(i, cHat));
1122
- for (let i = 0;i < L; i++) {
1123
- polyAdd(NTT.decode(cs1[i]), y[i]);
1124
- if (polyChknorm(cs1[i], GAMMA1 - BETA))
1125
- continue main_loop;
1126
- }
1127
- let cnt = 0;
1128
- const h = [];
1129
- for (let i = 0;i < K; i++) {
1130
- const cs2 = NTT.decode(MultiplyNTTs(s2[i], cHat));
1131
- const r0 = polySub(w[i], cs2).map(LowBits);
1132
- if (polyChknorm(r0, GAMMA2 - BETA))
1133
- continue main_loop;
1134
- const ct0 = NTT.decode(MultiplyNTTs(t0[i], cHat));
1135
- if (polyChknorm(ct0, GAMMA2))
1136
- continue main_loop;
1137
- polyAdd(r0, ct0);
1138
- const hint = polyMakeHint(r0, w1[i]);
1139
- h.push(hint.v);
1140
- cnt += hint.cnt;
1141
- }
1142
- if (cnt > OMEGA)
1143
- continue;
1144
- x256.clean();
1145
- const res = sigCoder.encode([cTilde, cs1, h]);
1146
- cleanBytes(cTilde, cs1, h, cHat, w1, w, z, y, rhoprime, mu, s1, s2, t0, ...A);
1147
- return res;
1148
- }
1149
- throw new Error("Unreachable code path reached, report this error");
1150
- },
1151
- verify: (publicKey, msg, sig) => {
1152
- const [rho, t1] = publicCoder.decode(publicKey);
1153
- const tr = shake256(publicKey, { dkLen: TR_BYTES });
1154
- if (sig.length !== sigCoder.bytesLen)
1155
- return false;
1156
- const [cTilde, z, h] = sigCoder.decode(sig);
1157
- if (h === false)
1158
- return false;
1159
- for (let i = 0;i < L; i++)
1160
- if (polyChknorm(z[i], GAMMA1 - BETA))
1161
- return false;
1162
- const mu = shake256.create({ dkLen: CRH_BYTES }).update(tr).update(msg).digest();
1163
- const c = NTT.encode(SampleInBall(cTilde));
1164
- const zNtt = z.map((i) => i.slice());
1165
- for (let i = 0;i < L; i++)
1166
- NTT.encode(zNtt[i]);
1167
- const wTick1 = [];
1168
- const xof = XOF1282(rho);
1169
- for (let i = 0;i < K; i++) {
1170
- const ct12d = MultiplyNTTs(NTT.encode(polyShiftl(t1[i])), c);
1171
- const Az = newPoly(N);
1172
- for (let j = 0;j < L; j++) {
1173
- const aij = RejNTTPoly(xof.get(j, i));
1174
- polyAdd(Az, MultiplyNTTs(aij, zNtt[j]));
1175
- }
1176
- const wApprox = NTT.decode(polySub(Az, ct12d));
1177
- wTick1.push(polyUseHint(wApprox, h[i]));
1178
- }
1179
- xof.clean();
1180
- const c2 = shake256.create({ dkLen: C_TILDE_BYTES }).update(mu).update(W1Vec.encode(wTick1)).digest();
1181
- for (const t of h) {
1182
- const sum = t.reduce((acc, i) => acc + i, 0);
1183
- if (!(sum <= OMEGA))
1184
- return false;
1185
- }
1186
- for (const t of z)
1187
- if (polyChknorm(t, GAMMA1 - BETA))
1188
- return false;
1189
- return equalBytes(cTilde, c2);
1190
- }
1191
- };
1192
- const getMessage = (msg, ctx = EMPTY) => {
1193
- ensureBytes(msg);
1194
- ensureBytes(ctx);
1195
- if (ctx.length > 255)
1196
- throw new Error("context should be less than 255 bytes");
1197
- return concatBytes(new Uint8Array([0, ctx.length]), ctx, msg);
1198
- };
1199
- return {
1200
- internal,
1201
- keygen: internal.keygen,
1202
- signRandBytes: internal.signRandBytes,
1203
- sign: (secretKey, msg, ctx = EMPTY, random) => {
1204
- const M = getMessage(msg, ctx);
1205
- const res = internal.sign(secretKey, M, random);
1206
- M.fill(0);
1207
- return res;
1208
- },
1209
- verify: (publicKey, msg, sig, ctx = EMPTY) => {
1210
- return internal.verify(publicKey, getMessage(msg, ctx), sig);
1211
- }
1212
- };
1213
- }
1214
- var ml_dsa44 = /* @__PURE__ */ getDilithium({
1215
- ...PARAMS[2],
1216
- CRH_BYTES: 64,
1217
- TR_BYTES: 64,
1218
- C_TILDE_BYTES: 32,
1219
- XOF128,
1220
- XOF256
1221
- });
1222
- var ml_dsa65 = /* @__PURE__ */ getDilithium({
1223
- ...PARAMS[3],
1224
- CRH_BYTES: 64,
1225
- TR_BYTES: 64,
1226
- C_TILDE_BYTES: 48,
1227
- XOF128,
1228
- XOF256
1229
- });
1230
- var ml_dsa87 = /* @__PURE__ */ getDilithium({
1231
- ...PARAMS[5],
1232
- CRH_BYTES: 64,
1233
- TR_BYTES: 64,
1234
- C_TILDE_BYTES: 64,
1235
- XOF128,
1236
- XOF256
1237
- });
1238
-
1239
- // ../crypto/src/mlDsa.ts
1240
- function toBase64(bytes) {
1241
- let binary = "";
1242
- for (const byte of bytes)
1243
- binary += String.fromCharCode(byte);
1244
- return btoa(binary);
1245
- }
1246
- function fromBase64(b64) {
1247
- return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
1248
- }
1249
- function sign(privateKey, message) {
1250
- const secretKey = fromBase64(privateKey);
1251
- const msg = new TextEncoder().encode(message);
1252
- const signature = ml_dsa65.sign(secretKey, msg);
1253
- return toBase64(signature);
1254
- }
1255
- // ../crypto/src/xmp.ts
1256
- var XMP_NS = "http://ns.adobe.com/xap/1.0/\x00";
1257
- var XMP_NS_BYTES = new TextEncoder().encode(XMP_NS);
1258
15
  // src/methods/sign.ts
1259
16
  function toUint8Array(content) {
1260
17
  if (typeof content === "string")
@@ -1263,38 +20,23 @@ function toUint8Array(content) {
1263
20
  }
1264
21
  async function signContent(baseUrl, apiKey, input) {
1265
22
  const bytes = toUint8Array(input.content);
1266
- const contentHash = hash(bytes);
1267
- const watermarkId = input.watermarkId ?? crypto.randomUUID();
1268
- const signedAt = new Date().toISOString();
1269
- const signedPayload = {
1270
- generator_id: input.generatorId,
1271
- model: input.model,
1272
- content_hash: contentHash,
1273
- watermark_id: watermarkId,
1274
- signed_at: signedAt
1275
- };
1276
- const message = canonicalJson(signedPayload);
1277
- const signature = sign(input.privateKey, message);
1278
- const res = await fetch(`${baseUrl}/v1/records`, {
23
+ const form = new FormData;
24
+ form.append("image", new Blob([bytes]), "image");
25
+ form.append("model", input.model);
26
+ form.append("generator_id", input.generatorId);
27
+ const res = await fetch(`${baseUrl}/v1/sign`, {
1279
28
  method: "POST",
1280
- headers: {
1281
- "Content-Type": "application/json",
1282
- Authorization: `Bearer ${apiKey}`
1283
- },
1284
- body: JSON.stringify({
1285
- watermark_id: watermarkId,
1286
- generator_id: input.generatorId,
1287
- model: input.model,
1288
- content_hash: contentHash,
1289
- signature,
1290
- signed_payload: signedPayload
1291
- })
29
+ headers: { Authorization: `Bearer ${apiKey}` },
30
+ body: form
1292
31
  });
1293
32
  if (!res.ok) {
1294
33
  const err = await res.json().catch(() => ({}));
1295
34
  throw new Error(err.error ?? `HTTP ${res.status}`);
1296
35
  }
1297
- return res.json();
36
+ const token = res.headers.get("X-Certivu-Token") ?? "";
37
+ const record_id = res.headers.get("X-Certivu-Record-Id") ?? "";
38
+ const watermarkedContent = new Uint8Array(await res.arrayBuffer());
39
+ return { token, record_id, watermarkedContent };
1298
40
  }
1299
41
 
1300
42
  // src/methods/verify.ts
@@ -1354,21 +96,16 @@ class CertivuClient {
1354
96
  apiKey;
1355
97
  baseUrl;
1356
98
  generatorId;
1357
- privateKey;
1358
99
  constructor(config) {
1359
100
  this.apiKey = config.apiKey;
1360
101
  this.baseUrl = (config.baseUrl ?? "https://api.certivu.ai").replace(/\/$/, "");
1361
102
  this.generatorId = config.generatorId;
1362
- this.privateKey = config.privateKey;
1363
103
  }
1364
104
  async sign(input) {
1365
105
  const generatorId = input.generatorId ?? this.generatorId;
1366
- const privateKey = input.privateKey ?? this.privateKey;
1367
106
  if (!generatorId)
1368
107
  throw new Error("generatorId is required for sign (provide in config or input)");
1369
- if (!privateKey)
1370
- throw new Error("privateKey is required for sign (provide in config or input)");
1371
- return signContent(this.baseUrl, this.apiKey, { ...input, generatorId, privateKey });
108
+ return signContent(this.baseUrl, this.apiKey, { ...input, generatorId });
1372
109
  }
1373
110
  async verify(input) {
1374
111
  return verifyContent(this.baseUrl, this.apiKey, input);
@@ -1379,6 +116,67 @@ class CertivuClient {
1379
116
  async getAuditLog(options) {
1380
117
  return getAuditLog(this.baseUrl, this.apiKey, options);
1381
118
  }
119
+ async getTokenStatus(token) {
120
+ const res = await fetch(`${this.baseUrl}/v1/verify/status/${encodeURIComponent(token)}`);
121
+ if (!res.ok) {
122
+ const err = await res.json().catch(() => ({}));
123
+ throw new Error(err.error ?? `HTTP ${res.status}`);
124
+ }
125
+ return res.json();
126
+ }
127
+ async getAnalyticsOverview(days) {
128
+ const url = new URL(`${this.baseUrl}/v1/analytics/overview`);
129
+ if (days !== undefined)
130
+ url.searchParams.set("days", String(days));
131
+ const res = await fetch(url.toString(), { headers: { Authorization: `Bearer ${this.apiKey}` } });
132
+ if (!res.ok) {
133
+ const err = await res.json().catch(() => ({}));
134
+ throw new Error(err.error ?? `HTTP ${res.status}`);
135
+ }
136
+ return res.json();
137
+ }
138
+ async getRecordAnalytics(recordId) {
139
+ const res = await fetch(`${this.baseUrl}/v1/analytics/records/${encodeURIComponent(recordId)}`, {
140
+ headers: { Authorization: `Bearer ${this.apiKey}` }
141
+ });
142
+ if (!res.ok) {
143
+ const err = await res.json().catch(() => ({}));
144
+ throw new Error(err.error ?? `HTTP ${res.status}`);
145
+ }
146
+ return res.json();
147
+ }
148
+ async listWebhooks() {
149
+ const res = await fetch(`${this.baseUrl}/v1/webhooks`, {
150
+ headers: { Authorization: `Bearer ${this.apiKey}` }
151
+ });
152
+ if (!res.ok) {
153
+ const err = await res.json().catch(() => ({}));
154
+ throw new Error(err.error ?? `HTTP ${res.status}`);
155
+ }
156
+ return res.json();
157
+ }
158
+ async createWebhook(input) {
159
+ const res = await fetch(`${this.baseUrl}/v1/webhooks`, {
160
+ method: "POST",
161
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}` },
162
+ body: JSON.stringify(input)
163
+ });
164
+ if (!res.ok) {
165
+ const err = await res.json().catch(() => ({}));
166
+ throw new Error(err.error ?? `HTTP ${res.status}`);
167
+ }
168
+ return res.json();
169
+ }
170
+ async deleteWebhook(webhookId) {
171
+ const res = await fetch(`${this.baseUrl}/v1/webhooks/${encodeURIComponent(webhookId)}`, {
172
+ method: "DELETE",
173
+ headers: { Authorization: `Bearer ${this.apiKey}` }
174
+ });
175
+ if (!res.ok) {
176
+ const err = await res.json().catch(() => ({}));
177
+ throw new Error(err.error ?? `HTTP ${res.status}`);
178
+ }
179
+ }
1382
180
  }
1383
181
  export {
1384
182
  CertivuClient