def test_dec2bin_valid(self): """Compute binary string using various valid arguments""" self.assertEqual(bip39.dec2bin(0), "0") self.assertEqual(bip39.dec2bin(1), "1") self.assertEqual(bip39.dec2bin(2), "10") self.assertEqual(bip39.dec2bin(00), "0") self.assertEqual(bip39.dec2bin(0, zero_padding=7), "0000000") self.assertEqual(bip39.dec2bin(1, zero_padding=7), "0000001") self.assertEqual(bip39.dec2bin(0b111, zero_padding=3), "111")
def test_dec2bin_invalid(self): """Test function with invalid arugments""" with self.assertRaises(bip39.InvalidIntValueError): bip39.dec2bin("a") with self.assertRaises(bip39.InvalidIntValueError): bip39.dec2bin("") with self.assertRaises(bip39.InvalidIntValueError): bip39.dec2bin(-1)
def test_num_entropy_warnings(self): """"Provide weak entropy to generate warnings""" self.assertEqual(app.num_entropy_warnings("0", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("1", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("11", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("111", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("1111", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("11111", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("111111", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("1111111", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings("11111111", print_warning=False), 1) self.assertEqual(app.num_entropy_warnings(bip39.dec2bin(sys.maxint), print_warning=False), 1)
def test_num_entropy_warnings_none(self): """Provide entropy that should not generate warnings""" self.assertEqual(app.num_entropy_warnings("10", print_warning=False), 0) self.assertEqual(app.num_entropy_warnings(bip39.dec2bin(sys.maxint - 1), print_warning=False), 0)
def die_rolls_to_bitstring(dice_vals, bitstring_len): """Convert dice rolls to a bit string of min length. Args: dice_vals (List[int]): List of dice values in range 1 to 6 bitstring_len (int): Number of bits that should be in bit string returned. Must be a multiple of 2. (TODO: or 4?) Consistent with Ian Coleman tool during entropy "filtering" https://github.com/iancoleman/bip39/blob/434caecd96740bbec488429026830b5ad24f628a/src/js/entropy.js#L73-L74 but not with zero padding https://github.com/iancoleman/bip39/blob/434caecd96740bbec488429026830b5ad24f628a/src/js/entropy.js#L113-L122 Raises: TypeError if args are wrong type ValueError if args are invalid int values InsufficientEntropyError if not enough die rolls provided """ if not isinstance(dice_vals, list): raise TypeError _assert_positive_int(bitstring_len) if bitstring_len % 2 != 0: raise ValueError("bitstring_len must be a multiple of 2") absolute_min_rolls = die_rolls_per_bits(bitstring_len) if len(dice_vals) < absolute_min_rolls: raise InsufficientEntropyError() total = 0 roll_num = 0 accepted_rolls = 0 for roll_val in dice_vals: _assert_int(roll_val) if roll_val < 1 or roll_val > 6: raise ValueError #filter value of '6' a la Ian Coleman tool filtered_roll_val = 0 if roll_val == 6 else roll_val if roll_val in IGNORED_BIASED_VALUES: continue accepted_rolls += 1 #cacluate new value of dice rolls so far in base 4 total += ((4**roll_num) * filtered_roll_val) roll_num += 1 if accepted_rolls < absolute_min_rolls: raise InsufficientEntropyError() bitstring = bip39.dec2bin(dec=total, zero_padding=bitstring_len) if len(bitstring) < bitstring_len: raise ValueError("This condition should not be reachable") return bitstring[-bitstring_len:] #take first n bits generated