starknet 11.7.1 → 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.
- package/CHANGELOG.md +12 -0
- package/dist/falcon/index.d.ts +139 -39
- package/dist/falcon/index.js +464 -224
- package/dist/falcon/index.js.map +1 -1
- package/dist/falcon/index.mjs +462 -222
- package/dist/falcon/index.mjs.map +1 -1
- package/dist/index.d.ts +10 -3
- package/dist/index.global.js +21 -17
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +21 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +21 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/falcon/index.mjs
CHANGED
|
@@ -5,11 +5,77 @@ var __export = (target, all) => {
|
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
// src/falcon/Falcon512Signer.ts
|
|
8
|
-
import * as
|
|
8
|
+
import * as starknet8 from "starknet";
|
|
9
9
|
|
|
10
|
-
// src/falcon/
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
// src/falcon/accountType.ts
|
|
11
|
+
var ACCOUNT_TYPES = Object.freeze([
|
|
12
|
+
"s2morrow",
|
|
13
|
+
"oz-hint",
|
|
14
|
+
"oz-direct"
|
|
15
|
+
]);
|
|
16
|
+
function parseFalconOptions(options) {
|
|
17
|
+
if (options === void 0) return "s2morrow";
|
|
18
|
+
if (typeof options !== "object" || options === null || Array.isArray(options)) {
|
|
19
|
+
throw new Error("Falcon512: options must be an object");
|
|
20
|
+
}
|
|
21
|
+
Object.keys(options).forEach((key) => {
|
|
22
|
+
if (key !== "accountType") throw new Error(`Falcon512: unknown option "${key}"`);
|
|
23
|
+
});
|
|
24
|
+
const { accountType } = options;
|
|
25
|
+
if (accountType === void 0) return "s2morrow";
|
|
26
|
+
if (!ACCOUNT_TYPES.includes(accountType)) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Falcon512: unknown accountType "${String(accountType)}" (expected 's2morrow', 'oz-hint' or 'oz-direct')`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return accountType;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/falcon/oz/ownership.ts
|
|
35
|
+
import * as starknet2 from "starknet";
|
|
36
|
+
|
|
37
|
+
// src/falcon/s2morrow/constants.ts
|
|
38
|
+
var Q = 12289;
|
|
39
|
+
var QB = 12289n;
|
|
40
|
+
var N = 512;
|
|
41
|
+
var SIG_BOUND_512 = 34034726;
|
|
42
|
+
var PACKED_SLOTS = 29;
|
|
43
|
+
var VALUES_PER_HALF = 9;
|
|
44
|
+
var SALT_FELTS = 2;
|
|
45
|
+
var SIGNATURE_FELTS = 61;
|
|
46
|
+
|
|
47
|
+
// src/falcon/s2morrow/felt.ts
|
|
48
|
+
import * as starknet from "starknet";
|
|
49
|
+
function assertFelt(x, label) {
|
|
50
|
+
if (typeof x !== "bigint") throw new Error(`Falcon512: ${label} is not a felt252`);
|
|
51
|
+
starknet.CairoFelt252.assertRange(x);
|
|
52
|
+
}
|
|
53
|
+
var FELT_STRING = /^(0[xX][0-9a-fA-F]{1,64}|[0-9]{1,76})$/;
|
|
54
|
+
function parseFelt(value, label) {
|
|
55
|
+
const accepted = typeof value === "bigint" && value >= 0n || typeof value === "number" && Number.isSafeInteger(value) && value >= 0 || typeof value === "string" && FELT_STRING.test(value);
|
|
56
|
+
if (!accepted) throw new Error(`Falcon512: ${label} is not a felt252`);
|
|
57
|
+
return new starknet.CairoFelt252(value).toBigInt();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/falcon/oz/ownership.ts
|
|
61
|
+
function ownershipAcceptanceHash(accountAddress, currentPublicKey) {
|
|
62
|
+
if (!Array.isArray(currentPublicKey) || currentPublicKey.length !== PACKED_SLOTS && currentPublicKey.length !== 0) {
|
|
63
|
+
throw new Error("Falcon512: current public key must have 29 felts, or none");
|
|
64
|
+
}
|
|
65
|
+
const address = parseFelt(accountAddress, "account address");
|
|
66
|
+
const key = currentPublicKey.map((x, i) => parseFelt(x, `current public key[${i}]`));
|
|
67
|
+
const { computePoseidonHashOnElements } = starknet2.hash;
|
|
68
|
+
return computePoseidonHashOnElements([
|
|
69
|
+
starknet2.CairoBytes31.fromText("StarkNet Message").toBigInt(),
|
|
70
|
+
starknet2.CairoBytes31.fromText("accept_ownership").toBigInt(),
|
|
71
|
+
address,
|
|
72
|
+
computePoseidonHashOnElements(key)
|
|
73
|
+
]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/falcon/oz/signature.ts
|
|
77
|
+
import { randomBytes as cryptoRandomBytes2 } from "@noble/hashes/utils.js";
|
|
78
|
+
import * as starknet6 from "starknet";
|
|
13
79
|
|
|
14
80
|
// src/falcon/engine/noble/falcon.ts
|
|
15
81
|
import { rngAesCtrDrbg256 } from "@noble/ciphers/aes.js";
|
|
@@ -493,9 +559,9 @@ var Float = /* @__PURE__ */ Object.freeze({
|
|
|
493
559
|
var f64b = (n) => Float.decode(numberToHexUnpadded(n));
|
|
494
560
|
var EMPTY_CHACHA20_BLOCK = /* @__PURE__ */ new Uint8Array(64);
|
|
495
561
|
var NONCELEN = 40;
|
|
496
|
-
var
|
|
497
|
-
var Qhalf =
|
|
498
|
-
var QBig = BigInt(
|
|
562
|
+
var Q2 = 12289;
|
|
563
|
+
var Qhalf = Q2 >> 1;
|
|
564
|
+
var QBig = BigInt(Q2);
|
|
499
565
|
var _0n = /* @__PURE__ */ BigInt(0);
|
|
500
566
|
var _1n = /* @__PURE__ */ BigInt(1);
|
|
501
567
|
var _10n = /* @__PURE__ */ BigInt(10);
|
|
@@ -508,7 +574,7 @@ var MASK_32n = /* @__PURE__ */ BigInt("0xffffffff");
|
|
|
508
574
|
var MASK_63n = /* @__PURE__ */ BigInt("0x7fffffffffffffff");
|
|
509
575
|
var R2 = 10952;
|
|
510
576
|
var Q0I = 12287;
|
|
511
|
-
var F_INV_Q = 1 /
|
|
577
|
+
var F_INV_Q = 1 / Q2;
|
|
512
578
|
var F_MINUS_INV_Q = -F_INV_Q;
|
|
513
579
|
var MAX_BL_SMALL = [1, 1, 2, 2, 4, 7, 14, 27, 53, 106, 209];
|
|
514
580
|
var MAX_BL_LARGE = [2, 2, 5, 7, 12, 21, 40, 78, 157, 308];
|
|
@@ -669,9 +735,9 @@ var COMPLEX_ROOTS = /* @__PURE__ */ (() => {
|
|
|
669
735
|
var intField = {
|
|
670
736
|
mul(x, y) {
|
|
671
737
|
let z = Math.imul(x, y);
|
|
672
|
-
let w = Math.imul(
|
|
673
|
-
z = (z + w >>> 16) -
|
|
674
|
-
z +=
|
|
738
|
+
let w = Math.imul(Q2, Math.imul(z, Q0I) & 65535);
|
|
739
|
+
z = (z + w >>> 16) - Q2;
|
|
740
|
+
z += Q2 & z >> 31;
|
|
675
741
|
return z >>> 0;
|
|
676
742
|
},
|
|
677
743
|
inv(y) {
|
|
@@ -705,7 +771,7 @@ function getIntPoly(logn) {
|
|
|
705
771
|
const F = Number(invert(BigInt(n), QBig));
|
|
706
772
|
const { mod, smod, NTT } = genCrystals({
|
|
707
773
|
N: n,
|
|
708
|
-
Q,
|
|
774
|
+
Q: Q2,
|
|
709
775
|
F,
|
|
710
776
|
ROOT_OF_UNITY: 7,
|
|
711
777
|
newPoly,
|
|
@@ -1067,8 +1133,8 @@ function genFalcon(opts) {
|
|
|
1067
1133
|
let rt1 = floatPoly.FFT(floatPoly.convSmall(f));
|
|
1068
1134
|
let rt2 = floatPoly.FFT(floatPoly.convSmall(g));
|
|
1069
1135
|
const rt3 = floatPoly.invNorm(rt1, rt2);
|
|
1070
|
-
rt1 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt1),
|
|
1071
|
-
rt2 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt2),
|
|
1136
|
+
rt1 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt1), Q2), rt3));
|
|
1137
|
+
rt2 = floatPoly.iFFT(floatPoly.scaleNorm(floatPoly.mulConst(floatPoly.conj(rt2), Q2), rt3));
|
|
1072
1138
|
let bnorm = 0;
|
|
1073
1139
|
for (let u = 0; u < rt1.length; u++) {
|
|
1074
1140
|
bnorm += rt1[u].re * rt1[u].re;
|
|
@@ -1260,7 +1326,7 @@ function genFalcon(opts) {
|
|
|
1260
1326
|
const G = new Int8Array(N2);
|
|
1261
1327
|
for (let u = 0; u < N2; u++) {
|
|
1262
1328
|
let w = t1[u];
|
|
1263
|
-
w -=
|
|
1329
|
+
w -= Q2 & ~-(w - Qhalf >>> 31);
|
|
1264
1330
|
const gi = w | 0;
|
|
1265
1331
|
if (gi < -127 || gi > 127) {
|
|
1266
1332
|
cleanBytes(t1, t2, tt, G);
|
|
@@ -1274,11 +1340,11 @@ function genFalcon(opts) {
|
|
|
1274
1340
|
function HashToPoint(nonce, msg) {
|
|
1275
1341
|
const h = shake2562.create().update(nonce).update(msg);
|
|
1276
1342
|
const c = new Uint16Array(N2);
|
|
1277
|
-
const kQ = 5 *
|
|
1343
|
+
const kQ = 5 * Q2;
|
|
1278
1344
|
for (let i = 0; i < N2; ) {
|
|
1279
1345
|
const tmp = h.xof(2);
|
|
1280
1346
|
let w = tmp[0] << 8 | tmp[1];
|
|
1281
|
-
if (w < kQ) c[i++] = w %
|
|
1347
|
+
if (w < kQ) c[i++] = w % Q2;
|
|
1282
1348
|
}
|
|
1283
1349
|
return c;
|
|
1284
1350
|
}
|
|
@@ -1692,7 +1758,7 @@ function genFalcon(opts) {
|
|
|
1692
1758
|
if (!(hashPoint instanceof Uint16Array) || hashPoint.length !== N2)
|
|
1693
1759
|
throw new Error(`hashPoint: expected Uint16Array(${N2})`);
|
|
1694
1760
|
for (let i = 0; i < N2; i++)
|
|
1695
|
-
if (hashPoint[i] >=
|
|
1761
|
+
if (hashPoint[i] >= Q2) throw new Error("hashPoint: coefficient out of range");
|
|
1696
1762
|
};
|
|
1697
1763
|
const internal = Object.freeze({
|
|
1698
1764
|
sign(hashPoint, sk, internalOpts = {}) {
|
|
@@ -1794,16 +1860,6 @@ var nobleEngine = Object.freeze({
|
|
|
1794
1860
|
verifyHashPoint: (s2, hashPoint, publicKey) => falcon512.internal.verify(s2, hashPoint, publicKey)
|
|
1795
1861
|
});
|
|
1796
1862
|
|
|
1797
|
-
// src/falcon/s2morrow/constants.ts
|
|
1798
|
-
var Q2 = 12289;
|
|
1799
|
-
var QB = 12289n;
|
|
1800
|
-
var N = 512;
|
|
1801
|
-
var SIG_BOUND_512 = 34034726;
|
|
1802
|
-
var PACKED_SLOTS = 29;
|
|
1803
|
-
var VALUES_PER_HALF = 9;
|
|
1804
|
-
var SALT_FELTS = 2;
|
|
1805
|
-
var SIGNATURE_FELTS = 61;
|
|
1806
|
-
|
|
1807
1863
|
// src/falcon/s2morrow/nttRoots.ts
|
|
1808
1864
|
var NTT_ROOTS_ALL = Object.freeze([
|
|
1809
1865
|
1479,
|
|
@@ -2833,20 +2889,20 @@ var NTT_ROOTS_ALL = Object.freeze([
|
|
|
2833
2889
|
// src/falcon/s2morrow/ntt.ts
|
|
2834
2890
|
var I2 = 6145;
|
|
2835
2891
|
var SQR1 = 1479;
|
|
2836
|
-
var modQ = (a) => (a %
|
|
2892
|
+
var modQ = (a) => (a % Q + Q) % Q;
|
|
2837
2893
|
function powMod(base, exponent) {
|
|
2838
2894
|
let result = 1;
|
|
2839
2895
|
let b = modQ(base);
|
|
2840
2896
|
for (let e = exponent; e > 0; e >>= 1) {
|
|
2841
|
-
if (e & 1) result = result * b %
|
|
2842
|
-
b = b * b %
|
|
2897
|
+
if (e & 1) result = result * b % Q;
|
|
2898
|
+
b = b * b % Q;
|
|
2843
2899
|
}
|
|
2844
2900
|
return result;
|
|
2845
2901
|
}
|
|
2846
2902
|
function invMod(x) {
|
|
2847
2903
|
const r = modQ(x);
|
|
2848
2904
|
if (r === 0) throw new Error("Falcon512: 0 has no inverse mod q");
|
|
2849
|
-
return powMod(r,
|
|
2905
|
+
return powMod(r, Q - 2);
|
|
2850
2906
|
}
|
|
2851
2907
|
var nttRoots = (n) => NTT_ROOTS_ALL.slice(n - 2, 2 * n - 2);
|
|
2852
2908
|
function checkSize(n) {
|
|
@@ -2857,7 +2913,7 @@ function checkSize(n) {
|
|
|
2857
2913
|
function nttRec(f) {
|
|
2858
2914
|
const n = f.length;
|
|
2859
2915
|
if (n === 2) {
|
|
2860
|
-
const t = SQR1 * f[1] %
|
|
2916
|
+
const t = SQR1 * f[1] % Q;
|
|
2861
2917
|
return [modQ(f[0] + t), modQ(f[0] - t)];
|
|
2862
2918
|
}
|
|
2863
2919
|
const half = n / 2;
|
|
@@ -2872,7 +2928,7 @@ function nttRec(f) {
|
|
|
2872
2928
|
const w = nttRoots(n);
|
|
2873
2929
|
const out = new Array(n);
|
|
2874
2930
|
for (let i = 0; i < half; i++) {
|
|
2875
|
-
const wb = w[2 * i] * b[i] %
|
|
2931
|
+
const wb = w[2 * i] * b[i] % Q;
|
|
2876
2932
|
out[2 * i] = modQ(a[i] + wb);
|
|
2877
2933
|
out[2 * i + 1] = modQ(a[i] - wb);
|
|
2878
2934
|
}
|
|
@@ -2883,7 +2939,7 @@ function inttRec(fNtt) {
|
|
|
2883
2939
|
if (n === 2) {
|
|
2884
2940
|
const s = modQ(fNtt[0] + fNtt[1]);
|
|
2885
2941
|
const d = modQ(fNtt[0] - fNtt[1]);
|
|
2886
|
-
return [I2 * s %
|
|
2942
|
+
return [I2 * s % Q, I2 * invMod(SQR1) % Q * d % Q];
|
|
2887
2943
|
}
|
|
2888
2944
|
const half = n / 2;
|
|
2889
2945
|
const w = nttRoots(n);
|
|
@@ -2892,8 +2948,8 @@ function inttRec(fNtt) {
|
|
|
2892
2948
|
for (let i = 0; i < half; i++) {
|
|
2893
2949
|
const s = modQ(fNtt[2 * i] + fNtt[2 * i + 1]);
|
|
2894
2950
|
const d = modQ(fNtt[2 * i] - fNtt[2 * i + 1]);
|
|
2895
|
-
f0[i] = I2 * s %
|
|
2896
|
-
f1[i] = I2 * d %
|
|
2951
|
+
f0[i] = I2 * s % Q;
|
|
2952
|
+
f1[i] = I2 * d % Q * invMod(w[2 * i]) % Q;
|
|
2897
2953
|
}
|
|
2898
2954
|
const a = inttRec(f0);
|
|
2899
2955
|
const b = inttRec(f1);
|
|
@@ -2916,14 +2972,14 @@ function mulZq(a, b) {
|
|
|
2916
2972
|
if (a.length !== b.length) throw new Error("Falcon512: polynomial sizes differ");
|
|
2917
2973
|
const an = ntt(a);
|
|
2918
2974
|
const bn = ntt(b);
|
|
2919
|
-
return intt(an.map((v, i) => v * bn[i] %
|
|
2975
|
+
return intt(an.map((v, i) => v * bn[i] % Q));
|
|
2920
2976
|
}
|
|
2921
2977
|
function divZq(a, b) {
|
|
2922
2978
|
if (a.length !== b.length) throw new Error("Falcon512: polynomial sizes differ");
|
|
2923
2979
|
const an = ntt(a);
|
|
2924
2980
|
const bn = ntt(b);
|
|
2925
2981
|
if (bn.some((v) => v === 0)) throw new Error("Falcon512: polynomial is not invertible mod q");
|
|
2926
|
-
return intt(an.map((v, i) => v * invMod(bn[i]) %
|
|
2982
|
+
return intt(an.map((v, i) => v * invMod(bn[i]) % Q));
|
|
2927
2983
|
}
|
|
2928
2984
|
function mulNegacyclicInt(a, b) {
|
|
2929
2985
|
const n = a.length;
|
|
@@ -2943,22 +2999,7 @@ function mulNegacyclicInt(a, b) {
|
|
|
2943
2999
|
}
|
|
2944
3000
|
|
|
2945
3001
|
// src/falcon/s2morrow/packing.ts
|
|
2946
|
-
import * as
|
|
2947
|
-
|
|
2948
|
-
// src/falcon/s2morrow/felt.ts
|
|
2949
|
-
import * as starknet from "starknet";
|
|
2950
|
-
function assertFelt(x, label) {
|
|
2951
|
-
if (typeof x !== "bigint") throw new Error(`Falcon512: ${label} is not a felt252`);
|
|
2952
|
-
starknet.CairoFelt252.assertRange(x);
|
|
2953
|
-
}
|
|
2954
|
-
var FELT_STRING = /^(0[xX][0-9a-fA-F]{1,64}|[0-9]{1,76})$/;
|
|
2955
|
-
function parseFelt(value, label) {
|
|
2956
|
-
const accepted = typeof value === "bigint" && value >= 0n || typeof value === "number" && Number.isSafeInteger(value) && value >= 0 || typeof value === "string" && FELT_STRING.test(value);
|
|
2957
|
-
if (!accepted) throw new Error(`Falcon512: ${label} is not a felt252`);
|
|
2958
|
-
return new starknet.CairoFelt252(value).toBigInt();
|
|
2959
|
-
}
|
|
2960
|
-
|
|
2961
|
-
// src/falcon/s2morrow/packing.ts
|
|
3002
|
+
import * as starknet3 from "starknet";
|
|
2962
3003
|
var horner = (values, start, end) => {
|
|
2963
3004
|
let acc = 0n;
|
|
2964
3005
|
for (let i = end - 1; i >= start; i--) acc = acc * QB + BigInt(values[i]);
|
|
@@ -2968,7 +3009,7 @@ function packPoly(poly) {
|
|
|
2968
3009
|
if (poly.length !== N) throw new Error(`Falcon512: polynomial must have ${N} coefficients`);
|
|
2969
3010
|
for (let i = 0; i < N; i++) {
|
|
2970
3011
|
const v = poly[i];
|
|
2971
|
-
if (!Number.isInteger(v) || v < 0 || v >=
|
|
3012
|
+
if (!Number.isInteger(v) || v < 0 || v >= Q) {
|
|
2972
3013
|
throw new Error("Falcon512: polynomial coefficient out of range");
|
|
2973
3014
|
}
|
|
2974
3015
|
}
|
|
@@ -2988,7 +3029,7 @@ function unpackPoly(felts) {
|
|
|
2988
3029
|
let idx = 0;
|
|
2989
3030
|
for (const felt of felts) {
|
|
2990
3031
|
assertFelt(felt, "packed polynomial slot");
|
|
2991
|
-
for (const half of [felt &
|
|
3032
|
+
for (const half of [felt & starknet3.UINT_128_MAX, felt >> 128n]) {
|
|
2992
3033
|
const count = Math.min(VALUES_PER_HALF, N - idx);
|
|
2993
3034
|
if (half >= QB ** BigInt(count)) {
|
|
2994
3035
|
throw new Error("Falcon512: non-canonical packed polynomial");
|
|
@@ -3003,7 +3044,227 @@ function unpackPoly(felts) {
|
|
|
3003
3044
|
return out;
|
|
3004
3045
|
}
|
|
3005
3046
|
|
|
3047
|
+
// src/falcon/s2morrow/signature.ts
|
|
3048
|
+
import { randomBytes as cryptoRandomBytes } from "@noble/hashes/utils.js";
|
|
3049
|
+
import * as starknet5 from "starknet";
|
|
3050
|
+
|
|
3051
|
+
// src/falcon/s2morrow/hashToPoint.ts
|
|
3052
|
+
import { poseidonHashMany, poseidonSmall } from "@scure/starknet";
|
|
3053
|
+
import * as starknet4 from "starknet";
|
|
3054
|
+
function hashToPoint(message, salt) {
|
|
3055
|
+
message.forEach((m) => assertFelt(m, "message element"));
|
|
3056
|
+
salt.forEach((s) => assertFelt(s, "salt element"));
|
|
3057
|
+
const out = new Uint16Array(N);
|
|
3058
|
+
let idx = 0;
|
|
3059
|
+
const extract6 = (value) => {
|
|
3060
|
+
let v = value;
|
|
3061
|
+
for (let i = 0; i < 6 && idx < N; i++) {
|
|
3062
|
+
out[idx++] = Number(v % QB);
|
|
3063
|
+
v /= QB;
|
|
3064
|
+
}
|
|
3065
|
+
};
|
|
3066
|
+
const extract12 = (felt) => {
|
|
3067
|
+
extract6(felt & starknet4.UINT_128_MAX);
|
|
3068
|
+
extract6(felt >> 128n);
|
|
3069
|
+
};
|
|
3070
|
+
let state = [poseidonHashMany([...message, ...salt]), 0n, 0n];
|
|
3071
|
+
for (let round = 0; round < 21; round++) {
|
|
3072
|
+
state = poseidonSmall(state);
|
|
3073
|
+
extract12(state[0]);
|
|
3074
|
+
extract12(state[1]);
|
|
3075
|
+
}
|
|
3076
|
+
state = poseidonSmall(state);
|
|
3077
|
+
extract12(state[0]);
|
|
3078
|
+
return out;
|
|
3079
|
+
}
|
|
3080
|
+
|
|
3081
|
+
// src/falcon/s2morrow/signature.ts
|
|
3082
|
+
var defaultDeps = { engine: nobleEngine, randomBytes: cryptoRandomBytes };
|
|
3083
|
+
function computeMulHint(s1, pkNtt) {
|
|
3084
|
+
const s1Ntt = ntt(s1);
|
|
3085
|
+
return intt(s1Ntt.map((v, i) => v * pkNtt[i] % Q));
|
|
3086
|
+
}
|
|
3087
|
+
var centeredSquare = (x) => {
|
|
3088
|
+
const c = x <= (Q - 1) / 2 ? x : Q - x;
|
|
3089
|
+
return c * c;
|
|
3090
|
+
};
|
|
3091
|
+
function checkSignatureEquation(hashPoint, s1, mulHint, pkNtt) {
|
|
3092
|
+
if (hashPoint.length !== N || s1.length !== N || mulHint.length !== N || pkNtt.length !== N) {
|
|
3093
|
+
return false;
|
|
3094
|
+
}
|
|
3095
|
+
const s1Ntt = ntt(s1);
|
|
3096
|
+
const hintNtt = ntt(mulHint);
|
|
3097
|
+
for (let i = 0; i < N; i += 1) {
|
|
3098
|
+
if (s1Ntt[i] * pkNtt[i] % Q !== hintNtt[i]) return false;
|
|
3099
|
+
}
|
|
3100
|
+
let norm = 0;
|
|
3101
|
+
for (let i = 0; i < N; i += 1) {
|
|
3102
|
+
norm += centeredSquare(modQ(hashPoint[i] - mulHint[i])) + centeredSquare(s1[i]);
|
|
3103
|
+
}
|
|
3104
|
+
return norm <= SIG_BOUND_512;
|
|
3105
|
+
}
|
|
3106
|
+
function formatSignature(sig) {
|
|
3107
|
+
const felts = [
|
|
3108
|
+
...packPoly(sig.s1),
|
|
3109
|
+
BigInt(sig.salt.length),
|
|
3110
|
+
...sig.salt,
|
|
3111
|
+
...packPoly(sig.mulHint)
|
|
3112
|
+
];
|
|
3113
|
+
return felts.map((x) => starknet5.num.toHex(x));
|
|
3114
|
+
}
|
|
3115
|
+
function parseSignature(signature) {
|
|
3116
|
+
if (!Array.isArray(signature) || signature.length !== SIGNATURE_FELTS) {
|
|
3117
|
+
throw new Error(`Falcon512: signature must have ${SIGNATURE_FELTS} felts`);
|
|
3118
|
+
}
|
|
3119
|
+
const felts = signature.map((x, i) => parseFelt(x, `signature[${i}]`));
|
|
3120
|
+
if (felts[PACKED_SLOTS] !== BigInt(SALT_FELTS)) {
|
|
3121
|
+
throw new Error("Falcon512: unexpected salt length");
|
|
3122
|
+
}
|
|
3123
|
+
const saltStart = PACKED_SLOTS + 1;
|
|
3124
|
+
return {
|
|
3125
|
+
s1: unpackPoly(felts.slice(0, PACKED_SLOTS)),
|
|
3126
|
+
salt: felts.slice(saltStart, saltStart + SALT_FELTS),
|
|
3127
|
+
mulHint: unpackPoly(felts.slice(saltStart + SALT_FELTS))
|
|
3128
|
+
};
|
|
3129
|
+
}
|
|
3130
|
+
var randomSaltFelt = (randomBytes2) => BigInt(`0x${starknet5.encode.buf2hex(randomBytes2(31))}`);
|
|
3131
|
+
function signHash(msgHash, secretKey, pkNtt, deps = defaultDeps) {
|
|
3132
|
+
assertFelt(msgHash, "message hash");
|
|
3133
|
+
const salt = [randomSaltFelt(deps.randomBytes), randomSaltFelt(deps.randomBytes)];
|
|
3134
|
+
const hashPoint = hashToPoint([msgHash], salt);
|
|
3135
|
+
const s2 = deps.engine.signHashPoint(hashPoint, secretKey);
|
|
3136
|
+
const s1 = Uint16Array.from(s2, modQ);
|
|
3137
|
+
const mulHint = Uint16Array.from(computeMulHint(s1, pkNtt));
|
|
3138
|
+
const signature = formatSignature({ s1, salt, mulHint });
|
|
3139
|
+
const decoded = parseSignature(signature);
|
|
3140
|
+
if (!checkSignatureEquation(hashPoint, decoded.s1, decoded.mulHint, pkNtt)) {
|
|
3141
|
+
throw new Error("Falcon512: self-verification failed, signature discarded");
|
|
3142
|
+
}
|
|
3143
|
+
return signature;
|
|
3144
|
+
}
|
|
3145
|
+
function verifyHash(msgHash, signature, pkNtt) {
|
|
3146
|
+
let decoded;
|
|
3147
|
+
try {
|
|
3148
|
+
assertFelt(msgHash, "message hash");
|
|
3149
|
+
decoded = parseSignature(signature);
|
|
3150
|
+
} catch {
|
|
3151
|
+
return false;
|
|
3152
|
+
}
|
|
3153
|
+
return checkSignatureEquation(
|
|
3154
|
+
hashToPoint([msgHash], decoded.salt),
|
|
3155
|
+
decoded.s1,
|
|
3156
|
+
decoded.mulHint,
|
|
3157
|
+
pkNtt
|
|
3158
|
+
);
|
|
3159
|
+
}
|
|
3160
|
+
|
|
3161
|
+
// src/falcon/oz/hashToPoint.ts
|
|
3162
|
+
import { shake256 as shake2563 } from "@noble/hashes/sha3.js";
|
|
3163
|
+
var SALT_BYTES = 20;
|
|
3164
|
+
var SALT_LIMIT = 1n << BigInt(8 * SALT_BYTES);
|
|
3165
|
+
var KQ = 5 * Q;
|
|
3166
|
+
var writeLittleEndian = (x, out, offset, length) => {
|
|
3167
|
+
let v = x;
|
|
3168
|
+
for (let i = 0; i < length; i += 1) {
|
|
3169
|
+
out[offset + i] = Number(v & 0xffn);
|
|
3170
|
+
v >>= 8n;
|
|
3171
|
+
}
|
|
3172
|
+
};
|
|
3173
|
+
function saltFeltsToBytes(saltA, saltB) {
|
|
3174
|
+
[saltA, saltB].forEach((salt) => {
|
|
3175
|
+
if (typeof salt !== "bigint" || salt < 0n || salt >= SALT_LIMIT) {
|
|
3176
|
+
throw new Error("Falcon512: salt felt must be non-negative and below 2^160");
|
|
3177
|
+
}
|
|
3178
|
+
});
|
|
3179
|
+
const out = new Uint8Array(2 * SALT_BYTES);
|
|
3180
|
+
writeLittleEndian(saltA, out, 0, SALT_BYTES);
|
|
3181
|
+
writeLittleEndian(saltB, out, SALT_BYTES, SALT_BYTES);
|
|
3182
|
+
return out;
|
|
3183
|
+
}
|
|
3184
|
+
function hashToPointShake(msgHash, saltA, saltB) {
|
|
3185
|
+
assertFelt(msgHash, "message hash");
|
|
3186
|
+
const message = new Uint8Array(32);
|
|
3187
|
+
writeLittleEndian(msgHash, message, 0, 32);
|
|
3188
|
+
const xof = shake2563.create().update(saltFeltsToBytes(saltA, saltB)).update(message);
|
|
3189
|
+
const out = new Uint16Array(N);
|
|
3190
|
+
for (let i = 0; i < N; ) {
|
|
3191
|
+
const [high, low] = xof.xof(2);
|
|
3192
|
+
const word = high << 8 | low;
|
|
3193
|
+
if (word < KQ) {
|
|
3194
|
+
out[i] = word % Q;
|
|
3195
|
+
i += 1;
|
|
3196
|
+
}
|
|
3197
|
+
}
|
|
3198
|
+
return out;
|
|
3199
|
+
}
|
|
3200
|
+
|
|
3201
|
+
// src/falcon/oz/signature.ts
|
|
3202
|
+
var OZ_SIGNATURE_FELTS = Object.freeze({
|
|
3203
|
+
hint: 2 * PACKED_SLOTS + 2,
|
|
3204
|
+
direct: PACKED_SLOTS + 2
|
|
3205
|
+
});
|
|
3206
|
+
var defaultDeps2 = { engine: nobleEngine, randomBytes: cryptoRandomBytes2 };
|
|
3207
|
+
var readLittleEndian = (bytes) => bytes.reduceRight((acc, byte) => acc * 256n + BigInt(byte), 0n);
|
|
3208
|
+
function formatOzSignature(sig, variant) {
|
|
3209
|
+
const felts = [...packPoly(sig.s1), sig.saltA, sig.saltB];
|
|
3210
|
+
if (variant === "hint") {
|
|
3211
|
+
if (!sig.mulHint) throw new Error("Falcon512: the hint layout needs the hint polynomial");
|
|
3212
|
+
felts.push(...packPoly(sig.mulHint));
|
|
3213
|
+
}
|
|
3214
|
+
return felts.map((x) => starknet6.num.toHex(x));
|
|
3215
|
+
}
|
|
3216
|
+
function parseOzSignature(signature, variant) {
|
|
3217
|
+
const length = OZ_SIGNATURE_FELTS[variant];
|
|
3218
|
+
if (!Array.isArray(signature) || signature.length !== length) {
|
|
3219
|
+
throw new Error(`Falcon512: signature must have ${length} felts`);
|
|
3220
|
+
}
|
|
3221
|
+
const felts = signature.map((x, i) => parseFelt(x, `signature[${i}]`));
|
|
3222
|
+
const saltA = felts[PACKED_SLOTS];
|
|
3223
|
+
const saltB = felts[PACKED_SLOTS + 1];
|
|
3224
|
+
saltFeltsToBytes(saltA, saltB);
|
|
3225
|
+
return {
|
|
3226
|
+
s1: unpackPoly(felts.slice(0, PACKED_SLOTS)),
|
|
3227
|
+
saltA,
|
|
3228
|
+
saltB,
|
|
3229
|
+
mulHint: variant === "hint" ? unpackPoly(felts.slice(PACKED_SLOTS + 2)) : void 0
|
|
3230
|
+
};
|
|
3231
|
+
}
|
|
3232
|
+
function signHashOz(msgHash, secretKey, pkNtt, variant, deps = defaultDeps2) {
|
|
3233
|
+
assertFelt(msgHash, "message hash");
|
|
3234
|
+
const salt = deps.randomBytes(2 * SALT_BYTES);
|
|
3235
|
+
const saltA = readLittleEndian(salt.subarray(0, SALT_BYTES));
|
|
3236
|
+
const saltB = readLittleEndian(salt.subarray(SALT_BYTES));
|
|
3237
|
+
const hashPoint = hashToPointShake(msgHash, saltA, saltB);
|
|
3238
|
+
const s1 = Uint16Array.from(deps.engine.signHashPoint(hashPoint, secretKey), modQ);
|
|
3239
|
+
const mulHint = variant === "hint" ? Uint16Array.from(computeMulHint(s1, pkNtt)) : void 0;
|
|
3240
|
+
const signature = formatOzSignature({ s1, saltA, saltB, mulHint }, variant);
|
|
3241
|
+
const decoded = parseOzSignature(signature, variant);
|
|
3242
|
+
const decodedHint = decoded.mulHint ?? computeMulHint(decoded.s1, pkNtt);
|
|
3243
|
+
if (!checkSignatureEquation(hashPoint, decoded.s1, decodedHint, pkNtt)) {
|
|
3244
|
+
throw new Error("Falcon512: self-verification failed, signature discarded");
|
|
3245
|
+
}
|
|
3246
|
+
return signature;
|
|
3247
|
+
}
|
|
3248
|
+
function verifyHashOz(msgHash, signature, pkNtt, variant) {
|
|
3249
|
+
let decoded;
|
|
3250
|
+
try {
|
|
3251
|
+
assertFelt(msgHash, "message hash");
|
|
3252
|
+
decoded = parseOzSignature(signature, variant);
|
|
3253
|
+
} catch {
|
|
3254
|
+
return false;
|
|
3255
|
+
}
|
|
3256
|
+
const mulHint = decoded.mulHint ?? computeMulHint(decoded.s1, pkNtt);
|
|
3257
|
+
return checkSignatureEquation(
|
|
3258
|
+
hashToPointShake(msgHash, decoded.saltA, decoded.saltB),
|
|
3259
|
+
decoded.s1,
|
|
3260
|
+
mulHint,
|
|
3261
|
+
pkNtt
|
|
3262
|
+
);
|
|
3263
|
+
}
|
|
3264
|
+
|
|
3006
3265
|
// src/falcon/s2morrow/keys.ts
|
|
3266
|
+
import { isBytes as isBytes2 } from "@noble/hashes/utils.js";
|
|
3267
|
+
import * as starknet7 from "starknet";
|
|
3007
3268
|
var SECRET_KEY_LENGTH = 1281;
|
|
3008
3269
|
var PUBLIC_KEY_LENGTH = 897;
|
|
3009
3270
|
var SEED_LENGTH = 48;
|
|
@@ -3026,7 +3287,7 @@ function toBytes(input, expectedLength, label) {
|
|
|
3026
3287
|
if (typeof input !== "string") {
|
|
3027
3288
|
throw new Error(`Falcon512: ${label} must be a Uint8Array or a hex string`);
|
|
3028
3289
|
}
|
|
3029
|
-
const hex =
|
|
3290
|
+
const hex = starknet7.encode.removeHexPrefix(input);
|
|
3030
3291
|
if (!/^[0-9a-fA-F]*$/.test(hex)) {
|
|
3031
3292
|
throw new Error(`Falcon512: ${label} is not a valid hex string`);
|
|
3032
3293
|
}
|
|
@@ -3035,12 +3296,12 @@ function toBytes(input, expectedLength, label) {
|
|
|
3035
3296
|
`Falcon512: ${label} must have ${2 * expectedLength} hex digits, got ${hex.length}`
|
|
3036
3297
|
);
|
|
3037
3298
|
}
|
|
3038
|
-
return
|
|
3299
|
+
return starknet7.num.hexToBytes(starknet7.encode.addHexPrefix(hex));
|
|
3039
3300
|
}
|
|
3040
3301
|
var wipe = (...arrays) => {
|
|
3041
3302
|
arrays.forEach((a) => a.fill(0));
|
|
3042
3303
|
};
|
|
3043
|
-
var center = (x) => x >
|
|
3304
|
+
var center = (x) => x > Q >> 1 ? x - Q : x;
|
|
3044
3305
|
var toSigned = (raw, bits) => raw << 32 - bits >> 32 - bits;
|
|
3045
3306
|
function encodeBitsMSB(values, bits) {
|
|
3046
3307
|
const out = new Uint8Array(values.length * bits / 8);
|
|
@@ -3106,7 +3367,7 @@ function decode14LSB(bytes) {
|
|
|
3106
3367
|
const v = acc & 16383;
|
|
3107
3368
|
acc >>>= 14;
|
|
3108
3369
|
accBits -= 14;
|
|
3109
|
-
if (v >=
|
|
3370
|
+
if (v >= Q) {
|
|
3110
3371
|
throw new Error("Falcon512: invalid falcon-rs secret key (coefficient out of range)");
|
|
3111
3372
|
}
|
|
3112
3373
|
out.push(v);
|
|
@@ -3142,7 +3403,7 @@ function checkNtruEquation({ f, g, F, G }) {
|
|
|
3142
3403
|
const gF = mulNegacyclicInt(g, F);
|
|
3143
3404
|
try {
|
|
3144
3405
|
for (let i = 0; i < N; i++) {
|
|
3145
|
-
if (fG[i] - gF[i] !== (i === 0 ?
|
|
3406
|
+
if (fG[i] - gF[i] !== (i === 0 ? Q : 0)) {
|
|
3146
3407
|
throw new Error("Falcon512: invalid secret key (NTRU equation not satisfied)");
|
|
3147
3408
|
}
|
|
3148
3409
|
}
|
|
@@ -3215,7 +3476,7 @@ function decodePublicKey(publicKey) {
|
|
|
3215
3476
|
const bytes = toBytes(publicKey, PUBLIC_KEY_LENGTH, "public key");
|
|
3216
3477
|
if (bytes[0] !== PUBLIC_KEY_HEADER) throw new Error("Falcon512: invalid public key header");
|
|
3217
3478
|
const h = decodeBitsMSB(bytes.subarray(1), N, 14);
|
|
3218
|
-
if (h.some((v) => v >=
|
|
3479
|
+
if (h.some((v) => v >= Q)) {
|
|
3219
3480
|
throw new Error("Falcon512: invalid public key (coefficient out of range)");
|
|
3220
3481
|
}
|
|
3221
3482
|
return Uint16Array.from(h);
|
|
@@ -3225,7 +3486,7 @@ function encodePublicKey(h) {
|
|
|
3225
3486
|
throw new Error(`Falcon512: public key polynomial must have ${N} coefficients`);
|
|
3226
3487
|
}
|
|
3227
3488
|
for (let i = 0; i < N; i++) {
|
|
3228
|
-
if (!Number.isInteger(h[i]) || h[i] < 0 || h[i] >=
|
|
3489
|
+
if (!Number.isInteger(h[i]) || h[i] < 0 || h[i] >= Q) {
|
|
3229
3490
|
throw new Error("Falcon512: invalid public key (coefficient out of range)");
|
|
3230
3491
|
}
|
|
3231
3492
|
}
|
|
@@ -3252,7 +3513,7 @@ function publicKeyToNtt(publicKey) {
|
|
|
3252
3513
|
return Uint16Array.from(ntt(decodePublicKey(publicKey)));
|
|
3253
3514
|
}
|
|
3254
3515
|
function getPackedPublicKey(publicKey) {
|
|
3255
|
-
return packPoly(publicKeyToNtt(publicKey)).map((x) =>
|
|
3516
|
+
return packPoly(publicKeyToNtt(publicKey)).map((x) => starknet7.num.toHex(x));
|
|
3256
3517
|
}
|
|
3257
3518
|
function importFalconRsSecretKey(secretKey) {
|
|
3258
3519
|
const bytes = toBytes(secretKey, FALCON_RS_SECRET_KEY_LENGTH, "falcon-rs secret key");
|
|
@@ -3298,144 +3559,35 @@ function exportFalconRsSecretKey(secretKey) {
|
|
|
3298
3559
|
}
|
|
3299
3560
|
}
|
|
3300
3561
|
|
|
3301
|
-
// src/falcon/s2morrow/signature.ts
|
|
3302
|
-
import { randomBytes as cryptoRandomBytes } from "@noble/hashes/utils.js";
|
|
3303
|
-
import * as starknet5 from "starknet";
|
|
3304
|
-
|
|
3305
|
-
// src/falcon/s2morrow/hashToPoint.ts
|
|
3306
|
-
import { poseidonHashMany, poseidonSmall } from "@scure/starknet";
|
|
3307
|
-
import * as starknet4 from "starknet";
|
|
3308
|
-
function hashToPoint(message, salt) {
|
|
3309
|
-
message.forEach((m) => assertFelt(m, "message element"));
|
|
3310
|
-
salt.forEach((s) => assertFelt(s, "salt element"));
|
|
3311
|
-
const out = new Uint16Array(N);
|
|
3312
|
-
let idx = 0;
|
|
3313
|
-
const extract6 = (value) => {
|
|
3314
|
-
let v = value;
|
|
3315
|
-
for (let i = 0; i < 6 && idx < N; i++) {
|
|
3316
|
-
out[idx++] = Number(v % QB);
|
|
3317
|
-
v /= QB;
|
|
3318
|
-
}
|
|
3319
|
-
};
|
|
3320
|
-
const extract12 = (felt) => {
|
|
3321
|
-
extract6(felt & starknet4.UINT_128_MAX);
|
|
3322
|
-
extract6(felt >> 128n);
|
|
3323
|
-
};
|
|
3324
|
-
let state = [poseidonHashMany([...message, ...salt]), 0n, 0n];
|
|
3325
|
-
for (let round = 0; round < 21; round++) {
|
|
3326
|
-
state = poseidonSmall(state);
|
|
3327
|
-
extract12(state[0]);
|
|
3328
|
-
extract12(state[1]);
|
|
3329
|
-
}
|
|
3330
|
-
state = poseidonSmall(state);
|
|
3331
|
-
extract12(state[0]);
|
|
3332
|
-
return out;
|
|
3333
|
-
}
|
|
3334
|
-
|
|
3335
|
-
// src/falcon/s2morrow/signature.ts
|
|
3336
|
-
var defaultDeps = { engine: nobleEngine, randomBytes: cryptoRandomBytes };
|
|
3337
|
-
function computeMulHint(s1, pkNtt) {
|
|
3338
|
-
const s1Ntt = ntt(s1);
|
|
3339
|
-
return intt(s1Ntt.map((v, i) => v * pkNtt[i] % Q2));
|
|
3340
|
-
}
|
|
3341
|
-
var centeredSquare = (x) => {
|
|
3342
|
-
const c = x <= (Q2 - 1) / 2 ? x : Q2 - x;
|
|
3343
|
-
return c * c;
|
|
3344
|
-
};
|
|
3345
|
-
function checkSignatureEquation(hashPoint, s1, mulHint, pkNtt) {
|
|
3346
|
-
if (hashPoint.length !== N || s1.length !== N || mulHint.length !== N || pkNtt.length !== N) {
|
|
3347
|
-
return false;
|
|
3348
|
-
}
|
|
3349
|
-
const s1Ntt = ntt(s1);
|
|
3350
|
-
const hintNtt = ntt(mulHint);
|
|
3351
|
-
for (let i = 0; i < N; i += 1) {
|
|
3352
|
-
if (s1Ntt[i] * pkNtt[i] % Q2 !== hintNtt[i]) return false;
|
|
3353
|
-
}
|
|
3354
|
-
let norm = 0;
|
|
3355
|
-
for (let i = 0; i < N; i += 1) {
|
|
3356
|
-
norm += centeredSquare(modQ(hashPoint[i] - mulHint[i])) + centeredSquare(s1[i]);
|
|
3357
|
-
}
|
|
3358
|
-
return norm <= SIG_BOUND_512;
|
|
3359
|
-
}
|
|
3360
|
-
function formatSignature(sig) {
|
|
3361
|
-
const felts = [
|
|
3362
|
-
...packPoly(sig.s1),
|
|
3363
|
-
BigInt(sig.salt.length),
|
|
3364
|
-
...sig.salt,
|
|
3365
|
-
...packPoly(sig.mulHint)
|
|
3366
|
-
];
|
|
3367
|
-
return felts.map((x) => starknet5.num.toHex(x));
|
|
3368
|
-
}
|
|
3369
|
-
function parseSignature(signature) {
|
|
3370
|
-
if (!Array.isArray(signature) || signature.length !== SIGNATURE_FELTS) {
|
|
3371
|
-
throw new Error(`Falcon512: signature must have ${SIGNATURE_FELTS} felts`);
|
|
3372
|
-
}
|
|
3373
|
-
const felts = signature.map((x, i) => parseFelt(x, `signature[${i}]`));
|
|
3374
|
-
if (felts[PACKED_SLOTS] !== BigInt(SALT_FELTS)) {
|
|
3375
|
-
throw new Error("Falcon512: unexpected salt length");
|
|
3376
|
-
}
|
|
3377
|
-
const saltStart = PACKED_SLOTS + 1;
|
|
3378
|
-
return {
|
|
3379
|
-
s1: unpackPoly(felts.slice(0, PACKED_SLOTS)),
|
|
3380
|
-
salt: felts.slice(saltStart, saltStart + SALT_FELTS),
|
|
3381
|
-
mulHint: unpackPoly(felts.slice(saltStart + SALT_FELTS))
|
|
3382
|
-
};
|
|
3383
|
-
}
|
|
3384
|
-
var randomSaltFelt = (randomBytes2) => BigInt(`0x${starknet5.encode.buf2hex(randomBytes2(31))}`);
|
|
3385
|
-
function signHash(msgHash, secretKey, pkNtt, deps = defaultDeps) {
|
|
3386
|
-
assertFelt(msgHash, "message hash");
|
|
3387
|
-
const salt = [randomSaltFelt(deps.randomBytes), randomSaltFelt(deps.randomBytes)];
|
|
3388
|
-
const hashPoint = hashToPoint([msgHash], salt);
|
|
3389
|
-
const s2 = deps.engine.signHashPoint(hashPoint, secretKey);
|
|
3390
|
-
const s1 = Uint16Array.from(s2, modQ);
|
|
3391
|
-
const mulHint = Uint16Array.from(computeMulHint(s1, pkNtt));
|
|
3392
|
-
const signature = formatSignature({ s1, salt, mulHint });
|
|
3393
|
-
const decoded = parseSignature(signature);
|
|
3394
|
-
if (!checkSignatureEquation(hashPoint, decoded.s1, decoded.mulHint, pkNtt)) {
|
|
3395
|
-
throw new Error("Falcon512: self-verification failed, signature discarded");
|
|
3396
|
-
}
|
|
3397
|
-
return signature;
|
|
3398
|
-
}
|
|
3399
|
-
function verifyHash(msgHash, signature, pkNtt) {
|
|
3400
|
-
let decoded;
|
|
3401
|
-
try {
|
|
3402
|
-
assertFelt(msgHash, "message hash");
|
|
3403
|
-
decoded = parseSignature(signature);
|
|
3404
|
-
} catch {
|
|
3405
|
-
return false;
|
|
3406
|
-
}
|
|
3407
|
-
return checkSignatureEquation(
|
|
3408
|
-
hashToPoint([msgHash], decoded.salt),
|
|
3409
|
-
decoded.s1,
|
|
3410
|
-
decoded.mulHint,
|
|
3411
|
-
pkNtt
|
|
3412
|
-
);
|
|
3413
|
-
}
|
|
3414
|
-
|
|
3415
3562
|
// src/falcon/Falcon512Signer.ts
|
|
3416
3563
|
function assertV3(details) {
|
|
3417
|
-
if (!
|
|
3564
|
+
if (!starknet8.isV3Tx(details)) throw new Error("Falcon512: only V3 transactions are supported");
|
|
3418
3565
|
}
|
|
3419
3566
|
var Falcon512Signer = class _Falcon512Signer {
|
|
3420
3567
|
#secretKey;
|
|
3421
3568
|
#publicKey;
|
|
3422
3569
|
#pkNtt;
|
|
3423
3570
|
#packedPublicKey;
|
|
3571
|
+
#accountType;
|
|
3424
3572
|
/**
|
|
3425
3573
|
* Builds a signer from a NIST Falcon-512 secret key. The key is fully checked, then copied: the
|
|
3426
3574
|
* signer keeps its own copy, in a private field.
|
|
3427
3575
|
* @param {Uint8Array | string} secretKey NIST Falcon-512 secret key (1281 bytes), Uint8Array or
|
|
3428
3576
|
* hex string. Prefer a Uint8Array: a string cannot be wiped from memory.
|
|
3429
|
-
* @
|
|
3577
|
+
* @param {Falcon512Options} [options] `accountType`: the type of the account that checks the
|
|
3578
|
+
* signatures, `'s2morrow'` (default), `'oz-hint'` or `'oz-direct'`
|
|
3579
|
+
* @throws {Error} when the secret key or an option is invalid
|
|
3430
3580
|
* @example
|
|
3431
3581
|
* ```typescript
|
|
3432
3582
|
* const { secretKey } = falcon512.generateKeyPair();
|
|
3433
|
-
* const mySigner = new Falcon512Signer(secretKey);
|
|
3583
|
+
* const mySigner = new Falcon512Signer(secretKey); // for an S2morrow account
|
|
3584
|
+
* const myOzSigner = new Falcon512Signer(secretKey, { accountType: 'oz-hint' });
|
|
3434
3585
|
* new Falcon512Signer(new Uint8Array(1281));
|
|
3435
3586
|
* // throws Error("Falcon512: invalid secret key header")
|
|
3436
3587
|
* ```
|
|
3437
3588
|
*/
|
|
3438
|
-
constructor(secretKey) {
|
|
3589
|
+
constructor(secretKey, options) {
|
|
3590
|
+
const accountType = parseFalconOptions(options);
|
|
3439
3591
|
const bytes = toBytes(secretKey, SECRET_KEY_LENGTH, "secret key");
|
|
3440
3592
|
let publicKey;
|
|
3441
3593
|
try {
|
|
@@ -3444,6 +3596,7 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3444
3596
|
bytes.fill(0);
|
|
3445
3597
|
throw error;
|
|
3446
3598
|
}
|
|
3599
|
+
this.#accountType = accountType;
|
|
3447
3600
|
this.#publicKey = publicKey;
|
|
3448
3601
|
this.#secretKey = bytes;
|
|
3449
3602
|
this.#pkNtt = publicKeyToNtt(this.#publicKey);
|
|
@@ -3455,10 +3608,12 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3455
3608
|
* Warning: falcon-rs derives the seed of its sampler from the public salt of each signature, so
|
|
3456
3609
|
* the signatures it produced may leak information about the secret key. Use this for continuity
|
|
3457
3610
|
* only; for real funds, generate a new key with `falcon512.generateKeyPair`. The S2morrow account
|
|
3458
|
-
* cannot change its key: a new key means a new account.
|
|
3611
|
+
* cannot change its key: a new key means a new account. An OpenZeppelin account can change its
|
|
3612
|
+
* key (see `signOwnershipAcceptance`).
|
|
3459
3613
|
* @param {Uint8Array | string} secretKey the falcon-rs secret key (3584 bytes), or its hex string
|
|
3614
|
+
* @param {Falcon512Options} [options] `accountType`, as for the constructor
|
|
3460
3615
|
* @returns {Falcon512Signer} a signer that holds the same key, in the NIST format
|
|
3461
|
-
* @throws {Error} when the falcon-rs secret key is invalid
|
|
3616
|
+
* @throws {Error} when the falcon-rs secret key or an option is invalid
|
|
3462
3617
|
* @example
|
|
3463
3618
|
* ```typescript
|
|
3464
3619
|
* const mySigner = Falcon512Signer.fromFalconRs(falconRsSecretKey); // 3584 bytes, from falcon-rs
|
|
@@ -3466,10 +3621,10 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3466
3621
|
* // result = ['0xb2cf4f8b3c6f60cc662f438970ad34017e260c52d7911297a42811ce3cea2b', …] (29 felts)
|
|
3467
3622
|
* ```
|
|
3468
3623
|
*/
|
|
3469
|
-
static fromFalconRs(secretKey) {
|
|
3624
|
+
static fromFalconRs(secretKey, options) {
|
|
3470
3625
|
const nist = importFalconRsSecretKey(secretKey);
|
|
3471
3626
|
try {
|
|
3472
|
-
return new _Falcon512Signer(nist);
|
|
3627
|
+
return new _Falcon512Signer(nist, options);
|
|
3473
3628
|
} finally {
|
|
3474
3629
|
nist.fill(0);
|
|
3475
3630
|
}
|
|
@@ -3486,10 +3641,12 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3486
3641
|
* ```
|
|
3487
3642
|
*/
|
|
3488
3643
|
async getPubKey() {
|
|
3489
|
-
return
|
|
3644
|
+
return starknet8.encode.addHexPrefix(starknet8.encode.buf2hex(this.#publicKey));
|
|
3490
3645
|
}
|
|
3491
3646
|
/**
|
|
3492
|
-
* The 29 felts of the
|
|
3647
|
+
* The 29 felts of the public key, in the form the accounts store it: the constructor argument
|
|
3648
|
+
* `pk_packed` of S2morrow, or the `public_key` array of an OpenZeppelin account (whose
|
|
3649
|
+
* constructor calldata is `29`, then these 29 felts). The same for the three account types.
|
|
3493
3650
|
* @returns {string[]} the 29 felts, as hex strings (a new array at each call)
|
|
3494
3651
|
* @example
|
|
3495
3652
|
* ```typescript
|
|
@@ -3506,21 +3663,23 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3506
3663
|
* Signs a SNIP-12 message (TypedData) for off-chain use.
|
|
3507
3664
|
* @param {TypedData} typedData the message to sign
|
|
3508
3665
|
* @param {string} accountAddress address of the account that signs the message
|
|
3509
|
-
* @returns {Promise<Signature>} the
|
|
3666
|
+
* @returns {Promise<Signature>} the signature, as hex strings: 61 felts (S2morrow), 60
|
|
3667
|
+
* (OpenZeppelin, hint) or 31 (OpenZeppelin, direct)
|
|
3510
3668
|
* @example
|
|
3511
3669
|
* ```typescript
|
|
3512
3670
|
* const result = await mySigner.signMessage(myTypedData, accountAddress);
|
|
3513
|
-
* // result = ['0x…', …] (61 felts, random salt); result[29] = '0x2' (salt length)
|
|
3671
|
+
* // result = ['0x…', …] (S2morrow: 61 felts, random salt); result[29] = '0x2' (salt length)
|
|
3514
3672
|
* ```
|
|
3515
3673
|
*/
|
|
3516
3674
|
async signMessage(typedData3, accountAddress) {
|
|
3517
|
-
return this.signRaw(
|
|
3675
|
+
return this.signRaw(starknet8.typedData.getMessageHash(typedData3, accountAddress));
|
|
3518
3676
|
}
|
|
3519
3677
|
/**
|
|
3520
3678
|
* Signs an INVOKE transaction (V3 only).
|
|
3521
3679
|
* @param {Call[]} transactions the calls of the transaction
|
|
3522
3680
|
* @param {InvocationsSignerDetails} details the details needed to hash the transaction
|
|
3523
|
-
* @returns {Promise<Signature>} the 61
|
|
3681
|
+
* @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
|
|
3682
|
+
* the account type
|
|
3524
3683
|
* @throws {Error} when the transaction is not V3
|
|
3525
3684
|
* @example
|
|
3526
3685
|
* ```typescript
|
|
@@ -3549,29 +3708,30 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3549
3708
|
* feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1,
|
|
3550
3709
|
* };
|
|
3551
3710
|
* const result = await mySigner.signTransaction(calls, details);
|
|
3552
|
-
* // result = ['0x…', …] (61 felts)
|
|
3711
|
+
* // result = ['0x…', …] (S2morrow: 61 felts)
|
|
3553
3712
|
* ```
|
|
3554
3713
|
*/
|
|
3555
3714
|
async signTransaction(transactions, details) {
|
|
3556
3715
|
assertV3(details);
|
|
3557
|
-
const compiledCalldata =
|
|
3716
|
+
const compiledCalldata = starknet8.transaction.getExecuteCalldata(
|
|
3558
3717
|
transactions,
|
|
3559
3718
|
details.cairoVersion
|
|
3560
3719
|
);
|
|
3561
|
-
const msgHash =
|
|
3720
|
+
const msgHash = starknet8.hash.calculateInvokeTransactionHash({
|
|
3562
3721
|
...details,
|
|
3563
3722
|
senderAddress: details.walletAddress,
|
|
3564
3723
|
compiledCalldata,
|
|
3565
3724
|
version: details.version,
|
|
3566
|
-
nonceDataAvailabilityMode:
|
|
3567
|
-
feeDataAvailabilityMode:
|
|
3725
|
+
nonceDataAvailabilityMode: starknet8.stark.intDAM(details.nonceDataAvailabilityMode),
|
|
3726
|
+
feeDataAvailabilityMode: starknet8.stark.intDAM(details.feeDataAvailabilityMode)
|
|
3568
3727
|
});
|
|
3569
3728
|
return this.signRaw(msgHash);
|
|
3570
3729
|
}
|
|
3571
3730
|
/**
|
|
3572
3731
|
* Signs the DEPLOY_ACCOUNT transaction of a Falcon-512 account (V3 only).
|
|
3573
3732
|
* @param {DeployAccountSignerDetails} details the details needed to hash the transaction
|
|
3574
|
-
* @returns {Promise<Signature>} the 61
|
|
3733
|
+
* @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
|
|
3734
|
+
* the account type
|
|
3575
3735
|
* @throws {Error} when the transaction is not V3
|
|
3576
3736
|
* @example
|
|
3577
3737
|
* ```typescript
|
|
@@ -3598,26 +3758,27 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3598
3758
|
* feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1,
|
|
3599
3759
|
* };
|
|
3600
3760
|
* const result = await mySigner.signDeployAccountTransaction(details);
|
|
3601
|
-
* // result = ['0x…', …] (61 felts)
|
|
3761
|
+
* // result = ['0x…', …] (S2morrow: 61 felts)
|
|
3602
3762
|
* ```
|
|
3603
3763
|
*/
|
|
3604
3764
|
async signDeployAccountTransaction(details) {
|
|
3605
3765
|
assertV3(details);
|
|
3606
|
-
const compiledConstructorCalldata =
|
|
3607
|
-
const msgHash =
|
|
3766
|
+
const compiledConstructorCalldata = starknet8.CallData.compile(details.constructorCalldata);
|
|
3767
|
+
const msgHash = starknet8.hash.calculateDeployAccountTransactionHash({
|
|
3608
3768
|
...details,
|
|
3609
3769
|
salt: details.addressSalt,
|
|
3610
3770
|
compiledConstructorCalldata,
|
|
3611
3771
|
version: details.version,
|
|
3612
|
-
nonceDataAvailabilityMode:
|
|
3613
|
-
feeDataAvailabilityMode:
|
|
3772
|
+
nonceDataAvailabilityMode: starknet8.stark.intDAM(details.nonceDataAvailabilityMode),
|
|
3773
|
+
feeDataAvailabilityMode: starknet8.stark.intDAM(details.feeDataAvailabilityMode)
|
|
3614
3774
|
});
|
|
3615
3775
|
return this.signRaw(msgHash);
|
|
3616
3776
|
}
|
|
3617
3777
|
/**
|
|
3618
3778
|
* Signs a DECLARE transaction (V3 only).
|
|
3619
3779
|
* @param {DeclareSignerDetails} details the details needed to hash the transaction
|
|
3620
|
-
* @returns {Promise<Signature>} the 61
|
|
3780
|
+
* @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
|
|
3781
|
+
* the account type
|
|
3621
3782
|
* @throws {Error} when the transaction is not V3
|
|
3622
3783
|
* @example
|
|
3623
3784
|
* ```typescript
|
|
@@ -3640,24 +3801,75 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3640
3801
|
* feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1,
|
|
3641
3802
|
* };
|
|
3642
3803
|
* const result = await mySigner.signDeclareTransaction(details);
|
|
3643
|
-
* // result = ['0x…', …] (61 felts)
|
|
3804
|
+
* // result = ['0x…', …] (S2morrow: 61 felts)
|
|
3644
3805
|
* ```
|
|
3645
3806
|
*/
|
|
3646
3807
|
async signDeclareTransaction(details) {
|
|
3647
3808
|
assertV3(details);
|
|
3648
|
-
const msgHash =
|
|
3809
|
+
const msgHash = starknet8.hash.calculateDeclareTransactionHash({
|
|
3649
3810
|
...details,
|
|
3650
3811
|
version: details.version,
|
|
3651
|
-
nonceDataAvailabilityMode:
|
|
3652
|
-
feeDataAvailabilityMode:
|
|
3812
|
+
nonceDataAvailabilityMode: starknet8.stark.intDAM(details.nonceDataAvailabilityMode),
|
|
3813
|
+
feeDataAvailabilityMode: starknet8.stark.intDAM(details.feeDataAvailabilityMode)
|
|
3653
3814
|
});
|
|
3654
3815
|
return this.signRaw(msgHash);
|
|
3655
3816
|
}
|
|
3656
3817
|
/**
|
|
3657
|
-
* Signs
|
|
3658
|
-
*
|
|
3818
|
+
* Signs the acceptance of a key change of an OpenZeppelin Falcon-512 account.
|
|
3819
|
+
*
|
|
3820
|
+
* The account changes its key when it calls its own `set_public_key(new_public_key, signature)`:
|
|
3821
|
+
* the current key signs that transaction, as any other, and the new key must sign an acceptance.
|
|
3822
|
+
* Call this method on the signer of the new key, with the key that the account holds now. The
|
|
3823
|
+
* signed hash is Poseidon('StarkNet Message', 'accept_ownership', account address,
|
|
3824
|
+
* Poseidon(current key)). It holds no chain id, nonce or expiry: an acceptance is not single-use,
|
|
3825
|
+
* and the current key must still sign each key change.
|
|
3826
|
+
* @param {BigNumberish} accountAddress the address of the account
|
|
3827
|
+
* @param {BigNumberish[]} currentPublicKey the 29 felts of the key that the account holds now
|
|
3828
|
+
* (`getPackedPublicKey()` of its current signer, or its `get_public_key`)
|
|
3829
|
+
* @returns {Promise<string[]>} the acceptance, as hex strings, in the layout of this signer: 60
|
|
3830
|
+
* felts (`'oz-hint'`) or 31 (`'oz-direct'`)
|
|
3831
|
+
* @throws {Error} for an S2morrow signer (that account cannot change its key), or when the
|
|
3832
|
+
* current key has neither 29 felts nor none, or a value is not a felt252. Also when a subclass
|
|
3833
|
+
* makes `signRaw` return something else than an array of felts
|
|
3834
|
+
* @example
|
|
3835
|
+
* ```typescript
|
|
3836
|
+
* const newSigner = new Falcon512Signer(newSecretKey, { accountType: 'oz-hint' });
|
|
3837
|
+
* const acceptance = await newSigner.signOwnershipAcceptance(
|
|
3838
|
+
* falconAccount.address,
|
|
3839
|
+
* currentSigner.getPackedPublicKey()
|
|
3840
|
+
* );
|
|
3841
|
+
* const { transaction_hash } = await falconAccount.execute(
|
|
3842
|
+
* {
|
|
3843
|
+
* contractAddress: falconAccount.address,
|
|
3844
|
+
* entrypoint: 'set_public_key',
|
|
3845
|
+
* calldata: CallData.compile({
|
|
3846
|
+
* new_public_key: newSigner.getPackedPublicKey(),
|
|
3847
|
+
* signature: acceptance,
|
|
3848
|
+
* }),
|
|
3849
|
+
* },
|
|
3850
|
+
* { skipValidate: false }
|
|
3851
|
+
* );
|
|
3852
|
+
* await myProvider.waitForTransaction(transaction_hash);
|
|
3853
|
+
* // From now on, the account checks the signatures of newSigner.
|
|
3854
|
+
* ```
|
|
3855
|
+
*/
|
|
3856
|
+
async signOwnershipAcceptance(accountAddress, currentPublicKey) {
|
|
3857
|
+
if (this.#accountType === "s2morrow") {
|
|
3858
|
+
throw new Error("Falcon512: an S2morrow account cannot change its key");
|
|
3859
|
+
}
|
|
3860
|
+
const signature = await this.signRaw(ownershipAcceptanceHash(accountAddress, currentPublicKey));
|
|
3861
|
+
if (!Array.isArray(signature)) {
|
|
3862
|
+
throw new Error("Falcon512: signRaw must return an array of felts");
|
|
3863
|
+
}
|
|
3864
|
+
return signature;
|
|
3865
|
+
}
|
|
3866
|
+
/**
|
|
3867
|
+
* Signs a hash: the last step of all the methods above, in the layout of the account type. It
|
|
3868
|
+
* stays protected, so that no public method signs an arbitrary hash. A subclass can override it,
|
|
3869
|
+
* as for every starknet.js signer.
|
|
3659
3870
|
* @param {string} msgHash the hash to sign, a felt252
|
|
3660
|
-
* @returns {Promise<Signature>} the 61
|
|
3871
|
+
* @returns {Promise<Signature>} the signature, as hex strings: 61, 60 or 31 felts, according to
|
|
3872
|
+
* the account type
|
|
3661
3873
|
* @example
|
|
3662
3874
|
* ```typescript
|
|
3663
3875
|
* class LoggedFalconSigner extends Falcon512Signer {
|
|
@@ -3671,7 +3883,15 @@ var Falcon512Signer = class _Falcon512Signer {
|
|
|
3671
3883
|
* ```
|
|
3672
3884
|
*/
|
|
3673
3885
|
async signRaw(msgHash) {
|
|
3674
|
-
|
|
3886
|
+
const value = starknet8.num.toBigInt(msgHash);
|
|
3887
|
+
switch (this.#accountType) {
|
|
3888
|
+
case "oz-hint":
|
|
3889
|
+
return signHashOz(value, this.#secretKey, this.#pkNtt, "hint");
|
|
3890
|
+
case "oz-direct":
|
|
3891
|
+
return signHashOz(value, this.#secretKey, this.#pkNtt, "direct");
|
|
3892
|
+
default:
|
|
3893
|
+
return signHash(value, this.#secretKey, this.#pkNtt);
|
|
3894
|
+
}
|
|
3675
3895
|
}
|
|
3676
3896
|
};
|
|
3677
3897
|
|
|
@@ -3688,7 +3908,7 @@ __export(falcon512_exports, {
|
|
|
3688
3908
|
});
|
|
3689
3909
|
|
|
3690
3910
|
// src/falcon/verify.ts
|
|
3691
|
-
import * as
|
|
3911
|
+
import * as starknet9 from "starknet";
|
|
3692
3912
|
function toMessageHash(value) {
|
|
3693
3913
|
try {
|
|
3694
3914
|
return parseFelt(value, "message hash");
|
|
@@ -3696,24 +3916,44 @@ function toMessageHash(value) {
|
|
|
3696
3916
|
return void 0;
|
|
3697
3917
|
}
|
|
3698
3918
|
}
|
|
3699
|
-
function
|
|
3700
|
-
|
|
3919
|
+
function toPkNtt(fullPublicKey) {
|
|
3920
|
+
if (Array.isArray(fullPublicKey)) {
|
|
3921
|
+
if (fullPublicKey.length !== PACKED_SLOTS) {
|
|
3922
|
+
throw new Error(`Falcon512: packed public key must have ${PACKED_SLOTS} felts`);
|
|
3923
|
+
}
|
|
3924
|
+
return unpackPoly(fullPublicKey.map((x, i) => parseFelt(x, `public key[${i}]`)));
|
|
3925
|
+
}
|
|
3926
|
+
return publicKeyToNtt(fullPublicKey);
|
|
3927
|
+
}
|
|
3928
|
+
function verify(signature, msgHash, fullPublicKey, options) {
|
|
3929
|
+
const accountType = parseFalconOptions(options);
|
|
3930
|
+
const pkNtt = toPkNtt(fullPublicKey);
|
|
3701
3931
|
const h = toMessageHash(msgHash);
|
|
3702
3932
|
if (h === void 0 || !Array.isArray(signature)) return false;
|
|
3703
|
-
|
|
3933
|
+
switch (accountType) {
|
|
3934
|
+
case "oz-hint":
|
|
3935
|
+
return verifyHashOz(h, signature, pkNtt, "hint");
|
|
3936
|
+
case "oz-direct":
|
|
3937
|
+
return verifyHashOz(h, signature, pkNtt, "direct");
|
|
3938
|
+
default:
|
|
3939
|
+
return verifyHash(h, signature, pkNtt);
|
|
3940
|
+
}
|
|
3704
3941
|
}
|
|
3705
|
-
function verifyMessage(message, signature, fullPublicKey,
|
|
3942
|
+
function verifyMessage(message, signature, fullPublicKey, accountAddressOrOptions, options) {
|
|
3706
3943
|
if (typeof message === "object" && message !== null) {
|
|
3707
|
-
if (
|
|
3944
|
+
if (accountAddressOrOptions === void 0) {
|
|
3708
3945
|
throw new Error("Falcon512: accountAddress is required to verify a TypedData message");
|
|
3709
3946
|
}
|
|
3947
|
+
const accountAddress = accountAddressOrOptions;
|
|
3710
3948
|
return verify(
|
|
3711
3949
|
signature,
|
|
3712
|
-
|
|
3713
|
-
fullPublicKey
|
|
3950
|
+
starknet9.typedData.getMessageHash(message, accountAddress),
|
|
3951
|
+
fullPublicKey,
|
|
3952
|
+
options
|
|
3714
3953
|
);
|
|
3715
3954
|
}
|
|
3716
|
-
|
|
3955
|
+
const hashOptions = typeof accountAddressOrOptions === "object" && accountAddressOrOptions !== null && !Array.isArray(accountAddressOrOptions) ? accountAddressOrOptions : void 0;
|
|
3956
|
+
return verify(signature, message, fullPublicKey, hashOptions);
|
|
3717
3957
|
}
|
|
3718
3958
|
export {
|
|
3719
3959
|
Falcon512Signer,
|