import numpy as np

from typing import Iterable, Tuple, List, Union

_lfsr_length = 16
_key_length = 128 // 8
_iv_length = 128 // 8

_d_table = [
    0b100010011010111, 0b010011010111100, 0b110001001101011, 0b001001101011110, 0b101011110001001,
    0b011010111100010, 0b111000100110101, 0b000100110101111, 0b100110101111000, 0b010111100010011,
    0b110101111000100, 0b001101011110001, 0b101111000100110, 0b011110001001101, 0b111100010011010,
    0b100011110101100
]

_init_phase_round_count = 32
_2_power_31_minus_1 = 0x7fffffff
_2_power_32_minus_1 = 0xffffffff

_s_table = [
    [
        0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB, 0x7B, 0x1B,
        0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90, 0x4D, 0x4E, 0x84, 0x99,
        0xE4, 0xCE, 0xD9, 0x91, 0xDD, 0xB6, 0x85, 0x48, 0x8B, 0x29, 0x6E, 0xAC, 0xCD, 0xC1, 0xF8, 0x1E, 0x73, 0x43,
        0x69, 0xC6, 0xB5, 0xBD, 0xFD, 0x39, 0x63, 0x20, 0xD4, 0x38, 0x76, 0x7D, 0xB2, 0xA7, 0xCF, 0xED, 0x57, 0xC5,
        0xF3, 0x2C, 0xBB, 0x14, 0x21, 0x06, 0x55, 0x9B, 0xE3, 0xEF, 0x5E, 0x31, 0x4F, 0x7F, 0x5A, 0xA4, 0x0D, 0x82,
        0x51, 0x49, 0x5F, 0xBA, 0x58, 0x1C, 0x4A, 0x16, 0xD5, 0x17, 0xA8, 0x92, 0x24, 0x1F, 0x8C, 0xFF, 0xD8, 0xAE,
        0x2E, 0x01, 0xD3, 0xAD, 0x3B, 0x4B, 0xDA, 0x46, 0xEB, 0xC9, 0xDE, 0x9A, 0x8F, 0x87, 0xD7, 0x3A, 0x80, 0x6F,
        0x2F, 0xC8, 0xB1, 0xB4, 0x37, 0xF7, 0x0A, 0x22, 0x13, 0x28, 0x7C, 0xCC, 0x3C, 0x89, 0xC7, 0xC3, 0x96, 0x56,
        0x07, 0xBF, 0x7E, 0xF0, 0x0B, 0x2B, 0x97, 0x52, 0x35, 0x41, 0x79, 0x61, 0xA6, 0x4C, 0x10, 0xFE, 0xBC, 0x26,
        0x95, 0x88, 0x8A, 0xB0, 0xA3, 0xFB, 0xC0, 0x18, 0x94, 0xF2, 0xE1, 0xE5, 0xE9, 0x5D, 0xD0, 0xDC, 0x11, 0x66,
        0x64, 0x5C, 0xEC, 0x59, 0x42, 0x75, 0x12, 0xF5, 0x74, 0x9C, 0xAA, 0x23, 0x0E, 0x86, 0xAB, 0xBE, 0x2A, 0x02,
        0xE7, 0x67, 0xE6, 0x44, 0xA2, 0x6C, 0xC2, 0x93, 0x9F, 0xF1, 0xF6, 0xFA, 0x36, 0xD2, 0x50, 0x68, 0x9E, 0x62,
        0x71, 0x15, 0x3D, 0xD6, 0x40, 0xC4, 0xE2, 0x0F, 0x8E, 0x83, 0x77, 0x6B, 0x25, 0x05, 0x3F, 0x0C, 0x30, 0xEA,
        0x70, 0xB7, 0xA1, 0xE8, 0xA9, 0x65, 0x8D, 0x27, 0x1A, 0xDB, 0x81, 0xB3, 0xA0, 0xF4, 0x45, 0x7A, 0x19, 0xDF,
        0xEE, 0x78, 0x34, 0x60
    ],
    [
        0x55, 0xC2, 0x63, 0x71, 0x3B, 0xC8, 0x47, 0x86, 0x9F, 0x3C, 0xDA, 0x5B, 0x29, 0xAA, 0xFD, 0x77, 0x8C, 0xC5,
        0x94, 0x0C, 0xA6, 0x1A, 0x13, 0x00, 0xE3, 0xA8, 0x16, 0x72, 0x40, 0xF9, 0xF8, 0x42, 0x44, 0x26, 0x68, 0x96,
        0x81, 0xD9, 0x45, 0x3E, 0x10, 0x76, 0xC6, 0xA7, 0x8B, 0x39, 0x43, 0xE1, 0x3A, 0xB5, 0x56, 0x2A, 0xC0, 0x6D,
        0xB3, 0x05, 0x22, 0x66, 0xBF, 0xDC, 0x0B, 0xFA, 0x62, 0x48, 0xDD, 0x20, 0x11, 0x06, 0x36, 0xC9, 0xC1, 0xCF,
        0xF6, 0x27, 0x52, 0xBB, 0x69, 0xF5, 0xD4, 0x87, 0x7F, 0x84, 0x4C, 0xD2, 0x9C, 0x57, 0xA4, 0xBC, 0x4F, 0x9A,
        0xDF, 0xFE, 0xD6, 0x8D, 0x7A, 0xEB, 0x2B, 0x53, 0xD8, 0x5C, 0xA1, 0x14, 0x17, 0xFB, 0x23, 0xD5, 0x7D, 0x30,
        0x67, 0x73, 0x08, 0x09, 0xEE, 0xB7, 0x70, 0x3F, 0x61, 0xB2, 0x19, 0x8E, 0x4E, 0xE5, 0x4B, 0x93, 0x8F, 0x5D,
        0xDB, 0xA9, 0xAD, 0xF1, 0xAE, 0x2E, 0xCB, 0x0D, 0xFC, 0xF4, 0x2D, 0x46, 0x6E, 0x1D, 0x97, 0xE8, 0xD1, 0xE9,
        0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89, 0x01, 0xB6,
        0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4, 0xD0, 0x91, 0xC7, 0xCE,
        0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE, 0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B,
        0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21, 0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35,
        0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34, 0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA,
        0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28, 0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF,
        0x12, 0x03, 0xE2, 0xF2
    ],
    None,
    None
]
_s_table[2] = _s_table[0]
_s_table[3] = _s_table[1]


def _print_lfsr(lfsr: List[int], lfsr_curr_ptr: int) -> None:
    print('LFSR state: ', end='')
    print('(', end='')

    print(', '.join(map(hex,
                        (lfsr[(i + lfsr_curr_ptr) % _lfsr_length] for i in range(_lfsr_length)))))

    print(')', end='')
    print()


def gen_key_iter_infinite(k: bytes, iv: bytes) -> Iterable[int]:
    def _s(x: int) -> int:
        x0_ = (x & 0xff000000) >> 24
        x1_ = (x & 0x00ff0000) >> 16
        x2_ = (x & 0x0000ff00) >> 8
        x3_ = x & 0x000000ff

        y0_ = _s_table[0][x0_]
        y1_ = _s_table[1][x1_]
        y2_ = _s_table[2][x2_]
        y3_ = _s_table[3][x3_]

        return (y0_ << 24) | (y1_ << 16) | (y2_ << 8) | y3_

    def _l1(x: int) -> int:
        # return (x ^ (x << 2) ^ (x >> 30) ^ (x << 10) ^ (x >> 22) ^
        #        (x << 18) ^ (x >> 14) ^ (x << 24) ^ (x >> 8)) & 0xffffffff

        result = x

        l_factor = x << 2
        r_factor = x >> 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 6
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 6
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        return result

    def _l2(x: int) -> int:
        # return (x ^ (x << 8) ^ (x >> 24) ^ (x << 14) ^ (x >> 18) ^
        #        (x << 22) ^ (x >> 10) ^ (x << 30) ^ (x >> 2)) & 0xffffffff

        result = x

        l_factor = x << 8
        r_factor = x >> 2
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 6
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 6
        result ^= l_factor
        result ^= r_factor

        return result

    def _lfsr(index: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr
        return lfsr[(lfsr_curr_ptr + index) % _lfsr_length]

    def _get_h_l(h_index: int, l_index: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr

        _high_bitmask = 0x7fff8000
        _low_bitmask = 0x0000ffff

        return ((_lfsr(h_index) & _high_bitmask) << 1) | (_lfsr(l_index) & _low_bitmask)

    def _get_l_h(l_index: int, h_index: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr

        _high_bitmask = 0x7fff8000
        _low_bitmask = 0x0000ffff

        return ((_lfsr(l_index) & _low_bitmask) << 16) | ((_lfsr(h_index) & _high_bitmask) >> 15)

    def _bit_reconstruction_and_f(xor_x4=False) -> Union[int, None]:
        nonlocal lfsr, lfsr_curr_ptr, r1, r2

        w_ = ((_get_h_l(15, 14) ^ r1) + r2) & _2_power_32_minus_1

        w1 = r1 + _get_l_h(11, 9)
        # w2 = r2 ^ _get_l_h(7, 5)
        w1l = (w1 & 0x0000ffff) << 16
        w1h = (w1 & 0xffff0000) >> 16

        # w2 = r2 ^ _get_l_h(7, 5)
        # w2l = r2l ^ s7l
        w2l = ((r2 << 16) ^ (_lfsr(5) << 1)) & 0xffff0000
        w2h = ((r2 >> 16) ^ _lfsr(7)) & 0x0000ffff

        r1 = _s(_l1(w1l | w2h))
        r2 = _s(_l2(w2l | w1h))

        if xor_x4:
            # _lfsr_with_work_mode() will shift away s0
            old_l_h = _get_l_h(2, 0)

            _lfsr_with_work_mode()

            return w_ ^ old_l_h
        else:
            _lfsr_with_initialization_mode(w_ >> 1)

    def _magic_polynomial(u: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr

        v = _lfsr(15) << 15
        v += _lfsr(13) << 17
        v += _lfsr(10) << 21
        v += _lfsr(4) << 20
        v += _lfsr(0) << 8
        v += _lfsr(0)
        v += u
        v %= _2_power_31_minus_1

        if v == 0:
            v = _2_power_31_minus_1

        return v

    def _lfsr_with_initialization_mode(u: int) -> None:
        nonlocal lfsr, lfsr_curr_ptr

        lfsr[lfsr_curr_ptr] = _magic_polynomial(u)
        lfsr_curr_ptr += 1
        lfsr_curr_ptr %= _lfsr_length

    def _lfsr_with_work_mode() -> None:
        _lfsr_with_initialization_mode(0)

    r1, r2 = 0, 0

    if len(k) != _key_length:
        raise ValueError("gen_key_iter_infinite(): invalid key length")

    if len(iv) != _iv_length:
        raise ValueError("gen_key_iter_infinite(): invalid iv length")

    # lfsr is a ring buffer
    lfsr = [(k[i] << 23) | (_d_table[i] << 8) | iv[i] for i in range(_lfsr_length)]
    lfsr_curr_ptr = 0

    for _ in range(_init_phase_round_count):
        _bit_reconstruction_and_f()

    _bit_reconstruction_and_f(xor_x4=True)

    while True:
        z = _bit_reconstruction_and_f(xor_x4=True)

        yield (z & 0xff000000) >> 24
        yield (z & 0x00ff0000) >> 16
        yield (z & 0x0000ff00) >> 8
        yield z & 0x000000ff


def gen_key_iter(k: bytes, iv: bytes, byte_length: int) -> Iterable[int]:
    g = gen_key_iter_infinite(k, iv)

    # make g an iterator, in order to make types match
    g = iter(g)

    count = 1

    while count <= byte_length:
        yield next(g)
        count += 1

    return


def encrypt(k: bytes, iv: bytes, m: bytes) -> bytes:
    length = len(m)
    return bytes(map(lambda x, y: x ^ y, gen_key_iter(k, iv, length), m))


def decrypt(k: bytes, iv: bytes, c: bytes) -> bytes:
    return encrypt(k, iv, c)

import numpy as np

from typing import Iterable, Tuple, List, Union

_lfsr_length = 16
_key_length = 128 // 8
_iv_length = 128 // 8

_d_table = [
    0b100010011010111, 0b010011010111100, 0b110001001101011, 0b001001101011110, 0b101011110001001,
    0b011010111100010, 0b111000100110101, 0b000100110101111, 0b100110101111000, 0b010111100010011,
    0b110101111000100, 0b001101011110001, 0b101111000100110, 0b011110001001101, 0b111100010011010,
    0b100011110101100
]

_init_phase_round_count = 32
_2_power_31_minus_1 = 0x7fffffff
_2_power_32_minus_1 = 0xffffffff

_s_table = [
    [
        0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB, 0x7B, 0x1B,
        0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90, 0x4D, 0x4E, 0x84, 0x99,
        0xE4, 0xCE, 0xD9, 0x91, 0xDD, 0xB6, 0x85, 0x48, 0x8B, 0x29, 0x6E, 0xAC, 0xCD, 0xC1, 0xF8, 0x1E, 0x73, 0x43,
        0x69, 0xC6, 0xB5, 0xBD, 0xFD, 0x39, 0x63, 0x20, 0xD4, 0x38, 0x76, 0x7D, 0xB2, 0xA7, 0xCF, 0xED, 0x57, 0xC5,
        0xF3, 0x2C, 0xBB, 0x14, 0x21, 0x06, 0x55, 0x9B, 0xE3, 0xEF, 0x5E, 0x31, 0x4F, 0x7F, 0x5A, 0xA4, 0x0D, 0x82,
        0x51, 0x49, 0x5F, 0xBA, 0x58, 0x1C, 0x4A, 0x16, 0xD5, 0x17, 0xA8, 0x92, 0x24, 0x1F, 0x8C, 0xFF, 0xD8, 0xAE,
        0x2E, 0x01, 0xD3, 0xAD, 0x3B, 0x4B, 0xDA, 0x46, 0xEB, 0xC9, 0xDE, 0x9A, 0x8F, 0x87, 0xD7, 0x3A, 0x80, 0x6F,
        0x2F, 0xC8, 0xB1, 0xB4, 0x37, 0xF7, 0x0A, 0x22, 0x13, 0x28, 0x7C, 0xCC, 0x3C, 0x89, 0xC7, 0xC3, 0x96, 0x56,
        0x07, 0xBF, 0x7E, 0xF0, 0x0B, 0x2B, 0x97, 0x52, 0x35, 0x41, 0x79, 0x61, 0xA6, 0x4C, 0x10, 0xFE, 0xBC, 0x26,
        0x95, 0x88, 0x8A, 0xB0, 0xA3, 0xFB, 0xC0, 0x18, 0x94, 0xF2, 0xE1, 0xE5, 0xE9, 0x5D, 0xD0, 0xDC, 0x11, 0x66,
        0x64, 0x5C, 0xEC, 0x59, 0x42, 0x75, 0x12, 0xF5, 0x74, 0x9C, 0xAA, 0x23, 0x0E, 0x86, 0xAB, 0xBE, 0x2A, 0x02,
        0xE7, 0x67, 0xE6, 0x44, 0xA2, 0x6C, 0xC2, 0x93, 0x9F, 0xF1, 0xF6, 0xFA, 0x36, 0xD2, 0x50, 0x68, 0x9E, 0x62,
        0x71, 0x15, 0x3D, 0xD6, 0x40, 0xC4, 0xE2, 0x0F, 0x8E, 0x83, 0x77, 0x6B, 0x25, 0x05, 0x3F, 0x0C, 0x30, 0xEA,
        0x70, 0xB7, 0xA1, 0xE8, 0xA9, 0x65, 0x8D, 0x27, 0x1A, 0xDB, 0x81, 0xB3, 0xA0, 0xF4, 0x45, 0x7A, 0x19, 0xDF,
        0xEE, 0x78, 0x34, 0x60
    ],
    [
        0x55, 0xC2, 0x63, 0x71, 0x3B, 0xC8, 0x47, 0x86, 0x9F, 0x3C, 0xDA, 0x5B, 0x29, 0xAA, 0xFD, 0x77, 0x8C, 0xC5,
        0x94, 0x0C, 0xA6, 0x1A, 0x13, 0x00, 0xE3, 0xA8, 0x16, 0x72, 0x40, 0xF9, 0xF8, 0x42, 0x44, 0x26, 0x68, 0x96,
        0x81, 0xD9, 0x45, 0x3E, 0x10, 0x76, 0xC6, 0xA7, 0x8B, 0x39, 0x43, 0xE1, 0x3A, 0xB5, 0x56, 0x2A, 0xC0, 0x6D,
        0xB3, 0x05, 0x22, 0x66, 0xBF, 0xDC, 0x0B, 0xFA, 0x62, 0x48, 0xDD, 0x20, 0x11, 0x06, 0x36, 0xC9, 0xC1, 0xCF,
        0xF6, 0x27, 0x52, 0xBB, 0x69, 0xF5, 0xD4, 0x87, 0x7F, 0x84, 0x4C, 0xD2, 0x9C, 0x57, 0xA4, 0xBC, 0x4F, 0x9A,
        0xDF, 0xFE, 0xD6, 0x8D, 0x7A, 0xEB, 0x2B, 0x53, 0xD8, 0x5C, 0xA1, 0x14, 0x17, 0xFB, 0x23, 0xD5, 0x7D, 0x30,
        0x67, 0x73, 0x08, 0x09, 0xEE, 0xB7, 0x70, 0x3F, 0x61, 0xB2, 0x19, 0x8E, 0x4E, 0xE5, 0x4B, 0x93, 0x8F, 0x5D,
        0xDB, 0xA9, 0xAD, 0xF1, 0xAE, 0x2E, 0xCB, 0x0D, 0xFC, 0xF4, 0x2D, 0x46, 0x6E, 0x1D, 0x97, 0xE8, 0xD1, 0xE9,
        0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89, 0x01, 0xB6,
        0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4, 0xD0, 0x91, 0xC7, 0xCE,
        0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE, 0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B,
        0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21, 0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35,
        0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34, 0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA,
        0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28, 0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF,
        0x12, 0x03, 0xE2, 0xF2
    ],
    None,
    None
]
_s_table[2] = _s_table[0]
_s_table[3] = _s_table[1]


def _print_lfsr(lfsr: List[int], lfsr_curr_ptr: int) -> None:
    print('LFSR state: ', end='')
    print('(', end='')

    print(', '.join(map(hex,
                        (lfsr[(i + lfsr_curr_ptr) % _lfsr_length] for i in range(_lfsr_length)))))

    print(')', end='')
    print()


def gen_key_iter_infinite(k: bytes, iv: bytes) -> Iterable[int]:
    def _s(x: int) -> int:
        x0_ = (x & 0xff000000) >> 24
        x1_ = (x & 0x00ff0000) >> 16
        x2_ = (x & 0x0000ff00) >> 8
        x3_ = x & 0x000000ff

        y0_ = _s_table[0][x0_]
        y1_ = _s_table[1][x1_]
        y2_ = _s_table[2][x2_]
        y3_ = _s_table[3][x3_]

        return (y0_ << 24) | (y1_ << 16) | (y2_ << 8) | y3_

    def _l1(x: int) -> int:
        # return (x ^ (x << 2) ^ (x >> 30) ^ (x << 10) ^ (x >> 22) ^
        #        (x << 18) ^ (x >> 14) ^ (x << 24) ^ (x >> 8)) & 0xffffffff

        result = x

        l_factor = x << 2
        r_factor = x >> 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 6
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 6
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        return result

    def _l2(x: int) -> int:
        # return (x ^ (x << 8) ^ (x >> 24) ^ (x << 14) ^ (x >> 18) ^
        #        (x << 22) ^ (x >> 10) ^ (x << 30) ^ (x >> 2)) & 0xffffffff

        result = x

        l_factor = x << 8
        r_factor = x >> 2
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 6
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 8
        result ^= l_factor
        result ^= r_factor

        l_factor <<= 8
        r_factor >>= 6
        result ^= l_factor
        result ^= r_factor

        return result

    def _lfsr(index: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr
        return lfsr[(lfsr_curr_ptr + index) % _lfsr_length]

    def _get_h_l(h_index: int, l_index: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr

        _high_bitmask = 0x7fff8000
        _low_bitmask = 0x0000ffff

        return ((_lfsr(h_index) & _high_bitmask) << 1) | (_lfsr(l_index) & _low_bitmask)

    def _get_l_h(l_index: int, h_index: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr

        _high_bitmask = 0x7fff8000
        _low_bitmask = 0x0000ffff

        return ((_lfsr(l_index) & _low_bitmask) << 16) | ((_lfsr(h_index) & _high_bitmask) >> 15)

    def _bit_reconstruction_and_f(xor_x4=False) -> Union[int, None]:
        nonlocal lfsr, lfsr_curr_ptr, r1, r2

        w_ = ((_get_h_l(15, 14) ^ r1) + r2) & _2_power_32_minus_1

        w1 = r1 + _get_l_h(11, 9)
        # w2 = r2 ^ _get_l_h(7, 5)
        w1l = (w1 & 0x0000ffff) << 16
        w1h = (w1 & 0xffff0000) >> 16

        # w2 = r2 ^ _get_l_h(7, 5)
        # w2l = r2l ^ s7l
        w2l = ((r2 << 16) ^ (_lfsr(5) << 1)) & 0xffff0000
        w2h = ((r2 >> 16) ^ _lfsr(7)) & 0x0000ffff

        r1 = _s(_l1(w1l | w2h))
        r2 = _s(_l2(w2l | w1h))

        if xor_x4:
            # _lfsr_with_work_mode() will shift away s0
            old_l_h = _get_l_h(2, 0)

            _lfsr_with_work_mode()

            return w_ ^ old_l_h
        else:
            _lfsr_with_initialization_mode(w_ >> 1)

    def _magic_polynomial(u: int) -> int:
        nonlocal lfsr, lfsr_curr_ptr

        v = _lfsr(15) << 15
        v += _lfsr(13) << 17
        v += _lfsr(10) << 21
        v += _lfsr(4) << 20
        v += _lfsr(0) << 8
        v += _lfsr(0)
        v += u
        v %= _2_power_31_minus_1

        if v == 0:
            v = _2_power_31_minus_1

        return v

    def _lfsr_with_initialization_mode(u: int) -> None:
        nonlocal lfsr, lfsr_curr_ptr

        lfsr[lfsr_curr_ptr] = _magic_polynomial(u)
        lfsr_curr_ptr += 1
        lfsr_curr_ptr %= _lfsr_length

    def _lfsr_with_work_mode() -> None:
        _lfsr_with_initialization_mode(0)

    r1, r2 = 0, 0

    if len(k) != _key_length:
        raise ValueError("gen_key_iter_infinite(): invalid key length")

    if len(iv) != _iv_length:
        raise ValueError("gen_key_iter_infinite(): invalid iv length")

    # lfsr is a ring buffer
    lfsr = [(k[i] << 23) | (_d_table[i] << 8) | iv[i] for i in range(_lfsr_length)]
    lfsr_curr_ptr = 0

    for _ in range(_init_phase_round_count):
        _bit_reconstruction_and_f()

    _bit_reconstruction_and_f(xor_x4=True)

    while True:
        z = _bit_reconstruction_and_f(xor_x4=True)

        yield (z & 0xff000000) >> 24
        yield (z & 0x00ff0000) >> 16
        yield (z & 0x0000ff00) >> 8
        yield z & 0x000000ff


def gen_key_iter(k: bytes, iv: bytes, byte_length: int) -> Iterable[int]:
    g = gen_key_iter_infinite(k, iv)

    # make g an iterator, in order to make types match
    g = iter(g)

    count = 1

    while count <= byte_length:
        yield next(g)
        count += 1

    return


def encrypt(k: bytes, iv: bytes, m: bytes) -> bytes:
    length = len(m)
    return bytes(map(lambda x, y: x ^ y, gen_key_iter(k, iv, length), m))


def decrypt(k: bytes, iv: bytes, c: bytes) -> bytes:
    return encrypt(k, iv, c)

# return (x ^ (x << 2) ^ (x >> 30) ^ (x << 10) ^ (x >> 22) ^ #        (x << 18) ^ (x >> 14) ^ (x << 24) ^ (x >> 8)) & 0xffffffff # return (x ^ (x << 8) ^ (x >> 24) ^ (x << 14) ^ (x >> 18) ^ #        (x << 22) ^ (x >> 10) ^ (x << 30) ^ (x >> 2)) & 0xffffffff # w2 = r2 ^ _get_l_h(7, 5) # w2 = r2 ^ _get_l_h(7, 5) # w2l = r2l ^ s7l # _lfsr_with_work_mode() will shift away s0 # lfsr is a ring buffer # make g an iterator, in order to make types match