diff options
Diffstat (limited to 'vendor/crypto-bigint/src/uint/encoding.rs')
-rw-r--r-- | vendor/crypto-bigint/src/uint/encoding.rs | 104 |
1 files changed, 51 insertions, 53 deletions
diff --git a/vendor/crypto-bigint/src/uint/encoding.rs b/vendor/crypto-bigint/src/uint/encoding.rs index a83976238..5431b8166 100644 --- a/vendor/crypto-bigint/src/uint/encoding.rs +++ b/vendor/crypto-bigint/src/uint/encoding.rs @@ -1,4 +1,4 @@ -//! Const-friendly decoding operations for [`UInt`] +//! Const-friendly decoding operations for [`Uint`] #[cfg(all(feature = "der", feature = "generic-array"))] mod der; @@ -6,51 +6,51 @@ mod der; #[cfg(feature = "rlp")] mod rlp; -use super::UInt; +use super::Uint; use crate::{Encoding, Limb, Word}; -impl<const LIMBS: usize> UInt<LIMBS> { - /// Create a new [`UInt`] from the provided big endian bytes. +impl<const LIMBS: usize> Uint<LIMBS> { + /// Create a new [`Uint`] from the provided big endian bytes. pub const fn from_be_slice(bytes: &[u8]) -> Self { assert!( - bytes.len() == Limb::BYTE_SIZE * LIMBS, + bytes.len() == Limb::BYTES * LIMBS, "bytes are not the expected size" ); let mut res = [Limb::ZERO; LIMBS]; - let mut buf = [0u8; Limb::BYTE_SIZE]; + let mut buf = [0u8; Limb::BYTES]; let mut i = 0; while i < LIMBS { let mut j = 0; - while j < Limb::BYTE_SIZE { - buf[j] = bytes[i * Limb::BYTE_SIZE + j]; + while j < Limb::BYTES { + buf[j] = bytes[i * Limb::BYTES + j]; j += 1; } res[LIMBS - i - 1] = Limb(Word::from_be_bytes(buf)); i += 1; } - UInt::new(res) + Uint::new(res) } - /// Create a new [`UInt`] from the provided big endian hex string. + /// Create a new [`Uint`] from the provided big endian hex string. pub const fn from_be_hex(hex: &str) -> Self { let bytes = hex.as_bytes(); assert!( - bytes.len() == Limb::BYTE_SIZE * LIMBS * 2, + bytes.len() == Limb::BYTES * LIMBS * 2, "hex string is not the expected size" ); let mut res = [Limb::ZERO; LIMBS]; - let mut buf = [0u8; Limb::BYTE_SIZE]; + let mut buf = [0u8; Limb::BYTES]; let mut i = 0; while i < LIMBS { let mut j = 0; - while j < Limb::BYTE_SIZE { - let offset = (i * Limb::BYTE_SIZE + j) * 2; + while j < Limb::BYTES { + let offset = (i * Limb::BYTES + j) * 2; buf[j] = decode_hex_byte([bytes[offset], bytes[offset + 1]]); j += 1; } @@ -58,50 +58,50 @@ impl<const LIMBS: usize> UInt<LIMBS> { i += 1; } - UInt::new(res) + Uint::new(res) } - /// Create a new [`UInt`] from the provided little endian bytes. + /// Create a new [`Uint`] from the provided little endian bytes. pub const fn from_le_slice(bytes: &[u8]) -> Self { assert!( - bytes.len() == Limb::BYTE_SIZE * LIMBS, + bytes.len() == Limb::BYTES * LIMBS, "bytes are not the expected size" ); let mut res = [Limb::ZERO; LIMBS]; - let mut buf = [0u8; Limb::BYTE_SIZE]; + let mut buf = [0u8; Limb::BYTES]; let mut i = 0; while i < LIMBS { let mut j = 0; - while j < Limb::BYTE_SIZE { - buf[j] = bytes[i * Limb::BYTE_SIZE + j]; + while j < Limb::BYTES { + buf[j] = bytes[i * Limb::BYTES + j]; j += 1; } res[i] = Limb(Word::from_le_bytes(buf)); i += 1; } - UInt::new(res) + Uint::new(res) } - /// Create a new [`UInt`] from the provided little endian hex string. + /// Create a new [`Uint`] from the provided little endian hex string. pub const fn from_le_hex(hex: &str) -> Self { let bytes = hex.as_bytes(); assert!( - bytes.len() == Limb::BYTE_SIZE * LIMBS * 2, + bytes.len() == Limb::BYTES * LIMBS * 2, "bytes are not the expected size" ); let mut res = [Limb::ZERO; LIMBS]; - let mut buf = [0u8; Limb::BYTE_SIZE]; + let mut buf = [0u8; Limb::BYTES]; let mut i = 0; while i < LIMBS { let mut j = 0; - while j < Limb::BYTE_SIZE { - let offset = (i * Limb::BYTE_SIZE + j) * 2; + while j < Limb::BYTES { + let offset = (i * Limb::BYTES + j) * 2; buf[j] = decode_hex_byte([bytes[offset], bytes[offset + 1]]); j += 1; } @@ -109,39 +109,37 @@ impl<const LIMBS: usize> UInt<LIMBS> { i += 1; } - UInt::new(res) + Uint::new(res) } - /// Serialize this [`UInt`] as big-endian, writing it into the provided + /// Serialize this [`Uint`] as big-endian, writing it into the provided /// byte slice. #[inline] - #[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))] pub(crate) fn write_be_bytes(&self, out: &mut [u8]) { - debug_assert_eq!(out.len(), Limb::BYTE_SIZE * LIMBS); + debug_assert_eq!(out.len(), Limb::BYTES * LIMBS); for (src, dst) in self .limbs .iter() .rev() .cloned() - .zip(out.chunks_exact_mut(Limb::BYTE_SIZE)) + .zip(out.chunks_exact_mut(Limb::BYTES)) { dst.copy_from_slice(&src.to_be_bytes()); } } - /// Serialize this [`UInt`] as little-endian, writing it into the provided + /// Serialize this [`Uint`] as little-endian, writing it into the provided /// byte slice. #[inline] - #[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))] pub(crate) fn write_le_bytes(&self, out: &mut [u8]) { - debug_assert_eq!(out.len(), Limb::BYTE_SIZE * LIMBS); + debug_assert_eq!(out.len(), Limb::BYTES * LIMBS); for (src, dst) in self .limbs .iter() .cloned() - .zip(out.chunks_exact_mut(Limb::BYTE_SIZE)) + .zip(out.chunks_exact_mut(Limb::BYTES)) { dst.copy_from_slice(&src.to_le_bytes()); } @@ -183,26 +181,26 @@ mod tests { use {crate::U128, alloc::format}; #[cfg(target_pointer_width = "32")] - use crate::U64 as UIntEx; + use crate::U64 as UintEx; #[cfg(target_pointer_width = "64")] - use crate::U128 as UIntEx; + use crate::U128 as UintEx; #[test] #[cfg(target_pointer_width = "32")] fn from_be_slice() { let bytes = hex!("0011223344556677"); - let n = UIntEx::from_be_slice(&bytes); - assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); + let n = UintEx::from_be_slice(&bytes); + assert_eq!(n.as_limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_be_slice() { let bytes = hex!("00112233445566778899aabbccddeeff"); - let n = UIntEx::from_be_slice(&bytes); + let n = UintEx::from_be_slice(&bytes); assert_eq!( - n.limbs(), + n.as_limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] ); } @@ -211,17 +209,17 @@ mod tests { #[cfg(target_pointer_width = "32")] fn from_le_slice() { let bytes = hex!("7766554433221100"); - let n = UIntEx::from_le_slice(&bytes); - assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); + let n = UintEx::from_le_slice(&bytes); + assert_eq!(n.as_limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_le_slice() { let bytes = hex!("ffeeddccbbaa99887766554433221100"); - let n = UIntEx::from_le_slice(&bytes); + let n = UintEx::from_le_slice(&bytes); assert_eq!( - n.limbs(), + n.as_limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] ); } @@ -229,16 +227,16 @@ mod tests { #[test] #[cfg(target_pointer_width = "32")] fn from_be_hex() { - let n = UIntEx::from_be_hex("0011223344556677"); - assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); + let n = UintEx::from_be_hex("0011223344556677"); + assert_eq!(n.as_limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_be_hex() { - let n = UIntEx::from_be_hex("00112233445566778899aabbccddeeff"); + let n = UintEx::from_be_hex("00112233445566778899aabbccddeeff"); assert_eq!( - n.limbs(), + n.as_limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] ); } @@ -246,16 +244,16 @@ mod tests { #[test] #[cfg(target_pointer_width = "32")] fn from_le_hex() { - let n = UIntEx::from_le_hex("7766554433221100"); - assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]); + let n = UintEx::from_le_hex("7766554433221100"); + assert_eq!(n.as_limbs(), &[Limb(0x44556677), Limb(0x00112233)]); } #[test] #[cfg(target_pointer_width = "64")] fn from_le_hex() { - let n = UIntEx::from_le_hex("ffeeddccbbaa99887766554433221100"); + let n = UintEx::from_le_hex("ffeeddccbbaa99887766554433221100"); assert_eq!( - n.limbs(), + n.as_limbs(), &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)] ); } |