Skip to content

Commit

Permalink
xmr: step 03 review
Browse files Browse the repository at this point in the history
  • Loading branch information
tsusanka authored and ph4r05 committed Oct 3, 2018
1 parent daf7b7d commit 0266440
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 39 deletions.
33 changes: 20 additions & 13 deletions src/apps/monero/protocol/signing/step_03_inputs_permutation.py
Original file line number Diff line number Diff line change
@@ -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")
26 changes: 0 additions & 26 deletions src/apps/monero/xmr/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0266440

Please sign in to comment.