[cae0b6] | 1 | /* $Id: NTLconvert.cc,v 1.4 2002-10-10 17:43:38 Singular Exp $ */ |
---|
[a99e31] | 2 | #include <config.h> |
---|
| 3 | |
---|
| 4 | #include "cf_gmp.h" |
---|
| 5 | |
---|
| 6 | #include "assert.h" |
---|
| 7 | |
---|
| 8 | #include "cf_defs.h" |
---|
| 9 | #include "canonicalform.h" |
---|
| 10 | #include "cf_iter.h" |
---|
| 11 | #include "fac_berlekamp.h" |
---|
| 12 | #include "fac_cantzass.h" |
---|
| 13 | #include "fac_univar.h" |
---|
| 14 | #include "fac_multivar.h" |
---|
| 15 | #include "fac_sqrfree.h" |
---|
| 16 | #include "cf_algorithm.h" |
---|
| 17 | |
---|
[d30633d] | 18 | #ifdef HAVE_NTL |
---|
| 19 | |
---|
[a99e31] | 20 | #include <NTL/ZZXFactoring.h> |
---|
| 21 | #include <NTL/ZZ_pXFactoring.h> |
---|
| 22 | #include <NTL/GF2XFactoring.h> |
---|
| 23 | #include "int_int.h" |
---|
| 24 | #include <limits.h> |
---|
| 25 | #include <NTL/ZZ_pEXFactoring.h> |
---|
| 26 | #include <NTL/GF2EXFactoring.h> |
---|
| 27 | #include "NTLconvert.h" |
---|
| 28 | |
---|
[d30633d] | 29 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 30 | // NAME: convertFacCF2NTLZZpX // |
---|
| 31 | // // |
---|
| 32 | // DESCRIPTION: // |
---|
| 33 | // Conversion routine for Factory-type canonicalform into ZZpX of NTL, // |
---|
| 34 | // i.e. polynomials over F_p. As a precondition for correct execution, // |
---|
| 35 | // the characteristic has to a a prime number. // |
---|
| 36 | // // |
---|
| 37 | // INPUT: A canonicalform f // |
---|
| 38 | // OUTPUT: The converted NTL-polynomial over F_p of type ZZpX // |
---|
| 39 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 40 | |
---|
[d30633d] | 41 | #if 0 |
---|
| 42 | void out_cf(char *s1,const CanonicalForm &f,char *s2) |
---|
| 43 | { |
---|
| 44 | printf("%s",s1); |
---|
| 45 | if (f==0) printf("+0"); |
---|
| 46 | else if (! f.inCoeffDomain() ) |
---|
| 47 | { |
---|
| 48 | int l = f.level(); |
---|
| 49 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 50 | { |
---|
| 51 | int e=i.exp(); |
---|
| 52 | printf("+(");out_cf("+(",i.coeff(),")*v(");printf("%d)^%d",l,e); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | else |
---|
| 56 | { |
---|
| 57 | if ( f.isImm() ) |
---|
| 58 | { |
---|
| 59 | printf("+%d(",f.intval()); |
---|
| 60 | } |
---|
| 61 | else printf("+...("); |
---|
| 62 | if (f.inZ()) printf("Z)"); |
---|
| 63 | else if (f.inQ()) printf("Q)"); |
---|
| 64 | else if (f.inFF()) printf("FF)"); |
---|
| 65 | else if (f.inPP()) printf("PP)"); |
---|
| 66 | else if (f.inGF()) printf("PP)"); |
---|
| 67 | else if (f.inExtension()) printf("E(%d))",f.level()); |
---|
| 68 | } |
---|
| 69 | printf("%s",s2); |
---|
| 70 | } |
---|
| 71 | #endif |
---|
[a99e31] | 72 | |
---|
| 73 | ZZ_pX convertFacCF2NTLZZpX(CanonicalForm f) |
---|
[d30633d] | 74 | { |
---|
[a99e31] | 75 | ZZ_pX ntl_poly; |
---|
| 76 | |
---|
[d30633d] | 77 | CFIterator i; |
---|
| 78 | i=f; |
---|
[a99e31] | 79 | |
---|
[d30633d] | 80 | int j=0; |
---|
| 81 | int NTLcurrentExp=i.exp(); |
---|
| 82 | int largestExp=i.exp(); |
---|
| 83 | int k; |
---|
[a99e31] | 84 | |
---|
[d30633d] | 85 | // we now build up the NTL-polynomial |
---|
| 86 | ntl_poly.SetMaxLength(largestExp+1); |
---|
[a99e31] | 87 | |
---|
[d30633d] | 88 | for (;i.hasTerms();i++) |
---|
| 89 | { |
---|
| 90 | for (k=NTLcurrentExp;k>i.exp();k--) |
---|
| 91 | { |
---|
| 92 | SetCoeff(ntl_poly,k,0); |
---|
| 93 | } |
---|
| 94 | NTLcurrentExp=i.exp(); |
---|
| 95 | |
---|
| 96 | CanonicalForm c=i.coeff(); |
---|
| 97 | if (!c.isImm()) c.mapinto(); //c%= getCharacteristic(); |
---|
| 98 | if (!c.isImm()) |
---|
| 99 | { //This case will never happen if the characteristic is in fact a prime |
---|
| 100 | // number, since all coefficients are represented as immediates |
---|
| 101 | #ifndef NOSTREAMIO |
---|
| 102 | cout<<"convertFacCF2NTLZZ_pX: coefficient not immediate! : "<<f<<"\n"; |
---|
| 103 | #else |
---|
| 104 | printf("convertFacCF2NTLZZ_pX: coefficient not immediate!, char=%d\n", |
---|
| 105 | getCharacteristic()); |
---|
| 106 | #endif |
---|
| 107 | exit(1); |
---|
[a99e31] | 108 | } |
---|
[d30633d] | 109 | else |
---|
| 110 | { |
---|
| 111 | SetCoeff(ntl_poly,NTLcurrentExp,c.intval()); |
---|
| 112 | } |
---|
| 113 | NTLcurrentExp--; |
---|
| 114 | } |
---|
[a99e31] | 115 | |
---|
[d30633d] | 116 | //Set the remaining coefficients of ntl_poly to zero. |
---|
| 117 | // This is necessary, because NTL internally |
---|
| 118 | // also stores powers with zero coefficient, |
---|
| 119 | // whereas factory stores tuples of degree and coefficient |
---|
| 120 | //leaving out tuples if the coefficient equals zero |
---|
| 121 | for (k=NTLcurrentExp;k>=0;k--) |
---|
| 122 | { |
---|
| 123 | SetCoeff(ntl_poly,k,0); |
---|
| 124 | } |
---|
[a99e31] | 125 | |
---|
[d30633d] | 126 | //normalize the polynomial and return it |
---|
| 127 | ntl_poly.normalize(); |
---|
[a99e31] | 128 | |
---|
[d30633d] | 129 | return ntl_poly; |
---|
[a99e31] | 130 | } |
---|
| 131 | |
---|
[d30633d] | 132 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 133 | // NAME: convertFacCF2NTLGF2X // |
---|
| 134 | // // |
---|
| 135 | // DESCRIPTION: // |
---|
| 136 | // Conversion routine for Factory-type canonicalform into GF2X of NTL, // |
---|
| 137 | // i.e. polynomials over F_2. As precondition for correct execution, // |
---|
| 138 | // the characteristic must equal two. // |
---|
| 139 | // This is a special case of the more general conversion routine for // |
---|
| 140 | // canonicalform to ZZpX. It is included because NTL provides additional // |
---|
| 141 | // support and faster algorithms over F_2, moreover the conversion code // |
---|
| 142 | // can be optimized, because certain steps are either completely obsolent // |
---|
| 143 | // (like normalizing the polynomial) or they can be made significantly // |
---|
| 144 | // faster (like building up the NTL-polynomial). // |
---|
| 145 | // // |
---|
| 146 | // INPUT: A canonicalform f // |
---|
| 147 | // OUTPUT: The converted NTL-polynomial over F_2 of type GF2X // |
---|
| 148 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 149 | |
---|
| 150 | GF2X convertFacCF2NTLGF2X(CanonicalForm f) |
---|
[d30633d] | 151 | { |
---|
| 152 | //printf("convertFacCF2NTLGF2X\n"); |
---|
| 153 | GF2X ntl_poly; |
---|
[a99e31] | 154 | |
---|
[d30633d] | 155 | CFIterator i; |
---|
| 156 | i=f; |
---|
[a99e31] | 157 | |
---|
[d30633d] | 158 | int j=0; |
---|
| 159 | int NTLcurrentExp=i.exp(); |
---|
| 160 | int largestExp=i.exp(); |
---|
| 161 | int k; |
---|
[a99e31] | 162 | |
---|
[d30633d] | 163 | //building the NTL-polynomial |
---|
| 164 | ntl_poly.SetMaxLength(largestExp+1); |
---|
| 165 | |
---|
| 166 | for (;i.hasTerms();i++) |
---|
| 167 | { |
---|
| 168 | |
---|
| 169 | for (k=NTLcurrentExp;k>i.exp();k--) |
---|
[a99e31] | 170 | { |
---|
[d30633d] | 171 | SetCoeff(ntl_poly,k,0); |
---|
| 172 | } |
---|
| 173 | NTLcurrentExp=i.exp(); |
---|
[a99e31] | 174 | |
---|
[d30633d] | 175 | if (!i.coeff().isImm()) i.coeff()=i.coeff().mapinto(); |
---|
| 176 | if (!i.coeff().isImm()) |
---|
| 177 | { |
---|
| 178 | #ifndef NOSTREAMIO |
---|
| 179 | cout<<"convertFacCF2NTLGF2X: coefficient not immidiate! : " << f << "\n"; |
---|
| 180 | #else |
---|
| 181 | printf("convertFacCF2NTLGF2X: coefficient not immidiate!"); |
---|
| 182 | #endif |
---|
| 183 | exit(1); |
---|
[a99e31] | 184 | } |
---|
[d30633d] | 185 | else |
---|
| 186 | { |
---|
| 187 | SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval()); |
---|
| 188 | } |
---|
| 189 | NTLcurrentExp--; |
---|
| 190 | } |
---|
| 191 | for (k=NTLcurrentExp;k>=0;k--) |
---|
| 192 | { |
---|
| 193 | SetCoeff(ntl_poly,k,0); |
---|
| 194 | } |
---|
| 195 | //normalization is not necessary of F_2 |
---|
[a99e31] | 196 | |
---|
[d30633d] | 197 | return ntl_poly; |
---|
[a99e31] | 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
[d30633d] | 201 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 202 | // NAME: convertNTLZZpX2CF // |
---|
| 203 | // // |
---|
| 204 | // DESCRIPTION: // |
---|
| 205 | // Conversion routine for NTL-Type ZZpX to Factory-Type canonicalform. // |
---|
| 206 | // Additionally a variable x is needed as a parameter indicating the // |
---|
| 207 | // main variable of the computed canonicalform. To guarantee the correct // |
---|
| 208 | // execution of the algorithm, the characteristic has a be an arbitrary // |
---|
| 209 | // prime number. // |
---|
| 210 | // // |
---|
| 211 | // INPUT: A canonicalform f, a variable x // |
---|
| 212 | // OUTPUT: The converted Factory-polynomial of type canonicalform, // |
---|
| 213 | // built by the main variable x // |
---|
| 214 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 215 | |
---|
| 216 | CanonicalForm convertNTLZZpX2CF(ZZ_pX poly,Variable x) |
---|
| 217 | { |
---|
[d30633d] | 218 | //printf("convertNTLZZpX2CF\n"); |
---|
[a99e31] | 219 | CanonicalForm bigone; |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | if (deg(poly)>0) |
---|
| 223 | { |
---|
| 224 | // poly is non-constant |
---|
| 225 | bigone=0; |
---|
[d30633d] | 226 | bigone.mapinto(); |
---|
| 227 | // Compute the canonicalform coefficient by coefficient, |
---|
| 228 | // bigone summarizes the result. |
---|
[a99e31] | 229 | for (int j=0;j<deg(poly)+1;j++) |
---|
| 230 | { |
---|
[d30633d] | 231 | if (coeff(poly,j)!=0) |
---|
| 232 | { |
---|
| 233 | bigone+=(power(x,j)*CanonicalForm(to_long(rep(coeff(poly,j))))); |
---|
| 234 | } |
---|
[a99e31] | 235 | } |
---|
| 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
| 239 | // poly is immediate |
---|
| 240 | bigone=CanonicalForm(to_long(rep(coeff(poly,0)))); |
---|
[d30633d] | 241 | bigone.mapinto(); |
---|
[a99e31] | 242 | } |
---|
| 243 | return bigone; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
[d30633d] | 247 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 248 | // NAME: convertNTLGF2X2CF // |
---|
| 249 | // // |
---|
| 250 | // DESCRIPTION: // |
---|
| 251 | // Conversion routine for NTL-Type GF2X to Factory-Type canonicalform, // |
---|
| 252 | // the routine is again an optimized special case of the more general // |
---|
| 253 | // conversion to ZZpX. Additionally a variable x is needed as a // |
---|
| 254 | // parameter indicating the main variable of the computed canonicalform. // |
---|
| 255 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 256 | // has a be an arbitrary prime number. // |
---|
| 257 | // // |
---|
| 258 | // INPUT: A canonicalform f, a variable x // |
---|
| 259 | // OUTPUT: The converted Factory-polynomial of type canonicalform, // |
---|
| 260 | // built by the main variable x // |
---|
| 261 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 262 | |
---|
| 263 | CanonicalForm convertNTLGF2X2CF(GF2X poly,Variable x) |
---|
| 264 | { |
---|
[d30633d] | 265 | //printf("convertNTLGF2X2CF\n"); |
---|
[a99e31] | 266 | CanonicalForm bigone; |
---|
| 267 | |
---|
| 268 | if (deg(poly)>0) |
---|
| 269 | { |
---|
| 270 | // poly is non-constant |
---|
| 271 | bigone=0; |
---|
[d30633d] | 272 | bigone.mapinto(); |
---|
| 273 | // Compute the canonicalform coefficient by coefficient, |
---|
| 274 | // bigone summarizes the result. |
---|
| 275 | // In constrast to the more general conversion to ZZpX |
---|
| 276 | // the only possible coefficients are zero |
---|
| 277 | // and one yielding the following simplified loop |
---|
[a99e31] | 278 | for (int j=0;j<deg(poly)+1;j++) |
---|
| 279 | { |
---|
[d30633d] | 280 | if (coeff(poly,j)!=0) bigone+=power(x,j); |
---|
[a99e31] | 281 | // *CanonicalForm(to_long(rep(coeff(poly,j))))) is not necessary any more; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | else |
---|
| 285 | { |
---|
| 286 | // poly is immediate |
---|
| 287 | bigone=CanonicalForm(to_long(rep(coeff(poly,0)))); |
---|
[d30633d] | 288 | bigone.mapinto(); |
---|
[a99e31] | 289 | } |
---|
| 290 | |
---|
| 291 | return bigone; |
---|
| 292 | } |
---|
| 293 | |
---|
[d30633d] | 294 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 295 | // NAME: convertNTLvec_pair_ZZpX_long2FacCFFList // |
---|
| 296 | // // |
---|
| 297 | // DESCRIPTION: // |
---|
| 298 | // Routine for converting a vector of polynomials from ZZpX to // |
---|
| 299 | // a CFFList of Factory. This routine will be used after a successful // |
---|
| 300 | // factorization of NTL to convert the result back to Factory. // |
---|
| 301 | // // |
---|
| 302 | // Additionally a variable x and the computed multiplicity, as a type ZZp // |
---|
| 303 | // of NTL, is needed as parameters indicating the main variable of the // |
---|
| 304 | // computed canonicalform and the multiplicity of the original polynomial. // |
---|
| 305 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 306 | // has a be an arbitrary prime number. // |
---|
| 307 | // // |
---|
| 308 | // INPUT: A vector of polynomials over ZZp of type vec_pair_ZZ_pX_long and // |
---|
| 309 | // a variable x and a multiplicity of type ZZp // |
---|
| 310 | // OUTPUT: The converted list of polynomials of type CFFList, all polynomials // |
---|
| 311 | // have x as their main variable // |
---|
| 312 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 313 | |
---|
| 314 | CFFList convertNTLvec_pair_ZZpX_long2FacCFFList |
---|
| 315 | (vec_pair_ZZ_pX_long e,ZZ_p multi,Variable x) |
---|
[a99e31] | 316 | { |
---|
[d30633d] | 317 | //printf("convertNTLvec_pair_ZZpX_long2FacCFFList\n"); |
---|
[a99e31] | 318 | CFFList rueckgabe; |
---|
| 319 | ZZ_pX polynom; |
---|
| 320 | long exponent; |
---|
| 321 | CanonicalForm bigone; |
---|
| 322 | |
---|
[d30633d] | 323 | // Maybe, e may additionally be sorted with respect to increasing degree of x |
---|
| 324 | // but this is not |
---|
| 325 | //important for the factorization, but nevertheless would take computing time, |
---|
| 326 | // so it is omitted |
---|
[a99e31] | 327 | |
---|
[d30633d] | 328 | // Start by appending the multiplicity |
---|
| 329 | if (!IsOne(multi)) |
---|
| 330 | rueckgabe.append(CFFactor(CanonicalForm(to_long(rep(multi))),1)); |
---|
[a99e31] | 331 | |
---|
| 332 | // Go through the vector e and compute the CFFList |
---|
| 333 | // again bigone summarizes the result |
---|
| 334 | for (int i=e.length()-1;i>=0;i--) |
---|
| 335 | { |
---|
| 336 | rueckgabe.append(CFFactor(convertNTLZZpX2CF(e[i].a,x),e[i].b)); |
---|
| 337 | } |
---|
[d30633d] | 338 | |
---|
[a99e31] | 339 | return rueckgabe; |
---|
| 340 | } |
---|
| 341 | |
---|
[d30633d] | 342 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 343 | // NAME: convertNTLvec_pair_GF2X_long2FacCFFList // |
---|
| 344 | // // |
---|
| 345 | // DESCRIPTION: // |
---|
| 346 | // Routine for converting a vector of polynomials of type GF2X from // |
---|
| 347 | // NTL to a list CFFList of Factory. This routine will be used after a // |
---|
| 348 | // successful factorization of NTL to convert the result back to Factory. // |
---|
| 349 | // As usual this is simply a special case of the more general conversion // |
---|
| 350 | // routine but again speeded up by leaving out unnecessary steps. // |
---|
| 351 | // Additionally a variable x and the computed multiplicity, as type // |
---|
| 352 | // GF2 of NTL, are needed as parameters indicating the main variable of the // |
---|
| 353 | // computed canonicalform and the multiplicity of the original polynomial. // |
---|
| 354 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 355 | // has a be an arbitrary prime number. // |
---|
| 356 | // // |
---|
| 357 | // INPUT: A vector of polynomials over GF2 of type vec_pair_GF2X_long and // |
---|
| 358 | // a variable x and a multiplicity of type GF2 // |
---|
| 359 | // OUTPUT: The converted list of polynomials of type CFFList, all // |
---|
| 360 | // polynomials have x as their main variable // |
---|
| 361 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 362 | |
---|
| 363 | CFFList convertNTLvec_pair_GF2X_long2FacCFFList |
---|
| 364 | (vec_pair_GF2X_long e,GF2 multi,Variable x) |
---|
[a99e31] | 365 | { |
---|
[d30633d] | 366 | //printf("convertNTLvec_pair_GF2X_long2FacCFFList\n"); |
---|
[a99e31] | 367 | CFFList rueckgabe; |
---|
| 368 | GF2X polynom; |
---|
| 369 | long exponent; |
---|
| 370 | CanonicalForm bigone; |
---|
| 371 | |
---|
[d30633d] | 372 | // Maybe, e may additionally be sorted with respect to increasing degree of x |
---|
| 373 | // but this is not |
---|
| 374 | //important for the factorization, but nevertheless would take computing time |
---|
| 375 | // so it is omitted. |
---|
[a99e31] | 376 | |
---|
| 377 | //We do not have to worry about the multiplicity in GF2 since it equals one. |
---|
| 378 | |
---|
| 379 | // Go through the vector e and compute the CFFList |
---|
| 380 | // bigone summarizes the result again |
---|
| 381 | for (int i=e.length()-1;i>=0;i--) |
---|
| 382 | { |
---|
| 383 | bigone=0; |
---|
[d30633d] | 384 | |
---|
[a99e31] | 385 | polynom=e[i].a; |
---|
| 386 | exponent=e[i].b; |
---|
| 387 | for (int j=0;j<deg(polynom)+1;j++) |
---|
| 388 | { |
---|
[d30633d] | 389 | if (coeff(polynom,j)!=0) |
---|
| 390 | bigone += (power(x,j)*CanonicalForm(to_long(rep(coeff(polynom,j))))); |
---|
[a99e31] | 391 | } |
---|
| 392 | |
---|
| 393 | //append the converted polynomial to the CFFList |
---|
| 394 | rueckgabe.append(CFFactor(bigone,exponent)); |
---|
| 395 | } |
---|
[d30633d] | 396 | |
---|
[a99e31] | 397 | return rueckgabe; |
---|
| 398 | } |
---|
| 399 | |
---|
[d30633d] | 400 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 401 | // NAME: convertZZ2CF // |
---|
| 402 | // // |
---|
| 403 | // DESCRIPTION: // |
---|
| 404 | // Routine for conversion of integers represented in NTL as Type ZZ to // |
---|
| 405 | // integers in Factory represented as canonicalform. // |
---|
| 406 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 407 | // has to equal zero. // |
---|
| 408 | // // |
---|
| 409 | // INPUT: The value coefficient of type ZZ that has to be converted // |
---|
| 410 | // OUTPUT: The converted Factory-integer of type canonicalform // |
---|
| 411 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 412 | |
---|
| 413 | CanonicalForm convertZZ2CF(ZZ coefficient) |
---|
[d30633d] | 414 | { |
---|
[a99e31] | 415 | long coeff_long; |
---|
[b1476d0] | 416 | //CanonicalForm tmp=0; |
---|
| 417 | char stringtemp[5000]=""; |
---|
| 418 | char stringtemp2[5000]=""; |
---|
| 419 | char dummy[2]; |
---|
[a99e31] | 420 | int minusremainder=0; |
---|
[d30633d] | 421 | |
---|
[a99e31] | 422 | coeff_long=to_long(coefficient); |
---|
| 423 | |
---|
| 424 | //Test whether coefficient can be represented as an immediate integer in Factory |
---|
[d30633d] | 425 | if ( (NumBits(coefficient)<=NTL_ZZ_NBITS) |
---|
| 426 | && (coeff_long>MINIMMEDIATE) |
---|
| 427 | && (coeff_long<MAXIMMEDIATE)) |
---|
| 428 | { |
---|
[a99e31] | 429 | // coefficient is immediate --> return the coefficient as canonicalform |
---|
[d30633d] | 430 | return CanonicalForm(coeff_long); |
---|
[a99e31] | 431 | } |
---|
[d30633d] | 432 | else |
---|
| 433 | { |
---|
[a99e31] | 434 | // coefficient is not immediate (gmp-number) |
---|
[d30633d] | 435 | |
---|
[a99e31] | 436 | // convert coefficient to char* (input for gmp) |
---|
[b1476d0] | 437 | dummy[1]='\0'; |
---|
[d30633d] | 438 | |
---|
[a99e31] | 439 | if (coefficient<0) |
---|
[d30633d] | 440 | { |
---|
[a99e31] | 441 | // negate coefficient, but store the sign in minusremainder |
---|
| 442 | minusremainder=1; |
---|
| 443 | coefficient=-coefficient; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | while (coefficient>9) |
---|
| 447 | { |
---|
| 448 | ZZ quotient,remaind; |
---|
[d30633d] | 449 | ZZ ten;ten=10; |
---|
[a99e31] | 450 | DivRem(quotient,remaind,coefficient,ten); |
---|
[b1476d0] | 451 | dummy[0]=(char)(to_long(remaind)+'0'); |
---|
| 452 | //tmp*=10; tmp+=to_long(remaind); |
---|
[d30633d] | 453 | |
---|
[b1476d0] | 454 | strcat(stringtemp,dummy); |
---|
[d30633d] | 455 | |
---|
[a99e31] | 456 | coefficient=quotient; |
---|
| 457 | } |
---|
| 458 | //built up the string in dummy[0] |
---|
[b1476d0] | 459 | dummy[0]=(char)(to_long(coefficient)+'0'); |
---|
| 460 | strcat(stringtemp,dummy); |
---|
| 461 | //tmp*=10; tmp+=to_long(coefficient); |
---|
[d30633d] | 462 | |
---|
[a99e31] | 463 | if (minusremainder==1) |
---|
| 464 | { |
---|
| 465 | //Check whether coefficient has been negative at the start of the procedure |
---|
[b1476d0] | 466 | stringtemp2[0]='-'; |
---|
| 467 | //tmp*=(-1); |
---|
[a99e31] | 468 | } |
---|
[d30633d] | 469 | |
---|
[a99e31] | 470 | //reverse the list to obtain the correct string |
---|
[b1476d0] | 471 | int len=strlen(stringtemp); |
---|
| 472 | for (int i=len-1;i>=0;i--) |
---|
| 473 | { |
---|
| 474 | stringtemp2[len-i-1+minusremainder]=stringtemp[i]; |
---|
| 475 | } |
---|
| 476 | stringtemp2[len+minusremainder]='\0'; |
---|
[a99e31] | 477 | } |
---|
| 478 | |
---|
| 479 | //convert the string to canonicalform using the char*-Constructor |
---|
[b1476d0] | 480 | return CanonicalForm(stringtemp2); |
---|
| 481 | //return tmp; |
---|
[a99e31] | 482 | } |
---|
| 483 | |
---|
[d30633d] | 484 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 485 | // NAME: convertFacCF2NTLZZX // |
---|
| 486 | // // |
---|
| 487 | // DESCRIPTION: // |
---|
| 488 | // Routine for conversion of canonicalforms in Factory to polynomials // |
---|
| 489 | // of type ZZX of NTL. To guarantee the correct execution of the // |
---|
| 490 | // algorithm the characteristic has to equal zero. // |
---|
| 491 | // // |
---|
| 492 | // INPUT: The canonicalform that has to be converted // |
---|
| 493 | // OUTPUT: The converted NTL-polynom of type ZZX // |
---|
| 494 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 495 | |
---|
| 496 | ZZX convertFacCF2NTLZZX(CanonicalForm f) |
---|
[d30633d] | 497 | { |
---|
[a99e31] | 498 | ZZX ntl_poly; |
---|
| 499 | |
---|
| 500 | CFIterator i; |
---|
| 501 | i=f; |
---|
| 502 | |
---|
| 503 | int j=0; |
---|
| 504 | int NTLcurrentExp=i.exp(); |
---|
| 505 | int largestExp=i.exp(); |
---|
| 506 | int k; |
---|
| 507 | |
---|
| 508 | //set the length of the NTL-polynomial |
---|
| 509 | ntl_poly.SetMaxLength(largestExp+1); |
---|
[d30633d] | 510 | |
---|
[a99e31] | 511 | //Go through the coefficients of the canonicalform and build up the NTL-polynomial |
---|
[d30633d] | 512 | for (;i.hasTerms();i++) |
---|
[a99e31] | 513 | { |
---|
| 514 | for (k=NTLcurrentExp;k>i.exp();k--) |
---|
| 515 | { |
---|
| 516 | SetCoeff(ntl_poly,k,0); |
---|
| 517 | } |
---|
| 518 | NTLcurrentExp=i.exp(); |
---|
| 519 | |
---|
| 520 | if (!i.coeff().isImm()) |
---|
[d30633d] | 521 | { |
---|
| 522 | //Coefficient is a gmp-number |
---|
| 523 | mpz_t gmp_val; |
---|
| 524 | ZZ temp; |
---|
| 525 | char* stringtemp; |
---|
| 526 | |
---|
| 527 | gmp_val[0]=getmpi(i.coeff().getval()); |
---|
| 528 | int l=mpz_sizeinbase(gmp_val,10)+2; |
---|
| 529 | stringtemp=(char*)omAlloc(l); |
---|
| 530 | stringtemp=mpz_get_str(stringtemp,10,gmp_val); |
---|
| 531 | conv(temp,stringtemp); |
---|
| 532 | omFreeSize(stringtemp,l); |
---|
| 533 | |
---|
| 534 | //set the computed coefficient |
---|
| 535 | SetCoeff(ntl_poly,NTLcurrentExp,temp); |
---|
| 536 | } |
---|
[a99e31] | 537 | else |
---|
| 538 | { |
---|
| 539 | //Coefficient is immediate --> use its value |
---|
| 540 | SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval()); |
---|
| 541 | } |
---|
[d30633d] | 542 | |
---|
[a99e31] | 543 | NTLcurrentExp--; |
---|
| 544 | } |
---|
| 545 | for (k=NTLcurrentExp;k>=0;k--) |
---|
[d30633d] | 546 | { |
---|
| 547 | SetCoeff(ntl_poly,k,0); |
---|
| 548 | } |
---|
[a99e31] | 549 | |
---|
| 550 | //normalize the polynomial |
---|
| 551 | ntl_poly.normalize(); |
---|
[d30633d] | 552 | |
---|
[a99e31] | 553 | return ntl_poly; |
---|
| 554 | } |
---|
| 555 | |
---|
[d30633d] | 556 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 557 | // NAME: convertNTLvec_pair_ZZX_long2FacCFFList // |
---|
| 558 | // // |
---|
| 559 | // DESCRIPTION: // |
---|
| 560 | // Routine for converting a vector of polynomials from ZZ to a list // |
---|
| 561 | // CFFList of Factory. This routine will be used after a successful // |
---|
| 562 | // factorization of NTL to convert the result back to Factory. // |
---|
| 563 | // Additionally a variable x and the computed multiplicity, as a type // |
---|
| 564 | // ZZ of NTL, is needed as parameters indicating the main variable of the // |
---|
| 565 | // computed canonicalform and the multiplicity of the original polynomial. // |
---|
| 566 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 567 | // has to equal zero. // |
---|
| 568 | // // |
---|
| 569 | // INPUT: A vector of polynomials over ZZ of type vec_pair_ZZX_long and // |
---|
| 570 | // a variable x and a multiplicity of type ZZ // |
---|
| 571 | // OUTPUT: The converted list of polynomials of type CFFList, all // |
---|
| 572 | // have x as their main variable // |
---|
| 573 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 574 | |
---|
| 575 | CFFList convertNTLvec_pair_ZZX_long2FacCFFList(vec_pair_ZZX_long e,ZZ multi,Variable x) |
---|
| 576 | { |
---|
| 577 | CFFList rueckgabe; |
---|
| 578 | ZZX polynom; |
---|
| 579 | long exponent; |
---|
| 580 | CanonicalForm bigone; |
---|
| 581 | |
---|
| 582 | // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not |
---|
| 583 | //important for the factorization, but nevertheless would take computing time, so it is omitted |
---|
| 584 | |
---|
[d30633d] | 585 | |
---|
| 586 | // Start by appending the multiplicity |
---|
| 587 | |
---|
[cae0b6] | 588 | //if (!IsOne(multi)) |
---|
[d30633d] | 589 | rueckgabe.append(CFFactor(convertZZ2CF(multi),1)); |
---|
[a99e31] | 590 | |
---|
| 591 | // Go through the vector e and build up the CFFList |
---|
| 592 | // As usual bigone summarizes the result |
---|
| 593 | for (int i=e.length()-1;i>=0;i--) |
---|
| 594 | { |
---|
| 595 | bigone=0; |
---|
| 596 | ZZ coefficient; |
---|
| 597 | polynom=e[i].a; |
---|
| 598 | exponent=e[i].b; |
---|
| 599 | long coeff_long; |
---|
[d30633d] | 600 | |
---|
[a99e31] | 601 | for (int j=0;j<deg(polynom)+1;j++) |
---|
| 602 | { |
---|
| 603 | coefficient=coeff(polynom,j); |
---|
| 604 | if (!IsZero(coefficient)) |
---|
| 605 | { |
---|
[d30633d] | 606 | bigone += (power(x,j)*convertZZ2CF(coefficient)); |
---|
| 607 | } |
---|
[a99e31] | 608 | } |
---|
| 609 | |
---|
| 610 | //append the converted polynomial to the list |
---|
| 611 | rueckgabe.append(CFFactor(bigone,exponent)); |
---|
| 612 | } |
---|
| 613 | //return the converted list |
---|
| 614 | return rueckgabe; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | |
---|
[d30633d] | 618 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 619 | // NAME: convertNTLZZpX2CF // |
---|
| 620 | // // |
---|
| 621 | // DESCRIPTION: // |
---|
| 622 | // Routine for conversion of elements of arbitrary extensions of ZZp, // |
---|
| 623 | // having type ZZpE, of NTL to their corresponding values of type // |
---|
| 624 | // canonicalform in Factory. // |
---|
| 625 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 626 | // has to be an arbitrary prime number and Factory has to compute in an // |
---|
| 627 | // extension of F_p. // |
---|
| 628 | // // |
---|
| 629 | // INPUT: The coefficient of type ZZpE and the variable x indicating the main// |
---|
| 630 | // variable of the computed canonicalform // |
---|
| 631 | // OUTPUT: The converted value of coefficient as type canonicalform // |
---|
| 632 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 633 | |
---|
| 634 | CanonicalForm convertNTLZZpE2CF(ZZ_pE coefficient,Variable x) |
---|
| 635 | { |
---|
| 636 | return convertNTLZZpX2CF(rep(coefficient),x); |
---|
| 637 | } |
---|
| 638 | |
---|
[d30633d] | 639 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 640 | // NAME: convertNTLvec_pair_ZZpEX_long2FacCFFList // |
---|
| 641 | // // |
---|
| 642 | // DESCRIPTION: // |
---|
| 643 | // Routine for converting a vector of polynomials from ZZpEX to a CFFList // |
---|
| 644 | // of Factory. This routine will be used after a successful factorization // |
---|
| 645 | // of NTL to convert the result back to Factory. // |
---|
| 646 | // Additionally a variable x and the computed multiplicity, as a type // |
---|
| 647 | // ZZpE of NTL, is needed as parameters indicating the main variable of the // |
---|
| 648 | // computed canonicalform and the multiplicity of the original polynomial. // |
---|
| 649 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 650 | // has a be an arbitrary prime number p and computations have to be done // |
---|
| 651 | // in an extention of F_p. // |
---|
| 652 | // // |
---|
| 653 | // INPUT: A vector of polynomials over ZZpE of type vec_pair_ZZ_pEX_long and // |
---|
| 654 | // a variable x and a multiplicity of type ZZpE // |
---|
| 655 | // OUTPUT: The converted list of polynomials of type CFFList, all polynomials // |
---|
| 656 | // have x as their main variable // |
---|
| 657 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 658 | |
---|
| 659 | CFFList convertNTLvec_pair_ZZpEX_long2FacCFFList(vec_pair_ZZ_pEX_long e,ZZ_pE multi,Variable x,Variable alpha) |
---|
| 660 | { |
---|
| 661 | CFFList rueckgabe; |
---|
| 662 | ZZ_pEX polynom; |
---|
| 663 | long exponent; |
---|
| 664 | CanonicalForm bigone; |
---|
| 665 | |
---|
| 666 | // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not |
---|
| 667 | //important for the factorization, but nevertheless would take computing time, so it is omitted |
---|
[d30633d] | 668 | |
---|
[a99e31] | 669 | |
---|
| 670 | // Start by appending the multiplicity |
---|
[d30633d] | 671 | if (!IsOne(multi)) |
---|
[b1476d0] | 672 | rueckgabe.append(CFFactor(convertNTLZZpE2CF(multi,alpha),1)); |
---|
[d30633d] | 673 | |
---|
[a99e31] | 674 | |
---|
| 675 | // Go through the vector e and build up the CFFList |
---|
[d30633d] | 676 | // As usual bigone summarizes the result during every loop |
---|
| 677 | for (int i=e.length()-1;i>=0;i--) |
---|
| 678 | { |
---|
| 679 | bigone=0; |
---|
[a99e31] | 680 | |
---|
[d30633d] | 681 | polynom=e[i].a; |
---|
| 682 | exponent=e[i].b; |
---|
[a99e31] | 683 | |
---|
[d30633d] | 684 | for (int j=0;j<deg(polynom)+1;j++) |
---|
| 685 | { |
---|
| 686 | if (IsOne(coeff(polynom,j))) |
---|
| 687 | { |
---|
| 688 | bigone+=power(x,j); |
---|
| 689 | } |
---|
| 690 | else |
---|
| 691 | { |
---|
| 692 | CanonicalForm coefficient=convertNTLZZpE2CF(coeff(polynom,j),alpha); |
---|
| 693 | if (coeff(polynom,j)!=0) |
---|
| 694 | { |
---|
| 695 | bigone += (power(x,j)*coefficient); |
---|
| 696 | } |
---|
| 697 | } |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | //append the computed polynomials together with its exponent to the CFFList |
---|
| 701 | rueckgabe.append(CFFactor(bigone,exponent)); |
---|
| 702 | |
---|
| 703 | } |
---|
| 704 | //return the computed CFFList |
---|
[a99e31] | 705 | return rueckgabe; |
---|
| 706 | } |
---|
| 707 | |
---|
[d30633d] | 708 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 709 | // NAME: convertNTLGF2E2CF // |
---|
| 710 | // // |
---|
| 711 | // DESCRIPTION: // |
---|
| 712 | // Routine for conversion of elements of extensions of GF2, having type // |
---|
| 713 | // GF2E, of NTL to their corresponding values of type canonicalform in // |
---|
| 714 | // Factory. // |
---|
| 715 | // To guarantee the correct execution of the algorithm, the characteristic // |
---|
| 716 | // must equal two and Factory has to compute in an extension of F_2. // |
---|
| 717 | // As usual this is an optimized special case of the more general conversion // |
---|
| 718 | // routine from ZZpE to Factory. // |
---|
| 719 | // // |
---|
| 720 | // INPUT: The coefficient of type GF2E and the variable x indicating the // |
---|
| 721 | // main variable of the computed canonicalform // |
---|
| 722 | // OUTPUT: The converted value of coefficient as type canonicalform // |
---|
| 723 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 724 | |
---|
| 725 | CanonicalForm convertNTLGF2E2CF(GF2E coefficient,Variable x) |
---|
| 726 | { |
---|
| 727 | return convertNTLGF2X2CF(rep(coefficient),x); |
---|
| 728 | } |
---|
| 729 | |
---|
[d30633d] | 730 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 731 | // NAME: convertNTLvec_pair_GF2EX_long2FacCFFList // |
---|
| 732 | // // |
---|
| 733 | // DESCRIPTION: // |
---|
| 734 | // Routine for converting a vector of polynomials from GF2EX to a CFFList // |
---|
| 735 | // of Factory. This routine will be used after a successful factorization // |
---|
| 736 | // of NTL to convert the result back to Factory. // |
---|
| 737 | // This is a special, but optimized case of the more general conversion // |
---|
| 738 | // from ZZpE to canonicalform. // |
---|
| 739 | // Additionally a variable x and the computed multiplicity, as a type GF2E // |
---|
| 740 | // of NTL, is needed as parameters indicating the main variable of the // |
---|
| 741 | // computed canonicalform and the multiplicity of the original polynomial. // |
---|
| 742 | // To guarantee the correct execution of the algorithm the characteristic // |
---|
| 743 | // has to equal two and computations have to be done in an extention of F_2. // |
---|
| 744 | // // |
---|
| 745 | // INPUT: A vector of polynomials over GF2E of type vec_pair_GF2EX_long and // |
---|
| 746 | // a variable x and a multiplicity of type GF2E // |
---|
| 747 | // OUTPUT: The converted list of polynomials of type CFFList, all polynomials // |
---|
| 748 | // have x as their main variable // |
---|
| 749 | //////////////////////////////////////////////////////////////////////////////// |
---|
[a99e31] | 750 | |
---|
| 751 | CFFList convertNTLvec_pair_GF2EX_long2FacCFFList(vec_pair_GF2EX_long e,GF2E multi,Variable x,Variable alpha) |
---|
| 752 | { |
---|
| 753 | CFFList rueckgabe; |
---|
| 754 | GF2EX polynom; |
---|
| 755 | long exponent; |
---|
| 756 | CanonicalForm bigone; |
---|
| 757 | |
---|
| 758 | // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not |
---|
| 759 | //important for the factorization, but nevertheless would take computing time, so it is omitted |
---|
[d30633d] | 760 | |
---|
[a99e31] | 761 | // multiplicity is always one, so we do not have to worry about that |
---|
| 762 | |
---|
| 763 | // Go through the vector e and build up the CFFList |
---|
[d30633d] | 764 | // As usual bigone summarizes the result during every loop |
---|
| 765 | for (int i=e.length()-1;i>=0;i--) |
---|
| 766 | { |
---|
[a99e31] | 767 | bigone=0; |
---|
[d30633d] | 768 | |
---|
[a99e31] | 769 | polynom=e[i].a; |
---|
| 770 | exponent=e[i].b; |
---|
| 771 | |
---|
| 772 | for (int j=0;j<deg(polynom)+1;j++) |
---|
| 773 | { |
---|
| 774 | if (IsOne(coeff(polynom,j))) |
---|
[d30633d] | 775 | { |
---|
| 776 | bigone+=power(x,j); |
---|
[a99e31] | 777 | } |
---|
[d30633d] | 778 | else |
---|
| 779 | { |
---|
[a99e31] | 780 | CanonicalForm coefficient=convertNTLGF2E2CF(coeff(polynom,j),alpha); |
---|
[d30633d] | 781 | if (coeff(polynom,j)!=0) |
---|
| 782 | { |
---|
| 783 | bigone += (power(x,j)*coefficient); |
---|
[a99e31] | 784 | } |
---|
| 785 | } |
---|
| 786 | } |
---|
[d30633d] | 787 | |
---|
[a99e31] | 788 | // append the computed polynomial together with its multiplicity |
---|
| 789 | rueckgabe.append(CFFactor(bigone,exponent)); |
---|
[d30633d] | 790 | |
---|
[a99e31] | 791 | } |
---|
| 792 | // return the computed CFFList |
---|
| 793 | return rueckgabe; |
---|
| 794 | } |
---|
[d30633d] | 795 | |
---|
| 796 | //////////////////////////////////////////////////// |
---|
| 797 | // CanonicalForm in Z_2(a)[X] to NTL GF2EX // |
---|
| 798 | //////////////////////////////////////////////////// |
---|
| 799 | GF2EX convertFacCF2NTLGF2EX(CanonicalForm f,ZZ_pX mipo) {} |
---|
| 800 | //////////////////////////////////////////////////// |
---|
| 801 | // CanonicalForm in Z_p(a)[X] to NTL ZZ_pEX // |
---|
| 802 | //////////////////////////////////////////////////// |
---|
| 803 | ZZ_pEX convertFacCF2NTLZZ_pEX(CanonicalForm f, ZZ_pX mipo) |
---|
| 804 | { |
---|
| 805 | ZZ_pE::init(mipo); |
---|
| 806 | ZZ_pEX result; |
---|
| 807 | CFIterator i; |
---|
| 808 | i=f; |
---|
| 809 | |
---|
| 810 | int j=0; |
---|
| 811 | int NTLcurrentExp=i.exp(); |
---|
| 812 | int largestExp=i.exp(); |
---|
| 813 | int k; |
---|
| 814 | |
---|
| 815 | result.SetMaxLength(largestExp+1); |
---|
| 816 | for(;i.hasTerms();i++) |
---|
| 817 | { |
---|
| 818 | for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0); |
---|
| 819 | NTLcurrentExp=i.exp(); |
---|
| 820 | CanonicalForm c=i.coeff(); |
---|
| 821 | ZZ_pX cc=convertFacCF2NTLZZpX(c); |
---|
| 822 | //ZZ_pE ccc; |
---|
| 823 | //conv(ccc,cc); |
---|
| 824 | SetCoeff(result,NTLcurrentExp,to_ZZ_pE(cc)); |
---|
| 825 | NTLcurrentExp--; |
---|
| 826 | } |
---|
| 827 | for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0); |
---|
| 828 | result.normalize(); |
---|
| 829 | return result; |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | |
---|
[a99e31] | 833 | #endif |
---|