arnaucube
arnaucube/fhe-studyPublic Self-hosted repository
TLev encryption & decryption
arnaucube git@arnaucube.com
Tue, 22 Jul 2025 15:13:35 +0000
commit

fd5c4124ea918bfcb6a1c619f5b42b0f9a110c4f

parent

0ca73ac5055769da6772d2de06b28715fcb01abf

3 files changed, 94 insertions(+), 1 deletions(-)

jump to
M README.md → README.md

@@ -1,7 +1,7 @@

# fhe-study Implementations from scratch done while studying some FHE papers; do not use in production. -- `arith`: contains $\mathbb{Z}_q$, $R_q=\mathbb{Z}_q[X]/(X^N+1)$, $R=\mathbb{Z}[X]/(X^N+1)$, $\mathbb{T}_{Q}[X]/(X^N +1)$ arithmetic implementations, together with the NTT implementation. +- `arith`: contains $\mathbb{Z}_q$, $R_q=\mathbb{Z}_q[X]/(X^N+1)$, $R=\mathbb{Z}[X]/(X^N+1)$, $\mathbb{T}_Q[X]/(X^N +1)$ arithmetic implementations, together with the NTT implementation. - `gfhe`: (gfhe=generalized-fhe) contains the structs and logic for RLWE, GLWE, GLev, GGSW, RGSW cryptosystems, and modulus switching and key switching methods, which can be used by concrete FHE schemes. - `bfv`: https://eprint.iacr.org/2012/144.pdf scheme implementation - `ckks`: https://eprint.iacr.org/2016/421.pdf scheme implementation
M tfhe/src/lib.rs → tfhe/src/lib.rs

@@ -5,4 +5,5 @@ #![allow(non_camel_case_types)]

#![allow(clippy::upper_case_acronyms)] #![allow(dead_code)] // TMP +pub mod tlev; pub mod tlwe;
A tfhe/src/tlev.rs

@@ -0,0 +1,92 @@

+use anyhow::Result; +use rand::Rng; +use std::array; +use std::ops::{Add, Mul}; + +use arith::{Ring, Rq, Tn, T64, TR}; + +use crate::tlwe::{PublicKey, SecretKey, TLWE}; + +#[derive(Clone, Debug)] +pub struct TLev<const K: usize>(pub(crate) Vec<TLWE<K>>); + +impl<const K: usize> TLev<K> { + pub fn encode<const T: u64>(m: &Rq<T, 1>) -> Tn<1> { + let coeffs = m.coeffs(); + Tn(array::from_fn(|i| T64(coeffs[i].0))) + } + pub fn decode<const T: u64>(p: &Tn<1>) -> Rq<T, 1> { + Rq::<T, 1>::from_vec_u64(p.coeffs().iter().map(|c| c.0).collect()) + } + pub fn encrypt( + mut rng: impl Rng, + beta: u32, + l: u32, + pk: &PublicKey<K>, + m: &Tn<1>, + ) -> Result<Self> { + let tlev: Vec<TLWE<K>> = (1..l + 1) + .map(|i| { + TLWE::<K>::encrypt(&mut rng, pk, &(*m * (u64::MAX / beta.pow(i as u32) as u64))) + }) + .collect::<Result<Vec<_>>>()?; + + Ok(Self(tlev)) + } + pub fn encrypt_s( + mut rng: impl Rng, + beta: u32, + l: u32, + sk: &SecretKey<K>, + m: &Tn<1>, + ) -> Result<Self> { + let tlev: Vec<TLWE<K>> = (1..l + 1) + .map(|i| { + TLWE::<K>::encrypt_s(&mut rng, sk, &(*m * (u64::MAX / beta.pow(i as u32) as u64))) + }) + .collect::<Result<Vec<_>>>()?; + + Ok(Self(tlev)) + } + + pub fn decrypt(&self, sk: &SecretKey<K>, beta: u32) -> Tn<1> { + let pt = self.0[0].decrypt(sk); + pt.mul_div_round(beta as u64, u64::MAX) + } +} + +#[cfg(test)] +mod tests { + use anyhow::Result; + use rand::distributions::Uniform; + + use super::*; + + #[test] + fn test_encrypt_decrypt() -> Result<()> { + const T: u64 = 2; // plaintext modulus + const K: usize = 16; + type S = TLev<K>; + + let beta: u32 = 2; + let l: u32 = 16; + + let mut rng = rand::thread_rng(); + + for _ in 0..200 { + let (sk, pk) = TLWE::<K>::new_key(&mut rng)?; + + let msg_dist = Uniform::new(0_u64, T); + let m: Rq<T, 1> = Rq::rand_u64(&mut rng, msg_dist)?; + let p: Tn<1> = S::encode::<T>(&m); // plaintext + + let c = S::encrypt(&mut rng, beta, l, &pk, &p)?; + let p_recovered = c.decrypt(&sk, beta); + let m_recovered = S::decode::<T>(&p_recovered); + + assert_eq!(m.remodule::<T>(), m_recovered.remodule::<T>()); + } + + Ok(()) + } +}