@webpqc/fndsa 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/LICENSE +49 -0
  2. package/README.md +220 -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/codec.d.ts +28 -0
  8. package/esm/src/codec.d.ts.map +1 -0
  9. package/esm/src/codec.js +212 -0
  10. package/esm/src/common.d.ts +49 -0
  11. package/esm/src/common.d.ts.map +1 -0
  12. package/esm/src/common.js +112 -0
  13. package/esm/src/consts.d.ts +5 -0
  14. package/esm/src/consts.d.ts.map +1 -0
  15. package/esm/src/consts.js +4 -0
  16. package/esm/src/deps.d.ts +2 -0
  17. package/esm/src/deps.d.ts.map +1 -0
  18. package/esm/src/deps.js +1 -0
  19. package/esm/src/errors.d.ts +7 -0
  20. package/esm/src/errors.d.ts.map +1 -0
  21. package/esm/src/errors.js +19 -0
  22. package/esm/src/fft.d.ts +53 -0
  23. package/esm/src/fft.d.ts.map +1 -0
  24. package/esm/src/fft.js +329 -0
  25. package/esm/src/fnDsa1024.d.ts +88 -0
  26. package/esm/src/fnDsa1024.d.ts.map +1 -0
  27. package/esm/src/fnDsa1024.js +97 -0
  28. package/esm/src/fnDsa512.d.ts +88 -0
  29. package/esm/src/fnDsa512.d.ts.map +1 -0
  30. package/esm/src/fnDsa512.js +97 -0
  31. package/esm/src/fnDsaSigner.d.ts +44 -0
  32. package/esm/src/fnDsaSigner.d.ts.map +1 -0
  33. package/esm/src/fnDsaSigner.js +310 -0
  34. package/esm/src/fnDsaVerifier.d.ts +24 -0
  35. package/esm/src/fnDsaVerifier.d.ts.map +1 -0
  36. package/esm/src/fnDsaVerifier.js +136 -0
  37. package/esm/src/interfaces.d.ts +49 -0
  38. package/esm/src/interfaces.d.ts.map +1 -0
  39. package/esm/src/interfaces.js +1 -0
  40. package/esm/src/keygen.d.ts +34 -0
  41. package/esm/src/keygen.d.ts.map +1 -0
  42. package/esm/src/keygen.js +2450 -0
  43. package/esm/src/native.d.ts +15 -0
  44. package/esm/src/native.d.ts.map +1 -0
  45. package/esm/src/native.js +16 -0
  46. package/esm/src/params.d.ts +20 -0
  47. package/esm/src/params.d.ts.map +1 -0
  48. package/esm/src/params.js +18 -0
  49. package/esm/src/rng.d.ts +22 -0
  50. package/esm/src/rng.d.ts.map +1 -0
  51. package/esm/src/rng.js +151 -0
  52. package/esm/src/sha3/keccak.d.ts +58 -0
  53. package/esm/src/sha3/keccak.d.ts.map +1 -0
  54. package/esm/src/sha3/keccak.js +500 -0
  55. package/esm/src/sign.d.ts +31 -0
  56. package/esm/src/sign.d.ts.map +1 -0
  57. package/esm/src/sign.js +718 -0
  58. package/esm/src/utils.d.ts +22 -0
  59. package/esm/src/utils.d.ts.map +1 -0
  60. package/esm/src/utils.js +44 -0
  61. package/esm/src/vrfy.d.ts +27 -0
  62. package/esm/src/vrfy.d.ts.map +1 -0
  63. package/esm/src/vrfy.js +242 -0
  64. package/package.json +49 -4
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The option `useWebCrypto`, which is the one of `@webpqc/mldsa`.
3
+ *
4
+ * Nothing specifies FN-DSA in the Web Cryptography API so far: it is not
5
+ * among the algorithms of WICG webcrypto-modern-algos, as FIPS 206 is not
6
+ * published, and no runtime has it. There is therefore nothing to delegate
7
+ * to yet, and this implementation always runs. The option is taken and
8
+ * validated all the same, so that an application need not change when
9
+ * delegation is added.
10
+ */
11
+ /** See `FnDsaCreateOptions.useWebCrypto`. */
12
+ export type WebCryptoOption = boolean | "auto";
13
+ /** @internal */
14
+ export declare function assertWebCryptoOption(option: WebCryptoOption | undefined): void;
15
+ //# sourceMappingURL=native.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../../src/src/native.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,6CAA6C;AAC7C,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,MAAM,CAAC;AAE/C,gBAAgB;AAChB,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,eAAe,GAAG,SAAS,GAClC,IAAI,CAIN"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The option `useWebCrypto`, which is the one of `@webpqc/mldsa`.
3
+ *
4
+ * Nothing specifies FN-DSA in the Web Cryptography API so far: it is not
5
+ * among the algorithms of WICG webcrypto-modern-algos, as FIPS 206 is not
6
+ * published, and no runtime has it. There is therefore nothing to delegate
7
+ * to yet, and this implementation always runs. The option is taken and
8
+ * validated all the same, so that an application need not change when
9
+ * delegation is added.
10
+ */
11
+ /** @internal */
12
+ export function assertWebCryptoOption(option) {
13
+ if (option !== true && option !== false && (option ?? "auto") !== "auto") {
14
+ throw new Error('useWebCrypto must be a boolean or "auto"');
15
+ }
16
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * A parameter set of FN-DSA.
3
+ */
4
+ export interface FnDsaParams {
5
+ /** The name of the algorithm. */
6
+ readonly name: string;
7
+ /** The degree n of the ring is 2^logn. */
8
+ readonly logn: number;
9
+ /** The size of a public key in octets. */
10
+ readonly pkSize: number;
11
+ /** The size of a private key in octets. */
12
+ readonly skSize: number;
13
+ /** The size of a signature in octets. */
14
+ readonly sigSize: number;
15
+ }
16
+ /** FN-DSA-512 (NIST security category 1). */
17
+ export declare const FN_DSA_512: FnDsaParams;
18
+ /** FN-DSA-1024 (NIST security category 5). */
19
+ export declare const FN_DSA_1024: FnDsaParams;
20
+ //# sourceMappingURL=params.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"params.d.ts","sourceRoot":"","sources":["../../src/src/params.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0CAA0C;IAC1C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2CAA2C;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,yCAAyC;IACzC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,6CAA6C;AAC7C,eAAO,MAAM,UAAU,EAAE,WAMxB,CAAC;AAEF,8CAA8C;AAC9C,eAAO,MAAM,WAAW,EAAE,WAMzB,CAAC"}
@@ -0,0 +1,18 @@
1
+ // The parameter sets are plain literals, with no expression to evaluate, so
2
+ // that a bundler drops the ones which an application does not use.
3
+ /** FN-DSA-512 (NIST security category 1). */
4
+ export const FN_DSA_512 = {
5
+ name: "FN-DSA-512",
6
+ logn: 9,
7
+ pkSize: 897,
8
+ skSize: 1281,
9
+ sigSize: 666,
10
+ };
11
+ /** FN-DSA-1024 (NIST security category 5). */
12
+ export const FN_DSA_1024 = {
13
+ name: "FN-DSA-1024",
14
+ logn: 10,
15
+ pkSize: 1793,
16
+ skSize: 2305,
17
+ sigSize: 1280,
18
+ };
@@ -0,0 +1,22 @@
1
+ import type { Keccak } from "./deps.js";
2
+ /** @internal */
3
+ export declare class Prng {
4
+ private _buf;
5
+ private _ptr;
6
+ /** The key (8 words), the nonce (4 words) and the block counter. */
7
+ private _state;
8
+ private _w;
9
+ /** @param src A SHAKE256 instance which is squeezed for the seed. */
10
+ constructor(src: Keccak);
11
+ /** Erases the state. */
12
+ destroy(): void;
13
+ private _refill;
14
+ /**
15
+ * Draws 8 octets, and returns the low 24 bits, the next 24 bits and the
16
+ * high 16 bits of the little-endian 64-bit value in `out`.
17
+ */
18
+ getU64(out: Uint32Array): void;
19
+ /** Draws an octet. */
20
+ getU8(): number;
21
+ }
22
+ //# sourceMappingURL=rng.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rng.d.ts","sourceRoot":"","sources":["../../src/src/rng.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAIxC,gBAAgB;AAChB,qBAAa,IAAI;IACf,OAAO,CAAC,IAAI,CAA2B;IACvC,OAAO,CAAC,IAAI,CAAK;IACjB,oEAAoE;IACpE,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,EAAE,CAAuB;IAEjC,qEAAqE;gBACzD,GAAG,EAAE,MAAM;IAWvB,wBAAwB;IACxB,OAAO,IAAI,IAAI;IAMf,OAAO,CAAC,OAAO;IAuDf;;;OAGG;IACH,MAAM,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI;IAc9B,sBAAsB;IACtB,KAAK,IAAI,MAAM;CAOhB"}
package/esm/src/rng.js ADDED
@@ -0,0 +1,151 @@
1
+ /*
2
+ * The pseudorandom generator of the sampler: ChaCha20, seeded from SHAKE256.
3
+ *
4
+ * This is a port of rng.c of the reference implementation of Falcon
5
+ * (round 3), which is under the MIT license:
6
+ *
7
+ * Copyright (c) 2017-2019 Falcon Project
8
+ * Author: Thomas Pornin <thomas.pornin@nccgroup.com>
9
+ *
10
+ * It is the generator which the Known Answer Test values are defined with.
11
+ */
12
+ const BUF_LEN = 512;
13
+ /** @internal */
14
+ export class Prng {
15
+ /** @param src A SHAKE256 instance which is squeezed for the seed. */
16
+ constructor(src) {
17
+ Object.defineProperty(this, "_buf", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: new Uint8Array(BUF_LEN)
22
+ });
23
+ Object.defineProperty(this, "_ptr", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: 0
28
+ });
29
+ /** The key (8 words), the nonce (4 words) and the block counter. */
30
+ Object.defineProperty(this, "_state", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: new Uint32Array(14)
35
+ });
36
+ Object.defineProperty(this, "_w", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: new Uint32Array(16)
41
+ });
42
+ const tmp = new Uint8Array(56);
43
+ src.writeIntoUnsafe(tmp);
44
+ const view = new DataView(tmp.buffer);
45
+ for (let i = 0; i < 14; i++) {
46
+ this._state[i] = view.getUint32(i << 2, true);
47
+ }
48
+ tmp.fill(0);
49
+ this._refill();
50
+ }
51
+ /** Erases the state. */
52
+ destroy() {
53
+ this._buf.fill(0);
54
+ this._state.fill(0);
55
+ this._w.fill(0);
56
+ }
57
+ _refill() {
58
+ const st = this._state;
59
+ const w = this._w;
60
+ const buf = this._buf;
61
+ // The 64-bit block counter.
62
+ let ccLo = st[12];
63
+ let ccHi = st[13];
64
+ for (let u = 0; u < 8; u++) {
65
+ w[0] = 0x61707865;
66
+ w[1] = 0x3320646e;
67
+ w[2] = 0x79622d32;
68
+ w[3] = 0x6b206574;
69
+ for (let v = 0; v < 12; v++) {
70
+ w[v + 4] = st[v];
71
+ }
72
+ w[14] ^= ccLo;
73
+ w[15] ^= ccHi;
74
+ for (let i = 0; i < 10; i++) {
75
+ qround(w, 0, 4, 8, 12);
76
+ qround(w, 1, 5, 9, 13);
77
+ qround(w, 2, 6, 10, 14);
78
+ qround(w, 3, 7, 11, 15);
79
+ qround(w, 0, 5, 10, 15);
80
+ qround(w, 1, 6, 11, 12);
81
+ qround(w, 2, 7, 8, 13);
82
+ qround(w, 3, 4, 9, 14);
83
+ }
84
+ w[0] += 0x61707865;
85
+ w[1] += 0x3320646e;
86
+ w[2] += 0x79622d32;
87
+ w[3] += 0x6b206574;
88
+ for (let v = 4; v < 14; v++) {
89
+ w[v] += st[v - 4];
90
+ }
91
+ w[14] += st[10] ^ ccLo;
92
+ w[15] += st[11] ^ ccHi;
93
+ ccLo = (ccLo + 1) >>> 0;
94
+ if (ccLo === 0) {
95
+ ccHi = (ccHi + 1) >>> 0;
96
+ }
97
+ // The eight blocks are interleaved, word by word.
98
+ for (let v = 0; v < 16; v++) {
99
+ const x = w[v];
100
+ const o = (u << 2) + (v << 5);
101
+ buf[o] = x;
102
+ buf[o + 1] = x >>> 8;
103
+ buf[o + 2] = x >>> 16;
104
+ buf[o + 3] = x >>> 24;
105
+ }
106
+ }
107
+ st[12] = ccLo;
108
+ st[13] = ccHi;
109
+ this._ptr = 0;
110
+ }
111
+ /**
112
+ * Draws 8 octets, and returns the low 24 bits, the next 24 bits and the
113
+ * high 16 bits of the little-endian 64-bit value in `out`.
114
+ */
115
+ getU64(out) {
116
+ let u = this._ptr;
117
+ // The rest of the buffer is dropped if less than 9 octets remain.
118
+ if (u >= BUF_LEN - 9) {
119
+ this._refill();
120
+ u = 0;
121
+ }
122
+ this._ptr = u + 8;
123
+ const buf = this._buf;
124
+ out[0] = buf[u] | (buf[u + 1] << 8) | (buf[u + 2] << 16);
125
+ out[1] = buf[u + 3] | (buf[u + 4] << 8) | (buf[u + 5] << 16);
126
+ out[2] = buf[u + 6] | (buf[u + 7] << 8);
127
+ }
128
+ /** Draws an octet. */
129
+ getU8() {
130
+ const v = this._buf[this._ptr++];
131
+ if (this._ptr === BUF_LEN) {
132
+ this._refill();
133
+ }
134
+ return v;
135
+ }
136
+ }
137
+ function qround(s, a, b, c, d) {
138
+ let x;
139
+ s[a] += s[b];
140
+ x = s[d] ^ s[a];
141
+ s[d] = (x << 16) | (x >>> 16);
142
+ s[c] += s[d];
143
+ x = s[b] ^ s[c];
144
+ s[b] = (x << 12) | (x >>> 20);
145
+ s[a] += s[b];
146
+ x = s[d] ^ s[a];
147
+ s[d] = (x << 8) | (x >>> 24);
148
+ s[c] += s[d];
149
+ x = s[b] ^ s[c];
150
+ s[b] = (x << 7) | (x >>> 25);
151
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * `keccakf1600` internal function, additionally allows to adjust round count,
3
+ * which must be even.
4
+ *
5
+ * The state is 25 lanes of 64 bits, A[x + 5y], each of which is two 32-bit
6
+ * words of `s`: the low one at 2(x + 5y), and the high one next to it (a
7
+ * BigInt is too slow in JavaScript).
8
+ *
9
+ * A round reads the state from one array and writes the next state to
10
+ * another one, so that each word is read twice and written once in a round,
11
+ * and nothing is updated in place. The rounds are therefore done in pairs:
12
+ * from `s` to KECCAK_T, and back to `s`. All the intermediate values are
13
+ * local variables. This is several times faster on some JavaScript engines
14
+ * than updating one array in place with a scratch array.
15
+ *
16
+ * A round is written out, as follows.
17
+ *
18
+ * - Theta: `c<x>` is the parity of the column x, and `d<x>` is what is
19
+ * added to its lanes.
20
+ * - Rho and Pi: `b<x>` is the lane which moves to the column x of a row,
21
+ * with `d` added, rotated. They are computed for a row at a time.
22
+ * - Chi and Iota: the row of the next state is written, with the round
23
+ * constant added to the lane 0.
24
+ *
25
+ * There is no branch and no table lookup which depends on the state.
26
+ */
27
+ export declare function keccakP(s: Uint32Array, rounds?: number): void;
28
+ /**
29
+ * A minimal Keccak sponge: absorb with `updateUnsafe()`, squeeze with
30
+ * `writeIntoUnsafe()` (which can be called repeatedly), and `reset()` to
31
+ * reuse the instance.
32
+ *
33
+ * Nothing is validated: the caller must pass Uint8Array, and must not absorb
34
+ * after squeezing without `reset()`. See `Keccak` in `sha3.ts` for the
35
+ * validating hash interface built on this.
36
+ */
37
+ export declare class KeccakCore {
38
+ protected state: Uint8Array;
39
+ protected pos: number;
40
+ protected posOut: number;
41
+ protected finished: boolean;
42
+ protected state32: Uint32Array;
43
+ blockLen: number;
44
+ suffix: number;
45
+ outputLen: number;
46
+ protected enableXOF: boolean;
47
+ protected rounds: number;
48
+ constructor(blockLen: number, suffix: number, outputLen: number, enableXOF?: boolean, rounds?: number);
49
+ /** Resets instance to initial (empty) state for reuse. */
50
+ reset(): void;
51
+ protected keccak(): void;
52
+ /** Absorbs data. Caller must ensure valid state and input. */
53
+ updateUnsafe(data: Uint8Array): this;
54
+ protected finish(): void;
55
+ /** Squeezes into `out`. Caller must ensure valid state and output. */
56
+ writeIntoUnsafe(out: Uint8Array): Uint8Array;
57
+ }
58
+ //# sourceMappingURL=keccak.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keccak.d.ts","sourceRoot":"","sources":["../../../src/src/sha3/keccak.ts"],"names":[],"mappings":"AA0DA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,WAAW,EAAE,MAAM,GAAE,MAAW,GAAG,IAAI,CAmSjE;AAmBD;;;;;;;;GAQG;AACH,qBAAa,UAAU;IACrB,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC;IAC5B,SAAS,CAAC,GAAG,SAAK;IAClB,SAAS,CAAC,MAAM,SAAK;IACrB,SAAS,CAAC,QAAQ,UAAS;IAC3B,SAAS,CAAC,OAAO,EAAE,WAAW,CAAC;IAExB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,SAAS,EAAE,OAAO,CAAC;IAC7B,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;gBAIvB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,SAAS,UAAQ,EACjB,MAAM,GAAE,MAAW;IAWrB,0DAA0D;IAC1D,KAAK,IAAI,IAAI;IAMb,SAAS,CAAC,MAAM,IAAI,IAAI;IAOxB,8DAA8D;IAC9D,YAAY,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAUpC,SAAS,CAAC,MAAM,IAAI,IAAI;IAUxB,sEAAsE;IACtE,eAAe,CAAC,GAAG,EAAE,UAAU,GAAG,UAAU;CAa7C"}