1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +00:00
trezor-firmware/tests/device_tests/test_msg_getentropy.py

52 lines
1.7 KiB
Python
Raw Normal View History

2017-01-03 18:40:05 +00:00
# This file is part of the TREZOR project.
#
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
2016-05-20 20:27:20 +00:00
from __future__ import print_function
import unittest
import math
2017-06-23 19:31:42 +00:00
import common
import trezorlib.messages_pb2 as proto
import trezorlib.types_pb2 as proto_types
2017-06-23 19:31:42 +00:00
def entropy(data):
counts = {}
for c in data:
if c in counts:
counts[c] += 1
else:
counts[c] = 1
e = 0
for _, v in counts.items():
p = 1.0 * v / len(data)
e -= p * math.log(p, 256)
return e
2017-06-23 19:31:42 +00:00
class TestMsgGetentropy(common.TrezorTest):
def test_entropy(self):
for l in [0, 1, 2, 3, 4, 5, 8, 9, 16, 17, 32, 33, 64, 65, 128, 129, 256, 257, 512, 513, 1024]:
with self.client:
self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ProtectCall), proto.Entropy()])
ent = self.client.get_entropy(l)
2017-05-08 20:10:47 +00:00
self.assertTrue(len(ent) == l)
2016-05-05 01:16:17 +00:00
print('entropy = ', entropy(ent))