1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright © 2023 Niklas Siemer
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! This module contains algorithms for sampling according to the discrete Gaussian distribution.
use crate::{
error::MathError,
integer_mod_q::{MatZq, Modulus},
rational::{MatQ, Q},
traits::{MatrixDimensions, MatrixSetEntry},
utils::sample::discrete_gauss::{
DiscreteGaussianIntegerSampler, LookupTableSetting, TAILCUT, sample_d,
sample_d_precomputed_gso,
},
};
use std::fmt::Display;
impl MatZq {
/// Initializes a new matrix with dimensions `num_rows` x `num_columns` and with each entry
/// sampled independently according to the discrete Gaussian distribution,
/// using [`Z::sample_discrete_gauss`](crate::integer::Z::sample_discrete_gauss).
///
/// Parameters:
/// - `num_rows`: specifies the number of rows the new matrix should have
/// - `num_cols`: specifies the number of columns the new matrix should have
/// - `center`: specifies the positions of the center with peak probability
/// - `s`: specifies the Gaussian parameter, which is proportional
/// to the standard deviation `sigma * sqrt(2 * pi) = s`
///
/// Returns a matrix with each entry sampled independently from the
/// specified discrete Gaussian distribution or an error if `s < 0`.
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::MatZq;
///
/// let sample = MatZq::sample_discrete_gauss(3, 1, 83, 0, 1.25f32).unwrap();
/// ```
///
/// # Errors and Failures
/// - Returns a [`MathError`] of type [`InvalidIntegerInput`](MathError::InvalidIntegerInput)
/// if `s < 0`.
///
/// # Panics ...
/// - if the provided number of rows and columns or the modulus are not suited to create a matrix.
/// For further information see [`MatZq::new`].
/// - if the provided `modulus < 2`.
pub fn sample_discrete_gauss(
num_rows: impl TryInto<i64> + Display,
num_cols: impl TryInto<i64> + Display,
modulus: impl Into<Modulus>,
center: impl Into<Q>,
s: impl Into<Q>,
) -> Result<MatZq, MathError> {
let center: Q = center.into();
let s: Q = s.into();
let mut out = Self::new(num_rows, num_cols, modulus);
let mut dgis = DiscreteGaussianIntegerSampler::init(
¢er,
&s,
unsafe { TAILCUT },
LookupTableSetting::FillOnTheFly,
)?;
for row in 0..out.get_num_rows() {
for col in 0..out.get_num_columns() {
let sample = dgis.sample_z();
unsafe { out.set_entry_unchecked(row, col, sample) };
}
}
Ok(out)
}
/// SampleD samples a discrete Gaussian from the lattice with a provided `basis`.
///
/// We do not check whether `basis` is actually a basis. Hence, the callee is
/// responsible for making sure that `basis` provides a suitable basis.
///
/// Parameters:
/// - `basis`: specifies a basis for the lattice from which is sampled
/// - `center`: specifies the positions of the center with peak probability
/// - `s`: specifies the Gaussian parameter, which is proportional
/// to the standard deviation `sigma * sqrt(2 * pi) = s`
///
/// Returns a lattice vector sampled according to the discrete Gaussian distribution
/// or an error if `s < 0`, the number of rows of the `basis` and `center` differ,
/// or if `center` is not a column vector.
///
/// # Examples
/// ```
/// use qfall_math::{integer_mod_q::MatZq, rational::MatQ};
/// let basis = MatZq::identity(5, 5, 17);
/// let center = MatQ::new(5, 1);
///
/// let sample = MatZq::sample_d(&basis, ¢er, 1.25f32).unwrap();
/// ```
///
/// # Errors and Failures
/// - Returns a [`MathError`] of type [`InvalidIntegerInput`](MathError::InvalidIntegerInput)
/// if `s < 0`.
/// - Returns a [`MathError`] of type [`MismatchingMatrixDimension`](MathError::MismatchingMatrixDimension)
/// if the number of rows of the `basis` and `center` differ.
/// - Returns a [`MathError`] of type [`StringConversionError`](MathError::StringConversionError)
/// if `center` is not a column vector.
///
/// This function implements SampleD according to:
/// - \[1\] Gentry, Craig and Peikert, Chris and Vaikuntanathan, Vinod (2008).
/// Trapdoors for hard lattices and new cryptographic constructions.
/// In: Proceedings of the fortieth annual ACM symposium on Theory of computing.
/// <https://dl.acm.org/doi/pdf/10.1145/1374376.1374407>
pub fn sample_d(basis: &MatZq, center: &MatQ, s: impl Into<Q>) -> Result<Self, MathError> {
let s: Q = s.into();
let sample = sample_d(
&basis.get_representative_least_nonnegative_residue(),
center,
&s,
)?;
Ok(MatZq::from((&sample, basis.get_mod())))
}
/// SampleD samples a discrete Gaussian from the lattice with a provided `basis`.
///
/// We do not check whether `basis` is actually a basis or whether `basis_gso` is
/// actually the gso of `basis`. Hence, the callee is responsible for making sure
/// that `basis` provides a suitable basis and `basis_gso` is a corresponding GSO.
///
/// Parameters:
/// - `basis`: specifies a basis for the lattice from which is sampled
/// - `basis_gso`: specifies the precomputed gso for `basis`
/// - `center`: specifies the positions of the center with peak probability
/// - `s`: specifies the Gaussian parameter, which is proportional
/// to the standard deviation `sigma * sqrt(2 * pi) = s`
///
/// Returns a lattice vector sampled according to the discrete Gaussian distribution
/// or an error if `s < 0`, the number of rows of the `basis` and `center` differ,
/// or if `center` is not a column vector.
///
/// # Examples
/// ```
/// use qfall_math::{integer::MatZ, integer_mod_q::MatZq, rational::MatQ};
/// let basis = MatZq::identity(5, 5, 17);
/// let center = MatQ::new(5, 1);
/// let basis_gso = MatQ::from(&basis.get_representative_least_nonnegative_residue()).gso();
///
/// let sample = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1.25f32).unwrap();
/// ```
///
/// # Errors and Failures
/// - Returns a [`MathError`] of type [`InvalidIntegerInput`](MathError::InvalidIntegerInput)
/// if `s < 0`.
/// - Returns a [`MathError`] of type [`MismatchingMatrixDimension`](MathError::MismatchingMatrixDimension)
/// if the number of rows of the `basis` and `center` differ.
/// - Returns a [`MathError`] of type [`StringConversionError`](MathError::StringConversionError)
/// if `center` is not a column vector.
///
/// # Panics ...
/// - if the number of rows/columns of `basis_gso` and `basis` mismatch.
///
/// This function implements SampleD according to:
/// - \[1\] Gentry, Craig and Peikert, Chris and Vaikuntanathan, Vinod (2008).
/// Trapdoors for hard lattices and new cryptographic constructions.
/// In: Proceedings of the fortieth annual ACM symposium on Theory of computing.
/// <https://dl.acm.org/doi/pdf/10.1145/1374376.1374407>
pub fn sample_d_precomputed_gso(
basis: &MatZq,
basis_gso: &MatQ,
center: &MatQ,
s: impl Into<Q>,
) -> Result<Self, MathError> {
let s: Q = s.into();
let sample = sample_d_precomputed_gso(
&basis.get_representative_least_nonnegative_residue(),
basis_gso,
center,
&s,
)?;
Ok(MatZq::from((&sample, basis.get_mod())))
}
}
#[cfg(test)]
mod test_sample_discrete_gauss {
use crate::{
integer::Z,
integer_mod_q::{MatZq, Modulus},
rational::Q,
};
// This function only allows for a broader availability, which is tested here.
/// Checks whether `sample_discrete_gauss` is available for all types
/// implementing [`Into<Z>`], i.e. u8, u16, u32, u64, i8, ...
/// or [`Into<Q>`], i.e. u8, i16, f32, Z, Q, ...
#[test]
fn availability() {
let n = Z::from(1024);
let center = Q::ZERO;
let s = Q::ONE;
let modulus = Modulus::from(83);
let _ = MatZq::sample_discrete_gauss(2u64, 3i8, &modulus, 0f32, 1u16);
let _ = MatZq::sample_discrete_gauss(3u8, 2i16, 83u8, ¢er, 1u8);
let _ = MatZq::sample_discrete_gauss(1, 1, &n, ¢er, 1u32);
let _ = MatZq::sample_discrete_gauss(1, 1, 83i8, ¢er, 1u64);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, 1i64);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, 1i32);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, 1i16);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, 1i8);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, 1i64);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, &n);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, &s);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, ¢er, 1.25f64);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, 0, 1.25f64);
let _ = MatZq::sample_discrete_gauss(1, 1, 83, center, 15.75f32);
}
}
#[cfg(test)]
mod test_sample_d {
use crate::{
integer::Z,
integer_mod_q::MatZq,
rational::{MatQ, Q},
};
// Appropriate inputs were tested in utils and thus omitted here.
// This function only allows for a broader availability, which is tested here.
/// Checks whether `sample_d` is available for all types
/// implementing [`Into<Z>`], i.e. u8, u16, u32, u64, i8, ...
/// or [`Into<Q>`], i.e. u8, i16, f32, Z, Q, ...
#[test]
fn availability() {
let basis = MatZq::identity(5, 5, 17);
let n = Z::from(1024);
let center = MatQ::new(5, 1);
let s = Q::ONE;
let _ = MatZq::sample_d(&basis, ¢er, 1u16);
let _ = MatZq::sample_d(&basis, ¢er, 1u8);
let _ = MatZq::sample_d(&basis, ¢er, 1u32);
let _ = MatZq::sample_d(&basis, ¢er, 1u64);
let _ = MatZq::sample_d(&basis, ¢er, 1i64);
let _ = MatZq::sample_d(&basis, ¢er, 1i32);
let _ = MatZq::sample_d(&basis, ¢er, 1i16);
let _ = MatZq::sample_d(&basis, ¢er, 1i8);
let _ = MatZq::sample_d(&basis, ¢er, 1i64);
let _ = MatZq::sample_d(&basis, ¢er, &n);
let _ = MatZq::sample_d(&basis, ¢er, &s);
let _ = MatZq::sample_d(&basis, ¢er, 1.25f64);
let _ = MatZq::sample_d(&basis, ¢er, 15.75f32);
}
/// Checks whether `sample_d_precomputed_gso` is available for all types
/// implementing [`Into<Z>`], i.e. u8, u16, u32, u64, i8, ...
/// or [`Into<Q>`], i.e. u8, i16, f32, Z, Q, ...
#[test]
fn availability_prec_gso() {
let basis = MatZq::identity(5, 5, 17);
let n = Z::from(1024);
let center = MatQ::new(5, 1);
let s = Q::ONE;
let basis_gso = MatQ::from(&basis.get_representative_least_nonnegative_residue());
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1u16);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1u8);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1u32);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1u64);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1i64);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1i32);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1i16);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1i8);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1i64);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, &n);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, &s);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 1.25f64);
let _ = MatZq::sample_d_precomputed_gso(&basis, &basis_gso, ¢er, 15.75f32);
}
}