From 9e5e047a1fd3ee3768121438cd8bb58440729e59 Mon Sep 17 00:00:00 2001 From: Dusan Klinec Date: Wed, 12 Sep 2018 21:51:14 +0200 Subject: [PATCH] xmr: manual serialization improvements --- src/apps/monero/protocol/tsx_sign_builder.py | 2 +- src/apps/monero/xmr/serialize/xmrserialize.py | 6 ++--- src/apps/monero/xmr/sub/keccak_hasher.py | 8 ++++++ src/apps/monero/xmr/sub/mlsag_hasher.py | 25 +++++-------------- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/apps/monero/protocol/tsx_sign_builder.py b/src/apps/monero/protocol/tsx_sign_builder.py index a116a1d68..9628081d1 100644 --- a/src/apps/monero/protocol/tsx_sign_builder.py +++ b/src/apps/monero/protocol/tsx_sign_builder.py @@ -1306,7 +1306,7 @@ async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None): # Incremental hashing of the ECDH info. # RctSigBase allows to hash only one of the (ecdh, out_pk) as they are serialized # as whole vectors. Hashing ECDH info saves state space. - self.full_message_hasher.set_ecdh(ecdh_info_bin, True) + self.full_message_hasher.set_ecdh(ecdh_info_bin) self._mem_trace(13, True) # Output_pk is stored to the state as it is used during the signature and hashed to the diff --git a/src/apps/monero/xmr/serialize/xmrserialize.py b/src/apps/monero/xmr/serialize/xmrserialize.py index c565ccfbc..83fd4aa88 100644 --- a/src/apps/monero/xmr/serialize/xmrserialize.py +++ b/src/apps/monero/xmr/serialize/xmrserialize.py @@ -117,14 +117,14 @@ def uvarint(self, elem): else: return load_uvarint(self.iobj) - def uint(self, elem, elem_type, params=None): + def uint(self, elem, elem_type=None, width=None): """ Fixed size int """ if self.writing: - return dump_uint(self.iobj, elem, elem_type.WIDTH) + return dump_uint(self.iobj, elem, width if width else elem_type.WIDTH) else: - return load_uint(self.iobj, elem_type.WIDTH) + return load_uint(self.iobj, width if width else elem_type.WIDTH) def unicode_type(self, elem): """ diff --git a/src/apps/monero/xmr/sub/keccak_hasher.py b/src/apps/monero/xmr/sub/keccak_hasher.py index f135743b5..d28ef947a 100644 --- a/src/apps/monero/xmr/sub/keccak_hasher.py +++ b/src/apps/monero/xmr/sub/keccak_hasher.py @@ -39,6 +39,14 @@ def release(self): def buffer(self, buf): return self.kwriter.write(buf) + def uvarint(self, i): + ar = self._ar(None) + ar.uvarint(i) + + def uint(self, i, width): + ar = self._ar(None) + ar.uint(i, width=width) + def field(self, elem=None, elem_type=None, params=None, xser=None): ar = self._ar(xser) return ar.field(elem, elem_type, params) diff --git a/src/apps/monero/xmr/sub/mlsag_hasher.py b/src/apps/monero/xmr/sub/mlsag_hasher.py index 723895f0f..71c145734 100644 --- a/src/apps/monero/xmr/sub/mlsag_hasher.py +++ b/src/apps/monero/xmr/sub/mlsag_hasher.py @@ -54,12 +54,8 @@ def set_type_fee(self, rv_type, fee): if self.state != 1: raise ValueError("State error") self.state = 2 - - from apps.monero.xmr.serialize_messages.tx_full import RctSigBase - - rfields = RctSigBase.f_specs() - self.rtcsig_hasher.message_field(None, field=rfields[0], fvalue=rv_type) - self.rtcsig_hasher.message_field(None, field=rfields[1], fvalue=fee) + self.rtcsig_hasher.uint(rv_type, 1) # UInt8 + self.rtcsig_hasher.uvarint(fee) # UVarintType def set_pseudo_out(self, out): if self.state != 2 and self.state != 3: @@ -70,26 +66,17 @@ def set_pseudo_out(self, out): self.rtcsig_hasher.field(out, KeyV.ELEM_TYPE) - def set_ecdh(self, ecdh, raw=False): + def set_ecdh(self, ecdh): if self.state != 2 and self.state != 3 and self.state != 4: raise ValueError("State error") self.state = 4 + self.rtcsig_hasher.buffer(ecdh) - if raw: - self.rtcsig_hasher.buffer(ecdh) - else: - from apps.monero.xmr.serialize_messages.tx_ecdh import EcdhInfo - - self.rtcsig_hasher.field(ecdh, EcdhInfo.ELEM_TYPE) - - def set_out_pk(self, out_pk, mask=None): + def set_out_pk(self, out_pk): if self.state != 4 and self.state != 5: raise ValueError("State error") self.state = 5 - - from apps.monero.xmr.serialize_messages.base import ECKey - - self.rtcsig_hasher.field(mask if mask else out_pk.mask, ECKey) + self.rtcsig_hasher.buffer(out_pk.mask) # ECKey def rctsig_base_done(self): if self.state != 5: