diff --git a/src/apps/monero/protocol/signing/step_03_inputs_permutation.py b/src/apps/monero/protocol/signing/step_03_inputs_permutation.py index 90a3bc0e3..d6c3c4d18 100644 --- a/src/apps/monero/protocol/signing/step_03_inputs_permutation.py +++ b/src/apps/monero/protocol/signing/step_03_inputs_permutation.py @@ -1,32 +1,39 @@ """ -Set permutation on the inputs - sorted by key image on host. +Inputs in transaction need to be sorted by their key image, otherwise the +transaction is rejected. The sorting is done on host and then sent here in +the MoneroTransactionInputsPermutationRequest message. + +The message contains just a simple array where each item stands for the +input's position in the transaction. + +We do not do the actual sorting here (we do not store the complete input +data anyway, so we can't) we just save the array to the state and use +it later when needed. """ from .state import State from apps.monero.layout.confirms import transaction_step -from apps.monero.xmr import common -async def tsx_inputs_permutation(state: State, permutation): - """ - Set permutation on the inputs - sorted by key image on host. - """ +async def tsx_inputs_permutation(state: State, permutation: list): from trezor.messages.MoneroTransactionInputsPermutationAck import ( MoneroTransactionInputsPermutationAck ) await transaction_step(state.ctx, state.STEP_PERM) - _tsx_inputs_permutation(state, permutation) - - return MoneroTransactionInputsPermutationAck() - - -def _tsx_inputs_permutation(state: State, permutation): """ Set permutation on the inputs - sorted by key image on host. """ state.source_permutation = permutation - common.check_permutation(permutation) + _check_permutation(permutation) state.current_input_index = -1 + + return MoneroTransactionInputsPermutationAck() + + +def _check_permutation(permutation): + for n in range(len(permutation)): + if n not in permutation: + raise ValueError("Invalid permutation") diff --git a/src/apps/monero/xmr/common.py b/src/apps/monero/xmr/common.py index eed46c8c3..154fdc801 100644 --- a/src/apps/monero/xmr/common.py +++ b/src/apps/monero/xmr/common.py @@ -9,31 +9,5 @@ def ct_equal(a, b): return monero.ct_equals(a, b) -def check_permutation(permutation): - for n in range(len(permutation)): - if n not in permutation: - raise ValueError("Invalid permutation") - - -def apply_permutation(permutation, swapper): - """ - Apply permutation from idx. Used for in-place permutation application with swapper. - Ported from Monero. - :param permutation: - :param swapper: function(x,y) - :return: - """ - check_permutation(permutation) - perm = list(permutation) - for i in range(len(perm)): - current = i - while i != perm[current]: - nxt = perm[current] - swapper(current, nxt) - perm[current] = current - current = nxt - perm[current] = current - - def is_empty(inp): return inp is None or len(inp) == 0