mirror of
https://github.com/arnaucube/fhe-study.git
synced 2026-01-24 04:33:52 +01:00
add BFV newkey, encrypt, decrypt, and homomorphic addition impl
This commit is contained in:
@@ -136,7 +136,7 @@ mod tests {
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
let uniform_distr = Uniform::new(0_f64, Q as f64);
|
||||
let a = PR::<Q, N>::rand(&mut rng, uniform_distr)?;
|
||||
let a = PR::<Q, N>::rand_f64(&mut rng, uniform_distr)?;
|
||||
// let a = PR::<Q, N>::new_from_u64(vec![36, 21, 9, 19]);
|
||||
|
||||
// let a_padded_coeffs: [Zq<Q>; 2 * N] =
|
||||
@@ -181,7 +181,7 @@ mod tests {
|
||||
let ntt = NTT::<Q, N>::new()?;
|
||||
|
||||
let rng = rand::thread_rng();
|
||||
let a = PR::<Q, { 2 * N }>::rand(rng, Uniform::new(0_f64, (Q - 1) as f64))?;
|
||||
let a = PR::<Q, { 2 * N }>::rand_f64(rng, Uniform::new(0_f64, (Q - 1) as f64))?;
|
||||
let a = a.coeffs;
|
||||
dbg!(&a);
|
||||
let a_ntt = matrix_vec_product(&ntt.ntt, &a.to_vec())?;
|
||||
@@ -189,6 +189,7 @@ mod tests {
|
||||
let a_intt = matrix_vec_product(&ntt.intt, &a_ntt)?;
|
||||
dbg!(&a_intt);
|
||||
assert_eq!(a_intt, a);
|
||||
// TODO bench
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Implementation of the NTT & iNTT, following the CT & GS algorighms, more
|
||||
//! details in https://github.com/arnaucube/math/blob/master/notes_ntt.pdf .
|
||||
//! Implementation of the NTT & iNTT, following the CT & GS algorighms, more details in
|
||||
//! https://eprint.iacr.org/2017/727.pdf, some notes at
|
||||
//! https://github.com/arnaucube/math/blob/master/notes_ntt.pdf .
|
||||
use crate::zq::Zq;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -14,7 +15,8 @@ impl<const Q: u64, const N: usize> NTT<Q, N> {
|
||||
}
|
||||
|
||||
impl<const Q: u64, const N: usize> NTT<Q, N> {
|
||||
/// implements the Cooley-Tukey (CT) algorithm. Details at section 3.1 of
|
||||
/// implements the Cooley-Tukey (CT) algorithm. Details at
|
||||
/// https://eprint.iacr.org/2017/727.pdf, also some notes at section 3.1 of
|
||||
/// https://github.com/arnaucube/math/blob/master/notes_ntt.pdf
|
||||
pub fn ntt(a: [Zq<Q>; N]) -> [Zq<Q>; N] {
|
||||
let mut t = N / 2;
|
||||
@@ -38,7 +40,8 @@ impl<const Q: u64, const N: usize> NTT<Q, N> {
|
||||
r
|
||||
}
|
||||
|
||||
/// implements the Gentleman-Sande (GS) algorithm. Details at section 3.2 of
|
||||
/// implements the Cooley-Tukey (CT) algorithm. Details at
|
||||
/// https://eprint.iacr.org/2017/727.pdf, also some notes at section 3.2 of
|
||||
/// https://github.com/arnaucube/math/blob/master/notes_ntt.pdf
|
||||
pub fn intt(a: [Zq<Q>; N]) -> [Zq<Q>; N] {
|
||||
let mut t = 1;
|
||||
|
||||
@@ -63,13 +63,20 @@ impl<const Q: u64, const N: usize> PR<Q, N> {
|
||||
evals: None,
|
||||
})
|
||||
}
|
||||
pub fn rand(mut rng: impl Rng, dist: impl Distribution<f64>) -> Result<Self> {
|
||||
pub fn rand_f64(mut rng: impl Rng, dist: impl Distribution<f64>) -> Result<Self> {
|
||||
let coeffs: [Zq<Q>; N] = array::from_fn(|_| Zq::from_f64(dist.sample(&mut rng)));
|
||||
Ok(Self {
|
||||
coeffs,
|
||||
evals: None,
|
||||
})
|
||||
}
|
||||
pub fn rand_u64(mut rng: impl Rng, dist: impl Distribution<u64>) -> Result<Self> {
|
||||
let coeffs: [Zq<Q>; N] = array::from_fn(|_| Zq::new(dist.sample(&mut rng)));
|
||||
Ok(Self {
|
||||
coeffs,
|
||||
evals: None,
|
||||
})
|
||||
}
|
||||
// WIP. returns random v \in {0,1}. // TODO {-1, 0, 1}
|
||||
pub fn rand_bin(mut rng: impl Rng, dist: impl Distribution<bool>) -> Result<Self> {
|
||||
let coeffs: [Zq<Q>; N] = array::from_fn(|_| Zq::from_bool(dist.sample(&mut rng)));
|
||||
|
||||
Reference in New Issue
Block a user