def field2x_mod(dividend, divisor): '''uint as polynomial over field[2,x] >>> field2x_mod(3, 5) 3 >>> field2x_mod(4, 5) # == 4^5 1 >>> field2x_mod(5, 5) # == 5^5 0 >>> field2x_mod(12, 5) # == 12^(5<<1)^5 3 >>> field2x_mod(4, 1) # == 4^(1<<2) 0 ''' ## assert dividend >= 0 ## assert divisor > 0 dividend = to_uint(dividend, 'dividend') divisor = to_pint(divisor, 'divisor') D = divisor.bit_length() L = dividend.bit_length() if L <= D*1*8: return field2x_mod__plain(dividend, divisor) bs = dividend.to_bytes((L+8-1)//8, 'big') #assert int.from_bytes(bs, 'big') == dividend return _field2x_mod__bytes(bs, divisor)
def field2x_divmod(dividend, divisor): '''uint as polynomial over field[2,x] >>> field2x_divmod(3, 5) (0, 3) >>> field2x_divmod(4, 5) # == 4^5 (1, 1) >>> field2x_divmod(5, 5) # == 5^5 (1, 0) >>> field2x_divmod(12, 5) # == 12^(5<<1)^5 (3, 3) >>> field2x_divmod(4, 1) # == 4^(1<<2) (4, 0) ''' ## assert dividend >= 0 ## assert divisor > 0 dividend = to_uint(dividend, 'dividend') divisor = to_pint(divisor, 'divisor') D = divisor.bit_length() L = dividend.bit_length() if L <= D*1*8: return field2x_divmod__plain(dividend, divisor) bs = dividend.to_bytes((L+8-1)//8, 'big') return _field2x_divmod__bytes(bs, divisor)
def field2x_invmod(x, M): x = to_uint(x, 'x') M = to_pint(M, 'M') ## assert x >= 0 ## assert M > 0 x = field2x_mod(x, M) return field2x_invmod__plain(x, M)
def icut_seq_to(seq, block_size, begin=None, end=None): block_size = to_pint(block_size, 'block_size') if begin is None: begin = 0 if end is None: end = len(seq) for i in range(begin, end, block_size): yield seq[i:i + block_size]
def _icut_to(iterable, block_size, container): block_size = to_pint(block_size, 'block_size') if container is None: container = list it = iter(iterable) while True: c = container(islice(it, block_size)) if not c: break yield c
def field2x_divmod__bytes(dividend_in_bytes, divisor, byte_MSB_first): r'''bytes as uint; uint as polynomial over field[2,x] >>> field2x_divmod__bytes(b'\x03', 5, True) (0, 3) >>> field2x_divmod__bytes(b'\xc0', 5, False) (0, 3) >>> field2x_divmod__bytes(b'\x00\x03', 5, True) (0, 3) ''' divisor = to_pint(divisor, 'divisor') #assert divisor > 0 byte_MSB_first = bool(byte_MSB_first) dividend_in_bytes = std_data_bitorder(dividend_in_bytes, byte_MSB_first) return _field2x_divmod__bytes(dividend_in_bytes, divisor)
def CRC_generator2exps(generator): 'generator is uint > 0; frame_length = generator.bit_length()-1' generator = to_pint(generator, 'generator') #frame_length = generator.bit_length()-1 return uint2expsLE(generator)
def field2x_invmod__bytes(x_in_bytes, M, byte_MSB_first): M = to_pint(M, 'M') #assert M > 0 x = field2x_mod__bytes(x_in_bytes, M, byte_MSB_first) return field2x_invmod__plain(x, M)