crypto_bigint/uint/
macros.rs

1// TODO(tarcieri): use `generic_const_exprs` when stable to make generic around bits.
2macro_rules! impl_uint_aliases {
3    ($(($name:ident, $bits:expr, $doc:expr)),+) => {
4        $(
5            #[doc = $doc]
6            #[doc="unsigned big integer."]
7            pub type $name = Uint<{nlimbs!($bits)}>;
8
9            impl Encoding for $name {
10
11                type Repr = [u8; $bits / 8];
12
13                #[inline]
14                fn from_be_bytes(bytes: Self::Repr) -> Self {
15                    Self::from_be_slice(&bytes)
16                }
17
18                #[inline]
19                fn from_le_bytes(bytes: Self::Repr) -> Self {
20                    Self::from_le_slice(&bytes)
21                }
22
23                #[inline]
24                fn to_be_bytes(&self) -> Self::Repr {
25                    let mut result = [0u8; $bits / 8];
26                    self.write_be_bytes(&mut result);
27                    result
28                }
29
30                #[inline]
31                fn to_le_bytes(&self) -> Self::Repr {
32                    let mut result = [0u8; $bits / 8];
33                    self.write_le_bytes(&mut result);
34                    result
35                }
36            }
37        )+
38     };
39}
40
41macro_rules! impl_uint_concat_split_mixed {
42    ($name:ident, $size:literal) => {
43        impl $crate::traits::ConcatMixed<Uint<{ U64::LIMBS * $size }>> for Uint<{ <$name>::LIMBS - U64::LIMBS * $size }>
44        {
45            type MixedOutput = $name;
46
47            fn concat_mixed(&self, lo: &Uint<{ U64::LIMBS * $size }>) -> Self::MixedOutput {
48                $crate::uint::concat::concat_mixed(lo, self)
49            }
50        }
51
52        impl $crate::traits::SplitMixed<Uint<{ U64::LIMBS * $size }>, Uint<{ <$name>::LIMBS - U64::LIMBS * $size }>> for $name
53        {
54            fn split_mixed(&self) -> (Uint<{ U64::LIMBS * $size }>, Uint<{ <$name>::LIMBS - U64::LIMBS * $size }>) {
55                $crate::uint::split::split_mixed(self)
56            }
57        }
58    };
59    ($name:ident, [ $($size:literal),+ ]) => {
60        $(
61            impl_uint_concat_split_mixed!($name, $size);
62        )+
63    };
64    ($( ($name:ident, $sizes:tt), )+) => {
65        $(
66            impl_uint_concat_split_mixed!($name, $sizes);
67        )+
68    };
69}
70
71macro_rules! impl_uint_concat_split_even {
72    ($name:ident) => {
73        impl $crate::traits::ConcatMixed<Uint<{ <$name>::LIMBS / 2 }>> for Uint<{ <$name>::LIMBS / 2 }>
74        {
75            type MixedOutput = $name;
76
77            fn concat_mixed(&self, lo: &Uint<{ <$name>::LIMBS / 2 }>) -> Self::MixedOutput {
78                $crate::uint::concat::concat_mixed(lo, self)
79            }
80        }
81
82        impl Uint<{ <$name>::LIMBS / 2 }> {
83            /// Concatenate the two values, with `self` as most significant and `rhs`
84            /// as the least significant.
85            pub const fn concat(&self, lo: &Uint<{ <$name>::LIMBS / 2 }>) -> $name {
86                $crate::uint::concat::concat_mixed(lo, self)
87            }
88        }
89
90        impl $crate::traits::SplitMixed<Uint<{ <$name>::LIMBS / 2 }>, Uint<{ <$name>::LIMBS / 2 }>> for $name
91        {
92            fn split_mixed(&self) -> (Uint<{ <$name>::LIMBS / 2 }>, Uint<{ <$name>::LIMBS / 2 }>) {
93                $crate::uint::split::split_mixed(self)
94            }
95        }
96
97        impl $crate::traits::Split for $name
98        {
99            type Output = Uint<{ <$name>::LIMBS / 2 }>;
100        }
101
102        impl $name {
103            /// Split this number in half, returning its high and low components
104            /// respectively.
105            pub const fn split(&self) -> (Uint<{ <$name>::LIMBS / 2 }>, Uint<{ <$name>::LIMBS / 2 }>) {
106                $crate::uint::split::split_mixed(self)
107            }
108        }
109    };
110    ($($name:ident,)+) => {
111        $(
112            impl_uint_concat_split_even!($name);
113        )+
114    }
115}