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

Commit

Permalink
xmr: typos and renames
Browse files Browse the repository at this point in the history
  • Loading branch information
tsusanka authored and ph4r05 committed Sep 27, 2018
1 parent 87f718b commit 9be1e0a
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 79 deletions.
11 changes: 1 addition & 10 deletions src/apps/monero/protocol/signing/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _init(self):
self.tx_prefix_hasher = KeccakXmrArchive()
self.full_message_hasher = PreMlsagHasher()

def _mem_trace(self, x=None, collect=False):
def mem_trace(self, x=None, collect=False):
if __debug__:
log.debug(
__name__,
Expand All @@ -100,15 +100,6 @@ def assrt(self, condition, msg=None):
return
raise ValueError("Assertion error%s" % (" : %s" % msg if msg else ""))

def num_inputs(self):
return self.input_count

def num_dests(self):
return self.output_count

def get_fee(self):
return self.fee if self.fee > 0 else 0

def change_address(self):
return self.output_change.addr if self.output_change else None

Expand Down
22 changes: 12 additions & 10 deletions src/apps/monero/protocol/signing/step_01_init_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ async def init_transaction(state: State, address_n, network_type, tsx_data):

state.creds = await misc.monero_get_creds(state.ctx, address_n, network_type)

state.fee = state.fee if state.fee > 0 else 0

state.tx_priv = crypto.random_scalar()
state.tx_pub = crypto.scalarmult_base(state.tx_priv)

state._mem_trace(1)
state.mem_trace(1)

# Ask for confirmation
await confirms.confirm_transaction(state.ctx, tsx_data, state.creds)
gc.collect()
state._mem_trace(3)
state.mem_trace(3)

# Basic transaction parameters
state.input_count = tsx_data.num_inputs
Expand Down Expand Up @@ -68,7 +70,7 @@ async def init_transaction(state: State, address_n, network_type, tsx_data):
state.need_additional_txkeys = num_subaddresses > 0 and (
num_stdaddresses > 0 or num_subaddresses > 1
)
state._mem_trace(4, True)
state.mem_trace(4, True)

# Extra processing, payment id
state.tx.version = 2
Expand All @@ -83,27 +85,27 @@ async def init_transaction(state: State, address_n, network_type, tsx_data):
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._mem_trace(10, True)
state.mem_trace(10, True)

# Final message hasher
state.full_message_hasher.init(state.use_simple_rct)
state.full_message_hasher.set_type_fee(state.get_rct_type(), state.get_fee())
state.full_message_hasher.set_type_fee(state.get_rct_type(), state.fee)

# Sub address precomputation
if tsx_data.account is not None and tsx_data.minor_indices:
precompute_subaddr(state, tsx_data.account, tsx_data.minor_indices)
state._mem_trace(5, True)
state.mem_trace(5, True)

# HMAC outputs - pinning
hmacs = []
for idx in range(state.num_dests()):
for idx in range(state.output_count):
c_hmac = await hmac_encryption_keys.gen_hmac_tsxdest(
state.key_hmac, tsx_data.outputs[idx], idx
)
hmacs.append(c_hmac)
gc.collect()

state._mem_trace(6)
state.mem_trace(6)

from trezor.messages.MoneroTransactionInitAck import MoneroTransactionInitAck
from trezor.messages.MoneroTransactionRsigData import MoneroTransactionRsigData
Expand Down Expand Up @@ -141,11 +143,11 @@ def check_change(state: State, outputs):

change_addr = state.change_address()
if change_addr is None:
state._mem_trace("No change" if __debug__ else None)
state.mem_trace("No change" if __debug__ else None)
return

if change_idx is None and state.output_change.amount == 0 and len(outputs) == 2:
state._mem_trace("Sweep tsx" if __debug__ else None)
state.mem_trace("Sweep tsx" if __debug__ else None)
return # sweep dummy tsx

found = False
Expand Down
14 changes: 7 additions & 7 deletions src/apps/monero/protocol/signing/step_02_set_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ async def set_input(state: State, src_entr):

state.inp_idx += 1

await transaction_step(state.STEP_INP, state.inp_idx, state.num_inputs())
await transaction_step(state.STEP_INP, state.inp_idx, state.input_count)

if state.inp_idx >= state.num_inputs():
if state.inp_idx >= state.input_count:
raise ValueError("Too many inputs")
if src_entr.real_output >= len(src_entr.outputs):
raise ValueError(
Expand All @@ -60,7 +60,7 @@ async def set_input(state: State, src_entr):
src_entr.real_output_in_tx_index,
)
xi, ki, di = secs
state._mem_trace(1, True)
state.mem_trace(1, True)

# Construct tx.vin
ki_real = src_entr.multisig_kLRki.ki if state.multi_sig else ki
Expand All @@ -74,13 +74,13 @@ async def set_input(state: State, src_entr):

# Serialize with variant code for TxinToKey
vini_bin = misc.dump_msg(vini, preallocate=64, prefix=b"\x02")
state._mem_trace(2, True)
state.mem_trace(2, True)

# HMAC(T_in,i || vin_i)
hmac_vini = await hmac_encryption_keys.gen_hmac_vini(
state.key_hmac, src_entr, vini_bin, state.inp_idx
)
state._mem_trace(3, True)
state.mem_trace(3, True)

# PseudoOuts commitment, alphas stored to state
pseudo_out = None
Expand All @@ -107,7 +107,7 @@ async def set_input(state: State, src_entr):
)

# All inputs done?
if state.inp_idx + 1 == state.num_inputs():
if state.inp_idx + 1 == state.input_count:
tsx_inputs_done(state)

return MoneroTransactionSetInputAck(
Expand All @@ -127,7 +127,7 @@ def tsx_inputs_done(state: State):
# self.state.input_done()
state.subaddresses = None # TODO why? remove this?

if state.inp_idx + 1 != state.num_inputs():
if state.inp_idx + 1 != state.input_count:
raise ValueError("Input count mismatch")


Expand Down
4 changes: 2 additions & 2 deletions src/apps/monero/protocol/signing/step_04_input_vini.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ async def input_vini(
)

await confirms.transaction_step(
state.ctx, state.STEP_VINI, state.inp_idx + 1, state.num_inputs()
state.ctx, state.STEP_VINI, state.inp_idx + 1, state.input_count
)

if state.inp_idx >= state.num_inputs():
if state.inp_idx >= state.input_count:
raise ValueError("Too many inputs")

state.inp_idx += 1
Expand Down
8 changes: 4 additions & 4 deletions src/apps/monero/protocol/signing/step_05_all_in_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def all_in_set(state: State, rsig_data): # todo: rsig_data not used?
"""
If in the applicable offloading mode, generate commitment masks.
"""
state._mem_trace(0)
state.mem_trace(0)
# state.state.input_all_done() todo check if needed?
await confirms.transaction_step(state.ctx, state.STEP_ALL_IN)

Expand All @@ -31,11 +31,11 @@ async def all_in_set(state: State, rsig_data): # todo: rsig_data not used?

# Simple offloading - generate random masks that sum to the input mask sum.
tmp_buff = bytearray(32)
rsig_data.mask = bytearray(32 * state.num_dests())
rsig_data.mask = bytearray(32 * state.output_count)
state.sumout = crypto.sc_init(0)
for i in range(state.num_dests()):
for i in range(state.output_count):
cur_mask = crypto.new_scalar()
is_last = i + 1 == state.num_dests()
is_last = i + 1 == state.output_count
if is_last and state.use_simple_rct:
crypto.sc_sub_into(cur_mask, state.sumpouts_alphas, state.sumout)
else:
Expand Down
52 changes: 26 additions & 26 deletions src/apps/monero/protocol/signing/step_06_set_out1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@


async def set_out1(state: State, dst_entr, dst_entr_hmac, rsig_data=None):
state._mem_trace(0, True)
state.mem_trace(0, True)
mods = utils.unimport_begin()

await confirms.transaction_step(
state.ctx, state.STEP_OUT, state.out_idx + 1, state.num_dests()
state.ctx, state.STEP_OUT, state.out_idx + 1, state.output_count
)
state._mem_trace(1)
state.mem_trace(1)

if (
state.inp_idx + 1 != state.num_inputs()
state.inp_idx + 1 != state.input_count
): # todo check state.state.is_input_vins() - needed?
raise ValueError("Invalid number of inputs")

state.out_idx += 1
state._mem_trace(2, True)
state.mem_trace(2, True)

if dst_entr.amount <= 0 and state.tx.version <= 1:
raise ValueError("Destination with wrong amount: %s" % dst_entr.amount)
Expand All @@ -41,21 +41,21 @@ async def set_out1(state: State, dst_entr, dst_entr_hmac, rsig_data=None):
if not common.ct_equal(dst_entr_hmac, dst_entr_hmac_computed):
raise ValueError("HMAC invalid")
del (dst_entr_hmac, dst_entr_hmac_computed)
state._mem_trace(3, True)
state.mem_trace(3, True)

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

state.summary_outs_money += dst_entr.amount
utils.unimport_end(mods)
state._mem_trace(5, True)
state.mem_trace(5, True)

# Range proof first, memory intensive
rsig, mask = _range_proof(state, state.out_idx, dst_entr.amount, rsig_data)
utils.unimport_end(mods)
state._mem_trace(6, True)
state.mem_trace(6, True)

# Amount key, tx out key
additional_txkey_priv = _set_out1_additional_keys(state, dst_entr)
Expand All @@ -65,11 +65,11 @@ async def set_out1(state: State, dst_entr, dst_entr_hmac, rsig_data=None):
derivation, state.out_idx, crypto.decodepoint(dst_entr.addr.spend_public_key)
)
del (derivation, additional_txkey_priv)
state._mem_trace(7, True)
state.mem_trace(7, True)

# Tx header prefix hashing, hmac dst_entr
tx_out_bin, hmac_vouti = await _set_out1_tx_out(state, dst_entr, tx_out_key)
state._mem_trace(11, True)
state.mem_trace(11, True)

# Out_pk, ecdh_info
out_pk, ecdh_info_bin = _set_out1_ecdh(
Expand All @@ -80,18 +80,18 @@ async def set_out1(state: State, dst_entr, dst_entr_hmac, rsig_data=None):
amount_key=amount_key,
)
del (dst_entr, mask, amount_key, tx_out_key)
state._mem_trace(12, True)
state.mem_trace(12, True)

# 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.
state.full_message_hasher.set_ecdh(ecdh_info_bin)
state._mem_trace(13, True)
state.mem_trace(13, True)

# Output_pk is stored to the state as it is used during the signature and hashed to the
# RctSigBase later.
state.output_pk.append(out_pk)
state._mem_trace(14, True)
state.mem_trace(14, True)

from trezor.messages.MoneroTransactionSetOutputAck import (
MoneroTransactionSetOutputAck
Expand All @@ -116,17 +116,17 @@ async def _set_out1_tx_out(state: State, dst_entr, tx_out_key):
tx_out_bin[0] = 0 # amount varint
tx_out_bin[1] = 2 # variant code TxoutToKey
crypto.encodepoint_into(tx_out_bin, tx_out_key, 2)
state._mem_trace(8)
state.mem_trace(8)

# Tx header prefix hashing
state.tx_prefix_hasher.buffer(tx_out_bin)
state._mem_trace(9, True)
state.mem_trace(9, True)

# Hmac dest_entr.
hmac_vouti = await hmac_encryption_keys.gen_hmac_vouti(
state.key_hmac, dst_entr, tx_out_bin, state.out_idx
)
state._mem_trace(10, True)
state.mem_trace(10, True)
return tx_out_bin, hmac_vouti


Expand Down Expand Up @@ -173,17 +173,17 @@ def _range_proof(state, idx, amount, rsig_data=None):
# Pedersen commitment on the value, mask from the commitment, range signature.
C, rsig = None, None

state._mem_trace("pre-rproof" if __debug__ else None, collect=True)
state.mem_trace("pre-rproof" if __debug__ else None, collect=True)
if not state.rsig_offload and state.use_bulletproof:
rsig = ring_ct.prove_range_bp_batch(state.output_amounts, state.output_masks)
state._mem_trace("post-bp" if __debug__ else None, collect=True)
state.mem_trace("post-bp" if __debug__ else None, collect=True)

# Incremental hashing
state.full_message_hasher.rsig_val(rsig, True, raw=False)
state._mem_trace("post-bp-hash" if __debug__ else None, collect=True)
state.mem_trace("post-bp-hash" if __debug__ else None, collect=True)

rsig = misc.dump_rsig_bp(rsig)
state._mem_trace(
state.mem_trace(
"post-bp-ser, size: %s" % len(rsig) if __debug__ else None, collect=True
)

Expand All @@ -208,7 +208,7 @@ def _range_proof(state, idx, amount, rsig_data=None):
state.full_message_hasher.rsig_val(bp_obj, True, raw=False)
res = ring_ct.verify_bp(bp_obj, state.output_amounts, masks)
state.assrt(res, "BP verification fail")
state._mem_trace("BP verified" if __debug__ else None, collect=True)
state.mem_trace("BP verified" if __debug__ else None, collect=True)
del (bp_obj, ring_ct)

elif state.rsig_offload and not state.use_bulletproof:
Expand All @@ -218,7 +218,7 @@ def _range_proof(state, idx, amount, rsig_data=None):
else:
raise misc.TrezorError("Unexpected rsig state")

state._mem_trace("rproof" if __debug__ else None, collect=True)
state.mem_trace("rproof" if __debug__ else None, collect=True)
state.output_amounts = []
if not state.rsig_offload:
state.output_masks = []
Expand Down Expand Up @@ -268,7 +268,7 @@ def _set_out1_additional_keys(state: State, dst_entr):
additional_txkey = None
additional_txkey_priv = None
if state.need_additional_txkeys:
use_provided = state.num_dests() == len(state.additional_tx_private_keys)
use_provided = state.output_count == len(state.additional_tx_private_keys)
additional_txkey_priv = (
state.additional_tx_private_keys[state.out_idx]
if use_provided
Expand Down Expand Up @@ -347,7 +347,7 @@ def _get_out_mask(state: State, idx):
if state.rsig_offload:
return state.output_masks[idx]
else:
is_last = idx + 1 == state.num_dests()
is_last = idx + 1 == state.output_count
if is_last:
return crypto.sc_sub(state.sumpouts_alphas, state.sumout)
else:
Expand Down
Loading

0 comments on commit 9be1e0a

Please sign in to comment.