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
use std::io;
use amplify::flags::FlagVec;
use amplify::num::{u1024, u256, u512};
use crate::{Error, StrictDecode, StrictEncode};
impl StrictEncode for FlagVec {
#[inline]
fn strict_encode<E: io::Write>(&self, e: E) -> Result<usize, Error> {
self.shrunk().as_inner().strict_encode(e)
}
}
impl StrictDecode for FlagVec {
#[inline]
fn strict_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(Self::from_inner(StrictDecode::strict_decode(d)?))
}
}
impl StrictEncode for u256 {
fn strict_encode<E: io::Write>(&self, e: E) -> Result<usize, Error> {
self.to_le_bytes().strict_encode(e)
}
}
impl StrictDecode for u256 {
fn strict_decode<D: io::Read>(d: D) -> Result<Self, Error> {
Ok(u256::from_le_bytes(<[u8; 32]>::strict_decode(d)?))
}
}
impl StrictEncode for u512 {
fn strict_encode<E: io::Write>(&self, mut e: E) -> Result<usize, Error> {
let bytes = self.to_le_bytes();
e.write_all(&bytes)?;
Ok(bytes.len())
}
}
impl StrictDecode for u512 {
fn strict_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
let mut bytes = [0u8; 64];
d.read_exact(&mut bytes)?;
Ok(u512::from_le_bytes(bytes))
}
}
impl StrictEncode for u1024 {
fn strict_encode<E: io::Write>(&self, mut e: E) -> Result<usize, Error> {
let bytes = self.to_le_bytes();
e.write_all(&bytes)?;
Ok(bytes.len())
}
}
impl StrictDecode for u1024 {
fn strict_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
let mut bytes = [0u8; 128];
d.read_exact(&mut bytes)?;
Ok(u1024::from_le_bytes(bytes))
}
}
#[cfg(test)]
mod test {
use strict_encoding_test::test_encoding_roundtrip;
use super::*;
#[test]
fn test_large_uints() {
test_encoding_roundtrip(&u256::from(0x_dead_cafe_4bad_beef_u64), [
0xef, 0xbe, 0xad, 0x4b, 0xfe, 0xca, 0xad, 0xde, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
])
.unwrap();
test_encoding_roundtrip(&u512::from(0x_dead_cafe_4bad_beef_u64), [
0xef, 0xbe, 0xad, 0x4b, 0xfe, 0xca, 0xad, 0xde, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
])
.unwrap();
test_encoding_roundtrip(&u1024::from(0x_dead_cafe_4bad_beef_u64), [
0xef, 0xbe, 0xad, 0x4b, 0xfe, 0xca, 0xad, 0xde, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
])
.unwrap();
}
}