File: pmf.c

package info (click to toggle)
libzn-poly 0.8-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 700 kB
  • sloc: ansic: 9,295; python: 162; makefile: 7; sh: 6
file content (592 lines) | stat: -rw-r--r-- 14,772 bytes parent folder | download | duplicates (2)
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
/*
   pmf.c:  polynomials modulo a fermat polynomial
   
   Copyright (C) 2007, 2008, David Harvey
   
   This file is part of the zn_poly library (version 0.8).
   
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) version 3 of the License.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "zn_poly_internal.h"


#if DEBUG

/* ============================================================================

     debugging stuff

============================================================================ */

#include <stdio.h>


/*
   res := op, with bias set to zero.

   Inplace operation not allowed.
*/
void zn_pmf_normalise(zn_pmf_t res, zn_pmf_t op, ulong M, const zn_mod_t mod)
{
   ZNP_ASSERT(res != op);
   
   res[0] = 0;

   ulong i, b = op[0] & (2*M - 1);

   op++;
   res++;

   if (b < M)
   {
      for (i = 0; i < M-b; i++)
         res[b+i] = op[i];
      for (i = 0; i < b; i++)
         res[i] = zn_mod_neg(op[i+M-b], mod);
   }
   else
   {
      b -= M;
      for (i = 0; i < M-b; i++)
         res[b+i] = zn_mod_neg(op[i], mod);
      for (i = 0; i < b; i++)
         res[i] = op[i+M-b];
   }
}


void zn_pmf_print(const zn_pmf_t op, ulong M, const zn_mod_t mod)
{
   ZNP_FASTALLOC(buf, ulong, 6624, M + 1);

   zn_pmf_normalise(buf, op, M, mod);
   
   printf("[%lu", buf[1]);
   ulong i;
   for (i = 1; i < M; i++)
      printf(" %lu", buf[i+1]);
   printf("]");
   
   ZNP_FASTFREE(buf);
}


void zn_pmf_vec_print(const zn_pmf_vec_t op)
{
   printf("M = %lu, K = %lu\n", op->M, op->K);
   ulong i;
   for (i = 0; i < op->K; i++)
   {
      printf("%3lu: ", i);
      zn_pmf_print(op->data + i*op->skip, op->M, op->mod);
      printf("\n");
   }
}


void zn_pmf_vec_print_trunc(const zn_pmf_vec_t op, ulong length)
{
   printf("M = %lu, K = %lu\n", op->M, op->K);
   ulong i;
   for (i = 0; i < length; i++)
   {
      printf("%3lu: ", i);
      zn_pmf_print(op->data + i*op->skip, op->M, op->mod);
      printf("\n");
   }
}

#endif



/* ============================================================================

     inplace array butterflies

============================================================================ */


/*
   op1 := op2 + op1
   op2 := op2 - op1
   where op1 and op2 are arrays of length _len_.

   Inputs must be [0, n); outputs will be in [0, n).
*/
void zn_array_bfly_inplace(ulong* op1, ulong* op2, ulong len,
                           const zn_mod_t mod)
{
   ulong x, y;
   
   if (zn_mod_is_slim(mod))
   {
      // slim version
      // (unrolled)
      for (; len >= 4; len -= 4)
      {
         x = *op1; y = *op2;
         *op1++ = zn_mod_add_slim(y, x, mod);
         *op2++ = zn_mod_sub_slim(y, x, mod);

         x = *op1; y = *op2;
         *op1++ = zn_mod_add_slim(y, x, mod);
         *op2++ = zn_mod_sub_slim(y, x, mod);

         x = *op1; y = *op2;
         *op1++ = zn_mod_add_slim(y, x, mod);
         *op2++ = zn_mod_sub_slim(y, x, mod);

         x = *op1; y = *op2;
         *op1++ = zn_mod_add_slim(y, x, mod);
         *op2++ = zn_mod_sub_slim(y, x, mod);
      }
      for (; len; len--)
      {
         x = *op1; y = *op2;
         *op1++ = zn_mod_add_slim(y, x, mod);
         *op2++ = zn_mod_sub_slim(y, x, mod);
      }
   }
   else
   {
      // non-slim version
      // (unrolled)
      for (; len >= 4; len -= 4)
      {
         x = *op1; y = *op2;
         *op1++ = zn_mod_add(y, x, mod);
         *op2++ = zn_mod_sub(y, x, mod);

         x = *op1; y = *op2;
         *op1++ = zn_mod_add(y, x, mod);
         *op2++ = zn_mod_sub(y, x, mod);

         x = *op1; y = *op2;
         *op1++ = zn_mod_add(y, x, mod);
         *op2++ = zn_mod_sub(y, x, mod);

         x = *op1; y = *op2;
         *op1++ = zn_mod_add(y, x, mod);
         *op2++ = zn_mod_sub(y, x, mod);
      }
      for (; len; len--)
      {
         x = *op1; y = *op2;
         *op1++ = zn_mod_add(y, x, mod);
         *op2++ = zn_mod_sub(y, x, mod);
      }
   }
}


/*
   op1 := op1 + op2
   where op1 and op2 are arrays of length _len_.

   Inputs must be [0, n); outputs will be in [0, n).
*/
void zn_array_add_inplace(ulong* op1, const ulong* op2, ulong len,
                          const zn_mod_t mod)
{
   if (zn_mod_is_slim(mod))
   {
      // slim version
      // (unrolled)
      for (; len >= 4; len -= 4)
      {
         *op1 = zn_mod_add_slim(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_add_slim(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_add_slim(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_add_slim(*op1, *op2, mod);
         op1++; op2++;
      }
      for (; len; len--)
      {
         *op1 = zn_mod_add_slim(*op1, *op2, mod);
         op1++; op2++;
      }
   }
   else
   {
      // non-slim version
      // (unrolled)
      for (; len >= 4; len -= 4)
      {
         *op1 = zn_mod_add(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_add(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_add(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_add(*op1, *op2, mod);
         op1++; op2++;
      }
      for (; len; len--)
      {
         *op1 = zn_mod_add(*op1, *op2, mod);
         op1++; op2++;
      }
   }
}



/*
   op1 := op1 - op2
   where op1 and op2 are arrays of length _len_.

   Inputs must be [0, n); outputs will be in [0, n).
*/
void zn_array_sub_inplace(ulong* op1, const ulong* op2, ulong len,
                          const zn_mod_t mod)
{
   if (zn_mod_is_slim(mod))
   {
      // slim version
      // (unrolled)
      for (; len >= 4; len -= 4)
      {
         *op1 = zn_mod_sub_slim(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_sub_slim(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_sub_slim(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_sub_slim(*op1, *op2, mod);
         op1++; op2++;
      }
      for (; len; len--)
      {
         *op1 = zn_mod_sub_slim(*op1, *op2, mod);
         op1++; op2++;
      }
   }
   else
   {
      // non-slim version
      // (unrolled)
      for (; len >= 4; len -= 4)
      {
         *op1 = zn_mod_sub(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_sub(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_sub(*op1, *op2, mod);
         op1++; op2++;
         *op1 = zn_mod_sub(*op1, *op2, mod);
         op1++; op2++;
      }
      for (; len; len--)
      {
         *op1 = zn_mod_sub(*op1, *op2, mod);
         op1++; op2++;
      }
   }
}



/* ============================================================================

     inplace zn_pmf_t butterflies

============================================================================ */


/*
   In the following routines, we work with the "relative bias" between the
   two inputs, i.e. b = difference between bias of op1 and op2. This allows
   us to avoid unnecessary normalisation steps; we just add and subtract
   directly into the correct memory locations.
*/


void zn_pmf_bfly(zn_pmf_t op1, zn_pmf_t op2, ulong M, const zn_mod_t mod)
{
   ulong b = op2[0] - op1[0];
   
   if (b & M)
   {
      // bias is in [M, 2M) mod 2M
      b &= (M - 1);
      
      // butterfly on op1[0, b) and op2[M-b, M)
      zn_array_bfly_inplace(op1+1, op2+1+M-b, b, mod);
      // butterfly on op1[b, M) and op2[0, M-b)
      zn_array_bfly_inplace(op2+1, op1+1+b, M-b, mod);
   }
   else
   {
      // bias is in [0, M) mod 2M
      b &= (M - 1);
      
      // butterfly on op1[0, b) and op2[M-b, M)
      zn_array_bfly_inplace(op2+1+M-b, op1+1, b, mod);
      // butterfly on op1[b, M) and op2[0, M-b)
      zn_array_bfly_inplace(op1+1+b, op2+1, M-b, mod);
   }
}


void zn_pmf_add(zn_pmf_t op1, const zn_pmf_t op2, ulong M, const zn_mod_t mod)
{
   ulong b = op2[0] - op1[0];

   if (b & M)
   {
      // bias is in [M, 2M) mod 2M
      b &= (M - 1);
      
      // add op2[M-b, M) to op1[0, b)
      zn_array_add_inplace(op1+1, op2+1+M-b, b, mod);
      // subtract op2[0, M-b) from op1[b, M)
      zn_array_sub_inplace(op1+1+b, op2+1, M-b, mod);
   }
   else
   {
      // bias is in [0, M) mod 2M
      b &= (M - 1);

      // subtract op2[M-b, M) from op1[0, b)
      zn_array_sub_inplace(op1+1, op2+1+M-b, b, mod);
      // add op2[0, M-b) to op1[b, M)
      zn_array_add_inplace(op1+1+b, op2+1, M-b, mod);
   }
}


void zn_pmf_sub(zn_pmf_t op1, const zn_pmf_t op2, ulong M, const zn_mod_t mod)
{
   ulong b = op2[0] - op1[0];

   if (b & M)
   {
      // bias is in [M, 2M) mod 2M
      b &= (M - 1);
      
      // subtract op2[M-b, M) from op1[0, b)
      zn_array_sub_inplace(op1+1, op2+1+M-b, b, mod);
      // add op2[0, M-b) to op1[b, M)
      zn_array_add_inplace(op1+1+b, op2+1, M-b, mod);
   }
   else
   {
      // bias is in [0, M) mod 2M
      b &= (M - 1);
      
      // add op2[M-b, M) to op1[0, b)
      zn_array_add_inplace(op1+1, op2+1+M-b, b, mod);
      // subtract op2[0, M-b) from op1[b, M)
      zn_array_sub_inplace(op1+1+b, op2+1, M-b, mod);
   }
}



/* ============================================================================

     zn_pmf_vec stuff

============================================================================ */


void zn_pmf_vec_init(zn_pmf_vec_t res, unsigned lgK, ptrdiff_t skip,
                     unsigned lgM, const zn_mod_t mod)
{
   ZNP_ASSERT(skip >= (1UL << lgM) + 1);
   
   res->lgK = lgK;
   res->lgM = lgM;
   res->skip = skip;
   res->K = 1UL << lgK;
   res->M = 1UL << lgM;
   res->mod = mod;
   res->data = (ulong*) malloc(sizeof(ulong) * skip * res->K);
}


void zn_pmf_vec_init_nussbaumer(zn_pmf_vec_t res, unsigned lgL,
                                const zn_mod_t mod)
{
   unsigned lgK, lgM;
   nussbaumer_params(&lgK, &lgM, lgL);
   zn_pmf_vec_init(res, lgK, (1UL << lgM) + 1, lgM, mod);
}


void zn_pmf_vec_clear(zn_pmf_vec_t op)
{
   free(op->data);
}


void zn_pmf_vec_set(zn_pmf_vec_t res, const zn_pmf_vec_t op)
{
   ZNP_ASSERT(zn_pmf_vec_compatible(res, op));
   
   ulong i;
   for (i = 0; i < op->K; i++)
      zn_pmf_set(res->data + i * res->skip, op->data + i * op->skip, op->M);
}


void zn_pmf_vec_scalar_mul(zn_pmf_vec_t op, ulong len, ulong x)
{
   ZNP_ASSERT(len <= op->K);

   ulong i;
   zn_pmf_t ptr = op->data;
   for (i = 0; i < len; i++, ptr += op->skip)
      zn_pmf_scalar_mul(ptr, op->M, x, op->mod);
}


ulong zn_pmf_vec_mul_get_fudge(unsigned lgM, int squaring, const zn_mod_t mod)
{
   int use_nussbaumer = (lgM >= (squaring
                     ? tuning_info[mod->bits].nuss_sqr_crossover
                     : tuning_info[mod->bits].nuss_mul_crossover));
                     
   if (use_nussbaumer)
      return nussbaumer_mul_get_fudge(lgM, squaring, mod);
   else
      return _zn_array_mul_get_fudge(1UL << lgM, 1UL << lgM, squaring, mod);
}


void zn_pmf_vec_mul(zn_pmf_vec_t res, const zn_pmf_vec_t op1,
                    const zn_pmf_vec_t op2, ulong length,
                    int special_first_two)
{
   ZNP_ASSERT(res->mod->n & 1);
   ZNP_ASSERT(zn_pmf_vec_compatible(res, op1));
   ZNP_ASSERT(zn_pmf_vec_compatible(res, op2));
   ZNP_ASSERT(res->M >= 2 || !special_first_two);

   zn_pmf_t res_ptr = res->data;
   zn_pmf_const_t op1_ptr = op1->data;
   zn_pmf_const_t op2_ptr = op2->data;

   const zn_mod_struct* mod = res->mod;
   
   ulong M = op1->M;
   unsigned lgM = op1->lgM;
   
   int squaring = (op1 == op2);
   
   // use nussbaumer algorithm if the pointwise mults are large enough
   int use_nussbaumer = (lgM >= (squaring
                     ? tuning_info[mod->bits].nuss_sqr_crossover
                     : tuning_info[mod->bits].nuss_mul_crossover));

   // scratch space for nussbaumer multiplications
   zn_pmf_vec_t temp1, temp2;
   unsigned nuss_lgK;

   if (use_nussbaumer)
   {
      zn_pmf_vec_init_nussbaumer(temp1, lgM, mod);
      zn_pmf_vec_init_nussbaumer(temp2, lgM, mod);
      nuss_lgK = temp1->lgK;
   }

   ulong i = 0;

   if (special_first_two)
   {
      ZNP_FASTALLOC(temp, ulong, 6624, 2*M);
      
      // need to adjust the fudge factors, so that the fudge factor for these
      // first two special products matches up with the fudge factors for the
      // remaining products
      ulong fixup;
      fixup = use_nussbaumer ? nussbaumer_mul_get_fudge(lgM, squaring, mod)
                             : _zn_array_mul_get_fudge(M, M, squaring, mod);
      fixup = zn_mod_mul(_zn_array_mul_get_fudge(M/2, M/2, squaring, mod),
                         zn_mod_invert(fixup, mod), mod);

      // length M/2 multiplications
      for (; i < 2 && i < length; i++, res_ptr += res->skip,
             op1_ptr += op1->skip, op2_ptr += op2->skip)
      {
         // add biases
         res_ptr[0] = op1_ptr[0] + op2_ptr[0];

         // do the actual multiplication
         _zn_array_mul(temp, op1_ptr + 1, M/2, op2_ptr + 1, M/2, 1, mod);

         // apply the fudge factor
         if (fixup == 1)
            zn_array_copy(res_ptr + 1, temp, M - 1);
         else
            zn_array_scalar_mul(res_ptr + 1, temp, M - 1, fixup, mod);
         res_ptr[M] = 0;
      }
      
      ZNP_FASTFREE(temp);
   }
   
   if (use_nussbaumer)
   {
      for (; i < length; i++, res_ptr += res->skip,
             op1_ptr += op1->skip, op2_ptr += op2->skip)
      {
         // add biases
         res_ptr[0] = op1_ptr[0] + op2_ptr[0];

         nussbaumer_mul(res_ptr + 1, op1_ptr + 1, op2_ptr + 1, temp1, temp2);
      }

      zn_pmf_vec_clear(temp2);
      zn_pmf_vec_clear(temp1);
   }
   else
   {
      // scratch space for KS negacyclic multiplication
      ZNP_FASTALLOC(temp, ulong, 6624, 2*M);
      temp[2*M - 1] = 0;

      for (; i < length; i++, res_ptr += res->skip,
             op1_ptr += op1->skip, op2_ptr += op2->skip)
      {
         // add biases
         res_ptr[0] = op1_ptr[0] + op2_ptr[0];
         
         // ordinary multiplication...
         _zn_array_mul(temp, op1_ptr + 1, M, op2_ptr + 1, M, 1, mod);
         // ... negacyclic reduction
         zn_array_sub(res_ptr + 1, temp, temp + M, M, mod);
      }

      ZNP_FASTFREE(temp);
   }
}



void zn_pmf_vec_reverse(zn_pmf_vec_t op, ulong length)
{
   op->data += op->skip * (length - 1);
   op->skip = -op->skip;
}



// end of file ****************************************************************