1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 01:18:28 +00:00

embed: cleanup Keccak API (drop keccak arg from digest() func)

This commit is contained in:
Pavol Rusnak 2018-07-23 15:19:38 +02:00
parent 8cf0b93df7
commit cb9e7b5885
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
15 changed files with 89 additions and 61 deletions

View File

@ -29,7 +29,7 @@
typedef struct _mp_obj_Sha3_256_t {
mp_obj_base_t base;
SHA3_CTX ctx;
int keccak;
bool keccak;
} mp_obj_Sha3_256_t;
STATIC mp_obj_t mod_trezorcrypto_Sha3_256_update(mp_obj_t self, mp_obj_t data);
@ -52,7 +52,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_256_make_new(const mp_obj_type_t *type, si
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
if (vals[1].u_obj != MP_OBJ_NULL){
o->keccak = mp_obj_is_true(vals[1].u_obj) ? 1 : -1;
o->keccak = mp_obj_is_true(vals[1].u_obj);
}
if (vals[0].u_obj != mp_const_none){
@ -76,17 +76,16 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_256_update(mp_obj_t self, mp_obj_t data) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Sha3_256_update_obj, mod_trezorcrypto_Sha3_256_update);
/// def digest(self, keccak: bool = False) -> bytes:
/// def digest(self) -> bytes:
/// '''
/// Returns the digest of hashed data.
/// '''
STATIC mp_obj_t mod_trezorcrypto_Sha3_256_digest(size_t n_args, const mp_obj_t *args) {
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(args[0]);
STATIC mp_obj_t mod_trezorcrypto_Sha3_256_digest(mp_obj_t self) {
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(self);
uint8_t out[SHA3_256_DIGEST_LENGTH];
SHA3_CTX ctx;
bool use_keccak = n_args > 1 ? args[1] == mp_const_true : o->keccak == 1;
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
if (use_keccak) {
if (o->keccak) {
keccak_Final(&ctx, out);
} else {
sha3_Final(&ctx, out);
@ -94,7 +93,8 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_256_digest(size_t n_args, const mp_obj_t *
memset(&ctx, 0, sizeof(SHA3_CTX));
return mp_obj_new_bytes(out, sizeof(out));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Sha3_256_digest_obj, 1, 2, mod_trezorcrypto_Sha3_256_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_256_digest_obj, mod_trezorcrypto_Sha3_256_digest);
/// def copy(self) -> sha3:
/// '''

View File

@ -29,22 +29,34 @@
typedef struct _mp_obj_Sha3_512_t {
mp_obj_base_t base;
SHA3_CTX ctx;
bool keccak;
} mp_obj_Sha3_512_t;
STATIC mp_obj_t mod_trezorcrypto_Sha3_512_update(mp_obj_t self, mp_obj_t data);
/// def __init__(self, data: bytes = None) -> None:
/// def __init__(self, data: bytes = None, keccak = False) -> None:
/// '''
/// Creates a hash context object.
/// '''
STATIC mp_obj_t mod_trezorcrypto_Sha3_512_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
mp_arg_check_num(n_args, n_kw, 0, 1, true);
mp_obj_Sha3_512_t *o = m_new_obj(mp_obj_Sha3_512_t);
o->base.type = type;
o->keccak = 0;
sha3_512_Init(&(o->ctx));
// constructor called with bytes/str as first parameter
if (n_args == 1) {
mod_trezorcrypto_Sha3_512_update(MP_OBJ_FROM_PTR(o), args[0]);
STATIC const mp_arg_t allowed_args[] = {
{ MP_QSTR_data, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_keccak, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
};
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
if (vals[1].u_obj != MP_OBJ_NULL){
o->keccak = mp_obj_is_true(vals[1].u_obj);
}
if (vals[0].u_obj != mp_const_none){
mod_trezorcrypto_Sha3_512_update(MP_OBJ_FROM_PTR(o), vals[0].u_obj);
}
return MP_OBJ_FROM_PTR(o);
}
@ -64,16 +76,16 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_512_update(mp_obj_t self, mp_obj_t data) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Sha3_512_update_obj, mod_trezorcrypto_Sha3_512_update);
/// def digest(self, keccak: bool = False) -> bytes:
/// def digest(self) -> bytes:
/// '''
/// Returns the digest of hashed data.
/// '''
STATIC mp_obj_t mod_trezorcrypto_Sha3_512_digest(size_t n_args, const mp_obj_t *args) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(args[0]);
STATIC mp_obj_t mod_trezorcrypto_Sha3_512_digest(mp_obj_t self) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self);
uint8_t out[SHA3_512_DIGEST_LENGTH];
SHA3_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
if (n_args > 1 && args[1] == mp_const_true) {
if (o->keccak) {
keccak_Final(&ctx, out);
} else {
sha3_Final(&ctx, out);
@ -81,7 +93,22 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_512_digest(size_t n_args, const mp_obj_t *
memset(&ctx, 0, sizeof(SHA3_CTX));
return mp_obj_new_bytes(out, sizeof(out));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Sha3_512_digest_obj, 1, 2, mod_trezorcrypto_Sha3_512_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_512_digest_obj, mod_trezorcrypto_Sha3_512_digest);
/// def copy(self) -> sha3:
/// '''
/// Returns the copy of the digest object with the current state
/// '''
STATIC mp_obj_t mod_trezorcrypto_Sha3_512_copy(size_t n_args, const mp_obj_t *args) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(args[0]);
mp_obj_Sha3_512_t *out = m_new_obj(mp_obj_Sha3_512_t);
out->base.type = o->base.type;
out->keccak = o->keccak;
memcpy(&(out->ctx), &(o->ctx), sizeof(SHA3_CTX));
return MP_OBJ_FROM_PTR(out);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Sha3_512_copy_obj, 1, 1, mod_trezorcrypto_Sha3_512_copy);
STATIC mp_obj_t mod_trezorcrypto_Sha3_512___del__(mp_obj_t self) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self);
@ -93,6 +120,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_512___del___obj, mod_trez
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Sha3_512_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_trezorcrypto_Sha3_512_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_trezorcrypto_Sha3_512_digest_obj) },
{ MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&mod_trezorcrypto_Sha3_512_copy_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_trezorcrypto_Sha3_512___del___obj) },
{ MP_ROM_QSTR(MP_QSTR_block_size), MP_OBJ_NEW_SMALL_INT(SHA3_512_BLOCK_LENGTH) },
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_OBJ_NEW_SMALL_INT(SHA3_512_DIGEST_LENGTH) },

View File

@ -14,7 +14,7 @@ async def ethereum_get_address(ctx, msg):
seckey = node.private_key()
public_key = secp256k1.publickey(seckey, False) # uncompressed
address = sha3_256(public_key[1:]).digest(True)[12:] # Keccak
address = sha3_256(public_key[1:], keccak=True).digest()[12:]
if msg.show_display:
if len(address_n) > 1: # path has slip44 network identifier
@ -41,7 +41,7 @@ def _ethereum_address_hex(address, network=None):
hx = hexlify(address).decode()
prefix = str(network.chain_id) + "0x" if rskip60 else ""
hs = sha3_256(prefix + hx).digest(True)
hs = sha3_256(prefix + hx, keccak=True).digest()
h = ""
for i in range(20):

View File

@ -11,13 +11,13 @@ from apps.common.signverify import split_message
def message_digest(message):
h = HashWriter(sha3_256)
h = HashWriter(sha3_256, keccak=True)
signed_message_header = "\x19Ethereum Signed Message:\n"
h.extend(signed_message_header)
h.extend(str(len(message)))
h.extend(message)
return h.get_digest(True)
return h.get_digest()
async def ethereum_sign_message(ctx, msg):

View File

@ -59,7 +59,7 @@ async def ethereum_sign_tx(ctx, msg):
total_length = get_total_length(msg, data_total)
sha = HashWriter(sha3_256)
sha = HashWriter(sha3_256, keccak=True)
sha.extend(rlp.encode_length(total_length, True)) # total length
if msg.tx_type is not None:
@ -85,7 +85,7 @@ async def ethereum_sign_tx(ctx, msg):
sha.extend(rlp.encode(0))
sha.extend(rlp.encode(0))
digest = sha.get_digest(True) # True -> use keccak mode
digest = sha.get_digest()
return await send_signature(ctx, msg, digest)

View File

@ -20,7 +20,7 @@ async def ethereum_verify_message(ctx, msg):
if not pubkey:
raise ValueError("Invalid signature")
pkh = sha3_256(pubkey[1:]).digest(True)[-20:]
pkh = sha3_256(pubkey[1:], keccak=True).digest()[-20:]
if msg.address != pkh:
raise ValueError("Invalid signature")

View File

@ -26,7 +26,7 @@ def serialize_multisig_signature(
w = write_common(
common, bytearray(public_key), NEM_TRANSACTION_TYPE_MULTISIG_SIGNATURE
)
digest = hashlib.sha3_256(inner).digest(True)
digest = hashlib.sha3_256(inner, keccak=True).digest()
write_uint32(w, 4 + len(digest))
write_bytes_with_length(w, digest)

View File

@ -84,5 +84,5 @@ class HashWriter:
self.buf[0] = b
self.ctx.update(self.buf)
def get_digest(self, *args) -> bytes:
return self.ctx.digest(*args)
def get_digest(self) -> bytes:
return self.ctx.digest()

View File

@ -37,7 +37,7 @@ class TestNemMosaicCreation(unittest.TestCase):
t = serialize_mosaic_creation(m.transaction, m.mosaic_creation, unhexlify('994793ba1c789fa9bdea918afc9b06e2d0309beb1081ac5b6952991e4defd324'))
self.assertEqual(t, unhexlify('014000000100009870b4d60020000000994793ba1c789fa9bdea918afc9b06e2d0309beb1081ac5b6952991e4defd32400f36f060000000080c2d600de00000020000000994793ba1c789fa9bdea918afc9b06e2d0309beb1081ac5b6952991e4defd3241f0000001000000067696d72652e67616d65732e706f6e6707000000706164646c65731b000000506164646c657320666f722074686520626f6e672067616d652e0a04000000150000000c00000064697669736962696c69747901000000301a0000000d000000696e697469616c537570706c79050000003130303030190000000d000000737570706c794d757461626c650400000074727565180000000c0000007472616e7366657261626c650400000074727565000000002800000054424d4f534149434f443446353445453543444d523233434342474f414d3258534a4252354f4c4300743ba40b000000'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('68364353c29105e6d361ad1a42abbccbf419cfc7adb8b74c8f35d8f8bdaca3fa'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('68364353c29105e6d361ad1a42abbccbf419cfc7adb8b74c8f35d8f8bdaca3fa'))
def test_nem_transaction_mosaic_creation_with_levy(self):
# http://bob.nem.ninja:8765/#/mosaic/b2f4a98113ff1f3a8f1e9d7197aa982545297fe0aa3fa6094af8031569953a55/0
@ -62,7 +62,7 @@ class TestNemMosaicCreation(unittest.TestCase):
t = serialize_mosaic_creation(m.transaction, m.mosaic_creation, unhexlify("244fa194e2509ac0d2fbc18779c2618d8c2ebb61c16a3bcbebcf448c661ba8dc"),)
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('b2f4a98113ff1f3a8f1e9d7197aa982545297fe0aa3fa6094af8031569953a55'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('b2f4a98113ff1f3a8f1e9d7197aa982545297fe0aa3fa6094af8031569953a55'))
# http://chain.nem.ninja/#/mosaic/e8dc14821dbea4831d9051f86158ef348001447968fc22c01644fdaf2bda75c6/0
m = _create_msg(NEM_NETWORK_MAINNET,
@ -86,7 +86,7 @@ class TestNemMosaicCreation(unittest.TestCase):
t = serialize_mosaic_creation(m.transaction, m.mosaic_creation, unhexlify("a1df5306355766bd2f9a64efdc089eb294be265987b3359093ae474c051d7d5a"))
self.assertEqual(t, unhexlify('0140000001000068ccaf200420000000a1df5306355766bd2f9a64efdc089eb294be265987b3359093ae474c051d7d5a002d3101000000004c0122040c01000020000000a1df5306355766bd2f9a64efdc089eb294be265987b3359093ae474c051d7d5a0f0000000300000064696d04000000636f696e0800000044494d20434f494e04000000150000000c00000064697669736962696c69747901000000361f0000000d000000696e697469616c537570706c790a000000393030303030303030301a0000000d000000737570706c794d757461626c650500000066616c7365180000000c0000007472616e7366657261626c6504000000747275654b00000002000000280000004e4347474c564f32473343554143564935474e58324b52424a5351434e3452444c325a574a3444500f0000000300000064696d04000000636f696e0a00000000000000280000004e424d4f534149434f443446353445453543444d523233434342474f414d325853495558365452530065cd1d00000000'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('e8dc14821dbea4831d9051f86158ef348001447968fc22c01644fdaf2bda75c6'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('e8dc14821dbea4831d9051f86158ef348001447968fc22c01644fdaf2bda75c6'))
def test_nem_transaction_mosaic_creation_with_description(self):
# http://chain.nem.ninja/#/mosaic/269c6fda657aba3053a0e5b138c075808cc20e244e1182d9b730798b60a1f77b/0
@ -115,7 +115,7 @@ class TestNemMosaicCreation(unittest.TestCase):
t = serialize_mosaic_creation(m.transaction, m.mosaic_creation, unhexlify("58956ac77951622dc5f1c938affbf017c458e30e6b21ddb5783d38b302531f23"))
self.assertEqual(t, unhexlify('0140000001000068d2dd97012000000058956ac77951622dc5f1c938affbf017c458e30e6b21ddb5783d38b302531f2300f36f0600000000e2eb9701c80100002000000058956ac77951622dc5f1c938affbf017c458e30e6b21ddb5783d38b302531f2317000000060000006a61626f3338090000007265645f746f6b656e0c0100005468697320746f6b656e20697320746f2063656c656272617465207468652072656c65617365206f66204e616d6573706163657320616e64204d6f7361696373206f6e20746865204e454d2073797374656d2e205468697320746f6b656e207761732074686520666973742065766572206d6f736169632063726561746564206f74686572207468616e206e656d2e78656d2e20546865726520617265206f6e6c792031302c3030302052656420546f6b656e7320746861742077696c6c206576657220626520637265617465642e20497420686173206e6f206c65767920616e642063616e2062652074726164656420667265656c7920616d6f6e6720746869726420706172746965732e04000000150000000c00000064697669736962696c69747901000000321a0000000d000000696e697469616c537570706c790500000031303030301a0000000d000000737570706c794d757461626c650500000066616c7365180000000c0000007472616e7366657261626c65040000007472756500000000280000004e424d4f534149434f443446353445453543444d523233434342474f414d3258534955583654525300743ba40b000000'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('269c6fda657aba3053a0e5b138c075808cc20e244e1182d9b730798b60a1f77b'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('269c6fda657aba3053a0e5b138c075808cc20e244e1182d9b730798b60a1f77b'))
def _create_msg(network: int, timestamp: int, fee: int, deadline: int,

View File

@ -24,7 +24,7 @@ class TestNemMosaicSupplyChange(unittest.TestCase):
1234)
t = serialize_mosaic_supply_change(m.transaction, m.supply_change, unhexlify("994793ba1c789fa9bdea918afc9b06e2d0309beb1081ac5b6952991e4defd324"))
self.assertEqual(hashlib.sha3_256(t).digest(True),
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(),
unhexlify('33a50fdd4a54913643a580b2af08b9a5b51b7cee922bde380e84c573a7969c50'))
# http://bigalice2.nem.ninja:7890/transaction/get?hash=1ce8e8894d077a66ff22294b000825d090a60742ec407efd80eb8b19657704f2
@ -38,7 +38,7 @@ class TestNemMosaicSupplyChange(unittest.TestCase):
1)
t = serialize_mosaic_supply_change(m.transaction, m.supply_change, unhexlify("84afa1bbc993b7f5536344914dde86141e61f8cbecaf8c9cefc07391f3287cf5"))
self.assertEqual(hashlib.sha3_256(t).digest(True),
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(),
unhexlify('1ce8e8894d077a66ff22294b000825d090a60742ec407efd80eb8b19657704f2'))
# http://bigalice3.nem.ninja:7890/transaction/get?hash=694e493e9576d2bcf60d85747e302ac2e1cc27783187947180d4275a713ff1ff
@ -52,7 +52,7 @@ class TestNemMosaicSupplyChange(unittest.TestCase):
9000000)
t = serialize_mosaic_supply_change(m.transaction, m.supply_change, unhexlify("b7ccc27b21ba6cf5c699a8dc86ba6ba98950442597ff9fa30e0abe0f5f4dd05d"))
self.assertEqual(hashlib.sha3_256(t).digest(True),
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(),
unhexlify('694e493e9576d2bcf60d85747e302ac2e1cc27783187947180d4275a713ff1ff'))
# http://bigalice3.nem.ninja:7890/transaction/get?hash=09836334e123970e068d5b411e4d1df54a3ead10acf1ad5935a2cdd9f9680185
@ -66,7 +66,7 @@ class TestNemMosaicSupplyChange(unittest.TestCase):
20)
t = serialize_mosaic_supply_change(m.transaction, m.supply_change, unhexlify("75f001a8641e2ce5c4386883dda561399ed346177411b492a677b73899502f13"))
self.assertEqual(hashlib.sha3_256(t).digest(True),
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(),
unhexlify('09836334e123970e068d5b411e4d1df54a3ead10acf1ad5935a2cdd9f9680185'))

View File

@ -28,7 +28,7 @@ class TestNemMultisigAggregateModification(unittest.TestCase):
serialize_cosignatory_modification(t, 1, unhexlify(
"c54d6e33ed1446eedd7f7a80a588dd01857f723687a09200c1917d5524752f8b"))
self.assertEqual(hashlib.sha3_256(t).digest(True),
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(),
unhexlify("6a55471b17159e5b6cd579c421e95a4e39d92e3f78b0a55ee337e785a601d3a2"))
# http://chain.nem.ninja/#/aggregate/cc64ca69bfa95db2ff7ac1e21fe6d27ece189c603200ebc9778d8bb80ca25c3c
@ -51,7 +51,7 @@ class TestNemMultisigAggregateModification(unittest.TestCase):
serialize_cosignatory_modification(t, 1, unhexlify(
"43aa69177018fc3e2bdbeb259c81cddf24be50eef9c5386db51d82386c41475a"))
self.assertEqual(hashlib.sha3_256(t).digest(True),
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(),
unhexlify("cc64ca69bfa95db2ff7ac1e21fe6d27ece189c603200ebc9778d8bb80ca25c3c"))
def test_nem_transaction_aggregate_modification_relative_change(self):
@ -74,7 +74,7 @@ class TestNemMultisigAggregateModification(unittest.TestCase):
"d88c6ee2a2cd3929d0d76b6b14ecb549d21296ab196a2b3a4cb2536bcce32e87"))
serialize_minimum_cosignatories(t, 2)
self.assertEqual(hashlib.sha3_256(t).digest(True),
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(),
unhexlify("1fbdae5ba753e68af270930413ae90f671eb8ab58988116684bac0abd5726584"))

View File

@ -23,7 +23,7 @@ class TestNemNamespace(unittest.TestCase):
'TAMESPACEWH4MKFMBCVFERDPOOP4FK7MTDJEYP35',
5000000000)
t = serialize_provision_namespace(m.transaction, m.provision_namespace, unhexlify('84afa1bbc993b7f5536344914dde86141e61f8cbecaf8c9cefc07391f3287cf5'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('f7cab28da57204d01a907c697836577a4ae755e6c9bac60dcc318494a22debb3'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('f7cab28da57204d01a907c697836577a4ae755e6c9bac60dcc318494a22debb3'))
# http://bob.nem.ninja:8765/#/namespace/7ddd5fe607e1bfb5606e0ac576024c318c8300d237273117d4db32a60c49524d
m = _create_msg(NEM_NETWORK_TESTNET,
@ -36,7 +36,7 @@ class TestNemNamespace(unittest.TestCase):
5000000000)
t = serialize_provision_namespace(m.transaction, m.provision_namespace, unhexlify('244fa194e2509ac0d2fbc18779c2618d8c2ebb61c16a3bcbebcf448c661ba8dc'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('7ddd5fe607e1bfb5606e0ac576024c318c8300d237273117d4db32a60c49524d'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('7ddd5fe607e1bfb5606e0ac576024c318c8300d237273117d4db32a60c49524d'))
# http://chain.nem.ninja/#/namespace/57071aad93ca125dc231dc02c07ad8610cd243d35068f9b36a7d231383907569
m = _create_msg(NEM_NETWORK_MAINNET,
@ -49,7 +49,7 @@ class TestNemNamespace(unittest.TestCase):
50000000000)
t = serialize_provision_namespace(m.transaction, m.provision_namespace, unhexlify('9f3c14f304309c8b72b2821339c4428793b1518bea72d58dd01f19d523518614'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('57071aad93ca125dc231dc02c07ad8610cd243d35068f9b36a7d231383907569'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('57071aad93ca125dc231dc02c07ad8610cd243d35068f9b36a7d231383907569'))
def _create_msg(network: int, timestamp: int, fee: int, deadline: int,

View File

@ -24,7 +24,7 @@ class TestNemTransfer(unittest.TestCase):
t = serialize_transfer(m.transaction, m.transfer, unhexlify('e59ef184a612d4c3c4d89b5950eb57262c69862b2f96e59c5043bf41765c482f'))
self.assertEqual(t, unhexlify('01010000010000980000000020000000e59ef184a612d4c3c4d89b5950eb57262c69862b2f96e59c5043bf41765c482f00000000000000000000000028000000544247494d52453453424652554a584d48374456463249425933364c324544575a3337475653433400203d88792d000000000000'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('0acbf8df91e6a65dc56c56c43d65f31ff2a6a48d06fc66e78c7f3436faf3e74f'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('0acbf8df91e6a65dc56c56c43d65f31ff2a6a48d06fc66e78c7f3436faf3e74f'))
def test_create_transfer_with_payload(self):
@ -39,7 +39,7 @@ class TestNemTransfer(unittest.TestCase):
t = serialize_transfer(m.transaction, m.transfer,
unhexlify('8d07f90fb4bbe7715fa327c926770166a11be2e494a970605f2e12557f66c9b9'),
bytearray('Good luck!'))
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('e90e98614c7598fbfa4db5411db1b331d157c2f86b558fb7c943d013ed9f71cb'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('e90e98614c7598fbfa4db5411db1b331d157c2f86b558fb7c943d013ed9f71cb'))
def test_create_transfer_with_encrypted_payload(self):
@ -55,7 +55,7 @@ class TestNemTransfer(unittest.TestCase):
unhexlify('f85ab43dad059b9d2331ddacc384ad925d3467f03207182e01296bacfb242d01'),
unhexlify('4d9dcf9186967d30be93d6d5404ded22812dbbae7c3f0de501bcd7228cba45bded13000eec7b4c6215fc4d3588168c9218167cec98e6977359153a4132e050f594548e61e0dc61c153f0f53c5e65c595239c9eb7c4e7d48e0f4bb8b1dd2f5ddc'),
True)
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('40e89160e6f83d37f7c82defc0afe2c1605ae8c919134570a51dd27ea1bb516c'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('40e89160e6f83d37f7c82defc0afe2c1605ae8c919134570a51dd27ea1bb516c'))
def test_create_transfer_with_mosaic(self):
@ -78,7 +78,7 @@ class TestNemTransfer(unittest.TestCase):
serialize_mosaic(t, 'gimre.games.pong', 'paddles', 2)
serialize_mosaic(t, 'nem', 'xem', 44000000)
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('3409d9ece28d6296d6d5e220a7e3cb8641a3fb235ffcbd20c95da64f003ace6c'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('3409d9ece28d6296d6d5e220a7e3cb8641a3fb235ffcbd20c95da64f003ace6c'))
# http://chain.nem.ninja/#/transfer/882dca18dcbe075e15e0ec5a1d7e6ccd69cc0f1309ffd3fde227bfbc107b3f6e
m = _create_msg(NEM_NETWORK_MAINNET,
@ -95,7 +95,7 @@ class TestNemTransfer(unittest.TestCase):
False)
serialize_mosaic(t, 'imre.g', 'tokens', 1)
self.assertEqual(hashlib.sha3_256(t).digest(True), unhexlify('882dca18dcbe075e15e0ec5a1d7e6ccd69cc0f1309ffd3fde227bfbc107b3f6e'))
self.assertEqual(hashlib.sha3_256(t, keccak=True).digest(), unhexlify('882dca18dcbe075e15e0ec5a1d7e6ccd69cc0f1309ffd3fde227bfbc107b3f6e'))
def _create_msg(network: int, timestamp: int, fee: int, deadline: int,

View File

@ -26,7 +26,7 @@ class TestCryptoSha3_256(unittest.TestCase):
def test_digest_keccak(self):
for b, d in self.vectors_keccak:
self.assertEqual(hashlib.sha3_256(b).digest(True), unhexlify(d))
self.assertEqual(hashlib.sha3_256(b, keccak=True).digest(), unhexlify(d))
def test_update(self):
for b, d in self.vectors:
@ -48,9 +48,9 @@ class TestCryptoSha3_256(unittest.TestCase):
def test_update_keccak(self):
for b, d in self.vectors_keccak:
x = hashlib.sha3_256()
x = hashlib.sha3_256(keccak=True)
x.update(b)
self.assertEqual(x.digest(True), unhexlify(d))
self.assertEqual(x.digest(), unhexlify(d))
def test_digest_multi(self):
x = hashlib.sha3_256()
@ -61,10 +61,10 @@ class TestCryptoSha3_256(unittest.TestCase):
self.assertEqual(d0, d2)
def test_digest_multi_keccak(self):
x = hashlib.sha3_256()
d0 = x.digest(True)
d1 = x.digest(True)
d2 = x.digest(True)
x = hashlib.sha3_256(keccak=True)
d0 = x.digest()
d1 = x.digest()
d2 = x.digest()
self.assertEqual(d0, d1)
self.assertEqual(d0, d2)

View File

@ -26,7 +26,7 @@ class TestCryptoSha3_512(unittest.TestCase):
def test_digest_keccak(self):
for b, d in self.vectors_keccak:
self.assertEqual(hashlib.sha3_512(b).digest(True), unhexlify(d))
self.assertEqual(hashlib.sha3_512(b, keccak=True).digest(), unhexlify(d))
def test_update(self):
for b, d in self.vectors:
@ -48,9 +48,9 @@ class TestCryptoSha3_512(unittest.TestCase):
def test_update_keccak(self):
for b, d in self.vectors_keccak:
x = hashlib.sha3_512()
x = hashlib.sha3_512(keccak=True)
x.update(b)
self.assertEqual(x.digest(True), unhexlify(d))
self.assertEqual(x.digest(), unhexlify(d))
def test_digest_multi(self):
x = hashlib.sha3_512()
@ -61,10 +61,10 @@ class TestCryptoSha3_512(unittest.TestCase):
self.assertEqual(d0, d2)
def test_digest_multi_keccak(self):
x = hashlib.sha3_512()
d0 = x.digest(True)
d1 = x.digest(True)
d2 = x.digest(True)
x = hashlib.sha3_512(keccak=True)
d0 = x.digest()
d1 = x.digest()
d2 = x.digest()
self.assertEqual(d0, d1)
self.assertEqual(d0, d2)