Révision 191 pobysoPythonSage/src/sageSLZ/runSLZ-01.sage
runSLZ-01.sage (revision 191) | ||
---|---|---|
14 | 14 |
load("/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage") |
15 | 15 |
|
16 | 16 |
|
17 |
def run_SLZ(inputFunction, |
|
18 |
inputLowerBound, |
|
19 |
inputUpperBound, |
|
20 |
alpha, |
|
21 |
degree, |
|
22 |
precision, |
|
23 |
emin, |
|
24 |
emax, |
|
25 |
targetHardnessToRound, |
|
26 |
debug = False): |
|
17 |
def run_SLZ_v01(inputFunction,
|
|
18 |
inputLowerBound,
|
|
19 |
inputUpperBound,
|
|
20 |
alpha,
|
|
21 |
degree,
|
|
22 |
precision,
|
|
23 |
emin,
|
|
24 |
emax,
|
|
25 |
targetHardnessToRound,
|
|
26 |
debug = False):
|
|
27 | 27 |
|
28 | 28 |
if debug: |
29 | 29 |
print "Function :", inputFunction |
... | ... | |
212 | 212 |
intUb = int(ub * toIntegerFactor) - intIc |
213 | 213 |
# |
214 | 214 |
#### Loop flesh |
215 |
#### For polynomials |
|
216 |
basisConstructionTime = cputime() |
|
217 |
##### To a polynomial with rational coefficients with rational arguments |
|
218 |
ratRatP = slz_float_poly_of_float_to_rat_poly_of_rat_pow_two(floatP) |
|
219 |
##### To a polynomial with rational coefficients with integer arguments |
|
220 |
ratIntP = \ |
|
221 |
slz_rat_poly_of_rat_to_rat_poly_of_int(ratRatP, precision) |
|
222 |
##### Ultimately a polynomial with integer coefficients with integer |
|
223 |
# arguments. |
|
224 |
coppersmithTuple = \ |
|
225 |
slz_rat_poly_of_int_to_poly_for_coppersmith(ratIntP, |
|
226 |
precision, |
|
227 |
targetHardnessToRound, |
|
228 |
i, t) |
|
229 |
#### Recover Coppersmith information. |
|
230 |
intIntP = coppersmithTuple[0] |
|
231 |
N = coppersmithTuple[1] |
|
232 |
nAtAlpha = N^alpha |
|
233 |
tBound = coppersmithTuple[2] |
|
234 |
leastCommonMultiple = coppersmithTuple[3] |
|
235 |
iBound = max(abs(intLb),abs(intUb)) |
|
236 |
basisConstructionsFullTime += cputime(basisConstructionTime) |
|
237 |
basisConstructionsCount += 1 |
|
238 |
reductionTime = cputime() |
|
239 |
# Compute the reduced polynomials. |
|
240 |
ccReducedPolynomialsList = \ |
|
241 |
slz_compute_coppersmith_reduced_polynomials(intIntP, |
|
242 |
alpha, |
|
243 |
N, |
|
244 |
iBound, |
|
245 |
tBound) |
|
246 |
if ccReducedPolynomialsList is None: |
|
247 |
raise Exception("Reduction failed.") |
|
248 |
reductionsFullTime += cputime(reductionTime) |
|
249 |
reductionsCount += 1 |
|
250 |
if len(ccReducedPolynomialsList) < 2: |
|
251 |
print "Nothing to form resultants with." |
|
252 |
|
|
253 |
coppCondFailedCount += 1 |
|
254 |
coppCondFailed = True |
|
255 |
##### Apply a different shrink factor according to |
|
256 |
# the number of complient polynomials. |
|
257 |
if len(ccReducedPolynomialsList) == 0: |
|
258 |
ub = lb + bw / 4 |
|
259 |
else: # At least one complient polynomial. |
|
260 |
ub = lb + bw / 2 |
|
261 |
if ub > sdub: |
|
262 |
ub = sdub |
|
263 |
if lb == ub: |
|
264 |
raise Exception("Cant shrink interval \ |
|
265 |
anymore to get Coppersmith condition.") |
|
266 |
nbw = 0 |
|
267 |
continue |
|
268 |
#### We have at least two polynomials. |
|
269 |
# Let us try to compute resultants. |
|
270 |
resultantsComputationTime = cputime() |
|
271 |
resultantsInTTuplesList = [] |
|
272 |
for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList) - 1): |
|
273 |
for polyInnerIndex in xrange(polyOuterIndex+1, |
|
274 |
len(ccReducedPolynomialsList)): |
|
275 |
resultantTuple = \ |
|
276 |
slz_resultant_tuple(ccReducedPolynomialsList[polyOuterIndex], |
|
277 |
ccReducedPolynomialsList[polyInnerIndex], |
|
278 |
t) |
|
279 |
if len(resultantTuple) > 2: |
|
280 |
#print resultantTuple[2] |
|
281 |
resultantsInTTuplesList.append(resultantTuple) |
|
282 |
else: |
|
283 |
print "No non nul resultant" |
|
284 |
print len(resultantsInTTuplesList), "resultant(s) in t tuple(s) created." |
|
285 |
resultantsComputationsFullTime += cputime(resultantsComputationTime) |
|
286 |
resultantsComputationsCount += 1 |
|
287 |
if len(resultantsInTTuplesList) == 0: |
|
288 |
print "Only null resultants, shrinking interval." |
|
289 |
resultCondFailed = True |
|
290 |
resultCondFailedCount += 1 |
|
291 |
### Shrink interval for next iteration. |
|
292 |
ub = lb + bw / 2 |
|
293 |
if ub > sdub: |
|
294 |
ub = sdub |
|
295 |
nbw = 0 |
|
296 |
continue |
|
297 |
#### Compute roots. |
|
298 |
reducedPolynomialsRootsSet = set() |
|
299 |
##### Solve in the second variable since resultants are in the first |
|
300 |
# variable. |
|
301 |
for resultantInTTuple in resultantsInTTuplesList: |
|
302 |
currentResultant = resultantInTTuple[2] |
|
303 |
##### If the resultant degree is not at least 1, there are no roots. |
|
304 |
if currentResultant.degree() < 1: |
|
305 |
print "Resultant is constant:", currentResultant |
|
306 |
continue # Next resultantInTTuple |
|
307 |
##### Compute i roots |
|
308 |
iRootsList = Zi(currentResultant).roots() |
|
309 |
##### For each iRoot, compute the corresponding tRoots and check |
|
310 |
# them in the input polynomial. |
|
311 |
for iRoot in iRootsList: |
|
312 |
####### Roots returned by roots() are (value, multiplicity) |
|
313 |
# tuples. |
|
314 |
#print "iRoot:", iRoot |
|
315 |
###### Use the tRoot against each polynomial, alternatively. |
|
316 |
for indexInTuple in range(0,2): |
|
317 |
currentPolynomial = resultantInTTuple[indexInTuple] |
|
318 |
####### If the polynomial is univariate, just drop it. |
|
319 |
if len(currentPolynomial.variables()) < 2: |
|
320 |
print " Current polynomial is not in two variables." |
|
321 |
continue # Next indexInTuple |
|
322 |
tRootsList = \ |
|
323 |
Zt(currentPolynomial.subs({currentPolynomial.variables()[0]:iRoot[0]})).roots() |
|
324 |
####### The tRootsList can be empty, hence the test. |
|
325 |
if len(tRootsList) == 0: |
|
326 |
print " No t root." |
|
327 |
continue # Next indexInTuple |
|
328 |
for tRoot in tRootsList: |
|
329 |
reducedPolynomialsRootsSet.add((iRoot[0], tRoot[0])) |
|
330 |
# End of roots computation |
|
331 |
## Prepare for results. |
|
332 |
intervalResultsList = [] |
|
333 |
intervalResultsList.append((lb, ub)) |
|
334 |
## Check roots. |
|
335 |
rootsResultsList = [] |
|
336 |
rootsComputationTime = cputime() |
|
337 |
for root in reducedPolynomialsRootsSet: |
|
338 |
specificRootResultsList = [] |
|
339 |
failingBounds = [] |
|
340 |
intIntPdivN = intIntP(root[0], root[1]) / N |
|
341 |
if int(intIntPdivN) != intIntPdivN: |
|
342 |
continue # Next root |
|
343 |
# Root qualifies for modular equation, test it for hardness to round. |
|
344 |
hardToRoundCaseAsFloat = RRR((icAsInt + root[0]) / toIntegerFactor) |
|
345 |
#print "Before unscaling:", hardToRoundCaseAsFloat.n(prec=precision) |
|
346 |
#print scalingFunction |
|
347 |
scaledHardToRoundCaseAsFloat = scalingFunction(hardToRoundCaseAsFloat) |
|
348 |
print "Candidate HTRNc at x =", scaledHardToRoundCaseAsFloat.n().str(base=2), |
|
349 |
if slz_is_htrn(scaledHardToRoundCaseAsFloat, |
|
350 |
f, |
|
351 |
2^-(targetHardnessToRound), |
|
352 |
RRR): |
|
353 |
print hardToRoundCaseAsFloat, "is HTRN case." |
|
354 |
if lb <= hardToRoundCaseAsFloat and hardToRoundCaseAsFloat <= ub: |
|
355 |
print "Found in interval." |
|
356 |
else: |
|
357 |
print "Found out of interval." |
|
358 |
specificRootResultsList.append(hardToRoundCaseAsFloat.n().str(base=2)) |
|
359 |
# Check the root is in the bounds |
|
360 |
if abs(root[0]) > iBound or abs(root[1]) > tBound: |
|
361 |
print "Root", root, "is out of bounds." |
|
362 |
if abs(root[0]) > iBound: |
|
363 |
print "root[0]:", root[0] |
|
364 |
print "i bound:", iBound |
|
365 |
failingBounds.append('i') |
|
366 |
failingBounds.append(root[0]) |
|
367 |
failingBounds.append(iBound) |
|
368 |
if abs(root[1]) > tBound: |
|
369 |
print "root[1]:", root[1] |
|
370 |
print "t bound:", tBound |
|
371 |
failingBounds.append('t') |
|
372 |
failingBounds.append(root[1]) |
|
373 |
failingBounds.append(tBound) |
|
374 |
if len(failingBounds) > 0: |
|
375 |
specificRootResultsList.append(failingBounds) |
|
376 |
else: # From slz_is_htrn... |
|
377 |
print "is not an HTRN case." |
|
378 |
if len(specificRootResultsList) > 0: |
|
379 |
rootsResultsList.append(specificRootResultsList) |
|
380 |
rootsComputationsFullTime = cputime(rootsComputationTime) |
|
381 |
rootsComputationsCount += 1 |
|
382 |
if len(rootsResultsList) > 0: |
|
383 |
intervalResultsList.append(rootsResultsList) |
|
384 |
## An intervalResultsList has at least the bounds. |
|
385 |
globalResultsList.append(intervalResultsList) |
|
215 | 386 |
#### End loop flesh. |
216 | 387 |
#### Compute an incremented width for next upper bound, only |
217 | 388 |
# if not Coppersmith condition nor resultant condition |
... | ... | |
222 | 393 |
# again if needed. |
223 | 394 |
else: |
224 | 395 |
nbw = bw |
225 |
rootsComputationsFullTime = cputime(rootsComputationTime) |
|
226 |
rootsComputationsCount += 1 |
|
227 | 396 |
coppCondFailed = False |
228 | 397 |
resultCondFailed = False |
229 | 398 |
#### For next iteration (at end of loop) |
... | ... | |
285 | 454 |
print "Global Wall time:", globalWallTime |
286 | 455 |
print "Global CPU time:", globalCpuTime |
287 | 456 |
## Output counters |
457 |
# End runSLZ-v01 |
|
288 | 458 |
|
289 | 459 |
print "Running SLZ..." |
290 | 460 |
initialize_env() |
... | ... | |
292 | 462 |
func(x) = exp(x) |
293 | 463 |
precision = 53 |
294 | 464 |
RRR = RealField(precision) |
295 |
run_SLZ(inputFunction=func, |
|
296 |
inputLowerBound = 1/4, |
|
297 |
inputUpperBound = RRR(1/2) - RRR(1/4).ulp(), |
|
298 |
alpha = 2, |
|
299 |
degree = 10, |
|
300 |
precision = 53, |
|
301 |
emin = -1022, |
|
302 |
emax = 1023, |
|
303 |
targetHardnessToRound = precision+50, |
|
304 |
debug = True) |
|
465 |
run_SLZ_v01(inputFunction=func, |
|
466 |
inputLowerBound = 1/4, |
|
467 |
inputUpperBound = RRR(1/2) - RRR(1/4).ulp(), |
|
468 |
alpha = 2, |
|
469 |
degree = 10, |
|
470 |
precision = 53, |
|
471 |
emin = -1022, |
|
472 |
emax = 1023, |
|
473 |
targetHardnessToRound = precision+50, |
|
474 |
debug = True) |
Formats disponibles : Unified diff