-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactorbase.cpp
More file actions
139 lines (128 loc) · 3.25 KB
/
factorbase.cpp
File metadata and controls
139 lines (128 loc) · 3.25 KB
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
134
135
136
137
138
139
#include "GNFS.h"
#include "poly.h"
#include "factorbase.h"
#include "util.h"
int MaxPrime = DefaultMaxPrime;
/**
* Get the bound for smoothness (that upperbound for the rational factor base)
*/
ulong boundForSmoothness(slong d, const fmpz_t n)
{
#ifdef DEBUG
assert(d > 0);
assert(n > 0);
#endif
double dlogd = d*log(d);
double temp = 1.0/d * fmpz_dlog(n);
#ifdef DEBUG
assert(temp > 0);
#endif
double e = dlogd + sqrt(dlogd*dlogd + 4*temp*log(temp));
return 0.5*exp(0.5*e);
}
/**
* Prepare the bases
*
* 1. rational base: a set of prime numbers
* 2. algebraic base: a set of pairs (p,r), where p is prime number,
* r is a root of f modular p.
* 3. quadratic base: a set of pairs (q,s), where q is a prime number,
* s is a root of f modular p. It is required that (q,s) should never
* divide the pairs (a,b) sieved out by the (p,r) pairs, so the smallest
* q is usually larger than the largest p in algebraic base.
*/
void prepareRationalBase(ulong *RB, ulong &nRB, ulong bound)
{
ulong p;
n_primes_t iter;
n_primes_init(iter);
for(nRB = 0; (p = n_primes_next(iter)) <= bound; nRB++)
{
RB[nRB] = p;
}
//slong r = rand() % (nRB/10);
//nRB -= r;
assert(nRB <= MaxPrimeBufSize);
n_primes_clear(iter);
}
void prepareAlgebraicBase(MyPair *AB, ulong &nAB, ulong size, fmpz_poly_t f)
{
n_primes_t iter;
n_primes_init(iter);
for(ulong i = 0; i < size; i++)
{
ulong p = n_primes_next(iter);
ulong roots[16]; ulong nroot;
rootsMod(f,p,roots,nroot);
for(ulong j = 0; j < nroot; j++)
{
AB[nAB] = MyPair(roots[j],p);
nAB++;
}
}
n_primes_clear(iter);
assert(nAB <= MaxPrimeBufSize);
}
void prepareQuadraticBase(MyPair *QB, ulong &nQB, ulong min, ulong max, fmpz_poly_t &f)
{
ulong p;
n_primes_t iter;
n_primes_init(iter);
n_primes_jump_after(iter,min);
for(ulong i = 0; (p=n_primes_next(iter)) <= max; i++)
{
ulong roots[16]; ulong nroot;
rootsMod(f,p,roots,nroot);
for(ulong j = 0; j < nroot; j++)
{
QB[nQB] = MyPair(roots[j],p);
nQB++;
}
}
assert(nQB <= MaxPrimeBufSize);
n_primes_clear(iter);
}
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 3)
{
cerr << "Usage: factorbase inputfile outputfile" << endl;
exit(-1);
}
FILE *input = fopen(argv[1],"r");
if(!input) perror(argv[1]);
FILE *output = fopen(argv[2],"w");
if(!output) perror(argv[2]);
fmpz_t n, m;
fmpz_poly_t f;
fmpz_init(n);
fmpz_init(m);
fmpz_poly_init(f);
fmpz_fread(input,n);
fmpz_fread(input,m);
fmpz_poly_fread(input,f);
slong d = fmpz_poly_degree(f);
ulong smoothBound = boundForSmoothness(d,n);
ulong RB[MaxPrimeBufSize], nRB = 0, nAB = 0, nQB = 0;
MyPair AB[MaxPrimeBufSize], QB[MaxPrimeBufSize];
prepareRationalBase(RB,nRB,smoothBound);
prepareAlgebraicBase(AB,nAB,nRB,f);
prepareQuadraticBase(QB,nQB,smoothBound,smoothBound+20*log(smoothBound),f);
fmpz_fprint(output,n); fprintf(output,"\n");
fmpz_fprint(output,m); fprintf(output,"\n");
fprintf(output,"%lu\n",smoothBound);
fmpz_poly_fprint(output,f); fprintf(output,"\n");
fprintf(output,"%lu\n",nRB);
printListOfNumbers(output,RB,nRB,10);
fprintf(output,"%lu\n",nAB);
printListOfPairs(output,AB,nAB,5);
fprintf(output,"%lu\n",nQB);
printListOfPairs(output,QB,nQB,5);
fmpz_clear(n);
fmpz_clear(m);
fmpz_poly_clear(f);
fclose(input);
fclose(output);
return 0;
}