@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,97 @@
1
+ import { createSignerHandle, FnDsaSigner } from "./fnDsaSigner.js";
2
+ import { FnDsaVerifier } from "./fnDsaVerifier.js";
3
+ import { FN_DSA_1024 } from "./params.js";
4
+ /**
5
+ * Generates a key pair of FN-DSA-1024 (NIST security category 5).
6
+ *
7
+ * The public key is 1793 octets in length, and the private key 2305 octets.
8
+ *
9
+ * @returns A promise that resolves to a key pair [publicKey, privateKey].
10
+ *
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * import {
15
+ * createFnDsa1024Signer,
16
+ * createFnDsa1024Verifier,
17
+ * generateFnDsa1024KeyPair,
18
+ * } from "@webpqc/fndsa";
19
+ *
20
+ * const [pk, sk] = await generateFnDsa1024KeyPair();
21
+ *
22
+ * const signer = await createFnDsa1024Signer(sk);
23
+ * const msg = new TextEncoder().encode("hello world");
24
+ * const sig = await signer.sign(msg);
25
+ * signer.destroy();
26
+ *
27
+ * const verifier = await createFnDsa1024Verifier(pk);
28
+ * const ok = await verifier.verify(sig, msg);
29
+ * // ok === true
30
+ * ```
31
+ */
32
+ export function generateFnDsa1024KeyPair() {
33
+ return FnDsaSigner._generateKeyPair(FN_DSA_1024);
34
+ }
35
+ /**
36
+ * Derives a key pair of FN-DSA-1024 deterministically from a 48-octet seed.
37
+ *
38
+ * Unlike the other packages of webpqc, the private key is not the seed: it
39
+ * is the encoded private key (2305 octets), as key generation takes much
40
+ * longer than decoding a key.
41
+ *
42
+ * @param seed A 48-octet seed.
43
+ * @returns A promise that resolves to a key pair [publicKey, privateKey].
44
+ */
45
+ export function deriveFnDsa1024KeyPair(seed) {
46
+ return FnDsaSigner._deriveKeyPair(FN_DSA_1024, seed);
47
+ }
48
+ /**
49
+ * Creates an FN-DSA-1024 (NIST security category 5) signer which holds a
50
+ * private key.
51
+ *
52
+ * The signature is 1280 octets in length. Call `destroy()` of the signer to
53
+ * erase the private key which it holds, when it is no longer needed.
54
+ *
55
+ * @param sk A private key (2305 octets).
56
+ * @param opts Options: see {@link FnDsaCreateOptions}.
57
+ * @returns A promise that resolves to an {@link FnDsaSignerInterface} instance.
58
+ *
59
+ * @example
60
+ *
61
+ * ```ts
62
+ * import { createFnDsa1024Signer, generateFnDsa1024KeyPair } from "@webpqc/fndsa";
63
+ *
64
+ * const [_pk, sk] = await generateFnDsa1024KeyPair();
65
+ * const signer = await createFnDsa1024Signer(sk);
66
+ * const sig = await signer.sign(new TextEncoder().encode("hello world"));
67
+ * signer.destroy();
68
+ * ```
69
+ */
70
+ export function createFnDsa1024Signer(sk, opts) {
71
+ return createSignerHandle(FN_DSA_1024, sk, opts);
72
+ }
73
+ /**
74
+ * Creates an FN-DSA-1024 (NIST security category 5) verifier which holds a
75
+ * public key.
76
+ *
77
+ * An application which uses only this does not bundle the code for key
78
+ * generation and signing.
79
+ *
80
+ * @param pk A public key (1793 octets).
81
+ * @param opts Options: see {@link FnDsaCreateOptions}.
82
+ * @returns A promise that resolves to an {@link FnDsaVerifierInterface} instance.
83
+ *
84
+ * @example
85
+ *
86
+ * ```ts
87
+ * import { createFnDsa1024Verifier } from "@webpqc/fndsa";
88
+ *
89
+ * async function verify(pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) {
90
+ * const verifier = await createFnDsa1024Verifier(pk);
91
+ * return await verifier.verify(sig, msg);
92
+ * }
93
+ * ```
94
+ */
95
+ export function createFnDsa1024Verifier(pk, opts) {
96
+ return FnDsaVerifier._create(FN_DSA_1024, pk, opts);
97
+ }
@@ -0,0 +1,88 @@
1
+ import type { FnDsaCreateOptions, FnDsaSignerInterface, FnDsaVerifierInterface } from "./interfaces.js";
2
+ /**
3
+ * Generates a key pair of FN-DSA-512 (NIST security category 1).
4
+ *
5
+ * The public key is 897 octets in length, and the private key 1281 octets.
6
+ *
7
+ * @returns A promise that resolves to a key pair [publicKey, privateKey].
8
+ *
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * import {
13
+ * createFnDsa512Signer,
14
+ * createFnDsa512Verifier,
15
+ * generateFnDsa512KeyPair,
16
+ * } from "@webpqc/fndsa";
17
+ *
18
+ * const [pk, sk] = await generateFnDsa512KeyPair();
19
+ *
20
+ * const signer = await createFnDsa512Signer(sk);
21
+ * const msg = new TextEncoder().encode("hello world");
22
+ * const sig = await signer.sign(msg);
23
+ * signer.destroy();
24
+ *
25
+ * const verifier = await createFnDsa512Verifier(pk);
26
+ * const ok = await verifier.verify(sig, msg);
27
+ * // ok === true
28
+ * ```
29
+ */
30
+ export declare function generateFnDsa512KeyPair(): Promise<[Uint8Array, Uint8Array]>;
31
+ /**
32
+ * Derives a key pair of FN-DSA-512 deterministically from a 48-octet seed.
33
+ *
34
+ * Unlike the other packages of webpqc, the private key is not the seed: it
35
+ * is the encoded private key (1281 octets), as key generation takes much
36
+ * longer than decoding a key.
37
+ *
38
+ * @param seed A 48-octet seed.
39
+ * @returns A promise that resolves to a key pair [publicKey, privateKey].
40
+ */
41
+ export declare function deriveFnDsa512KeyPair(seed: Uint8Array): Promise<[Uint8Array, Uint8Array]>;
42
+ /**
43
+ * Creates an FN-DSA-512 (NIST security category 1) signer which holds a
44
+ * private key.
45
+ *
46
+ * The signature is 666 octets in length. Call `destroy()` of the signer to
47
+ * erase the private key which it holds, when it is no longer needed.
48
+ *
49
+ * @param sk A private key (1281 octets).
50
+ * @param opts Options: see {@link FnDsaCreateOptions}.
51
+ * @returns A promise that resolves to an {@link FnDsaSignerInterface} instance.
52
+ *
53
+ * @example
54
+ *
55
+ * ```ts
56
+ * import { createFnDsa512Signer, generateFnDsa512KeyPair } from "@webpqc/fndsa";
57
+ *
58
+ * const [_pk, sk] = await generateFnDsa512KeyPair();
59
+ * const signer = await createFnDsa512Signer(sk);
60
+ * const sig = await signer.sign(new TextEncoder().encode("hello world"));
61
+ * signer.destroy();
62
+ * ```
63
+ */
64
+ export declare function createFnDsa512Signer(sk: Uint8Array, opts?: FnDsaCreateOptions): Promise<FnDsaSignerInterface>;
65
+ /**
66
+ * Creates an FN-DSA-512 (NIST security category 1) verifier which holds a
67
+ * public key.
68
+ *
69
+ * An application which uses only this does not bundle the code for key
70
+ * generation and signing.
71
+ *
72
+ * @param pk A public key (897 octets).
73
+ * @param opts Options: see {@link FnDsaCreateOptions}.
74
+ * @returns A promise that resolves to an {@link FnDsaVerifierInterface} instance.
75
+ *
76
+ * @example
77
+ *
78
+ * ```ts
79
+ * import { createFnDsa512Verifier } from "@webpqc/fndsa";
80
+ *
81
+ * async function verify(pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) {
82
+ * const verifier = await createFnDsa512Verifier(pk);
83
+ * return await verifier.verify(sig, msg);
84
+ * }
85
+ * ```
86
+ */
87
+ export declare function createFnDsa512Verifier(pk: Uint8Array, opts?: FnDsaCreateOptions): Promise<FnDsaVerifierInterface>;
88
+ //# sourceMappingURL=fnDsa512.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fnDsa512.d.ts","sourceRoot":"","sources":["../../src/src/fnDsa512.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AAGzB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAE3E;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAEnC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,oBAAoB,CAClC,EAAE,EAAE,UAAU,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,oBAAoB,CAAC,CAE/B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,sBAAsB,CACpC,EAAE,EAAE,UAAU,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,sBAAsB,CAAC,CAEjC"}
@@ -0,0 +1,97 @@
1
+ import { createSignerHandle, FnDsaSigner } from "./fnDsaSigner.js";
2
+ import { FnDsaVerifier } from "./fnDsaVerifier.js";
3
+ import { FN_DSA_512 } from "./params.js";
4
+ /**
5
+ * Generates a key pair of FN-DSA-512 (NIST security category 1).
6
+ *
7
+ * The public key is 897 octets in length, and the private key 1281 octets.
8
+ *
9
+ * @returns A promise that resolves to a key pair [publicKey, privateKey].
10
+ *
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * import {
15
+ * createFnDsa512Signer,
16
+ * createFnDsa512Verifier,
17
+ * generateFnDsa512KeyPair,
18
+ * } from "@webpqc/fndsa";
19
+ *
20
+ * const [pk, sk] = await generateFnDsa512KeyPair();
21
+ *
22
+ * const signer = await createFnDsa512Signer(sk);
23
+ * const msg = new TextEncoder().encode("hello world");
24
+ * const sig = await signer.sign(msg);
25
+ * signer.destroy();
26
+ *
27
+ * const verifier = await createFnDsa512Verifier(pk);
28
+ * const ok = await verifier.verify(sig, msg);
29
+ * // ok === true
30
+ * ```
31
+ */
32
+ export function generateFnDsa512KeyPair() {
33
+ return FnDsaSigner._generateKeyPair(FN_DSA_512);
34
+ }
35
+ /**
36
+ * Derives a key pair of FN-DSA-512 deterministically from a 48-octet seed.
37
+ *
38
+ * Unlike the other packages of webpqc, the private key is not the seed: it
39
+ * is the encoded private key (1281 octets), as key generation takes much
40
+ * longer than decoding a key.
41
+ *
42
+ * @param seed A 48-octet seed.
43
+ * @returns A promise that resolves to a key pair [publicKey, privateKey].
44
+ */
45
+ export function deriveFnDsa512KeyPair(seed) {
46
+ return FnDsaSigner._deriveKeyPair(FN_DSA_512, seed);
47
+ }
48
+ /**
49
+ * Creates an FN-DSA-512 (NIST security category 1) signer which holds a
50
+ * private key.
51
+ *
52
+ * The signature is 666 octets in length. Call `destroy()` of the signer to
53
+ * erase the private key which it holds, when it is no longer needed.
54
+ *
55
+ * @param sk A private key (1281 octets).
56
+ * @param opts Options: see {@link FnDsaCreateOptions}.
57
+ * @returns A promise that resolves to an {@link FnDsaSignerInterface} instance.
58
+ *
59
+ * @example
60
+ *
61
+ * ```ts
62
+ * import { createFnDsa512Signer, generateFnDsa512KeyPair } from "@webpqc/fndsa";
63
+ *
64
+ * const [_pk, sk] = await generateFnDsa512KeyPair();
65
+ * const signer = await createFnDsa512Signer(sk);
66
+ * const sig = await signer.sign(new TextEncoder().encode("hello world"));
67
+ * signer.destroy();
68
+ * ```
69
+ */
70
+ export function createFnDsa512Signer(sk, opts) {
71
+ return createSignerHandle(FN_DSA_512, sk, opts);
72
+ }
73
+ /**
74
+ * Creates an FN-DSA-512 (NIST security category 1) verifier which holds a
75
+ * public key.
76
+ *
77
+ * An application which uses only this does not bundle the code for key
78
+ * generation and signing.
79
+ *
80
+ * @param pk A public key (897 octets).
81
+ * @param opts Options: see {@link FnDsaCreateOptions}.
82
+ * @returns A promise that resolves to an {@link FnDsaVerifierInterface} instance.
83
+ *
84
+ * @example
85
+ *
86
+ * ```ts
87
+ * import { createFnDsa512Verifier } from "@webpqc/fndsa";
88
+ *
89
+ * async function verify(pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) {
90
+ * const verifier = await createFnDsa512Verifier(pk);
91
+ * return await verifier.verify(sig, msg);
92
+ * }
93
+ * ```
94
+ */
95
+ export function createFnDsa512Verifier(pk, opts) {
96
+ return FnDsaVerifier._create(FN_DSA_512, pk, opts);
97
+ }
@@ -0,0 +1,44 @@
1
+ import type { FnDsaCreateOptions, FnDsaSignerInterface } from "./interfaces.js";
2
+ import type { FnDsaParams } from "./params.js";
3
+ /**
4
+ * Key generation and signing of FN-DSA.
5
+ *
6
+ * An instance holds a private key, which signs any number of messages until
7
+ * destroy() erases it. Use one of the factory functions to create an
8
+ * instance: `createFnDsa512Signer` or `createFnDsa1024Signer`.
9
+ */
10
+ export declare class FnDsaSigner implements FnDsaSignerInterface {
11
+ #private;
12
+ protected _api: Crypto;
13
+ protected _logn: number;
14
+ protected _sigSize: number;
15
+ constructor(params: FnDsaParams, api: Crypto);
16
+ /** Signs a message: returns signature. */
17
+ sign(msg: Uint8Array): Promise<Uint8Array>;
18
+ /** Erases the private key which the instance holds. */
19
+ destroy(): void;
20
+ /**
21
+ * Creates a signer which holds a private key.
22
+ * @internal
23
+ */
24
+ static _create(params: FnDsaParams, sk: Uint8Array, opts?: FnDsaCreateOptions): Promise<FnDsaSigner>;
25
+ /**
26
+ * Generates a keypair [publicKey, privateKey].
27
+ * @internal
28
+ */
29
+ static _generateKeyPair(params: FnDsaParams): Promise<[Uint8Array, Uint8Array]>;
30
+ /**
31
+ * Derives a keypair [publicKey, privateKey] from a 48-octet seed.
32
+ * @internal
33
+ */
34
+ static _deriveKeyPair(params: FnDsaParams, seed: Uint8Array): Promise<[Uint8Array, Uint8Array]>;
35
+ /**
36
+ * Key generation from a seed, which SHAKE256 expands.
37
+ */
38
+ private static _keyGen;
39
+ }
40
+ /**
41
+ * Creates a signer which holds a private key, for a factory function.
42
+ */
43
+ export declare function createSignerHandle(params: FnDsaParams, sk: Uint8Array, opts?: FnDsaCreateOptions): Promise<FnDsaSignerInterface>;
44
+ //# sourceMappingURL=fnDsaSigner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fnDsaSigner.d.ts","sourceRoot":"","sources":["../../src/src/fnDsaSigner.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAGhF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA2B/C;;;;;;GAMG;AACH,qBAAa,WAAY,YAAW,oBAAoB;;IACtD,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC;IACxB,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC;gBAWf,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM;IAO5C,0CAA0C;IAEpC,IAAI,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IA8ChD,uDAAuD;IACvD,OAAO,IAAI,IAAI;IAKf;;;OAGG;WAEU,OAAO,CAClB,MAAM,EAAE,WAAW,EACnB,EAAE,EAAE,UAAU,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,WAAW,CAAC;IAiFvB;;;OAGG;WAEU,gBAAgB,CAC3B,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IAYpC;;;OAGG;WAEU,cAAc,CACzB,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IAiBpC;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,OAAO;CA8CvB;AA6BD;;GAEG;AACH,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,WAAW,EACnB,EAAE,EAAE,UAAU,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,oBAAoB,CAAC,CAE/B"}
@@ -0,0 +1,310 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _FnDsaSigner_hasKey, _FnDsaSigner_expandedKey, _FnDsaSignerHandle_signer;
13
+ import { compEncode, MAX_FG_BITS, MAX_fg_BITS, modqEncode, trimI8Decode, trimI8Encode, } from "./codec.js";
14
+ import { hashToPoint, newShake256 } from "./common.js";
15
+ import { NONCE_LEN } from "./consts.js";
16
+ import { FnDsaError } from "./errors.js";
17
+ import { isShortBasis, keygen } from "./keygen.js";
18
+ import { assertWebCryptoOption } from "./native.js";
19
+ import { expandedKeySize, expandPrivkey, isNtruBasis, signTree, } from "./sign.js";
20
+ import { assertBytes, byteLengthOf, copyBytes } from "./utils.js";
21
+ import { completePrivate } from "./vrfy.js";
22
+ /** The size of the seed of key generation, and of signing, in octets. */
23
+ const SEED_SIZE = 48;
24
+ /** The header of a public key: 0x00 + logn. */
25
+ const PK_HEADER = 0x00;
26
+ /** The header of a private key: 0x50 + logn. */
27
+ const SK_HEADER = 0x50;
28
+ /** The header of a signature with the compressed encoding: 0x30 + logn. */
29
+ const SIG_HEADER = 0x30;
30
+ function getCrypto() {
31
+ if (globalThis.crypto === undefined) {
32
+ throw new Error("Web Crypto API is not available");
33
+ }
34
+ return globalThis.crypto;
35
+ }
36
+ /**
37
+ * Key generation and signing of FN-DSA.
38
+ *
39
+ * An instance holds a private key, which signs any number of messages until
40
+ * destroy() erases it. Use one of the factory functions to create an
41
+ * instance: `createFnDsa512Signer` or `createFnDsa1024Signer`.
42
+ */
43
+ export class FnDsaSigner {
44
+ constructor(params, api) {
45
+ Object.defineProperty(this, "_api", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "_logn", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "_sigSize", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ // The private key which the instance holds, expanded: the basis in the
64
+ // FFT representation and the LDL tree, which signing uses.
65
+ //
66
+ // They are private fields of JavaScript, not properties: what serializes
67
+ // or inspects an instance, such as JSON.stringify(), structuredClone() or
68
+ // console.log(), must not copy the private key to where destroy() cannot
69
+ // erase it.
70
+ _FnDsaSigner_hasKey.set(this, false);
71
+ _FnDsaSigner_expandedKey.set(this, void 0);
72
+ this._api = api;
73
+ this._logn = params.logn;
74
+ this._sigSize = params.sigSize;
75
+ __classPrivateFieldSet(this, _FnDsaSigner_expandedKey, new Float64Array(expandedKeySize(params.logn)), "f");
76
+ }
77
+ /** Signs a message: returns signature. */
78
+ // deno-lint-ignore require-await
79
+ async sign(msg) {
80
+ const logn = this._logn;
81
+ const n = 1 << logn;
82
+ const seed = new Uint8Array(SEED_SIZE);
83
+ const hm = new Uint16Array(n);
84
+ const s2 = new Int16Array(n);
85
+ const sc = newShake256();
86
+ try {
87
+ if (!__classPrivateFieldGet(this, _FnDsaSigner_hasKey, "f")) {
88
+ throw new Error("the signer has been destroyed");
89
+ }
90
+ assertBytes(msg, "msg");
91
+ const sig = new Uint8Array(this._sigSize);
92
+ sig[0] = SIG_HEADER + logn;
93
+ // c = HashToPoint(nonce || msg), with a fresh nonce: two signatures
94
+ // of the same c would reveal the private key.
95
+ const nonce = sig.subarray(1, 1 + NONCE_LEN);
96
+ this._api.getRandomValues(nonce);
97
+ sc.updateUnsafe(nonce).updateUnsafe(msg);
98
+ hashToPoint(sc, hm, logn);
99
+ // The seed of the sampler.
100
+ this._api.getRandomValues(seed);
101
+ sc.reset();
102
+ sc.updateUnsafe(seed);
103
+ // A signature whose encoding is longer than the fixed size, which is
104
+ // rare, is dropped, and another one is sampled.
105
+ const body = sig.subarray(1 + NONCE_LEN);
106
+ for (;;) {
107
+ signTree(s2, sc, __classPrivateFieldGet(this, _FnDsaSigner_expandedKey, "f"), hm, logn);
108
+ body.fill(0);
109
+ if (compEncode(body, s2, logn) !== 0)
110
+ break;
111
+ }
112
+ return sig;
113
+ }
114
+ catch (e) {
115
+ throw new FnDsaError(e);
116
+ }
117
+ finally {
118
+ seed.fill(0);
119
+ hm.fill(0);
120
+ s2.fill(0);
121
+ sc.reset();
122
+ }
123
+ }
124
+ /** Erases the private key which the instance holds. */
125
+ destroy() {
126
+ __classPrivateFieldSet(this, _FnDsaSigner_hasKey, false, "f");
127
+ __classPrivateFieldGet(this, _FnDsaSigner_expandedKey, "f").fill(0);
128
+ }
129
+ /**
130
+ * Creates a signer which holds a private key.
131
+ * @internal
132
+ */
133
+ // deno-lint-ignore require-await
134
+ static async _create(params, sk, opts) {
135
+ const logn = params.logn;
136
+ const n = 1 << logn;
137
+ let key;
138
+ const f = new Int8Array(n);
139
+ const g = new Int8Array(n);
140
+ const F = new Int8Array(n);
141
+ const G = new Int8Array(n);
142
+ const rt = new Float64Array(n + n + (n >> 1));
143
+ let signer;
144
+ try {
145
+ assertWebCryptoOption(opts?.useWebCrypto);
146
+ assertBytes(sk, "sk");
147
+ if (byteLengthOf(sk) !== params.skSize) {
148
+ throw new Error("invalid private key");
149
+ }
150
+ // The key is decoded from a copy, which has the length validated
151
+ // above and does not change meanwhile: see copyBytes().
152
+ key = copyBytes(sk);
153
+ if (key[0] !== SK_HEADER + logn) {
154
+ throw new Error("invalid private key");
155
+ }
156
+ const fgSize = ((n * MAX_fg_BITS[logn]) + 7) >> 3;
157
+ const FSize = ((n * MAX_FG_BITS[logn]) + 7) >> 3;
158
+ // The lengths of the three parts add up to the size of the key.
159
+ if (trimI8Decode(f, logn, MAX_fg_BITS[logn], key.subarray(1, 1 + fgSize)) === 0 ||
160
+ trimI8Decode(g, logn, MAX_fg_BITS[logn], key.subarray(1 + fgSize, 1 + 2 * fgSize)) === 0 ||
161
+ trimI8Decode(F, logn, MAX_FG_BITS[logn], key.subarray(1 + 2 * fgSize, 1 + 2 * fgSize + FSize)) === 0 ||
162
+ 1 + 2 * fgSize + FSize !== params.skSize ||
163
+ // G is not in the encoding: it is computed from f * G - g * F = q
164
+ // modulo q, which any F satisfies. A key which does not satisfy the
165
+ // equation over the integers is not a basis of the lattice, and
166
+ // signing with it may never end.
167
+ !completePrivate(G, f, g, F, logn) ||
168
+ !isNtruBasis(f, g, F, G, logn) ||
169
+ // The bounds which key generation ensures, and which the sampler
170
+ // of signing relies on.
171
+ !isShortBasis(f, g, logn, rt.subarray(0, n), rt.subarray(n, 2 * n), rt.subarray(2 * n))) {
172
+ throw new Error("invalid private key");
173
+ }
174
+ signer = new FnDsaSigner(params, getCrypto());
175
+ expandPrivkey(__classPrivateFieldGet(signer, _FnDsaSigner_expandedKey, "f"), f, g, F, G, logn);
176
+ __classPrivateFieldSet(signer, _FnDsaSigner_hasKey, true, "f");
177
+ return signer;
178
+ }
179
+ catch (e) {
180
+ signer?.destroy();
181
+ throw new FnDsaError(e);
182
+ }
183
+ finally {
184
+ key?.fill(0);
185
+ f.fill(0);
186
+ g.fill(0);
187
+ F.fill(0);
188
+ G.fill(0);
189
+ rt.fill(0);
190
+ }
191
+ }
192
+ /**
193
+ * Generates a keypair [publicKey, privateKey].
194
+ * @internal
195
+ */
196
+ // deno-lint-ignore require-await
197
+ static async _generateKeyPair(params) {
198
+ const seed = new Uint8Array(SEED_SIZE);
199
+ try {
200
+ getCrypto().getRandomValues(seed);
201
+ return FnDsaSigner._keyGen(params, seed);
202
+ }
203
+ catch (e) {
204
+ throw new FnDsaError(e);
205
+ }
206
+ finally {
207
+ seed.fill(0);
208
+ }
209
+ }
210
+ /**
211
+ * Derives a keypair [publicKey, privateKey] from a 48-octet seed.
212
+ * @internal
213
+ */
214
+ // deno-lint-ignore require-await
215
+ static async _deriveKeyPair(params, seed) {
216
+ let copy;
217
+ try {
218
+ assertBytes(seed, "seed");
219
+ if (byteLengthOf(seed) !== SEED_SIZE) {
220
+ throw new Error("seed must be 48 bytes in length");
221
+ }
222
+ // See copyBytes().
223
+ copy = copyBytes(seed);
224
+ return FnDsaSigner._keyGen(params, copy);
225
+ }
226
+ catch (e) {
227
+ throw new FnDsaError(e);
228
+ }
229
+ finally {
230
+ copy?.fill(0);
231
+ }
232
+ }
233
+ /**
234
+ * Key generation from a seed, which SHAKE256 expands.
235
+ */
236
+ static _keyGen(params, seed) {
237
+ const logn = params.logn;
238
+ const n = 1 << logn;
239
+ const f = new Int8Array(n);
240
+ const g = new Int8Array(n);
241
+ const F = new Int8Array(n);
242
+ const h = new Uint16Array(n);
243
+ const rng = newShake256();
244
+ const sk = new Uint8Array(params.skSize);
245
+ let ok = false;
246
+ try {
247
+ rng.updateUnsafe(seed);
248
+ keygen(rng, f, g, F, null, h, logn);
249
+ sk[0] = SK_HEADER + logn;
250
+ let u = 1;
251
+ for (const [x, bits] of [[f, MAX_fg_BITS], [g, MAX_fg_BITS]]) {
252
+ const v = trimI8Encode(sk.subarray(u), x, logn, bits[logn]);
253
+ if (v === 0) {
254
+ throw new Error("key generation failed");
255
+ }
256
+ u += v;
257
+ }
258
+ const v = trimI8Encode(sk.subarray(u), F, logn, MAX_FG_BITS[logn]);
259
+ if (v === 0 || u + v !== params.skSize) {
260
+ throw new Error("key generation failed");
261
+ }
262
+ const pk = new Uint8Array(params.pkSize);
263
+ pk[0] = PK_HEADER + logn;
264
+ if (modqEncode(pk.subarray(1), h, logn) !== params.pkSize - 1) {
265
+ throw new Error("key generation failed");
266
+ }
267
+ ok = true;
268
+ return [pk, sk];
269
+ }
270
+ finally {
271
+ if (!ok)
272
+ sk.fill(0);
273
+ f.fill(0);
274
+ g.fill(0);
275
+ F.fill(0);
276
+ rng.reset();
277
+ }
278
+ }
279
+ }
280
+ _FnDsaSigner_hasKey = new WeakMap(), _FnDsaSigner_expandedKey = new WeakMap();
281
+ /**
282
+ * What the factory functions return: a signer of which nothing but sign()
283
+ * and destroy() can be reached.
284
+ *
285
+ * The implementation is in a private field of JavaScript. Its methods and
286
+ * its work areas, which handle the private key, are properties which who
287
+ * holds the instance could read, call or replace: a signer may be given to
288
+ * a party which is to sign, and not to have the private key.
289
+ */
290
+ class FnDsaSignerHandle {
291
+ constructor(signer) {
292
+ _FnDsaSignerHandle_signer.set(this, void 0);
293
+ __classPrivateFieldSet(this, _FnDsaSignerHandle_signer, signer, "f");
294
+ }
295
+ /** Signs a message: returns signature. */
296
+ sign(msg) {
297
+ return __classPrivateFieldGet(this, _FnDsaSignerHandle_signer, "f").sign(msg);
298
+ }
299
+ /** Erases the private key which the instance holds. */
300
+ destroy() {
301
+ __classPrivateFieldGet(this, _FnDsaSignerHandle_signer, "f").destroy();
302
+ }
303
+ }
304
+ _FnDsaSignerHandle_signer = new WeakMap();
305
+ /**
306
+ * Creates a signer which holds a private key, for a factory function.
307
+ */
308
+ export async function createSignerHandle(params, sk, opts) {
309
+ return new FnDsaSignerHandle(await FnDsaSigner._create(params, sk, opts));
310
+ }
@@ -0,0 +1,24 @@
1
+ import type { FnDsaCreateOptions, FnDsaVerifierInterface } from "./interfaces.js";
2
+ import type { FnDsaParams } from "./params.js";
3
+ /**
4
+ * Verification of FN-DSA.
5
+ *
6
+ * An instance holds a public key, which verifies any number of signatures.
7
+ * Use one of the factory functions to create an instance:
8
+ * `createFnDsa512Verifier` or `createFnDsa1024Verifier`.
9
+ */
10
+ export declare class FnDsaVerifier implements FnDsaVerifierInterface {
11
+ protected _logn: number;
12
+ protected _sigSize: number;
13
+ protected _sigMaxSize: number;
14
+ protected _h: Uint16Array;
15
+ constructor(params: FnDsaParams);
16
+ /** Verifies a signature: resolves to true if the signature is valid. */
17
+ verify(sig: Uint8Array, msg: Uint8Array): Promise<boolean>;
18
+ /**
19
+ * Creates a verifier which holds a public key.
20
+ * @internal
21
+ */
22
+ static _create(params: FnDsaParams, pk: Uint8Array, opts?: FnDsaCreateOptions): Promise<FnDsaVerifier>;
23
+ }
24
+ //# sourceMappingURL=fnDsaVerifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fnDsaVerifier.d.ts","sourceRoot":"","sources":["../../src/src/fnDsaVerifier.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,kBAAkB,EAClB,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAS/C;;;;;;GAMG;AACH,qBAAa,aAAc,YAAW,sBAAsB;IAC1D,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC;IACxB,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC;IAI3B,SAAS,CAAC,WAAW,EAAE,MAAM,CAAC;IAG9B,SAAS,CAAC,EAAE,EAAE,WAAW,CAAC;gBAEd,MAAM,EAAE,WAAW;IAQ/B,wEAAwE;IAElE,MAAM,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC;IAsDhE;;;OAGG;WAEU,OAAO,CAClB,MAAM,EAAE,WAAW,EACnB,EAAE,EAAE,UAAU,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,aAAa,CAAC;CAwB1B"}