Esempio n. 1
0
def test_randbinstr() -> None:
    for to_be_hashed in (True, False):
        bits = 256
        binstr = randbinstr(bits, to_be_hashed=to_be_hashed)
        assert len(binstr) == bits
        binstr2 = randbinstr(bits, "", to_be_hashed=to_be_hashed)
        assert len(binstr2) == bits
        assert binstr != binstr2
        binstr2 = randbinstr(bits, to_be_hashed=to_be_hashed)
        assert len(binstr2) == bits
        assert binstr != binstr2
        binstr2 = randbinstr(bits, "", to_be_hashed=to_be_hashed)
        assert len(binstr2) == bits
        assert binstr != binstr2

        bits = 512
        binstr = randbinstr(bits, to_be_hashed=to_be_hashed)
        assert len(binstr) == bits
        binstr2 = randbinstr(bits, binstr, to_be_hashed=to_be_hashed)
        assert len(binstr2) == bits
        assert binstr != binstr2

        binstr2 = randbinstr(256, binstr, to_be_hashed=to_be_hashed)
        assert len(binstr2) == 256

    binstr = randbinstr(1024, to_be_hashed=False)
    assert len(binstr) == 1024
    err_msg = "Too many bits required: "
    with pytest.raises(BTClibValueError, match=err_msg):
        randbinstr(1024)
Esempio n. 2
0
def test_randbinstr() -> None:
    for hash in (True, False):
        bits = 256
        binstr = randbinstr(bits, hash=hash)
        assert len(binstr) == bits
        binstr2 = randbinstr(bits, "", hash=hash)
        assert len(binstr2) == bits
        assert binstr != binstr2
        binstr2 = randbinstr(bits, hash=hash)
        assert len(binstr2) == bits
        assert binstr != binstr2
        binstr2 = randbinstr(bits, "", hash=hash)
        assert len(binstr2) == bits
        assert binstr != binstr2

        bits = 512
        binstr = randbinstr(bits, hash=hash)
        assert len(binstr) == bits
        binstr2 = randbinstr(bits, binstr, hash=hash)
        assert len(binstr2) == bits
        assert binstr != binstr2

        binstr2 = randbinstr(256, binstr, hash=hash)
        assert len(binstr2) == 256

    binstr = randbinstr(1024, hash=False)
    assert len(binstr) == 1024
    err_msg = "Too many bits required: "
    with pytest.raises(ValueError, match=err_msg):
        randbinstr(1024)
Esempio n. 3
0
def test_binstr_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)]
    binstr = binstr_from_rolls(bits, dice_base, rolls)
    assert binstr == "1" * 256

    rolls = [base for _ in range(2 * roll_number)]
    binstr = binstr_from_rolls(bits, dice_base, rolls)
    assert binstr == "1" * 256

    rolls = [1 for _ in range(roll_number)]
    binstr = binstr_from_rolls(bits, dice_base, rolls)
    assert binstr == "0" * 256

    rolls = [1 for _ in range(2 * roll_number)]
    binstr = binstr_from_rolls(bits, dice_base, rolls)
    assert binstr == "0" * 256

    rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
    binstr = binstr_from_rolls(bits, dice_base, rolls)
    assert len(binstr) == 256

    rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
    binstr2 = binstr_from_rolls(bits, dice_base, rolls)
    assert len(binstr2) == 256
    assert binstr != binstr2

    binstr = binstr_from_rolls(bits - 1, dice_base, rolls)
    assert len(binstr) == bits - 1

    rolls = [base for _ in range(roll_number + 1)]
    binstr = binstr_from_rolls(bits + 1, dice_base, rolls)
    assert len(binstr) == bits + 1

    rolls = [base for _ in range(roll_number + 1)]
    binstr_rolls = binstr_from_rolls(bits, dice_base, rolls)
    binstr = randbinstr(bits, binstr_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):
        binstr_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):
        binstr_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):
        binstr_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):
        binstr_from_rolls(bits, 1, rolls)
Esempio n. 4
0
    def test_randbinstr(self):
        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)]
        binstr = randbinstr(bits, dice_base, rolls, False, False, False)
        self.assertEqual(binstr, "1" * 256)

        rolls = [base for _ in range(2 * roll_number)]
        binstr = randbinstr(bits, dice_base, rolls, False, False, False)
        self.assertEqual(binstr, "1" * 256)

        rolls = [1 for _ in range(roll_number)]
        binstr = randbinstr(bits, dice_base, rolls, False, False, False)
        self.assertEqual(binstr, "0" * 256)

        rolls = [1 for _ in range(2 * roll_number)]
        binstr = randbinstr(bits, dice_base, rolls, False, False, False)
        self.assertEqual(binstr, "0" * 256)

        rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
        binstr = randbinstr(bits, dice_base, rolls)
        self.assertEqual(len(binstr), 256)
        rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
        binstr2 = randbinstr(bits, dice_base, rolls)
        self.assertEqual(len(binstr2), 256)
        self.assertNotEqual(binstr, binstr2)

        binstr = randbinstr(bits)
        self.assertEqual(len(binstr), 256)
        binstr2 = randbinstr(bits)
        self.assertEqual(len(binstr2), 256)
        self.assertNotEqual(binstr, binstr2)

        # goes through bit lenght reduction before hashing
        rolls = [base for _ in range(roll_number + 1)]
        binstr = randbinstr(bits, dice_base, rolls)

        # Number of bits (255) must be in (128, 160, 192, 224, 256)
        self.assertRaises(ValueError, randbinstr, bits - 1, dice_base, rolls)
        # randbinstr(bits-1, dice_base, rolls)

        # too few usable [1-16] rolls, missing 2
        rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number - 2)]
        self.assertRaises(ValueError, randbinstr, bits, dice_base, rolls)
        # randbinstr(bits, dice_base, rolls)

        # too few usable [1-16] rolls, missing 1
        rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
        rolls[1] = base + 1
        self.assertRaises(ValueError, randbinstr, bits, dice_base, rolls)
        # randbinstr(bits, dice_base, rolls)

        # invalid (21) roll, not in [1-20]
        rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
        rolls[1] = dice_base + 1
        self.assertRaises(ValueError, randbinstr, bits, dice_base, rolls)
        # randbinstr(bits, dice_base, rolls)

        # Invalid dice base (1): must be >= 2
        rolls = [secrets.randbelow(base) + 1 for _ in range(roll_number)]
        self.assertRaises(ValueError, randbinstr, bits, 1, rolls)