source: git/factory/NTLconvert.cc @ 3d6980b

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