-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbitpolymul.c
409 lines (317 loc) · 9.84 KB
/
bitpolymul.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
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
/*
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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bc.h"
#include "butterfly_net.h"
#include "config_profile.h"
#define MAX_TERMS 65536
#include "gfext_aesni.h"
#ifdef _PROFILE_
#include "benchmark.h"
struct benchmark bm_ch;
struct benchmark bm_bc;
struct benchmark bm_butterfly;
struct benchmark bm_pointmul;
struct benchmark bm_pointmul_tower;
struct benchmark bm_ich;
struct benchmark bm_ibc;
struct benchmark bm_ibutterfly;
struct benchmark bm_tr;
struct benchmark bm_tr2;
#endif
#define LOG2(X) ((unsigned) (8*sizeof (unsigned long long) - __builtin_clzll((X)) - 1))
// for removing warning.
void *aligned_alloc( size_t alignment, size_t size );
void bitpolymul_simple( uint64_t * c , const uint64_t * a , const uint64_t * b , unsigned _n_64 )
{
if( 0 == _n_64 ) return;
unsigned n_64 = 0;
if( 1 == _n_64 ) n_64 = _n_64;
else {
unsigned log_2_n64 = LOG2(_n_64);
unsigned log_2_n64_1 = LOG2(_n_64-1);
if( log_2_n64 == log_2_n64_1 )log_2_n64 += 1;
n_64 = 1<<log_2_n64;
}
//uint64_t a_bc[MAX_TERMS] __attribute__((aligned(32)));
//uint64_t b_bc[MAX_TERMS] __attribute__((aligned(32)));
uint64_t * a_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64 );
if( NULL == a_bc ) { printf("alloc fail.\n"); exit(-1); }
uint64_t * b_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64 );
if( NULL == b_bc ) { printf("alloc fail.\n"); exit(-1); }
#ifdef _PROFILE_SIMPLE_
bm_start(&bm_bc);
#endif
memcpy( a_bc , a , sizeof(uint64_t)*_n_64 );
for(unsigned i=_n_64;i<n_64;i++) a_bc[i] = 0;
bc_to_lch( a_bc , n_64 );
memcpy( b_bc , b , sizeof(uint64_t)*_n_64 );
for(unsigned i=_n_64;i<n_64;i++) b_bc[i] = 0;
bc_to_lch( b_bc , n_64 );
#ifdef _PROFILE_SIMPLE_
bm_stop(&bm_bc);
#endif
unsigned n_terms = 2*n_64;
//uint64_t a_fx[4*MAX_TERMS] __attribute__((aligned(32))) = {0};
//uint64_t b_fx[4*MAX_TERMS] __attribute__((aligned(32))) = {0};
uint64_t * a_fx = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*2*n_terms );
if( NULL == a_fx ) { printf("alloc fail.\n"); exit(-1); }
uint64_t * b_fx = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*2*n_terms );
if( NULL == b_fx ) { printf("alloc fail.\n"); exit(-1); }
for(unsigned i=0;i<_n_64;i++) {
a_fx[2*i] = a_bc[i];
a_fx[2*i+1] = 0;
}
for(unsigned i=_n_64;i<n_64;i++) { a_fx[2*i]=0; a_fx[2*i+1]=0; }
for(unsigned i=0;i<_n_64;i++) {
b_fx[2*i] = b_bc[i];
b_fx[2*i+1] = 0;
}
for(unsigned i=_n_64;i<n_64;i++) { b_fx[2*i]=0; b_fx[2*i+1]=0; }
#ifdef _PROFILE_SIMPLE_
bm_start(&bm_butterfly);
#endif
butterfly_net_half_inp_clmul( a_fx , n_terms );
butterfly_net_half_inp_clmul( b_fx , n_terms );
#ifdef _PROFILE_SIMPLE_
bm_stop(&bm_butterfly);
bm_start(&bm_pointmul);
#endif
for(unsigned i=0;i<n_terms;i++) {
gf2ext128_mul_sse( (uint8_t *)&a_fx[i*2] , (uint8_t *)&a_fx[i*2] , (uint8_t*)& b_fx[i*2] );
}
#ifdef _PROFILE_SIMPLE_
bm_stop(&bm_pointmul);
bm_start(&bm_ibutterfly);
#endif
i_butterfly_net_clmul( a_fx , n_terms );
#ifdef _PROFILE_SIMPLE_
bm_stop(&bm_ibutterfly);
bm_start(&bm_ibc);
#endif
bc_to_mono_128( a_fx , n_terms );
#ifdef _PROFILE_SIMPLE_
bm_stop(&bm_ibc);
#endif
c[0] = a_fx[0];
for(unsigned i=1;i<(2*_n_64);i++) {
c[i] = a_fx[i*2];
c[i] ^= a_fx[(i-1)*2+1];
}
free(a_bc);
free(b_bc);
free(a_fx);
free(b_fx);
}
/////////////////////////////////////////////////////////////////////////////////
#include "btfy.h"
#include "encode.h"
void bitpolymul_2_128( uint64_t * c , const uint64_t * a , const uint64_t * b , unsigned _n_64 )
{
if( 0 == _n_64 ) return;
unsigned n_64 = 0;
if( 1 == _n_64 ) n_64 = _n_64;
else {
unsigned log_2_n64 = LOG2(_n_64);
unsigned log_2_n64_1 = LOG2(_n_64-1);
if( log_2_n64 == log_2_n64_1 )log_2_n64 += 1;
n_64 = 1<<log_2_n64;
}
if( 256 > n_64 ) n_64 = 256;
uint64_t * a_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64 );
//uint64_t * a_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64*2 );
if( NULL == a_bc ) { printf("alloc fail.\n"); exit(-1); }
uint64_t * b_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64 );
//uint64_t * b_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64*2 );
if( NULL == b_bc ) { printf("alloc fail.\n"); exit(-1); }
#ifdef _PROFILE_
bm_start(&bm_bc);
#endif
memcpy( a_bc , a , sizeof(uint64_t)*_n_64 );
for(unsigned i=_n_64;i<n_64;i++) a_bc[i] = 0;
//bc_to_lch_2( a_bc , n_64 );
bc_to_lch_2_unit256( a_bc , n_64 );
//for(unsigned i=n_64;i<n_64*2;i++) a_bc[i] = 0;
memcpy( b_bc , b , sizeof(uint64_t)*_n_64 );
for(unsigned i=_n_64;i<n_64;i++) b_bc[i] = 0;
//bc_to_lch_2( b_bc , n_64 );
bc_to_lch_2_unit256( b_bc , n_64 );
//for(unsigned i=n_64;i<n_64*2;i++) b_bc[i] = 0;
#ifdef _PROFILE_
bm_stop(&bm_bc);
#endif
unsigned n_terms = n_64;
unsigned log_n = __builtin_ctz( n_terms );
uint64_t * a_fx = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*2*n_terms );
if( NULL == a_fx ) { printf("alloc fail.\n"); exit(-1); }
uint64_t * b_fx = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*2*n_terms );
if( NULL == b_fx ) { printf("alloc fail.\n"); exit(-1); }
#ifdef _PROFILE_
bm_start(&bm_tr);
#endif
encode_128_half_input_zero( a_fx , a_bc , n_terms );
//encode( a_fx , a_bc , n_terms );
encode_128_half_input_zero( b_fx , b_bc , n_terms );
//encode( b_fx , b_bc , n_terms );
#ifdef _PROFILE_
bm_stop(&bm_tr);
#endif
#ifdef _PROFILE_
bm_start(&bm_butterfly);
#endif
btfy_128( b_fx , n_terms , 64+log_n+1 );
btfy_128( a_fx , n_terms , 64+log_n+1 );
#ifdef _PROFILE_
bm_stop(&bm_butterfly);
#endif
#ifdef _PROFILE_
bm_start(&bm_pointmul);
#endif
for(unsigned i=0;i<n_terms;i++) gf2ext128_mul_sse( (uint8_t *)&a_fx[i*2] , (uint8_t *)&a_fx[i*2] , (uint8_t*)& b_fx[i*2] );
#ifdef _PROFILE_
bm_stop(&bm_pointmul);
#endif
#ifdef _PROFILE_
bm_start(&bm_ibutterfly);
#endif
i_btfy_128( a_fx , n_terms , 64+log_n+1 );
#ifdef _PROFILE_
bm_stop(&bm_ibutterfly);
#endif
#ifdef _PROFILE_
bm_start(&bm_tr2);
#endif
decode_128( b_fx , a_fx , n_terms );
#ifdef _PROFILE_
bm_stop(&bm_tr2);
#endif
#ifdef _PROFILE_
bm_start(&bm_ibc);
#endif
//bc_to_mono_2( b_fx , 2*n_64 );
bc_to_mono_2_unit256( b_fx , 2*n_64 );
#ifdef _PROFILE_
bm_stop(&bm_ibc);
#endif
for(unsigned i=0;i<(2*_n_64);i++) {
c[i] = b_fx[i];
}
free(a_bc);
free(b_bc);
free(a_fx);
free(b_fx);
}
///////////////////////////////////////////////////
void bitpolymul_2_64( uint64_t * c , const uint64_t * a , const uint64_t * b , unsigned _n_64 )
{
if( 0 == _n_64 ) return;
if( _n_64 > (1<<26) ) { printf("un-supported length of polynomials."); exit(-1); }
unsigned n_64 = 0;
if( 1 == _n_64 ) n_64 = _n_64;
else {
unsigned log_2_n64 = LOG2(_n_64);
unsigned log_2_n64_1 = LOG2(_n_64-1);
if( log_2_n64 == log_2_n64_1 )log_2_n64 += 1;
n_64 = 1<<log_2_n64;
}
if( 256 > n_64 ) n_64 = 256;
uint64_t * a_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64 );
//uint64_t * a_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64*2 );
if( NULL == a_bc ) { printf("alloc fail.\n"); exit(-1); }
uint64_t * b_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64 );
//uint64_t * b_bc = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_64*2 );
if( NULL == b_bc ) { printf("alloc fail.\n"); exit(-1); }
#ifdef _PROFILE_
bm_start(&bm_bc);
#endif
memcpy( a_bc , a , sizeof(uint64_t)*_n_64 );
for(unsigned i=_n_64;i<n_64;i++) a_bc[i] = 0;
//for(unsigned i=n_64;i<2*n_64;i++) a_bc[i] = 0;
//bc_to_lch_2( a_bc , n_64 );
bc_to_lch_2_unit256( a_bc , n_64 );
//for(unsigned i=n_64;i<n_64*2;i++) a_bc[i] = 0;
memcpy( b_bc , b , sizeof(uint64_t)*_n_64 );
for(unsigned i=_n_64;i<n_64;i++) b_bc[i] = 0;
//for(unsigned i=n_64;i<2*n_64;i++) b_bc[i] = 0;
//bc_to_lch_2( b_bc , n_64 );
bc_to_lch_2_unit256( b_bc , n_64 );
//for(unsigned i=n_64;i<n_64*2;i++) b_bc[i] = 0;
#ifdef _PROFILE_
bm_stop(&bm_bc);
#endif
unsigned n_terms = n_64*2;
unsigned log_n = __builtin_ctz( n_terms );
uint64_t * a_fx = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_terms );
if( NULL == a_fx ) { printf("alloc fail.\n"); exit(-1); }
uint64_t * b_fx = (uint64_t*)aligned_alloc( 32 , sizeof(uint64_t)*n_terms );
if( NULL == b_fx ) { printf("alloc fail.\n"); exit(-1); }
#ifdef _PROFILE_
bm_start(&bm_tr);
#endif
encode_64_half_input_zero( a_fx , a_bc , n_terms );
encode_64_half_input_zero( b_fx , b_bc , n_terms );
#ifdef _PROFILE_
bm_stop(&bm_tr);
#endif
#ifdef _PROFILE_
bm_start(&bm_butterfly);
#endif
btfy_64( b_fx , n_terms , 32+log_n+1 );
btfy_64( a_fx , n_terms , 32+log_n+1 );
#ifdef _PROFILE_
bm_stop(&bm_butterfly);
#endif
#ifdef _PROFILE_
bm_start(&bm_pointmul);
#endif
for(unsigned i=0;i<n_terms;i+=4) {
_mm_prefetch( &a_fx[i+4] , _MM_HINT_T0 );
_mm_prefetch( &b_fx[i+4] , _MM_HINT_T0 );
gf2ext64_mul_4x4_avx2( (uint8_t *)&a_fx[i] , (uint8_t *)&a_fx[i] , (uint8_t*)& b_fx[i] );
}
#ifdef _PROFILE_
bm_stop(&bm_pointmul);
#endif
#ifdef _PROFILE_
bm_start(&bm_ibutterfly);
#endif
i_btfy_64( a_fx , n_terms , 32+log_n+1 );
#ifdef _PROFILE_
bm_stop(&bm_ibutterfly);
#endif
#ifdef _PROFILE_
bm_start(&bm_tr2);
#endif
decode_64( b_fx , a_fx , n_terms );
#ifdef _PROFILE_
bm_stop(&bm_tr2);
#endif
#ifdef _PROFILE_
bm_start(&bm_ibc);
#endif
bc_to_mono_2_unit256( b_fx , n_terms );
#ifdef _PROFILE_
bm_stop(&bm_ibc);
#endif
for(unsigned i=0;i<(2*_n_64);i++) {
c[i] = b_fx[i];
}
free(a_bc);
free(b_bc);
free(a_fx);
free(b_fx);
}