Random module¶
Distributions¶
pqlattice.random.distribution.Uniform ¶
Bases: Distribution
Creates a uniform sampler from range [range_beg; range_end].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range_beg
|
int
|
begin of sampling range. Inclusive |
required |
range_end
|
int
|
end of sampling range. Inclusive |
required |
seed
|
int | None
|
seed for random number generator, by default None |
None
|
get_params ¶
returns dictionary of parameters of the distribution
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
dict |
sample_int ¶
Get uniform random int from range [self.beg_range, self.end_range]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
set the new seed, if None does nothing, by default None |
None
|
Returns:
| Type | Description |
|---|---|
int
|
random integer from range [self.beg_range, self.end_range] |
sample_matrix ¶
Get a matrix of random uniform ints.
Each element of the matrix is sampled from range [self.beg_range, self.end_range].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
number of rows |
required |
cols
|
int | None
|
number of cols, if None equal to number of rows , by default None |
None
|
seed
|
int | None
|
set the new seed, if None does nothing, by default None |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
matrix |
sample_vector ¶
Get a vector of random uniform ints.
Each element of the vector is sampled from range [self.beg_range, self.end_range].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
dimension of the vector |
required |
seed
|
int | None
|
set the new seed, if None does nothing, by default None |
None
|
Returns:
| Type | Description |
|---|---|
Vector
|
vector |
set_params ¶
Set the parameters of the distribution
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range_beg
|
int | None
|
if None does nothing, by default None |
None
|
range_end
|
int | None
|
if None does nothing, by default None |
None
|
pqlattice.random.distribution.DiscreteGaussian ¶
DiscreteGaussian(
sigma: float,
center: int | float = 0,
tail_cut: float = 6.0,
seed: int | None = None,
)
Bases: Distribution
Creates DiscreteGaussian sampler that uses rejection sampling method.
Samples x are accepted with probability `exp(-((x - center) ** 2) / (2 * sigma ** 2)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma
|
float
|
standard deviation of the distribution |
required |
center
|
int | float
|
mean of the distribution, by default 0 |
0
|
tail_cut
|
float
|
samples outside the range |
6.0
|
seed
|
int | None
|
seed for random number generator, by default None |
None
|
get_params ¶
returns dictionary of parameters of the distribution
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
dict |
sample_int ¶
Get random int according to the gaussian probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
set the new seed, if None does nothing, by default None |
None
|
Returns:
| Type | Description |
|---|---|
int
|
random integer sampled from discrete gaussian distribution |
sample_matrix ¶
Get a matrix of integers sampled from discrete gaussian distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
number of rows |
required |
cols
|
int | None
|
number of cols, if None equal to number of rows , by default None |
None
|
seed
|
int | None
|
set the new seed, if None does nothing, by default None |
None
|
Returns:
| Type | Description |
|---|---|
Matrix
|
matrix |
sample_vector ¶
Get a vector of integers sampled from discrete gaussian distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
dimension of the vector |
required |
seed
|
int | None
|
set the new seed, if None does nothing, by default None |
None
|
Returns:
| Type | Description |
|---|---|
Vector
|
vector |
set_params ¶
set_params(
sigma: float | None = None,
center: float | int | None = None,
tail_cut: float | None = None,
) -> None
Set the parameters of the distribution
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma
|
float | None
|
if None does nothing, by default None |
None
|
center
|
float | int | None
|
if None does nothing, by default None |
None
|
tail_cut
|
float | None
|
if None does nothing, by default None |
None
|
Learning with errors¶
pqlattice.random.LWE ¶
Creates LWE sampler with DiscreteGuassianDistribution centered at 0 as noise sampler
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
length of secret vector |
required |
q
|
int
|
modulus |
required |
sigma
|
float
|
sigma value for DiscreteGaussianDistribution |
required |
seed
|
int
|
seed for random number generator |
required |
secret
property
¶
Retrieve underlying secret
Returns:
| Type | Description |
|---|---|
Vector
|
s: n-vector |
next_sample ¶
Generates a single sample pair (a, b).
Returns:
| Type | Description |
|---|---|
tuple[Vector, int]
|
a (Vector): n-vector (Uniform mod q) b (int): as + e mod q |
sample_matrix ¶
Generates a full matrix system (A, b) with 'm' samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
how many samples should the resulting matrix have |
required |
Returns:
| Type | Description |
|---|---|
tuple[Matrix, Vector]:
|
A (Matrix): m x n matrix (Uniform mod q) b (Vector): m-vector (As + e mod q) |
set_secret ¶
Set the underlying secret
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
Vector
|
secret vector to set |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
when lenght of the provided vector is not correct with the parameter of the LWE sampler |
Miscellaneous¶
pqlattice.random.randlattice ¶
Generates lattice basis by, first generating random square matrix in Hermite normal form and then by transforming it using random unimodular matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
lattice's rank |
required |
det_upper_bound
|
int | None
|
upper bound of lattice volume, by default 2 ** n |
None
|
seed
|
int | None
|
seed for random number generator |
None
|
Returns:
| Type | Description |
|---|---|
SquareMatrix
|
n x n matrix representing lattice basis |
pqlattice.random.randprime ¶
Generates random prime number from range [2 ** (kbits - 1); 2 ** (kbist)]. Uses Miller-Rabin primality test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kbits
|
int
|
number of bits the prime number should have |
required |
seed
|
int | None
|
seed for random number generator, by default None |
None
|
Returns:
| Type | Description |
|---|---|
int
|
prime number |