root / pobysoPythonSage / src / sageSLZ / sageRunSLZ.sage @ 207
Historique | Voir | Annoter | Télécharger (42,4 ko)
1 | 194 | storres | """ |
---|---|---|---|
2 | 194 | storres | SLZ runtime function. |
3 | 194 | storres | """ |
4 | 194 | storres | |
5 | 194 | storres | def srs_run_SLZ_v01(inputFunction, |
6 | 194 | storres | inputLowerBound, |
7 | 194 | storres | inputUpperBound, |
8 | 194 | storres | alpha, |
9 | 194 | storres | degree, |
10 | 194 | storres | precision, |
11 | 194 | storres | emin, |
12 | 194 | storres | emax, |
13 | 194 | storres | targetHardnessToRound, |
14 | 194 | storres | debug = False): |
15 | 194 | storres | |
16 | 194 | storres | if debug: |
17 | 194 | storres | print "Function :", inputFunction |
18 | 194 | storres | print "Lower bound :", inputLowerBound |
19 | 194 | storres | print "Upper bounds :", inputUpperBound |
20 | 194 | storres | print "Alpha :", alpha |
21 | 194 | storres | print "Degree :", degree |
22 | 194 | storres | print "Precision :", precision |
23 | 194 | storres | print "Emin :", emin |
24 | 194 | storres | print "Emax :", emax |
25 | 194 | storres | print "Target hardness-to-round:", targetHardnessToRound |
26 | 194 | storres | |
27 | 194 | storres | ## Important constants. |
28 | 194 | storres | ### Stretch the interval if no error happens. |
29 | 194 | storres | noErrorIntervalStretch = 1 + 2^(-5) |
30 | 194 | storres | ### If no vector validates the Coppersmith condition, shrink the interval |
31 | 194 | storres | # by the following factor. |
32 | 194 | storres | noCoppersmithIntervalShrink = 1/2 |
33 | 194 | storres | ### If only (or at least) one vector validates the Coppersmith condition, |
34 | 194 | storres | # shrink the interval by the following factor. |
35 | 194 | storres | oneCoppersmithIntervalShrink = 3/4 |
36 | 194 | storres | #### If only null resultants are found, shrink the interval by the |
37 | 194 | storres | # following factor. |
38 | 194 | storres | onlyNullResultantsShrink = 3/4 |
39 | 194 | storres | ## Structures. |
40 | 194 | storres | RRR = RealField(precision) |
41 | 194 | storres | RRIF = RealIntervalField(precision) |
42 | 194 | storres | ## Converting input bound into the "right" field. |
43 | 194 | storres | lowerBound = RRR(inputLowerBound) |
44 | 194 | storres | upperBound = RRR(inputUpperBound) |
45 | 194 | storres | ## Before going any further, check domain and image binade conditions. |
46 | 194 | storres | print inputFunction(1).n() |
47 | 206 | storres | output = slz_fix_bounds_for_binades(lowerBound, upperBound, inputFunction) |
48 | 206 | storres | if output is None: |
49 | 206 | storres | print "Invalid domain/image binades. Domain:",\ |
50 | 206 | storres | lowerBound, upperBound, "Images:", \ |
51 | 206 | storres | inputFunction(lowerBound), inputFunction(upperBound) |
52 | 206 | storres | raise Exception("Invalid domain/image binades.") |
53 | 206 | storres | lb = output[0] ; ub = output[1] |
54 | 206 | storres | if lb is None or lb != lowerBound or ub != upperBound: |
55 | 194 | storres | print "lb:", lb, " - ub:", ub |
56 | 194 | storres | print "Invalid domain/image binades. Domain:",\ |
57 | 194 | storres | lowerBound, upperBound, "Images:", \ |
58 | 194 | storres | inputFunction(lowerBound), inputFunction(upperBound) |
59 | 194 | storres | raise Exception("Invalid domain/image binades.") |
60 | 194 | storres | # |
61 | 194 | storres | ## Progam initialization |
62 | 194 | storres | ### Approximation polynomial accuracy and hardness to round. |
63 | 194 | storres | polyApproxAccur = 2^(-(targetHardnessToRound + 1)) |
64 | 194 | storres | polyTargetHardnessToRound = targetHardnessToRound + 1 |
65 | 194 | storres | ### Significand to integer conversion ratio. |
66 | 194 | storres | toIntegerFactor = 2^(precision-1) |
67 | 194 | storres | print "Polynomial approximation required accuracy:", polyApproxAccur.n() |
68 | 194 | storres | ### Variables and rings for polynomials and root searching. |
69 | 194 | storres | i=var('i') |
70 | 194 | storres | t=var('t') |
71 | 194 | storres | inputFunctionVariable = inputFunction.variables()[0] |
72 | 194 | storres | function = inputFunction.subs({inputFunctionVariable:i}) |
73 | 194 | storres | # Polynomial Rings over the integers, for root finding. |
74 | 194 | storres | Zi = ZZ[i] |
75 | 194 | storres | Zt = ZZ[t] |
76 | 194 | storres | Zit = ZZ[i,t] |
77 | 194 | storres | ## Number of iterations limit. |
78 | 194 | storres | maxIter = 100000 |
79 | 194 | storres | # |
80 | 194 | storres | ## Compute the scaled function and the degree, in their Sollya version |
81 | 194 | storres | # once for all. |
82 | 194 | storres | (scaledf, sdlb, sdub, silb, siub) = \ |
83 | 194 | storres | slz_compute_scaled_function(function, lowerBound, upperBound, precision) |
84 | 194 | storres | print "Scaled function:", scaledf._assume_str().replace('_SAGE_VAR_', '') |
85 | 194 | storres | scaledfSo = sollya_lib_parse_string(scaledf._assume_str().replace('_SAGE_VAR_', '')) |
86 | 194 | storres | degreeSo = pobyso_constant_from_int_sa_so(degree) |
87 | 194 | storres | # |
88 | 194 | storres | ## Compute the scaling. boundsIntervalRifSa defined out of the loops. |
89 | 194 | storres | domainBoundsInterval = RRIF(lowerBound, upperBound) |
90 | 194 | storres | (unscalingFunction, scalingFunction) = \ |
91 | 194 | storres | slz_interval_scaling_expression(domainBoundsInterval, i) |
92 | 194 | storres | #print scalingFunction, unscalingFunction |
93 | 194 | storres | ## Set the Sollya internal precision (with an arbitrary minimum of 192). |
94 | 194 | storres | internalSollyaPrec = ceil((RR('1.5') * targetHardnessToRound) / 64) * 64 |
95 | 194 | storres | if internalSollyaPrec < 192: |
96 | 194 | storres | internalSollyaPrec = 192 |
97 | 194 | storres | pobyso_set_prec_sa_so(internalSollyaPrec) |
98 | 194 | storres | print "Sollya internal precision:", internalSollyaPrec |
99 | 194 | storres | ## Some variables. |
100 | 194 | storres | ### General variables |
101 | 194 | storres | lb = sdlb |
102 | 194 | storres | ub = sdub |
103 | 194 | storres | nbw = 0 |
104 | 194 | storres | intervalUlp = ub.ulp() |
105 | 194 | storres | #### Will be set by slz_interval_and_polynomila_to_sage. |
106 | 194 | storres | ic = 0 |
107 | 194 | storres | icAsInt = 0 # Set from ic. |
108 | 194 | storres | solutionsSet = set() |
109 | 194 | storres | tsErrorWidth = [] |
110 | 194 | storres | csErrorVectors = [] |
111 | 194 | storres | csVectorsResultants = [] |
112 | 194 | storres | floatP = 0 # Taylor polynomial. |
113 | 194 | storres | floatPcv = 0 # Ditto with variable change. |
114 | 194 | storres | intvl = "" # Taylor interval |
115 | 194 | storres | terr = 0 # Taylor error. |
116 | 194 | storres | iterCount = 0 |
117 | 194 | storres | htrnSet = set() |
118 | 194 | storres | ### Timers and counters. |
119 | 194 | storres | wallTimeStart = 0 |
120 | 194 | storres | cpuTimeStart = 0 |
121 | 194 | storres | taylCondFailedCount = 0 |
122 | 194 | storres | coppCondFailedCount = 0 |
123 | 194 | storres | resultCondFailedCount = 0 |
124 | 194 | storres | coppCondFailed = False |
125 | 194 | storres | resultCondFailed = False |
126 | 194 | storres | globalResultsList = [] |
127 | 194 | storres | basisConstructionsCount = 0 |
128 | 194 | storres | basisConstructionsFullTime = 0 |
129 | 194 | storres | basisConstructionTime = 0 |
130 | 194 | storres | reductionsCount = 0 |
131 | 194 | storres | reductionsFullTime = 0 |
132 | 194 | storres | reductionTime = 0 |
133 | 194 | storres | resultantsComputationsCount = 0 |
134 | 194 | storres | resultantsComputationsFullTime = 0 |
135 | 194 | storres | resultantsComputationTime = 0 |
136 | 194 | storres | rootsComputationsCount = 0 |
137 | 194 | storres | rootsComputationsFullTime = 0 |
138 | 194 | storres | rootsComputationTime = 0 |
139 | 194 | storres | |
140 | 194 | storres | ## Global times are started here. |
141 | 194 | storres | wallTimeStart = walltime() |
142 | 194 | storres | cpuTimeStart = cputime() |
143 | 194 | storres | ## Main loop. |
144 | 194 | storres | while True: |
145 | 194 | storres | if lb >= sdub: |
146 | 194 | storres | print "Lower bound reached upper bound." |
147 | 194 | storres | break |
148 | 194 | storres | if iterCount == maxIter: |
149 | 194 | storres | print "Reached maxIter. Aborting" |
150 | 194 | storres | break |
151 | 194 | storres | iterCount += 1 |
152 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
153 | 194 | storres | "log2(numbers)." |
154 | 194 | storres | ### Compute a Sollya polynomial that will honor the Taylor condition. |
155 | 194 | storres | prceSo = slz_compute_polynomial_and_interval(scaledfSo, |
156 | 194 | storres | degreeSo, |
157 | 194 | storres | lb, |
158 | 194 | storres | ub, |
159 | 194 | storres | polyApproxAccur) |
160 | 194 | storres | ### Convert back the data into Sage space. |
161 | 194 | storres | (floatP, floatPcv, intvl, ic, terr) = \ |
162 | 194 | storres | slz_interval_and_polynomial_to_sage((prceSo[0], prceSo[0], |
163 | 194 | storres | prceSo[1], prceSo[2], |
164 | 194 | storres | prceSo[3])) |
165 | 194 | storres | intvl = RRIF(intvl) |
166 | 194 | storres | ## Clean-up Sollya stuff. |
167 | 194 | storres | for elem in prceSo: |
168 | 194 | storres | sollya_lib_clear_obj(elem) |
169 | 194 | storres | #print floatP, floatPcv, intvl, ic, terr |
170 | 194 | storres | #print floatP |
171 | 194 | storres | #print intvl.endpoints()[0].n(), \ |
172 | 194 | storres | # ic.n(), |
173 | 194 | storres | #intvl.endpoints()[1].n() |
174 | 194 | storres | ### Check returned data. |
175 | 194 | storres | #### Is approximation error OK? |
176 | 194 | storres | if terr > polyApproxAccur: |
177 | 194 | storres | exceptionErrorMess = \ |
178 | 194 | storres | "Approximation failed - computed error:" + \ |
179 | 194 | storres | str(terr) + " - target error: " |
180 | 194 | storres | exceptionErrorMess += \ |
181 | 194 | storres | str(polyApproxAccur) + ". Aborting!" |
182 | 194 | storres | raise Exception(exceptionErrorMess) |
183 | 194 | storres | #### Is lower bound OK? |
184 | 194 | storres | if lb != intvl.endpoints()[0]: |
185 | 194 | storres | exceptionErrorMess = "Wrong lower bound:" + \ |
186 | 194 | storres | str(lb) + ". Aborting!" |
187 | 194 | storres | raise Exception(exceptionErrorMess) |
188 | 194 | storres | #### Set upper bound. |
189 | 194 | storres | if ub > intvl.endpoints()[1]: |
190 | 194 | storres | ub = intvl.endpoints()[1] |
191 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
192 | 194 | storres | "log2(numbers)." |
193 | 194 | storres | taylCondFailedCount += 1 |
194 | 194 | storres | #### Is interval not degenerate? |
195 | 194 | storres | if lb >= ub: |
196 | 194 | storres | exceptionErrorMess = "Degenerate interval: " + \ |
197 | 194 | storres | "lowerBound(" + str(lb) +\ |
198 | 194 | storres | ")>= upperBound(" + str(ub) + \ |
199 | 194 | storres | "). Aborting!" |
200 | 194 | storres | raise Exception(exceptionErrorMess) |
201 | 194 | storres | #### Is interval center ok? |
202 | 194 | storres | if ic <= lb or ic >= ub: |
203 | 194 | storres | exceptionErrorMess = "Invalid interval center for " + \ |
204 | 194 | storres | str(lb) + ',' + str(ic) + ',' + \ |
205 | 194 | storres | str(ub) + ". Aborting!" |
206 | 194 | storres | raise Exception(exceptionErrorMess) |
207 | 194 | storres | ##### Current interval width and reset future interval width. |
208 | 194 | storres | bw = ub - lb |
209 | 194 | storres | nbw = 0 |
210 | 194 | storres | icAsInt = int(ic * toIntegerFactor) |
211 | 194 | storres | #### The following ratio is always >= 1. In case we may want to |
212 | 194 | storres | # enlarge the interval |
213 | 194 | storres | curTaylErrRat = polyApproxAccur / terr |
214 | 194 | storres | ## Make the integral transformations. |
215 | 194 | storres | ### First for interval center and bounds. |
216 | 194 | storres | intIc = int(ic * toIntegerFactor) |
217 | 194 | storres | intLb = int(lb * toIntegerFactor) - intIc |
218 | 194 | storres | intUb = int(ub * toIntegerFactor) - intIc |
219 | 194 | storres | # |
220 | 194 | storres | #### For polynomials |
221 | 194 | storres | basisConstructionTime = cputime() |
222 | 194 | storres | ##### To a polynomial with rational coefficients with rational arguments |
223 | 194 | storres | ratRatP = slz_float_poly_of_float_to_rat_poly_of_rat_pow_two(floatP) |
224 | 194 | storres | ##### To a polynomial with rational coefficients with integer arguments |
225 | 194 | storres | ratIntP = \ |
226 | 194 | storres | slz_rat_poly_of_rat_to_rat_poly_of_int(ratRatP, precision) |
227 | 194 | storres | ##### Ultimately a polynomial with integer coefficients with integer |
228 | 194 | storres | # arguments. |
229 | 194 | storres | coppersmithTuple = \ |
230 | 194 | storres | slz_rat_poly_of_int_to_poly_for_coppersmith(ratIntP, |
231 | 194 | storres | precision, |
232 | 194 | storres | targetHardnessToRound, |
233 | 194 | storres | i, t) |
234 | 194 | storres | #### Recover Coppersmith information. |
235 | 194 | storres | intIntP = coppersmithTuple[0] |
236 | 194 | storres | N = coppersmithTuple[1] |
237 | 194 | storres | nAtAlpha = N^alpha |
238 | 194 | storres | tBound = coppersmithTuple[2] |
239 | 194 | storres | leastCommonMultiple = coppersmithTuple[3] |
240 | 194 | storres | iBound = max(abs(intLb),abs(intUb)) |
241 | 194 | storres | basisConstructionsFullTime += cputime(basisConstructionTime) |
242 | 194 | storres | basisConstructionsCount += 1 |
243 | 194 | storres | reductionTime = cputime() |
244 | 194 | storres | # Compute the reduced polynomials. |
245 | 194 | storres | ccReducedPolynomialsList = \ |
246 | 194 | storres | slz_compute_coppersmith_reduced_polynomials(intIntP, |
247 | 194 | storres | alpha, |
248 | 194 | storres | N, |
249 | 194 | storres | iBound, |
250 | 194 | storres | tBound) |
251 | 194 | storres | if ccReducedPolynomialsList is None: |
252 | 194 | storres | raise Exception("Reduction failed.") |
253 | 194 | storres | reductionsFullTime += cputime(reductionTime) |
254 | 194 | storres | reductionsCount += 1 |
255 | 194 | storres | if len(ccReducedPolynomialsList) < 2: |
256 | 194 | storres | print "Nothing to form resultants with." |
257 | 194 | storres | |
258 | 194 | storres | coppCondFailedCount += 1 |
259 | 194 | storres | coppCondFailed = True |
260 | 194 | storres | ##### Apply a different shrink factor according to |
261 | 194 | storres | # the number of compliant polynomials. |
262 | 194 | storres | if len(ccReducedPolynomialsList) == 0: |
263 | 194 | storres | ub = lb + bw * noCoppersmithIntervalShrink |
264 | 194 | storres | else: # At least one compliant polynomial. |
265 | 194 | storres | ub = lb + bw * oneCoppersmithIntervalShrink |
266 | 194 | storres | if ub > sdub: |
267 | 194 | storres | ub = sdub |
268 | 194 | storres | if lb == ub: |
269 | 194 | storres | raise Exception("Cant shrink interval \ |
270 | 194 | storres | anymore to get Coppersmith condition.") |
271 | 194 | storres | nbw = 0 |
272 | 194 | storres | continue |
273 | 194 | storres | #### We have at least two polynomials. |
274 | 194 | storres | # Let us try to compute resultants. |
275 | 194 | storres | resultantsComputationTime = cputime() |
276 | 194 | storres | resultantsInTTuplesList = [] |
277 | 194 | storres | for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList) - 1): |
278 | 194 | storres | for polyInnerIndex in xrange(polyOuterIndex+1, |
279 | 194 | storres | len(ccReducedPolynomialsList)): |
280 | 194 | storres | resultantTuple = \ |
281 | 194 | storres | slz_resultant_tuple(ccReducedPolynomialsList[polyOuterIndex], |
282 | 194 | storres | ccReducedPolynomialsList[polyInnerIndex], |
283 | 194 | storres | t) |
284 | 194 | storres | if len(resultantTuple) > 2: |
285 | 194 | storres | #print resultantTuple[2] |
286 | 194 | storres | resultantsInTTuplesList.append(resultantTuple) |
287 | 194 | storres | else: |
288 | 194 | storres | print "No non nul resultant" |
289 | 194 | storres | print len(resultantsInTTuplesList), "resultant(s) in t tuple(s) created." |
290 | 194 | storres | resultantsComputationsFullTime += cputime(resultantsComputationTime) |
291 | 194 | storres | resultantsComputationsCount += 1 |
292 | 194 | storres | if len(resultantsInTTuplesList) == 0: |
293 | 194 | storres | print "Only null resultants, shrinking interval." |
294 | 194 | storres | resultCondFailed = True |
295 | 194 | storres | resultCondFailedCount += 1 |
296 | 194 | storres | ### Shrink interval for next iteration. |
297 | 194 | storres | ub = lb + bw * onlyNullResultantsShrink |
298 | 194 | storres | if ub > sdub: |
299 | 194 | storres | ub = sdub |
300 | 194 | storres | nbw = 0 |
301 | 194 | storres | continue |
302 | 194 | storres | #### Compute roots. |
303 | 194 | storres | rootsComputationTime = cputime() |
304 | 194 | storres | reducedPolynomialsRootsSet = set() |
305 | 194 | storres | ##### Solve in the second variable since resultants are in the first |
306 | 194 | storres | # variable. |
307 | 194 | storres | for resultantInTTuple in resultantsInTTuplesList: |
308 | 194 | storres | currentResultant = resultantInTTuple[2] |
309 | 194 | storres | ##### If the resultant degree is not at least 1, there are no roots. |
310 | 194 | storres | if currentResultant.degree() < 1: |
311 | 194 | storres | print "Resultant is constant:", currentResultant |
312 | 194 | storres | continue # Next resultantInTTuple |
313 | 194 | storres | ##### Compute i roots |
314 | 194 | storres | iRootsList = Zi(currentResultant).roots() |
315 | 194 | storres | ##### For each iRoot, compute the corresponding tRoots and check |
316 | 194 | storres | # them in the input polynomial. |
317 | 194 | storres | for iRoot in iRootsList: |
318 | 194 | storres | ####### Roots returned by roots() are (value, multiplicity) |
319 | 194 | storres | # tuples. |
320 | 194 | storres | #print "iRoot:", iRoot |
321 | 194 | storres | ###### Use the tRoot against each polynomial, alternatively. |
322 | 194 | storres | for indexInTuple in range(0,2): |
323 | 194 | storres | currentPolynomial = resultantInTTuple[indexInTuple] |
324 | 194 | storres | ####### If the polynomial is univariate, just drop it. |
325 | 194 | storres | if len(currentPolynomial.variables()) < 2: |
326 | 194 | storres | print " Current polynomial is not in two variables." |
327 | 194 | storres | continue # Next indexInTuple |
328 | 194 | storres | tRootsList = \ |
329 | 194 | storres | Zt(currentPolynomial.subs({currentPolynomial.variables()[0]:iRoot[0]})).roots() |
330 | 194 | storres | ####### The tRootsList can be empty, hence the test. |
331 | 194 | storres | if len(tRootsList) == 0: |
332 | 194 | storres | print " No t root." |
333 | 194 | storres | continue # Next indexInTuple |
334 | 194 | storres | for tRoot in tRootsList: |
335 | 194 | storres | reducedPolynomialsRootsSet.add((iRoot[0], tRoot[0])) |
336 | 194 | storres | # End of roots computation |
337 | 194 | storres | rootsComputationsFullTime = cputime(rootsComputationTime) |
338 | 194 | storres | rootsComputationsCount += 1 |
339 | 194 | storres | ##### Prepare for results. |
340 | 194 | storres | intervalResultsList = [] |
341 | 194 | storres | intervalResultsList.append((lb, ub)) |
342 | 194 | storres | #### Check roots. |
343 | 194 | storres | rootsResultsList = [] |
344 | 194 | storres | for root in reducedPolynomialsRootsSet: |
345 | 194 | storres | specificRootResultsList = [] |
346 | 194 | storres | failingBounds = [] |
347 | 194 | storres | intIntPdivN = intIntP(root[0], root[1]) / N |
348 | 194 | storres | if int(intIntPdivN) != intIntPdivN: |
349 | 194 | storres | continue # Next root |
350 | 194 | storres | # Root qualifies for modular equation, test it for hardness to round. |
351 | 194 | storres | hardToRoundCaseAsFloat = RRR((icAsInt + root[0]) / toIntegerFactor) |
352 | 194 | storres | #print "Before unscaling:", hardToRoundCaseAsFloat.n(prec=precision) |
353 | 194 | storres | #print scalingFunction |
354 | 194 | storres | scaledHardToRoundCaseAsFloat = \ |
355 | 194 | storres | scalingFunction(hardToRoundCaseAsFloat) |
356 | 194 | storres | print "Candidate HTRNc at x =", \ |
357 | 194 | storres | scaledHardToRoundCaseAsFloat.n().str(base=2), |
358 | 194 | storres | if slz_is_htrn(scaledHardToRoundCaseAsFloat, |
359 | 194 | storres | function, |
360 | 194 | storres | 2^-(targetHardnessToRound), |
361 | 194 | storres | RRR): |
362 | 194 | storres | print hardToRoundCaseAsFloat, "is HTRN case." |
363 | 194 | storres | if lb <= hardToRoundCaseAsFloat and hardToRoundCaseAsFloat <= ub: |
364 | 194 | storres | print "Found in interval." |
365 | 194 | storres | else: |
366 | 194 | storres | print "Found out of interval." |
367 | 194 | storres | specificRootResultsList.append(hardToRoundCaseAsFloat.n().str(base=2)) |
368 | 194 | storres | # Check the root is in the bounds |
369 | 194 | storres | if abs(root[0]) > iBound or abs(root[1]) > tBound: |
370 | 194 | storres | print "Root", root, "is out of bounds." |
371 | 194 | storres | if abs(root[0]) > iBound: |
372 | 194 | storres | print "root[0]:", root[0] |
373 | 194 | storres | print "i bound:", iBound |
374 | 194 | storres | failingBounds.append('i') |
375 | 194 | storres | failingBounds.append(root[0]) |
376 | 194 | storres | failingBounds.append(iBound) |
377 | 194 | storres | if abs(root[1]) > tBound: |
378 | 194 | storres | print "root[1]:", root[1] |
379 | 194 | storres | print "t bound:", tBound |
380 | 194 | storres | failingBounds.append('t') |
381 | 194 | storres | failingBounds.append(root[1]) |
382 | 194 | storres | failingBounds.append(tBound) |
383 | 194 | storres | if len(failingBounds) > 0: |
384 | 194 | storres | specificRootResultsList.append(failingBounds) |
385 | 194 | storres | else: # From slz_is_htrn... |
386 | 194 | storres | print "is not an HTRN case." |
387 | 194 | storres | if len(specificRootResultsList) > 0: |
388 | 194 | storres | rootsResultsList.append(specificRootResultsList) |
389 | 194 | storres | if len(rootsResultsList) > 0: |
390 | 194 | storres | intervalResultsList.append(rootsResultsList) |
391 | 194 | storres | #### An intervalResultsList has at least the bounds. |
392 | 194 | storres | globalResultsList.append(intervalResultsList) |
393 | 194 | storres | #### Compute an incremented width for next upper bound, only |
394 | 194 | storres | # if not Coppersmith condition nor resultant condition |
395 | 194 | storres | # failed at the previous run. |
396 | 194 | storres | if not coppCondFailed and not resultCondFailed: |
397 | 194 | storres | nbw = noErrorIntervalStretch * bw |
398 | 194 | storres | else: |
399 | 194 | storres | nbw = bw |
400 | 194 | storres | ##### Reset the failure flags. They will be raised |
401 | 194 | storres | # again if needed. |
402 | 194 | storres | coppCondFailed = False |
403 | 194 | storres | resultCondFailed = False |
404 | 194 | storres | #### For next iteration (at end of loop) |
405 | 194 | storres | #print "nbw:", nbw |
406 | 194 | storres | lb = ub |
407 | 194 | storres | ub += nbw |
408 | 194 | storres | if ub > sdub: |
409 | 194 | storres | ub = sdub |
410 | 194 | storres | |
411 | 194 | storres | # End while True |
412 | 194 | storres | ## Main loop just ended. |
413 | 194 | storres | globalWallTime = walltime(wallTimeStart) |
414 | 194 | storres | globalCpuTime = cputime(cpuTimeStart) |
415 | 194 | storres | ## Output results |
416 | 194 | storres | print ; print "Intervals and HTRNs" ; print |
417 | 194 | storres | for intervalResultsList in globalResultsList: |
418 | 194 | storres | print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]", |
419 | 194 | storres | if len(intervalResultsList) > 1: |
420 | 194 | storres | rootsResultsList = intervalResultsList[1] |
421 | 194 | storres | for specificRootResultsList in rootsResultsList: |
422 | 194 | storres | print "\t", specificRootResultsList[0], |
423 | 194 | storres | if len(specificRootResultsList) > 1: |
424 | 194 | storres | print specificRootResultsList[1], |
425 | 194 | storres | print ; print |
426 | 194 | storres | #print globalResultsList |
427 | 194 | storres | # |
428 | 194 | storres | print "Timers and counters" |
429 | 194 | storres | |
430 | 194 | storres | print "Number of iterations:", iterCount |
431 | 194 | storres | print "Taylor condition failures:", taylCondFailedCount |
432 | 194 | storres | print "Coppersmith condition failures:", coppCondFailedCount |
433 | 194 | storres | print "Resultant condition failures:", resultCondFailedCount |
434 | 194 | storres | print "Iterations count: ", iterCount |
435 | 194 | storres | print "Number of intervals:", len(globalResultsList) |
436 | 194 | storres | print "Number of basis constructions:", basisConstructionsCount |
437 | 194 | storres | print "Total CPU time spent in basis constructions:", \ |
438 | 194 | storres | basisConstructionsFullTime |
439 | 194 | storres | if basisConstructionsCount != 0: |
440 | 194 | storres | print "Average basis construction CPU time:", \ |
441 | 194 | storres | basisConstructionsFullTime/basisConstructionsCount |
442 | 194 | storres | print "Number of reductions:", reductionsCount |
443 | 194 | storres | print "Total CPU time spent in reductions:", reductionsFullTime |
444 | 194 | storres | if reductionsCount != 0: |
445 | 194 | storres | print "Average reduction CPU time:", \ |
446 | 194 | storres | reductionsFullTime/reductionsCount |
447 | 194 | storres | print "Number of resultants computation rounds:", \ |
448 | 194 | storres | resultantsComputationsCount |
449 | 194 | storres | print "Total CPU time spent in resultants computation rounds:", \ |
450 | 194 | storres | resultantsComputationsFullTime |
451 | 194 | storres | if resultantsComputationsCount != 0: |
452 | 194 | storres | print "Average resultants computation round CPU time:", \ |
453 | 194 | storres | resultantsComputationsFullTime/resultantsComputationsCount |
454 | 194 | storres | print "Number of root finding rounds:", rootsComputationsCount |
455 | 194 | storres | print "Total CPU time spent in roots finding rounds:", \ |
456 | 194 | storres | rootsComputationsFullTime |
457 | 194 | storres | if rootsComputationsCount != 0: |
458 | 194 | storres | print "Average roots finding round CPU time:", \ |
459 | 194 | storres | rootsComputationsFullTime/rootsComputationsCount |
460 | 194 | storres | print "Global Wall time:", globalWallTime |
461 | 194 | storres | print "Global CPU time:", globalCpuTime |
462 | 194 | storres | ## Output counters |
463 | 194 | storres | # End srs_runSLZ-v01 |
464 | 194 | storres | |
465 | 194 | storres | def srs_run_SLZ_v02(inputFunction, |
466 | 194 | storres | inputLowerBound, |
467 | 194 | storres | inputUpperBound, |
468 | 194 | storres | alpha, |
469 | 194 | storres | degree, |
470 | 194 | storres | precision, |
471 | 194 | storres | emin, |
472 | 194 | storres | emax, |
473 | 194 | storres | targetHardnessToRound, |
474 | 194 | storres | debug = False): |
475 | 194 | storres | """ |
476 | 194 | storres | Changes from V1: |
477 | 194 | storres | 1- check for roots as soon as a resultant is computed; |
478 | 194 | storres | 2- once a non null resultant is found, check for roots; |
479 | 194 | storres | 3- constant resultant == no root. |
480 | 194 | storres | """ |
481 | 194 | storres | |
482 | 194 | storres | if debug: |
483 | 194 | storres | print "Function :", inputFunction |
484 | 194 | storres | print "Lower bound :", inputLowerBound |
485 | 194 | storres | print "Upper bounds :", inputUpperBound |
486 | 194 | storres | print "Alpha :", alpha |
487 | 194 | storres | print "Degree :", degree |
488 | 194 | storres | print "Precision :", precision |
489 | 194 | storres | print "Emin :", emin |
490 | 194 | storres | print "Emax :", emax |
491 | 194 | storres | print "Target hardness-to-round:", targetHardnessToRound |
492 | 194 | storres | |
493 | 194 | storres | ## Important constants. |
494 | 194 | storres | ### Stretch the interval if no error happens. |
495 | 194 | storres | noErrorIntervalStretch = 1 + 2^(-5) |
496 | 194 | storres | ### If no vector validates the Coppersmith condition, shrink the interval |
497 | 194 | storres | # by the following factor. |
498 | 194 | storres | noCoppersmithIntervalShrink = 1/2 |
499 | 194 | storres | ### If only (or at least) one vector validates the Coppersmith condition, |
500 | 194 | storres | # shrink the interval by the following factor. |
501 | 194 | storres | oneCoppersmithIntervalShrink = 3/4 |
502 | 194 | storres | #### If only null resultants are found, shrink the interval by the |
503 | 194 | storres | # following factor. |
504 | 194 | storres | onlyNullResultantsShrink = 3/4 |
505 | 194 | storres | ## Structures. |
506 | 194 | storres | RRR = RealField(precision) |
507 | 194 | storres | RRIF = RealIntervalField(precision) |
508 | 194 | storres | ## Converting input bound into the "right" field. |
509 | 194 | storres | lowerBound = RRR(inputLowerBound) |
510 | 194 | storres | upperBound = RRR(inputUpperBound) |
511 | 194 | storres | ## Before going any further, check domain and image binade conditions. |
512 | 194 | storres | print inputFunction(1).n() |
513 | 206 | storres | output = slz_fix_bounds_for_binades(lowerBound, upperBound, inputFunction) |
514 | 206 | storres | if output is None: |
515 | 206 | storres | print "Invalid domain/image binades. Domain:",\ |
516 | 206 | storres | lowerBound, upperBound, "Images:", \ |
517 | 206 | storres | inputFunction(lowerBound), inputFunction(upperBound) |
518 | 206 | storres | raise Exception("Invalid domain/image binades.") |
519 | 206 | storres | lb = output[0] ; ub = output[1] |
520 | 194 | storres | if lb != lowerBound or ub != upperBound: |
521 | 194 | storres | print "lb:", lb, " - ub:", ub |
522 | 194 | storres | print "Invalid domain/image binades. Domain:",\ |
523 | 194 | storres | lowerBound, upperBound, "Images:", \ |
524 | 194 | storres | inputFunction(lowerBound), inputFunction(upperBound) |
525 | 194 | storres | raise Exception("Invalid domain/image binades.") |
526 | 194 | storres | # |
527 | 194 | storres | ## Progam initialization |
528 | 194 | storres | ### Approximation polynomial accuracy and hardness to round. |
529 | 194 | storres | polyApproxAccur = 2^(-(targetHardnessToRound + 1)) |
530 | 194 | storres | polyTargetHardnessToRound = targetHardnessToRound + 1 |
531 | 194 | storres | ### Significand to integer conversion ratio. |
532 | 194 | storres | toIntegerFactor = 2^(precision-1) |
533 | 194 | storres | print "Polynomial approximation required accuracy:", polyApproxAccur.n() |
534 | 194 | storres | ### Variables and rings for polynomials and root searching. |
535 | 194 | storres | i=var('i') |
536 | 194 | storres | t=var('t') |
537 | 194 | storres | inputFunctionVariable = inputFunction.variables()[0] |
538 | 194 | storres | function = inputFunction.subs({inputFunctionVariable:i}) |
539 | 194 | storres | # Polynomial Rings over the integers, for root finding. |
540 | 194 | storres | Zi = ZZ[i] |
541 | 194 | storres | Zt = ZZ[t] |
542 | 194 | storres | Zit = ZZ[i,t] |
543 | 194 | storres | ## Number of iterations limit. |
544 | 194 | storres | maxIter = 100000 |
545 | 194 | storres | # |
546 | 194 | storres | ## Compute the scaled function and the degree, in their Sollya version |
547 | 194 | storres | # once for all. |
548 | 194 | storres | (scaledf, sdlb, sdub, silb, siub) = \ |
549 | 194 | storres | slz_compute_scaled_function(function, lowerBound, upperBound, precision) |
550 | 194 | storres | print "Scaled function:", scaledf._assume_str().replace('_SAGE_VAR_', '') |
551 | 194 | storres | scaledfSo = sollya_lib_parse_string(scaledf._assume_str().replace('_SAGE_VAR_', '')) |
552 | 194 | storres | degreeSo = pobyso_constant_from_int_sa_so(degree) |
553 | 194 | storres | # |
554 | 194 | storres | ## Compute the scaling. boundsIntervalRifSa defined out of the loops. |
555 | 194 | storres | domainBoundsInterval = RRIF(lowerBound, upperBound) |
556 | 194 | storres | (unscalingFunction, scalingFunction) = \ |
557 | 194 | storres | slz_interval_scaling_expression(domainBoundsInterval, i) |
558 | 194 | storres | #print scalingFunction, unscalingFunction |
559 | 194 | storres | ## Set the Sollya internal precision (with an arbitrary minimum of 192). |
560 | 194 | storres | internalSollyaPrec = ceil((RR('1.5') * targetHardnessToRound) / 64) * 64 |
561 | 194 | storres | if internalSollyaPrec < 192: |
562 | 194 | storres | internalSollyaPrec = 192 |
563 | 194 | storres | pobyso_set_prec_sa_so(internalSollyaPrec) |
564 | 194 | storres | print "Sollya internal precision:", internalSollyaPrec |
565 | 194 | storres | ## Some variables. |
566 | 194 | storres | ### General variables |
567 | 194 | storres | lb = sdlb |
568 | 194 | storres | ub = sdub |
569 | 194 | storres | nbw = 0 |
570 | 194 | storres | intervalUlp = ub.ulp() |
571 | 194 | storres | #### Will be set by slz_interval_and_polynomila_to_sage. |
572 | 194 | storres | ic = 0 |
573 | 194 | storres | icAsInt = 0 # Set from ic. |
574 | 194 | storres | solutionsSet = set() |
575 | 194 | storres | tsErrorWidth = [] |
576 | 194 | storres | csErrorVectors = [] |
577 | 194 | storres | csVectorsResultants = [] |
578 | 194 | storres | floatP = 0 # Taylor polynomial. |
579 | 194 | storres | floatPcv = 0 # Ditto with variable change. |
580 | 194 | storres | intvl = "" # Taylor interval |
581 | 194 | storres | terr = 0 # Taylor error. |
582 | 194 | storres | iterCount = 0 |
583 | 194 | storres | htrnSet = set() |
584 | 194 | storres | ### Timers and counters. |
585 | 194 | storres | wallTimeStart = 0 |
586 | 194 | storres | cpuTimeStart = 0 |
587 | 194 | storres | taylCondFailedCount = 0 |
588 | 194 | storres | coppCondFailedCount = 0 |
589 | 194 | storres | resultCondFailedCount = 0 |
590 | 194 | storres | coppCondFailed = False |
591 | 194 | storres | resultCondFailed = False |
592 | 194 | storres | globalResultsList = [] |
593 | 194 | storres | basisConstructionsCount = 0 |
594 | 194 | storres | basisConstructionsFullTime = 0 |
595 | 194 | storres | basisConstructionTime = 0 |
596 | 194 | storres | reductionsCount = 0 |
597 | 194 | storres | reductionsFullTime = 0 |
598 | 194 | storres | reductionTime = 0 |
599 | 194 | storres | resultantsComputationsCount = 0 |
600 | 194 | storres | resultantsComputationsFullTime = 0 |
601 | 194 | storres | resultantsComputationTime = 0 |
602 | 194 | storres | rootsComputationsCount = 0 |
603 | 194 | storres | rootsComputationsFullTime = 0 |
604 | 194 | storres | rootsComputationTime = 0 |
605 | 194 | storres | |
606 | 194 | storres | ## Global times are started here. |
607 | 194 | storres | wallTimeStart = walltime() |
608 | 194 | storres | cpuTimeStart = cputime() |
609 | 194 | storres | ## Main loop. |
610 | 194 | storres | while True: |
611 | 194 | storres | if lb >= sdub: |
612 | 194 | storres | print "Lower bound reached upper bound." |
613 | 194 | storres | break |
614 | 194 | storres | if iterCount == maxIter: |
615 | 194 | storres | print "Reached maxIter. Aborting" |
616 | 194 | storres | break |
617 | 194 | storres | iterCount += 1 |
618 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
619 | 194 | storres | "log2(numbers)." |
620 | 194 | storres | ### Compute a Sollya polynomial that will honor the Taylor condition. |
621 | 194 | storres | prceSo = slz_compute_polynomial_and_interval(scaledfSo, |
622 | 194 | storres | degreeSo, |
623 | 194 | storres | lb, |
624 | 194 | storres | ub, |
625 | 194 | storres | polyApproxAccur) |
626 | 194 | storres | ### Convert back the data into Sage space. |
627 | 194 | storres | (floatP, floatPcv, intvl, ic, terr) = \ |
628 | 194 | storres | slz_interval_and_polynomial_to_sage((prceSo[0], prceSo[0], |
629 | 194 | storres | prceSo[1], prceSo[2], |
630 | 194 | storres | prceSo[3])) |
631 | 194 | storres | intvl = RRIF(intvl) |
632 | 194 | storres | ## Clean-up Sollya stuff. |
633 | 194 | storres | for elem in prceSo: |
634 | 194 | storres | sollya_lib_clear_obj(elem) |
635 | 194 | storres | #print floatP, floatPcv, intvl, ic, terr |
636 | 194 | storres | #print floatP |
637 | 194 | storres | #print intvl.endpoints()[0].n(), \ |
638 | 194 | storres | # ic.n(), |
639 | 194 | storres | #intvl.endpoints()[1].n() |
640 | 194 | storres | ### Check returned data. |
641 | 194 | storres | #### Is approximation error OK? |
642 | 194 | storres | if terr > polyApproxAccur: |
643 | 194 | storres | exceptionErrorMess = \ |
644 | 194 | storres | "Approximation failed - computed error:" + \ |
645 | 194 | storres | str(terr) + " - target error: " |
646 | 194 | storres | exceptionErrorMess += \ |
647 | 194 | storres | str(polyApproxAccur) + ". Aborting!" |
648 | 194 | storres | raise Exception(exceptionErrorMess) |
649 | 194 | storres | #### Is lower bound OK? |
650 | 194 | storres | if lb != intvl.endpoints()[0]: |
651 | 194 | storres | exceptionErrorMess = "Wrong lower bound:" + \ |
652 | 194 | storres | str(lb) + ". Aborting!" |
653 | 194 | storres | raise Exception(exceptionErrorMess) |
654 | 194 | storres | #### Set upper bound. |
655 | 194 | storres | if ub > intvl.endpoints()[1]: |
656 | 194 | storres | ub = intvl.endpoints()[1] |
657 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
658 | 194 | storres | "log2(numbers)." |
659 | 194 | storres | taylCondFailedCount += 1 |
660 | 194 | storres | #### Is interval not degenerate? |
661 | 194 | storres | if lb >= ub: |
662 | 194 | storres | exceptionErrorMess = "Degenerate interval: " + \ |
663 | 194 | storres | "lowerBound(" + str(lb) +\ |
664 | 194 | storres | ")>= upperBound(" + str(ub) + \ |
665 | 194 | storres | "). Aborting!" |
666 | 194 | storres | raise Exception(exceptionErrorMess) |
667 | 194 | storres | #### Is interval center ok? |
668 | 194 | storres | if ic <= lb or ic >= ub: |
669 | 194 | storres | exceptionErrorMess = "Invalid interval center for " + \ |
670 | 194 | storres | str(lb) + ',' + str(ic) + ',' + \ |
671 | 194 | storres | str(ub) + ". Aborting!" |
672 | 194 | storres | raise Exception(exceptionErrorMess) |
673 | 194 | storres | ##### Current interval width and reset future interval width. |
674 | 194 | storres | bw = ub - lb |
675 | 194 | storres | nbw = 0 |
676 | 194 | storres | icAsInt = int(ic * toIntegerFactor) |
677 | 194 | storres | #### The following ratio is always >= 1. In case we may want to |
678 | 197 | storres | # enlarge the interval |
679 | 194 | storres | curTaylErrRat = polyApproxAccur / terr |
680 | 197 | storres | ### Make the integral transformations. |
681 | 197 | storres | #### Bounds and interval center. |
682 | 194 | storres | intIc = int(ic * toIntegerFactor) |
683 | 194 | storres | intLb = int(lb * toIntegerFactor) - intIc |
684 | 194 | storres | intUb = int(ub * toIntegerFactor) - intIc |
685 | 194 | storres | # |
686 | 197 | storres | #### Polynomials |
687 | 194 | storres | basisConstructionTime = cputime() |
688 | 194 | storres | ##### To a polynomial with rational coefficients with rational arguments |
689 | 194 | storres | ratRatP = slz_float_poly_of_float_to_rat_poly_of_rat_pow_two(floatP) |
690 | 194 | storres | ##### To a polynomial with rational coefficients with integer arguments |
691 | 194 | storres | ratIntP = \ |
692 | 194 | storres | slz_rat_poly_of_rat_to_rat_poly_of_int(ratRatP, precision) |
693 | 197 | storres | ##### Ultimately a multivariate polynomial with integer coefficients |
694 | 197 | storres | # with integer arguments. |
695 | 194 | storres | coppersmithTuple = \ |
696 | 194 | storres | slz_rat_poly_of_int_to_poly_for_coppersmith(ratIntP, |
697 | 194 | storres | precision, |
698 | 194 | storres | targetHardnessToRound, |
699 | 194 | storres | i, t) |
700 | 194 | storres | #### Recover Coppersmith information. |
701 | 194 | storres | intIntP = coppersmithTuple[0] |
702 | 194 | storres | N = coppersmithTuple[1] |
703 | 194 | storres | nAtAlpha = N^alpha |
704 | 194 | storres | tBound = coppersmithTuple[2] |
705 | 194 | storres | leastCommonMultiple = coppersmithTuple[3] |
706 | 194 | storres | iBound = max(abs(intLb),abs(intUb)) |
707 | 194 | storres | basisConstructionsFullTime += cputime(basisConstructionTime) |
708 | 194 | storres | basisConstructionsCount += 1 |
709 | 194 | storres | reductionTime = cputime() |
710 | 197 | storres | #### Compute the reduced polynomials. |
711 | 194 | storres | ccReducedPolynomialsList = \ |
712 | 194 | storres | slz_compute_coppersmith_reduced_polynomials(intIntP, |
713 | 194 | storres | alpha, |
714 | 194 | storres | N, |
715 | 194 | storres | iBound, |
716 | 194 | storres | tBound) |
717 | 194 | storres | if ccReducedPolynomialsList is None: |
718 | 194 | storres | raise Exception("Reduction failed.") |
719 | 194 | storres | reductionsFullTime += cputime(reductionTime) |
720 | 194 | storres | reductionsCount += 1 |
721 | 194 | storres | if len(ccReducedPolynomialsList) < 2: |
722 | 194 | storres | print "Nothing to form resultants with." |
723 | 194 | storres | |
724 | 194 | storres | coppCondFailedCount += 1 |
725 | 194 | storres | coppCondFailed = True |
726 | 194 | storres | ##### Apply a different shrink factor according to |
727 | 194 | storres | # the number of compliant polynomials. |
728 | 194 | storres | if len(ccReducedPolynomialsList) == 0: |
729 | 194 | storres | ub = lb + bw * noCoppersmithIntervalShrink |
730 | 194 | storres | else: # At least one compliant polynomial. |
731 | 194 | storres | ub = lb + bw * oneCoppersmithIntervalShrink |
732 | 194 | storres | if ub > sdub: |
733 | 194 | storres | ub = sdub |
734 | 194 | storres | if lb == ub: |
735 | 194 | storres | raise Exception("Cant shrink interval \ |
736 | 194 | storres | anymore to get Coppersmith condition.") |
737 | 194 | storres | nbw = 0 |
738 | 194 | storres | continue |
739 | 194 | storres | #### We have at least two polynomials. |
740 | 194 | storres | # Let us try to compute resultants. |
741 | 194 | storres | # For each resultant computed, go for the solutions. |
742 | 194 | storres | ##### Build the pairs list. |
743 | 194 | storres | polyPairsList = [] |
744 | 194 | storres | for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList) - 1): |
745 | 194 | storres | for polyInnerIndex in xrange(polyOuterIndex+1, |
746 | 194 | storres | len(ccReducedPolynomialsList)): |
747 | 194 | storres | polyPairsList.append((ccReducedPolynomialsList[polyOuterIndex], |
748 | 194 | storres | ccReducedPolynomialsList[polyInnerIndex])) |
749 | 197 | storres | #### Actual root search. |
750 | 197 | storres | rootsSet = set() |
751 | 197 | storres | hasNonNullResultant = False |
752 | 194 | storres | for polyPair in polyPairsList: |
753 | 197 | storres | if hasNonNullResultant: |
754 | 197 | storres | break |
755 | 197 | storres | resultantsComputationTime = cputime() |
756 | 197 | storres | currentResultant = \ |
757 | 197 | storres | slz_resultant(polyPair[0], |
758 | 197 | storres | polyPair[1], |
759 | 197 | storres | t) |
760 | 194 | storres | resultantsComputationsFullTime += cputime(resultantsComputationTime) |
761 | 194 | storres | resultantsComputationsCount += 1 |
762 | 197 | storres | if currentResultant is None: |
763 | 197 | storres | print "Nul resultant" |
764 | 197 | storres | continue # Next polyPair. |
765 | 197 | storres | else: |
766 | 194 | storres | hasNonNullResultant = True |
767 | 197 | storres | #### We have a non null resultant. From now on, whatever the |
768 | 197 | storres | # root search yields, no extra root search is necessary. |
769 | 197 | storres | #### A constant resultant leads to no root. Root search is done. |
770 | 194 | storres | if currentResultant.degree() < 1: |
771 | 194 | storres | print "Resultant is constant:", currentResultant |
772 | 197 | storres | continue # Next polyPair and should break. |
773 | 197 | storres | #### Actual roots computation. |
774 | 197 | storres | rootsComputationTime = cputime() |
775 | 194 | storres | ##### Compute i roots |
776 | 194 | storres | iRootsList = Zi(currentResultant).roots() |
777 | 197 | storres | ##### For each iRoot, compute the corresponding tRoots and |
778 | 197 | storres | # and build populate the .rootsSet. |
779 | 194 | storres | for iRoot in iRootsList: |
780 | 194 | storres | ####### Roots returned by roots() are (value, multiplicity) |
781 | 194 | storres | # tuples. |
782 | 194 | storres | #print "iRoot:", iRoot |
783 | 194 | storres | ###### Use the tRoot against each polynomial, alternatively. |
784 | 197 | storres | for indexInPair in range(0,2): |
785 | 197 | storres | currentPolynomial = polyPair[indexInPair] |
786 | 194 | storres | ####### If the polynomial is univariate, just drop it. |
787 | 194 | storres | if len(currentPolynomial.variables()) < 2: |
788 | 194 | storres | print " Current polynomial is not in two variables." |
789 | 197 | storres | continue # Next indexInPair |
790 | 194 | storres | tRootsList = \ |
791 | 194 | storres | Zt(currentPolynomial.subs({currentPolynomial.variables()[0]:iRoot[0]})).roots() |
792 | 194 | storres | ####### The tRootsList can be empty, hence the test. |
793 | 194 | storres | if len(tRootsList) == 0: |
794 | 194 | storres | print " No t root." |
795 | 197 | storres | continue # Next indexInPair |
796 | 194 | storres | for tRoot in tRootsList: |
797 | 197 | storres | rootsSet.add((iRoot[0], tRoot[0])) |
798 | 197 | storres | # End of roots computation. |
799 | 197 | storres | rootsComputationsFullTime = cputime(rootsComputationTime) |
800 | 197 | storres | rootsComputationsCount += 1 |
801 | 197 | storres | # End loop for polyPair in polyParsList. Will break at next iteration. |
802 | 197 | storres | # since a non null resultant was found. |
803 | 197 | storres | #### Prepare for results for the current interval.. |
804 | 194 | storres | intervalResultsList = [] |
805 | 194 | storres | intervalResultsList.append((lb, ub)) |
806 | 194 | storres | #### Check roots. |
807 | 194 | storres | rootsResultsList = [] |
808 | 197 | storres | for root in rootsSet: |
809 | 194 | storres | specificRootResultsList = [] |
810 | 194 | storres | failingBounds = [] |
811 | 194 | storres | intIntPdivN = intIntP(root[0], root[1]) / N |
812 | 194 | storres | if int(intIntPdivN) != intIntPdivN: |
813 | 194 | storres | continue # Next root |
814 | 194 | storres | # Root qualifies for modular equation, test it for hardness to round. |
815 | 194 | storres | hardToRoundCaseAsFloat = RRR((icAsInt + root[0]) / toIntegerFactor) |
816 | 194 | storres | #print "Before unscaling:", hardToRoundCaseAsFloat.n(prec=precision) |
817 | 194 | storres | #print scalingFunction |
818 | 194 | storres | scaledHardToRoundCaseAsFloat = \ |
819 | 194 | storres | scalingFunction(hardToRoundCaseAsFloat) |
820 | 194 | storres | print "Candidate HTRNc at x =", \ |
821 | 194 | storres | scaledHardToRoundCaseAsFloat.n().str(base=2), |
822 | 194 | storres | if slz_is_htrn(scaledHardToRoundCaseAsFloat, |
823 | 194 | storres | function, |
824 | 194 | storres | 2^-(targetHardnessToRound), |
825 | 194 | storres | RRR): |
826 | 194 | storres | print hardToRoundCaseAsFloat, "is HTRN case." |
827 | 194 | storres | if lb <= hardToRoundCaseAsFloat and hardToRoundCaseAsFloat <= ub: |
828 | 194 | storres | print "Found in interval." |
829 | 194 | storres | else: |
830 | 194 | storres | print "Found out of interval." |
831 | 194 | storres | specificRootResultsList.append(hardToRoundCaseAsFloat.n().str(base=2)) |
832 | 194 | storres | # Check the root is in the bounds |
833 | 194 | storres | if abs(root[0]) > iBound or abs(root[1]) > tBound: |
834 | 197 | storres | print "Root", root, "is out of bounds for modular equation." |
835 | 194 | storres | if abs(root[0]) > iBound: |
836 | 194 | storres | print "root[0]:", root[0] |
837 | 194 | storres | print "i bound:", iBound |
838 | 194 | storres | failingBounds.append('i') |
839 | 194 | storres | failingBounds.append(root[0]) |
840 | 194 | storres | failingBounds.append(iBound) |
841 | 194 | storres | if abs(root[1]) > tBound: |
842 | 194 | storres | print "root[1]:", root[1] |
843 | 194 | storres | print "t bound:", tBound |
844 | 194 | storres | failingBounds.append('t') |
845 | 194 | storres | failingBounds.append(root[1]) |
846 | 194 | storres | failingBounds.append(tBound) |
847 | 194 | storres | if len(failingBounds) > 0: |
848 | 194 | storres | specificRootResultsList.append(failingBounds) |
849 | 194 | storres | else: # From slz_is_htrn... |
850 | 194 | storres | print "is not an HTRN case." |
851 | 194 | storres | if len(specificRootResultsList) > 0: |
852 | 194 | storres | rootsResultsList.append(specificRootResultsList) |
853 | 194 | storres | if len(rootsResultsList) > 0: |
854 | 194 | storres | intervalResultsList.append(rootsResultsList) |
855 | 197 | storres | ### Check if a non null resultant was found. If not shrink the interval. |
856 | 197 | storres | if not hasNonNullResultant: |
857 | 197 | storres | print "Only null resultants for this reduction, shrinking interval." |
858 | 197 | storres | resultCondFailed = True |
859 | 197 | storres | resultCondFailedCount += 1 |
860 | 197 | storres | ### Shrink interval for next iteration. |
861 | 197 | storres | ub = lb + bw * onlyNullResultantsShrink |
862 | 197 | storres | if ub > sdub: |
863 | 197 | storres | ub = sdub |
864 | 197 | storres | nbw = 0 |
865 | 197 | storres | continue |
866 | 194 | storres | #### An intervalResultsList has at least the bounds. |
867 | 194 | storres | globalResultsList.append(intervalResultsList) |
868 | 194 | storres | #### Compute an incremented width for next upper bound, only |
869 | 194 | storres | # if not Coppersmith condition nor resultant condition |
870 | 194 | storres | # failed at the previous run. |
871 | 194 | storres | if not coppCondFailed and not resultCondFailed: |
872 | 194 | storres | nbw = noErrorIntervalStretch * bw |
873 | 194 | storres | else: |
874 | 194 | storres | nbw = bw |
875 | 194 | storres | ##### Reset the failure flags. They will be raised |
876 | 194 | storres | # again if needed. |
877 | 194 | storres | coppCondFailed = False |
878 | 194 | storres | resultCondFailed = False |
879 | 194 | storres | #### For next iteration (at end of loop) |
880 | 194 | storres | #print "nbw:", nbw |
881 | 194 | storres | lb = ub |
882 | 194 | storres | ub += nbw |
883 | 194 | storres | if ub > sdub: |
884 | 194 | storres | ub = sdub |
885 | 194 | storres | |
886 | 194 | storres | # End while True |
887 | 194 | storres | ## Main loop just ended. |
888 | 194 | storres | globalWallTime = walltime(wallTimeStart) |
889 | 194 | storres | globalCpuTime = cputime(cpuTimeStart) |
890 | 194 | storres | ## Output results |
891 | 194 | storres | print ; print "Intervals and HTRNs" ; print |
892 | 194 | storres | for intervalResultsList in globalResultsList: |
893 | 194 | storres | print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]", |
894 | 194 | storres | if len(intervalResultsList) > 1: |
895 | 194 | storres | rootsResultsList = intervalResultsList[1] |
896 | 194 | storres | for specificRootResultsList in rootsResultsList: |
897 | 194 | storres | print "\t", specificRootResultsList[0], |
898 | 194 | storres | if len(specificRootResultsList) > 1: |
899 | 194 | storres | print specificRootResultsList[1], |
900 | 194 | storres | print ; print |
901 | 194 | storres | #print globalResultsList |
902 | 194 | storres | # |
903 | 194 | storres | print "Timers and counters" |
904 | 194 | storres | |
905 | 194 | storres | print "Number of iterations:", iterCount |
906 | 194 | storres | print "Taylor condition failures:", taylCondFailedCount |
907 | 194 | storres | print "Coppersmith condition failures:", coppCondFailedCount |
908 | 194 | storres | print "Resultant condition failures:", resultCondFailedCount |
909 | 194 | storres | print "Iterations count: ", iterCount |
910 | 194 | storres | print "Number of intervals:", len(globalResultsList) |
911 | 194 | storres | print "Number of basis constructions:", basisConstructionsCount |
912 | 194 | storres | print "Total CPU time spent in basis constructions:", \ |
913 | 194 | storres | basisConstructionsFullTime |
914 | 194 | storres | if basisConstructionsCount != 0: |
915 | 194 | storres | print "Average basis construction CPU time:", \ |
916 | 194 | storres | basisConstructionsFullTime/basisConstructionsCount |
917 | 194 | storres | print "Number of reductions:", reductionsCount |
918 | 194 | storres | print "Total CPU time spent in reductions:", reductionsFullTime |
919 | 194 | storres | if reductionsCount != 0: |
920 | 194 | storres | print "Average reduction CPU time:", \ |
921 | 194 | storres | reductionsFullTime/reductionsCount |
922 | 194 | storres | print "Number of resultants computation rounds:", \ |
923 | 194 | storres | resultantsComputationsCount |
924 | 194 | storres | print "Total CPU time spent in resultants computation rounds:", \ |
925 | 194 | storres | resultantsComputationsFullTime |
926 | 194 | storres | if resultantsComputationsCount != 0: |
927 | 194 | storres | print "Average resultants computation round CPU time:", \ |
928 | 194 | storres | resultantsComputationsFullTime/resultantsComputationsCount |
929 | 194 | storres | print "Number of root finding rounds:", rootsComputationsCount |
930 | 194 | storres | print "Total CPU time spent in roots finding rounds:", \ |
931 | 194 | storres | rootsComputationsFullTime |
932 | 194 | storres | if rootsComputationsCount != 0: |
933 | 194 | storres | print "Average roots finding round CPU time:", \ |
934 | 194 | storres | rootsComputationsFullTime/rootsComputationsCount |
935 | 194 | storres | print "Global Wall time:", globalWallTime |
936 | 194 | storres | print "Global CPU time:", globalCpuTime |
937 | 194 | storres | ## Output counters |
938 | 194 | storres | # End srs_runSLZ-v02 |
939 | 194 | storres |