diff --git a/core/src/apps/monero/signing/offloading_keys.py b/core/src/apps/monero/signing/offloading_keys.py index d463e116d..7611ee8b7 100644 --- a/core/src/apps/monero/signing/offloading_keys.py +++ b/core/src/apps/monero/signing/offloading_keys.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING from trezor import utils -from apps.monero.xmr import crypto, crypto_helpers +from apps.monero.xmr import crypto_helpers if TYPE_CHECKING: from trezor.messages import ( @@ -81,13 +81,6 @@ def hmac_key_txout(key_hmac: bytes, idx: int) -> bytes: return _build_key(key_hmac, b"txout", idx) -def hmac_key_txout_asig(key_hmac: bytes, idx: int) -> bytes: - """ - rsig[i] hmac key. Range signature HMAC - """ - return _build_key(key_hmac, b"txout-asig", idx) - - def enc_key_txin_alpha(key_enc: bytes, idx: int) -> bytes: """ Chacha20Poly1305 encryption key for alpha[i] used in Pedersen commitment in pseudo_outs[i] @@ -102,13 +95,6 @@ def enc_key_spend(key_enc: bytes, idx: int) -> bytes: return _build_key(key_enc, b"txin-spend", idx) -def enc_key_cout(key_enc: bytes, idx: int | None = None) -> bytes: - """ - Chacha20Poly1305 encryption key for multisig C values from MLASG. - """ - return _build_key(key_enc, b"cout", idx) - - def key_signature(master: bytes, idx: int, is_iv: bool = False) -> bytes: """ Generates signature offloading related offloading keys @@ -116,13 +102,6 @@ def key_signature(master: bytes, idx: int, is_iv: bool = False) -> bytes: return _build_key(master, b"sig-iv" if is_iv else b"sig-key", idx) -def det_comm_masks(key_enc: bytes, idx: int) -> crypto.Scalar: - """ - Deterministic output commitment masks - """ - return crypto_helpers.decodeint(_build_key(key_enc, b"out-mask", idx)) - - def gen_hmac_vini( key: bytes, src_entr: MoneroTransactionSourceEntry, vini_bin: bytes, idx: int ) -> bytes: diff --git a/core/src/apps/monero/xmr/addresses.py b/core/src/apps/monero/xmr/addresses.py index eaeb417cc..24f3a5184 100644 --- a/core/src/apps/monero/xmr/addresses.py +++ b/core/src/apps/monero/xmr/addresses.py @@ -1,9 +1,6 @@ from typing import TYPE_CHECKING from trezor.crypto import monero as tcry -from trezor.enums import MoneroNetworkType - -from apps.monero.xmr.networks import net_version if TYPE_CHECKING: from trezor.messages import MoneroAccountPublicAddress @@ -29,28 +26,6 @@ def encode_addr( return tcry.xmr_base58_addr_encode_check(ord(version), bytes(buf)) -def decode_addr(addr: bytes) -> tuple[int, bytes, bytes]: - """ - Given address, get version and public spend and view keys. - """ - d, version = tcry.xmr_base58_addr_decode_check(bytes(addr)) - pub_spend_key = d[0:32] - pub_view_key = d[32:64] - return version, pub_spend_key, pub_view_key - - -def public_addr_encode( - pub_addr: MoneroAccountPublicAddress, - is_sub: bool = False, - net: MoneroNetworkType = MoneroNetworkType.MAINNET, -) -> str: - """ - Encodes public address to Monero address - """ - net_ver = net_version(net, is_sub) - return encode_addr(net_ver, pub_addr.spend_public_key, pub_addr.view_public_key) - - def classify_subaddresses( tx_dests: list[MoneroTransactionDestinationEntry], change_addr: MoneroAccountPublicAddress, diff --git a/core/src/apps/monero/xmr/bulletproof.py b/core/src/apps/monero/xmr/bulletproof.py index e83ce7b02..60ee54bf3 100644 --- a/core/src/apps/monero/xmr/bulletproof.py +++ b/core/src/apps/monero/xmr/bulletproof.py @@ -85,11 +85,6 @@ def _copy_key(dst: bytearray | None, src: bytes) -> bytearray: return dst -def _init_key(val: bytes, dst: bytearray | None = None) -> bytearray: - dst = _ensure_dst_key(dst) - return _copy_key(dst, val) - - def _load_scalar(dst: crypto.Scalar | None, a: ScalarDst) -> crypto.Scalar: return ( crypto.sc_copy(dst, a) @@ -139,14 +134,6 @@ def _scalarmult8(dst: bytearray | None, P, tmp_pt: crypto.Point = _tmp_pt_1): return dst -def _scalarmultH(dst: bytearray, x: bytes) -> bytearray: - dst = _ensure_dst_key(dst) - crypto.decodeint_into(_tmp_sc_1, x) - crypto.scalarmult_into(_tmp_pt_1, _XMR_HP, _tmp_sc_1) - crypto.encodepoint_into(dst, _tmp_pt_1) - return dst - - def _scalarmult_base(dst: bytearray, x: bytes) -> bytearray: dst = _ensure_dst_key(dst) crypto.decodeint_into_noreduce(_tmp_sc_1, x) @@ -199,15 +186,6 @@ def _sc_mul(dst: bytearray | None, a: bytes, b: bytes | crypto.Scalar) -> bytear return dst -def _sc_mul8(dst: bytearray | None, a: bytes) -> bytearray: - dst = _ensure_dst_key(dst) - crypto.decodeint_into_noreduce(_tmp_sc_1, a) - crypto.decodeint_into_noreduce(_tmp_sc_2, _EIGHT) - crypto.sc_mul_into(_tmp_sc_3, _tmp_sc_1, _tmp_sc_2) - crypto.encodeint_into(dst, _tmp_sc_3) - return dst - - def _sc_muladd( dst: ScalarDst | None, a: bytes | crypto.Scalar, @@ -234,16 +212,6 @@ def _sc_muladd( return dst -def _sc_mulsub(dst: bytearray | None, a: bytes, b: bytes, c: bytes) -> bytearray: - dst = _ensure_dst_key(dst) - crypto.decodeint_into_noreduce(_tmp_sc_1, a) - crypto.decodeint_into_noreduce(_tmp_sc_2, b) - crypto.decodeint_into_noreduce(_tmp_sc_3, c) - crypto.sc_mulsub_into(_tmp_sc_4, _tmp_sc_1, _tmp_sc_2, _tmp_sc_3) - crypto.encodeint_into(dst, _tmp_sc_4) - return dst - - def _add_keys(dst: bytearray | None, A: bytes, B: bytes) -> bytearray: dst = _ensure_dst_key(dst) crypto.decodepoint_into(_tmp_pt_1, A) @@ -253,15 +221,6 @@ def _add_keys(dst: bytearray | None, A: bytes, B: bytes) -> bytearray: return dst -def _sub_keys(dst: bytearray | None, A: bytes, B: bytes) -> bytearray: - dst = _ensure_dst_key(dst) - crypto.decodepoint_into(_tmp_pt_1, A) - crypto.decodepoint_into(_tmp_pt_2, B) - crypto.point_sub_into(_tmp_pt_3, _tmp_pt_1, _tmp_pt_2) - crypto.encodepoint_into(dst, _tmp_pt_3) - return dst - - def _add_keys2(dst: bytearray | None, a: bytes, b: bytes, B: bytes) -> bytearray: dst = _ensure_dst_key(dst) crypto.decodeint_into_noreduce(_tmp_sc_1, a) @@ -447,11 +406,6 @@ class KeyVBase(Generic[T]): def read(self, idx: int, buff: bytes, offset: int = 0) -> bytes: raise NotImplementedError - def slice(self, res, start: int, stop: int): - for i in range(start, stop): - res[i - start] = self[i] - return res - def slice_view(self, start: int, stop: int) -> "KeyVSliced": return KeyVSliced(self, start, stop) @@ -626,32 +580,6 @@ class KeyV(KeyVBaseType[T]): self.size = nsize self._set_mv() - def realloc_init_from(self, nsize, src, offset: int = 0, collect: int = False): - if not isinstance(src, KeyV): - raise ValueError("KeyV supported only") - self.realloc(nsize, collect) - - if not self.chunked and not src.chunked: - assert isinstance(self.d, bytearray) - assert isinstance(src.d, (bytes, bytearray)) - memcpy(self.d, 0, src.d, offset << 5, nsize << 5) - - elif self.chunked and not src.chunked or self.chunked and src.chunked: - for i in range(nsize): - self.read(i, src.to(i + offset)) - - elif not self.chunked and src.chunked: - assert isinstance(self.d, bytearray) - assert isinstance(src.d, list) - for i in range(nsize >> _CHBITS): - memcpy( - self.d, - i << 11, - src.d[i + (offset >> _CHBITS)], - (offset & (_CHSIZE - 1)) << 5 if i == 0 else 0, - nsize << 5 if i <= nsize >> _CHBITS else (nsize & _CHSIZE) << 5, - ) - class KeyVEval(KeyVBase): """ @@ -690,40 +618,6 @@ class KeyVEval(KeyVBase): return buff if buff else self.buff -class KeyVSized(KeyVBase): - """ - Resized vector, wrapping possibly larger vector - (e.g., precomputed, but has to have exact size for further computations) - """ - - __slots__ = ("current_idx", "size", "wrapped") - - def __init__(self, wrapped, new_size): - super().__init__(new_size) - self.wrapped = wrapped - - def __getitem__(self, item): - return self.wrapped[self.idxize(item)] - - def __setitem__(self, key, value): - self.wrapped[self.idxize(key)] = value - - -class KeyVConst(KeyVBase): - __slots__ = ("current_idx", "size", "elem") - - def __init__(self, size, elem, copy=True): - super().__init__(size) - self.elem = _init_key(elem) if copy else elem - - def __getitem__(self, item): - return self.elem - - def to(self, idx: int, buff: bytearray, offset: int = 0): - memcpy(buff, offset, self.elem, 0, 32) - return buff if buff else self.elem - - class KeyVPrecomp(KeyVBase): """ Vector with possibly large size and some precomputed prefix. @@ -1197,32 +1091,6 @@ def _hadamard_fold(v, a, b, into=None, into_offset: int = 0, vR=None, vRoff=0): return into -def _cross_inner_product(l0, r0, l1, r1): - """ - t1 = l0 . r1 + l1 . r0 - t2 = l1 . r1 - """ - sc_t1 = crypto.Scalar() - sc_t2 = crypto.Scalar() - tl = crypto.Scalar() - tr = crypto.Scalar() - - for i in range(len(l0)): - crypto.decodeint_into_noreduce(tl, l0.to(i)) - crypto.decodeint_into_noreduce(tr, r1.to(i)) - crypto.sc_muladd_into(sc_t1, tl, tr, sc_t1) - - crypto.decodeint_into_noreduce(tl, l1.to(i)) - crypto.sc_muladd_into(sc_t2, tl, tr, sc_t2) - - crypto.decodeint_into_noreduce(tr, r0.to(i)) - crypto.sc_muladd_into(sc_t1, tl, tr, sc_t1) - - _gc_iter(i) - - return crypto_helpers.encodeint(sc_t1), crypto_helpers.encodeint(sc_t2) - - def _hash_cache_mash(dst, hash_cache, *args): dst = _ensure_dst_key(dst) ctx = crypto_helpers.get_keccak() @@ -1280,9 +1148,6 @@ class MultiExpSequential: def add_pair(self, scalar, point) -> None: self._acc(scalar, point) - def add_scalar(self, scalar) -> None: - self._acc(scalar, self.get_point(self.current_idx)) - def add_scalar_idx(self, scalar, idx: int) -> None: self._acc(scalar, self.get_point(idx)) @@ -1414,9 +1279,6 @@ class BulletProofPlusBuilder: size, self.Hprec, lambda i, d: _get_exponent_plus(d, _XMR_H, i * 2) ) - def vector_exponent(self, a, b, dst=None, a_raw=None, b_raw=None): - return _vector_exponent_custom(self.Gprec, self.Hprec, a, b, dst, a_raw, b_raw) - def prove( self, sv: list[crypto.Scalar], gamma: list[crypto.Scalar] ) -> BulletproofPlus: diff --git a/core/src/apps/monero/xmr/clsag.py b/core/src/apps/monero/xmr/clsag.py index cabe732b9..c2daa86b3 100644 --- a/core/src/apps/monero/xmr/clsag.py +++ b/core/src/apps/monero/xmr/clsag.py @@ -54,8 +54,6 @@ if TYPE_CHECKING: from .serialize_messages.tx_ct_key import CtKey from trezor.messages import MoneroRctKeyPublic - KeyM = list[list[bytes]] - T = TypeVar("T") def list_of_type(lst: list[Any], typ: type[T]) -> TypeGuard[list[T]]: diff --git a/core/src/apps/monero/xmr/crypto_helpers.py b/core/src/apps/monero/xmr/crypto_helpers.py index a66a9d101..b9d8bc6d3 100644 --- a/core/src/apps/monero/xmr/crypto_helpers.py +++ b/core/src/apps/monero/xmr/crypto_helpers.py @@ -119,43 +119,6 @@ def get_subaddress_secret_key( return tcry.xmr_get_subaddress_secret_key(None, major, minor, secret_key) -def generate_signature( - data: bytes, priv: tcry.Scalar -) -> tuple[tcry.Scalar, tcry.Scalar, tcry.Point]: - """ - Generate EC signature - crypto_ops::generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig) - """ - pub = tcry.scalarmult_base_into(None, priv) - - k = tcry.random_scalar() - comm = tcry.scalarmult_base_into(None, k) - - buff = data + encodepoint(pub) + encodepoint(comm) - c = tcry.hash_to_scalar_into(None, buff) - r = tcry.sc_mulsub_into(None, priv, c, k) - return c, r, pub - - -def check_signature( - data: bytes, c: tcry.Scalar, r: tcry.Scalar, pub: tcry.Point -) -> bool: - """ - EC signature verification - """ - tcry.ge25519_check(pub) - if tcry.sc_check(c) != 0 or tcry.sc_check(r) != 0: - raise ValueError("Signature error") - - tmp2 = tcry.point_add_into( - None, tcry.scalarmult_into(None, pub, c), tcry.scalarmult_base_into(None, r) - ) - buff = data + encodepoint(pub) + encodepoint(tmp2) - tmp_c = tcry.hash_to_scalar_into(None, buff) - res = tcry.sc_sub_into(None, tmp_c, c) - return tcry.sc_iszero(res) - - def xor8(buff: bytearray, key: bytes) -> bytes: for i in range(8): buff[i] ^= key[i] diff --git a/core/src/apps/monero/xmr/serialize/__init__.py b/core/src/apps/monero/xmr/serialize/__init__.py index c410ea0c6..23290ec4a 100644 --- a/core/src/apps/monero/xmr/serialize/__init__.py +++ b/core/src/apps/monero/xmr/serialize/__init__.py @@ -1,4 +1,3 @@ -import gc from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -28,12 +27,3 @@ def dump_msg( msg_type.dump(writer, msg) return writer.get_buffer() - - -def dump_msg_gc( - msg: MessageType, preallocate: int | None = None, prefix: bytes | None = None -) -> bytes: - buf = dump_msg(msg, preallocate, prefix) - del msg - gc.collect() - return buf diff --git a/core/src/apps/monero/xmr/serialize/base_types.py b/core/src/apps/monero/xmr/serialize/base_types.py index 04557a525..6becfc671 100644 --- a/core/src/apps/monero/xmr/serialize/base_types.py +++ b/core/src/apps/monero/xmr/serialize/base_types.py @@ -1,11 +1,6 @@ from typing import TYPE_CHECKING -from apps.monero.xmr.serialize.int_serialize import ( - dump_uint, - dump_uvarint, - load_uint, - load_uvarint, -) +from apps.monero.xmr.serialize.int_serialize import dump_uvarint, load_uvarint if TYPE_CHECKING: from typing import Protocol, TypeVar, Union @@ -50,19 +45,3 @@ class UVarintType: @staticmethod def dump(writer: Writer, n: int) -> None: return dump_uvarint(writer, n) - - -class IntType: - WIDTH = 0 - - @classmethod - def load(cls, reader: Reader) -> int: - return load_uint(reader, cls.WIDTH) - - @classmethod - def dump(cls, writer: Writer, n: int): - return dump_uint(writer, n, cls.WIDTH) - - -class UInt8(IntType): - WIDTH = 1 diff --git a/core/src/apps/monero/xmr/serialize/int_serialize.py b/core/src/apps/monero/xmr/serialize/int_serialize.py index 871faf0d0..ad3b673aa 100644 --- a/core/src/apps/monero/xmr/serialize/int_serialize.py +++ b/core/src/apps/monero/xmr/serialize/int_serialize.py @@ -6,20 +6,6 @@ if TYPE_CHECKING: _UINT_BUFFER = bytearray(1) -def load_uint(reader: Reader, width: int) -> int: - """ - Constant-width integer serialization - """ - buffer = _UINT_BUFFER - result = 0 - shift = 0 - for _ in range(width): - reader.readinto(buffer) - result += buffer[0] << shift - shift += 8 - return result - - def dump_uint(writer: Writer, n: int, width: int) -> None: """ Constant-width integer serialization @@ -42,20 +28,6 @@ def uvarint_size(n: int) -> int: return bts -def load_uvarint_b(buffer: bytes) -> int: - """ - Variable int deserialization, synchronous from buffer. - """ - result = 0 - idx = 0 - byte = 0x80 - while byte & 0x80: - byte = buffer[idx] - result += (byte & 0x7F) << (7 * idx) - idx += 1 - return result - - def dump_uvarint_b(n: int) -> bytearray: """ Serializes uvarint to the buffer @@ -79,18 +51,6 @@ def dump_uvarint_b_into(n: int, buffer: bytearray, offset: int = 0) -> bytearray return buffer -def dump_uint_b_into( - n: int, width: int, buffer: bytearray, offset: int = 0 -) -> bytearray: - """ - Serializes fixed size integer to the buffer - """ - for idx in range(width): - buffer[idx + offset] = n & 0xFF - n >>= 8 - return buffer - - def load_uvarint(reader: Reader) -> int: buffer = _UINT_BUFFER result = 0 diff --git a/core/src/apps/monero/xmr/serialize/message_types.py b/core/src/apps/monero/xmr/serialize/message_types.py index f3f6588d6..7605b00c0 100644 --- a/core/src/apps/monero/xmr/serialize/message_types.py +++ b/core/src/apps/monero/xmr/serialize/message_types.py @@ -17,24 +17,6 @@ else: T = 0 -class UnicodeType: - """ - Unicode data in UTF-8 encoding. - """ - - @staticmethod - def dump(writer: Writer, s: str) -> None: - dump_uvarint(writer, len(s)) - writer.write(s.encode()) - - @staticmethod - def load(reader: Reader) -> str: - ivalue = load_uvarint(reader) - fvalue = bytearray(ivalue) - reader.readinto(fvalue) - return str(fvalue) - - class BlobType: """ Binary data, represented as bytearray. BlobType is only a scheme diff --git a/core/src/apps/monero/xmr/serialize/readwriter.py b/core/src/apps/monero/xmr/serialize/readwriter.py index 5df774f51..d23fd0517 100644 --- a/core/src/apps/monero/xmr/serialize/readwriter.py +++ b/core/src/apps/monero/xmr/serialize/readwriter.py @@ -29,9 +29,6 @@ class MemoryReaderWriter: self.buffer = buffer self.woffset = len(buffer) - def is_empty(self) -> bool: - return self.offset == len(self.buffer) or self.offset == self.woffset - def preallocate(self, size: int) -> None: self.buffer = bytearray(size) self.offset = 0 diff --git a/core/src/apps/monero/xmr/serialize_messages/base.py b/core/src/apps/monero/xmr/serialize_messages/base.py index e77780bb7..1b4952bd1 100644 --- a/core/src/apps/monero/xmr/serialize_messages/base.py +++ b/core/src/apps/monero/xmr/serialize_messages/base.py @@ -26,5 +26,4 @@ class ECKey(BlobType): ECPoint = Hash -ECPublicKey = ECPoint KeyImage = ECPoint diff --git a/core/tests/test_apps.monero.serializer.py b/core/tests/test_apps.monero.serializer.py index 4ef4828c1..dba49456c 100644 --- a/core/tests/test_apps.monero.serializer.py +++ b/core/tests/test_apps.monero.serializer.py @@ -1,11 +1,9 @@ from common import * -from trezor import log, loop, utils +from trezor import utils if not utils.BITCOIN_ONLY: from apps.monero.xmr.serialize.int_serialize import ( - dump_uint, dump_uvarint, - load_uint, load_uvarint, ) from apps.monero.xmr.serialize.readwriter import MemoryReaderWriter @@ -56,7 +54,7 @@ class TestMoneroSerializer(unittest.TestCase): :return: """ msg = TxinToKey( - amount=123, key_offsets=[1, 2, 3, 2 ** 76], k_image=bytearray(range(32)) + amount=123, key_offsets=[1, 2, 3, 2**76], k_image=bytearray(range(32)) ) writer = MemoryReaderWriter()