-
Notifications
You must be signed in to change notification settings - Fork 4
Encryption/decryption for i32 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbenke
wants to merge
10
commits into
master
Choose a base branch
from
mbenke/i32
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3aab1ac
Sub+SubAssign+Neg for Enc
mbenke cc81096
Mod231::From/Into<i32>
mbenke 14ff40e
cargo fmt
mbenke b5297f4
enc_i32/dec_i32
mbenke 36e6aa3
clean up
mbenke bf049a6
test prop_roundtrip_i32
mbenke a4147ae
TryFrom<i32> instead of From<i32> for Mod231
mbenke 63ed785
fix test prop_roundtrip_i32
mbenke 7140c28
cargo fmt
mbenke 93397db
Returning Result in quickcheck is OK
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 { | ||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this also work if we're encrypting/decrypting |
||
| } | ||
| } | ||
|
|
||
| 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)] | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, casting a
u32toi32like this could potentially lead to a difficult to track overflow, am I right? In such cases, we could use aTryIntotraitand wrap everything inside a
TryIntorather thanIntoforMod231. What do you think? Or the overflows are not possible here perhaps?