Untitled
import random import itertools def crc_remainder(input_bitstring, polynomial_bitstring, initial_filler): """ Calculate the CRC remainder of a string of bits using a chosen polynomial. Args: input_bitstring (str): The data to be divided polynomial_bitstring (str): The divisor in binary form initial_filler (str): The string of bits to be used as initial filler Returns: str: The remainder of the division as a binary string """ # Initial setup len_input = len(input_bitstring) len_polynomial = len(polynomial_bitstring) # Append initial filler (zeros) to the input bit string augmented_bitstring = input_bitstring + initial_filler # Perform modulo-2 division (XOR operation) for i in range(len_input): if augmented_bitstring[i] == '1': # Only if the current bit is 1 for j in range(len_polynomial): augmented_bitstring = (augmented_bitstring[:i + j] + str(int(augmented_bitstring[i + j]) ^ int(polynomial_bitstring[j])) + augmented_bitstring[i + j + 1:]) # The remainder is the last 'len_polynomial - 1' bits of the augmented bit string remainder = augmented_bitstring[-(len_polynomial - 1):] return remainder def crc_check(received_bitstring, polynomial_bitstring): """ Check if the received bitstring has an error using the CRC method. Args: received_bitstring (str): The received data including the CRC code polynomial_bitstring (str): The divisor in binary form Returns: bool: True if no error is detected, False otherwise """ # Perform the same modulo-2 division (XOR operation) as in the sender's end len_received = len(received_bitstring) len_polynomial = len(polynomial_bitstring) # Perform division for i in range(len_received - len_polynomial + 1): if received_bitstring[i] == '1': # Only if the current bit is 1 for j in range(len_polynomial): received_bitstring = (received_bitstring[:i + j] + str(int(received_bitstring[i + j]) ^ int(polynomial_bitstring[j])) + received_bitstring[i + j + 1:]) # If the remainder is all zeros, the data is considered to be correct remainder = received_bitstring[-(len_polynomial - 1):] return remainder == '0' * (len_polynomial - 1) def generate_bit_strings(length): """ Generate all bit strings of a given length. Args: length (int): The length of the bit strings to generate. Yields: str: Each bit string of the specified length. """ # Generate all combinations of '0' and '1' of the given length for bits in itertools.product('01', repeat=length): yield ''.join(bits) def flip_bit(bitstring, index): flipped = list(bitstring) flipped[index] = '1' if bitstring[index] == '0' else '0' return ''.join(flipped) # Example usage: length = 20 bit_strings = generate_bit_strings(length) polynomial_bitstring = "1111111111101111" initial_filler = "0"*(len(polynomial_bitstring)-1) for bit_string in bit_strings: input_bitstring = bit_string # print(bit_string) crc_code = input_bitstring + crc_remainder(input_bitstring, polynomial_bitstring, initial_filler) indices = random.sample(range(len(crc_code)), 2) corrupted_code = crc_code for index in indices: corrupted_code = flip_bit(corrupted_code, index) count = 0 for i, j in itertools.combinations(range(len(corrupted_code)), 2): corrected_code = flip_bit(flip_bit(corrupted_code, i), j) if crc_check(corrected_code, polynomial_bitstring): count += 1 # print(count) if count > 1: print("Unsuccessful")
Leave a Comment