1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-10-19 14:30:31 +00:00
trezor-firmware/tests/device_tests/misc/test_msg_getentropy.py
matejcik 8c3d3c6548 chore: bump black to 22.3
changes:
* exponent operator ** now allows no spaces around itself: https://github.com/psf/black/issues/538
* optional unicode marker u"" is no longer allowed

[no changelog]
2022-06-29 14:35:16 +02:00

50 lines
1.6 KiB
Python

# This file is part of the Trezor project.
#
# Copyright (C) 2012-2019 SatoshiLabs and contributors
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# 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.
#
# 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>.
import math
import pytest
from trezorlib import messages as m, misc
from trezorlib.debuglink import TrezorClientDebugLink as Client
ENTROPY_LENGTHS_POW2 = [2**l for l in range(10)]
ENTROPY_LENGTHS_POW2_1 = [2**l + 1 for l in range(10)]
ENTROPY_LENGTHS = ENTROPY_LENGTHS_POW2 + ENTROPY_LENGTHS_POW2_1
def entropy(data):
counts = {}
for c in data:
counts[c] = counts.get(c, 0) + 1
e = 0
for v in counts.values():
p = v / len(data)
e -= p * math.log(p, 256)
return e
@pytest.mark.parametrize("entropy_length", ENTROPY_LENGTHS)
def test_entropy(client: Client, entropy_length):
with client:
client.set_expected_responses(
[m.ButtonRequest(code=m.ButtonRequestType.ProtectCall), m.Entropy]
)
ent = misc.get_entropy(client, entropy_length)
assert len(ent) == entropy_length
print(f"{entropy_length} bytes: entropy = {entropy(ent)}")