2016-05-20 20:27:20 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2014-02-21 01:33:23 +00:00
|
|
|
import unittest
|
|
|
|
import common
|
|
|
|
import math
|
|
|
|
|
|
|
|
import trezorlib.messages_pb2 as proto
|
|
|
|
import trezorlib.types_pb2 as proto_types
|
|
|
|
|
|
|
|
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.iteritems():
|
|
|
|
p = 1.0 * v / len(data)
|
|
|
|
e -= p * math.log(p, 256)
|
|
|
|
return e
|
|
|
|
|
2014-02-21 06:28:56 +00:00
|
|
|
class TestMsgGetentropy(common.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:
|
2014-04-28 12:39:05 +00:00
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ProtectCall), proto.Entropy()])
|
2014-02-21 06:28:56 +00:00
|
|
|
ent = self.client.get_entropy(l)
|
|
|
|
self.assertTrue(len(ent) >= l)
|
2016-05-05 01:16:17 +00:00
|
|
|
print('entropy = ', entropy(ent))
|
2014-02-21 01:33:23 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|