refactor(core): move `compact_size()` from `common.py` to `tools.py`
What changed, and why it matters
This commit simply moves a helper function called compact_size from one internal file to another and updates the import statements in files that use it. There is no change to what the function does, no bug fix, and no security impact.
No action needed; this is a non-security code relocation.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit is a pure refactor: the compact_size() implementation is relocated from python/src/trezorlib/testing/common.py to python/src/trezorlib/tools.py, and all callers are updated to import from the new location. The function body, behavior, and tests remain identical.
Changed components
python/src/trezorlib/testing/common.pypython/src/trezorlib/tools.pytests/device_tests/evolu/common.pytests/device_tests/evolu/test_sign_registration.pytests/device_tests/payment_req.pytests/device_tests/test_authenticate_device.pyInspect captured patch +29 / −29
diff --git a/python/src/trezorlib/testing/common.py b/python/src/trezorlib/testing/common.py
index 5c883e32..f3601f8b 100644
--- a/python/src/trezorlib/testing/common.py
+++ b/python/src/trezorlib/testing/common.py
@@ -13,31 +13,6 @@ BRGeneratorType = Generator[None, messages.ButtonRequest, None]
PRIVATE_KEYS_DEV = [byte * 32 for byte in (b"\xdd", b"\xde", b"\xdf")]
-def compact_size(n: int) -> bytes:
- """
- Encode an integer using Bitcoin's compact size format.
-
- Args:
- n (int): The integer to encode.
-
- Returns:
- bytes: The encoded integer.
-
- Raises:
- ValueError: If n is not in the range 0..2^64-1.
- """
- if n < 0 or n > 0xFFFF_FFFF_FFFF_FFFF:
- raise ValueError("compact_size supports integers in range 0..2^64-1")
- if n < 253:
- return n.to_bytes(1, "little")
- elif n < 0x1_0000:
- return bytes([253]) + n.to_bytes(2, "little")
- elif n < 0x1_0000_0000:
- return bytes([254]) + n.to_bytes(4, "little")
- else:
- return bytes([255]) + n.to_bytes(8, "little")
-
-
def get_text_possible_pagination(debug: "DebugLink", br: messages.ButtonRequest) -> str:
"""
Read all text content from the device, handling possible pagination.
diff --git a/python/src/trezorlib/tools.py b/python/src/trezorlib/tools.py
index b882c5af..66e4c5d2 100644
--- a/python/src/trezorlib/tools.py
+++ b/python/src/trezorlib/tools.py
@@ -44,6 +44,31 @@ HARDENED_FLAG = 1 << 31
Address = t.NewType("Address", list[int])
+def compact_size(n: int) -> bytes:
+ """
+ Encode an integer using Bitcoin's compact size format.
+
+ Args:
+ n (int): The integer to encode.
+
+ Returns:
+ bytes: The encoded integer.
+
+ Raises:
+ ValueError: If n is not in the range 0..2^64-1.
+ """
+ if n < 0 or n > 0xFFFF_FFFF_FFFF_FFFF:
+ raise ValueError("compact_size supports integers in range 0..2^64-1")
+ if n < 253:
+ return n.to_bytes(1, "little")
+ elif n < 0x1_0000:
+ return bytes([253]) + n.to_bytes(2, "little")
+ elif n < 0x1_0000_0000:
+ return bytes([254]) + n.to_bytes(4, "little")
+ else:
+ return bytes([255]) + n.to_bytes(8, "little")
+
+
def H_(x: int) -> int:
"""
Shortcut function that "hardens" a number in a BIP44 path.
diff --git a/tests/device_tests/evolu/common.py b/tests/device_tests/evolu/common.py
index 141c854d..01ad4c64 100644
--- a/tests/device_tests/evolu/common.py
+++ b/tests/device_tests/evolu/common.py
@@ -8,8 +8,8 @@ from trezorlib import evolu
from trezorlib.debuglink import DebugSession as Session
from trezorlib.debuglink import TrezorTestContext as Client
from trezorlib.messages import EvoluDelegatedIdentityKey, ThpCredentialResponse
-from trezorlib.testing.common import compact_size
from trezorlib.thp import curve25519
+from trezorlib.tools import compact_size
TEST_randomness = os.urandom(32)
TEST_host_static_private_key = curve25519.get_private_key(TEST_randomness)
diff --git a/tests/device_tests/evolu/test_sign_registration.py b/tests/device_tests/evolu/test_sign_registration.py
index 1ca78134..f8ee51cd 100644
--- a/tests/device_tests/evolu/test_sign_registration.py
+++ b/tests/device_tests/evolu/test_sign_registration.py
@@ -4,7 +4,7 @@ from ecdsa import NIST256p, SigningKey, VerifyingKey
from trezorlib import evolu
from trezorlib.debuglink import TrezorTestContext as Client
from trezorlib.exceptions import TrezorFailure
-from trezorlib.testing.common import compact_size
+from trezorlib.tools import compact_size
from ..certificate import check_signature_optiga
from .common import get_delegated_identity_key, get_invalid_proof, get_proof
diff --git a/tests/device_tests/payment_req.py b/tests/device_tests/payment_req.py
index 6b33735a..8a3215fb 100644
--- a/tests/device_tests/payment_req.py
+++ b/tests/device_tests/payment_req.py
@@ -5,7 +5,7 @@ from ecdsa import NIST256p, SigningKey
from trezorlib import messages
from trezorlib.client import Session
-from trezorlib.testing.common import compact_size
+from trezorlib.tools import compact_size
SLIP44_ID_UNDEFINED = 0xFFFF_FFFF
diff --git a/tests/device_tests/test_authenticate_device.py b/tests/device_tests/test_authenticate_device.py
index a9c58b91..6815f284 100644
--- a/tests/device_tests/test_authenticate_device.py
+++ b/tests/device_tests/test_authenticate_device.py
@@ -3,7 +3,7 @@ from cryptography import x509
from trezorlib import device, exceptions, messages
from trezorlib.debuglink import DebugSession as Session
-from trezorlib.testing.common import compact_size
+from trezorlib.tools import compact_size
from .certificate import (
check_signature_mcu,
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.