-
Notifications
You must be signed in to change notification settings - Fork 42
/
test_entropy.py
294 lines (230 loc) · 10.4 KB
/
test_entropy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
# Copyright (C) The btclib developers
#
# This file is part of btclib. It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution.
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.
"""Tests for the `btclib.entropy` module."""
import math
import secrets
from io import StringIO
import pytest
from btclib.exceptions import BTClibValueError
from btclib.mnemonic import (
bin_str_entropy_from_bytes,
bin_str_entropy_from_entropy,
bin_str_entropy_from_int,
bin_str_entropy_from_random,
bin_str_entropy_from_rolls,
bin_str_entropy_from_str,
bin_str_entropy_from_wordlist_indexes,
bytes_entropy_from_str,
collect_rolls,
wordlist_indexes_from_bin_str_entropy,
)
from btclib.mnemonic.entropy import _bits
def test_indexes() -> None:
for entropy in ("0", "00000000000"):
indexes = wordlist_indexes_from_bin_str_entropy(entropy, 2048)
assert indexes == [0]
entropy = "000000000000"
indexes = wordlist_indexes_from_bin_str_entropy(entropy, 2048)
assert indexes == [0, 0]
test_vector = [
[1268, 535, 810, 685, 433, 811, 1385, 1790, 421, 570, 567, 1313],
[0, 0, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 0],
[0, 0, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 0],
]
for indx in test_vector:
entropy = bin_str_entropy_from_wordlist_indexes(indx, 2048)
indexes = wordlist_indexes_from_bin_str_entropy(entropy, 2048)
assert indexes == indx
def test_conversions() -> None:
test_vectors = [
"10101011" * 32,
"00101011" * 32,
"00000000" + "10101011" * 31,
]
for raw in test_vectors:
assert bin_str_entropy_from_str(raw) == raw
i = int(raw, 2)
assert bin_str_entropy_from_int(i) == raw
assert bin_str_entropy_from_int(bin(i).upper()) == raw
assert bin_str_entropy_from_int(hex(i).upper()) == raw
b = i.to_bytes(32, byteorder="big", signed=False)
assert bin_str_entropy_from_bytes(b) == raw
assert bin_str_entropy_from_bytes(b.hex()) == raw
assert bin_str_entropy_from_entropy(raw) == raw
assert bin_str_entropy_from_entropy(i) == raw
assert bin_str_entropy_from_entropy(b) == raw
max_bits = max(_bits)
raw = "10" + "11111111" * (max_bits // 8)
assert bin_str_entropy_from_entropy(raw) == bin_str_entropy_from_entropy(raw[:-2])
# entr integer has its leftmost bit set to 0
i = 1 << max_bits - 1
bin_str_entropy = bin_str_entropy_from_entropy(i)
assert len(bin_str_entropy) == max_bits
# entr integer has its leftmost bit set to 1
i = 1 << max_bits
bin_str_entropy = bin_str_entropy_from_entropy(i)
assert len(bin_str_entropy) == max_bits
exp_i = i >> 1
i = int(bin_str_entropy, 2)
assert i == exp_i
i = secrets.randbits(255)
raw = bin_str_entropy_from_int(i)
assert int(raw, 2) == i
assert len(raw) == 256
assert bin_str_entropy_from_str(raw) == raw
assert bin_str_entropy_from_int(hex(i).upper()) == raw
b = i.to_bytes(32, byteorder="big", signed=False)
assert bin_str_entropy_from_bytes(b) == raw
raw2 = bin_str_entropy_from_int(i, 255)
assert int(raw2, 2) == i
assert len(raw2) == 255
assert bin_str_entropy_from_str(f"0{raw2}") == raw
raw2 = bin_str_entropy_from_str(raw, 128)
assert len(raw2) == 128
assert raw2 == raw[:128]
def test_exceptions() -> None:
bin_str_entropy216 = "00011010" * 27 # 216 bits
bin_str_entropy214 = bin_str_entropy216[:-2] # 214 bits
entropy = bin_str_entropy_from_entropy(bin_str_entropy214, 214)
assert entropy == bin_str_entropy214
# 214 is not in [128, 160, 192, 224, 256, 512]
err_msg = "invalid number of bits: "
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_entropy(bin_str_entropy214)
# 214 is not in [216]
err_msg = "invalid number of bits: "
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_entropy(bin_str_entropy214, 216)
int_entropy211 = int(bin_str_entropy214, 2) # 211 bits
assert int_entropy211.bit_length() == 211
entropy = bin_str_entropy_from_entropy(int_entropy211, 214)
assert entropy == bin_str_entropy214
entropy = bin_str_entropy_from_entropy(int_entropy211, 256)
assert len(entropy) == 256
assert int(entropy, 2) == int_entropy211
entropy = bin_str_entropy_from_entropy(int_entropy211)
assert len(entropy) == 224
assert int(entropy, 2) == int_entropy211
err_msg = "negative entropy: "
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_entropy(-1 * int_entropy211)
bytes_entropy216 = int_entropy211.to_bytes(27, byteorder="big", signed=False)
entropy = bin_str_entropy_from_entropy(bytes_entropy216, 214)
assert entropy == bin_str_entropy214
entropy = bin_str_entropy_from_entropy(bytes_entropy216, 216)
assert entropy != bin_str_entropy216
err_msg = "invalid number of bits: "
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_entropy(bytes_entropy216, 224)
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_entropy(tuple()) # type: ignore[arg-type]
with pytest.raises(ValueError):
bin_str_entropy_from_int("not an int")
with pytest.raises(TypeError):
bin_str_entropy_from_str(3) # type: ignore[arg-type]
err_msg = "invalid number of bits: "
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy = "01" * 65 # 130 bits
bytes_entropy_from_str(bin_str_entropy)
# 2 input failures, then automatic rolls with default D6
inputs = [StringIO("3\npluto\na\n")]
# D120, then 43 automatic rolls
inputs.append(StringIO("a120\n"))
# D120, one input failure, then 43 (implausible but valid) non-automatic rolls
inputs.append(StringIO("120\npluto\n" + "64\n" * 43))
def test_collect_rolls(monkeypatch: pytest.MonkeyPatch) -> None:
bits = 256
for i, sides in enumerate((6, 120, 120)):
monkeypatch.setattr("sys.stdin", inputs[i])
dice_sides, dice_rolls = collect_rolls(bits)
assert dice_sides == sides
bits_per_roll = math.floor(math.log2(sides))
base = 2**bits_per_roll
for roll in dice_rolls:
assert 0 < roll <= base
min_roll_number = math.ceil(bits / bits_per_roll)
assert len(dice_rolls) == min_roll_number
def test_bin_str_entropy_from_rolls() -> None:
bits = 256
dice_base = 20
bits_per_roll = math.floor(math.log2(dice_base))
base = 2**bits_per_roll
roll_number = math.ceil(bits / bits_per_roll)
rolls = [base for _ in range(roll_number)]
bin_str = bin_str_entropy_from_rolls(bits, dice_base, rolls)
assert bin_str == "1" * 256
rolls = [base for _ in range(2 * roll_number)]
bin_str = bin_str_entropy_from_rolls(bits, dice_base, rolls)
assert bin_str == "1" * 256
rolls = [1 for _ in range(roll_number)]
bin_str = bin_str_entropy_from_rolls(bits, dice_base, rolls)
assert bin_str == "0" * 256
rolls = [1 for _ in range(2 * roll_number)]
bin_str = bin_str_entropy_from_rolls(bits, dice_base, rolls)
assert bin_str == "0" * 256
rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
bin_str = bin_str_entropy_from_rolls(bits, dice_base, rolls)
assert len(bin_str) == 256
rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
bin_str2 = bin_str_entropy_from_rolls(bits, dice_base, rolls)
assert len(bin_str2) == 256
assert bin_str != bin_str2
bin_str = bin_str_entropy_from_rolls(bits - 1, dice_base, rolls)
assert len(bin_str) == bits - 1
rolls = [base for _ in range(roll_number + 1)]
bin_str = bin_str_entropy_from_rolls(bits + 1, dice_base, rolls)
assert len(bin_str) == bits + 1
rolls = [base for _ in range(roll_number + 1)]
bin_str_rolls = bin_str_entropy_from_rolls(bits, dice_base, rolls)
bin_str = bin_str_entropy_from_random(bits, bin_str_rolls)
rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number - 2)]
err_msg = "too few rolls in the usable " # [1-16] range, missing 2 rolls
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_rolls(bits, dice_base, rolls)
rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
rolls[1] = base + 1
err_msg = "too few rolls in the usable " # [1-16] range, missing 1 rolls
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_rolls(bits, dice_base, rolls)
rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
rolls[1] = dice_base + 1
err_msg = "invalid roll: " # 21 is not in [1-20]
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_rolls(bits, dice_base, rolls)
rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
err_msg = "invalid dice base: "
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_rolls(bits, 1, rolls)
def test_bin_str_entropy_from_random() -> None:
for to_be_hashed in (True, False):
bits = 256
bin_str = bin_str_entropy_from_random(bits, to_be_hashed=to_be_hashed)
assert len(bin_str) == bits
bin_str2 = bin_str_entropy_from_random(bits, "", to_be_hashed=to_be_hashed)
assert len(bin_str2) == bits
assert bin_str != bin_str2
bin_str2 = bin_str_entropy_from_random(bits, to_be_hashed=to_be_hashed)
assert len(bin_str2) == bits
assert bin_str != bin_str2
bin_str2 = bin_str_entropy_from_random(bits, "", to_be_hashed=to_be_hashed)
assert len(bin_str2) == bits
assert bin_str != bin_str2
bits = 512
bin_str = bin_str_entropy_from_random(bits, to_be_hashed=to_be_hashed)
assert len(bin_str) == bits
bin_str2 = bin_str_entropy_from_random(bits, bin_str, to_be_hashed=to_be_hashed)
assert len(bin_str2) == bits
assert bin_str != bin_str2
bin_str2 = bin_str_entropy_from_random(256, bin_str, to_be_hashed=to_be_hashed)
assert len(bin_str2) == 256
bin_str = bin_str_entropy_from_random(1024, to_be_hashed=False)
assert len(bin_str) == 1024
err_msg = "too many bits required: "
with pytest.raises(BTClibValueError, match=err_msg):
bin_str_entropy_from_random(1024)