Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/algebra/m231.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use num_traits::identities::{One, Zero};
use rand::distributions::{Distribution, Standard};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt;
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};

Expand Down Expand Up @@ -106,6 +107,42 @@ impl From<u32> for Mod231 {
}
}

impl TryFrom<i32> for Mod231 {
type Error = &'static str;

fn try_from(x: i32) -> Result<Self, Self::Error> {
let y: u32 = if x < 0 {
let y = x + MODULUSI32;
if y < 0 {
return Err("i32 out of range for Mod231");
} else {
y as u32
}
} else {
if x < MODULUSI32 {
x as u32
} else {
return Err("i32 out of range for Mod231");
}
};

Ok(Mod231(normalize(y)))
}
}

impl Into<i32> for Mod231 {
fn into(self) -> i32 {
let y: u32 = self.0;
let max = MODULUS / 2;

if y > max {
(y as i32) - MODULUSI32
} else {
y as i32
Comment on lines +139 to +141

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, casting a u32 to i32 like this could potentially lead to a difficult to track overflow, am I right? In such cases, we could use a TryInto trait

let y: u32 = self.0;
// ...
y.try_into()? - MODULUSI32

and wrap everything inside a TryInto rather than Into for Mod231. What do you think? Or the overflows are not possible here perhaps?

}
}
}

impl Add for Mod231 {
type Output = Self;

Expand Down Expand Up @@ -284,4 +321,10 @@ mod tests {
}
}
}

#[quickcheck]
fn prop_roundtrip_i32(x: Mod231) -> Result<bool, &'static str> {
let y: i32 = x.into();
Mod231::try_from(y).map(|z| z == x)
}
}
47 changes: 46 additions & 1 deletion src/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use nalgebra::Matrix3;
use num_traits::Zero;
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt;
use std::ops::{Add, AddAssign, Mul, MulAssign};
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// Wrapper type for lifting `u32` type to FHE compatible
/// form
Expand All @@ -32,6 +33,21 @@ impl Enc {
let dec = key_pair.backwards * self.inner * key_pair.forwards;
dec[0].w.0
}

#[inline]
fn enc_i32(key_pair: &KeyPair, value: i32) -> Option<Self> {
let m = Mod231::try_from(value).ok()?;
let enc: Matrix3<_> = Q231::from(m).into();
let inner = key_pair.forwards * enc * key_pair.backwards;

Some(Self { inner })
}

#[inline]
fn dec_i32(&self, key_pair: &KeyPair) -> i32 {
let dec = key_pair.backwards * self.inner * key_pair.forwards;
Mod231::into(dec[0].w)
}
}

impl fmt::Display for Enc {
Expand Down Expand Up @@ -77,6 +93,35 @@ impl MulAssign for Enc {
}
}

impl Sub for Enc {
type Output = Self;

#[inline]
fn sub(self, rhs: Self) -> Self::Output {
Self {
inner: self.inner - rhs.inner,
}
}
}

impl SubAssign for Enc {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also work if we're encrypting/decrypting u32? Or is there some trick to it?

}
}

impl Neg for Enc {
type Output = Self;

#[inline]
fn neg(self) -> Self::Output {
Self {
inner: self.inner.neg(),
}
}
}

/// Type representing a key pair which can be used for encrypting
/// and decrypting data
#[derive(Debug, Serialize, Deserialize)]
Expand Down