Updated Python module, Numpy module and randquik CLI for new core.
This commit is contained in:
+20
-10
@@ -12,15 +12,20 @@ if not src.is_dir():
|
||||
ffi = cffi.FFI()
|
||||
ffi.cdef(
|
||||
"""
|
||||
typedef uint64_t (*genfunc)(
|
||||
uint8_t* out, size_t outsize, uint32_t state[16], unsigned rounds
|
||||
);
|
||||
typedef struct cha_ctx {
|
||||
uint32_t input[16];
|
||||
uint8_t unconsumed[64];
|
||||
uint8_t uncount;
|
||||
uint8_t unconsumed[512];
|
||||
uint32_t offset, end;
|
||||
unsigned rounds;
|
||||
genfunc gen;
|
||||
} cha_ctx;
|
||||
|
||||
int cha_generate(uint8_t* out, uint64_t outlen, const uint8_t key[32], const uint8_t iv[16]);
|
||||
int cha_generate(uint8_t* out, uint64_t outlen, const uint8_t key[32], const uint8_t iv[16], unsigned rounds);
|
||||
|
||||
void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv);
|
||||
void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv, unsigned rounds);
|
||||
void cha_wipe(cha_ctx* ctx);
|
||||
int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen);
|
||||
"""
|
||||
@@ -65,11 +70,11 @@ def _processBuffer(out):
|
||||
|
||||
|
||||
class Cha:
|
||||
def __init__(self, key: bytes | Any, iv: bytes | Any):
|
||||
def __init__(self, key: bytes | Any, iv: bytes | Any, *, rounds=8):
|
||||
"""Construct a generator that holds its internal state, moving forward on each call."""
|
||||
key, iv = _processKeys(key, iv)
|
||||
self.ctx = ffi.new("cha_ctx*")
|
||||
lib.cha_init(self.ctx, key, iv)
|
||||
lib.cha_init(self.ctx, key, iv, rounds)
|
||||
|
||||
def __del__(self):
|
||||
lib.cha_wipe(self.ctx)
|
||||
@@ -82,15 +87,20 @@ class Cha:
|
||||
|
||||
|
||||
def generate_into(
|
||||
out: bytearray | memoryview | Any, key: bytes | Any, iv: bytes | Any = bytes(16)
|
||||
out: bytearray | memoryview | Any,
|
||||
key: bytes | Any,
|
||||
iv: bytes | Any = bytes(16),
|
||||
*,
|
||||
rounds=8,
|
||||
):
|
||||
"""Fill in random bytes into an existing array (buffer interface)"""
|
||||
key, iv = _processKeys(key, iv)
|
||||
outbuf, outlen = _processBuffer(out)
|
||||
lib.cha_generate(outbuf, outlen, key, iv)
|
||||
lib.cha_generate(outbuf, outlen, key, iv, rounds)
|
||||
return out
|
||||
|
||||
|
||||
def generate(outlen: int, key: bytes | Any, iv: bytes | Any = bytes(16)):
|
||||
def generate(outlen: int, key: bytes | Any, iv: bytes | Any = bytes(16), *, rounds=8):
|
||||
"""Return a bytearray of random bytes"""
|
||||
return generate_into(bytearray(outlen), key, iv)
|
||||
assert outlen >= 0
|
||||
return generate_into(bytearray(outlen), key, iv, rounds=rounds)
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
import secrets
|
||||
import sys
|
||||
|
||||
import cha
|
||||
import numpy as np
|
||||
|
||||
|
||||
class ChaRandom(np.random.BitGenerator):
|
||||
def __init__(self, seed=None):
|
||||
super().__init__(seed)
|
||||
sys.stderr.write("Construct\n")
|
||||
if seed is None:
|
||||
key = secrets.token_bytes(32)
|
||||
else:
|
||||
key = (
|
||||
np.random.SeedSequence(seed, pool_size=8)
|
||||
.generate_state(4, dtype=np.uint64)
|
||||
.tobytes()
|
||||
)
|
||||
self._generator = cha.Cha(key, bytes(8) + b"NumpyGen")
|
||||
|
||||
def random_raw(self, size=None):
|
||||
sys.stderr.write(f"Random raw {size=}\n")
|
||||
if size is None:
|
||||
return int.from_bytes(self._generator(bytearray(8)), "little")
|
||||
ret = np.empty(size, np.uint64)
|
||||
self._generator(ret.data)
|
||||
return ret
|
||||
|
||||
def spawn(self, n):
|
||||
sys.stderr.write(f"Spawn {n=}\n")
|
||||
raise NotImplementedError
|
||||
Reference in New Issue
Block a user