ntt.c 5.54 KB
Newer Older
Ruben Anthony Gonzalez's avatar
Ruben Anthony Gonzalez committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "ntt.h"

#include "params.h"

#include <stdint.h>

// for basemul
const int16_t PQM4_KYBER512_M4fspeed_zetas[64] =
{
    2226, 430, 555, 843, 2078, 871, 1550, 105, 422, 587, 177, 3094, 3038, 2869,
    1574, 1653, 3083, 778, 1159, 3182, 2552, 1483, 2727, 1119, 1739, 644, 2457, 349,
    418, 329, 3173, 3254, 817, 1097, 603, 610, 1322, 2044, 1864, 384, 2114, 3193,
    1218, 1994, 2455, 220, 2142, 1670, 2144, 1799, 2051, 794, 1819, 2475, 2459, 478,
    3221, 3021, 996, 991, 958, 1869, 1522, 1628
15
} ;
Ruben Anthony Gonzalez's avatar
Ruben Anthony Gonzalez committed
16

17
const int16_t __attribute__ ((aligned (4))) PQM4_KYBER512_M4fspeed_zetas_asm[128] = {
Ruben Anthony Gonzalez's avatar
Ruben Anthony Gonzalez committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    // 7 & 6 & 5 & 4 layers
    0, 2571, 2970, 1812, 1493, 1422, 287, 202, 3158, 622, 1577, 182, 962, 2127, 1855, 1468, 
    // 1st loop of 3 & 2 & 1 layers
    573, 1223, 652, 2226, 430, 555, 843, 2004, 2777, 1015, 2078, 871, 1550, 105,
    // 2nd loop of 3 & 2 & 1 layers
    264, 2036, 1491, 422, 587, 177, 3094, 383, 3047, 1785, 3038, 2869, 1574, 1653,
    // 3rd loop of 3 & 2 & 1 layers
    2500, 516, 3321, 3083, 778, 1159, 3182, 1458, 3009, 2663, 2552, 1483, 2727, 1119,
    // 4th loop of 3 & 2 & 1 layers
    1727, 1711, 2167, 1739, 644, 2457, 349, 3199, 126, 1469, 418, 329, 3173, 3254,
    // 5th loop of 3 & 2 & 1 layers
    2648, 2476, 3239, 817, 1097, 603, 610, 1017, 3058, 830, 1322, 2044, 1864, 384,
    // 6th loop of 3 & 2 & 1 layers
    732, 107, 1908, 2114, 3193, 1218, 1994, 608, 3082, 2378, 2455, 220, 2142, 1670,
    // 7th loop of 3 & 2 & 1 layers
    1787, 2931, 961, 2144, 1799, 2051, 794, 411, 1821, 2604, 1819, 2475, 2459, 478,
    // 8th loop of 3 & 2 & 1 layers
    3124, 448, 2264, 3221, 3021, 996, 991, 1758, 677, 2054, 958, 1869, 1522, 1628
};

38
const int16_t __attribute__ ((aligned (4))) PQM4_KYBER512_M4fspeed_zetas_inv_ct_asm[256] =
Ruben Anthony Gonzalez's avatar
Ruben Anthony Gonzalez committed
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
{
    // pad + LAYER 7+6+5+4
    0, 2285, 2285, 758, 2285, 1517, 758, 359, 2285, 3127, 1517, 1907, 758, 3042, 359, 1836,
    // removed first "2285" + LAYER 3+2+1 - 1 - butterfly
    2285, 758, 2285, 1517, 758, 359,
    // LAYER 3+2+1 - 1 - twist
    1441, 3104, 1047, 987, 1932, 2861, 713, 1254,

    // LAYER 3+2+1 - 2 - butterfly
    1861, 1571, 205, 1275, 1065, 2652, 2881,
    // LAYER 3+2+1 - 2 - twist
    2043, 1945, 1824, 1233, 3051, 2714, 2196, 2032,

    // LAYER 3+2+1 - 3 - butterfly
    3127, 1861, 1474, 1571, 2918, 205, 1542,
    // LAYER 3+2+1 - 3 - twist
    316, 1681, 2653, 660, 2921, 3097, 325, 707,

    // LAYER 3+2+1 - 4 - butterfly
    3147, 130, 1602, 1860, 1162, 3203, 1618,
    // LAYER 3+2+1 - 4 - twist
    1781, 1078, 1331, 3172, 3305, 378, 2369, 1804,

    // LAYER 3+2+1 - 5 - butterfly
    1517, 3127, 3042, 1861, 1202, 1474, 2367,
    // LAYER 3+2+1 - 5 - twist
    2063, 1630, 2624, 1949, 1761, 1393, 531, 2456,

    // LAYER 3+2+1 - 6 - butterfly
    1202, 2721, 2597, 951, 1421, 247, 3222,
    // LAYER 3+2+1 - 6 - twist
    513, 1075, 546, 3052, 1866, 2236, 1402, 2886,

    // LAYER 3+2+1 - 7 - butterfly
    1907, 3147, 1752, 130, 1871, 1602, 829,
    // LAYER 3+2+1 - 7 - twist
    226, 1434, 2382, 767, 2068, 719, 2824, 2128,

    // LAYER 3+2+1 - 8 - butterfly
    2707, 2946, 3065, 1544, 1838, 282, 1293,
    // LAYER 3+2+1 - 8 - twist
    2559, 476, 2490, 2395, 3059, 2588, 2516, 321,

    // LAYER 3+2+1 - 9 - butterfly
    758, 1517, 359, 3127, 1907, 3042, 1836,
    // LAYER 3+2+1 - 9 - twist
    738, 28, 2888, 1120, 2334, 1523, 148, 998,

    // LAYER 3+2+1 - 10 - butterfly
    1474, 2918, 1542, 725, 2368, 1508, 398,
    // LAYER 3+2+1 - 10 - twist
    1610, 2939, 1149, 1045, 2683, 1852, 792, 842,

    // LAYER 3+2+1 - 11 - butterfly
    3042, 1202, 2367, 2721, 2312, 2597, 681,
    // LAYER 3+2+1 - 11 - twist
    878, 1152, 1830, 2803, 3291, 2263, 1809, 637,

    // LAYER 3+2+1 - 12 - butterfly
    1752, 1871, 829, 666, 8, 320, 2813,
    // LAYER 3+2+1 - 12 - twist
    2989, 2026, 3045, 1144, 1956, 2483, 1673, 2779, 

    // LAYER 3+2+1 - 13 - butterfly
    359, 1907, 1836, 3147, 2707, 1752, 171, 
    // LAYER 3+2+1 - 13 - twist
    3309, 315, 2529, 2613, 1290, 1321, 1665, 2905, 

    // LAYER 3+2+1 - 14 - butterfly
    2367, 2312, 681, 2499, 90, 271, 853,
    // LAYER 3+2+1 - 14 - twist
    3132, 606, 2107, 937, 1055, 861, 2252, 1150,

    // LAYER 3+2+1 - 15 - butterfly
    1836, 2707, 171, 2946, 1325, 3065, 2756, 
    // LAYER 3+2+1 - 15 - twist
    1555, 2973, 2278, 2405, 1237, 2988, 2874, 3005, 

    // LAYER 3+2+1 - 16 - butterfly
    171, 1325, 2756, 2314, 2677, 552, 2106, 2833, 
    // LAYER 3+2+1 - 16 - twist
    1154, 134, 2883, 2031, 2134, 1344, 2135
};

extern void PQM4_KYBER512_M4fspeed_ntt_fast(int16_t *, const int16_t *);
/*************************************************
* Name:        PQM4_KYBER512_M4fspeed_ntt
*
* Description: Inplace number-theoretic transform (NTT) in Rq
*              input is in standard order, output is in bitreversed order
*
* Arguments:   - int16_t *poly: pointer to input/output vector of 256 elements of Zq
**************************************************/
void PQM4_KYBER512_M4fspeed_ntt(int16_t *poly) {
    PQM4_KYBER512_M4fspeed_ntt_fast(poly, PQM4_KYBER512_M4fspeed_zetas_asm);
}

extern void PQM4_KYBER512_M4fspeed_invntt_fast(int16_t *, const int16_t *);
/*************************************************
* Name:        PQM4_KYBER512_M4fspeed_invntt
*
* Description: Inplace inverse number-theoretic transform in Rq
*              input is in bitreversed order, output is in standard order
*
* Arguments:   - int16_t *poly: pointer to input/output vector of 256 elements of Zq
**************************************************/
void PQM4_KYBER512_M4fspeed_invntt(int16_t *poly) {
    PQM4_KYBER512_M4fspeed_invntt_fast(poly, PQM4_KYBER512_M4fspeed_zetas_inv_ct_asm);
}