fhelium/random.h
#include <fhelium/random.h> · namespace fhelium::random
Cryptographically secure sampling for CKKS secrets, masks, and errors.
Every sampler reads the ChaCha20 key stream selected by a seed Tensor: an int64 Tensor of shape [5] on the device that receives the samples. Elements 0 to 3 hold the 256-bit key as little-endian 64-bit words, and element 4 holds the 64-bit nonce. Block
Sample shape argument is a coefficient shape [*batch, coefficient]. A sampler given rns_params returns the residues of its integer samples modulo every prime [*batch, limb, coefficient] and the dtype of rns_params (row layout in fhelium/rns_parameters.h); without rns_params it returns the centered int64 samples with shape shape. The integers do not depend on rns_params, so the same seed yields the same polynomial in every RNS basis.
| Function | Devices | Summary |
|---|---|---|
generate_seed | Any device | Returns a seed whose key comes from the operating system's cryptographic random source and whose nonce is zero. |
chacha20_keystream | CPU, CUDA, HIP | Returns block_count ChaCha20 key-stream blocks starting at block first_block. |
sample_uniform_residues | CPU, CUDA, HIP | Samples residues uniformly and independently modulo every prime. |
expand_seeded_key | CPU, CUDA, HIP | Expands the uniform component of a seeded key-switching key. |
gaussian_cdt_table | Any device | Returns the cumulative distribution table of the centered discrete Gaussian distribution with parameter |
sample_discrete_gaussian | CPU, CUDA, HIP | Samples the centered discrete Gaussian distribution described by cdt_table. |
add_discrete_gaussian_ | CPU, CUDA, HIP | Adds discrete Gaussian samples to residues in place. |
sample_centered_binomial | CPU, CUDA, HIP | Samples the centered binomial distribution with parameter |
sample_ternary | CPU, CUDA, HIP | Samples independent ternary coefficients with |
sample_fixed_weight_ternary | Any device | Samples ternary coefficients with exactly weight nonzero entries in each row of the final axis. |
stochastic_round | CPU, CUDA, HIP | Rounds float64 values to integers randomly with expectation equal to the value. |
generate_seed
function View source
at::Tensor fhelium::random::generate_seed();Devices: Any device
Torch operator: torch.ops.fhelium_random_ops.generate_seed
fhelium_random_ops::generate_seed() -> TensorReturns a seed whose key comes from the operating system's cryptographic random source and whose nonce is zero.
The result is a CPU int64 Tensor of shape [5]; move it to the sampling device before use.
chacha20_keystream
function View source
at::Tensor fhelium::random::chacha20_keystream(
const at::Tensor& seed,
int64_t first_block,
int64_t block_count);2
3
4
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.chacha20_keystream
fhelium_random_ops::chacha20_keystream(Tensor seed, int first_block, int block_count) -> TensorReturns block_count ChaCha20 key-stream blocks starting at block first_block.
The result is a uint32 Tensor of shape [block_count, 16] on the device of seed; row first_block
sample_uniform_residues
function View source
at::Tensor fhelium::random::sample_uniform_residues(
const at::Tensor& seed,
at::IntArrayRef shape,
const at::Tensor& rns_params);2
3
4
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.sample_uniform_residues
fhelium_random_ops::sample_uniform_residues(Tensor seed, int[] shape, Tensor rns_params) -> TensorSamples residues uniformly and independently modulo every prime.
Each residue reads four words as a 128-bit integer [*shape[:-1], limb, shape[-1]] and the dtype of rns_params.
expand_seeded_key
function View source
at::Tensor fhelium::random::expand_seeded_key(
const at::Tensor& seed_rows,
const at::Tensor& moduli,
int64_t ring_dimension,
int64_t rounds);2
3
4
5
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.expand_seeded_key
fhelium_random_ops::expand_seeded_key(Tensor seed_rows, Tensor moduli, int ring_dimension, int rounds) -> TensorExpands the uniform component of a seeded key-switching key.
FHElium key generation gives every key-switching key one seed row per digit; its uniform component
seed_rows is a contiguous int64 Tensor [key_digit, 6] whose row holds the 256-bit key as four little-endian 64-bit words, the 64-bit key identifier and the stored digit index moduli[r], is sampled by segments of rounds blocks whose nonce is the key identifier and whose 64-bit block counter is [key_digit, limb, ring_dimension] and the dtype of moduli, an int32 or int64 Tensor [limb] on the device of seed_rows. Requires fewer than rounds must be a supported round count (12).
gaussian_cdt_table
function View source
at::Tensor fhelium::random::gaussian_cdt_table(
double sigma);2
Devices: Any device
Torch operator: torch.ops.fhelium_random_ops.gaussian_cdt_table
fhelium_random_ops::gaussian_cdt_table(float sigma) -> TensorReturns the cumulative distribution table of the centered discrete Gaussian distribution with parameter
The distribution assigns [M, 2] for the magnitude bound
sample_discrete_gaussian
function View source
at::Tensor fhelium::random::sample_discrete_gaussian(
const at::Tensor& seed,
at::IntArrayRef shape,
const at::Tensor& cdt_table,
const std::optional<at::Tensor>& rns_params);2
3
4
5
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.sample_discrete_gaussian
fhelium_random_ops::sample_discrete_gaussian(Tensor seed, int[] shape, Tensor cdt_table, Tensor? rns_params=None) -> TensorSamples the centered discrete Gaussian distribution described by cdt_table.
Each sample reads four words as a 128-bit integer. Its lowest bit selects the sign, and the magnitude is the number of table rows not exceeding the remaining 127 bits. The comparison visits every row, so the work per sample does not depend on the sampled value. cdt_table comes from gaussian_cdt_table and resides on the device of seed.
add_discrete_gaussian_
function View source
void fhelium::random::add_discrete_gaussian_(
at::Tensor& residues,
const at::Tensor& seed,
const at::Tensor& cdt_table,
const at::Tensor& rns_params);2
3
4
5
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.add_discrete_gaussian_
fhelium_random_ops::add_discrete_gaussian_(Tensor(a!) residues, Tensor seed, Tensor cdt_table, Tensor rns_params) -> ()Adds discrete Gaussian samples to residues in place.
residues is a contiguous Tensor of shape [*batch, limb, coefficient] with the dtype and limbs of rns_params and entries in sample_discrete_gaussian for the shape [*batch, coefficient] and the same seed, and every entry becomes
sample_centered_binomial
function View source
at::Tensor fhelium::random::sample_centered_binomial(
const at::Tensor& seed,
at::IntArrayRef shape,
int64_t k,
const std::optional<at::Tensor>& rns_params);2
3
4
5
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.sample_centered_binomial
fhelium_random_ops::sample_centered_binomial(Tensor seed, int[] shape, int k, Tensor? rns_params=None) -> TensorSamples the centered binomial distribution with parameter
Each sample reads two words
sample_ternary
function View source
at::Tensor fhelium::random::sample_ternary(
const at::Tensor& seed,
at::IntArrayRef shape,
double nonzero_probability,
const std::optional<at::Tensor>& rns_params);2
3
4
5
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.sample_ternary
fhelium_random_ops::sample_ternary(Tensor seed, int[] shape, float nonzero_probability, Tensor? rns_params=None) -> TensorSamples independent ternary coefficients with
Each sample reads two words as a 64-bit integer
sample_fixed_weight_ternary
function View source
at::Tensor fhelium::random::sample_fixed_weight_ternary(
const at::Tensor& seed,
at::IntArrayRef shape,
int64_t weight,
const std::optional<at::Tensor>& rns_params);2
3
4
5
Devices: Any device
Torch operator: torch.ops.fhelium_random_ops.sample_fixed_weight_ternary
fhelium_random_ops::sample_fixed_weight_ternary(Tensor seed, int[] shape, int weight, Tensor? rns_params=None) -> TensorSamples ternary coefficients with exactly weight nonzero entries in each row of the final axis.
Coefficient weight coefficients with the smallest labels in a row receive weight-subset unless two labels collide, which has probability below weight shape[-1].
stochastic_round
function View source
at::Tensor fhelium::random::stochastic_round(
const at::Tensor& seed,
const at::Tensor& values,
int64_t word_stride = 1);2
3
4
Devices: CPU, CUDA, HIP
Torch operator: torch.ops.fhelium_random_ops.stochastic_round
fhelium_random_ops::stochastic_round(Tensor seed, Tensor values, int word_stride=1) -> TensorRounds float64 values to integers randomly with expectation equal to the value.
Value values reads word word_stride, so a compact array of every word_stride-th coefficient rounds with the words of the corresponding full array. The result is an int64 Tensor with the shape of values, which reside on the device of seed. Requires word_stride