Exemple #1
0
def test_create_shuffle_from_identifier():
    Key.set_random_seed(1111)
    Shuffle.set_random_seed(1112)

    # Empty keep-same shuffle
    key = Key()
    original_shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_KEEP_SAME)
    recreated_shuffle = Shuffle.create_shuffle_from_identifier(original_shuffle.get_identifier())
    assert original_shuffle.__repr__() == recreated_shuffle.__repr__()

    # Empty random shuffle
    key = Key()
    original_shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    recreated_shuffle = Shuffle.create_shuffle_from_identifier(original_shuffle.get_identifier())
    assert original_shuffle.__repr__() == recreated_shuffle.__repr__()

    # Non-empty keep-same shuffle
    key = Key.create_random_key(32)
    original_shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_KEEP_SAME)
    recreated_shuffle = Shuffle.create_shuffle_from_identifier(original_shuffle.get_identifier())
    assert original_shuffle.__repr__() == recreated_shuffle.__repr__()

    # Non-empty random shuffle
    key = Key.create_random_key(32)
    original_shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    recreated_shuffle = Shuffle.create_shuffle_from_identifier(original_shuffle.get_identifier())
    assert original_shuffle.__repr__() == recreated_shuffle.__repr__()
Exemple #2
0
def test_repr():
    Key.set_random_seed(2221)
    Shuffle.set_random_seed(2222)
    key = Key.create_random_key(8)
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_KEEP_SAME)
    assert shuffle.__repr__() == "Shuffle: 0->0 1->1 2->2 3->3 4->4 5->5 6->6 7->7"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == "Shuffle: 0->0 1->1 2->6 3->4 4->3 5->2 6->5 7->7"
Exemple #3
0
def test_get_key_index():
    Shuffle.set_random_seed(9992)
    shuffle = Shuffle(6, Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == "Shuffle: 0->5 1->4 2->2 3->0 4->1 5->3"
    assert shuffle.get_key_index(0) == 5
    assert shuffle.get_key_index(1) == 4
    assert shuffle.get_key_index(5) == 3
Exemple #4
0
def test_flip_bit():
    Key.set_random_seed(7771)
    Shuffle.set_random_seed(7772)
    key = Key.create_random_key(6)
    assert key.__repr__() == "Key: 010011"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == "Shuffle: 0->5 1->2 2->4 3->3 4->1 5->0"
    shuffle.flip_bit(key, 0)                    # Shuffle bit 0 -> Key bit 5 -> Bit value 1->0
    assert key.__repr__() == "Key: 010010"
Exemple #5
0
def test_create_covering_blocks():

    # Prepare key and shuffle.
    Key.set_random_seed(3331)
    Shuffle.set_random_seed(3332)
    key = Key.create_random_key(16)
    assert key.__repr__() == "Key: 0011011001100110"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == (
        "Shuffle: 0->4 1->14 2->5 3->15 4->0 5->1 6->7 7->11 "
        "8->6 9->12 10->13 11->3 12->9 13->8 14->2 15->10")

    # Multiple blocks, last block is partially filled.
    blocks = Block.create_covering_blocks(key, shuffle, 5)
    assert len(blocks) == 4
    assert blocks[0].__repr__(
    ) == "Block: 0->4=0 1->14=1 2->5=1 3->15=0 4->0=0"
    assert blocks[1].__repr__(
    ) == "Block: 5->1=0 6->7=0 7->11=0 8->6=1 9->12=0"
    assert blocks[2].__repr__(
    ) == "Block: 10->13=1 11->3=1 12->9=1 13->8=0 14->2=1"
    assert blocks[3].__repr__() == "Block: 15->10=1"

    # Multiple blocks, last block is fully filled.
    blocks = Block.create_covering_blocks(key, shuffle, 4)
    assert len(blocks) == 4
    assert blocks[0].__repr__() == "Block: 0->4=0 1->14=1 2->5=1 3->15=0"
    assert blocks[1].__repr__() == "Block: 4->0=0 5->1=0 6->7=0 7->11=0"
    assert blocks[2].__repr__() == "Block: 8->6=1 9->12=0 10->13=1 11->3=1"
    assert blocks[3].__repr__() == "Block: 12->9=1 13->8=0 14->2=1 15->10=1"

    # Single block, partially filled.
    key = Key.create_random_key(4)
    assert key.__repr__() == "Key: 1111"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == "Shuffle: 0->0 1->1 2->3 3->2"
    blocks = Block.create_covering_blocks(key, shuffle, 5)
    assert len(blocks) == 1
    assert blocks[0].__repr__() == "Block: 0->0=1 1->1=1 2->3=1 3->2=1"

    # Single block, fully filled.
    blocks = Block.create_covering_blocks(key, shuffle, 4)
    assert len(blocks) == 1
    assert blocks[0].__repr__() == "Block: 0->0=1 1->1=1 2->3=1 3->2=1"
Exemple #6
0
def test_repr():
    Key.set_random_seed(4441)
    Shuffle.set_random_seed(4442)
    key = Key.create_random_key(4)
    assert key.__repr__() == "Key: 1111"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == "Shuffle: 0->3 1->2 2->1 3->0"
    blocks = Block.create_covering_blocks(key, shuffle, 5)
    assert len(blocks) == 1
    assert blocks[0].__repr__() == "Block: 0->3=1 1->2=1 2->1=1 3->0=1"
Exemple #7
0
def test_calculate_parity():
    Key.set_random_seed(8881)
    Shuffle.set_random_seed(8882)
    key = Key.create_random_key(10)
    assert key.__repr__() == "Key: 1011111100"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == "Shuffle: 0->1 1->4 2->5 3->9 4->6 5->0 6->7 7->2 8->3 9->8"
    assert shuffle.__str__() == "0->1 1->4 2->5 3->9 4->6 5->0 6->7 7->2 8->3 9->8"
    assert shuffle.calculate_parity(key, 0, 10) == 1
    assert shuffle.calculate_parity(key, 4, 8) == 0
    assert shuffle.calculate_parity(key, 1, 2) == 1
Exemple #8
0
def test_set_bit():
    Key.set_random_seed(6661)
    Shuffle.set_random_seed(6662)
    key = Key.create_random_key(6)
    assert key.__repr__() == "Key: 011110"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == "Shuffle: 0->1 1->5 2->0 3->4 4->2 5->3"
    shuffle.set_bit(key, 3, 0)                  # Shuffle bit 3 -> Key bit 4 -> Bit value 1->0
    assert key.__repr__() == "Key: 011100"
    shuffle.set_bit(key, 0, 1)                  # Shuffle bit 0 -> Key bit 1 -> Bit value 1->1
    assert key.__repr__() == "Key: 011100"
    shuffle.set_bit(key, 5, 0)                  # Shuffle bit 5 -> Key bit 3 -> Bit value 0->0
    assert key.__repr__() == "Key: 011000"
Exemple #9
0
def test_get_error_parity():

    # Create the original (sent) key.
    Key.set_random_seed(8881)
    Shuffle.set_random_seed(8882)
    correct_key = Key.create_random_key(16)
    assert correct_key.__repr__() == "Key: 1011111100101110"

    # Create the noisy (received) key, which has 3 errors relative to the original key.
    noisy_key = correct_key.copy(0.1875, Key.ERROR_METHOD_EXACT)
    assert correct_key.__repr__() == "Key: 1011111100101110"
    assert noisy_key.__repr__() == "Key: 1111111110101100"
    # Errors:  ^      ^     ^
    #                   111111
    #         0123456789012345

    # Create a random shuffling.
    shuffle = Shuffle(noisy_key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__() == (
        "Shuffle: 0->8 1->12 2->6 3->7 4->4 5->10 6->11 7->2 8->1 9->9 "
        "10->15 11->0 12->13 13->3 14->5 15->14")

    # Create a block that covers the entire shuffled noisy key.
    # The block has errors at the following shuffle indexes: 2->6 8->1 14->5
    rx_blocks = Block.create_covering_blocks(noisy_key, shuffle,
                                             noisy_key.get_size())
    assert len(rx_blocks) == 1
    rx_block = rx_blocks[0]
    assert rx_block.__repr__() == (
        "Block: 0->8=1 1->12=1 2->6=1 3->7=1 4->4=1 5->10=1 6->11=0 "
        "7->2=1 8->1=1 9->9=0 10->15=0 11->0=1 12->13=1 13->3=1 14->5=1 "
        "15->14=0")
    assert rx_block.__str__() == "1111110110011110"  # 12 ones -> even parity
    # Errors:  ^   ^^             # 3 errors -> odd number of errors
    #                   111111
    #         0123456789012345
    assert rx_block.get_current_parity() == 0

    # At this point, we have not yet corrected any error in the block.
    assert rx_block.get_error_parity() == Block.ERRORS_UNKNOWN
Exemple #10
0
def test_create_block():

    # Block covers entire shuffle.
    Key.set_random_seed(2221)
    Shuffle.set_random_seed(2222)
    key = Key.create_random_key(8)
    assert key.__repr__() == "Key: 10111010"
    shuffle = Shuffle(key.get_size(), Shuffle.SHUFFLE_RANDOM)
    assert shuffle.__repr__(
    ) == "Shuffle: 0->0 1->1 2->6 3->4 4->3 5->2 6->5 7->7"
    block = Block(key, shuffle, 3, 6, None)
    assert block.__repr__() == "Block: 3->4=1 4->3=1 5->2=1"
    block = Block(key, shuffle, 0, 8, None)
    assert block.__repr__(
    ) == "Block: 0->0=1 1->1=0 2->6=1 3->4=1 4->3=1 5->2=1 6->5=0 7->7=0"

    # Block covers part of the shuffle.
    block = Block(key, shuffle, 1, 3, None)
    assert block.__repr__() == "Block: 1->1=0 2->6=1"

    # Single bit block.
    block = Block(key, shuffle, 2, 3, None)
    assert block.__repr__() == "Block: 2->6=1"