QSC Post Quantum Cryptographic Library 1.1.0.2 (B2)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
falconbase.h
1/* 2020-2026 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE:
5 * This software and all accompanying materials are the exclusive property of
6 * Quantum Resistant Cryptographic Solutions Corporation (QRCS). The intellectual
7 * and technical concepts contained herein are proprietary to QRCS and are
8 * protected under applicable Canadian, U.S., and international copyright,
9 * patent, and trade secret laws.
10 *
11 * CRYPTOGRAPHIC ALGORITHMS AND IMPLEMENTATIONS:
12 * - This software includes implementations of cryptographic primitives and
13 * algorithms that are standardized or in the public domain, such as AES
14 * and SHA-3, which are not proprietary to QRCS.
15 * - This software also includes cryptographic primitives, constructions, and
16 * algorithms designed by QRCS, including but not limited to RCS, SCB, CSX, QMAC, and
17 * related components, which are proprietary to QRCS.
18 * - All source code, implementations, protocol compositions, optimizations,
19 * parameter selections, and engineering work contained in this software are
20 * original works of QRCS and are protected under this license.
21 *
22 * LICENSE AND USE RESTRICTIONS:
23 * - This software is licensed under the Quantum Resistant Cryptographic Solutions
24 * Public Research and Evaluation License (QRCS-PREL), 2025-2026.
25 * - Permission is granted solely for non-commercial evaluation, academic research,
26 * cryptographic analysis, interoperability testing, and feasibility assessment.
27 * - Commercial use, production deployment, commercial redistribution, or
28 * integration into products or services is strictly prohibited without a
29 * separate written license agreement executed with QRCS.
30 * - Licensing and authorized distribution are solely at the discretion of QRCS.
31 *
32 * EXPERIMENTAL CRYPTOGRAPHY NOTICE:
33 * Portions of this software may include experimental, novel, or evolving
34 * cryptographic designs. Use of this software is entirely at the user's risk.
35 *
36 * DISCLAIMER:
37 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS
39 * FOR A PARTICULAR PURPOSE, SECURITY, OR NON-INFRINGEMENT. QRCS DISCLAIMS ALL
40 * LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
41 * ARISING FROM THE USE OR MISUSE OF THIS SOFTWARE.
42 *
43 * FULL LICENSE:
44 * This software is subject to the Quantum Resistant Cryptographic Solutions
45 * Public Research and Evaluation License (QRCS-PREL), 2025-2026. The complete license terms
46 * are provided in the accompanying LICENSE file or at https://www.qrcscorp.ca.
47 *
48 * Written by: John G. Underhill
49 * Contact: contact@qrcscorp.ca
50 */
51
52#ifndef QSC_FALCONBASE_H
53#define QSC_FALCONBASE_H
54
55#include "qsccommon.h"
56
57 /* \cond NO_DOCUMENT */
58
59/* api.h */
60
61#if defined(QSC_FALCON_S3SHAKE256F512)
62# define FALCON_CRYPTO_SECRETKEYBYTES 1281
63# define FALCON_CRYPTO_PUBLICKEY_BYTES 897
64# define FALCON_CRYPTO_SIGNATURE_BYTES 658
65#elif defined(QSC_FALCON_S5SHAKE256F1024)
66# define FALCON_CRYPTO_SECRETKEYBYTES 2305
67# define FALCON_CRYPTO_PUBLICKEY_BYTES 1793
68# define FALCON_CRYPTO_SIGNATURE_BYTES 1276
69#endif
70
71/* fpr.h */
72
73#define FALCON_FPR_GM_TAB_SIZE 2048
74#define FALCON_FPR_INV_SIGMA_SIZE 11
75#define FALCON_FPR_GM_P2_SIZE 11
76#define FALCON_Q 12289
77#define FALCON_Q0I 12287
78#define FALCON_R 4091
79#define FALCON_R2 10952
80#define FALCON_GMB_SIZE 1024
81#define FALCON_KEYGEN_TEMP_1 136
82#define FALCON_KEYGEN_TEMP_2 272
83#define FALCON_KEYGEN_TEMP_3 224
84#define FALCON_KEYGEN_TEMP_4 448
85#define FALCON_KEYGEN_TEMP_5 896
86#define FALCON_KEYGEN_TEMP_6 1792
87#define FALCON_KEYGEN_TEMP_7 3584
88#define FALCON_KEYGEN_TEMP_8 7168
89#define FALCON_KEYGEN_TEMP_9 14336
90#define FALCON_KEYGEN_TEMP_10 28672
91#define FALCON_SMALL_PRIME_SIZE 522
92#define FALCON_GAUS_1024_12289_SIZE 27
93#define FALCON_MAX_BL_SMALL_SIZE 11
94#define FALCON_MAX_BL_LARGE_SIZE 10
95#define FALCON_DEPTH_INT_FG 4
96#define FALCON_NONCE_SIZE 40
97#define FALCON_L2BOUND_SIZE 11
98#define FALCON_MAXBITS_SIZE 11
99#define FALCON_REV10_SIZE 1024
100
101/* prng.c */
102
103typedef struct
104{
105 QSC_ALIGN(8) uint8_t buf[512];
106 QSC_ALIGN(8) uint8_t state[256];
107 size_t ptr;
108 int32_t type;
109} falcon_prng_state;
110
111/* fpr.c */
112
113typedef uint64_t falcon_fpr;
114
115static const falcon_fpr falcon_fpr_q = 4667981563525332992;
116static const falcon_fpr falcon_fpr_inverse_of_q = 4545632735260551042;
117static const falcon_fpr falcon_fpr_inv_2sqrsigma0 = 4594603506513722306;
118static const falcon_fpr falcon_fpr_log2 = 4604418534313441775;
119static const falcon_fpr falcon_fpr_inv_log2 = 4609176140021203710;
120static const falcon_fpr falcon_fpr_bnorm_max = 4670353323383631276;
121static const falcon_fpr falcon_fpr_zero = 0;
122static const falcon_fpr falcon_fpr_one = 4607182418800017408;
123static const falcon_fpr falcon_fpr_two = 4611686018427387904;
124static const falcon_fpr falcon_fpr_onehalf = 4602678819172646912;
125static const falcon_fpr falcon_fpr_invsqrt2 = 4604544271217802189;
126static const falcon_fpr falcon_fpr_invsqrt8 = 4600040671590431693;
127static const falcon_fpr falcon_fpr_ptwo31 = 4746794007248502784;
128static const falcon_fpr falcon_fpr_ptwo31m1 = 4746794007244308480;
129static const falcon_fpr falcon_fpr_mtwo31m1 = 13970166044099084288U;
130static const falcon_fpr falcon_fpr_ptwo63m1 = 4890909195324358656;
131static const falcon_fpr falcon_fpr_mtwo63m1 = 14114281232179134464U;
132static const falcon_fpr falcon_fpr_ptwo63 = 4890909195324358656;
133
134typedef struct
135{
136 uint32_t p;
137 uint32_t g;
138 uint32_t s;
139} falcon_small_prime;
140
141extern const falcon_fpr falcon_fpr_inv_sigma[FALCON_FPR_INV_SIGMA_SIZE];
142extern const falcon_fpr falcon_fpr_sigma_min[FALCON_FPR_INV_SIGMA_SIZE];
143extern const falcon_fpr falcon_fpr_gm_tab[FALCON_FPR_GM_TAB_SIZE];
144extern const falcon_fpr falcon_fpr_p2_tab[FALCON_FPR_GM_P2_SIZE];
145extern const uint8_t falcon_max_fg_bits[FALCON_MAXBITS_SIZE];
146extern const uint8_t falcon_max_FG_bits[FALCON_MAXBITS_SIZE];
147extern const uint16_t falcon_GMb[FALCON_GMB_SIZE];
148extern const uint16_t falcon_iGMb[FALCON_GMB_SIZE];
149extern const falcon_small_prime falcon_small_primes[FALCON_SMALL_PRIME_SIZE];
150/*
151 * Table below incarnates a discrete Gaussian distribution:
152 * D(x) = exp(-(x^2)/(2*sigma^2))
153 * where sigma = 1.17*sqrt(q/(2*N)), q = 12289, and N = 1024.
154 * Element 0 of the table is P(x = 0).
155 * For k > 0, element k is P(x >= k+1 | x > 0).
156 * Probabilities are scaled up by 2^63.
157 */
158extern const uint64_t falcon_gauss_1024_12289[FALCON_GAUS_1024_12289_SIZE];
159extern const uint16_t falcon_rev10[FALCON_REV10_SIZE];
160/*
161 * The falcon_max_bl_small[] and falcon_max_bl_large[] contain the lengths, in 31-bit
162 * words, of intermediate values in the computation:
163 *
164 * falcon_max_bl_small[depth]: length for the input f and g at that depth
165 * falcon_max_bl_large[depth]: length for the unreduced F and G at that depth
166 *
167 * Rules:
168 *
169 * - Within an array, values grow.
170 *
171 * - The 'SMALL' array must have an entry for maximum depth, corresponding
172 * to the size of values used in the binary GCD. There is no such value
173 * for the 'LARGE' array (the binary GCD yields already reduced
174 * coefficients).
175 *
176 * - falcon_max_bl_large[depth] >= falcon_max_bl_small[depth + 1].
177 *
178 * - Values must be large enough to handle the common cases, with some
179 * margins.
180 *
181 * - Values must not be "too large" either because we will convert some
182 * integers into floating-point values by considering the top 10 words,
183 * i.e. 310 bits; hence, for values of length more than 10 words, we
184 * should take care to have the length centered on the expected size.
185 *
186 * The following average lengths, in bits, have been measured on thousands
187 * of random keys (fg = max length of the absolute value of coefficients
188 * of f and g at that depth; FG = idem for the unreduced F and G; for the
189 * maximum depth, F and G are the output of binary GCD, multiplied by q;
190 * for each value, the average and standard deviation are provided).
191 *
192 * Binary case:
193 * depth: 10 fg: 6307.52 (24.48) FG: 6319.66 (24.51)
194 * depth: 9 fg: 3138.35 (12.25) FG: 9403.29 (27.55)
195 * depth: 8 fg: 1576.87 ( 7.49) FG: 4703.30 (14.77)
196 * depth: 7 fg: 794.17 ( 4.98) FG: 2361.84 ( 9.31)
197 * depth: 6 fg: 400.67 ( 3.10) FG: 1188.68 ( 6.04)
198 * depth: 5 fg: 202.22 ( 1.87) FG: 599.81 ( 3.87)
199 * depth: 4 fg: 101.62 ( 1.02) FG: 303.49 ( 2.38)
200 * depth: 3 fg: 50.37 ( 0.53) FG: 153.65 ( 1.39)
201 * depth: 2 fg: 24.07 ( 0.25) FG: 78.20 ( 0.73)
202 * depth: 1 fg: 10.99 ( 0.08) FG: 39.82 ( 0.41)
203 * depth: 0 fg: 4.00 ( 0.00) FG: 19.61 ( 0.49)
204 *
205 * Integers are actually represented either in binary notation over
206 * 31-bit words (signed, using two's complement), or in RNS, modulo
207 * many small primes. These small primes are close to, but slightly
208 * lower than, 2^31. Use of RNS loses less than two bits, even for
209 * the largest values.
210 *
211 * IMPORTANT: if these values are modified, then the temporary buffer
212 * sizes (FALCON_KEYGEN_TEMP_*, in inner.h) must be recomputed
213 * accordingly.
214 */
215extern const size_t falcon_max_bl_small[FALCON_MAX_BL_SMALL_SIZE];
216extern const size_t falcon_max_bl_large[FALCON_MAX_BL_LARGE_SIZE];
217extern const uint32_t falcon_l2bound[FALCON_L2BOUND_SIZE];
218
219/* public functions */
220
229int32_t qsc_falcon_ref_generate_keypair(uint8_t *pk, uint8_t *sk, bool (*rng_generate)(uint8_t*, size_t));
230
241int32_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));
242
253bool qsc_falcon_ref_open(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen, const uint8_t *pk);
254
255/* \endcond NO_DOCUMENT */
256
257#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 qsccommon.h:1041