From 3af75c0b8b28cc7fd28f6c16894cfe411aa49338 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Sun, 9 Jun 2019 11:24:06 +0200 Subject: [PATCH] core/typing: improve extmod types --- .../extmod/modtrezorconfig/modtrezorconfig.c | 4 +- .../modtrezorcrypto/modtrezorcrypto-aes.h | 2 +- .../modtrezorcrypto/modtrezorcrypto-bip32.h | 8 ++- .../modtrezorcrypto-blake256.h | 4 +- .../modtrezorcrypto/modtrezorcrypto-blake2b.h | 6 +- .../modtrezorcrypto/modtrezorcrypto-blake2s.h | 6 +- .../modtrezorcrypto-chacha20poly1305.h | 2 +- .../modtrezorcrypto/modtrezorcrypto-crc.h | 6 ++ .../modtrezorcrypto/modtrezorcrypto-groestl.h | 4 +- .../modtrezorcrypto/modtrezorcrypto-monero.h | 16 ++--- .../modtrezorcrypto/modtrezorcrypto-pbkdf2.h | 4 +- .../modtrezorcrypto/modtrezorcrypto-random.h | 3 +- .../modtrezorcrypto/modtrezorcrypto-rfc6979.h | 2 +- .../modtrezorcrypto-ripemd160.h | 4 +- .../modtrezorcrypto/modtrezorcrypto-sha1.h | 4 +- .../modtrezorcrypto/modtrezorcrypto-sha256.h | 4 +- .../modtrezorcrypto-sha3-256.h | 6 +- .../modtrezorcrypto-sha3-512.h | 6 +- .../modtrezorcrypto/modtrezorcrypto-sha512.h | 4 +- .../modtrezorcrypto/modtrezorcrypto-shamir.h | 2 +- core/embed/extmod/modtrezorio/modtrezorio.c | 10 +++ .../extmod/modtrezorui/modtrezorui-display.h | 9 +++ .../extmod/modtrezorutils/modtrezorutils.c | 9 ++- core/mocks/{gc.py => gc.pyi} | 0 .../{trezorconfig.py => trezorconfig.pyi} | 4 +- .../{__init__.py => __init__.pyi} | 58 +++++++++++----- .../trezorcrypto/{bip32.py => bip32.pyi} | 35 ++++++---- .../trezorcrypto/{bip39.py => bip39.pyi} | 0 core/mocks/generated/trezorcrypto/crc.pyi | 8 +++ .../{curve25519.py => curve25519.pyi} | 0 .../trezorcrypto/{ed25519.py => ed25519.pyi} | 0 .../trezorcrypto/{monero.py => monero.pyi} | 16 ++--- .../trezorcrypto/{nem.py => nem.pyi} | 0 .../{nist256p1.py => nist256p1.pyi} | 0 .../trezorcrypto/{random.py => random.pyi} | 3 +- .../{secp256k1.py => secp256k1.pyi} | 0 .../{secp256k1_zkp.py => secp256k1_zkp.pyi} | 0 .../trezorcrypto/{shamir.py => shamir.pyi} | 2 +- .../trezorcrypto/{slip39.py => slip39.pyi} | 0 .../generated/{trezorio.py => trezorio.pyi} | 7 ++ .../generated/{trezorui.py => trezorui.pyi} | 7 ++ .../{trezorutils.py => trezorutils.pyi} | 8 ++- .../mocks/{micropython.py => micropython.pyi} | 4 +- core/mocks/{sys.py => sys.pyi} | 0 core/mocks/typing.py | 69 ------------------- core/mocks/{ubinascii.py => ubinascii.pyi} | 0 core/mocks/{uctypes.py => uctypes.pyi} | 0 core/mocks/{uio.py => uio.pyi} | 0 core/mocks/uos.pyi | 1 + core/mocks/{ustruct.py => ustruct.pyi} | 0 core/mocks/{utime.py => utime.pyi} | 0 core/mocks/{utimeq.py => utimeq.pyi} | 0 core/tools/build_mocks | 3 +- 53 files changed, 205 insertions(+), 145 deletions(-) rename core/mocks/{gc.py => gc.pyi} (100%) rename core/mocks/generated/{trezorconfig.py => trezorconfig.pyi} (96%) rename core/mocks/generated/trezorcrypto/{__init__.py => __init__.pyi} (89%) rename core/mocks/generated/trezorcrypto/{bip32.py => bip32.pyi} (80%) rename core/mocks/generated/trezorcrypto/{bip39.py => bip39.pyi} (100%) create mode 100644 core/mocks/generated/trezorcrypto/crc.pyi rename core/mocks/generated/trezorcrypto/{curve25519.py => curve25519.pyi} (100%) rename core/mocks/generated/trezorcrypto/{ed25519.py => ed25519.pyi} (100%) rename core/mocks/generated/trezorcrypto/{monero.py => monero.pyi} (95%) rename core/mocks/generated/trezorcrypto/{nem.py => nem.pyi} (100%) rename core/mocks/generated/trezorcrypto/{nist256p1.py => nist256p1.pyi} (100%) rename core/mocks/generated/trezorcrypto/{random.py => random.pyi} (88%) rename core/mocks/generated/trezorcrypto/{secp256k1.py => secp256k1.pyi} (100%) rename core/mocks/generated/trezorcrypto/{secp256k1_zkp.py => secp256k1_zkp.pyi} (100%) rename core/mocks/generated/trezorcrypto/{shamir.py => shamir.pyi} (88%) rename core/mocks/generated/trezorcrypto/{slip39.py => slip39.pyi} (100%) rename core/mocks/generated/{trezorio.py => trezorio.pyi} (93%) rename core/mocks/generated/{trezorui.py => trezorui.pyi} (94%) rename core/mocks/generated/{trezorutils.py => trezorutils.pyi} (84%) rename core/mocks/{micropython.py => micropython.pyi} (64%) rename core/mocks/{sys.py => sys.pyi} (100%) delete mode 100644 core/mocks/typing.py rename core/mocks/{ubinascii.py => ubinascii.pyi} (100%) rename core/mocks/{uctypes.py => uctypes.pyi} (100%) rename core/mocks/{uio.py => uio.pyi} (100%) create mode 100644 core/mocks/uos.pyi rename core/mocks/{ustruct.py => ustruct.pyi} (100%) rename core/mocks/{utime.py => utime.pyi} (100%) rename core/mocks/{utimeq.py => utimeq.pyi} (100%) diff --git a/core/embed/extmod/modtrezorconfig/modtrezorconfig.c b/core/embed/extmod/modtrezorconfig/modtrezorconfig.c index 28a034965..55c6513b5 100644 --- a/core/embed/extmod/modtrezorconfig/modtrezorconfig.c +++ b/core/embed/extmod/modtrezorconfig/modtrezorconfig.c @@ -47,7 +47,9 @@ STATIC secbool wrapped_ui_wait_callback(uint32_t wait, uint32_t progress, return secfalse; } -/// def init(ui_wait_callback: Tuple[int, Callable[int, None]] = None) -> None: +/// def init( +/// ui_wait_callback: Callable[[int, int, str], bool] = None +/// ) -> None: /// """ /// Initializes the storage. Must be called before any other method is /// called from this module! diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h index fe759eef7..34c84cfe9 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h @@ -32,7 +32,7 @@ enum AESMode { /// package: trezorcrypto.__init__ -/// class AES: +/// class aes: /// """ /// AES context. /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h index 98fc4a7a4..d92aa3a46 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h @@ -489,6 +489,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1( mod_trezorcrypto_HDNode_ethereum_pubkeyhash_obj, mod_trezorcrypto_HDNode_ethereum_pubkeyhash); +/// def __del__(self) -> None: +/// """ +/// Cleans up sensitive memory. +/// """ STATIC mp_obj_t mod_trezorcrypto_HDNode___del__(mp_obj_t self) { mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self); o->fingerprint = 0; @@ -547,8 +551,10 @@ STATIC const mp_obj_type_t mod_trezorcrypto_HDNode_type = { .locals_dict = (void *)&mod_trezorcrypto_HDNode_locals_dict, }; +/// mock:global + /// def deserialize( -/// self, value: str, version_public: int, version_private: int +/// value: str, version_public: int, version_private: int /// ) -> HDNode: /// """ /// Construct a BIP0032 HD node from a base58-serialized value. diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h index 47750427d..9d9f2cfc0 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h @@ -24,10 +24,12 @@ /// package: trezorcrypto.__init__ -/// class Blake256: +/// class blake256: /// """ /// Blake256 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Blake256_t { mp_obj_base_t base; BLAKE256_CTX ctx; diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h index 84d83aeb6..700b6f91b 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h @@ -26,10 +26,12 @@ /// package: trezorcrypto.__init__ -/// class Blake2b: +/// class blake2b: /// """ /// Blake2b context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Blake2b_t { mp_obj_base_t base; BLAKE2B_CTX ctx; @@ -40,7 +42,7 @@ STATIC mp_obj_t mod_trezorcrypto_Blake2b_update(mp_obj_t self, mp_obj_t data); /// def __init__( /// self, /// data: bytes = None, -/// outlen: int = Blake2b.digest_size, +/// outlen: int = blake2b.digest_size, /// personal: bytes = None, /// ) -> None: /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h index 3788d131f..2aeb94dc1 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h @@ -26,10 +26,12 @@ /// package: trezorcrypto.__init__ -/// class Blake2s: +/// class blake2s: /// """ /// Blake2s context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Blake2s_t { mp_obj_base_t base; BLAKE2S_CTX ctx; @@ -40,7 +42,7 @@ STATIC mp_obj_t mod_trezorcrypto_Blake2s_update(mp_obj_t self, mp_obj_t data); /// def __init__( /// self, /// data: bytes = None, -/// outlen: int = Blake2s.digest_size, +/// outlen: int = blake2s.digest_size, /// key: bytes = None, /// personal: bytes = None, /// ) -> None: diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h index d748a8780..e413ab3da 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h @@ -24,7 +24,7 @@ /// package: trezorcrypto.__init__ -/// class ChaCha20Poly1305: +/// class chacha20poly1305: /// """ /// ChaCha20Poly1305 context. /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h index c7e54c1bc..14f9e84f1 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h @@ -23,6 +23,12 @@ #include "crc.h" +/// package: trezorcrypto.crc + +/// def crc32(data: bytes, crc: int = 0) -> int: +/// """ +/// Computes a CRC32 checksum of `data`. +/// """ mp_obj_t mod_trezorcrypto_crc_crc32(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-groestl.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-groestl.h index 60fc57848..37d018bd5 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-groestl.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-groestl.h @@ -27,10 +27,12 @@ /// package: trezorcrypto.__init__ -/// class Groestl512: +/// class groestl512: /// """ /// GROESTL512 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Groestl512_t { mp_obj_base_t base; GROESTL512_CTX ctx; diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-monero.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-monero.h index ab1d35bcf..bb7a36d6f 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-monero.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-monero.h @@ -180,7 +180,7 @@ STATIC void mp_unpack_scalar(bignum256modm r, const mp_obj_t arg, /// EC point on ED25519 /// """ /// -/// def __init__(x: Optional[Union[Ge25519, bytes]] = None): +/// def __init__(self, x: Optional[Union[Ge25519, bytes]] = None): /// """ /// Constructor /// """ @@ -218,7 +218,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_monero_ge25519___del___obj, /// EC scalar on SC25519 /// """ /// -/// def __init__(x: Optional[Union[Sc25519, bytes, int]] = None): +/// def __init__(self, x: Optional[Union[Sc25519, bytes, int]] = None): /// """ /// Constructor /// """ @@ -261,22 +261,22 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1( /// XMR hasher /// """ /// -/// def __init__(x: Optional[bytes] = None): +/// def __init__(self, x: Optional[bytes] = None): /// """ /// Constructor /// """ /// -/// def update(buffer: bytes): +/// def update(self, buffer: bytes) -> None: /// """ /// Update hasher /// """ /// -/// def digest() -> bytes: +/// def digest(self) -> bytes: /// """ /// Computes digest /// """ /// -/// def copy() -> Hasher: +/// def copy(self) -> Hasher: /// """ /// Creates copy of the hasher, preserving the state /// """ @@ -343,7 +343,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( mod_trezorcrypto_monero_init256_modm_obj, 0, 2, mod_trezorcrypto_monero_init256_modm); -/// def check256_modm(val: Sc25519): +/// def check256_modm(val: Sc25519) -> None: /// """ /// Throws exception if scalar is invalid /// """ @@ -640,7 +640,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( mod_trezorcrypto_monero_ge25519_set_xmr_h_obj, 0, 1, mod_trezorcrypto_monero_ge25519_set_xmr_h); -/// def ge25519_check(r: Ge25519): +/// def ge25519_check(r: Ge25519) -> None: /// """ /// Checks point, throws if not on curve /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h index 1d6d662fc..e015a7ce2 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h @@ -27,10 +27,12 @@ /// package: trezorcrypto.__init__ -/// class Pbkdf2: +/// class pbkdf2: /// """ /// PBKDF2 context. /// """ +/// HMAC_SHA256: int +/// HMAC_SHA512: int typedef struct _mp_obj_Pbkdf2_t { mp_obj_base_t base; union { diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h index 50b3715a6..a3c384950 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h @@ -39,7 +39,8 @@ STATIC mp_obj_t mod_trezorcrypto_random_uniform(mp_obj_t n) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_uniform_obj, mod_trezorcrypto_random_uniform); -/// def bytes(len: int) -> bytes: +/// import builtins +/// def bytes(len: int) -> builtins.bytes: /// """ /// Generate random bytes sequence of length len. /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h index bd3df29ea..55a840850 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h @@ -23,7 +23,7 @@ /// package: trezorcrypto.__init__ -/// class Rfc6979: +/// class rfc6979: /// """ /// RFC6979 context. /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h index 045fe7fe5..5b580041e 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h @@ -24,10 +24,12 @@ /// package: trezorcrypto.__init__ -/// class Ripemd160: +/// class ripemd160: /// """ /// RIPEMD160 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Ripemd160_t { mp_obj_base_t base; RIPEMD160_CTX ctx; diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h index 810b63ed8..6b57c4c68 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h @@ -24,10 +24,12 @@ /// package: trezorcrypto.__init__ -/// class Sha1: +/// class sha1: /// """ /// SHA1 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Sha1_t { mp_obj_base_t base; SHA1_CTX ctx; diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h index 01f49245d..5ae3f7c70 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h @@ -24,10 +24,12 @@ /// package: trezorcrypto.__init__ -/// class Sha256: +/// class sha256: /// """ /// SHA256 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Sha256_t { mp_obj_base_t base; SHA256_CTX ctx; diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h index a0157d613..0fbd25f2f 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h @@ -24,10 +24,12 @@ /// package: trezorcrypto.__init__ -/// class Sha3_256: +/// class sha3_256: /// """ /// SHA3_256 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Sha3_256_t { mp_obj_base_t base; SHA3_CTX ctx; @@ -102,7 +104,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_256_digest(mp_obj_t self) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_256_digest_obj, mod_trezorcrypto_Sha3_256_digest); -/// def copy(self) -> sha3: +/// def copy(self) -> sha3_256: /// """ /// Returns the copy of the digest object with the current state /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h index c227e58a3..6280e1382 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h @@ -24,10 +24,12 @@ /// package: trezorcrypto.__init__ -/// class Sha3_512: +/// class sha3_512: /// """ /// SHA3_512 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Sha3_512_t { mp_obj_base_t base; SHA3_CTX ctx; @@ -102,7 +104,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_512_digest(mp_obj_t self) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_512_digest_obj, mod_trezorcrypto_Sha3_512_digest); -/// def copy(self) -> sha3: +/// def copy(self) -> sha3_512: /// """ /// Returns the copy of the digest object with the current state /// """ diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h index a9ba4ebe6..5ea27f8f7 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h @@ -24,10 +24,12 @@ /// package: trezorcrypto.__init__ -/// class Sha512: +/// class sha512: /// """ /// SHA512 context. /// """ +/// block_size: int +/// digest_size: int typedef struct _mp_obj_Sha512_t { mp_obj_base_t base; SHA512_CTX ctx; diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-shamir.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-shamir.h index 5e1645394..5c75d0702 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-shamir.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-shamir.h @@ -27,7 +27,7 @@ /// package: trezorcrypto.shamir -/// def interpolate(shares, x) -> bytes: +/// def interpolate(shares: List[Tuple[int, bytes]], x: int) -> bytes: /// """ /// Returns f(x) given the Shamir shares (x_1, f(x_1)), ... , (x_k, f(x_k)). /// :param shares: The Shamir shares. diff --git a/core/embed/extmod/modtrezorio/modtrezorio.c b/core/embed/extmod/modtrezorio/modtrezorio.c index b728e8adf..49535b87b 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio.c +++ b/core/embed/extmod/modtrezorio/modtrezorio.c @@ -44,6 +44,16 @@ #include "modtrezorio-usb.h" // clang-format on +/// POLL_READ: int # wait until interface is readable and return read data +/// POLL_WRITE: int # wait until interface is writable +/// +/// TOUCH: int # interface id of the touch events +/// TOUCH_START: int # event id of touch start event +/// TOUCH_MOVE: int # event id of touch move event +/// TOUCH_END: int # event id of touch end event + +/// WireInterface = Union[HID, WebUSB] + STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = { {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorio)}, diff --git a/core/embed/extmod/modtrezorui/modtrezorui-display.h b/core/embed/extmod/modtrezorui/modtrezorui-display.h index 3d9a2547b..09b876e84 100644 --- a/core/embed/extmod/modtrezorui/modtrezorui-display.h +++ b/core/embed/extmod/modtrezorui/modtrezorui-display.h @@ -25,6 +25,15 @@ /// """ /// Provide access to device display. /// """ +/// +/// WIDTH: int # display width in pixels +/// HEIGHT: int # display height in pixels +/// FONT_SIZE: int # font height in pixels +/// FONT_MONO: int # id of monospace font +/// FONT_NORMAL: int # id of normal-width font +/// FONT_BOLD: int # id of bold-width font +/// FONT_MONO_BOLD: int # id of monospace bold-width font +/// typedef struct _mp_obj_Display_t { mp_obj_base_t base; } mp_obj_Display_t; diff --git a/core/embed/extmod/modtrezorutils/modtrezorutils.c b/core/embed/extmod/modtrezorutils/modtrezorutils.c index 859b5050c..e51998443 100644 --- a/core/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/core/embed/extmod/modtrezorutils/modtrezorutils.c @@ -58,7 +58,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorutils_consteq_obj, mod_trezorutils_consteq); /// def memcpy( -/// dst: bytearray, dst_ofs: int, src: bytearray, src_ofs: int, n: int +/// dst: bytearray, dst_ofs: int, src: bytes, src_ofs: int, n: int /// ) -> int: /// """ /// Copies at most `n` bytes from `src` at offset `src_ofs` to @@ -122,6 +122,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_set_mode_unprivileged_obj, #define PASTER(s) MP_QSTR_##s #define MP_QSTR(s) PASTER(s) +/// GITREV: str +/// VERSION_MAJOR: int +/// VERSION_MINOR: int +/// VERSION_PATCH: int +/// MODEL: str +/// EMULATOR: bool + STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorutils)}, {MP_ROM_QSTR(MP_QSTR_consteq), MP_ROM_PTR(&mod_trezorutils_consteq_obj)}, diff --git a/core/mocks/gc.py b/core/mocks/gc.pyi similarity index 100% rename from core/mocks/gc.py rename to core/mocks/gc.pyi diff --git a/core/mocks/generated/trezorconfig.py b/core/mocks/generated/trezorconfig.pyi similarity index 96% rename from core/mocks/generated/trezorconfig.py rename to core/mocks/generated/trezorconfig.pyi index a47d886b0..2dc0175b0 100644 --- a/core/mocks/generated/trezorconfig.py +++ b/core/mocks/generated/trezorconfig.pyi @@ -2,7 +2,9 @@ from typing import * # extmod/modtrezorconfig/modtrezorconfig.c -def init(ui_wait_callback: Tuple[int, Callable[int, None]] = None) -> None: +def init( + ui_wait_callback: Callable[[int, int, str], bool] = None +) -> None: """ Initializes the storage. Must be called before any other method is called from this module! diff --git a/core/mocks/generated/trezorcrypto/__init__.py b/core/mocks/generated/trezorcrypto/__init__.pyi similarity index 89% rename from core/mocks/generated/trezorcrypto/__init__.py rename to core/mocks/generated/trezorcrypto/__init__.pyi index 38353db4b..4c26b5f8f 100644 --- a/core/mocks/generated/trezorcrypto/__init__.py +++ b/core/mocks/generated/trezorcrypto/__init__.pyi @@ -2,7 +2,7 @@ from typing import * # extmod/modtrezorcrypto/modtrezorcrypto-aes.h -class AES: +class aes: """ AES context. """ @@ -24,10 +24,12 @@ class AES: # extmod/modtrezorcrypto/modtrezorcrypto-blake256.h -class Blake256: +class blake256: """ Blake256 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None) -> None: """ @@ -46,15 +48,17 @@ class Blake256: # extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h -class Blake2b: +class blake2b: """ Blake2b context. """ + block_size: int + digest_size: int def __init__( self, data: bytes = None, - outlen: int = Blake2b.digest_size, + outlen: int = blake2b.digest_size, personal: bytes = None, ) -> None: """ @@ -73,15 +77,17 @@ class Blake2b: # extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h -class Blake2s: +class blake2s: """ Blake2s context. """ + block_size: int + digest_size: int def __init__( self, data: bytes = None, - outlen: int = Blake2s.digest_size, + outlen: int = blake2s.digest_size, key: bytes = None, personal: bytes = None, ) -> None: @@ -101,7 +107,7 @@ class Blake2s: # extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h -class ChaCha20Poly1305: +class chacha20poly1305: """ ChaCha20Poly1305 context. """ @@ -138,10 +144,12 @@ class ChaCha20Poly1305: # extmod/modtrezorcrypto/modtrezorcrypto-groestl.h -class Groestl512: +class groestl512: """ GROESTL512 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None) -> None: """ @@ -160,10 +168,12 @@ class Groestl512: # extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h -class Pbkdf2: +class pbkdf2: """ PBKDF2 context. """ + HMAC_SHA256: int + HMAC_SHA512: int def __init__( self, @@ -189,7 +199,7 @@ class Pbkdf2: # extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h -class Rfc6979: +class rfc6979: """ RFC6979 context. """ @@ -206,10 +216,12 @@ class Rfc6979: # extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h -class Ripemd160: +class ripemd160: """ RIPEMD160 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None) -> None: """ @@ -228,10 +240,12 @@ class Ripemd160: # extmod/modtrezorcrypto/modtrezorcrypto-sha1.h -class Sha1: +class sha1: """ SHA1 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None) -> None: """ @@ -250,10 +264,12 @@ class Sha1: # extmod/modtrezorcrypto/modtrezorcrypto-sha256.h -class Sha256: +class sha256: """ SHA256 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None) -> None: """ @@ -272,10 +288,12 @@ class Sha256: # extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h -class Sha3_256: +class sha3_256: """ SHA3_256 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None, keccak: bool = False) -> None: """ @@ -292,17 +310,19 @@ class Sha3_256: Returns the digest of hashed data. """ - def copy(self) -> sha3: + def copy(self) -> sha3_256: """ Returns the copy of the digest object with the current state """ # extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h -class Sha3_512: +class sha3_512: """ SHA3_512 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None, keccak: bool = False) -> None: """ @@ -319,17 +339,19 @@ class Sha3_512: Returns the digest of hashed data. """ - def copy(self) -> sha3: + def copy(self) -> sha3_512: """ Returns the copy of the digest object with the current state """ # extmod/modtrezorcrypto/modtrezorcrypto-sha512.h -class Sha512: +class sha512: """ SHA512 context. """ + block_size: int + digest_size: int def __init__(self, data: bytes = None) -> None: """ diff --git a/core/mocks/generated/trezorcrypto/bip32.py b/core/mocks/generated/trezorcrypto/bip32.pyi similarity index 80% rename from core/mocks/generated/trezorcrypto/bip32.py rename to core/mocks/generated/trezorcrypto/bip32.pyi index d125d9a7f..17959bb77 100644 --- a/core/mocks/generated/trezorcrypto/bip32.py +++ b/core/mocks/generated/trezorcrypto/bip32.pyi @@ -108,19 +108,30 @@ class HDNode: Compute an Ethereum pubkeyhash (aka address) from the HD node. """ - def deserialize( - self, value: str, version_public: int, version_private: int - ) -> HDNode: + def __del__(self) -> None: """ - Construct a BIP0032 HD node from a base58-serialized value. + Cleans up sensitive memory. """ - def from_seed(seed: bytes, curve_name: str) -> HDNode: - """ - Construct a BIP0032 HD node from a BIP0039 seed value. - """ - def from_mnemonic_cardano(mnemonic: str, passphrase: str) -> bytes: - """ - Convert mnemonic to hdnode - """ +# extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +def deserialize( + value: str, version_public: int, version_private: int +) -> HDNode: + """ + Construct a BIP0032 HD node from a base58-serialized value. + """ + + +# extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +def from_seed(seed: bytes, curve_name: str) -> HDNode: + """ + Construct a BIP0032 HD node from a BIP0039 seed value. + """ + + +# extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +def from_mnemonic_cardano(mnemonic: str, passphrase: str) -> bytes: + """ + Convert mnemonic to hdnode + """ diff --git a/core/mocks/generated/trezorcrypto/bip39.py b/core/mocks/generated/trezorcrypto/bip39.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/bip39.py rename to core/mocks/generated/trezorcrypto/bip39.pyi diff --git a/core/mocks/generated/trezorcrypto/crc.pyi b/core/mocks/generated/trezorcrypto/crc.pyi new file mode 100644 index 000000000..f7243114c --- /dev/null +++ b/core/mocks/generated/trezorcrypto/crc.pyi @@ -0,0 +1,8 @@ +from typing import * + + +# extmod/modtrezorcrypto/modtrezorcrypto-crc.h +def crc32(data: bytes, crc: int = 0) -> int: + """ + Computes a CRC32 checksum of `data`. + """ diff --git a/core/mocks/generated/trezorcrypto/curve25519.py b/core/mocks/generated/trezorcrypto/curve25519.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/curve25519.py rename to core/mocks/generated/trezorcrypto/curve25519.pyi diff --git a/core/mocks/generated/trezorcrypto/ed25519.py b/core/mocks/generated/trezorcrypto/ed25519.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/ed25519.py rename to core/mocks/generated/trezorcrypto/ed25519.pyi diff --git a/core/mocks/generated/trezorcrypto/monero.py b/core/mocks/generated/trezorcrypto/monero.pyi similarity index 95% rename from core/mocks/generated/trezorcrypto/monero.py rename to core/mocks/generated/trezorcrypto/monero.pyi index 138c55d99..9884eb02f 100644 --- a/core/mocks/generated/trezorcrypto/monero.py +++ b/core/mocks/generated/trezorcrypto/monero.pyi @@ -6,7 +6,7 @@ class Ge25519: """ EC point on ED25519 """ - def __init__(x: Optional[Union[Ge25519, bytes]] = None): + def __init__(self, x: Optional[Union[Ge25519, bytes]] = None): """ Constructor """ @@ -17,7 +17,7 @@ class Sc25519: """ EC scalar on SC25519 """ - def __init__(x: Optional[Union[Sc25519, bytes, int]] = None): + def __init__(self, x: Optional[Union[Sc25519, bytes, int]] = None): """ Constructor """ @@ -28,19 +28,19 @@ class Hasher: """ XMR hasher """ - def __init__(x: Optional[bytes] = None): + def __init__(self, x: Optional[bytes] = None): """ Constructor """ - def update(buffer: bytes): + def update(self, buffer: bytes) -> None: """ Update hasher """ - def digest() -> bytes: + def digest(self) -> bytes: """ Computes digest """ - def copy() -> Hasher: + def copy(self) -> Hasher: """ Creates copy of the hasher, preserving the state """ @@ -56,7 +56,7 @@ def init256_modm( # extmod/modtrezorcrypto/modtrezorcrypto-monero.h -def check256_modm(val: Sc25519): +def check256_modm(val: Sc25519) -> None: """ Throws exception if scalar is invalid """ @@ -172,7 +172,7 @@ def ge25519_set_xmr_h(r: Optional[Ge25519]) -> Ge25519: # extmod/modtrezorcrypto/modtrezorcrypto-monero.h -def ge25519_check(r: Ge25519): +def ge25519_check(r: Ge25519) -> None: """ Checks point, throws if not on curve """ diff --git a/core/mocks/generated/trezorcrypto/nem.py b/core/mocks/generated/trezorcrypto/nem.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/nem.py rename to core/mocks/generated/trezorcrypto/nem.pyi diff --git a/core/mocks/generated/trezorcrypto/nist256p1.py b/core/mocks/generated/trezorcrypto/nist256p1.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/nist256p1.py rename to core/mocks/generated/trezorcrypto/nist256p1.pyi diff --git a/core/mocks/generated/trezorcrypto/random.py b/core/mocks/generated/trezorcrypto/random.pyi similarity index 88% rename from core/mocks/generated/trezorcrypto/random.py rename to core/mocks/generated/trezorcrypto/random.pyi index a6a9d92cc..0eb41717b 100644 --- a/core/mocks/generated/trezorcrypto/random.py +++ b/core/mocks/generated/trezorcrypto/random.pyi @@ -6,10 +6,11 @@ def uniform(n: int) -> int: """ Compute uniform random number from interval 0 ... n - 1. """ +import builtins # extmod/modtrezorcrypto/modtrezorcrypto-random.h -def bytes(len: int) -> bytes: +def bytes(len: int) -> builtins.bytes: """ Generate random bytes sequence of length len. """ diff --git a/core/mocks/generated/trezorcrypto/secp256k1.py b/core/mocks/generated/trezorcrypto/secp256k1.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/secp256k1.py rename to core/mocks/generated/trezorcrypto/secp256k1.pyi diff --git a/core/mocks/generated/trezorcrypto/secp256k1_zkp.py b/core/mocks/generated/trezorcrypto/secp256k1_zkp.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/secp256k1_zkp.py rename to core/mocks/generated/trezorcrypto/secp256k1_zkp.pyi diff --git a/core/mocks/generated/trezorcrypto/shamir.py b/core/mocks/generated/trezorcrypto/shamir.pyi similarity index 88% rename from core/mocks/generated/trezorcrypto/shamir.py rename to core/mocks/generated/trezorcrypto/shamir.pyi index e6dbdc805..6ab016f8d 100644 --- a/core/mocks/generated/trezorcrypto/shamir.py +++ b/core/mocks/generated/trezorcrypto/shamir.pyi @@ -2,7 +2,7 @@ from typing import * # extmod/modtrezorcrypto/modtrezorcrypto-shamir.h -def interpolate(shares, x) -> bytes: +def interpolate(shares: List[Tuple[int, bytes]], x: int) -> bytes: """ Returns f(x) given the Shamir shares (x_1, f(x_1)), ... , (x_k, f(x_k)). :param shares: The Shamir shares. diff --git a/core/mocks/generated/trezorcrypto/slip39.py b/core/mocks/generated/trezorcrypto/slip39.pyi similarity index 100% rename from core/mocks/generated/trezorcrypto/slip39.py rename to core/mocks/generated/trezorcrypto/slip39.pyi diff --git a/core/mocks/generated/trezorio.py b/core/mocks/generated/trezorio.pyi similarity index 93% rename from core/mocks/generated/trezorio.py rename to core/mocks/generated/trezorio.pyi index 8e147d025..ed515b3b3 100644 --- a/core/mocks/generated/trezorio.py +++ b/core/mocks/generated/trezorio.pyi @@ -221,3 +221,10 @@ class WebUSB: """ Sends message using USB WebUSB (device) or UDP (emulator). """ +POLL_READ: int # wait until interface is readable and return read data +POLL_WRITE: int # wait until interface is writable +TOUCH: int # interface id of the touch events +TOUCH_START: int # event id of touch start event +TOUCH_MOVE: int # event id of touch move event +TOUCH_END: int # event id of touch end event +WireInterface = Union[HID, WebUSB] diff --git a/core/mocks/generated/trezorui.py b/core/mocks/generated/trezorui.pyi similarity index 94% rename from core/mocks/generated/trezorui.py rename to core/mocks/generated/trezorui.pyi index f9e082f2f..d1cab0816 100644 --- a/core/mocks/generated/trezorui.py +++ b/core/mocks/generated/trezorui.pyi @@ -6,6 +6,13 @@ class Display: """ Provide access to device display. """ + WIDTH: int # display width in pixels + HEIGHT: int # display height in pixels + FONT_SIZE: int # font height in pixels + FONT_MONO: int # id of monospace font + FONT_NORMAL: int # id of normal-width font + FONT_BOLD: int # id of bold-width font + FONT_MONO_BOLD: int # id of monospace bold-width font def __init__(self) -> None: """ diff --git a/core/mocks/generated/trezorutils.py b/core/mocks/generated/trezorutils.pyi similarity index 84% rename from core/mocks/generated/trezorutils.py rename to core/mocks/generated/trezorutils.pyi index fddc7546e..7ef8521d3 100644 --- a/core/mocks/generated/trezorutils.py +++ b/core/mocks/generated/trezorutils.pyi @@ -13,7 +13,7 @@ def consteq(sec: bytes, pub: bytes) -> bool: # extmod/modtrezorutils/modtrezorutils.c def memcpy( - dst: bytearray, dst_ofs: int, src: bytearray, src_ofs: int, n: int + dst: bytearray, dst_ofs: int, src: bytes, src_ofs: int, n: int ) -> int: """ Copies at most `n` bytes from `src` at offset `src_ofs` to @@ -34,3 +34,9 @@ def set_mode_unprivileged() -> None: """ Set unprivileged mode. """ +GITREV: str +VERSION_MAJOR: int +VERSION_MINOR: int +VERSION_PATCH: int +MODEL: str +EMULATOR: bool diff --git a/core/mocks/micropython.py b/core/mocks/micropython.pyi similarity index 64% rename from core/mocks/micropython.py rename to core/mocks/micropython.pyi index c034d8811..5c10f2110 100644 --- a/core/mocks/micropython.py +++ b/core/mocks/micropython.pyi @@ -1,5 +1,7 @@ -def const(c): +def const(c: int) -> int: return c + +def mem_info() -> None: ... def mem_current() -> int: ... def mem_total() -> int: ... def mem_peak() -> int: ... diff --git a/core/mocks/sys.py b/core/mocks/sys.pyi similarity index 100% rename from core/mocks/sys.py rename to core/mocks/sys.pyi diff --git a/core/mocks/typing.py b/core/mocks/typing.py deleted file mode 100644 index 1e8321ed0..000000000 --- a/core/mocks/typing.py +++ /dev/null @@ -1,69 +0,0 @@ -__names_get = [ - 'AbstractSet', - 'AsyncIterable', - 'AsyncIterator', - 'Awaitable', - 'ByteString', - 'Callable', - 'Container', - 'DefaultDict', - 'Dict', - 'Generator', - 'Generic', - 'ItemsView', - 'Iterable', - 'Iterator', - 'KeysView', - 'List', - 'Mapping', - 'MappingView', - 'MutableMapping', - 'MutableSequence', - 'MutableSet', - 'Optional', - 'Reversible', - 'Sequence', - 'Set', - 'Tuple', - 'Type', - 'Union', - 'ValuesView', -] - -__names_obj = [ - 'Any', - 'AnyStr', - 'Hashable', - 'Sized', - 'SupportsAbs', - 'SupportsFloat', - 'SupportsInt', - 'SupportsRound', - 'Text', -] - - -class __dummy: - - def __getitem__(self, *args): - return object - - -__t = __dummy() - -for __n in __names_get: - globals()[__n] = __t - -for __n in __names_obj: - globals()[__n] = object - - -def TypeVar(*args): - return object - - -def NewType(*args): - return lambda x: x - - -TYPE_CHECKING = False diff --git a/core/mocks/ubinascii.py b/core/mocks/ubinascii.pyi similarity index 100% rename from core/mocks/ubinascii.py rename to core/mocks/ubinascii.pyi diff --git a/core/mocks/uctypes.py b/core/mocks/uctypes.pyi similarity index 100% rename from core/mocks/uctypes.py rename to core/mocks/uctypes.pyi diff --git a/core/mocks/uio.py b/core/mocks/uio.pyi similarity index 100% rename from core/mocks/uio.py rename to core/mocks/uio.pyi diff --git a/core/mocks/uos.pyi b/core/mocks/uos.pyi new file mode 100644 index 000000000..9fb4e3a85 --- /dev/null +++ b/core/mocks/uos.pyi @@ -0,0 +1 @@ +def getenv(env: str) -> str: ... diff --git a/core/mocks/ustruct.py b/core/mocks/ustruct.pyi similarity index 100% rename from core/mocks/ustruct.py rename to core/mocks/ustruct.pyi diff --git a/core/mocks/utime.py b/core/mocks/utime.pyi similarity index 100% rename from core/mocks/utime.py rename to core/mocks/utime.pyi diff --git a/core/mocks/utimeq.py b/core/mocks/utimeq.pyi similarity index 100% rename from core/mocks/utimeq.py rename to core/mocks/utimeq.pyi diff --git a/core/tools/build_mocks b/core/tools/build_mocks index 1135ad82f..50225967a 100755 --- a/core/tools/build_mocks +++ b/core/tools/build_mocks @@ -60,7 +60,7 @@ def store_to_file(dest, parts): for package, line in parts: package = package.replace(".", "/") dirpath = os.path.join(dest, os.path.dirname(package)) - filename = os.path.basename(package) + ".py" + filename = os.path.basename(package) + ".pyi" filepath = os.path.join(dirpath, filename) os.makedirs(dirpath, exist_ok=True) @@ -72,7 +72,6 @@ def store_to_file(dest, parts): print("You should set 'package:' in {}".format(line.strip())) sys.exit(1) - if not os.path.exists(filepath): with open(filepath, "a") as f: f.write("from typing import *\n")