summaryrefslogtreecommitdiffstats
path: root/vendor/crypto-bigint/src/uint/array.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/crypto-bigint/src/uint/array.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/crypto-bigint/src/uint/array.rs')
-rw-r--r--vendor/crypto-bigint/src/uint/array.rs189
1 files changed, 189 insertions, 0 deletions
diff --git a/vendor/crypto-bigint/src/uint/array.rs b/vendor/crypto-bigint/src/uint/array.rs
new file mode 100644
index 000000000..cba2b3716
--- /dev/null
+++ b/vendor/crypto-bigint/src/uint/array.rs
@@ -0,0 +1,189 @@
+//! `generic-array` integration with `UInt`.
+// TODO(tarcieri): completely phase out `generic-array` when const generics are powerful enough
+
+use crate::{ArrayDecoding, ArrayEncoding, ByteArray};
+use generic_array::{typenum, GenericArray};
+
+macro_rules! impl_uint_array_encoding {
+ ($(($uint:ident, $bytes:path)),+) => {
+ $(
+ #[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))]
+ impl ArrayEncoding for super::$uint {
+ type ByteSize = $bytes;
+
+ #[inline]
+ fn from_be_byte_array(bytes: ByteArray<Self>) -> Self {
+ Self::from_be_slice(&bytes)
+ }
+
+ #[inline]
+ fn from_le_byte_array(bytes: ByteArray<Self>) -> Self {
+ Self::from_le_slice(&bytes)
+ }
+
+ #[inline]
+ fn to_be_byte_array(&self) -> ByteArray<Self> {
+ let mut result = GenericArray::default();
+ self.write_be_bytes(&mut result);
+ result
+ }
+
+ #[inline]
+ fn to_le_byte_array(&self) -> ByteArray<Self> {
+ let mut result = GenericArray::default();
+ self.write_le_bytes(&mut result);
+ result
+ }
+ }
+
+ #[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))]
+ impl ArrayDecoding for GenericArray<u8, $bytes> {
+ type Output = super::$uint;
+
+ fn into_uint_be(self) -> Self::Output {
+ Self::Output::from_be_byte_array(self)
+ }
+
+ fn into_uint_le(self) -> Self::Output {
+ Self::Output::from_le_byte_array(self)
+ }
+ }
+ )+
+ };
+}
+
+// TODO(tarcieri): use `const_evaluatable_checked` when stable to make generic around bits.
+impl_uint_array_encoding! {
+ (U64, typenum::U8),
+ (U128, typenum::U16),
+ (U192, typenum::U24),
+ (U256, typenum::U32),
+ (U384, typenum::U48),
+ (U448, typenum::U56),
+ (U512, typenum::U64),
+ (U576, typenum::U72),
+ (U768, typenum::U96),
+ (U896, typenum::U112),
+ (U1024, typenum::U128),
+ (U1536, typenum::U192),
+ (U1792, typenum::U224),
+ (U2048, typenum::U256),
+ (U3072, typenum::U384),
+ (U3584, typenum::U448),
+ (U4096, typenum::U512),
+ (U6144, typenum::U768),
+ (U8192, typenum::U1024)
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::{ArrayDecoding, ArrayEncoding, Limb};
+ use hex_literal::hex;
+
+ #[cfg(target_pointer_width = "32")]
+ use crate::U64 as UIntEx;
+
+ #[cfg(target_pointer_width = "64")]
+ use crate::U128 as UIntEx;
+
+ /// Byte array that corresponds to `UIntEx`
+ type ByteArray = crate::ByteArray<UIntEx>;
+
+ #[test]
+ #[cfg(target_pointer_width = "32")]
+ fn from_be_byte_array() {
+ let n = UIntEx::from_be_byte_array(hex!("0011223344556677").into());
+ assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "64")]
+ fn from_be_byte_array() {
+ let n = UIntEx::from_be_byte_array(hex!("00112233445566778899aabbccddeeff").into());
+ assert_eq!(
+ n.limbs(),
+ &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)]
+ );
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "32")]
+ fn from_le_byte_array() {
+ let n = UIntEx::from_le_byte_array(hex!("7766554433221100").into());
+ assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "64")]
+ fn from_le_byte_array() {
+ let n = UIntEx::from_le_byte_array(hex!("ffeeddccbbaa99887766554433221100").into());
+ assert_eq!(
+ n.limbs(),
+ &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)]
+ );
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "32")]
+ fn to_be_byte_array() {
+ let expected_bytes = ByteArray::from(hex!("0011223344556677"));
+ let actual_bytes = UIntEx::from_be_byte_array(expected_bytes).to_be_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "64")]
+ fn to_be_byte_array() {
+ let expected_bytes = ByteArray::from(hex!("00112233445566778899aabbccddeeff"));
+ let actual_bytes = UIntEx::from_be_byte_array(expected_bytes).to_be_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "32")]
+ fn to_le_byte_array() {
+ let expected_bytes = ByteArray::from(hex!("7766554433221100"));
+ let actual_bytes = UIntEx::from_le_byte_array(expected_bytes).to_le_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "64")]
+ fn to_le_byte_array() {
+ let expected_bytes = ByteArray::from(hex!("ffeeddccbbaa99887766554433221100"));
+ let actual_bytes = UIntEx::from_le_byte_array(expected_bytes).to_le_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "32")]
+ fn into_uint_be() {
+ let expected_bytes = ByteArray::from(hex!("0011223344556677"));
+ let actual_bytes = expected_bytes.into_uint_be().to_be_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "64")]
+ fn into_uint_be() {
+ let expected_bytes = ByteArray::from(hex!("00112233445566778899aabbccddeeff"));
+ let actual_bytes = expected_bytes.into_uint_be().to_be_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "32")]
+ fn into_uint_le() {
+ let expected_bytes = ByteArray::from(hex!("7766554433221100"));
+ let actual_bytes = expected_bytes.into_uint_le().to_le_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "64")]
+ fn into_uint_le() {
+ let expected_bytes = ByteArray::from(hex!("ffeeddccbbaa99887766554433221100"));
+ let actual_bytes = expected_bytes.into_uint_le().to_le_byte_array();
+ assert_eq!(expected_bytes, actual_bytes);
+ }
+}