source: git/factory/NTLconvert.cc @ c1b52b

spielwiese
Last change on this file since c1b52b was 9084a3, checked in by Hans Schoenemann <hannes@…>, 4 years ago
fix: univariate factorization over Z/p(alpha) via NTL
  • Property mode set to 100644
File size: 34.8 KB
Line 
1
2#include "config.h"
3
4#include "cf_assert.h"
5
6#include "cf_defs.h"
7#include "canonicalform.h"
8#include "cf_iter.h"
9#include "fac_sqrfree.h"
10#include "cf_algorithm.h"
11
12#ifdef HAVE_NTL
13#ifndef NOSTREAMIO
14#ifdef HAVE_CSTDIO
15#include <cstdio>
16#else
17#include <stdio.h>
18#endif
19#endif
20#include <string.h>
21#include <NTL/ZZXFactoring.h>
22#include <NTL/ZZ_pXFactoring.h>
23#include <NTL/lzz_pXFactoring.h>
24#include <NTL/GF2XFactoring.h>
25#include <NTL/ZZ_pEXFactoring.h>
26#include <NTL/lzz_pEXFactoring.h>
27#include <NTL/GF2EXFactoring.h>
28#include <NTL/tools.h>
29#include <NTL/mat_ZZ.h>
30#include <NTL/version.h>
31#include "int_int.h"
32#include <limits.h>
33#include "NTLconvert.h"
34
35#ifdef HAVE_OMALLOC
36#define Alloc(L) omAlloc(L)
37#define Free(A,L) omFreeSize(A,L)
38#else
39#define Alloc(L) malloc(L)
40#define Free(A,L) free(A)
41#endif
42
43void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
44
45
46VAR long fac_NTL_char = -1;         // the current characterstic for NTL calls
47                                // -1: undefined
48#ifdef NTL_CLIENT               // in <NTL/tools.h>: using of name space NTL
49NTL_CLIENT
50#endif
51
52////////////////////////////////////////////////////////////////////////////////
53/// NAME: convertFacCF2NTLZZpX
54///
55/// DESCRIPTION:
56/// Conversion routine for Factory-type canonicalform into ZZpX of NTL,
57/// i.e. polynomials over F_p. As a precondition for correct execution,
58/// the characteristic has to a a prime number.
59///
60/// INPUT:  A canonicalform f
61/// OUTPUT: The converted NTL-polynomial over F_p of type ZZpX
62////////////////////////////////////////////////////////////////////////////////
63
64ZZ_pX convertFacCF2NTLZZpX(const CanonicalForm & f)
65{
66  ZZ_pX ntl_poly;
67
68  CFIterator i;
69  i=f;
70
71  int NTLcurrentExp=i.exp();
72  int largestExp=i.exp();
73  int k;
74
75  // we now build up the NTL-polynomial
76  ntl_poly.SetMaxLength(largestExp+1);
77
78  for (;i.hasTerms();i++)
79  {
80    for (k=NTLcurrentExp;k>i.exp();k--)
81    {
82      SetCoeff(ntl_poly,k,0);
83    }
84    NTLcurrentExp=i.exp();
85
86    SetCoeff(ntl_poly,NTLcurrentExp,to_ZZ_p (convertFacCF2NTLZZ (i.coeff())));
87    NTLcurrentExp--;
88  }
89
90  //Set the remaining coefficients of ntl_poly to zero.
91  // This is necessary, because NTL internally
92  // also stores powers with zero coefficient,
93  // whereas factory stores tuples of degree and coefficient
94  //leaving out tuples if the coefficient equals zero
95  for (k=NTLcurrentExp;k>=0;k--)
96  {
97    SetCoeff(ntl_poly,k,0);
98  }
99
100  //normalize the polynomial and return it
101  ntl_poly.normalize();
102
103  return ntl_poly;
104}
105zz_pX convertFacCF2NTLzzpX(const CanonicalForm & f)
106{
107  zz_pX ntl_poly;
108
109  CFIterator i;
110  i=f;
111
112  int NTLcurrentExp=i.exp();
113  int largestExp=i.exp();
114  int k;
115
116  // we now build up the NTL-polynomial
117  ntl_poly.SetMaxLength(largestExp+1);
118
119  for (;i.hasTerms();i++)
120  {
121    for (k=NTLcurrentExp;k>i.exp();k--)
122    {
123      SetCoeff(ntl_poly,k,0);
124    }
125    NTLcurrentExp=i.exp();
126
127    CanonicalForm c=i.coeff();
128    if (!c.isImm()) c=c.mapinto(); //c%= getCharacteristic();
129    if (!c.isImm())
130    {  //This case will never happen if the characteristic is in fact a prime
131       // number, since all coefficients are represented as immediates
132       out_cf("f:->",f,"\n");
133       out_cf("c:->",c,"\n");
134       #ifndef NOSTREAMIO
135       cout<<"convertFacCF2NTLzz_pX: coefficient not immediate! : "<<f<<"\n";
136       #else
137       //NTL_SNS
138       printf("convertFacCF2NTLzz_pX: coefficient not immediate!, char=%d\n",
139              getCharacteristic());
140       #endif
141       NTL_SNS exit(1);
142    }
143    else
144    {
145      SetCoeff(ntl_poly,NTLcurrentExp,c.intval());
146    }
147    NTLcurrentExp--;
148  }
149
150  //Set the remaining coefficients of ntl_poly to zero.
151  // This is necessary, because NTL internally
152  // also stores powers with zero coefficient,
153  // whereas factory stores tuples of degree and coefficient
154  //leaving out tuples if the coefficient equals zero
155  for (k=NTLcurrentExp;k>=0;k--)
156  {
157    SetCoeff(ntl_poly,k,0);
158  }
159
160  //normalize the polynomial and return it
161  ntl_poly.normalize();
162
163  return ntl_poly;
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// NAME: convertFacCF2NTLGF2X
168///
169/// DESCRIPTION:
170/// Conversion routine for Factory-type canonicalform into GF2X of NTL,
171/// i.e. polynomials over F_2. As precondition for correct execution,
172/// the characteristic must equal two.
173/// This is a special case of the more general conversion routine for
174/// canonicalform to ZZpX. It is included because NTL provides additional
175/// support and faster algorithms over F_2, moreover the conversion code
176/// can be optimized, because certain steps are either completely obsolent
177/// (like normalizing the polynomial) or they can be made significantly
178/// faster (like building up the NTL-polynomial).
179///
180/// INPUT:  A canonicalform f
181/// OUTPUT: The converted NTL-polynomial over F_2 of type GF2X
182////////////////////////////////////////////////////////////////////////////////
183
184GF2X convertFacCF2NTLGF2X(const CanonicalForm & f)
185{
186  //printf("convertFacCF2NTLGF2X\n");
187  GF2X ntl_poly;
188
189  CFIterator i;
190  i=f;
191
192  int NTLcurrentExp=i.exp();
193  int largestExp=i.exp();
194  int k;
195
196  //building the NTL-polynomial
197  ntl_poly.SetMaxLength(largestExp+1);
198
199  for (;i.hasTerms();i++)
200  {
201
202    for (k=NTLcurrentExp;k>i.exp();k--)
203    {
204      SetCoeff(ntl_poly,k,0);
205    }
206    NTLcurrentExp=i.exp();
207
208    if (!i.coeff().isImm()) i.coeff()=i.coeff().mapinto();
209    if (!i.coeff().isImm())
210    {
211      #ifndef NOSTREAMIO
212      cout<<"convertFacCF2NTLGF2X: coefficient not immediate! : " << f << "\n";
213      #else
214      //NTL_SNS
215      printf("convertFacCF2NTLGF2X: coefficient not immediate!");
216      #endif
217      NTL_SNS exit(1);
218    }
219    else
220    {
221      SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval());
222    }
223    NTLcurrentExp--;
224  }
225  for (k=NTLcurrentExp;k>=0;k--)
226  {
227    SetCoeff(ntl_poly,k,0);
228  }
229  //normalization is not necessary of F_2
230
231  return ntl_poly;
232}
233
234
235////////////////////////////////////////////////////////////////////////////////
236/// NAME: convertNTLZZpX2CF
237///
238/// DESCRIPTION:
239/// Conversion routine for NTL-Type ZZpX to Factory-Type canonicalform.
240/// Additionally a variable x is needed as a parameter indicating the
241/// main variable of the computed canonicalform. To guarantee the correct
242/// execution of the algorithm, the characteristic has a be an arbitrary
243/// prime number.
244///
245/// INPUT:  A canonicalform f, a variable x
246/// OUTPUT: The converted Factory-polynomial of type canonicalform,
247///         built by the main variable x
248////////////////////////////////////////////////////////////////////////////////
249
250CanonicalForm convertNTLZZpX2CF(const ZZ_pX & poly,const Variable & x)
251{
252  return convertNTLZZX2CF (to_ZZX (poly), x);
253}
254
255CanonicalForm convertNTLzzpX2CF(const zz_pX & poly,const Variable & x)
256{
257  //printf("convertNTLzzpX2CF\n");
258  CanonicalForm bigone;
259
260
261  if (deg(poly)>0)
262  {
263    // poly is non-constant
264    bigone=0;
265    bigone.mapinto();
266    // Compute the canonicalform coefficient by coefficient,
267    // bigone summarizes the result.
268    for (int j=0;j<=deg(poly);j++)
269    {
270      if (coeff(poly,j)!=0)
271      {
272        bigone+=(power(x,j)*CanonicalForm(to_long(rep(coeff(poly,j)))));
273      }
274    }
275  }
276  else
277  {
278    // poly is immediate
279    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
280    bigone.mapinto();
281  }
282  return bigone;
283}
284
285CanonicalForm convertNTLZZX2CF(const ZZX & polynom,const Variable & x)
286{
287  //printf("convertNTLZZX2CF\n");
288  CanonicalForm bigone;
289
290  // Go through the vector e and build up the CFFList
291  // As usual bigone summarizes the result
292  bigone=0;
293  ZZ coefficient;
294
295  for (int j=0;j<=deg(polynom);j++)
296  {
297    coefficient=coeff(polynom,j);
298    if (!IsZero(coefficient))
299    {
300      bigone += (power(x,j)*convertZZ2CF(coefficient));
301    }
302  }
303  return bigone;
304}
305
306////////////////////////////////////////////////////////////////////////////////
307/// NAME: convertNTLGF2X2CF
308///
309/// DESCRIPTION:
310/// Conversion routine for NTL-Type GF2X to Factory-Type canonicalform,
311/// the routine is again an optimized special case of the more general
312/// conversion to ZZpX. Additionally a variable x is needed as a
313/// parameter indicating the main variable of the computed canonicalform.
314/// To guarantee the correct execution of the algorithm the characteristic
315/// has a be an arbitrary prime number.
316///
317/// INPUT:  A canonicalform f, a variable x
318/// OUTPUT: The converted Factory-polynomial of type canonicalform,
319///         built by the main variable x
320////////////////////////////////////////////////////////////////////////////////
321
322CanonicalForm convertNTLGF2X2CF(const GF2X & poly,const Variable & x)
323{
324  //printf("convertNTLGF2X2CF\n");
325  CanonicalForm bigone;
326
327  if (deg(poly)>0)
328  {
329    // poly is non-constant
330    bigone=0;
331    bigone.mapinto();
332    // Compute the canonicalform coefficient by coefficient,
333    // bigone summarizes the result.
334    // In constrast to the more general conversion to ZZpX
335    // the only possible coefficients are zero
336    // and one yielding the following simplified loop
337    for (int j=0;j<=deg(poly);j++)
338    {
339      if (coeff(poly,j)!=0) bigone+=power(x,j);
340     // *CanonicalForm(to_long(rep(coeff(poly,j))))) is not necessary any more;
341    }
342  }
343  else
344  {
345    // poly is immediate
346    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
347    bigone.mapinto();
348  }
349
350  return bigone;
351}
352
353////////////////////////////////////////////////////////////////////////////////
354/// NAME: convertNTLvec_pair_ZZpX_long2FacCFFList
355///
356/// DESCRIPTION:
357/// Routine for converting a vector of polynomials from ZZpX to
358/// a CFFList of Factory. This routine will be used after a successful
359/// factorization of NTL to convert the result back to Factory.
360///
361/// Additionally a variable x and the computed multiplicity, as a type ZZp
362/// of NTL, is needed as parameters indicating the main variable of the
363/// computed canonicalform and the multiplicity of the original polynomial.
364/// To guarantee the correct execution of the algorithm the characteristic
365/// has a be an arbitrary prime number.
366///
367/// INPUT:  A vector of polynomials over ZZp of type vec_pair_ZZ_pX_long and
368///         a variable x and a multiplicity of type ZZp
369/// OUTPUT: The converted list of polynomials of type CFFList, all polynomials
370///         have x as their main variable
371////////////////////////////////////////////////////////////////////////////////
372
373CFFList convertNTLvec_pair_ZZpX_long2FacCFFList
374                                  (const vec_pair_ZZ_pX_long & e,const ZZ_p & multi,const Variable & x)
375{
376  //printf("convertNTLvec_pair_ZZpX_long2FacCFFList\n");
377  CFFList result;
378  ZZ_pX polynom;
379  CanonicalForm bigone;
380
381  // Maybe, e may additionally be sorted with respect to increasing degree of x
382  // but this is not
383  //important for the factorization, but nevertheless would take computing time,
384  // so it is omitted
385
386
387  // Go through the vector e and compute the CFFList
388  // again bigone summarizes the result
389  for (int i=e.length()-1;i>=0;i--)
390  {
391    result.append(CFFactor(convertNTLZZpX2CF(e[i].a,x),e[i].b));
392  }
393  // the multiplicity at pos 1
394  if (!IsOne(multi))
395    result.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
396  return result;
397}
398CFFList convertNTLvec_pair_zzpX_long2FacCFFList
399                                  (const vec_pair_zz_pX_long & e,const zz_p multi,const Variable & x)
400{
401  //printf("convertNTLvec_pair_zzpX_long2FacCFFList\n");
402  CFFList result;
403  zz_pX polynom;
404  CanonicalForm bigone;
405
406  // Maybe, e may additionally be sorted with respect to increasing degree of x
407  // but this is not
408  //important for the factorization, but nevertheless would take computing time,
409  // so it is omitted
410
411
412  // Go through the vector e and compute the CFFList
413  // again bigone summarizes the result
414  for (int i=e.length()-1;i>=0;i--)
415  {
416    result.append(CFFactor(convertNTLzzpX2CF(e[i].a,x),e[i].b));
417  }
418  // the multiplicity at pos 1
419  if (!IsOne(multi))
420    result.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
421  return result;
422}
423
424////////////////////////////////////////////////////////////////////////////////
425/// NAME: convertNTLvec_pair_GF2X_long2FacCFFList
426///
427/// DESCRIPTION:
428/// Routine for converting a vector of polynomials of type GF2X from
429/// NTL to a list CFFList of Factory. This routine will be used after a
430/// successful factorization of NTL to convert the result back to Factory.
431/// As usual this is simply a special case of the more general conversion
432/// routine but again speeded up by leaving out unnecessary steps.
433/// Additionally a variable x and the computed multiplicity, as type
434/// GF2 of NTL, are needed as parameters indicating the main variable of the
435/// computed canonicalform and the multiplicity of the original polynomial.
436/// To guarantee the correct execution of the algorithm the characteristic
437/// has a be an arbitrary prime number.
438///
439/// INPUT:  A vector of polynomials over GF2 of type vec_pair_GF2X_long and
440///         a variable x and a multiplicity of type GF2
441/// OUTPUT: The converted list of polynomials of type CFFList, all
442///         polynomials have x as their main variable
443////////////////////////////////////////////////////////////////////////////////
444
445CFFList convertNTLvec_pair_GF2X_long2FacCFFList
446    (const vec_pair_GF2X_long& e, GF2 /*multi*/, const Variable & x)
447{
448  //printf("convertNTLvec_pair_GF2X_long2FacCFFList\n");
449  CFFList result;
450  GF2X polynom;
451  long exponent;
452  CanonicalForm bigone;
453
454  // Maybe, e may additionally be sorted with respect to increasing degree of x
455  // but this is not
456  //important for the factorization, but nevertheless would take computing time
457  // so it is omitted.
458
459  //We do not have to worry about the multiplicity in GF2 since it equals one.
460
461  // Go through the vector e and compute the CFFList
462  // bigone summarizes the result again
463  for (int i=e.length()-1;i>=0;i--)
464  {
465    bigone=0;
466
467    polynom=e[i].a;
468    exponent=e[i].b;
469    for (int j=0;j<=deg(polynom);j++)
470    {
471      if (coeff(polynom,j)!=0)
472        bigone += (power(x,j)*CanonicalForm(to_long(rep(coeff(polynom,j)))));
473    }
474
475    //append the converted polynomial to the CFFList
476    result.append(CFFactor(bigone,exponent));
477  }
478  return result;
479}
480
481STATIC_VAR unsigned char *cf_stringtemp;
482STATIC_VAR unsigned long cf_stringtemp_l=0L;
483////////////////////////////////////////////////////////////////////////////////
484/// NAME: convertZZ2CF
485///
486/// DESCRIPTION:
487/// Routine for conversion of integers represented in NTL as Type ZZ to
488/// integers in Factory represented as canonicalform.
489/// To guarantee the correct execution of the algorithm the characteristic
490/// has to equal zero.
491///
492/// INPUT:  The value coefficient of type ZZ that has to be converted
493/// OUTPUT: The converted Factory-integer of type canonicalform
494////////////////////////////////////////////////////////////////////////////////
495CanonicalForm
496convertZZ2CF (const ZZ & a)
497{
498  long coeff_long=to_long(a);
499
500  CanonicalForm result;
501  if ( (NumBits(a)<((long)NTL_ZZ_NBITS))
502  && (coeff_long>((long)MINIMMEDIATE))
503  && (coeff_long<((long)MAXIMMEDIATE)))
504  {
505    return CanonicalForm(coeff_long);
506  }
507  else
508  {
509    const long * rep =
510#if NTL_MAJOR_VERSION <= 6
511      static_cast<long *>( a.rep );
512#elif NTL_MAJOR_VERSION <=9
513      static_cast<long *>( a.rep.rep ); // what about NTL7?
514#else
515      (long*)( a.rep.rep );
516#endif
517    long sizeofrep= rep[1];
518    bool lessZero= false;
519    if (sizeofrep < 0)
520    {
521      lessZero= true;
522      sizeofrep= -sizeofrep;
523    }
524    if (cf_stringtemp_l == 0)
525    {
526      cf_stringtemp_l= sizeofrep*sizeof(mp_limb_t)*2;
527      cf_stringtemp= (unsigned char*) Alloc (cf_stringtemp_l);
528    }
529    else if (cf_stringtemp_l < sizeofrep*sizeof(mp_limb_t)*2)
530    {
531      Free (cf_stringtemp, cf_stringtemp_l);
532      cf_stringtemp_l= sizeofrep*sizeof(mp_limb_t)*2;
533      cf_stringtemp= (unsigned char*) Alloc (cf_stringtemp_l);
534    }
535    int cc= mpn_get_str (cf_stringtemp, 16, (mp_limb_t *) ((rep) + 2), sizeofrep);
536
537    char* cf_stringtemp2;
538    if (lessZero)
539    {
540      cf_stringtemp2= new char [cc + 2];
541      cf_stringtemp2[0]='-';
542      for (int j= 1; j <= cc; j++)
543        cf_stringtemp2[j]= IntValToChar ((int) cf_stringtemp [j-1]);
544      cf_stringtemp2[cc+1]='\0';
545    }
546    else
547    {
548      cf_stringtemp2= new char [cc + 1];
549      for (int j= 0; j < cc; j++)
550        cf_stringtemp2[j]= IntValToChar ((int) cf_stringtemp [j]);
551      cf_stringtemp2[cc]='\0';
552    }
553
554    result= CanonicalForm (cf_stringtemp2, 16);
555    delete [] cf_stringtemp2;
556  }
557  return result;
558}
559
560/*static char *cf_stringtemp;
561static char *cf_stringtemp2;
562static int cf_stringtemp_l=0;
563CanonicalForm convertZZ2CF(const ZZ & coefficient)
564{
565  long coeff_long;
566  //CanonicalForm tmp=0;
567  char dummy[2];
568  int minusremainder=0;
569  char numbers[]="0123456789abcdef";
570
571  coeff_long=to_long(coefficient);
572
573  //Test whether coefficient can be represented as an immediate integer in Factory
574  if ( (NumBits(coefficient)<((long)NTL_ZZ_NBITS))
575  && (coeff_long>((long)MINIMMEDIATE))
576  && (coeff_long<((long)MAXIMMEDIATE)))
577  {
578    // coefficient is immediate --> return the coefficient as canonicalform
579    return CanonicalForm(coeff_long);
580  }
581  else
582  {
583    // coefficient is not immediate (gmp-number)
584    if (cf_stringtemp_l==0)
585    {
586      cf_stringtemp=(char *)Alloc(1023);
587      cf_stringtemp2=(char *)Alloc(1023);
588      cf_stringtemp[0]='\0';
589      cf_stringtemp2[0]='\0';
590      cf_stringtemp_l=1023;
591    }
592
593    // convert coefficient to char* (input for gmp)
594    dummy[1]='\0';
595
596    if (coefficient<0)
597    {
598      // negate coefficient, but store the sign in minusremainder
599      minusremainder=1;
600      coefficient=-coefficient;
601    }
602
603    int l=0;
604    while (coefficient>15)
605    {
606      ZZ quotient,remaind;
607      ZZ ten;ten=16;
608      DivRem(quotient,remaind,coefficient,ten);
609      dummy[0]=numbers[to_long(remaind)];
610      //tmp*=10; tmp+=to_long(remaind);
611
612      l++;
613      if (l>=cf_stringtemp_l-2)
614      {
615        Free(cf_stringtemp2,cf_stringtemp_l);
616        char *p=(char *)Alloc(cf_stringtemp_l*2);
617        //NTL_SNS
618        memcpy(p,cf_stringtemp,cf_stringtemp_l);
619        Free(cf_stringtemp,cf_stringtemp_l);
620        cf_stringtemp_l*=2;
621        cf_stringtemp=p;
622        cf_stringtemp2=(char *)Alloc(cf_stringtemp_l);
623      }
624      cf_stringtemp[l-1]=dummy[0];
625      cf_stringtemp[l]='\0';
626      //strcat(stringtemp,dummy);
627
628      coefficient=quotient;
629    }
630    //built up the string in dummy[0]
631    dummy[0]=numbers[to_long(coefficient)];
632    //NTL_SNS
633    l++;
634    cf_stringtemp[l-1]=dummy[0];
635    cf_stringtemp[l]='\0';
636    //tmp*=10; tmp+=to_long(coefficient);
637
638    if (minusremainder==1)
639    {
640      //Check whether coefficient has been negative at the start of the procedure
641      cf_stringtemp2[0]='-';
642      //tmp*=(-1);
643    }
644
645    //reverse the list to obtain the correct string
646    //NTL_SNS
647    for (int i=l-1;i>=0;i--) // l ist the position of \0
648    {
649      cf_stringtemp2[l-i-1+minusremainder]=cf_stringtemp[i];
650    }
651    cf_stringtemp2[l+minusremainder]='\0';
652  }
653
654  //convert the string to canonicalform using the char*-Constructor
655  return CanonicalForm(cf_stringtemp2,16);
656  //return tmp;
657}*/
658
659////////////////////////////////////////////////////////////////////////////////
660/// NAME: convertFacCF2NTLZZX
661///
662/// DESCRIPTION:
663/// Routine for conversion of canonicalforms in Factory to polynomials
664/// of type ZZX of NTL. To guarantee the correct execution of the
665/// algorithm the characteristic has to equal zero.
666///
667/// INPUT:  The canonicalform that has to be converted
668/// OUTPUT: The converted NTL-polynom of type ZZX
669////////////////////////////////////////////////////////////////////////////////
670
671ZZ convertFacCF2NTLZZ(const CanonicalForm & f)
672{
673  ZZ temp;
674  if (f.isImm()) temp=f.intval();
675  else
676  {
677    //Coefficient is a gmp-number
678    mpz_t gmp_val;
679    char* stringtemp;
680
681    f.mpzval (gmp_val);
682    int l=mpz_sizeinbase(gmp_val,10)+2;
683    stringtemp=(char*)Alloc(l);
684    stringtemp=mpz_get_str(stringtemp,10,gmp_val);
685    mpz_clear(gmp_val);
686    conv(temp,stringtemp);
687    Free(stringtemp,l);
688  }
689  return temp;
690}
691
692ZZX convertFacCF2NTLZZX(const CanonicalForm & f)
693{
694    ZZX ntl_poly;
695
696    CFIterator i;
697    i=f;
698
699    int NTLcurrentExp=i.exp();
700    int largestExp=i.exp();
701    int k;
702
703    //set the length of the NTL-polynomial
704    ntl_poly.SetMaxLength(largestExp+1);
705
706    //Go through the coefficients of the canonicalform and build up the NTL-polynomial
707    for (;i.hasTerms();i++)
708    {
709      for (k=NTLcurrentExp;k>i.exp();k--)
710      {
711        SetCoeff(ntl_poly,k,0);
712      }
713      NTLcurrentExp=i.exp();
714
715      //Coefficient is a gmp-number
716      ZZ temp=convertFacCF2NTLZZ(i.coeff());
717
718      //set the computed coefficient
719      SetCoeff(ntl_poly,NTLcurrentExp,temp);
720
721      NTLcurrentExp--;
722    }
723    for (k=NTLcurrentExp;k>=0;k--)
724    {
725      SetCoeff(ntl_poly,k,0);
726    }
727
728    //normalize the polynomial
729    ntl_poly.normalize();
730
731    return ntl_poly;
732}
733
734////////////////////////////////////////////////////////////////////////////////
735/// NAME: convertNTLvec_pair_ZZX_long2FacCFFList
736///
737/// DESCRIPTION:
738/// Routine for converting a vector of polynomials from ZZ to a list
739/// CFFList of Factory. This routine will be used after a successful
740/// factorization of NTL to convert the result back to Factory.
741/// Additionally a variable x and the computed multiplicity, as a type
742/// ZZ of NTL, is needed as parameters indicating the main variable of the
743/// computed canonicalform and the multiplicity of the original polynomial.
744/// To guarantee the correct execution of the algorithm the characteristic
745/// has to equal zero.
746///
747/// INPUT:  A vector of polynomials over ZZ of type vec_pair_ZZX_long and
748///         a variable x and a multiplicity of type ZZ
749/// OUTPUT: The converted list of polynomials of type CFFList, all
750///         have x as their main variable
751////////////////////////////////////////////////////////////////////////////////
752
753CFFList
754convertNTLvec_pair_ZZX_long2FacCFFList (const vec_pair_ZZX_long & e,const ZZ & multi,const Variable & x)
755{
756  CFFList result;
757  ZZX polynom;
758  long exponent;
759  CanonicalForm bigone;
760
761  // Go through the vector e and build up the CFFList
762  // As usual bigone summarizes the result
763  for (int i=e.length()-1;i>=0;i--)
764  {
765    ZZ coefficient;
766    polynom=e[i].a;
767    exponent=e[i].b;
768    bigone=convertNTLZZX2CF(polynom,x);
769    //append the converted polynomial to the list
770    result.append(CFFactor(bigone,exponent));
771  }
772  // the multiplicity at pos 1
773  result.insert(CFFactor(convertZZ2CF(multi),1));
774
775  //return the converted list
776  return result;
777}
778
779
780////////////////////////////////////////////////////////////////////////////////
781/// NAME: convertNTLZZpX2CF
782///
783/// DESCRIPTION:
784/// Routine for conversion of elements of arbitrary extensions of ZZp,
785/// having type ZZpE, of NTL to their corresponding values of type
786/// canonicalform in Factory.
787/// To guarantee the correct execution of the algorithm the characteristic
788/// has to be an arbitrary prime number and Factory has to compute in an
789/// extension of F_p.
790///
791/// INPUT:  The coefficient of type ZZpE and the variable x indicating the main//
792///         variable of the computed canonicalform
793/// OUTPUT: The converted value of coefficient as type canonicalform
794////////////////////////////////////////////////////////////////////////////////
795
796CanonicalForm convertNTLZZpE2CF(const ZZ_pE & coefficient,const Variable & x)
797{
798  return convertNTLZZpX2CF(rep(coefficient),x);
799}
800CanonicalForm convertNTLzzpE2CF(const zz_pE & coefficient,const Variable & x)
801{
802  return convertNTLzzpX2CF(rep(coefficient),x);
803}
804
805////////////////////////////////////////////////////////////////////////////////
806/// NAME: convertNTLvec_pair_ZZpEX_long2FacCFFList
807///
808/// DESCRIPTION:
809/// Routine for converting a vector of polynomials from ZZpEX to a CFFList
810/// of Factory. This routine will be used after a successful factorization
811/// of NTL to convert the result back to Factory.
812/// Additionally a variable x and the computed multiplicity, as a type
813/// ZZpE of NTL, is needed as parameters indicating the main variable of the
814/// computed canonicalform and the multiplicity of the original polynomial.
815/// To guarantee the correct execution of the algorithm the characteristic
816/// has a be an arbitrary prime number p and computations have to be done
817/// in an extention of F_p.
818///
819/// INPUT:  A vector of polynomials over ZZpE of type vec_pair_ZZ_pEX_long and
820///         a variable x and a multiplicity of type ZZpE
821/// OUTPUT: The converted list of polynomials of type CFFList, all polynomials
822///         have x as their main variable
823////////////////////////////////////////////////////////////////////////////////
824
825CFFList
826convertNTLvec_pair_ZZpEX_long2FacCFFList(const vec_pair_ZZ_pEX_long & e,const ZZ_pE & multi,const Variable & x,const Variable & alpha)
827{
828  CFFList result;
829  ZZ_pEX polynom;
830  long exponent;
831  CanonicalForm bigone;
832
833  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
834  //important for the factorization, but nevertheless would take computing time, so it is omitted
835
836  // Go through the vector e and build up the CFFList
837  // As usual bigone summarizes the result during every loop
838  for (int i=e.length()-1;i>=0;i--)
839  {
840    bigone=0;
841
842    polynom=e[i].a;
843    exponent=e[i].b;
844
845    for (int j=0;j<=deg(polynom);j++)
846    {
847      if (IsOne(coeff(polynom,j)))
848      {
849        bigone+=power(x,j);
850      }
851      else
852      {
853        CanonicalForm coefficient=convertNTLZZpE2CF(coeff(polynom,j),alpha);
854        if (coeff(polynom,j)!=0)
855        {
856          bigone += (power(x,j)*coefficient);
857        }
858      }
859    }
860    //append the computed polynomials together with its exponent to the CFFList
861    result.append(CFFactor(bigone,exponent));
862  }
863  // Start by appending the multiplicity
864  if (!IsOne(multi))
865    result.insert(CFFactor(convertNTLZZpE2CF(multi,alpha),1));
866
867  //return the computed CFFList
868  return result;
869}
870CFFList
871convertNTLvec_pair_zzpEX_long2FacCFFList(const vec_pair_zz_pEX_long & e,const zz_pE & multi,const Variable & x,const Variable & alpha)
872{
873  CFFList result;
874  zz_pEX polynom;
875  long exponent;
876  CanonicalForm bigone;
877
878  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
879  //important for the factorization, but nevertheless would take computing time, so it is omitted
880
881  // Go through the vector e and build up the CFFList
882  // As usual bigone summarizes the result during every loop
883  for (int i=e.length()-1;i>=0;i--)
884  {
885    bigone=0;
886
887    polynom=e[i].a;
888    exponent=e[i].b;
889
890    for (int j=0;j<=deg(polynom);j++)
891    {
892      if (IsOne(coeff(polynom,j)))
893      {
894        bigone+=power(x,j);
895      }
896      else
897      {
898        CanonicalForm coefficient=convertNTLzzpE2CF(coeff(polynom,j),alpha);
899        if (coeff(polynom,j)!=0)
900        {
901          bigone += (power(x,j)*coefficient);
902        }
903      }
904    }
905    //append the computed polynomials together with its exponent to the CFFList
906    result.append(CFFactor(bigone,exponent));
907  }
908  // Start by appending the multiplicity
909  if (!IsOne(multi))
910    result.insert(CFFactor(convertNTLzzpE2CF(multi,alpha),1));
911
912  //return the computed CFFList
913  return result;
914}
915
916////////////////////////////////////////////////////////////////////////////////
917/// NAME: convertNTLGF2E2CF
918///
919/// DESCRIPTION:
920/// Routine for conversion of elements of extensions of GF2, having type
921/// GF2E, of NTL to their corresponding values of type canonicalform in
922/// Factory.
923/// To guarantee the correct execution of the algorithm, the characteristic
924/// must equal two and Factory has to compute in an extension of F_2.
925/// As usual this is an optimized special case of the more general conversion
926/// routine from ZZpE to Factory.
927///
928/// INPUT:  The coefficient of type GF2E and the variable x indicating the
929///         main variable of the computed canonicalform
930/// OUTPUT: The converted value of coefficient as type canonicalform
931////////////////////////////////////////////////////////////////////////////////
932
933CanonicalForm convertNTLGF2E2CF(const GF2E & coefficient,const Variable & x)
934{
935  return convertNTLGF2X2CF(rep(coefficient),x);
936}
937
938////////////////////////////////////////////////////////////////////////////////
939/// NAME: convertNTLvec_pair_GF2EX_long2FacCFFList
940///
941/// DESCRIPTION:
942/// Routine for converting a vector of polynomials from GF2EX to a CFFList
943/// of Factory. This routine will be used after a successful factorization
944/// of NTL to convert the result back to Factory.
945/// This is a special, but optimized case of the more general conversion
946/// from ZZpE to canonicalform.
947/// Additionally a variable x and the computed multiplicity, as a type GF2E
948/// of NTL, is needed as parameters indicating the main variable of the
949/// computed canonicalform and the multiplicity of the original polynomial.
950/// To guarantee the correct execution of the algorithm the characteristic
951/// has to equal two and computations have to be done in an extention of F_2.
952///
953/// INPUT:  A vector of polynomials over GF2E of type vec_pair_GF2EX_long and
954///         a variable x and a multiplicity of type GF2E
955/// OUTPUT: The converted list of polynomials of type CFFList, all polynomials
956///         have x as their main variable
957////////////////////////////////////////////////////////////////////////////////
958
959CFFList convertNTLvec_pair_GF2EX_long2FacCFFList
960    (const vec_pair_GF2EX_long & e, const GF2E & multi, const Variable & x, const Variable & alpha)
961{
962  CFFList result;
963  GF2EX polynom;
964  long exponent;
965  CanonicalForm bigone;
966
967  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
968  //important for the factorization, but nevertheless would take computing time, so it is omitted
969
970  // multiplicity is always one, so we do not have to worry about that
971
972  // Go through the vector e and build up the CFFList
973  // As usual bigone summarizes the result during every loop
974  for (int i=e.length()-1;i>=0;i--)
975  {
976    bigone=0;
977
978    polynom=e[i].a;
979    exponent=e[i].b;
980
981    for (int j=0;j<=deg(polynom);j++)
982    {
983      if (IsOne(coeff(polynom,j)))
984      {
985        bigone+=power(x,j);
986      }
987      else
988      {
989        CanonicalForm coefficient=convertNTLGF2E2CF(coeff(polynom,j),alpha);
990        if (coeff(polynom,j)!=0)
991        {
992          bigone += (power(x,j)*coefficient);
993        }
994      }
995    }
996
997    // append the computed polynomial together with its multiplicity
998    result.append(CFFactor(bigone,exponent));
999
1000  }
1001
1002  if (!IsOne(multi))
1003    result.insert(CFFactor(convertNTLGF2E2CF(multi,alpha),1));
1004
1005  // return the computed CFFList
1006  return result;
1007}
1008
1009////////////////////////////////////////////////////
1010/// CanonicalForm in Z_2(a)[X] to NTL GF2EX
1011////////////////////////////////////////////////////
1012GF2EX convertFacCF2NTLGF2EX(const CanonicalForm & f,const GF2X & mipo)
1013{
1014  GF2E::init(mipo);
1015  GF2EX result;
1016  CFIterator i;
1017  i=f;
1018
1019  int NTLcurrentExp=i.exp();
1020  int largestExp=i.exp();
1021  int k;
1022
1023  result.SetMaxLength(largestExp+1);
1024  for(;i.hasTerms();i++)
1025  {
1026    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1027    NTLcurrentExp=i.exp();
1028    CanonicalForm c=i.coeff();
1029    GF2X cc=convertFacCF2NTLGF2X(c);
1030    //ZZ_pE ccc;
1031    //conv(ccc,cc);
1032    SetCoeff(result,NTLcurrentExp,to_GF2E(cc));
1033    NTLcurrentExp--;
1034  }
1035  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1036  result.normalize();
1037  return result;
1038}
1039////////////////////////////////////////////////////
1040/// CanonicalForm in Z_p(a)[X] to NTL ZZ_pEX
1041////////////////////////////////////////////////////
1042ZZ_pEX convertFacCF2NTLZZ_pEX(const CanonicalForm & f, const ZZ_pX & mipo)
1043{
1044  ZZ_pE::init(mipo);
1045  ZZ_pEX result;
1046  CFIterator i;
1047  i=f;
1048
1049  int NTLcurrentExp=i.exp();
1050  int largestExp=i.exp();
1051  int k;
1052
1053  result.SetMaxLength(largestExp+1);
1054  for(;i.hasTerms();i++)
1055  {
1056    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1057    NTLcurrentExp=i.exp();
1058    CanonicalForm c=i.coeff();
1059    ZZ_pX cc=convertFacCF2NTLZZpX(c);
1060    //ZZ_pE ccc;
1061    //conv(ccc,cc);
1062    SetCoeff(result,NTLcurrentExp,to_ZZ_pE(cc));
1063    NTLcurrentExp--;
1064  }
1065  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1066  result.normalize();
1067  return result;
1068}
1069zz_pEX convertFacCF2NTLzz_pEX(const CanonicalForm & f, const zz_pX & mipo)
1070{
1071  zz_pE::init(mipo);
1072  zz_pEX result;
1073  CFIterator i;
1074  i=f;
1075
1076  int NTLcurrentExp=i.exp();
1077  int largestExp=i.exp();
1078  int k;
1079
1080  result.SetMaxLength(largestExp+1);
1081  for(;i.hasTerms();i++)
1082  {
1083    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1084    NTLcurrentExp=i.exp();
1085    CanonicalForm c=i.coeff();
1086    zz_pX cc=convertFacCF2NTLzzpX(c);
1087    //ZZ_pE ccc;
1088    //conv(ccc,cc);
1089    SetCoeff(result,NTLcurrentExp,to_zz_pE(cc));
1090    NTLcurrentExp--;
1091  }
1092  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1093  result.normalize();
1094  return result;
1095}
1096
1097CanonicalForm convertNTLzz_pEX2CF (const zz_pEX& f, const Variable & x, const Variable & alpha)
1098{
1099  CanonicalForm bigone;
1100  if (deg (f) > 0)
1101  {
1102    bigone= 0;
1103    bigone.mapinto();
1104    for (int j=0;j<deg(f)+1;j++)
1105    {
1106      if (coeff(f,j)!=0)
1107      {
1108        bigone+=(power(x,j)*convertNTLzzpE2CF(coeff(f,j),alpha));
1109      }
1110    }
1111  }
1112  else
1113  {
1114    bigone= convertNTLzzpE2CF(coeff(f,0),alpha);
1115    bigone.mapinto();
1116  }
1117  return bigone;
1118}
1119
1120CanonicalForm convertNTLZZ_pEX2CF (const ZZ_pEX& f, const Variable & x, const Variable & alpha)
1121{
1122  CanonicalForm bigone;
1123  if (deg (f) > 0)
1124  {
1125    bigone= 0;
1126    bigone.mapinto();
1127    for (int j=0;j<deg(f)+1;j++)
1128    {
1129      if (coeff(f,j)!=0)
1130      {
1131        bigone+=(power(x,j)*convertNTLZZpE2CF(coeff(f,j),alpha));
1132      }
1133    }
1134  }
1135  else
1136  {
1137    bigone= convertNTLZZpE2CF(coeff(f,0),alpha);
1138    bigone.mapinto();
1139  }
1140  return bigone;
1141}
1142//----------------------------------------------------------------------
1143mat_ZZ* convertFacCFMatrix2NTLmat_ZZ(const CFMatrix &m)
1144{
1145  mat_ZZ *res=new mat_ZZ;
1146  res->SetDims(m.rows(),m.columns());
1147
1148  int i,j;
1149  for(i=m.rows();i>0;i--)
1150  {
1151    for(j=m.columns();j>0;j--)
1152    {
1153      (*res)(i,j)=convertFacCF2NTLZZ(m(i,j));
1154    }
1155  }
1156  return res;
1157}
1158CFMatrix* convertNTLmat_ZZ2FacCFMatrix(const mat_ZZ &m)
1159{
1160  CFMatrix *res=new CFMatrix(m.NumRows(),m.NumCols());
1161  int i,j;
1162  for(i=res->rows();i>0;i--)
1163  {
1164    for(j=res->columns();j>0;j--)
1165    {
1166      (*res)(i,j)=convertZZ2CF(m(i,j));
1167    }
1168  }
1169  return res;
1170}
1171
1172mat_zz_p* convertFacCFMatrix2NTLmat_zz_p(const CFMatrix &m)
1173{
1174  mat_zz_p *res=new mat_zz_p;
1175  res->SetDims(m.rows(),m.columns());
1176
1177  int i,j;
1178  for(i=m.rows();i>0;i--)
1179  {
1180    for(j=m.columns();j>0;j--)
1181    {
1182      if(!(m(i,j)).isImm()) printf("convertFacCFMatrix2NTLmat_zz_p: not imm.\n");
1183      (*res)(i,j)=(m(i,j)).intval();
1184    }
1185  }
1186  return res;
1187}
1188CFMatrix* convertNTLmat_zz_p2FacCFMatrix(const mat_zz_p &m)
1189{
1190  CFMatrix *res=new CFMatrix(m.NumRows(),m.NumCols());
1191  int i,j;
1192  for(i=res->rows();i>0;i--)
1193  {
1194    for(j=res->columns();j>0;j--)
1195    {
1196      (*res)(i,j)=CanonicalForm(to_long(rep(m(i,j))));
1197    }
1198  }
1199  return res;
1200}
1201mat_zz_pE* convertFacCFMatrix2NTLmat_zz_pE(const CFMatrix &m)
1202{
1203  mat_zz_pE *res=new mat_zz_pE;
1204  res->SetDims(m.rows(),m.columns());
1205
1206  int i,j;
1207  for(i=m.rows();i>0;i--)
1208  {
1209    for(j=m.columns();j>0;j--)
1210    {
1211      zz_pX cc=convertFacCF2NTLzzpX(m(i,j));
1212      (*res)(i,j)=to_zz_pE(cc);
1213    }
1214  }
1215  return res;
1216}
1217CFMatrix* convertNTLmat_zz_pE2FacCFMatrix(const mat_zz_pE &m, const Variable & alpha)
1218{
1219  CFMatrix *res=new CFMatrix(m.NumRows(),m.NumCols());
1220  int i,j;
1221  for(i=res->rows();i>0;i--)
1222  {
1223    for(j=res->columns();j>0;j--)
1224    {
1225      (*res)(i,j)=convertNTLzzpE2CF(m(i,j), alpha);
1226    }
1227  }
1228  return res;
1229}
1230#endif
Note: See TracBrowser for help on using the repository browser.