inst/openfhe/src/core/include/math/SAMPLING_README.md

Sampling in OpenFHE

OpenFHE library offers various methods for sampling from discrete Gaussian distribution. The methods are implemented in both DiscreteGaussianGenerator and DiscreteGaussianGeneratorGeneric classes, which have their specific uses. All of the samplers discussed have been tested in GLITCH Discrete Gaussian Testing Suite and no problems/abnormalities have been found.

Samplers in DiscreteGaussianGenerator

The class DiscreteGaussianGenerator is the main class for homomorphic encryption and includes the implementations of rejection sampling, Karney's method and Peikert's inversion method.

Since DiscreteGaussianGenerator contains both rejection based & precomputation-based sampling algorithms, a different constructor must be called based on the desired algorithm to be used. If Peikert's method is desired, then the object must be constructed with a distribution parameter whereas using rejection or Karney's method does not require such constraint. (Refer to How to Use Sampling Methods section for example code) The std parameter in the constructor is only used by Peikert's method.

Samplers in DiscreteGaussianGeneratorGeneric

The class DiscreteGaussianGeneratorGeneric is the constant-time generic sampler developed by UCSD, and it contains the definitions for this new sampling method and base samplers required for it.

How to Use Sampling Methods

Rejection Sampling

/*Create the generator object, std is not important as we choose it arbitrarily during sampling*/
DiscreteGaussianGenerator dggRejection;
/*First parameter is the mean, second one is the distribution parameter and third one is the ring dimension.*/
int64_t number = dggRejection.GenerateInteger(0,4,1024);

Karney's Method

/*Create the generator object, std is not important as we choose it arbitrarily during sampling*/
DiscreteGaussianGenerator dggKarney;
/*First parameter is the mean, second one is the distribution parameter*/
int64_t number = dggKarney.GenerateIntegerKarney(0,4);

Peikert's Inversion Method (As defined in DiscreteGaussianGenerator)

/*Create the generator object, the parameter is the distribution parameter*/
DiscreteGaussianGenerator dggPeikert(4);
/*This will create a single number*/
int64_t number = dggPeikert.GenerateInt();

Peikert's Inversion Method (As defined in DiscreteGaussianGeneratorGeneric)

/*Create a bit generator that will feed the random bits*/
BitGenerator* bg = new BitGenerator();

/*Mean and distribution parameter*/
double std= 4;
double mean = 0;

/*Create the sampler object*/
BaseSampler peikert_sampler(mean,std,bg,PEIKERT);

/*Generate Integer */
int64_t number = peikert_sampler.GenerateInteger();

Knuth-Yao's Method (As defined in DiscreteGaussianGeneratorGeneric)

/*Create a bit generator that will feed the random bits*/
BitGenerator* bg = new BitGenerator();

/*Mean and distribution parameter*/
double std= 4;
double mean = 0;

/*Create the sampler object*/
BaseSampler ky_sampler(mean,std,bg,KNUTH_YAO);

/*Generate Integer */
int64_t number = ky_sampler.GenerateInteger();

Generic Sampler


/*Create a bit generator that will feed the random bits*/
BitGenerator* bg = new BitGenerator();

/*Distribution parameter of the base samplers, distribution parameter of the actual distribution, number of base samplers, mean of the actual distribution*/
double stdBase = 34;
double std = (1<<22);
int CENTER_COUNT = 1024
double mean = 0;

/*Initialize base samplers*/
BaseSampler **peikert_samplers;
for(int i=0;i<CENTER_COUNT;i++){
    double center = ((double)i/(double)CENTER_COUNT);
    peikert_samplers[i]=new BaseSampler((double)center,stdBase,bg,PEIKERT);
}

/*Create the sampler object*/
int base = std::log2(CENTER_COUNT);
DiscreteGaussianGeneratorGeneric dggGeneric(peikert_samplers,stdBase,base,SMOOTHING_PARAMETER);

/*Generate Integer */
int64_t number = dggGeneric.GenerateInteger(mean,std);


Try the openfhe.R package in your browser

Any scripts or data that you put into this service are public.

openfhe.R documentation built on Nov. 13, 2026, 5:07 p.m.