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
// 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 binomial distribution.
use crate::{
error::MathError,
integer::Z,
integer_mod_q::{Modulus, PolyOverZq},
rational::Q,
traits::SetCoefficient,
utils::{index::evaluate_index, sample::binomial::BinomialSampler},
};
use std::fmt::Display;
impl PolyOverZq {
/// Generates a [`PolyOverZq`] instance of maximum degree `max_degree` and
/// coefficients chosen according to the binomial distribution
/// parameterized by `n` and `p`.
///
/// Parameters:
/// - `max_degree`: specifies the length of the polynomial,
/// i.e. the number of coefficients
/// - `modulus`: specifies the [`Modulus`] of the new [`PolyOverZq`] instance
/// - `n`: specifies the number of trials
/// - `p`: specifies the probability of success
///
/// Returns a fresh [`PolyOverZq`] instance with each value sampled
/// according to the binomial distribution or a [`MathError`]
/// if `n < 0`, `p ∉ (0,1)`, `n` does not fit into an [`i64`],
/// or `max_degree` is negative or does not into an [`i64`].
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::PolyOverZq;
///
/// let sample = PolyOverZq::sample_binomial(2, 7, 2, 0.5).unwrap();
/// ```
///
/// # Errors and Failures
/// - Returns a [`MathError`] of type [`InvalidIntegerInput`](MathError::InvalidIntegerInput)
/// if `n < 0`.
/// - Returns a [`MathError`] of type [`InvalidInterval`](MathError::InvalidInterval)
/// if `p ∉ (0,1)`.
/// - Returns a [`MathError`] of type [`ConversionError`](MathError::ConversionError)
/// if `n` does not fit into an [`i64`].
/// - Returns a [`MathError`] of type [`OutOfBounds`](MathError::OutOfBounds) if
/// the `max_degree` is negative or it does not fit into an [`i64`].
pub fn sample_binomial(
max_degree: impl TryInto<i64> + Display,
modulus: impl Into<Modulus>,
n: impl Into<Z>,
p: impl Into<Q>,
) -> Result<Self, MathError> {
Self::sample_binomial_with_offset(max_degree, 0, modulus, n, p)
}
/// Generates a [`PolyOverZq`] instance of maximum degree `max_degree` and
/// coefficients chosen according to the binomial distribution
/// parameterized by `n` and `p` with given `offset`.
///
/// Parameters:
/// - `max_degree`: specifies the length of the polynomial,
/// i.e. the number of coefficients
/// - `offset`: specifies an offset applied to each sample
/// collected from the binomial distribution
/// - `modulus`: specifies the [`Modulus`] of the new [`PolyOverZq`] instance
/// - `n`: specifies the number of trials
/// - `p`: specifies the probability of success
///
/// Returns a fresh [`PolyOverZq`] instance with each value sampled
/// according to the binomial distribution or a [`MathError`]
/// if `n < 0`, `p ∉ (0,1)`, `n` does not fit into an [`i64`],
/// or `max_degree` is negative or does not into an [`i64`].
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::PolyOverZq;
///
/// let sample = PolyOverZq::sample_binomial_with_offset(2, -1, 7, 2, 0.5).unwrap();
/// ```
///
/// # Errors and Failures
/// - Returns a [`MathError`] of type [`InvalidIntegerInput`](MathError::InvalidIntegerInput)
/// if `n < 0`.
/// - Returns a [`MathError`] of type [`InvalidInterval`](MathError::InvalidInterval)
/// if `p ∉ (0,1)`.
/// - Returns a [`MathError`] of type [`ConversionError`](MathError::ConversionError)
/// if `n` does not fit into an [`i64`].
/// - Returns a [`MathError`] of type [`OutOfBounds`](MathError::OutOfBounds) if
/// the `max_degree` is negative or it does not fit into an [`i64`].
///
/// # Panics ...
/// - if `modulus` is smaller than `2`.
pub fn sample_binomial_with_offset(
max_degree: impl TryInto<i64> + Display,
offset: impl Into<Z>,
modulus: impl Into<Modulus>,
n: impl Into<Z>,
p: impl Into<Q>,
) -> Result<Self, MathError> {
let max_degree = evaluate_index(max_degree)?;
let offset: Z = offset.into();
let modulus: Modulus = modulus.into();
let mut bin_sampler = BinomialSampler::init(n, p)?;
let mut poly_z = PolyOverZq::from(&modulus);
for index in 0..=max_degree {
let mut sample = bin_sampler.sample();
sample += &offset;
unsafe { poly_z.set_coeff_unchecked(index, sample) };
}
Ok(poly_z)
}
}
#[cfg(test)]
mod test_sample_binomial {
use super::{PolyOverZq, Q, Z};
use crate::traits::GetCoefficient;
// As all major tests regarding an appropriate binomial distribution,
// whether the correct interval is kept, and if the errors are thrown correctly,
// are performed in the `utils` module, we omit these tests here.
/// Checks whether the boundaries of the interval are kept.
#[test]
fn boundaries_kept() {
for _ in 0..8 {
let poly = PolyOverZq::sample_binomial(0, 7, 2, 0.5).unwrap();
let sample: Z = poly.get_coeff(0).unwrap();
assert!(Z::ZERO <= sample || sample <= 2);
}
}
/// Checks whether the number of coefficients is correct.
#[test]
fn nr_coeffs() {
let degrees = [1, 3, 7, 15, 32, 120];
for degree in degrees {
let res = PolyOverZq::sample_binomial(degree, u64::MAX, 256, 0.99999).unwrap();
assert_eq!(
degree,
res.get_degree(),
"This test can fail with probability close to 0."
);
}
}
/// Checks whether providing a length smaller than `0` results in an error.
#[test]
fn invalid_max_degree() {
let res_0 = PolyOverZq::sample_binomial(-1, 7, 2, 0.5);
let res_1 = PolyOverZq::sample_binomial(i64::MIN, 7, 2, 0.5);
assert!(res_0.is_err());
assert!(res_1.is_err());
}
/// Checks whether `sample_binomial` is available for all types
/// implementing [`Into<Z>`], i.e. u8, u16, u32, u64, i8, ...
/// and [`Into<Q>`], i.e. u8, u16, i8, i16, f32, f64, ...
#[test]
fn availability() {
let _ = PolyOverZq::sample_binomial(1, 7u8, 1u16, 7u8);
let _ = PolyOverZq::sample_binomial(1, 7u16, 1u32, 7u16);
let _ = PolyOverZq::sample_binomial(1, 7u32, 1u64, 7u32);
let _ = PolyOverZq::sample_binomial(1, 7u64, 1i8, 7u64);
let _ = PolyOverZq::sample_binomial(1, 7i8, 1i16, 7i8);
let _ = PolyOverZq::sample_binomial(1, 7i16, 1i32, 7i16);
let _ = PolyOverZq::sample_binomial(1, 7i32, 1i64, 7i32);
let _ = PolyOverZq::sample_binomial(1, 7i64, Z::ONE, 7i64);
let _ = PolyOverZq::sample_binomial(1, 7, 1u8, 0.5f32);
let _ = PolyOverZq::sample_binomial(1, 7, 1u8, 0.5f64);
let _ = PolyOverZq::sample_binomial(1, 7, 1, Q::from((1, 2)));
}
}
#[cfg(test)]
mod test_sample_binomial_with_offset {
use super::{PolyOverZq, Q, Z};
use crate::traits::GetCoefficient;
// As all major tests regarding an appropriate binomial distribution,
// whether the correct interval is kept, and if the errors are thrown correctly,
// are performed in the `utils` module, we omit these tests here.
/// Checks whether the boundaries of the interval are kept.
#[test]
fn boundaries_kept() {
for _ in 0..8 {
let poly = PolyOverZq::sample_binomial_with_offset(0, -1, 7, 2, 0.5).unwrap();
let sample: Z = poly.get_coeff(0).unwrap();
assert!(Z::MINUS_ONE <= sample || sample <= Z::ONE);
}
}
/// Checks whether the number of coefficients is correct.
#[test]
fn nr_coeffs() {
let degrees = [1, 3, 7, 15, 32, 120];
for degree in degrees {
let res = PolyOverZq::sample_binomial_with_offset(degree, 1, 7, 2, 0.5).unwrap();
assert_eq!(degree, res.get_degree());
}
}
/// Checks whether providing a length smaller than `0` results in an error.
#[test]
fn invalid_max_degree() {
let res_0 = PolyOverZq::sample_binomial_with_offset(-1, -1, 7, 2, 0.5);
let res_1 = PolyOverZq::sample_binomial_with_offset(i64::MIN, -1, 7, 2, 0.5);
assert!(res_0.is_err());
assert!(res_1.is_err());
}
/// Checks whether `sample_binomial_with_offset` is available for all types
/// implementing [`Into<Z>`], i.e. u8, u16, u32, u64, i8, ...
/// and [`Into<Q>`], i.e. u8, u16, i8, i16, f32, f64, ...
#[test]
fn availability() {
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7u8, 1u16, 7u8);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7u16, 1u32, 7u16);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7u32, 1u64, 7u32);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7u64, 1i8, 7u64);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7i8, 1i16, 7i8);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7i16, 1i32, 7i16);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7i32, 1i64, 7i32);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7i64, Z::ONE, 7i64);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7, 1u8, 0.5f32);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7, 1u8, 0.5f64);
let _ = PolyOverZq::sample_binomial_with_offset(1, 0, 7, 1, Q::from((1, 2)));
}
}