-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_selection.cpp
135 lines (108 loc) · 2.43 KB
/
polynomial_selection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: polynomial_selection.cpp
* Author: pankaj
*
* Created on 20 February, 2016, 11:40 AM
*/
#include <cstdlib>
#include <iostream>
#include<string>
#include<NTL/tools.h>
#include<NTL/vector.h>
#include<NTL/matrix.h>
#include<NTL/ZZX.h>
#include<NTL/mat_ZZ.h>
#include<NTL/GF2X.h>
#include <NTL/pair_GF2X_long.h>
#include<NTL/GF2XFactoring.h>
#include<NTL/GF2E.h>
#include<NTL/mat_GF2E.h>
#include<math.h>
#include"ffspol.h"
#include"ffstools.h"
#include<string>
//Resultant by sylvester's matrix method
void resultant(GF2X& r,const ffs_poly& f,const ffs_poly& g)
{
GF2X p=BuildSparseIrred_GF2X(1000); //set some large bound of degree for sparse irreducible polynomial which will not affect the computation
GF2E::init(p);
Mat<GF2E> M;
long d=f.deg+g.deg;
M.SetDims(d,d);
long n=f.deg;
long m=g.deg;
int i,j;
//Initialize the matrix with zero's
for(i=0;i<d;i++)
for(j=0;j<d;j++)
M[i][j]=conv<GF2E>(0);
for(i=0;i<m;i++)
for(j=0;j<d;j++)
{
if(i>=0 && m>i)
{
M[i][j]=conv<GF2E>(f.coeffs[n+i-j]);
}
}
for(;i<d;i++)
for(j=0;j<d;j++)
{
if((i-j)>=0 && (i-j)<2)
{
M[i][j]=conv<GF2E>(g.coeffs[i-j]);
}
}
r=conv<GF2X>(determinant(M));
}
ffs_poly polynomial_selection(const ffs_poly& f,const long n)
{
ffs_poly g;
long t=2;
Vec< Pair< GF2X,long > > factors;
// vec_GF2X factors;
factors.SetLength(100);
long d=ceil(n/f.deg);
g.deg=1;
g.coeffs.SetLength(t);
GF2X g0,g1;
GF2X res;
while(1)
{
g0=random_GF2X(d+1);
g1=random_GF2X(d+1);
cout<<"g0="<<g0<<endl;
cout<<"g0="<<g0<<endl;
g.coeffs[0]=g0;
g.coeffs[1]=g1;
resultant(res,f,g);
cout<<"\n\nResulant="<<res<<endl;
CanZass(factors,res,(long)0); //defined in GF2XFactoring.h
cout<<"Factors="<<factors<<endl;
for(int i=0;i<factors.length();i++)
{
if(IterIrredTest(factors[i].a) && (deg(factors[i].a)==n))
{
cout<<"Irreducible factor of degree n:-"<<factors[i].a<<endl;
return g;
}
}
}
}
int main()
{
ffs_poly f,g;
long n=607;
string pol_f="4,0,3,0,0,1";
cout<<"\n polynomial f:\n";
read_ffs_poly(f,pol_f);
print_ffs_poly(f);
cout<<"\nPolynomial g:\n";
g=polynomial_selection(f,n);
print_ffs_poly(g);
return 0;
}