Skip to content

Commit

Permalink
remove redundant result in byte_length & bit_length
Browse files Browse the repository at this point in the history
  • Loading branch information
MihirLuthra authored and Taowyoo committed Oct 27, 2023
1 parent 847011e commit f94fafe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions mbedtls/src/bignum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Mpi {
}

pub fn as_u32(&self) -> Result<u32> {
if self.bit_length()? > 32 {
if self.bit_length() > 32 {
// Not exactly correct but close enough
return Err(Error::MpiBufferTooSmall);
}
Expand Down Expand Up @@ -181,7 +181,7 @@ impl Mpi {

/// Serialize the MPI as big endian binary data
pub fn to_binary(&self) -> Result<Vec<u8>> {
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)
Expand All @@ -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<Vec<u8>> {
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;
Expand All @@ -199,15 +199,15 @@ impl Mpi {
}

/// Return size of this MPI in bits
pub fn bit_length(&self) -> Result<usize> {
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<usize> {
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)> {
Expand Down
10 changes: 5 additions & 5 deletions mbedtls/src/ecp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EcGroup> {
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)?;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions mbedtls/src/pk/dsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mpi> {
// 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;

Expand Down Expand Up @@ -201,7 +201,7 @@ fn sample_secret_value<F: Random>(upper_bound: &Mpi, rng: &mut F) -> Result<Mpi>
/*
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)?;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions mbedtls/src/pk/rfc6979.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> {
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)?;

Expand Down
8 changes: 4 additions & 4 deletions mbedtls/tests/bignum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit f94fafe

Please sign in to comment.