Révision 122 pobysoPythonSage/src/sageSLZ/sageSLZ.sage
sageSLZ.sage (revision 122) | ||
---|---|---|
86 | 86 |
RRR(rationalPolynomialOfIntegers(upperBoundAsIntegerSa)) |
87 | 87 |
|
88 | 88 |
# End slz_check_htr_value. |
89 |
|
|
89 | 90 |
# |
90 | 91 |
def slz_compute_binade_bounds(number, emin, emax=sys.maxint): |
91 | 92 |
""" |
... | ... | |
168 | 169 |
return (lb, ub) |
169 | 170 |
# End slz_compute_binade_bounds |
170 | 171 |
# |
172 |
def slz_compute_integer_polynomial_modular_roots(inputPolynomial, |
|
173 |
alpha, |
|
174 |
N, |
|
175 |
iBound, |
|
176 |
tBound): |
|
177 |
""" |
|
178 |
For a given set of arguments (see below) , compute the polynomial modular |
|
179 |
roots, if any. |
|
180 |
""" |
|
181 |
nAtAlpha = N^alpha |
|
182 |
## Building polynomials for matrix. |
|
183 |
polyRing = inputPolynomial.parent() |
|
184 |
# Whatever the 2 variables are actually called, we call them |
|
185 |
# 'i' and 't' in all the variable names. |
|
186 |
(iVariable, tVariable) = inputPolynomial.variables()[:2] |
|
187 |
#print polyVars[0], type(polyVars[0]) |
|
188 |
initialPolynomial = inputPolynomial.subs({iVariable:iVariable * iBound, |
|
189 |
tVariable:tVariable * tBound}) |
|
190 |
polynomialsList = \ |
|
191 |
spo_polynomial_to_polynomials_list_5(initialPolynomial, |
|
192 |
alpha, |
|
193 |
N, |
|
194 |
iBound, |
|
195 |
tBound, |
|
196 |
10) |
|
197 |
print "Polynomials list:", polynomialsList |
|
198 |
## Building the proto matrix. |
|
199 |
knownMonomials = [] |
|
200 |
protoMatrix = [] |
|
201 |
for poly in polynomialsList: |
|
202 |
spo_add_polynomial_coeffs_to_matrix_row(poly, |
|
203 |
knownMonomials, |
|
204 |
protoMatrix, |
|
205 |
0) |
|
206 |
matrixToReduce = spo_proto_to_row_matrix(protoMatrix) |
|
207 |
#print matrixToReduce |
|
208 |
## Reduction and checking. |
|
209 |
reducedMatrix = matrixToReduce.LLL(fp='fp') |
|
210 |
isLLLReduced = reducedMatrix.is_LLL_reduced() |
|
211 |
if not isLLLReduced: |
|
212 |
return None |
|
213 |
monomialsCount = len(knownMonomials) |
|
214 |
monomialsCountSqrt = sqrt(monomialsCount) |
|
215 |
## Check the Coppersmith condition for each row and build the reduced |
|
216 |
# polynomials. |
|
217 |
ccReducedPolynomialsList = [] |
|
218 |
for row in reducedMatrix.rows(): |
|
219 |
l2Norm = row.norm(2) |
|
220 |
if (l2Norm * monomialsCountSqrt < nAtAlpha): |
|
221 |
print (l2Norm * monomialsCountSqrt).n() |
|
222 |
ccReducedPolynomial = \ |
|
223 |
slz_compute_reduced_polynomial(row, |
|
224 |
knownMonomials, |
|
225 |
iVariable, |
|
226 |
iBound, |
|
227 |
tVariable, |
|
228 |
tBound) |
|
229 |
if not ccReducedPolynomial is None: |
|
230 |
ccReducedPolynomialsList.append(ccReducedPolynomial) |
|
231 |
if len(ccReducedPolynomialsList) < 2: |
|
232 |
print "Less than 2 Coppersmith condition compliant vectors." |
|
233 |
return None |
|
234 |
## Create the valid (poly1 and poly2 are algebraically independent) |
|
235 |
# resultant tuples (poly1, poly2, resultant(poly1, poly2)). |
|
236 |
# Try to mix and match all the polynomial pairs built from the |
|
237 |
# ccReducedPolynomialsList to obtain non zero resultants. |
|
238 |
resultantsInITuplesList = [] |
|
239 |
for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList)-1): |
|
240 |
for polyInnerIndex in xrange(polyOuterIndex+1, |
|
241 |
len(ccReducedPolynomialsList)): |
|
242 |
# Compute the resultant in resultants in the |
|
243 |
# first variable (is it the optimal choice?). |
|
244 |
resultantInI = \ |
|
245 |
ccReducedPolynomialsList[polyOuterIndex].resultant(ccReducedPolynomialsList[polyInnerIndex], |
|
246 |
ccReducedPolynomialsList[0].parent(str(iVariable))) |
|
247 |
#print resultantInI |
|
248 |
#print "Resultant", resultantInI |
|
249 |
# Test algebraic independence. |
|
250 |
if not resultantInI.is_zero(): |
|
251 |
resultantsInITuplesList.append((ccReducedPolynomialsList[polyOuterIndex], |
|
252 |
ccReducedPolynomialsList[polyInnerIndex], |
|
253 |
resultantInI)) |
|
254 |
# If no non zero resultant was found: we can't get no algebraically |
|
255 |
# independent polynomials pair. Give up! |
|
256 |
if len(resultantsInITuplesList) == 0: |
|
257 |
return None |
|
258 |
# Compute the roots. |
|
259 |
Zi = ZZ[str(iVariable)] |
|
260 |
Zt = ZZ[str(tVariable)] |
|
261 |
polynomialRootsSet = set() |
|
262 |
# First, solve in the second variable since resultants are in the first |
|
263 |
# variable. |
|
264 |
for resultantInITuple in resultantsInITuplesList: |
|
265 |
tRootsList = Zt(resultantInITuple[2]).roots() |
|
266 |
# For each tRoot, compute the corresponding iRoots and check |
|
267 |
# them in the polynomial. |
|
268 |
for tRoot in tRootsList: |
|
269 |
print "tRoot:", tRoot |
|
270 |
# Roots returned by root() are (value, multiplicity) tuples. |
|
271 |
iRootsList = \ |
|
272 |
Zi(resultantInITuple[0].subs({resultantInITuple[0].variables()[1]:tRoot[0]})).roots() |
|
273 |
# The iRootsList can be empty, hence the test. |
|
274 |
if len(iRootsList) != 0: |
|
275 |
for iRoot in iRootsList: |
|
276 |
polyEvalModN = inputPolynomial(iRoot[0], tRoot[0]) / N |
|
277 |
# polyEvalModN must be an integer. |
|
278 |
if polyEvalModN == int(polyEvalModN): |
|
279 |
polynomialRootsSet.add((iRoot[0],tRoot[0])) |
|
280 |
return polynomialRootsSet |
|
281 |
# End slz_compute_integer_polynomial_modular_roots. |
|
282 |
# |
|
171 | 283 |
def slz_compute_polynomial_and_interval(functionSo, degreeSo, lowerBoundSa, |
172 | 284 |
upperBoundSa, approxPrecSa, |
173 | 285 |
sollyaPrecSa=None): |
... | ... | |
245 | 357 |
return((polySo, currentRangeSo, intervalCenterSo, maxErrorSo)) |
246 | 358 |
# End slz_compute_polynomial_and_interval |
247 | 359 |
|
248 |
def slz_compute_reduced_polynomials(reducedMatrix,
|
|
360 |
def slz_compute_reduced_polynomial(matrixRow,
|
|
249 | 361 |
knownMonomials, |
250 | 362 |
var1, |
251 | 363 |
var1Bound, |
252 | 364 |
var2, |
253 | 365 |
var2Bound): |
254 | 366 |
""" |
367 |
Compute a polynomial from a reduced matrix row. |
|
368 |
This function was introduced in order to avoid the computation of the |
|
369 |
polynomials (even those built from rows that do no verify the Coppersmith |
|
370 |
condition. |
|
371 |
""" |
|
372 |
## Check arguments. |
|
373 |
if len(knownMonomials) == 0: |
|
374 |
return None |
|
375 |
# varNounds can be zero since 0^0 returns 1. |
|
376 |
if (var1Bound < 0) or (var2Bound < 0): |
|
377 |
return None |
|
378 |
## Initialisations. |
|
379 |
polynomialRing = knownMonomials[0].parent() |
|
380 |
currentPolynomial = polynomialRing(0) |
|
381 |
for colIndex in xrange(0, len(knownMonomials)): |
|
382 |
currentCoefficient = matrixRow[colIndex] |
|
383 |
if currentCoefficient != 0: |
|
384 |
#print "Current coefficient:", currentCoefficient |
|
385 |
currentMonomial = knownMonomials[colIndex] |
|
386 |
#print "Monomial as multivariate polynomial:", \ |
|
387 |
#currentMonomial, type(currentMonomial) |
|
388 |
degreeInVar1 = currentMonomial.degree(var1) |
|
389 |
#print "Degree in var", var1, ":", degreeInVar1 |
|
390 |
degreeInVar2 = currentMonomial.degree(var2) |
|
391 |
#print "Degree in var", var2, ":", degreeInVar2 |
|
392 |
if degreeInVar1 > 0: |
|
393 |
currentCoefficient = \ |
|
394 |
currentCoefficient / var1Bound^degreeInVar1 |
|
395 |
#print "varBound1 in degree:", var1Bound^degreeInVar1 |
|
396 |
#print "Current coefficient(1)", currentCoefficient |
|
397 |
if degreeInVar2 > 0: |
|
398 |
currentCoefficient = \ |
|
399 |
currentCoefficient / var2Bound^degreeInVar2 |
|
400 |
#print "Current coefficient(2)", currentCoefficient |
|
401 |
#print "Current reduced monomial:", (currentCoefficient * \ |
|
402 |
# currentMonomial) |
|
403 |
currentPolynomial += (currentCoefficient * currentMonomial) |
|
404 |
#print "Current polynomial:", currentPolynomial |
|
405 |
# End if |
|
406 |
# End for colIndex. |
|
407 |
#print "Type of the current polynomial:", type(currentPolynomial) |
|
408 |
return(currentPolynomial) |
|
409 |
# End slz_compute_reduced_polynomial |
|
410 |
# |
|
411 |
def slz_compute_reduced_polynomials(reducedMatrix, |
|
412 |
knownMonomials, |
|
413 |
var1, |
|
414 |
var1Bound, |
|
415 |
var2, |
|
416 |
var2Bound): |
|
417 |
""" |
|
418 |
Legacy function, use slz_compute_reduced_polynomials_list |
|
419 |
""" |
|
420 |
return(slz_compute_reduced_polynomials_list(reducedMatrix, |
|
421 |
knownMonomials, |
|
422 |
var1, |
|
423 |
var1Bound, |
|
424 |
var2, |
|
425 |
var2Bound) |
|
426 |
) |
|
427 |
def slz_compute_reduced_polynomials_list(reducedMatrix, |
|
428 |
knownMonomials, |
|
429 |
var1, |
|
430 |
var1Bound, |
|
431 |
var2, |
|
432 |
var2Bound): |
|
433 |
""" |
|
255 | 434 |
From a reduced matrix, holding the coefficients, from a monomials list, |
256 | 435 |
from the bounds of each variable, compute the corresponding polynomials |
257 | 436 |
scaled back by dividing by the "right" powers of the variables bounds. |
... | ... | |
660 | 839 |
|
661 | 840 |
|
662 | 841 |
print "\t...sageSLZ loaded" |
842 |
|
Formats disponibles : Unified diff