-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.cpp
153 lines (125 loc) · 4.21 KB
/
parameters.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "parameters.h"
#include <cstdio>
void order(fmpz_t rot, const fmpz_t generator, const fmpz_t modulus)
{
fmpz_set_ui(rot, 1);
fmpz_t pow;
fmpz_init(pow);
while (true) {
fmpz_powm(pow, generator, rot, modulus);
if (fmpz_is_one(pow)) {
fmpz_clear(pow);
return;
}
fmpz_add_ui(rot, rot, 1);
}
}
void algorithm2(fmpz_t res, const fmpz_t M, const fmpz_factor_t M_decomposition, const fmpz_t generator, const fmpz_t ordp)
{
fmpz_set(res, M);
fmpz_t ord;
fmpz_init(ord);
for (unsigned int i = 0; i < M_decomposition->num; ++i) {
fmpz* p = M_decomposition->p + i;
order(ord, generator, p);
if (!fmpz_divisible(ordp, ord)) {
// eliminate this prime from M'
fmpz_fdiv_q(res, res, p);
}
}
fmpz_clear(ord);
}
void parameters(fmpz_t M, fmpz_t ord, unsigned int* m, unsigned int* t, const fmpz_t N)
{
mp_bitcnt_t bits = fmpz_bits(N);
if (bits >= 512 && bits <= 960) {
fmpz_primorial(M, 167);
fmpz_set_str(ord, "2454106387091158800", 10);
*m = 5;
*t = 6;
} else if (bits >= 962 && bits <= 1952) {
fmpz_primorial(M, 353);
fmpz_set_str(ord, "18008752815808067874770317538232798996000", 10);
*m = 4;
*t = 5;
} else if (bits >= 1984 && bits < 3072) {
fmpz_primorial(M, 701);
fmpz_set_str(ord, "6d9bc8b22f6a31bd9622dfb3d3b93df4a7f37f1443ad5880236fd6ee5c5e5a80", 16);
*m = 6;
*t = 7;
} else if (bits >= 3072 && bits <= 3936) {
fmpz_primorial(M, 701);
fmpz_set_str(ord, "6d9bc8b22f6a31bd9622dfb3d3b93df4a7f37f1443ad5880236fd6ee5c5e5a80", 10);
*m = 25;
*t = 26;
} else if (bits >= 3968 && bits <= 4096) {
fmpz_primorial(M, 1427);
fmpz_set_str(ord, "33c07a6215a59c179c46f668d09de25aa7a394ec6d453871b4342d55115c8b8d2086d445a0b8ee950ee16fa5dae8ffd231191d87b5d80", 10);
*m = 7;
*t = 8;
} else {
// unsupported bit length
return;
}
fmpz_t generator, Mcandidate, pv, candidate, ordDivCandidate, Mnew, eliminate;
fmpz_init(Mcandidate);
fmpz_init(pv);
fmpz_init(candidate);
fmpz_init(ordDivCandidate);
fmpz_init(Mnew);
fmpz_init(eliminate);
fmpz_init_set_ui(generator, 65537ul);
while (true) {
double bestReward = 0.0;
fmpz_t pv;
fmpz_init_set_ui(pv, 0);
ulong ev = 0;
fmpz_set(Mcandidate, M);
fmpz_factor_t Mfactors;
fmpz_factor_init(Mfactors);
fmpz_factor(Mfactors, M);
fmpz_factor_t factors;
fmpz_factor_init(factors);
fmpz_factor(factors, ord);
for (unsigned int i = 0; i < factors->num; ++i) {
for (unsigned int exp = 1; exp <= factors->exp[i]; ++exp) {
// candidate prime factor p^e for elimination
fmpz_pow_ui(candidate, factors->p + i, exp);
// ord/p^e
fmpz_fdiv_q(ordDivCandidate, ord, candidate);
// find M'_new
algorithm2(Mnew, M, Mfactors, generator, ordDivCandidate);
// calculate reward
double reward = (fmpz_dlog(ord) - fmpz_dlog(ordDivCandidate)) / (fmpz_dlog(M) - fmpz_dlog(Mnew));
if (reward > bestReward) {
bestReward = reward;
fmpz_set(pv, factors->p + i);
ev = exp;
fmpz_set(Mcandidate, Mnew);
}
}
}
fmpz_factor_clear(factors);
fmpz_factor_clear(Mfactors);
if (fmpz_is_zero(pv)) {
// no candidates found
break;
}
if (4.0 * fmpz_dlog(Mcandidate) < fmpz_dlog(N)) {
// done
//printf("done eliminating\n");
break;
}
fmpz_set(M, Mcandidate);
fmpz_pow_ui(eliminate, pv, ev);
fmpz_fdiv_q(ord, ord, eliminate);
//printf("eliminating "); fmpz_print(pv); printf("^%lu (reward %f)\n", ev, bestReward);
}
fmpz_clear(eliminate);
fmpz_clear(pv);
fmpz_clear(candidate);
fmpz_clear(ordDivCandidate);
fmpz_clear(Mnew);
fmpz_clear(Mcandidate);
fmpz_clear(generator);
}