@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,112 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @webpqc/fndsa is a port of the reference implementation of Falcon
|
|
3
|
+
* (round 3), which is under the MIT license:
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2017-2019 Falcon Project
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
8
|
+
* a copy of this software and associated documentation files (the
|
|
9
|
+
* "Software"), to deal in the Software without restriction, including
|
|
10
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
12
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
13
|
+
* the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be
|
|
16
|
+
* included in all copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
21
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
22
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
23
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
24
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
/*
|
|
27
|
+
* Hashing to a point, and the bound on the norm of a signature: a port of
|
|
28
|
+
* common.c. Author: Thomas Pornin <thomas.pornin@nccgroup.com>
|
|
29
|
+
*
|
|
30
|
+
* The notice above is a legal comment (it starts with "!"), which bundlers
|
|
31
|
+
* and minifiers keep: this module is in every bundle of the package, as
|
|
32
|
+
* both signing and verification import it.
|
|
33
|
+
*/
|
|
34
|
+
import { Q } from "./consts.js";
|
|
35
|
+
import { Keccak } from "./deps.js";
|
|
36
|
+
/** A new SHAKE256 instance. */
|
|
37
|
+
export function newShake256() {
|
|
38
|
+
return new Keccak(136, 0x1f, 32, true);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Squeezes a SHAKE256 instance, which has absorbed the nonce and the
|
|
42
|
+
* message, to n values modulo q (HashToPoint), by rejection sampling of
|
|
43
|
+
* 16-bit values.
|
|
44
|
+
*
|
|
45
|
+
* Its timing depends on the hash, that is, on the nonce and the message,
|
|
46
|
+
* and not on a key: the nonce is in the signature, so this reveals nothing
|
|
47
|
+
* to who has the signed message.
|
|
48
|
+
*/
|
|
49
|
+
export function hashToPoint(sc, x, logn) {
|
|
50
|
+
const n = 1 << logn;
|
|
51
|
+
// A multiple of 2, to keep whole values.
|
|
52
|
+
const buf = new Uint8Array(272);
|
|
53
|
+
let u = 0;
|
|
54
|
+
while (u < n) {
|
|
55
|
+
sc.writeIntoUnsafe(buf);
|
|
56
|
+
for (let i = 0; i < buf.length && u < n; i += 2) {
|
|
57
|
+
let w = (buf[i] << 8) | buf[i + 1];
|
|
58
|
+
// 61445 = 5 * q
|
|
59
|
+
if (w < 61445) {
|
|
60
|
+
while (w >= Q) {
|
|
61
|
+
w -= Q;
|
|
62
|
+
}
|
|
63
|
+
x[u++] = w;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** The bound on the squared norm of a signature (s1, s2), by logn. */
|
|
69
|
+
const L2BOUND = [
|
|
70
|
+
0,
|
|
71
|
+
101498,
|
|
72
|
+
208714,
|
|
73
|
+
428865,
|
|
74
|
+
892039,
|
|
75
|
+
1852696,
|
|
76
|
+
3842630,
|
|
77
|
+
7959734,
|
|
78
|
+
16468416,
|
|
79
|
+
34034726,
|
|
80
|
+
70265242,
|
|
81
|
+
];
|
|
82
|
+
/**
|
|
83
|
+
* Whether the squared norm of (s1, s2) is at most the bound. The sum
|
|
84
|
+
* saturates if it reaches 2^31, so it does not wrap around.
|
|
85
|
+
*/
|
|
86
|
+
export function isShort(s1, s2, logn) {
|
|
87
|
+
const n = 1 << logn;
|
|
88
|
+
let s = 0;
|
|
89
|
+
let ng = 0;
|
|
90
|
+
for (let u = 0; u < n; u++) {
|
|
91
|
+
s = (s + s1[u] * s1[u]) | 0;
|
|
92
|
+
ng |= s;
|
|
93
|
+
s = (s + s2[u] * s2[u]) | 0;
|
|
94
|
+
ng |= s;
|
|
95
|
+
}
|
|
96
|
+
s |= -(ng >>> 31);
|
|
97
|
+
return (s >>> 0) <= L2BOUND[logn];
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Like {@link isShort}, with the squared norm of s1 already computed (and
|
|
101
|
+
* saturated) in `sqn`.
|
|
102
|
+
*/
|
|
103
|
+
export function isShortHalf(sqn, s2, logn) {
|
|
104
|
+
const n = 1 << logn;
|
|
105
|
+
let ng = -(sqn >>> 31);
|
|
106
|
+
for (let u = 0; u < n; u++) {
|
|
107
|
+
sqn = (sqn + s2[u] * s2[u]) | 0;
|
|
108
|
+
ng |= sqn;
|
|
109
|
+
}
|
|
110
|
+
sqn |= -(ng >>> 31);
|
|
111
|
+
return (sqn >>> 0) <= L2BOUND[logn];
|
|
112
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../src/src/consts.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,eAAO,MAAM,CAAC,QAAQ,CAAC;AAEvB,qEAAqE;AACrE,eAAO,MAAM,SAAS,KAAK,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deps.d.ts","sourceRoot":"","sources":["../../src/src/deps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,MAAM,EAAE,MAAM,kBAAkB,CAAC"}
|
package/esm/src/deps.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { KeccakCore as Keccak } from "./sha3/keccak.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBAChB,CAAC,EAAE,OAAO;CAc9B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The base error class of fndsa.
|
|
3
|
+
*/
|
|
4
|
+
export class FnDsaError extends Error {
|
|
5
|
+
constructor(e) {
|
|
6
|
+
let message;
|
|
7
|
+
if (e instanceof Error) {
|
|
8
|
+
message = e.message;
|
|
9
|
+
}
|
|
10
|
+
else if (typeof e === "string") {
|
|
11
|
+
message = e;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
message = "";
|
|
15
|
+
}
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = this.constructor.name;
|
|
18
|
+
}
|
|
19
|
+
}
|
package/esm/src/fft.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Converts a polynomial to the FFT representation, in place. */
|
|
2
|
+
export declare function FFT(f: Float64Array, logn: number): void;
|
|
3
|
+
/** Converts a polynomial back from the FFT representation, in place. */
|
|
4
|
+
export declare function iFFT(f: Float64Array, logn: number): void;
|
|
5
|
+
/** a += b */
|
|
6
|
+
export declare function polyAdd(a: Float64Array, b: Float64Array, logn: number): void;
|
|
7
|
+
/** a -= b */
|
|
8
|
+
export declare function polySub(a: Float64Array, b: Float64Array, logn: number): void;
|
|
9
|
+
/** a = -a */
|
|
10
|
+
export declare function polyNeg(a: Float64Array, logn: number): void;
|
|
11
|
+
/** a = adj(a), in the FFT representation. */
|
|
12
|
+
export declare function polyAdjFft(a: Float64Array, logn: number): void;
|
|
13
|
+
/** a *= b, in the FFT representation. */
|
|
14
|
+
export declare function polyMulFft(a: Float64Array, b: Float64Array, logn: number): void;
|
|
15
|
+
/** a *= adj(b), in the FFT representation. */
|
|
16
|
+
export declare function polyMuladjFft(a: Float64Array, b: Float64Array, logn: number): void;
|
|
17
|
+
/** a *= adj(a), in the FFT representation. */
|
|
18
|
+
export declare function polyMulselfadjFft(a: Float64Array, logn: number): void;
|
|
19
|
+
/** a *= x, for a real constant x. */
|
|
20
|
+
export declare function polyMulconst(a: Float64Array, x: number, logn: number): void;
|
|
21
|
+
/** a /= b, in the FFT representation. */
|
|
22
|
+
export declare function polyDivFft(a: Float64Array, b: Float64Array, logn: number): void;
|
|
23
|
+
/**
|
|
24
|
+
* d = 1 / (a * adj(a) + b * adj(b)), in the FFT representation. The result
|
|
25
|
+
* is real: only the first half of d is set.
|
|
26
|
+
*/
|
|
27
|
+
export declare function polyInvnorm2Fft(d: Float64Array, a: Float64Array, b: Float64Array, logn: number): void;
|
|
28
|
+
/** d = F * adj(f) + G * adj(g), in the FFT representation. */
|
|
29
|
+
export declare function polyAddMuladjFft(d: Float64Array, F: Float64Array, G: Float64Array, f: Float64Array, g: Float64Array, logn: number): void;
|
|
30
|
+
/**
|
|
31
|
+
* a *= b, where b is self-adjoint (real in the FFT representation): only
|
|
32
|
+
* the first half of b is read.
|
|
33
|
+
*/
|
|
34
|
+
export declare function polyMulAutoadjFft(a: Float64Array, b: Float64Array, logn: number): void;
|
|
35
|
+
/**
|
|
36
|
+
* a /= b, where b is self-adjoint (real in the FFT representation): only
|
|
37
|
+
* the first half of b is read.
|
|
38
|
+
*/
|
|
39
|
+
export declare function polyDivAutoadjFft(a: Float64Array, b: Float64Array, logn: number): void;
|
|
40
|
+
/**
|
|
41
|
+
* The LDL decomposition of the self-adjoint matrix [[g00, g01], [adj(g01),
|
|
42
|
+
* g11]]: d11 and l10 are set so that it is L * diag(g00, d11) * adj(L) with
|
|
43
|
+
* L = [[1, 0], [l10, 1]]. The inputs are not modified.
|
|
44
|
+
*/
|
|
45
|
+
export declare function polyLDLmvFft(d11: Float64Array, l10: Float64Array, g00: Float64Array, g01: Float64Array, g11: Float64Array, logn: number): void;
|
|
46
|
+
/**
|
|
47
|
+
* Splits f, in the FFT representation, to f0 and f1 of half its degree,
|
|
48
|
+
* such that f = f0(x^2) + x * f1(x^2).
|
|
49
|
+
*/
|
|
50
|
+
export declare function polySplitFft(f0: Float64Array, f1: Float64Array, f: Float64Array, logn: number): void;
|
|
51
|
+
/** The inverse of {@link polySplitFft}. */
|
|
52
|
+
export declare function polyMergeFft(f: Float64Array, f0: Float64Array, f1: Float64Array, logn: number): void;
|
|
53
|
+
//# sourceMappingURL=fft.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fft.d.ts","sourceRoot":"","sources":["../../src/src/fft.ts"],"names":[],"mappings":"AAsDA,iEAAiE;AACjE,wBAAgB,GAAG,CAAC,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CA0BvD;AAED,wEAAwE;AACxE,wBAAgB,IAAI,CAAC,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAmCxD;AAED,aAAa;AACb,wBAAgB,OAAO,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAK5E;AAED,aAAa;AACb,wBAAgB,OAAO,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAK5E;AAED,aAAa;AACb,wBAAgB,OAAO,CAAC,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAK3D;AAED,6CAA6C;AAC7C,wBAAgB,UAAU,CAAC,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAK9D;AAED,yCAAyC;AACzC,wBAAgB,UAAU,CACxB,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAUN;AAED,8CAA8C;AAC9C,wBAAgB,aAAa,CAC3B,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAUN;AAED,8CAA8C;AAC9C,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAQrE;AAED,qCAAqC;AACrC,wBAAgB,YAAY,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAK3E;AAED,yCAAyC;AACzC,wBAAgB,UAAU,CACxB,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAaN;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CASN;AAED,8DAA8D;AAC9D,wBAAgB,gBAAgB,CAC9B,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAkBN;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAMN;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAON;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,MAAM,GACX,IAAI,CAwBN;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,CAAC,EAAE,YAAY,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAoBN;AAED,2CAA2C;AAC3C,wBAAgB,YAAY,CAC1B,CAAC,EAAE,YAAY,EACf,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,IAAI,EAAE,MAAM,GACX,IAAI,CAmBN"}
|
package/esm/src/fft.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* FFT over the ring of FN-DSA, on IEEE 754 binary64 values.
|
|
3
|
+
*
|
|
4
|
+
* This is a port of fft.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
|
+
* A polynomial of degree less than n = 2^logn is n values: in the FFT
|
|
11
|
+
* representation, the real parts of n/2 complex values and then their
|
|
12
|
+
* imaginary parts (the other n/2 values are their conjugates).
|
|
13
|
+
*
|
|
14
|
+
* Only +, -, * and / are used, which are correctly rounded in every runtime
|
|
15
|
+
* and are not fused, so the results are those of the reference
|
|
16
|
+
* implementation, bit for bit.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* cos(j * pi / 1024) for j = 0..512, correctly rounded, as big-endian
|
|
20
|
+
* binary64 values in hexadecimal. Math.cos is not used: its result may
|
|
21
|
+
* differ between runtimes, and so would the signatures.
|
|
22
|
+
*/
|
|
23
|
+
const COS_HEX = "3ff00000000000003feffff621621d023fefffd8858e8a923fefffa72c978c4f3fefff62169b92db3fefff0943c53bd13feffe9cb44b51a13feffe1c6870cb773feffd886084cd0d3feffce09ce2a6793feffc251df1d3f83feffb55e425fdae3feffa72effef75d3feff97c4208c0143feff871dadb81df3feff753bb1b91643feff621e3796d7e3feff4dc54b1bed33feff3830f8d575c3feff21614e131ed3feff095658e71ad3fefef01028261913fefed58ecb673c43fefeb9d2530410f3fefe9cdad01883a3fefe7ea85482d603fefe5f3af2e39403fefe3e92be9d8863fefe1cafcbd5b093fefdf9922f733073fefdd539ff1f4563fefdafa7514538c3fefd88da3d125263fefd60d2da75c9e3fefd37914220b843fefd0d158d860873fefce15fd6da67b3fefcb47039143543fefc8646cfeb7213fefc56e3b7d9af63fefc26470e19fd33fefbf470f0a8d883fefbc1617e441863fefb8d18d66adb73fefb5797195d7413fefb20dc681d54d3fefae8e8e46cfbb3fefaafbcb0cfddc3fefa7557f08a5173fefa39bac7a17913fef9fce55adb2c83fef9bed7cfbde293fef97f924c9099b3fef93f14f85ac083fef8fd5ffae41db3fef8ba737cb4b783fef8764fa714ba93fef830f4a40c60c3fef7ea629e63d6e3fef7a299c1a322a3fef7599a3a120773fef70f6434b7eb73fef6c3f7df5bbb73fef677556883cee3fef6297cff75cb03fef5da6ed43685d3fef58a2b1789e843fef538b1faf2d073fef4e603b0b2f2d3fef492206bcabb43fef43d085ff92dd3fef3e6bbc1bbc653fef38f3ac64e5893fef33685a3aaef03fef2dc9c9089a9d3fef2817fc4609ce3fef2252f7763ada3fef1c7abe2847083fef168f53f7205d3fef1090bc898f5f3fef0a7efb9230d73fef045a14cf738c3feefe220c0b95ec3feef7d6e51ca3c03feef178a3e473c23feeeb074c50a5443feee482e25a9dbc3feeddeb6a0786513feed740e76849633feed0835e9990093feec9b2d3c3bf843feec2cf4b1af6b23feebbd8c8df0b743feeb4cf515b88113feeadb2e8e7a88e3feea68393e658003fee9f4156c62dda3fee97ec36016b303fee9084361df7f23fee89095bad60253fee817bab4cd10d3fee79db29a5165a3fee7227db6a97443fee6a61c55d53a73fee6288ec48e1123fee5a9d550467d33fee529f04729ffc3fee4a8dff81ce5e3fee426a4b2bc17e3fee3a33ec75ce853fee31eae870ce253fee298f4439197a3fee212104f686e53fee18a02fdc66d93fee100cca2980ac3fee0766d9280f543fedfeae622dbe2b3fedf5e36a9ba59c3feded05f7de47da3fede4160f6d8d813feddb13b6ccc23c3fedd1fef38a915a3fedc8d7cb4102603fedbf9e4395759a3fedb6526238a09b3fedacf42ce68ab93feda383a96689883fed9a00dd8b3d463fed906bcf328d463fed86c48445a44f3fed7d0b02b8ecf93fed733f508c0dff3fed696173c9e68b3fed5f7172888a7f3fed556f52e93eb13fed4b5b1b1875243fed4134d14dc93a3fed36fc7bcbfbdc3fed2cb220e0ef9f3fed2255c6e5a4e13fed17e7743e35dc3fed0d672f59d2b93fed02d4feb2bd923fecf830e8ce467b3feced7af43cc7733fece2b32799a0603fecd7d9898b32f63fecccee20c2dea03fecc1f0f3fcfc5c3fecb6e20a00da993fecabc169a0b9003feca08f19b9c4493fec954b213411f53fec89f587029c133fec7e8e52233cf33fec7315899eaad73fec678b3488739b3fec5bef59fef85a3fec5042012b69073fec44833141c0043fec38b2f180bdb13fec2cd14931e3f13fec20de3fa971b03fec14d9dc465e573fec08c4267255493febfc9d25a1b1473febf064e15377dd3febe41b611154c13febd7c0ac6f952a3febcb54cb0d23273febbed7c49380ea3febb249a0b6c40d3feba5aa673590d23feb98fa1fd9155e3feb8c38d27504e93feb7f6686e792e93feb728345196e3e3feb658f14fdbc473feb5889fe9214053feb4b7409de79253feb3e4d3ef557123feb3115a5f37bf33feb23cd470013b43feb16742a4ca2f53feb090a581502003feafb8fd89f57b63feaee04b43c14743feae068f345ecef3fead2bc9e21d5113feac4ffbd3efac83feab7325916c0d43feaa9547a2cb98e3fea9b66290ea1a33fea8d676e545ad23fea7f58529fe69d3fea7138de9d60f53fea63091b02fae23fea54c91090f5233fea4678c8119ac83fea38184a593bc63fea29a7a04627823fea1b26d2c0a75e3fea0c95eabaf9373fe9fdf4f13149de3fe9ef43ef29af943fe9e082edb424723fe9d1b1f5ea80d53fe9c2d110f075c23fe9b3e047f387413fe9a4dfa42b06b23fe995cf2ed80d223fe986aef14575943fe9777ef4c7d7423fe9683f42bd7fe13fe958efe48e6dd73fe94990e3ac4a6c3fe93a22499263fb3fe92aa41fc5a8153fe91b166fd49da23fe90b7943575efe3fe8fbcca3ef940d3fe8ec109b486c493fe8dc45331698cc3fe8cc6a751846553fe8bc806b1517413fe8ac871ede1d883fe89c7e9a4dd4aa3fe88c66e7481ba13fe87c400fba2ebf3fe86c0a1d9aa1953fe85bc51ae958cc3fe84b7111af83fa3fe83b0e0bff976e3fe82a9c13f545ff3fe81a1b33b57acc3fe8098b756e52fa3fe7f8ece35717713fe7e83f87b036863fe7d7836cc33db23fe7c6b89ce2d3333fe7b5df226aafaf3fe7a4f707bf97d23fe79400574f55e53fe782fb1b90b35b3fe771e75f0372613fe760c52c3047643fe74f948da8d28d3fe73e558e0799423fe72d0837efff963fe71bac960e41bf3fe70a42b3176d7a3fe6f8ca99c95b753fe6e74454eaa8af3fe6d5afef4aafcd3fe6c40d73c182753fe6b25ced2fe29c3fe6a09e667f3bcd3fe68ed1eaa19c713fe67cf78491af103fe66b0f3f52b3863fe6591925f0783d3fe64715437f535b3fe63503a31c1be93fe622e44fec22ff3fe610b7551d2cdf3fe5fe7cbde56a103fe5ec34958370743fe5d9dee73e345c3fe5c77bbe65018c3fe5b50b264f74483fe5a28d2a5d72503fe59001d5f723df3fe57d69348ceca03fe56ac35197649f3fe55810389751373fe5454ff5159dfc3fe5328292a355963fe51fa81cd99aa63fe50cc09f59a09b3fe4f9cc25cca4863fe4e6cabbe3e5e93fe4d3bc6d589f7f3fe4c0a145ec00043fe4ad79516722f13fe49a449b9b09393fe48703306091ff3fe473b51b9873473fe4605a692b32a23fe44cf325091dd63fe4397f5b2a43803fe425ff178e6bb13fe41272663d108c3fe3fed9534556d43fe3eb33eabe06803fe3d78238c583443fe3c3c44981c5183fe3affa292050b93fe39c23e3d630293fe3884185dfeb223fe374531b817f8d3fe36058b10659f33fe34c5252c14de13fe338400d0c8e573fe32421ec49a61f3fe30ff7fce170353fe2fbc24b4410153fe2e780e3e8ea173fe2d333d34e9bb83fe2bedb25faf3ea3fe2aa76e87aeb583fe2960727629ca83fe2818bef4d3cba3fe26d054cdd12df3fe258734cbb71103fe243d5fb98ac1f3fe22f2d662c13e23fe21a799933eb593fe205baa17560d63fe1f0f08bbc861b3fe1dc1b64dc48723fe1c73b39ae68c83fe1b250171373bf3fe19d5a09f2b9b83fe188591f3a46e53fe1734d63dedb493fe15e36e4dbe2bc3fe14915af336ceb3fe133e9cfee254f3fe11eb3541b4b233fe1097248d0a9573fe0f426bb2a8e7e3fe0ded0b84bc4b63fe0c9704d5d898f3fe0b405878f85ec3fe09e907417c5e13fe089112032b08c3fe073879922ffee3fe05df3ec31b8b73fe0485626ae221a3fe032ae55edbd963fe01cfc874c3eb73fe00740c82b82e13fdfe2f64be712103fdfb7575c24d2de3fdf8ba4dbf89aba3fdf5fdee656cda33fdf3405963fd0673fdf081906bff7fe3fdedc1952ef78d63fdeb00695f256203fde83e0eaf851143fde57a86d3cd8253fde2b5d3806f63b3fddfeff66a941de3fddd28f1481cc583fdda60c5cfa10d93fdd79775b86e3893fdd4cd02ba8609d3fdd2016e8e9db5b3fdcf34baee1cd213fdcc66e9931c45e3fdc997fc38653893fdc6c7f4997000b3fdc3f6d472631293fdc1249d8011ee73fdbe51517ffc0d93fdbb7cf2304bd013fdb8a7814fd56933fdb5d1009e15cc03fdb2f971db319723fdb020d6c7f40093fdad473125cdc093fdaa6c82b6d3fca3fda790cd3dbf31b3fda4b4127dea1e53fda1d6543b50ac03fd9ef7943a8ed8a3fd9c17d440df9f23fd993716141bdff3fd96555b7ab948f3fd9372a63bc93d73fd908ef81ef7bd13fd8daa52ec8a4b03fd8ac4b86d5ed443fd87de2a6aea9633fd84f6aaaf3903f3fd820e3b04eaac43fd7f24dd37341e43fd7c3a9311dcce73fd794f5e613dfae3fd766340f2418f63fd73763c92610923fd7088530fa459f3fd6d998638a0cb63fd6aa9d7dc77e173fd67b949cad63cb3fd64c7ddd3f27c63fd61d595c88c2023fd5ee27379ea6933fd5bee78b9db3b63fd58f9a75ab1fdd3fd5604012f467b43fd530d880af3c243fd50163dc1970483fd4d1e24278e76a3fd4a253d11b82f33fd472b8a55710543fd44310dc8936f03fd4135c941766013fd3e39be96ec2713fd3b3cefa0414b73fd383f5e353b6ab3fd35410c2e181523fd3241fb638baaf3fd2f422daec03873fd2c41a4e9545203fd294062ed59f063fd263e6995554ba3fd233bbabc3bb713fd2038583d727be3fd1d3443f4cdb3e3fd1a2f7fbe8f2433fd172a0d77651773fd1423eefc693783fd111d262b1f6773fd0e15b4e1749ce3fd0b0d9cfdbdb903fd0804e05eb661e3fd04fb80e37fdae3fd01f1806b9fdd23fcfdcdc1adfedf93fcf7b7480bd38023fcf19f97b215f1b3fceb86b462de3483fce56ca1e101a1b3fcdf5163f01099a3fcd934fe54543113fcd31774d2cbdee3fcccf8cb312b2863fcc6d90535d74dd3fcc0b826a7e4f633fcba96334f15dad3fcb4732ef3d67223fcae4f1d5f3b9ab3fca82a025b004513fca203e1b1831da3fc9bdcbf2dc43663fc95b49e9b62afa3fc8f8b83c69a60b3fc8961727c418043fc83366e89c64c63fc7d0a7bbd2cb1c3fc76dd9de50bf313fc70afd8d08c4ff3fc6a81304f64ab23fc6451a831d830d3fc5e214448b3fc63fc57f008654cbde3fc51bdf8597c5f23fc4b8b17f79fa883fc45576b1293e5a3fc3f22f57db48933fc38edbb0cd8d143fc32b7bf94516a73fc2c8106e8e613a3fc264994dfd34093fc20116d4ec7bcf3fc19d8940be24e73fc139f0cedaf5773fc0d64dbcb267863fc072a047ba831d3fc00ee8ad6fb85b3fbf564e56a9730e3fbe8eb7fde4aa3f3fbdc70ecbae9fc93fbcff533b307dc13fbc3785c79ec2d53fbb6fa6ec38f64c3fbaa7b724495c033fb9dfb6eb24a85c3fb917a6bc29b42c3fb84f8712c130a13fb787586a5d5b213fb6bf1b3e79b1293fb5f6d00a9aa4193fb52e774a4d4d0a3fb46611792720963fb39d9f12c5a2993fb2d52092ce19f63fb20c9674ed444d3fb1440134d709b33fb07b614e4630643faf656e79f820e03fadd406f9808ec93fac428d12c0d7e33faab101bd5f83173fa91f65f10dd8143fa78dbaa58746863fa5fc00d290cd433fa46a396ff861793fa2d865759455cd3fa14685db42c17f3f9f693731d1cf013f9c454f4ce53b1d3f992155f7a3667e3f95fd4d21fab2263f92d936bbe30efd3f8f6a296ab997cb3f8921d1fcdec7843f82d96b0e5097033f7921f0fe6700713f6921f8becca4ba8000000000000000";
|
|
24
|
+
/**
|
|
25
|
+
* GM[2k] + i * GM[2k + 1] = exp(i * pi * brv(k) / 1024), where brv reverses
|
|
26
|
+
* 10 bits (fpr_gm_tab of the reference implementation).
|
|
27
|
+
*/
|
|
28
|
+
const GM = /* @__PURE__ */ (() => {
|
|
29
|
+
const view = new DataView(new ArrayBuffer(8));
|
|
30
|
+
const cos = new Float64Array(513);
|
|
31
|
+
for (let j = 0; j <= 512; j++) {
|
|
32
|
+
for (let b = 0; b < 8; b++) {
|
|
33
|
+
view.setUint8(b, parseInt(COS_HEX.substring(16 * j + 2 * b, 16 * j + 2 * b + 2), 16));
|
|
34
|
+
}
|
|
35
|
+
cos[j] = view.getFloat64(0);
|
|
36
|
+
}
|
|
37
|
+
const gm = new Float64Array(2048);
|
|
38
|
+
for (let k = 1; k < 1024; k++) {
|
|
39
|
+
let j = 0;
|
|
40
|
+
for (let b = 0; b < 10; b++) {
|
|
41
|
+
j |= ((k >> b) & 1) << (9 - b);
|
|
42
|
+
}
|
|
43
|
+
gm[2 * k] = j <= 512 ? cos[j] : -cos[1024 - j];
|
|
44
|
+
gm[2 * k + 1] = cos[j <= 512 ? 512 - j : j - 512];
|
|
45
|
+
}
|
|
46
|
+
return gm;
|
|
47
|
+
})();
|
|
48
|
+
/** Converts a polynomial to the FFT representation, in place. */
|
|
49
|
+
export function FFT(f, logn) {
|
|
50
|
+
const n = 1 << logn;
|
|
51
|
+
const hn = n >> 1;
|
|
52
|
+
let t = hn;
|
|
53
|
+
for (let u = 1, m = 2; u < logn; u++, m <<= 1) {
|
|
54
|
+
const ht = t >> 1;
|
|
55
|
+
const hm = m >> 1;
|
|
56
|
+
for (let i1 = 0, j1 = 0; i1 < hm; i1++, j1 += t) {
|
|
57
|
+
const j2 = j1 + ht;
|
|
58
|
+
const sRe = GM[(m + i1) << 1];
|
|
59
|
+
const sIm = GM[((m + i1) << 1) + 1];
|
|
60
|
+
for (let j = j1; j < j2; j++) {
|
|
61
|
+
const xRe = f[j];
|
|
62
|
+
const xIm = f[j + hn];
|
|
63
|
+
const aRe = f[j + ht];
|
|
64
|
+
const aIm = f[j + ht + hn];
|
|
65
|
+
const yRe = aRe * sRe - aIm * sIm;
|
|
66
|
+
const yIm = aRe * sIm + aIm * sRe;
|
|
67
|
+
f[j] = xRe + yRe;
|
|
68
|
+
f[j + hn] = xIm + yIm;
|
|
69
|
+
f[j + ht] = xRe - yRe;
|
|
70
|
+
f[j + ht + hn] = xIm - yIm;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
t = ht;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Converts a polynomial back from the FFT representation, in place. */
|
|
77
|
+
export function iFFT(f, logn) {
|
|
78
|
+
const n = 1 << logn;
|
|
79
|
+
const hn = n >> 1;
|
|
80
|
+
let t = 1;
|
|
81
|
+
let m = n;
|
|
82
|
+
for (let u = logn; u > 1; u--) {
|
|
83
|
+
const hm = m >> 1;
|
|
84
|
+
const dt = t << 1;
|
|
85
|
+
for (let i1 = 0, j1 = 0; j1 < hn; i1++, j1 += dt) {
|
|
86
|
+
const j2 = j1 + t;
|
|
87
|
+
const sRe = GM[(hm + i1) << 1];
|
|
88
|
+
const sIm = -GM[((hm + i1) << 1) + 1];
|
|
89
|
+
for (let j = j1; j < j2; j++) {
|
|
90
|
+
const xRe = f[j];
|
|
91
|
+
const xIm = f[j + hn];
|
|
92
|
+
const yRe = f[j + t];
|
|
93
|
+
const yIm = f[j + t + hn];
|
|
94
|
+
f[j] = xRe + yRe;
|
|
95
|
+
f[j + hn] = xIm + yIm;
|
|
96
|
+
const dRe = xRe - yRe;
|
|
97
|
+
const dIm = xIm - yIm;
|
|
98
|
+
f[j + t] = dRe * sRe - dIm * sIm;
|
|
99
|
+
f[j + t + hn] = dRe * sIm + dIm * sRe;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
t = dt;
|
|
103
|
+
m = hm;
|
|
104
|
+
}
|
|
105
|
+
if (logn > 0) {
|
|
106
|
+
// 2 / n, a power of two.
|
|
107
|
+
const ni = 2 / n;
|
|
108
|
+
for (let u = 0; u < n; u++) {
|
|
109
|
+
f[u] *= ni;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** a += b */
|
|
114
|
+
export function polyAdd(a, b, logn) {
|
|
115
|
+
const n = 1 << logn;
|
|
116
|
+
for (let u = 0; u < n; u++) {
|
|
117
|
+
a[u] += b[u];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** a -= b */
|
|
121
|
+
export function polySub(a, b, logn) {
|
|
122
|
+
const n = 1 << logn;
|
|
123
|
+
for (let u = 0; u < n; u++) {
|
|
124
|
+
a[u] -= b[u];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** a = -a */
|
|
128
|
+
export function polyNeg(a, logn) {
|
|
129
|
+
const n = 1 << logn;
|
|
130
|
+
for (let u = 0; u < n; u++) {
|
|
131
|
+
a[u] = -a[u];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** a = adj(a), in the FFT representation. */
|
|
135
|
+
export function polyAdjFft(a, logn) {
|
|
136
|
+
const n = 1 << logn;
|
|
137
|
+
for (let u = n >> 1; u < n; u++) {
|
|
138
|
+
a[u] = -a[u];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** a *= b, in the FFT representation. */
|
|
142
|
+
export function polyMulFft(a, b, logn) {
|
|
143
|
+
const hn = (1 << logn) >> 1;
|
|
144
|
+
for (let u = 0; u < hn; u++) {
|
|
145
|
+
const aRe = a[u];
|
|
146
|
+
const aIm = a[u + hn];
|
|
147
|
+
const bRe = b[u];
|
|
148
|
+
const bIm = b[u + hn];
|
|
149
|
+
a[u] = aRe * bRe - aIm * bIm;
|
|
150
|
+
a[u + hn] = aRe * bIm + aIm * bRe;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** a *= adj(b), in the FFT representation. */
|
|
154
|
+
export function polyMuladjFft(a, b, logn) {
|
|
155
|
+
const hn = (1 << logn) >> 1;
|
|
156
|
+
for (let u = 0; u < hn; u++) {
|
|
157
|
+
const aRe = a[u];
|
|
158
|
+
const aIm = a[u + hn];
|
|
159
|
+
const bRe = b[u];
|
|
160
|
+
const bIm = -b[u + hn];
|
|
161
|
+
a[u] = aRe * bRe - aIm * bIm;
|
|
162
|
+
a[u + hn] = aRe * bIm + aIm * bRe;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** a *= adj(a), in the FFT representation. */
|
|
166
|
+
export function polyMulselfadjFft(a, logn) {
|
|
167
|
+
const hn = (1 << logn) >> 1;
|
|
168
|
+
for (let u = 0; u < hn; u++) {
|
|
169
|
+
const aRe = a[u];
|
|
170
|
+
const aIm = a[u + hn];
|
|
171
|
+
a[u] = aRe * aRe + aIm * aIm;
|
|
172
|
+
a[u + hn] = 0;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/** a *= x, for a real constant x. */
|
|
176
|
+
export function polyMulconst(a, x, logn) {
|
|
177
|
+
const n = 1 << logn;
|
|
178
|
+
for (let u = 0; u < n; u++) {
|
|
179
|
+
a[u] *= x;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/** a /= b, in the FFT representation. */
|
|
183
|
+
export function polyDivFft(a, b, logn) {
|
|
184
|
+
const hn = (1 << logn) >> 1;
|
|
185
|
+
for (let u = 0; u < hn; u++) {
|
|
186
|
+
const aRe = a[u];
|
|
187
|
+
const aIm = a[u + hn];
|
|
188
|
+
let bRe = b[u];
|
|
189
|
+
let bIm = b[u + hn];
|
|
190
|
+
const m = 1 / (bRe * bRe + bIm * bIm);
|
|
191
|
+
bRe = bRe * m;
|
|
192
|
+
bIm = -bIm * m;
|
|
193
|
+
a[u] = aRe * bRe - aIm * bIm;
|
|
194
|
+
a[u + hn] = aRe * bIm + aIm * bRe;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* d = 1 / (a * adj(a) + b * adj(b)), in the FFT representation. The result
|
|
199
|
+
* is real: only the first half of d is set.
|
|
200
|
+
*/
|
|
201
|
+
export function polyInvnorm2Fft(d, a, b, logn) {
|
|
202
|
+
const hn = (1 << logn) >> 1;
|
|
203
|
+
for (let u = 0; u < hn; u++) {
|
|
204
|
+
const aRe = a[u];
|
|
205
|
+
const aIm = a[u + hn];
|
|
206
|
+
const bRe = b[u];
|
|
207
|
+
const bIm = b[u + hn];
|
|
208
|
+
d[u] = 1 / ((aRe * aRe + aIm * aIm) + (bRe * bRe + bIm * bIm));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/** d = F * adj(f) + G * adj(g), in the FFT representation. */
|
|
212
|
+
export function polyAddMuladjFft(d, F, G, f, g, logn) {
|
|
213
|
+
const hn = (1 << logn) >> 1;
|
|
214
|
+
for (let u = 0; u < hn; u++) {
|
|
215
|
+
const FRe = F[u];
|
|
216
|
+
const FIm = F[u + hn];
|
|
217
|
+
const GRe = G[u];
|
|
218
|
+
const GIm = G[u + hn];
|
|
219
|
+
const fRe = f[u];
|
|
220
|
+
const fIm = -f[u + hn];
|
|
221
|
+
const gRe = g[u];
|
|
222
|
+
const gIm = -g[u + hn];
|
|
223
|
+
const aRe = FRe * fRe - FIm * fIm;
|
|
224
|
+
const aIm = FRe * fIm + FIm * fRe;
|
|
225
|
+
const bRe = GRe * gRe - GIm * gIm;
|
|
226
|
+
const bIm = GRe * gIm + GIm * gRe;
|
|
227
|
+
d[u] = aRe + bRe;
|
|
228
|
+
d[u + hn] = aIm + bIm;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* a *= b, where b is self-adjoint (real in the FFT representation): only
|
|
233
|
+
* the first half of b is read.
|
|
234
|
+
*/
|
|
235
|
+
export function polyMulAutoadjFft(a, b, logn) {
|
|
236
|
+
const hn = (1 << logn) >> 1;
|
|
237
|
+
for (let u = 0; u < hn; u++) {
|
|
238
|
+
a[u] *= b[u];
|
|
239
|
+
a[u + hn] *= b[u];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* a /= b, where b is self-adjoint (real in the FFT representation): only
|
|
244
|
+
* the first half of b is read.
|
|
245
|
+
*/
|
|
246
|
+
export function polyDivAutoadjFft(a, b, logn) {
|
|
247
|
+
const hn = (1 << logn) >> 1;
|
|
248
|
+
for (let u = 0; u < hn; u++) {
|
|
249
|
+
const ib = 1 / b[u];
|
|
250
|
+
a[u] *= ib;
|
|
251
|
+
a[u + hn] *= ib;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The LDL decomposition of the self-adjoint matrix [[g00, g01], [adj(g01),
|
|
256
|
+
* g11]]: d11 and l10 are set so that it is L * diag(g00, d11) * adj(L) with
|
|
257
|
+
* L = [[1, 0], [l10, 1]]. The inputs are not modified.
|
|
258
|
+
*/
|
|
259
|
+
export function polyLDLmvFft(d11, l10, g00, g01, g11, logn) {
|
|
260
|
+
const hn = (1 << logn) >> 1;
|
|
261
|
+
for (let u = 0; u < hn; u++) {
|
|
262
|
+
const g00Re = g00[u];
|
|
263
|
+
const g00Im = g00[u + hn];
|
|
264
|
+
const g01Re = g01[u];
|
|
265
|
+
const g01Im = g01[u + hn];
|
|
266
|
+
const g11Re = g11[u];
|
|
267
|
+
const g11Im = g11[u + hn];
|
|
268
|
+
// mu = g01 / g00
|
|
269
|
+
const m = 1 / (g00Re * g00Re + g00Im * g00Im);
|
|
270
|
+
const iRe = g00Re * m;
|
|
271
|
+
const iIm = -g00Im * m;
|
|
272
|
+
const muRe = g01Re * iRe - g01Im * iIm;
|
|
273
|
+
const muIm = g01Re * iIm + g01Im * iRe;
|
|
274
|
+
// mu * adj(g01)
|
|
275
|
+
const cIm = -g01Im;
|
|
276
|
+
const tRe = muRe * g01Re - muIm * cIm;
|
|
277
|
+
const tIm = muRe * cIm + muIm * g01Re;
|
|
278
|
+
d11[u] = g11Re - tRe;
|
|
279
|
+
d11[u + hn] = g11Im - tIm;
|
|
280
|
+
l10[u] = muRe;
|
|
281
|
+
l10[u + hn] = -muIm;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Splits f, in the FFT representation, to f0 and f1 of half its degree,
|
|
286
|
+
* such that f = f0(x^2) + x * f1(x^2).
|
|
287
|
+
*/
|
|
288
|
+
export function polySplitFft(f0, f1, f, logn) {
|
|
289
|
+
const hn = (1 << logn) >> 1;
|
|
290
|
+
const qn = hn >> 1;
|
|
291
|
+
// For n = 2, which the loop below does not cover.
|
|
292
|
+
f0[0] = f[0];
|
|
293
|
+
f1[0] = f[hn];
|
|
294
|
+
for (let u = 0; u < qn; u++) {
|
|
295
|
+
const aRe = f[u << 1];
|
|
296
|
+
const aIm = f[(u << 1) + hn];
|
|
297
|
+
const bRe = f[(u << 1) + 1];
|
|
298
|
+
const bIm = f[(u << 1) + 1 + hn];
|
|
299
|
+
f0[u] = (aRe + bRe) * 0.5;
|
|
300
|
+
f0[u + qn] = (aIm + bIm) * 0.5;
|
|
301
|
+
const tRe = aRe - bRe;
|
|
302
|
+
const tIm = aIm - bIm;
|
|
303
|
+
const sRe = GM[(u + hn) << 1];
|
|
304
|
+
const sIm = -GM[((u + hn) << 1) + 1];
|
|
305
|
+
f1[u] = (tRe * sRe - tIm * sIm) * 0.5;
|
|
306
|
+
f1[u + qn] = (tRe * sIm + tIm * sRe) * 0.5;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/** The inverse of {@link polySplitFft}. */
|
|
310
|
+
export function polyMergeFft(f, f0, f1, logn) {
|
|
311
|
+
const hn = (1 << logn) >> 1;
|
|
312
|
+
const qn = hn >> 1;
|
|
313
|
+
f[0] = f0[0];
|
|
314
|
+
f[hn] = f1[0];
|
|
315
|
+
for (let u = 0; u < qn; u++) {
|
|
316
|
+
const aRe = f0[u];
|
|
317
|
+
const aIm = f0[u + qn];
|
|
318
|
+
const cRe = f1[u];
|
|
319
|
+
const cIm = f1[u + qn];
|
|
320
|
+
const sRe = GM[(u + hn) << 1];
|
|
321
|
+
const sIm = GM[((u + hn) << 1) + 1];
|
|
322
|
+
const bRe = cRe * sRe - cIm * sIm;
|
|
323
|
+
const bIm = cRe * sIm + cIm * sRe;
|
|
324
|
+
f[u << 1] = aRe + bRe;
|
|
325
|
+
f[(u << 1) + hn] = aIm + bIm;
|
|
326
|
+
f[(u << 1) + 1] = aRe - bRe;
|
|
327
|
+
f[(u << 1) + 1 + hn] = aIm - bIm;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { FnDsaCreateOptions, FnDsaSignerInterface, FnDsaVerifierInterface } from "./interfaces.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generates a key pair of FN-DSA-1024 (NIST security category 5).
|
|
4
|
+
*
|
|
5
|
+
* The public key is 1793 octets in length, and the private key 2305 octets.
|
|
6
|
+
*
|
|
7
|
+
* @returns A promise that resolves to a key pair [publicKey, privateKey].
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {
|
|
13
|
+
* createFnDsa1024Signer,
|
|
14
|
+
* createFnDsa1024Verifier,
|
|
15
|
+
* generateFnDsa1024KeyPair,
|
|
16
|
+
* } from "@webpqc/fndsa";
|
|
17
|
+
*
|
|
18
|
+
* const [pk, sk] = await generateFnDsa1024KeyPair();
|
|
19
|
+
*
|
|
20
|
+
* const signer = await createFnDsa1024Signer(sk);
|
|
21
|
+
* const msg = new TextEncoder().encode("hello world");
|
|
22
|
+
* const sig = await signer.sign(msg);
|
|
23
|
+
* signer.destroy();
|
|
24
|
+
*
|
|
25
|
+
* const verifier = await createFnDsa1024Verifier(pk);
|
|
26
|
+
* const ok = await verifier.verify(sig, msg);
|
|
27
|
+
* // ok === true
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function generateFnDsa1024KeyPair(): Promise<[Uint8Array, Uint8Array]>;
|
|
31
|
+
/**
|
|
32
|
+
* Derives a key pair of FN-DSA-1024 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 (2305 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 deriveFnDsa1024KeyPair(seed: Uint8Array): Promise<[Uint8Array, Uint8Array]>;
|
|
42
|
+
/**
|
|
43
|
+
* Creates an FN-DSA-1024 (NIST security category 5) signer which holds a
|
|
44
|
+
* private key.
|
|
45
|
+
*
|
|
46
|
+
* The signature is 1280 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 (2305 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 { createFnDsa1024Signer, generateFnDsa1024KeyPair } from "@webpqc/fndsa";
|
|
57
|
+
*
|
|
58
|
+
* const [_pk, sk] = await generateFnDsa1024KeyPair();
|
|
59
|
+
* const signer = await createFnDsa1024Signer(sk);
|
|
60
|
+
* const sig = await signer.sign(new TextEncoder().encode("hello world"));
|
|
61
|
+
* signer.destroy();
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function createFnDsa1024Signer(sk: Uint8Array, opts?: FnDsaCreateOptions): Promise<FnDsaSignerInterface>;
|
|
65
|
+
/**
|
|
66
|
+
* Creates an FN-DSA-1024 (NIST security category 5) 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 (1793 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 { createFnDsa1024Verifier } from "@webpqc/fndsa";
|
|
80
|
+
*
|
|
81
|
+
* async function verify(pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) {
|
|
82
|
+
* const verifier = await createFnDsa1024Verifier(pk);
|
|
83
|
+
* return await verifier.verify(sig, msg);
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function createFnDsa1024Verifier(pk: Uint8Array, opts?: FnDsaCreateOptions): Promise<FnDsaVerifierInterface>;
|
|
88
|
+
//# sourceMappingURL=fnDsa1024.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fnDsa1024.d.ts","sourceRoot":"","sources":["../../src/src/fnDsa1024.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AAGzB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,wBAAwB,IAAI,OAAO,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAE5E;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAEnC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,qBAAqB,CACnC,EAAE,EAAE,UAAU,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,oBAAoB,CAAC,CAE/B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,uBAAuB,CACrC,EAAE,EAAE,UAAU,EACd,IAAI,CAAC,EAAE,kBAAkB,GACxB,OAAO,CAAC,sBAAsB,CAAC,CAEjC"}
|