Skip to content

Commit

Permalink
xmr: refactoring, typing, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Sep 14, 2018
1 parent 28df866 commit 93af8af
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/apps/monero/protocol/tsx_sign_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ async def set_input(self, src_entr):
crypto.decodepoint(x) for x in src_entr.real_out_additional_tx_keys
]

secs = monero.generate_key_image_helper(
secs = monero.generate_tx_spend_and_key_image_and_derivation(
self.creds,
self.subaddresses,
out_key,
Expand Down Expand Up @@ -1068,7 +1068,7 @@ def _set_out1_derivation(self, dst_entr, additional_txkey_priv):
change_addr = self.change_address()
if change_addr and addr_eq(dst_entr.addr, change_addr):
# sending change to yourself; derivation = a*R
derivation = monero.generate_key_derivation(
derivation = crypto.generate_key_derivation(
self.r_pub, self.creds.view_key_private
)

Expand All @@ -1079,7 +1079,7 @@ def _set_out1_derivation(self, dst_entr, additional_txkey_priv):
if dst_entr.is_subaddress and self.need_additional_txkeys
else self.r
)
derivation = monero.generate_key_derivation(
derivation = crypto.generate_key_derivation(
crypto.decodepoint(dst_entr.addr.view_public_key), deriv_priv
)
return derivation
Expand Down
2 changes: 1 addition & 1 deletion src/apps/monero/protocol_lite/lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ async def stealth(self):
sec = self._fetch_decrypt_key()
pay_id = self._fetch(8)

drv = monero.generate_key_derivation(pub, sec)
drv = crypto.generate_key_derivation(pub, sec)
drv += b"\x8d"
sec = crypto.keccak_hash(drv)
for i in range(8):
Expand Down
95 changes: 33 additions & 62 deletions src/apps/monero/xmr/monero.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from apps.monero.xmr import common, crypto

if False:
from apps.monero.xmr.types import *


DISPLAY_DECIMAL_POINT = const(12)


Expand All @@ -14,12 +18,6 @@ def get_subaddress_secret_key(secret_key, index=None, major=None, minor=None):
"""
Builds subaddress secret key from the subaddress index
Hs(SubAddr || a || index_major || index_minor)
:param secret_key:
:param index:
:param major:
:param minor:
:return:
"""
if index:
major = index.major
Expand All @@ -34,11 +32,6 @@ def get_subaddress_secret_key(secret_key, index=None, major=None, minor=None):
def get_subaddress_spend_public_key(view_private, spend_public, major, minor):
"""
Generates subaddress spend public key D_{major, minor}
:param view_private:
:param spend_public:
:param major:
:param minor:
:return:
"""
if major == 0 and minor == 0:
return spend_public
Expand All @@ -49,24 +42,9 @@ def get_subaddress_spend_public_key(view_private, spend_public, major, minor):
return D


def generate_key_derivation(pub_key, priv_key):
"""
Generates derivation priv_key * pub_key.
Simple ECDH.
:param pub_key:
:param priv_key:
:return:
"""
return crypto.generate_key_derivation(pub_key, priv_key)


def derive_subaddress_public_key(out_key, derivation, output_index):
"""
out_key - H_s(derivation || varint(output_index))G
:param out_key:
:param derivation:
:param output_index:
:return:
"""
crypto.check_ed25519point(out_key)
scalar = crypto.derivation_to_scalar(derivation, output_index)
Expand All @@ -78,28 +56,24 @@ def derive_subaddress_public_key(out_key, derivation, output_index):
def generate_key_image(public_key, secret_key):
"""
Key image: secret_key * H_p(pub_key)
:param public_key: encoded point
:param secret_key:
:return:
"""
point = crypto.hash_to_ec(public_key)
point2 = crypto.scalarmult(point, secret_key)
return point2


def is_out_to_acc_precomp(
subaddresses, out_key, derivation, additional_derivations, output_index
def is_out_to_account(
subaddresses: dict,
out_key: Ge25519,
derivation: Ge25519,
additional_derivations: list,
output_index: int,
):
"""
Checks whether the given transaction is sent to the account.
Searches subaddresses for the computed subaddress_spendkey.
If found, returns (major, minor), derivation.
:param subaddresses:
:param out_key:
:param derivation:
:param additional_derivations:
:param output_index:
:return:
Corresponds to is_out_to_acc_precomp() in the Monero codebase.
If found, returns (major, minor), derivation, otherwise None.
"""
subaddress_spendkey = crypto.encodepoint(
derive_subaddress_public_key(out_key, derivation, output_index)
Expand All @@ -124,11 +98,12 @@ def is_out_to_acc_precomp(
return None


def generate_key_image_helper_precomp(
ack, out_key, recv_derivation, real_output_index, received_index
):
def generate_tx_spend_and_key_image(
ack, out_key, recv_derivation, real_output_index, received_index: tuple
) -> Optional[Tuple[Sc25519, Ge25519]]:
"""
Generates UTXO spending key and key image.
Corresponds to generate_key_image_helper_precomp() in the Monero codebase.
:param ack: sender credentials
:type ack: apps.monero.xmr.sub.creds.AccountCreds
Expand Down Expand Up @@ -182,17 +157,18 @@ def generate_key_image_helper_precomp(
return scalar_step2, ki


def generate_key_image_helper(
def generate_tx_spend_and_key_image_and_derivation(
creds,
subaddresses,
out_key,
tx_public_key,
additional_tx_public_keys,
real_output_index,
):
subaddresses: dict,
out_key: Ge25519,
tx_public_key: Ge25519,
additional_tx_public_keys: list,
real_output_index: int,
) -> Tuple[Sc25519, Ge25519, Ge25519]:
"""
Generates UTXO spending key and key image.
Generates UTXO spending key and key image and corresponding derivation.
Supports subaddresses.
Corresponds to generate_key_image_helper() in the Monero codebase.
:param creds:
:param subaddresses:
Expand All @@ -202,15 +178,17 @@ def generate_key_image_helper(
:param real_output_index: index of the real output in the RCT
:return:
"""
recv_derivation = generate_key_derivation(tx_public_key, creds.view_key_private)
recv_derivation = crypto.generate_key_derivation(
tx_public_key, creds.view_key_private
)

additional_recv_derivations = []
for add_pub_key in additional_tx_public_keys:
additional_recv_derivations.append(
generate_key_derivation(add_pub_key, creds.view_key_private)
crypto.generate_key_derivation(add_pub_key, creds.view_key_private)
)

subaddr_recv_info = is_out_to_acc_precomp(
subaddr_recv_info = is_out_to_account(
subaddresses,
out_key,
recv_derivation,
Expand All @@ -220,13 +198,13 @@ def generate_key_image_helper(
if subaddr_recv_info is None:
raise XmrNoSuchAddressException("No such addr")

xi, ki = generate_key_image_helper_precomp(
xi, ki = generate_tx_spend_and_key_image(
creds, out_key, subaddr_recv_info[1], real_output_index, subaddr_recv_info[0]
)
return xi, ki, recv_derivation


def compute_subaddresses(creds, account, indices, subaddresses=None):
def compute_subaddresses(creds, account: int, indices, subaddresses=None):
"""
Computes subaddress public spend key for receiving transactions.
Expand All @@ -253,11 +231,6 @@ def compute_subaddresses(creds, account, indices, subaddresses=None):


def generate_keys(recovery_key):
"""
Wallet gen.
:param recovery_key:
:return:
"""
pub = crypto.scalarmult_base(recovery_key)
return recovery_key, pub

Expand All @@ -268,8 +241,6 @@ def generate_monero_keys(seed):
account.cpp:
crypto::secret_key account_base::generate(const crypto::secret_key& recovery_key, bool recover, bool two_random).
:param seed:
:return:
"""
spend_sec, spend_pub = generate_keys(crypto.decodeint(seed))
hash = crypto.cn_fast_hash(crypto.encodeint(spend_sec))
Expand Down
2 changes: 1 addition & 1 deletion src/apps/monero/xmr/ring_ct.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def export_key_image(
"""
from apps.monero.xmr import monero

r = monero.generate_key_image_helper(
r = monero.generate_tx_spend_and_key_image_and_derivation(
creds, subaddresses, pkey, tx_pub_key, additional_tx_pub_keys, out_idx
)
xi, ki, recv_derivation = r[:3]
Expand Down
6 changes: 6 additions & 0 deletions src/apps/monero/xmr/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if False:
from trezor.crypto import monero as tcry
from typing import * # noqa: F401

Ge25519 = tcry.ge25519
Sc25519 = tcry.bignum256modm

0 comments on commit 93af8af

Please sign in to comment.