@webpqc/fndsa 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/LICENSE +49 -0
  2. package/README.md +220 -2
  3. package/esm/mod.d.ts +6 -0
  4. package/esm/mod.d.ts.map +1 -0
  5. package/esm/mod.js +3 -0
  6. package/esm/package.json +3 -0
  7. package/esm/src/codec.d.ts +28 -0
  8. package/esm/src/codec.d.ts.map +1 -0
  9. package/esm/src/codec.js +212 -0
  10. package/esm/src/common.d.ts +49 -0
  11. package/esm/src/common.d.ts.map +1 -0
  12. package/esm/src/common.js +112 -0
  13. package/esm/src/consts.d.ts +5 -0
  14. package/esm/src/consts.d.ts.map +1 -0
  15. package/esm/src/consts.js +4 -0
  16. package/esm/src/deps.d.ts +2 -0
  17. package/esm/src/deps.d.ts.map +1 -0
  18. package/esm/src/deps.js +1 -0
  19. package/esm/src/errors.d.ts +7 -0
  20. package/esm/src/errors.d.ts.map +1 -0
  21. package/esm/src/errors.js +19 -0
  22. package/esm/src/fft.d.ts +53 -0
  23. package/esm/src/fft.d.ts.map +1 -0
  24. package/esm/src/fft.js +329 -0
  25. package/esm/src/fnDsa1024.d.ts +88 -0
  26. package/esm/src/fnDsa1024.d.ts.map +1 -0
  27. package/esm/src/fnDsa1024.js +97 -0
  28. package/esm/src/fnDsa512.d.ts +88 -0
  29. package/esm/src/fnDsa512.d.ts.map +1 -0
  30. package/esm/src/fnDsa512.js +97 -0
  31. package/esm/src/fnDsaSigner.d.ts +44 -0
  32. package/esm/src/fnDsaSigner.d.ts.map +1 -0
  33. package/esm/src/fnDsaSigner.js +310 -0
  34. package/esm/src/fnDsaVerifier.d.ts +24 -0
  35. package/esm/src/fnDsaVerifier.d.ts.map +1 -0
  36. package/esm/src/fnDsaVerifier.js +136 -0
  37. package/esm/src/interfaces.d.ts +49 -0
  38. package/esm/src/interfaces.d.ts.map +1 -0
  39. package/esm/src/interfaces.js +1 -0
  40. package/esm/src/keygen.d.ts +34 -0
  41. package/esm/src/keygen.d.ts.map +1 -0
  42. package/esm/src/keygen.js +2450 -0
  43. package/esm/src/native.d.ts +15 -0
  44. package/esm/src/native.d.ts.map +1 -0
  45. package/esm/src/native.js +16 -0
  46. package/esm/src/params.d.ts +20 -0
  47. package/esm/src/params.d.ts.map +1 -0
  48. package/esm/src/params.js +18 -0
  49. package/esm/src/rng.d.ts +22 -0
  50. package/esm/src/rng.d.ts.map +1 -0
  51. package/esm/src/rng.js +151 -0
  52. package/esm/src/sha3/keccak.d.ts +58 -0
  53. package/esm/src/sha3/keccak.d.ts.map +1 -0
  54. package/esm/src/sha3/keccak.js +500 -0
  55. package/esm/src/sign.d.ts +31 -0
  56. package/esm/src/sign.d.ts.map +1 -0
  57. package/esm/src/sign.js +718 -0
  58. package/esm/src/utils.d.ts +22 -0
  59. package/esm/src/utils.d.ts.map +1 -0
  60. package/esm/src/utils.js +44 -0
  61. package/esm/src/vrfy.d.ts +27 -0
  62. package/esm/src/vrfy.d.ts.map +1 -0
  63. package/esm/src/vrfy.js +242 -0
  64. package/package.json +49 -4
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,221 @@
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 and signing are separated from
53
+ verification, which uses only integers.
54
+ - ✅ The same API as [`@webpqc/mldsa`](../mldsa/) and
55
+ [`@webpqc/slhdsa`](../slhdsa/): a signer holds a private key and a verifier a
56
+ public key, and all the operations are asynchronous.
57
+ - ✅ Passed the Known Answer Test values of the round 3 submission (100 vectors
58
+ for each parameter set): the key pairs are the same for 200 of 200, and the
59
+ signatures, once converted to the format of this package, for 199 of 200. The
60
+ other one (`count = 82` of Falcon-1024) is of 1281 octets, which does not fit
61
+ in the fixed size of 1280: another signature is sampled instead, as the padded
62
+ format of the reference implementation does.
63
+
64
+ ## Installation
65
+
66
+ `@webpqc/fndsa` is published on [JSR](https://jsr.io/@webpqc/fndsa) and
67
+ [npm](https://www.npmjs.com/package/@webpqc/fndsa). It is an ESM-only package,
68
+ and requires Node.js 20 or later.
69
+
70
+ ```sh
71
+ # Node.js, Cloudflare Workers and Bun, using npm:
72
+ npm install @webpqc/fndsa
73
+ # Node.js, Cloudflare Workers and Bun, using jsr:
74
+ npx jsr add @webpqc/fndsa
75
+ # Deno:
76
+ deno add jsr:@webpqc/fndsa
77
+ ```
78
+
79
+ ## Usage
80
+
81
+ ```ts
82
+ import {
83
+ createFnDsa512Signer,
84
+ createFnDsa512Verifier,
85
+ generateFnDsa512KeyPair,
86
+ } from "@webpqc/fndsa";
87
+
88
+ // A signer generates a key pair.
89
+ // The functions of FN-DSA-1024 are also available.
90
+ const [pk, sk] = await generateFnDsa512KeyPair();
91
+ //// Deterministic key generation is also supported
92
+ // import { deriveFnDsa512KeyPair } from "@webpqc/fndsa";
93
+ // const seed = new Uint8Array(48);
94
+ // globalThis.crypto.getRandomValues(seed);
95
+ // const [pk, sk] = await deriveFnDsa512KeyPair(seed);
96
+
97
+ // The signer creates a signer which holds sk, and signs a message with it.
98
+ const signer = await createFnDsa512Signer(sk);
99
+ const msg = new TextEncoder().encode("hello world");
100
+ const sig = await signer.sign(msg);
101
+
102
+ // Erase the private key which the signer holds, when it is no longer needed.
103
+ signer.destroy();
104
+
105
+ // A verifier creates a verifier which holds pk, and verifies the signature.
106
+ const verifier = await createFnDsa512Verifier(pk);
107
+ const ok = await verifier.verify(sig, msg);
108
+ // ok === true
109
+ ```
110
+
111
+ ## API
112
+
113
+ The functions of a parameter set are named after it: `FnDsa512` or `FnDsa1024`
114
+ for `*` below. All of them return a promise.
115
+
116
+ | Function | Description |
117
+ | --------------------------------- | ------------------------------------------------------------------ |
118
+ | `generateFnDsa*KeyPair()` | Generates a key pair `[publicKey, privateKey]`. |
119
+ | `deriveFnDsa*KeyPair(seed)` | Derives a key pair `[publicKey, privateKey]` from a 48-octet seed. |
120
+ | `createFnDsa*Signer(sk, opts?)` | Creates a signer which holds a private key. |
121
+ | `createFnDsa*Verifier(pk, opts?)` | Creates a verifier which holds a public key. |
122
+
123
+ | Method | Description |
124
+ | --------------------------- | ---------------------------------------------- |
125
+ | `signer.sign(msg)` | Signs a message: resolves to the signature. |
126
+ | `signer.destroy()` | Erases the private key which the signer holds. |
127
+ | `verifier.verify(sig, msg)` | Resolves to true if the signature is valid. |
128
+
129
+ The sizes are as follows, in octets:
130
+
131
+ | Parameter set | NIST security category | Public key | Private key | Signature |
132
+ | ------------- | ---------------------- | ---------- | ----------- | --------- |
133
+ | FN-DSA-512 | 1 | 897 | 1281 | 666 |
134
+ | FN-DSA-1024 | 5 | 1793 | 2305 | 1280 |
135
+
136
+ - The keys are in the encodings of the Falcon specification (a header octet,
137
+ then h for a public key, and f, g and F for a private key).
138
+ - Unlike the other packages of webpqc, the private key is not the seed which the
139
+ key pair is derived from, as key generation takes much longer than decoding a
140
+ key. `deriveFnDsa*KeyPair(seed)` therefore returns an encoded private key, and
141
+ there is no function which expands a seed. The seed is expanded with SHAKE256,
142
+ as the reference implementation does.
143
+ - A signature is randomized: it has a nonce of 40 random octets, and signing the
144
+ same message again gives another signature. There is neither a deterministic
145
+ variant nor a context string.
146
+ - A signature is in the padded format of the reference implementation: the
147
+ compressed signature (the header `0x30 + logn`, the nonce and the compressed
148
+ s2), padded with zeros to the fixed size above. `verify` also accepts a
149
+ signature in the compressed format, which is the same without the padding and
150
+ may be longer than the fixed size (752 and 1462 octets at most), and nothing
151
+ else: in particular, a signed message of the NIST API has to be converted.
152
+ - `sign` rejects after `destroy()`.
153
+
154
+ The promises are rejected with `FnDsaError` for an invalid argument: what is not
155
+ a `Uint8Array`, a seed or a key of an incorrect length, a key which cannot be
156
+ decoded, or a private key which does not satisfy f * G - g * F = q or of which f
157
+ and g are not as short as key generation requires. `verify` resolves to false
158
+ for a signature which cannot be decoded.
159
+
160
+ See [bench/bundle](../../bench/bundle/) for the bundle sizes of the other
161
+ packages.
162
+
163
+ ### Web Cryptography API
164
+
165
+ `createFnDsa*Signer` and `createFnDsa*Verifier` take the option `useWebCrypto`
166
+ of [`@webpqc/mldsa`](../mldsa/#web-cryptography-api): `"auto"` (default), `true`
167
+ or `false`.
168
+
169
+ ```ts
170
+ const signer = await createFnDsa512Signer(sk, { useWebCrypto: false });
171
+ ```
172
+
173
+ It has no effect so far. FN-DSA is not among the algorithms of
174
+ [Modern Algorithms in the Web Cryptography API](https://wicg.github.io/webcrypto-modern-algos/),
175
+ and no runtime has it, so there is nothing to delegate to and the TypeScript
176
+ implementation always runs. The option is there so that an application need not
177
+ change when a runtime has FN-DSA, which is also why the operations are
178
+ asynchronous.
179
+
180
+ ## Performance
181
+
182
+ Run `deno task bench` in this directory to see the benchmark results. They are
183
+ around the following on a laptop (AMD Ryzen 9 6900HX, Deno 2.9):
184
+
185
+ | Parameter set | `generateFnDsa*KeyPair` | `createFnDsa*Signer` | `sign` | `verify` |
186
+ | ------------- | ----------------------- | -------------------- | ------ | -------- |
187
+ | FN-DSA-512 | 22 ms | 0.32 ms | 1.2 ms | 0.04 ms |
188
+ | FN-DSA-1024 | 68 ms | 0.62 ms | 2.3 ms | 0.07 ms |
189
+
190
+ The time of key generation varies a lot from a key pair to another, as
191
+ candidates are generated until one is suitable. A signer expands its private key
192
+ once, when it is created, to the form which signing uses.
193
+
194
+ ## Limitations
195
+
196
+ - It is Falcon of the round 3 submission, not FN-DSA of FIPS 206: see the
197
+ warning above.
198
+ - Signing computes with the floating-point numbers of JavaScript (IEEE 754
199
+ binary64) on values which depend on the private key, as the reference
200
+ implementation does with the ones of the processor. The operations are
201
+ additions, subtractions, multiplications, divisions and square roots, of which
202
+ the time may depend on the operands on some processors. NIST plans to specify
203
+ FN-DSA with fixed-point arithmetic for this reason.
204
+ - JavaScript gives no guarantee about the execution time. Key generation and
205
+ signing follow the reference implementation, which avoids the branches and the
206
+ table lookups which depend on secret values where its design requires it, but
207
+ the resistance to side-channel attacks cannot be ensured in this language.
208
+ - The signature formats other than the padded and the compressed ones (the
209
+ constant-time format, and the key-recovery mode) are not supported.
210
+
211
+ ## Acknowledgements
212
+
213
+ The implementation is a port of the
214
+ [reference implementation of Falcon](https://falcon-sign.info/) by Thomas
215
+ Pornin, which is under the MIT license. Its copyright notice and license are in
216
+ [LICENSE](./LICENSE), and in a legal comment which bundlers keep.
217
+
218
+ ## Contributing
219
+
220
+ We welcome all kind of contributions, filing issues, suggesting new features or
221
+ 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,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
+ }
@@ -0,0 +1,49 @@
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
+ import { Keccak } from "./deps.js";
27
+ /** A new SHAKE256 instance. */
28
+ export declare function newShake256(): Keccak;
29
+ /**
30
+ * Squeezes a SHAKE256 instance, which has absorbed the nonce and the
31
+ * message, to n values modulo q (HashToPoint), by rejection sampling of
32
+ * 16-bit values.
33
+ *
34
+ * Its timing depends on the hash, that is, on the nonce and the message,
35
+ * and not on a key: the nonce is in the signature, so this reveals nothing
36
+ * to who has the signed message.
37
+ */
38
+ export declare function hashToPoint(sc: Keccak, x: Uint16Array, logn: number): void;
39
+ /**
40
+ * Whether the squared norm of (s1, s2) is at most the bound. The sum
41
+ * saturates if it reaches 2^31, so it does not wrap around.
42
+ */
43
+ export declare function isShort(s1: Int16Array, s2: Int16Array, logn: number): boolean;
44
+ /**
45
+ * Like {@link isShort}, with the squared norm of s1 already computed (and
46
+ * saturated) in `sqn`.
47
+ */
48
+ export declare function isShortHalf(sqn: number, s2: Int16Array, logn: number): boolean;
49
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/src/common.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAYH,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,+BAA+B;AAC/B,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAkB1E;AAiBD;;;GAGG;AACH,wBAAgB,OAAO,CACrB,EAAE,EAAE,UAAU,EACd,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,MAAM,GACX,OAAO,CAYT;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,EACX,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,MAAM,GACX,OAAO,CAST"}