2024-11-15 12:55:14 +00:00
|
|
|
# flake8: noqa: F403,F405
|
2024-01-26 11:16:55 +00:00
|
|
|
from common import * # isort:skip
|
2016-06-02 12:32:12 +00:00
|
|
|
|
|
|
|
from trezor.crypto import random
|
|
|
|
|
2017-12-16 01:11:26 +00:00
|
|
|
|
2016-06-02 12:32:12 +00:00
|
|
|
class TestCryptoRandom(unittest.TestCase):
|
|
|
|
def test_uniform(self):
|
|
|
|
c = {}
|
|
|
|
for i in range(15):
|
|
|
|
c[i] = 0
|
|
|
|
for _ in range(15000):
|
|
|
|
r = random.uniform(15)
|
|
|
|
c[r] += 1
|
|
|
|
for i in range(15):
|
2018-07-10 18:50:04 +00:00
|
|
|
self.assertAlmostEqual(c[r], 1000, delta=200)
|
2016-06-02 12:32:12 +00:00
|
|
|
|
|
|
|
def test_bytes_length(self):
|
|
|
|
for l in range(1024 + 1):
|
|
|
|
lst = random.bytes(l)
|
|
|
|
self.assertEqual(len(lst), l)
|
|
|
|
|
2016-10-07 11:57:21 +00:00
|
|
|
def test_bytes_big_length(self):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
random.bytes(10000)
|
|
|
|
|
2016-06-02 12:32:12 +00:00
|
|
|
def test_bytes_uniform(self):
|
|
|
|
for _ in range(100):
|
|
|
|
c = {}
|
2023-06-28 10:46:29 +00:00
|
|
|
for h in "0123456789abcdef":
|
2016-06-02 12:32:12 +00:00
|
|
|
c[h] = 0
|
2018-01-03 20:53:58 +00:00
|
|
|
for _ in range(8):
|
|
|
|
b = random.bytes(1000)
|
|
|
|
for h in hexlify(b):
|
|
|
|
c[chr(h)] += 1
|
2023-06-28 10:46:29 +00:00
|
|
|
for h in "0123456789abcdef":
|
2018-07-10 18:50:04 +00:00
|
|
|
self.assertAlmostEqual(c[h], 1000, delta=200)
|
2016-06-02 12:32:12 +00:00
|
|
|
|
|
|
|
def test_shuffle(self):
|
|
|
|
for l in range(256 + 1):
|
|
|
|
lst = list(range(l))
|
|
|
|
random.shuffle(lst)
|
|
|
|
self.assertEqual(len(lst), l)
|
|
|
|
self.assertEqual(sorted(lst), list(range(l)))
|
|
|
|
|
2017-12-16 01:11:26 +00:00
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
if __name__ == "__main__":
|
2016-06-02 12:32:12 +00:00
|
|
|
unittest.main()
|