QSC Post Quantum Cryptographic Library 1.3.0.0 (C1)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
falconbase_avx2.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_AVX2_H
53#define QSC_FALCONBASE_AVX2_H
54
55 /* \cond NO_DOCUMENT */
56
57#include "qsccommon.h"
58
59#if defined(QSC_SYSTEM_HAS_AVX2)
60
61#include "intrinsics.h"
62#include "sha3.h"
63#include <math.h>
64
65/* api.h */
66
67#if defined(QSC_FALCON_S3SHAKE256F512)
68# define FALCON_CRYPTO_SECRETKEYBYTES 1281
69# define FALCON_CRYPTO_PUBLICKEY_BYTES 897
70# define FALCON_CRYPTO_SIGNATURE_BYTES 658
71# define CRYPTO_ALGNAME "Falcon-512"
72#elif defined(QSC_FALCON_S5SHAKE256F1024)
73# define FALCON_CRYPTO_SECRETKEYBYTES 2305
74# define FALCON_CRYPTO_PUBLICKEY_BYTES 1793
75# define FALCON_CRYPTO_SIGNATURE_BYTES 1276
76# define CRYPTO_ALGNAME "Falcon-1024"
77#endif
78
79/* falcon_fpr.h */
80
81#define FALCON_FPR_GM_TAB_SIZE 2048
82#define FALCON_FPR_INV_SIGMA_SIZE 11
83#define FALCON_FPR_GM_P2_SIZE 11
84#define FALCON_Q 12289
85#define FALCON_Q0I 12287
86#define FALCON_R 4091
87#define FALCON_R2 10952
88#define FALCON_GMB_SIZE 1024
89#define FALCON_KEYGEN_TEMP_1 136
90#define FALCON_KEYGEN_TEMP_2 272
91#define FALCON_KEYGEN_TEMP_3 224
92#define FALCON_KEYGEN_TEMP_4 448
93#define FALCON_KEYGEN_TEMP_5 896
94#define FALCON_KEYGEN_TEMP_6 1792
95#define FALCON_KEYGEN_TEMP_7 3584
96#define FALCON_KEYGEN_TEMP_8 7168
97#define FALCON_KEYGEN_TEMP_9 14336
98#define FALCON_KEYGEN_TEMP_10 28672
99#define FALCON_SMALL_PRIME_SIZE 522
100#define FALCON_GAUS_1024_12289_SIZE 27
101#define FALCON_MAX_BL_SMALL_SIZE 11
102#define FALCON_MAX_BL_LARGE_SIZE 10
103#define FALCON_DEPTH_INT_FG 4
104#define FALCON_NONCE_SIZE 40
105#define FALCON_L2BOUND_SIZE 11
106#define FALCON_MAXBITS_SIZE 11
107#define FALCON_REV10_SIZE 1024
108
109#if defined(__GNUC__)
110# if defined(FALCON_FMA)
111# define FALCON_TARGET_AVX2 __attribute__((target("avx2,fma")))
112# else
113# define FALCON_TARGET_AVX2 __attribute__((target("avx2")))
114# endif
115#elif defined(_MSC_VER)
116# define FALCON_TARGET_AVX2
117# pragma warning( disable : 4752 )
118#endif
119
120inline static __m256d falcon_fmadd(__m256d a, __m256d b, __m256d c)
121{
122#if defined(FALCON_FMA)
123 return _mm256_fmadd_pd(a, b, c);
124#else
125 __m256d tmp;
126 tmp = _mm256_mul_pd(a, b);
127 tmp = _mm256_add_pd(tmp, c);
128 return tmp;
129#endif
130}
131
132inline static __m256d falcon_fmsub(__m256d a, __m256d b, __m256d c)
133{
134 /* Note artifact, unused function */
135#if defined(FALCON_FMA)
136 return _mm256_fmsub_pd(a, b, c);
137#else
138 __m256d tmp;
139 tmp = _mm256_mul_pd(a, b);
140 return _mm256_sub_pd(tmp, c);
141#endif
142}
143
144//inline static uint32_t falcon_set_fpu_cw(uint32_t x)
145//{
146//#if defined __GNUC__ && defined __i386__
147// uint32_t short t;
148// uint32_t old;
149//
150// __asm__ __volatile__("fstcw %0" : "=m" (t) : : );
151// old = (t & 0x0300u) >> 8;
152// t = (uint32_t short)((t & ~0x0300u) | (x << 8));
153// __asm__ __volatile__("fldcw %0" : : "m" (t) : );
154// return old;
155//#elif defined _M_IX86
156// uint32_t short t;
157// uint32_t old;
158//
159// __asm { fstcw t }
160// old = (t & 0x0300u) >> 8;
161// t = (uint32_t short)((t & ~0x0300u) | (x << 8));
162// __asm { fldcw t }
163// return old;
164//#else
165// return x;
166//#endif
167//}
168
169/*
170 * For optimal reproducibility of values, we need to disable contraction
171 * of floating-point expressions; otherwise, on some architectures (e.g.
172 * PowerPC), the compiler may generate fused-multiply-add opcodes that
173 * may round differently than two successive separate opcodes. C99 defines
174 * a standard pragma for that, but GCC-6.2.2 appears to ignore it,
175 * hence the GCC-specific pragma (that Clang does not support).
176 */
177#if defined __clang__
178# pragma STDC FP_CONTRACT OFF
179#elif defined __GNUC__
180# pragma GCC optimize ("fp-contract=off")
181#endif
182
183 /* prng.c */
184
185typedef struct
186{
187 QSC_ALIGN(8) uint8_t buf[512];
188 QSC_ALIGN(8) uint8_t state[256];
189 size_t ptr;
190 int32_t type;
191} falcon_prng_state;
192
193/*
194 * We wrap the native 'double' type into a structure so that the C compiler
195 * complains if we inadvertently use raw arithmetic operators on the 'falcon_fpr'
196 * type instead of using the inline functions below. This should have no
197 * extra runtime cost, since all the functions below are 'inline'.
198 */
199typedef struct { double v; } falcon_fpr;
200
201static const falcon_fpr falcon_fpr_q = { 12289.0 };
202static const falcon_fpr falcon_fpr_inverse_of_q = { 1.0 / 12289.0 };
203static const falcon_fpr falcon_fpr_inv_2sqrsigma0 = { 0.150865048875372721532312163019 };
204static const falcon_fpr falcon_fpr_log2 = { 0.69314718055994530941723212146 };
205static const falcon_fpr falcon_fpr_inv_log2 = { 1.4426950408889634073599246810 };
206static const falcon_fpr falcon_fpr_bnorm_max = { 16822.4121 };
207static const falcon_fpr falcon_fpr_zero = { 0.0 };
208static const falcon_fpr falcon_fpr_one = { 1.0 };
209static const falcon_fpr falcon_fpr_two = { 2.0 };
210static const falcon_fpr falcon_fpr_onehalf = { 0.5 };
211static const falcon_fpr falcon_fpr_invsqrt2 = { 0.707106781186547524400844362105 };
212static const falcon_fpr falcon_fpr_invsqrt8 = { 0.353553390593273762200422181052 };
213static const falcon_fpr falcon_fpr_ptwo31 = { 2147483648.0 };
214static const falcon_fpr falcon_fpr_ptwo31m1 = { 2147483647.0 };
215static const falcon_fpr falcon_fpr_mtwo31m1 = { -2147483647.0 };
216static const falcon_fpr falcon_fpr_ptwo63m1 = { 9223372036854775807.0 };
217static const falcon_fpr falcon_fpr_mtwo63m1 = { -9223372036854775807.0 };
218static const falcon_fpr falcon_fpr_ptwo63 = { 9223372036854775808.0 };
219
220typedef struct
221{
222 uint32_t p;
223 uint32_t g;
224 uint32_t s;
225} falcon_small_prime;
226
227extern const falcon_fpr falcon_avx2_fpr_inv_sigma[FALCON_FPR_INV_SIGMA_SIZE];
228extern const falcon_fpr falcon_avx2_fpr_sigma_min[FALCON_FPR_INV_SIGMA_SIZE];
229extern const falcon_fpr falcon_avx2_fpr_gm_tab[FALCON_FPR_GM_TAB_SIZE];
230extern const falcon_fpr falcon_avx2_fpr_p2_tab[FALCON_FPR_GM_P2_SIZE];
231extern const uint8_t falcon_avx2_max_fg_bits[FALCON_MAXBITS_SIZE];
232extern const uint8_t falcon_falcon_max_FG_bits[FALCON_MAXBITS_SIZE];
233extern const uint32_t falcon_avx2_l2bound[FALCON_L2BOUND_SIZE];
234extern const uint64_t falcon_avx2_gauss_1024_12289[FALCON_GAUS_1024_12289_SIZE];
235extern const uint16_t falcon_avx2_falcon_rev10[FALCON_REV10_SIZE];
236extern const size_t falcon_avx2_max_bl_small[FALCON_MAX_BL_SMALL_SIZE];
237extern const size_t falcon_avx2_max_bl_large[FALCON_MAX_BL_LARGE_SIZE];
238extern const uint16_t falcon_avx2_GMb[FALCON_GMB_SIZE];
239extern const uint16_t falcon_avx2_iGMb[FALCON_GMB_SIZE];
240extern const falcon_small_prime falcon_avx2_small_primes[FALCON_SMALL_PRIME_SIZE];
241
250int32_t qsc_falcon_avx2_generate_keypair(uint8_t *pk, uint8_t *sk, bool (*rng_generate)(uint8_t*, size_t));
251
262int32_t qsc_falcon_avx2_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));
263
274bool qsc_falcon_avx2_open(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen, const uint8_t *pk);\
275
276/* \endcond NO_DOCUMENT */
277#endif
278#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:1025
SHA3 family of hash functions.