source: git/factory/NTLconvert.cc @ 9f7665

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