Skip to content

Commit 7d34aa5

Browse files
committed
fix(clippy): More aggressive clippy
1 parent f1deab9 commit 7d34aa5

File tree

9 files changed

+151
-140
lines changed

9 files changed

+151
-140
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
run: cargo run --release --example new_struct
3939

4040
- name: Run Clippy
41-
run: cargo clippy --all-targets --all-features --no-deps -- -D warnings
41+
run: cargo clippy --all-targets --all-features -- -D warnings --no-deps
4242

4343
- name: Check formatting
4444
run: cargo fmt -- --check

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
name = "homomorph"
33
version = "1.0.0"
44
edition = "2021"
5+
description = "A Rust implementation of an homomorphic encryption scheme"
6+
license = "MIT"
7+
repository = "https://github.com/mathisbot/homomorph-rust"
58

69
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
710

@@ -15,7 +18,7 @@ rand = "0.8.5"
1518

1619
[features]
1720
default = []
18-
no_rand = ["getrandom/custom"]
21+
custom_rand = ["getrandom/custom"]
1922

2023
[profile.release]
2124
codegen-units = 1

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ I do not intend to publish the crate on `crates.io`.
4646
4747
## Features
4848
49-
- `no_rand`: Allows to implement a fallback method to `getrandom` on unsupported targets
49+
- `custom_rand`: Allows to implement a fallback method to `getrandom` on unsupported targets
5050
5151
## Bare metal
5252
5353
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.
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 `no_rand` feature.
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.
5656

5757
## Benchmarks
5858

src/cipher.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::polynomial::Polynomial;
22
use crate::{PublicKey, SecretKey};
33

44
use alloc::vec::Vec;
5+
use core::mem::MaybeUninit;
56
use core::ops::Deref;
67
use core::ptr::copy_nonoverlapping as memcpy;
78

@@ -12,46 +13,52 @@ use core::ptr::copy_nonoverlapping as memcpy;
1213
pub struct CipheredBit(Polynomial);
1314

1415
impl CipheredBit {
16+
#[must_use]
1517
/// Returns the null bit
1618
///
1719
/// Properties of the system allow you to blindly
1820
/// use it as if it were a ciphered bit
1921
pub fn zero() -> Self {
20-
CipheredBit(Polynomial::null())
22+
Self(Polynomial::null())
2123
}
2224

25+
#[must_use]
2326
/// Returns the bit 1
2427
///
2528
/// Properties of the system allow you to blindly
2629
/// use it as if it were a ciphered bit
2730
pub fn one() -> Self {
28-
CipheredBit(Polynomial::monomial(0))
31+
Self(Polynomial::monomial(0))
2932
}
3033

34+
#[must_use]
3135
/// Apply the AND gate to two ciphered bits
3236
///
3337
/// In the backend, this is done by multiplying the two polynomials
3438
pub fn and(&self, other: &Self) -> Self {
35-
CipheredBit(self.0.mul(&other.0))
39+
Self(self.0.mul(&other.0))
3640
}
3741

42+
#[must_use]
3843
/// Apply the XOR gate to two ciphered bits
3944
///
4045
/// In the backend, this is done by adding the two polynomials
4146
pub fn xor(&self, other: &Self) -> Self {
42-
CipheredBit(self.0.add(&other.0))
47+
Self(self.0.add(&other.0))
4348
}
4449

50+
#[must_use]
4551
/// Apply the OR gate to two ciphered bits
4652
///
4753
/// In the backend, this is done by adding the two polynomials and their product
4854
///
4955
/// Keep in mind that it may be faster to simplify the overall expression of your operation
5056
/// instead of using the OR gate
5157
pub fn or(&self, other: &Self) -> Self {
52-
CipheredBit(self.0.add(&other.0).add(&self.0.mul(&other.0)))
58+
Self(self.0.add(&other.0).add(&self.0.mul(&other.0)))
5359
}
5460

61+
#[must_use]
5562
/// Apply the NOT gate to a ciphered bit
5663
///
5764
/// In the backend, this is done by adding the polynomial to the unit polynomial
@@ -81,7 +88,7 @@ unsafe impl<T: Copy + Sized> ByteConvertible for T {
8188
let mut bytes = Vec::with_capacity(size_of::<T>());
8289
unsafe {
8390
memcpy(
84-
self as *const T as *const u8,
91+
core::ptr::from_ref(self).cast::<u8>(),
8592
bytes.as_mut_ptr(),
8693
size_of::<T>(),
8794
);
@@ -101,17 +108,20 @@ unsafe impl<T: Copy + Sized> ByteConvertible for T {
101108
/// If the byte array is too big, data will be truncated.
102109
/// This can happen with overflows when adding two unsigned integers for example.
103110
fn from_bytes(bytes: &[u8]) -> Self {
104-
if bytes.len() < size_of::<T>() {
105-
panic!(
106-
"Invalid size of bytes for conversion: {} instead of {}",
107-
bytes.len(),
108-
size_of::<T>()
109-
);
110-
}
111-
112-
let mut data = core::mem::MaybeUninit::uninit();
111+
assert!(
112+
bytes.len() >= size_of::<T>(),
113+
"Invalid size of bytes for conversion: {} instead of {}",
114+
bytes.len(),
115+
size_of::<T>()
116+
);
117+
118+
let mut data: MaybeUninit<T> = core::mem::MaybeUninit::uninit();
113119
unsafe {
114-
memcpy(bytes.as_ptr(), data.as_mut_ptr() as *mut u8, size_of::<T>());
120+
memcpy(
121+
bytes.as_ptr(),
122+
data.as_mut_ptr().cast::<u8>(),
123+
size_of::<T>(),
124+
);
115125
data.assume_init()
116126
}
117127
}
@@ -125,6 +135,7 @@ pub struct Ciphered<T: ByteConvertible> {
125135
}
126136

127137
impl<T: ByteConvertible> Ciphered<T> {
138+
#[must_use]
128139
/// This function is used to create a new `Ciphered` object
129140
///
130141
/// This function should only be used in unsafe contexts
@@ -143,6 +154,7 @@ impl<T: ByteConvertible> Ciphered<T> {
143154
}
144155
}
145156

157+
#[must_use]
146158
// u8 is used instead of bool because they are the same size
147159
// while u8 can store 8 times more information
148160
fn part(tau: usize) -> Vec<u8> {
@@ -156,13 +168,15 @@ impl<T: ByteConvertible> Ciphered<T> {
156168
part
157169
}
158170

171+
#[must_use]
159172
// See https://github.com/mathisbot/homomorph-rust?tab=readme-ov-file#system
160173
fn cipher_bit(x: bool, pk: &PublicKey) -> CipheredBit {
161174
let pk = pk.get_polynomials();
162175
let tau = pk.len();
163176
let random_part = Self::part(tau);
164177

165-
let mut sum = unsafe { Polynomial::new_unchecked(vec![if x { 1 } else { 0 }], 0) };
178+
let mut sum =
179+
unsafe { Polynomial::new_unchecked(vec![crate::polynomial::Coefficient::from(x)], 0) };
166180
for i in 0..tau {
167181
let random = random_part[i / 8] & (1 << (i % 8));
168182
if random != 0 {
@@ -173,6 +187,7 @@ impl<T: ByteConvertible> Ciphered<T> {
173187
CipheredBit(sum)
174188
}
175189

190+
#[must_use]
176191
/// Ciphers data
177192
///
178193
/// ## Arguments
@@ -192,13 +207,15 @@ impl<T: ByteConvertible> Ciphered<T> {
192207
}
193208
}
194209

210+
#[must_use]
195211
// See https://github.com/mathisbot/homomorph-rust?tab=readme-ov-file#system
196212
fn decipher_bit(c_bit: &CipheredBit, sk: &SecretKey) -> bool {
197213
let sk = sk.get_polynomial();
198214
let remainder = c_bit.0.rem(sk);
199215
remainder.evaluate(false)
200216
}
201217

218+
#[must_use]
202219
/// Deciphers data
203220
///
204221
/// ## Arguments
@@ -217,7 +234,7 @@ impl<T: ByteConvertible> Ciphered<T> {
217234
chunk
218235
.iter()
219236
.enumerate()
220-
.fold(0u8, |byte, (i, &bit)| byte | ((bit as u8) << i))
237+
.fold(0u8, |byte, (i, &bit)| byte | ((u8::from(bit)) << i))
221238
})
222239
.collect::<Vec<_>>();
223240

@@ -241,6 +258,13 @@ mod tests {
241258
use super::*;
242259
use crate::{Context, Parameters};
243260

261+
#[derive(Copy, Clone, Debug, PartialEq)]
262+
#[repr(C)]
263+
struct MyStruct {
264+
a: usize,
265+
b: usize,
266+
}
267+
244268
#[test]
245269
fn test_cipher() {
246270
let parameters = Parameters::new(64, 32, 8, 32);
@@ -250,7 +274,7 @@ mod tests {
250274
let sk = context.get_secret_key().unwrap();
251275
let pk = context.get_public_key().unwrap();
252276

253-
let data = 0b10001010u8;
277+
let data = 0b1000_1010_u8;
254278
let ciphered = Ciphered::cipher(&data, pk);
255279
let decrypted = ciphered.decipher(sk);
256280
assert_eq!(data, decrypted);
@@ -265,12 +289,6 @@ mod tests {
265289
let decrypted = ciphered.decipher(sk);
266290
assert_eq!(data, decrypted);
267291

268-
#[derive(Copy, Clone, Debug, PartialEq)]
269-
#[repr(C)]
270-
struct MyStruct {
271-
a: usize,
272-
b: usize,
273-
}
274292
let data = MyStruct { a: 42, b: 69 };
275293
let ciphered = Ciphered::cipher(&data, pk);
276294
let decrypted = ciphered.decipher(sk);

0 commit comments

Comments
 (0)