-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproblem-instances.sage
305 lines (200 loc) · 7.69 KB
/
problem-instances.sage
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from dependencies.timer import Timer
def sample_smooth_prime_excluding_factors(l, B=DEFAULT_BOUND_B, factors=set()):
"""
@brief Samples an l-bit prime p such that p - 1 is B-smooth for B some
small bound, and such that p - 1 has no factor that is in the
factors set besides two.
@param l The bit length of each prime p.
@param B The bound B on the smoothness of p - 1.
@param factors The set of prime factors less than B already used. If two
is a part of this set, two will be ignored, since p - 1
must be even when p is a large prime.
@return [p, factors], for p the prime selected, and factors an updated
set of used factors that also contains the factors of p - 1.
"""
# Create a list F of all prime factors up to B not in factors.
F = []
for p in primes(3, B):
if p not in factors:
F.append(p)
# Search exhaustively for a combination of factors that yields an l-bit
# prime p such that p - 1 is B-smooth and return this prime.
while True:
shuffle(F)
used = []
x = 2
for p in F:
while x * p >= 2^l:
x = x // used[0]
used = used[1:]
x *= p
used.append(p)
if (2^(l - 1) <= x) and (x < 2^l):
q = x + 1
if q.is_prime(proof=False):
return [q, factors.union(set(used)).union(set([2]))]
def sample_smooth_prime(l, B=DEFAULT_BOUND_B):
"""
@brief Samples an l-bit prime p such that p - 1 is B-smooth for B some
small bound.
@param l The bit length of the prime p.
@param B The bound B on the smoothness of p - 1.
@return The prime p sampled.
"""
return sample_smooth_prime_excluding_factors(l, B)[0]
def sample_integer(t, l, B=DEFAULT_BOUND_B, verbose=False):
"""
@brief Samples an integer N = p_1 * ... * p_t, with t distinct l-bit prime
factors such that all p_i - 1 are B-smooth for B some small bound,
and such that gcd(p_i, p_j) = 2 for all i ≠ j.
@param t The number of distinct l-bit primes p_i.
@param l The bit length of each prime p_i.
@param B The bound B on the smoothness of each p_i - 1.
@param verbose A flag that may be set to True to print verbose status
messages, or to False not to print such messages.
@return [N, [p_1, .., p_t]] for N = p_1 * ... * p_t.
"""
# Setup and start a timer.
timer = Timer().start()
if verbose:
print("Sampling N on special form to enable efficient simulation...")
# Primes.
primes = set()
factors = set()
# Pick t distinct l-bit primes p_i, such that p_i - 1 are B-smooth.
for _ in range(t):
while True:
[p, factors] = sample_smooth_prime_excluding_factors(l, B, factors)
if p not in primes:
primes.add(p)
if verbose:
print(" Sampled factor:", p)
break
# Compute the product.
N = prod(primes)
# Stop the timer.
timer.stop()
if verbose:
print("")
print(" Sampled N =", N)
print("")
print(" Time required to sample:", timer)
return [N, primes]
def find_smooth_order_mod_p(g, p):
"""
@brief Finds the multiplicative order of g mod p, for p a prime such that
p - 1 is B-smooth for some small B.
@param g The generator g.
@param p The prime p such that p - 1 is B-smooth.
@return The multiplicative order of g mod p.
"""
r = p - 1
F = GF(p, proof=False)
g = F(g)
for [q, _] in factor(p - 1):
if g^((p - 1) / q) == 1:
r /= q
return ZZ(r)
def sample_x(g, p):
"""
@brief Samples an element x = g^e mod p uniformly at random from <g> by
sampling e uniformly at random from [0, r), for r the order of g,
and for p a prime such that p - 1 is B-smooth for B some small
bound.
@param g The generator g.
@param p The prime p.
@return [x, e] for x = g^e the element sampled and e the exponent.
"""
R = IntegerModRing(p)
r = find_smooth_order_mod_p(g, p)
e = IntegerModRing(r).random_element().lift()
x = (R(g)^e).lift()
return [x, e]
def sample_safe_prime(l):
"""
@brief Samples an l-bit prime p such that (p - 1) / 2 is also a prime.
@param l The bit length of the prime p.
@return The prime p selected.
"""
while True:
p = random_prime(2^l-1, proof=False, lbound=2^(l-1))
if ZZ((p - 1)/2).is_prime():
return p
def sample_domain_parameters_safe_prime(l, B=DEFAULT_BOUND_B, verbose=False):
"""
@brief Samples domain parameters to simulate a safe-prime group.
Specifically, this function first samples an l-bit prime p such that p - 1
is B-smooth for B some small bound (to enable efficient simulation). It then
picks the first generator g that is of order r = (p - 1) / 2.
The above assuming that ACTUAL_SAFE_PRIMES is set to False in common.sage,
otherwise this function samples an actual safe-prime; an l-bit prime p such
that (p - 1) / 2 is also a prime.
@param l The bit length of the prime p.
@param B The bound B on the smoothness of p - 1.
@param verbose A flag that may be set to True to print verbose status
messages, or to False not to print such messages.
@return [g, p] for g the generator picked and p the prime sampled.
"""
# Setup and start a timer.
timer = Timer().start()
if verbose:
print("Sampling domain parameters...")
if ACTUAL_SAFE_PRIMES:
p = sample_safe_prime(l)
else:
p = sample_smooth_prime(l, B)
if verbose:
print("")
print(" Sampled p =", p)
g = 2
while find_smooth_order_mod_p(g, p) != (p - 1) / 2:
g += 1
if g >= p:
raise Exception("Error: Failed to sample g.")
if verbose:
print(" Sampled g =", g)
print("")
print(" Time required to sample:", timer)
return [g, p]
def sample_domain_parameters_schnorr(l, k, B=DEFAULT_BOUND_B, verbose=False):
"""
@brief Samples domain parameters to simulate a Schnorr group.
Specifically, this function first samples an l-bit prime p such that p - 1
is B-smooth for B some small bound (to enable efficient simulation), and
such that p - 1 = 2 * u * r where r is of length approximately k bits.
It then picks a random generator g that is of order r.
@param l The bit length of the prime p.
@param k The approximate bit length of the order r of g.
@param B The bound B on the smoothness of p - 1.
@param verbose A flag that may be set to True to print verbose status
messages, or to False not to print such messages.
@return [g, p] for g the generator picked and p the prime sampled.
"""
# Setup and start a timer.
timer = Timer().start()
if verbose:
print("Sampling domain parameters...")
p = sample_smooth_prime(l, B)
if verbose:
print(" Sampled p =", p)
# Select r.
factors = [q for [q, _] in factor((p - 1) / 2)]
shuffle(factors)
r = 1
for q in factors:
if r.nbits() < k:
r *= q
# Select g.
while True:
g = IntegerModRing(p).random_element()
if g == 0:
continue;
g = g^((p - 1) / r)
g = g.lift()
if find_smooth_order_mod_p(g, p) == r:
break
if verbose:
print(" Sampled g =", g)
print("")
print(" Time required to sample:", timer)
return [g, p]