ark-r1cs-std/src/uint/prim_uint.rs
src/uint/prim_uint.rsRaw
1use core::ops::{Shl, ShlAssign, Shr, ShrAssign};
2use core::usize;
3
4#[doc(hidden)]
5// Adapted from <https://github.com/rust-num/num-traits/pull/224>
6pub trait PrimUInt:
7 core::fmt::Debug
8 + num_traits::PrimInt
9 + num_traits::WrappingAdd
10 + num_traits::SaturatingAdd
11 + Shl<usize, Output = Self>
12 + Shl<u8, Output = Self>
13 + Shl<u16, Output = Self>
14 + Shl<u32, Output = Self>
15 + Shl<u64, Output = Self>
16 + Shl<u128, Output = Self>
17 + Shr<usize, Output = Self>
18 + Shr<u8, Output = Self>
19 + Shr<u16, Output = Self>
20 + Shr<u32, Output = Self>
21 + Shr<u64, Output = Self>
22 + Shr<u128, Output = Self>
23 + ShlAssign<usize>
24 + ShlAssign<u8>
25 + ShlAssign<u16>
26 + ShlAssign<u32>
27 + ShlAssign<u64>
28 + ShlAssign<u128>
29 + ShrAssign<usize>
30 + ShrAssign<u8>
31 + ShrAssign<u16>
32 + ShrAssign<u32>
33 + ShrAssign<u64>
34 + ShrAssign<u128>
35 + Into<u128>
36 + _private::Sealed
37 + ark_std::UniformRand
38{
39 type Bytes: NumBytes;
40 const MAX: Self;
41 #[doc(hidden)]
42 const MAX_VALUE_BIT_DECOMP: &'static [bool];
43
44 /// Return the memory representation of this number as a byte array in little-endian byte order.
45 ///
46 /// # Examples
47 ///
48 /// ```
49 /// use ark_r1cs_std::uint::PrimUInt;
50 ///
51 /// let bytes = PrimUInt::to_le_bytes(&0x12345678u32);
52 /// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
53 /// ```
54 fn to_le_bytes(&self) -> Self::Bytes;
55
56 /// Return the memory representation of this number as a byte array in big-endian byte order.
57 ///
58 /// # Examples
59 ///
60 /// ```
61 /// use ark_r1cs_std::uint::PrimUInt;
62 ///
63 /// let bytes = PrimUInt::to_be_bytes(&0x12345678u32);
64 /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
65 /// ```
66 fn to_be_bytes(&self) -> Self::Bytes;
67}
68
69impl PrimUInt for u8 {
70 const MAX: Self = u8::MAX;
71 const MAX_VALUE_BIT_DECOMP: &'static [bool] = &[true; 8];
72 type Bytes = [u8; 1];
73
74 #[inline]
75 fn to_le_bytes(&self) -> Self::Bytes {
76 u8::to_le_bytes(*self)
77 }
78
79 #[inline]
80 fn to_be_bytes(&self) -> Self::Bytes {
81 u8::to_be_bytes(*self)
82 }
83}
84
85impl PrimUInt for u16 {
86 const MAX: Self = u16::MAX;
87 const MAX_VALUE_BIT_DECOMP: &'static [bool] = &[true; 16];
88 type Bytes = [u8; 2];
89
90 #[inline]
91 fn to_le_bytes(&self) -> Self::Bytes {
92 u16::to_le_bytes(*self)
93 }
94
95 #[inline]
96 fn to_be_bytes(&self) -> Self::Bytes {
97 u16::to_be_bytes(*self)
98 }
99}
100
101impl PrimUInt for u32 {
102 const MAX: Self = u32::MAX;
103 const MAX_VALUE_BIT_DECOMP: &'static [bool] = &[true; 32];
104 type Bytes = [u8; 4];
105
106 #[inline]
107 fn to_le_bytes(&self) -> Self::Bytes {
108 u32::to_le_bytes(*self)
109 }
110
111 #[inline]
112 fn to_be_bytes(&self) -> Self::Bytes {
113 u32::to_be_bytes(*self)
114 }
115}
116
117impl PrimUInt for u64 {
118 const MAX: Self = u64::MAX;
119 const MAX_VALUE_BIT_DECOMP: &'static [bool] = &[true; 64];
120 type Bytes = [u8; 8];
121
122 #[inline]
123 fn to_le_bytes(&self) -> Self::Bytes {
124 u64::to_le_bytes(*self)
125 }
126
127 #[inline]
128 fn to_be_bytes(&self) -> Self::Bytes {
129 u64::to_be_bytes(*self)
130 }
131}
132
133impl PrimUInt for u128 {
134 const MAX: Self = u128::MAX;
135 const MAX_VALUE_BIT_DECOMP: &'static [bool] = &[true; 128];
136 type Bytes = [u8; 16];
137
138 #[inline]
139 fn to_le_bytes(&self) -> Self::Bytes {
140 u128::to_le_bytes(*self)
141 }
142
143 #[inline]
144 fn to_be_bytes(&self) -> Self::Bytes {
145 u128::to_be_bytes(*self)
146 }
147}
148
149#[doc(hidden)]
150pub trait NumBytes:
151 core::fmt::Debug
152 + AsRef<[u8]>
153 + AsMut<[u8]>
154 + PartialEq
155 + Eq
156 + PartialOrd
157 + Ord
158 + core::hash::Hash
159 + core::borrow::Borrow<[u8]>
160 + core::borrow::BorrowMut<[u8]>
161{
162}
163
164#[doc(hidden)]
165impl<const N: usize> NumBytes for [u8; N] {}
166
167mod _private {
168 pub trait Sealed {}
169
170 impl Sealed for u8 {}
171 impl Sealed for u16 {}
172 impl Sealed for u32 {}
173 impl Sealed for u64 {}
174 impl Sealed for u128 {}
175}