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
use byteorder::{ByteOrder, BigEndian};
use unverified::Unverified;
pub fn encode(input: &[u8]) -> (Vec<u8>, ::Digest) {
let mut output = vec![0; ::HEADER_SIZE];
let root_hash = encode_recurse(input, &mut output);
let header = to_header_bytes(input.len() as u64, &root_hash);
output[..::HEADER_SIZE].copy_from_slice(&header);
(output, ::hash(&header))
}
fn encode_recurse(input: &[u8], output: &mut Vec<u8>) -> ::Digest {
if input.len() <= ::CHUNK_SIZE {
output.extend_from_slice(input);
return ::hash(input);
}
let node_start = output.len();
let node_half = node_start + ::DIGEST_SIZE;
let node_end = node_half + ::DIGEST_SIZE;
output.resize(node_end, 0);
let left_len = left_subregion_len(input.len() as u64) as usize;
let left_hash = encode_recurse(&input[..left_len], output);
let right_hash = encode_recurse(&input[left_len..], output);
output[node_start..node_half].copy_from_slice(&left_hash);
output[node_half..node_end].copy_from_slice(&right_hash);
::hash(&output[node_start..node_end])
}
pub fn decode(encoded: &[u8], hash: &::Digest) -> ::Result<Vec<u8>> {
let mut encoded = Unverified::wrap(encoded);
let header_bytes = encoded.read_verify(::HEADER_SIZE, hash)?;
let (len, hash) = from_header_bytes(header_bytes);
let mut output = Vec::with_capacity(len as usize);
decode_recurse(&mut encoded, len, &hash, &mut output)?;
Ok(output)
}
fn decode_recurse(
encoded: &mut Unverified,
region_len: u64,
hash: &::Digest,
output: &mut Vec<u8>,
) -> ::Result<()> {
if region_len <= ::CHUNK_SIZE as u64 {
let chunk_bytes = encoded.read_verify(region_len as usize, hash)?;
output.extend_from_slice(chunk_bytes);
return Ok(());
}
let node_bytes = encoded.read_verify(::NODE_SIZE, hash)?;
let (left_len, right_len, left_hash, right_hash) = split_node(region_len, node_bytes);
decode_recurse(encoded, left_len, &left_hash, output)?;
decode_recurse(encoded, right_len, &right_hash, output)?;
Ok(())
}
pub(crate) fn left_subregion_len(region_len: u64) -> u64 {
debug_assert!(region_len > ::CHUNK_SIZE as u64);
let full_chunks = (region_len - 1) / ::CHUNK_SIZE as u64;
largest_power_of_two(full_chunks) * ::CHUNK_SIZE as u64
}
fn largest_power_of_two(n: u64) -> u64 {
1 << (63 - (n | 1).leading_zeros())
}
pub(crate) fn from_header_bytes(bytes: &[u8]) -> (u64, ::Digest) {
let len = BigEndian::read_u64(&bytes[..8]);
let hash = *array_ref!(bytes, 8, ::DIGEST_SIZE);
(len, hash)
}
pub(crate) fn to_header_bytes(len: u64, hash: &::Digest) -> [u8; ::HEADER_SIZE] {
let mut ret = [0; ::HEADER_SIZE];
BigEndian::write_u64(&mut ret[..8], len);
ret[8..].copy_from_slice(hash);
ret
}
pub(crate) fn split_node(region_len: u64, node_bytes: &[u8]) -> (u64, u64, ::Digest, ::Digest) {
let left_len = left_subregion_len(region_len);
let right_len = region_len - left_len;
let left_hash = *array_ref!(node_bytes, 0, ::DIGEST_SIZE);
let right_hash = *array_ref!(node_bytes, ::DIGEST_SIZE, ::DIGEST_SIZE);
(left_len, right_len, left_hash, right_hash)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_power_of_two() {
let input_output = &[
(0, 1),
(1, 1),
(2, 2),
(3, 2),
(4, 4),
(5, 4),
(6, 4),
(7, 4),
(8, 8),
(0xffffffffffffffff, 0x8000000000000000),
];
for &(input, output) in input_output {
assert_eq!(
output,
largest_power_of_two(input),
"wrong output for n={}",
input
);
}
}
#[test]
fn test_left_subregion_len() {
let s = ::CHUNK_SIZE as u64;
let input_output = &[(s + 1, s), (2 * s - 1, s), (2 * s, s), (2 * s + 1, 2 * s)];
for &(input, output) in input_output {
println!("testing {} and {}", input, output);
assert_eq!(left_subregion_len(input), output);
}
}
#[test]
fn test_simple_encode_decode() {
for &case in ::TEST_CASES {
println!("starting case {}", case);
let input = vec![0xab; case];
let (encoded, hash) = ::simple::encode(&input);
let decoded = ::simple::decode(&encoded, &hash).unwrap();
assert_eq!(input, decoded);
}
}
#[test]
fn test_simple_corrupted() {
for &case in ::TEST_CASES {
let input = vec![0xbc; case];
let (mut encoded, hash) = ::simple::encode(&input[..]);
for &tweak_case in ::TEST_CASES {
if tweak_case < encoded.len() {
encoded[tweak_case] ^= 1;
println!("testing input len {} tweak {}", case, tweak_case);
assert!(::simple::decode(&encoded, &hash).is_err());
encoded[tweak_case] ^= 1;
}
}
}
}
}