mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-25 01:02:24 +00:00
Make bignum.py self-contained, by importing versions of the two
functions I was depending on from my personal Python maths utility module. [originally from svn r9104]
This commit is contained in:
parent
77180221bd
commit
9d4005e5c1
44
testdata/bignum.py
vendored
44
testdata/bignum.py
vendored
@ -1,14 +1,40 @@
|
|||||||
# Generate test cases for a bignum implementation.
|
# Generate test cases for a bignum implementation.
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import mathlib
|
|
||||||
|
# integer square roots
|
||||||
|
def sqrt(n):
|
||||||
|
d = long(n)
|
||||||
|
a = 0L
|
||||||
|
# b must start off as a power of 4 at least as large as n
|
||||||
|
ndigits = len(hex(long(n)))
|
||||||
|
b = 1L << (ndigits*4)
|
||||||
|
while 1:
|
||||||
|
a = a >> 1
|
||||||
|
di = 2*a + b
|
||||||
|
if di <= d:
|
||||||
|
d = d - di
|
||||||
|
a = a + b
|
||||||
|
b = b >> 2
|
||||||
|
if b == 0: break
|
||||||
|
return a
|
||||||
|
|
||||||
|
# continued fraction convergents of a rational
|
||||||
|
def confrac(n, d):
|
||||||
|
coeffs = [(1,0),(0,1)]
|
||||||
|
while d != 0:
|
||||||
|
i = n / d
|
||||||
|
n, d = d, n % d
|
||||||
|
coeffs.append((coeffs[-2][0]-i*coeffs[-1][0],
|
||||||
|
coeffs[-2][1]-i*coeffs[-1][1]))
|
||||||
|
return coeffs
|
||||||
|
|
||||||
def findprod(target, dir = +1, ratio=(1,1)):
|
def findprod(target, dir = +1, ratio=(1,1)):
|
||||||
# Return two numbers whose product is as close as we can get to
|
# Return two numbers whose product is as close as we can get to
|
||||||
# 'target', with any deviation having the sign of 'dir', and in
|
# 'target', with any deviation having the sign of 'dir', and in
|
||||||
# the same approximate ratio as 'ratio'.
|
# the same approximate ratio as 'ratio'.
|
||||||
|
|
||||||
r = mathlib.sqrt(target * ratio[0] * ratio[1])
|
r = sqrt(target * ratio[0] * ratio[1])
|
||||||
a = r / ratio[1]
|
a = r / ratio[1]
|
||||||
b = r / ratio[0]
|
b = r / ratio[0]
|
||||||
if a*b * dir < target * dir:
|
if a*b * dir < target * dir:
|
||||||
@ -22,11 +48,7 @@ def findprod(target, dir = +1, ratio=(1,1)):
|
|||||||
improved = 0
|
improved = 0
|
||||||
a, b = best[:2]
|
a, b = best[:2]
|
||||||
|
|
||||||
terms = mathlib.confracr(a, b, output=None)
|
coeffs = confrac(a, b)
|
||||||
coeffs = [(1,0),(0,1)]
|
|
||||||
for t in terms:
|
|
||||||
coeffs.append((coeffs[-2][0]-t*coeffs[-1][0],
|
|
||||||
coeffs[-2][1]-t*coeffs[-1][1]))
|
|
||||||
for c in coeffs:
|
for c in coeffs:
|
||||||
# a*c[0]+b*c[1] is as close as we can get it to zero. So
|
# a*c[0]+b*c[1] is as close as we can get it to zero. So
|
||||||
# if we replace a and b with a+c[1] and b+c[0], then that
|
# if we replace a and b with a+c[1] and b+c[0], then that
|
||||||
@ -45,7 +67,7 @@ def findprod(target, dir = +1, ratio=(1,1)):
|
|||||||
A,B,C = da*db, b*da+a*db, a*b-target
|
A,B,C = da*db, b*da+a*db, a*b-target
|
||||||
discrim = B^2-4*A*C
|
discrim = B^2-4*A*C
|
||||||
if discrim > 0 and A != 0:
|
if discrim > 0 and A != 0:
|
||||||
root = mathlib.sqrt(discrim)
|
root = sqrt(discrim)
|
||||||
vals = []
|
vals = []
|
||||||
vals.append((-B + root) / (2*A))
|
vals.append((-B + root) / (2*A))
|
||||||
vals.append((-B - root) / (2*A))
|
vals.append((-B - root) / (2*A))
|
||||||
@ -83,9 +105,9 @@ for i in range(1,4200):
|
|||||||
|
|
||||||
# Simple tests of modpow.
|
# Simple tests of modpow.
|
||||||
for i in range(64, 4097, 63):
|
for i in range(64, 4097, 63):
|
||||||
modulus = mathlib.sqrt(1<<(2*i-1)) | 1
|
modulus = sqrt(1<<(2*i-1)) | 1
|
||||||
base = mathlib.sqrt(3*modulus*modulus) % modulus
|
base = sqrt(3*modulus*modulus) % modulus
|
||||||
expt = mathlib.sqrt(modulus*modulus*2/5)
|
expt = sqrt(modulus*modulus*2/5)
|
||||||
print "pow", hexstr(base), hexstr(expt), hexstr(modulus), hexstr(pow(base, expt, modulus))
|
print "pow", hexstr(base), hexstr(expt), hexstr(modulus), hexstr(pow(base, expt, modulus))
|
||||||
if i <= 1024:
|
if i <= 1024:
|
||||||
# Test even moduli, which can't be done by Montgomery.
|
# Test even moduli, which can't be done by Montgomery.
|
||||||
|
Loading…
Reference in New Issue
Block a user