starknet 11.8.0 → 11.9.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/CHANGELOG.md +12 -0
- package/dist/falcon/index.d.ts +194 -2
- package/dist/falcon/index.js +833 -154
- package/dist/falcon/index.js.map +1 -1
- package/dist/falcon/index.mjs +828 -150
- package/dist/falcon/index.mjs.map +1 -1
- package/dist/index.d.ts +102 -47
- package/dist/index.global.js +130 -67
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +130 -67
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +130 -67
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/falcon/index.mjs
CHANGED
|
@@ -89,7 +89,7 @@ import {
|
|
|
89
89
|
bytesToHex as bytesToHex2,
|
|
90
90
|
createView,
|
|
91
91
|
hexToBytes,
|
|
92
|
-
randomBytes,
|
|
92
|
+
randomBytes as randomBytes2,
|
|
93
93
|
swap32IfBE,
|
|
94
94
|
u32,
|
|
95
95
|
u8
|
|
@@ -111,6 +111,15 @@ import {
|
|
|
111
111
|
isLE,
|
|
112
112
|
randomBytes as randb
|
|
113
113
|
} from "@noble/hashes/utils.js";
|
|
114
|
+
var abytesDoc = abytes;
|
|
115
|
+
var randomBytes = randb;
|
|
116
|
+
function aarray(item, title, inner = () => {
|
|
117
|
+
}) {
|
|
118
|
+
if (!Array.isArray(item))
|
|
119
|
+
throw new TypeError(`"${title}" expected array, got type=${typeof item}`);
|
|
120
|
+
for (let i = 0; i < item.length; i++) inner(item[i], `${title}[${i}]`);
|
|
121
|
+
return item;
|
|
122
|
+
}
|
|
114
123
|
function aobject(value, title = "object") {
|
|
115
124
|
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
116
125
|
throw new TypeError(
|
|
@@ -118,6 +127,14 @@ function aobject(value, title = "object") {
|
|
|
118
127
|
);
|
|
119
128
|
return value;
|
|
120
129
|
}
|
|
130
|
+
function equalBytes(a, b) {
|
|
131
|
+
a = abytes(a);
|
|
132
|
+
b = abytes(b);
|
|
133
|
+
if (a.length !== b.length) return false;
|
|
134
|
+
let diff = 0;
|
|
135
|
+
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
136
|
+
return diff === 0;
|
|
137
|
+
}
|
|
121
138
|
function copyBytes(bytes) {
|
|
122
139
|
return new Uint8Array(abytes(bytes));
|
|
123
140
|
}
|
|
@@ -140,10 +157,10 @@ function byteSwap64(arr) {
|
|
|
140
157
|
return arr;
|
|
141
158
|
}
|
|
142
159
|
var baswap64If = isLE ? (arr) => arr : byteSwap64;
|
|
143
|
-
function validateOpts(
|
|
144
|
-
if (isBytes(
|
|
145
|
-
aobject(
|
|
146
|
-
const proto = Object.getPrototypeOf(
|
|
160
|
+
function validateOpts(opts2) {
|
|
161
|
+
if (isBytes(opts2)) throw new TypeError('"opts" expected object, got Uint8Array');
|
|
162
|
+
aobject(opts2, "opts");
|
|
163
|
+
const proto = Object.getPrototypeOf(opts2);
|
|
147
164
|
if (proto !== null && proto !== Object.prototype)
|
|
148
165
|
throw new TypeError('"opts" expected a plain object');
|
|
149
166
|
}
|
|
@@ -154,9 +171,9 @@ var SIG_OPT_KEYS = /* @__PURE__ */ Object.freeze([
|
|
|
154
171
|
"context",
|
|
155
172
|
"extraEntropy"
|
|
156
173
|
]);
|
|
157
|
-
function checkOptKeys(
|
|
158
|
-
validateOpts(
|
|
159
|
-
const normalized = Object.assign(/* @__PURE__ */ Object.create(null),
|
|
174
|
+
function checkOptKeys(opts2, allowed) {
|
|
175
|
+
validateOpts(opts2);
|
|
176
|
+
const normalized = Object.assign(/* @__PURE__ */ Object.create(null), opts2);
|
|
160
177
|
for (const [k, v] of Object.entries(normalized)) {
|
|
161
178
|
if (v === void 0) continue;
|
|
162
179
|
if (!allowed.includes(k))
|
|
@@ -166,13 +183,13 @@ function checkOptKeys(opts, allowed) {
|
|
|
166
183
|
}
|
|
167
184
|
return Object.freeze(normalized);
|
|
168
185
|
}
|
|
169
|
-
function validateVerOpts(
|
|
170
|
-
const normalized = checkOptKeys(
|
|
186
|
+
function validateVerOpts(opts2, allowed = VER_OPT_KEYS) {
|
|
187
|
+
const normalized = checkOptKeys(opts2, allowed);
|
|
171
188
|
if (normalized.context !== void 0) abytes(normalized.context, void 0, "opts.context");
|
|
172
189
|
return normalized;
|
|
173
190
|
}
|
|
174
|
-
function validateSigOpts(
|
|
175
|
-
const normalized = checkOptKeys(
|
|
191
|
+
function validateSigOpts(opts2, allowed = SIG_OPT_KEYS) {
|
|
192
|
+
const normalized = checkOptKeys(opts2, allowed);
|
|
176
193
|
if (normalized.context !== void 0) abytes(normalized.context, void 0, "opts.context");
|
|
177
194
|
if (normalized.extraEntropy !== false && normalized.extraEntropy !== void 0)
|
|
178
195
|
abytes(normalized.extraEntropy, void 0, "opts.extraEntropy");
|
|
@@ -209,6 +226,33 @@ function splitCoder(label, ...lengths) {
|
|
|
209
226
|
}
|
|
210
227
|
};
|
|
211
228
|
}
|
|
229
|
+
function vecCoder(c, vecLen) {
|
|
230
|
+
const coder = c;
|
|
231
|
+
const bytesLen = vecLen * coder.bytesLen;
|
|
232
|
+
return {
|
|
233
|
+
bytesLen,
|
|
234
|
+
encode: (u) => {
|
|
235
|
+
const uArr = aarray(u, "u");
|
|
236
|
+
if (uArr.length !== vecLen)
|
|
237
|
+
throw new RangeError(`vecCoder.encode: wrong length=${uArr.length}. Expected: ${vecLen}`);
|
|
238
|
+
const res = new Uint8Array(bytesLen);
|
|
239
|
+
for (let i = 0, pos = 0; i < uArr.length; i++) {
|
|
240
|
+
const b = coder.encode(uArr[i]);
|
|
241
|
+
res.set(b, pos);
|
|
242
|
+
b.fill(0);
|
|
243
|
+
pos += b.length;
|
|
244
|
+
}
|
|
245
|
+
return res;
|
|
246
|
+
},
|
|
247
|
+
decode: (a) => {
|
|
248
|
+
abytes_(a, bytesLen);
|
|
249
|
+
const r = [];
|
|
250
|
+
for (let i = 0; i < a.length; i += coder.bytesLen)
|
|
251
|
+
r.push(coder.decode(a.subarray(i, i + coder.bytesLen)));
|
|
252
|
+
return r;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
}
|
|
212
256
|
function cleanBytes(...list) {
|
|
213
257
|
for (const t of list) {
|
|
214
258
|
if (Array.isArray(t)) for (const b of t) b.fill(0);
|
|
@@ -222,21 +266,21 @@ function getMask(bits) {
|
|
|
222
266
|
}
|
|
223
267
|
|
|
224
268
|
// src/falcon/engine/noble/_crystals.ts
|
|
225
|
-
var genCrystals = (
|
|
226
|
-
const { newPoly, N:
|
|
227
|
-
const mod = (a, modulo =
|
|
269
|
+
var genCrystals = (opts2) => {
|
|
270
|
+
const { newPoly, N: N3, Q: Q4, F: F2, ROOT_OF_UNITY: ROOT_OF_UNITY2, brvBits, isKyber } = opts2;
|
|
271
|
+
const mod = (a, modulo = Q4) => {
|
|
228
272
|
const result = a % modulo | 0;
|
|
229
273
|
return (result >= 0 ? result | 0 : modulo + result | 0) | 0;
|
|
230
274
|
};
|
|
231
|
-
const smod = (a, modulo =
|
|
275
|
+
const smod = (a, modulo = Q4) => {
|
|
232
276
|
const r = mod(a, modulo) | 0;
|
|
233
277
|
return (r > modulo >> 1 ? r - modulo | 0 : r) | 0;
|
|
234
278
|
};
|
|
235
279
|
function getZettas() {
|
|
236
|
-
const out = newPoly(
|
|
237
|
-
for (let i = 0; i <
|
|
280
|
+
const out = newPoly(N3);
|
|
281
|
+
for (let i = 0; i < N3; i++) {
|
|
238
282
|
const b = reverseBits(i, brvBits);
|
|
239
|
-
const p = BigInt(
|
|
283
|
+
const p = BigInt(ROOT_OF_UNITY2) ** BigInt(b) % BigInt(Q4);
|
|
240
284
|
out[i] = Number(p) | 0;
|
|
241
285
|
}
|
|
242
286
|
return out;
|
|
@@ -248,11 +292,11 @@ var genCrystals = (opts) => {
|
|
|
248
292
|
const field = isKyber ? {
|
|
249
293
|
add: (a, b) => {
|
|
250
294
|
const r = a + b | 0;
|
|
251
|
-
return r >=
|
|
295
|
+
return r >= Q4 ? r - Q4 | 0 : r;
|
|
252
296
|
},
|
|
253
297
|
sub: (a, b) => {
|
|
254
298
|
const r = a - b | 0;
|
|
255
|
-
return r < 0 ? r +
|
|
299
|
+
return r < 0 ? r + Q4 | 0 : r;
|
|
256
300
|
},
|
|
257
301
|
mul: (a, b) => mod((a | 0) * (b | 0)) | 0,
|
|
258
302
|
inv
|
|
@@ -263,7 +307,7 @@ var genCrystals = (opts) => {
|
|
|
263
307
|
inv
|
|
264
308
|
};
|
|
265
309
|
const nttOpts = {
|
|
266
|
-
N:
|
|
310
|
+
N: N3,
|
|
267
311
|
roots: nttZetas,
|
|
268
312
|
invertButterflies: true,
|
|
269
313
|
skipStages: isKyber ? 1 : 0,
|
|
@@ -277,18 +321,18 @@ var genCrystals = (opts) => {
|
|
|
277
321
|
},
|
|
278
322
|
decode: (r) => {
|
|
279
323
|
dit(r);
|
|
280
|
-
for (let i = 0; i < r.length; i++) r[i] = mod(
|
|
324
|
+
for (let i = 0; i < r.length; i++) r[i] = mod(F2 * r[i]);
|
|
281
325
|
return r;
|
|
282
326
|
}
|
|
283
327
|
};
|
|
284
328
|
const bitsCoder = (d, c) => {
|
|
285
|
-
for (let i = 0, bufLen = 0; i <
|
|
329
|
+
for (let i = 0, bufLen = 0; i < N3; i++) {
|
|
286
330
|
bufLen += d;
|
|
287
331
|
if (bufLen > 32) getMask(bufLen);
|
|
288
332
|
bufLen %= 8;
|
|
289
333
|
}
|
|
290
334
|
const mask = getMask(d);
|
|
291
|
-
const bytesLen = d * (
|
|
335
|
+
const bytesLen = d * (N3 / 8);
|
|
292
336
|
return {
|
|
293
337
|
bytesLen,
|
|
294
338
|
encode: (poly_) => {
|
|
@@ -302,7 +346,7 @@ var genCrystals = (opts) => {
|
|
|
302
346
|
return r;
|
|
303
347
|
},
|
|
304
348
|
decode: (bytes) => {
|
|
305
|
-
const r = newPoly(
|
|
349
|
+
const r = newPoly(N3);
|
|
306
350
|
for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < bytes.length; i++) {
|
|
307
351
|
buf |= bytes[i] << bufLen;
|
|
308
352
|
bufLen += 8;
|
|
@@ -323,15 +367,44 @@ var genCrystals = (opts) => {
|
|
|
323
367
|
bitsCoder
|
|
324
368
|
};
|
|
325
369
|
};
|
|
370
|
+
var createXofShake = (shake) => (seed, blockLen) => {
|
|
371
|
+
if (!blockLen) blockLen = shake.blockLen;
|
|
372
|
+
const _seed = new Uint8Array(seed.length + 2);
|
|
373
|
+
_seed.set(seed);
|
|
374
|
+
const seedLen = seed.length;
|
|
375
|
+
const buf = new Uint8Array(blockLen);
|
|
376
|
+
let h = shake.create({});
|
|
377
|
+
let calls = 0;
|
|
378
|
+
let xofs = 0;
|
|
379
|
+
return {
|
|
380
|
+
stats: () => ({ calls, xofs }),
|
|
381
|
+
get: (x, y) => {
|
|
382
|
+
_seed[seedLen + 0] = x;
|
|
383
|
+
_seed[seedLen + 1] = y;
|
|
384
|
+
h.destroy();
|
|
385
|
+
h = shake.create({}).update(_seed);
|
|
386
|
+
calls++;
|
|
387
|
+
return () => {
|
|
388
|
+
xofs++;
|
|
389
|
+
return h.xofInto(buf);
|
|
390
|
+
};
|
|
391
|
+
},
|
|
392
|
+
clean: () => {
|
|
393
|
+
h.destroy();
|
|
394
|
+
cleanBytes(buf, _seed);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
var XOF128 = /* @__PURE__ */ createXofShake(shake128);
|
|
326
399
|
|
|
327
400
|
// src/falcon/engine/noble/falcon.ts
|
|
328
|
-
var bitsCoderMSB = (newPoly,
|
|
401
|
+
var bitsCoderMSB = (newPoly, N3, d, c) => {
|
|
329
402
|
const mask = getMask(d);
|
|
330
|
-
const bytesLen = d * (
|
|
403
|
+
const bytesLen = d * (N3 / 8);
|
|
331
404
|
return {
|
|
332
405
|
bytesLen,
|
|
333
406
|
encode: (poly) => {
|
|
334
|
-
if (poly.length !==
|
|
407
|
+
if (poly.length !== N3) throw new Error(`wrong length: expected ${N3}, got ${poly.length}`);
|
|
335
408
|
const r = new Uint8Array(bytesLen);
|
|
336
409
|
for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < poly.length; i++) {
|
|
337
410
|
buf = buf << d | c.encode(poly[i]) & mask;
|
|
@@ -341,7 +414,7 @@ var bitsCoderMSB = (newPoly, N2, d, c) => {
|
|
|
341
414
|
return r;
|
|
342
415
|
},
|
|
343
416
|
decode: (bytes) => {
|
|
344
|
-
const r = newPoly(
|
|
417
|
+
const r = newPoly(N3);
|
|
345
418
|
for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < bytes.length; i++) {
|
|
346
419
|
buf = buf << 8 | bytes[i];
|
|
347
420
|
bufLen += 8;
|
|
@@ -451,43 +524,43 @@ var cleanCPoly = (...list) => {
|
|
|
451
524
|
}
|
|
452
525
|
};
|
|
453
526
|
function getComplex(field) {
|
|
454
|
-
const
|
|
527
|
+
const F2 = field;
|
|
455
528
|
return {
|
|
456
529
|
lift: (x) => {
|
|
457
530
|
if (x.re !== void 0 && x.im !== void 0) return x;
|
|
458
|
-
return { re: x, im:
|
|
531
|
+
return { re: x, im: F2.ZERO };
|
|
459
532
|
},
|
|
460
533
|
add: (a, b) => ({
|
|
461
|
-
re:
|
|
462
|
-
im:
|
|
534
|
+
re: F2.add(a.re, b.re),
|
|
535
|
+
im: F2.add(a.im, b.im)
|
|
463
536
|
}),
|
|
464
537
|
sub: (a, b) => ({
|
|
465
|
-
re:
|
|
466
|
-
im:
|
|
538
|
+
re: F2.sub(a.re, b.re),
|
|
539
|
+
im: F2.sub(a.im, b.im)
|
|
467
540
|
}),
|
|
468
541
|
mul: (a, b) => ({
|
|
469
|
-
re:
|
|
470
|
-
im:
|
|
542
|
+
re: F2.sub(F2.mul(a.re, b.re), F2.mul(a.im, b.im)),
|
|
543
|
+
im: F2.add(F2.mul(a.re, b.im), F2.mul(a.im, b.re))
|
|
471
544
|
}),
|
|
472
545
|
div: (a, b) => {
|
|
473
|
-
const denom =
|
|
546
|
+
const denom = F2.add(F2.mul(b.re, b.re), F2.mul(b.im, b.im));
|
|
474
547
|
return {
|
|
475
|
-
re:
|
|
476
|
-
im:
|
|
548
|
+
re: F2.div(F2.add(F2.mul(a.re, b.re), F2.mul(a.im, b.im)), denom),
|
|
549
|
+
im: F2.div(F2.sub(F2.mul(a.im, b.re), F2.mul(a.re, b.im)), denom)
|
|
477
550
|
};
|
|
478
551
|
},
|
|
479
|
-
neg: (a) => ({ re:
|
|
480
|
-
conj: (a) => ({ re: a.re, im:
|
|
552
|
+
neg: (a) => ({ re: F2.neg(a.re), im: F2.neg(a.im) }),
|
|
553
|
+
conj: (a) => ({ re: a.re, im: F2.neg(a.im) }),
|
|
481
554
|
scale: (a, x) => ({
|
|
482
|
-
re:
|
|
483
|
-
im:
|
|
555
|
+
re: F2.mul(a.re, x),
|
|
556
|
+
im: F2.mul(a.im, x)
|
|
484
557
|
}),
|
|
485
558
|
// a.re * a.re + a.im * a.im + b.re * b.re + b.im * b.im;
|
|
486
|
-
magSqSum: (a, b) =>
|
|
487
|
-
|
|
488
|
-
|
|
559
|
+
magSqSum: (a, b) => F2.add(
|
|
560
|
+
F2.add(F2.add(F2.mul(a.re, a.re), F2.mul(a.im, a.im)), F2.mul(b.re, b.re)),
|
|
561
|
+
F2.mul(b.im, b.im)
|
|
489
562
|
),
|
|
490
|
-
eql: (a, b) =>
|
|
563
|
+
eql: (a, b) => F2.eql(a.re, b.re) && F2.eql(a.im, b.im),
|
|
491
564
|
clone: (a) => ({ re: a.re, im: a.im }),
|
|
492
565
|
inv: () => {
|
|
493
566
|
throw new Error("not implemented");
|
|
@@ -496,8 +569,8 @@ function getComplex(field) {
|
|
|
496
569
|
}
|
|
497
570
|
var ComplexArr = {
|
|
498
571
|
decode(lst) {
|
|
499
|
-
const
|
|
500
|
-
const hn =
|
|
572
|
+
const N3 = lst.length;
|
|
573
|
+
const hn = N3 >> 1;
|
|
501
574
|
const len = lst.length;
|
|
502
575
|
if (len === 0) return [];
|
|
503
576
|
if (len % 2 !== 0)
|
|
@@ -768,11 +841,11 @@ var intField = {
|
|
|
768
841
|
function getIntPoly(logn) {
|
|
769
842
|
const n = 1 << logn;
|
|
770
843
|
const newPoly = (n2) => new Uint16Array(n2);
|
|
771
|
-
const
|
|
844
|
+
const F2 = Number(invert(BigInt(n), QBig));
|
|
772
845
|
const { mod, smod, NTT } = genCrystals({
|
|
773
846
|
N: n,
|
|
774
847
|
Q: Q2,
|
|
775
|
-
F,
|
|
848
|
+
F: F2,
|
|
776
849
|
ROOT_OF_UNITY: 7,
|
|
777
850
|
newPoly,
|
|
778
851
|
isKyber: false,
|
|
@@ -922,9 +995,9 @@ function ApproxExp(x, ccs) {
|
|
|
922
995
|
for (let i = ev.length - 2; i >= 0; i--) z = z * y + ev[i];
|
|
923
996
|
return ccs * (1 + z * y);
|
|
924
997
|
}
|
|
925
|
-
function genFalcon(
|
|
926
|
-
const { N:
|
|
927
|
-
const logn = Math.log2(
|
|
998
|
+
function genFalcon(opts2) {
|
|
999
|
+
const { N: N3 } = opts2;
|
|
1000
|
+
const logn = Math.log2(N3);
|
|
928
1001
|
const id = (n) => n;
|
|
929
1002
|
const { newPoly, intPoly, signedCoder } = getIntPoly(logn);
|
|
930
1003
|
const floatPoly = getFloatPoly(logn);
|
|
@@ -1011,7 +1084,7 @@ function genFalcon(opts) {
|
|
|
1011
1084
|
}
|
|
1012
1085
|
return a;
|
|
1013
1086
|
}
|
|
1014
|
-
reduce(logn2, f, g,
|
|
1087
|
+
reduce(logn2, f, g, F2, G, logn_top) {
|
|
1015
1088
|
const n = 1 << logn2;
|
|
1016
1089
|
const depth = logn_top - logn2;
|
|
1017
1090
|
const floatPoly2 = getFloatPoly(logn2);
|
|
@@ -1038,7 +1111,7 @@ function genFalcon(opts) {
|
|
|
1038
1111
|
while (true) {
|
|
1039
1112
|
let scaleFG = _31n * (FGlen - _10n);
|
|
1040
1113
|
for (let i = 0; i < n; i++) {
|
|
1041
|
-
Fx[i] = Number(
|
|
1114
|
+
Fx[i] = Number(F2[i] >> scaleFG);
|
|
1042
1115
|
Gx[i] = Number(G[i] >> scaleFG);
|
|
1043
1116
|
}
|
|
1044
1117
|
const rt2 = floatPoly2.mul(floatPoly2.FFT(floatPoly2.to(Gx)), rt4);
|
|
@@ -1053,7 +1126,7 @@ function genFalcon(opts) {
|
|
|
1053
1126
|
if (val <= -BOUND || val >= BOUND) return false;
|
|
1054
1127
|
k[i] = BigInt(Math.round(val));
|
|
1055
1128
|
}
|
|
1056
|
-
|
|
1129
|
+
F2 = this.subMul(logn2, F2, f, k, scaleK);
|
|
1057
1130
|
G = this.subMul(logn2, G, g, k, scaleK);
|
|
1058
1131
|
const maxfgNew = scaleK + BigInt(Math.round(fgMaxBits)) + _10n;
|
|
1059
1132
|
if (maxfgNew < maxFGBits) maxFGBits = maxfgNew;
|
|
@@ -1065,7 +1138,7 @@ function genFalcon(opts) {
|
|
|
1065
1138
|
return true;
|
|
1066
1139
|
}
|
|
1067
1140
|
// This is recursive thing that goes from logn to 0
|
|
1068
|
-
solveBranch(logn2, f, g,
|
|
1141
|
+
solveBranch(logn2, f, g, F2, G, logn_top) {
|
|
1069
1142
|
if (logn2 === 0) {
|
|
1070
1143
|
const xf = f[0];
|
|
1071
1144
|
const xg = g[0];
|
|
@@ -1073,7 +1146,7 @@ function genFalcon(opts) {
|
|
|
1073
1146
|
try {
|
|
1074
1147
|
const u1 = invert(xf, xg);
|
|
1075
1148
|
const v1 = (_1n - u1 * xf) / xg;
|
|
1076
|
-
|
|
1149
|
+
F2[0] = -v1 * QBig;
|
|
1077
1150
|
G[0] = u1 * QBig;
|
|
1078
1151
|
return true;
|
|
1079
1152
|
} catch (e) {
|
|
@@ -1089,9 +1162,9 @@ function genFalcon(opts) {
|
|
|
1089
1162
|
const Fp = new Array(hn);
|
|
1090
1163
|
const Gp = new Array(hn);
|
|
1091
1164
|
if (!this.solveBranch(logn2 - 1, fp, gp, Fp, Gp, logn_top)) return false;
|
|
1092
|
-
|
|
1165
|
+
F2 = this.mulConjD(logn2, F2, g, Fp);
|
|
1093
1166
|
G = this.mulConjD(logn2, G, f, Gp);
|
|
1094
|
-
return this.reduce(logn2, f, g,
|
|
1167
|
+
return this.reduce(logn2, f, g, F2, G, logn_top);
|
|
1095
1168
|
}
|
|
1096
1169
|
solve(f, g) {
|
|
1097
1170
|
const n = 1 << logn;
|
|
@@ -1100,16 +1173,16 @@ function genFalcon(opts) {
|
|
|
1100
1173
|
const bF = new Array(n);
|
|
1101
1174
|
const bG = new Array(n);
|
|
1102
1175
|
if (!this.solveBranch(logn, bf, bg, bF, bG)) return false;
|
|
1103
|
-
const
|
|
1176
|
+
const F2 = new Int8Array(n);
|
|
1104
1177
|
const G = new Int8Array(n);
|
|
1105
1178
|
for (let i = 0; i < n; i++) {
|
|
1106
1179
|
const x = bF[i];
|
|
1107
1180
|
const y = bG[i];
|
|
1108
1181
|
if (x < -127 || x > 127 || y < -127 || y > 127) return false;
|
|
1109
|
-
|
|
1182
|
+
F2[i] = Number(x);
|
|
1110
1183
|
G[i] = Number(y);
|
|
1111
1184
|
}
|
|
1112
|
-
return [
|
|
1185
|
+
return [F2, G];
|
|
1113
1186
|
}
|
|
1114
1187
|
generate() {
|
|
1115
1188
|
let max = 1e6;
|
|
@@ -1118,8 +1191,8 @@ function genFalcon(opts) {
|
|
|
1118
1191
|
if (curr++ === max) throw new Error("can't generate key");
|
|
1119
1192
|
const f = this.polyGauss();
|
|
1120
1193
|
const g = this.polyGauss();
|
|
1121
|
-
let lim = 1 <<
|
|
1122
|
-
for (let u = 0; u <
|
|
1194
|
+
let lim = 1 << opts2.fgBits - 1;
|
|
1195
|
+
for (let u = 0; u < N3; u++) {
|
|
1123
1196
|
if (f[u] >= lim || f[u] <= -lim || g[u] >= lim || g[u] <= -lim) {
|
|
1124
1197
|
lim = -1;
|
|
1125
1198
|
break;
|
|
@@ -1158,7 +1231,7 @@ function genFalcon(opts) {
|
|
|
1158
1231
|
}
|
|
1159
1232
|
}
|
|
1160
1233
|
const modqCoder = () => {
|
|
1161
|
-
const coder = bitsCoderMSB(newPoly,
|
|
1234
|
+
const coder = bitsCoderMSB(newPoly, N3, 14, {
|
|
1162
1235
|
encode: id,
|
|
1163
1236
|
decode: id
|
|
1164
1237
|
});
|
|
@@ -1184,7 +1257,7 @@ function genFalcon(opts) {
|
|
|
1184
1257
|
};
|
|
1185
1258
|
const trimI8Coder = (bits) => {
|
|
1186
1259
|
const shift = 32 - bits;
|
|
1187
|
-
const coder = bitsCoderMSB((len) => new Int8Array(len),
|
|
1260
|
+
const coder = bitsCoderMSB((len) => new Int8Array(len), N3, bits, {
|
|
1188
1261
|
encode: (v) => v & (1 << bits) - 1,
|
|
1189
1262
|
decode: (w) => (w & getMask(bits)) << shift >> shift
|
|
1190
1263
|
});
|
|
@@ -1206,25 +1279,25 @@ function genFalcon(opts) {
|
|
|
1206
1279
|
}
|
|
1207
1280
|
};
|
|
1208
1281
|
};
|
|
1209
|
-
const fgCoder = trimI8Coder(
|
|
1210
|
-
const FGCoder = trimI8Coder(
|
|
1282
|
+
const fgCoder = trimI8Coder(opts2.fgBits);
|
|
1283
|
+
const FGCoder = trimI8Coder(opts2.FGBits);
|
|
1211
1284
|
const secretKeyCoder = headerCoder(
|
|
1212
1285
|
80 + logn,
|
|
1213
1286
|
splitCoder("falcon.secretKey", fgCoder, fgCoder, FGCoder)
|
|
1214
1287
|
);
|
|
1215
1288
|
const publicKeyCoder = headerCoder(0 + logn, modqCoder());
|
|
1216
1289
|
const decodePaddedSig = (s2) => {
|
|
1217
|
-
const normalized = compCoder(
|
|
1290
|
+
const normalized = compCoder(N3).encode(compCoder(N3).decode(s2));
|
|
1218
1291
|
for (let i = normalized.length; i < s2.length; i++)
|
|
1219
1292
|
if (s2[i] !== 0) throw new Error("non-zero padding");
|
|
1220
1293
|
return normalized;
|
|
1221
1294
|
};
|
|
1222
1295
|
const decodeUnpaddedSig = (s2) => {
|
|
1223
|
-
const normalized = compCoder(
|
|
1296
|
+
const normalized = compCoder(N3).encode(compCoder(N3).decode(s2));
|
|
1224
1297
|
if (normalized.length !== s2.length) throw new Error("wrong signature length");
|
|
1225
1298
|
return s2;
|
|
1226
1299
|
};
|
|
1227
|
-
const decodeSig =
|
|
1300
|
+
const decodeSig = opts2.padded ? decodePaddedSig : decodeUnpaddedSig;
|
|
1228
1301
|
const SignatureCoderBasic = (logn2) => {
|
|
1229
1302
|
const TYPE_BYTE = 32 + logn2;
|
|
1230
1303
|
return {
|
|
@@ -1261,7 +1334,7 @@ function genFalcon(opts) {
|
|
|
1261
1334
|
};
|
|
1262
1335
|
};
|
|
1263
1336
|
const SignatureCoderPadded = (logn2) => {
|
|
1264
|
-
const sigLen =
|
|
1337
|
+
const sigLen = opts2.paddedLen;
|
|
1265
1338
|
return {
|
|
1266
1339
|
encode({ msg, nonce, s2 }) {
|
|
1267
1340
|
return headerCoder(
|
|
@@ -1282,19 +1355,19 @@ function genFalcon(opts) {
|
|
|
1282
1355
|
};
|
|
1283
1356
|
};
|
|
1284
1357
|
const SignatureCoderDetached = (logn2) => {
|
|
1285
|
-
const paddedSigLen =
|
|
1286
|
-
const getSigLen = (s2) =>
|
|
1358
|
+
const paddedSigLen = opts2.sigLen - 1 - NONCELEN;
|
|
1359
|
+
const getSigLen = (s2) => opts2.padded ? paddedSigLen : s2.length;
|
|
1287
1360
|
return {
|
|
1288
1361
|
encode({ nonce, s2 }) {
|
|
1289
1362
|
return headerCoder(
|
|
1290
1363
|
48 + logn2,
|
|
1291
1364
|
splitCoder("falcon.detachedSignature", NONCELEN, getSigLen(s2))
|
|
1292
|
-
).encode([nonce,
|
|
1365
|
+
).encode([nonce, opts2.padded ? pad(paddedSigLen).encode(s2) : s2]);
|
|
1293
1366
|
},
|
|
1294
1367
|
decode(data) {
|
|
1295
|
-
if (!
|
|
1368
|
+
if (!opts2.padded && data.length > 1 + NONCELEN + opts2.maxS2Len)
|
|
1296
1369
|
throw new Error("detached signature too long");
|
|
1297
|
-
const payloadLen =
|
|
1370
|
+
const payloadLen = opts2.padded ? paddedSigLen : data.length - NONCELEN - 1;
|
|
1298
1371
|
const [nonce, raw] = headerCoder(
|
|
1299
1372
|
48 + logn2,
|
|
1300
1373
|
splitCoder("falcon.detachedSignature", NONCELEN, payloadLen)
|
|
@@ -1304,10 +1377,10 @@ function genFalcon(opts) {
|
|
|
1304
1377
|
}
|
|
1305
1378
|
};
|
|
1306
1379
|
};
|
|
1307
|
-
const SignatureCoder = (
|
|
1380
|
+
const SignatureCoder = (opts2.padded ? SignatureCoderPadded : SignatureCoderBasic)(logn);
|
|
1308
1381
|
const invertF = (f) => {
|
|
1309
1382
|
const tt = intPoly.ntt(signedCoder.decode(f));
|
|
1310
|
-
for (let u = 0; u <
|
|
1383
|
+
for (let u = 0; u < N3; u++)
|
|
1311
1384
|
if (tt[u] === 0) throw new Error("invalid secretKey: non-invertible f");
|
|
1312
1385
|
return tt;
|
|
1313
1386
|
};
|
|
@@ -1318,13 +1391,13 @@ function genFalcon(opts) {
|
|
|
1318
1391
|
cleanBytes(tt);
|
|
1319
1392
|
return res2;
|
|
1320
1393
|
}
|
|
1321
|
-
function completePrivate(f, g,
|
|
1394
|
+
function completePrivate(f, g, F2) {
|
|
1322
1395
|
let t1 = intPoly.toMontgomery(intPoly.ntt(signedCoder.decode(g)));
|
|
1323
|
-
const t2 = intPoly.ntt(signedCoder.decode(
|
|
1396
|
+
const t2 = intPoly.ntt(signedCoder.decode(F2));
|
|
1324
1397
|
const tt = invertF(f);
|
|
1325
1398
|
t1 = intPoly.div(intPoly.mul(t1, t2), tt);
|
|
1326
|
-
const G = new Int8Array(
|
|
1327
|
-
for (let u = 0; u <
|
|
1399
|
+
const G = new Int8Array(N3);
|
|
1400
|
+
for (let u = 0; u < N3; u++) {
|
|
1328
1401
|
let w = t1[u];
|
|
1329
1402
|
w -= Q2 & ~-(w - Qhalf >>> 31);
|
|
1330
1403
|
const gi = w | 0;
|
|
@@ -1339,9 +1412,9 @@ function genFalcon(opts) {
|
|
|
1339
1412
|
}
|
|
1340
1413
|
function HashToPoint(nonce, msg) {
|
|
1341
1414
|
const h = shake2562.create().update(nonce).update(msg);
|
|
1342
|
-
const c = new Uint16Array(
|
|
1415
|
+
const c = new Uint16Array(N3);
|
|
1343
1416
|
const kQ = 5 * Q2;
|
|
1344
|
-
for (let i = 0; i <
|
|
1417
|
+
for (let i = 0; i < N3; ) {
|
|
1345
1418
|
const tmp = h.xof(2);
|
|
1346
1419
|
let w = tmp[0] << 8 | tmp[1];
|
|
1347
1420
|
if (w < kQ) c[i++] = w % Q2;
|
|
@@ -1587,7 +1660,7 @@ function genFalcon(opts) {
|
|
|
1587
1660
|
return this.ffsampRec(this.logn, t0f, t1f, this.g00, this.g01, this.g11);
|
|
1588
1661
|
}
|
|
1589
1662
|
}
|
|
1590
|
-
const signRaw = (sk, msg, maxLen, rnd =
|
|
1663
|
+
const signRaw = (sk, msg, maxLen, rnd = randomBytes2) => {
|
|
1591
1664
|
abytes2(msg, void 0, "msg");
|
|
1592
1665
|
abytes2(sk, secretKeyCoder.bytesLen, "secretKey");
|
|
1593
1666
|
const nonce = rnd(40);
|
|
@@ -1596,15 +1669,15 @@ function genFalcon(opts) {
|
|
|
1596
1669
|
const seed = rnd(48);
|
|
1597
1670
|
abytes2(seed, 48, "seed");
|
|
1598
1671
|
try {
|
|
1599
|
-
const [f, g,
|
|
1672
|
+
const [f, g, F2] = secretKeyCoder.decode(sk);
|
|
1600
1673
|
try {
|
|
1601
|
-
const G = completePrivate(f, g,
|
|
1674
|
+
const G = completePrivate(f, g, F2);
|
|
1602
1675
|
const b00 = floatPoly.FFT(floatPoly.convSmall(g));
|
|
1603
1676
|
const b01 = floatPoly.FFT(floatPoly.neg(floatPoly.convSmall(f)));
|
|
1604
1677
|
const b10 = floatPoly.FFT(floatPoly.convSmall(G));
|
|
1605
|
-
const b11 = floatPoly.FFT(floatPoly.neg(floatPoly.convSmall(
|
|
1678
|
+
const b11 = floatPoly.FFT(floatPoly.neg(floatPoly.convSmall(F2)));
|
|
1606
1679
|
const sampler = new FFSampler(logn, seed, b00, b01, b10, b11);
|
|
1607
|
-
const s2 = new Int16Array(
|
|
1680
|
+
const s2 = new Int16Array(N3);
|
|
1608
1681
|
try {
|
|
1609
1682
|
while (true) {
|
|
1610
1683
|
const { t0, t1 } = sampler.sample(hm);
|
|
@@ -1612,7 +1685,7 @@ function genFalcon(opts) {
|
|
|
1612
1685
|
const t3 = floatPoly.mul(t0, b01);
|
|
1613
1686
|
const t4 = floatPoly.iFFT(t2);
|
|
1614
1687
|
const t5 = floatPoly.iFFT(floatPoly.add(floatPoly.mul(t1, b11), t3));
|
|
1615
|
-
const hn =
|
|
1688
|
+
const hn = N3 >> 1;
|
|
1616
1689
|
let sqn = 0;
|
|
1617
1690
|
for (let i = 0; i < hn; i++) {
|
|
1618
1691
|
sqn += (hm[i] - (Math.round(t4[i].re) | 0)) ** 2;
|
|
@@ -1626,7 +1699,7 @@ function genFalcon(opts) {
|
|
|
1626
1699
|
}
|
|
1627
1700
|
cleanCPoly(t0, t1, t2, t3, t4, t5);
|
|
1628
1701
|
if (!(sqn <= L2BOUND[logn])) continue;
|
|
1629
|
-
const s2comp = compCoder(
|
|
1702
|
+
const s2comp = compCoder(N3).encode(s2);
|
|
1630
1703
|
if (s2comp.length > maxLen) {
|
|
1631
1704
|
cleanBytes(s2comp);
|
|
1632
1705
|
continue;
|
|
@@ -1640,14 +1713,14 @@ function genFalcon(opts) {
|
|
|
1640
1713
|
cleanBytes(G);
|
|
1641
1714
|
}
|
|
1642
1715
|
} finally {
|
|
1643
|
-
cleanBytes(f, g,
|
|
1716
|
+
cleanBytes(f, g, F2);
|
|
1644
1717
|
}
|
|
1645
1718
|
} finally {
|
|
1646
1719
|
cleanBytes(seed);
|
|
1647
1720
|
}
|
|
1648
1721
|
};
|
|
1649
1722
|
const verifyRaw = (pk, s2comp, nonce, msg) => {
|
|
1650
|
-
const s2 = compCoder(
|
|
1723
|
+
const s2 = compCoder(N3).decode(s2comp);
|
|
1651
1724
|
const c0 = HashToPoint(nonce, msg);
|
|
1652
1725
|
const h = intPoly.toMontgomery(intPoly.ntt(publicKeyCoder.decode(pk)));
|
|
1653
1726
|
const s1 = intPoly.intt(intPoly.mul(intPoly.ntt(signedCoder.decode(s2)), h));
|
|
@@ -1661,54 +1734,54 @@ function genFalcon(opts) {
|
|
|
1661
1734
|
secretKey: secretKeyCoder.bytesLen
|
|
1662
1735
|
});
|
|
1663
1736
|
const FALCON_SIG_OPT_KEYS = [...SIG_OPT_KEYS, "random"];
|
|
1664
|
-
const getRnd = (
|
|
1665
|
-
|
|
1666
|
-
if (
|
|
1667
|
-
if (
|
|
1668
|
-
throw new TypeError('"opts.random" expected function, got type=' + typeof
|
|
1669
|
-
if (
|
|
1670
|
-
if (
|
|
1671
|
-
const seed =
|
|
1737
|
+
const getRnd = (opts3 = {}) => {
|
|
1738
|
+
opts3 = validateSigOpts(opts3, FALCON_SIG_OPT_KEYS);
|
|
1739
|
+
if (opts3.context !== void 0) throw new Error("context is not supported");
|
|
1740
|
+
if (opts3.random !== void 0 && typeof opts3.random !== "function")
|
|
1741
|
+
throw new TypeError('"opts.random" expected function, got type=' + typeof opts3.random);
|
|
1742
|
+
if (opts3.random !== void 0) return opts3.random;
|
|
1743
|
+
if (opts3.extraEntropy === void 0) return randomBytes2;
|
|
1744
|
+
const seed = opts3.extraEntropy === false ? new Uint8Array(48) : opts3.extraEntropy;
|
|
1672
1745
|
abytes2(seed, 48, "opts.extraEntropy");
|
|
1673
1746
|
const drbg = rngAesCtrDrbg256(seed);
|
|
1674
1747
|
return (len = 0) => drbg.randomBytes(len);
|
|
1675
1748
|
};
|
|
1676
|
-
const checkVerOpts = (
|
|
1677
|
-
const normalized = validateVerOpts(
|
|
1749
|
+
const checkVerOpts = (opts3 = {}) => {
|
|
1750
|
+
const normalized = validateVerOpts(opts3);
|
|
1678
1751
|
if (normalized.context !== void 0) throw new Error("context is not supported");
|
|
1679
1752
|
};
|
|
1680
1753
|
const tests = Object.freeze({
|
|
1681
1754
|
publicKeyCoder: Object.freeze(publicKeyCoder),
|
|
1682
1755
|
privateKeyCoder: Object.freeze(secretKeyCoder),
|
|
1683
|
-
maxS2Len:
|
|
1756
|
+
maxS2Len: opts2.maxS2Len
|
|
1684
1757
|
});
|
|
1685
1758
|
const attachedLengths = Object.freeze({ ...keyLengths, signRand: 48 });
|
|
1686
|
-
const lengths =
|
|
1759
|
+
const lengths = opts2.padded ? Object.freeze({ ...attachedLengths, signature: opts2.sigLen }) : attachedLengths;
|
|
1687
1760
|
const keygen = (seed) => {
|
|
1688
1761
|
const randSeed = seed === void 0;
|
|
1689
|
-
if (randSeed) seed =
|
|
1762
|
+
if (randSeed) seed = randomBytes2(48);
|
|
1690
1763
|
abytes2(seed, 48, "seed");
|
|
1691
|
-
const [f, g,
|
|
1692
|
-
const sk = secretKeyCoder.encode([f, g,
|
|
1764
|
+
const [f, g, F2, _G, pub] = new NTRU(logn, seed).generate();
|
|
1765
|
+
const sk = secretKeyCoder.encode([f, g, F2]);
|
|
1693
1766
|
const pk = publicKeyCoder.encode(pub);
|
|
1694
1767
|
if (randSeed) cleanBytes(seed);
|
|
1695
|
-
cleanBytes(f, g,
|
|
1768
|
+
cleanBytes(f, g, F2, _G);
|
|
1696
1769
|
return { publicKey: pk, secretKey: sk };
|
|
1697
1770
|
};
|
|
1698
1771
|
const getPublicKey2 = (sk) => {
|
|
1699
1772
|
abytes2(sk, secretKeyCoder.bytesLen, "secretKey");
|
|
1700
|
-
const [f, g,
|
|
1773
|
+
const [f, g, F2] = secretKeyCoder.decode(sk);
|
|
1701
1774
|
try {
|
|
1702
1775
|
const h = computePublic(f, g);
|
|
1703
|
-
cleanBytes(f, g,
|
|
1776
|
+
cleanBytes(f, g, F2);
|
|
1704
1777
|
return publicKeyCoder.encode(h);
|
|
1705
1778
|
} catch (e) {
|
|
1706
|
-
cleanBytes(f, g,
|
|
1779
|
+
cleanBytes(f, g, F2);
|
|
1707
1780
|
throw e;
|
|
1708
1781
|
}
|
|
1709
1782
|
};
|
|
1710
1783
|
const sign = (msg, sk, sigOpts = {}) => {
|
|
1711
|
-
const { s2, nonce } = signRaw(sk, msg,
|
|
1784
|
+
const { s2, nonce } = signRaw(sk, msg, opts2.maxS2Len, getRnd(sigOpts));
|
|
1712
1785
|
return SignatureCoderDetached(logn).encode({ nonce, s2 });
|
|
1713
1786
|
};
|
|
1714
1787
|
const verify2 = (sig, msg, pk, verOpts = {}) => {
|
|
@@ -1729,7 +1802,7 @@ function genFalcon(opts) {
|
|
|
1729
1802
|
keygen,
|
|
1730
1803
|
getPublicKey: getPublicKey2,
|
|
1731
1804
|
seal(msg, sk, sigOpts = {}) {
|
|
1732
|
-
const { s2, nonce } = signRaw(sk, msg,
|
|
1805
|
+
const { s2, nonce } = signRaw(sk, msg, opts2.maxS2Len, getRnd(sigOpts));
|
|
1733
1806
|
return SignatureCoder.encode({ msg, nonce, s2 });
|
|
1734
1807
|
},
|
|
1735
1808
|
open(sig, pk, verOpts = {}) {
|
|
@@ -1755,30 +1828,30 @@ function genFalcon(opts) {
|
|
|
1755
1828
|
});
|
|
1756
1829
|
const INTERNAL_MAX_ITERATIONS = 1e3;
|
|
1757
1830
|
const checkHashPoint = (hashPoint) => {
|
|
1758
|
-
if (!(hashPoint instanceof Uint16Array) || hashPoint.length !==
|
|
1759
|
-
throw new Error(`hashPoint: expected Uint16Array(${
|
|
1760
|
-
for (let i = 0; i <
|
|
1831
|
+
if (!(hashPoint instanceof Uint16Array) || hashPoint.length !== N3)
|
|
1832
|
+
throw new Error(`hashPoint: expected Uint16Array(${N3})`);
|
|
1833
|
+
for (let i = 0; i < N3; i++)
|
|
1761
1834
|
if (hashPoint[i] >= Q2) throw new Error("hashPoint: coefficient out of range");
|
|
1762
1835
|
};
|
|
1763
1836
|
const internal = Object.freeze({
|
|
1764
1837
|
sign(hashPoint, sk, internalOpts = {}) {
|
|
1765
1838
|
checkHashPoint(hashPoint);
|
|
1766
1839
|
abytes2(sk, secretKeyCoder.bytesLen, "secretKey");
|
|
1767
|
-
const rnd = internalOpts.random ??
|
|
1840
|
+
const rnd = internalOpts.random ?? randomBytes2;
|
|
1768
1841
|
const maxIterations = internalOpts.maxIterations ?? INTERNAL_MAX_ITERATIONS;
|
|
1769
1842
|
const hm = Uint16Array.from(hashPoint);
|
|
1770
1843
|
const seed = rnd(48);
|
|
1771
1844
|
abytes2(seed, 48, "seed");
|
|
1772
1845
|
try {
|
|
1773
|
-
const [f, g,
|
|
1846
|
+
const [f, g, F2] = secretKeyCoder.decode(sk);
|
|
1774
1847
|
try {
|
|
1775
|
-
const G = completePrivate(f, g,
|
|
1848
|
+
const G = completePrivate(f, g, F2);
|
|
1776
1849
|
const b00 = floatPoly.FFT(floatPoly.convSmall(g));
|
|
1777
1850
|
const b01 = floatPoly.FFT(floatPoly.neg(floatPoly.convSmall(f)));
|
|
1778
1851
|
const b10 = floatPoly.FFT(floatPoly.convSmall(G));
|
|
1779
|
-
const b11 = floatPoly.FFT(floatPoly.neg(floatPoly.convSmall(
|
|
1852
|
+
const b11 = floatPoly.FFT(floatPoly.neg(floatPoly.convSmall(F2)));
|
|
1780
1853
|
const sampler = new FFSampler(logn, seed, b00, b01, b10, b11);
|
|
1781
|
-
const s2 = new Int16Array(
|
|
1854
|
+
const s2 = new Int16Array(N3);
|
|
1782
1855
|
try {
|
|
1783
1856
|
for (let iteration = 0; iteration < maxIterations; iteration++) {
|
|
1784
1857
|
const { t0, t1 } = sampler.sample(hm);
|
|
@@ -1786,7 +1859,7 @@ function genFalcon(opts) {
|
|
|
1786
1859
|
const t3 = floatPoly.mul(t0, b01);
|
|
1787
1860
|
const t4 = floatPoly.iFFT(t2);
|
|
1788
1861
|
const t5 = floatPoly.iFFT(floatPoly.add(floatPoly.mul(t1, b11), t3));
|
|
1789
|
-
const hn =
|
|
1862
|
+
const hn = N3 >> 1;
|
|
1790
1863
|
let sqn = 0;
|
|
1791
1864
|
for (let i = 0; i < hn; i++) {
|
|
1792
1865
|
sqn += (hm[i] - (Math.round(t4[i].re) | 0)) ** 2;
|
|
@@ -1809,7 +1882,7 @@ function genFalcon(opts) {
|
|
|
1809
1882
|
cleanBytes(G);
|
|
1810
1883
|
}
|
|
1811
1884
|
} finally {
|
|
1812
|
-
cleanBytes(f, g,
|
|
1885
|
+
cleanBytes(f, g, F2);
|
|
1813
1886
|
}
|
|
1814
1887
|
} finally {
|
|
1815
1888
|
cleanBytes(seed);
|
|
@@ -1817,8 +1890,8 @@ function genFalcon(opts) {
|
|
|
1817
1890
|
},
|
|
1818
1891
|
verify(s2, hashPoint, pk) {
|
|
1819
1892
|
checkHashPoint(hashPoint);
|
|
1820
|
-
if (!(s2 instanceof Int16Array) || s2.length !==
|
|
1821
|
-
throw new Error(`s2: expected Int16Array(${
|
|
1893
|
+
if (!(s2 instanceof Int16Array) || s2.length !== N3)
|
|
1894
|
+
throw new Error(`s2: expected Int16Array(${N3})`);
|
|
1822
1895
|
abytes2(pk, publicKeyCoder.bytesLen, "publicKey");
|
|
1823
1896
|
const h = intPoly.toMontgomery(intPoly.ntt(publicKeyCoder.decode(pk)));
|
|
1824
1897
|
const s1 = intPoly.intt(intPoly.mul(intPoly.ntt(signedCoder.decode(s2)), h));
|
|
@@ -1852,6 +1925,380 @@ var falcon512opts = {
|
|
|
1852
1925
|
};
|
|
1853
1926
|
var falcon512 = /* @__PURE__ */ (() => genFalcon({ ...falcon512opts, maxS2Len: 711 }))();
|
|
1854
1927
|
|
|
1928
|
+
// src/falcon/engine/noble/ml-kem.ts
|
|
1929
|
+
import { sha3_256, sha3_512, shake256 as shake2563 } from "@noble/hashes/sha3.js";
|
|
1930
|
+
import { swap32IfBE as swap32IfBE2, u32 as u322 } from "@noble/hashes/utils.js";
|
|
1931
|
+
var N2 = 256;
|
|
1932
|
+
var Q3 = 3329;
|
|
1933
|
+
var F = 3303;
|
|
1934
|
+
var ROOT_OF_UNITY = 17;
|
|
1935
|
+
var crystals = /* @__PURE__ */ genCrystals({
|
|
1936
|
+
N: N2,
|
|
1937
|
+
Q: Q3,
|
|
1938
|
+
F,
|
|
1939
|
+
ROOT_OF_UNITY,
|
|
1940
|
+
newPoly: (n) => new Uint16Array(n),
|
|
1941
|
+
brvBits: 7,
|
|
1942
|
+
isKyber: true
|
|
1943
|
+
});
|
|
1944
|
+
var PARAMS = /* @__PURE__ */ (() => Object.freeze({
|
|
1945
|
+
512: Object.freeze({ N: N2, Q: Q3, K: 2, ETA1: 3, ETA2: 2, du: 10, dv: 4, RBGstrength: 128 }),
|
|
1946
|
+
768: Object.freeze({ N: N2, Q: Q3, K: 3, ETA1: 2, ETA2: 2, du: 10, dv: 4, RBGstrength: 192 }),
|
|
1947
|
+
1024: Object.freeze({ N: N2, Q: Q3, K: 4, ETA1: 2, ETA2: 2, du: 11, dv: 5, RBGstrength: 256 })
|
|
1948
|
+
}))();
|
|
1949
|
+
var compress = (d) => {
|
|
1950
|
+
if (d >= 12) return { encode: (i) => i, decode: (i) => i >= Q3 ? i - Q3 : i };
|
|
1951
|
+
const a = 2 ** (d - 1);
|
|
1952
|
+
return {
|
|
1953
|
+
// This only matches standalone Compress_d after bitsCoder masks the result into Z_(2^d).
|
|
1954
|
+
encode: (i) => ((i << d) + Q3 / 2) / Q3,
|
|
1955
|
+
// const decompress = (i: number) => round((Q / 2 ** d) * i);
|
|
1956
|
+
decode: (i) => i * Q3 + a >>> d
|
|
1957
|
+
};
|
|
1958
|
+
};
|
|
1959
|
+
var byteCoder = (d) => crystals.bitsCoder(
|
|
1960
|
+
d,
|
|
1961
|
+
d === 12 ? { encode: (i) => i, decode: (i) => i >= Q3 ? i - Q3 : i } : { encode: (i) => i, decode: (i) => i }
|
|
1962
|
+
);
|
|
1963
|
+
var polyCoder = (d) => d === 12 ? byteCoder(12) : crystals.bitsCoder(d, compress(d));
|
|
1964
|
+
function polyAdd(a_, b_) {
|
|
1965
|
+
const a = a_;
|
|
1966
|
+
const b = b_;
|
|
1967
|
+
for (let i = 0; i < N2; i++) {
|
|
1968
|
+
const r = a[i] + b[i];
|
|
1969
|
+
a[i] = r >= Q3 ? r - Q3 : r;
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
function polySub(a_, b_) {
|
|
1973
|
+
const a = a_;
|
|
1974
|
+
const b = b_;
|
|
1975
|
+
for (let i = 0; i < N2; i++) {
|
|
1976
|
+
const r = a[i] - b[i];
|
|
1977
|
+
a[i] = r < 0 ? r + Q3 : r;
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
function BaseCaseMultiply(a0, a1, b0, b1, zeta) {
|
|
1981
|
+
const c0 = crystals.mod(crystals.mod(a1 * b1) * zeta + a0 * b0);
|
|
1982
|
+
const c1 = crystals.mod(a0 * b1 + a1 * b0);
|
|
1983
|
+
return { c0, c1 };
|
|
1984
|
+
}
|
|
1985
|
+
function MultiplyNTTs(f_, g_) {
|
|
1986
|
+
const f = f_;
|
|
1987
|
+
const g = g_;
|
|
1988
|
+
for (let i = 0; i < N2 / 2; i++) {
|
|
1989
|
+
let z = crystals.nttZetas[64 + (i >> 1)];
|
|
1990
|
+
if (i & 1) z = -z;
|
|
1991
|
+
const { c0, c1 } = BaseCaseMultiply(f[2 * i + 0], f[2 * i + 1], g[2 * i + 0], g[2 * i + 1], z);
|
|
1992
|
+
f[2 * i + 0] = c0;
|
|
1993
|
+
f[2 * i + 1] = c1;
|
|
1994
|
+
}
|
|
1995
|
+
return f;
|
|
1996
|
+
}
|
|
1997
|
+
function SampleNTT(xof_) {
|
|
1998
|
+
const xof = xof_;
|
|
1999
|
+
const r = new Uint16Array(N2);
|
|
2000
|
+
for (let j = 0; j < N2; ) {
|
|
2001
|
+
const b = xof();
|
|
2002
|
+
if (b.length % 3) throw new Error("SampleNTT: unaligned block");
|
|
2003
|
+
for (let i = 0; j < N2 && i + 3 <= b.length; i += 3) {
|
|
2004
|
+
const d1 = (b[i + 0] >> 0 | b[i + 1] << 8) & 4095;
|
|
2005
|
+
const d2 = (b[i + 1] >> 4 | b[i + 2] << 4) & 4095;
|
|
2006
|
+
if (d1 < Q3) r[j++] = d1;
|
|
2007
|
+
if (j < N2 && d2 < Q3) r[j++] = d2;
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
return r;
|
|
2011
|
+
}
|
|
2012
|
+
var sampleCBDBytes = (buf, eta) => {
|
|
2013
|
+
const r = new Uint16Array(N2);
|
|
2014
|
+
const b32 = u322(buf);
|
|
2015
|
+
swap32IfBE2(b32);
|
|
2016
|
+
let len = 0;
|
|
2017
|
+
for (let i = 0, p = 0, bb = 0, t0 = 0; i < b32.length; i++) {
|
|
2018
|
+
let b = b32[i];
|
|
2019
|
+
for (let j = 0; j < 32; j++) {
|
|
2020
|
+
bb += b & 1;
|
|
2021
|
+
b >>= 1;
|
|
2022
|
+
len += 1;
|
|
2023
|
+
if (len === eta) {
|
|
2024
|
+
t0 = bb;
|
|
2025
|
+
bb = 0;
|
|
2026
|
+
} else if (len === 2 * eta) {
|
|
2027
|
+
r[p++] = crystals.mod(t0 - bb);
|
|
2028
|
+
bb = 0;
|
|
2029
|
+
len = 0;
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
}
|
|
2033
|
+
swap32IfBE2(b32);
|
|
2034
|
+
if (len) throw new Error(`sampleCBD: leftover bits: ${len}`);
|
|
2035
|
+
return r;
|
|
2036
|
+
};
|
|
2037
|
+
function sampleCBD(PRF_, seed, nonce, eta) {
|
|
2038
|
+
const PRF = PRF_;
|
|
2039
|
+
return sampleCBDBytes(PRF(eta * N2 / 4, seed, nonce), eta);
|
|
2040
|
+
}
|
|
2041
|
+
var genKPKE = (opts_) => {
|
|
2042
|
+
const opts2 = opts_;
|
|
2043
|
+
const { K, PRF, XOF, HASH512, ETA1, ETA2, du, dv } = opts2;
|
|
2044
|
+
const poly1 = polyCoder(1);
|
|
2045
|
+
const polyV = polyCoder(dv);
|
|
2046
|
+
const polyU = polyCoder(du);
|
|
2047
|
+
const publicCoder = splitCoder("publicKey", vecCoder(polyCoder(12), K), 32);
|
|
2048
|
+
const secretCoder = vecCoder(polyCoder(12), K);
|
|
2049
|
+
const cipherCoder = splitCoder("ciphertext", vecCoder(polyU, K), polyV);
|
|
2050
|
+
const seedCoder = splitCoder("seed", 32, 32);
|
|
2051
|
+
const encryptCore = (tHat, getA, msg, seed) => {
|
|
2052
|
+
const rHat = [];
|
|
2053
|
+
for (let i = 0; i < K; i++) rHat.push(crystals.NTT.encode(sampleCBD(PRF, seed, i, ETA1)));
|
|
2054
|
+
const tmp2 = new Uint16Array(N2);
|
|
2055
|
+
const u = [];
|
|
2056
|
+
for (let i = 0; i < K; i++) {
|
|
2057
|
+
const e1 = sampleCBD(PRF, seed, K + i, ETA2);
|
|
2058
|
+
const tmp = new Uint16Array(N2);
|
|
2059
|
+
for (let j = 0; j < K; j++) {
|
|
2060
|
+
const aij = getA(i, j);
|
|
2061
|
+
polyAdd(tmp, MultiplyNTTs(aij, rHat[j]));
|
|
2062
|
+
}
|
|
2063
|
+
polyAdd(e1, crystals.NTT.decode(tmp));
|
|
2064
|
+
u.push(e1);
|
|
2065
|
+
polyAdd(tmp2, MultiplyNTTs(tHat[i], rHat[i]));
|
|
2066
|
+
cleanBytes(tmp);
|
|
2067
|
+
}
|
|
2068
|
+
const e2 = sampleCBD(PRF, seed, 2 * K, ETA2);
|
|
2069
|
+
polyAdd(e2, crystals.NTT.decode(tmp2));
|
|
2070
|
+
const v = poly1.decode(msg);
|
|
2071
|
+
polyAdd(v, e2);
|
|
2072
|
+
cleanBytes(tHat, rHat, tmp2, e2);
|
|
2073
|
+
return cipherCoder.encode([u, v]);
|
|
2074
|
+
};
|
|
2075
|
+
return {
|
|
2076
|
+
secretCoder,
|
|
2077
|
+
lengths: {
|
|
2078
|
+
secretKey: secretCoder.bytesLen,
|
|
2079
|
+
publicKey: publicCoder.bytesLen,
|
|
2080
|
+
cipherText: cipherCoder.bytesLen
|
|
2081
|
+
},
|
|
2082
|
+
keygen: (seed) => {
|
|
2083
|
+
abytesDoc(seed, 32, "seed");
|
|
2084
|
+
const seedDst = new Uint8Array(33);
|
|
2085
|
+
seedDst.set(seed);
|
|
2086
|
+
seedDst[32] = K;
|
|
2087
|
+
const seedHash = HASH512(seedDst);
|
|
2088
|
+
const [rho, sigma] = seedCoder.decode(seedHash);
|
|
2089
|
+
const sHat = [];
|
|
2090
|
+
const tHat = [];
|
|
2091
|
+
for (let i = 0; i < K; i++) sHat.push(crystals.NTT.encode(sampleCBD(PRF, sigma, i, ETA1)));
|
|
2092
|
+
const x = XOF(rho);
|
|
2093
|
+
for (let i = 0; i < K; i++) {
|
|
2094
|
+
const e = crystals.NTT.encode(sampleCBD(PRF, sigma, K + i, ETA1));
|
|
2095
|
+
for (let j = 0; j < K; j++) {
|
|
2096
|
+
const aji = SampleNTT(x.get(j, i));
|
|
2097
|
+
polyAdd(e, MultiplyNTTs(aji, sHat[j]));
|
|
2098
|
+
}
|
|
2099
|
+
tHat.push(e);
|
|
2100
|
+
}
|
|
2101
|
+
x.clean();
|
|
2102
|
+
const res = {
|
|
2103
|
+
publicKey: publicCoder.encode([tHat, rho]),
|
|
2104
|
+
secretKey: secretCoder.encode(sHat)
|
|
2105
|
+
};
|
|
2106
|
+
cleanBytes(rho, sigma, sHat, tHat, seedDst, seedHash);
|
|
2107
|
+
return res;
|
|
2108
|
+
},
|
|
2109
|
+
encrypt: (publicKey, msg, seed) => {
|
|
2110
|
+
const [tHat, rho] = publicCoder.decode(publicKey);
|
|
2111
|
+
const x = XOF(rho);
|
|
2112
|
+
const res = encryptCore(tHat, (i, j) => SampleNTT(x.get(i, j)), msg, seed);
|
|
2113
|
+
x.clean();
|
|
2114
|
+
return res;
|
|
2115
|
+
},
|
|
2116
|
+
// Expands the full  matrix (public data derived from rho) once, so repeated encryptions
|
|
2117
|
+
// against the same ek skip the K² SampleNTT XOF expansions. Cached polys are copied per
|
|
2118
|
+
// call because encryptCore mutates its inputs in place.
|
|
2119
|
+
prepare: (publicKey) => {
|
|
2120
|
+
const [tHat, rho] = publicCoder.decode(publicKey);
|
|
2121
|
+
const x = XOF(rho);
|
|
2122
|
+
const A = [];
|
|
2123
|
+
for (let i = 0; i < K; i++)
|
|
2124
|
+
for (let j = 0; j < K; j++) A.push(SampleNTT(x.get(i, j)));
|
|
2125
|
+
x.clean();
|
|
2126
|
+
return {
|
|
2127
|
+
encrypt: (msg, seed) => encryptCore(
|
|
2128
|
+
tHat.map((p) => p.slice()),
|
|
2129
|
+
(i, j) => A[i * K + j].slice(),
|
|
2130
|
+
msg,
|
|
2131
|
+
seed
|
|
2132
|
+
),
|
|
2133
|
+
clean: () => cleanBytes(tHat, A)
|
|
2134
|
+
};
|
|
2135
|
+
},
|
|
2136
|
+
decrypt: (cipherText, privateKey) => {
|
|
2137
|
+
const [u, v] = cipherCoder.decode(cipherText);
|
|
2138
|
+
const sk = secretCoder.decode(privateKey);
|
|
2139
|
+
const tmp = new Uint16Array(N2);
|
|
2140
|
+
for (let i = 0; i < K; i++) polyAdd(tmp, MultiplyNTTs(sk[i], crystals.NTT.encode(u[i])));
|
|
2141
|
+
polySub(v, crystals.NTT.decode(tmp));
|
|
2142
|
+
const res = poly1.encode(v);
|
|
2143
|
+
cleanBytes(tmp, sk, u, v);
|
|
2144
|
+
return res;
|
|
2145
|
+
}
|
|
2146
|
+
};
|
|
2147
|
+
};
|
|
2148
|
+
function createKyber(opts2) {
|
|
2149
|
+
const rawOpts = opts2;
|
|
2150
|
+
const KPKE = genKPKE(rawOpts);
|
|
2151
|
+
const { HASH256, HASH512, KDF } = rawOpts;
|
|
2152
|
+
const { secretCoder: KPKESecretCoder, lengths } = KPKE;
|
|
2153
|
+
const secretCoder = splitCoder("secretKey", lengths.secretKey, lengths.publicKey, 32, 32);
|
|
2154
|
+
const msgLen = 32;
|
|
2155
|
+
const seedLen = 64;
|
|
2156
|
+
const validateModulus = (publicKey, fn) => {
|
|
2157
|
+
const eke = publicKey.subarray(0, 384 * rawOpts.K);
|
|
2158
|
+
const ek = KPKESecretCoder.encode(KPKESecretCoder.decode(copyBytes(eke)));
|
|
2159
|
+
const ok = equalBytes(ek, eke);
|
|
2160
|
+
cleanBytes(ek);
|
|
2161
|
+
if (!ok) throw new Error(`ML-KEM.${fn}: wrong publicKey modulus`);
|
|
2162
|
+
};
|
|
2163
|
+
const kemLengths = Object.freeze({
|
|
2164
|
+
...lengths,
|
|
2165
|
+
seed: 64,
|
|
2166
|
+
msg: msgLen,
|
|
2167
|
+
msgRand: msgLen,
|
|
2168
|
+
secretKey: secretCoder.bytesLen
|
|
2169
|
+
});
|
|
2170
|
+
return Object.freeze({
|
|
2171
|
+
info: Object.freeze({ type: "ml-kem" }),
|
|
2172
|
+
lengths: kemLengths,
|
|
2173
|
+
keygen: (seed) => {
|
|
2174
|
+
const ownSeed = seed === void 0;
|
|
2175
|
+
const s = ownSeed ? randomBytes(seedLen) : seed;
|
|
2176
|
+
let sk;
|
|
2177
|
+
let publicKeyHash;
|
|
2178
|
+
try {
|
|
2179
|
+
abytesDoc(s, seedLen, "seed");
|
|
2180
|
+
const keys = KPKE.keygen(s.subarray(0, 32));
|
|
2181
|
+
const publicKey = keys.publicKey;
|
|
2182
|
+
sk = keys.secretKey;
|
|
2183
|
+
publicKeyHash = HASH256(publicKey);
|
|
2184
|
+
const secretKey = secretCoder.encode([sk, publicKey, publicKeyHash, s.subarray(32)]);
|
|
2185
|
+
return {
|
|
2186
|
+
publicKey,
|
|
2187
|
+
secretKey
|
|
2188
|
+
};
|
|
2189
|
+
} finally {
|
|
2190
|
+
if (sk !== void 0) cleanBytes(sk);
|
|
2191
|
+
if (publicKeyHash !== void 0) cleanBytes(publicKeyHash);
|
|
2192
|
+
if (ownSeed) cleanBytes(s);
|
|
2193
|
+
}
|
|
2194
|
+
},
|
|
2195
|
+
getPublicKey: (secretKey) => {
|
|
2196
|
+
const [_sk, publicKey, _publicKeyHash, _z] = secretCoder.decode(secretKey);
|
|
2197
|
+
return Uint8Array.from(publicKey);
|
|
2198
|
+
},
|
|
2199
|
+
encapsulate: (publicKey, msg) => {
|
|
2200
|
+
const ownMsg = msg === void 0;
|
|
2201
|
+
const m = ownMsg ? randomBytes(msgLen) : msg;
|
|
2202
|
+
let kr;
|
|
2203
|
+
try {
|
|
2204
|
+
abytesDoc(publicKey, lengths.publicKey, "publicKey");
|
|
2205
|
+
abytesDoc(m, msgLen, "message");
|
|
2206
|
+
validateModulus(publicKey, "encapsulate");
|
|
2207
|
+
kr = HASH512.create().update(m).update(HASH256(publicKey)).digest();
|
|
2208
|
+
const cipherText = KPKE.encrypt(publicKey, m, kr.subarray(32, 64));
|
|
2209
|
+
return {
|
|
2210
|
+
cipherText,
|
|
2211
|
+
sharedSecret: kr.subarray(0, 32)
|
|
2212
|
+
};
|
|
2213
|
+
} finally {
|
|
2214
|
+
if (kr !== void 0) cleanBytes(kr.subarray(32));
|
|
2215
|
+
if (ownMsg) cleanBytes(m);
|
|
2216
|
+
}
|
|
2217
|
+
},
|
|
2218
|
+
decapsulate: (cipherText, secretKey) => {
|
|
2219
|
+
abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
|
|
2220
|
+
abytesDoc(cipherText, lengths.cipherText, "cipherText");
|
|
2221
|
+
const k768 = secretCoder.bytesLen - 96;
|
|
2222
|
+
const start = k768 + 32;
|
|
2223
|
+
const test = HASH256(secretKey.subarray(k768 / 2, start));
|
|
2224
|
+
if (!equalBytes(test, secretKey.subarray(start, start + 32)))
|
|
2225
|
+
throw new Error("invalid secretKey: hash check failed");
|
|
2226
|
+
const [sk, publicKey, publicKeyHash, z] = secretCoder.decode(secretKey);
|
|
2227
|
+
const msg = KPKE.decrypt(cipherText, sk);
|
|
2228
|
+
const kr = HASH512.create().update(msg).update(publicKeyHash).digest();
|
|
2229
|
+
const Khat = kr.subarray(0, 32);
|
|
2230
|
+
const cipherText2 = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
|
|
2231
|
+
const isValid = equalBytes(cipherText, cipherText2);
|
|
2232
|
+
const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
|
|
2233
|
+
cleanBytes(msg, cipherText2, kr.subarray(32), !isValid ? Khat : Kbar);
|
|
2234
|
+
return isValid ? Khat : Kbar;
|
|
2235
|
+
},
|
|
2236
|
+
/**
|
|
2237
|
+
* Experimental prototype: pre-expand a public key so repeated encapsulate/decapsulate
|
|
2238
|
+
* against the same key skip re-validation, H(ek), t̂ decoding and the K² SampleNTT
|
|
2239
|
+
* XOF expansions of Â. Only public data is cached; see {@link KEMPrepared}.
|
|
2240
|
+
*/
|
|
2241
|
+
prepare: (publicKey) => {
|
|
2242
|
+
abytesDoc(publicKey, lengths.publicKey, "publicKey");
|
|
2243
|
+
validateModulus(publicKey, "prepare");
|
|
2244
|
+
const ek = copyBytes(publicKey);
|
|
2245
|
+
const publicKeyHash = HASH256(ek);
|
|
2246
|
+
const cached = KPKE.prepare(ek);
|
|
2247
|
+
return Object.freeze({
|
|
2248
|
+
publicKey: ek,
|
|
2249
|
+
encapsulate: (msg) => {
|
|
2250
|
+
const ownMsg = msg === void 0;
|
|
2251
|
+
const m = ownMsg ? randomBytes(msgLen) : msg;
|
|
2252
|
+
let kr;
|
|
2253
|
+
try {
|
|
2254
|
+
abytesDoc(m, msgLen, "message");
|
|
2255
|
+
kr = HASH512.create().update(m).update(publicKeyHash).digest();
|
|
2256
|
+
const cipherText = cached.encrypt(m, kr.subarray(32, 64));
|
|
2257
|
+
return {
|
|
2258
|
+
cipherText,
|
|
2259
|
+
sharedSecret: kr.subarray(0, 32)
|
|
2260
|
+
};
|
|
2261
|
+
} finally {
|
|
2262
|
+
if (kr !== void 0) cleanBytes(kr.subarray(32));
|
|
2263
|
+
if (ownMsg) cleanBytes(m);
|
|
2264
|
+
}
|
|
2265
|
+
},
|
|
2266
|
+
decapsulate: (cipherText, secretKey) => {
|
|
2267
|
+
abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
|
|
2268
|
+
abytesDoc(cipherText, lengths.cipherText, "cipherText");
|
|
2269
|
+
const [sk, ekEmbedded, storedHash, z] = secretCoder.decode(secretKey);
|
|
2270
|
+
if (!equalBytes(ekEmbedded, ek) || !equalBytes(storedHash, publicKeyHash))
|
|
2271
|
+
throw new Error("ML-KEM.decapsulate: secretKey does not match prepared publicKey");
|
|
2272
|
+
const msg = KPKE.decrypt(cipherText, sk);
|
|
2273
|
+
const kr = HASH512.create().update(msg).update(publicKeyHash).digest();
|
|
2274
|
+
const Khat = kr.subarray(0, 32);
|
|
2275
|
+
const cipherText2 = cached.encrypt(msg, kr.subarray(32, 64));
|
|
2276
|
+
const isValid = equalBytes(cipherText, cipherText2);
|
|
2277
|
+
const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
|
|
2278
|
+
cleanBytes(msg, cipherText2, kr.subarray(32), !isValid ? Khat : Kbar);
|
|
2279
|
+
return isValid ? Khat : Kbar;
|
|
2280
|
+
},
|
|
2281
|
+
clean: cached.clean
|
|
2282
|
+
});
|
|
2283
|
+
}
|
|
2284
|
+
});
|
|
2285
|
+
}
|
|
2286
|
+
function shakePRF(dkLen, key, nonce) {
|
|
2287
|
+
return shake2563.create({ dkLen }).update(key).update(new Uint8Array([nonce])).digest();
|
|
2288
|
+
}
|
|
2289
|
+
var opts = /* @__PURE__ */ (() => ({
|
|
2290
|
+
HASH256: sha3_256,
|
|
2291
|
+
HASH512: sha3_512,
|
|
2292
|
+
KDF: shake2563,
|
|
2293
|
+
XOF: XOF128,
|
|
2294
|
+
PRF: shakePRF
|
|
2295
|
+
}))();
|
|
2296
|
+
var mk = (params) => createKyber({
|
|
2297
|
+
...opts,
|
|
2298
|
+
...params
|
|
2299
|
+
});
|
|
2300
|
+
var ml_kem768 = /* @__PURE__ */ (() => mk(PARAMS[768]))();
|
|
2301
|
+
|
|
1855
2302
|
// src/falcon/engine/engine.ts
|
|
1856
2303
|
var nobleEngine = Object.freeze({
|
|
1857
2304
|
keygen: (seed) => falcon512.keygen(seed),
|
|
@@ -3127,7 +3574,7 @@ function parseSignature(signature) {
|
|
|
3127
3574
|
mulHint: unpackPoly(felts.slice(saltStart + SALT_FELTS))
|
|
3128
3575
|
};
|
|
3129
3576
|
}
|
|
3130
|
-
var randomSaltFelt = (
|
|
3577
|
+
var randomSaltFelt = (randomBytes4) => BigInt(`0x${starknet5.encode.buf2hex(randomBytes4(31))}`);
|
|
3131
3578
|
function signHash(msgHash, secretKey, pkNtt, deps = defaultDeps) {
|
|
3132
3579
|
assertFelt(msgHash, "message hash");
|
|
3133
3580
|
const salt = [randomSaltFelt(deps.randomBytes), randomSaltFelt(deps.randomBytes)];
|
|
@@ -3159,7 +3606,7 @@ function verifyHash(msgHash, signature, pkNtt) {
|
|
|
3159
3606
|
}
|
|
3160
3607
|
|
|
3161
3608
|
// src/falcon/oz/hashToPoint.ts
|
|
3162
|
-
import { shake256 as
|
|
3609
|
+
import { shake256 as shake2564 } from "@noble/hashes/sha3.js";
|
|
3163
3610
|
var SALT_BYTES = 20;
|
|
3164
3611
|
var SALT_LIMIT = 1n << BigInt(8 * SALT_BYTES);
|
|
3165
3612
|
var KQ = 5 * Q;
|
|
@@ -3185,7 +3632,7 @@ function hashToPointShake(msgHash, saltA, saltB) {
|
|
|
3185
3632
|
assertFelt(msgHash, "message hash");
|
|
3186
3633
|
const message = new Uint8Array(32);
|
|
3187
3634
|
writeLittleEndian(msgHash, message, 0, 32);
|
|
3188
|
-
const xof =
|
|
3635
|
+
const xof = shake2564.create().update(saltFeltsToBytes(saltA, saltB)).update(message);
|
|
3189
3636
|
const out = new Uint16Array(N);
|
|
3190
3637
|
for (let i = 0; i < N; ) {
|
|
3191
3638
|
const [high, low] = xof.xof(2);
|
|
@@ -3385,8 +3832,8 @@ function checkRange(poly, max, label) {
|
|
|
3385
3832
|
}
|
|
3386
3833
|
}
|
|
3387
3834
|
}
|
|
3388
|
-
function completeG(f, g,
|
|
3389
|
-
const gF = mulZq(g,
|
|
3835
|
+
function completeG(f, g, F2) {
|
|
3836
|
+
const gF = mulZq(g, F2);
|
|
3390
3837
|
let quotient = [];
|
|
3391
3838
|
let G = [];
|
|
3392
3839
|
try {
|
|
@@ -3398,9 +3845,9 @@ function completeG(f, g, F) {
|
|
|
3398
3845
|
wipe(gF, quotient, G);
|
|
3399
3846
|
}
|
|
3400
3847
|
}
|
|
3401
|
-
function checkNtruEquation({ f, g, F, G }) {
|
|
3848
|
+
function checkNtruEquation({ f, g, F: F2, G }) {
|
|
3402
3849
|
const fG = mulNegacyclicInt(f, G);
|
|
3403
|
-
const gF = mulNegacyclicInt(g,
|
|
3850
|
+
const gF = mulNegacyclicInt(g, F2);
|
|
3404
3851
|
try {
|
|
3405
3852
|
for (let i = 0; i < N; i++) {
|
|
3406
3853
|
if (fG[i] - gF[i] !== (i === 0 ? Q : 0)) {
|
|
@@ -3435,11 +3882,11 @@ function decodeSecretKey(secretKey) {
|
|
|
3435
3882
|
parts.push(f);
|
|
3436
3883
|
const g = decodeSigned(bytes.subarray(1 + FG_BYTES, 1 + 2 * FG_BYTES), FG_BITS, "g");
|
|
3437
3884
|
parts.push(g);
|
|
3438
|
-
const
|
|
3439
|
-
parts.push(
|
|
3440
|
-
const G = completeG(f, g,
|
|
3885
|
+
const F2 = decodeSigned(bytes.subarray(1 + 2 * FG_BYTES), CAPITAL_FG_BITS, "F");
|
|
3886
|
+
parts.push(F2);
|
|
3887
|
+
const G = completeG(f, g, F2);
|
|
3441
3888
|
parts.push(G);
|
|
3442
|
-
const polys = { f, g, F, G };
|
|
3889
|
+
const polys = { f, g, F: F2, G };
|
|
3443
3890
|
checkNtruEquation(polys);
|
|
3444
3891
|
return polys;
|
|
3445
3892
|
} catch (error) {
|
|
@@ -3449,13 +3896,13 @@ function decodeSecretKey(secretKey) {
|
|
|
3449
3896
|
wipe(bytes);
|
|
3450
3897
|
}
|
|
3451
3898
|
}
|
|
3452
|
-
function encodeSecretKey({ f, g, F }) {
|
|
3899
|
+
function encodeSecretKey({ f, g, F: F2 }) {
|
|
3453
3900
|
checkRange(f, FG_MAX, "f");
|
|
3454
3901
|
checkRange(g, FG_MAX, "g");
|
|
3455
|
-
checkRange(
|
|
3902
|
+
checkRange(F2, CAPITAL_FG_MAX, "F");
|
|
3456
3903
|
const fBits = Array.from(f, (v) => v & 63);
|
|
3457
3904
|
const gBits = Array.from(g, (v) => v & 63);
|
|
3458
|
-
const capitalFBits = Array.from(
|
|
3905
|
+
const capitalFBits = Array.from(F2, (v) => v & 255);
|
|
3459
3906
|
const encoded = [
|
|
3460
3907
|
encodeBitsMSB(fBits, FG_BITS),
|
|
3461
3908
|
encodeBitsMSB(gBits, FG_BITS),
|
|
@@ -3520,7 +3967,7 @@ function importFalconRsSecretKey(secretKey) {
|
|
|
3520
3967
|
const parts = [];
|
|
3521
3968
|
try {
|
|
3522
3969
|
const names = ["f", "g", "F", "G"];
|
|
3523
|
-
const [f, g,
|
|
3970
|
+
const [f, g, F2, G] = names.map((name, k) => {
|
|
3524
3971
|
const chunk = bytes.subarray(k * FALCON_RS_POLY_LENGTH, (k + 1) * FALCON_RS_POLY_LENGTH);
|
|
3525
3972
|
const decoded = decode14LSB(chunk);
|
|
3526
3973
|
parts.push(decoded);
|
|
@@ -3531,13 +3978,13 @@ function importFalconRsSecretKey(secretKey) {
|
|
|
3531
3978
|
parts.push(p);
|
|
3532
3979
|
return p;
|
|
3533
3980
|
});
|
|
3534
|
-
const recomputed = completeG(f, g,
|
|
3981
|
+
const recomputed = completeG(f, g, F2);
|
|
3535
3982
|
parts.push(recomputed);
|
|
3536
3983
|
if (recomputed.some((v, i) => v !== G[i])) {
|
|
3537
3984
|
throw new Error("Falcon512: invalid falcon-rs secret key (G does not match f, g, F)");
|
|
3538
3985
|
}
|
|
3539
|
-
checkNtruEquation({ f, g, F, G });
|
|
3540
|
-
return encodeSecretKey({ f, g, F });
|
|
3986
|
+
checkNtruEquation({ f, g, F: F2, G });
|
|
3987
|
+
return encodeSecretKey({ f, g, F: F2 });
|
|
3541
3988
|
} finally {
|
|
3542
3989
|
wipe(bytes, ...parts);
|
|
3543
3990
|
}
|
|
@@ -3955,9 +4402,240 @@ function verifyMessage(message, signature, fullPublicKey, accountAddressOrOption
|
|
|
3955
4402
|
const hashOptions = typeof accountAddressOrOptions === "object" && accountAddressOrOptions !== null && !Array.isArray(accountAddressOrOptions) ? accountAddressOrOptions : void 0;
|
|
3956
4403
|
return verify(signature, message, fullPublicKey, hashOptions);
|
|
3957
4404
|
}
|
|
4405
|
+
|
|
4406
|
+
// src/falcon/mlkem/index.ts
|
|
4407
|
+
var mlkem_exports = {};
|
|
4408
|
+
__export(mlkem_exports, {
|
|
4409
|
+
createKey: () => createKey,
|
|
4410
|
+
decapsulate: () => decapsulate,
|
|
4411
|
+
encapsulate: () => encapsulate
|
|
4412
|
+
});
|
|
4413
|
+
|
|
4414
|
+
// src/falcon/mlkem/exchange.ts
|
|
4415
|
+
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
4416
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
4417
|
+
import { randomBytes as randomBytes3 } from "@noble/hashes/utils.js";
|
|
4418
|
+
import * as starknet13 from "starknet";
|
|
4419
|
+
|
|
4420
|
+
// src/falcon/mlkem/messages.ts
|
|
4421
|
+
import * as starknet10 from "starknet";
|
|
4422
|
+
var DOMAIN_NAME = "ML-KEM key exchange";
|
|
4423
|
+
var DOMAIN_VERSION = "1";
|
|
4424
|
+
var ALGORITHM = "ML-KEM-768";
|
|
4425
|
+
function messageDomain(chainId) {
|
|
4426
|
+
return {
|
|
4427
|
+
type: [
|
|
4428
|
+
{ name: "name", type: "shortstring" },
|
|
4429
|
+
{ name: "version", type: "shortstring" },
|
|
4430
|
+
{ name: "chainId", type: "shortstring" },
|
|
4431
|
+
{ name: "revision", type: "shortstring" }
|
|
4432
|
+
],
|
|
4433
|
+
domain: {
|
|
4434
|
+
name: DOMAIN_NAME,
|
|
4435
|
+
version: DOMAIN_VERSION,
|
|
4436
|
+
chainId,
|
|
4437
|
+
revision: starknet10.TypedDataRevision.ACTIVE
|
|
4438
|
+
}
|
|
4439
|
+
};
|
|
4440
|
+
}
|
|
4441
|
+
function publicKeyMessage(chainId, keyHash, validUntil) {
|
|
4442
|
+
const { type, domain } = messageDomain(chainId);
|
|
4443
|
+
return {
|
|
4444
|
+
types: {
|
|
4445
|
+
StarknetDomain: type,
|
|
4446
|
+
MlKemPublicKey: [
|
|
4447
|
+
{ name: "Algorithm", type: "shortstring" },
|
|
4448
|
+
{ name: "Key hash", type: "felt" },
|
|
4449
|
+
{ name: "Valid until", type: "timestamp" }
|
|
4450
|
+
]
|
|
4451
|
+
},
|
|
4452
|
+
primaryType: "MlKemPublicKey",
|
|
4453
|
+
domain,
|
|
4454
|
+
message: { Algorithm: ALGORITHM, "Key hash": keyHash, "Valid until": validUntil }
|
|
4455
|
+
};
|
|
4456
|
+
}
|
|
4457
|
+
function capsuleMessage(chainId, keyOwnerAddress, keyHash, capsuleHash, timestamp) {
|
|
4458
|
+
const { type, domain } = messageDomain(chainId);
|
|
4459
|
+
return {
|
|
4460
|
+
types: {
|
|
4461
|
+
StarknetDomain: type,
|
|
4462
|
+
MlKemCapsule: [
|
|
4463
|
+
{ name: "Key owner", type: "ContractAddress" },
|
|
4464
|
+
{ name: "Key hash", type: "felt" },
|
|
4465
|
+
{ name: "Capsule hash", type: "felt" },
|
|
4466
|
+
{ name: "Timestamp", type: "timestamp" }
|
|
4467
|
+
]
|
|
4468
|
+
},
|
|
4469
|
+
primaryType: "MlKemCapsule",
|
|
4470
|
+
domain,
|
|
4471
|
+
message: {
|
|
4472
|
+
"Key owner": starknet10.num.toHex(keyOwnerAddress),
|
|
4473
|
+
"Key hash": keyHash,
|
|
4474
|
+
"Capsule hash": capsuleHash,
|
|
4475
|
+
Timestamp: timestamp
|
|
4476
|
+
}
|
|
4477
|
+
};
|
|
4478
|
+
}
|
|
4479
|
+
|
|
4480
|
+
// src/falcon/mlkem/signatures.ts
|
|
4481
|
+
import * as starknet11 from "starknet";
|
|
4482
|
+
async function checkSignature(provider, typedData3, signature, accountAddress) {
|
|
4483
|
+
const address = starknet11.num.toHex(parseFelt(accountAddress, "ML-KEM account address"));
|
|
4484
|
+
let answer;
|
|
4485
|
+
try {
|
|
4486
|
+
answer = await provider.callContract({
|
|
4487
|
+
contractAddress: address,
|
|
4488
|
+
entrypoint: "get_public_key"
|
|
4489
|
+
});
|
|
4490
|
+
} catch (error) {
|
|
4491
|
+
throw new Error(
|
|
4492
|
+
`ML-KEM: cannot read the public key of the account ${address}. The account must be deployed, and must be a Falcon-512 account of OpenZeppelin.`,
|
|
4493
|
+
{ cause: error }
|
|
4494
|
+
);
|
|
4495
|
+
}
|
|
4496
|
+
if (answer.length !== PACKED_SLOTS + 1 || BigInt(answer[0]) !== BigInt(PACKED_SLOTS)) {
|
|
4497
|
+
throw new Error(`ML-KEM: the account ${address} is not a Falcon-512 account of OpenZeppelin.`);
|
|
4498
|
+
}
|
|
4499
|
+
const length = Array.isArray(signature) ? signature.length : 0;
|
|
4500
|
+
let accountType;
|
|
4501
|
+
if (length === 60) {
|
|
4502
|
+
accountType = "oz-hint";
|
|
4503
|
+
} else if (length === 31) {
|
|
4504
|
+
accountType = "oz-direct";
|
|
4505
|
+
} else {
|
|
4506
|
+
throw new Error(
|
|
4507
|
+
`ML-KEM: the signature of the account ${address} is not a Falcon-512 signature.`
|
|
4508
|
+
);
|
|
4509
|
+
}
|
|
4510
|
+
if (!verifyMessage(typedData3, signature, answer.slice(1), address, { accountType })) {
|
|
4511
|
+
throw new Error(`ML-KEM: the signature of the account ${address} is not valid.`);
|
|
4512
|
+
}
|
|
4513
|
+
}
|
|
4514
|
+
|
|
4515
|
+
// src/falcon/mlkem/utils/bytes.ts
|
|
4516
|
+
import { isBytes as isBytes3 } from "@noble/hashes/utils.js";
|
|
4517
|
+
import * as starknet12 from "starknet";
|
|
4518
|
+
function checkBytes(value, size, name) {
|
|
4519
|
+
if (!isBytes3(value) || value.length !== size) {
|
|
4520
|
+
throw new Error(`ML-KEM: the ${name} must be a Uint8Array of ${size} bytes.`);
|
|
4521
|
+
}
|
|
4522
|
+
return value;
|
|
4523
|
+
}
|
|
4524
|
+
function hashBytes(bytes) {
|
|
4525
|
+
const groupSize = starknet12.constants.TEXT_TO_FELT_MAX_LEN;
|
|
4526
|
+
const felts = [];
|
|
4527
|
+
for (let start = 0; start < bytes.length; start += groupSize) {
|
|
4528
|
+
const group = bytes.subarray(start, start + groupSize);
|
|
4529
|
+
felts.push(starknet12.encode.addHexPrefix(starknet12.encode.buf2hex(group)));
|
|
4530
|
+
}
|
|
4531
|
+
return starknet12.hash.computePoseidonHashOnElements(felts);
|
|
4532
|
+
}
|
|
4533
|
+
|
|
4534
|
+
// src/falcon/mlkem/utils/time.ts
|
|
4535
|
+
var MAX_TIME = 4294967295;
|
|
4536
|
+
function nowInSeconds() {
|
|
4537
|
+
return Math.floor(Date.now() / 1e3);
|
|
4538
|
+
}
|
|
4539
|
+
function checkTime(value, name) {
|
|
4540
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > MAX_TIME) {
|
|
4541
|
+
throw new Error(
|
|
4542
|
+
`ML-KEM: ${name} must be a time in seconds, from 0 to ${MAX_TIME}. Note: Date.now() gives milliseconds, so divide it by 1000.`
|
|
4543
|
+
);
|
|
4544
|
+
}
|
|
4545
|
+
return value;
|
|
4546
|
+
}
|
|
4547
|
+
|
|
4548
|
+
// src/falcon/mlkem/exchange.ts
|
|
4549
|
+
var PUBLIC_KEY_SIZE = 1184;
|
|
4550
|
+
var CAPSULE_SIZE = 1088;
|
|
4551
|
+
var SEED_SIZE = 64;
|
|
4552
|
+
var SHARED_SECRET_SIZE = 32;
|
|
4553
|
+
function finalSecret(mlKemSecret, context) {
|
|
4554
|
+
const contextHash = starknet13.hash.computePoseidonHashOnElements([
|
|
4555
|
+
starknet13.CairoBytes31.fromText(DOMAIN_NAME).toBigInt(),
|
|
4556
|
+
starknet13.CairoBytes31.fromText(DOMAIN_VERSION).toBigInt(),
|
|
4557
|
+
context.chainId,
|
|
4558
|
+
context.keyOwnerAddress,
|
|
4559
|
+
context.senderAddress,
|
|
4560
|
+
context.keyHash,
|
|
4561
|
+
context.capsuleHash
|
|
4562
|
+
]);
|
|
4563
|
+
const info = starknet13.num.hexToBytes(starknet13.num.toHex64(contextHash));
|
|
4564
|
+
return hkdf(sha256, mlKemSecret, void 0, info, SHARED_SECRET_SIZE);
|
|
4565
|
+
}
|
|
4566
|
+
function createKey(chainId, validUntil) {
|
|
4567
|
+
if (typeof chainId !== "string" || chainId === "") {
|
|
4568
|
+
throw new Error("ML-KEM: chainId must be the chain id, as provider.getChainId() returns it.");
|
|
4569
|
+
}
|
|
4570
|
+
checkTime(validUntil, "validUntil");
|
|
4571
|
+
if (validUntil <= nowInSeconds()) throw new Error("ML-KEM: validUntil must be in the future.");
|
|
4572
|
+
const seed = randomBytes3(SEED_SIZE);
|
|
4573
|
+
const { publicKey, secretKey } = ml_kem768.keygen(seed);
|
|
4574
|
+
secretKey.fill(0);
|
|
4575
|
+
const typedData3 = publicKeyMessage(chainId, hashBytes(publicKey), validUntil);
|
|
4576
|
+
return { seed, publicKey, typedData: typedData3 };
|
|
4577
|
+
}
|
|
4578
|
+
async function encapsulate(provider, signedKey, senderAddress) {
|
|
4579
|
+
checkBytes(signedKey.publicKey, PUBLIC_KEY_SIZE, "public key");
|
|
4580
|
+
checkTime(signedKey.validUntil, "validUntil");
|
|
4581
|
+
const aliceAddress = parseFelt(signedKey.keyOwnerAddress, "ML-KEM key owner address");
|
|
4582
|
+
const bobAddress = parseFelt(senderAddress, "ML-KEM sender address");
|
|
4583
|
+
if (signedKey.validUntil <= nowInSeconds()) {
|
|
4584
|
+
const owner = starknet13.num.toHex(aliceAddress);
|
|
4585
|
+
throw new Error(`ML-KEM: the key of the account ${owner} has expired.`);
|
|
4586
|
+
}
|
|
4587
|
+
const chainId = await provider.getChainId();
|
|
4588
|
+
const keyHash = hashBytes(signedKey.publicKey);
|
|
4589
|
+
const keyMessage = publicKeyMessage(chainId, keyHash, signedKey.validUntil);
|
|
4590
|
+
await checkSignature(provider, keyMessage, signedKey.signature, aliceAddress);
|
|
4591
|
+
const { cipherText: capsule, sharedSecret: mlKemSecret } = ml_kem768.encapsulate(
|
|
4592
|
+
signedKey.publicKey
|
|
4593
|
+
);
|
|
4594
|
+
const capsuleHash = hashBytes(capsule);
|
|
4595
|
+
const timestamp = nowInSeconds();
|
|
4596
|
+
const sharedSecret = finalSecret(mlKemSecret, {
|
|
4597
|
+
chainId,
|
|
4598
|
+
keyOwnerAddress: aliceAddress,
|
|
4599
|
+
senderAddress: bobAddress,
|
|
4600
|
+
keyHash,
|
|
4601
|
+
capsuleHash
|
|
4602
|
+
});
|
|
4603
|
+
mlKemSecret.fill(0);
|
|
4604
|
+
const typedData3 = capsuleMessage(chainId, aliceAddress, keyHash, capsuleHash, timestamp);
|
|
4605
|
+
return { sharedSecret, capsule, timestamp, typedData: typedData3 };
|
|
4606
|
+
}
|
|
4607
|
+
async function decapsulate(provider, signedCapsule, keyOwnerAddress, seed) {
|
|
4608
|
+
checkBytes(signedCapsule.capsule, CAPSULE_SIZE, "capsule");
|
|
4609
|
+
checkTime(signedCapsule.timestamp, "timestamp");
|
|
4610
|
+
checkBytes(seed, SEED_SIZE, "seed");
|
|
4611
|
+
const aliceAddress = parseFelt(keyOwnerAddress, "ML-KEM key owner address");
|
|
4612
|
+
const bobAddress = parseFelt(signedCapsule.senderAddress, "ML-KEM sender address");
|
|
4613
|
+
const { publicKey, secretKey } = ml_kem768.keygen(seed);
|
|
4614
|
+
try {
|
|
4615
|
+
const chainId = await provider.getChainId();
|
|
4616
|
+
const keyHash = hashBytes(publicKey);
|
|
4617
|
+
const capsuleHash = hashBytes(signedCapsule.capsule);
|
|
4618
|
+
const { timestamp } = signedCapsule;
|
|
4619
|
+
const typedData3 = capsuleMessage(chainId, aliceAddress, keyHash, capsuleHash, timestamp);
|
|
4620
|
+
await checkSignature(provider, typedData3, signedCapsule.signature, bobAddress);
|
|
4621
|
+
const mlKemSecret = ml_kem768.decapsulate(signedCapsule.capsule, secretKey);
|
|
4622
|
+
const sharedSecret = finalSecret(mlKemSecret, {
|
|
4623
|
+
chainId,
|
|
4624
|
+
keyOwnerAddress: aliceAddress,
|
|
4625
|
+
senderAddress: bobAddress,
|
|
4626
|
+
keyHash,
|
|
4627
|
+
capsuleHash
|
|
4628
|
+
});
|
|
4629
|
+
mlKemSecret.fill(0);
|
|
4630
|
+
return sharedSecret;
|
|
4631
|
+
} finally {
|
|
4632
|
+
secretKey.fill(0);
|
|
4633
|
+
}
|
|
4634
|
+
}
|
|
3958
4635
|
export {
|
|
3959
4636
|
Falcon512Signer,
|
|
3960
|
-
falcon512_exports as falcon512
|
|
4637
|
+
falcon512_exports as falcon512,
|
|
4638
|
+
mlkem_exports as mlkem
|
|
3961
4639
|
};
|
|
3962
4640
|
/*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
|
|
3963
4641
|
//# sourceMappingURL=index.mjs.map
|