Skip to content

Commit 5963352

Browse files
committed
Improved performances
1 parent 9177520 commit 5963352

File tree

8 files changed

+147
-108
lines changed

8 files changed

+147
-108
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ custom_rand = ["getrandom/custom"]
2121
[profile.release]
2222
codegen-units = 1
2323
lto = "thin"
24+
debug = 1

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ Parameters used for this benchmark were :
6666

6767
| Operation | Average time |
6868
|:-----------------:|:----------------:|
69-
| Encryption | 83.2 µs |
70-
| Decryption | 18.1 µs |
71-
| Add | 2.4 ms |
72-
| Dec. after add | 1.4 ms |
69+
| Encryption | 80.5 µs |
70+
| Decryption | 12.9 µs |
71+
| Add | 1.57 ms |
72+
| Dec. after add | 1.03 ms |
7373

7474
It is still more efficient to decrypt, operate and then re-encrypt the data. This limits the use of the system to applications where security is paramount, and takes precedence over speed.
7575

examples/new_struct.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ unsafe impl ByteConvertible for Vec3 {
2828
}
2929

3030
fn from_bytes(bytes: &[u8]) -> Self {
31-
if bytes.len() != 3 * size_of::<Coordinate>() {
32-
panic!(
33-
"Invalid size of bytes for conversion: {} instead of {}",
34-
bytes.len(),
35-
3 * size_of::<Coordinate>()
36-
);
37-
}
31+
assert!(
32+
3 * size_of::<Coordinate>() == bytes.len(),
33+
"Invalid size of bytes for conversion: expected {} got {}.",
34+
3 * size_of::<Coordinate>(),
35+
bytes.len(),
36+
);
3837

3938
let x = u16::from_le_bytes([bytes[0], bytes[1]]);
4039
let y = u16::from_le_bytes([bytes[2], bytes[3]]);
4140
let z = u16::from_le_bytes([bytes[4], bytes[5]]);
42-
Vec3 { x, y, z }
41+
42+
Self { x, y, z }
4343
}
4444
}
4545

@@ -52,15 +52,15 @@ impl HomomorphicOperation2<Vec3> for Vec3Add {
5252
unsafe fn apply(a: &Ciphered<Vec3>, b: &Ciphered<Vec3>) -> Ciphered<Vec3> {
5353
// Unwrap the first `Vec3`
5454
let (ax, a) = a.split_at(Coordinate::BITS as usize);
55-
let ax: Ciphered<Coordinate> = Ciphered::new_from_raw(ax.to_vec());
5655
let (ay, az) = a.split_at(Coordinate::BITS as usize);
56+
let ax: Ciphered<Coordinate> = Ciphered::new_from_raw(ax.to_vec());
5757
let ay: Ciphered<Coordinate> = Ciphered::new_from_raw(ay.to_vec());
5858
let az: Ciphered<Coordinate> = Ciphered::new_from_raw(az.to_vec());
5959

6060
// Unwrap the second `Vec3`
6161
let (bx, b) = b.split_at(Coordinate::BITS as usize);
62-
let bx: Ciphered<Coordinate> = Ciphered::new_from_raw(bx.to_vec());
6362
let (by, bz) = b.split_at(Coordinate::BITS as usize);
63+
let bx: Ciphered<Coordinate> = Ciphered::new_from_raw(bx.to_vec());
6464
let by: Ciphered<Coordinate> = Ciphered::new_from_raw(by.to_vec());
6565
let bz: Ciphered<Coordinate> = Ciphered::new_from_raw(bz.to_vec());
6666

examples/uint_add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::time::Instant;
33

44
use homomorph::prelude::*;
55

6-
const NUMBER_OF_TESTS: usize = 10_000;
6+
const NUMBER_OF_TESTS: usize = 1_000;
77

88
fn main() {
99
let mut rng = rand::thread_rng();

src/cipher.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ pub unsafe trait ByteConvertible {
8181
fn from_bytes(bytes: &[u8]) -> Self;
8282
}
8383

84-
// All types that implement Copy and Sized can be converted to bytes
84+
// All types that implement Copy can be converted to bytes
8585
// by simply reading stack data as bytes
8686
// TODO: Make it derivable ?
87-
unsafe impl<T: Copy + Sized> ByteConvertible for T {
87+
unsafe impl<T: Copy> ByteConvertible for T {
8888
/// This function is used to convert a type to a byte array
8989
fn to_bytes(&self) -> Vec<u8> {
9090
let mut bytes = Vec::with_capacity(size_of::<T>());
9191

9292
// Safety
9393
// Because of `Vec::with_capacity`, we know that the buffer
9494
// is big enough to hold the `size_from::<T>()` bytes.
95-
// We also know that the source is valid, because T is Sized and Copy,
96-
// and we are reading exactly `size_of::<T>()` bytes from it.
95+
// We also know that the source is valid, because we are reading
96+
// exactly `size_of::<T>()` bytes from `T`.
9797
unsafe {
9898
memcpy(
9999
core::ptr::from_ref(self).cast::<u8>(),
@@ -225,8 +225,7 @@ impl<T: ByteConvertible> Ciphered<T> {
225225
#[must_use]
226226
// See https://github.com/mathisbot/homomorph-rust?tab=readme-ov-file#system
227227
fn decipher_bit(c_bit: &CipheredBit, sk: &SecretKey) -> bool {
228-
let sk = sk.get_polynomial();
229-
let remainder = c_bit.0.rem(sk);
228+
let remainder = c_bit.0.rem(sk.get_polynomial());
230229
remainder.evaluate(false)
231230
}
232231

@@ -250,8 +249,8 @@ impl<T: ByteConvertible> Ciphered<T> {
250249

251250
let mut bytes = Vec::with_capacity(self.len() / 8);
252251

253-
let mut byte = 0u8;
254-
let mut bit_count = 0;
252+
let mut byte = 0;
253+
let mut bit_count: u8 = 0;
255254

256255
for c_bit in self.iter() {
257256
let bit = u8::from(Self::decipher_bit(c_bit, sk));

src/impls/numbers/int.rs

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,82 @@ impl_homomorphic_multiplication_int!(i8, i16, i32, isize, i64, i128);
9898

9999
#[cfg(test)]
100100
mod tests {
101-
use crate::impls::numbers::{HomomorphicAddition, HomomorphicMultiplication};
102-
use crate::operations::HomomorphicOperation2;
103-
use crate::{Ciphered, Context, Parameters};
101+
use crate::prelude::*;
102+
use homomorph_impls::numbers::{
103+
HomomorphicAddition, HomomorphicAndGate, HomomorphicMultiplication, HomomorphicNotGate,
104+
HomomorphicOrGate, HomomorphicXorGate,
105+
};
104106

105107
use rand::{thread_rng, Rng};
106108

109+
#[test]
110+
fn test_homomorphic_and_gate() {
111+
let parameters = Parameters::new(32, 8, 8, 8);
112+
let mut context = Context::new(parameters);
113+
context.generate_secret_key();
114+
context.generate_public_key();
115+
let sk = context.get_secret_key().unwrap();
116+
let pk = context.get_public_key().unwrap();
117+
118+
let a = Ciphered::cipher(&0b1010_i8, pk);
119+
let b = Ciphered::cipher(&0b1100_i8, pk);
120+
let c = unsafe { HomomorphicAndGate::apply(&a, &b) };
121+
let d = c.decipher(sk);
122+
assert_eq!(0b1000, d);
123+
}
124+
125+
#[test]
126+
fn test_homomorphic_or_gate() {
127+
let parameters = Parameters::new(32, 8, 8, 8);
128+
let mut context = Context::new(parameters);
129+
context.generate_secret_key();
130+
context.generate_public_key();
131+
let sk = context.get_secret_key().unwrap();
132+
let pk = context.get_public_key().unwrap();
133+
134+
let a = Ciphered::cipher(&0b1010_i8, pk);
135+
let b = Ciphered::cipher(&0b1100_i8, pk);
136+
let c = unsafe { HomomorphicOrGate::apply(&a, &b) };
137+
let d = c.decipher(sk);
138+
assert_eq!(0b1110, d);
139+
}
140+
141+
#[test]
142+
fn test_homomorphic_xor_gate() {
143+
let parameters = Parameters::new(16, 8, 8, 8);
144+
let mut context = Context::new(parameters);
145+
context.generate_secret_key();
146+
context.generate_public_key();
147+
let sk = context.get_secret_key().unwrap();
148+
let pk = context.get_public_key().unwrap();
149+
150+
let a = Ciphered::cipher(&0b1010_i8, pk);
151+
let b = Ciphered::cipher(&0b1100_i8, pk);
152+
let c = unsafe { HomomorphicXorGate::apply(&a, &b) };
153+
let d = c.decipher(sk);
154+
assert_eq!(0b0110, d);
155+
}
156+
157+
#[test]
158+
fn test_homomorphic_not_gate() {
159+
let parameters = Parameters::new(16, 8, 8, 8);
160+
let mut context = Context::new(parameters);
161+
context.generate_secret_key();
162+
context.generate_public_key();
163+
let sk = context.get_secret_key().unwrap();
164+
let pk = context.get_public_key().unwrap();
165+
166+
let mut a = Ciphered::cipher(&0b0000_1010_i8, pk);
167+
unsafe { HomomorphicNotGate::apply(&mut a) };
168+
let d = a.decipher(sk);
169+
assert_eq!(-11, d);
170+
171+
let mut a = Ciphered::cipher(&0b0000_1100_i8, pk);
172+
unsafe { HomomorphicNotGate::apply(&mut a) };
173+
let d = a.decipher(sk);
174+
assert_eq!(-13, d);
175+
}
176+
107177
#[test]
108178
#[should_panic = "not yet implemented: Homormophic addition for int"]
109179
fn test_homomorphic_addition() {

0 commit comments

Comments
 (0)