1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-03 12:00:59 +00:00

tools: update ed25519{cosi,raw}.py to work in both py2 and py3

This commit is contained in:
Pavol Rusnak 2017-10-03 23:48:12 +02:00
parent 3db571f63a
commit 5884d1c03f
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 28 additions and 14 deletions

View File

@ -1,5 +1,7 @@
import sys
from functools import reduce
import binascii
import ed25519raw
@ -19,6 +21,9 @@ def combine_sig(R, sigs):
def get_nonce(sk, data, ctr):
h = ed25519raw.H(sk)
b = ed25519raw.b
if sys.version_info.major < 3:
r = ed25519raw.Hint(''.join([h[i] for i in range(b >> 3, b >> 2)]) + data + binascii.unhexlify('%08x' % ctr))
else:
r = ed25519raw.Hint(bytes([h[i] for i in range(b >> 3, b >> 2)]) + data + binascii.unhexlify('%08x' % ctr))
R = ed25519raw.scalarmult(ed25519raw.B, r)
return r, ed25519raw.encodepoint(R)
@ -41,7 +46,7 @@ def self_test(digest):
sigs = []
for i in range(0, N):
print('----- Key %d ------' % (i + 1))
seckey = bytes([0x41 + i]) * 32
seckey = (chr(0x41 + i) * 32).encode()
pubkey = ed25519raw.publickey(seckey)
print('Secret Key: %s' % to_hex(seckey))
print('Public Key: %s' % to_hex(pubkey))
@ -81,7 +86,6 @@ def self_test(digest):
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
self_test(digest=sys.argv[1])
else:

View File

@ -1,6 +1,7 @@
# orignal version downloaded from https://ed25519.cr.yp.to/python/ed25519.py
# modified for Python 3 by Jochen Hoenicke <hoenicke@gmail.com>
import sys
import hashlib
b = 256
@ -68,6 +69,9 @@ def scalarmult(P, e):
def encodeint(y):
bits = [(y >> i) & 1 for i in range(b)]
if sys.version_info.major < 3:
return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b >> 3)])
else:
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b >> 3)])
@ -75,10 +79,16 @@ def encodepoint(P):
x = P[0]
y = P[1]
bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
if sys.version_info.major < 3:
return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b >> 3)])
else:
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b >> 3)])
def bit(h, i):
if sys.version_info.major < 3:
return (ord(h[i >> 3]) >> (i & 7)) & 1
else:
return (h[i >> 3] >> (i & 7)) & 1