File: toom2.c

package info (click to toggle)
gf2x 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 2,612 kB
  • ctags: 725
  • sloc: sh: 11,112; ansic: 7,549; cpp: 1,369; makefile: 490; perl: 151
file content (1700 lines) | stat: -rw-r--r-- 50,519 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
/* This file is part of the gf2x library.

   Copyright 2007, 2008, 2009
   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 the GNU General Public License as published by the
   Free Software Foundation; either version 2 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 GNU General Public License for
   more details.

   You should have received a copy of the GNU General Public License along
   with this program; see the file COPYING.  If not, write to the Free
   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   02111-1307, 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>

#include "gf2x.h"
#include "gf2x/gf2x-impl.h"

/* We need gf2x_addmul_1_n */
#include "gf2x/gf2x-small.h"

#include "gf2x/gf2x-config.h"

/* 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];
}

/* 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 */
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 * c, return carry out */
static
unsigned long Lsh1(unsigned long *c, long n, unsigned long cy)
{
    unsigned long t;
    long i;
    for (i = 0; i < n; i++) {
	t = (c[i] << 1) | cy;
	cy = c[i] >> (GF2X_WORDSIZE - 1);
	c[i] = t;
    }
    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;

    for (i = 0; i < n; i++) {
	t ^= c[i];
	t ^= t << 2;
	t ^= t << 4;
	t ^= t << 8;
	t ^= t << 16;
#if (GF2X_WORDSIZE == 64)
	t ^= t << 32;
#elif (GF2X_WORDSIZE != 32)
#error "GF2X_WORDSIZE should be 32 or 64"
#endif
	c[i] = t;
	t >>= (GF2X_WORDSIZE - 2);
    }
    return t;
}



/********************************************************************
 * Below this line, experimental code
 * (C) 2007 Marco Bodrato <optimaltoom@bodrato.it>
 * 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 + (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;

    for (i = 0; i < n; i++) {
	t ^= c[i];
	t ^= t << 1;
	t ^= t << 2;
	t ^= t << 4;
	t ^= t << 8;
	t ^= t << 16;
#if (GF2X_WORDSIZE == 64)
	t ^= t << 32;
#elif (GF2X_WORDSIZE != 32)
#error "GF2X_WORDSIZE should be 32 or 64"
#endif
	c[i] = t;
	t >>= (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 */
    cy = AddLsh1(W0, a + k, a + 2 * k, r);	/* a1 + x a2 */
    cy = Add1(W0 + r, a + k + r, k - r, cy);
    W0[k] = (cy << 1) ^ Lsh1(W0, k, 0);	/* x a1 + x^2 a2 */
    cy = AddLsh1(W4 + 2, b + k, b + 2 * k, r);
    cy = Add1(W4 + 2 + r, b + k + r, k - r, cy);
    W4[2 + k] = (cy << 1) ^ Lsh1(W4 + 2, k, 0);	/* 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
       Toom3Mul 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)    */
    Add(c + k + 1, a, a + k, k);
    Add(c + k + 1, c + k + 1, a + 2 * k, r);	/* a0 + a1 + a2 */
    Add(W2 + 2, b, b + k, k);
    Add(W2 + 2, W2 + 2, 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 */
    Rsh1Add(W2, W0, 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. */
    Add(W2, W2, W3, 2 * k);
    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 <trinom@rpbrent.com>
 * 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.

#if (GF2X_MUL_TOOMU_THRESHOLD < 33)
#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 >= 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 <trinom@rpbrent.com>
 * 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)
// W2 += W4 == B(1+y)                           // Size(W2) < Size(W4)
// W0 += a0 == A(y)                             // Size(W0) > Size(a0)
// W4 += b0 == B(y)                             // Size(W4) > Size(b0)

    for (j = 0; j < k; j++)	// First k iterations
    {
	unsigned long u, v;
	W5[j] ^= (u = W0[j]);
	W2[j] ^= (v = W4[j]);
	W0[j] = u ^ a0[j];
	W4[j] = v ^ b0[j];
    }

    for (; j < kd + 2; j++)	// Last 2-d iterations
    {
	W5[j] = W0[j];		// Size(W5) := kd+2
	W2[j] = W4[j];		// Size(W2) := kd+2
    }

// W3 = W2 * W5 == C(1+y)

// Output argument in recursive call must differ from inputs.
// That is why we need both W3 and W5.

// ASSERT ((kd+2) <= (n/3 + 2));                // Explaining the space bound

    gf2x_mul_toom(W3, W2, W5, kd + 2, stk);	// Size(W3) := 2*kd + 4

// W2 = W0 * W4 == C(y)

    gf2x_mul_toom(W2, W0, W4, kd + 2, stk);	// Size(W2) := 2*kd + 4

// W0 = a0 * b0 == c0 == C(0/1) == C(0)

    gf2x_mul_toom(W0, a0, b0, k, stk);	// Size(W0) := 2*k
    // so c[0..(2k-1)] defined
// W4 = a2 * b2 == c4 == C(1/0) == C(infinity)

    gf2x_mul_toom(W4, a2, b2, r, stk);	// Size(W4) := 2*r

// 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
	s = W2[j];
	W3[j] ^= s;		// Size(W0) = 2*k
	W2[j] = s ^ W0[j];	// other sizes 2*kd + 4
    }

    for (; j < 2 * kd + 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 * kd + 3; j++)
	W2[j] = W2[j + 1] ^ W3[j];
    W2[j] = W3[j];		// Size(W2) := 2*kd + 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 * r; j++) {
	s ^= W2[j] ^ W4[j] ^ W4[j - 3];	// next 2r-3 are usual case
	W2[j] = s;
    }

    for (; j < 2 * r + 3; j++) {
	s ^= W2[j] ^ W4[j - 3];	// next 3 are special
	W2[j] = s;
    }

    for (; j < 2 * kd + 4; j++) {
	s ^= W2[j];		// last (k-r-d) == 0 or 1
	W2[j] = s;		// Size(W2) = 2*kd + 3
    }

// ASSERT (s == 0);                             // Division should be exact

// W1 += W0 == c1 + c2 + c3 + c4
// W3 += W1 == c3*y*(1+y)

    for (j = 0; j < 2 * k; j++) {
	s = W0[j] ^ W1[j];
	W1[j] = s;		// Size(W0) = Size(W1) = 2*k
	W3[j] ^= s;		// Size(W3) = 2*kd + 4 > 2*k
    }

// ASSERT (W3[0] == 0);                         // Next division exact

// W3 = W3/(y + y^2) == c3

    for (j = 0, s = 0; j < 2 * kd + 3; j++) {
	s ^= W3[j + 1];
	W3[j] = s;
    }
    W3[j] = 0;
// ASSERT (s == 0);                             // Division exact
    // Size(W3) := 2*kd + 2

// W1 += W2 + W4 == c1                          // Size(W4) == 2*r
// W2 += W3      == c2                          //  <= Size(W1) == 2*k
    //  <= Size(W3) == 2*kd + 2
    //  <  Size(W2) == 2*kd + 4
    for (j = 0; j < 2 * r; j++) {	// Usual case
	s = W2[j];
	W1[j] ^= s ^ W4[j];
	W2[j] = s ^ W3[j];
    }
    for (; j < 2 * k; j++) {	// Next 2*(k-r) iterations
	s = W2[j];
	W1[j] ^= s;		// No W4[j] here
	W2[j] = s ^ W3[j];
    }
    for (; j < 2 * kd + 2; j++) {	// Next 2*(1-d) iterations
	s = W2[j];
	W1[j] = s;		// Extending size of W1
	W2[j] = s ^ W3[j];
    }
    for (; j < 2 * kd + 4; j++)	// Last 2 iterations
	W1[j] = W2[j];		// Size(W1) := 2*kd + 4
    // Size(W2)  = 2*kd + 4

// 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*kd+3 because W2 = c + 2*k

    for (j = 0; j < 4 - 2 * d; j++)	// 4 - 2*d words of W2
	c[j + 4 * k] ^= W4[j];	// overlap the W4 region

    for (; j < 2 * r; j++)	// Copy rest of W4
	c[j + 4 * k] = W4[j];	// Here c was undefined

    for (j = 0; j < 2 * kd + 4; j++)
	c[j + k] ^= W1[j];

// ASSERT (2*kd + 2 + 3*k <= 2*n);              // True if n >= 8 so need
    // GF2X_MUL_TOOMW_THRESHOLD >= 8
    for (j = 0; j < 2 * kd + 2; j++)
	c[j + 3 * k] ^= W3[j];

}
#else
/*
 * Below this line, experimental code
 * (C) 2007 Richard Brent <trinom@rpbrent.com>
 * 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).
*/


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)
// W2 += W4 == B(1+y)				// Size(W2) < Size(W4)
// W0 += a0 == A(y)				// Size(W0) > Size(a0)
// W4 += b0 == B(y)				// Size(W4) > Size(b0)

  for (j = 0; j < k; j++)			// First k iterations
    {
    unsigned long u, v;
    W5[j] ^= (u = W0[j]);
    W2[j] ^= (v = W4[j]);
    W0[j]  = u ^ a0[j];
    W4[j]  = v ^ b0[j];
    }

  for (; j < kd+2; j++)				// Last 2-d iterations
    {
    W5[j] = W0[j];				// Size(W5) := kd+2
    W2[j] = W4[j];				// Size(W2) := kd+2
    }

// W3 = W2 * W5 == C(1+y)

// Output argument in recursive call must differ from inputs.
// That is why we need both W3 and W5.

// ASSERT ((kd+2) <= (n/3 + 2));		// Explaining the space bound

  gf2x_mul_toom (W3, W2, W5, kd+2, stk);			// Size(W3) := 2*kd + 4

// W2 = W0 * W4 == C(y)

  gf2x_mul_toom (W2, W0, W4, kd+2, stk);			// Size(W2) := 2*kd + 4

// W0 = a0 * b0 == c0 == C(0/1) == C(0)

  gf2x_mul_toom (W0, a0, b0, k, stk);			// Size(W0) := 2*k
						// so c[0..(2k-1)] defined
// W4 = a2 * b2 == c4 == C(1/0) == C(infinity)

  gf2x_mul_toom (W4, a2, b2, r, stk);			// Size(W4) := 2*r

// 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
    s = W2[j];
    W3[j] ^= s;					// Size(W0) = 2*k
    W2[j]  = s ^ W0[j];				// other sizes 2*kd + 4
    }

  for (; j < 2*kd+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*kd + 3; j++)
    W2[j] = W2[j+1] ^ W3[j];
  W2[j] = W3[j];				// Size(W2) := 2*kd + 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*r; j++)
    {
    s ^= W2[j] ^ W4[j] ^ W4[j-3];		// next 2r-3 are usual case
    W2[j] = s;
    }

  for (; j < 2*r+3; j++)
    {
    s ^= W2[j] ^ W4[j-3];			// next 3 are special
    W2[j] = s;
    }

  for (; j < 2*kd+4; j++)
    {
    s ^= W2[j]; 				// last (k-r-d) == 0 or 1
    W2[j] = s;					// Size(W2) = 2*kd + 3
    }

// ASSERT (s == 0);				// Division should be exact

// W1 += W0 == c1 + c2 + c3 + c4
// W3 += W1 == c3*y*(1+y)

  for (long j = 0; j < 2*k; j++)
    {
    s = W0[j] ^ W1[j];
    W1[j] = s;					// Size(W0) = Size(W1) = 2*k
    W3[j] ^= s;					// Size(W3) = 2*kd + 4 > 2*k
    }

// ASSERT (W3[0] == 0);				// Next division exact

// W3 = W3/(y + y^2) == c3

  for (j = 0, s = 0; j < 2*kd + 3; j++)
    {
    s ^= W3[j+1];
    W3[j] = s;
    }
  W3[j] = 0;
// ASSERT (s == 0);				// Division exact
			  			// Size(W3) := 2*kd + 2

// W1 += W2 + W4 == c1				// Size(W4) == 2*r
// W2 += W3      == c2				//  <= Size(W1) == 2*k
						//  <= Size(W3) == 2*kd + 2
						//  <  Size(W2) == 2*kd + 4
  for (j = 0; j < 2*r; j++)
    {						// Usual case
    s = W2[j];
    W1[j] ^= s ^ W4[j];
    W2[j]  = s ^ W3[j];
    }
  for (; j < 2*k; j++)
    {						// Next 2*(k-r) iterations
    s = W2[j];
    W1[j] ^= s;					// No W4[j] here
    W2[j]  = s ^ W3[j];
    }
  for (; j < 2*kd + 2; j++)
    {						// Next 2*(1-d) iterations
    s = W2[j];
    W1[j] = s;					// Extending size of W1
    W2[j] = s ^ W3[j];
    }
  for (; j < 2*kd + 4; j++)			// Last 2 iterations
    W1[j] = W2[j];				// Size(W1) := 2*kd + 4
    						// Size(W2)  = 2*kd + 4

// 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*kd+3 because W2 = c + 2*k

  for (j = 0; j < 4 - 2*d; j++)			// 4 - 2*d words of W2
    c[j+4*k] ^= W4[j];    			// overlap the W4 region

  for (; j < 2*r; j++)				// Copy rest of W4
    c[j+4*k] = W4[j];    			// Here c was undefined

  for (long j = 0; j < 2*kd + 4; j++)
    c[j+k]   ^= W1[j];

// ASSERT (2*kd + 2 + 3*k <= 2*n);		// True if n >= 8 so need
						// GF2X_MUL_TOOMW_THRESHOLD >= 8
  for (long j = 0; j < 2*kd + 2; j++)
    c[j+3*k] ^= W3[j];

}
#endif


/********************************************************************
 * Below this line, experimental code
 * (C) 2007 Marco Bodrato <optimaltoom@bodrato.it>
 * Modified by Paul Zimmermann, April 2007.
 * This code is released under the GPL 2.0 license, or any later version.
 *
 * 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_TOOM4_THRESHOLD < 30)
#error "GF2X_MUL_TOOM4_THRESHOLD should be at least 30"
#endif

/* c <- x * a + x^2 * b, return carry out */
static
unsigned long AddLsh1Lsh2(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] << 1) ^ ((b[i] << 2) | cy);
	cy = (a[i] >> (GF2X_WORDSIZE - 1)) ^ (b[i] >> (GF2X_WORDSIZE - 2));
	c[i] = t;
    }
    return cy;
}

/* c <- a + x^2 * b, return carry out */
static
unsigned long AddLsh2(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] << 2) | cy);
	cy = b[i] >> (GF2X_WORDSIZE - 2);
	c[i] = t;
    }
    return cy;
}

/* c <- a + x^6 * b, return carry out */
static
unsigned long AddLsh6(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] << 6) | cy);
	cy = b[i] >> (GF2X_WORDSIZE - 6);
	c[i] = t;
    }
    return cy;
}

/* let c = q*(x+x^4) + X^n*r with X = x^GF2X_WORDSIZE and deg(r) < 1
   then c <- q, returns r.
*/
static unsigned long DivExactD1(unsigned long * c, long n)
{
    unsigned long t = 0;
    long i;

    for (i = 0; i < n; i++) {
	t ^= (c[i] >> 1) | ((i + 1 < n)
			    ? (c[i + 1] << (GF2X_WORDSIZE - 1)) : 0);
	t ^= t << 3 ^ t << 6;
	t ^= t << 9 ^ t << 18;
	t ^= t << 27
#if (GF2X_WORDSIZE == 64)
	    ^ t << 54
#elif (GF2X_WORDSIZE != 32)
#error "GF2X_WORDSIZE should be 32 or 64"
#endif
	    ;
	c[i] = t;
	t >>= (GF2X_WORDSIZE - 3);
    }
    return t;
}

/* let c = q*(x^2+x^4) + X^n*r with X = x^GF2X_WORDSIZE and deg(r) < 1
   then c <- q, returns r.
*/
static unsigned long DivExactD2(unsigned long * c, long n)
{
    /* c <- c/x^2 */
    unsigned long cy = 0, t;

    long i;

    for (i = n - 1; i >= 0; i--) {
	t = (c[i] >> 2) | (cy << (GF2X_WORDSIZE - 2));
	cy = c[i];		/* no need to mask the low 2 bits, since they will disappear
				   with the next cy << (GF2X_WORDSIZE - 2) */
	c[i] = t;
    }
    return DivOnePlusX2(c, n);
}

#if 0
/* Same as DivExactD2, but with one pass only. However, does not seem to give
   a significant speedup, thus disabled for now. */
static unsigned long DivExactD2a(unsigned long * c, long n)
{
    unsigned long t, ci;

    t = c[0];
    t ^= t << 2;
    t ^= t << 4;
    t ^= t << 8;
    t ^= t << 16;
#if (GF2X_WORDSIZE == 64)
    t ^= t << 32;
#elif (GF2X_WORDSIZE != 32)
#error "GF2X_WORDSIZE should be 32 or 64"
#endif
    ci = t;
    t >>= (GF2X_WORDSIZE - 2);
    for (long i = 1; i < n; i++) {
	t ^= c[i];
	t ^= t << 2;
	t ^= t << 4;
	t ^= t << 8;
	t ^= t << 16;
#if (GF2X_WORDSIZE == 64)
	t ^= t << 32;
#endif
	/* now t is the result of the division of c[i] by (1+x^2), and
	   t >> (GF2X_WORDSIZE - 2) the corresponding carry */
	c[i - 1] = (ci >> 2) | (t << (GF2X_WORDSIZE - 2));
	ci = t;
	t >>= (GF2X_WORDSIZE - 2);
    }
    c[n - 1] = ci >> 2;
    return t;
}
#endif

/*
\\ gp-pari check code.
\\ (C) 2007 Marco Bodrato <optimaltoom@bodrato.it>
\\ This code is released under the GPL 2.0 license, or any later version.

U0=u0*Mod(1,2);U1=u1*Mod(1,2);U2=u2*Mod(1,2);U3=u3*Mod(1,2);
V0=v0*Mod(1,2);V1=v1*Mod(1,2);V2=v2*Mod(1,2);V3=v3*Mod(1,2);

U = U3*Y^3 + U2*Y^2*X + U1*Y*X^2 + U0*X^3
V = V3*Y^3 + V2*Y^2*X + V1*Y*X^2 + V0*X^3

\\ P(X,Y): P0=(1,0); P1=(x+1,1); P2=(x,1); P3=(1,1); P4=(1,x); P5=(1,x+1); P6=(0,1)
\\Evaluation phase: 13*2 add, 7*2 shift, 2Smul; 7 mul (n)

W1 = U0 + U1 + U2 + U3    ; W2 = V0 + V1 + V2 + V3
W0 = U1 +(U2 + U3*x)*x    ; W6 = V1 +(V2 + V3*x)*x
W4 = W1 +(W0 + U3*(x+1))*x; W3 = W2 +(W6 + V3*(x+1))*x
W0 = W0*x + U0            ; W6 = W6*x + V0

W5 = W4 * W3              ; W4 = W0 * W6
W3 = W1 * W2

W0 =(U2 +(U1 + U0*x)*x)*x ; W6 =(V2 +(V1 + V0*x)*x)*x
W1 = W1 + W0 + U0*(x^2+x) ; W2 = W2 + W6 + V0*(x^2+x)
W0 = W0 + U3              ; W6 = W6 + V3

W1 = W1 * W2              ; W2 = W0 * W6
W6 = U3 * V3              ; W0 = U0 * V0

\\Interpolation: 22 add, 4 shift, 5 Smul, 4 div (2n)
d1=(x^4+x)*Mod(1,2)	; d1== (x)^1*(x+1)^1*(x^2+x+1)^1 *Mod(1,2)
d2=(x^4+x^2)*Mod(1,2)	; d2== (x)^2*(x+1)^2*(x^2+x+1)^0 *Mod(1,2)

W1 = W1 + W2 + W0*(x^4+x^2+1)
W5 =(W5 + W4 + W1 + W6*(x^4+x^2+1))/d1
W2 = W2 + W6 + W0*(x^6)
W4 = W4 + W2 + W0 + W6*(x^6)
W4 =(W4 + W5*(x^5+x))/d2
W3 = W3 + W0 + W6
W1 = W1 + W3
W2 = W2 +(W1 + W3*x)*x
W3 = W3 + W4 + W5
W1 =(W1 + W3*(x^2+x))/d1
W5 = W5 + W1
W2 =(W2 + W5*(x^2+x))/d2
W4 = W4 + W2

\\Recomposition
W  = W6*Y^6 + W5*Y^5*X + W4*Y^4*X^2+ W3*Y^3*X^3+ W2*Y^2*X^4+ W1*Y*X^5 + W0*X^6
W == U*V

Memory Usage: stk must have space for sp(n), where
sp(n) = 6k+2 + sp(k+1) with k = ceil(n/4).
*/

void gf2x_mul_tc4(unsigned long * c, const unsigned long * a, const unsigned long * b,
	      long n, unsigned long * stk)
{
    long k = (n + 3) / 4;	/* ceil(n/4) */
    long r = n - 3 * k;
    unsigned long cy, cy1, cy2, cy3, cy4;
    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;
    unsigned long *W5 = stk + 4 * k;
    unsigned long *W6 = c + 6 * k;
    unsigned long *newstk = stk + 6 * k + 2;

/* \\Evaluation phase: 13*2 add, 7*2 shift, 2Smul; 7 mul (n) */

/* W1 = U0 + U1 + U2 + U3    ; W2 = V0 + V1 + V2 + V3 */
    Add(W1, a, a + 3 * k, r);
    Add1(W1 + r, a + r, k - r, 0);
    Add3(W1, a + k, a + 2 * k, k);	/* U0 + U1 + U2 + U3 */
    Add(W2 + 2, b, b + 3 * k, r);
    Add1(W2 + r + 2, b + r, k - r, 0);
    Add3(W2 + 2, b + k, b + 2 * k, k);	/* V0 + V1 + V2 + V3 */
/*   Add (W1,     a, a + k,     k); */
/*   Add (W1,    W1, a + 2 * k, k); */
/*   Add (W1,    W1, a + 3 * k, r); /\* U0 + U1 + U2 + U3 *\/ */
/*   Add (W2+2,   b, b + k,     k); */
/*   Add (W2+2,W2+2, b + 2 * k, k); */
/*   Add (W2+2,W2+2, b + 3 * k, r); /\* V0 + V1 + V2 + V3 *\/ */

/* W0 = U1 +(U2 + U3*x)*x    ; W6 = V1 +(V2 + V3*x)*x */
    cy = AddLsh1(W0, a + 2 * k, a + 3 * k, r);	/* U2 + x U3 */
    cy = Add1(W0 + r, a + 2 * k + r, k - r, cy);
    W0[k] = (cy << 1) ^ AddLsh1(W0, a + k, W0, k);	/* U1+x U2 + x^2 U3 */
    cy = AddLsh1(W6 + 2, b + 2 * k, b + 3 * k, r);	/* V2 + x V3 */
    cy = Add1(W6 + 2 + r, b + 2 * k + r, k - r, cy);
    W6[k + 2] = (cy << 1) ^ AddLsh1(W6 + 2, b + k, W6 + 2, k);	/* V1+x V2 + x^2 V3 */
    /* since we use W6[k+2], and we have space for 2r words in W6, we need
       k+3 <= 2*r, which requires n>=30. */
/* W4 = W1 +(W0 + U3*(x+1))*x; W3 = W2 +(W6 + V3*(x+1))*x */
    cy = AddLsh1(W4 + 2, W0, a + 3 * k, r);	/* W0 + x U3 */
    cy = Add1(W4 + 2 + r, W0 + r, k + 1 - r, cy);	/* cy == 0 */
    ASSERT(cy == 0);
    Add(W4 + 2, W4 + 2, a + 3 * k, r);	/* W0 + x U3 + U3 */
    W4[k + 2] = (W4[k + 2] << 1) ^ AddLsh1(W4 + 2, W1, W4 + 2, k);	/* W1+x(W0 +(x+1) U3) */
    cy = AddLsh1(W3 + 2, W6 + 2, b + 3 * k, r);	/* W6 + x V3 */
    cy = Add1(W3 + 2 + r, W6 + 2 + r, k + 1 - r, cy);	/* cy == 0 */
    Add(W3 + 2, W3 + 2, b + 3 * k, r);	/* W6 + x V3 + V3 */
    W3[k + 2] = (W3[k + 2] << 1) ^ AddLsh1(W3 + 2, W2 + 2, W3 + 2, k);	/* W2+x(W6 + (x+1) V3) */
/* W0 = W0*x + U0            ; W6 = W6*x + V0 */
    W0[k] = (W0[k] << 1) ^ AddLsh1(W0, a, W0, k);	/* U0+x W0 */
    W6[k + 2] = (W6[k + 2] << 1) ^ AddLsh1(W6 + 2, b, W6 + 2, k);	/* V0+x W6 */
/* W5 = W4 * W3              ; W4 = W0 * W6 */
    gf2x_mul_toom(W5, W4 + 2, W3 + 2, k + 1, newstk);	/* W5 : 2*k+1 */

    gf2x_mul_toom(W4, W0, W6 + 2, k + 1, newstk);	/* W4 : 2*k+1 */
    cy4 = W6[0];		/* Take care of overlapping byte. */
/* W3 = W1 * W2 */
    gf2x_mul_toom(W3, W1, W2 + 2, k, newstk);	/* W3 : 2*k */

/* W0 =(U2 +(U1 + U0*x)*x)*x ; W6 =(V2 +(V1 + V0*x)*x)*x */
    cy = AddLsh1(W0, a + 1 * k, a + 0 * k, k);	/* U1 + x U0 */
    W0[k] = (cy << 2) ^ AddLsh1Lsh2(W0, a + 2 * k, W0, k);	/* U2+x U1 + x^2 U0 */
    cy = AddLsh1(W6 + 2, b + 1 * k, b + 0 * k, k);	/* V1 + x V0 */
    W6[k + 2] = (cy << 2) ^ AddLsh1Lsh2(W6 + 2, b + 2 * k, W6 + 2, k);	/* V2+x V1 + x^2 V0 */
/* W1 = W1 + W0 + U0*(x^2+x) ; W2 = W2 + W6 + V0*(x^2+x) */
    W1[k] = gf2x_addmul_1_n(W1, W1, a, k, 4 + 2);
    Add(W0 + k + 1, W1, W0, k + 1);
    W2[k + 2] = gf2x_addmul_1_n(W2 + 2, W2 + 2, b, k, 4 + 2);
    Add(W2 + 2, W2 + 2, W6 + 2, k + 1);
/* W0 = W0 + U3              ; W6 = W6 + V3 */
    Add(W0, W0, a + 3 * k, r);	/* + U3 */
    Add(W6 + 2, W6 + 2, b + 3 * k, r);	/* + V3 */
/* W1 = W1 * W2              ; W2 = W0 * W6 */
    cy = W3[0];
    cy2 = W3[1];		/* Take care of overlapping byte. */
    gf2x_mul_toom(W1, W0 + k + 1, W2 + 2, k + 1, newstk);	/* W1 : 2*k+1 */
    cy1 = W3[0];
    W3[0] = cy;
    W3[1] = cy2;
    cy = W4[0];
    cy2 = W4[1];		/* Take care of overlapping byte. */
    gf2x_mul_toom(W2, W0, W6 + 2, k + 1, newstk);	/* W2 : 2*k+1 */
    W4[1] = cy2;
    cy2 = W4[0];
    W4[0] = cy;
/* W6 = U3 * V3              ; W0 = U0 * V0 */
    gf2x_mul_toom(W0, a, b, k, newstk);	/* W0 : 2*k */
    gf2x_mul_toom(W6, a + 3 * k, b + 3 * k, r, newstk);	/* W6 : 2*r */
/* \\Interpolation: 22 add, 4 shift, 5 Smul, 4 div (2n) */
/* d1=(x^4+x)*Mod(1,2)	; d1== (x)^1*(x+1)^1*(x^2+x+1)^1 *Mod(1,2) */
/* d2=(x^4+x^2)*Mod(1,2)	; d2== (x)^2*(x+1)^2*(x^2+x+1)^0 *Mod(1,2) */

/* W1 = W1 + W2 + W0*(x^4+x^2+1) */

    Add(W1, W1, W2, 2 * k);
    cy1 ^= cy2 ^ gf2x_addmul_1_n(W1, W1, W0, 2 * k, 16 + 4 + 1);

/* W5 =(W5 + W4 + W1 + W6*(x^4+x^2+1))/d1 */
    Add3(W5, W4, W1, 2 * k);
    W5[2 * k] ^= cy1 ^ cy4;
    W5[2 * r] ^= gf2x_addmul_1_n(W5, W5, W6, 2 * r, 16 + 4 + 1);
    DivExactD1(W5, 2 * k + 1);

/* W2 = W2 + W6 + W0*(x^6) */
    Add(W2, W2, W6, 2 * r);
    cy2 ^= AddLsh6(W2, W2, W0, 2 * k);
/* W4 = W4 + W2 + W0 + W6*(x^6) */
    Add3(W4, W2, W0, 2 * k);
    cy3 = AddLsh6(W4, W4, W6, 2 * r);
    cy = W6[0];			/* save W6[0]=W4[2k]: we cannot do it before the AddLsh6 call
				   because W6 is used as input */
    W6[0] = cy4 ^ cy2;
    W4[2 * r] ^= cy3;		/* must come after W6[0] = cy4 in case r=k */
/* W4 =(W4 + W5*(x^5+x))/d2 */
    gf2x_addmul_1_n(W4, W4, W5, 2 * k + 1, 32 + 2);
    DivExactD2(W4, 2 * k + 1);
    W6[0] = cy;
/* W3 = W3 + W0 + W6 */
    Add3(W3, W0, W6, 2 * r);
    if (r != k)
	Add(W3 + 2 * r, W3 + 2 * r, W0 + 2 * r, 2 * (k - r));	/* warning: 2r instead of r */
/* W1 = W1 + W3 */
    Add(W1, W1, W3, 2 * k);
/* W2 = W2 +(W1 + W3*x)*x */
    cy2 ^=
	AddLsh1(W2, W2, W1, 2 * k) ^ AddLsh2(W2, W2, W3, 2 * k) ^ (cy1 << 1);
/* W3 = W3 + W4 + W5 */
    Add3(W3, W4, W5, 2 * k);
/* W1 =(W1 + W3*(x^2+x))/d1 */
    cy = W3[0];
    cy1 ^= gf2x_addmul_1_n(W1, W1, W3, 2 * k, 4 + 2);
    W3[0] = cy1;
    DivExactD1(W1, 2 * k + 1);
    W3[0] = cy;
/* W5 = W5 + W1 */
    Add(W5, W5, W1, 2 * k);
/* W2 =(W2 + W5*(x^2+x))/d2 */
    cy = W4[0];
    W4[0] = cy2 ^ gf2x_addmul_1_n(W2, W2, W5, 2 * k, 4 + 2);
    DivExactD2(W2, 2 * k + 1);
    W4[0] = cy;
/* W4 = W4 + W2 */
    Add(W4, W4, W2, 2 * k);

/* \\Recomposition */
/* W = W6*Y^6 + W5*Y^5 + W4*Y^4+ W3*Y^3+ W2*Y^2+ W1*Y + W0 */
    Add(c + k, c + k, W1, 6 * k);

}