1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-30 01:01:00 +00:00
trezor-firmware/tools/ed25519cosi.py

89 lines
2.6 KiB
Python
Raw Normal View History

2017-10-01 17:42:29 +00:00
from functools import reduce
2017-04-02 15:27:36 +00:00
import binascii
import ed25519raw
2017-06-13 14:50:03 +00:00
2017-04-02 15:27:36 +00:00
def combine_keys(pks):
2017-10-01 17:42:29 +00:00
P = [ed25519raw.decodepoint(pk) for pk in pks]
combine = reduce(ed25519raw.edwards, P)
2017-04-02 15:27:36 +00:00
return ed25519raw.encodepoint(combine)
2017-06-13 14:50:03 +00:00
2017-04-02 15:27:36 +00:00
def combine_sig(R, sigs):
2017-10-01 17:42:29 +00:00
S = [ed25519raw.decodeint(si) for si in sigs]
s = sum(S) % ed25519raw.l
2017-04-02 15:27:36 +00:00
sig = R + ed25519raw.encodeint(s)
return sig
2017-06-13 14:50:03 +00:00
def get_nonce(sk, data, ctr):
2017-04-02 15:27:36 +00:00
h = ed25519raw.H(sk)
b = ed25519raw.b
2017-09-05 21:15:47 +00:00
r = ed25519raw.Hint(bytes([h[i] for i in range(b >> 3, b >> 2)]) + data + binascii.unhexlify('%08x' % ctr))
2017-06-13 14:50:03 +00:00
R = ed25519raw.scalarmult(ed25519raw.B, r)
2017-09-30 08:26:36 +00:00
return r, ed25519raw.encodepoint(R)
2017-04-02 15:27:36 +00:00
2017-06-13 14:50:03 +00:00
2017-10-01 17:42:29 +00:00
def self_test(digest):
2017-04-02 15:27:36 +00:00
2017-10-01 17:42:29 +00:00
def to_hex(by):
return binascii.hexlify(by).decode()
2017-06-13 14:50:03 +00:00
2017-09-30 08:26:36 +00:00
N = 3
keyset = [0, 2]
2017-10-01 17:42:29 +00:00
digest = binascii.unhexlify(digest)
print('Digest: %s' % to_hex(digest))
2017-04-02 15:27:36 +00:00
sks = []
pks = []
nonces = []
commits = []
sigs = []
for i in range(0, N):
2017-06-13 14:50:03 +00:00
print('----- Key %d ------' % (i + 1))
2017-09-30 08:26:36 +00:00
seckey = bytes([0x41 + i]) * 32
2017-04-02 15:27:36 +00:00
pubkey = ed25519raw.publickey(seckey)
2017-10-01 17:42:29 +00:00
print('Secret Key: %s' % to_hex(seckey))
print('Public Key: %s' % to_hex(pubkey))
2017-04-02 15:27:36 +00:00
sks.append(seckey)
pks.append(pubkey)
ctr = 0
r, R = get_nonce(seckey, digest, ctr)
2017-10-01 17:42:29 +00:00
print('Local nonce: %s' % to_hex(ed25519raw.encodeint(r)))
print('Local commit: %s' % to_hex(R))
2017-04-02 15:27:36 +00:00
nonces.append(r)
commits.append(R)
global_pk = combine_keys([pks[i] for i in keyset])
global_R = combine_keys([commits[i] for i in keyset])
2017-04-02 15:27:36 +00:00
print('-----------------')
2017-10-01 17:42:29 +00:00
print('Global pubkey: %s' % to_hex(global_pk))
print('Global commit: %s' % to_hex(global_R))
2017-04-02 15:27:36 +00:00
print('-----------------')
2017-09-30 08:26:36 +00:00
for i in range(0, N):
2017-04-02 15:27:36 +00:00
seckey = sks[i]
pubkey = pks[i]
r = nonces[i]
R = commits[i]
h = ed25519raw.H(seckey)
b = ed25519raw.b
a = 2**(b - 2) + sum(2**i * ed25519raw.bit(h, i) for i in range(3, b - 2))
S = (r + ed25519raw.Hint(global_R + global_pk + digest) * a) % ed25519raw.l
2017-10-01 17:42:29 +00:00
print('Local sig %d: %s' % (i + 1, to_hex(ed25519raw.encodeint(S))))
2017-04-02 15:27:36 +00:00
sigs.append(ed25519raw.encodeint(S))
print('-----------------')
sig = combine_sig(global_R, [sigs[i] for i in keyset])
2017-10-01 17:42:29 +00:00
print('Global sig: %s' % to_hex(sig))
ed25519raw.checkvalid(sig, digest, global_pk)
2017-04-02 15:27:36 +00:00
print('Valid Signature!')
2017-09-05 21:15:47 +00:00
2017-04-02 15:27:36 +00:00
if __name__ == '__main__':
2017-10-01 17:42:29 +00:00
import sys
if len(sys.argv) > 1:
2017-10-01 17:42:29 +00:00
self_test(digest=sys.argv[1])
else:
2017-10-01 17:42:29 +00:00
self_test(digest='4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b')