|
|
@ -8,32 +8,26 @@ use std::iter::Sum; |
|
|
|
use std::ops::{Add, AddAssign, Mul, Sub};
|
|
|
|
|
|
|
|
use arith::{Ring, Rq, Tn, T64, TR};
|
|
|
|
use gfhe::{glwe, GLWE};
|
|
|
|
|
|
|
|
const ERR_SIGMA: f64 = 3.2;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct TLWE<const K: usize>(TR<Tn<1>, K>, Tn<1>);
|
|
|
|
|
|
|
|
pub struct SecretKey<const K: usize>(glwe::SecretKey<Tn<1>, K>);
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct SecretKey<const K: usize>(TR<Tn<1>, K>);
|
|
|
|
pub struct PublicKey<const K: usize>(glwe::PublicKey<Tn<1>, K>);
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct PublicKey<const K: usize>(Tn<1>, TR<Tn<1>, K>);
|
|
|
|
pub struct TLWE<const K: usize>(pub GLWE<Tn<1>, K>);
|
|
|
|
|
|
|
|
impl<const K: usize> TLWE<K> {
|
|
|
|
pub fn zero() -> Self {
|
|
|
|
Self(TR::zero(), Tn::zero())
|
|
|
|
Self(GLWE::<Tn<1>, K>::zero())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_key(mut rng: impl Rng) -> Result<(SecretKey<K>, PublicKey<K>)> {
|
|
|
|
let Xi_key = Uniform::new(0_f64, 2_f64);
|
|
|
|
let Xi_err = Normal::new(0_f64, ERR_SIGMA)?;
|
|
|
|
|
|
|
|
let s: TR<Tn<1>, K> = TR::rand(&mut rng, Xi_key);
|
|
|
|
let a: TR<Tn<1>, K> = TR::rand(&mut rng, Standard);
|
|
|
|
let e = Tn::rand(&mut rng, Xi_err);
|
|
|
|
|
|
|
|
let pk: PublicKey<K> = PublicKey((&a * &s) + e, a);
|
|
|
|
Ok((SecretKey(s), pk))
|
|
|
|
pub fn new_key(rng: impl Rng) -> Result<(SecretKey<K>, PublicKey<K>)> {
|
|
|
|
let (sk, pk) = GLWE::new_key(rng)?;
|
|
|
|
Ok((SecretKey(sk), PublicKey(pk)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn encode<const P: u64>(m: &Rq<P, 1>) -> Tn<1> {
|
|
|
@ -47,50 +41,28 @@ impl TLWE { |
|
|
|
}
|
|
|
|
|
|
|
|
// encrypts with the given SecretKey (instead of PublicKey)
|
|
|
|
pub fn encrypt_s(mut rng: impl Rng, sk: &SecretKey<K>, m: &Tn<1>) -> Result<Self> {
|
|
|
|
let Xi_key = Uniform::new(0_f64, 2_f64);
|
|
|
|
let Xi_err = Normal::new(0_f64, ERR_SIGMA)?;
|
|
|
|
|
|
|
|
let a: TR<Tn<1>, K> = TR::rand(&mut rng, Xi_key);
|
|
|
|
let e = Tn::rand(&mut rng, Xi_err);
|
|
|
|
|
|
|
|
let b: Tn<1> = (&a * &sk.0) + *m + e;
|
|
|
|
Ok(Self(a, b))
|
|
|
|
pub fn encrypt_s(rng: impl Rng, sk: &SecretKey<K>, p: &Tn<1>) -> Result<Self> {
|
|
|
|
let glwe = GLWE::encrypt_s(rng, &sk.0, p)?;
|
|
|
|
Ok(Self(glwe))
|
|
|
|
}
|
|
|
|
pub fn encrypt(mut rng: impl Rng, pk: &PublicKey<K>, m: &Tn<1>) -> Result<Self> {
|
|
|
|
let Xi_key = Uniform::new(0_f64, 2_f64);
|
|
|
|
let Xi_err = Normal::new(0_f64, ERR_SIGMA)?;
|
|
|
|
|
|
|
|
let u: Tn<1> = Tn::rand(&mut rng, Xi_key);
|
|
|
|
|
|
|
|
let e0: Tn<1> = Tn::rand(&mut rng, Xi_err);
|
|
|
|
let e1 = TR::<Tn<1>, K>::rand(&mut rng, Xi_err);
|
|
|
|
|
|
|
|
let b: Tn<1> = pk.0 * u + *m + e0;
|
|
|
|
let d: TR<Tn<1>, K> = &pk.1 * &u + e1;
|
|
|
|
|
|
|
|
Ok(Self(d, b))
|
|
|
|
pub fn encrypt(rng: impl Rng, pk: &PublicKey<K>, p: &Tn<1>) -> Result<Self> {
|
|
|
|
let glwe = GLWE::encrypt(rng, &pk.0, p)?;
|
|
|
|
Ok(Self(glwe))
|
|
|
|
}
|
|
|
|
pub fn decrypt(&self, sk: &SecretKey<K>) -> Tn<1> {
|
|
|
|
let (d, b): (TR<Tn<1>, K>, Tn<1>) = (self.0.clone(), self.1);
|
|
|
|
b - &d * &sk.0
|
|
|
|
self.0.decrypt(&sk.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const K: usize> Add<TLWE<K>> for TLWE<K> {
|
|
|
|
type Output = Self;
|
|
|
|
fn add(self, other: Self) -> Self {
|
|
|
|
let a: TR<Tn<1>, K> = self.0 + other.0;
|
|
|
|
let b: Tn<1> = self.1 + other.1;
|
|
|
|
Self(a, b)
|
|
|
|
Self(self.0 + other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<const K: usize> AddAssign for TLWE<K> {
|
|
|
|
fn add_assign(&mut self, rhs: Self) {
|
|
|
|
for i in 0..K {
|
|
|
|
self.0 .0[i] = self.0 .0[i] + rhs.0 .0[i];
|
|
|
|
}
|
|
|
|
self.1 = self.1 + rhs.1;
|
|
|
|
self.0 += rhs.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<const K: usize> Sum<TLWE<K>> for TLWE<K> {
|
|
|
@ -109,9 +81,7 @@ impl Sum> for TLWE { |
|
|
|
impl<const K: usize> Sub<TLWE<K>> for TLWE<K> {
|
|
|
|
type Output = Self;
|
|
|
|
fn sub(self, other: Self) -> Self {
|
|
|
|
let a: TR<Tn<1>, K> = self.0 - other.0;
|
|
|
|
let b: Tn<1> = self.1 - other.1;
|
|
|
|
Self(a, b)
|
|
|
|
Self(self.0 - other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
@ -119,27 +89,27 @@ impl Sub> for TLWE { |
|
|
|
impl<const K: usize> Add<Tn<1>> for TLWE<K> {
|
|
|
|
type Output = Self;
|
|
|
|
fn add(self, plaintext: Tn<1>) -> Self {
|
|
|
|
let a: TR<Tn<1>, K> = self.0;
|
|
|
|
let b: Tn<1> = self.1 + plaintext;
|
|
|
|
Self(a, b)
|
|
|
|
let a: TR<Tn<1>, K> = self.0 .0;
|
|
|
|
let b: Tn<1> = self.0 .1 + plaintext;
|
|
|
|
Self(GLWE(a, b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// plaintext substraction
|
|
|
|
impl<const K: usize> Sub<Tn<1>> for TLWE<K> {
|
|
|
|
type Output = Self;
|
|
|
|
fn sub(self, plaintext: Tn<1>) -> Self {
|
|
|
|
let a: TR<Tn<1>, K> = self.0;
|
|
|
|
let b: Tn<1> = self.1 - plaintext;
|
|
|
|
Self(a, b)
|
|
|
|
let a: TR<Tn<1>, K> = self.0 .0;
|
|
|
|
let b: Tn<1> = self.0 .1 - plaintext;
|
|
|
|
Self(GLWE(a, b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// plaintext multiplication
|
|
|
|
impl<const K: usize> Mul<Tn<1>> for TLWE<K> {
|
|
|
|
type Output = Self;
|
|
|
|
fn mul(self, plaintext: Tn<1>) -> Self {
|
|
|
|
let a: TR<Tn<1>, K> = TR(self.0 .0.iter().map(|r_i| *r_i * plaintext).collect());
|
|
|
|
let b: Tn<1> = self.1 * plaintext;
|
|
|
|
Self(a, b)
|
|
|
|
let a: TR<Tn<1>, K> = TR(self.0 .0 .0.iter().map(|r_i| *r_i * plaintext).collect());
|
|
|
|
let b: Tn<1> = self.0 .1 * plaintext;
|
|
|
|
Self(GLWE(a, b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
@ -157,11 +127,11 @@ mod tests { |
|
|
|
type S = TLWE<K>;
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
|
|
|
|
for _ in 0..200 {
|
|
|
|
let (sk, pk) = S::new_key(&mut rng)?;
|
|
|
|
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
let m = Rq::<T, 1>::rand_u64(&mut rng, msg_dist)?;
|
|
|
|
dbg!(&m);
|
|
|
|
let p: Tn<1> = S::encode::<T>(&m);
|
|
|
@ -191,11 +161,11 @@ mod tests { |
|
|
|
type S = TLWE<K>;
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
|
|
|
|
for _ in 0..200 {
|
|
|
|
let (sk, pk) = S::new_key(&mut rng)?;
|
|
|
|
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
let m1 = Rq::<T, 1>::rand_u64(&mut rng, msg_dist)?;
|
|
|
|
let m2 = Rq::<T, 1>::rand_u64(&mut rng, msg_dist)?;
|
|
|
|
let p1: Tn<1> = S::encode::<T>(&m1); // plaintext
|
|
|
@ -222,11 +192,11 @@ mod tests { |
|
|
|
type S = TLWE<K>;
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
|
|
|
|
for _ in 0..200 {
|
|
|
|
let (sk, pk) = S::new_key(&mut rng)?;
|
|
|
|
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
let m1 = Rq::<T, 1>::rand_u64(&mut rng, msg_dist)?;
|
|
|
|
let m2 = Rq::<T, 1>::rand_u64(&mut rng, msg_dist)?;
|
|
|
|
let p1: Tn<1> = S::encode::<T>(&m1); // plaintext
|
|
|
@ -252,11 +222,11 @@ mod tests { |
|
|
|
type S = TLWE<K>;
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
|
|
|
|
for _ in 0..200 {
|
|
|
|
let (sk, pk) = S::new_key(&mut rng)?;
|
|
|
|
|
|
|
|
let msg_dist = Uniform::new(0_u64, T);
|
|
|
|
let m1 = Rq::<T, 1>::rand_u64(&mut rng, msg_dist)?;
|
|
|
|
let m2 = Rq::<T, 1>::rand_u64(&mut rng, msg_dist)?;
|
|
|
|
let p1: Tn<1> = S::encode::<T>(&m1);
|
|
|
|