-
Notifications
You must be signed in to change notification settings - Fork 0
/
toom-gpl.c
2046 lines (1701 loc) · 59.7 KB
/
toom-gpl.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
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
/* This file is part of the gf2x library.
Copyright 2007, 2008, 2009, 2010, 2013, 2015
Richard Brent, Pierrick Gaudry, Emmanuel Thome', Paul Zimmermann
This program is free software; you can redistribute it and/or modify it
under the terms of either:
- If the archive contains a file named toom-gpl.c (not a trivial
placeholder), the GNU General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
- If the archive contains a file named toom-gpl.c which is a trivial
placeholder, the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
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 license text for more details.
You should have received a copy of the GNU General Public License as
well as the GNU Lesser General Public License along with this program;
see the files COPYING and COPYING.LIB. If not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/* General Toom_Cook multiplication, calls KarMul, Toom3Mul, Toom3WMul
or Toom4Mul depending on which is expected to be the fastest. */
#include <limits.h>
#include <string.h>
/* from https://www.gnu.org/software/autoconf/manual/autoconf-2.60/html_node/Particular-Functions.html */
#if HAVE_ALLOCA_H
# include <alloca.h>
#elif defined __GNUC__
# define alloca __builtin_alloca
#elif defined _AIX
# define alloca __alloca
#elif defined _MSC_VER
# include <malloc.h>
# define alloca _alloca
#else
# include <stddef.h>
void *alloca (size_t);
#endif
#include "gf2x.h"
#include "gf2x/gf2x-impl.h"
/* We need gf2x_addmul_1_n */
#include "gf2x/gf2x-small.h"
const char * gf2x_toom_gpl_status="GPL-licensed GF2X";
/* the following routines come from the irred-ntl package from Paul Zimmermann,
(http://webloria.loria.fr/~zimmerma/irred/), who contributes them under
LGPL for gf2x */
/* c <- a + b */
static
void Add (unsigned long *c, const unsigned long *a, const unsigned long *b,
long n)
{
long i;
for (i = 0; i < n; i++)
c[i] = a[i] ^ b[i];
}
/* {d,n} <- {a,n} + {b,n} + {c,k} with k <= n */
static void
Add2 (unsigned long *d, const unsigned long *a, const unsigned long *b,
long n, const unsigned long *c, long k)
{
long i;
for (i = 0; i < k; i++)
d[i] = a[i] ^ b[i] ^ c[i];
for (; i < n; i++)
d[i] = a[i] ^ b[i];
}
/* c <- c + a + b */
static
void Add3(unsigned long *c, const unsigned long *a, const unsigned long *b,
long n)
{
long i;
for (i = 0; i < n; i++)
c[i] ^= a[i] ^ b[i];
}
/* c <- a + x * b, return carry out.
Warning: c might overlap with b. */
static
unsigned long AddLsh1(unsigned long *c, const unsigned long *a,
const unsigned long *b, long n)
{
unsigned long cy = 0UL, t;
long i;
for (i = 0; i < n; i++) {
t = a[i] ^ ((b[i] << 1) | cy);
cy = b[i] >> (GF2X_WORDSIZE - 1);
c[i] = t;
}
return cy;
}
/* c <- x * {a, n} + x^2 * {b, r} with r <= n, return carry out.
c should not overlap with a nor b. */
static
unsigned long AddLsh12a (unsigned long *c, const unsigned long *a, long n,
const unsigned long *b, long r)
{
unsigned long cy = 0UL;
long i;
for (i = 0; i < r; i++) {
c[i] = (a[i] << 1) ^ (b[i] << 2) ^ cy;
cy = a[i] >> (GF2X_WORDSIZE - 1) ^ b[i] >> (GF2X_WORDSIZE - 2);
}
for (; i < n; i++)
{
c[i] = (a[i] << 1) ^ cy;
cy = a[i] >> (GF2X_WORDSIZE - 1);
}
return cy;
}
/* c <- a + cy, return carry out (0 for n > 0, cy for n=0) */
static
unsigned long Add1(unsigned long *c, const unsigned long *a, long n,
unsigned long cy)
{
if (n) {
long i;
c[0] = a[0] ^ cy;
for (i = 1; i < n; i++)
c[i] = a[i];
return 0;
} else
return cy;
}
/* let c = q*(1+x^2) + X^n*r with X = x^GF2X_WORDSIZE and deg(r) < 2
then c <- q, returns r.
(Algorithm from Michel Quercia.)
*/
static unsigned long DivOnePlusX2(unsigned long * c, long n)
{
unsigned long t = 0;
long i;
#if (GF2X_WORDSIZE == 64)
/* mask[t] = t * (1 + x^2 + x^4 + ... + x^(GF2X_WORDSIZE-2)) */
unsigned long mask[4] = {0UL, 0x5555555555555555UL,
0xAAAAAAAAAAAAAAAAUL, 0xFFFFFFFFFFFFFFFFUL};
#elif (GF2X_WORDSIZE == 32)
unsigned long mask[4] = {0UL, 0x55555555UL, 0xAAAAAAAAUL, 0xFFFFFFFFUL};
#else
#error "GF2X_WORDSIZE should be 32 or 64"
#endif
for (i = 0; i < n; i++) {
/* invariant: t < 4 */
#ifndef GF2X_HAVE_PCLMUL_SUPPORT
unsigned long u;
/* u <- c[i] * (1 + x^2 + x^4 + ... + x^(GF2X_WORDSIZE-2)) */
u = c[i] ^ (c[i] << 2);
u ^= u << 4;
u ^= u << 8;
u ^= u << 16;
#if (GF2X_WORDSIZE == 64)
u ^= u << 32;
#endif
c[i] = u ^ mask[t];
#else /* use pclmul */
unsigned long cc[2];
gf2x_mul1 (cc, c[i], 0x5555555555555555UL);
c[i] = cc[0] ^ mask[t];
#endif
t = c[i] >> (GF2X_WORDSIZE - 2);
}
return t;
}
/********************************************************************
* Below this line, experimental code
* (C) 2007 Marco Bodrato <[email protected]>
* This code is released under the GPL 2.0 license, or any later version.
* Modified by Paul Zimmermann, April 2007.
*
* Reference: http://bodrato.it/papers/#WAIFI2007
*
* "Towards Optimal Toom-Cook Multiplication for Univariate and
* Multivariate Polynomials in Characteristic 2 and 0." by Marco
* BODRATO; in C.Carlet and B.Sunar, editors, "WAIFI'07 proceedings",
* LNCS 4547, pp. 119-136. Springer, Madrid, Spain, June 21-22, 2007.
*/
#if (GF2X_MUL_TOOM_THRESHOLD < 17)
#error "GF2X_MUL_TOOM_THRESHOLD should be at least 17"
#endif
/* c <- ( c + b )/x, return carry */
static
unsigned long Rsh1Add(unsigned long *c, const unsigned long *b, long n)
{
unsigned long cy = 0, t;
long i;
for (i = n - 1; i >= 0; i--) {
t = c[i] ^ b[i];
cy <<= GF2X_WORDSIZE - 1;
c[i] = (t >> 1) | cy;
cy = t;
}
return cy;
}
/* c <- ( c + b )/x + a, return carry */
static unsigned long
Rsh1Adda (unsigned long *c, const unsigned long *b, const unsigned long *a,
long n)
{
unsigned long cy = 0, t;
long i;
for (i = n - 1; i >= 0; i--) {
t = c[i] ^ b[i];
cy <<= GF2X_WORDSIZE - 1;
c[i] = (t >> 1) ^ cy ^ a[i];
cy = t;
}
return cy;
}
/* c <- c + (1+x^3) * b, return carry out */
static
unsigned long AddLsh13(unsigned long *c, const unsigned long *b, long n)
{
unsigned long cy = 0UL, t;
long i;
for (i = 0; i < n; i++) {
t = b[i];
c[i] ^= t ^ (t << 3) ^ cy;
cy = t >> (GF2X_WORDSIZE - 3);
}
return cy;
}
/* c <- c + a + b + d */
static
void Add4(unsigned long *c, const unsigned long *a, const unsigned long *b,
const unsigned long *d, long n)
{
long i;
for (i = 0; i < n; i++)
c[i] ^= a[i] ^ b[i] ^ d[i];
}
/* let c = q*(1+x) + X^n*r with X = x^GF2X_WORDSIZE and deg(r) < 1
then c <- q, returns r.
(Algorithm from Michel Quercia.)
*/
static
unsigned long DivOnePlusX(unsigned long *c, long n)
{
unsigned long t = 0;
long i;
#if (GF2X_WORDSIZE == 64)
/* mask[t] = t * (1 + x + x^2 + ... + x^(GF2X_WORDSIZE-1)) */
unsigned long mask[2] = {0UL, 0xFFFFFFFFFFFFFFFFUL};
#elif (GF2X_WORDSIZE == 32)
unsigned long mask[2] = {0UL, 0xFFFFFFFFUL};
#else
#error "GF2X_WORDSIZE should be 32 or 64"
#endif
for (i = 0; i < n; i++) {
/* invariant: t < 2 */
#ifndef GF2X_HAVE_PCLMUL_SUPPORT
unsigned long u;
/* u <- c[i] * (1 + x + x^2 + ... + x^(GF2X_WORDSIZE-1)) */
u = c[i] ^ (c[i] << 1);
u ^= u << 2;
u ^= u << 4;
u ^= u << 8;
u ^= u << 16;
#if (GF2X_WORDSIZE == 64)
u ^= u << 32;
#endif
c[i] = u ^ mask[t];
#else /* use pclmul */
unsigned long cc[2];
gf2x_mul1 (cc, c[i], 0xFFFFFFFFFFFFFFFFUL);
c[i] = cc[0] ^ mask[t];
#endif
t = c[i] >> (GF2X_WORDSIZE - 1);
}
return t;
}
#if (defined(DEBUG))
static void dump(const unsigned long *a, long n)
{
long i;
for (i = 0; i < n; i++) {
printf("+%lu*X^%lu", a[i], i);
if ((i + 1) % 3 == 0)
printf("\n");
}
printf(":\n");
}
#endif
/*
\\ gp-pari check code.
default(echo, 1);
A = (a2*x^2 + a1*x + a0)*Mod(1,2)
B = (b2*x^2 + b1*x + b0)*Mod(1,2)
C = A * B
c0 = polcoeff(C, 0)
c1 = polcoeff(C, 1)
c2 = polcoeff(C, 2)
c3 = polcoeff(C, 3)
c4 = polcoeff(C, 4)
\\ --- Evaluation phase. 10 add, 4 shift, 5 mul.
W0 = (a2*y^2+a1*y)*Mod(1,2)
W4 = (b2*y^2+b1*y)*Mod(1,2)
W3 = (a2+a1+a0) *Mod(1,2)
W2 = (b2+b1+b0) *Mod(1,2)
W1 = W2 * W3 \\ C(1)
W3 = W3 + W0
W2 = W2 + W4
W0 = W0+a0 *Mod(1,2)
W4 = W4+b0 *Mod(1,2)
W3 = W2 * W3 \\ C(y+1)
W2 = W0 * W4 \\ C(y)
W4 = a2 * b2 *Mod(1,2) \\ C(\infty)
W0 = a0 * b0 *Mod(1,2) \\ C(0)
\\ ------ Interpolation phase. 10 add, 2 shift, 2 div.
W3 = W3 + W2
W3 == ( c4 + (y^2+y+1)*c3 + c2 + c1 ) \\ check
W2 = ( ( W2 + W0 )/y + W3 + W4*(y^3+1) ) / (y+1)
\\W2 = ( W2 + W0 + W3*y + W4*(y^4+y) ) / (y^2+y)
W2 == ( c2 + c3 ) \\ check
W1 = W1 + W0
W1 == ( c4 + c3 + c2 + c1 ) \\ check
W3 = ( W3 + W1 ) / (y*(y+1))
W3 == ( c3 ) \\ check
W1 = W1 + W4 + W2
W1 == ( c1 ) \\ check
W2 = W2 + W3
W2 == ( c2 ) \\ check
C == W4*x^4+ W3*x^3+ W2*x^2+ W1*x + W0 \\ check
quit;
*/
/*
c must have space for 2n words.
stk must have space for max( KarMem(n), 5k+2 + ToomMem(k+1))
where k = ceil(n/3).
*/
void gf2x_mul_tc3(unsigned long *c, const unsigned long *a,
const unsigned long *b, long n, unsigned long *stk)
{
long k = (n + 2) / 3; /* ceil(n/3) */
long r = n - 2 * k;
unsigned long cy;
unsigned long *W0 = c;
unsigned long *W1 = stk;
unsigned long *W2 = c + 2 * k;
unsigned long *W3 = stk + 2 * k;
unsigned long *W4 = c + 4 * k;
assert(c != a);
assert(c != b);
/* \\ --- Evaluation phase. 10 add, 4 shift, 5 mul. */
/* W0 = (a2*y^2+a1*y) */
/* W4 = (b2*y^2+b1*y) */
/* W0 = (a2*y+a1)*y */
/* W4 = (b2*y+b1)*y */
W0[k] = AddLsh12a (W0, a + k, k, a + 2 * k, r); /* x * a1 + x^2 * a2 */
W4[2 + k] = AddLsh12a (W4 + 2, b + k, k, b + 2 * k, r); /* x * b1 + x^2 b2 */
/* using W4[2+k] requires that k+3 words are available at W4=c+4k.
Since c contains 2n=4k+2r words, then W4 contains 2r words, thus
we need k+3 <= 2r. This is true for n >= 17.
Also true for n = 9, 12, 14, 15 but timing tests show that
this is not the fastest routine for such small n. */
ASSERT(k + 3 <= 2 * r);
/* {c, k+1}: x*a1+x^2*a2, {c+4k, k+1}: x*b1+x^2*b2 */
/* W3 = ((a2+a1)+a0) */
/* W2 = ((b2+b1)+b0) */
Add2 (c + k + 1, a, a + k, k, a + 2 * k, r); /* a0 + a1 + a2 */
Add2 (W2 + 2, b, b + k, k, b + 2 * k, r); /* b0 + b1 + b2 */
/* W1 = W2 * W3 \\ C(1) */
/* {c, k+1}: x*a1+x^2*a2, {c+k+1, k}: a0+a1+a2, {c+2k+2,k}: b0+b1+b2,
{c+4k, k+1}: x*b1+x^2*b2 */
gf2x_mul_toom(W1, c + k + 1, W2 + 2, k, W3); /* W1 : 2*k */
/* {c, k+1}: x*a1+x^2*a2, {c+k+1, k}: a0+a1+a2, {c+2k+2,k}: b0+b1+b2,
{c+4k, k+1}: x*b1+x^2*b2, {stk, 2k}: C(1) */
/* W3 = W3 + W0 */
/* W2 = W2 + W4 */
Add(c + k + 1, c + k + 1, W0, k);
c[2 * k + 1] = W0[k]; /* a0 + (x+1)a1 + (x^2+1)a2 */
Add(W2 + 2, W2 + 2, W4 + 2, k);
W2[k + 2] = W4[k + 2]; /* b0 + (x+1)b1 + (x^2+1)b2 */
/* since we use W2[k+2], we need k+3 words in W2, i.e., 3 <= k */
// ASSERT (k >= 3);
/* {c, k+1}: x*a1+x^2*a2, {c+k+1, k+1}: a0+(1+x)*a1+(1+x^2)*a2,
{c+2k+2,k+1}: b0+(1+x)*b1+(1+x^2)*b2,
{c+4k, k+1}: x*b1+x^2*b2, {stk, 2k}: C(1) */
/* W0 = W0+a0 */
/* W4 = W4+b0 */
Add(W0, W0, a, k); /* a0 + (x)a1 + (x^2)a2 */
Add(W4 + 2, W4 + 2, b, k); /* b0 + (x)b1 + (x^2)b2 */
/* {c, k+1}: a0+x*a1+x^2*a2, {c+k+1, k+1}: a0+(1+x)*a1+(1+x^2)*a2,
{c+2k+2,k+1}: b0+(1+x)*b1+(1+x^2)*b2,
{c+4k, k+1}: b0+x*b1+x^2*b2, {stk, 2k}: C(1) */
/* W3 = W2 * W3 \\ C(y+1) */
/* W2 = W0 * W4 \\ C(y) */
gf2x_mul_toom(W3, W2 + 2, c + k + 1, k + 1, stk + 5 * k + 2); /* W3 : 2*k+1 */
/* {c, k+1}: a0+x*a1+x^2*a2, {c+k+1, k+1}: a0+(1+x)*a1+(1+x^2)*a2,
{c+2k+2,k+1}: b0+(1+x)*b1+(1+x^2)*b2,
{c+4k, k+1}: b0+x*b1+x^2*b2, {stk, 2k}: C(1), {stk+2k,2k+2}: C(1+x) */
gf2x_mul_toom(W2, W0, W4 + 2, k + 1, stk + 4 * k + 3);
cy = W4[0]; /* contains at most 3 bits */
ASSERT(cy <= 7);
/* {c, k+1}: a0+x*a1+x^2*a2, {c+2k, 2k+2}: C(x),
{c+4k, k+1}: b0+x*b1+x^2*b2, {stk, 2k}: C(1), {stk+2k,2k+2}: C(1+x) */
/* W4 = a2 * b2 \\ C(\infty) */
/* W0 = a0 * b0 \\ C(0) */
gf2x_mul_toom(W0, a, b, k, stk + 4 * k + 3); /* W0 : 2*k */
/* {c, 2k}: C(0), {c+2k, 2k+2}: C(x),
{c+4k, k+1}: b0+x*b1+x^2*b2, {stk, 2k}: C(1), {stk+2k,2k+2}: C(1+x) */
gf2x_mul_toom(W4, a + 2 * k, b + 2 * k, r, stk + 4 * k + 3); /* W4 : 2*r */
/* {c, 2k}: C(0), {c+2k, 2k}+cy: C(x),
{c+4k, 2r}: C(Inf), {stk, 2k}: C(1), {stk+2k,2k+2}: C(1+x) */
/* \\ ------ Interpolation phase. 10 add, 2 shift, 2 div. */
/* W3 = W3 + W2: W3 has at most 2k words + 3 bits, W2 has 2k words +
at most 3 bits (stored in cy). */
Add(W3, W3, W2, 2 * k);
W3[2 * k] ^= cy;
/* now W3 has at most 2k words + 1 bit, which can be non-zero only
if r = k one most significant bit from a2 and b2 is set. */
ASSERT(W3[2 * k] <= 1);
/* {c, 2k}: C(0), {c+2k, 2k}+cy: C(x),
{c+4k, 2r}: C(Inf), {stk, 2k}: C(1), {stk+2k,2k+2}: C(1+x)+C(x) */
/* W2 = ( ( W2 + W0 )/y + W3 + W4*(y^3+1) ) / (y+1) */
/* \\W2 = ( W2 + W0 + W3*y + W4*(y^4+y) ) / (y^2+y) */
/* W2 has 2k words + at most 3 bits (stored in cy), W0 has 2k words */
Rsh1Adda (W2, W0, W3, 2 * k);
W2[2 * k - 1] ^= cy << (GF2X_WORDSIZE - 1);
/* now W2 has at most 2k words + 3 bits (cy >> 1), but since the final
results will have 2k words only, we can ignore cy. */
cy = AddLsh13(W2, W4, 2 * r);
if (r != k)
W2[2 * r] ^= cy;
/* else ignore the carry, since W2 should have 2k words, taking into
account the above ignored cy >> 1. */
DivOnePlusX(W2, 2 * k);
/* W1 = W1 + W0 */
/* W1 == ( c4 + c3 + c2 + c1 ) \\ check */
Add(W1, W1, W0, 2 * k);
/* W3 = ( W3 + W1 ) / (y*(y+1)) */
/* W3 == ( c3 ) \\ check */
Rsh1Add(W3, W1, 2 * k);
W3[2 * k - 1] |= W3[2 * k] << (GF2X_WORDSIZE - 1);
DivOnePlusX(W3, 2 * k);
/* W1 = W1 + W4 */
Add(W1, W1, W4, 2 * r);
/* perform simultaneously W1 <- W1 + W2, W2 <- W2 + W3,
and {c + k, 4k} <- {c + k, 4k} + {W1, 4k} */
Add3(c + k, W1, W2, k);
Add4(W2, W1 + k, W2 + k, W3, k);
Add3(W2 + k, W3, W3 + k, k);
Add(W4, W4, W3 + k, k);
/* C == W4*x^4+ W3*x^3+ W2*x^2+ W1*x + W0 \\ check */
/* assume 5*k <= 2*n = 4*k + 2*r, i.e., k <= 2*r, which is true for n >= 8 */
ASSERT(k <= 2 * r);
}
/*
* Below this line, experimental code
* (C) 2007 Richard Brent <[email protected]>
* This code is released under the GPL 2.0 license, or any later version.
*
* Based on Sec. 5.2 of Marco Bodrato's paper (reference below)
* but with full-word aligned operations to reduce overheads.
*
* Reference: http://bodrato.it/papers/#WAIFI2007
*
* "Towards Optimal Toom-Cook Multiplication for Univariate and
* Multivariate Polynomials in Characteristic 2 and 0." by Marco
* BODRATO; in C.Carlet and B.Sunar, editors, "WAIFI'07 proceedings",
* LNCS 4547, pp. 119-136. Springer, Madrid, Spain, June 21-22, 2007.
*/
// Need GF2X_MUL_TOOMU_THRESHOLD >= 11 for internal reasons
// but calls to Toom should have size at least 8 so
// need GF2X_MUL_TOOMU_THRESHOLD >= 33.
#define MINI_GF2X_MUL_TOOMU_THRESHOLD 33
#if (GF2X_MUL_TOOMU_THRESHOLD < MINI_GF2X_MUL_TOOMU_THRESHOLD)
#error "GF2X_MUL_TOOMU_THRESHOLD should be at least 33"
#endif
/*
Unbalanced Toom-Cook multiplication, assumes a takes sa words,
b takes n = sb = (sa+1)/2 words,
returns product c of sa+sb words using five multiplications of
size (n/2 + O(1)) by (n/2 + O(1)). See Bodrato, pg. 125, top right.
c should not overlap the inputs.
stk must have space for sp(sa) = gf2x_toomuspace(sa) words, where
sp(sa) = 2*sa + 32 + gf2x_toomspace(sa/4 + 4)
>= 4*(2*ceil(n/2) + 3) + gf2x_toomspace(floor(n/2) + 3)
and gf2x_toomspace(n) is the maximum space needed for the Toom-Cook routines
KarMul, Toom3Mul, Toom3wMul, Toom4Mul.
It is assumed that sa >= 33 so n >= 17.
*/
void gf2x_mul_tc3u(unsigned long * c, const unsigned long * a, long sa,
const unsigned long * b, unsigned long * stk)
{
ASSERT(sa >= MINI_GF2X_MUL_TOOMU_THRESHOLD);
// n should be at least 6 for internal
// reasons and 17 so calls to Toom
// have size at least 9, so need
// sa >= 33.
long n = (sa + 1) / 2; // Assume sb == n == ceil(sa/2)
long k = (n + 1) / 2; // ceil(n/2)
long d = n & 1; // d = odd(n) = 2k - n, n = 2k - d
long rb = n - k; // Size(b1) = k - d
long ra = sa - 3 * k; // Size(a3) = ra = k - 2d - odd(sa)
long sc = sa + n; // Size(c) = 6k - 3d - odd(sa)
const unsigned long *a0 = a; // Aliases for four parts of a
const unsigned long *a1 = a0 + k;
const unsigned long *a2 = a1 + k;
const unsigned long *a3 = a2 + k;
const unsigned long *b0 = b; // Aliases for two parts of b
const unsigned long *b1 = b0 + k;
long k2 = 2 * (k + 3); // Size of temporary arrays
unsigned long *W0 = c; // Overlap W0 (size 2*k) with c
unsigned long *W1 = stk;
unsigned long *W2 = c + 2 * k; // Overlap W2 with c + 2*k ...
unsigned long *W3 = W1 + k2;
unsigned long *W4 = W3 + k2; // But not W4 as W2 too large
unsigned long *W5 = W4 + k2; // W5 is synonymous with W3 in
// Bodrato's paper
stk += 4 * k2; // 4 temporaries of size k2
unsigned long s, t;
long j;
// In the comments y = x**w where w = wordlength = NTL_BITS_PER_LONG
// y can be thought of as a w-bit shift operator.
// Bodrato's code corresponds to w = 1, which minimises the size of
// the arguments in the Toom calls, but requires a lot of fiddly
// bit-operations. By choosing w = 32 or 64 we simplify the coding
// and obtain opportunities for loop optimisation. Both methods have
// the same asymptotic complexity.
//
// If the equal-size multiplication is O(n^alpha) then we expect Toom3uMul
// to be worthwhile when alpha > lg(5/2) = 1.3219...
// TC2 has alpha = lg(3) = 1.58...,
// TC3 has alpha = lg(5)/lg(3) = 1.46...,
// TC4 has alpha = lg(7)/2 = 1.40...,
// thus in all these cases Toom3uMul should be worthwhile on average
// (saving about 5.5% for the case of TC4, and more for other cases).
// However, this analysis does not take O(n) overheads into account
// so it is inaccurate for small n.
//
// In the comments " + " means addition in GF(2) and " ^ " means
// exponentiation.
// Evaluation phase Size is (max) size in words
// W5 = a3 + a2 + a1 + a0 == A(1) // Size(W5) := k
// W2 = b1 + b0 == B(1) // Size(W2) := k
for (j = 0; j < ra; j++) {
W5[j] = a3[j] ^ a2[j] ^ a1[j] ^ a0[j];
W2[j] = b1[j] ^ b0[j];
}
for (; j < rb; j++) {
W5[j] = a2[j] ^ a1[j] ^ a0[j]; // No a3[j] here
W2[j] = b1[j] ^ b0[j];
}
for (; j < k; j++) {
W5[j] = a2[j] ^ a1[j] ^ a0[j];
W2[j] = b0[j]; // No b1[j] here
}
// Calls to Toom mixed with further evaluation. There are 5 calls
// to Toom with sizes at most k+3-d = n/2 + 3 = (sa+1)/4 + 3.
// W1 = W2 * W5 == C(1)
gf2x_mul_toom(W1, W2, W5, k, stk); // Size(W1) := 2*k
// W0 = a3*y^3 + a2*y^2 + a1*y == A(y) - a0 // Size(W0) := k+3-d (at most)
W0[0] = 0;
W0[1] = a1[0];
W0[2] = a2[0] ^ a1[1];
for (j = 0; j < k - 3; j++) // Assumes k > 2, i.e. n > 4
W0[j + 3] = a3[j] ^ a2[j + 1] ^ a1[j + 2];
W0[j + 3] = a2[j + 1] ^ a1[j + 2]; // Fix up a3 contribution later
j++;
W0[j + 3] = a2[j + 1]; // No a1[j+2] here, a3 later
for (j++; j < rb; j++) // Size(W0) := k+3-d (at most)
W0[j + 3] = 0; // Need k+3-d <= 2*k
// which is true if n > 4.
for (j = k - 3; j < ra; j++)
W0[j + 3] ^= a3[j]; // Fix up a3 contribution
// W5 += W0 + a3*(y^2 + y) // Size(W5) := k+3-d
// W0 += a0 == A(y) // Size(W0) = k+3-d > k
for (j = k; j < rb + 3; j++) // rb == k-d so rb+3 == k+3-d
W5[j] = 0;
W5[0] ^= W0[0];
W0[0] ^= a0[0];
t = 0;
for (j = 1; j < ra; j++) // Usual case, ra-1 iterations
{
unsigned long u;
u = W0[j];
W0[j] = u ^ a0[j];
s = a3[j - 1];
W5[j] ^= s ^ (t ^ u);
t = s;
}
s = a3[j - 1];
W5[j] ^= W0[j] ^ s ^ t;
j++;
W5[j] ^= s;
for (; j < rb + 3; j++)
W5[j] ^= W0[j];
for (j = ra; j < k; j++)
W0[j] ^= a0[j];
// Pad W2 to size k+3-d for future Toom call (which requires equal-sized
// inputs). This is (asymptotically) more efficient that calling AddMul1.
for (j = k; j < rb + 3; j++)
W2[j] = 0; // Size(W2) := k+3-d
// W2 += b1*y
// W4 = W2 + b1 == B(y) // Size(W4) := k+3-d
// but 2 high words zero
W4[0] = W2[0] ^ b1[0];
t = b1[0];
for (j = 1; j < rb; j++) {
unsigned long s;
s = W2[j] ^ t;
W2[j] = s;
t = b1[j];
W4[j] = s ^ t;
}
W2[j] ^= t;
for (; j < rb + 3; j++)
W4[j] = W2[j];
// W3 = W5 * W2 == C(1+y) // Size(W3) := 2*(k+3-d)
// but high 2 words zero
gf2x_mul_toom(W3, W5, W2, rb + 3, stk);
// W2 = W0 * W4 == C(y)
gf2x_mul_toom(W2, W0, W4, rb + 3, stk); // Size(W2) := 2*(k+3-d)
// but 2 high words zero
// W4 = a3 * b1 == C(infinity)
if (ra < rb) {
for (j = 0; j < ra; j++) // W5 := a3 padded to
W5[j] = a3[j]; // size rb > ra
for (; j < rb; j++)
W5[j] = 0; // Size(W5) := rb
gf2x_mul_toom(W4, W5, b1, rb, stk); // Size(W4) := 2*rb
}
else
gf2x_mul_toom(W4, a3, b1, rb, stk); // Avoid copy if ra == rb
// W0 = a0 * b0 == C(0)
gf2x_mul_toom(W0, a0, b0, k, stk); // Size(W0) := 2*k;
// Interpolation phase
// W3 += W2 == c1 + c2 + c3*(1 + y + y^2) + c4
// W2 += W0 == C(y) + C(0)
for (j = 0; j < 2 * k; j++) { // First 2*k iterations
unsigned long s;
s = W2[j];
W3[j] ^= s; // Size(W0) = 2*k
W2[j] = s ^ W0[j]; // other sizes 2*rb + 4
} // ignoring known zeros
for (; j < 2 * rb + 4; j++)
W3[j] ^= W2[j]; // Last 4 - 2*d iterations
ASSERT(W2[0] == 0); // Division should be exact
// W2 = W2/y + W3
for (j = 0; j < 2 * rb + 3; j++)
W2[j] = W2[j + 1] ^ W3[j];
W2[j] = W3[j]; // Size(W2) := 2*rb + 4
// W2 = (W2 + W4*(1+y^3))/(1+y) == c2 + c3
for (j = 0, s = 0; j < 3; j++) {
s ^= W2[j] ^ W4[j];
W2[j] = s; // first 3 iterations special
}
for (; j < 2 * rb; j++) {
s ^= W2[j] ^ W4[j] ^ W4[j - 3]; // next 2*rb-3 are usual case
W2[j] = s;
}
for (; j < 2 * rb + 3; j++) {
s ^= W2[j] ^ W4[j - 3]; // next 3 are special
W2[j] = s;
}
// W2[j] = 0; // Size(W2) = 2*rb + 4
// but last word zero
// so Size(W2) := 2*rb + 3
// W1 += W0 == c1 + c2 + c3 + c4
// W3 += W1 == c3*y*(1+y)
for (j = 0; j < 2 * k; j++) {
unsigned long s;
s = W0[j] ^ W1[j];
W1[j] = s; // Size(W0) = Size(W1) = 2*k
W3[j] ^= s; // Size(W3) = 2*rb + 4 > 2*k
}
ASSERT(W3[0] == 0); // Next division exact
// W3 = W3/(y + y^2) == c3
for (j = 0, s = 0; j < 2 * rb + 3; j++) {
s ^= W3[j + 1];
W3[j] = s;
}
// W3[j] = 0;
ASSERT(s == 0); // Division exact
// Size(W3) := 2*rb + 2
// W1 += W2 + W4 == c1 // Size(W4) == 2*rb
// W2 += W3 == c2 // <= Size(W1) == 2*k
// <= Size(W3) == 2*rb + 2
// < Size(W2) == 2*rb + 3
for (j = 0; j < 2 * rb; j++) { // Usual case
unsigned long s;
s = W2[j];
W1[j] ^= s ^ W4[j];
W2[j] = s ^ W3[j];
}
for (; j < 2 * k; j++) { // Next 2*d iterations
unsigned long s;
s = W2[j];
W1[j] ^= s; // No W4[j] here
W2[j] = s ^ W3[j];
}
for (; j < 2 * rb + 2; j++) { // Next 2*(1-d) iterations
unsigned long s;
s = W2[j];
W1[j] = s; // Extending size of W1
W2[j] = s ^ W3[j];
}
W1[j] = W2[j]; // Size(W1) := 2*rb + 3
// Size(W2) = 2*rb + 3
// c = W0 + W1*y + W2*y^2 + W3*y^3 + W4*y^4
// We already have
// W0[j] == c[j] for j = 0 .. 2*k-1 because W0 = c, and
// W2[j] == c[j] for j = 2*k .. 2*k+2*rb+2 because W2 = c + 2*k
ASSERT(3 - 2 * d + 4 * k <= sc);
for (j = 0; j < 3 - 2 * d; j++) // 3 - 2*d words of W2
c[j + 4 * k] ^= W4[j]; // overlap the W4 region
for (; j < sc - 4 * k; j++) // Copy rest of W4
c[j + 4 * k] = W4[j]; // Here c was undefined
ASSERT(2 * rb + 3 + k <= sc);
for (j = 0; j < 2 * rb + 3; j++)
c[j + k] ^= W1[j];
ASSERT(2 * rb + 2 + 3 * k <= sc); // True if n >= 6 so need
// GF2X_MUL_TOOMU_THRESHOLD >= 6
for (j = 0; j < 2 * rb + 2; j++)
c[j + 3 * k] ^= W3[j];
}
/*
* Below this line, experimental code
* (C) 2007 Richard Brent <[email protected]>
* This code is released under the GPL 2.0 license, or any later version.
*
* Based on Marco Bodrato's mul-tc3.c but with full-word aligned
* operations to reduce overheads.
*
* Reference: http://bodrato.it/papers/#WAIFI2007
*
* "Towards Optimal Toom-Cook Multiplication for Univariate and
* Multivariate Polynomials in Characteristic 2 and 0." by Marco
* BODRATO; in C.Carlet and B.Sunar, editors, "WAIFI'07 proceedings",
* LNCS 4547, pp. 119-136. Springer, Madrid, Spain, June 21-22, 2007.
*/
#if (GF2X_MUL_TOOMW_THRESHOLD < 8)
#error "GF2X_MUL_TOOMW_THRESHOLD should be at least 8"
#endif
/*
c must have space for 2n words and should not overlap the inputs.
stk must have space for sp(n) = gf2x_toomspace(n) words
sp(n) = (n lt 8) ? KarMem(7) : 8*(n/3 + 3) + sp(n/3 + 2)
and KarMem(7) = 19 is the space required by KarMul.
A simpler bound on the memory required is 5*n + 17 (equality at n = 19).
*/
#if 0
void gf2x_mul_tc3w(unsigned long * c, const unsigned long * a, const unsigned long * b,
long n, unsigned long * stk)
{
long k = (n + 2) / 3; // size of a0, a1, b0, b1
long r = n - 2 * k; // size of a2, b2
long d = (r < k) ? 1 : 0; // 1 if r < k, 0 otherwise
long kd = k - d;
const unsigned long *a0 = a; // Aliases for three parts of a
const unsigned long *a1 = a + k;
const unsigned long *a2 = a + 2 * k;
const unsigned long *b0 = b; // Ditto for b
const unsigned long *b1 = b + k;
const unsigned long *b2 = b + 2 * k;
long k2 = 2 * (k + 2); // Size of temporary arrays
unsigned long *W0 = c; // Overlap W0 (size 2*k) with c
unsigned long *W1 = stk;
unsigned long *W2 = c + 2 * k; // Overlap W2 with c + 2*k ...
unsigned long *W3 = W1 + k2;
unsigned long *W4 = W3 + k2; // But not W4 as W2 too large
unsigned long *W5 = W4 + k2; // W5 is synonymous with W3 in
// Bodrato's mul-tc3.c
stk += 4 * k2; // 4 temporaries of size k2
long j;
unsigned long s, u2, v2;
// In the comments y = x**w where w = wordlength = NTL_BITS_PER_LONG
// y can be thought of as a w-bit shift operator.
// Bodrato's code corresponds to w = 1, which minimises the size of
// the arguments in the recursive calls, but requires a lot of fiddly
// bit-operations. By choosing w = 32 or 64 we simplify the coding
// and obtain opportunities for loop optimisation. Both methods have
// the same asymptotic complexity O(n**(ln(5)/ln(3))) = O(n**1.464).
// We try to combine loops as far as possible to reduce overheads and memory
// references. This often means splitting a loop into the "usual" case and
// "special" cases at the start or end, due to different size arrays etc.
// In the comments " + " means addition in GF(2) and " ^ " means
// exponentiation.
// Evaluation phase Size is (max) size in words
// W0 = a1*y + a2*y^2 == A(y) - a0 == A(1+y) - A(1)
// W4 = b1*y + b2*y^2 == B(y) - b0 == B(1+y) - B(1)
// W5 = a0 + a1 + a2 == A(1)
// W2 = b0 + b1 + b2 == B(1)
W0[0] = W4[0] = 0;
W0[1] = a1[0];
W4[1] = b1[0]; // No a2, b2 here
W5[0] = a0[0] ^ a1[0] ^ (u2 = a2[0]);
W2[0] = b0[0] ^ b1[0] ^ (v2 = b2[0]);
for (j = 1; j < r; j++) // Next r-1 iterations
{ // This is the usual case
unsigned long u1, v1;
W0[j + 1] = (u1 = a1[j]) ^ u2; // Size(a1) = Size(b1) = k
W4[j + 1] = (v1 = b1[j]) ^ v2;
W5[j] = a0[j] ^ u1 ^ (u2 = a2[j]); // Size(a2) = Size(b2) = r
W2[j] = b0[j] ^ v1 ^ (v2 = b2[j]);
}
for (; j < k; j++) // Last iterations for W5, W2
{
W0[j + 1] = a1[j]; // Omit a2, b2 here
W4[j + 1] = b1[j];
W5[j] = a0[j] ^ a1[j]; // Size(W5) := k
W2[j] = b0[j] ^ b1[j]; // Size(W2) := k;
}
W0[k + 1] = W4[k + 1] = 0; // In case r == k
W0[r + 1] ^= a2[r - 1]; // Size(W0) := kd+2
W4[r + 1] ^= b2[r - 1]; // Size(W4) := kd+2
// Recursive calls mixed with further evaluation
// There are 5 recursive calls with sizes at most k+2.
// Thus it is necessary that n > 4 (but we assume that
// Karatsuba's method or some other method will be used
// for very small n, say n < GF2X_MUL_TOOMW_THRESHOLD).
// W1 = W2 * W5 == C(1)
gf2x_mul_toom(W1, W2, W5, k, stk); // Size(W1) := 2*k
// W5 += W0 == A(1+y) // Size(W5) < Size(W0)