fhelium.backend.random.stream
Per-device ChaCha20 random streams for CKKS sampling.
A stream is an int64 seed Tensor of shape [5] on its device: four 64-bit ChaCha20 key words followed by the 64-bit nonce. Every sampling call reads the key stream selected by the current nonce and then increments the nonce in place on the device, so consecutive calls, including replays of captured device work, read disjoint streams. The samplers are the fhelium_random_ops native operators; their sample-to-word assignment is documented with the C++ header fhelium/random.h.
RandomStream
class View source
RandomStream(device: torch.device | str, *, sigma: float, seed: int | None=None, nonce: int | None=None, stream_id: int=0)ChaCha20 sampling stream owned by one device.
Parameters
device: Device that holds the seed and receives samples.sigma: Discrete-Gaussian parameter of the error distribution.seed: Optional integer from which the key is derived together withstream_id. Without it, the key comes from the operating system's cryptographic random source.nonce: Initial nonce. The caller owns nonce uniqueness across streams that share a derived key.stream_id: Identifier separating streams derived from one seed, such as the streams of different devices.
uniform_residues
method
def uniform_residues(shape: Sequence[int], rns_params: torch.Tensor) -> torch.Tensor: ...Sample residues uniformly modulo every prime of rns_params.
shape is [*batch, coefficient]; the result has shape [*batch, limb, coefficient].
discrete_gaussian
method
def discrete_gaussian(shape: Sequence[int], rns_params: torch.Tensor | None=None) -> torch.Tensor: ...Sample the error distribution as centered int64 values or residues.
add_discrete_gaussian_
method
def add_discrete_gaussian_(residues: torch.Tensor, rns_params: torch.Tensor) -> None: ...Add error samples to residues in place.
Entries of [*batch, limb, coefficient] residues in
ternary
method
def ternary(shape: Sequence[int], nonzero_probability: float, rns_params: torch.Tensor | None=None) -> torch.Tensor: ...Sample independent coefficients with
fixed_weight_ternary
method
def fixed_weight_ternary(shape: Sequence[int], weight: int, rns_params: torch.Tensor | None=None) -> torch.Tensor: ...Sample ternary rows with exactly weight nonzero coefficients.
stochastic_round
method
def stochastic_round(values: torch.Tensor, *, word_stride: int=1) -> torch.Tensor: ...Round values randomly with expectation equal to each value.
advance_
function View source
def advance_(seed: torch.Tensor, count: int=1) -> None: ...Advance the nonce of seed in place by count.
stochastic_round_
function View source
def stochastic_round_(values: torch.Tensor, seed: torch.Tensor, *, word_stride: int=1) -> torch.Tensor: ...Round float64 values randomly and advance the stream of seed.
Value word_stride. A compact array holding every word_stride-th coefficient of a full array therefore uses the words of the corresponding full-array coefficients. Returns int64 values.