Skip to content

Commit 160f8a3

Browse files
committed
Refactor: better API
1 parent 31ec7fa commit 160f8a3

File tree

15 files changed

+198
-288
lines changed

15 files changed

+198
-288
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
run: cargo test --verbose
3232

3333
- name: Run a basic example
34-
run: cargo run --example new_struct
34+
run: cargo run --example simple_struct
3535

3636
clippy-fmt:
3737
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 46 additions & 0 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
@@ -9,6 +9,7 @@ repository = "https://github.com/mathisbot/homomorph-rust"
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12+
bincode = { version = "2.0.0-rc.3", default-features = false, features = ["alloc", "derive"] }
1213
getrandom = { version = "0.2.15", default-features = false, features = ["rdrand"] }
1314

1415
[dev-dependencies]

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,19 @@ I do not intend to publish the crate on `crates.io`.
5050
5151
## Bare metal
5252
53-
The crates partially supports `no_std` environments: it uses `Vec` a lot, so it relies on an external `alloc` crate. As each bit ciphered takes up a lot of space, storing ciphered objects on the stack wouldn't be possible (at least on low end machines). This is why the heap is needed here.
53+
The crates supports `no_std` environments.
5454
55-
You may also need a source of randomness. On bare x86, randomness can still be retrieved using `RDRAND`. On other architectures, such as `aarch64-unknown-none`, you will have to implement `provide_getrandom`, which is a re-export of `getrandom::register_custom_getrandom`, gated behind the `custom_rand` feature.
55+
As `Vec` is used a lot, the crate relies on an external `alloc` crate.
56+
As each bit ciphered takes up a lot of space, storing ciphered objects
57+
on the stack wouldn't be possible (at least on low end machines).
58+
This is why the heap is needed here.
59+
60+
You may also need a source of randomness.
61+
On bare x86, randomness can still be retrieved using `RDRAND`.
62+
On other architectures, such as `aarch64-unknown-none`,
63+
you will have to implement `provide_getrandom`,
64+
which is a re-export of `getrandom::register_custom_getrandom`,
65+
gated behind the `custom_rand` feature.
5666

5767
## Benchmarks
5868

@@ -62,7 +72,7 @@ Parameters used for this benchmark were :
6272
- `d` = 128
6373
- `dp` = 128
6474
- `delta` = 1
65-
- `tau` = 128.
75+
- `tau` = 128
6676

6777
| Operation | Average time |
6878
|:-----------------:|:----------------:|

check.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function Count-Lines {
3838
Run-Command "cargo build --lib"
3939
Run-Command "cargo build --lib --target x86_64-unknown-none"
4040
Run-Command "cargo test"
41-
Run-Command "cargo run --example new_struct"
41+
Run-Command "cargo run --example simple_struct"
4242
Run-Command "cargo clippy --all-targets --all-features -- -D warnings --no-deps"
4343
Run-Command "cargo test --release -- --ignored"
4444
Run-Command "cargo fmt"

check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ count_lines() {
3636
run_command "cargo build --lib"
3737
run_command "cargo build --lib --target x86_64-unknown-none"
3838
run_command "cargo test"
39-
run_command "cargo run --example new_struct"
39+
run_command "cargo run --example simple_struct"
4040
run_command "cargo clippy --all-targets --all-features -- -D warnings --no-deps"
4141
run_command "cargo test --release -- --ignored"
4242
run_command "cargo fmt"

examples/new_struct.rs renamed to examples/simple_struct.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,13 @@ use homomorph::prelude::*;
99

1010
type Coordinate = u16;
1111

12-
#[derive(Clone, Debug, PartialEq, Eq)]
12+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Encode, Decode)]
1313
struct Vec3 {
1414
x: Coordinate,
1515
y: Coordinate,
1616
z: Coordinate,
1717
}
1818

19-
// If you don't want to implement `ByteConvertible` for your struct,
20-
// use repr(C) and derive Copy
21-
unsafe impl ByteConvertible for Vec3 {
22-
fn to_bytes(&self) -> Vec<u8> {
23-
let mut bytes = Vec::with_capacity(3 * size_of::<Coordinate>());
24-
bytes.extend_from_slice(&self.x.to_le_bytes());
25-
bytes.extend_from_slice(&self.y.to_le_bytes());
26-
bytes.extend_from_slice(&self.z.to_le_bytes());
27-
bytes
28-
}
29-
30-
fn from_bytes(bytes: &[u8]) -> Self {
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-
);
37-
38-
let x = Coordinate::from_le_bytes([bytes[0], bytes[1]]);
39-
let y = Coordinate::from_le_bytes([bytes[2], bytes[3]]);
40-
let z = Coordinate::from_le_bytes([bytes[4], bytes[5]]);
41-
42-
Self { x, y, z }
43-
}
44-
}
45-
4619
struct Vec3Add;
4720

4821
impl HomomorphicOperation2<Vec3> for Vec3Add {

examples/uint_add.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rand::{self, Rng};
1+
use rand::{thread_rng, Rng};
22
use std::time::Instant;
33

44
use homomorph::prelude::*;
@@ -7,7 +7,7 @@ use homomorph_impls::numbers::HomomorphicAddition;
77
const NUMBER_OF_TESTS: usize = 1_000;
88

99
fn main() {
10-
let mut rng = rand::thread_rng();
10+
let mut rng = thread_rng();
1111

1212
// Create a new context
1313
let params = Parameters::new(128, 128, 1, 128);
@@ -95,6 +95,13 @@ fn main() {
9595

9696
// Check if the results are correct
9797
for i in 0..NUMBER_OF_TESTS {
98-
assert_eq!(data1[i] + data2[i], decrypted_data_add[i]);
98+
assert_eq!(
99+
data1[i] + data2[i],
100+
decrypted_data_add[i],
101+
"Error at index {}: {} != {}",
102+
i,
103+
data1[i] + data2[i],
104+
decrypted_data_add[i]
105+
);
99106
}
100107
}

0 commit comments

Comments
 (0)