use anyhow::Result; use itertools::zip_eq; use rand::Rng; use std::array; use std::ops::{Add, Mul}; use arith::{Ring, Rq, Tn, T64, TR}; use crate::tglwe::{PublicKey, SecretKey, TGLWE}; use gfhe::glwe::GLWE; /// vector of length K+1 = ([K * TGLev], [1 * TGLev]) #[derive(Clone, Debug)] pub struct TGGSW(pub(crate) Vec>, TGLev); impl TGGSW { pub fn encrypt_s( mut rng: impl Rng, beta: u32, l: u32, sk: &SecretKey, m: &Tn, ) -> Result { let a: Vec> = (0..K) .map(|i| TGLev::encrypt_s(&mut rng, beta, l, sk, &(-sk.0 .0 .0[i] * *m))) .collect::>>()?; let b: TGLev = TGLev::encrypt_s(&mut rng, beta, l, sk, m)?; Ok(Self(a, b)) } pub fn decrypt(&self, sk: &SecretKey, beta: u32) -> Tn { self.1.decrypt(sk, beta) } pub fn cmux(bit: Self, ct1: TGLWE, ct2: TGLWE) -> TGLWE { ct1.clone() + (bit * (ct2 - ct1)) } } /// External product TGGSW x TGLWE impl Mul> for TGGSW { type Output = TGLWE; fn mul(self, tglwe: TGLWE) -> TGLWE { let beta: u32 = 2; let l: u32 = 64; // TODO wip let tglwe_ab: Vec> = [tglwe.0 .0 .0.clone(), vec![tglwe.0 .1]].concat(); let tgsw_ab: Vec> = [self.0.clone(), vec![self.1]].concat(); assert_eq!(tgsw_ab.len(), tglwe_ab.len()); let r: TGLWE = zip_eq(tgsw_ab, tglwe_ab) .map(|(tlev_i, tglwe_i)| tlev_i * tglwe_i.decompose(beta, l)) .sum(); r } } #[derive(Clone, Debug)] pub struct TGLev(pub(crate) Vec>); impl TGLev { pub fn encode(m: &Rq) -> Tn { let coeffs = m.coeffs(); Tn(array::from_fn(|i| T64(coeffs[i].0))) } pub fn decode(p: &Tn) -> Rq { Rq::::from_vec_u64(p.coeffs().iter().map(|c| c.0).collect()) } pub fn encrypt( mut rng: impl Rng, beta: u32, l: u32, pk: &PublicKey, m: &Tn, ) -> Result { let tlev: Vec> = (1..l + 1) .map(|i| { TGLWE::::encrypt(&mut rng, pk, &(*m * (u64::MAX / beta.pow(i as u32) as u64))) }) .collect::>>()?; Ok(Self(tlev)) } pub fn encrypt_s( mut rng: impl Rng, _beta: u32, // TODO rm, and make beta=2 always l: u32, sk: &SecretKey, m: &Tn, ) -> Result { let tlev: Vec> = (1..l as u64 + 1) .map(|i| { let aux = if i < 64 { *m * (u64::MAX / (1u64 << i)) } else { // 1<<64 would overflow, and anyways we're dividing u64::MAX // by it, which would be equal to 1 *m }; TGLWE::::encrypt_s(&mut rng, sk, &aux) }) .collect::>>()?; Ok(Self(tlev)) } pub fn decrypt(&self, sk: &SecretKey, beta: u32) -> Tn { let pt = self.0[0].decrypt(sk); pt.mul_div_round(beta as u64, u64::MAX) } } impl TGLev { pub fn iter(&self) -> std::slice::Iter> { self.0.iter() } } // dot product between a TGLev and Vec>, usually Vec> comes from a // decomposition of Tn // TGLev * Vec> --> TGLWE impl Mul>> for TGLev { type Output = TGLWE; fn mul(self, v: Vec>) -> Self::Output { assert_eq!(self.0.len(), v.len()); // l TGLWES let tlwes: Vec> = self.0; let r: TGLWE = zip_eq(v, tlwes).map(|(a_d_i, glwe_i)| glwe_i * a_d_i).sum(); r } }