@blamejs/core 0.18.55 → 0.18.57

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.
@@ -1,4 +1,4 @@
1
- // @noble/post-quantum v0.7.0 — vendored from Paul Miller
1
+ // @noble/post-quantum v0.7.1 — vendored from Paul Miller
2
2
  // License: MIT — https://github.com/paulmillr/noble-post-quantum
3
3
  // Browser build (ESM), bundled with esbuild from the same install as the
4
4
  // server bundle beside it. The KEM suites only — a client half encapsulates
@@ -57,6 +57,14 @@ var aobject = (value, label) => {
57
57
  if (value === null || typeof value !== "object" || Array.isArray(value))
58
58
  throw new TypeError((label === "object" ? "" : `"${label}" `) + "expected object, got type=" + typeof value);
59
59
  };
60
+ var aopts = (value, label) => {
61
+ aobject(value, label);
62
+ const proto = Object.getPrototypeOf(value);
63
+ if (proto !== Object.prototype && proto !== null)
64
+ throw new TypeError(`"${label}" expected plain object`);
65
+ if (Object.hasOwn(value, "__proto__"))
66
+ throw new TypeError(`"${label}.__proto__" is not allowed`);
67
+ };
60
68
  function aexists(instance, checkFinished = true) {
61
69
  if (instance.destroyed)
62
70
  throw new Error("hash was destroyed");
@@ -90,10 +98,10 @@ function byteSwap32(arr) {
90
98
  }
91
99
  var swap32IfBE = isLE ? (u) => u : byteSwap32;
92
100
  function checkOpts(defaults, opts2, title = "opts") {
93
- aobject(defaults, "defaults");
101
+ aopts(defaults, "defaults");
94
102
  if (opts2 !== void 0)
95
- aobject(opts2, title);
96
- const merged = Object.assign(defaults, opts2);
103
+ aopts(opts2, title);
104
+ const merged = Object.assign(/* @__PURE__ */ Object.create(null), defaults, opts2);
97
105
  return merged;
98
106
  }
99
107
  function createHasher(hashCons, info = {}) {
@@ -509,7 +517,7 @@ function equalBytes(a, b) {
509
517
  return diff === 0;
510
518
  }
511
519
  function copyBytes(bytes) {
512
- return Uint8Array.from(abytes(bytes));
520
+ return new Uint8Array(abytes(bytes));
513
521
  }
514
522
  function splitCoder(label, ...lengths) {
515
523
  const getLength = (c) => typeof c === "number" ? c : c.bytesLen;
@@ -937,8 +945,9 @@ var genKPKE = (opts_) => {
937
945
  for (let i = 0; i < K; i++)
938
946
  polyAdd(tmp, MultiplyNTTs(sk[i], crystals.NTT.encode(u[i])));
939
947
  polySub(v, crystals.NTT.decode(tmp));
940
- cleanBytes(tmp, sk, u);
941
- return poly1.encode(v);
948
+ const res = poly1.encode(v);
949
+ cleanBytes(tmp, sk, u, v);
950
+ return res;
942
951
  }
943
952
  };
944
953
  };
@@ -968,32 +977,55 @@ function createKyber(opts2) {
968
977
  return Object.freeze({
969
978
  info: Object.freeze({ type: "ml-kem" }),
970
979
  lengths: kemLengths,
971
- keygen: (seed = randomBytes2(seedLen)) => {
972
- abytesDoc(seed, seedLen, "seed");
973
- const { publicKey, secretKey: sk } = KPKE.keygen(seed.subarray(0, 32));
974
- const publicKeyHash = HASH256(publicKey);
975
- const secretKey = secretCoder.encode([sk, publicKey, publicKeyHash, seed.subarray(32)]);
976
- cleanBytes(sk, publicKeyHash);
977
- return {
978
- publicKey,
979
- secretKey
980
- };
980
+ keygen: (seed) => {
981
+ const ownSeed = seed === void 0;
982
+ const s = ownSeed ? randomBytes2(seedLen) : seed;
983
+ let sk;
984
+ let publicKeyHash;
985
+ try {
986
+ abytesDoc(s, seedLen, "seed");
987
+ const keys = KPKE.keygen(s.subarray(0, 32));
988
+ const publicKey = keys.publicKey;
989
+ sk = keys.secretKey;
990
+ publicKeyHash = HASH256(publicKey);
991
+ const secretKey = secretCoder.encode([sk, publicKey, publicKeyHash, s.subarray(32)]);
992
+ return {
993
+ publicKey,
994
+ secretKey
995
+ };
996
+ } finally {
997
+ if (sk !== void 0)
998
+ cleanBytes(sk);
999
+ if (publicKeyHash !== void 0)
1000
+ cleanBytes(publicKeyHash);
1001
+ if (ownSeed)
1002
+ cleanBytes(s);
1003
+ }
981
1004
  },
982
1005
  getPublicKey: (secretKey) => {
983
1006
  const [_sk, publicKey, _publicKeyHash, _z] = secretCoder.decode(secretKey);
984
1007
  return Uint8Array.from(publicKey);
985
1008
  },
986
- encapsulate: (publicKey, msg = randomBytes2(msgLen)) => {
987
- abytesDoc(publicKey, lengths.publicKey, "publicKey");
988
- abytesDoc(msg, msgLen, "message");
989
- validateModulus(publicKey, "encapsulate");
990
- const kr = HASH512.create().update(msg).update(HASH256(publicKey)).digest();
991
- const cipherText = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
992
- cleanBytes(kr.subarray(32));
993
- return {
994
- cipherText,
995
- sharedSecret: kr.subarray(0, 32)
996
- };
1009
+ encapsulate: (publicKey, msg) => {
1010
+ const ownMsg = msg === void 0;
1011
+ const m = ownMsg ? randomBytes2(msgLen) : msg;
1012
+ let kr;
1013
+ try {
1014
+ abytesDoc(publicKey, lengths.publicKey, "publicKey");
1015
+ abytesDoc(m, msgLen, "message");
1016
+ validateModulus(publicKey, "encapsulate");
1017
+ kr = HASH512.create().update(m).update(HASH256(publicKey)).digest();
1018
+ const cipherText = KPKE.encrypt(publicKey, m, kr.subarray(32, 64));
1019
+ return {
1020
+ cipherText,
1021
+ sharedSecret: kr.subarray(0, 32)
1022
+ };
1023
+ } finally {
1024
+ if (kr !== void 0)
1025
+ cleanBytes(kr.subarray(32));
1026
+ if (ownMsg)
1027
+ cleanBytes(m);
1028
+ }
997
1029
  },
998
1030
  decapsulate: (cipherText, secretKey) => {
999
1031
  abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
@@ -1026,15 +1058,24 @@ function createKyber(opts2) {
1026
1058
  const cached = KPKE.prepare(ek);
1027
1059
  return Object.freeze({
1028
1060
  publicKey: ek,
1029
- encapsulate: (msg = randomBytes2(msgLen)) => {
1030
- abytesDoc(msg, msgLen, "message");
1031
- const kr = HASH512.create().update(msg).update(publicKeyHash).digest();
1032
- const cipherText = cached.encrypt(msg, kr.subarray(32, 64));
1033
- cleanBytes(kr.subarray(32));
1034
- return {
1035
- cipherText,
1036
- sharedSecret: kr.subarray(0, 32)
1037
- };
1061
+ encapsulate: (msg) => {
1062
+ const ownMsg = msg === void 0;
1063
+ const m = ownMsg ? randomBytes2(msgLen) : msg;
1064
+ let kr;
1065
+ try {
1066
+ abytesDoc(m, msgLen, "message");
1067
+ kr = HASH512.create().update(m).update(publicKeyHash).digest();
1068
+ const cipherText = cached.encrypt(m, kr.subarray(32, 64));
1069
+ return {
1070
+ cipherText,
1071
+ sharedSecret: kr.subarray(0, 32)
1072
+ };
1073
+ } finally {
1074
+ if (kr !== void 0)
1075
+ cleanBytes(kr.subarray(32));
1076
+ if (ownMsg)
1077
+ cleanBytes(m);
1078
+ }
1038
1079
  },
1039
1080
  decapsulate: (cipherText, secretKey) => {
1040
1081
  abytesDoc(secretKey, secretCoder.bytesLen, "secretKey");
@@ -1,4 +1,4 @@
1
- // XChaCha20-Poly1305 — vendored from @noble/ciphers v2.3.0 by Paul Miller
1
+ // XChaCha20-Poly1305 — vendored from @noble/ciphers v2.4.0 by Paul Miller
2
2
  // License: MIT — https://github.com/paulmillr/noble-ciphers
3
3
  // Bundled with esbuild. Exports: xchacha20poly1305
4
4
  var __defProp = Object.defineProperty;
@@ -95,6 +95,17 @@ function byteSwap32(arr) {
95
95
  return arr;
96
96
  }
97
97
  var swap32IfBE = isLE ? (u) => u : byteSwap32;
98
+ function overlapBytes(a, b) {
99
+ if (!a.byteLength || !b.byteLength)
100
+ return false;
101
+ return a.buffer === b.buffer && // best we can do, may fail with an obscure Proxy
102
+ a.byteOffset < b.byteOffset + b.byteLength && // a starts before b end
103
+ b.byteOffset < a.byteOffset + a.byteLength;
104
+ }
105
+ function complexOverlapBytes(input, output) {
106
+ if (overlapBytes(input, output) && input.byteOffset < output.byteOffset)
107
+ throw new Error("complex overlap of input and output is not supported");
108
+ }
98
109
  function checkOpts(defaults, opts) {
99
110
  aobject(defaults, "defaults");
100
111
  aobject(opts, "opts");
@@ -262,7 +273,10 @@ function createCipher(core, opts) {
262
273
  abytes(nonce, void 0, "nonce");
263
274
  abytes(data, void 0, "data");
264
275
  const len = data.length;
276
+ const hasOutput = output !== void 0;
265
277
  output = getOutput(len, output, false);
278
+ if (hasOutput)
279
+ complexOverlapBytes(data, output);
266
280
  anumber(counter);
267
281
  if (counter < 0 || counter >= MAX_COUNTER)
268
282
  throw new Error("arx: counter overflow");
@@ -1,4 +1,4 @@
1
- // @noble/curves v2.3.0 — vendored from Paul Miller
1
+ // @noble/curves v2.4.0 — vendored from Paul Miller
2
2
  // License: MIT — https://github.com/paulmillr/noble-curves
3
3
  // Bundled with esbuild. Exports the RFC 9497 OPRF suites:
4
4
  // ristretto255_oprf (ristretto255-SHA512), p256_oprf (P-256-SHA256),
@@ -104,6 +104,14 @@ var aobject = (value, label) => {
104
104
  if (value === null || typeof value !== "object" || Array.isArray(value))
105
105
  throw new TypeError((label === "object" ? "" : `"${label}" `) + "expected object, got type=" + typeof value);
106
106
  };
107
+ var aopts = (value, label) => {
108
+ aobject(value, label);
109
+ const proto = Object.getPrototypeOf(value);
110
+ if (proto !== Object.prototype && proto !== null)
111
+ throw new TypeError(`"${label}" expected plain object`);
112
+ if (Object.hasOwn(value, "__proto__"))
113
+ throw new TypeError(`"${label}.__proto__" is not allowed`);
114
+ };
107
115
  function aexists(instance, checkFinished = true) {
108
116
  if (instance.destroyed)
109
117
  throw new Error("hash was destroyed");
@@ -190,10 +198,10 @@ function concatBytes(...arrays) {
190
198
  return res;
191
199
  }
192
200
  function checkOpts(defaults, opts, title = "opts") {
193
- aobject(defaults, "defaults");
201
+ aopts(defaults, "defaults");
194
202
  if (opts !== void 0)
195
- aobject(opts, title);
196
- const merged = Object.assign(defaults, opts);
203
+ aopts(opts, title);
204
+ const merged = Object.assign(/* @__PURE__ */ Object.create(null), defaults, opts);
197
205
  return merged;
198
206
  }
199
207
  function createHasher(hashCons, info = {}) {
@@ -1019,6 +1027,17 @@ function invert(number, modulo) {
1019
1027
  throw new Error("invert: does not exist");
1020
1028
  return mod(x, modulo);
1021
1029
  }
1030
+ function invertCt(a, prime) {
1031
+ if (prime <= _1n2)
1032
+ throw new Error("invertCt: expected prime modulus > 1, got " + prime);
1033
+ const an = mod(a, prime);
1034
+ if (an === _0n2)
1035
+ throw new Error("invertCt: expected non-zero number");
1036
+ const inverse = pow(an, prime - _2n, prime);
1037
+ if (mod(an * inverse, prime) !== _1n2)
1038
+ throw new Error("invertCt: does not exist");
1039
+ return inverse;
1040
+ }
1022
1041
  function assertIsSquare(Fp2, root, n) {
1023
1042
  const F = Fp2;
1024
1043
  if (!F.eql(F.sqr(root), n))
@@ -2454,12 +2473,13 @@ function createOPRF(opts) {
2454
2473
  hashToGroup: "function"
2455
2474
  });
2456
2475
  validatePointCons(opts.Point);
2457
- const { name, Point, hash } = opts;
2476
+ const { name, Point, hash, hashToGroup: hashToGroupHook, hashToScalar } = opts;
2458
2477
  const { Fn: Fn2 } = Point;
2459
- const hashToGroup = (msg, ctx) => opts.hashToGroup(msg, {
2478
+ const invertSecret = (value) => invertCt(value, Fn2.ORDER);
2479
+ const hashToGroup = (msg, ctx) => hashToGroupHook(msg, {
2460
2480
  DST: concatBytes2(asciiToBytes("HashToGroup-"), ctx)
2461
2481
  });
2462
- const hashToScalarPrefixed = (msg, ctx) => opts.hashToScalar(msg, { DST: concatBytes2(_DST_scalarBytes, ctx) });
2482
+ const hashToScalarPrefixed = (msg, ctx) => hashToScalar(msg, { DST: concatBytes2(_DST_scalarBytes, ctx) });
2463
2483
  const randomScalar = (rng = randomBytes2) => {
2464
2484
  if (typeof rng !== "function")
2465
2485
  throw new TypeError('"rng" expected function, got type=' + typeof rng);
@@ -2551,7 +2571,7 @@ function createOPRF(opts) {
2551
2571
  const msg = concatBytes2(seed, encode(info), Uint8Array.of(0));
2552
2572
  for (let counter = 0; counter <= 255; counter++) {
2553
2573
  msg[msg.length - 1] = counter;
2554
- const skS = opts.hashToScalar(msg, { DST: dst });
2574
+ const skS = hashToScalar(msg, { DST: dst });
2555
2575
  if (Fn2.is0(skS))
2556
2576
  continue;
2557
2577
  return {
@@ -2669,7 +2689,7 @@ function createOPRF(opts) {
2669
2689
  throw new Error("expected array");
2670
2690
  const skS = Fn2.fromBytes(secretKey);
2671
2691
  const t = Fn2.add(skS, m);
2672
- const invT = Fn2.inv(t);
2692
+ const invT = invertSecret(t);
2673
2693
  const blindedPoints = blinded.map((i) => wirePoint("blinded", i));
2674
2694
  const evalPoints = blindedPoints.map((i) => i.multiply(invT));
2675
2695
  const tweakedKey = Point.BASE.multiply(t);
@@ -2702,13 +2722,13 @@ function createOPRF(opts) {
2702
2722
  if (inputPoint.equals(Point.ZERO))
2703
2723
  throw new Error("Input point at infinity");
2704
2724
  const t = Fn2.add(skS, m);
2705
- const invT = Fn2.inv(t);
2725
+ const invT = invertSecret(t);
2706
2726
  const unblinded = inputPoint.multiply(invT).toBytes();
2707
2727
  return hashInput(input, info, unblinded);
2708
2728
  }
2709
2729
  });
2710
2730
  };
2711
- const res = { name, oprf, voprf, poprf, __tests: Object.freeze({ Fn: Fn2 }) };
2731
+ const res = { name, oprf, voprf, poprf, __tests: Object.freeze({ Fn: Fn2, invertSecret }) };
2712
2732
  return Object.freeze(res);
2713
2733
  }
2714
2734
 
@@ -2999,21 +3019,28 @@ function weierstrass(params, extraOpts = {}) {
2999
3019
  endo: "object",
3000
3020
  randomBytes: "function"
3001
3021
  });
3002
- const { endo, allowInfinityPoint } = extraOpts;
3022
+ const { endo: endoOpts, allowInfinityPoint, clearCofactor, isTorsionFree, fromBytes, toBytes } = extraOpts;
3003
3023
  const randomBytes3 = extraOpts.randomBytes === void 0 ? randomBytes2 : extraOpts.randomBytes;
3004
- if (endo) {
3005
- if (!Fp2.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) {
3024
+ if (endoOpts) {
3025
+ if (!Fp2.is0(CURVE.a) || typeof endoOpts.beta !== "bigint" || !Array.isArray(endoOpts.basises)) {
3006
3026
  throw new Error('invalid endo: expected "beta": bigint and "basises": array');
3007
3027
  }
3008
3028
  }
3029
+ const endo = endoOpts ? {
3030
+ beta: endoOpts.beta,
3031
+ basises: endoOpts.basises.map((basis) => [...basis])
3032
+ } : void 0;
3009
3033
  const lengths = getWLengths(Fp2, Fn2);
3010
3034
  function assertCompressionIsSupported() {
3011
3035
  if (!Fp2.isOdd)
3012
3036
  throw new Error("compression is not supported: Field does not have .isOdd()");
3013
3037
  }
3014
3038
  function pointToBytes(_c, point, isCompressed) {
3015
- if (allowInfinityPoint && point.is0())
3039
+ if (point.is0()) {
3040
+ if (!allowInfinityPoint)
3041
+ throw new Error("bad point: ZERO");
3016
3042
  return Uint8Array.of(0);
3043
+ }
3017
3044
  const { x, y } = point.toAffine();
3018
3045
  const bx = Fp2.toBytes(x);
3019
3046
  abool(isCompressed, "isCompressed");
@@ -3062,8 +3089,8 @@ function weierstrass(params, extraOpts = {}) {
3062
3089
  throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
3063
3090
  }
3064
3091
  }
3065
- const encodePoint = extraOpts.toBytes === void 0 ? pointToBytes : extraOpts.toBytes;
3066
- const decodePoint = extraOpts.fromBytes === void 0 ? pointFromBytes : extraOpts.fromBytes;
3092
+ const encodePoint = toBytes === void 0 ? pointToBytes : toBytes;
3093
+ const decodePoint = fromBytes === void 0 ? pointFromBytes : fromBytes;
3067
3094
  const b3 = Fp2.mul(CURVE.b, _3n3);
3068
3095
  const mulA = Fp2.is0(CURVE.a) ? (_) => Fp2.ZERO : (x) => Fp2.mul(CURVE.a, x);
3069
3096
  function weierstrassEquation(x) {
@@ -3085,7 +3112,7 @@ function weierstrass(params, extraOpts = {}) {
3085
3112
  function acoord(title, n, banZero = false) {
3086
3113
  if (!Fp2.isValid(n) || banZero && Fp2.is0(n))
3087
3114
  throw new Error(`bad point coordinate ${title}`);
3088
- return n;
3115
+ return typeof n === "object" && n !== null ? Fp2.create(n) : n;
3089
3116
  }
3090
3117
  function aprjpoint(other) {
3091
3118
  if (!(other instanceof Point))
@@ -3167,7 +3194,7 @@ function weierstrass(params, extraOpts = {}) {
3167
3194
  assertValidity() {
3168
3195
  const p = this;
3169
3196
  if (p.is0()) {
3170
- if (extraOpts.allowInfinityPoint && Fp2.is0(p.X) && Fp2.eql(p.Y, Fp2.ONE) && Fp2.is0(p.Z))
3197
+ if (allowInfinityPoint && Fp2.is0(p.X) && Fp2.eql(p.Y, Fp2.ONE) && Fp2.is0(p.Z))
3171
3198
  return;
3172
3199
  throw new Error("bad point: ZERO");
3173
3200
  }
@@ -3379,7 +3406,6 @@ function weierstrass(params, extraOpts = {}) {
3379
3406
  * Always torsion-free for cofactor=1 curves.
3380
3407
  */
3381
3408
  isTorsionFree() {
3382
- const { isTorsionFree } = extraOpts;
3383
3409
  if (cofactor === _1n7)
3384
3410
  return true;
3385
3411
  if (isTorsionFree)
@@ -3387,7 +3413,6 @@ function weierstrass(params, extraOpts = {}) {
3387
3413
  return wnaf.mulUnsafe(this, CURVE_ORDER).is0();
3388
3414
  }
3389
3415
  clearCofactor() {
3390
- const { clearCofactor } = extraOpts;
3391
3416
  if (cofactor === _1n7)
3392
3417
  return this;
3393
3418
  if (clearCofactor)