Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
xmr: manual BP serialization
Browse files Browse the repository at this point in the history
- more memory effective as the memory is critical in the range proof section
  • Loading branch information
ph4r05 committed Sep 17, 2018
1 parent d64bda7 commit 5d81c2a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/apps/monero/controller/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,50 @@ def dump_msg_gc(msg, preallocate=None, msg_type=None, del_msg=False):
return b


def dump_rsig_bp(rsig):
from trezor.utils import memcpy

if len(rsig.L) > 127:
raise ValueError("Too large")

# Manual serialization as the generic purpose misc.dump_msg_gc
# is more memory intensive which is not desired in the range proof section.

# BP: V, A, S, T1, T2, taux, mu, L, R, a, b, t
# Commitment vector V is not serialized
# Vector size under 127 thus varint occupies 1 B
buff_size = 32 * (9 + 2 * (len(rsig.L))) + 2
buff = bytearray(buff_size)

memcpy(buff, 0, rsig.A, 0, 32)
memcpy(buff, 32, rsig.S, 0, 32)
memcpy(buff, 32 * 2, rsig.T1, 0, 32)
memcpy(buff, 32 * 3, rsig.T2, 0, 32)
memcpy(buff, 32 * 4, rsig.taux, 0, 32)
memcpy(buff, 32 * 5, rsig.mu, 0, 32)

buff[32 * 6] = len(rsig.L)
offset = 32 * 6 + 1

for x in rsig.L:
memcpy(buff, offset, x, 0, 32)
offset += 32

buff[offset] = len(rsig.R)
offset += 1

for x in rsig.R:
memcpy(buff, offset, x, 0, 32)
offset += 32

memcpy(buff, offset, rsig.a, 0, 32)
offset += 32
memcpy(buff, offset, rsig.b, 0, 32)
offset += 32
memcpy(buff, offset, rsig.t, 0, 32)
return buff


def dst_entry_to_stdobj(dst):
if dst is None:
return None
Expand Down
4 changes: 1 addition & 3 deletions src/apps/monero/protocol/tsx_sign_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,7 @@ def _range_proof(self, idx, amount, rsig_data=None):
self.full_message_hasher.rsig_val(rsig, True, raw=False)
self._mem_trace("post-bp-hash" if __debug__ else None, collect=True)

rsig = misc.dump_msg_gc(
rsig, preallocate=ring_ct.bp_size(batch_size) + 8, del_msg=True
)
rsig = misc.dump_rsig_bp(rsig)
self._mem_trace(
"post-bp-ser, size: %s" % len(rsig) if __debug__ else None, collect=True
)
Expand Down

0 comments on commit 5d81c2a

Please sign in to comment.