use anyhow::Result; use itertools::zip_eq; use rand::distributions::Standard; use rand::Rng; use rand_distr::{Normal, Uniform}; use std::array; use std::iter::Sum; use std::ops::{Add, AddAssign, Mul, Sub}; use arith::{Ring, Rq, Tn, T64, TR}; use gfhe::{glwe, GLWE}; use crate::tlev::TLev; use crate::{tlwe, tlwe::TLWE}; // pub type SecretKey = glwe::SecretKey, K>; #[derive(Clone, Debug)] pub struct SecretKey(pub glwe::SecretKey, K>); // pub struct SecretKey(pub tlwe::SecretKey); impl SecretKey { pub fn to_tlwe(&self) -> tlwe::SecretKey { let s: TR, K> = self.0 .0.clone(); let r: Vec> = s.0.iter().map(|s_i| s_i.coeffs()).collect(); let r: Vec = r.into_iter().flatten().collect(); tlwe::SecretKey::(glwe::SecretKey::(TR::::new(r))) } } pub type PublicKey = glwe::PublicKey, K>; #[derive(Clone, Debug)] pub struct TGLWE(pub GLWE, K>); impl TGLWE { pub fn zero() -> Self { Self(GLWE::, K>::zero()) } pub fn from_plaintext(p: Tn) -> Self { Self(GLWE::, K>::from_plaintext(p)) } pub fn new_key( mut rng: impl Rng, ) -> Result<(SecretKey, PublicKey)> { // assert_eq!(KN, K * N); // this is wip, while not being able to compute K*N let (sk_tlwe, _) = TLWE::::new_key(&mut rng)?; // let sk = crate::tlwe::sk_to_tglwe::(sk_tlwe); let sk = sk_tlwe.to_tglwe::(); let pk: PublicKey = GLWE::pk_from_sk(rng, sk.0.clone())?; Ok((sk, pk)) } pub fn encode(m: &Rq) -> Tn { let delta = u64::MAX / P; // floored let coeffs = m.coeffs(); Tn(array::from_fn(|i| T64(coeffs[i].0 * delta))) } pub fn decode(p: &Tn) -> Rq { let p = p.mul_div_round(P, u64::MAX); Rq::::from_vec_u64(p.coeffs().iter().map(|c| c.0).collect()) } // encrypts with the given SecretKey (instead of PublicKey) pub fn encrypt_s(rng: impl Rng, sk: &SecretKey, p: &Tn) -> Result { let glwe = GLWE::encrypt_s(rng, &sk.0, p)?; Ok(Self(glwe)) } pub fn encrypt(rng: impl Rng, pk: &PublicKey, p: &Tn) -> Result { let glwe = GLWE::encrypt(rng, &pk, p)?; Ok(Self(glwe)) } pub fn decrypt(&self, sk: &SecretKey) -> Tn { self.0.decrypt(&sk.0) } /// Sample extraction / Coefficient extraction pub fn sample_extraction(&self, h: usize) -> TLWE { assert!(h < N); let a: TR, K> = self.0 .0.clone(); // set a_{n*i+j} = a_{i, h-j} if j \in {0, h} // -a_{i, n+h-j} if j \in {h+1, n-1} let new_a: Vec = a .iter() .flat_map(|a_i| { let a_i = a_i.coeffs(); (0..N) .map(|j| if j <= h { a_i[h - j] } else { -a_i[N + h - j] }) .collect::>() }) .collect::>(); TLWE(GLWE(TR(new_a), self.0 .1.coeffs()[h])) } pub fn left_rotate(&self, h: usize) -> Self { dbg!(&h); let (a, b): (TR, K>, Tn) = (self.0 .0.clone(), self.0 .1); Self(GLWE(a.left_rotate(h), b.left_rotate(h))) } } impl Add> for TGLWE { type Output = Self; fn add(self, other: Self) -> Self { Self(self.0 + other.0) } } impl AddAssign for TGLWE { fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0 } } impl Sum> for TGLWE { fn sum(iter: I) -> Self where I: Iterator, { let mut acc = TGLWE::::zero(); for e in iter { acc += e; } acc } } impl Sub> for TGLWE { type Output = Self; fn sub(self, other: Self) -> Self { Self(self.0 - other.0) } } // plaintext addition impl Add> for TGLWE { type Output = Self; fn add(self, plaintext: Tn) -> Self { let a: TR, K> = self.0 .0; let b: Tn = self.0 .1 + plaintext; Self(GLWE(a, b)) } } // plaintext substraction impl Sub> for TGLWE { type Output = Self; fn sub(self, plaintext: Tn) -> Self { let a: TR, K> = self.0 .0; let b: Tn = self.0 .1 - plaintext; Self(GLWE(a, b)) } } // plaintext multiplication impl Mul> for TGLWE { type Output = Self; fn mul(self, plaintext: Tn) -> Self { let a: TR, K> = TR(self.0 .0 .0.iter().map(|r_i| *r_i * plaintext).collect()); let b: Tn = self.0 .1 * plaintext; Self(GLWE(a, b)) } } #[cfg(test)] mod tests { use anyhow::Result; use rand::distributions::Uniform; use super::*; #[test] fn test_encrypt_decrypt() -> Result<()> { const T: u64 = 128; // msg space (msg modulus) const N: usize = 64; const K: usize = 16; type S = TGLWE; let mut rng = rand::thread_rng(); let msg_dist = Uniform::new(0_u64, T); for _ in 0..200 { let (sk, pk) = TGLWE::::new_key::<{ K * N }>(&mut rng)?; let m = Rq::::rand_u64(&mut rng, msg_dist)?; let p: Tn = S::encode::(&m); let c = S::encrypt(&mut rng, &pk, &p)?; let p_recovered = c.decrypt(&sk); let m_recovered = S::decode::(&p_recovered); assert_eq!(m, m_recovered); // same but using encrypt_s (with sk instead of pk)) let c = S::encrypt_s(&mut rng, &sk, &p)?; let p_recovered = c.decrypt(&sk); let m_recovered = S::decode::(&p_recovered); assert_eq!(m, m_recovered); } Ok(()) } #[test] fn test_addition() -> Result<()> { const T: u64 = 128; const N: usize = 64; const K: usize = 16; type S = TGLWE; let mut rng = rand::thread_rng(); let msg_dist = Uniform::new(0_u64, T); for _ in 0..200 { let (sk, pk) = S::new_key::<{ K * N }>(&mut rng)?; let m1 = Rq::::rand_u64(&mut rng, msg_dist)?; let m2 = Rq::::rand_u64(&mut rng, msg_dist)?; let p1: Tn = S::encode::(&m1); // plaintext let p2: Tn = S::encode::(&m2); // plaintext let c1 = S::encrypt(&mut rng, &pk, &p1)?; let c2 = S::encrypt(&mut rng, &pk, &p2)?; let c3 = c1 + c2; let p3_recovered = c3.decrypt(&sk); let m3_recovered = S::decode::(&p3_recovered); assert_eq!((m1 + m2).remodule::(), m3_recovered.remodule::()); } Ok(()) } #[test] fn test_add_plaintext() -> Result<()> { const T: u64 = 128; const N: usize = 64; const K: usize = 16; type S = TGLWE; let mut rng = rand::thread_rng(); let msg_dist = Uniform::new(0_u64, T); for _ in 0..200 { let (sk, pk) = S::new_key::<{ K * N }>(&mut rng)?; let m1 = Rq::::rand_u64(&mut rng, msg_dist)?; let m2 = Rq::::rand_u64(&mut rng, msg_dist)?; let p1: Tn = S::encode::(&m1); // plaintext let p2: Tn = S::encode::(&m2); // plaintext let c1 = S::encrypt(&mut rng, &pk, &p1)?; let c3 = c1 + p2; let p3_recovered = c3.decrypt(&sk); let m3_recovered = S::decode::(&p3_recovered); assert_eq!(m1 + m2, m3_recovered); } Ok(()) } #[test] fn test_mul_plaintext() -> Result<()> { const T: u64 = 128; const N: usize = 64; const K: usize = 16; type S = TGLWE; let mut rng = rand::thread_rng(); let msg_dist = Uniform::new(0_u64, T); for _ in 0..200 { let (sk, pk) = S::new_key::<{ K * N }>(&mut rng)?; let m1 = Rq::::rand_u64(&mut rng, msg_dist)?; let m2 = Rq::::rand_u64(&mut rng, msg_dist)?; let p1: Tn = S::encode::(&m1); // don't scale up p2, set it directly from m2 let p2: Tn = Tn(array::from_fn(|i| T64(m2.coeffs()[i].0))); let c1 = S::encrypt(&mut rng, &pk, &p1)?; let c3 = c1 * p2; let p3_recovered: Tn = c3.decrypt(&sk); let m3_recovered = S::decode::(&p3_recovered); assert_eq!((m1.to_r() * m2.to_r()).to_rq::(), m3_recovered); } Ok(()) } #[test] fn test_sample_extraction() -> Result<()> { const T: u64 = 128; // msg space (msg modulus) const N: usize = 64; const K: usize = 16; const KN: usize = K * N; let mut rng = rand::thread_rng(); let msg_dist = Uniform::new(0_u64, T); for _ in 0..20 { let (sk, pk) = TGLWE::::new_key::(&mut rng)?; let sk_tlwe = sk.to_tlwe::(); let m = Rq::::rand_u64(&mut rng, msg_dist)?; let p: Tn = TGLWE::::encode::(&m); let c = TGLWE::::encrypt(&mut rng, &pk, &p)?; for h in 0..N { let c_h: TLWE = c.sample_extraction(h); let p_recovered = c_h.decrypt(&sk_tlwe); let m_recovered = TLWE::::decode::(&p_recovered); assert_eq!(m.coeffs()[h], m_recovered.coeffs()[0]); } } Ok(()) } }