def generate_root(polynom): exponents = get_exponents(polynom) # assert polynom == sorted(polynom, reverse=True) width = max(exponents) # polynom[0] # the number whose all bits of an exponent are sets, except the highest one all_bits = or_all(1 << i for i in exponents) highest_bit = 1 << width polynomial_constant = all_bits ^ highest_bit constant_bitstring = getpadbinstr(polynomial_constant, width) reverse_bitstring = constant_bitstring[::-1] reverse_constant = getvaluefrombinarystring(reverse_bitstring) return polynomial_constant, reverse_constant, width
def change_base(block, block_length, source_base, source_size, target_base, target_size): """takes block as a block_length-bit long binary stream. reads it as entries of source_base, on source_size bits, resplit bits as target_base chars, which are stored on target_size""" # let's get a binary representation of our block, in our source base bits_string = "" for char in list(block): bits_string += getpadbinstr(source_base.index(char), source_size) # then split that representation in target_size bits long blocks split_bits = (bits_string[i : i + target_size] for i in range(0, block_length, target_size)) # get the according values, encoded_values = (getvaluefrombinarystring(i) for i in split_bits) # and the corresponding entries from our target base output_chars = [target_base[i] for i in encoded_values] output_block = "".join(output_chars) return output_block
def change_base(block, block_length, source_base, source_size, target_base, target_size): """takes block as a block_length-bit long binary stream. reads it as entries of source_base, on source_size bits, resplit bits as target_base chars, which are stored on target_size""" #let's get a binary representation of our block, in our source base bits_string = "" for char in list(block): bits_string += getpadbinstr(source_base.index(char), source_size) #then split that representation in target_size bits long blocks split_bits = (bits_string[i:i + target_size] for i in range(0, block_length, target_size)) #get the according values, encoded_values = (getvaluefrombinarystring(i) for i in split_bits) #and the corresponding entries from our target base output_chars = [target_base[i] for i in encoded_values] output_block = "".join(output_chars) return output_block
#returns the greatest common divisor of both parameters assert gcd(6, 8) == 2 #lcm #returns the least common multiplier of both parameters assert lcm(6, 9) == 18 assert lcm(6, 8) == 24 assert getbinlen(0) == 1 assert getbinlen(1) == 1 assert getbinlen(3) == 2 assert getvaluefrombinarystring("1000") == 8 assert getvaluefrombinarystring("001000") == 8 assert getpadbinstr(8, 8) == "00001000" assert getunkbinstr(0, 0, 8) == "000000000" assert getunkbinstr(1, 0, 8) == "000000001" assert getunkbinstr(2, 1, 8) == "00000001x" assert getunkbinstr(237, 3, 8) == "011101xxx" assert gethexstr("\x00") == "00" assert gethexstr("\x00\x01") == "00 01" assert gethexstr("abcd") == "61 62 63 64" assert int2lebin(1, 2) == '\x01\x00' assert int2lebin(65535, 2) == "\xff\xff" assert int2lebin(65535, 3) == "\xff\xff\x00" assert int2bebin(1, 2) == '\x00\x01'