ChaCha
Generate reproducible bytes from a seed with ChaCha8, ChaCha12, or ChaCha20.
from oxcrypto import random
from oxcrypto.random.chacha import CHACHA
seed = random.bytes(32)
rng = CHACHA.CHACHA20.new(seed, stream=1)
block = rng.bytes(64)
buffer = bytearray(16)
rng.fill(buffer)new takes a 32-byte seed and a keyword-only 64-bit stream (default 0).
Output is the original ChaCha keystream (64-bit block counter starting at 0,
64-bit nonce set to stream in little-endian order), so the same seed and
stream always give the same bytes. bytes(n) returns the next n bytes and
fill(buffer) writes into a writable bytes-like object.
Output is produced in 32-bit words. A request whose length is not a multiple
of 4 discards the rest of its last word, so rng.bytes(3) + rng.bytes(5)
differs from rng.bytes(8).
Threads can share a generator; each call takes the next bytes of the stream.
It cannot be copied or pickled. The chacha20 crate is written to zeroize the
generator state when it is freed; oxcrypto has not verified this independently.
Implementation
- Crate:
chacha200.10 (RustCrypto),rngfeature. Stable, no published audit. - Standard: ChaCha by D. J. Bernstein (2008). There is no NIST or RFC standard for ChaCha as a random generator; RFC 8439 standardizes the ChaCha20 cipher with a 96-bit nonce and 32-bit counter, which this layout differs from.
- Tests: draft-strombergson-chacha-test-vectors-01 (256-bit keys, 8, 12, and 20 rounds).
- Use it for reproducible simulations and tests, not to stretch a secret seed
into keys. Use a KDF or
random.bytesfor keys.