@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.
- package/LICENSE +49 -0
- package/README.md +220 -2
- package/esm/mod.d.ts +6 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +3 -0
- package/esm/package.json +3 -0
- package/esm/src/codec.d.ts +28 -0
- package/esm/src/codec.d.ts.map +1 -0
- package/esm/src/codec.js +212 -0
- package/esm/src/common.d.ts +49 -0
- package/esm/src/common.d.ts.map +1 -0
- package/esm/src/common.js +112 -0
- package/esm/src/consts.d.ts +5 -0
- package/esm/src/consts.d.ts.map +1 -0
- package/esm/src/consts.js +4 -0
- package/esm/src/deps.d.ts +2 -0
- package/esm/src/deps.d.ts.map +1 -0
- package/esm/src/deps.js +1 -0
- package/esm/src/errors.d.ts +7 -0
- package/esm/src/errors.d.ts.map +1 -0
- package/esm/src/errors.js +19 -0
- package/esm/src/fft.d.ts +53 -0
- package/esm/src/fft.d.ts.map +1 -0
- package/esm/src/fft.js +329 -0
- package/esm/src/fnDsa1024.d.ts +88 -0
- package/esm/src/fnDsa1024.d.ts.map +1 -0
- package/esm/src/fnDsa1024.js +97 -0
- package/esm/src/fnDsa512.d.ts +88 -0
- package/esm/src/fnDsa512.d.ts.map +1 -0
- package/esm/src/fnDsa512.js +97 -0
- package/esm/src/fnDsaSigner.d.ts +44 -0
- package/esm/src/fnDsaSigner.d.ts.map +1 -0
- package/esm/src/fnDsaSigner.js +310 -0
- package/esm/src/fnDsaVerifier.d.ts +24 -0
- package/esm/src/fnDsaVerifier.d.ts.map +1 -0
- package/esm/src/fnDsaVerifier.js +136 -0
- package/esm/src/interfaces.d.ts +49 -0
- package/esm/src/interfaces.d.ts.map +1 -0
- package/esm/src/interfaces.js +1 -0
- package/esm/src/keygen.d.ts +34 -0
- package/esm/src/keygen.d.ts.map +1 -0
- package/esm/src/keygen.js +2450 -0
- package/esm/src/native.d.ts +15 -0
- package/esm/src/native.d.ts.map +1 -0
- package/esm/src/native.js +16 -0
- package/esm/src/params.d.ts +20 -0
- package/esm/src/params.d.ts.map +1 -0
- package/esm/src/params.js +18 -0
- package/esm/src/rng.d.ts +22 -0
- package/esm/src/rng.d.ts.map +1 -0
- package/esm/src/rng.js +151 -0
- package/esm/src/sha3/keccak.d.ts +58 -0
- package/esm/src/sha3/keccak.d.ts.map +1 -0
- package/esm/src/sha3/keccak.js +500 -0
- package/esm/src/sign.d.ts +31 -0
- package/esm/src/sign.d.ts.map +1 -0
- package/esm/src/sign.js +718 -0
- package/esm/src/utils.d.ts +22 -0
- package/esm/src/utils.d.ts.map +1 -0
- package/esm/src/utils.js +44 -0
- package/esm/src/vrfy.d.ts +27 -0
- package/esm/src/vrfy.d.ts.map +1 -0
- package/esm/src/vrfy.js +242 -0
- package/package.json +49 -4
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { compDecode, modqDecode } from "./codec.js";
|
|
2
|
+
import { hashToPoint, newShake256 } from "./common.js";
|
|
3
|
+
import { NONCE_LEN } from "./consts.js";
|
|
4
|
+
import { FnDsaError } from "./errors.js";
|
|
5
|
+
import { assertWebCryptoOption } from "./native.js";
|
|
6
|
+
import { assertBytes, byteLengthOf, copyBytes } from "./utils.js";
|
|
7
|
+
import { toNttMonty, verifyRaw } from "./vrfy.js";
|
|
8
|
+
/** The header of a public key: 0x00 + logn. */
|
|
9
|
+
const PK_HEADER = 0x00;
|
|
10
|
+
/** The header of a signature with the compressed encoding: 0x30 + logn. */
|
|
11
|
+
const SIG_HEADER = 0x30;
|
|
12
|
+
/**
|
|
13
|
+
* Verification of FN-DSA.
|
|
14
|
+
*
|
|
15
|
+
* An instance holds a public key, which verifies any number of signatures.
|
|
16
|
+
* Use one of the factory functions to create an instance:
|
|
17
|
+
* `createFnDsa512Verifier` or `createFnDsa1024Verifier`.
|
|
18
|
+
*/
|
|
19
|
+
export class FnDsaVerifier {
|
|
20
|
+
constructor(params) {
|
|
21
|
+
Object.defineProperty(this, "_logn", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "_sigSize", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
// The maximum size of a signature in the compressed format, which is
|
|
34
|
+
// longer than the fixed size: FALCON_SIG_COMPRESSED_MAXSIZE of the
|
|
35
|
+
// reference implementation (752 and 1462 octets).
|
|
36
|
+
Object.defineProperty(this, "_sigMaxSize", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: void 0
|
|
41
|
+
});
|
|
42
|
+
// The public key h which the instance holds, in the NTT and Montgomery
|
|
43
|
+
// representation.
|
|
44
|
+
Object.defineProperty(this, "_h", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
this._logn = params.logn;
|
|
51
|
+
this._sigSize = params.sigSize;
|
|
52
|
+
this._sigMaxSize =
|
|
53
|
+
((((11 << params.logn) + (101 >> (10 - params.logn))) + 7) >> 3) + 41;
|
|
54
|
+
this._h = new Uint16Array(1 << params.logn);
|
|
55
|
+
}
|
|
56
|
+
/** Verifies a signature: resolves to true if the signature is valid. */
|
|
57
|
+
// deno-lint-ignore require-await
|
|
58
|
+
async verify(sig, msg) {
|
|
59
|
+
try {
|
|
60
|
+
assertBytes(sig, "sig");
|
|
61
|
+
assertBytes(msg, "msg");
|
|
62
|
+
const size = byteLengthOf(sig);
|
|
63
|
+
// A signature is the header, the nonce and the encoded s2, which is
|
|
64
|
+
// of a variable length, and may be padded with zeros to the fixed
|
|
65
|
+
// size. What is longer than any signature is rejected before it is
|
|
66
|
+
// copied.
|
|
67
|
+
if (size <= 1 + NONCE_LEN || size > this._sigMaxSize) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
// The signature is decoded from a copy, which has the length
|
|
71
|
+
// validated above and does not change meanwhile: see copyBytes().
|
|
72
|
+
const copy = copyBytes(sig);
|
|
73
|
+
if (copy[0] !== SIG_HEADER + this._logn) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const n = 1 << this._logn;
|
|
77
|
+
const s2 = new Int16Array(n);
|
|
78
|
+
const used = compDecode(s2, this._logn, copy.subarray(1 + NONCE_LEN));
|
|
79
|
+
if (used === 0) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const end = 1 + NONCE_LEN + used;
|
|
83
|
+
if (end !== size) {
|
|
84
|
+
// Only a signature of the fixed size is padded, with zeros.
|
|
85
|
+
if (size !== this._sigSize) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
let rest = 0;
|
|
89
|
+
for (let i = end; i < size; i++) {
|
|
90
|
+
rest |= copy[i];
|
|
91
|
+
}
|
|
92
|
+
if (rest !== 0) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// c = HashToPoint(nonce || msg)
|
|
97
|
+
const hm = new Uint16Array(n);
|
|
98
|
+
const sc = newShake256();
|
|
99
|
+
sc.updateUnsafe(copy.subarray(1, 1 + NONCE_LEN)).updateUnsafe(msg);
|
|
100
|
+
hashToPoint(sc, hm, this._logn);
|
|
101
|
+
sc.reset();
|
|
102
|
+
return verifyRaw(hm, s2, this._h, this._logn);
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
throw new FnDsaError(e);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Creates a verifier which holds a public key.
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
// deno-lint-ignore require-await
|
|
113
|
+
static async _create(params, pk, opts) {
|
|
114
|
+
try {
|
|
115
|
+
assertWebCryptoOption(opts?.useWebCrypto);
|
|
116
|
+
assertBytes(pk, "pk");
|
|
117
|
+
if (byteLengthOf(pk) !== params.pkSize) {
|
|
118
|
+
throw new Error("invalid public key");
|
|
119
|
+
}
|
|
120
|
+
// The key is decoded from a copy, which has the length validated
|
|
121
|
+
// above and does not change meanwhile: see copyBytes().
|
|
122
|
+
const key = copyBytes(pk);
|
|
123
|
+
const verifier = new FnDsaVerifier(params);
|
|
124
|
+
if (key[0] !== PK_HEADER + params.logn ||
|
|
125
|
+
modqDecode(verifier._h, params.logn, key.subarray(1)) !==
|
|
126
|
+
params.pkSize - 1) {
|
|
127
|
+
throw new Error("invalid public key");
|
|
128
|
+
}
|
|
129
|
+
toNttMonty(verifier._h, params.logn);
|
|
130
|
+
return verifier;
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
throw new FnDsaError(e);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { WebCryptoOption } from "./native.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for the factory functions of a signer and a verifier, such as
|
|
4
|
+
* {@link createFnDsa512Signer} and {@link createFnDsa512Verifier}.
|
|
5
|
+
*/
|
|
6
|
+
export interface FnDsaCreateOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Whether to delegate signing and verification to the runtime's Web
|
|
9
|
+
* Cryptography API. It has no effect so far, as FN-DSA is not specified
|
|
10
|
+
* for the Web Cryptography API: this implementation always runs.
|
|
11
|
+
*
|
|
12
|
+
* - `"auto"` (default), `true` or `false`.
|
|
13
|
+
*/
|
|
14
|
+
useWebCrypto?: WebCryptoOption;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Interface for signing of FN-DSA with the private key which the instance
|
|
18
|
+
* holds.
|
|
19
|
+
*
|
|
20
|
+
* Use the async factory functions {@link createFnDsa512Signer} or
|
|
21
|
+
* {@link createFnDsa1024Signer} to create instances that implement this
|
|
22
|
+
* interface.
|
|
23
|
+
*/
|
|
24
|
+
export interface FnDsaSignerInterface {
|
|
25
|
+
/**
|
|
26
|
+
* Signs a message: returns signature. The signature is randomized: signing
|
|
27
|
+
* the same message again gives another signature.
|
|
28
|
+
*/
|
|
29
|
+
sign(msg: Uint8Array): Promise<Uint8Array>;
|
|
30
|
+
/**
|
|
31
|
+
* Erases the private key which the instance holds. `sign` rejects after
|
|
32
|
+
* this.
|
|
33
|
+
*/
|
|
34
|
+
destroy(): void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Interface for verification of FN-DSA with the public key which the
|
|
38
|
+
* instance holds.
|
|
39
|
+
*
|
|
40
|
+
* Use the async factory functions {@link createFnDsa512Verifier} or
|
|
41
|
+
* {@link createFnDsa1024Verifier} to create instances that implement this
|
|
42
|
+
* interface. An application which only verifies signatures does not bundle
|
|
43
|
+
* the code for key generation and signing.
|
|
44
|
+
*/
|
|
45
|
+
export interface FnDsaVerifierInterface {
|
|
46
|
+
/** Verifies a signature: resolves to true if the signature is valid. */
|
|
47
|
+
verify(sig: Uint8Array, msg: Uint8Array): Promise<boolean>;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=interfaces.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../src/src/interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,eAAe,CAAC;CAChC;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,IAAI,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3C;;;OAGG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,sBAAsB;IACrC,wEAAwE;IACxE,MAAM,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC5D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Keccak } from "./deps.js";
|
|
2
|
+
/**
|
|
3
|
+
* Whether (g, -f) and its orthogonalized vector are short enough: the
|
|
4
|
+
* bounds which the sampler of signing relies on, as the standard deviations
|
|
5
|
+
* which it is given are in the range it supports only for such a basis.
|
|
6
|
+
*
|
|
7
|
+
* Key generation keeps only the f and g which pass this, and a private key
|
|
8
|
+
* which is imported has to pass it as well. rt1 and rt2 are work areas of n
|
|
9
|
+
* values, and rt3 of n/2: they are left with values which depend on f and g.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isShortBasis(f: Int8Array, g: Int8Array, logn: number, rt1: Float64Array, rt2: Float64Array, rt3: Float64Array): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Generates a key pair (Zf(keygen)).
|
|
14
|
+
*
|
|
15
|
+
* f and g are sampled with the Gaussian distribution until:
|
|
16
|
+
*
|
|
17
|
+
* - their resultants with X^N+1 are odd;
|
|
18
|
+
* - the norm of (g, -f) and the one of the orthogonalized vector are not
|
|
19
|
+
* greater than 1.17*sqrt(q);
|
|
20
|
+
* - f is invertible modulo X^N+1 modulo q: then h = g/f;
|
|
21
|
+
* - the NTRU equation f*G - g*F = q has a solution with small
|
|
22
|
+
* coefficients.
|
|
23
|
+
*
|
|
24
|
+
* @param rng A SHAKE256 context which is in the squeeze phase.
|
|
25
|
+
* @param f The output f: n = 2^logn coefficients.
|
|
26
|
+
* @param g The output g.
|
|
27
|
+
* @param F The output F.
|
|
28
|
+
* @param G The output G, or null if it is not needed.
|
|
29
|
+
* @param h The output public key (values from 0 to q-1), or null if it is
|
|
30
|
+
* not needed.
|
|
31
|
+
* @param logn The base-2 logarithm of the degree, from 1 to 10.
|
|
32
|
+
*/
|
|
33
|
+
export declare function keygen(rng: Keccak, f: Int8Array, g: Int8Array, F: Int8Array, G: Int8Array | null, h: Uint16Array | null, logn: number): void;
|
|
34
|
+
//# sourceMappingURL=keygen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keygen.d.ts","sourceRoot":"","sources":["../../src/src/keygen.ts"],"names":[],"mappings":"AA2CA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAytFxC;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC1B,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,YAAY,GAChB,OAAO,CAkCT;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,MAAM,CACpB,GAAG,EAAE,MAAM,EACX,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,GAAG,IAAI,EACnB,CAAC,EAAE,WAAW,GAAG,IAAI,EACrB,IAAI,EAAE,MAAM,GACX,IAAI,CAiDN"}
|