mirror of
https://github.com/arnaucube/fhe-study.git
synced 2026-01-24 04:33:52 +01:00
generalized-fhe: add GLWE encryption & decryption
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"arith",
|
||||
"generalized-fhe",
|
||||
"bfv",
|
||||
"ckks"
|
||||
]
|
||||
|
||||
@@ -2,5 +2,6 @@
|
||||
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
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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)]
|
||||
|
||||
12
generalized-fhe/Cargo.toml
Normal file
12
generalized-fhe/Cargo.toml
Normal file
@@ -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" }
|
||||
2
generalized-fhe/README.md
Normal file
2
generalized-fhe/README.md
Normal file
@@ -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.
|
||||
115
generalized-fhe/src/glwe.rs
Normal file
115
generalized-fhe/src/glwe.rs
Normal file
@@ -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(())
|
||||
}
|
||||
}
|
||||
8
generalized-fhe/src/lib.rs
Normal file
8
generalized-fhe/src/lib.rs
Normal file
@@ -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;
|
||||
Reference in New Issue
Block a user