This file is indexed.

/usr/include/NTL/ZZ_pX.h is in libntl-dev 9.9.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#ifndef NTL_ZZ_pX__H
#define NTL_ZZ_pX__H

#include <NTL/vector.h>
#include <NTL/ZZ_p.h>
#include <NTL/vec_ZZ.h>
#include <NTL/vec_ZZ_p.h>
#include <NTL/FFT.h>
#include <NTL/Lazy.h>
#include <NTL/SmartPtr.h>

NTL_OPEN_NNS




// some cross-over points
// macros are used so as to be consistent with zz_pX 

#define NTL_ZZ_pX_FFT_CROSSOVER (20)  
#define NTL_ZZ_pX_NEWTON_CROSSOVER (45)
#define NTL_ZZ_pX_DIV_CROSSOVER (90)
#define NTL_ZZ_pX_HalfGCD_CROSSOVER (25)
#define NTL_ZZ_pX_GCD_CROSSOVER (180)
#define NTL_ZZ_pX_BERMASS_CROSSOVER (90)
#define NTL_ZZ_pX_TRACE_CROSSOVER (90)



/************************************************************

                         ZZ_pX

The class ZZ_pX implements polynomial arithmetic modulo p.
Polynomials are represented as vec_ZZ_p's.
If f is a ZZ_pX, then f.rep is a vec_ZZ_p.
The zero polynomial is represented as a zero length vector.
Otherwise. f.rep[0] is the constant-term, and f.rep[f.rep.length()-1]
is the leading coefficient, which is always non-zero.
The member f.rep is public, so the vector representation is fully
accessible.
Use the member function normalize() to strip leading zeros.

**************************************************************/

class ZZ_pE; // forward declaration
class ZZ_pXModulus;
class FFTRep;
class ZZ_pXMultiplier;

class ZZ_pX {
public:
typedef ZZ_p coeff_type;
typedef ZZ_pE residue_type;
typedef ZZ_pXModulus modulus_type;
typedef ZZ_pXMultiplier multiplier_type;
typedef FFTRep fft_type;

typedef vec_ZZ_p VectorBaseType; 


vec_ZZ_p rep;


/***************************************************************

          Constructors, Destructors, and Assignment

****************************************************************/


ZZ_pX() { }
//  initial value 0


explicit ZZ_pX(long a) { *this = a; }
explicit ZZ_pX(const ZZ_p& a) { *this = a; }


ZZ_pX(INIT_SIZE_TYPE, long n) { rep.SetMaxLength(n); }

ZZ_pX(const ZZ_pX& a) : rep(a.rep) { }
// initial value is a


ZZ_pX& operator=(const ZZ_pX& a) 
   { rep = a.rep; return *this; }

~ZZ_pX() { }

void normalize();
// strip leading zeros

void SetMaxLength(long n) 
// pre-allocate space for n coefficients.
// Value is unchanged

   { rep.SetMaxLength(n); }


void kill() 
// free space held by this polynomial.  Value becomes 0.

   { rep.kill(); }


void SetLength(long n) { rep.SetLength(n); }
ZZ_p& operator[](long i) { return rep[i]; }
const ZZ_p& operator[](long i) const { return rep[i]; }


static const ZZ_pX& zero();


ZZ_pX(ZZ_pX& x, INIT_TRANS_TYPE) : rep(x.rep, INIT_TRANS) { }

inline ZZ_pX(long i, const ZZ_p& c);
inline ZZ_pX(long i, long c);

inline ZZ_pX(INIT_MONO_TYPE, long i, const ZZ_p& c);
inline ZZ_pX(INIT_MONO_TYPE, long i, long c);
inline ZZ_pX(INIT_MONO_TYPE, long i);

ZZ_pX& operator=(long a);
ZZ_pX& operator=(const ZZ_p& a);


void swap(ZZ_pX& x)
{
   rep.swap(x.rep);
}


};




/********************************************************************

                           input and output

I/O format:

   [a_0 a_1 ... a_n],

represents the polynomial a_0 + a_1*X + ... + a_n*X^n.

On output, all coefficients will be integers between 0 and p-1,
amd a_n not zero (the zero polynomial is [ ]).
On input, the coefficients are arbitrary integers which are
then reduced modulo p, and leading zeros stripped.

*********************************************************************/


NTL_SNS istream& operator>>(NTL_SNS istream& s, ZZ_pX& x);
NTL_SNS ostream& operator<<(NTL_SNS ostream& s, const ZZ_pX& a);




/**********************************************************

                   Some utility routines

***********************************************************/


inline long deg(const ZZ_pX& a) { return a.rep.length() - 1; }
// degree of a polynomial.
// note that the zero polynomial has degree -1.

const ZZ_p& coeff(const ZZ_pX& a, long i);
// zero if i not in range

void GetCoeff(ZZ_p& x, const ZZ_pX& a, long i);
// x = a[i], or zero if i not in range

const ZZ_p& LeadCoeff(const ZZ_pX& a);
// zero if a == 0

const ZZ_p& ConstTerm(const ZZ_pX& a);
// zero if a == 0

void SetCoeff(ZZ_pX& x, long i, const ZZ_p& a);
// x[i] = a, error is raised if i < 0

void SetCoeff(ZZ_pX& x, long i, long a);

void SetCoeff(ZZ_pX& x, long i);
// x[i] = 1, error is raised if i < 0

inline ZZ_pX::ZZ_pX(long i, const ZZ_p& a) { SetCoeff(*this, i, a); } 
inline ZZ_pX::ZZ_pX(long i, long a) { SetCoeff(*this, i, a); } 

inline ZZ_pX::ZZ_pX(INIT_MONO_TYPE, long i, const ZZ_p& a) { SetCoeff(*this, i, a); } 
inline ZZ_pX::ZZ_pX(INIT_MONO_TYPE, long i, long a) { SetCoeff(*this, i, a); } 
inline ZZ_pX::ZZ_pX(INIT_MONO_TYPE, long i) { SetCoeff(*this, i); } 

void SetX(ZZ_pX& x);
// x is set to the monomial X

long IsX(const ZZ_pX& a);
// test if a = X

inline void clear(ZZ_pX& x) 
// x = 0

   { x.rep.SetLength(0); }

inline void set(ZZ_pX& x)
// x = 1

   { x.rep.SetLength(1); set(x.rep[0]); }

inline void swap(ZZ_pX& x, ZZ_pX& y)
// swap x & y (only pointers are swapped)

   { x.swap(y); }

void random(ZZ_pX& x, long n);
inline ZZ_pX random_ZZ_pX(long n)
   { ZZ_pX x; random(x, n); NTL_OPT_RETURN(ZZ_pX, x); }
// generate a random polynomial of degree < n 

void trunc(ZZ_pX& x, const ZZ_pX& a, long m);
// x = a % X^m

inline ZZ_pX trunc(const ZZ_pX& a, long m)
   { ZZ_pX x; trunc(x, a, m); NTL_OPT_RETURN(ZZ_pX, x); }

void RightShift(ZZ_pX& x, const ZZ_pX& a, long n);
// x = a/X^n

inline ZZ_pX RightShift(const ZZ_pX& a, long n)
   { ZZ_pX x; RightShift(x, a, n); NTL_OPT_RETURN(ZZ_pX, x); }

void LeftShift(ZZ_pX& x, const ZZ_pX& a, long n);
// x = a*X^n

inline ZZ_pX LeftShift(const ZZ_pX& a, long n)
   { ZZ_pX x; LeftShift(x, a, n); NTL_OPT_RETURN(ZZ_pX, x); }

#ifndef NTL_TRANSITION

inline ZZ_pX operator>>(const ZZ_pX& a, long n)
   { ZZ_pX x; RightShift(x, a, n); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator<<(const ZZ_pX& a, long n)
   { ZZ_pX x; LeftShift(x, a, n); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX& operator<<=(ZZ_pX& x, long n)
   { LeftShift(x, x, n); return x; }

inline ZZ_pX& operator>>=(ZZ_pX& x, long n)
   { RightShift(x, x, n); return x; }

#endif



void diff(ZZ_pX& x, const ZZ_pX& a);
// x = derivative of a

inline ZZ_pX diff(const ZZ_pX& a)
   { ZZ_pX x; diff(x, a); NTL_OPT_RETURN(ZZ_pX, x); }


void MakeMonic(ZZ_pX& x);

void reverse(ZZ_pX& c, const ZZ_pX& a, long hi);

inline ZZ_pX reverse(const ZZ_pX& a, long hi)
   { ZZ_pX x; reverse(x, a, hi); NTL_OPT_RETURN(ZZ_pX, x); }

inline void reverse(ZZ_pX& c, const ZZ_pX& a)
{  reverse(c, a, deg(a)); }

inline ZZ_pX reverse(const ZZ_pX& a)
   { ZZ_pX x; reverse(x, a); NTL_OPT_RETURN(ZZ_pX, x); }

inline void VectorCopy(vec_ZZ_p& x, const ZZ_pX& a, long n)
   { VectorCopy(x, a.rep, n); }

inline vec_ZZ_p VectorCopy(const ZZ_pX& a, long n)
   { return VectorCopy(a.rep, n); }




/*******************************************************************

                        conversion routines

********************************************************************/



void conv(ZZ_pX& x, long a);
void conv(ZZ_pX& x, const ZZ& a);
void conv(ZZ_pX& x, const ZZ_p& a);
void conv(ZZ_pX& x, const vec_ZZ_p& a);

inline ZZ_pX to_ZZ_pX(long a)
   { ZZ_pX x; conv(x, a); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX to_ZZ_pX(const ZZ& a)
   { ZZ_pX x; conv(x, a); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX to_ZZ_pX(const ZZ_p& a)
   { ZZ_pX x; conv(x, a); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX to_ZZ_pX(const vec_ZZ_p& a)
   { ZZ_pX x; conv(x, a); NTL_OPT_RETURN(ZZ_pX, x); }





/* additional legacy conversions for v6 conversion regime */

inline void conv(ZZ_pX& x, const ZZ_pX& a)
   { x = a; }

inline void conv(vec_ZZ_p& x, const ZZ_pX& a)
   { x = a.rep; }


/* ------------------------------------- */



/*************************************************************

                        Comparison

**************************************************************/

long IsZero(const ZZ_pX& a); 

long IsOne(const ZZ_pX& a);

inline long operator==(const ZZ_pX& a, const ZZ_pX& b)
{
   return a.rep == b.rep;
}

inline long operator!=(const ZZ_pX& a, const ZZ_pX& b)
{
   return !(a == b);
}

long operator==(const ZZ_pX& a, long b);
long operator==(const ZZ_pX& a, const ZZ_p& b);

inline long operator==(long a, const ZZ_pX& b) { return b == a; }
inline long operator==(const ZZ_p& a, const ZZ_pX& b) { return b == a; }

inline long operator!=(const ZZ_pX& a, long b) { return !(a == b); }
inline long operator!=(const ZZ_pX& a, const ZZ_p& b) { return !(a == b); }

inline long operator!=(long a, const ZZ_pX& b) { return !(a == b); }
inline long operator!=(const ZZ_p& a, const ZZ_pX& b) { return !(a == b); }


/***************************************************************

                         Addition

****************************************************************/

void add(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);
// x = a + b

void sub(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);
// x = a - b

void negate(ZZ_pX& x, const ZZ_pX& a);
// x = -a

// scalar versions

void add(ZZ_pX& x, const ZZ_pX& a, const ZZ_p& b); // x = a + b
void add(ZZ_pX& x, const ZZ_pX& a, long b);

inline void add(ZZ_pX& x, const ZZ_p& a, const ZZ_pX& b) { add(x, b, a); }
inline void add(ZZ_pX& x, long a, const ZZ_pX& b) { add(x, b, a); }


void sub(ZZ_pX & x, const ZZ_pX& a, const ZZ_p& b); // x = a - b

void sub(ZZ_pX& x, const ZZ_pX& a, long b);
void sub(ZZ_pX& x, const ZZ_pX& a, const ZZ_p& b);

void sub(ZZ_pX& x, long a, const ZZ_pX& b);
void sub(ZZ_pX& x, const ZZ_p& a, const ZZ_pX& b);

inline ZZ_pX operator+(const ZZ_pX& a, const ZZ_pX& b)
   { ZZ_pX x; add(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator+(const ZZ_pX& a, const ZZ_p& b)
   { ZZ_pX x; add(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator+(const ZZ_pX& a, long b)
   { ZZ_pX x; add(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator+(const ZZ_p& a, const ZZ_pX& b)
   { ZZ_pX x; add(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator+(long a, const ZZ_pX& b)
   { ZZ_pX x; add(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }


inline ZZ_pX operator-(const ZZ_pX& a, const ZZ_pX& b)
   { ZZ_pX x; sub(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator-(const ZZ_pX& a, const ZZ_p& b)
   { ZZ_pX x; sub(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator-(const ZZ_pX& a, long b)
   { ZZ_pX x; sub(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator-(const ZZ_p& a, const ZZ_pX& b)
   { ZZ_pX x; sub(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator-(long a, const ZZ_pX& b)
   { ZZ_pX x; sub(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }


inline ZZ_pX& operator+=(ZZ_pX& x, const ZZ_pX& b)
   { add(x, x, b); return x; }

inline ZZ_pX& operator+=(ZZ_pX& x, const ZZ_p& b)
   { add(x, x, b); return x; }

inline ZZ_pX& operator+=(ZZ_pX& x, long b)
   { add(x, x, b); return x; }

inline ZZ_pX& operator-=(ZZ_pX& x, const ZZ_pX& b)
   { sub(x, x, b); return x; }

inline ZZ_pX& operator-=(ZZ_pX& x, const ZZ_p& b)
   { sub(x, x, b); return x; }

inline ZZ_pX& operator-=(ZZ_pX& x, long b)
   { sub(x, x, b); return x; }


inline ZZ_pX operator-(const ZZ_pX& a) 
   { ZZ_pX x; negate(x, a); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX& operator++(ZZ_pX& x) { add(x, x, 1); return x; }
inline void operator++(ZZ_pX& x, int) { add(x, x, 1); }
inline ZZ_pX& operator--(ZZ_pX& x) { sub(x, x, 1); return x; }
inline void operator--(ZZ_pX& x, int) { sub(x, x, 1); }

/*****************************************************************

                        Multiplication

******************************************************************/


void mul(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);
// x = a * b

void sqr(ZZ_pX& x, const ZZ_pX& a);
inline ZZ_pX sqr(const ZZ_pX& a)
   { ZZ_pX x; sqr(x, a); NTL_OPT_RETURN(ZZ_pX, x); }
// x = a^2

void mul(ZZ_pX & x, const ZZ_pX& a, const ZZ_p& b); 
void mul(ZZ_pX& x, const ZZ_pX& a, long b);


inline void mul(ZZ_pX& x, const ZZ_p& a, const ZZ_pX& b)
   { mul(x, b, a); }

inline void mul(ZZ_pX& x, long a, const ZZ_pX& b)
   { mul(x, b, a); }

inline ZZ_pX operator*(const ZZ_pX& a, const ZZ_pX& b)
   { ZZ_pX x; mul(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator*(const ZZ_pX& a, const ZZ_p& b)
   { ZZ_pX x; mul(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator*(const ZZ_pX& a, long b)
   { ZZ_pX x; mul(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator*(const ZZ_p& a, const ZZ_pX& b)
   { ZZ_pX x; mul(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator*(long a, const ZZ_pX& b)
   { ZZ_pX x; mul(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX& operator*=(ZZ_pX& x, const ZZ_pX& b)
   { mul(x, x, b); return x; }

inline ZZ_pX& operator*=(ZZ_pX& x, const ZZ_p& b)
   { mul(x, x, b); return x; }

inline ZZ_pX& operator*=(ZZ_pX& x, long b)
   { mul(x, x, b); return x; }


void PlainMul(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);
// always uses the "classical" algorithm

void PlainSqr(ZZ_pX& x, const ZZ_pX& a);
// always uses the "classical" algorithm


void FFTMul(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);
// always uses the FFT

void FFTSqr(ZZ_pX& x, const ZZ_pX& a);
// always uses the FFT

void MulTrunc(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, long n);
// x = a * b % X^n

inline ZZ_pX MulTrunc(const ZZ_pX& a, const ZZ_pX& b, long n)
   { ZZ_pX x; MulTrunc(x, a, b, n); NTL_OPT_RETURN(ZZ_pX, x); }

void PlainMulTrunc(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, long n);
void FFTMulTrunc(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, long n);

void SqrTrunc(ZZ_pX& x, const ZZ_pX& a, long n);
// x = a^2 % X^n

inline ZZ_pX SqrTrunc(const ZZ_pX& a, long n)
   { ZZ_pX x; SqrTrunc(x, a, n); NTL_OPT_RETURN(ZZ_pX, x); }

void PlainSqrTrunc(ZZ_pX& x, const ZZ_pX& a, long n);
void FFTSqrTrunc(ZZ_pX& x, const ZZ_pX& a, long n);


void power(ZZ_pX& x, const ZZ_pX& a, long e);
inline ZZ_pX power(const ZZ_pX& a, long e)
   { ZZ_pX x; power(x, a, e); NTL_OPT_RETURN(ZZ_pX, x); }


// The following data structures and routines allow one
// to hand-craft various algorithms, using the FFT convolution
// algorithms directly.
// Look in the file ZZ_pX.c for examples.




// FFT representation of polynomials

class FFTRep {
public:
   long k;                // a 2^k point representation
   long MaxK;             // maximum space allocated
   long NumPrimes; 
   Unique2DArray<long> tbl;

   FFTRep() : k(-1), MaxK(-1), NumPrimes(0) { }

   FFTRep(const FFTRep& R) : k(-1), MaxK(-1), NumPrimes(0)
   { *this = R; }

   FFTRep(INIT_SIZE_TYPE, long InitK) : k(-1), MaxK(-1), NumPrimes(0)
   {  SetSize(InitK); }

   FFTRep& operator=(const FFTRep& R);
   void SetSize(long NewK);
   void DoSetSize(long NewK, long NewNumPrimes);
};


void ToFFTRep(FFTRep& y, const ZZ_pX& x, long k, long lo, long hi);
// computes an n = 2^k point convolution of x[lo..hi].

inline void ToFFTRep(FFTRep& y, const ZZ_pX& x, long k)

   { ToFFTRep(y, x, k, 0, deg(x)); }

void RevToFFTRep(FFTRep& y, const vec_ZZ_p& x,
                 long k, long lo, long hi, long offset);
// computes an n = 2^k point convolution of X^offset*x[lo..hi] mod X^n-1
// using "inverted" evaluation points.


void FromFFTRep(ZZ_pX& x, FFTRep& y, long lo, long hi);
// converts from FFT-representation to coefficient representation
// only the coefficients lo..hi are computed
// NOTE: this version destroys the data in y

// non-destructive versions of the above

void NDFromFFTRep(ZZ_pX& x, const FFTRep& y, long lo, long hi, FFTRep& temp);
void NDFromFFTRep(ZZ_pX& x, const FFTRep& y, long lo, long hi);

void RevFromFFTRep(vec_ZZ_p& x, FFTRep& y, long lo, long hi);

   // converts from FFT-representation to coefficient representation
   // using "inverted" evaluation points.
   // only the coefficients lo..hi are computed




void FromFFTRep(ZZ_p* x, FFTRep& y, long lo, long hi);
// convert out coefficients lo..hi of y, store result in x.
// no normalization is done.


// direct manipulation of FFT reps

void mul(FFTRep& z, const FFTRep& x, const FFTRep& y);
void sub(FFTRep& z, const FFTRep& x, const FFTRep& y);
void add(FFTRep& z, const FFTRep& x, const FFTRep& y);

void reduce(FFTRep& x, const FFTRep& a, long k);
// reduces a 2^l point FFT-rep to a 2^k point FFT-rep

void AddExpand(FFTRep& x, const FFTRep& a);
//  x = x + (an "expanded" version of a)





// This data structure holds unconvoluted modular representations
// of polynomials

class ZZ_pXModRep {
private:
   ZZ_pXModRep(const ZZ_pXModRep&); // disabled
   void operator=(const ZZ_pXModRep&); // disabled

public:
   long n;
   long MaxN;
   long NumPrimes; 
   Unique2DArray<long> tbl;

   void SetSize(long NewN);

   ZZ_pXModRep() : n(0), MaxN(0), NumPrimes(0) {  }
   ZZ_pXModRep(INIT_SIZE_TYPE, long NewN) : n(0), MaxN(0), NumPrimes(0)
   {  SetSize(NewN); }
};


void ToZZ_pXModRep(ZZ_pXModRep& x, const ZZ_pX& a, long lo, long hi);

void ToFFTRep(FFTRep& x, const ZZ_pXModRep& a, long k, long lo, long hi);
// converts coefficients lo..hi to a 2^k-point FFTRep.
// must have hi-lo+1 < 2^k


void FromFFTRep(ZZ_pXModRep& x, const FFTRep& a);
// for testing and timing purposes only -- converts from FFTRep

void FromZZ_pXModRep(ZZ_pX& x, const ZZ_pXModRep& a, long lo, long hi);
// for testing and timing purposes only -- converts from ZZ_pXModRep




/*************************************************************

                      Division

**************************************************************/

void DivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);
// q = a/b, r = a%b

void div(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b);
// q = a/b

void div(ZZ_pX& q, const ZZ_pX& a, const ZZ_p& b);
void div(ZZ_pX& q, const ZZ_pX& a, long b);


void rem(ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);
// r = a%b

long divide(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b);
// if b | a, sets q = a/b and returns 1; otherwise returns 0

long divide(const ZZ_pX& a, const ZZ_pX& b);
// if b | a, sets q = a/b and returns 1; otherwise returns 0

void InvTrunc(ZZ_pX& x, const ZZ_pX& a, long m);
// computes x = a^{-1} % X^m 
// constant term must be non-zero

inline ZZ_pX InvTrunc(const ZZ_pX& a, long m)
   { ZZ_pX x; InvTrunc(x, a, m); NTL_OPT_RETURN(ZZ_pX, x); }



// These always use "classical" arithmetic
void PlainDivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);
void PlainDiv(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b);
void PlainRem(ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);

void PlainRem(ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b, ZZVec& tmp);
void PlainDivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b,
                 ZZVec& tmp);


// These always use FFT arithmetic
void FFTDivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);
void FFTDiv(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b);
void FFTRem(ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b);

void PlainInvTrunc(ZZ_pX& x, const ZZ_pX& a, long m);
// always uses "classical" algorithm
// ALIAS RESTRICTION: input may not alias output

void NewtonInvTrunc(ZZ_pX& x, const ZZ_pX& a, long m);
// uses a Newton Iteration with the FFT.
// ALIAS RESTRICTION: input may not alias output


inline ZZ_pX operator/(const ZZ_pX& a, const ZZ_pX& b)
   { ZZ_pX x; div(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator/(const ZZ_pX& a, const ZZ_p& b)
   { ZZ_pX x; div(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX operator/(const ZZ_pX& a, long b)
   { ZZ_pX x; div(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX& operator/=(ZZ_pX& x, const ZZ_p& b)
   { div(x, x, b); return x; }

inline ZZ_pX& operator/=(ZZ_pX& x, long b)
   { div(x, x, b); return x; }

inline ZZ_pX& operator/=(ZZ_pX& x, const ZZ_pX& b)
   { div(x, x, b); return x; }


inline ZZ_pX operator%(const ZZ_pX& a, const ZZ_pX& b)
   { ZZ_pX x; rem(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX& operator%=(ZZ_pX& x, const ZZ_pX& b)
   { rem(x, x, b); return x; }


/***********************************************************

                         GCD's

************************************************************/


void GCD(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);
// x = GCD(a, b),  x is always monic (or zero if a==b==0).

inline ZZ_pX GCD(const ZZ_pX& a, const ZZ_pX& b)
   { ZZ_pX x; GCD(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }

void XGCD(ZZ_pX& d, ZZ_pX& s, ZZ_pX& t, const ZZ_pX& a, const ZZ_pX& b);
// d = gcd(a,b), a s + b t = d 

void PlainXGCD(ZZ_pX& d, ZZ_pX& s, ZZ_pX& t, const ZZ_pX& a, const ZZ_pX& b);
// same as above, but uses classical algorithm


void PlainGCD(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b);
// always uses "cdlassical" arithmetic


class ZZ_pXMatrix {
private:

   ZZ_pXMatrix(const ZZ_pXMatrix&);  // disable
   ZZ_pX elts[2][2];

public:

   ZZ_pXMatrix() { }
   ~ZZ_pXMatrix() { }

   void operator=(const ZZ_pXMatrix&);
   ZZ_pX& operator() (long i, long j) { return elts[i][j]; }
   const ZZ_pX& operator() (long i, long j) const { return elts[i][j]; }
};


void HalfGCD(ZZ_pXMatrix& M_out, const ZZ_pX& U, const ZZ_pX& V, long d_red);
// deg(U) > deg(V),   1 <= d_red <= deg(U)+1.
//
// This computes a 2 x 2 polynomial matrix M_out such that
//    M_out * (U, V)^T = (U', V')^T,
// where U', V' are consecutive polynomials in the Euclidean remainder
// sequence of U, V, and V' is the polynomial of highest degree
// satisfying deg(V') <= deg(U) - d_red.

void XHalfGCD(ZZ_pXMatrix& M_out, ZZ_pX& U, ZZ_pX& V, long d_red);

// same as above, except that U is replaced by U', and V by V'


/*************************************************************

             Modular Arithmetic without pre-conditioning

**************************************************************/

// arithmetic mod f.
// all inputs and outputs are polynomials of degree less than deg(f).
// ASSUMPTION: f is assumed monic, and deg(f) > 0.
// NOTE: if you want to do many computations with a fixed f,
//       use the ZZ_pXModulus data structure and associated routines below.



void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, const ZZ_pX& f);
// x = (a * b) % f

inline ZZ_pX MulMod(const ZZ_pX& a, const ZZ_pX& b, const ZZ_pX& f)
   { ZZ_pX x; MulMod(x, a, b, f); NTL_OPT_RETURN(ZZ_pX, x); }

void SqrMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);
// x = a^2 % f

inline ZZ_pX SqrMod(const ZZ_pX& a,  const ZZ_pX& f)
   { ZZ_pX x; SqrMod(x, a, f); NTL_OPT_RETURN(ZZ_pX, x); }

void MulByXMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);
// x = (a * X) mod f

inline ZZ_pX MulByXMod(const ZZ_pX& a,  const ZZ_pX& f)
   { ZZ_pX x; MulByXMod(x, a, f); NTL_OPT_RETURN(ZZ_pX, x); }



void InvMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);
// x = a^{-1} % f, error is a is not invertible

inline ZZ_pX InvMod(const ZZ_pX& a,  const ZZ_pX& f)
   { ZZ_pX x; InvMod(x, a, f); NTL_OPT_RETURN(ZZ_pX, x); }

long InvModStatus(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f);
// if (a, f) = 1, returns 0 and sets x = a^{-1} % f
// otherwise, returns 1 and sets x = (a, f)




/******************************************************************

        Modular Arithmetic with Pre-conditioning

*******************************************************************/


// If you need to do a lot of arithmetic modulo a fixed f,
// build ZZ_pXModulus F for f.  This pre-computes information about f
// that speeds up the computation a great deal.


class ZZ_pXModulus {
public:
   ZZ_pXModulus() : UseFFT(0), n(-1)  { }
   ~ZZ_pXModulus() { }
   

   // the following members may become private in future
   ZZ_pX f;   // the modulus
   long UseFFT;// flag indicating whether FFT should be used.
   long n;     // n = deg(f)
   long k;     // least k s/t 2^k >= n
   long l;     // least l s/t 2^l >= 2n-3
   FFTRep FRep; // 2^k point rep of f
                // H = rev((rev(f))^{-1} rem X^{n-1})
   FFTRep HRep; // 2^l point rep of H

   OptionalVal< Lazy<vec_ZZ_p> > tracevec;
   // an extra level of indirection to ensure the class
   // can be used in a Vec (there may be a mutex in the Lazy object)

   // but these will remain public
   ZZ_pXModulus(const ZZ_pX& ff);

   const ZZ_pX& val() const { return f; }
   operator const ZZ_pX& () const { return f; }

};

inline long deg(const ZZ_pXModulus& F) { return F.n; }

void build(ZZ_pXModulus& F, const ZZ_pX& f);
// deg(f) > 0.


void rem21(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXModulus& F);
// x = a % f
// deg(a) <= 2(n-1), where n = F.n = deg(f)

void rem(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXModulus& F);
// x = a % f, no restrictions on deg(a);  makes repeated calls to rem21

inline ZZ_pX operator%(const ZZ_pX& a, const ZZ_pXModulus& F)
   { ZZ_pX x; rem(x, a, F); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX& operator%=(ZZ_pX& x, const ZZ_pXModulus& F)
   { rem(x, x, F); return x; } 

void DivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pXModulus& F);

void div(ZZ_pX& q, const ZZ_pX& a, const ZZ_pXModulus& F);

inline ZZ_pX operator/(const ZZ_pX& a, const ZZ_pXModulus& F)
   { ZZ_pX x; div(x, a, F); NTL_OPT_RETURN(ZZ_pX, x); }

inline ZZ_pX& operator/=(ZZ_pX& x, const ZZ_pXModulus& F)
   { div(x, x, F); return x; } 

void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, const ZZ_pXModulus& F);
// x = (a * b) % f
// deg(a), deg(b) < n

inline ZZ_pX MulMod(const ZZ_pX& a, const ZZ_pX& b, const ZZ_pXModulus& F)
   { ZZ_pX x; MulMod(x, a, b, F); NTL_OPT_RETURN(ZZ_pX, x); }

void SqrMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXModulus& F);
// x = a^2 % f
// deg(a) < n

inline ZZ_pX SqrMod(const ZZ_pX& a, const ZZ_pXModulus& F)
   { ZZ_pX x; SqrMod(x, a, F); NTL_OPT_RETURN(ZZ_pX, x); }

void PowerMod(ZZ_pX& x, const ZZ_pX& a, const ZZ& e, const ZZ_pXModulus& F);
// x = a^e % f, e >= 0

inline ZZ_pX PowerMod(const ZZ_pX& a, const ZZ& e, const ZZ_pXModulus& F)
   { ZZ_pX x; PowerMod(x, a, e, F); NTL_OPT_RETURN(ZZ_pX, x); }

inline void PowerMod(ZZ_pX& x, const ZZ_pX& a, long e, const ZZ_pXModulus& F)
   { PowerMod(x, a, ZZ_expo(e), F); }

inline ZZ_pX PowerMod(const ZZ_pX& a, long e, const ZZ_pXModulus& F)
   { ZZ_pX x; PowerMod(x, a, e, F); NTL_OPT_RETURN(ZZ_pX, x); }



void PowerXMod(ZZ_pX& x, const ZZ& e, const ZZ_pXModulus& F);
// x = X^e % f, e >= 0

inline ZZ_pX PowerXMod(const ZZ& e, const ZZ_pXModulus& F)
   { ZZ_pX x; PowerXMod(x, e, F); NTL_OPT_RETURN(ZZ_pX, x); }

inline void PowerXMod(ZZ_pX& x, long e, const ZZ_pXModulus& F)
   { PowerXMod(x, ZZ_expo(e), F); }

inline ZZ_pX PowerXMod(long e, const ZZ_pXModulus& F)
   { ZZ_pX x; PowerXMod(x, e, F); NTL_OPT_RETURN(ZZ_pX, x); }

void PowerXPlusAMod(ZZ_pX& x, const ZZ_p& a, const ZZ& e, const ZZ_pXModulus& F);
// x = (X + a)^e % f, e >= 0

inline ZZ_pX PowerXPlusAMod(const ZZ_p& a, const ZZ& e, const ZZ_pXModulus& F)
   { ZZ_pX x; PowerXPlusAMod(x, a, e, F); NTL_OPT_RETURN(ZZ_pX, x); }

inline void PowerXPlusAMod(ZZ_pX& x, const ZZ_p& a, long e, const ZZ_pXModulus& F)
   { PowerXPlusAMod(x, a, ZZ_expo(e), F); }


inline ZZ_pX PowerXPlusAMod(const ZZ_p& a, long e, const ZZ_pXModulus& F)
   { ZZ_pX x; PowerXPlusAMod(x, a, e, F); NTL_OPT_RETURN(ZZ_pX, x); }

// If you need to compute a * b % f for a fixed b, but for many a's
// (for example, computing powers of b modulo f), it is
// much more efficient to first build a ZZ_pXMultiplier B for b,
// and then use the routine below.

class ZZ_pXMultiplier {
public:
   ZZ_pXMultiplier() : UseFFT(0)  { }
   ZZ_pXMultiplier(const ZZ_pX& b, const ZZ_pXModulus& F);

   ~ZZ_pXMultiplier() { }


   // the following members may become private in the future
   ZZ_pX b;   
   long UseFFT;
   FFTRep B1; 
   FFTRep B2; 

   // but this will remain public
   const ZZ_pX& val() const { return b; }

};

void build(ZZ_pXMultiplier& B, const ZZ_pX& b, const ZZ_pXModulus& F);

void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXMultiplier& B,
                                      const ZZ_pXModulus& F);

inline ZZ_pX MulMod(const ZZ_pX& a, const ZZ_pXMultiplier& B,
                                          const ZZ_pXModulus& F)
   { ZZ_pX x; MulMod(x, a, B, F); NTL_OPT_RETURN(ZZ_pX, x); }

// x = (a * b) % f


/*******************************************************

              Evaluation and related problems

********************************************************/


void BuildFromRoots(ZZ_pX& x, const vec_ZZ_p& a);
// computes the polynomial (X-a[0]) ... (X-a[n-1]), where n = a.length()

inline ZZ_pX BuildFromRoots(const vec_ZZ_p& a)
   { ZZ_pX x; BuildFromRoots(x, a); NTL_OPT_RETURN(ZZ_pX, x); }


void eval(ZZ_p& b, const ZZ_pX& f, const ZZ_p& a);
// b = f(a)

inline ZZ_p eval(const ZZ_pX& f, const ZZ_p& a)
   { ZZ_p x; eval(x, f, a); NTL_OPT_RETURN(ZZ_p, x); }

void eval(vec_ZZ_p& b, const ZZ_pX& f, const vec_ZZ_p& a);
//  b[i] = f(a[i])

inline vec_ZZ_p eval(const ZZ_pX& f, const vec_ZZ_p& a)
   { vec_ZZ_p x; eval(x, f, a); NTL_OPT_RETURN(vec_ZZ_p, x); }


void interpolate(ZZ_pX& f, const vec_ZZ_p& a, const vec_ZZ_p& b);
// computes f such that f(a[i]) = b[i]

inline ZZ_pX interpolate(const vec_ZZ_p& a, const vec_ZZ_p& b)
   { ZZ_pX x; interpolate(x, a, b); NTL_OPT_RETURN(ZZ_pX, x); }


/*****************************************************************

                       vectors of ZZ_pX's

*****************************************************************/

typedef Vec<ZZ_pX> vec_ZZ_pX;



/**********************************************************

         Modular Composition and Minimal Polynomials

***********************************************************/


// algorithms for computing g(h) mod f



void CompMod(ZZ_pX& x, const ZZ_pX& g, const ZZ_pX& h, const ZZ_pXModulus& F);
// x = g(h) mod f

inline ZZ_pX CompMod(const ZZ_pX& g, const ZZ_pX& h, 
                           const ZZ_pXModulus& F)
   { ZZ_pX x; CompMod(x, g, h, F); NTL_OPT_RETURN(ZZ_pX, x); }

void Comp2Mod(ZZ_pX& x1, ZZ_pX& x2, const ZZ_pX& g1, const ZZ_pX& g2,
              const ZZ_pX& h, const ZZ_pXModulus& F);
// xi = gi(h) mod f (i=1,2)

void Comp3Mod(ZZ_pX& x1, ZZ_pX& x2, ZZ_pX& x3, 
              const ZZ_pX& g1, const ZZ_pX& g2, const ZZ_pX& g3,
              const ZZ_pX& h, const ZZ_pXModulus& F);
// xi = gi(h) mod f (i=1..3)



// The routine build (see below) which is implicitly called
// by the various compose and UpdateMap routines builds a table
// of polynomials.  
// If ZZ_pXArgBound > 0, then the table is limited in
// size to approximamtely that many KB.
// If ZZ_pXArgBound <= 0, then it is ignored, and space is allocated
// so as to maximize speed.
// Initially, ZZ_pXArgBound = 0.


// If a single h is going to be used with many g's
// then you should build a ZZ_pXArgument for h,
// and then use the compose routine below.
// build computes and stores h, h^2, ..., h^m mod f.
// After this pre-computation, composing a polynomial of degree 
// roughly n with h takes n/m multiplies mod f, plus n^2
// scalar multiplies.
// Thus, increasing m increases the space requirement and the pre-computation
// time, but reduces the composition time.
// If ZZ_pXArgBound > 0, a table of size less than m may be built.

struct ZZ_pXArgument {
   vec_ZZ_pX H;
};

extern NTL_CHEAP_THREAD_LOCAL long ZZ_pXArgBound;


void build(ZZ_pXArgument& H, const ZZ_pX& h, const ZZ_pXModulus& F, long m);

// m must be > 0, otherwise an error is raised

void CompMod(ZZ_pX& x, const ZZ_pX& g, const ZZ_pXArgument& H, 
             const ZZ_pXModulus& F);

inline ZZ_pX 
CompMod(const ZZ_pX& g, const ZZ_pXArgument& H, const ZZ_pXModulus& F)
   { ZZ_pX x; CompMod(x, g, H, F); NTL_OPT_RETURN(ZZ_pX, x); }


#ifndef NTL_TRANSITION

void UpdateMap(vec_ZZ_p& x, const vec_ZZ_p& a,
               const ZZ_pXMultiplier& B, const ZZ_pXModulus& F);

inline vec_ZZ_p
UpdateMap(const vec_ZZ_p& a, 
          const ZZ_pXMultiplier& B, const ZZ_pXModulus& F)
   { vec_ZZ_p x; UpdateMap(x, a, B, F); 
     NTL_OPT_RETURN(vec_ZZ_p, x); }

#endif


/* computes (a, b), (a, (b*X)%f), ..., (a, (b*X^{n-1})%f),
   where ( , ) denotes the vector inner product.

   This is really a "transposed" MulMod by B.
*/

void PlainUpdateMap(vec_ZZ_p& x, const vec_ZZ_p& a,
                    const ZZ_pX& b, const ZZ_pX& f);

// same as above, but uses only classical arithmetic


void ProjectPowers(vec_ZZ_p& x, const vec_ZZ_p& a, long k,
                   const ZZ_pX& h, const ZZ_pXModulus& F);

inline vec_ZZ_p ProjectPowers(const vec_ZZ_p& a, long k,
                   const ZZ_pX& h, const ZZ_pXModulus& F)
{
   vec_ZZ_p x;
   ProjectPowers(x, a, k, h, F);
   NTL_OPT_RETURN(vec_ZZ_p, x);
}


// computes (a, 1), (a, h), ..., (a, h^{k-1} % f)
// this is really a "transposed" compose.

void ProjectPowers(vec_ZZ_p& x, const vec_ZZ_p& a, long k,
                   const ZZ_pXArgument& H, const ZZ_pXModulus& F);

inline vec_ZZ_p ProjectPowers(const vec_ZZ_p& a, long k,
                   const ZZ_pXArgument& H, const ZZ_pXModulus& F)
{
   vec_ZZ_p x;
   ProjectPowers(x, a, k, H, F);
   NTL_OPT_RETURN(vec_ZZ_p, x);
}

// same as above, but uses a pre-computed ZZ_pXArgument

inline void project(ZZ_p& x, const vec_ZZ_p& a, const ZZ_pX& b)
   { InnerProduct(x, a, b.rep); }

inline ZZ_p project(const vec_ZZ_p& a, const ZZ_pX& b)
   {  ZZ_p x; project(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }

void MinPolySeq(ZZ_pX& h, const vec_ZZ_p& a, long m);
// computes the minimum polynomial of a linealy generated sequence;
// m is a bound on the degree of the polynomial;
// required: a.length() >= 2*m

inline ZZ_pX MinPolySeq(const vec_ZZ_p& a, long m)
   { ZZ_pX x; MinPolySeq(x, a, m); NTL_OPT_RETURN(ZZ_pX, x); }

void ProbMinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m);

inline ZZ_pX ProbMinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m)
   { ZZ_pX x; ProbMinPolyMod(x, g, F, m); NTL_OPT_RETURN(ZZ_pX, x); }


inline void ProbMinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F)
   { ProbMinPolyMod(h, g, F, F.n); }

inline ZZ_pX ProbMinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F)
   { ZZ_pX x; ProbMinPolyMod(x, g, F); NTL_OPT_RETURN(ZZ_pX, x); }


// computes the monic minimal polynomial if (g mod f).
// m = a bound on the degree of the minimal polynomial.
// If this argument is not supplied, it defaults to deg(f).
// The algorithm is probabilistic, always returns a divisor of
// the minimal polynomial, and returns a proper divisor with
// probability at most m/p.

void MinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m);

inline ZZ_pX MinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m)
   { ZZ_pX x; MinPolyMod(x, g, F, m); NTL_OPT_RETURN(ZZ_pX, x); }

inline void MinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F)
   { MinPolyMod(h, g, F, F.n); }

inline ZZ_pX MinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F)
   { ZZ_pX x; MinPolyMod(x, g, F); NTL_OPT_RETURN(ZZ_pX, x); }

// same as above, but guarantees that result is correct

void IrredPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m);

inline ZZ_pX IrredPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m)
   { ZZ_pX x; IrredPolyMod(x, g, F, m); NTL_OPT_RETURN(ZZ_pX, x); }


inline void IrredPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F)
   { IrredPolyMod(h, g, F, F.n); }

inline ZZ_pX IrredPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F)
   { ZZ_pX x; IrredPolyMod(x, g, F); NTL_OPT_RETURN(ZZ_pX, x); }

// same as above, but assumes that f is irreducible, 
// or at least that the minimal poly of g is itself irreducible.
// The algorithm is deterministic (and is always correct).

/*****************************************************************

                   Traces, norms, resultants

******************************************************************/

void TraceVec(vec_ZZ_p& S, const ZZ_pX& f);

inline vec_ZZ_p TraceVec(const ZZ_pX& f)
   { vec_ZZ_p x; TraceVec(x, f); NTL_OPT_RETURN(vec_ZZ_p, x); }

void FastTraceVec(vec_ZZ_p& S, const ZZ_pX& f);
void PlainTraceVec(vec_ZZ_p& S, const ZZ_pX& f);

void TraceMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pXModulus& F);

inline ZZ_p TraceMod(const ZZ_pX& a, const ZZ_pXModulus& F)
   { ZZ_p x; TraceMod(x, a, F); NTL_OPT_RETURN(ZZ_p, x); }

void TraceMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& f);

inline ZZ_p TraceMod(const ZZ_pX& a, const ZZ_pX& f)
   { ZZ_p x; TraceMod(x, a, f); NTL_OPT_RETURN(ZZ_p, x); }



void ComputeTraceVec(const ZZ_pXModulus& F);


void NormMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& f);

inline ZZ_p NormMod(const ZZ_pX& a, const ZZ_pX& f)
   { ZZ_p x; NormMod(x, a, f); NTL_OPT_RETURN(ZZ_p, x); }

void resultant(ZZ_p& rres, const ZZ_pX& a, const ZZ_pX& b);

inline ZZ_p resultant(const ZZ_pX& a, const ZZ_pX& b)
   { ZZ_p x; resultant(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }

void CharPolyMod(ZZ_pX& g, const ZZ_pX& a, const ZZ_pX& f);
// g = char poly of (a mod f)

inline ZZ_pX CharPolyMod(const ZZ_pX& a, const ZZ_pX& f)
   { ZZ_pX x; CharPolyMod(x, a, f); NTL_OPT_RETURN(ZZ_pX, x); }



NTL_CLOSE_NNS

#endif