diff --git a/src/apps/monero/controller/misc.py b/src/apps/monero/controller/misc.py index 83781ecaf..a49733a1f 100644 --- a/src/apps/monero/controller/misc.py +++ b/src/apps/monero/controller/misc.py @@ -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 diff --git a/src/apps/monero/protocol/tsx_sign_builder.py b/src/apps/monero/protocol/tsx_sign_builder.py index bdff6a7a5..fe30eba97 100644 --- a/src/apps/monero/protocol/tsx_sign_builder.py +++ b/src/apps/monero/protocol/tsx_sign_builder.py @@ -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 )