Skip to content

Commit

Permalink
Drop python3 (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Sep 13, 2023
1 parent 77abf98 commit 11b3d81
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 40 deletions.
40 changes: 6 additions & 34 deletions ed25519.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,11 @@
"""

import hashlib
import operator
import sys


__version__ = "1.0.dev0"


# Useful for very coarse version differentiation.
PY3 = sys.version_info[0] == 3

if PY3:
indexbytes = operator.getitem
intlist2bytes = bytes
int2byte = operator.methodcaller("to_bytes", 1, "big")
else:
int2byte = chr
range = xrange # noqa: F821

def indexbytes(buf, i):
return ord(buf[i])

def intlist2bytes(l):
return b"".join(chr(c) for c in l)


b = 256
q = 2**255 - 19
l = 2**252 + 27742317777372353535851937790883648493
Expand Down Expand Up @@ -197,11 +177,8 @@ def scalarmult_B(e):

def encodeint(y):
bits = [(y >> i) & 1 for i in range(b)]
return b"".join(
[
int2byte(sum([bits[i * 8 + j] << j for j in range(8)]))
for i in range(b // 8)
]
return bytes(
[sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b // 8)]
)


Expand All @@ -211,16 +188,13 @@ def encodepoint(P):
x = (x * zi) % q
y = (y * zi) % q
bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
return b"".join(
[
int2byte(sum([bits[i * 8 + j] << j for j in range(8)]))
for i in range(b // 8)
]
return bytes(
[sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b // 8)]
)


def bit(h, i):
return (indexbytes(h, i // 8) >> (i % 8)) & 1
return (h[i // 8] >> (i % 8)) & 1


def publickey_unsafe(sk):
Expand Down Expand Up @@ -248,9 +222,7 @@ def signature_unsafe(m, sk, pk):
"""
h = H(sk)
a = 2 ** (b - 2) + sum(2**i * bit(h, i) for i in range(3, b - 2))
r = Hint(
intlist2bytes([indexbytes(h, j) for j in range(b // 8, b // 4)]) + m
)
r = Hint(bytes([h[j] for j in range(b // 8, b // 4)]) + m)
R = scalarmult_B(r)
S = (r + Hint(encodepoint(R) + pk + m) * a) % l
return encodepoint(R) + encodeint(S)
Expand Down
7 changes: 1 addition & 6 deletions test_ed25519.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ def test_ed25519_kat(secret_key, public_key, message, signed, signature):
if len(m) == 0:
forgedm = b"x"
else:
forgedm = ed25519.intlist2bytes(
[
ed25519.indexbytes(m, i) + (i == len(m) - 1)
for i in range(len(m))
]
)
forgedm = bytes([m[i] + (i == len(m) - 1) for i in range(len(m))])
except ValueError:
# TODO: Yes this means that we "pass" a test if we can't generate a
# forged message. This matches the original test suite, it's
Expand Down

0 comments on commit 11b3d81

Please sign in to comment.