Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/integer_mod_q/mat_zq/sample/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ impl MatZq {
p: impl Into<Q>,
) -> Result<Self, MathError> {
let offset: Z = offset.into();
let modulus: Modulus = modulus.into();
let mut bin_sampler = BinomialSampler::init(n, p)?;
let mut matrix = MatZq::new(num_rows, num_cols, modulus);
let mut matrix = MatZq::new(num_rows, num_cols, &modulus);

for row in 0..matrix.get_num_rows() {
for col in 0..matrix.get_num_columns() {
let mut sample = bin_sampler.sample();
sample += &offset;
sample = sample % &modulus;
unsafe { matrix.set_entry_unchecked(row, col, sample) };
}
}
Expand Down Expand Up @@ -143,6 +145,14 @@ mod test_sample_binomial {
}
}

/// Checks if entries are properly reduced.
#[test]
fn reduced() {
let matrix = MatZq::sample_binomial(1, 1, 3, 7, 0.5).unwrap();
let value: Z = matrix.get_entry(0, 0).unwrap();
assert!(Z::ZERO <= value && value <= 3)
}

/// Checks whether matrices with at least one dimension chosen smaller than `1`
/// or too big for an [`i64`] results in an error.
#[should_panic]
Expand Down
15 changes: 13 additions & 2 deletions src/integer_mod_q/mat_zq/sample/discrete_gauss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ impl MatZq {
center: impl Into<Q>,
s: impl Into<Q>,
) -> Result<MatZq, MathError> {
let modulus: Modulus = modulus.into();
let center: Q = center.into();
let s: Q = s.into();
let mut out = Self::new(num_rows, num_cols, modulus);
let mut out = Self::new(num_rows, num_cols, &modulus);

let mut dgis = DiscreteGaussianIntegerSampler::init(
&center,
Expand All @@ -70,7 +71,8 @@ impl MatZq {

for row in 0..out.get_num_rows() {
for col in 0..out.get_num_columns() {
let sample = dgis.sample_z();
let mut sample = dgis.sample_z();
sample = sample % &modulus;
unsafe { out.set_entry_unchecked(row, col, sample) };
}
}
Expand Down Expand Up @@ -195,10 +197,19 @@ mod test_sample_discrete_gauss {
integer::Z,
integer_mod_q::{MatZq, Modulus},
rational::Q,
traits::MatrixGetEntry,
};

// This function only allows for a broader availability, which is tested here.

/// Checks if entries are properly reduced.
#[test]
fn reduced() {
let matrix = MatZq::sample_discrete_gauss(1, 1, 3, 0, 15.0).unwrap();
let value: Z = matrix.get_entry(0, 0).unwrap();
assert!(Z::ZERO <= value && value <= 3)
}

/// 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, ...
Expand Down
Loading