Skip to content

Commit 29db39c

Browse files
committed
fix: code coverage
1 parent 7d34aa5 commit 29db39c

File tree

14 files changed

+242
-199
lines changed

14 files changed

+242
-199
lines changed

.github/workflows/rust.yml

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Rust
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: ["main"]
66
pull_request:
7-
branches: [ "main" ]
7+
branches: ["main"]
88

99
env:
1010
CARGO_TERM_COLOR: always
@@ -14,48 +14,57 @@ jobs:
1414
runs-on: ubuntu-latest
1515

1616
steps:
17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v3
1818

19-
- name: Install Rust
20-
uses: actions-rs/toolchain@v1
21-
with:
22-
toolchain: stable
23-
target: x86_64-unknown-linux-gnu
24-
profile: minimal
25-
components: rustfmt, clippy
26-
override: true
19+
- name: Install Rust
20+
uses: actions-rs/toolchain@v1
21+
with:
22+
toolchain: stable
23+
target: x86_64-unknown-linux-gnu
24+
profile: minimal
25+
override: true
2726

28-
- name: Build
29-
run: cargo build --lib --verbose
27+
- name: Build
28+
run: cargo build --lib --verbose
3029

31-
- name: Run basic tests
32-
run: cargo test --verbose
30+
- name: Run basic tests
31+
run: cargo test --verbose
3332

34-
- name: Run extensive tests
35-
run: cargo test --release --verbose -- --ignored
33+
- name: Run a basic example
34+
run: cargo run --release --example new_struct
3635

37-
- name: Run a basic example
38-
run: cargo run --release --example new_struct
36+
clippy-fmt:
37+
runs-on: ubuntu-latest
38+
39+
steps:
40+
- name: Install Rust
41+
uses: actions-rs/toolchain@v1
42+
with:
43+
toolchain: stable
44+
target: x86_64-unknown-linux-gnu
45+
profile: minimal
46+
components: rustfmt, clippy
47+
override: true
3948

40-
- name: Run Clippy
41-
run: cargo clippy --all-targets --all-features -- -D warnings --no-deps
49+
- name: Run clippy
50+
run: cargo clippy --verbose --all-targets --all-features -- -D warnings --no-deps
4251

43-
- name: Check formatting
44-
run: cargo fmt -- --check
52+
- name: Run fmt
53+
run: cargo fmt --verbose --all -- --check
4554

4655
x86_64-unknown-none:
4756
runs-on: ubuntu-latest
4857

4958
steps:
50-
- uses: actions/checkout@v3
51-
52-
- name: Install Rust
53-
uses: actions-rs/toolchain@v1
54-
with:
55-
toolchain: stable
56-
target: x86_64-unknown-none
57-
profile: minimal
58-
override: true
59-
60-
- name: Build
61-
run: cargo build --lib --verbose
59+
- uses: actions/checkout@v3
60+
61+
- name: Install Rust
62+
uses: actions-rs/toolchain@v1
63+
with:
64+
toolchain: stable
65+
target: x86_64-unknown-none
66+
profile: minimal
67+
override: true
68+
69+
- name: Build
70+
run: cargo build --lib --verbose --target x86_64-unknown-none

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ getrandom = { version = "0.2.15", default-features = false, features = ["rdrand"
1313
zeroize = { version = "1.8.1", default-features = false, features = ["alloc","zeroize_derive"] }
1414

1515
[dev-dependencies]
16-
mimalloc = { version = "0.1.43", features = ["secure"] }
1716
rand = "0.8.5"
1817

1918
[features]

examples/new_struct.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ impl HomomorphicOperation2<Vec3> for Vec3Add {
8383
}
8484
}
8585

86+
struct Vec3Sub;
87+
88+
impl HomomorphicOperation<0, u8> for Vec3Sub {
89+
unsafe fn apply(args: [&Ciphered<u8>; 0]) -> Ciphered<u8> {
90+
panic!(
91+
"This function should never be called {}, {:?}",
92+
size_of::<[&Ciphered<u8>; 0]>(),
93+
args
94+
);
95+
}
96+
}
97+
8698
fn main() {
8799
let params = Parameters::new(64, 32, 1, 32);
88100
let mut context = Context::new(params);
@@ -92,16 +104,12 @@ fn main() {
92104
let pk = context.get_public_key().unwrap();
93105

94106
let a = Ciphered::cipher(&Vec3 { x: 1, y: 2, z: 3 }, pk);
95-
let b = Ciphered::cipher(&Vec3 { x: 9, y: 8, z: 7 }, pk);
107+
let b = Ciphered::cipher(&Vec3 { x: 4, y: 5, z: 6 }, pk);
96108
let c = unsafe { Vec3Add::apply(&a, &b) };
97109
let d = Ciphered::decipher(&c, sk);
98110

99-
assert_eq!(
100-
Vec3 {
101-
x: 10,
102-
y: 10,
103-
z: 10
104-
},
105-
d
106-
);
111+
let s = unsafe { Vec3Sub::apply([]) };
112+
let _ds = Ciphered::decipher(&s, sk);
113+
114+
assert_eq!(Vec3 { x: 5, y: 7, z: 9 }, d);
107115
}

examples/uint_add.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ use std::time::Instant;
33

44
use homomorph::prelude::*;
55

6-
#[global_allocator]
7-
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
8-
96
const NUMBER_OF_TESTS: usize = 1_000;
107

118
fn main() {

src/cipher.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub unsafe trait ByteConvertible {
8383

8484
// All types that implement Copy and Sized can be converted to bytes
8585
// by simply reading stack data as bytes
86+
// TODO: Make it derivable ?
8687
unsafe impl<T: Copy + Sized> ByteConvertible for T {
8788
fn to_bytes(&self) -> Vec<u8> {
8889
let mut bytes = Vec::with_capacity(size_of::<T>());
@@ -109,7 +110,7 @@ unsafe impl<T: Copy + Sized> ByteConvertible for T {
109110
/// This can happen with overflows when adding two unsigned integers for example.
110111
fn from_bytes(bytes: &[u8]) -> Self {
111112
assert!(
112-
bytes.len() >= size_of::<T>(),
113+
bytes.len() == size_of::<T>(),
113114
"Invalid size of bytes for conversion: {} instead of {}",
114115
bytes.len(),
115116
size_of::<T>()
@@ -175,8 +176,7 @@ impl<T: ByteConvertible> Ciphered<T> {
175176
let tau = pk.len();
176177
let random_part = Self::part(tau);
177178

178-
let mut sum =
179-
unsafe { Polynomial::new_unchecked(vec![crate::polynomial::Coefficient::from(x)], 0) };
179+
let mut sum = Polynomial::new_from_bool(x);
180180
for i in 0..tau {
181181
let random = random_part[i / 8] & (1 << (i % 8));
182182
if random != 0 {
@@ -234,7 +234,7 @@ impl<T: ByteConvertible> Ciphered<T> {
234234
chunk
235235
.iter()
236236
.enumerate()
237-
.fold(0u8, |byte, (i, &bit)| byte | ((u8::from(bit)) << i))
237+
.fold(0, |byte, (i, &bit)| byte | ((u8::from(bit)) << i))
238238
})
239239
.collect::<Vec<_>>();
240240

@@ -265,6 +265,32 @@ mod tests {
265265
b: usize,
266266
}
267267

268+
#[test]
269+
fn test_byteconvertible() {
270+
let data = 0b1000_1010_u8;
271+
let bytes = data.to_bytes();
272+
let decrypted = u8::from_bytes(&bytes);
273+
assert_eq!(data, decrypted);
274+
275+
let data = MyStruct { a: 42, b: 69 };
276+
let bytes = data.to_bytes();
277+
let decrypted = MyStruct::from_bytes(&bytes);
278+
assert_eq!(data, decrypted);
279+
280+
let data = MyStruct { a: 42, b: 69 };
281+
let mut bytes = data.to_bytes();
282+
bytes[0] ^= 1;
283+
let decrypted = MyStruct::from_bytes(&bytes);
284+
assert_ne!(data, decrypted);
285+
}
286+
287+
#[test]
288+
#[should_panic = "Invalid size of bytes for conversion: 2 instead of 1"]
289+
fn test_byteconvertible_panic() {
290+
let bytes = [0u8; 2];
291+
let _ = u8::from_bytes(&bytes);
292+
}
293+
268294
#[test]
269295
fn test_cipher() {
270296
let parameters = Parameters::new(64, 32, 8, 32);
@@ -284,14 +310,24 @@ mod tests {
284310
let decrypted = ciphered.decipher(sk);
285311
assert_eq!(data, decrypted);
286312

287-
let data = "Hello, World!";
313+
let data = MyStruct { a: 42, b: 69 };
288314
let ciphered = Ciphered::cipher(&data, pk);
289315
let decrypted = ciphered.decipher(sk);
290316
assert_eq!(data, decrypted);
317+
}
291318

292-
let data = MyStruct { a: 42, b: 69 };
319+
#[test]
320+
fn test_deref() {
321+
let parameters = Parameters::new(64, 32, 8, 32);
322+
let mut context = Context::new(parameters);
323+
context.generate_secret_key();
324+
context.generate_public_key();
325+
let pk = context.get_public_key().unwrap();
326+
327+
let data = 0b1000_1010_u8;
293328
let ciphered = Ciphered::cipher(&data, pk);
294-
let decrypted = ciphered.decipher(sk);
295-
assert_eq!(data, decrypted);
329+
330+
// Assert it is possible to work with bits
331+
let _iter = ciphered.iter();
296332
}
297333
}

src/context.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use alloc::vec::Vec;
2424
/// `delta` is strictly less than `d`.
2525
///
2626
/// For more information, visit <https://github.com/mathisbot/homomorph-rust?tab=readme-ov-file#system>.
27+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2728
pub struct Parameters {
2829
d: u16,
2930
dp: u16,
@@ -110,7 +111,7 @@ impl SecretKey {
110111
let mut coeffs: Vec<_> =
111112
Vec::with_capacity(bytes.len() / size_of::<crate::polynomial::Coefficient>());
112113
for chunk in bytes.chunks(size_of::<crate::polynomial::Coefficient>()) {
113-
let mut array = [0u8; size_of::<crate::polynomial::Coefficient>()];
114+
let mut array = [0; size_of::<crate::polynomial::Coefficient>()];
114115
array[..chunk.len()].copy_from_slice(chunk);
115116
coeffs.push(crate::polynomial::Coefficient::from_le_bytes(array));
116117
}
@@ -200,7 +201,7 @@ impl PublicKey {
200201
let mut coeffs: Vec<_> =
201202
Vec::with_capacity(bytes.len() / size_of::<crate::polynomial::Coefficient>());
202203
for chunk in bytes.chunks(size_of::<crate::polynomial::Coefficient>()) {
203-
let mut array = [0u8; size_of::<crate::polynomial::Coefficient>()];
204+
let mut array = [0; size_of::<crate::polynomial::Coefficient>()];
204205
array[..chunk.len()].copy_from_slice(chunk);
205206
coeffs.push(crate::polynomial::Coefficient::from_le_bytes(array));
206207
}
@@ -266,6 +267,7 @@ impl PublicKey {
266267
}
267268

268269
/// The cipher context.
270+
#[derive(Clone, Debug)]
269271
pub struct Context {
270272
secret_key: Option<SecretKey>,
271273
public_key: Option<PublicKey>,
@@ -449,7 +451,7 @@ mod tests {
449451
let bytes = sk.get_bytes();
450452
let sk2 = SecretKey::new(&bytes);
451453

452-
assert_eq!(sk.s, sk2.s);
454+
assert_eq!(sk, sk2);
453455
}
454456

455457
#[test]
@@ -460,7 +462,7 @@ mod tests {
460462
let bytes = pk.get_bytes();
461463
let pk2 = PublicKey::new(&bytes);
462464

463-
assert_eq!(pk.list, pk2.list);
465+
assert_eq!(pk, pk2);
464466
}
465467

466468
#[test]
@@ -480,4 +482,12 @@ mod tests {
480482
assert_eq!(sk, sk2);
481483
assert_eq!(pk, pk2);
482484
}
485+
486+
#[test]
487+
#[should_panic = "Secret key not generated yet"]
488+
fn test_context_panic() {
489+
let params = Parameters::new(64, 32, 8, 32);
490+
let mut context = Context::new(params);
491+
context.generate_public_key();
492+
}
483493
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)