-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspec2.tex
2430 lines (2055 loc) · 124 KB
/
spec2.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% !TeX root = falcon.tex
% @copyright Galois, Inc
% @author Marios Georgiou <[email protected]>
% @editor Marcella Hastings <[email protected]>
\chapter{\texorpdfstring{Specification of \falcon}{Specification of Falcon}}\label{chap:spec}
\section{Overview}\label{sec:spec:overview}
\begin{code}
// For the purpose of writing these specs we did not use the
// release version of Cryptol. Instead we use the functors-merge
// branch at:
// https://github.com/GaloisInc/cryptol/tree/functors-merge
// The branch allows use of constraint guards with parameterized
// modules, which allows easier definition of recursive functions
// so that the Cryptol specs are less distinguishable from the
// original ones.
\end{code}
\begin{code}
module spec2 where
// We include the definition of some generic helper
// functions.
while : {a} (a -> Bit) -> (a -> a) -> a -> a
while condition body initial_state =
if(condition initial_state)
then while condition body (body initial_state)
else initial_state
dowhile : {a} (a -> Bit) -> (a -> a) -> a -> a
dowhile condition body initial_state =
if(condition next_state)
then while condition body next_state
else next_state
where next_state = body initial_state
\end{code}
\begin{code}
import Float
parameter
type pkbytelen : #
type coeffs_number_of_bits : #
type Q = Rational
type C = (Float64,Float64)
Omega_phi: [_n]C
type constraint (8 * sbytelen >= 328)
\end{code}
Main elements in \falcon are polynomials of degree $n$ with integer
coefficients. The degree $n$ is normally a power of two (typically 512 or
1024). Computations are done modulo a monic polynomial of degree $n$ denoted
$\phi$ (which is always of the form $\phi = x^n + 1$).
\begin{code}
parameter
type _k : #
type _n : #
type constraint (fin _k, _n == 2^^_k, _k > 0)
type Poly degree a = [degree]a
type ZZ = Integer
type constraint isPowerOfTwo a b = (fin a, b == 2^^a)
phi : {k, n} (isPowerOfTwo k n) => Poly (n+1) ZZ
phi = [1]#zero#[1]
\end{code}
Mathematically, within the algorithm, some polynomials are interpreted as
vectors, and some others as matrices: a polynomial $f$ modulo $\phi$
then stands for a square $n\times n$ matrix, whose rows are $x^if \bmod
\phi$ for all $i$ from $0$ to $n-1$. It can be shown that addition and
multiplication of such matrices map to addition and multiplication of
polynomials modulo $\phi$. We can therefore express most of \falcon in
terms of operations on polynomials, even when we really are handling
matrices that define a \emph{lattice}.
The public key is a basis for a lattice of dimension $2n$:
\begin{equation}
\twotwo{-h}{I_n}{qI_n}{O_n}
\end{equation}
where $I_n$ is the identity matrix of dimension $n$, $O_n$ contains
only zeros, and $h$ is a polynomial modulo $\phi$ that stands for an
$n\times n$ sub-matrix, as explained above. Coefficients of $h$ are
integers that range from $0$ to $q-1$, where $q$ is a specific small
prime (in the recommended parameters, $q = 12289$).
The corresponding private key is another basis for the very same lattice,
expressed as:
\begin{equation}
\twotwo{g}{-f}{G}{-F}
\end{equation}
where $f$, $g$, $F$ and $G$ are short integral polynomials modulo $\phi$,
that fulfil the two following relations:
\begin{equation}
\begin{array}{rcll}
h &=& g/f &\mod \phi \bmod q \\
fG - gF &=& q &\mod \phi
\end{array}
\end{equation}
Such a lattice is known as a \emph{complete NTRU lattice}, and the second
relation, in particular, is called the \emph{NTRU equation}. Take care
that while the relation $h = g/f$ is expressed modulo $q$, the lattice
itself, and the polynomials, use nominally unbounded integers.
\emph{Key pair generation} involves choosing random $f$ and $g$
polynomials using an appropriate distribution that yields short, but not
too short, vectors; then, the NTRU equation is solved to find matching
$F$ and $G$. Keys are described in \cref{sec:spec:keys}, and
their generation is covered in \cref{sec:spec:keygen}.
\emph{Signature generation} consists in first hashing the message to
sign, along with a random nonce, into a polynomial $c$ modulo $\phi$,
whose coefficients are uniformly mapped to integers in the $0$ to $q-1$
range; this process is described in \cref{sec:spec:hash}. Then,
the signer uses his knowledge of the secret lattice basis $(f,g,F,G)$ to
produce a pair of short polynomials $(s_1,s_2)$ such that $s_1 = c - s_2
h \bmod \phi \bmod q$. The signature properly said is $s_2$.
Finding small vectors $s_1$ and $s_2$ is, in all generality, an
expensive process. $\falcon$ leverages the special structure of $\phi$
to implement it as a divide-and-conquer algorithm similar to the Fast
Fourier Transform, which greatly speeds up operations. Moreover, some
``noise'' is added to the sampled vectors, with carefully tuned Gaussian
distributions, to prevent signatures from leaking too much information
about the private key. The signature generation process is described
in \cref{sec:spec:sign}.
\emph{Signature verification} consists in recomputing $s_1$ from the
hashed message $c$ and the signature $s_2$, and then verifying that
$(s_1,s_2)$ is an appropriately short vector. Signature verification can
be done entirely with integer computations modulo $q$; it is described
in \cref{sec:spec:verify}.
Encoding formats for keys and signatures are described in
\cref{sec:spec:encode}. In particular, since the signature is a
short polynomial $s_2$, its elements are on average close to $0$, which
allows for a custom compressed format that reduces signature size.
Recommended parameters for several security levels are defined in
\cref{sec:spec:params}.
\section{Technical Overview}\label{sec:spec:techoverview}
% TODO: explicit the use of these tower of fields
In this section, we provide an overview of the used techniques. As \falcon is arguably math-heavy, a clear comprehension of the mathematical principles in action goes a long way towards understanding and implementing it.
\falcon works with elements in number fields of the form $\bQ[x]/(\phi)$, with $\phi = x^n+1$ for $n = 2^\kappa$ a power-of-two. We note that $\phi$ is a cyclotomic polynomial, therefore it can be written as $\phi(x) = \prod_{k \in \bZ_{m}^\times} (x - \zeta^k)$, with $m = 2n$ and $\zeta$ an arbitrary primitive $m$-th root of $1$ (\eg $\zeta = \exp(\frac{2i\pi}{m})$).
The interesting part about these number fields $\bQ[x]/(\phi)$ is that they come with a tower-of-fields structure. Indeed, we have the following tower of fields:
\begin{equation}\label{eq:binarytower}
\bQ \subseteq \bQ[x]/(x^{2} + 1) \subseteq \dots \subseteq \bQ[x]/(x^{n/2} + 1) \subseteq \bQ[x]/(x^{n} + 1)
\end{equation}
We will rely on this tower-of-fields structure. Even more importantly for our purposes, by splitting polynomials between their odd and even coefficients we have the following chain of space isomorphisms:
\begin{equation}\label{eq:binaryisomorphism}
\bQ^n \cong (\bQ[x]/(x^{2} + 1))^{n/2} \cong \dots \cong (\bQ[x]/(x^{n/2} + 1))^2 \cong \bQ[x]/(x^{n} + 1)
\end{equation}
\eqref{eq:binarytower} and \eqref{eq:binaryisomorphism} remain valid when replacing $\bQ$ by $\bZ$, in which case they describe a tower of rings and a chain of module isomorphisms.
We will see in \cref{sec:spec:splitmerge} that for appropriately defined multiplications, these are actually chains of \emph{ring} isomorphisms. \eqref{eq:binaryisomorphism} will be used to make our signature generation fast and ``good'': in lattice-based cryptography, the smaller the norm of signatures are, the better. So by ``good'' we mean that our signature generation will output signatures with a small norm.
On one hand, classical algebraic operations in the field $\bQ[x]/(x^{n} + 1)$ are fast, and using them will make our signature generation fast. On the other hand, we will use the isomorphisms exposed in \eqref{eq:binaryisomorphism} as a leverage to output signatures with small norm. Using these endomorphisms to their full potential entails manipulating individual coefficients of polynomials (or of their Fourier transform) and working with binary trees.
\section{Notations}\label{sec:spec:notations}
\paragraph{Cryptographic parameters.} For a cryptographic signature scheme, $\lambda$ denotes its security level and $\queries$ the maximal number of signing queries. Following \cite{NIST}, we assume $\queries = 2^{64}$.
\paragraph{Matrices, vectors and scalars.} Matrices will usually be in bold uppercase (e.g. $\matB$), vectors in bold lowercase (e.g. $\vecv$) and scalars -- which include polynomials -- in italic (e.g. $s$). We use the row convention for vectors. The transpose of a matrix $\matB$ may be noted $\matB^\t$. It is to be noted that for a polynomial $f$, we do \emph{not} use $f'$ to denote its derivative in this document.
\paragraph{Quotient rings.} For $q \in \bN^\star$, we denote by $\bZ_q$ the quotient ring $\bZ/q\bZ$. In \falcon, our integer modulus $q = 12289$ is prime so $\bZ_q$ is also a finite field. We denote by $\bZ_q^\times$ the group of invertible elements of $\bZ_q$, and by $\varphi$ Euler's totient function: $\varphi(q) = |\bZ_q^\times| = q - 1 = 3 \cdot 2^{12}$ since $q$ is prime.
\begin{code}
parameter
type q : #
type constraint (fin q, q > 0, q <= 2 ^^ 16, width q >= 7,
prime q, Literal q (Float 11 53), q >= _n+1)
\end{code}
\paragraph{Number fields.} \falcon uses a polynomial modulus $\phi = x^n+1$ (for $n = 2^\kappa$). It is a monic polynomial of $\bZ[x]$, irreducible in $\bQ[x]$ and with distinct roots over $\bC$.
Let $a =\sum_{i=0}^{n-1} a_i x^i$ and $b =\sum_{i=0}^{n-1} b_i x^i$ be arbitrary elements of the number field $\cQ = \bQ[x]/(\phi)$.
% \begin{itemize}
% \item
We note $\adj{a}$ and call (Hermitian) adjoint of $a$ the unique element of $\cQ$ such that for any root $\zeta$ of $\phi$, $\adj{a}(\zeta) = \overline{a(\zeta)}$, where $\overline{\cdot}$ is the usual complex conjugation over $\bC$. For $\phi = x^n+1$, the Hermitian adjoint $\adj a$ can be expressed simply:
% \begin{itemize}
% \item \emph{Binary case.} If $\phi = x^n+1$ with $n = 2^\kappa$ a power of $2$, then
\begin{equation}
\adj a = a_0 - \sum_{i=1}^{n-1} a_{i} x^{n-i}
\end{equation}
% \item \emph{Ternary case.} If $\phi = x^n - x^{n/2} + 1$ with $n = 3 \cdot 2^\kappa$, then
% \begin{equation}
% \adj a = a_0 + \sum_{i=1}^{n-1} a_{i} (x^{n/2-i} - x^{n-i})
% \end{equation}
% \end{itemize}
\begin{code}
star : {n, t} (fin n, n >= 1, Ring t) => Poly n t -> Poly n t
star ([a0] # as) = [a0] # [ -ai | ai <- reverse as ]
\end{code}
We extend this definition to vectors and matrices: the adjoint $\adj\matB$of a matrix $\matB \in \cQ^{n\times m}$ (resp. a vector $\vecv$) is the component-wise adjoint of the transpose of $\matB$ (resp. $\vecv$):
\begin{equation}
\matB = \twotwo{a}{b}{c}{d} \quad \Leftrightarrow \quad \adj \matB = \twotwo{\adj a}{\adj c}{\adj b}{\adj d}
\end{equation}
% \item
\paragraph{Inner product.} The inner product $\inner{\cdot}{\cdot}$ over $\cQ$ and its associated norm $\|\cdot\|$ are
\noindent
\begin{tabular}{@{}p{.5\linewidth}@{}p{.5\linewidth}@{}}
\begin{equation}\label{eq:innerfft}
\inner{a}{b} = \frac{1}{\deg(\phi)}\sum_{\phi(\zeta)=0} a(\zeta)\cdot \overline{b(\zeta)}
\end{equation}
&
\begin{equation}\label{eq:norm}
\|a\| = \sqrt{\inner{a}{a}}
\end{equation}
\end{tabular}
\begin{code}
eval_poly : {k, n} (isPowerOfTwo k n) => ((Poly n C), C) -> C
eval_poly(a, zeta) = CAddList [CMul(a@i, (powers zeta)@i) | i <- [0 .. n]]
powers : C -> [inf]C
powers zeta = iterate (\x -> CMul(x,zeta)) (1,0)
innerProductOverQ : {l, k, n} (fin l, l > 0, isPowerOfTwo k n) =>
([l](Poly n C), [l](Poly n C)) -> C
innerProductOverQ(a, b) = CAddList[result i | i <- [0..(l-1)]] where
a_evals i = [eval_poly`{k}(a@i,zeta) | zeta <- Omega_phi]
b_evals i = [Conjugate (eval_poly`{k}(b@i,zeta)) | zeta <- Omega_phi]
result i = CAddList [CMul(aj, bj) | aj <- a_evals i | bj <- b_evals i]
norm_sq : {l, k, n} (fin l, l > 0, isPowerOfTwo k n) =>
[l](Poly n C) -> Float64
norm_sq(f) = ip.0 * ip.0 + ip.1 * ip.1
where ip = innerProductOverQ`{l,k}(f,f)
\end{code}
We extend these definitions to vectors: for $\vecu = (u_i)_i$ and $\vecv = (v_i)_i$ in $\cQ^{m}$, $\inner{\vecu}{\vecv} = \sum_i \inner{u_i}{v_i}$.
For our choice of $\phi$, the inner product coincides with the usual coefficient-wise inner product:
\begin{equation}\label{eq:innercoef}
\inner{a}{b} = \sum_{0 \leq i < n} a_i b_i;
\end{equation}
From an algorithmic point of view, computing the inner product or the norm is most easily done by using \eqref{eq:innerfft} if polynomials are in FFT representation, and by using \eqref{eq:innercoef} if they are in coefficient representation.
\paragraph{Ring Lattices.} For the rings $\cQ = \bQ[x]/(\phi)$ and $\cZ = \bZ[x]/(\phi)$, positive integers $m \geq n$ and a full-rank matrix $\matB \in \cQ^{n\times m}$, we denote by $\Lambda(\matB)$ and call lattice generated by $\matB$ the set $\cZ^n \cdot \matB = \{ \vecz \matB | \vecz \in \cZ^{n}\}$. By extension, a set $\Lambda$ is a lattice if there exists a matrix $\matB$ such that $\Lambda = \Lambda(\matB)$. We may say that $\Lambda \subseteq \cZ^m$ is a $q$-ary lattice if $ q\cZ^m \subseteq \Lambda$.
\paragraph{Discrete Gaussians.} For $\sigma, \mu\in \bR$ with $\sigma >0$, we define the Gaussian function $\rho_{\sigma,\mu}$ as $\rho_{\sigma,\mu}(x) = \exp(-|x-\mu|^2/2\sigma^2)$, and the discrete Gaussian distribution $D_{\bZ,\sigma,\mu}$ over the integers as
\begin{equation}
D_{\bZ,\sigma,\mu}(x) = \frac{\rho_{\sigma,\mu}(x)}{ \sum_{z \in \bZ} \rho_{\sigma,\mu}(z) } .
\end{equation}
The parameter $\mu$ may be omitted when it is equal to zero.
%\paragraph{Field norm.} Let $\bK$ be a number field of degree $n = [\bK : \bQ]$ over $\bQ$ and $\bL$ be a Galois extension of $\bK$. We denote by $\gal(\bL/\bK)$ the Galois group of $\bL/\bK$.
%The field norm $\N_{\bL/\bK} : \bL \rightarrow \bK$ is a map defined for any $f \in \bL$ by the product of the Galois conjugates of $f$:
%\begin{equation}
% \N_{\bL/\bK} (f) = \prod_{\g \in \gal(\bL/\bK)} \g(f).
%\end{equation}
%Equivalently, $\N_{\bL/\bK} (f)$ can be defined as the determinant of the $\bK$-linear map $y \in \bL \mapsto f y$. One can check that the field norm is a multiplicative morphism.
%\tprcomment{I commented the paragraph relative to the field norm}
\paragraph{The Gram-Schmidt orthogonalization.}
Any matrix $\matB \in \cQ^{n \times m}$ can be decomposed as follows:
\begin{equation}
\matB = \matL \times \tBB,
\end{equation}
where $\matL$ is lower triangular with $1$'s on the diagonal, and the rows $\tilde \vecb_i$'s of $\tBB$ verify $\inner{\vecb_i}{ \vecb_j} = 0$ for $i \neq j$. When $\matB$ is full-rank , this decomposition is unique, and it is called the Gram-Schmidt orthogonalization (or \gso).
We will also call Gram-Schmidt norm of $\matB$ the following value:
\begin{equation}
\gsnorm{\matB} = \max_{\tilde\vecb_i \in \tBB} \|\tilde\vecb_i\|.
\end{equation}
\paragraph{The $\LDLs$ decomposition.} The $\LDLs$ decomposition writes any full-rank Gram matrix as a product $\L \matD \adj\L$, where $\matL \in \cQ^{n\times n}$ is lower triangular with $1$'s on the diagonal, and $\matD \in \cQ^{n\times n}$ is diagonal.
The $\LDLs$ decomposition and the \gso are closely related as for a basis $\matB$, there exists a unique \gso $\matB = \L \cdot \tBB$ and for a full-rank Gram matrix $\matG$, there exists a unique $\LDLs$ decomposition $\matG = \L \matD \adj\L$. If $\matG = \matB \adj \matB$, then $\matG = \L \cdot (\tBB \adj \tBB) \cdot \adj\L$ is a valid $\LDLs$ decomposition of $\matG$. As both decompositions are unique, the matrices $\L$ in both cases are actually the same. In a nutshell:
\begin{equation}
\left[\L\cdot\tBB \text{ is the \gso of } \matB \right]
\Leftrightarrow \left[ \L \cdot (\tBB\adj \tBB) \cdot \adj\L \text{ is the $\LDLs$ decomposition of }(\matB\adj \matB)\right].
\end{equation}
The reason why we present both equivalent decompositions is because the \gso is a more familiar concept in lattice-based cryptography, whereas the use of $\LDLs$ decomposition is faster and therefore makes more sense from an algorithmic point of view.
\section{Keys} \label{sec:spec:keys}
\subsection{Public Parameters}
Public keys use some public parameters that are shared by many key
pairs:
\begin{enumerate}
\item The cyclotomic polynomial $\phi = x^n+1$, where $n = 2^\kappa$ is a power of $2$. We note that $\phi$ is monic and irreducible.
\item A modulus $q \in \bN^\star$. In \falcon, $q = 12289$. We note that $(\phi \bmod q)$ splits over $\bZ_q[x]$.
\item A real bound $\sqsignorm > 0$.
\item Standard deviations $\sigma$ and $\sigmin < \sigmax$.
\item A signature bytelength \sigbytelen.
\end{enumerate}
% Many of our algorithms are different whether we take $\phi = x^n+1$ or $\phi = x^n - x^{n/2} + 1$. Since $x^n+1$ is a binary polynomial and its working with it implies manipulating binary trees, we will often refer to situations involving it as \emph{binary cases}; similarly, since $x^n - x^{n/2} + 1$ is a ternary polynomial and implies ternary trees, situations involving it will be called \emph{ternary cases}. In \falcon, we do not consider values of $n$ higher than $1024$.
For clarity, public parameters may be omitted (\eg in algorithms' headers) when clear from context.
% The definition can be extended to larger primes $q$, and degrees $n$
% larger than $1024$. Some operations are made more efficient by choosing
% $q$ such that $q$ is prime and $q = 1 \bmod 2n$; for $n\leq 1024$, $q =
% 18433$ is such an alternate modulus value.
\begin{code}
parameter
sigma : Float64
type sbytelen : #
type constraint (fin sbytelen)
\end{code}
\subsection{Private Key}
The core of a \falcon private key \sk consists of four polynomials
$f,g,F,G \in \bZ[x]/(\phi)$ with short integer coefficients, verifying the
NTRU equation:
\begin{equation}\label{eq:ntru}
fG -gF = q \bmod \phi.
\end{equation}
The polynomial $f$ shall furthermore be invertible in $\bZ_q[x]/(\phi)$.
Given $f$ and $g$ such that there exists a solution $(F,G)$ to the NTRU
equation, $F$ and $G$ may be recomputed dynamically, but that process is
computationally expensive; therefore, it is normally expected that at
least $F$ will be stored along $f$ and $g$ (given $f$, $g$ and $F$, $G$
can be efficiently recomputed).
Two additional elements are computed from the private key, and may be
recomputed dynamically, or stored along $f$, $g$ and $F$:
\begin{itemize}
\item The FFT representations of $f$, $g$, $F$ and $G$, ordered in the
form of a matrix:
\begin{equation}\label{eq:hatb}
\hat \matB = \twotwo{\fft(g)}{-\fft(f)}{\fft(G)}{-\fft(F)},
\end{equation}
$\fft(a)$ being the fast Fourier transform of $a$ in the
underlying ring (here, $\bR[x]/(\phi)$).
\item A \falcon tree \tree, described at the end of this section.
\end{itemize}
\begin{code}
type privateKey = ([2][2](Poly _n ZZ), [2][2](FFT _n), falconTree _k)
\end{code}
FFT representations are described in \cref{sec:spec:fftntt}. The
FFT representation of a polynomial formally consists of $n$ complex
numbers (a complex number is normally encoded as two 64-bit
floating-point values); however, the FFT representation of a \emph{real}
polynomial $f$ is redundant, because for each complex root $\zeta$ of
$\phi$, its conjugate $\overline{\zeta}$ is also a root of $\phi$, and
$f(\overline{\zeta}) = \overline{f(\zeta)}$. Therefore, the FFT
representation of a polynomial may be stored as $n/2$ complex numbers,
and $\hat \matB$, when stored, requires $2n$ complex numbers.
\paragraph{\falcon trees.} \falcon trees are binary trees defined inductively as follows:
\begin{itemize}
\item A \falcon tree \tree of height $0$ consists of a single node whose value is a real $\sigma > 0$.
\item A \falcon tree \tree of height $\kappa$ verifies these properties:
\begin{itemize}
\item The value of its root, noted \tree.\data, is a polynomial $\ell \in \bQ[x]/(x^n+1)$ with $n = 2^\kappa$.
\item Its left and right children, noted \tree.\lchild and \tree.\rchild, are \falcon trees of height $\kappa-1$.
\end{itemize}
\end{itemize}
The values of internal nodes -- which are real polynomials -- are stored in \fft representation (\ie as complex numbers, see \cref{sec:spec:fftntt} for a formal definition). Hence all the nodes of a \falcon tree contain polynomials in \fft representation, except the leaves which contain real values $>0$.
\begin{code}
// helper function for recursive functions. Will be removed
// when Cryptol is updated.
resize : {m,n,a} (fin m, fin n, Zero a) => [m]a -> [n]a
resize xs = take`{n} (xs # repeat`{inf} zero)
getValue : {k, n} (isPowerOfTwo k n) => falconTree k -> Poly n C
getValue T = take`{n} T
get_leftchild : {k, n} (isPowerOfTwo k n, k > 0) =>
falconTree k -> falconTree (k-1)
get_leftchild T = left where
children = resize (drop`{n} T): [2*2^^(k-1)*k]C
[left, right] = split`{2,2^^(k-1)*k} children
get_rightchild : {k, n} (isPowerOfTwo k n, k > 0) =>
falconTree k -> falconTree (k-1)
get_rightchild T = right where
children = resize (drop`{n} T): [2*2^^(k-1)*k]C
[left, right] = split`{2,2^^(k-1)*k} children
newTree : {k, n} (isPowerOfTwo k n, k >= 1) =>
(Poly n C, falconTree (k-1), falconTree (k-1)) -> falconTree k
newTree(value, leftchild, rightchild) = T where
T = zero
// The leaves contain real numbers
// For consistency their type is C
newLeaf : C -> falconTree 0
newLeaf sigma_leaf = [sigma_leaf]
\end{code}
\begin{code}
get_leaves : {k, n} (isPowerOfTwo k n) =>
falconTree k -> [2^^k]C
get_leaves T
| k == 0 => T
| k > 0 => resize leaves where
left_child = get_leftchild`{k} T : falconTree (k-1)
right_child = get_rightchild`{k} T : falconTree (k-1)
left_leaves = get_leaves`{k-1} left_child : [2^^(k-1)]C
right_leaves = get_leaves`{k-1} right_child : [2^^(k-1)]C
leaves = left_leaves # right_leaves : [2*2^^(k-1)]C
set_leaves : {k, n} (isPowerOfTwo k n) =>
(falconTree k, [2^^k]C) -> falconTree k
set_leaves(T, leaves)
| k == 0 => leaves
| k > 0 => newTree`{k}(value, newleftchild, newrightchild) where
value = getValue`{k} T
leftchild = get_leftchild`{k} T
rightchild = get_rightchild`{k} T
[leftleaves, rightleaves] = split`{2,2^^(k-1)} (resize leaves)
newleftchild = set_leaves`{k-1}(leftchild, leftleaves)
newrightchild = set_leaves`{k-1}(rightchild, rightleaves)
\end{code}
A \falcon tree of height $3$ is represented in \cref{fig:falcontree}. As illustrated by the figure, a \falcon tree can be easily represented by an array of $2^\kappa (1 + \kappa)$ complex numbers (or exactly half as many, if the redundancy of FFT representation is leveraged, as explained above), and access to the left and right children can be performed efficiently using simple pointer arithmetic.
\begin{code}
type falconTree k = [2^^k*(1+k)]C
\end{code}
\begin{figure}%[H]
\centering
\includegraphics[width=\textwidth]{tikz/FalconTree}
\caption{A \falcon tree of height $3$}\label{fig:falcontree}
\end{figure}
The contents of a \falcon tree \tree are computed from the private key
elements $f$, $g$, $F$ and $G$ using the algorithm described in
\cref{sec:spec:keygen:ffldl} (see also \cref{alg:keygen}).
\subsection{Public key}
The \falcon public key \pk corresponding to the private key $\sk =
(f,g,F,G)$ is a polynomial $h \in \bZ_q[x]/(\phi)$ such that:
\begin{equation}
h = gf^{-1} \bmod (\phi,q).
\end{equation}
\begin{code}
type publicKey = Poly _n (Z q)
\end{code}
\section{FFT and NTT} \label{sec:spec:fftntt}
\begin{code}
// Helper functions for Complex Numbers
CMul : (C, C) -> C
CMul(x, y) = (x.0*y.0-x.1*y.1, x.0*y.1+x.1*y.0)
CAdd : (C, C) -> C
CAdd(x, y) = (x.0 + x.1, y.0 + y.1)
CSub : (C, C) -> C
CSub(x, y) = (x.0 - x.1, y.0 - y.1)
CAddList : {n} (fin n) => [n]C -> C
CAddList(l) = sums ! 0
where sums = [zero] # [CAdd(el,sums') | el <- l
| sums' <- sums
]
Conjugate : C -> C
Conjugate((x, y)) = (x, -y)
PolyConj : {k, n} (isPowerOfTwo k n) => Poly n C -> Poly n C
PolyConj(f) = map Conjugate f
dot : {n} (fin n) => FFT n -> FFT n -> FFT n
dot f g = [CMul(fi,gi) | fi <- f | gi <- g]
add : {n} (fin n) => FFT n -> FFT n -> FFT n
add f g = [CAdd(fi,gi) | fi <- f | gi <- g]
sub : {n} (fin n) => FFT n -> FFT n -> FFT n
sub f g = [CSub(fi,gi) | fi <- f | gi <- g]
one : {k, n} (isPowerOfTwo k n) => FFT n
one = [(1.0,0.0) | i <- [0 .. (n-1)]]
HadamardDivision : {n} (fin n) => FFT n -> FFT n -> FFT n
HadamardDivision f g = dot f (map Cinv g)
Cinv : C -> C
Cinv((a,b)) = (a/.denominator, -(b/.denominator)) where
denominator = a^^2 + b^^2
\end{code}
\begin{code}
// Vector Operations
dotVecVec : {m,n} (fin m, fin n) => [m](FFT n) -> [m](FFT n) -> (FFT n)
dotVecVec fvec gvec = foldl add zero (zipWith dot fvec gvec)
subVecVec : {m,n} (fin m, fin n) => [m](FFT n) -> [m](FFT n) -> [m](FFT n)
subVecVec fvec gvec = [sub f g | f <- fvec | g <- gvec]
addVecVec : {m,n} (fin m, fin n) => [m](FFT n) -> [m](FFT n) -> [m](FFT n)
addVecVec fvec gvec = [add f g | f <- fvec | g <- gvec]
dotVecMat : {m,l,n} (fin m, fin l, fin n) =>
[m](FFT n) -> [m][l](FFT n) -> [l](FFT n)
dotVecMat vector matrix = [dotVecVec vector v | v <- (transpose matrix)]
NormalizePoly : {k, n} (isPowerOfTwo k n) =>
(Float64, Poly n C) -> Poly n C
NormalizePoly(_q, F) = dot F [Cinv(_q,0) | i <- [0 .. (n-1)]]
\end{code}
\begin{code}
// Necessary casting functions
IntToCmplx : ZZ -> C
IntToCmplx(x) = ((fromInteger x),zero)
CmplxToInt : C -> ZZ
CmplxToInt(x) = roundToEven x.0
CmplxToQ : C -> Q
CmplxToQ(x) = fpToRational (x.0)
IntPolyToCmplxPoly : {k, n} (isPowerOfTwo k n) => Poly n ZZ -> Poly n C
IntPolyToCmplxPoly(f) = map IntToCmplx f
CmplxPolyToIntPoly : {k, n} (isPowerOfTwo k n) => Poly n C -> Poly n ZZ
CmplxPolyToIntPoly(f) = map CmplxToInt f
CmplxPolyToQPoly : {k, n} (isPowerOfTwo k n) => Poly n C -> Poly n Q
CmplxPolyToQPoly(f) = map CmplxToQ f
\end{code}
% TODO: ternary case
\paragraph{The \fft.} Let $f \in \bQ[x]/(\phi)$. We note $\Omega_\phi$ the set of complex roots of $\phi$. We suppose that $\phi$ is monic with distrinct roots over $\bC$, so that $\phi(x) = \prod\limits_{\zeta \in \Omega_\phi} (x - \zeta)$. We denote by $\fft_\phi(f)$ the fast Fourier transform of $f$ with respect to $\phi$:
\begin{equation}
\fft_\phi(f) = (f(\zeta))_{\zeta \in \Omega_\phi}
\end{equation}
When $\phi$ is clear from context, we simply note $\fft(f)$. We may also use the notation $\hat f$ to indicate that $\hat f$ is the \fft of $f$. $\fft_\phi$ is a ring isomorphism, and we note $\ifft_\phi$ its inverse. The multiplication in the \fft domain is denoted by $\fdot$. We extend the \fft and its inverse to matrices and vectors by component-wise application.
\begin{code}
FFT : {k, n} (isPowerOfTwo k n) => Poly n C -> FFT n
FFT(x)
| k == 0 => x
| k > 0 => resize(join([result0,result1])) where
even = [x@(2*i ) | i <- [0 .. (2^^(k-1)-1)]]
odd = [x@(2*i+1) | i <- [0 .. (2^^(k-1)-1)]]
left = FFT`{k-1}(even)
right = FFT`{k-1}(odd)
X = join([left,right])
result0 =
[X@i + CMul(Omega_phi@(`n*i),(X@(i+`n/2))) | i <- [0 .. (n/2-1)]]
result1 =
[X@i - CMul(Omega_phi@(`n*i),(X@(i+`n/2))) | i <- [0 .. (n/2-1)]]
invFFT : {k, n} (isPowerOfTwo k n) => (FFT n) -> (Poly n C)
invFFT(x) = FFT`{k}(x)
PolyMulInC : {k, n} (isPowerOfTwo k n) =>
((Poly n C), (Poly n C)) -> (Poly n C)
PolyMulInC(f, g) = invFFT`{k}(dot (FFT`{k} f) (FFT`{k} g))
PolyDivInC : {k, n} (isPowerOfTwo k n) =>
((Poly n C), (Poly n C)) -> (Poly n C)
PolyDivInC(f, g) = invFFT`{k}(HadamardDivision`{n} (FFT`{k} f) (FFT`{k} g))
PolyMulInZ : {k, n} (isPowerOfTwo k n) =>
((Poly n ZZ), (Poly n ZZ)) -> (Poly n ZZ)
PolyMulInZ(f, g) = CmplxPolyToIntPoly`{k}
(PolyMulInC`{k}(IntPolyToCmplxPoly`{k} f, IntPolyToCmplxPoly`{k} g))
// The result is in Poly n Q since we divide
PolyDivInZ : {k, n} (isPowerOfTwo k n) =>
((Poly n ZZ), (Poly n ZZ)) -> (Poly n Q)
PolyDivInZ(f, g) = CmplxPolyToQPoly`{k}
(PolyDivInC`{k}(IntPolyToCmplxPoly`{k} f, IntPolyToCmplxPoly`{k} g))
FFT' : {k, n} (isPowerOfTwo k n) => Poly n ZZ -> FFT n
FFT'(x) = FFT`{k} (IntPolyToCmplxPoly`{k} x)
invFFT' : {k, n} (isPowerOfTwo k n) => FFT n -> Poly n ZZ
invFFT'(x) = CmplxPolyToIntPoly`{k}(invFFT`{k} x)
FFT'' : {k, n} (isPowerOfTwo k n) => Poly n (Z q) -> FFT n
FFT''(x) = FFT'`{k}(map fromZ x)
\end{code}
Additions, subtractions, multiplications and divisions of polynomials
modulo $\phi$ can be computed in FFT representations by simply
performing them on each coordinate. In particular, this makes
multiplications and divisions very efficient.
For $\phi = x^n + 1$, the set of complex roots $\zeta$ of $\phi$ is:
\begin{equation}\label{eq:phi}
\Omega_\phi = \left\{\left. \exp\left(\frac{i (2k+1)\pi}{n}\right) \right| 0 \leq k < n \right\}
\end{equation}
\begin{code}
// We precompute all roots and store them in Omega_phi.
// Check falcon_512.cry and falcon_1024.cry
\end{code}
\paragraph{A note on implementing the \fft.} There exist several ways of implementing the \fft, which may yield slightly different results. For example, some implementations of the \fft scale our definition by a constant factor (\eg $1/\deg(\phi)$). Another differentiation point is the order of (the roots of) the \fft. Common orders are the increasing order (\ie the roots are sorted by their order on the unit circle, starting at $1$ and moving clockwise) or (variants of) the bit-reversal order. In the case of \falcon:
\begin{itemize}
\item The \fft is not scaled by a constant factor.
\item There is no constraint on the order of the \fft, the choice is left to the implementer. However, the chosen order shall be consistent for all the algorithms using the \fft.
\end{itemize}
\paragraph{Representation of polynomials in algorithms.} The algorithms which specify \falcon heavily rely on the fast Fourier transform, and some of them explicitly require that the inputs and/or outputs are given in \fft representation. When the directive ``\algorithmicformat'' is present at the beginning of an algorithm, it specifies in which format (coefficient or \fft representation) the input/output polynomials shall be represented. When the directive ``\algorithmicformat'' is absent, no assumption on the format of the input/output polynomials is made.
\begin{code}
type FFT degree = Poly degree C
\end{code}
\paragraph{The NTT.} The NTT (Number Theoretic Transform) is the analog
of the FFT in the field $\bZ_p$, where $p$ is a prime such that $p = 1
\bmod 2n$. Under these
conditions, $\phi$ has exactly $n$ roots $(\omega_i)$ over $\bZ_p$, and
any polynomial $f \in \bZ_p[x]/(\phi)$ can be represented by the values
$f(\omega_i)$. Conversion to and from NTT representation can be done
efficiently in $O(n \log n)$ operations in $\bZ_p$. When in NTT
representation, additions, subtractions, multiplications and divisions
of polynomials (modulo $\phi$ and $p$) can be performed coordinate-wise
in $\bZ_p$.
\begin{code}
/* Recursive NTT */
parameter
r : Z q
roots : [inf](Z q)
roots = iterate ((*) (r * r)) 1
NTT : {k, n} (isPowerOfTwo k n) => Poly n (Z q) -> Poly n (Z q)
NTT a = ntt_r`{lg2 n} 0 a
ntt_r : {n} (fin n) =>
Integer -> [2 ^^ n](Z q) -> [2 ^^ n](Z q)
ntt_r depth a
| n == 0 => a
| n > 0 => butterfly depth even odd
where
(lft, rht) = shuffle a
even = ntt_r`{n - 1} (depth + 1) lft
odd = ntt_r`{n - 1} (depth + 1) rht
shuffle : {n, a} (fin n, n > 0) => [2 * n]a -> ([n]a, [n]a)
shuffle a =
([ a @ (i * 2) | i <- [0 .. <n]], [ a @ (i * 2 + 1) | i <- [0 .. <n]])
butterfly : {n} (fin n, n > 0) =>
Integer -> [n](Z q) -> [n](Z q) -> [2 * n](Z q)
butterfly depth even odd =
lft # rht
where
j = 2 ^^ depth
lft = [ even @ i + roots @ (i * j) * odd @ i | i <- [0 .. <n] ]
rht = [ even @ i - roots @ (i * j) * odd @ i | i <- [0 .. <n] ]
\end{code}
\begin{code}
/* INVERSE NTT */
ivn : (Z q)
ivn = recip (`_n : (Z q))
ivr : (Z q)
ivr = recip r
ivroots : [inf](Z q)
ivroots = iterate ((*) (ivr * ivr)) 1
NTTInv : {k, n} (isPowerOfTwo k n) => Poly n (Z q) -> Poly n (Z q)
NTTInv a =
map ((*) ivn) (ivntt_r`{lg2 n} 0 a)
ivntt_r : {n} (fin n) => Integer -> [2 ^^ n](Z q) -> [2 ^^ n](Z q)
ivntt_r depth a
| n == 0 => a
| n > 0 => ivbutterfly depth even odd
where
(lft, rht) = shuffle a
even = ivntt_r`{n - 1} (depth + 1) lft
odd = ivntt_r`{n - 1} (depth + 1) rht
ivbutterfly : {n} (fin n, n > 0) =>
Integer -> [n](Z q) -> [n](Z q) -> [2 * n](Z q)
ivbutterfly depth even odd =
lft # rht
where
j = 2 ^^ depth
lft = [ even @ i + ivroots @ (i * j) * odd @ i | i <- [0 .. <n] ]
rht = [ even @ i - ivroots @ (i * j) * odd @ i | i <- [0 .. <n] ]
\end{code}
\begin{code}
prodZq : {k, n} (isPowerOfTwo k n) =>
Poly n (Z q) -> Poly n (Z q) -> Poly n (Z q)
prodZq f g = NTTInv`{k}((NTT`{k} f) * (NTT`{k} g))
ModInv : Z q -> Z q
ModInv(f) = 1/. f
\end{code}
% Care must be taken that the roots of $\phi$ in $\bZ_p$ are unrelated to
% the roots of $\phi$ in $\bC$. %TPr: as noticed by Gregor, there actually is a group morphism mapping the first set to the other
In \falcon, the NTT allows for faster
implementations of public key operations (using $\bZ_q$) and key pair
generation (with various medium-sized primes $p$). Private key
operations, though, rely on the fast Fourier sampling, which uses the
FFT, not the NTT.
\section{Splitting and Merging} \label{sec:spec:splitmerge}
In this section, we make explicit the chains of isomorphisms described in \cref{sec:spec:techoverview}, by presenting splitting (resp. merging) operators which allow to travel these chains from right to left (resp. left to right).
Let $\phi, \phi'$ be cyclotomic polynomials such that $\phi(x) = \phi'(x^2)$ (for example, $\phi(x) = x^n + 1$ and $\phi'(x) = x^{n/2} + 1$). We define operators which are at the heart of our signing algorithm. Our algorithms require the ability to split an element of $\bQ[x]/(\phi)$ into two smaller elements of $\bQ[x]/(\phi')$. Conversely, we require the ability to merge two elements of $\bQ[x]/(\phi')$ into an element of $\bQ[x]/(\phi)$.
\paragraph{The \splitfft operator.} Let $n$ be the degree of $\phi$, and $f = \sum_{i=0}^{n-1} a_i x^i$ be an arbitrary element of $\bQ[x]/(\phi)$, $f$ can be decomposed uniquely as $f(x) = f_0(x^2) + xf_1(x^2)$, with $f_0, f_1 \in \bQ[x]/(\phi')$. In coefficient representation, such a decomposition is straightforward to write:
\begin{equation}\label{eq:split}
f_0 = \sum\limits_{0 \leq i < n/2} a_{2i} x^i \text{\ \ \ and\ \ \ }f_1 = \sum\limits_{0 \leq i < n/2} a_{2i+1} x^i
\end{equation}
In \eqref{eq:split}, we simply split $f$ with respect to its even or odd coefficients. With this notation, we note:
\begin{equation}\label{eq:splitdef}
\polsplit(f) = (f_0,f_1).
\end{equation}
In \falcon, polynomials are repeatedly split, multiplied together, split again and so forth. To avoid switching back and forth between the coefficient and \fft representation, we always perform the split operation in the \fft representation. It is defined in \longsplitfft.
\begin{algorithm}%[H]
\caption{$\splitfft(\fft(f))$}\label{alg:splitfft}
\begin{algorithmic}[1]
\Require {$\fft(f) = (f(\zeta))_{\zeta}$ for some $f \in \bQ[x]/(\phi)$}
\Ensure {$\fft(f_0)= (f_0(\zeta'))_{\zeta'}$ and $\fft(f_1)= (f_1(\zeta'))_{\zeta'}$ for some $f_0,f_1 \in \bQ[x]/(\phi')$}
\Format{All polynomials are in \fft representation.}
\For{$\zeta$ such that $\phi(\zeta) = 0$ and Im$(\zeta) > 0$}
\Comment{See \cref{eq:phi} with $0 \leq k < n/2$}
\State{$\zeta' \gets \zeta^2$}
\State{$f_0(\zeta') \gets \frac{1}{2} \left[ f(\zeta) + f(-\zeta) \right]$}
\State{$f_1(\zeta') \gets \frac{1}{2\zeta} \left[ f(\zeta) - f(-\zeta) \right]$}
\EndFor
\Return{$(\fft(f_0), \fft(f_1))$}
\end{algorithmic}
\end{algorithm}
\splitfft is \polsplit realized in the \fft representation: for any $f, \fft(\polsplit(f)) = \splitfft(\fft(f))$. Readers familiar with the Fourier transform will recognize that \splitfft is a subroutine of the inverse fast Fourier transform, more precisely the part which from $\fft(f)$ computes two \fft's twice smaller.
\begin{code}
splitfft : {k, n} (isPowerOfTwo k n, n > 1) =>
(FFT n) -> (FFT (n/2), FFT (n/2))
splitfft FFTf = (resize(FFTf0), resize(FFTf1)) where
FFTf0 = [FFTf@(2*i ) | i <- [0 .. (2^^(k-1)-1)]]
FFTf1 = [FFTf@(2*i+1) | i <- [0 .. (2^^(k-1)-1)]]
\end{code}
\paragraph{The \mergefft operator.} With the previous notations, we define the operator \polmerge as follows:
\begin{equation}\label{eq:merge}
\polmerge(f_0,f_1) = f_0(x^2) + xf_1(x^2) \in \bQ[x]/(\phi).
\end{equation}
Similarly to \polsplit, it is often relevant from an efficiently standpoint to perform \polmerge in the \fft representation. This is done in \longmergefft.
\begin{algorithm}%[H]
\caption{$\mergefft(f_0,f_1)$}\label{alg:mergefft}
\begin{algorithmic}[1]
\Require {$\fft(f_0) = (f_0(\zeta'))_{\zeta'}$ and $\fft(f_1) = (f_1(\zeta'))_{\zeta'}$ for some $f_0,f_1 \in \bQ[x]/(\phi')$}
\Ensure {$\fft(f) = (f(\zeta))_{\zeta}$ for some $f \in \bQ[x]/(\phi)$}
\Format{All polynomials are in \fft representation.}
\For{$\zeta$ such that $\phi(\zeta) = 0$}
\Comment{See \cref{eq:phi}}
\State{$\zeta' \gets \zeta^2$}
\State{$f(\zeta) \gets f_0(\zeta') + \zeta f_1(\zeta')$}
% \State{$f(-\zeta) \gets f_0(\zeta') - \zeta f_1(\zeta')$}
\EndFor
\Return{$\fft(f)$}
\end{algorithmic}
\end{algorithm}
\begin{code}
mergefft : {k, n} (isPowerOfTwo k n, n>=2) =>
(FFT (n/2), FFT (n/2)) -> (FFT n)
mergefft (f0, f1) = resize FFTf where
FFTf = join[[f0@i,f1@i] | i <- [0 .. (2^^(k-1)-1)]]
\end{code}
It is immediate that \polsplit and \polmerge are inverses of each other, and equivalently \splitfft and \mergefft are inverses of each other. Just as for \splitfft, readers familiar with the Fourier transform can observe that \mergefft is a step of the fast Fourier transform: it is the reconstruction step which from two small \fft's computes a larger \fft.
\begin{code}
FFTInvFFT : (FFT 4) -> Bit
property FFTInvFFT f = mergefft`{2,4} (splitfft`{2,4} f) == f
\end{code}
\paragraph{Relationship with the \fft.} There is no requirement on the order in which the values $f(\zeta)$ (resp. $f_0(\zeta')$, resp. $f_1(\zeta')$) are to be stored, and the choice of this order is left to the implementer. It is however recommended to use a unique order convention for the \fft, \ifft, \splitfft and \mergefft operators. Since the \fft and \ifft need to implemented anyway, this unique convention can be achieved \eg by implementing \splitfft as part of \ifft, and \mergefft as part of the \fft.
\tprcomment{should we provide an example of \fft/\ifft algorithm?}
The intricate relationships between the \polsplit and \polmerge operators, their counterparts in the \fft representation and the (inverse) fast Fourier transform are illustrated in the commutative diagram of \cref{fig:splitmerge}.
\begin{figure}%[H]
\centering
\begin{tikzpicture}[]
\matrix (m) [matrix of nodes,row sep=15mm,column sep = 25mm,draw=none]
{
$f\in \bQ[x]/(\phi)$ & $f_0,f_1 \in \bQ[x]/(\phi')$ \\
$\hat f\in \fft(\bQ[x]/(\phi))$ & $\hat f_0, \hat f_1 \in \fft(\bQ[x]/(\phi'))$ \\
};
\draw[line] (m-1-1.259) -> (m-2-1.100) node[midway,left] {\fft};
\draw[line] (m-1-2.259) -> (m-2-2.100) node[midway,left] {\fft};
\draw[line] (m-2-1.80) -> (m-1-1.281) node[midway,right] {\ifft};
\draw[line] (m-2-2.80) -> (m-1-2.281) node[midway,right] {\ifft};
\draw[line] ($(m-1-1.east)+(0,.1)$) -> ($(m-1-2.west)+(0,.1)$) node[midway,above] {\polsplit~\eqref{eq:splitdef}};
\draw[line] ($(m-2-1.east)+(0,.1)$) -> ($(m-2-2.west)+(0,.1)$) node[midway,above] {\splitfft};
\draw[line] ($(m-1-2.west)-(0,.1)$) -> ($(m-1-1.east)-(0,.1)$) node[midway,below] {\polmerge~\eqref{eq:merge}};
\draw[line] ($(m-2-2.west)-(0,.1)$) -> ($(m-2-1.east)-(0,.1)$) node[midway,below] {\mergefft};
\end{tikzpicture}
\caption{Relationship between \fft, \ifft, \polsplit, \polmerge, \splitfft and \mergefft}\label{fig:splitmerge}
\end{figure}
\subsection{Algebraic interpretation}\label{sec:spec:splitmerge:algebraic}
The purpose of the splitting and merging operators that we defined is not only to represent an element of $\bQ[x]/(\phi)$ using two elements of $\bQ[x]/(\phi')$, but to do so in a manner compatible with ring operations. As an illustration, we consider the operation:
\begin{equation}\label{eq:simpleproduct}
a = b c
\end{equation}
where $a, b, c \in \bQ[x]/(\phi)$. For $f \in \bQ[x]/(\phi)$, we consider the associated endomorphism $\psi_f : z \in \bQ[x]/(\phi) \mapsto fz$. \eqref{eq:simpleproduct} can be rewritten as $a = \psi_c(b)$. By the $\polsplit$ isomorphism, $a$ and $b$ (resp. $\psi_c$) can also be considered as elements (resp. an endomorphism) of $(\bQ[x]/(\phi'))^2$. We can rewrite \eqref{eq:simpleproduct} as:
\begin{equation}\label{eq:bisection}
\onetwo{a_0}{a_1} = \onetwo{b_0}{b_1} \twotwo{c_{0}}{c_{1}}{x c_{1}}{c_{0}}
\end{equation}
More formally, we have used the fact that splitting operators are isomorphisms between $\bQ[x]/(\phi)$ and $(\bQ[x]/(\phi'))^k$, which express elements of $\bQ[x]/(\phi)$ in the $(\bQ[x]/(\phi'))$-basis $\{1,x\}$ (hence ``breaking'' $a,b$ in vectors over a smaller field). Similarly, writing the transformation matrix of the endomorphism $\psi_c$ in the basis $\{1,x\}$ yields the $2\times 2$ matrix of \eqref{eq:bisection}.
%\subsection{Relationship with the field norm}\label{sec:spec:splitmerge:fieldnorm} The splitting and merging operators allow to easily express the field norm for some specific cyclotomic fields. Let $\bL = \bQ[x]/(\phi), \bK = \bQ[x]/(\phi')$ and $f \in \bL$. Since by definition $\N_{\bL/\bK}(f) = \det_\bK(\psi_d)$, we can use \eqref{eq:bisection} to compute it explicitly. This yields:
%\begin{itemize}
% \item If $\phi'(x^2) = \phi(x)$, then $\N_{\bL/\bK}(f) = f_0^2 - x f_1^2$, where $(f_0, f_1) = \polsplit(f)$;
%\end{itemize}
%
%For $f \in \bL$ with $\bL = \bQ[x]/(x^{2^\kappa} + 1)$, we also denote $\N(f) = f_0^2 - x f_1^2 = \N_{\bL/\bK}(f)$, where $\bK$ is the largest strict subfield of $\bL$ (see \eqref{eq:binarytower}). For the values of $\phi$ considered in this document, this allows to define $\N(f)$ in an unambiguous way.
%\tprcomment{I simplified everything related to the field norm, it was too verbose}
\paragraph{Relationship with the field norm.} The field norm (or relative norm) $\N_{\bL/\bK}$ maps elements of a larger field $\bL$ onto a subfield $\bK$. It is an important notion in field theory, but in this document, we only need to define it for a simple, particular case. Let $n = 2^\kappa$ a power of two, $\bL = \bQ[x]/(x^{n} + 1)$ and $\bK = \bQ[x]/(x^{n/2} + 1)$. We define the field norm $\N_{\bL/\bK}$ as follows:
\begin{equation}\label{eq:fieldnorm}
\begin{array}{llllc}
\N_{\bL/\bK} & : & \bL & \rightarrow & \bK \\
& & f & \mapsto & f_0^2 - x f_1^2
\end{array}
\end{equation}
where $(f_0,f_1) = \polsplit(f) \in \bK^2$, see \eqref{eq:split} and \eqref{eq:splitdef} for explicit formulae. When $\bL$ and $\bK$ are clear from context, we simply note $\N(f) = \N_{\bL/\bK}(f)$. An equivalent formulation for $\N_{\bL/\bK}$ is:
\begin{equation}\label{eq:fieldnormmul}
\N_{\bL/\bK} (f) = f(x) \cdot f(-x)
\end{equation}
Both \eqref{eq:fieldnorm} and \eqref{eq:fieldnormmul} are valid formulae for $\N_{\bL/\bK}(f)$, but \eqref{eq:fieldnorm} is more suited to the coefficient representation, and \eqref{eq:fieldnormmul} is more suited to the NTT representation.
\begin{code}
// N is only used on integer polynomials in NTRUSolve
// so we set its type accordingly.
N : {k, n} (isPowerOfTwo k n, k > 0) =>
(Poly n ZZ) -> (Poly (2^^(k-1)) ZZ)
N(f) = f0sq + minusxf1sq where
(_f0, _f1) = splitfft`{k} (IntPolyToCmplxPoly`{k} f) // split*fft*
f0 = CmplxPolyToIntPoly`{k-1} (resize _f0)
f1 = CmplxPolyToIntPoly`{k-1} (resize _f1)
f0sq = PolyMulInZ`{k-1}(resize f0, resize f0)
f1sq = PolyMulInZ`{k-1}(resize f1, resize f1)
minusxf1sq = [0] # [-(f1sq@i) | i <- [1..<(2^^(k-1))]]
\end{code}
\section{Hashing} \label{sec:spec:hash}
As for any hash-and-sign signature scheme, the first step to sign a message or verify a signature consists of hashing the message. In our case, the message needs to be hashed into a polynomial in $\bZ_q[x]/(\phi)$. An approved extendable-output hash function (XOF), as specified in FIPS 202~\cite{FIPS}, shall be used during this procedure.
This XOF shall have a security level at least equal to the security level targeted by our signature scheme. In addition, we should be able to start hashing a message without knowing the security level at which it will be signed. For these reasons, we use a unique XOF for all security levels: \shake.
\begin{itemize}
\item \shakeinit() denotes the initialization of a \shake hashing context;
\item \shakeinject(\shakectx, \str) denotes the injection of the data \str in the hashing context \shakectx;
\item \shakeextract(\shakectx, $b$) denotes extraction from a hashing context \shakectx of $b$ bits of pseudorandomness.
\end{itemize}
\longhashtopoint defines the hashing process used in \falcon. It is defined for any $q \leq 2^{16}$. In \falcon, big-endian convention is used to interpret a chunk of $b$
bits, extracted from a \shake instance, into an integer in the $0$ to
$2^b-1$ range (the first of the $b$ bits has numerical weight $2^{b-1}$,
the last has weight $1$).
\begin{algorithm}[htb]
\caption{$\hashtopoint(\str, q, n)$}\label{alg:hashtopoint}
\begin{algorithmic}[1]
\Require{A string \str, a modulus $q \leq 2^{16}$, a degree $n \in \bN^\star$}
\Ensure{An polynomial $c = \sum_{i=0}^{n-1} c_i x^i $ in $\bZ_q[x]$}
\State{$k \gets \lfloor 2^{16}/q \rfloor$}
\State{$\shakectx \gets \shakeinit()$}
\State{$\shakeinject(\shakectx, \str)$}
\State{$i \gets 0$}
\While{$i < n$}
\State{$t \gets \shakeextract(\shakectx, 16)$}\label{step:extract}
\If{$t < k q$} \label{alg:hashtopoint:cmp}\label{step:check}
\State{$c_i \gets t \bmod q$} \label{alg:hashtopoint:mod}
\State{$i \gets i+1$}
\EndIf
\EndWhile
\Return{$c$}
\end{algorithmic}
\end{algorithm}
\begin{code}
import Primitive::Keyless::Hash::SHAKE::SHAKE256
HashToPoint : {q', k, n, len}
(q' <= 2^^16, q' >= 1, isPowerOfTwo k n, fin len) =>
[len] -> Poly n (Z q')
HashToPoint str = take`{n} (HashToPointInf`{q'} (shake256 str))
HashToPointInf : {q'} (q' <= 2^^16, q' >= 1) =>
[inf] -> Poly inf (Z q')
HashToPointInf hash =
if t < toInteger(k*`q') then
[ci] # HashToPointInf tailH
else
HashToPointInf tailH
where
t = toInteger(take`{16} hash)
tailH = drop`{16} hash
ci = fromInteger(toInteger(t%`q))
k = 2^^16 / `q
\end{code}
\paragraph{Possible variants.}
\begin{itemize}
\item If $q > 2^{16}$, then larger chunks can be extracted from \shake
at each step.
\item \hashtopoint may be difficult to efficiently
implement in a constant-time way; constant-timeness may be a desirable
feature if the signed data is also secret.
A variant which is easier to
implement with constant-time code extracts $64$ bits instead of $16$ at
step~\ref{step:extract}, and omits the conditional check of
step~\ref{step:check}. While the omission of the check means that some
outputs are slightly more probable than others, a
Rényi argument~\cite{AC:BLLSS15,AC:Prest17} allows to claim that this variant is
secure for the parameters set by NIST~\cite{NIST}.
\end{itemize}
Of course, any variant deviating from the procedure expressed in
\cref{alg:hashtopoint} implies that the same message will hash
to a different value, which breaks interoperability.
% Algorithm~\ref{alg:hashtopoint} can be used to efficiently achieve this \hashtopoint operation. It is not constant-time but, for most applications, variable-time generation of the public parameter $c$ is not a problem. It is defined for $q \leq 2^{16}$ but can be easily adapted for arbitrary large $q$. As described in \cite{https://eprint.iacr.org/2016/467.pdf}, step~\ref{alg:hashtopoint:cmp}-\ref{alg:hashtopoint:mod} execute a rejection on the \shake output considered as an array of 16-bit, unsigned, little-endian integers. Each of those integers is used as a coefficient of $c$, after having been reduced modulo $q$, if it is smaller than $\lfloor 2^{16}/q \rfloor q$ and rejected otherwise.
%
% Note that, when timing leak of public information can be a problem, one can use the alternative approach described in \cite{USENIX:ADPS16} to parse the \shake output, which is more slower and incompatible with the straightforward approach described above, but does not leak any timing information about $c$.
%
% Todo: describe this constant-time approach?
% !TeX root = ../falcon.tex
\section{Key Pair Generation} \label{sec:spec:keygen}
\subsection{Overview}\label{sec:spec:keygen:overview}
The key pair generation can be decomposed in two clearly separate parts.
\begin{itemize}
\item \emph{Solving the NTRU equation.} The first step of the key pair generation consists of computing polynomials $f, g, F, G \in \bZ[x]/(\phi)$ which verify \eqref{eq:ntru} -- the NTRU equation.
Generating $f$ and $g$ is easy; the hard part is to efficiently compute polynomials $F,G$ such that \eqref{eq:ntru} is verified.
To do this, we propose a novel method that exploits the tower-of-rings structure highlighted in \eqref{eq:binarytower}.
We use the field norm $\N$ to map the NTRU equation onto a smaller ring $\bZ[x]/(\phi')$ of the tower of rings, all the way down to $\bZ$. We then solve the equation in $\bZ$ -- using an extended gcd -- and use properties of the norm to lift the solutions $(F,G)$ back to the original ring $\bZ[x]/(\phi)$.
Implementers should be mindful that this step does \textit{not} perform modular reduction modulo $q$, which leads us to handle polynomials with large coefficients (a few thousands of bits per coefficient in the lowest levels of the recursion). See \cref{sec:spec:keygen:ntrugen} for a formal specification of this step, and \cite{PKC:PorPre19} for an in-depth analysis.
\item \emph{Computing a \falcon tree.} Once suitable polynomials $f,g,F,G$ are generated, the second part of the key generation consists of preprocessing them into an adequate format: by adequate we mean that this format should be reasonably compact and allow fast signature generation on-the-go.
\falcon trees are precisely this adequate format. To compute a \falcon tree, we compute the $\LDLs$ decomposition $\matG = \matL \matD \adj \matL$ of the matrix $\matG = \matB \adj \matB$, where
\begin{equation}
\matB = \twotwo{g}{-f}{G}{-F},
\end{equation}
which is equivalent to computing the Gram-Schmidt orthogonalization $\matB = \matL \times \tilde \matB$. If we were using Klein's well-known sampler (or a variant thereof) as a trapdoor sampler, knowing $\matL$ would be sufficient but a bit unsatisfactory as we would not exploit the tower-of-rings structure of $\bQ[x]/(\phi)$.
So instead of stopping there, we store $\matL$ (or rather $L_{10}$, its bottom-left and only non-trivial term) in the root of a tree, use the splitting operators defined in \cref{sec:spec:splitmerge} to ``break'' the diagonal elements $D_{ii}$ of $\matD$ into matrices $\matG_i$ over smaller rings $\bQ[x]/(\phi')$, at which point we create subtrees for each matrix $\matG_i$ and recursively start over the process of $\LDLs$ decomposition and splitting.
The recursion continues until the matrix $\matG$ has its coefficients in $\bQ$, which correspond to the bottom of the recursion tree. How this is done is specified in \cref{sec:spec:keygen:ffldl}.
The main technicality of this part is that it exploits the tower-of-rings structure of $\bQ[x]/(\phi)$ by breaking its elements onto smaller rings. In addition, intermediate results are stored in a tree, which requires precise bookkeeping as elements of different tree levels do not live in the same field. Finally, for performance reasons, the step is realized completely in the \fft domain.
\end{itemize}