Skip to content

Encryption/decryption for i32#8

Open
mbenke wants to merge 10 commits intomasterfrom
mbenke/i32
Open

Encryption/decryption for i32#8
mbenke wants to merge 10 commits intomasterfrom
mbenke/i32

Conversation

@mbenke
Copy link
Contributor

@mbenke mbenke commented Sep 19, 2019

Interpret numbers in the range 0..2^30-1 as nonnegative and numbers in the range 2^30..2^31-2 as negative.

For example -1 corresponds to 2^31-2

Copy link
Contributor

@kubkon kubkon left a comment

Choose a reason for hiding this comment

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

Looks good! Just a couple of things to address before we can merge it.

src/enc.rs Outdated
}

#[inline]
fn enc_i32(key_pair: &KeyPair, value: i32) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, it looks like an ideal candidate for a generic. Could we perhaps make enc generic, thus accepting either i32 or u32? In the future, we could think of some constraints, but for now, a simple generic type would work OK IMHO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Another thing, are enc_i32 and dec_i32 used anywhere? They don't seem though 🤔

impl SubAssign for Enc {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs
Copy link
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?

Comment on lines +128 to +141
(y as i32) - MODULUSI32
} else {
y as i32
Copy link
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?

Comment on lines +274 to +275
let z: Result<Mod231, _> = Mod231::try_from(y);
z == Ok(x)
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, in unit tests, it is possible to return a Result which saves the unwrapping step. I'll redo this test to demonstrate what I mean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants