Making a simple randomness extractor using random matrix multiplications.

In [2]:
def make_extractor(in_bytes, out_bytes):
    matr = [os.urandom(1)[0] for _ in range(in_bytes * out_bytes)]
    matr = np.array(matr, dtype=np.ubyte).reshape((in_bytes, out_bytes))
    def extract(buf):
        buf = np.array(list(buf), dtype=np.ubyte)
        res = np.dot(buf, matr)
        return bytes(list(res))
    return extract
In [3]:
output_size = int(256 / 8)

IV = bytes([os.urandom(1)[0] for _ in range(output_size)])

compr = make_extractor(output_size * 2, output_size)

data = b"Waddup my dudes, it's Pickle Rick! No, wait! It's Tiny Rick!"

state = IV
while data:
    block = bytearray(output_size * 2)
    i = 0
    for c in state:
        block[i] = c
        i += 1
    for _ in range(min(len(data), output_size)):
        block[i] = data[0]
        data = data[1:]
        i += 1
    state = compr(block)

print(state.hex())
Out:
b8bafef6b76092ee2241b356f2ad1e03ae69ff4ec65de0e404bec88d976c79ab