source: git/factory/NTLconvert.cc @ 2b76ff

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