2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 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>.
|
2016-05-20 20:27:20 +00:00
|
|
|
|
2014-02-21 01:33:23 +00:00
|
|
|
import math
|
2018-05-11 12:53:51 +00:00
|
|
|
from .common import TrezorTest
|
2014-02-21 01:33:23 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
import trezorlib.messages as proto
|
2014-02-21 01:33:23 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-02-21 01:33:23 +00:00
|
|
|
def entropy(data):
|
|
|
|
counts = {}
|
|
|
|
for c in data:
|
|
|
|
if c in counts:
|
|
|
|
counts[c] += 1
|
|
|
|
else:
|
|
|
|
counts[c] = 1
|
|
|
|
e = 0
|
2016-06-27 21:17:20 +00:00
|
|
|
for _, v in counts.items():
|
2014-02-21 01:33:23 +00:00
|
|
|
p = 1.0 * v / len(data)
|
|
|
|
e -= p * math.log(p, 256)
|
|
|
|
return e
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2017-12-23 20:20:49 +00:00
|
|
|
class TestMsgGetentropy(TrezorTest):
|
2014-02-21 01:33:23 +00:00
|
|
|
|
|
|
|
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]:
|
2014-02-21 06:28:56 +00:00
|
|
|
with self.client:
|
2017-12-12 15:40:11 +00:00
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(code=proto.ButtonRequestType.ProtectCall), proto.Entropy()])
|
2014-02-21 06:28:56 +00:00
|
|
|
ent = self.client.get_entropy(l)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert len(ent) == l
|
2016-05-05 01:16:17 +00:00
|
|
|
print('entropy = ', entropy(ent))
|