aboutsummaryrefslogtreecommitdiff
path: root/src/test/slownacl_curve25519.py
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-04-27 22:54:24 -0400
committerNick Mathewson <nickm@torproject.org>2014-04-27 22:54:24 -0400
commitf4be34f70d6f277a0f3f73e62ed358564588e92b (patch)
tree383603733ed9b7775ce60618b94dd271a3687bf9 /src/test/slownacl_curve25519.py
parent78b431d3e30def3641f25707197c55a1c7200269 (diff)
downloadtor-f4be34f70d6f277a0f3f73e62ed358564588e92b.tar.gz
tor-f4be34f70d6f277a0f3f73e62ed358564588e92b.zip
Make the python test scripts work on python3
The python scripts invoked by 'make check' didn't work on python3 before. That was a problem on systems where 'python' is python3. Fixes bug 11608; bugfix on 0.2.5.2-alpha.
Diffstat (limited to 'src/test/slownacl_curve25519.py')
-rw-r--r--src/test/slownacl_curve25519.py31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/test/slownacl_curve25519.py b/src/test/slownacl_curve25519.py
index 25244fb122..4dabab61b6 100644
--- a/src/test/slownacl_curve25519.py
+++ b/src/test/slownacl_curve25519.py
@@ -8,12 +8,14 @@
__all__ = ['smult_curve25519_base', 'smult_curve25519']
+import sys
+
P = 2 ** 255 - 19
A = 486662
def expmod(b, e, m):
if e == 0: return 1
- t = expmod(b, e / 2, m) ** 2 % m
+ t = expmod(b, e // 2, m) ** 2 % m
if e & 1: t = (t * b) % m
return t
@@ -23,12 +25,14 @@ def inv(x):
# Addition and doubling formulas taken from Appendix D of "Curve25519:
# new Diffie-Hellman speed records".
-def add((xn,zn), (xm,zm), (xd,zd)):
+def add(n,m,d):
+ (xn,zn), (xm,zm), (xd,zd) = n, m, d
x = 4 * (xm * xn - zm * zn) ** 2 * zd
z = 4 * (xm * zn - zm * xn) ** 2 * xd
return (x % P, z % P)
-def double((xn,zn)):
+def double(n):
+ (xn,zn) = n
x = (xn ** 2 - zn ** 2) ** 2
z = 4 * xn * zn * (xn ** 2 + A * xn * zn + zn ** 2)
return (x % P, z % P)
@@ -40,19 +44,34 @@ def curve25519(n, base):
# (m+1)th multiple of base.
def f(m):
if m == 1: return (one, two)
- (pm, pm1) = f(m / 2)
+ (pm, pm1) = f(m // 2)
if (m & 1):
return (add(pm, pm1, one), double(pm1))
return (double(pm), add(pm, pm1, one))
((x,z), _) = f(n)
return (x * inv(z)) % P
+if sys.version < '3':
+ def b2i(c):
+ return ord(c)
+ def i2b(i):
+ return chr(i)
+ def ba2bs(ba):
+ return "".join(ba)
+else:
+ def b2i(c):
+ return c
+ def i2b(i):
+ return i
+ def ba2bs(ba):
+ return bytes(ba)
+
def unpack(s):
if len(s) != 32: raise ValueError('Invalid Curve25519 argument')
- return sum(ord(s[i]) << (8 * i) for i in range(32))
+ return sum(b2i(s[i]) << (8 * i) for i in range(32))
def pack(n):
- return ''.join([chr((n >> (8 * i)) & 255) for i in range(32)])
+ return ba2bs([i2b((n >> (8 * i)) & 255) for i in range(32)])
def clamp(n):
n &= ~7