@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.
- package/LICENSE +49 -0
- package/README.md +221 -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/basis.d.ts +11 -0
- package/esm/src/basis.d.ts.map +1 -0
- package/esm/src/basis.js +78 -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 +11 -0
- package/esm/src/consts.d.ts.map +1 -0
- package/esm/src/consts.js +10 -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 +58 -0
- package/esm/src/fft.d.ts.map +1 -0
- package/esm/src/fft.js +364 -0
- package/esm/src/fnDsa1024.d.ts +88 -0
- package/esm/src/fnDsa1024.d.ts.map +1 -0
- package/esm/src/fnDsa1024.js +98 -0
- package/esm/src/fnDsa512.d.ts +88 -0
- package/esm/src/fnDsa512.d.ts.map +1 -0
- package/esm/src/fnDsa512.js +98 -0
- package/esm/src/fnDsaKeyGen.d.ts +6 -0
- package/esm/src/fnDsaKeyGen.d.ts.map +1 -0
- package/esm/src/fnDsaKeyGen.js +92 -0
- package/esm/src/fnDsaSigner.d.ts +30 -0
- package/esm/src/fnDsaSigner.d.ts.map +1 -0
- package/esm/src/fnDsaSigner.js +211 -0
- package/esm/src/fnDsaVerifier.d.ts +24 -0
- package/esm/src/fnDsaVerifier.d.ts.map +1 -0
- package/esm/src/fnDsaVerifier.js +134 -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 +29 -0
- package/esm/src/keygen.d.ts.map +1 -0
- package/esm/src/keygen.js +1956 -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 +24 -0
- package/esm/src/utils.d.ts.map +1 -0
- package/esm/src/utils.js +51 -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,211 @@
|
|
|
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 { isShortBasis } from "./basis.js";
|
|
14
|
+
import { compEncode, MAX_FG_BITS, MAX_fg_BITS, trimI8Decode } from "./codec.js";
|
|
15
|
+
import { hashToPoint, newShake256 } from "./common.js";
|
|
16
|
+
import { NONCE_LEN, SEED_SIZE, SK_HEADER } from "./consts.js";
|
|
17
|
+
import { FnDsaError } from "./errors.js";
|
|
18
|
+
import { assertWebCryptoOption } from "./native.js";
|
|
19
|
+
import { expandedKeySize, expandPrivkey, isNtruBasis, signTree, } from "./sign.js";
|
|
20
|
+
import { assertBytes, byteLengthOf, copyBytes, getCrypto } from "./utils.js";
|
|
21
|
+
import { completePrivate } from "./vrfy.js";
|
|
22
|
+
/** The header of a signature with the compressed encoding: 0x30 + logn. */
|
|
23
|
+
const SIG_HEADER = 0x30;
|
|
24
|
+
/**
|
|
25
|
+
* Signing of FN-DSA.
|
|
26
|
+
*
|
|
27
|
+
* An instance holds a private key, which signs any number of messages until
|
|
28
|
+
* destroy() erases it. Use one of the factory functions to create an
|
|
29
|
+
* instance: `createFnDsa512Signer` or `createFnDsa1024Signer`.
|
|
30
|
+
*/
|
|
31
|
+
export class FnDsaSigner {
|
|
32
|
+
constructor(params, api) {
|
|
33
|
+
Object.defineProperty(this, "_api", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: void 0
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "_logn", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
Object.defineProperty(this, "_sigSize", {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true,
|
|
49
|
+
value: void 0
|
|
50
|
+
});
|
|
51
|
+
// The private key which the instance holds, expanded: the basis in the
|
|
52
|
+
// FFT representation and the LDL tree, which signing uses.
|
|
53
|
+
//
|
|
54
|
+
// They are private fields of JavaScript, not properties: what serializes
|
|
55
|
+
// or inspects an instance, such as JSON.stringify(), structuredClone() or
|
|
56
|
+
// console.log(), must not copy the private key to where destroy() cannot
|
|
57
|
+
// erase it.
|
|
58
|
+
_FnDsaSigner_hasKey.set(this, false);
|
|
59
|
+
_FnDsaSigner_expandedKey.set(this, void 0);
|
|
60
|
+
this._api = api;
|
|
61
|
+
this._logn = params.logn;
|
|
62
|
+
this._sigSize = params.sigSize;
|
|
63
|
+
__classPrivateFieldSet(this, _FnDsaSigner_expandedKey, new Float64Array(expandedKeySize(params.logn)), "f");
|
|
64
|
+
}
|
|
65
|
+
/** Signs a message: returns signature. */
|
|
66
|
+
// deno-lint-ignore require-await
|
|
67
|
+
async sign(msg) {
|
|
68
|
+
const logn = this._logn;
|
|
69
|
+
const n = 1 << logn;
|
|
70
|
+
const seed = new Uint8Array(SEED_SIZE);
|
|
71
|
+
const hm = new Uint16Array(n);
|
|
72
|
+
const s2 = new Int16Array(n);
|
|
73
|
+
const sc = newShake256();
|
|
74
|
+
try {
|
|
75
|
+
if (!__classPrivateFieldGet(this, _FnDsaSigner_hasKey, "f")) {
|
|
76
|
+
throw new Error("the signer has been destroyed");
|
|
77
|
+
}
|
|
78
|
+
assertBytes(msg, "msg");
|
|
79
|
+
const sig = new Uint8Array(this._sigSize);
|
|
80
|
+
sig[0] = SIG_HEADER + logn;
|
|
81
|
+
// c = HashToPoint(nonce || msg), with a fresh nonce: two signatures
|
|
82
|
+
// of the same c would reveal the private key.
|
|
83
|
+
const nonce = sig.subarray(1, 1 + NONCE_LEN);
|
|
84
|
+
this._api.getRandomValues(nonce);
|
|
85
|
+
sc.updateUnsafe(nonce).updateUnsafe(msg);
|
|
86
|
+
hashToPoint(sc, hm, logn);
|
|
87
|
+
// The seed of the sampler.
|
|
88
|
+
this._api.getRandomValues(seed);
|
|
89
|
+
sc.reset();
|
|
90
|
+
sc.updateUnsafe(seed);
|
|
91
|
+
// A signature whose encoding is longer than the fixed size, which is
|
|
92
|
+
// rare, is dropped, and another one is sampled.
|
|
93
|
+
const body = sig.subarray(1 + NONCE_LEN);
|
|
94
|
+
for (;;) {
|
|
95
|
+
signTree(s2, sc, __classPrivateFieldGet(this, _FnDsaSigner_expandedKey, "f"), hm, logn);
|
|
96
|
+
body.fill(0);
|
|
97
|
+
if (compEncode(body, s2, logn) !== 0)
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
return sig;
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
throw new FnDsaError(e);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
seed.fill(0);
|
|
107
|
+
hm.fill(0);
|
|
108
|
+
s2.fill(0);
|
|
109
|
+
sc.reset();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Erases the private key which the instance holds. */
|
|
113
|
+
destroy() {
|
|
114
|
+
__classPrivateFieldSet(this, _FnDsaSigner_hasKey, false, "f");
|
|
115
|
+
__classPrivateFieldGet(this, _FnDsaSigner_expandedKey, "f").fill(0);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Creates a signer which holds a private key.
|
|
119
|
+
* @internal
|
|
120
|
+
*/
|
|
121
|
+
// deno-lint-ignore require-await
|
|
122
|
+
static async _create(params, sk, opts) {
|
|
123
|
+
const logn = params.logn;
|
|
124
|
+
const n = 1 << logn;
|
|
125
|
+
let key;
|
|
126
|
+
const f = new Int8Array(n);
|
|
127
|
+
const g = new Int8Array(n);
|
|
128
|
+
const F = new Int8Array(n);
|
|
129
|
+
const G = new Int8Array(n);
|
|
130
|
+
const rt = new Float64Array(n + n + (n >> 1));
|
|
131
|
+
let signer;
|
|
132
|
+
try {
|
|
133
|
+
assertWebCryptoOption(opts?.useWebCrypto);
|
|
134
|
+
assertBytes(sk, "sk");
|
|
135
|
+
if (byteLengthOf(sk) !== params.skSize) {
|
|
136
|
+
throw new Error("invalid private key");
|
|
137
|
+
}
|
|
138
|
+
// The key is decoded from a copy, which has the length validated
|
|
139
|
+
// above and does not change meanwhile: see copyBytes().
|
|
140
|
+
key = copyBytes(sk);
|
|
141
|
+
if (key[0] !== SK_HEADER + logn) {
|
|
142
|
+
throw new Error("invalid private key");
|
|
143
|
+
}
|
|
144
|
+
const fgSize = ((n * MAX_fg_BITS[logn]) + 7) >> 3;
|
|
145
|
+
const FSize = ((n * MAX_FG_BITS[logn]) + 7) >> 3;
|
|
146
|
+
// The lengths of the three parts add up to the size of the key.
|
|
147
|
+
if (trimI8Decode(f, logn, MAX_fg_BITS[logn], key.subarray(1, 1 + fgSize)) === 0 ||
|
|
148
|
+
trimI8Decode(g, logn, MAX_fg_BITS[logn], key.subarray(1 + fgSize, 1 + 2 * fgSize)) === 0 ||
|
|
149
|
+
trimI8Decode(F, logn, MAX_FG_BITS[logn], key.subarray(1 + 2 * fgSize, 1 + 2 * fgSize + FSize)) === 0 ||
|
|
150
|
+
1 + 2 * fgSize + FSize !== params.skSize ||
|
|
151
|
+
// G is not in the encoding: it is computed from f * G - g * F = q
|
|
152
|
+
// modulo q, which any F satisfies. A key which does not satisfy the
|
|
153
|
+
// equation over the integers is not a basis of the lattice, and
|
|
154
|
+
// signing with it may never end.
|
|
155
|
+
!completePrivate(G, f, g, F, logn) ||
|
|
156
|
+
!isNtruBasis(f, g, F, G, logn) ||
|
|
157
|
+
// The bounds which key generation ensures, and which the sampler
|
|
158
|
+
// of signing relies on.
|
|
159
|
+
!isShortBasis(f, g, logn, rt.subarray(0, n), rt.subarray(n, 2 * n), rt.subarray(2 * n))) {
|
|
160
|
+
throw new Error("invalid private key");
|
|
161
|
+
}
|
|
162
|
+
signer = new FnDsaSigner(params, getCrypto());
|
|
163
|
+
expandPrivkey(__classPrivateFieldGet(signer, _FnDsaSigner_expandedKey, "f"), f, g, F, G, logn);
|
|
164
|
+
__classPrivateFieldSet(signer, _FnDsaSigner_hasKey, true, "f");
|
|
165
|
+
return signer;
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
signer?.destroy();
|
|
169
|
+
throw new FnDsaError(e);
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
key?.fill(0);
|
|
173
|
+
f.fill(0);
|
|
174
|
+
g.fill(0);
|
|
175
|
+
F.fill(0);
|
|
176
|
+
G.fill(0);
|
|
177
|
+
rt.fill(0);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
_FnDsaSigner_hasKey = new WeakMap(), _FnDsaSigner_expandedKey = new WeakMap();
|
|
182
|
+
/**
|
|
183
|
+
* What the factory functions return: a signer of which nothing but sign()
|
|
184
|
+
* and destroy() can be reached.
|
|
185
|
+
*
|
|
186
|
+
* The implementation is in a private field of JavaScript. Its methods and
|
|
187
|
+
* its work areas, which handle the private key, are properties which who
|
|
188
|
+
* holds the instance could read, call or replace: a signer may be given to
|
|
189
|
+
* a party which is to sign, and not to have the private key.
|
|
190
|
+
*/
|
|
191
|
+
class FnDsaSignerHandle {
|
|
192
|
+
constructor(signer) {
|
|
193
|
+
_FnDsaSignerHandle_signer.set(this, void 0);
|
|
194
|
+
__classPrivateFieldSet(this, _FnDsaSignerHandle_signer, signer, "f");
|
|
195
|
+
}
|
|
196
|
+
/** Signs a message: returns signature. */
|
|
197
|
+
sign(msg) {
|
|
198
|
+
return __classPrivateFieldGet(this, _FnDsaSignerHandle_signer, "f").sign(msg);
|
|
199
|
+
}
|
|
200
|
+
/** Erases the private key which the instance holds. */
|
|
201
|
+
destroy() {
|
|
202
|
+
__classPrivateFieldGet(this, _FnDsaSignerHandle_signer, "f").destroy();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
_FnDsaSignerHandle_signer = new WeakMap();
|
|
206
|
+
/**
|
|
207
|
+
* Creates a signer which holds a private key, for a factory function.
|
|
208
|
+
*/
|
|
209
|
+
export async function createSignerHandle(params, sk, opts) {
|
|
210
|
+
return new FnDsaSignerHandle(await FnDsaSigner._create(params, sk, opts));
|
|
211
|
+
}
|
|
@@ -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;AAO/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"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { compDecode, modqDecode } from "./codec.js";
|
|
2
|
+
import { hashToPoint, newShake256 } from "./common.js";
|
|
3
|
+
import { NONCE_LEN, PK_HEADER } 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 signature with the compressed encoding: 0x30 + logn. */
|
|
9
|
+
const SIG_HEADER = 0x30;
|
|
10
|
+
/**
|
|
11
|
+
* Verification of FN-DSA.
|
|
12
|
+
*
|
|
13
|
+
* An instance holds a public key, which verifies any number of signatures.
|
|
14
|
+
* Use one of the factory functions to create an instance:
|
|
15
|
+
* `createFnDsa512Verifier` or `createFnDsa1024Verifier`.
|
|
16
|
+
*/
|
|
17
|
+
export class FnDsaVerifier {
|
|
18
|
+
constructor(params) {
|
|
19
|
+
Object.defineProperty(this, "_logn", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: void 0
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "_sigSize", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
// The maximum size of a signature in the compressed format, which is
|
|
32
|
+
// longer than the fixed size: FALCON_SIG_COMPRESSED_MAXSIZE of the
|
|
33
|
+
// reference implementation (752 and 1462 octets).
|
|
34
|
+
Object.defineProperty(this, "_sigMaxSize", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: void 0
|
|
39
|
+
});
|
|
40
|
+
// The public key h which the instance holds, in the NTT and Montgomery
|
|
41
|
+
// representation.
|
|
42
|
+
Object.defineProperty(this, "_h", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
48
|
+
this._logn = params.logn;
|
|
49
|
+
this._sigSize = params.sigSize;
|
|
50
|
+
this._sigMaxSize =
|
|
51
|
+
((((11 << params.logn) + (101 >> (10 - params.logn))) + 7) >> 3) + 41;
|
|
52
|
+
this._h = new Uint16Array(1 << params.logn);
|
|
53
|
+
}
|
|
54
|
+
/** Verifies a signature: resolves to true if the signature is valid. */
|
|
55
|
+
// deno-lint-ignore require-await
|
|
56
|
+
async verify(sig, msg) {
|
|
57
|
+
try {
|
|
58
|
+
assertBytes(sig, "sig");
|
|
59
|
+
assertBytes(msg, "msg");
|
|
60
|
+
const size = byteLengthOf(sig);
|
|
61
|
+
// A signature is the header, the nonce and the encoded s2, which is
|
|
62
|
+
// of a variable length, and may be padded with zeros to the fixed
|
|
63
|
+
// size. What is longer than any signature is rejected before it is
|
|
64
|
+
// copied.
|
|
65
|
+
if (size <= 1 + NONCE_LEN || size > this._sigMaxSize) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
// The signature is decoded from a copy, which has the length
|
|
69
|
+
// validated above and does not change meanwhile: see copyBytes().
|
|
70
|
+
const copy = copyBytes(sig);
|
|
71
|
+
if (copy[0] !== SIG_HEADER + this._logn) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
const n = 1 << this._logn;
|
|
75
|
+
const s2 = new Int16Array(n);
|
|
76
|
+
const used = compDecode(s2, this._logn, copy.subarray(1 + NONCE_LEN));
|
|
77
|
+
if (used === 0) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const end = 1 + NONCE_LEN + used;
|
|
81
|
+
if (end !== size) {
|
|
82
|
+
// Only a signature of the fixed size is padded, with zeros.
|
|
83
|
+
if (size !== this._sigSize) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
let rest = 0;
|
|
87
|
+
for (let i = end; i < size; i++) {
|
|
88
|
+
rest |= copy[i];
|
|
89
|
+
}
|
|
90
|
+
if (rest !== 0) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// c = HashToPoint(nonce || msg)
|
|
95
|
+
const hm = new Uint16Array(n);
|
|
96
|
+
const sc = newShake256();
|
|
97
|
+
sc.updateUnsafe(copy.subarray(1, 1 + NONCE_LEN)).updateUnsafe(msg);
|
|
98
|
+
hashToPoint(sc, hm, this._logn);
|
|
99
|
+
sc.reset();
|
|
100
|
+
return verifyRaw(hm, s2, this._h, this._logn);
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
throw new FnDsaError(e);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Creates a verifier which holds a public key.
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
// deno-lint-ignore require-await
|
|
111
|
+
static async _create(params, pk, opts) {
|
|
112
|
+
try {
|
|
113
|
+
assertWebCryptoOption(opts?.useWebCrypto);
|
|
114
|
+
assertBytes(pk, "pk");
|
|
115
|
+
if (byteLengthOf(pk) !== params.pkSize) {
|
|
116
|
+
throw new Error("invalid public key");
|
|
117
|
+
}
|
|
118
|
+
// The key is decoded from a copy, which has the length validated
|
|
119
|
+
// above and does not change meanwhile: see copyBytes().
|
|
120
|
+
const key = copyBytes(pk);
|
|
121
|
+
const verifier = new FnDsaVerifier(params);
|
|
122
|
+
if (key[0] !== PK_HEADER + params.logn ||
|
|
123
|
+
modqDecode(verifier._h, params.logn, key.subarray(1)) !==
|
|
124
|
+
params.pkSize - 1) {
|
|
125
|
+
throw new Error("invalid public key");
|
|
126
|
+
}
|
|
127
|
+
toNttMonty(verifier._h, params.logn);
|
|
128
|
+
return verifier;
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
throw new FnDsaError(e);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -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,29 @@
|
|
|
1
|
+
import type { Keccak } from "./deps.js";
|
|
2
|
+
/**
|
|
3
|
+
* PRIMES, with its first `count` primes at least: all of them by default,
|
|
4
|
+
* for the tests.
|
|
5
|
+
*/
|
|
6
|
+
export declare function primes(count?: number): Uint32Array;
|
|
7
|
+
/**
|
|
8
|
+
* Generates a key pair (Zf(keygen)).
|
|
9
|
+
*
|
|
10
|
+
* f and g are sampled with the Gaussian distribution until:
|
|
11
|
+
*
|
|
12
|
+
* - their resultants with X^N+1 are odd;
|
|
13
|
+
* - the norm of (g, -f) and the one of the orthogonalized vector are not
|
|
14
|
+
* greater than 1.17*sqrt(q);
|
|
15
|
+
* - f is invertible modulo X^N+1 modulo q: then h = g/f;
|
|
16
|
+
* - the NTRU equation f*G - g*F = q has a solution with small
|
|
17
|
+
* coefficients.
|
|
18
|
+
*
|
|
19
|
+
* @param rng A SHAKE256 context which is in the squeeze phase.
|
|
20
|
+
* @param f The output f: n = 2^logn coefficients.
|
|
21
|
+
* @param g The output g.
|
|
22
|
+
* @param F The output F.
|
|
23
|
+
* @param G The output G, or null if it is not needed.
|
|
24
|
+
* @param h The output public key (values from 0 to q-1), or null if it is
|
|
25
|
+
* not needed.
|
|
26
|
+
* @param logn The base-2 logarithm of the degree, from 1 to 10.
|
|
27
|
+
*/
|
|
28
|
+
export declare function keygen(rng: Keccak, f: Int8Array, g: Int8Array, F: Int8Array, G: Int8Array | null, h: Uint16Array | null, logn: number): void;
|
|
29
|
+
//# sourceMappingURL=keygen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keygen.d.ts","sourceRoot":"","sources":["../../src/src/keygen.ts"],"names":[],"mappings":"AA4CA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AA0HxC;;;GAGG;AACH,wBAAgB,MAAM,CAAC,KAAK,GAAE,MAAY,GAAG,WAAW,CAKvD;AAwpED;;;;;;;;;;;;;;;;;;;;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,CAkDN"}
|