-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencode.c
365 lines (311 loc) · 8.2 KB
/
encode.c
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
/*
Copyright (C) 2017 Ming-Shing Chen
This file is part of BitPolyMul.
BitPolyMul is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BitPolyMul is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BitPolyMul. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include "gfext_aesni.h"
#include "bitmat_prod.h"
#include "byte_inline_func.h"
#include "config_profile.h"
#include "string.h"
/////////////////////////////////////////////////
///
/// truncated FFT, 7 layers
///
//////////////////////////////////////////////////////
#include "trunc_btfy_tab.h"
#include "transpose.h"
static inline
void bit_bc_64x2_div( __m256i * x )
{
for(unsigned i=0;i<64;i++) x[64+i] = _mm256_srli_si256( x[i] , 8 );
for(int i=64-1;i>=0;i--) {
x[1+i] ^= x[64+i];
x[4+i] ^= x[64+i];
x[16+i] ^= x[64+i];
}
for(unsigned i=0;i<64;i++) x[i] = _mm256_unpacklo_epi64( x[i] , x[64+i] );
for(unsigned i=0;i<64;i+=64) {
__m256i * pi = x + i;
for(int j=32-1;j>=0;j--) {
pi[1+j] ^= pi[32+j];
pi[2+j] ^= pi[32+j];
pi[16+j] ^= pi[32+j];
}
}
for(unsigned i=0;i<64;i+=32) {
__m256i * pi = x + i;
for(int j=16-1;j>=0;j--) {
pi[1+j] ^= pi[16+j];
}
}
for(unsigned i=0;i<64;i+=16) {
__m256i * pi = x + i;
for(int j=8-1;j>=0;j--) {
pi[1+j] ^= pi[8+j];
pi[2+j] ^= pi[8+j];
pi[4+j] ^= pi[8+j];
}
}
for(unsigned i=0;i<64;i+=8) {
__m256i * pi = x + i;
pi[4] ^= pi[7];
pi[3] ^= pi[6];
pi[2] ^= pi[5];
pi[1] ^= pi[4];
}
for(unsigned i=0;i<64;i+=4) {
__m256i * pi = x + i;
pi[2] ^= pi[3];
pi[1] ^= pi[2];
}
}
void encode_half_inp( uint64_t * rfx , const uint64_t * fx , unsigned n_fx_128b )
{
if(128*2 > n_fx_128b) { printf("unsupported number of terms.\n"); exit(-1); }
__m256i temp[128];
__m128i * temp128 = (__m128i*) temp;
uint64_t * temp64 = (uint64_t *)temp;
const __m256i * fx_256 = (const __m256i*) fx;
__m128i * rfx_128 = (__m128i*) rfx;
unsigned n_fx_256b = n_fx_128b/2;
unsigned num = n_fx_256b/128;
__m256i t2[128];
__m128i * t2_128 = (__m128i*) t2;
for(unsigned i=0;i < num; i ++ ){
for(unsigned j=0;j<64;j++) {
temp[j] = fx_256[i + j*num];
temp[j] = div_s7( temp[j] );
}
tr_bit_64x64_b4_avx2( (uint8_t*)(temp128) , (const uint8_t *)temp );
bit_bc_64x2_div( temp );
// truncated FFT
for(unsigned j=0;j<8;j++) {
bitmatrix_prod_64x128_4R_b32_opt_avx2( (uint8_t*)(&t2_128[32*j]) , beta_mul_80_bm4r_ext_8 , (const uint8_t *)(&temp64[32*j]) );
}
for(unsigned k=0;k<8;k++) for(unsigned j=0;j<8;j++) rfx_128[i*256+k*8+j] = t2_128[k*32+j*2];
for(unsigned k=0;k<8;k++) for(unsigned j=0;j<8;j++) rfx_128[i*256+64+k*8+j] = t2_128[k*32+16+j*2];
for(unsigned k=0;k<8;k++) for(unsigned j=0;j<8;j++) rfx_128[i*256+128+k*8+j] = t2_128[k*32+j*2+1];
for(unsigned k=0;k<8;k++) for(unsigned j=0;j<8;j++) rfx_128[i*256+128+64+k*8+j] = t2_128[k*32+16+j*2+1];
}
}
#if 1
static inline
void bit_bc_div( __m256i * x )
{
/// div s6 = x^64 + x^16 + x^4 + x
for(int i=64-1;i>=0;i--) {
x[1+i] ^= x[64+i];
x[4+i] ^= x[64+i];
x[16+i] ^= x[64+i];
}
/// div s5 = s^32 + x^16 + x^2 + x
for(unsigned i=0;i<128;i+=64) {
__m256i * pi = x + i;
for(int j=32-1;j>=0;j--) {
pi[1+j] ^= pi[32+j];
pi[2+j] ^= pi[32+j];
pi[16+j] ^= pi[32+j];
}
}
/// div s4 = x^16 + x
for(unsigned i=0;i<128;i+=32) {
__m256i * pi = x + i;
for(int j=16-1;j>=0;j--) {
pi[1+j] ^= pi[16+j];
}
}
/// div s3 = x^8 + x^4 + x^2 + x
for(unsigned i=0;i<128;i+=16) {
__m256i * pi = x + i;
for(int j=8-1;j>=0;j--) {
pi[1+j] ^= pi[8+j];
pi[2+j] ^= pi[8+j];
pi[4+j] ^= pi[8+j];
}
}
/// div s2 = x^4 + x
for(unsigned i=0;i<128;i+=8) {
__m256i * pi = x + i;
pi[4] ^= pi[7];
pi[3] ^= pi[6];
pi[2] ^= pi[5];
pi[1] ^= pi[4];
}
/// div s1 = x^2 + x
for(unsigned i=0;i<128;i+=4) {
__m256i * pi = x + i;
pi[2] ^= pi[3];
pi[1] ^= pi[2];
}
}
#else
static inline
void bit_bc_div( __m256i * x )
{
/// var substitue s4^4 = x^64 + x^4
for(int i=64;i--;) x[4+i] ^= x[64+i];
/// var substitue s4^2 = x32 + x^2
for(int i=32;i--;) x[2+i] ^= x[32+i];
/// var substitue s4 = x16 + x
for(int i=16;i--;) x[1+i] ^= x[16+i];
for(int i=16;i--;) x[1+32+i] ^= x[16+32+i];
for(int i=32;i--;) x[2+64+i] ^= x[32+64+i];
/// var substitue s4 = x16 + x
for(int i=16;i--;) x[1+64+i] ^= x[16+64+i];
for(int i=16;i--;) x[1+96+i] ^= x[16+96+i];
/// 8 blocks, 16 elements per block
for(unsigned i=0;i<16;i++) {
/// div s6 = x^64 + x^16 + x^4 + x = s4^4 + s4
x[4*16+i] ^= x[7*16+i];
x[3*16+i] ^= x[6*16+i];
x[2*16+i] ^= x[5*16+i];
x[1*16+i] ^= x[4*16+i];
/// div s5 = x^32 + x^16 + x^2 + x = s4^2 + s4
x[6*16+i] ^= x[7*16+i];
x[2*16+i] ^= x[3*16+i];
x[5*16+i] ^= x[6*16+i];
x[1*16+i] ^= x[2*16+i];
}
for(unsigned k=0;k<16;k++) {
__m256i * f = &x[k*16];
f[9] ^= f[15];
f[8] ^= f[14];
f[7] ^= f[13];
f[6] ^= f[12];
f[5] ^= f[11];
f[4] ^= f[10];
f[3] ^= f[9];
f[2] ^= f[8];
f[12] ^= f[15];
f[11] ^= f[14];
f[4] ^= f[7];
f[3] ^= f[6];
f[10] ^= f[13];
f[9] ^= f[12];
f[2] ^= f[5];
f[1] ^= f[4];
f[11] ^= f[15];
f[10] ^= f[14];
f[9] ^= f[13];
f[8] ^= f[12];
f[7] ^= f[11];
f[6] ^= f[10];
f[5] ^= f[9];
f[4] ^= f[8];
f[14] ^= f[15];
f[10] ^= f[11];
f[6] ^= f[7];
f[2] ^= f[3];
f[13] ^= f[14];
f[9] ^= f[10];
f[5] ^= f[6];
f[1] ^= f[2];
}
}
#endif
void encode( uint64_t * rfx , const uint64_t * fx , unsigned n_fx_128b )
{
if(128*2 > n_fx_128b) { printf("unsupported number of terms.\n"); exit(-1); }
__m256i temp[128];
__m128i * temp128 = (__m128i*) temp;
const __m256i * fx_256 = (const __m256i*) fx;
__m128i * rfx_128 = (__m128i*) rfx;
unsigned n_fx_256b = n_fx_128b/2;
unsigned num = n_fx_256b/128;
for(unsigned i=0;i < num; i ++ ){
for(unsigned j=0;j<128;j++) {
temp[j] = fx_256[i + j*num];
temp[j] = div_s7( temp[j] );
}
tr_bit_128x128_b2_avx2( (uint8_t*)(temp128) , (const uint8_t *)temp );
bit_bc_div( temp );
// truncated FFT
for(unsigned j=0;j<8;j++) {
bitmatrix_prod_128x128_4R_b32_opt_avx2( (uint8_t*)(temp+16*j) , beta_mul_80_bm4r_ext_8 , (const uint8_t *)(temp+16*j) );
}
for(unsigned j=0;j<128;j++) {
rfx_128[i*256+j] = temp128[j*2];
rfx_128[i*256+128+j] = temp128[j*2+1];
}
}
}
static inline
void bit_bc_exp( __m256i * x )
{
for(unsigned i=0;i<128;i+=4) {
__m256i * pi = x + i;
pi[1] ^= pi[2];
pi[2] ^= pi[3];
}
for(unsigned i=0;i<128;i+=8) {
__m256i * pi = x + i;
pi[1] ^= pi[4];
pi[2] ^= pi[5];
pi[3] ^= pi[6];
pi[4] ^= pi[7];
}
for(unsigned i=0;i<128;i+=16) {
__m256i * pi = x + i;
for(unsigned j=0;j<8;j++) {
pi[1+j] ^= pi[8+j];
pi[2+j] ^= pi[8+j];
pi[4+j] ^= pi[8+j];
}
}
for(unsigned i=0;i<128;i+=32) {
__m256i * pi = x + i;
for(unsigned j=0;j<16;j++) {
pi[1+j] ^= pi[16+j];
}
}
for(unsigned i=0;i<128;i+=64) {
__m256i * pi = x + i;
for(unsigned j=0;j<32;j++) {
pi[1+j] ^= pi[32+j];
pi[2+j] ^= pi[32+j];
pi[16+j] ^= pi[32+j];
}
}
for(unsigned i=0;i<64;i++) {
x[1+i] ^= x[64+i];
x[4+i] ^= x[64+i];
x[16+i] ^= x[64+i];
}
}
void decode( uint64_t * rfx , const uint64_t * fx , unsigned n_fx_128b )
{
if(128*2 > n_fx_128b) { printf("unsupported number of terms.\n"); exit(-1); }
const __m128i * fx_128 = (__m128i*) fx;
__m256i * rfx_256 = (__m256i*) rfx;
unsigned n_fx_256b = n_fx_128b/2;
unsigned num = n_fx_256b/128;
__m256i temp[128];
__m128i * temp128 = (__m128i*) temp;
for(unsigned i=0;i < num; i ++ ){
/// truncated iFFT here.
for(unsigned j=0;j<128;j++) {
temp128[j*2] = fx_128[i*256+j];
temp128[j*2+1] = fx_128[i*256+128+j];
}
for(unsigned j=0;j<8;j++) {
bitmatrix_prod_128x128_4R_b32_opt_avx2( (uint8_t*)(temp+16*j) , i_beta_mul_80_bm4r_ext_8 , (const uint8_t *)(temp+16*j) );
}
bit_bc_exp( temp );
tr_bit_128x128_b2_avx2( (uint8_t*)temp , (const uint8_t *)temp128 );
for(unsigned j=0;j<128;j++) {
temp[j] = exp_s7( temp[j] );
rfx_256[i+j*num] = temp[j];
}
}
}