1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::error;
use ark_std::{
io::{Read, Result as IoResult, Write},
vec::Vec,
};
pub trait ToBytes {
fn write<W: Write>(&self, writer: W) -> IoResult<()>;
}
pub trait FromBytes: Sized {
fn read<R: Read>(reader: R) -> IoResult<Self>;
}
impl<const N: usize> ToBytes for [u8; N] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
writer.write_all(self)
}
}
impl<const N: usize> FromBytes for [u8; N] {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut arr = [0u8; N];
reader.read_exact(&mut arr)?;
Ok(arr)
}
}
impl<const N: usize> ToBytes for [u16; N] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for num in self {
writer.write_all(&num.to_le_bytes())?;
}
Ok(())
}
}
impl<const N: usize> FromBytes for [u16; N] {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut res = [0u16; N];
for num in res.iter_mut() {
let mut bytes = [0u8; 2];
reader.read_exact(&mut bytes)?;
*num = u16::from_le_bytes(bytes);
}
Ok(res)
}
}
impl<const N: usize> ToBytes for [u32; N] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for num in self {
writer.write_all(&num.to_le_bytes())?;
}
Ok(())
}
}
impl<const N: usize> FromBytes for [u32; N] {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut res = [0u32; N];
for num in res.iter_mut() {
let mut bytes = [0u8; 4];
reader.read_exact(&mut bytes)?;
*num = u32::from_le_bytes(bytes);
}
Ok(res)
}
}
impl<const N: usize> ToBytes for [u64; N] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for num in self {
writer.write_all(&num.to_le_bytes())?;
}
Ok(())
}
}
impl<const N: usize> FromBytes for [u64; N] {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut res = [0u64; N];
for num in res.iter_mut() {
let mut bytes = [0u8; 8];
reader.read_exact(&mut bytes)?;
*num = u64::from_le_bytes(bytes);
}
Ok(res)
}
}
impl<const N: usize> ToBytes for [u128; N] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for num in self {
writer.write_all(&num.to_le_bytes())?;
}
Ok(())
}
}
impl<const N: usize> FromBytes for [u128; N] {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut res = [0u128; N];
for num in res.iter_mut() {
let mut bytes = [0u8; 16];
reader.read_exact(&mut bytes)?;
*num = u128::from_le_bytes(bytes);
}
Ok(res)
}
}
#[macro_export]
macro_rules! to_bytes {
($($x:expr),*) => ({
let mut buf = $crate::vec![];
{$crate::push_to_vec!(buf, $($x),*)}.map(|_| buf)
});
}
#[macro_export]
macro_rules! push_to_vec {
($buf:expr, $y:expr, $($x:expr),*) => ({
{
$crate::ToBytes::write(&$y, &mut $buf)
}.and({$crate::push_to_vec!($buf, $($x),*)})
});
($buf:expr, $x:expr) => ({
$crate::ToBytes::write(&$x, &mut $buf)
})
}
impl ToBytes for u8 {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
writer.write_all(&[*self])
}
}
impl FromBytes for u8 {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut byte = [0u8];
reader.read_exact(&mut byte)?;
Ok(byte[0])
}
}
impl ToBytes for u16 {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
writer.write_all(&self.to_le_bytes())
}
}
impl FromBytes for u16 {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut bytes = [0u8; 2];
reader.read_exact(&mut bytes)?;
Ok(u16::from_le_bytes(bytes))
}
}
impl ToBytes for u32 {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
writer.write_all(&self.to_le_bytes())
}
}
impl FromBytes for u32 {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut bytes = [0u8; 4];
reader.read_exact(&mut bytes)?;
Ok(u32::from_le_bytes(bytes))
}
}
impl ToBytes for u64 {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
writer.write_all(&self.to_le_bytes())
}
}
impl FromBytes for u64 {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut bytes = [0u8; 8];
reader.read_exact(&mut bytes)?;
Ok(u64::from_le_bytes(bytes))
}
}
impl ToBytes for u128 {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
writer.write_all(&self.to_le_bytes())
}
}
impl FromBytes for u128 {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let mut bytes = [0u8; 16];
reader.read_exact(&mut bytes)?;
Ok(u128::from_le_bytes(bytes))
}
}
impl ToBytes for () {
#[inline]
fn write<W: Write>(&self, _writer: W) -> IoResult<()> {
Ok(())
}
}
impl FromBytes for () {
#[inline]
fn read<R: Read>(_bytes: R) -> IoResult<Self> {
Ok(())
}
}
impl ToBytes for bool {
#[inline]
fn write<W: Write>(&self, writer: W) -> IoResult<()> {
u8::write(&(*self as u8), writer)
}
}
impl FromBytes for bool {
#[inline]
fn read<R: Read>(reader: R) -> IoResult<Self> {
match u8::read(reader) {
Ok(0) => Ok(false),
Ok(1) => Ok(true),
Ok(_) => Err(error("FromBytes::read failed")),
Err(err) => Err(err),
}
}
}
impl<T: ToBytes> ToBytes for Vec<T> {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for item in self {
item.write(&mut writer)?;
}
Ok(())
}
}
impl<'a, T: 'a + ToBytes> ToBytes for &'a [T] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for item in *self {
item.write(&mut writer)?;
}
Ok(())
}
}
impl<'a, T: 'a + ToBytes> ToBytes for &'a T {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
(*self).write(&mut writer)
}
}
impl<T: ToBytes> ToBytes for Option<T> {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
if let Some(val) = self {
true.write(&mut writer)?;
val.write(&mut writer)
} else {
false.write(&mut writer)
}
}
}
impl<T: FromBytes> FromBytes for Option<T> {
#[inline]
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let is_some = bool::read(&mut reader)?;
if is_some {
T::read(&mut reader).map(Some)
} else {
Ok(None)
}
}
}
#[cfg(test)]
mod test {
use ark_std::vec::Vec;
#[test]
fn test_macro_empty() {
let array: Vec<u8> = vec![];
let bytes: Vec<u8> = to_bytes![array].unwrap();
assert_eq!(&bytes, &[]);
assert_eq!(bytes.len(), 0);
}
#[test]
fn test_macro() {
let array1 = [1u8; 32];
let array2 = [2u8; 16];
let array3 = [3u8; 8];
let bytes = to_bytes![array1, array2, array3].unwrap();
assert_eq!(bytes.len(), 56);
let mut actual_bytes = Vec::new();
actual_bytes.extend_from_slice(&array1);
actual_bytes.extend_from_slice(&array2);
actual_bytes.extend_from_slice(&array3);
assert_eq!(bytes, actual_bytes);
}
}