@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.
Files changed (70) hide show
  1. package/LICENSE +49 -0
  2. package/README.md +221 -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/basis.d.ts +11 -0
  8. package/esm/src/basis.d.ts.map +1 -0
  9. package/esm/src/basis.js +78 -0
  10. package/esm/src/codec.d.ts +28 -0
  11. package/esm/src/codec.d.ts.map +1 -0
  12. package/esm/src/codec.js +212 -0
  13. package/esm/src/common.d.ts +49 -0
  14. package/esm/src/common.d.ts.map +1 -0
  15. package/esm/src/common.js +112 -0
  16. package/esm/src/consts.d.ts +11 -0
  17. package/esm/src/consts.d.ts.map +1 -0
  18. package/esm/src/consts.js +10 -0
  19. package/esm/src/deps.d.ts +2 -0
  20. package/esm/src/deps.d.ts.map +1 -0
  21. package/esm/src/deps.js +1 -0
  22. package/esm/src/errors.d.ts +7 -0
  23. package/esm/src/errors.d.ts.map +1 -0
  24. package/esm/src/errors.js +19 -0
  25. package/esm/src/fft.d.ts +58 -0
  26. package/esm/src/fft.d.ts.map +1 -0
  27. package/esm/src/fft.js +364 -0
  28. package/esm/src/fnDsa1024.d.ts +88 -0
  29. package/esm/src/fnDsa1024.d.ts.map +1 -0
  30. package/esm/src/fnDsa1024.js +98 -0
  31. package/esm/src/fnDsa512.d.ts +88 -0
  32. package/esm/src/fnDsa512.d.ts.map +1 -0
  33. package/esm/src/fnDsa512.js +98 -0
  34. package/esm/src/fnDsaKeyGen.d.ts +6 -0
  35. package/esm/src/fnDsaKeyGen.d.ts.map +1 -0
  36. package/esm/src/fnDsaKeyGen.js +92 -0
  37. package/esm/src/fnDsaSigner.d.ts +30 -0
  38. package/esm/src/fnDsaSigner.d.ts.map +1 -0
  39. package/esm/src/fnDsaSigner.js +211 -0
  40. package/esm/src/fnDsaVerifier.d.ts +24 -0
  41. package/esm/src/fnDsaVerifier.d.ts.map +1 -0
  42. package/esm/src/fnDsaVerifier.js +134 -0
  43. package/esm/src/interfaces.d.ts +49 -0
  44. package/esm/src/interfaces.d.ts.map +1 -0
  45. package/esm/src/interfaces.js +1 -0
  46. package/esm/src/keygen.d.ts +29 -0
  47. package/esm/src/keygen.d.ts.map +1 -0
  48. package/esm/src/keygen.js +1956 -0
  49. package/esm/src/native.d.ts +15 -0
  50. package/esm/src/native.d.ts.map +1 -0
  51. package/esm/src/native.js +16 -0
  52. package/esm/src/params.d.ts +20 -0
  53. package/esm/src/params.d.ts.map +1 -0
  54. package/esm/src/params.js +18 -0
  55. package/esm/src/rng.d.ts +22 -0
  56. package/esm/src/rng.d.ts.map +1 -0
  57. package/esm/src/rng.js +151 -0
  58. package/esm/src/sha3/keccak.d.ts +58 -0
  59. package/esm/src/sha3/keccak.d.ts.map +1 -0
  60. package/esm/src/sha3/keccak.js +500 -0
  61. package/esm/src/sign.d.ts +31 -0
  62. package/esm/src/sign.d.ts.map +1 -0
  63. package/esm/src/sign.js +718 -0
  64. package/esm/src/utils.d.ts +24 -0
  65. package/esm/src/utils.d.ts.map +1 -0
  66. package/esm/src/utils.js +51 -0
  67. package/esm/src/vrfy.d.ts +27 -0
  68. package/esm/src/vrfy.d.ts.map +1 -0
  69. package/esm/src/vrfy.js +242 -0
  70. package/package.json +49 -4
package/LICENSE ADDED
@@ -0,0 +1,49 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Ajitomi Daisuke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ --------------------------------------------------------------------------------
24
+
25
+ This package is a port of the reference implementation of Falcon (round 3,
26
+ https://falcon-sign.info/), which is under the following license:
27
+
28
+ MIT License
29
+
30
+ Copyright (c) 2017-2019 Falcon Project
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining
33
+ a copy of this software and associated documentation files (the
34
+ "Software"), to deal in the Software without restriction, including
35
+ without limitation the rights to use, copy, modify, merge, publish,
36
+ distribute, sublicense, and/or sell copies of the Software, and to
37
+ permit persons to whom the Software is furnished to do so, subject to
38
+ the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be
41
+ included in all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
44
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,222 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/dajiaji/webpqc/main/assets/webpqc-logo.png" alt="webpqc logo" width="200" />
3
+ </p>
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ <h1 align="center">@webpqc/fndsa</h1>
6
+
7
+ <div align="center">
8
+ <a href="https://jsr.io/@webpqc/fndsa"><img src="https://jsr.io/badges/@webpqc/fndsa" alt="JSR"/></a>
9
+ <a href="https://www.npmjs.com/package/@webpqc/fndsa"><img src="https://img.shields.io/npm/v/@webpqc/fndsa?color=%23EE3214" alt="NPM"/></a>
10
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_browsers.yml/badge.svg" alt="Browser CI" />
11
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_node.yml/badge.svg" alt="Node.js CI" />
12
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_deno.yml/badge.svg" alt="Deno CI" />
13
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_cloudflare.yml/badge.svg" alt="Cloudflare Workers CI" />
14
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_bun.yml/badge.svg" alt="bun CI" />
15
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_llrt.yml/badge.svg" alt="LLRT CI" />
16
+ </div>
17
+
18
+ <div align="center">
19
+ An experimental FN-DSA (Falcon) implementation written in TypeScript.<br>
20
+ </div>
21
+ <p></p>
22
+
23
+ <div align="center">
24
+
25
+ [Documentation for main](https://dajiaji.github.io/webpqc/docs/fndsa/main/)
26
+
27
+ </div>
28
+
29
+ > [!WARNING]
30
+ > FN-DSA is not standardized yet: FIPS 206 has not been published, even as a
31
+ > draft. This package implements **Falcon of the round 3 submission**
32
+ > ([Falcon specification v1.2](https://falcon-sign.info/falcon.pdf)), from which
33
+ > FN-DSA is derived, under the names which FIPS 206 is expected to use. It is
34
+ > **not compatible with FN-DSA**: NIST has announced that FIPS 206 changes the
35
+ > hashing of the message, and specifies key generation and signing with
36
+ > fixed-point arithmetic. The keys and the signatures of this package will
37
+ > change when the draft is published.
38
+
39
+ > [!WARNING]
40
+ > This package is at an early stage. The API may change, and the implementation
41
+ > has not been audited. See also [Limitations](#limitations): signing uses the
42
+ > floating-point arithmetic of JavaScript on secret values.
43
+
44
+ - ✅ Falcon-512 and Falcon-1024 of the round 3 submission, as FN-DSA-512 and
45
+ FN-DSA-1024.
46
+ - ✅ Written in TypeScript.
47
+ - ✅ Deterministic key generation support.
48
+ - ✅ The same outputs as the reference implementation, bit for bit: only the
49
+ floating-point operations which IEEE 754 defines exactly are used, in the same
50
+ order.
51
+ - ✅ No dependencies on other libraries.
52
+ - ✅ Tree-shaking friendly: key generation, the signer and the verifier are
53
+ separated, so an application bundles only the code of the roles which it uses.
54
+ Verification uses only integers.
55
+ - ✅ The same API as [`@webpqc/mldsa`](../mldsa/) and
56
+ [`@webpqc/slhdsa`](../slhdsa/): a signer holds a private key and a verifier a
57
+ public key, and all the operations are asynchronous.
58
+ - ✅ Passed the Known Answer Test values of the round 3 submission (100 vectors
59
+ for each parameter set): the key pairs are the same for 200 of 200, and the
60
+ signatures, once converted to the format of this package, for 199 of 200. The
61
+ other one (`count = 82` of Falcon-1024) is of 1281 octets, which does not fit
62
+ in the fixed size of 1280: another signature is sampled instead, as the padded
63
+ format of the reference implementation does.
64
+
65
+ ## Installation
66
+
67
+ `@webpqc/fndsa` is published on [JSR](https://jsr.io/@webpqc/fndsa) and
68
+ [npm](https://www.npmjs.com/package/@webpqc/fndsa). It is an ESM-only package,
69
+ and requires Node.js 20 or later.
70
+
71
+ ```sh
72
+ # Node.js, Cloudflare Workers and Bun, using npm:
73
+ npm install @webpqc/fndsa
74
+ # Node.js, Cloudflare Workers and Bun, using jsr:
75
+ npx jsr add @webpqc/fndsa
76
+ # Deno:
77
+ deno add jsr:@webpqc/fndsa
78
+ ```
79
+
80
+ ## Usage
81
+
82
+ ```ts
83
+ import {
84
+ createFnDsa512Signer,
85
+ createFnDsa512Verifier,
86
+ generateFnDsa512KeyPair,
87
+ } from "@webpqc/fndsa";
88
+
89
+ // A signer generates a key pair.
90
+ // The functions of FN-DSA-1024 are also available.
91
+ const [pk, sk] = await generateFnDsa512KeyPair();
92
+ //// Deterministic key generation is also supported
93
+ // import { deriveFnDsa512KeyPair } from "@webpqc/fndsa";
94
+ // const seed = new Uint8Array(48);
95
+ // globalThis.crypto.getRandomValues(seed);
96
+ // const [pk, sk] = await deriveFnDsa512KeyPair(seed);
97
+
98
+ // The signer creates a signer which holds sk, and signs a message with it.
99
+ const signer = await createFnDsa512Signer(sk);
100
+ const msg = new TextEncoder().encode("hello world");
101
+ const sig = await signer.sign(msg);
102
+
103
+ // Erase the private key which the signer holds, when it is no longer needed.
104
+ signer.destroy();
105
+
106
+ // A verifier creates a verifier which holds pk, and verifies the signature.
107
+ const verifier = await createFnDsa512Verifier(pk);
108
+ const ok = await verifier.verify(sig, msg);
109
+ // ok === true
110
+ ```
111
+
112
+ ## API
113
+
114
+ The functions of a parameter set are named after it: `FnDsa512` or `FnDsa1024`
115
+ for `*` below. All of them return a promise.
116
+
117
+ | Function | Description |
118
+ | --------------------------------- | ------------------------------------------------------------------ |
119
+ | `generateFnDsa*KeyPair()` | Generates a key pair `[publicKey, privateKey]`. |
120
+ | `deriveFnDsa*KeyPair(seed)` | Derives a key pair `[publicKey, privateKey]` from a 48-octet seed. |
121
+ | `createFnDsa*Signer(sk, opts?)` | Creates a signer which holds a private key. |
122
+ | `createFnDsa*Verifier(pk, opts?)` | Creates a verifier which holds a public key. |
123
+
124
+ | Method | Description |
125
+ | --------------------------- | ---------------------------------------------- |
126
+ | `signer.sign(msg)` | Signs a message: resolves to the signature. |
127
+ | `signer.destroy()` | Erases the private key which the signer holds. |
128
+ | `verifier.verify(sig, msg)` | Resolves to true if the signature is valid. |
129
+
130
+ The sizes are as follows, in octets:
131
+
132
+ | Parameter set | NIST security category | Public key | Private key | Signature |
133
+ | ------------- | ---------------------- | ---------- | ----------- | --------- |
134
+ | FN-DSA-512 | 1 | 897 | 1281 | 666 |
135
+ | FN-DSA-1024 | 5 | 1793 | 2305 | 1280 |
136
+
137
+ - The keys are in the encodings of the Falcon specification (a header octet,
138
+ then h for a public key, and f, g and F for a private key).
139
+ - Unlike the other packages of webpqc, the private key is not the seed which the
140
+ key pair is derived from, as key generation takes much longer than decoding a
141
+ key. `deriveFnDsa*KeyPair(seed)` therefore returns an encoded private key, and
142
+ there is no function which expands a seed. The seed is expanded with SHAKE256,
143
+ as the reference implementation does.
144
+ - A signature is randomized: it has a nonce of 40 random octets, and signing the
145
+ same message again gives another signature. There is neither a deterministic
146
+ variant nor a context string.
147
+ - A signature is in the padded format of the reference implementation: the
148
+ compressed signature (the header `0x30 + logn`, the nonce and the compressed
149
+ s2), padded with zeros to the fixed size above. `verify` also accepts a
150
+ signature in the compressed format, which is the same without the padding and
151
+ may be longer than the fixed size (752 and 1462 octets at most), and nothing
152
+ else: in particular, a signed message of the NIST API has to be converted.
153
+ - `sign` rejects after `destroy()`.
154
+
155
+ The promises are rejected with `FnDsaError` for an invalid argument: what is not
156
+ a `Uint8Array`, a seed or a key of an incorrect length, a key which cannot be
157
+ decoded, or a private key which does not satisfy f * G - g * F = q or of which f
158
+ and g are not as short as key generation requires. `verify` resolves to false
159
+ for a signature which cannot be decoded.
160
+
161
+ See [bench/bundle](../../bench/bundle/) for the bundle sizes of the other
162
+ packages.
163
+
164
+ ### Web Cryptography API
165
+
166
+ `createFnDsa*Signer` and `createFnDsa*Verifier` take the option `useWebCrypto`
167
+ of [`@webpqc/mldsa`](../mldsa/#web-cryptography-api): `"auto"` (default), `true`
168
+ or `false`.
169
+
170
+ ```ts
171
+ const signer = await createFnDsa512Signer(sk, { useWebCrypto: false });
172
+ ```
173
+
174
+ It has no effect so far. FN-DSA is not among the algorithms of
175
+ [Modern Algorithms in the Web Cryptography API](https://wicg.github.io/webcrypto-modern-algos/),
176
+ and no runtime has it, so there is nothing to delegate to and the TypeScript
177
+ implementation always runs. The option is there so that an application need not
178
+ change when a runtime has FN-DSA, which is also why the operations are
179
+ asynchronous.
180
+
181
+ ## Performance
182
+
183
+ Run `deno task bench` in this directory to see the benchmark results. They are
184
+ around the following on a laptop (AMD Ryzen 9 6900HX, Deno 2.9):
185
+
186
+ | Parameter set | `generateFnDsa*KeyPair` | `createFnDsa*Signer` | `sign` | `verify` |
187
+ | ------------- | ----------------------- | -------------------- | ------ | -------- |
188
+ | FN-DSA-512 | 22 ms | 0.32 ms | 1.2 ms | 0.04 ms |
189
+ | FN-DSA-1024 | 68 ms | 0.62 ms | 2.3 ms | 0.07 ms |
190
+
191
+ The time of key generation varies a lot from a key pair to another, as
192
+ candidates are generated until one is suitable. A signer expands its private key
193
+ once, when it is created, to the form which signing uses.
194
+
195
+ ## Limitations
196
+
197
+ - It is Falcon of the round 3 submission, not FN-DSA of FIPS 206: see the
198
+ warning above.
199
+ - Signing computes with the floating-point numbers of JavaScript (IEEE 754
200
+ binary64) on values which depend on the private key, as the reference
201
+ implementation does with the ones of the processor. The operations are
202
+ additions, subtractions, multiplications, divisions and square roots, of which
203
+ the time may depend on the operands on some processors. NIST plans to specify
204
+ FN-DSA with fixed-point arithmetic for this reason.
205
+ - JavaScript gives no guarantee about the execution time. Key generation and
206
+ signing follow the reference implementation, which avoids the branches and the
207
+ table lookups which depend on secret values where its design requires it, but
208
+ the resistance to side-channel attacks cannot be ensured in this language.
209
+ - The signature formats other than the padded and the compressed ones (the
210
+ constant-time format, and the key-recovery mode) are not supported.
211
+
212
+ ## Acknowledgements
213
+
214
+ The implementation is a port of the
215
+ [reference implementation of Falcon](https://falcon-sign.info/) by Thomas
216
+ Pornin, which is under the MIT license. Its copyright notice and license are in
217
+ [LICENSE](./LICENSE), and in a legal comment which bundlers keep.
218
+
219
+ ## Contributing
220
+
221
+ We welcome all kind of contributions, filing issues, suggesting new features or
222
+ sending PRs.
package/esm/mod.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { FnDsaError } from "./src/errors.js";
2
+ export type { FnDsaCreateOptions, FnDsaSignerInterface, FnDsaVerifierInterface, } from "./src/interfaces.js";
3
+ export type { WebCryptoOption } from "./src/native.js";
4
+ export { createFnDsa512Signer, createFnDsa512Verifier, deriveFnDsa512KeyPair, generateFnDsa512KeyPair, } from "./src/fnDsa512.js";
5
+ export { createFnDsa1024Signer, createFnDsa1024Verifier, deriveFnDsa1024KeyPair, generateFnDsa1024KeyPair, } from "./src/fnDsa1024.js";
6
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,YAAY,EACV,kBAAkB,EAClB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,qBAAqB,EACrB,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,qBAAqB,EACrB,uBAAuB,EACvB,sBAAsB,EACtB,wBAAwB,GACzB,MAAM,oBAAoB,CAAC"}
package/esm/mod.js ADDED
@@ -0,0 +1,3 @@
1
+ export { FnDsaError } from "./src/errors.js";
2
+ export { createFnDsa512Signer, createFnDsa512Verifier, deriveFnDsa512KeyPair, generateFnDsa512KeyPair, } from "./src/fnDsa512.js";
3
+ export { createFnDsa1024Signer, createFnDsa1024Verifier, deriveFnDsa1024KeyPair, generateFnDsa1024KeyPair, } from "./src/fnDsa1024.js";
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Whether (g, -f) and its orthogonalized vector are short enough: the
3
+ * bounds which the sampler of signing relies on, as the standard deviations
4
+ * which it is given are in the range it supports only for such a basis.
5
+ *
6
+ * Key generation keeps only the f and g which pass this, and a private key
7
+ * which is imported has to pass it as well. rt1 and rt2 are work areas of n
8
+ * values, and rt3 of n/2: they are left with values which depend on f and g.
9
+ */
10
+ export declare function isShortBasis(f: Int8Array, g: Int8Array, logn: number, rt1: Float64Array, rt2: Float64Array, rt3: Float64Array): boolean;
11
+ //# sourceMappingURL=basis.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basis.d.ts","sourceRoot":"","sources":["../../src/src/basis.ts"],"names":[],"mappings":"AA8CA;;;;;;;;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"}
@@ -0,0 +1,78 @@
1
+ /*
2
+ * The bounds of a private key, which key generation ensures and which the
3
+ * sampler of signing relies on.
4
+ *
5
+ * This is a part of keygen.c of the reference implementation of Falcon
6
+ * (round 3), which is under the MIT license:
7
+ *
8
+ * Copyright (c) 2017-2019 Falcon Project
9
+ * Author: Thomas Pornin <thomas.pornin@nccgroup.com>
10
+ *
11
+ * It is a module of its own so that an application which only signs does not
12
+ * bundle key generation.
13
+ */
14
+ import { FFT, iFFT, polyAdjFft, polyInvnorm2Fft, polyMulAutoadjFft, polyMulconst, } from "./fft.js";
15
+ /**
16
+ * The squared norm of a short vector: 2^32-1 if it is not lower than 2^31.
17
+ */
18
+ function polySmallSqnorm(f, logn) {
19
+ const n = 1 << logn;
20
+ let s = 0;
21
+ let ng = 0;
22
+ for (let u = 0; u < n; u++) {
23
+ const z = f[u];
24
+ s = (s + z * z) | 0;
25
+ ng |= s;
26
+ }
27
+ return (s | -(ng >>> 31)) >>> 0;
28
+ }
29
+ /** Converts a small vector to floating point. */
30
+ function polySmallToFp(x, f, logn) {
31
+ const n = 1 << logn;
32
+ for (let u = 0; u < n; u++) {
33
+ x[u] = f[u];
34
+ }
35
+ }
36
+ /**
37
+ * Whether (g, -f) and its orthogonalized vector are short enough: the
38
+ * bounds which the sampler of signing relies on, as the standard deviations
39
+ * which it is given are in the range it supports only for such a basis.
40
+ *
41
+ * Key generation keeps only the f and g which pass this, and a private key
42
+ * which is imported has to pass it as well. rt1 and rt2 are work areas of n
43
+ * values, and rt3 of n/2: they are left with values which depend on f and g.
44
+ */
45
+ export function isShortBasis(f, g, logn, rt1, rt2, rt3) {
46
+ const n = 1 << logn;
47
+ // The bound is 1.17*sqrt(q): the squared bound is
48
+ // (1.17^2) * 12289 = 16822.4121, and the squared norm of (g, -f) is an
49
+ // integer.
50
+ const normf = polySmallSqnorm(f, logn);
51
+ const normg = polySmallSqnorm(g, logn);
52
+ const norm = ((normf + normg) | -((normf | normg) >>> 31)) >>> 0;
53
+ if (norm >= 16823) {
54
+ return false;
55
+ }
56
+ // The norm of the orthogonalized vector, which is
57
+ // (q*adj(f)/(f*adj(f)+g*adj(g)), q*adj(g)/(f*adj(f)+g*adj(g))).
58
+ polySmallToFp(rt1, f, logn);
59
+ polySmallToFp(rt2, g, logn);
60
+ FFT(rt1, logn);
61
+ FFT(rt2, logn);
62
+ polyInvnorm2Fft(rt3, rt1, rt2, logn);
63
+ polyAdjFft(rt1, logn);
64
+ polyAdjFft(rt2, logn);
65
+ polyMulconst(rt1, 12289, logn);
66
+ polyMulconst(rt2, 12289, logn);
67
+ polyMulAutoadjFft(rt1, rt3, logn);
68
+ polyMulAutoadjFft(rt2, rt3, logn);
69
+ iFFT(rt1, logn);
70
+ iFFT(rt2, logn);
71
+ let bnorm = 0;
72
+ for (let u = 0; u < n; u++) {
73
+ bnorm += rt1[u] * rt1[u];
74
+ bnorm += rt2[u] * rt2[u];
75
+ }
76
+ // Not NaN either, which a division by zero gives.
77
+ return bnorm < 16822.4121;
78
+ }
@@ -0,0 +1,28 @@
1
+ /** The number of bits of a coefficient of f and g, by logn. */
2
+ export declare const MAX_fg_BITS: readonly number[];
3
+ /** The number of bits of a coefficient of F and G, by logn. */
4
+ export declare const MAX_FG_BITS: readonly number[];
5
+ /** Encodes n values modulo q, 14 bits each. */
6
+ export declare function modqEncode(out: Uint8Array, x: Uint16Array, logn: number): number;
7
+ /** Decodes n values modulo q: a value which is not below q is rejected. */
8
+ export declare function modqDecode(x: Uint16Array, logn: number, input: Uint8Array): number;
9
+ /** Encodes n small signed values, `bits` bits each. */
10
+ export declare function trimI8Encode(out: Uint8Array, x: Int8Array, logn: number, bits: number): number;
11
+ /**
12
+ * Decodes n small signed values, `bits` bits each: -2^(bits-1), which the
13
+ * encoding does not produce, is rejected.
14
+ */
15
+ export declare function trimI8Decode(x: Int8Array, logn: number, bits: number, input: Uint8Array): number;
16
+ /**
17
+ * Encodes the n coefficients of a signature with the compression of Falcon:
18
+ * the sign, the low 7 bits, and the rest in unary.
19
+ */
20
+ export declare function compEncode(out: Uint8Array, x: Int16Array, logn: number): number;
21
+ /**
22
+ * Decodes the n coefficients of a signature. An encoding which
23
+ * {@link compEncode} does not produce is rejected (a value out of range,
24
+ * minus zero, or padding bits which are not zero), so that a signature has
25
+ * only one encoding.
26
+ */
27
+ export declare function compDecode(x: Int16Array, logn: number, input: Uint8Array): number;
28
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/src/codec.ts"],"names":[],"mappings":"AAgBA,+DAA+D;AAC/D,eAAO,MAAM,WAAW,EAAE,SAAS,MAAM,EAAsC,CAAC;AAEhF,+DAA+D;AAC/D,eAAO,MAAM,WAAW,EAAE,SAAS,MAAM,EAAsC,CAAC;AAEhF,+CAA+C;AAC/C,wBAAgB,UAAU,CACxB,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,WAAW,EACd,IAAI,EAAE,MAAM,GACX,MAAM,CAsBR;AAED,2EAA2E;AAC3E,wBAAgB,UAAU,CACxB,CAAC,EAAE,WAAW,EACd,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,UAAU,GAChB,MAAM,CAoBR;AAED,uDAAuD;AACvD,wBAAgB,YAAY,CAC1B,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,MAAM,CAyBR;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,UAAU,GAChB,MAAM,CAuBR;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,UAAU,EACb,IAAI,EAAE,MAAM,GACX,MAAM,CAkCR;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CACxB,CAAC,EAAE,UAAU,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,UAAU,GAChB,MAAM,CA4BR"}
@@ -0,0 +1,212 @@
1
+ /*
2
+ * Encoding and decoding of keys and signatures.
3
+ *
4
+ * This is a port of codec.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
+ * An encoding function returns the number of the octets it wrote, and a
11
+ * decoding function the number of the octets it read: 0 if the value cannot
12
+ * be encoded or the input is not valid.
13
+ */
14
+ import { Q } from "./consts.js";
15
+ /** The number of bits of a coefficient of f and g, by logn. */
16
+ export const MAX_fg_BITS = [0, 8, 8, 8, 8, 8, 7, 7, 6, 6, 5];
17
+ /** The number of bits of a coefficient of F and G, by logn. */
18
+ export const MAX_FG_BITS = [0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8];
19
+ /** Encodes n values modulo q, 14 bits each. */
20
+ export function modqEncode(out, x, logn) {
21
+ const n = 1 << logn;
22
+ for (let u = 0; u < n; u++) {
23
+ if (x[u] >= Q)
24
+ return 0;
25
+ }
26
+ const outLen = ((n * 14) + 7) >> 3;
27
+ if (outLen > out.length)
28
+ return 0;
29
+ let acc = 0;
30
+ let accLen = 0;
31
+ let v = 0;
32
+ for (let u = 0; u < n; u++) {
33
+ acc = (acc << 14) | x[u];
34
+ accLen += 14;
35
+ while (accLen >= 8) {
36
+ accLen -= 8;
37
+ out[v++] = acc >>> accLen;
38
+ }
39
+ }
40
+ if (accLen > 0) {
41
+ out[v] = acc << (8 - accLen);
42
+ }
43
+ return outLen;
44
+ }
45
+ /** Decodes n values modulo q: a value which is not below q is rejected. */
46
+ export function modqDecode(x, logn, input) {
47
+ const n = 1 << logn;
48
+ const inLen = ((n * 14) + 7) >> 3;
49
+ if (inLen > input.length)
50
+ return 0;
51
+ let acc = 0;
52
+ let accLen = 0;
53
+ let u = 0;
54
+ let v = 0;
55
+ while (u < n) {
56
+ acc = (acc << 8) | input[v++];
57
+ accLen += 8;
58
+ if (accLen >= 14) {
59
+ accLen -= 14;
60
+ const w = (acc >>> accLen) & 0x3fff;
61
+ if (w >= Q)
62
+ return 0;
63
+ x[u++] = w;
64
+ }
65
+ }
66
+ if ((acc & ((1 << accLen) - 1)) !== 0)
67
+ return 0;
68
+ return inLen;
69
+ }
70
+ /** Encodes n small signed values, `bits` bits each. */
71
+ export function trimI8Encode(out, x, logn, bits) {
72
+ const n = 1 << logn;
73
+ const maxv = (1 << (bits - 1)) - 1;
74
+ const minv = -maxv;
75
+ for (let u = 0; u < n; u++) {
76
+ if (x[u] < minv || x[u] > maxv)
77
+ return 0;
78
+ }
79
+ const outLen = ((n * bits) + 7) >> 3;
80
+ if (outLen > out.length)
81
+ return 0;
82
+ let acc = 0;
83
+ let accLen = 0;
84
+ let v = 0;
85
+ const mask = (1 << bits) - 1;
86
+ for (let u = 0; u < n; u++) {
87
+ acc = (acc << bits) | (x[u] & mask);
88
+ accLen += bits;
89
+ while (accLen >= 8) {
90
+ accLen -= 8;
91
+ out[v++] = acc >>> accLen;
92
+ }
93
+ }
94
+ if (accLen > 0) {
95
+ out[v] = acc << (8 - accLen);
96
+ }
97
+ return outLen;
98
+ }
99
+ /**
100
+ * Decodes n small signed values, `bits` bits each: -2^(bits-1), which the
101
+ * encoding does not produce, is rejected.
102
+ */
103
+ export function trimI8Decode(x, logn, bits, input) {
104
+ const n = 1 << logn;
105
+ const inLen = ((n * bits) + 7) >> 3;
106
+ if (inLen > input.length)
107
+ return 0;
108
+ let u = 0;
109
+ let v = 0;
110
+ let acc = 0;
111
+ let accLen = 0;
112
+ const mask1 = (1 << bits) - 1;
113
+ const mask2 = 1 << (bits - 1);
114
+ while (u < n) {
115
+ acc = (acc << 8) | input[v++];
116
+ accLen += 8;
117
+ while (accLen >= bits && u < n) {
118
+ accLen -= bits;
119
+ let w = (acc >>> accLen) & mask1;
120
+ w |= -(w & mask2);
121
+ if (w === -mask2)
122
+ return 0;
123
+ x[u++] = w;
124
+ }
125
+ }
126
+ if ((acc & ((1 << accLen) - 1)) !== 0)
127
+ return 0;
128
+ return inLen;
129
+ }
130
+ /**
131
+ * Encodes the n coefficients of a signature with the compression of Falcon:
132
+ * the sign, the low 7 bits, and the rest in unary.
133
+ */
134
+ export function compEncode(out, x, logn) {
135
+ const n = 1 << logn;
136
+ for (let u = 0; u < n; u++) {
137
+ if (x[u] < -2047 || x[u] > 2047)
138
+ return 0;
139
+ }
140
+ let acc = 0;
141
+ let accLen = 0;
142
+ let v = 0;
143
+ for (let u = 0; u < n; u++) {
144
+ acc <<= 1;
145
+ let t = x[u];
146
+ if (t < 0) {
147
+ t = -t;
148
+ acc |= 1;
149
+ }
150
+ let w = t;
151
+ acc <<= 7;
152
+ acc |= w & 127;
153
+ w >>>= 7;
154
+ accLen += 8;
155
+ acc <<= w + 1;
156
+ acc |= 1;
157
+ accLen += w + 1;
158
+ while (accLen >= 8) {
159
+ accLen -= 8;
160
+ if (v >= out.length)
161
+ return 0;
162
+ out[v++] = acc >>> accLen;
163
+ }
164
+ }
165
+ if (accLen > 0) {
166
+ if (v >= out.length)
167
+ return 0;
168
+ out[v++] = acc << (8 - accLen);
169
+ }
170
+ return v;
171
+ }
172
+ /**
173
+ * Decodes the n coefficients of a signature. An encoding which
174
+ * {@link compEncode} does not produce is rejected (a value out of range,
175
+ * minus zero, or padding bits which are not zero), so that a signature has
176
+ * only one encoding.
177
+ */
178
+ export function compDecode(x, logn, input) {
179
+ const n = 1 << logn;
180
+ const maxInLen = input.length;
181
+ let acc = 0;
182
+ let accLen = 0;
183
+ let v = 0;
184
+ for (let u = 0; u < n; u++) {
185
+ if (v >= maxInLen)
186
+ return 0;
187
+ acc = (acc << 8) | input[v++];
188
+ const b = acc >>> accLen;
189
+ const s = b & 128;
190
+ let m = b & 127;
191
+ for (;;) {
192
+ if (accLen === 0) {
193
+ if (v >= maxInLen)
194
+ return 0;
195
+ acc = (acc << 8) | input[v++];
196
+ accLen = 8;
197
+ }
198
+ accLen--;
199
+ if (((acc >>> accLen) & 1) !== 0)
200
+ break;
201
+ m += 128;
202
+ if (m > 2047)
203
+ return 0;
204
+ }
205
+ if (s !== 0 && m === 0)
206
+ return 0;
207
+ x[u] = s !== 0 ? -m : m;
208
+ }
209
+ if ((acc & ((1 << accLen) - 1)) !== 0)
210
+ return 0;
211
+ return v;
212
+ }