456 |
456 |
## Output results
|
457 |
457 |
print ; print "Intervals and HTRNs" ; print
|
458 |
458 |
for intervalResultsList in globalResultsList:
|
459 |
|
print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]",
|
|
459 |
intervalResultString = "[" + str(intervalResultsList[0][0]) +\
|
|
460 |
"," + str(intervalResultsList[0][1]) + "]"
|
|
461 |
print intervalResultString,
|
460 |
462 |
if len(intervalResultsList) > 1:
|
461 |
463 |
rootsResultsList = intervalResultsList[1]
|
|
464 |
specificRootResultIndex = 0
|
462 |
465 |
for specificRootResultsList in rootsResultsList:
|
463 |
|
print "\t", specificRootResultsList[0],
|
|
466 |
if specificRootResultIndex == 0:
|
|
467 |
print "\t", specificRootResultsList[0],
|
|
468 |
else:
|
|
469 |
print " " * len(intervalResultString), "\t", \
|
|
470 |
specificRootResultsList[0],
|
464 |
471 |
if len(specificRootResultsList) > 1:
|
465 |
|
print specificRootResultsList[1],
|
|
472 |
print specificRootResultsList[1]
|
|
473 |
specificRootResultIndex += 1
|
466 |
474 |
print ; print
|
467 |
475 |
#print globalResultsList
|
468 |
476 |
#
|
... | ... | |
920 |
928 |
## Output results
|
921 |
929 |
print ; print "Intervals and HTRNs" ; print
|
922 |
930 |
for intervalResultsList in globalResultsList:
|
923 |
|
print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]",
|
|
931 |
intervalResultString = "[" + str(intervalResultsList[0][0]) +\
|
|
932 |
"," + str(intervalResultsList[0][1]) + "]"
|
|
933 |
print intervalResultString,
|
924 |
934 |
if len(intervalResultsList) > 1:
|
925 |
935 |
rootsResultsList = intervalResultsList[1]
|
|
936 |
specificRootResultIndex = 0
|
926 |
937 |
for specificRootResultsList in rootsResultsList:
|
927 |
|
print "\t", specificRootResultsList[0],
|
|
938 |
if specificRootResultIndex == 0:
|
|
939 |
print "\t", specificRootResultsList[0],
|
|
940 |
else:
|
|
941 |
print " " * len(intervalResultString), "\t", \
|
|
942 |
specificRootResultsList[0],
|
928 |
943 |
if len(specificRootResultsList) > 1:
|
929 |
|
print specificRootResultsList[1],
|
|
944 |
print specificRootResultsList[1]
|
|
945 |
specificRootResultIndex += 1
|
930 |
946 |
print ; print
|
931 |
947 |
#print globalResultsList
|
932 |
948 |
#
|
... | ... | |
1395 |
1411 |
## Output results
|
1396 |
1412 |
print ; print "Intervals and HTRNs" ; print
|
1397 |
1413 |
for intervalResultsList in globalResultsList:
|
1398 |
|
print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]",
|
|
1414 |
intervalResultString = "[" + str(intervalResultsList[0][0]) +\
|
|
1415 |
"," + str(intervalResultsList[0][1]) + "]"
|
|
1416 |
print intervalResultString,
|
1399 |
1417 |
if len(intervalResultsList) > 1:
|
1400 |
1418 |
rootsResultsList = intervalResultsList[1]
|
|
1419 |
specificRootResultIndex = 0
|
1401 |
1420 |
for specificRootResultsList in rootsResultsList:
|
1402 |
|
print "\t", specificRootResultsList[0],
|
|
1421 |
if specificRootResultIndex == 0:
|
|
1422 |
print "\t", specificRootResultsList[0],
|
|
1423 |
else:
|
|
1424 |
print " " * len(intervalResultString), "\t", \
|
|
1425 |
specificRootResultsList[0],
|
1403 |
1426 |
if len(specificRootResultsList) > 1:
|
1404 |
|
print specificRootResultsList[1],
|
|
1427 |
print specificRootResultsList[1]
|
|
1428 |
specificRootResultIndex += 1
|
1405 |
1429 |
print ; print
|
1406 |
1430 |
#print globalResultsList
|
1407 |
1431 |
#
|
... | ... | |
1889 |
1913 |
## Output results
|
1890 |
1914 |
print ; print "Intervals and HTRNs" ; print
|
1891 |
1915 |
for intervalResultsList in globalResultsList:
|
1892 |
|
print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]",
|
|
1916 |
intervalResultString = "[" + str(intervalResultsList[0][0]) +\
|
|
1917 |
"," + str(intervalResultsList[0][1]) + "]"
|
|
1918 |
print intervalResultString,
|
1893 |
1919 |
if len(intervalResultsList) > 1:
|
1894 |
1920 |
rootsResultsList = intervalResultsList[1]
|
|
1921 |
specificRootResultIndex = 0
|
1895 |
1922 |
for specificRootResultsList in rootsResultsList:
|
1896 |
|
print "\t", specificRootResultsList[0],
|
|
1923 |
if specificRootResultIndex == 0:
|
|
1924 |
print "\t", specificRootResultsList[0],
|
|
1925 |
else:
|
|
1926 |
print " " * len(intervalResultString), "\t", \
|
|
1927 |
specificRootResultsList[0],
|
1897 |
1928 |
if len(specificRootResultsList) > 1:
|
1898 |
|
print specificRootResultsList[1],
|
|
1929 |
print specificRootResultsList[1]
|
|
1930 |
specificRootResultIndex += 1
|
1899 |
1931 |
print ; print
|
1900 |
1932 |
#print globalResultsList
|
1901 |
1933 |
#
|
... | ... | |
2349 |
2381 |
## Output results
|
2350 |
2382 |
print ; print "Intervals and HTRNs" ; print
|
2351 |
2383 |
for intervalResultsList in globalResultsList:
|
2352 |
|
print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]",
|
|
2384 |
intervalResultString = "[" + str(intervalResultsList[0][0]) +\
|
|
2385 |
"," + str(intervalResultsList[0][1]) + "]"
|
|
2386 |
print intervalResultString,
|
2353 |
2387 |
if len(intervalResultsList) > 1:
|
2354 |
2388 |
rootsResultsList = intervalResultsList[1]
|
|
2389 |
specificRootResultIndex = 0
|
2355 |
2390 |
for specificRootResultsList in rootsResultsList:
|
2356 |
|
print "\t", specificRootResultsList[0],
|
|
2391 |
if specificRootResultIndex == 0:
|
|
2392 |
print "\t", specificRootResultsList[0],
|
|
2393 |
else:
|
|
2394 |
print " " * len(intervalResultString), "\t", \
|
|
2395 |
specificRootResultsList[0],
|
2357 |
2396 |
if len(specificRootResultsList) > 1:
|
2358 |
|
print specificRootResultsList[1],
|
|
2397 |
print specificRootResultsList[1]
|
|
2398 |
specificRootResultIndex += 1
|
2359 |
2399 |
print ; print
|
2360 |
2400 |
#print globalResultsList
|
2361 |
2401 |
#
|
... | ... | |
2824 |
2864 |
## Output results
|
2825 |
2865 |
print ; print "Intervals and HTRNs" ; print
|
2826 |
2866 |
for intervalResultsList in globalResultsList:
|
2827 |
|
print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]",
|
|
2867 |
intervalResultString = "[" + str(intervalResultsList[0][0]) +\
|
|
2868 |
"," + str(intervalResultsList[0][1]) + "]"
|
|
2869 |
print intervalResultString,
|
2828 |
2870 |
if len(intervalResultsList) > 1:
|
2829 |
2871 |
rootsResultsList = intervalResultsList[1]
|
|
2872 |
specificRootResultIndex = 0
|
2830 |
2873 |
for specificRootResultsList in rootsResultsList:
|
2831 |
|
print "\t", specificRootResultsList[0],
|
|
2874 |
if specificRootResultIndex == 0:
|
|
2875 |
print "\t", specificRootResultsList[0],
|
|
2876 |
else:
|
|
2877 |
print " " * len(intervalResultString), "\t", \
|
|
2878 |
specificRootResultsList[0],
|
2832 |
2879 |
if len(specificRootResultsList) > 1:
|
2833 |
|
print specificRootResultsList[1],
|
|
2880 |
print specificRootResultsList[1]
|
|
2881 |
specificRootResultIndex += 1
|
2834 |
2882 |
print ; print
|
2835 |
2883 |
#print globalResultsList
|
2836 |
2884 |
#
|
... | ... | |
2870 |
2918 |
print "Global CPU time:", globalCpuTime
|
2871 |
2919 |
## Output counters
|
2872 |
2920 |
# End srs_runSLZ-v05
|
|
2921 |
|
|
2922 |
def srs_run_SLZ_v06(inputFunction,
|
|
2923 |
inputLowerBound,
|
|
2924 |
inputUpperBound,
|
|
2925 |
alpha,
|
|
2926 |
degree,
|
|
2927 |
precision,
|
|
2928 |
emin,
|
|
2929 |
emax,
|
|
2930 |
targetHardnessToRound,
|
|
2931 |
debug = True):
|
|
2932 |
"""
|
|
2933 |
Changes from V5:
|
|
2934 |
Very verbose
|
|
2935 |
Changes from V4:
|
|
2936 |
Approximation polynomial has coefficients rounded.
|
|
2937 |
Changes from V3:
|
|
2938 |
Root search is changed again:
|
|
2939 |
- only resultants in i are computed;
|
|
2940 |
- roots in i are searched for;
|
|
2941 |
- if any, they are tested for hardness-to-round.
|
|
2942 |
Changes from V2:
|
|
2943 |
Root search is changed:
|
|
2944 |
- we compute the resultants in i and in t;
|
|
2945 |
- we compute the roots set of each of these resultants;
|
|
2946 |
- we combine all the possible pairs between the two sets;
|
|
2947 |
- we check these pairs in polynomials for correctness.
|
|
2948 |
Changes from V1:
|
|
2949 |
1- check for roots as soon as a resultant is computed;
|
|
2950 |
2- once a non null resultant is found, check for roots;
|
|
2951 |
3- constant resultant == no root.
|
|
2952 |
"""
|
|
2953 |
|
|
2954 |
if debug:
|
|
2955 |
print "Function :", inputFunction
|
|
2956 |
print "Lower bound :", inputLowerBound
|
|
2957 |
print "Upper bounds :", inputUpperBound
|
|
2958 |
print "Alpha :", alpha
|
|
2959 |
print "Degree :", degree
|
|
2960 |
print "Precision :", precision
|
|
2961 |
print "Emin :", emin
|
|
2962 |
print "Emax :", emax
|
|
2963 |
print "Target hardness-to-round:", targetHardnessToRound
|
|
2964 |
print
|
|
2965 |
## Important constants.
|
|
2966 |
### Stretch the interval if no error happens.
|
|
2967 |
noErrorIntervalStretch = 1 + 2^(-5)
|
|
2968 |
### If no vector validates the Coppersmith condition, shrink the interval
|
|
2969 |
# by the following factor.
|
|
2970 |
noCoppersmithIntervalShrink = 1/2
|
|
2971 |
### If only (or at least) one vector validates the Coppersmith condition,
|
|
2972 |
# shrink the interval by the following factor.
|
|
2973 |
oneCoppersmithIntervalShrink = 3/4
|
|
2974 |
#### If only null resultants are found, shrink the interval by the
|
|
2975 |
# following factor.
|
|
2976 |
onlyNullResultantsShrink = 3/4
|
|
2977 |
## Structures.
|
|
2978 |
RRR = RealField(precision)
|
|
2979 |
RRIF = RealIntervalField(precision)
|
|
2980 |
## Converting input bound into the "right" field.
|
|
2981 |
lowerBound = RRR(inputLowerBound)
|
|
2982 |
upperBound = RRR(inputUpperBound)
|
|
2983 |
## Before going any further, check domain and image binade conditions.
|
|
2984 |
print inputFunction(1).n()
|
|
2985 |
output = slz_fix_bounds_for_binades(lowerBound, upperBound, inputFunction)
|
|
2986 |
if output is None:
|
|
2987 |
print "Invalid domain/image binades. Domain:",\
|
|
2988 |
lowerBound, upperBound, "Images:", \
|
|
2989 |
inputFunction(lowerBound), inputFunction(upperBound)
|
|
2990 |
raise Exception("Invalid domain/image binades.")
|
|
2991 |
lb = output[0] ; ub = output[1]
|
|
2992 |
if lb != lowerBound or ub != upperBound:
|
|
2993 |
print "lb:", lb, " - ub:", ub
|
|
2994 |
print "Invalid domain/image binades. Domain:",\
|
|
2995 |
lowerBound, upperBound, "Images:", \
|
|
2996 |
inputFunction(lowerBound), inputFunction(upperBound)
|
|
2997 |
raise Exception("Invalid domain/image binades.")
|
|
2998 |
#
|
|
2999 |
## Progam initialization
|
|
3000 |
### Approximation polynomial accuracy and hardness to round.
|
|
3001 |
polyApproxAccur = 2^(-(targetHardnessToRound + 1))
|
|
3002 |
polyTargetHardnessToRound = targetHardnessToRound + 1
|
|
3003 |
### Significand to integer conversion ratio.
|
|
3004 |
toIntegerFactor = 2^(precision-1)
|
|
3005 |
print "Polynomial approximation required accuracy:", polyApproxAccur.n()
|
|
3006 |
### Variables and rings for polynomials and root searching.
|
|
3007 |
i=var('i')
|
|
3008 |
t=var('t')
|
|
3009 |
inputFunctionVariable = inputFunction.variables()[0]
|
|
3010 |
function = inputFunction.subs({inputFunctionVariable:i})
|
|
3011 |
# Polynomial Rings over the integers, for root finding.
|
|
3012 |
Zi = ZZ[i]
|
|
3013 |
## Number of iterations limit.
|
|
3014 |
maxIter = 100000
|
|
3015 |
#
|
|
3016 |
## Compute the scaled function and the degree, in their Sollya version
|
|
3017 |
# once for all.
|
|
3018 |
(scaledf, sdlb, sdub, silb, siub) = \
|
|
3019 |
slz_compute_scaled_function(function, lowerBound, upperBound, precision)
|
|
3020 |
print "Scaled function:", scaledf._assume_str().replace('_SAGE_VAR_', '')
|
|
3021 |
scaledfSo = sollya_lib_parse_string(scaledf._assume_str().replace('_SAGE_VAR_', ''))
|
|
3022 |
degreeSo = pobyso_constant_from_int_sa_so(degree)
|
|
3023 |
#
|
|
3024 |
## Compute the scaling. boundsIntervalRifSa defined out of the loops.
|
|
3025 |
domainBoundsInterval = RRIF(lowerBound, upperBound)
|
|
3026 |
(unscalingFunction, scalingFunction) = \
|
|
3027 |
slz_interval_scaling_expression(domainBoundsInterval, i)
|
|
3028 |
#print scalingFunction, unscalingFunction
|
|
3029 |
## Set the Sollya internal precision (with an arbitrary minimum of 192).
|
|
3030 |
internalSollyaPrec = ceil((RR('1.5') * targetHardnessToRound) / 64) * 64
|
|
3031 |
if internalSollyaPrec < 192:
|
|
3032 |
internalSollyaPrec = 192
|
|
3033 |
pobyso_set_prec_sa_so(internalSollyaPrec)
|
|
3034 |
print "Sollya internal precision:", internalSollyaPrec
|
|
3035 |
targetPlusOnePrecRF = RealField(RRR.prec()+1)
|
|
3036 |
if internalSollyaPrec < 1024:
|
|
3037 |
quasiExactRF = RealField(1014)
|
|
3038 |
else:
|
|
3039 |
quasiExactRF = RealField(internalSollyaPrec)
|
|
3040 |
## Some variables.
|
|
3041 |
### General variables
|
|
3042 |
lb = sdlb
|
|
3043 |
ub = sdub
|
|
3044 |
nbw = 0
|
|
3045 |
intervalUlp = ub.ulp()
|
|
3046 |
#### Will be set by slz_interval_and_polynomila_to_sage.
|
|
3047 |
ic = 0
|
|
3048 |
icAsInt = 0 # Set from ic.
|
|
3049 |
solutionsSet = set()
|
|
3050 |
tsErrorWidth = []
|
|
3051 |
csErrorVectors = []
|
|
3052 |
csVectorsResultants = []
|
|
3053 |
floatP = 0 # Taylor polynomial.
|
|
3054 |
floatPcv = 0 # Ditto with variable change.
|
|
3055 |
intvl = "" # Taylor interval
|
|
3056 |
terr = 0 # Taylor error.
|
|
3057 |
iterCount = 0
|
|
3058 |
htrnSet = set()
|
|
3059 |
### Timers and counters.
|
|
3060 |
wallTimeStart = 0
|
|
3061 |
cpuTimeStart = 0
|
|
3062 |
taylCondFailedCount = 0
|
|
3063 |
coppCondFailedCount = 0
|
|
3064 |
resultCondFailedCount = 0
|
|
3065 |
coppCondFailed = False
|
|
3066 |
resultCondFailed = False
|
|
3067 |
globalResultsList = []
|
|
3068 |
basisConstructionsCount = 0
|
|
3069 |
basisConstructionsFullTime = 0
|
|
3070 |
basisConstructionTime = 0
|
|
3071 |
reductionsCount = 0
|
|
3072 |
reductionsFullTime = 0
|
|
3073 |
reductionTime = 0
|
|
3074 |
resultantsComputationsCount = 0
|
|
3075 |
resultantsComputationsFullTime = 0
|
|
3076 |
resultantsComputationTime = 0
|
|
3077 |
rootsComputationsCount = 0
|
|
3078 |
rootsComputationsFullTime = 0
|
|
3079 |
rootsComputationTime = 0
|
|
3080 |
print
|
|
3081 |
## Global times are started here.
|
|
3082 |
wallTimeStart = walltime()
|
|
3083 |
cpuTimeStart = cputime()
|
|
3084 |
## Main loop.
|
|
3085 |
while True:
|
|
3086 |
if lb >= sdub:
|
|
3087 |
print "Lower bound reached upper bound."
|
|
3088 |
break
|
|
3089 |
if iterCount == maxIter:
|
|
3090 |
print "Reached maxIter. Aborting"
|
|
3091 |
break
|
|
3092 |
iterCount += 1
|
|
3093 |
print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \
|
|
3094 |
"log2(numbers)."
|
|
3095 |
### Compute a Sollya polynomial that will honor the Taylor condition.
|
|
3096 |
prceSo = slz_compute_polynomial_and_interval_01(scaledfSo,
|
|
3097 |
degreeSo,
|
|
3098 |
lb,
|
|
3099 |
ub,
|
|
3100 |
polyApproxAccur,
|
|
3101 |
debug=True)
|
|
3102 |
if debug:
|
|
3103 |
print "Sollya Taylor polynomial:", pobyso_autoprint(prceSo[0])
|
|
3104 |
print "Sollya interval :", pobyso_autoprint(prceSo[1])
|
|
3105 |
print "Sollya interval center :", pobyso_autoprint(prceSo[2])
|
|
3106 |
print "Sollya Taylor error :", pobyso_autoprint(prceSo[3])
|
|
3107 |
|
|
3108 |
### Convert back the data into Sage space.
|
|
3109 |
(floatP, floatPcv, intvl, ic, terr) = \
|
|
3110 |
slz_interval_and_polynomial_to_sage((prceSo[0], prceSo[0],
|
|
3111 |
prceSo[1], prceSo[2],
|
|
3112 |
prceSo[3]))
|
|
3113 |
intvl = RRIF(intvl)
|
|
3114 |
## Clean-up Sollya stuff.
|
|
3115 |
for elem in prceSo:
|
|
3116 |
sollya_lib_clear_obj(elem)
|
|
3117 |
#print floatP, floatPcv, intvl, ic, terr
|
|
3118 |
#print floatP
|
|
3119 |
#print intvl.endpoints()[0].n(), \
|
|
3120 |
# ic.n(),
|
|
3121 |
#intvl.endpoints()[1].n()
|
|
3122 |
### Check returned data.
|
|
3123 |
#### Is approximation error OK?
|
|
3124 |
if terr > polyApproxAccur:
|
|
3125 |
exceptionErrorMess = \
|
|
3126 |
"Approximation failed - computed error:" + \
|
|
3127 |
str(terr) + " - target error: "
|
|
3128 |
exceptionErrorMess += \
|
|
3129 |
str(polyApproxAccur) + ". Aborting!"
|
|
3130 |
raise Exception(exceptionErrorMess)
|
|
3131 |
#### Is lower bound OK?
|
|
3132 |
if lb != intvl.endpoints()[0]:
|
|
3133 |
exceptionErrorMess = "Wrong lower bound:" + \
|
|
3134 |
str(lb) + ". Aborting!"
|
|
3135 |
raise Exception(exceptionErrorMess)
|
|
3136 |
#### Set upper bound.
|
|
3137 |
if ub > intvl.endpoints()[1]:
|
|
3138 |
ub = intvl.endpoints()[1]
|
|
3139 |
print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \
|
|
3140 |
"log2(numbers)."
|
|
3141 |
taylCondFailedCount += 1
|
|
3142 |
#### Is interval not degenerate?
|
|
3143 |
if lb >= ub:
|
|
3144 |
exceptionErrorMess = "Degenerate interval: " + \
|
|
3145 |
"lowerBound(" + str(lb) +\
|
|
3146 |
")>= upperBound(" + str(ub) + \
|
|
3147 |
"). Aborting!"
|
|
3148 |
raise Exception(exceptionErrorMess)
|
|
3149 |
#### Is interval center ok?
|
|
3150 |
if ic <= lb or ic >= ub:
|
|
3151 |
exceptionErrorMess = "Invalid interval center for " + \
|
|
3152 |
str(lb) + ',' + str(ic) + ',' + \
|
|
3153 |
str(ub) + ". Aborting!"
|
|
3154 |
raise Exception(exceptionErrorMess)
|
|
3155 |
##### Current interval width and reset future interval width.
|
|
3156 |
bw = ub - lb
|
|
3157 |
nbw = 0
|
|
3158 |
icAsInt = int(ic * toIntegerFactor)
|
|
3159 |
#### The following ratio is always >= 1. In case we may want to
|
|
3160 |
# enlarge the interval
|
|
3161 |
curTaylErrRat = polyApproxAccur / terr
|
|
3162 |
### Make the integral transformations.
|
|
3163 |
#### Bounds and interval center.
|
|
3164 |
intIc = int(ic * toIntegerFactor)
|
|
3165 |
intLb = int(lb * toIntegerFactor) - intIc
|
|
3166 |
intUb = int(ub * toIntegerFactor) - intIc
|
|
3167 |
#
|
|
3168 |
#### Polynomials
|
|
3169 |
basisConstructionTime = cputime()
|
|
3170 |
##### To a polynomial with rational coefficients with rational arguments
|
|
3171 |
ratRatP = slz_float_poly_of_float_to_rat_poly_of_rat_pow_two(floatP)
|
|
3172 |
if debug:
|
|
3173 |
print "Polynomial: rational coefficients for rational argument:"
|
|
3174 |
print ratRatP
|
|
3175 |
##### To a polynomial with rational coefficients with integer arguments
|
|
3176 |
ratIntP = \
|
|
3177 |
slz_rat_poly_of_rat_to_rat_poly_of_int(ratRatP, precision)
|
|
3178 |
if debug:
|
|
3179 |
print "Polynomial: rational coefficients for integer argument:"
|
|
3180 |
print ratIntP
|
|
3181 |
##### Ultimately a multivariate polynomial with integer coefficients
|
|
3182 |
# with integer arguments.
|
|
3183 |
coppersmithTuple = \
|
|
3184 |
slz_rat_poly_of_int_to_poly_for_coppersmith(ratIntP,
|
|
3185 |
precision,
|
|
3186 |
targetHardnessToRound,
|
|
3187 |
i, t)
|
|
3188 |
#### Recover Coppersmith information.
|
|
3189 |
intIntP = coppersmithTuple[0]
|
|
3190 |
N = coppersmithTuple[1]
|
|
3191 |
nAtAlpha = N^alpha
|
|
3192 |
tBound = coppersmithTuple[2]
|
|
3193 |
leastCommonMultiple = coppersmithTuple[3]
|
|
3194 |
iBound = max(abs(intLb),abs(intUb))
|
|
3195 |
if debug:
|
|
3196 |
print "Polynomial: integer coefficients for integer argument:"
|
|
3197 |
print intIntP
|
|
3198 |
print "N:", N
|
|
3199 |
print "t bound:", tBound
|
|
3200 |
print "i bound:", iBound
|
|
3201 |
print "Least common multiple:", leastCommonMultiple
|
|
3202 |
basisConstructionsFullTime += cputime(basisConstructionTime)
|
|
3203 |
basisConstructionsCount += 1
|
|
3204 |
"""
|
|
3205 |
#### Compute the matrix to reduce.
|
|
3206 |
matrixToReduce = slz_compute_initial_lattice_matrix(intIntP,
|
|
3207 |
alpha,
|
|
3208 |
N,
|
|
3209 |
iBound,
|
|
3210 |
tBound)
|
|
3211 |
matrixFile = file('/tmp/matrixToReduce.txt', 'w')
|
|
3212 |
for row in matrixToReduce.rows():
|
|
3213 |
matrixFile.write(str(row) + "\n")
|
|
3214 |
matrixFile.close()
|
|
3215 |
raise Exception("Deliberate stop here.")
|
|
3216 |
"""
|
|
3217 |
reductionTime = cputime()
|
|
3218 |
#### Compute the reduced polynomials.
|
|
3219 |
ccReducedPolynomialsList = \
|
|
3220 |
slz_compute_coppersmith_reduced_polynomials(intIntP,
|
|
3221 |
alpha,
|
|
3222 |
N,
|
|
3223 |
iBound,
|
|
3224 |
tBound)
|
|
3225 |
if ccReducedPolynomialsList is None:
|
|
3226 |
raise Exception("Reduction failed.")
|
|
3227 |
reductionsFullTime += cputime(reductionTime)
|
|
3228 |
reductionsCount += 1
|
|
3229 |
if len(ccReducedPolynomialsList) < 2:
|
|
3230 |
print "Nothing to form resultants with."
|
|
3231 |
print
|
|
3232 |
coppCondFailedCount += 1
|
|
3233 |
coppCondFailed = True
|
|
3234 |
##### Apply a different shrink factor according to
|
|
3235 |
# the number of compliant polynomials.
|
|
3236 |
if len(ccReducedPolynomialsList) == 0:
|
|
3237 |
ub = lb + bw * noCoppersmithIntervalShrink
|
|
3238 |
else: # At least one compliant polynomial.
|
|
3239 |
ub = lb + bw * oneCoppersmithIntervalShrink
|
|
3240 |
if ub > sdub:
|
|
3241 |
ub = sdub
|
|
3242 |
if lb == ub:
|
|
3243 |
raise Exception("Cant shrink interval \
|
|
3244 |
anymore to get Coppersmith condition.")
|
|
3245 |
nbw = 0
|
|
3246 |
continue
|
|
3247 |
#### We have at least two polynomials.
|
|
3248 |
# Let us try to compute resultants.
|
|
3249 |
# For each resultant computed, go for the solutions.
|
|
3250 |
##### Build the pairs list.
|
|
3251 |
polyPairsList = []
|
|
3252 |
for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList) - 1):
|
|
3253 |
for polyInnerIndex in xrange(polyOuterIndex+1,
|
|
3254 |
len(ccReducedPolynomialsList)):
|
|
3255 |
polyPairsList.append((ccReducedPolynomialsList[polyOuterIndex],
|
|
3256 |
ccReducedPolynomialsList[polyInnerIndex]))
|
|
3257 |
#### Actual root search.
|
|
3258 |
iRootsSet = set()
|
|
3259 |
hasNonNullResultant = False
|
|
3260 |
for polyPair in polyPairsList:
|
|
3261 |
resultantsComputationTime = cputime()
|
|
3262 |
currentResultantI = \
|
|
3263 |
slz_resultant(polyPair[0],
|
|
3264 |
polyPair[1],
|
|
3265 |
t)
|
|
3266 |
resultantsComputationsCount += 1
|
|
3267 |
resultantsComputationsFullTime += \
|
|
3268 |
cputime(resultantsComputationTime)
|
|
3269 |
#### Function slz_resultant returns None both for None and O
|
|
3270 |
# resultants.
|
|
3271 |
if currentResultantI is None:
|
|
3272 |
print "Nul resultant"
|
|
3273 |
continue # Next polyPair.
|
|
3274 |
## We deleted the currentResultantI computation.
|
|
3275 |
#### We have a non null resultant. From now on, whatever this
|
|
3276 |
# root search yields, no extra root search is necessary.
|
|
3277 |
hasNonNullResultant = True
|
|
3278 |
#### A constant resultant leads to no root. Root search is done.
|
|
3279 |
if currentResultantI.degree() < 1:
|
|
3280 |
print "Resultant is constant:", currentResultantI
|
|
3281 |
break # There is no root.
|
|
3282 |
#### Actual iroots computation.
|
|
3283 |
rootsComputationTime = cputime()
|
|
3284 |
iRootsList = Zi(currentResultantI).roots()
|
|
3285 |
rootsComputationsCount += 1
|
|
3286 |
rootsComputationsFullTime = cputime(rootsComputationTime)
|
|
3287 |
if len(iRootsList) == 0:
|
|
3288 |
print "No roots in \"i\"."
|
|
3289 |
break # No roots in i.
|
|
3290 |
else:
|
|
3291 |
for iRoot in iRootsList:
|
|
3292 |
# A root is given as a (value, multiplicity) tuple.
|
|
3293 |
iRootsSet.add(iRoot[0])
|
|
3294 |
# End loop for polyPair in polyParsList. We only loop again if a
|
|
3295 |
# None or zero resultant is found.
|
|
3296 |
#### Prepare for results for the current interval..
|
|
3297 |
intervalResultsList = []
|
|
3298 |
intervalResultsList.append((lb, ub))
|
|
3299 |
#### Check roots.
|
|
3300 |
rootsResultsList = []
|
|
3301 |
for iRoot in iRootsSet:
|
|
3302 |
specificRootResultsList = []
|
|
3303 |
failingBounds = []
|
|
3304 |
# Root qualifies for modular equation, test it for hardness to round.
|
|
3305 |
hardToRoundCaseAsFloat = RRR((icAsInt + iRoot) / toIntegerFactor)
|
|
3306 |
#print "Before unscaling:", hardToRoundCaseAsFloat.n(prec=precision)
|
|
3307 |
#print scalingFunction
|
|
3308 |
scaledHardToRoundCaseAsFloat = \
|
|
3309 |
scalingFunction(hardToRoundCaseAsFloat)
|
|
3310 |
print "Candidate HTRNc at x =", \
|
|
3311 |
scaledHardToRoundCaseAsFloat.n().str(base=2),
|
|
3312 |
if slz_is_htrn(scaledHardToRoundCaseAsFloat,
|
|
3313 |
function,
|
|
3314 |
2^-(targetHardnessToRound),
|
|
3315 |
RRR,
|
|
3316 |
targetPlusOnePrecRF,
|
|
3317 |
quasiExactRF):
|
|
3318 |
print hardToRoundCaseAsFloat, "is HTRN case."
|
|
3319 |
specificRootResultsList.append(hardToRoundCaseAsFloat.n().str(base=2))
|
|
3320 |
if lb <= hardToRoundCaseAsFloat and hardToRoundCaseAsFloat <= ub:
|
|
3321 |
print "Found in interval."
|
|
3322 |
else:
|
|
3323 |
print "Found out of interval."
|
|
3324 |
# Check the i root is within the i bound.
|
|
3325 |
if abs(iRoot) > iBound:
|
|
3326 |
print "IRoot", iRoot, "is out of bounds for modular equation."
|
|
3327 |
print "i bound:", iBound
|
|
3328 |
failingBounds.append('i')
|
|
3329 |
failingBounds.append(iRoot)
|
|
3330 |
failingBounds.append(iBound)
|
|
3331 |
if len(failingBounds) > 0:
|
|
3332 |
specificRootResultsList.append(failingBounds)
|
|
3333 |
else: # From slz_is_htrn...
|
|
3334 |
print "is not an HTRN case."
|
|
3335 |
if len(specificRootResultsList) > 0:
|
|
3336 |
rootsResultsList.append(specificRootResultsList)
|
|
3337 |
if len(rootsResultsList) > 0:
|
|
3338 |
intervalResultsList.append(rootsResultsList)
|
|
3339 |
### Check if a non null resultant was found. If not shrink the interval.
|
|
3340 |
if not hasNonNullResultant:
|
|
3341 |
print "Only null resultants for this reduction, shrinking interval."
|
|
3342 |
resultCondFailed = True
|
|
3343 |
resultCondFailedCount += 1
|
|
3344 |
### Shrink interval for next iteration.
|
|
3345 |
ub = lb + bw * onlyNullResultantsShrink
|
|
3346 |
if ub > sdub:
|
|
3347 |
ub = sdub
|
|
3348 |
nbw = 0
|
|
3349 |
continue
|
|
3350 |
#### An intervalResultsList has at least the bounds.
|
|
3351 |
globalResultsList.append(intervalResultsList)
|
|
3352 |
#### Compute an incremented width for next upper bound, only
|
|
3353 |
# if not Coppersmith condition nor resultant condition
|
|
3354 |
# failed at the previous run.
|
|
3355 |
if not coppCondFailed and not resultCondFailed:
|
|
3356 |
nbw = noErrorIntervalStretch * bw
|
|
3357 |
else:
|
|
3358 |
nbw = bw
|
|
3359 |
##### Reset the failure flags. They will be raised
|
|
3360 |
# again if needed.
|
|
3361 |
coppCondFailed = False
|
|
3362 |
resultCondFailed = False
|
|
3363 |
#### For next iteration (at end of loop)
|
|
3364 |
#print "nbw:", nbw
|
|
3365 |
lb = ub
|
|
3366 |
ub += nbw
|
|
3367 |
if ub > sdub:
|
|
3368 |
ub = sdub
|
|
3369 |
print
|
|
3370 |
# End while True
|
|
3371 |
## Main loop just ended.
|
|
3372 |
globalWallTime = walltime(wallTimeStart)
|
|
3373 |
globalCpuTime = cputime(cpuTimeStart)
|
|
3374 |
## Output results
|
|
3375 |
print ; print "Intervals and HTRNs" ; print
|
|
3376 |
for intervalResultsList in globalResultsList:
|
|
3377 |
intervalResultString = "[" + str(intervalResultsList[0][0]) +\
|
|
3378 |
"," + str(intervalResultsList[0][1]) + "]"
|
|
3379 |
print intervalResultString,
|
|
3380 |
if len(intervalResultsList) > 1:
|
|
3381 |
rootsResultsList = intervalResultsList[1]
|
|
3382 |
specificRootResultIndex = 0
|
|
3383 |
for specificRootResultsList in rootsResultsList:
|
|
3384 |
if specificRootResultIndex == 0:
|
|
3385 |
print "\t", specificRootResultsList[0],
|
|
3386 |
else:
|
|
3387 |
print " " * len(intervalResultString), "\t", \
|
|
3388 |
specificRootResultsList[0],
|
|
3389 |
if len(specificRootResultsList) > 1:
|
|
3390 |
print specificRootResultsList[1]
|
|
3391 |
specificRootResultIndex += 1
|
|
3392 |
print ; print
|
|
3393 |
#print globalResultsList
|
|
3394 |
#
|
|
3395 |
print "Timers and counters"
|
|
3396 |
print
|
|
3397 |
print "Number of iterations:", iterCount
|
|
3398 |
print "Taylor condition failures:", taylCondFailedCount
|
|
3399 |
print "Coppersmith condition failures:", coppCondFailedCount
|
|
3400 |
print "Resultant condition failures:", resultCondFailedCount
|
|
3401 |
print "Iterations count: ", iterCount
|
|
3402 |
print "Number of intervals:", len(globalResultsList)
|
|
3403 |
print "Number of basis constructions:", basisConstructionsCount
|
|
3404 |
print "Total CPU time spent in basis constructions:", \
|
|
3405 |
basisConstructionsFullTime
|
|
3406 |
if basisConstructionsCount != 0:
|
|
3407 |
print "Average basis construction CPU time:", \
|
|
3408 |
basisConstructionsFullTime/basisConstructionsCount
|
|
3409 |
print "Number of reductions:", reductionsCount
|
|
3410 |
print "Total CPU time spent in reductions:", reductionsFullTime
|
|
3411 |
if reductionsCount != 0:
|
|
3412 |
print "Average reduction CPU time:", \
|
|
3413 |
reductionsFullTime/reductionsCount
|
|
3414 |
print "Number of resultants computation rounds:", \
|
|
3415 |
resultantsComputationsCount
|
|
3416 |
print "Total CPU time spent in resultants computation rounds:", \
|
|
3417 |
resultantsComputationsFullTime
|
|
3418 |
if resultantsComputationsCount != 0:
|
|
3419 |
print "Average resultants computation round CPU time:", \
|
|
3420 |
resultantsComputationsFullTime/resultantsComputationsCount
|
|
3421 |
print "Number of root finding rounds:", rootsComputationsCount
|
|
3422 |
print "Total CPU time spent in roots finding rounds:", \
|
|
3423 |
rootsComputationsFullTime
|
|
3424 |
if rootsComputationsCount != 0:
|
|
3425 |
print "Average roots finding round CPU time:", \
|
|
3426 |
rootsComputationsFullTime/rootsComputationsCount
|
|
3427 |
print "Global Wall time:", globalWallTime
|
|
3428 |
print "Global CPU time:", globalCpuTime
|
|
3429 |
## Output counters
|
|
3430 |
# End srs_runSLZ-v06
|