-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
320 lines (264 loc) · 9.37 KB
/
lib.rs
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
//! This library provides methods for encoding the data into chunks and
//! reconstructing the original data from chunks as well as verifying
//! individual chunks against an erasure root.
mod error;
mod merklize;
mod subshard;
pub use self::{
error::Error,
merklize::{ErasureRoot, MerklizedChunks, Proof},
};
use scale::{Decode, Encode};
use std::ops::AddAssign;
pub use subshard::*;
pub const MAX_CHUNKS: u16 = 16384;
// The reed-solomon library requires each shards to be 64 bytes aligned.
const SHARD_ALIGNMENT: usize = 64;
/// The index of an erasure chunk.
#[derive(Eq, Ord, PartialEq, PartialOrd, Copy, Clone, Encode, Decode, Hash, Debug)]
pub struct ChunkIndex(pub u16);
impl From<u16> for ChunkIndex {
fn from(n: u16) -> Self {
ChunkIndex(n)
}
}
impl AddAssign<u16> for ChunkIndex {
fn add_assign(&mut self, rhs: u16) {
self.0 += rhs
}
}
/// A chunk of erasure-encoded block data.
#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug)]
pub struct ErasureChunk {
/// The erasure-encoded chunk of data belonging to the candidate block.
pub chunk: Vec<u8>,
/// The index of this erasure-encoded chunk of data.
pub index: ChunkIndex,
/// Proof for this chunk against an erasure root.
pub proof: Proof,
}
/// Obtain a threshold of chunks that should be enough to recover the data.
pub const fn recovery_threshold(n_chunks: u16) -> Result<u16, Error> {
if n_chunks > MAX_CHUNKS {
return Err(Error::TooManyTotalChunks);
}
if n_chunks == 0 {
return Err(Error::NotEnoughTotalChunks);
}
let needed = (n_chunks - 1) / 3;
Ok(needed + 1)
}
/// Obtain the threshold of systematic chunks that should be enough to recover the data.
pub fn systematic_recovery_threshold(n_chunks: u16) -> Result<u16, Error> {
recovery_threshold(n_chunks)
}
/// Reconstruct the original data from the set of systematic chunks.
///
/// Provide a vector containing the first k chunks in order. If too few chunks are provided,
/// recovery is not possible.
///
/// Due to the internals of the erasure coding algorithm, the output might be
/// larger than the original data and padded with zeroes; passing `data_len`
/// allows to truncate the output to the original data size.
pub fn reconstruct_from_systematic<'a>(
n_chunks: u16,
systematic_len: usize,
systematic_chunks: &'a mut impl Iterator<Item = &'a [u8]>,
data_len: usize,
) -> Result<Vec<u8>, Error> {
let k = systematic_recovery_threshold(n_chunks)? as usize;
if systematic_len < k {
return Err(Error::NotEnoughChunks);
}
let mut bytes: Vec<u8> = Vec::with_capacity(0);
let mut shard_len = 0;
let mut nb = 0;
for chunk in systematic_chunks.by_ref() {
nb += 1;
if shard_len == 0 {
shard_len = chunk.len();
if shard_len % SHARD_ALIGNMENT != 0 && nb != k {
return Err(Error::UnalignedChunk);
}
if k == 1 {
return Ok(chunk[..data_len].to_vec());
}
bytes = Vec::with_capacity(shard_len * k);
}
if chunk.len() != shard_len {
return Err(Error::NonUniformChunks)
}
bytes.extend_from_slice(chunk);
if nb == k {
break;
}
}
bytes.resize(data_len, 0);
Ok(bytes)
}
/// Construct erasure-coded chunks.
///
/// Works only for 1..65536 chunks.
/// The data must be non-empty.
pub fn construct_chunks(n_chunks: u16, data: &[u8]) -> Result<Vec<Vec<u8>>, Error> {
if data.is_empty() {
return Err(Error::BadPayload);
}
if n_chunks == 1 {
return Ok(vec![data.to_vec()]);
}
let systematic = systematic_recovery_threshold(n_chunks)?;
let original_data = make_original_shards(systematic, data);
let original_iter = original_data.iter();
let original_count = systematic as usize;
let recovery_count = (n_chunks - systematic) as usize;
let recovery = reed_solomon::encode(original_count, recovery_count, original_iter)?;
let mut result = original_data;
result.extend(recovery);
Ok(result)
}
fn next_aligned(n: usize, alignment: usize) -> usize {
((n + alignment - 1) / alignment) * alignment
}
fn shard_bytes(systematic: u16, data_len: usize) -> usize {
let shard_bytes = (data_len + systematic as usize - 1) / systematic as usize;
next_aligned(shard_bytes, SHARD_ALIGNMENT)
}
// The reed-solomon library takes sharded data as input.
fn make_original_shards(original_count: u16, data: &[u8]) -> Vec<Vec<u8>> {
assert!(!data.is_empty(), "data must be non-empty");
assert_ne!(original_count, 0);
let shard_bytes = shard_bytes(original_count, data.len());
assert_ne!(shard_bytes, 0);
let mut result = vec![vec![0u8; shard_bytes]; original_count as usize];
for (i, chunk) in data.chunks(shard_bytes).enumerate() {
result[i][..chunk.len()].as_mut().copy_from_slice(chunk);
}
result
}
/// Reconstruct the original data from a set of chunks.
///
/// Provide an iterator containing chunk data and the corresponding index.
/// The indices of the present chunks must be indicated. If too few chunks
/// are provided, recovery is not possible.
///
/// Works only for 1..65536 chunks.
///
/// Due to the internals of the erasure coding algorithm, the output might be
/// larger than the original data and padded with zeroes; passing `data_len`
/// allows to truncate the output to the original data size.
pub fn reconstruct<I>(n_chunks: u16, chunks: I, data_len: usize) -> Result<Vec<u8>, Error>
where
I: IntoIterator<Item = (ChunkIndex, Vec<u8>)>,
{
if n_chunks == 1 {
return chunks.into_iter().next().map(|(_, v)| v).ok_or(Error::NotEnoughChunks);
}
let n = n_chunks as usize;
let original_count = systematic_recovery_threshold(n_chunks)? as usize;
let recovery_count = n - original_count;
let (mut original, recovery): (Vec<_>, Vec<_>) = chunks
.into_iter()
.map(|(i, v)| (i.0 as usize, v))
.partition(|(i, _)| *i < original_count);
original.sort_by_key(|(i, _)| *i);
let original_iter = original.iter().map(|(i, v)| (*i, v));
let recovery = recovery.into_iter().map(|(i, v)| (i - original_count, v));
let mut recovered =
reed_solomon::decode(original_count, recovery_count, original_iter, recovery)?;
let shard_bytes = shard_bytes(original_count as u16, data_len);
let mut bytes = Vec::with_capacity(shard_bytes * original_count);
let mut original = original.into_iter();
for i in 0..original_count {
let chunk = recovered.remove(&i).unwrap_or_else(|| {
let (j, v) = original.next().expect("what is not recovered must be present; qed");
debug_assert_eq!(i, j);
v
});
bytes.extend_from_slice(chunk.as_slice());
}
bytes.truncate(data_len);
Ok(bytes)
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::*;
use quickcheck::{Arbitrary, Gen, QuickCheck};
#[derive(Clone, Debug)]
struct ArbitraryAvailableData(Vec<u8>);
impl Arbitrary for ArbitraryAvailableData {
fn arbitrary(g: &mut Gen) -> Self {
// Limit the POV len to 16KiB, otherwise the test will take forever
let data_len = (u32::arbitrary(g) % (16 * 1024)).max(2);
let data = (0..data_len).map(|_| u8::arbitrary(g)).collect();
ArbitraryAvailableData(data)
}
}
#[derive(Clone, Debug)]
struct SmallAvailableData(Vec<u8>);
impl Arbitrary for SmallAvailableData {
fn arbitrary(g: &mut Gen) -> Self {
let data_len = (u32::arbitrary(g) % (2 * 1024)).max(2);
let data = (0..data_len).map(|_| u8::arbitrary(g)).collect();
Self(data)
}
}
#[test]
fn round_trip_systematic_works() {
fn property(available_data: ArbitraryAvailableData, n_chunks: u16) {
let n_chunks = n_chunks.max(1).min(MAX_CHUNKS);
let threshold = systematic_recovery_threshold(n_chunks).unwrap();
let data_len = available_data.0.len();
let chunks = construct_chunks(n_chunks, &available_data.0).unwrap();
let reconstructed: Vec<u8> = reconstruct_from_systematic(
n_chunks,
chunks.len(),
&mut chunks.iter().take(threshold as usize).map(Vec::as_slice),
data_len,
)
.unwrap();
assert_eq!(reconstructed, available_data.0);
}
QuickCheck::new().quickcheck(property as fn(ArbitraryAvailableData, u16))
}
#[test]
fn round_trip_works() {
fn property(available_data: ArbitraryAvailableData, n_chunks: u16) {
let n_chunks = n_chunks.max(1).min(MAX_CHUNKS);
let data_len = available_data.0.len();
let threshold = recovery_threshold(n_chunks).unwrap();
let chunks = construct_chunks(n_chunks, &available_data.0).unwrap();
let map: HashMap<ChunkIndex, Vec<u8>> = chunks
.into_iter()
.enumerate()
.map(|(i, v)| (ChunkIndex::from(i as u16), v))
.collect();
let some_chunks = map.into_iter().take(threshold as usize);
let reconstructed: Vec<u8> = reconstruct(n_chunks, some_chunks, data_len).unwrap();
assert_eq!(reconstructed, available_data.0);
}
QuickCheck::new().quickcheck(property as fn(ArbitraryAvailableData, u16))
}
#[test]
fn proof_verification_works() {
fn property(data: SmallAvailableData, n_chunks: u16) {
let n_chunks = n_chunks.max(1).min(2048);
let chunks = construct_chunks(n_chunks, &data.0).unwrap();
assert_eq!(chunks.len() as u16, n_chunks);
let iter = MerklizedChunks::compute(chunks.clone());
let root = iter.root();
let erasure_chunks: Vec<_> = iter.collect();
assert_eq!(erasure_chunks.len(), chunks.len());
for erasure_chunk in erasure_chunks.into_iter() {
let encode = Encode::encode(&erasure_chunk.proof);
let decode = Decode::decode(&mut &encode[..]).unwrap();
assert_eq!(erasure_chunk.proof, decode);
assert_eq!(encode, Encode::encode(&decode));
assert_eq!(&erasure_chunk.chunk, &chunks[erasure_chunk.index.0 as usize]);
assert!(erasure_chunk.verify(&root));
}
}
QuickCheck::new().quickcheck(property as fn(SmallAvailableData, u16))
}
}