@webpqc/fndsa 0.0.0-stage → 0.1.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.
Files changed (70) hide show
  1. package/LICENSE +49 -0
  2. package/README.md +221 -2
  3. package/esm/mod.d.ts +6 -0
  4. package/esm/mod.d.ts.map +1 -0
  5. package/esm/mod.js +3 -0
  6. package/esm/package.json +3 -0
  7. package/esm/src/basis.d.ts +11 -0
  8. package/esm/src/basis.d.ts.map +1 -0
  9. package/esm/src/basis.js +78 -0
  10. package/esm/src/codec.d.ts +28 -0
  11. package/esm/src/codec.d.ts.map +1 -0
  12. package/esm/src/codec.js +212 -0
  13. package/esm/src/common.d.ts +49 -0
  14. package/esm/src/common.d.ts.map +1 -0
  15. package/esm/src/common.js +112 -0
  16. package/esm/src/consts.d.ts +11 -0
  17. package/esm/src/consts.d.ts.map +1 -0
  18. package/esm/src/consts.js +10 -0
  19. package/esm/src/deps.d.ts +2 -0
  20. package/esm/src/deps.d.ts.map +1 -0
  21. package/esm/src/deps.js +1 -0
  22. package/esm/src/errors.d.ts +7 -0
  23. package/esm/src/errors.d.ts.map +1 -0
  24. package/esm/src/errors.js +19 -0
  25. package/esm/src/fft.d.ts +58 -0
  26. package/esm/src/fft.d.ts.map +1 -0
  27. package/esm/src/fft.js +364 -0
  28. package/esm/src/fnDsa1024.d.ts +88 -0
  29. package/esm/src/fnDsa1024.d.ts.map +1 -0
  30. package/esm/src/fnDsa1024.js +98 -0
  31. package/esm/src/fnDsa512.d.ts +88 -0
  32. package/esm/src/fnDsa512.d.ts.map +1 -0
  33. package/esm/src/fnDsa512.js +98 -0
  34. package/esm/src/fnDsaKeyGen.d.ts +6 -0
  35. package/esm/src/fnDsaKeyGen.d.ts.map +1 -0
  36. package/esm/src/fnDsaKeyGen.js +92 -0
  37. package/esm/src/fnDsaSigner.d.ts +30 -0
  38. package/esm/src/fnDsaSigner.d.ts.map +1 -0
  39. package/esm/src/fnDsaSigner.js +211 -0
  40. package/esm/src/fnDsaVerifier.d.ts +24 -0
  41. package/esm/src/fnDsaVerifier.d.ts.map +1 -0
  42. package/esm/src/fnDsaVerifier.js +134 -0
  43. package/esm/src/interfaces.d.ts +49 -0
  44. package/esm/src/interfaces.d.ts.map +1 -0
  45. package/esm/src/interfaces.js +1 -0
  46. package/esm/src/keygen.d.ts +29 -0
  47. package/esm/src/keygen.d.ts.map +1 -0
  48. package/esm/src/keygen.js +1956 -0
  49. package/esm/src/native.d.ts +15 -0
  50. package/esm/src/native.d.ts.map +1 -0
  51. package/esm/src/native.js +16 -0
  52. package/esm/src/params.d.ts +20 -0
  53. package/esm/src/params.d.ts.map +1 -0
  54. package/esm/src/params.js +18 -0
  55. package/esm/src/rng.d.ts +22 -0
  56. package/esm/src/rng.d.ts.map +1 -0
  57. package/esm/src/rng.js +151 -0
  58. package/esm/src/sha3/keccak.d.ts +58 -0
  59. package/esm/src/sha3/keccak.d.ts.map +1 -0
  60. package/esm/src/sha3/keccak.js +500 -0
  61. package/esm/src/sign.d.ts +31 -0
  62. package/esm/src/sign.d.ts.map +1 -0
  63. package/esm/src/sign.js +718 -0
  64. package/esm/src/utils.d.ts +24 -0
  65. package/esm/src/utils.d.ts.map +1 -0
  66. package/esm/src/utils.js +51 -0
  67. package/esm/src/vrfy.d.ts +27 -0
  68. package/esm/src/vrfy.d.ts.map +1 -0
  69. package/esm/src/vrfy.js +242 -0
  70. package/package.json +49 -4
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The number of the elements which a Uint8Array really has, whatever
3
+ * `byteLength` a subclass of it reports. A length is validated with it, as
4
+ * the elements are what is copied and read afterwards.
5
+ */
6
+ export declare function byteLengthOf(value: Uint8Array): number;
7
+ /**
8
+ * Asserts that an argument is a Uint8Array. Another type such as ArrayBuffer
9
+ * has `byteLength` but no elements to read, so it would be hashed as empty.
10
+ */
11
+ export declare function assertBytes(value: unknown, name: string): void;
12
+ /**
13
+ * Copies a Uint8Array to a plain Uint8Array of its own buffer.
14
+ *
15
+ * A key or a seed is copied before anything is derived from it, and only the
16
+ * copy is used afterwards. Its length is the number of the elements which
17
+ * were copied, whatever `byteLength` a subclass of Uint8Array reports, and
18
+ * its content does not change while it is in use, even if the original is on
19
+ * a SharedArrayBuffer which another thread writes to.
20
+ */
21
+ export declare function copyBytes(value: Uint8Array): Uint8Array;
22
+ /** The Web Cryptography API of the runtime, which gives the random values. */
23
+ export declare function getCrypto(): Crypto;
24
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/src/utils.ts"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEtD;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAI9D;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAEvD;AAED,8EAA8E;AAC9E,wBAAgB,SAAS,IAAI,MAAM,CAKlC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The getter of %TypedArray%.prototype[Symbol.toStringTag], which returns the
3
+ * built-in type name of a typed array and undefined for anything else. Unlike
4
+ * `instanceof` or `constructor.name`, it also identifies a Uint8Array, or an
5
+ * instance of its subclass, which was created in another realm (an iframe or
6
+ * node:vm), and it cannot be overridden by the value.
7
+ */
8
+ const typedArrayName = /* @__PURE__ */ (() => Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get)();
9
+ /**
10
+ * The getter of %TypedArray%.prototype.byteLength. Unlike the property of the
11
+ * value, it cannot be overridden by a subclass of Uint8Array, so it gives the
12
+ * number of the elements which are really there.
13
+ */
14
+ const typedArrayByteLength =
15
+ /* @__PURE__ */ (() => Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), "byteLength").get)();
16
+ /**
17
+ * The number of the elements which a Uint8Array really has, whatever
18
+ * `byteLength` a subclass of it reports. A length is validated with it, as
19
+ * the elements are what is copied and read afterwards.
20
+ */
21
+ export function byteLengthOf(value) {
22
+ return typedArrayByteLength.call(value);
23
+ }
24
+ /**
25
+ * Asserts that an argument is a Uint8Array. Another type such as ArrayBuffer
26
+ * has `byteLength` but no elements to read, so it would be hashed as empty.
27
+ */
28
+ export function assertBytes(value, name) {
29
+ if (typedArrayName.call(value) !== "Uint8Array") {
30
+ throw new Error(`${name} must be a Uint8Array`);
31
+ }
32
+ }
33
+ /**
34
+ * Copies a Uint8Array to a plain Uint8Array of its own buffer.
35
+ *
36
+ * A key or a seed is copied before anything is derived from it, and only the
37
+ * copy is used afterwards. Its length is the number of the elements which
38
+ * were copied, whatever `byteLength` a subclass of Uint8Array reports, and
39
+ * its content does not change while it is in use, even if the original is on
40
+ * a SharedArrayBuffer which another thread writes to.
41
+ */
42
+ export function copyBytes(value) {
43
+ return new Uint8Array(value);
44
+ }
45
+ /** The Web Cryptography API of the runtime, which gives the random values. */
46
+ export function getCrypto() {
47
+ if (globalThis.crypto === undefined) {
48
+ throw new Error("Web Crypto API is not available");
49
+ }
50
+ return globalThis.crypto;
51
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Converts a public key h to the NTT and Montgomery representation, which
3
+ * {@link verifyRaw} takes.
4
+ */
5
+ export declare function toNttMonty(h: Uint16Array, logn: number): void;
6
+ /**
7
+ * Verifies a signature: computes s1 = c0 - s2 * h mod q, and returns true
8
+ * if (s1, s2) is short enough.
9
+ *
10
+ * @param c0 The hash of the message (and the nonce), as a point.
11
+ * @param s2 The signature.
12
+ * @param h The public key, in the NTT and Montgomery representation.
13
+ */
14
+ export declare function verifyRaw(c0: Uint16Array, s2: Int16Array, h: Uint16Array, logn: number): boolean;
15
+ /**
16
+ * Computes the public key h = g / f mod q. Returns false if f is not
17
+ * invertible modulo q.
18
+ */
19
+ export declare function computePublic(h: Uint16Array, f: Int8Array, g: Int8Array, logn: number): boolean;
20
+ /**
21
+ * Computes G of a private key from f, g and F, which the encoding of a
22
+ * private key has: G = g * F / f mod q, as f * G - g * F = q. Returns false
23
+ * if f is not invertible modulo q, or if a coefficient of G is out of the
24
+ * range of a private key.
25
+ */
26
+ export declare function completePrivate(G: Int8Array, f: Int8Array, g: Int8Array, F: Int8Array, logn: number): boolean;
27
+ //# sourceMappingURL=vrfy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vrfy.d.ts","sourceRoot":"","sources":["../../src/src/vrfy.ts"],"names":[],"mappings":"AAyJA;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAM7D;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CACvB,EAAE,EAAE,WAAW,EACf,EAAE,EAAE,UAAU,EACd,CAAC,EAAE,WAAW,EACd,IAAI,EAAE,MAAM,GACX,OAAO,CAkBT;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,CAAC,EAAE,WAAW,EACd,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,MAAM,GACX,OAAO,CAkBT;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,MAAM,GACX,OAAO,CAkCT"}
@@ -0,0 +1,242 @@
1
+ /*
2
+ * Arithmetic modulo q = 12289 with the NTT: signature verification, and
3
+ * the computations on a key which need no floating point.
4
+ *
5
+ * This is a port of vrfy.c of the reference implementation of Falcon
6
+ * (round 3), which is under the MIT license:
7
+ *
8
+ * Copyright (c) 2017-2019 Falcon Project
9
+ * Author: Thomas Pornin <thomas.pornin@nccgroup.com>
10
+ *
11
+ * A value modulo q is in the range 0..q-1. The Montgomery representation
12
+ * of x is x * R mod q with R = 2^16. Nothing here branches on a value, as
13
+ * some of the functions work on a private key.
14
+ */
15
+ import { isShort } from "./common.js";
16
+ import { Q } from "./consts.js";
17
+ // -1/q mod 2^16
18
+ const Q0I = 12287;
19
+ // R mod q
20
+ const R = 4091;
21
+ // R^2 mod q
22
+ const R2 = 10952;
23
+ // A primitive 2048th root of unity modulo q, and its inverse.
24
+ const G = 7;
25
+ const G_INV = 8778;
26
+ /** table[x] = R * g^brv(x) mod q, where brv reverses 10 bits. */
27
+ function mkgm(g) {
28
+ const powers = new Uint16Array(1024);
29
+ let x = R;
30
+ for (let i = 0; i < 1024; i++) {
31
+ powers[i] = x;
32
+ x = (x * g) % Q;
33
+ }
34
+ const table = new Uint16Array(1024);
35
+ for (let i = 0; i < 1024; i++) {
36
+ let brv = 0;
37
+ for (let b = 0; b < 10; b++) {
38
+ brv |= ((i >> b) & 1) << (9 - b);
39
+ }
40
+ table[i] = powers[brv];
41
+ }
42
+ return table;
43
+ }
44
+ const GMB = /* @__PURE__ */ mkgm(G);
45
+ const IGMB = /* @__PURE__ */ mkgm(G_INV);
46
+ /** Reduces a small signed value to 0..q-1. */
47
+ function mqConvSmall(x) {
48
+ return x + (Q & -(x >>> 31));
49
+ }
50
+ function mqAdd(x, y) {
51
+ const d = x + y - Q;
52
+ return d + (Q & -(d >>> 31));
53
+ }
54
+ function mqSub(x, y) {
55
+ const d = x - y;
56
+ return d + (Q & -(d >>> 31));
57
+ }
58
+ function mqRshift1(x) {
59
+ x += Q & -(x & 1);
60
+ return x >>> 1;
61
+ }
62
+ /** x * y / R mod q */
63
+ function mqMontymul(x, y) {
64
+ let z = x * y;
65
+ const w = (Math.imul(z, Q0I) & 0xffff) * Q;
66
+ z = ((z + w) >>> 16) - Q;
67
+ return z + (Q & -(z >>> 31));
68
+ }
69
+ /** x / y mod q, by y^(q-2). It is 0 if y is 0. */
70
+ function mqDiv12289(x, y) {
71
+ const y0 = mqMontymul(y, R2);
72
+ const y1 = mqMontymul(y0, y0);
73
+ const y2 = mqMontymul(y1, y0);
74
+ const y3 = mqMontymul(y2, y1);
75
+ const y4 = mqMontymul(y3, y3);
76
+ const y5 = mqMontymul(y4, y4);
77
+ const y6 = mqMontymul(y5, y5);
78
+ const y7 = mqMontymul(y6, y6);
79
+ const y8 = mqMontymul(y7, y7);
80
+ const y9 = mqMontymul(y8, y2);
81
+ const y10 = mqMontymul(y9, y8);
82
+ const y11 = mqMontymul(y10, y10);
83
+ const y12 = mqMontymul(y11, y11);
84
+ const y13 = mqMontymul(y12, y9);
85
+ const y14 = mqMontymul(y13, y13);
86
+ const y15 = mqMontymul(y14, y14);
87
+ const y16 = mqMontymul(y15, y10);
88
+ const y17 = mqMontymul(y16, y16);
89
+ const y18 = mqMontymul(y17, y0);
90
+ return mqMontymul(y18, x);
91
+ }
92
+ function mqNTT(a, logn) {
93
+ const n = 1 << logn;
94
+ let t = n;
95
+ for (let m = 1; m < n; m <<= 1) {
96
+ const ht = t >> 1;
97
+ for (let i = 0, j1 = 0; i < m; i++, j1 += t) {
98
+ const s = GMB[m + i];
99
+ const j2 = j1 + ht;
100
+ for (let j = j1; j < j2; j++) {
101
+ const u = a[j];
102
+ const v = mqMontymul(a[j + ht], s);
103
+ a[j] = mqAdd(u, v);
104
+ a[j + ht] = mqSub(u, v);
105
+ }
106
+ }
107
+ t = ht;
108
+ }
109
+ }
110
+ function mqiNTT(a, logn) {
111
+ const n = 1 << logn;
112
+ let t = 1;
113
+ let m = n;
114
+ while (m > 1) {
115
+ const hm = m >> 1;
116
+ const dt = t << 1;
117
+ for (let i = 0, j1 = 0; i < hm; i++, j1 += dt) {
118
+ const j2 = j1 + t;
119
+ const s = IGMB[hm + i];
120
+ for (let j = j1; j < j2; j++) {
121
+ const u = a[j];
122
+ const v = a[j + t];
123
+ a[j] = mqAdd(u, v);
124
+ a[j + t] = mqMontymul(mqSub(u, v), s);
125
+ }
126
+ }
127
+ t = dt;
128
+ m = hm;
129
+ }
130
+ // R / n: the multiplication below also takes the values out of the
131
+ // Montgomery representation which the table put them in.
132
+ let ni = R;
133
+ for (m = n; m > 1; m >>= 1) {
134
+ ni = mqRshift1(ni);
135
+ }
136
+ for (m = 0; m < n; m++) {
137
+ a[m] = mqMontymul(a[m], ni);
138
+ }
139
+ }
140
+ /**
141
+ * Converts a public key h to the NTT and Montgomery representation, which
142
+ * {@link verifyRaw} takes.
143
+ */
144
+ export function toNttMonty(h, logn) {
145
+ const n = 1 << logn;
146
+ mqNTT(h, logn);
147
+ for (let u = 0; u < n; u++) {
148
+ h[u] = mqMontymul(h[u], R2);
149
+ }
150
+ }
151
+ /**
152
+ * Verifies a signature: computes s1 = c0 - s2 * h mod q, and returns true
153
+ * if (s1, s2) is short enough.
154
+ *
155
+ * @param c0 The hash of the message (and the nonce), as a point.
156
+ * @param s2 The signature.
157
+ * @param h The public key, in the NTT and Montgomery representation.
158
+ */
159
+ export function verifyRaw(c0, s2, h, logn) {
160
+ const n = 1 << logn;
161
+ const tt = new Uint16Array(n);
162
+ for (let u = 0; u < n; u++) {
163
+ tt[u] = mqConvSmall(s2[u]);
164
+ }
165
+ mqNTT(tt, logn);
166
+ for (let u = 0; u < n; u++) {
167
+ tt[u] = mqMontymul(tt[u], h[u]);
168
+ }
169
+ mqiNTT(tt, logn);
170
+ const s1 = new Int16Array(n);
171
+ for (let u = 0; u < n; u++) {
172
+ // -s1 = s2 * h - c0, normalized to -q/2..q/2: its norm is the one of s1.
173
+ const w = mqSub(tt[u], c0[u]);
174
+ s1[u] = w - (Q & -(((Q >> 1) - w) >>> 31));
175
+ }
176
+ return isShort(s1, s2, logn);
177
+ }
178
+ /**
179
+ * Computes the public key h = g / f mod q. Returns false if f is not
180
+ * invertible modulo q.
181
+ */
182
+ export function computePublic(h, f, g, logn) {
183
+ const n = 1 << logn;
184
+ const tt = new Uint16Array(n);
185
+ for (let u = 0; u < n; u++) {
186
+ tt[u] = mqConvSmall(f[u]);
187
+ h[u] = mqConvSmall(g[u]);
188
+ }
189
+ mqNTT(h, logn);
190
+ mqNTT(tt, logn);
191
+ let zero = 0;
192
+ for (let u = 0; u < n; u++) {
193
+ zero |= tt[u] - 1;
194
+ h[u] = mqDiv12289(h[u], tt[u]);
195
+ }
196
+ tt.fill(0);
197
+ if ((zero >>> 31) !== 0)
198
+ return false;
199
+ mqiNTT(h, logn);
200
+ return true;
201
+ }
202
+ /**
203
+ * Computes G of a private key from f, g and F, which the encoding of a
204
+ * private key has: G = g * F / f mod q, as f * G - g * F = q. Returns false
205
+ * if f is not invertible modulo q, or if a coefficient of G is out of the
206
+ * range of a private key.
207
+ */
208
+ export function completePrivate(G, f, g, F, logn) {
209
+ const n = 1 << logn;
210
+ const t1 = new Uint16Array(n);
211
+ const t2 = new Uint16Array(n);
212
+ for (let u = 0; u < n; u++) {
213
+ t1[u] = mqConvSmall(g[u]);
214
+ t2[u] = mqConvSmall(F[u]);
215
+ }
216
+ mqNTT(t1, logn);
217
+ mqNTT(t2, logn);
218
+ for (let u = 0; u < n; u++) {
219
+ t1[u] = mqMontymul(mqMontymul(t1[u], R2), t2[u]);
220
+ }
221
+ for (let u = 0; u < n; u++) {
222
+ t2[u] = mqConvSmall(f[u]);
223
+ }
224
+ mqNTT(t2, logn);
225
+ let bad = 0;
226
+ for (let u = 0; u < n; u++) {
227
+ bad |= t2[u] - 1;
228
+ t1[u] = mqDiv12289(t1[u], t2[u]);
229
+ }
230
+ mqiNTT(t1, logn);
231
+ for (let u = 0; u < n; u++) {
232
+ const w = t1[u];
233
+ // Normalized to -q/2..q/2.
234
+ const gi = w - (Q & ~-((w - (Q >> 1)) >>> 31));
235
+ // Negative if gi is not in -127..127.
236
+ bad |= (gi + 127) | (127 - gi);
237
+ G[u] = gi;
238
+ }
239
+ t1.fill(0);
240
+ t2.fill(0);
241
+ return (bad >>> 31) === 0;
242
+ }
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@webpqc/fndsa",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "An experimental FN-DSA (Falcon) implementation written in TypeScript for various JavaScript runtimes",
5
+ "keywords": [
6
+ "fn-dsa",
7
+ "fndsa",
8
+ "fips206",
9
+ "falcon",
10
+ "signature",
11
+ "security",
12
+ "pqc",
13
+ "post-quantum"
14
+ ],
15
+ "author": "Ajitomi Daisuke",
16
+ "homepage": "https://github.com/dajiaji/webpqc/tree/main/packages/fndsa#readme",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/dajiaji/webpqc.git"
20
+ },
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/dajiaji/webpqc/issues"
24
+ },
25
+ "type": "module",
26
+ "main": "./esm/mod.js",
27
+ "module": "./esm/mod.js",
28
+ "types": "./esm/mod.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./esm/mod.d.ts",
32
+ "default": "./esm/mod.js"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "scripts": {
37
+ "test": "node test_runner.js"
38
+ },
39
+ "funding": {
40
+ "type": "github",
41
+ "url": "https://github.com/sponsors/dajiaji"
42
+ },
43
+ "sideEffects": false,
44
+ "engines": {
45
+ "node": ">=20.0.0"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "_generatedBy": "dnt@dev"
51
+ }