generalized-fhe: add GLWE encryption & decryption
arnaucube git@arnaucube.com
Wed, 09 Jul 2025 17:17:19 +0200
8 files changed,
142 insertions(+),
3 deletions(-)
M
Cargo.toml
→
Cargo.toml
@@ -1,6 +1,7 @@
[workspace] members = [ "arith", + "generalized-fhe", "bfv", "ckks" ]
M
README.md
→
README.md
@@ -2,5 +2,6 @@ # 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)$ and $R=\mathbb{Z}[X]/(X^N+1)$ arithmetic implementations, together with the NTT implementation. +- `generalized-fhe`: contains the structs and logic for RLWE, GLWE, GLev, GGSW, RGSW cryptosystems, 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
arith/src/ringq.rs
→
arith/src/ringq.rs
@@ -13,6 +13,9 @@ use anyhow::{anyhow, Result};
use crate::Ring; +// NOTE: currently using fixed-size arrays, but pending to see if with +// real-world parameters the stack can keep up; if not will move everything to +// use Vec. /// PolynomialRing element, where the PolynomialRing is R = Z_q[X]/(X^n +1) /// The implementation assumes that q is prime. #[derive(Clone, Copy)]
M
arith/src/tuple_ring.rs
→
arith/src/tuple_ring.rs
@@ -10,9 +10,6 @@ use std::{array, ops};
use crate::Ring; -// #[derive(Clone, Copy, Debug)] -// pub struct TR<R: Ring, const K: usize>([R; K]); - /// Tuple of K Ring (Rq) elements. We use Vec<R> to allocate it in the heap, /// since if using a fixed-size array it would overflow the stack. #[derive(Clone, Debug)]
A
generalized-fhe/Cargo.toml
@@ -0,0 +1,12 @@
+[package] +name = "generalized-fhe" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = { workspace = true } +rand = { workspace = true } +rand_distr = { workspace = true } +itertools = { workspace = true } + +arith = { path="../arith" }
A
generalized-fhe/README.md
@@ -0,0 +1,2 @@
+# common +Contains the structs and logic for RLWE, GLWE, GLev, GGSW, RGSW cryptosystems, which can be used by concrete FHE schemes.
A
generalized-fhe/src/glwe.rs
@@ -0,0 +1,115 @@
+use anyhow::Result; +use itertools::zip_eq; +use rand::Rng; +use rand_distr::{Normal, Uniform}; +use std::{array, ops}; + +use arith::{Ring, Rq, R, TR}; + +const ERR_SIGMA: f64 = 3.2; + +pub struct GLWE<const Q: u64, const N: usize, const K: usize>(TR<Rq<Q, N>, K>, Rq<Q, N>); + +#[derive(Clone, Debug)] +pub struct SecretKey<const Q: u64, const N: usize, const K: usize>(TR<Rq<Q, N>, K>); +#[derive(Clone, Debug)] +pub struct PublicKey<const Q: u64, const N: usize, const K: usize>(Rq<Q, N>, TR<Rq<Q, N>, K>); + +impl<const Q: u64, const N: usize, const K: usize> GLWE<Q, N, K> { + pub fn new_key(mut rng: impl Rng) -> Result<(SecretKey<Q, N, K>, PublicKey<Q, N, K>)> { + let Xi_key = Uniform::new(0_f64, 2_f64); + let Xi_err = Normal::new(0_f64, ERR_SIGMA)?; + + let s: TR<Rq<Q, N>, K> = TR::rand(&mut rng, Xi_key); + let a: TR<Rq<Q, N>, K> = TR::rand(&mut rng, Uniform::new(0_f64, Q as f64)); + let e = Rq::<Q, N>::rand(&mut rng, Xi_err); + + let pk: PublicKey<Q, N, K> = PublicKey((&a * &s) + e, a); + Ok((SecretKey(s), pk)) + } + + // TODO delta not as input + pub fn encrypt_s<const T: u64>( + mut rng: impl Rng, + sk: &SecretKey<Q, N, K>, + m: &Rq<T, N>, + delta: u64, + ) -> Result<Self> { + let m: Rq<Q, N> = m.remodule::<Q>(); + + let Xi_key = Uniform::new(0_f64, 2_f64); + let Xi_err = Normal::new(0_f64, ERR_SIGMA)?; + + let a: TR<Rq<Q, N>, K> = TR::rand(&mut rng, Xi_key); + let e = Rq::<Q, N>::rand(&mut rng, Xi_err); + + let b: Rq<Q, N> = (&a * &sk.0) + m * delta + e; + Ok(Self(a, b)) + } + pub fn encrypt<const T: u64>( + mut rng: impl Rng, + pk: &PublicKey<Q, N, K>, + m: &Rq<T, N>, + delta: u64, + ) -> Result<Self> { + let m: Rq<Q, N> = m.remodule::<Q>(); + + let Xi_key = Uniform::new(0_f64, 2_f64); + let Xi_err = Normal::new(0_f64, ERR_SIGMA)?; + + let u: Rq<Q, N> = Rq::rand(&mut rng, Xi_key); + + let e0 = Rq::<Q, N>::rand(&mut rng, Xi_err); + let e1 = TR::<Rq<Q, N>, K>::rand(&mut rng, Xi_err); + + let b: Rq<Q, N> = pk.0 * u + m * delta + e0; + let d: TR<Rq<Q, N>, K> = &pk.1 * &u + e1; + + Ok(Self(d, b)) + } + pub fn decrypt<const T: u64>(&self, sk: &SecretKey<Q, N, K>, delta: u64) -> Rq<T, N> { + let (d, b): (TR<Rq<Q, N>, K>, Rq<Q, N>) = (self.0.clone(), self.1); + let r: Rq<Q, N> = b - &d * &sk.0; + let r_scaled: Vec<f64> = r + .coeffs() + .iter() + .map(|e| (e.0 as f64 / delta as f64).round()) + .collect(); + let r = Rq::<Q, N>::from_vec_f64(r_scaled); + r.remodule::<T>() + } +} + +#[cfg(test)] +mod tests { + use anyhow::Result; + use rand::distributions::Uniform; + + use super::*; + + #[test] + fn test_encrypt_decrypt() -> Result<()> { + const Q: u64 = 2u64.pow(16) + 1; + const N: usize = 128; + const T: u64 = 32; // plaintext modulus + const K: usize = 16; + type S = GLWE<Q, N, K>; + + let delta: u64 = Q / T; // floored + let mut rng = rand::thread_rng(); + + for _ in 0..200 { + let (sk, pk) = S::new_key(&mut rng)?; + + let msg_dist = Uniform::new(0_u64, T); + let m = Rq::<T, N>::rand_u64(&mut rng, msg_dist)?; + + let c = S::encrypt(&mut rng, &pk, &m, delta)?; + let m_recovered = c.decrypt(&sk, delta); + + assert_eq!(m, m_recovered); + } + + Ok(()) + } +}
A
generalized-fhe/src/lib.rs
@@ -0,0 +1,8 @@
+//! Implementation of BFV https://eprint.iacr.org/2012/144.pdf +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(clippy::upper_case_acronyms)] +#![allow(dead_code)] // TMP + +pub mod glwe;