2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
2014-02-04 15:12:52 +00:00
|
|
|
import time
|
2018-05-11 12:53:51 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
import pytest
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2022-02-03 12:16:26 +00:00
|
|
|
from trezorlib import btc, device
|
2024-12-02 14:49:04 +00:00
|
|
|
from trezorlib.debuglink import SessionDebugWrapper as Session
|
2022-02-03 12:16:26 +00:00
|
|
|
from trezorlib.messages import SafetyCheckLevel
|
2018-08-13 16:21:24 +00:00
|
|
|
from trezorlib.tools import H_
|
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
pytestmark = [
|
2024-09-02 11:01:16 +00:00
|
|
|
pytest.mark.models("legacy"),
|
2021-11-10 21:19:53 +00:00
|
|
|
pytest.mark.flaky(max_runs=5),
|
|
|
|
]
|
2014-02-04 15:12:52 +00:00
|
|
|
|
2014-02-11 20:03:17 +00:00
|
|
|
|
2024-12-02 14:49:04 +00:00
|
|
|
def test_public_ckd(session: Session):
|
2022-02-03 12:16:26 +00:00
|
|
|
# disable safety checks to access non-standard paths
|
2024-12-02 14:49:04 +00:00
|
|
|
device.apply_settings(session, safety_checks=SafetyCheckLevel.PromptTemporarily)
|
|
|
|
btc.get_address(session, "Bitcoin", []) # to compute root node via BIP39
|
2014-02-04 15:12:52 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
for depth in range(8):
|
|
|
|
start = time.time()
|
2024-12-02 14:49:04 +00:00
|
|
|
btc.get_address(session, "Bitcoin", range(depth))
|
2021-11-10 21:19:53 +00:00
|
|
|
delay = time.time() - start
|
|
|
|
expected = (depth + 1) * 0.26
|
|
|
|
print("DEPTH", depth, "EXPECTED DELAY", expected, "REAL DELAY", delay)
|
|
|
|
assert delay <= expected
|
2014-02-11 20:03:17 +00:00
|
|
|
|
2014-02-04 15:12:52 +00:00
|
|
|
|
2024-12-02 14:49:04 +00:00
|
|
|
def test_private_ckd(session: Session):
|
2022-02-03 12:16:26 +00:00
|
|
|
# disable safety checks to access non-standard paths
|
2024-12-02 14:49:04 +00:00
|
|
|
device.apply_settings(session, safety_checks=SafetyCheckLevel.PromptTemporarily)
|
|
|
|
btc.get_address(session, "Bitcoin", []) # to compute root node via BIP39
|
2015-01-26 12:08:29 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
for depth in range(8):
|
2015-01-26 12:08:29 +00:00
|
|
|
start = time.time()
|
2021-11-10 21:19:53 +00:00
|
|
|
address_n = [H_(-i) for i in range(-depth, 0)]
|
2024-12-02 14:49:04 +00:00
|
|
|
btc.get_address(session, "Bitcoin", address_n)
|
2021-11-10 21:19:53 +00:00
|
|
|
delay = time.time() - start
|
|
|
|
expected = (depth + 1) * 0.26
|
|
|
|
print("DEPTH", depth, "EXPECTED DELAY", expected, "REAL DELAY", delay)
|
|
|
|
assert delay <= expected
|
|
|
|
|
|
|
|
|
2024-12-02 14:49:04 +00:00
|
|
|
def test_cache(session: Session):
|
2022-02-03 12:16:26 +00:00
|
|
|
# disable safety checks to access non-standard paths
|
2024-12-02 14:49:04 +00:00
|
|
|
device.apply_settings(session, safety_checks=SafetyCheckLevel.PromptTemporarily)
|
2022-02-03 12:16:26 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
start = time.time()
|
|
|
|
for x in range(10):
|
2024-12-02 14:49:04 +00:00
|
|
|
btc.get_address(session, "Bitcoin", [x, 2, 3, 4, 5, 6, 7, 8])
|
2021-11-10 21:19:53 +00:00
|
|
|
nocache_time = time.time() - start
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
for x in range(10):
|
2024-12-02 14:49:04 +00:00
|
|
|
btc.get_address(session, "Bitcoin", [1, 2, 3, 4, 5, 6, 7, x])
|
2021-11-10 21:19:53 +00:00
|
|
|
cache_time = time.time() - start
|
2015-01-26 12:08:29 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
print("NOCACHE TIME", nocache_time)
|
|
|
|
print("CACHED TIME", cache_time)
|
2015-01-26 12:08:29 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
# Cached time expected to be at least 2x faster
|
|
|
|
assert cache_time <= nocache_time / 2.0
|