ark_serialize/impls/
int_like.rs1use crate::{
2 CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
3};
4use ark_std::{
5 io::{Read, Write},
6 vec::*,
7};
8use num_bigint::BigUint;
9
10impl Valid for bool {
11 const TRIVIAL_CHECK: Self = true;
12 fn check(&self) -> Result<(), SerializationError> {
13 Ok(())
14 }
15}
16
17impl CanonicalSerialize for bool {
18 #[inline]
19 fn serialize_with_mode<W: Write>(
20 &self,
21 mut writer: W,
22 _compress: Compress,
23 ) -> Result<(), SerializationError> {
24 writer.write_all(&[*self as u8])?;
25 Ok(())
26 }
27
28 #[inline]
29 fn serialized_size(&self, _compress: Compress) -> usize {
30 1
31 }
32}
33
34impl CanonicalDeserialize for bool {
35 #[inline]
36 fn deserialize_with_mode<R: Read>(
37 reader: R,
38 compress: Compress,
39 validate: Validate,
40 ) -> Result<Self, SerializationError> {
41 match u8::deserialize_with_mode(reader, compress, validate)? {
42 0u8 => Ok(false),
43 1u8 => Ok(true),
44 _ => Err(SerializationError::InvalidData),
45 }
46 }
47}
48
49macro_rules! impl_uint {
50 ($type:ty) => {
51 impl CanonicalSerialize for $type {
52 #[inline]
53 fn serialize_with_mode<W: Write>(
54 &self,
55 mut writer: W,
56 _compress: Compress,
57 ) -> Result<(), SerializationError> {
58 Ok(writer.write_all(&self.to_le_bytes())?)
59 }
60
61 #[inline]
62 fn serialized_size(&self, _compress: Compress) -> usize {
63 core::mem::size_of::<$type>()
64 }
65 }
66
67 impl Valid for $type {
68 const TRIVIAL_CHECK: bool = true;
69
70 #[inline]
71 fn check(&self) -> Result<(), SerializationError> {
72 Ok(())
73 }
74
75 #[inline]
76 fn batch_check<'a>(
77 _batch: impl Iterator<Item = &'a Self>,
78 ) -> Result<(), SerializationError>
79 where
80 Self: 'a,
81 {
82 Ok(())
83 }
84 }
85
86 impl CanonicalDeserialize for $type {
87 #[inline]
88 fn deserialize_with_mode<R: Read>(
89 mut reader: R,
90 _compress: Compress,
91 _validate: Validate,
92 ) -> Result<Self, SerializationError> {
93 let mut bytes = [0u8; core::mem::size_of::<$type>()];
94 reader.read_exact(&mut bytes)?;
95 Ok(<$type>::from_le_bytes(bytes))
96 }
97 }
98 };
99}
100
101impl_uint!(u8);
102impl_uint!(u16);
103impl_uint!(u32);
104impl_uint!(u64);
105impl_uint!(i8);
106impl_uint!(i16);
107impl_uint!(i32);
108impl_uint!(i64);
109
110impl CanonicalSerialize for usize {
111 #[inline]
112 fn serialize_with_mode<W: Write>(
113 &self,
114 mut writer: W,
115 _compress: Compress,
116 ) -> Result<(), SerializationError> {
117 Ok(writer.write_all(&(*self as u64).to_le_bytes())?)
118 }
119
120 #[inline]
121 fn serialized_size(&self, _compress: Compress) -> usize {
122 core::mem::size_of::<u64>()
123 }
124}
125
126impl Valid for usize {
127 const TRIVIAL_CHECK: bool = true;
128
129 #[inline]
130 fn check(&self) -> Result<(), SerializationError> {
131 Ok(())
132 }
133
134 #[inline]
135 fn batch_check<'a>(_batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
136 where
137 Self: 'a,
138 {
139 Ok(())
140 }
141}
142
143impl CanonicalDeserialize for usize {
144 #[inline]
145 fn deserialize_with_mode<R: Read>(
146 mut reader: R,
147 _compress: Compress,
148 _validate: Validate,
149 ) -> Result<Self, SerializationError> {
150 let mut bytes = [0u8; core::mem::size_of::<u64>()];
151 reader.read_exact(&mut bytes)?;
152 Ok(<u64>::from_le_bytes(bytes) as Self)
153 }
154}
155
156impl CanonicalSerialize for isize {
157 #[inline]
158 fn serialize_with_mode<W: Write>(
159 &self,
160 mut writer: W,
161 _compress: Compress,
162 ) -> Result<(), SerializationError> {
163 Ok(writer.write_all(&(*self as i64).to_le_bytes())?)
164 }
165
166 #[inline]
167 fn serialized_size(&self, _compress: Compress) -> usize {
168 core::mem::size_of::<i64>()
169 }
170}
171
172impl Valid for isize {
173 const TRIVIAL_CHECK: bool = true;
174
175 #[inline]
176 fn check(&self) -> Result<(), SerializationError> {
177 Ok(())
178 }
179
180 #[inline]
181 fn batch_check<'a>(_batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
182 where
183 Self: 'a,
184 {
185 Ok(())
186 }
187}
188
189impl CanonicalDeserialize for isize {
190 #[inline]
191 fn deserialize_with_mode<R: Read>(
192 mut reader: R,
193 _compress: Compress,
194 _validate: Validate,
195 ) -> Result<Self, SerializationError> {
196 let mut bytes = [0u8; core::mem::size_of::<i64>()];
197 reader.read_exact(&mut bytes)?;
198 Ok(<i64>::from_le_bytes(bytes) as Self)
199 }
200}
201
202impl CanonicalSerialize for BigUint {
203 #[inline]
204 fn serialize_with_mode<W: Write>(
205 &self,
206 writer: W,
207 compress: Compress,
208 ) -> Result<(), SerializationError> {
209 self.to_bytes_le().serialize_with_mode(writer, compress)
210 }
211
212 #[inline]
213 fn serialized_size(&self, compress: Compress) -> usize {
214 self.to_bytes_le().serialized_size(compress)
215 }
216}
217
218impl CanonicalDeserialize for BigUint {
219 #[inline]
220 fn deserialize_with_mode<R: Read>(
221 reader: R,
222 compress: Compress,
223 validate: Validate,
224 ) -> Result<Self, SerializationError> {
225 Ok(Self::from_bytes_le(&Vec::<u8>::deserialize_with_mode(
226 reader, compress, validate,
227 )?))
228 }
229}
230
231impl Valid for BigUint {
232 const TRIVIAL_CHECK: bool = true;
233
234 #[inline]
235 fn check(&self) -> Result<(), SerializationError> {
236 Ok(())
237 }
238
239 #[inline]
240 fn batch_check<'a>(_batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
241 where
242 Self: 'a,
243 {
244 Ok(())
245 }
246}