Skip to content

Commit

Permalink
xmr: KeccakXmrArchive simplified, no archive used
Browse files Browse the repository at this point in the history
- getting rid of container_size. We dont use containers with fixed size so this special case can be abandoned.
- KeccakXmrArchive is lighweight without need to touch main serialization
  • Loading branch information
ph4r05 committed Oct 2, 2018
1 parent 90065bd commit ec4c4ad
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 36 deletions.
4 changes: 1 addition & 3 deletions src/apps/monero/protocol/signing/step_01_init_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ async def init_transaction(
gc.collect()

# Iterative tx_prefix_hash hash computation
state.tx_prefix_hasher.keep() # TODO investigate
state.tx_prefix_hasher.uvarint(state.tx.version)
state.tx_prefix_hasher.uvarint(state.tx.unlock_time)
state.tx_prefix_hasher.container_size(state.input_count) # ContainerType
state.tx_prefix_hasher.release()
state.tx_prefix_hasher.uvarint(state.input_count) # ContainerType, size
state.mem_trace(10, True)

# Final message hasher
Expand Down
2 changes: 1 addition & 1 deletion src/apps/monero/protocol/signing/step_06_set_out1.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def set_out1(state: State, dst_entr, dst_entr_hmac, rsig_data=None):

# First output - tx prefix hasher - size of the container
if state.out_idx == 0:
state.tx_prefix_hasher.container_size(state.output_count)
state.tx_prefix_hasher.uvarint(state.output_count)
state.mem_trace(4, True)

state.summary_outs_money += dst_entr.amount
Expand Down
2 changes: 0 additions & 2 deletions src/apps/monero/protocol/signing/step_07_all_out1_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ def _set_tx_extra(state: State):
def _set_tx_prefix(state: State):
# Serializing "extra" type as BlobType.
# uvarint(len(extra)) || extra
state.tx_prefix_hasher.keep()
state.tx_prefix_hasher.uvarint(len(state.tx.extra))
state.tx_prefix_hasher.buffer(state.tx.extra)
state.tx_prefix_hasher.release()

state.tx_prefix_hash = state.tx_prefix_hasher.get_digest()
state.tx_prefix_hasher = None
Expand Down
33 changes: 3 additions & 30 deletions src/apps/monero/xmr/sub/keccak_hasher.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
from apps.monero.xmr import crypto
from apps.monero.xmr.serialize import int_serialize


class KeccakXmrArchive:
def __init__(self, ctx=None):
self.kwriter = get_keccak_writer(ctx=ctx)
self.ar = None
self.keeping = False

def ctx(self):
return self.kwriter.ctx()

def get_digest(self):
return self.kwriter.get_digest()

def _ar(self, xser=None):
if self.keeping and self.ar:
return self.ar
if xser:
ar = xser.Archive(self.kwriter, True)
else:
from apps.monero.xmr.serialize import xmrserialize

ar = xmrserialize.Archive(self.kwriter, True)
self.ar = ar if self.keeping else None
return ar

def keep(self, keep=True):
self.keeping = keep

def release(self):
self.ar = None

def buffer(self, buf):
return self.kwriter.write(buf)

def uvarint(self, i):
ar = self._ar(None)
ar.uvarint(i)
int_serialize.dump_uvarint(self.kwriter, i)

def uint(self, i, width):
ar = self._ar(None)
ar.uint(i, width=width)

def container_size(
self, container_len=None, container_type=None, params=None, xser=None
):
ar = self._ar(xser)
return ar.container_size(container_len, container_type, params)
int_serialize.dump_uint(self.kwriter, i, width)


class AHashWriter:
Expand Down

0 comments on commit ec4c4ad

Please sign in to comment.