sample

package
v0.0.0-...-a536860 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package sample includes samplers for sampling random values from different probability distributions.

Package sample provides the Sampler interface along with different implementations of this interface. Its primary purpose is support choosing random *big.Int values from selected probability distributions.

Implementations of the Sampler interface can be used, for instance, to fill vector or matrix structures with the desired random data.

Index

Constants

This section is empty.

Variables

View Source
var SigmaCDT, _ = new(big.Float).SetString("0.84932180028801904272150283410")

SigmaCDT is a constant sqrt(1/(2ln(2)))

Functions

func Bernoulli

func Bernoulli(t *big.Int, lSquareInv *big.Float) (bool, error)

Bernoulli returns true with probability proportional to 2^{-t/l^2}. A polynomial approximation is used to evaluate the exponential function. The implementation is based on paper: "FACCT: FAst, Compact, and Constant-Time Discrete Gaussian Sampler over Integers" by R. K. Zhao, R. Steinfeld, and A. Sakzad (https://eprint.iacr.org/2018/1234.pdf). See the above paper where it is argued that such a sampling achieves a relative error at most 2^{-45} with the chosen parameters.

Types

type Bit

type Bit struct {
	Uniform
}

Bit samples a single random bit (value 0 or 1).

type NormalCDT

type NormalCDT struct {
	// contains filtered or unexported fields
}

NormalCDT samples random values from the discrete Normal (Gaussian) probability distribution, limited to non-negative values (half-Gaussian). In particular each value x from Z^+ is sampled with probability proportional to exp(-x^2/sigma^2) where sigma = sqrt(1/2ln(2)). The implementation is based on paper: "FACCT: FAst, Compact, and Constant-Time Discrete Gaussian Sampler over Integers" by R. K. Zhao, R. Steinfeld, and A. Sakzad (https://eprint.iacr.org/2018/1234.pdf). See the above paper where it is argued that such a sampling achieves a relative error at most 2^{-46} with the chosen parameters.

func NewNormalCDT

func NewNormalCDT() *NormalCDT

NewNormalCDT returns an instance of NormalCDT sampler.

func (*NormalCDT) Sample

func (c *NormalCDT) Sample() (*big.Int, error)

Sample samples discrete non-negative values with Gaussian distribution.

type NormalCumulative

type NormalCumulative struct {
	// contains filtered or unexported fields
}

NormalCumulative samples random values from the cumulative Normal (Gaussian) probability distribution, centered on 0. This sampler is the fastest, but is limited only to cases when sigma is not too big, due to the sizes of the precomputed tables. Note that the sampler offers arbitrary precision but the implementation is not constant time.

func NewNormalCumulative

func NewNormalCumulative(sigma *big.Float, n uint, twoSided bool) *NormalCumulative

NewNormalCumulative returns an instance of NormalCumulative sampler. It assumes mean = 0. Values are precomputed when this function is called, so that Sample merely returns a precomputed value.

func (*NormalCumulative) Sample

func (c *NormalCumulative) Sample() (*big.Int, error)

Sample samples discrete cumulative distribution with precomputed values.

type NormalDouble

type NormalDouble struct {
	// contains filtered or unexported fields
}

NormalDouble samples random values from the normal (Gaussian) probability distribution, centered on 0. This sampler works in a way that it first samples from a NormalCumulative with some small sigma and then using another sampling from uniform distribution creates a candidate for the output, which is accepted or rejected with certain probability. Note that the sampler offers arbitrary precision but the implementation is not constant time.

func NewNormalDouble

func NewNormalDouble(sigma *big.Float, n uint, firstSigma *big.Float) (*NormalDouble, error)

NewNormalDouble returns an instance of NormalDouble sampler. It assumes mean = 0. Values are precomputed when this function is called, so that Sample merely samples a value. sigma should be a multiple of firstSigma. Increasing firstSigma a bit speeds up the algorithm but increases the number of precomputed values

func (*NormalDouble) Sample

func (s *NormalDouble) Sample() (*big.Int, error)

Sample samples according to discrete Gauss distribution using NormalCumulative and second sampling.

type NormalDoubleConstant

type NormalDoubleConstant struct {
	// contains filtered or unexported fields
}

NormalDoubleConstant samples random values from the normal (Gaussian) probability distribution, centered on 0. This sampler works by double sampling: it first samples from a fixed Gaussian distribution with NormalCDT and then using another sampling from uniform distribution creates a candidate for the output, which is accepted or rejected with certain probability. The sampler algorithm is constant time in the sense that the sampled value is independent of the time needed. The implementation is based on paper: "FACCT: FAst, Compact, and Constant-Time Discrete Gaussian Sampler over Integers" by R. K. Zhao, R. Steinfeld, and A. Sakzad, see https://eprint.iacr.org/2018/1234.pdf. See the above paper for the argumentation of the choice of parameters and proof of precision and security.

func NewNormalDoubleConstant

func NewNormalDoubleConstant(l *big.Int) *NormalDoubleConstant

NewNormalDoubleConstant returns an instance of NormalDoubleConstant sampler. It assumes mean = 0. Parameter l needs to be given, such that sigma = l * sqrt(1/2ln(2)).

func (*NormalDoubleConstant) Sample

func (s *NormalDoubleConstant) Sample() (*big.Int, error)

Sample samples according to discrete Gauss distribution using NormalDoubleConstant and second sampling.

type NormalNegative

type NormalNegative struct {
	// contains filtered or unexported fields
}

NormalNegative samples random values from the possible outputs of Normal (Gaussian) probability distribution centered on 0 and accepts or denies each sample with probability defined by the distribution

func NewNormalNegative

func NewNormalNegative(sigma *big.Float, n uint) *NormalNegative

NewNormalNegative returns an instance of NormalNegative sampler. It assumes mean = 0. Values are precomputed when this function is called, so that Sample merely returns a precomputed value.

func (*NormalNegative) Sample

func (c *NormalNegative) Sample() (*big.Int, error)

Sample samples a value from discrete Gaussian distribution based on negative (rejection) sampling.

type Sampler

type Sampler interface {
	// Sample samples random big integer values,
	// possibly returning an error.
	Sample() (*big.Int, error)
}

Sampler samples random big integer values. Implementations of this interface provide a method for sampling random values from the desired distribution.

type Uniform

type Uniform struct {
	UniformRange
}

Uniform samples random values from the interval [0, max).

func (*Uniform) Sample

func (u *Uniform) Sample() (*big.Int, error)

Sample samples random values from the interval [0, max).

type UniformRange

type UniformRange struct {
	// contains filtered or unexported fields
}

UniformRange samples random values from the interval [min, max).

func NewBit

func NewBit() *UniformRange

NewBit returns an instance of Bit sampler.

func NewUniform

func NewUniform(max *big.Int) *UniformRange

NewUniform returns an instance of the Uniform sampler. It accepts an upper bound on the sampled values.

func NewUniformRange

func NewUniformRange(min, max *big.Int) *UniformRange

NewUniformRange returns an instance of the UniformRange sampler. It accepts lower and upper bounds on the sampled values.

func (*UniformRange) Sample

func (u *UniformRange) Sample() (*big.Int, error)

Sample samples random values from the interval [min, max).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL