def test_ed25519_kat(secret_key, public_key, message, signed, signature): sk = binascii.unhexlify(secret_key) m = binascii.unhexlify(message) pk = ed25519.publickey_unsafe(sk) sig = ed25519.signature_unsafe(m, sk, pk) # Assert that the signature and public key are what we expected assert binascii.hexlify(pk) == public_key assert binascii.hexlify(sig) == signature # Validate the signature using the checkvalid routine ed25519.checkvalid(sig, m, pk) # Assert that we cannot forge a message try: 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)) ]) 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 # unclear if it was intentional there or not. pass else: with pytest.raises(ed25519.SignatureMismatch): ed25519.checkvalid(sig, forgedm, pk)
def derive_public_key(private_key): """ Calculate public key from private key :param private_key: hex encoded private key :return: hex encoded public key """ secret = binascii.unhexlify(private_key) public_key = ed25519.publickey_unsafe(secret) hex_public_key = binascii.hexlify(public_key) return hex_public_key
def genesis_block(admin, alice, test_permissions, multidomain=False): """ Compose a set of common for all tests' genesis block transactions :param admin: dict of id and private key of admin :param alice: dict of id and private key of alice :param test_permissions: permissions for users in test domain :param multidomain: admin and alice accounts will be created in different domains and the first domain users will have admin right by default if True :return: a list of irohalib.Iroha.command's """ peer = primitive_pb2.Peer() peer.address = '0.0.0.0:50541' # ed25519.publickey_unsafe takes and returns a bytes object, while we have hex strings peer.peer_key = binascii.hexlify( ed25519.publickey_unsafe(binascii.unhexlify(admin['key']))) commands = [ command('AddPeer', peer=peer), command('CreateRole', role_name='admin_role', permissions=all_permissions()), command('CreateRole', role_name='test_role', permissions=test_permissions) ] if multidomain: commands.append( command('CreateDomain', domain_id='first', default_role='admin_role')) commands.extend([ command('CreateDomain', domain_id='second' if multidomain else 'test', default_role='test_role'), command('CreateAccount', account_name='admin', domain_id='first' if multidomain else 'test', public_key=irohalib.IrohaCrypto.derive_public_key( admin['key'])), command('CreateAccount', account_name='alice', domain_id='second' if multidomain else 'test', public_key=irohalib.IrohaCrypto.derive_public_key( alice['key'])) ]) if not multidomain: commands.append( command('AppendRole', account_id=admin['id'], role_name='admin_role')) return commands
# 2013 by Donald Stufft <*****@*****.**> # 2013 by Alex Gaynor <*****@*****.**> # 2013 by Greg Price <*****@*****.**> # # To the extent possible under law, the author(s) have dedicated all copyright # and related and neighboring rights to this software to the public domain # worldwide. This software is distributed without any warranty. # # You should have received a copy of the CC0 Public Domain Dedication along # with this software. If not, see # <http://creativecommons.org/publicdomain/zero/1.0/>. import os import timeit import ed25519 seed = os.urandom(32) data = b"The quick brown fox jumps over the lazy dog" private_key = seed public_key = ed25519.publickey_unsafe(seed) signature = ed25519.signature_unsafe(data, private_key, public_key) print('\nTime verify signature') print( timeit.timeit( "ed25519.checkvalid(signature, data, public_key)", setup="from __main__ import ed25519, signature, data, public_key", number=100, ))
from binascii import hexlify, unhexlify from hashlib import sha512 import sys import ed25519 sys.path.insert(0, 'python-sha3') from python_sha3 import * # unhexlify and reverse sk = unhexlify( '11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff')[::-1] pk0 = ed25519.publickey_unsafe(sk) pk1 = ed25519.publickey_hash_unsafe(sk, sha512) pk2 = ed25519.publickey_hash_unsafe(sk, sha3_512) print ' sec key:', hexlify(sk) print 'VALID NEM pub key:', hexlify(pk2) print '-' * 80 print 'NOT valid NEM pub keys produced by original ed25519' print 'v1:', hexlify(pk0) print 'v2:', hexlify(pk1)
from binascii import hexlify, unhexlify from hashlib import sha512 import sys import ed25519 sys.path.insert(0, 'python-sha3') from python_sha3 import * # unhexlify and reverse sk = unhexlify('11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff')[::-1] pk0 = ed25519.publickey_unsafe(sk) pk1 = ed25519.publickey_hash_unsafe(sk, sha512) pk2 = ed25519.publickey_hash_unsafe(sk, sha3_512) print ' sec key:', hexlify(sk) print 'VALID NEM pub key:', hexlify(pk2) print '-'*80 print 'NOT valid NEM pub keys produced by original ed25519' print 'v1:', hexlify(pk0) print 'v2:', hexlify(pk1)
# 2013 by Greg Price <*****@*****.**> # # To the extent possible under law, the author(s) have dedicated all copyright # and related and neighboring rights to this software to the public domain # worldwide. This software is distributed without any warranty. # # You should have received a copy of the CC0 Public Domain Dedication along # with this software. If not, see # <http://creativecommons.org/publicdomain/zero/1.0/>. import os import timeit import ed25519 seed = os.urandom(32) data = b"The quick brown fox jumps over the lazy dog" private_key = seed public_key = ed25519.publickey_unsafe(seed) signature = ed25519.signature_unsafe(data, private_key, public_key) print("\nTime verify signature") print( timeit.timeit( "ed25519.checkvalid(signature, data, public_key)", setup="from __main__ import ed25519, signature, data, public_key", number=100, ) )