Skip to content

Commit

Permalink
Merge pull request #5 from lattice-based-cryptography/remove-floats
Browse files Browse the repository at this point in the history
remove floats
  • Loading branch information
tjaysilver authored Jan 6, 2025
2 parents 495baa7 + 3253576 commit 62b2b36
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/decrypt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use polynomial_ring::Polynomial;
use module_lwe::{Parameters,mul_vec_simple};
use module_lwe::ring_mod::polysub;
use module_lwe::ring_mod::{polysub,nearest_int};

pub fn decrypt(
sk: &Vec<Polynomial<i64>>, //secret key
Expand All @@ -14,15 +14,11 @@ pub fn decrypt(

//Compute v-sk*u mod q
let scaled_pt = polysub(&v, &mul_vec_simple(&sk, &u, q, &f), q, &f);
let half_q = (q as f64 / 2.0 + 0.5) as i64;
let half_q = nearest_int(q,2);
let mut decrypted_coeffs = vec![];
let mut s;
for c in scaled_pt.coeffs().iter() {
if (half_q-c).abs() < std::cmp::min(*c, (q-c).abs()) {
s = 1;
} else {
s = 0;
};
s = nearest_int(*c,half_q) % 2;
decrypted_coeffs.push(s);
}
decrypted_coeffs
Expand Down
4 changes: 2 additions & 2 deletions src/encrypt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use polynomial_ring::Polynomial;
use module_lwe::ring_mod::{polyadd,polysub};
use module_lwe::ring_mod::{polyadd,polysub,nearest_int};
use module_lwe::{Parameters, add_vec, mul_mat_vec_simple, transpose, mul_vec_simple, gen_small_vector};

pub fn encrypt(
Expand All @@ -19,7 +19,7 @@ pub fn encrypt(
let e2 = gen_small_vector(n, 1, seed)[0].clone(); // Single polynomial

//compute nearest integer to q/2
let half_q = (q as f64 / 2.0 + 0.5) as i64;
let half_q = nearest_int(q,2);

// Convert binary message to polynomial
let m = Polynomial::new(vec![half_q])*Polynomial::new(m_b);
Expand Down
4 changes: 4 additions & 0 deletions src/ring_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ pub fn gen_uniform_poly(size: usize, q: i64, seed: Option<u64>) -> Polynomial<i6
coeffs[i] = between.sample(&mut rng);
}
Polynomial::new(coeffs)
}

pub fn nearest_int(a: i64, b: i64) -> i64 {
(a + b / 2) / b
}

0 comments on commit 62b2b36

Please sign in to comment.