1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-05-30 04:38:44 +00:00

perf(core): remove imports from hot path

[no changelog]
This commit is contained in:
Roman Zeyde 2025-04-10 09:14:00 +03:00 committed by Roman Zeyde
parent 3c08ce62b3
commit 07ad01220c
8 changed files with 18 additions and 33 deletions

View File

@ -2,17 +2,18 @@ from typing import TYPE_CHECKING
from trezor.crypto import base58 from trezor.crypto import base58
from trezor.crypto.hashlib import sha256 from trezor.crypto.hashlib import sha256
from trezor.enums import InputScriptType
from trezor.utils import HashWriter from trezor.utils import HashWriter
from trezor.wire import ProcessError from trezor.wire import ProcessError
from apps.common import address_type from apps.common import address_type
from .common import ecdsa_hash_pubkey, encode_bech32_address from .common import ecdsa_hash_pubkey, encode_bech32_address
from .multisig import multisig_get_pubkeys, multisig_pubkey_index
from .scripts import output_script_native_segwit, write_output_script_multisig from .scripts import output_script_native_segwit, write_output_script_multisig
if TYPE_CHECKING: if TYPE_CHECKING:
from trezor.crypto import bip32 from trezor.crypto import bip32
from trezor.enums import InputScriptType
from trezor.messages import MultisigRedeemScriptType from trezor.messages import MultisigRedeemScriptType
from apps.common.coininfo import CoinInfo from apps.common.coininfo import CoinInfo
@ -24,10 +25,6 @@ def get_address(
node: bip32.HDNode, node: bip32.HDNode,
multisig: MultisigRedeemScriptType | None = None, multisig: MultisigRedeemScriptType | None = None,
) -> str: ) -> str:
from trezor.enums import InputScriptType
from .multisig import multisig_get_pubkeys, multisig_pubkey_index
node_public_key = node.public_key() # result_cache node_public_key = node.public_key() # result_cache
if multisig: if multisig:

View File

@ -1,7 +1,10 @@
from micropython import const from micropython import const
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from .common import BIP32_WALLET_DEPTH from trezor import utils
from .common import BIP32_WALLET_DEPTH, CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
from .writers import write_bytes_prefixed
if TYPE_CHECKING: if TYPE_CHECKING:
from trezor.messages import ( from trezor.messages import (
@ -21,10 +24,6 @@ class CoinJoinAuthorization:
self.params = params self.params = params
def check_get_ownership_proof(self, msg: GetOwnershipProof) -> bool: def check_get_ownership_proof(self, msg: GetOwnershipProof) -> bool:
from trezor import utils
from .writers import write_bytes_prefixed
params = self.params # local_cache_attribute params = self.params # local_cache_attribute
# Check whether the current params matches the parameters of the request. # Check whether the current params matches the parameters of the request.
@ -47,8 +46,6 @@ class CoinJoinAuthorization:
) )
def check_internal_output(self, txo: TxOutput) -> bool: def check_internal_output(self, txo: TxOutput) -> bool:
from .common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
# Check whether the output matches the parameters of the request. # Check whether the output matches the parameters of the request.
return ( return (
len(txo.address_n) >= BIP32_WALLET_DEPTH len(txo.address_n) >= BIP32_WALLET_DEPTH

View File

@ -5,7 +5,7 @@ from trezor.wire import DataError, ProcessError
from apps.common import safety_checks from apps.common import safety_checks
from ..common import input_is_external_unverified from ..common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES, input_is_external_unverified
from ..keychain import validate_path_against_script_type from ..keychain import validate_path_against_script_type
from . import helpers, tx_weight from . import helpers, tx_weight
from .tx_info import OriginalTxInfo from .tx_info import OriginalTxInfo
@ -169,8 +169,6 @@ class BasicApprover(Approver):
raise ProcessError("Transaction has changed during signing") raise ProcessError("Transaction has changed during signing")
async def _add_output(self, txo: TxOutput, script_pubkey: bytes) -> None: async def _add_output(self, txo: TxOutput, script_pubkey: bytes) -> None:
from ..common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
if txo.address_n and not validate_path_against_script_type( if txo.address_n and not validate_path_against_script_type(
self.coin, self.coin,
address_n=txo.address_n, address_n=txo.address_n,

View File

@ -1,9 +1,14 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from trezor.enums import InputScriptType
from trezor.messages import TxOutput
from ..common import BIP32_WALLET_DEPTH, CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Any, Generic, TypeVar from typing import Any, Generic, TypeVar
from trezor.messages import TxInput, TxOutput from trezor.messages import TxInput
from apps.common.paths import Bip32Path from apps.common.paths import Bip32Path
@ -86,8 +91,6 @@ class MatchChecker(Generic[T]):
class WalletPathChecker(MatchChecker): class WalletPathChecker(MatchChecker):
def attribute_from_tx(self, txio: TxInput | TxOutput) -> Any: def attribute_from_tx(self, txio: TxInput | TxOutput) -> Any:
from ..common import BIP32_WALLET_DEPTH
if len(txio.address_n) <= BIP32_WALLET_DEPTH: if len(txio.address_n) <= BIP32_WALLET_DEPTH:
return None return None
return txio.address_n[:-BIP32_WALLET_DEPTH] return txio.address_n[:-BIP32_WALLET_DEPTH]
@ -113,11 +116,6 @@ class MultisigFingerprintChecker(MatchChecker):
class ScriptTypeChecker(MatchChecker): class ScriptTypeChecker(MatchChecker):
def attribute_from_tx(self, txio: TxInput | TxOutput) -> Any: def attribute_from_tx(self, txio: TxInput | TxOutput) -> Any:
from trezor.enums import InputScriptType
from trezor.messages import TxOutput
from ..common import CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES
if TxOutput.is_type_of(txio): if TxOutput.is_type_of(txio):
script_type = CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES[txio.script_type] script_type = CHANGE_OUTPUT_TO_INPUT_SCRIPT_TYPES[txio.script_type]
else: else:

View File

@ -3,7 +3,9 @@ from typing import TYPE_CHECKING
from ..writers import ( from ..writers import (
TX_HASH_SIZE, TX_HASH_SIZE,
write_bytes_fixed, write_bytes_fixed,
write_bytes_prefixed,
write_bytes_reversed, write_bytes_reversed,
write_tx_output,
write_uint32, write_uint32,
write_uint64, write_uint64,
) )
@ -59,8 +61,6 @@ class BitcoinSigHasher:
self.h_outputs = HashWriter(sha256()) self.h_outputs = HashWriter(sha256())
def add_input(self, txi: TxInput, script_pubkey: bytes) -> None: def add_input(self, txi: TxInput, script_pubkey: bytes) -> None:
from ..writers import write_bytes_prefixed
write_bytes_reversed(self.h_prevouts, txi.prev_hash, TX_HASH_SIZE) write_bytes_reversed(self.h_prevouts, txi.prev_hash, TX_HASH_SIZE)
write_uint32(self.h_prevouts, txi.prev_index) write_uint32(self.h_prevouts, txi.prev_index)
write_uint64(self.h_amounts, txi.amount) write_uint64(self.h_amounts, txi.amount)
@ -68,8 +68,6 @@ class BitcoinSigHasher:
write_uint32(self.h_sequences, txi.sequence) write_uint32(self.h_sequences, txi.sequence)
def add_output(self, txo: TxOutput, script_pubkey: bytes) -> None: def add_output(self, txo: TxOutput, script_pubkey: bytes) -> None:
from ..writers import write_tx_output
write_tx_output(self.h_outputs, txo, script_pubkey) write_tx_output(self.h_outputs, txo, script_pubkey)
def hash143( def hash143(

View File

@ -14,6 +14,8 @@ from apps.common.writers import ( # noqa: F401
write_uint64_le, write_uint64_le,
) )
from .multisig import multisig_fingerprint
if TYPE_CHECKING: if TYPE_CHECKING:
from trezor.messages import PrevInput, PrevOutput, TxInput, TxOutput from trezor.messages import PrevInput, PrevOutput, TxInput, TxOutput
from trezor.utils import HashWriter from trezor.utils import HashWriter
@ -40,8 +42,6 @@ def write_tx_input(w: Writer, i: TxInput | PrevInput, script: bytes) -> None:
def write_tx_input_check(w: Writer, i: TxInput) -> None: def write_tx_input_check(w: Writer, i: TxInput) -> None:
from .multisig import multisig_fingerprint
write_uint32(w, len(i.address_n)) write_uint32(w, len(i.address_n))
for n in i.address_n: for n in i.address_n:
write_uint32(w, n) write_uint32(w, n)

View File

@ -128,8 +128,6 @@ def convertbits(
def decode(hrp: str, addr: str) -> OptionalTuple2[int, bytes]: def decode(hrp: str, addr: str) -> OptionalTuple2[int, bytes]:
"""Decode a segwit address.""" """Decode a segwit address."""
from trezorcrypto import bech32
try: try:
hrpgot, data, spec = bech32.decode(addr) hrpgot, data, spec = bech32.decode(addr)
decoded = bytes(convertbits(data[1:], 5, 8, False)) decoded = bytes(convertbits(data[1:], 5, 8, False))

View File

@ -5,6 +5,7 @@ from storage.cache_common import DataCache, InvalidSessionError
from trezor import log, protobuf from trezor import log, protobuf
from trezor.wire.codec import codec_v1 from trezor.wire.codec import codec_v1
from trezor.wire.context import UnexpectedMessageException from trezor.wire.context import UnexpectedMessageException
from trezor.wire.message_handler import wrap_protobuf_load
from trezor.wire.protocol_common import Context, Message from trezor.wire.protocol_common import Context, Message
if TYPE_CHECKING: if TYPE_CHECKING:
@ -69,8 +70,6 @@ class CodecContext(Context):
) )
# look up the protobuf class and parse the message # look up the protobuf class and parse the message
from ..message_handler import wrap_protobuf_load
return wrap_protobuf_load(msg.data, expected_type) return wrap_protobuf_load(msg.data, expected_type)
async def write(self, msg: protobuf.MessageType) -> None: async def write(self, msg: protobuf.MessageType) -> None: