Skip to content

Commit

Permalink
fix: clippy lints (#93)
Browse files Browse the repository at this point in the history
- all `--all-features` to clippy lints in CI
- fix clippy lints
- adds a few docstrings that were missing.
  • Loading branch information
sdbondi authored Apr 4, 2022
1 parent e5fd715 commit fa0d728
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clippy-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: lints
args: clippy --all-targets
args: clippy --all-targets --all-features
17 changes: 13 additions & 4 deletions src/ffi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
convert::TryFrom,
os::raw::{c_char, c_int},
ptr,
slice,
Expand All @@ -35,8 +36,12 @@ pub unsafe extern "C" fn lookup_error_message(code: c_int, buffer: *mut c_char,
return NULL_POINTER;
}

let error_message = get_error_message(code).to_string();
let buffer = slice::from_raw_parts_mut(buffer as *mut u8, length as usize);
let error_message = get_error_message(code);
let length = match usize::try_from(length) {
Ok(l) => l,
Err(_) => return INTEGER_OVERFLOW,
};
let buffer = slice::from_raw_parts_mut(buffer as *mut u8, length);

if error_message.len() >= buffer.len() {
return BUFFER_TOO_SMALL;
Expand All @@ -48,7 +53,9 @@ pub unsafe extern "C" fn lookup_error_message(code: c_int, buffer: *mut c_char,
// accidentally read into garbage.
buffer[error_message.len()] = 0;

error_message.len() as c_int
// Explicitly truncate usize to c_int (not possible anyway) because adding #[allow(clippy::cast-possible-wrap)]
// attribute requires unstable rust feature
c_int::try_from(error_message.len()).unwrap_or(c_int::MAX)
}

pub const OK: i32 = 0;
Expand All @@ -57,6 +64,7 @@ pub const BUFFER_TOO_SMALL: i32 = -2;
pub const INVALID_SECRET_KEY_SER: i32 = -1000;
pub const SIGNING_ERROR: i32 = -1100;
pub const STR_CONV_ERR: i32 = -2000;
pub const INTEGER_OVERFLOW: i32 = -3000;

pub fn get_error_message(code: i32) -> &'static str {
match code {
Expand All @@ -66,6 +74,7 @@ pub fn get_error_message(code: i32) -> &'static str {
INVALID_SECRET_KEY_SER => "Invalid secret key representation.",
SIGNING_ERROR => "Error creating signature",
STR_CONV_ERR => "String conversion error",
INTEGER_OVERFLOW => "Integer overflowed",
_ => "Unknown error code.",
}
}
Expand All @@ -92,7 +101,7 @@ mod test {
unsafe {
let mut buffer = [0u8; 1000];
assert_eq!(
lookup_error_message(OK, buffer.as_mut_ptr() as *mut i8, 1000) as usize,
usize::try_from(lookup_error_message(OK, buffer.as_mut_ptr() as *mut i8, 1000)).unwrap(),
get_error_message(OK).len()
);
assert_eq!(
Expand Down
100 changes: 44 additions & 56 deletions src/ffi/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ mod test {
#[test]
pub fn test_random_keypair_with_valid_params() {
let mut priv_key: KeyArray = [0; KEY_LENGTH];
let priv_key_before = priv_key.clone();
let priv_key_before = priv_key;
let mut pub_key: KeyArray = [0; KEY_LENGTH];

// Public keys is null. A new private key is set
Expand All @@ -304,7 +304,7 @@ mod test {
}
assert_ne!(priv_key, priv_key_before);

let priv_key_before = priv_key.clone();
let priv_key_before = priv_key;
// Both are not null.
unsafe {
random_keypair(&mut priv_key, &mut pub_key);
Expand Down Expand Up @@ -361,50 +361,41 @@ mod test {
let mut signature = [0; KEY_LENGTH];
let mut err_code = 0i32;
unsafe {
assert_eq!(
verify(
null_mut(),
msg.as_ptr() as *const c_char,
&mut pub_nonce,
&mut signature,
&mut err_code
),
false
);
assert_eq!(
verify(&pub_key, null_mut(), &mut pub_nonce, &mut signature, &mut err_code),
false
);
assert_eq!(
verify(
&pub_key,
msg.as_ptr() as *const c_char,
null_mut(),
&mut signature,
&mut err_code
),
false
);
assert_eq!(
verify(
&pub_key,
msg.as_ptr() as *const c_char,
&mut pub_nonce,
null_mut(),
&mut err_code
),
false
);
assert_eq!(
verify(
&pub_key,
msg.as_ptr() as *const c_char,
&mut pub_nonce,
&mut signature,
null_mut()
),
false
);
assert!(!verify(
null_mut(),
msg.as_ptr() as *const c_char,
&mut pub_nonce,
&mut signature,
&mut err_code
),);
assert!(!verify(
&pub_key,
null_mut(),
&mut pub_nonce,
&mut signature,
&mut err_code
),);
assert!(!verify(
&pub_key,
msg.as_ptr() as *const c_char,
null_mut(),
&mut signature,
&mut err_code
),);
assert!(!verify(
&pub_key,
msg.as_ptr() as *const c_char,
&mut pub_nonce,
null_mut(),
&mut err_code
),);
assert!(!verify(
&pub_key,
msg.as_ptr() as *const c_char,
&mut pub_nonce,
&mut signature,
null_mut()
),);
}
}

Expand All @@ -419,16 +410,13 @@ mod test {
unsafe {
random_keypair(&mut priv_key, &mut pub_key);
sign(&priv_key, msg.as_ptr() as *const c_char, &mut pub_nonce, &mut signature);
assert_eq!(
verify(
&pub_key,
msg.as_ptr() as *const c_char,
&mut pub_nonce,
&mut signature,
&mut err_code
),
true
);
assert!(verify(
&pub_key,
msg.as_ptr() as *const c_char,
&mut pub_nonce,
&mut signature,
&mut err_code
));
}
}
}
1 change: 1 addition & 0 deletions src/hash/blake2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use digest::{
pub struct Blake256(VarBlake2b);

impl Blake256 {
/// Constructs a `Blake256` hashing context with parameters that allow hash keying, salting and personalization.
pub fn with_params(key: &[u8], salt: &[u8], persona: &[u8]) -> Self {
Self(VarBlake2b::with_params(
key,
Expand Down
2 changes: 1 addition & 1 deletion src/musig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ where
/// produces a byte array of the correct length.
fn calculate_common<D: Digest>(&self) -> K {
let mut common = D::new();
for k in self.pub_keys.iter() {
for k in &self.pub_keys {
common = common.chain(k.as_bytes());
}
K::from_bytes(&common.finalize())
Expand Down
7 changes: 6 additions & 1 deletion src/range_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait RangeProofService {
proof_message: &[u8; REWIND_USER_MESSAGE_LENGTH],
) -> Result<Self::P, RangeProofError>;

/// Rewind a rewindable range proof to reveal the committed value and the 19 byte proof message
/// Rewind a rewindable range proof to reveal the committed value and the 19 byte proof message.
fn rewind_proof_value_only(
&self,
proof: &Self::P,
Expand All @@ -96,13 +96,15 @@ pub trait RangeProofService {
) -> Result<FullRewindResult<Self::K>, RangeProofError>;
}

/// Rewind data extracted from a rangeproof containing the committed value and the 19 byte proof message.
#[derive(Debug, PartialEq)]
pub struct RewindResult {
pub committed_value: u64,
pub proof_message: [u8; REWIND_USER_MESSAGE_LENGTH],
}

impl RewindResult {
/// Creates a new `RewindResult`
pub fn new(committed_value: u64, proof_message: [u8; REWIND_USER_MESSAGE_LENGTH]) -> Self {
Self {
committed_value,
Expand All @@ -111,6 +113,8 @@ impl RewindResult {
}
}

/// Rewind data extracted from a rangeproof containing the committed value, a 19 byte proof message and the blinding
/// factor.
#[derive(Debug, PartialEq)]
pub struct FullRewindResult<K>
where K: SecretKey
Expand All @@ -123,6 +127,7 @@ where K: SecretKey
impl<K> FullRewindResult<K>
where K: SecretKey
{
/// Creates a new `FullRewindResult`
pub fn new(committed_value: u64, proof_message: [u8; REWIND_USER_MESSAGE_LENGTH], blinding_factor: K) -> Self {
Self {
committed_value,
Expand Down
Loading

0 comments on commit fa0d728

Please sign in to comment.