QSC Post Quantum Cryptographic Library 1.0.0.6c (A6)
A post quantum secure library written in Ansi C
 
Loading...
Searching...
No Matches
falconbase.h
1/* 2025 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE: This software and all accompanying materials are the exclusive
5 * property of Quantum Resistant Cryptographic Solutions Corporation (QRCS).
6 * The intellectual and technical concepts contained within this implementation
7 * are proprietary to QRCS and its authorized licensors and are protected under
8 * applicable U.S. and international copyright, patent, and trade secret laws.
9 *
10 * CRYPTOGRAPHIC STANDARDS:
11 * - This software includes implementations of cryptographic algorithms such as
12 * SHA3, AES, and others. These algorithms are public domain or standardized
13 * by organizations such as NIST and are NOT the property of QRCS.
14 * - However, all source code, optimizations, and implementations in this library
15 * are original works of QRCS and are protected under this license.
16 *
17 * RESTRICTIONS:
18 * - Redistribution, modification, or unauthorized distribution of this software,
19 * in whole or in part, is strictly prohibited.
20 * - This software is provided for non-commercial, educational, and research
21 * purposes only. Commercial use in any form is expressly forbidden.
22 * - Licensing and authorized distribution are solely at the discretion of QRCS.
23 * - Any use of this software implies acceptance of these restrictions.
24 *
25 * DISCLAIMER:
26 * This software is provided "as is," without warranty of any kind, express or
27 * implied, including but not limited to warranties of merchantability or fitness
28 * for a particular purpose. QRCS disclaims all liability for any direct, indirect,
29 * incidental, or consequential damages resulting from the use or misuse of this software.
30 *
31 * FULL LICENSE:
32 * This software is subject to the **Quantum Resistant Cryptographic Solutions
33 * Proprietary License (QRCS-PL)**. The complete license terms are included
34 * in the LICENSE.txt file distributed with this software.
35 *
36 * Written by: John G. Underhill
37 * Contact: john.underhill@protonmail.com
38 */
39
40
41#ifndef QSC_FALCONBASE_H
42#define QSC_FALCONBASE_H
43
44#include "common.h"
45
46/* \cond */
47
48QSC_CPLUSPLUS_ENABLED_START
49
50/* api.h */
51
52#if defined(QSC_FALCON_S3SHAKE256F512)
53# define FALCON_CRYPTO_SECRETKEYBYTES 1281
54# define FALCON_CRYPTO_PUBLICKEY_BYTES 897
55# define FALCON_CRYPTO_SIGNATURE_BYTES 690
56#elif defined(QSC_FALCON_S5SHAKE256F1024)
57# define FALCON_CRYPTO_SECRETKEYBYTES 2305
58# define FALCON_CRYPTO_PUBLICKEY_BYTES 1793
59# define FALCON_CRYPTO_SIGNATURE_BYTES 1330
60#endif
61
62/* fpr.h */
63
64#define FALCON_FPR_GM_TAB_SIZE 2048
65#define FALCON_FPR_INV_SIGMA_SIZE 11
66#define FALCON_FPR_GM_P2_SIZE 11
67#define FALCON_Q 12289
68#define FALCON_Q0I 12287
69#define FALCON_R 4091
70#define FALCON_R2 10952
71#define FALCON_GMB_SIZE 1024
72#define FALCON_KEYGEN_TEMP_1 136
73#define FALCON_KEYGEN_TEMP_2 272
74#define FALCON_KEYGEN_TEMP_3 224
75#define FALCON_KEYGEN_TEMP_4 448
76#define FALCON_KEYGEN_TEMP_5 896
77#define FALCON_KEYGEN_TEMP_6 1792
78#define FALCON_KEYGEN_TEMP_7 3584
79#define FALCON_KEYGEN_TEMP_8 7168
80#define FALCON_KEYGEN_TEMP_9 14336
81#define FALCON_KEYGEN_TEMP_10 28672
82#define FALCON_SMALL_PRIME_SIZE 522
83#define FALCON_GAUS_1024_12289_SIZE 27
84#define FALCON_MAX_BL_SMALL_SIZE 11
85#define FALCON_MAX_BL_LARGE_SIZE 10
86#define FALCON_DEPTH_INT_FG 4
87#define FALCON_NONCE_SIZE 40
88#define FALCON_L2BOUND_SIZE 11
89#define FALCON_MAXBITS_SIZE 11
90#define FALCON_REV10_SIZE 1024
91
92/* prng.c */
93
94typedef struct
95{
96 QSC_ALIGN(8) uint8_t buf[512];
97 QSC_ALIGN(8) uint8_t state[256];
98 size_t ptr;
99 int32_t type;
100} falcon_prng_state;
101
102/* fpr.c */
103
104typedef uint64_t falcon_fpr;
105
106static const falcon_fpr falcon_fpr_q = 4667981563525332992;
107static const falcon_fpr falcon_fpr_inverse_of_q = 4545632735260551042;
108static const falcon_fpr falcon_fpr_inv_2sqrsigma0 = 4594603506513722306;
109static const falcon_fpr falcon_fpr_log2 = 4604418534313441775;
110static const falcon_fpr falcon_fpr_inv_log2 = 4609176140021203710;
111static const falcon_fpr falcon_fpr_bnorm_max = 4670353323383631276;
112static const falcon_fpr falcon_fpr_zero = 0;
113static const falcon_fpr falcon_fpr_one = 4607182418800017408;
114static const falcon_fpr falcon_fpr_two = 4611686018427387904;
115static const falcon_fpr falcon_fpr_onehalf = 4602678819172646912;
116static const falcon_fpr falcon_fpr_invsqrt2 = 4604544271217802189;
117static const falcon_fpr falcon_fpr_invsqrt8 = 4600040671590431693;
118static const falcon_fpr falcon_fpr_ptwo31 = 4746794007248502784;
119static const falcon_fpr falcon_fpr_ptwo31m1 = 4746794007244308480;
120static const falcon_fpr falcon_fpr_mtwo31m1 = 13970166044099084288U;
121static const falcon_fpr falcon_fpr_ptwo63m1 = 4890909195324358656;
122static const falcon_fpr falcon_fpr_mtwo63m1 = 14114281232179134464U;
123static const falcon_fpr falcon_fpr_ptwo63 = 4890909195324358656;
124
125typedef struct
126{
127 uint32_t p;
128 uint32_t g;
129 uint32_t s;
130} falcon_small_prime;
131
132extern const falcon_fpr falcon_fpr_inv_sigma[FALCON_FPR_INV_SIGMA_SIZE];
133
134extern const falcon_fpr falcon_fpr_sigma_min[FALCON_FPR_INV_SIGMA_SIZE];
135
136extern const falcon_fpr falcon_fpr_gm_tab[FALCON_FPR_GM_TAB_SIZE];
137
138extern const falcon_fpr falcon_fpr_p2_tab[FALCON_FPR_GM_P2_SIZE];
139
140/* codec.c */
141
142extern const uint8_t falcon_max_fg_bits[FALCON_MAXBITS_SIZE];
143
144extern const uint8_t falcon_max_FG_bits[FALCON_MAXBITS_SIZE];
145
146/* verify.c */
147
148extern const uint16_t falcon_GMb[FALCON_GMB_SIZE];
149
150extern const uint16_t falcon_iGMb[FALCON_GMB_SIZE];
151
152extern const falcon_small_prime falcon_small_primes[FALCON_SMALL_PRIME_SIZE];
153
154/* keygen.c */
155
156/*
157 * Table below incarnates a discrete Gaussian distribution:
158 * D(x) = exp(-(x^2)/(2*sigma^2))
159 * where sigma = 1.17*sqrt(q/(2*N)), q = 12289, and N = 1024.
160 * Element 0 of the table is P(x = 0).
161 * For k > 0, element k is P(x >= k+1 | x > 0).
162 * Probabilities are scaled up by 2^63.
163 */
164extern const uint64_t falcon_gauss_1024_12289[FALCON_GAUS_1024_12289_SIZE];
165
166extern const uint16_t falcon_rev10[FALCON_REV10_SIZE];
167
168/*
169 * The falcon_max_bl_small[] and falcon_max_bl_large[] contain the lengths, in 31-bit
170 * words, of intermediate values in the computation:
171 *
172 * falcon_max_bl_small[depth]: length for the input f and g at that depth
173 * falcon_max_bl_large[depth]: length for the unreduced F and G at that depth
174 *
175 * Rules:
176 *
177 * - Within an array, values grow.
178 *
179 * - The 'SMALL' array must have an entry for maximum depth, corresponding
180 * to the size of values used in the binary GCD. There is no such value
181 * for the 'LARGE' array (the binary GCD yields already reduced
182 * coefficients).
183 *
184 * - falcon_max_bl_large[depth] >= falcon_max_bl_small[depth + 1].
185 *
186 * - Values must be large enough to handle the common cases, with some
187 * margins.
188 *
189 * - Values must not be "too large" either because we will convert some
190 * integers into floating-point values by considering the top 10 words,
191 * i.e. 310 bits; hence, for values of length more than 10 words, we
192 * should take care to have the length centered on the expected size.
193 *
194 * The following average lengths, in bits, have been measured on thousands
195 * of random keys (fg = max length of the absolute value of coefficients
196 * of f and g at that depth; FG = idem for the unreduced F and G; for the
197 * maximum depth, F and G are the output of binary GCD, multiplied by q;
198 * for each value, the average and standard deviation are provided).
199 *
200 * Binary case:
201 * depth: 10 fg: 6307.52 (24.48) FG: 6319.66 (24.51)
202 * depth: 9 fg: 3138.35 (12.25) FG: 9403.29 (27.55)
203 * depth: 8 fg: 1576.87 ( 7.49) FG: 4703.30 (14.77)
204 * depth: 7 fg: 794.17 ( 4.98) FG: 2361.84 ( 9.31)
205 * depth: 6 fg: 400.67 ( 3.10) FG: 1188.68 ( 6.04)
206 * depth: 5 fg: 202.22 ( 1.87) FG: 599.81 ( 3.87)
207 * depth: 4 fg: 101.62 ( 1.02) FG: 303.49 ( 2.38)
208 * depth: 3 fg: 50.37 ( 0.53) FG: 153.65 ( 1.39)
209 * depth: 2 fg: 24.07 ( 0.25) FG: 78.20 ( 0.73)
210 * depth: 1 fg: 10.99 ( 0.08) FG: 39.82 ( 0.41)
211 * depth: 0 fg: 4.00 ( 0.00) FG: 19.61 ( 0.49)
212 *
213 * Integers are actually represented either in binary notation over
214 * 31-bit words (signed, using two's complement), or in RNS, modulo
215 * many small primes. These small primes are close to, but slightly
216 * lower than, 2^31. Use of RNS loses less than two bits, even for
217 * the largest values.
218 *
219 * IMPORTANT: if these values are modified, then the temporary buffer
220 * sizes (FALCON_KEYGEN_TEMP_*, in inner.h) must be recomputed
221 * accordingly.
222 */
223extern const size_t falcon_max_bl_small[FALCON_MAX_BL_SMALL_SIZE];
224
225extern const size_t falcon_max_bl_large[FALCON_MAX_BL_LARGE_SIZE];
226
227/*
228 * Average and standard deviation for the maximum size (in bits) of
229 * coefficients of (f,g), depending on depth. These values are used
230 * to compute bounds for Babai's reduction.
231 */
232static const struct
233{
234 int32_t avg;
235 int32_t std;
236} falcon_bit_length[] =
237{
238 { 4, 0 },
239 { 11, 1 },
240 { 24, 1 },
241 { 50, 1 },
242 { 102, 1 },
243 { 202, 2 },
244 { 401, 4 },
245 { 794, 5 },
246 { 1577, 8 },
247 { 3138, 13 },
248 { 6308, 25 }
249};
250
251/* sign.c */
252
253typedef struct
254{
255 falcon_prng_state p;
256 falcon_fpr sigma_min;
257} falcon_sampler_context;
258
259typedef int32_t(*falcon_samplerZ)(void* ctx, falcon_fpr mu, falcon_fpr sigma);
260
261/* common.c */
262
263extern const uint32_t falcon_l2bound[FALCON_L2BOUND_SIZE];
264
265/* public functions */
266
277int32_t qsc_falcon_ref_generate_keypair(uint8_t* pk, uint8_t* sk, bool (*rng_generate)(uint8_t*, size_t));
278
291int32_t qsc_falcon_ref_sign(uint8_t* sm, size_t* smlen, const uint8_t* m, size_t mlen, const uint8_t* sk, bool (*rng_generate)(uint8_t*, size_t));
292
304bool qsc_falcon_ref_open(uint8_t* m, size_t* mlen, const uint8_t* sm, size_t smlen, const uint8_t* pk);
305
306QSC_CPLUSPLUS_ENABLED_END
307
308/* \endcond */
309
310#endif
Contains common definitions for the Quantum Secure Cryptographic (QSC) library.
#define QSC_ALIGN(x)
Macro for aligning data to 'x' bytes using GCC/Clang.
Definition common.h:593