From f94fafeba46c3cea68d1b68e47e078e7c8b3cde2 Mon Sep 17 00:00:00 2001 From: Mihir Luthra Date: Fri, 25 Mar 2022 19:26:11 +0530 Subject: [PATCH] remove redundant result in byte_length & bit_length --- mbedtls/src/bignum/mod.rs | 14 +++++++------- mbedtls/src/ecp/mod.rs | 10 +++++----- mbedtls/src/pk/dsa/mod.rs | 8 ++++---- mbedtls/src/pk/rfc6979.rs | 4 ++-- mbedtls/tests/bignum.rs | 8 ++++---- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/mbedtls/src/bignum/mod.rs b/mbedtls/src/bignum/mod.rs index a560ef343..c35625a55 100644 --- a/mbedtls/src/bignum/mod.rs +++ b/mbedtls/src/bignum/mod.rs @@ -140,7 +140,7 @@ impl Mpi { } pub fn as_u32(&self) -> Result { - if self.bit_length()? > 32 { + if self.bit_length() > 32 { // Not exactly correct but close enough return Err(Error::MpiBufferTooSmall); } @@ -181,7 +181,7 @@ impl Mpi { /// Serialize the MPI as big endian binary data pub fn to_binary(&self) -> Result> { - let len = self.byte_length()?; + let len = self.byte_length(); let mut ret = vec![0u8; len]; unsafe { mpi_write_binary(&self.inner, ret.as_mut_ptr(), ret.len()).into_result() }?; Ok(ret) @@ -190,7 +190,7 @@ impl Mpi { /// Serialize the MPI as big endian binary data, padding to at least min_len /// bytes pub fn to_binary_padded(&self, min_len: usize) -> Result> { - let len = self.byte_length()?; + let len = self.byte_length(); let larger_len = if len < min_len { min_len } else { len }; let mut ret = vec![0u8; larger_len]; let pad_len = ret.len() - len; @@ -199,15 +199,15 @@ impl Mpi { } /// Return size of this MPI in bits - pub fn bit_length(&self) -> Result { + pub fn bit_length(&self) -> usize { let l = unsafe { mpi_bitlen(&self.inner) }; - Ok(l) + l } /// Return size of this MPI in bytes (rounded up) - pub fn byte_length(&self) -> Result { + pub fn byte_length(&self) -> usize { let l = unsafe { mpi_size(&self.inner) }; - Ok(l) + l } pub fn divrem(&self, other: &Mpi) -> Result<(Mpi, Mpi)> { diff --git a/mbedtls/src/ecp/mod.rs b/mbedtls/src/ecp/mod.rs index 4a21ffa2f..5d7a0130c 100644 --- a/mbedtls/src/ecp/mod.rs +++ b/mbedtls/src/ecp/mod.rs @@ -89,8 +89,8 @@ impl EcGroup { pub fn from_parameters(p: Mpi, a: Mpi, b: Mpi, g_x: Mpi, g_y: Mpi, order: Mpi) -> Result { let mut ret = Self::init(); - ret.inner.pbits = p.bit_length()?; - ret.inner.nbits = order.bit_length()?; + ret.inner.pbits = p.bit_length(); + ret.inner.nbits = order.bit_length(); ret.inner.h = 0; // indicate to mbedtls that the values are not static constants let zero = Mpi::new(0)?; @@ -259,7 +259,7 @@ impl EcPoint { let a = group.a()?; let b = group.b()?; - if bin.len() != (p.byte_length()? + 1) { + if bin.len() != (p.byte_length() + 1) { return Err(Error::EcpBadInputData); } @@ -459,7 +459,7 @@ mod tests { for group_id in &groups { let mut group = EcGroup::new(*group_id).unwrap(); - let p_len = group.p().unwrap().byte_length().unwrap(); + let p_len = group.p().unwrap().byte_length(); let generator = group.generator().unwrap(); @@ -491,7 +491,7 @@ mod tests { use std::str::FromStr; let mut secp256k1 = EcGroup::new(EcGroupId::SecP256K1).unwrap(); - let bitlen = secp256k1.p().unwrap().bit_length().unwrap(); + let bitlen = secp256k1.p().unwrap().bit_length(); let g = secp256k1.generator().unwrap(); assert_eq!(g.is_zero().unwrap(), false); diff --git a/mbedtls/src/pk/dsa/mod.rs b/mbedtls/src/pk/dsa/mod.rs index cb3430c59..015d09886 100644 --- a/mbedtls/src/pk/dsa/mod.rs +++ b/mbedtls/src/pk/dsa/mod.rs @@ -38,13 +38,13 @@ impl DsaParams { } fn key_size(&self) -> Result<(usize, usize)> { - Ok((self.p.bit_length()?, self.q.bit_length()?)) + Ok((self.p.bit_length(), self.q.bit_length())) } } fn reduce_mod_q(m: &[u8], q: &Mpi) -> Result { // First truncate bitlength then reduce (see FIPS 186-4 sec 4.6) - let q_bits = q.bit_length()?; + let q_bits = q.bit_length(); let m_bits = m.len() * 8; @@ -201,7 +201,7 @@ fn sample_secret_value(upper_bound: &Mpi, rng: &mut F) -> Result /* See FIPS 186-4 Appendix B.2.1 */ - let bits = upper_bound.bit_length()?; + let bits = upper_bound.bit_length(); let mut rnd_buf = vec![0u8; (bits + 7 + 64) / 8]; rng.random(&mut rnd_buf)?; let c = Mpi::from_binary(&rnd_buf)?; @@ -476,7 +476,7 @@ mod tests { let q = params.q.clone(); - let q_bits = q.bit_length().unwrap(); + let q_bits = q.bit_length(); let y = params.g.clone(); let pubkey = DsaPublicKey::from_components(params, y).unwrap(); diff --git a/mbedtls/src/pk/rfc6979.rs b/mbedtls/src/pk/rfc6979.rs index d18b41bda..302859e03 100644 --- a/mbedtls/src/pk/rfc6979.rs +++ b/mbedtls/src/pk/rfc6979.rs @@ -19,8 +19,8 @@ use crate::error::Result; use crate::hash::{MdInfo, Type}; pub(crate) fn generate_rfc6979_nonce(md: &MdInfo, x: &Mpi, q: &Mpi, digest_bytes: &[u8]) -> Result> { - let q_bits = q.bit_length()?; - let q_bytes = q.byte_length()?; + let q_bits = q.bit_length(); + let q_bytes = q.byte_length(); let mut digest = Mpi::from_binary(&digest_bytes)?; diff --git a/mbedtls/tests/bignum.rs b/mbedtls/tests/bignum.rs index dcd88c938..d1a9061cc 100644 --- a/mbedtls/tests/bignum.rs +++ b/mbedtls/tests/bignum.rs @@ -37,8 +37,8 @@ fn bignum_from_str() { fn bignum() { let six = Mpi::new(6).unwrap(); - assert_eq!(six.byte_length().unwrap(), 1); - assert_eq!(six.bit_length().unwrap(), 3); + assert_eq!(six.byte_length(), 1); + assert_eq!(six.bit_length(), 3); let six_bytes = six.to_binary().unwrap(); assert_eq!(six_bytes.len(), 1); @@ -51,8 +51,8 @@ fn bignum() { let bigger = Mpi::new(0x2a2f5dce).unwrap(); - assert_eq!(bigger.byte_length().unwrap(), 4); - assert_eq!(bigger.bit_length().unwrap(), 30); + assert_eq!(bigger.byte_length(), 4); + assert_eq!(bigger.bit_length(), 30); let b_bytes = bigger.to_binary().unwrap(); assert_eq!(b_bytes.len(), 4);