starknet 11.7.2 → 11.8.0

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.
@@ -36,11 +36,77 @@ __export(falcon_exports, {
36
36
  module.exports = __toCommonJS(falcon_exports);
37
37
 
38
38
  // src/falcon/Falcon512Signer.ts
39
- var starknet6 = __toESM(require("starknet"));
39
+ var starknet8 = __toESM(require("starknet"));
40
40
 
41
- // src/falcon/s2morrow/keys.ts
42
- var import_utils6 = require("@noble/hashes/utils.js");
43
- var starknet3 = __toESM(require("starknet"));
41
+ // src/falcon/accountType.ts
42
+ var ACCOUNT_TYPES = Object.freeze([
43
+ "s2morrow",
44
+ "oz-hint",
45
+ "oz-direct"
46
+ ]);
47
+ function parseFalconOptions(options) {
48
+ if (options === void 0) return "s2morrow";
49
+ if (typeof options !== "object" || options === null || Array.isArray(options)) {
50
+ throw new Error("Falcon512: options must be an object");
51
+ }
52
+ Object.keys(options).forEach((key) => {
53
+ if (key !== "accountType") throw new Error(`Falcon512: unknown option "${key}"`);
54
+ });
55
+ const { accountType } = options;
56
+ if (accountType === void 0) return "s2morrow";
57
+ if (!ACCOUNT_TYPES.includes(accountType)) {
58
+ throw new Error(
59
+ `Falcon512: unknown accountType "${String(accountType)}" (expected 's2morrow', 'oz-hint' or 'oz-direct')`
60
+ );
61
+ }
62
+ return accountType;
63
+ }
64
+
65
+ // src/falcon/oz/ownership.ts
66
+ var starknet2 = __toESM(require("starknet"));
67
+
68
+ // src/falcon/s2morrow/constants.ts
69
+ var Q = 12289;
70
+ var QB = 12289n;
71
+ var N = 512;
72
+ var SIG_BOUND_512 = 34034726;
73
+ var PACKED_SLOTS = 29;
74
+ var VALUES_PER_HALF = 9;
75
+ var SALT_FELTS = 2;
76
+ var SIGNATURE_FELTS = 61;
77
+
78
+ // src/falcon/s2morrow/felt.ts
79
+ var starknet = __toESM(require("starknet"));
80
+ function assertFelt(x, label) {
81
+ if (typeof x !== "bigint") throw new Error(`Falcon512: ${label} is not a felt252`);
82
+ starknet.CairoFelt252.assertRange(x);
83
+ }
84
+ var FELT_STRING = /^(0[xX][0-9a-fA-F]{1,64}|[0-9]{1,76})$/;
85
+ function parseFelt(value, label) {
86
+ const accepted = typeof value === "bigint" && value >= 0n || typeof value === "number" && Number.isSafeInteger(value) && value >= 0 || typeof value === "string" && FELT_STRING.test(value);
87
+ if (!accepted) throw new Error(`Falcon512: ${label} is not a felt252`);
88
+ return new starknet.CairoFelt252(value).toBigInt();
89
+ }
90
+
91
+ // src/falcon/oz/ownership.ts
92
+ function ownershipAcceptanceHash(accountAddress, currentPublicKey) {
93
+ if (!Array.isArray(currentPublicKey) || currentPublicKey.length !== PACKED_SLOTS && currentPublicKey.length !== 0) {
94
+ throw new Error("Falcon512: current public key must have 29 felts, or none");
95
+ }
96
+ const address = parseFelt(accountAddress, "account address");
97
+ const key = currentPublicKey.map((x, i) => parseFelt(x, `current public key[${i}]`));
98
+ const { computePoseidonHashOnElements } = starknet2.hash;
99
+ return computePoseidonHashOnElements([
100
+ starknet2.CairoBytes31.fromText("StarkNet Message").toBigInt(),
101
+ starknet2.CairoBytes31.fromText("accept_ownership").toBigInt(),
102
+ address,
103
+ computePoseidonHashOnElements(key)
104
+ ]);
105
+ }
106
+
107
+ // src/falcon/oz/signature.ts
108
+ var import_utils7 = require("@noble/hashes/utils.js");
109
+ var starknet6 = __toESM(require("starknet"));
44
110
 
45
111
  // src/falcon/engine/noble/falcon.ts
46
112
  var import_aes = require("@noble/ciphers/aes.js");
@@ -505,9 +571,9 @@ var Float = /* @__PURE__ */ Object.freeze({
505
571
  var f64b = (n) => Float.decode((0, import_utils3.numberToHexUnpadded)(n));
506
572
  var EMPTY_CHACHA20_BLOCK = /* @__PURE__ */ new Uint8Array(64);
507
573
  var NONCELEN = 40;
508
- var Q = 12289;
509
- var Qhalf = Q >> 1;
510
- var QBig = BigInt(Q);
574
+ var Q2 = 12289;
575
+ var Qhalf = Q2 >> 1;
576
+ var QBig = BigInt(Q2);
511
577
  var _0n = /* @__PURE__ */ BigInt(0);
512
578
  var _1n = /* @__PURE__ */ BigInt(1);
513
579
  var _10n = /* @__PURE__ */ BigInt(10);
@@ -520,7 +586,7 @@ var MASK_32n = /* @__PURE__ */ BigInt("0xffffffff");
520
586
  var MASK_63n = /* @__PURE__ */ BigInt("0x7fffffffffffffff");
521
587
  var R2 = 10952;
522
588
  var Q0I = 12287;
523
- var F_INV_Q = 1 / Q;
589
+ var F_INV_Q = 1 / Q2;
524
590
  var F_MINUS_INV_Q = -F_INV_Q;
525
591
  var MAX_BL_SMALL = [1, 1, 2, 2, 4, 7, 14, 27, 53, 106, 209];
526
592
  var MAX_BL_LARGE = [2, 2, 5, 7, 12, 21, 40, 78, 157, 308];
@@ -681,9 +747,9 @@ var COMPLEX_ROOTS = /* @__PURE__ */ (() => {
681
747
  var intField = {
682
748
  mul(x, y) {
683
749
  let z = Math.imul(x, y);
684
- let w = Math.imul(Q, Math.imul(z, Q0I) & 65535);
685
- z = (z + w >>> 16) - Q;
686
- z += Q & z >> 31;
750
+ let w = Math.imul(Q2, Math.imul(z, Q0I) & 65535);
751
+ z = (z + w >>> 16) - Q2;
752
+ z += Q2 & z >> 31;
687
753
  return z >>> 0;
688
754
  },
689
755
  inv(y) {
@@ -717,7 +783,7 @@ function getIntPoly(logn) {
717
783
  const F = Number((0, import_modular.invert)(BigInt(n), QBig));
718
784
  const { mod, smod, NTT } = genCrystals({
719
785
  N: n,
720
- Q,
786
+ Q: Q2,
721
787
  F,
722
788
  ROOT_OF_UNITY: 7,
723
789
  newPoly,
@@ -1079,8 +1145,8 @@ function genFalcon(opts) {
1079
1145
  let rt1 = floatPoly.FFT(floatPoly.convSmall(f));
1080
1146
  let rt2 = floatPoly.FFT(floatPoly.convSmall(g));
1081
1147
  const rt3 = floatPoly.invNorm(rt1, rt2);
1082
- rt1 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt1), Q), rt3));
1083
- rt2 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt2), Q), rt3));
1148
+ rt1 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt1), Q2), rt3));
1149
+ rt2 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt2), Q2), rt3));
1084
1150
  let bnorm = 0;
1085
1151
  for (let u = 0; u < rt1.length; u++) {
1086
1152
  bnorm += rt1[u].re * rt1[u].re;
@@ -1272,7 +1338,7 @@ function genFalcon(opts) {
1272
1338
  const G = new Int8Array(N2);
1273
1339
  for (let u = 0; u < N2; u++) {
1274
1340
  let w = t1[u];
1275
- w -= Q & ~-(w - Qhalf >>> 31);
1341
+ w -= Q2 & ~-(w - Qhalf >>> 31);
1276
1342
  const gi = w | 0;
1277
1343
  if (gi < -127 || gi > 127) {
1278
1344
  cleanBytes(t1, t2, tt, G);
@@ -1286,11 +1352,11 @@ function genFalcon(opts) {
1286
1352
  function HashToPoint(nonce, msg) {
1287
1353
  const h = import_sha32.shake256.create().update(nonce).update(msg);
1288
1354
  const c = new Uint16Array(N2);
1289
- const kQ = 5 * Q;
1355
+ const kQ = 5 * Q2;
1290
1356
  for (let i = 0; i < N2; ) {
1291
1357
  const tmp = h.xof(2);
1292
1358
  let w = tmp[0] << 8 | tmp[1];
1293
- if (w < kQ) c[i++] = w % Q;
1359
+ if (w < kQ) c[i++] = w % Q2;
1294
1360
  }
1295
1361
  return c;
1296
1362
  }
@@ -1704,7 +1770,7 @@ function genFalcon(opts) {
1704
1770
  if (!(hashPoint instanceof Uint16Array) || hashPoint.length !== N2)
1705
1771
  throw new Error(`hashPoint: expected Uint16Array(${N2})`);
1706
1772
  for (let i = 0; i < N2; i++)
1707
- if (hashPoint[i] >= Q) throw new Error("hashPoint: coefficient out of range");
1773
+ if (hashPoint[i] >= Q2) throw new Error("hashPoint: coefficient out of range");
1708
1774
  };
1709
1775
  const internal = Object.freeze({
1710
1776
  sign(hashPoint, sk, internalOpts = {}) {
@@ -1806,16 +1872,6 @@ var nobleEngine = Object.freeze({
1806
1872
  verifyHashPoint: (s2, hashPoint, publicKey) => falcon512.internal.verify(s2, hashPoint, publicKey)
1807
1873
  });
1808
1874
 
1809
- // src/falcon/s2morrow/constants.ts
1810
- var Q2 = 12289;
1811
- var QB = 12289n;
1812
- var N = 512;
1813
- var SIG_BOUND_512 = 34034726;
1814
- var PACKED_SLOTS = 29;
1815
- var VALUES_PER_HALF = 9;
1816
- var SALT_FELTS = 2;
1817
- var SIGNATURE_FELTS = 61;
1818
-
1819
1875
  // src/falcon/s2morrow/nttRoots.ts
1820
1876
  var NTT_ROOTS_ALL = Object.freeze([
1821
1877
  1479,
@@ -2845,20 +2901,20 @@ var NTT_ROOTS_ALL = Object.freeze([
2845
2901
  // src/falcon/s2morrow/ntt.ts
2846
2902
  var I2 = 6145;
2847
2903
  var SQR1 = 1479;
2848
- var modQ = (a) => (a % Q2 + Q2) % Q2;
2904
+ var modQ = (a) => (a % Q + Q) % Q;
2849
2905
  function powMod(base, exponent) {
2850
2906
  let result = 1;
2851
2907
  let b = modQ(base);
2852
2908
  for (let e = exponent; e > 0; e >>= 1) {
2853
- if (e & 1) result = result * b % Q2;
2854
- b = b * b % Q2;
2909
+ if (e & 1) result = result * b % Q;
2910
+ b = b * b % Q;
2855
2911
  }
2856
2912
  return result;
2857
2913
  }
2858
2914
  function invMod(x) {
2859
2915
  const r = modQ(x);
2860
2916
  if (r === 0) throw new Error("Falcon512: 0 has no inverse mod q");
2861
- return powMod(r, Q2 - 2);
2917
+ return powMod(r, Q - 2);
2862
2918
  }
2863
2919
  var nttRoots = (n) => NTT_ROOTS_ALL.slice(n - 2, 2 * n - 2);
2864
2920
  function checkSize(n) {
@@ -2869,7 +2925,7 @@ function checkSize(n) {
2869
2925
  function nttRec(f) {
2870
2926
  const n = f.length;
2871
2927
  if (n === 2) {
2872
- const t = SQR1 * f[1] % Q2;
2928
+ const t = SQR1 * f[1] % Q;
2873
2929
  return [modQ(f[0] + t), modQ(f[0] - t)];
2874
2930
  }
2875
2931
  const half = n / 2;
@@ -2884,7 +2940,7 @@ function nttRec(f) {
2884
2940
  const w = nttRoots(n);
2885
2941
  const out = new Array(n);
2886
2942
  for (let i = 0; i < half; i++) {
2887
- const wb = w[2 * i] * b[i] % Q2;
2943
+ const wb = w[2 * i] * b[i] % Q;
2888
2944
  out[2 * i] = modQ(a[i] + wb);
2889
2945
  out[2 * i + 1] = modQ(a[i] - wb);
2890
2946
  }
@@ -2895,7 +2951,7 @@ function inttRec(fNtt) {
2895
2951
  if (n === 2) {
2896
2952
  const s = modQ(fNtt[0] + fNtt[1]);
2897
2953
  const d = modQ(fNtt[0] - fNtt[1]);
2898
- return [I2 * s % Q2, I2 * invMod(SQR1) % Q2 * d % Q2];
2954
+ return [I2 * s % Q, I2 * invMod(SQR1) % Q * d % Q];
2899
2955
  }
2900
2956
  const half = n / 2;
2901
2957
  const w = nttRoots(n);
@@ -2904,8 +2960,8 @@ function inttRec(fNtt) {
2904
2960
  for (let i = 0; i < half; i++) {
2905
2961
  const s = modQ(fNtt[2 * i] + fNtt[2 * i + 1]);
2906
2962
  const d = modQ(fNtt[2 * i] - fNtt[2 * i + 1]);
2907
- f0[i] = I2 * s % Q2;
2908
- f1[i] = I2 * d % Q2 * invMod(w[2 * i]) % Q2;
2963
+ f0[i] = I2 * s % Q;
2964
+ f1[i] = I2 * d % Q * invMod(w[2 * i]) % Q;
2909
2965
  }
2910
2966
  const a = inttRec(f0);
2911
2967
  const b = inttRec(f1);
@@ -2928,14 +2984,14 @@ function mulZq(a, b) {
2928
2984
  if (a.length !== b.length) throw new Error("Falcon512: polynomial sizes differ");
2929
2985
  const an = ntt(a);
2930
2986
  const bn = ntt(b);
2931
- return intt(an.map((v, i) => v * bn[i] % Q2));
2987
+ return intt(an.map((v, i) => v * bn[i] % Q));
2932
2988
  }
2933
2989
  function divZq(a, b) {
2934
2990
  if (a.length !== b.length) throw new Error("Falcon512: polynomial sizes differ");
2935
2991
  const an = ntt(a);
2936
2992
  const bn = ntt(b);
2937
2993
  if (bn.some((v) => v === 0)) throw new Error("Falcon512: polynomial is not invertible mod q");
2938
- return intt(an.map((v, i) => v * invMod(bn[i]) % Q2));
2994
+ return intt(an.map((v, i) => v * invMod(bn[i]) % Q));
2939
2995
  }
2940
2996
  function mulNegacyclicInt(a, b) {
2941
2997
  const n = a.length;
@@ -2955,22 +3011,7 @@ function mulNegacyclicInt(a, b) {
2955
3011
  }
2956
3012
 
2957
3013
  // src/falcon/s2morrow/packing.ts
2958
- var starknet2 = __toESM(require("starknet"));
2959
-
2960
- // src/falcon/s2morrow/felt.ts
2961
- var starknet = __toESM(require("starknet"));
2962
- function assertFelt(x, label) {
2963
- if (typeof x !== "bigint") throw new Error(`Falcon512: ${label} is not a felt252`);
2964
- starknet.CairoFelt252.assertRange(x);
2965
- }
2966
- var FELT_STRING = /^(0[xX][0-9a-fA-F]{1,64}|[0-9]{1,76})$/;
2967
- function parseFelt(value, label) {
2968
- const accepted = typeof value === "bigint" && value >= 0n || typeof value === "number" && Number.isSafeInteger(value) && value >= 0 || typeof value === "string" && FELT_STRING.test(value);
2969
- if (!accepted) throw new Error(`Falcon512: ${label} is not a felt252`);
2970
- return new starknet.CairoFelt252(value).toBigInt();
2971
- }
2972
-
2973
- // src/falcon/s2morrow/packing.ts
3014
+ var starknet3 = __toESM(require("starknet"));
2974
3015
  var horner = (values, start, end) => {
2975
3016
  let acc = 0n;
2976
3017
  for (let i = end - 1; i >= start; i--) acc = acc * QB + BigInt(values[i]);
@@ -2980,7 +3021,7 @@ function packPoly(poly) {
2980
3021
  if (poly.length !== N) throw new Error(`Falcon512: polynomial must have ${N} coefficients`);
2981
3022
  for (let i = 0; i < N; i++) {
2982
3023
  const v = poly[i];
2983
- if (!Number.isInteger(v) || v < 0 || v >= Q2) {
3024
+ if (!Number.isInteger(v) || v < 0 || v >= Q) {
2984
3025
  throw new Error("Falcon512: polynomial coefficient out of range");
2985
3026
  }
2986
3027
  }
@@ -3000,7 +3041,7 @@ function unpackPoly(felts) {
3000
3041
  let idx = 0;
3001
3042
  for (const felt of felts) {
3002
3043
  assertFelt(felt, "packed polynomial slot");
3003
- for (const half of [felt & starknet2.UINT_128_MAX, felt >> 128n]) {
3044
+ for (const half of [felt & starknet3.UINT_128_MAX, felt >> 128n]) {
3004
3045
  const count = Math.min(VALUES_PER_HALF, N - idx);
3005
3046
  if (half >= QB ** BigInt(count)) {
3006
3047
  throw new Error("Falcon512: non-canonical packed polynomial");
@@ -3015,7 +3056,227 @@ function unpackPoly(felts) {
3015
3056
  return out;
3016
3057
  }
3017
3058
 
3059
+ // src/falcon/s2morrow/signature.ts
3060
+ var import_utils6 = require("@noble/hashes/utils.js");
3061
+ var starknet5 = __toESM(require("starknet"));
3062
+
3063
+ // src/falcon/s2morrow/hashToPoint.ts
3064
+ var import_starknet = require("@scure/starknet");
3065
+ var starknet4 = __toESM(require("starknet"));
3066
+ function hashToPoint(message, salt) {
3067
+ message.forEach((m) => assertFelt(m, "message element"));
3068
+ salt.forEach((s) => assertFelt(s, "salt element"));
3069
+ const out = new Uint16Array(N);
3070
+ let idx = 0;
3071
+ const extract6 = (value) => {
3072
+ let v = value;
3073
+ for (let i = 0; i < 6 && idx < N; i++) {
3074
+ out[idx++] = Number(v % QB);
3075
+ v /= QB;
3076
+ }
3077
+ };
3078
+ const extract12 = (felt) => {
3079
+ extract6(felt & starknet4.UINT_128_MAX);
3080
+ extract6(felt >> 128n);
3081
+ };
3082
+ let state = [(0, import_starknet.poseidonHashMany)([...message, ...salt]), 0n, 0n];
3083
+ for (let round = 0; round < 21; round++) {
3084
+ state = (0, import_starknet.poseidonSmall)(state);
3085
+ extract12(state[0]);
3086
+ extract12(state[1]);
3087
+ }
3088
+ state = (0, import_starknet.poseidonSmall)(state);
3089
+ extract12(state[0]);
3090
+ return out;
3091
+ }
3092
+
3093
+ // src/falcon/s2morrow/signature.ts
3094
+ var defaultDeps = { engine: nobleEngine, randomBytes: import_utils6.randomBytes };
3095
+ function computeMulHint(s1, pkNtt) {
3096
+ const s1Ntt = ntt(s1);
3097
+ return intt(s1Ntt.map((v, i) => v * pkNtt[i] % Q));
3098
+ }
3099
+ var centeredSquare = (x) => {
3100
+ const c = x <= (Q - 1) / 2 ? x : Q - x;
3101
+ return c * c;
3102
+ };
3103
+ function checkSignatureEquation(hashPoint, s1, mulHint, pkNtt) {
3104
+ if (hashPoint.length !== N || s1.length !== N || mulHint.length !== N || pkNtt.length !== N) {
3105
+ return false;
3106
+ }
3107
+ const s1Ntt = ntt(s1);
3108
+ const hintNtt = ntt(mulHint);
3109
+ for (let i = 0; i < N; i += 1) {
3110
+ if (s1Ntt[i] * pkNtt[i] % Q !== hintNtt[i]) return false;
3111
+ }
3112
+ let norm = 0;
3113
+ for (let i = 0; i < N; i += 1) {
3114
+ norm += centeredSquare(modQ(hashPoint[i] - mulHint[i])) + centeredSquare(s1[i]);
3115
+ }
3116
+ return norm <= SIG_BOUND_512;
3117
+ }
3118
+ function formatSignature(sig) {
3119
+ const felts = [
3120
+ ...packPoly(sig.s1),
3121
+ BigInt(sig.salt.length),
3122
+ ...sig.salt,
3123
+ ...packPoly(sig.mulHint)
3124
+ ];
3125
+ return felts.map((x) => starknet5.num.toHex(x));
3126
+ }
3127
+ function parseSignature(signature) {
3128
+ if (!Array.isArray(signature) || signature.length !== SIGNATURE_FELTS) {
3129
+ throw new Error(`Falcon512: signature must have ${SIGNATURE_FELTS} felts`);
3130
+ }
3131
+ const felts = signature.map((x, i) => parseFelt(x, `signature[${i}]`));
3132
+ if (felts[PACKED_SLOTS] !== BigInt(SALT_FELTS)) {
3133
+ throw new Error("Falcon512: unexpected salt length");
3134
+ }
3135
+ const saltStart = PACKED_SLOTS + 1;
3136
+ return {
3137
+ s1: unpackPoly(felts.slice(0, PACKED_SLOTS)),
3138
+ salt: felts.slice(saltStart, saltStart + SALT_FELTS),
3139
+ mulHint: unpackPoly(felts.slice(saltStart + SALT_FELTS))
3140
+ };
3141
+ }
3142
+ var randomSaltFelt = (randomBytes2) => BigInt(`0x${starknet5.encode.buf2hex(randomBytes2(31))}`);
3143
+ function signHash(msgHash, secretKey, pkNtt, deps = defaultDeps) {
3144
+ assertFelt(msgHash, "message hash");
3145
+ const salt = [randomSaltFelt(deps.randomBytes), randomSaltFelt(deps.randomBytes)];
3146
+ const hashPoint = hashToPoint([msgHash], salt);
3147
+ const s2 = deps.engine.signHashPoint(hashPoint, secretKey);
3148
+ const s1 = Uint16Array.from(s2, modQ);
3149
+ const mulHint = Uint16Array.from(computeMulHint(s1, pkNtt));
3150
+ const signature = formatSignature({ s1, salt, mulHint });
3151
+ const decoded = parseSignature(signature);
3152
+ if (!checkSignatureEquation(hashPoint, decoded.s1, decoded.mulHint, pkNtt)) {
3153
+ throw new Error("Falcon512: self-verification failed, signature discarded");
3154
+ }
3155
+ return signature;
3156
+ }
3157
+ function verifyHash(msgHash, signature, pkNtt) {
3158
+ let decoded;
3159
+ try {
3160
+ assertFelt(msgHash, "message hash");
3161
+ decoded = parseSignature(signature);
3162
+ } catch {
3163
+ return false;
3164
+ }
3165
+ return checkSignatureEquation(
3166
+ hashToPoint([msgHash], decoded.salt),
3167
+ decoded.s1,
3168
+ decoded.mulHint,
3169
+ pkNtt
3170
+ );
3171
+ }
3172
+
3173
+ // src/falcon/oz/hashToPoint.ts
3174
+ var import_sha33 = require("@noble/hashes/sha3.js");
3175
+ var SALT_BYTES = 20;
3176
+ var SALT_LIMIT = 1n << BigInt(8 * SALT_BYTES);
3177
+ var KQ = 5 * Q;
3178
+ var writeLittleEndian = (x, out, offset, length) => {
3179
+ let v = x;
3180
+ for (let i = 0; i < length; i += 1) {
3181
+ out[offset + i] = Number(v & 0xffn);
3182
+ v >>= 8n;
3183
+ }
3184
+ };
3185
+ function saltFeltsToBytes(saltA, saltB) {
3186
+ [saltA, saltB].forEach((salt) => {
3187
+ if (typeof salt !== "bigint" || salt < 0n || salt >= SALT_LIMIT) {
3188
+ throw new Error("Falcon512: salt felt must be non-negative and below 2^160");
3189
+ }
3190
+ });
3191
+ const out = new Uint8Array(2 * SALT_BYTES);
3192
+ writeLittleEndian(saltA, out, 0, SALT_BYTES);
3193
+ writeLittleEndian(saltB, out, SALT_BYTES, SALT_BYTES);
3194
+ return out;
3195
+ }
3196
+ function hashToPointShake(msgHash, saltA, saltB) {
3197
+ assertFelt(msgHash, "message hash");
3198
+ const message = new Uint8Array(32);
3199
+ writeLittleEndian(msgHash, message, 0, 32);
3200
+ const xof = import_sha33.shake256.create().update(saltFeltsToBytes(saltA, saltB)).update(message);
3201
+ const out = new Uint16Array(N);
3202
+ for (let i = 0; i < N; ) {
3203
+ const [high, low] = xof.xof(2);
3204
+ const word = high << 8 | low;
3205
+ if (word < KQ) {
3206
+ out[i] = word % Q;
3207
+ i += 1;
3208
+ }
3209
+ }
3210
+ return out;
3211
+ }
3212
+
3213
+ // src/falcon/oz/signature.ts
3214
+ var OZ_SIGNATURE_FELTS = Object.freeze({
3215
+ hint: 2 * PACKED_SLOTS + 2,
3216
+ direct: PACKED_SLOTS + 2
3217
+ });
3218
+ var defaultDeps2 = { engine: nobleEngine, randomBytes: import_utils7.randomBytes };
3219
+ var readLittleEndian = (bytes) => bytes.reduceRight((acc, byte) => acc * 256n + BigInt(byte), 0n);
3220
+ function formatOzSignature(sig, variant) {
3221
+ const felts = [...packPoly(sig.s1), sig.saltA, sig.saltB];
3222
+ if (variant === "hint") {
3223
+ if (!sig.mulHint) throw new Error("Falcon512: the hint layout needs the hint polynomial");
3224
+ felts.push(...packPoly(sig.mulHint));
3225
+ }
3226
+ return felts.map((x) => starknet6.num.toHex(x));
3227
+ }
3228
+ function parseOzSignature(signature, variant) {
3229
+ const length = OZ_SIGNATURE_FELTS[variant];
3230
+ if (!Array.isArray(signature) || signature.length !== length) {
3231
+ throw new Error(`Falcon512: signature must have ${length} felts`);
3232
+ }
3233
+ const felts = signature.map((x, i) => parseFelt(x, `signature[${i}]`));
3234
+ const saltA = felts[PACKED_SLOTS];
3235
+ const saltB = felts[PACKED_SLOTS + 1];
3236
+ saltFeltsToBytes(saltA, saltB);
3237
+ return {
3238
+ s1: unpackPoly(felts.slice(0, PACKED_SLOTS)),
3239
+ saltA,
3240
+ saltB,
3241
+ mulHint: variant === "hint" ? unpackPoly(felts.slice(PACKED_SLOTS + 2)) : void 0
3242
+ };
3243
+ }
3244
+ function signHashOz(msgHash, secretKey, pkNtt, variant, deps = defaultDeps2) {
3245
+ assertFelt(msgHash, "message hash");
3246
+ const salt = deps.randomBytes(2 * SALT_BYTES);
3247
+ const saltA = readLittleEndian(salt.subarray(0, SALT_BYTES));
3248
+ const saltB = readLittleEndian(salt.subarray(SALT_BYTES));
3249
+ const hashPoint = hashToPointShake(msgHash, saltA, saltB);
3250
+ const s1 = Uint16Array.from(deps.engine.signHashPoint(hashPoint, secretKey), modQ);
3251
+ const mulHint = variant === "hint" ? Uint16Array.from(computeMulHint(s1, pkNtt)) : void 0;
3252
+ const signature = formatOzSignature({ s1, saltA, saltB, mulHint }, variant);
3253
+ const decoded = parseOzSignature(signature, variant);
3254
+ const decodedHint = decoded.mulHint ?? computeMulHint(decoded.s1, pkNtt);
3255
+ if (!checkSignatureEquation(hashPoint, decoded.s1, decodedHint, pkNtt)) {
3256
+ throw new Error("Falcon512: self-verification failed, signature discarded");
3257
+ }
3258
+ return signature;
3259
+ }
3260
+ function verifyHashOz(msgHash, signature, pkNtt, variant) {
3261
+ let decoded;
3262
+ try {
3263
+ assertFelt(msgHash, "message hash");
3264
+ decoded = parseOzSignature(signature, variant);
3265
+ } catch {
3266
+ return false;
3267
+ }
3268
+ const mulHint = decoded.mulHint ?? computeMulHint(decoded.s1, pkNtt);
3269
+ return checkSignatureEquation(
3270
+ hashToPointShake(msgHash, decoded.saltA, decoded.saltB),
3271
+ decoded.s1,
3272
+ mulHint,
3273
+ pkNtt
3274
+ );
3275
+ }
3276
+
3018
3277
  // src/falcon/s2morrow/keys.ts
3278
+ var import_utils8 = require("@noble/hashes/utils.js");
3279
+ var starknet7 = __toESM(require("starknet"));
3019
3280
  var SECRET_KEY_LENGTH = 1281;
3020
3281
  var PUBLIC_KEY_LENGTH = 897;
3021
3282
  var SEED_LENGTH = 48;
@@ -3029,7 +3290,7 @@ var FG_MAX = 31;
3029
3290
  var CAPITAL_FG_MAX = 127;
3030
3291
  var FG_BYTES = N * FG_BITS / 8;
3031
3292
  function toBytes(input, expectedLength, label) {
3032
- if ((0, import_utils6.isBytes)(input)) {
3293
+ if ((0, import_utils8.isBytes)(input)) {
3033
3294
  if (input.length !== expectedLength) {
3034
3295
  throw new Error(`Falcon512: ${label} must be ${expectedLength} bytes, got ${input.length}`);
3035
3296
  }
@@ -3038,7 +3299,7 @@ function toBytes(input, expectedLength, label) {
3038
3299
  if (typeof input !== "string") {
3039
3300
  throw new Error(`Falcon512: ${label} must be a Uint8Array or a hex string`);
3040
3301
  }
3041
- const hex = starknet3.encode.removeHexPrefix(input);
3302
+ const hex = starknet7.encode.removeHexPrefix(input);
3042
3303
  if (!/^[0-9a-fA-F]*$/.test(hex)) {
3043
3304
  throw new Error(`Falcon512: ${label} is not a valid hex string`);
3044
3305
  }
@@ -3047,12 +3308,12 @@ function toBytes(input, expectedLength, label) {
3047
3308
  `Falcon512: ${label} must have ${2 * expectedLength} hex digits, got ${hex.length}`
3048
3309
  );
3049
3310
  }
3050
- return starknet3.num.hexToBytes(starknet3.encode.addHexPrefix(hex));
3311
+ return starknet7.num.hexToBytes(starknet7.encode.addHexPrefix(hex));
3051
3312
  }
3052
3313
  var wipe = (...arrays) => {
3053
3314
  arrays.forEach((a) => a.fill(0));
3054
3315
  };
3055
- var center = (x) => x > Q2 >> 1 ? x - Q2 : x;
3316
+ var center = (x) => x > Q >> 1 ? x - Q : x;
3056
3317
  var toSigned = (raw, bits) => raw << 32 - bits >> 32 - bits;
3057
3318
  function encodeBitsMSB(values, bits) {
3058
3319
  const out = new Uint8Array(values.length * bits / 8);
@@ -3118,7 +3379,7 @@ function decode14LSB(bytes) {
3118
3379
  const v = acc & 16383;
3119
3380
  acc >>>= 14;
3120
3381
  accBits -= 14;
3121
- if (v >= Q2) {
3382
+ if (v >= Q) {
3122
3383
  throw new Error("Falcon512: invalid falcon-rs secret key (coefficient out of range)");
3123
3384
  }
3124
3385
  out.push(v);
@@ -3154,7 +3415,7 @@ function checkNtruEquation({ f, g, F, G }) {
3154
3415
  const gF = mulNegacyclicInt(g, F);
3155
3416
  try {
3156
3417
  for (let i = 0; i < N; i++) {
3157
- if (fG[i] - gF[i] !== (i === 0 ? Q2 : 0)) {
3418
+ if (fG[i] - gF[i] !== (i === 0 ? Q : 0)) {
3158
3419
  throw new Error("Falcon512: invalid secret key (NTRU equation not satisfied)");
3159
3420
  }
3160
3421
  }
@@ -3227,7 +3488,7 @@ function decodePublicKey(publicKey) {
3227
3488
  const bytes = toBytes(publicKey, PUBLIC_KEY_LENGTH, "public key");
3228
3489
  if (bytes[0] !== PUBLIC_KEY_HEADER) throw new Error("Falcon512: invalid public key header");
3229
3490
  const h = decodeBitsMSB(bytes.subarray(1), N, 14);
3230
- if (h.some((v) => v >= Q2)) {
3491
+ if (h.some((v) => v >= Q)) {
3231
3492
  throw new Error("Falcon512: invalid public key (coefficient out of range)");
3232
3493
  }
3233
3494
  return Uint16Array.from(h);
@@ -3237,7 +3498,7 @@ function encodePublicKey(h) {
3237
3498
  throw new Error(`Falcon512: public key polynomial must have ${N} coefficients`);
3238
3499
  }
3239
3500
  for (let i = 0; i < N; i++) {
3240
- if (!Number.isInteger(h[i]) || h[i] < 0 || h[i] >= Q2) {
3501
+ if (!Number.isInteger(h[i]) || h[i] < 0 || h[i] >= Q) {
3241
3502
  throw new Error("Falcon512: invalid public key (coefficient out of range)");
3242
3503
  }
3243
3504
  }
@@ -3247,7 +3508,7 @@ function encodePublicKey(h) {
3247
3508
  return out;
3248
3509
  }
3249
3510
  function generateKeyPair(seed) {
3250
- if (seed !== void 0 && (!(0, import_utils6.isBytes)(seed) || seed.length !== SEED_LENGTH)) {
3511
+ if (seed !== void 0 && (!(0, import_utils8.isBytes)(seed) || seed.length !== SEED_LENGTH)) {
3251
3512
  throw new Error(`Falcon512: seed must be a Uint8Array of ${SEED_LENGTH} bytes`);
3252
3513
  }
3253
3514
  return nobleEngine.keygen(seed);
@@ -3264,7 +3525,7 @@ function publicKeyToNtt(publicKey) {
3264
3525
  return Uint16Array.from(ntt(decodePublicKey(publicKey)));
3265
3526
  }
3266
3527
  function getPackedPublicKey(publicKey) {
3267
- return packPoly(publicKeyToNtt(publicKey)).map((x) => starknet3.num.toHex(x));
3528
+ return packPoly(publicKeyToNtt(publicKey)).map((x) => starknet7.num.toHex(x));
3268
3529
  }
3269
3530
  function importFalconRsSecretKey(secretKey) {
3270
3531
  const bytes = toBytes(secretKey, FALCON_RS_SECRET_KEY_LENGTH, "falcon-rs secret key");
@@ -3310,144 +3571,35 @@ function exportFalconRsSecretKey(secretKey) {
3310
3571
  }
3311
3572
  }
3312
3573
 
3313
- // src/falcon/s2morrow/signature.ts
3314
- var import_utils7 = require("@noble/hashes/utils.js");
3315
- var starknet5 = __toESM(require("starknet"));
3316
-
3317
- // src/falcon/s2morrow/hashToPoint.ts
3318
- var import_starknet = require("@scure/starknet");
3319
- var starknet4 = __toESM(require("starknet"));
3320
- function hashToPoint(message, salt) {
3321
- message.forEach((m) => assertFelt(m, "message element"));
3322
- salt.forEach((s) => assertFelt(s, "salt element"));
3323
- const out = new Uint16Array(N);
3324
- let idx = 0;
3325
- const extract6 = (value) => {
3326
- let v = value;
3327
- for (let i = 0; i < 6 && idx < N; i++) {
3328
- out[idx++] = Number(v % QB);
3329
- v /= QB;
3330
- }
3331
- };
3332
- const extract12 = (felt) => {
3333
- extract6(felt & starknet4.UINT_128_MAX);
3334
- extract6(felt >> 128n);
3335
- };
3336
- let state = [(0, import_starknet.poseidonHashMany)([...message, ...salt]), 0n, 0n];
3337
- for (let round = 0; round < 21; round++) {
3338
- state = (0, import_starknet.poseidonSmall)(state);
3339
- extract12(state[0]);
3340
- extract12(state[1]);
3341
- }
3342
- state = (0, import_starknet.poseidonSmall)(state);
3343
- extract12(state[0]);
3344
- return out;
3345
- }
3346
-
3347
- // src/falcon/s2morrow/signature.ts
3348
- var defaultDeps = { engine: nobleEngine, randomBytes: import_utils7.randomBytes };
3349
- function computeMulHint(s1, pkNtt) {
3350
- const s1Ntt = ntt(s1);
3351
- return intt(s1Ntt.map((v, i) => v * pkNtt[i] % Q2));
3352
- }
3353
- var centeredSquare = (x) => {
3354
- const c = x <= (Q2 - 1) / 2 ? x : Q2 - x;
3355
- return c * c;
3356
- };
3357
- function checkSignatureEquation(hashPoint, s1, mulHint, pkNtt) {
3358
- if (hashPoint.length !== N || s1.length !== N || mulHint.length !== N || pkNtt.length !== N) {
3359
- return false;
3360
- }
3361
- const s1Ntt = ntt(s1);
3362
- const hintNtt = ntt(mulHint);
3363
- for (let i = 0; i < N; i += 1) {
3364
- if (s1Ntt[i] * pkNtt[i] % Q2 !== hintNtt[i]) return false;
3365
- }
3366
- let norm = 0;
3367
- for (let i = 0; i < N; i += 1) {
3368
- norm += centeredSquare(modQ(hashPoint[i] - mulHint[i])) + centeredSquare(s1[i]);
3369
- }
3370
- return norm <= SIG_BOUND_512;
3371
- }
3372
- function formatSignature(sig) {
3373
- const felts = [
3374
- ...packPoly(sig.s1),
3375
- BigInt(sig.salt.length),
3376
- ...sig.salt,
3377
- ...packPoly(sig.mulHint)
3378
- ];
3379
- return felts.map((x) => starknet5.num.toHex(x));
3380
- }
3381
- function parseSignature(signature) {
3382
- if (!Array.isArray(signature) || signature.length !== SIGNATURE_FELTS) {
3383
- throw new Error(`Falcon512: signature must have ${SIGNATURE_FELTS} felts`);
3384
- }
3385
- const felts = signature.map((x, i) => parseFelt(x, `signature[${i}]`));
3386
- if (felts[PACKED_SLOTS] !== BigInt(SALT_FELTS)) {
3387
- throw new Error("Falcon512: unexpected salt length");
3388
- }
3389
- const saltStart = PACKED_SLOTS + 1;
3390
- return {
3391
- s1: unpackPoly(felts.slice(0, PACKED_SLOTS)),
3392
- salt: felts.slice(saltStart, saltStart + SALT_FELTS),
3393
- mulHint: unpackPoly(felts.slice(saltStart + SALT_FELTS))
3394
- };
3395
- }
3396
- var randomSaltFelt = (randomBytes2) => BigInt(`0x${starknet5.encode.buf2hex(randomBytes2(31))}`);
3397
- function signHash(msgHash, secretKey, pkNtt, deps = defaultDeps) {
3398
- assertFelt(msgHash, "message hash");
3399
- const salt = [randomSaltFelt(deps.randomBytes), randomSaltFelt(deps.randomBytes)];
3400
- const hashPoint = hashToPoint([msgHash], salt);
3401
- const s2 = deps.engine.signHashPoint(hashPoint, secretKey);
3402
- const s1 = Uint16Array.from(s2, modQ);
3403
- const mulHint = Uint16Array.from(computeMulHint(s1, pkNtt));
3404
- const signature = formatSignature({ s1, salt, mulHint });
3405
- const decoded = parseSignature(signature);
3406
- if (!checkSignatureEquation(hashPoint, decoded.s1, decoded.mulHint, pkNtt)) {
3407
- throw new Error("Falcon512: self-verification failed, signature discarded");
3408
- }
3409
- return signature;
3410
- }
3411
- function verifyHash(msgHash, signature, pkNtt) {
3412
- let decoded;
3413
- try {
3414
- assertFelt(msgHash, "message hash");
3415
- decoded = parseSignature(signature);
3416
- } catch {
3417
- return false;
3418
- }
3419
- return checkSignatureEquation(
3420
- hashToPoint([msgHash], decoded.salt),
3421
- decoded.s1,
3422
- decoded.mulHint,
3423
- pkNtt
3424
- );
3425
- }
3426
-
3427
3574
  // src/falcon/Falcon512Signer.ts
3428
3575
  function assertV3(details) {
3429
- if (!starknet6.isV3Tx(details)) throw new Error("Falcon512: only V3 transactions are supported");
3576
+ if (!starknet8.isV3Tx(details)) throw new Error("Falcon512: only V3 transactions are supported");
3430
3577
  }
3431
3578
  var Falcon512Signer = class _Falcon512Signer {
3432
3579
  #secretKey;
3433
3580
  #publicKey;
3434
3581
  #pkNtt;
3435
3582
  #packedPublicKey;
3583
+ #accountType;
3436
3584
  /**
3437
3585
  * Builds a signer from a NIST Falcon-512 secret key. The key is fully checked, then copied: the
3438
3586
  * signer keeps its own copy, in a private field.
3439
3587
  * @param {Uint8Array | string} secretKey NIST Falcon-512 secret key (1281 bytes), Uint8Array or
3440
3588
  * hex string. Prefer a Uint8Array: a string cannot be wiped from memory.
3441
- * @throws {Error} when the secret key is invalid
3589
+ * @param {Falcon512Options} [options] `accountType`: the type of the account that checks the
3590
+ * signatures, `'s2morrow'` (default), `'oz-hint'` or `'oz-direct'`
3591
+ * @throws {Error} when the secret key or an option is invalid
3442
3592
  * @example
3443
3593
  * ```typescript
3444
3594
  * const { secretKey } = falcon512.generateKeyPair();
3445
- * const mySigner = new Falcon512Signer(secretKey);
3595
+ * const mySigner = new Falcon512Signer(secretKey); // for an S2morrow account
3596
+ * const myOzSigner = new Falcon512Signer(secretKey, { accountType: 'oz-hint' });
3446
3597
  * new Falcon512Signer(new Uint8Array(1281));
3447
3598
  * // throws Error("Falcon512: invalid secret key header")
3448
3599
  * ```
3449
3600
  */
3450
- constructor(secretKey) {
3601
+ constructor(secretKey, options) {
3602
+ const accountType = parseFalconOptions(options);
3451
3603
  const bytes = toBytes(secretKey, SECRET_KEY_LENGTH, "secret key");
3452
3604
  let publicKey;
3453
3605
  try {
@@ -3456,6 +3608,7 @@ var Falcon512Signer = class _Falcon512Signer {
3456
3608
  bytes.fill(0);
3457
3609
  throw error;
3458
3610
  }
3611
+ this.#accountType = accountType;
3459
3612
  this.#publicKey = publicKey;
3460
3613
  this.#secretKey = bytes;
3461
3614
  this.#pkNtt = publicKeyToNtt(this.#publicKey);
@@ -3467,10 +3620,12 @@ var Falcon512Signer = class _Falcon512Signer {
3467
3620
  * Warning: falcon-rs derives the seed of its sampler from the public salt of each signature, so
3468
3621
  * the signatures it produced may leak information about the secret key. Use this for continuity
3469
3622
  * only; for real funds, generate a new key with `falcon512.generateKeyPair`. The S2morrow account
3470
- * cannot change its key: a new key means a new account.
3623
+ * cannot change its key: a new key means a new account. An OpenZeppelin account can change its
3624
+ * key (see `signOwnershipAcceptance`).
3471
3625
  * @param {Uint8Array | string} secretKey the falcon-rs secret key (3584 bytes), or its hex string
3626
+ * @param {Falcon512Options} [options] `accountType`, as for the constructor
3472
3627
  * @returns {Falcon512Signer} a signer that holds the same key, in the NIST format
3473
- * @throws {Error} when the falcon-rs secret key is invalid
3628
+ * @throws {Error} when the falcon-rs secret key or an option is invalid
3474
3629
  * @example
3475
3630
  * ```typescript
3476
3631
  * const mySigner = Falcon512Signer.fromFalconRs(falconRsSecretKey); // 3584 bytes, from falcon-rs
@@ -3478,10 +3633,10 @@ var Falcon512Signer = class _Falcon512Signer {
3478
3633
  * // result = ['0xb2cf4f8b3c6f60cc662f438970ad34017e260c52d7911297a42811ce3cea2b', …] (29 felts)
3479
3634
  * ```
3480
3635
  */
3481
- static fromFalconRs(secretKey) {
3636
+ static fromFalconRs(secretKey, options) {
3482
3637
  const nist = importFalconRsSecretKey(secretKey);
3483
3638
  try {
3484
- return new _Falcon512Signer(nist);
3639
+ return new _Falcon512Signer(nist, options);
3485
3640
  } finally {
3486
3641
  nist.fill(0);
3487
3642
  }
@@ -3498,10 +3653,12 @@ var Falcon512Signer = class _Falcon512Signer {
3498
3653
  * ```
3499
3654
  */
3500
3655
  async getPubKey() {
3501
- return starknet6.encode.addHexPrefix(starknet6.encode.buf2hex(this.#publicKey));
3656
+ return starknet8.encode.addHexPrefix(starknet8.encode.buf2hex(this.#publicKey));
3502
3657
  }
3503
3658
  /**
3504
- * The 29 felts of the account constructor argument `pk_packed`.
3659
+ * The 29 felts of the public key, in the form the accounts store it: the constructor argument
3660
+ * `pk_packed` of S2morrow, or the `public_key` array of an OpenZeppelin account (whose
3661
+ * constructor calldata is `29`, then these 29 felts). The same for the three account types.
3505
3662
  * @returns {string[]} the 29 felts, as hex strings (a new array at each call)
3506
3663
  * @example
3507
3664
  * ```typescript
@@ -3518,21 +3675,23 @@ var Falcon512Signer = class _Falcon512Signer {
3518
3675
  * Signs a SNIP-12 message (TypedData) for off-chain use.
3519
3676
  * @param {TypedData} typedData the message to sign
3520
3677
  * @param {string} accountAddress address of the account that signs the message
3521
- * @returns {Promise<Signature>} the 61 felts of the signature, as hex strings
3678
+ * @returns {Promise<Signature>} the signature, as hex strings: 61 felts (S2morrow), 60
3679
+ * (OpenZeppelin, hint) or 31 (OpenZeppelin, direct)
3522
3680
  * @example
3523
3681
  * ```typescript
3524
3682
  * const result = await mySigner.signMessage(myTypedData, accountAddress);
3525
- * // result = ['0x…', …] (61 felts, random salt); result[29] = '0x2' (salt length)
3683
+ * // result = ['0x…', …] (S2morrow: 61 felts, random salt); result[29] = '0x2' (salt length)
3526
3684
  * ```
3527
3685
  */
3528
3686
  async signMessage(typedData3, accountAddress) {
3529
- return this.signRaw(starknet6.typedData.getMessageHash(typedData3, accountAddress));
3687
+ return this.signRaw(starknet8.typedData.getMessageHash(typedData3, accountAddress));
3530
3688
  }
3531
3689
  /**
3532
3690
  * Signs an INVOKE transaction (V3 only).
3533
3691
  * @param {Call[]} transactions the calls of the transaction
3534
3692
  * @param {InvocationsSignerDetails} details the details needed to hash the transaction
3535
- * @returns {Promise<Signature>} the 61 felts of the signature, as hex strings
3693
+ * @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
3694
+ * the account type
3536
3695
  * @throws {Error} when the transaction is not V3
3537
3696
  * @example
3538
3697
  * ```typescript
@@ -3561,29 +3720,30 @@ var Falcon512Signer = class _Falcon512Signer {
3561
3720
  * feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1,
3562
3721
  * };
3563
3722
  * const result = await mySigner.signTransaction(calls, details);
3564
- * // result = ['0x…', …] (61 felts)
3723
+ * // result = ['0x…', …] (S2morrow: 61 felts)
3565
3724
  * ```
3566
3725
  */
3567
3726
  async signTransaction(transactions, details) {
3568
3727
  assertV3(details);
3569
- const compiledCalldata = starknet6.transaction.getExecuteCalldata(
3728
+ const compiledCalldata = starknet8.transaction.getExecuteCalldata(
3570
3729
  transactions,
3571
3730
  details.cairoVersion
3572
3731
  );
3573
- const msgHash = starknet6.hash.calculateInvokeTransactionHash({
3732
+ const msgHash = starknet8.hash.calculateInvokeTransactionHash({
3574
3733
  ...details,
3575
3734
  senderAddress: details.walletAddress,
3576
3735
  compiledCalldata,
3577
3736
  version: details.version,
3578
- nonceDataAvailabilityMode: starknet6.stark.intDAM(details.nonceDataAvailabilityMode),
3579
- feeDataAvailabilityMode: starknet6.stark.intDAM(details.feeDataAvailabilityMode)
3737
+ nonceDataAvailabilityMode: starknet8.stark.intDAM(details.nonceDataAvailabilityMode),
3738
+ feeDataAvailabilityMode: starknet8.stark.intDAM(details.feeDataAvailabilityMode)
3580
3739
  });
3581
3740
  return this.signRaw(msgHash);
3582
3741
  }
3583
3742
  /**
3584
3743
  * Signs the DEPLOY_ACCOUNT transaction of a Falcon-512 account (V3 only).
3585
3744
  * @param {DeployAccountSignerDetails} details the details needed to hash the transaction
3586
- * @returns {Promise<Signature>} the 61 felts of the signature, as hex strings
3745
+ * @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
3746
+ * the account type
3587
3747
  * @throws {Error} when the transaction is not V3
3588
3748
  * @example
3589
3749
  * ```typescript
@@ -3610,26 +3770,27 @@ var Falcon512Signer = class _Falcon512Signer {
3610
3770
  * feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1,
3611
3771
  * };
3612
3772
  * const result = await mySigner.signDeployAccountTransaction(details);
3613
- * // result = ['0x…', …] (61 felts)
3773
+ * // result = ['0x…', …] (S2morrow: 61 felts)
3614
3774
  * ```
3615
3775
  */
3616
3776
  async signDeployAccountTransaction(details) {
3617
3777
  assertV3(details);
3618
- const compiledConstructorCalldata = starknet6.CallData.compile(details.constructorCalldata);
3619
- const msgHash = starknet6.hash.calculateDeployAccountTransactionHash({
3778
+ const compiledConstructorCalldata = starknet8.CallData.compile(details.constructorCalldata);
3779
+ const msgHash = starknet8.hash.calculateDeployAccountTransactionHash({
3620
3780
  ...details,
3621
3781
  salt: details.addressSalt,
3622
3782
  compiledConstructorCalldata,
3623
3783
  version: details.version,
3624
- nonceDataAvailabilityMode: starknet6.stark.intDAM(details.nonceDataAvailabilityMode),
3625
- feeDataAvailabilityMode: starknet6.stark.intDAM(details.feeDataAvailabilityMode)
3784
+ nonceDataAvailabilityMode: starknet8.stark.intDAM(details.nonceDataAvailabilityMode),
3785
+ feeDataAvailabilityMode: starknet8.stark.intDAM(details.feeDataAvailabilityMode)
3626
3786
  });
3627
3787
  return this.signRaw(msgHash);
3628
3788
  }
3629
3789
  /**
3630
3790
  * Signs a DECLARE transaction (V3 only).
3631
3791
  * @param {DeclareSignerDetails} details the details needed to hash the transaction
3632
- * @returns {Promise<Signature>} the 61 felts of the signature, as hex strings
3792
+ * @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
3793
+ * the account type
3633
3794
  * @throws {Error} when the transaction is not V3
3634
3795
  * @example
3635
3796
  * ```typescript
@@ -3652,24 +3813,75 @@ var Falcon512Signer = class _Falcon512Signer {
3652
3813
  * feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1,
3653
3814
  * };
3654
3815
  * const result = await mySigner.signDeclareTransaction(details);
3655
- * // result = ['0x…', …] (61 felts)
3816
+ * // result = ['0x…', …] (S2morrow: 61 felts)
3656
3817
  * ```
3657
3818
  */
3658
3819
  async signDeclareTransaction(details) {
3659
3820
  assertV3(details);
3660
- const msgHash = starknet6.hash.calculateDeclareTransactionHash({
3821
+ const msgHash = starknet8.hash.calculateDeclareTransactionHash({
3661
3822
  ...details,
3662
3823
  version: details.version,
3663
- nonceDataAvailabilityMode: starknet6.stark.intDAM(details.nonceDataAvailabilityMode),
3664
- feeDataAvailabilityMode: starknet6.stark.intDAM(details.feeDataAvailabilityMode)
3824
+ nonceDataAvailabilityMode: starknet8.stark.intDAM(details.nonceDataAvailabilityMode),
3825
+ feeDataAvailabilityMode: starknet8.stark.intDAM(details.feeDataAvailabilityMode)
3665
3826
  });
3666
3827
  return this.signRaw(msgHash);
3667
3828
  }
3668
3829
  /**
3669
- * Signs a hash: the last step of all the methods above. It stays protected, so that no public
3670
- * method signs an arbitrary hash. A subclass can override it, as for every starknet.js signer.
3830
+ * Signs the acceptance of a key change of an OpenZeppelin Falcon-512 account.
3831
+ *
3832
+ * The account changes its key when it calls its own `set_public_key(new_public_key, signature)`:
3833
+ * the current key signs that transaction, as any other, and the new key must sign an acceptance.
3834
+ * Call this method on the signer of the new key, with the key that the account holds now. The
3835
+ * signed hash is Poseidon('StarkNet Message', 'accept_ownership', account address,
3836
+ * Poseidon(current key)). It holds no chain id, nonce or expiry: an acceptance is not single-use,
3837
+ * and the current key must still sign each key change.
3838
+ * @param {BigNumberish} accountAddress the address of the account
3839
+ * @param {BigNumberish[]} currentPublicKey the 29 felts of the key that the account holds now
3840
+ * (`getPackedPublicKey()` of its current signer, or its `get_public_key`)
3841
+ * @returns {Promise<string[]>} the acceptance, as hex strings, in the layout of this signer: 60
3842
+ * felts (`'oz-hint'`) or 31 (`'oz-direct'`)
3843
+ * @throws {Error} for an S2morrow signer (that account cannot change its key), or when the
3844
+ * current key has neither 29 felts nor none, or a value is not a felt252. Also when a subclass
3845
+ * makes `signRaw` return something else than an array of felts
3846
+ * @example
3847
+ * ```typescript
3848
+ * const newSigner = new Falcon512Signer(newSecretKey, { accountType: 'oz-hint' });
3849
+ * const acceptance = await newSigner.signOwnershipAcceptance(
3850
+ * falconAccount.address,
3851
+ * currentSigner.getPackedPublicKey()
3852
+ * );
3853
+ * const { transaction_hash } = await falconAccount.execute(
3854
+ * {
3855
+ * contractAddress: falconAccount.address,
3856
+ * entrypoint: 'set_public_key',
3857
+ * calldata: CallData.compile({
3858
+ * new_public_key: newSigner.getPackedPublicKey(),
3859
+ * signature: acceptance,
3860
+ * }),
3861
+ * },
3862
+ * { skipValidate: false }
3863
+ * );
3864
+ * await myProvider.waitForTransaction(transaction_hash);
3865
+ * // From now on, the account checks the signatures of newSigner.
3866
+ * ```
3867
+ */
3868
+ async signOwnershipAcceptance(accountAddress, currentPublicKey) {
3869
+ if (this.#accountType === "s2morrow") {
3870
+ throw new Error("Falcon512: an S2morrow account cannot change its key");
3871
+ }
3872
+ const signature = await this.signRaw(ownershipAcceptanceHash(accountAddress, currentPublicKey));
3873
+ if (!Array.isArray(signature)) {
3874
+ throw new Error("Falcon512: signRaw must return an array of felts");
3875
+ }
3876
+ return signature;
3877
+ }
3878
+ /**
3879
+ * Signs a hash: the last step of all the methods above, in the layout of the account type. It
3880
+ * stays protected, so that no public method signs an arbitrary hash. A subclass can override it,
3881
+ * as for every starknet.js signer.
3671
3882
  * @param {string} msgHash the hash to sign, a felt252
3672
- * @returns {Promise<Signature>} the 61 felts of the signature, as hex strings
3883
+ * @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
3884
+ * the account type
3673
3885
  * @example
3674
3886
  * ```typescript
3675
3887
  * class LoggedFalconSigner extends Falcon512Signer {
@@ -3683,7 +3895,15 @@ var Falcon512Signer = class _Falcon512Signer {
3683
3895
  * ```
3684
3896
  */
3685
3897
  async signRaw(msgHash) {
3686
- return signHash(starknet6.num.toBigInt(msgHash), this.#secretKey, this.#pkNtt);
3898
+ const value = starknet8.num.toBigInt(msgHash);
3899
+ switch (this.#accountType) {
3900
+ case "oz-hint":
3901
+ return signHashOz(value, this.#secretKey, this.#pkNtt, "hint");
3902
+ case "oz-direct":
3903
+ return signHashOz(value, this.#secretKey, this.#pkNtt, "direct");
3904
+ default:
3905
+ return signHash(value, this.#secretKey, this.#pkNtt);
3906
+ }
3687
3907
  }
3688
3908
  };
3689
3909
 
@@ -3700,7 +3920,7 @@ __export(falcon512_exports, {
3700
3920
  });
3701
3921
 
3702
3922
  // src/falcon/verify.ts
3703
- var starknet7 = __toESM(require("starknet"));
3923
+ var starknet9 = __toESM(require("starknet"));
3704
3924
  function toMessageHash(value) {
3705
3925
  try {
3706
3926
  return parseFelt(value, "message hash");
@@ -3708,24 +3928,44 @@ function toMessageHash(value) {
3708
3928
  return void 0;
3709
3929
  }
3710
3930
  }
3711
- function verify(signature, msgHash, fullPublicKey) {
3712
- const pkNtt = publicKeyToNtt(fullPublicKey);
3931
+ function toPkNtt(fullPublicKey) {
3932
+ if (Array.isArray(fullPublicKey)) {
3933
+ if (fullPublicKey.length !== PACKED_SLOTS) {
3934
+ throw new Error(`Falcon512: packed public key must have ${PACKED_SLOTS} felts`);
3935
+ }
3936
+ return unpackPoly(fullPublicKey.map((x, i) => parseFelt(x, `public key[${i}]`)));
3937
+ }
3938
+ return publicKeyToNtt(fullPublicKey);
3939
+ }
3940
+ function verify(signature, msgHash, fullPublicKey, options) {
3941
+ const accountType = parseFalconOptions(options);
3942
+ const pkNtt = toPkNtt(fullPublicKey);
3713
3943
  const h = toMessageHash(msgHash);
3714
3944
  if (h === void 0 || !Array.isArray(signature)) return false;
3715
- return verifyHash(h, signature, pkNtt);
3945
+ switch (accountType) {
3946
+ case "oz-hint":
3947
+ return verifyHashOz(h, signature, pkNtt, "hint");
3948
+ case "oz-direct":
3949
+ return verifyHashOz(h, signature, pkNtt, "direct");
3950
+ default:
3951
+ return verifyHash(h, signature, pkNtt);
3952
+ }
3716
3953
  }
3717
- function verifyMessage(message, signature, fullPublicKey, accountAddress) {
3954
+ function verifyMessage(message, signature, fullPublicKey, accountAddressOrOptions, options) {
3718
3955
  if (typeof message === "object" && message !== null) {
3719
- if (accountAddress === void 0) {
3956
+ if (accountAddressOrOptions === void 0) {
3720
3957
  throw new Error("Falcon512: accountAddress is required to verify a TypedData message");
3721
3958
  }
3959
+ const accountAddress = accountAddressOrOptions;
3722
3960
  return verify(
3723
3961
  signature,
3724
- starknet7.typedData.getMessageHash(message, accountAddress),
3725
- fullPublicKey
3962
+ starknet9.typedData.getMessageHash(message, accountAddress),
3963
+ fullPublicKey,
3964
+ options
3726
3965
  );
3727
3966
  }
3728
- return verify(signature, message, fullPublicKey);
3967
+ const hashOptions = typeof accountAddressOrOptions === "object" && accountAddressOrOptions !== null && !Array.isArray(accountAddressOrOptions) ? accountAddressOrOptions : void 0;
3968
+ return verify(signature, message, fullPublicKey, hashOptions);
3729
3969
  }
3730
3970
  // Annotate the CommonJS export names for ESM import in node:
3731
3971
  0 && (module.exports = {