tape-core 0.5.7

A decentralized object store using Solana.
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Blob payload metadata and commitment encoding.

use core::mem::size_of;

use bytemuck::{Pod, Zeroable};
use tape_crypto::Hash;
use tape_crypto::hash::hash;
use tape_crypto::merkle::root_from_leaf_hashes;
use tape_crypto::merkle::{create_proof_from_leaf_hashes, hash_leaf, verify_proof_hash};

use crate::encoding::ErasureCode;
use crate::erasure::{
    MAX_GROUP_SIZE, SAMPLE_WINDOW_BYTES, SLICE_TREE_HEIGHT, SUB_LEAF_BYTES, SUB_TREE_HEIGHT, slice_root,
    slice_root_from_sidecar, sub_leaf_hashes, window_root,
};
use crate::types::{SpoolIndex, StorageUnits, StripeCount};

#[cfg(feature = "wincode")]
use core::mem::MaybeUninit;
#[cfg(feature = "wincode")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wincode")]
use wincode::{
    io::{Reader, Writer},
    ReadResult, SchemaRead, SchemaWrite, WriteResult,
};

/// Blob payload metadata stored on nodes responsible for the track's spool group.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Pod, Zeroable)]
#[cfg_attr(feature = "wincode", derive(Serialize, Deserialize))]
pub struct BlobEncoding {
    /// Original unencoded data size in bytes.
    pub size: StorageUnits,

    /// Root of the erasure-coded commitment tree.
    pub commitment: Hash,

    /// Stripe size in bytes.
    pub stripe_size: StorageUnits,

    /// Number of stripes.
    pub stripe_count: StripeCount,

    /// Per-slice commitment leaves, each the root of that slice's sub-leaf tree.
    /// A network with n slices per track fills the first n and zeroes the rest.
    pub leaves: [Hash; MAX_GROUP_SIZE],
}

/// One sampled sub-leaf with its path to that slice's registered root
///
/// The path stops at the slice root rather than climbing to the commitment,
/// because every verifier holds the encoding and so already has the root the
/// path must reach. Carrying the top path would add 160 bytes a verifier can
/// derive for itself.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SubLeafProof {
    /// Bytes of the sampled leaf, shorter than a full leaf only at the end of a slice.
    pub sub_leaf: Vec<u8>,

    /// Path from the sampled leaf to the root of its slice.
    pub sub_proof: Vec<Hash>,
}

pub type PackedBlobEncoding = [u8; size_of::<BlobEncoding>()];

impl BlobEncoding {
    #[inline(always)]
    pub fn pack(&self) -> PackedBlobEncoding {
        let mut out = [0u8; size_of::<Self>()];
        out.copy_from_slice(bytemuck::bytes_of(self));
        out
    }

    #[inline(always)]
    pub fn unpack(data: PackedBlobEncoding) -> Self {
        let mut value = Self::zeroed();
        bytemuck::bytes_of_mut(&mut value).copy_from_slice(&data);
        value
    }

    /// Recompute the commitment root over the profile's n leaves.
    ///
    /// None when n is outside the array or a leaf past it is set. The value
    /// hash covers every leaf, so a set leaf the root ignores would let one
    /// commitment carry many values.
    pub fn commitment_root(&self, profile: ErasureCode) -> Option<Hash> {
        let (live, padding) = self.leaves.split_at_checked(usize::from(profile.n()))?;
        if live.is_empty() {
            return None;
        }
        if padding.iter().any(|leaf| *leaf != Hash::default()) {
            return None;
        }

        Some(root_from_leaf_hashes::<SLICE_TREE_HEIGHT>(live))
    }

    /// Verify a whole slice against its stored slice root, rebuilding its sub-leaf tree.
    pub fn verify_slice(&self, position: SpoolIndex, data: &[u8]) -> bool {
        let position = position.as_usize();
        if position >= self.leaves.len() {
            return false;
        }

        slice_root(data) == Some(self.leaves[position])
    }

    /// Verify whole windows cut from a slice from `first_window` on, against the sidecar and the slice root
    pub fn verify_slice_window(
        &self,
        position: SpoolIndex,
        sidecar: &[Hash],
        first_window: usize,
        data: &[u8],
    ) -> bool {
        let position = position.as_usize();
        if position >= self.leaves.len() || data.is_empty() {
            return false;
        }
        if slice_root_from_sidecar(sidecar) != self.leaves[position] {
            return false;
        }

        for (offset, window) in data.chunks(SAMPLE_WINDOW_BYTES).enumerate() {
            match sidecar.get(first_window + offset) {
                Some(node) if *node == window_root(window) => {}
                _ => return false,
            }
        }
        true
    }

    /// Prove one sample leaf of a slice the caller holds.
    pub fn prove_sub_leaf(
        &self,
        position: SpoolIndex,
        sub_leaf_index: usize,
        slice: &[u8],
    ) -> Option<SubLeafProof> {
        let position = position.as_usize();
        if position >= self.leaves.len() {
            return None;
        }

        let start = sub_leaf_index.checked_mul(SUB_LEAF_BYTES)?;
        if start >= slice.len() {
            return None;
        }
        let end = (start + SUB_LEAF_BYTES).min(slice.len());

        let hashes = sub_leaf_hashes(slice);
        let sub_proof =
            create_proof_from_leaf_hashes::<SUB_TREE_HEIGHT>(&hashes, sub_leaf_index).ok()?;

        Some(SubLeafProof {
            sub_leaf: slice[start..end].to_vec(),
            sub_proof,
        })
    }

    /// Verify a sampled sub-leaf against its slice's registered root.
    pub fn verify_sub_leaf(
        &self,
        position: SpoolIndex,
        sub_leaf_index: usize,
        proof: &SubLeafProof,
    ) -> bool {
        let position = position.as_usize();
        if position >= self.leaves.len() {
            return false;
        }
        // compute_path indexes the proof directly, so a short one must not reach it.
        if proof.sub_proof.len() != SUB_TREE_HEIGHT {
            return false;
        }
        // An empty leaf hashes to the value padding uses, so without this a prover
        // could answer at any index past the end of the slice.
        if proof.sub_leaf.is_empty() || proof.sub_leaf.len() > SUB_LEAF_BYTES {
            return false;
        }

        verify_proof_hash(
            hash_leaf(&proof.sub_leaf),
            &self.leaves[position],
            &proof.sub_proof,
            sub_leaf_index as u64,
            SUB_TREE_HEIGHT,
        )
    }

    /// Compute the canonical value hash for this blob payload.
    pub fn get_hash(&self) -> Hash {
        hash(bytemuck::bytes_of(self))
    }
}

#[cfg(feature = "wincode")]
impl SchemaWrite for BlobEncoding {
    type Src = Self;

    fn size_of(_src: &Self::Src) -> WriteResult<usize> {
        Ok(size_of::<Self>())
    }

    fn write(writer: &mut Writer, src: &Self::Src) -> WriteResult<()> {
        writer.write_exact(&src.pack())?;
        Ok(())
    }
}

#[cfg(feature = "wincode")]
impl<'de> SchemaRead<'de> for BlobEncoding {
    type Dst = Self;

    fn read(reader: &mut Reader<'de>, dst: &mut MaybeUninit<Self::Dst>) -> ReadResult<()> {
        // SAFETY: The serialized representation is exactly `PackedBlobEncoding` bytes for this
        // pod-compatible type.
        let packed: PackedBlobEncoding = unsafe { reader.get_t()? };
        dst.write(Self::unpack(packed));
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encoding::{RepParams, TAPENET_PROFILE};
    use crate::erasure::{slice_sidecar, sub_leaf_count};
    use crate::track::data::BlobDataSlice;

    /// A profile of n copies, which gives a group of any size
    fn copies(n: usize) -> ErasureCode {
        ErasureCode::Replication(RepParams::new(n as u8))
    }

    fn sample_blob_encoding() -> BlobEncoding {
        BlobEncoding {
            size: StorageUnits::from_bytes(512),
            commitment: Hash::from([0x22; 32]),
            stripe_size: StorageUnits::from_bytes(64),
            stripe_count: StripeCount(2),
            leaves: [Hash::from([0x33; 32]); MAX_GROUP_SIZE],
        }
    }

    #[cfg(feature = "wincode")]
    #[test]
    fn encoding_wincode() {
        let blob = sample_blob_encoding();
        let bytes = wincode::serialize(&blob).expect("serialize");
        let recovered: BlobEncoding = wincode::deserialize(&bytes).expect("deserialize");
        assert_eq!(recovered, blob);
    }

    #[test]
    fn encoding_pack() {
        let blob = sample_blob_encoding();
        let packed = blob.pack();
        let recovered = BlobEncoding::unpack(packed);

        assert_eq!(recovered.stripe_size, StorageUnits::from_bytes(64));
        assert_eq!(recovered.stripe_count, StripeCount(2));
        assert_eq!(recovered, blob);
    }

    // only an untouched window run at its true offset verifies against the sidecar
    #[test]
    fn slice_windows() {
        let slice: Vec<u8> = (0..SAMPLE_WINDOW_BYTES * 3 + 700).map(|b| (b % 251) as u8).collect();
        let sidecar = slice_sidecar(&slice).expect("sidecar");
        let mut leaves = [Hash::default(); MAX_GROUP_SIZE];
        leaves[4] = slice_root(&slice).expect("root");
        let blob = BlobEncoding { leaves, ..BlobEncoding::zeroed() };
        let tail = &slice[SAMPLE_WINDOW_BYTES * 2..];
        let mut tampered = tail.to_vec();
        tampered[10] ^= 1;

        assert!(blob.verify_slice_window(SpoolIndex(4), &sidecar, 2, tail));
        assert!(blob.verify_slice_window(SpoolIndex(4), &sidecar, 0, &slice[..SAMPLE_WINDOW_BYTES]));
        assert!(!blob.verify_slice_window(SpoolIndex(4), &sidecar, 2, &tampered));
        assert!(!blob.verify_slice_window(SpoolIndex(4), &sidecar, 1, tail));
        assert!(!blob.verify_slice_window(SpoolIndex(5), &sidecar, 2, tail));
    }

    /// A blob whose leaves are the real sub-leaf roots of the returned slices.
    fn coded_blob() -> (BlobEncoding, Vec<Vec<u8>>) {
        let slices: Vec<Vec<u8>> = (0..MAX_GROUP_SIZE)
            .map(|i| {
                let len = SUB_LEAF_BYTES * 3 + i + 1;
                (0..len).map(|b| (b + i) as u8).collect()
            })
            .collect();

        let leaves: [Hash; MAX_GROUP_SIZE] =
            core::array::from_fn(|i| slice_root(&slices[i]).expect("slice within capacity"));

        let blob = BlobEncoding {
            commitment: root_from_leaf_hashes::<SLICE_TREE_HEIGHT>(&leaves),
            leaves,
            ..sample_blob_encoding()
        };
        (blob, slices)
    }

    // tapenet's profile gives the root over every leaf
    #[test]
    fn full_root() {
        let (blob, _) = coded_blob();

        assert_eq!(blob.commitment_root(TAPENET_PROFILE), Some(blob.commitment));
    }

    // a narrow root covers the live leaves only, so zeroed padding can't stand in for them
    #[test]
    fn narrow_root() {
        let (full, _) = coded_blob();
        let mut leaves = [Hash::default(); MAX_GROUP_SIZE];
        leaves[..4].copy_from_slice(&full.leaves[..4]);
        let blob = BlobEncoding { leaves, ..full };

        let root = blob.commitment_root(copies(4)).expect("zero tail");

        assert_eq!(root, root_from_leaf_hashes::<SLICE_TREE_HEIGHT>(&leaves[..4]));
        assert_ne!(Some(root), blob.commitment_root(TAPENET_PROFILE));
    }

    // a set leaf past the profile's n, or an n outside the array, has no root
    #[test]
    fn padding_rejected() {
        let (blob, _) = coded_blob();

        assert_eq!(blob.commitment_root(copies(4)), None);
        assert_eq!(blob.commitment_root(copies(MAX_GROUP_SIZE - 1)), None);
        assert_eq!(blob.commitment_root(copies(0)), None);
        assert_eq!(blob.commitment_root(copies(MAX_GROUP_SIZE + 1)), None);
    }

    // a coded blob yields metadata only under the profile its commitment covers
    #[test]
    fn meta_profile() {
        let (blob, _) = coded_blob();
        let data = BlobDataSlice::Coded(blob);

        assert!(data.meta(TAPENET_PROFILE).is_some());
        assert!(data.meta(copies(MAX_GROUP_SIZE - 1)).is_none());
    }

    #[test]
    fn verify_slice_accepts_only_the_stored_slice() {
        let (blob, slices) = coded_blob();

        for (i, slice) in slices.iter().enumerate() {
            assert!(blob.verify_slice(SpoolIndex(i as u64), slice));
        }

        let mut tampered = slices[0].clone();
        tampered[SUB_LEAF_BYTES + 5] ^= 0xFF;
        assert!(!blob.verify_slice(SpoolIndex(0), &tampered));

        assert!(!blob.verify_slice(SpoolIndex(MAX_GROUP_SIZE as u64), &slices[0]));
    }

    #[test]
    fn sub_leaf_proof_round_trip() {
        let (blob, slices) = coded_blob();
        let position = SpoolIndex(3);
        let slice = &slices[3];

        for index in 0..sub_leaf_count(slice.len()) {
            let proof = blob
                .prove_sub_leaf(position, index, slice)
                .expect("proof for a held sub-leaf");
            assert_eq!(proof.sub_proof.len(), SUB_TREE_HEIGHT);
            assert!(blob.verify_sub_leaf(position, index, &proof));
        }
    }

    #[test]
    fn sub_leaf_proof_rejects_tampering() {
        let (blob, slices) = coded_blob();
        let position = SpoolIndex(3);
        let proof = blob
            .prove_sub_leaf(position, 1, &slices[3])
            .expect("proof for a held sub-leaf");

        let mut flipped = proof.clone();
        flipped.sub_leaf[0] ^= 0xFF;
        assert!(!blob.verify_sub_leaf(position, 1, &flipped));

        // The same proof replayed at a different index or slice must not verify.
        assert!(!blob.verify_sub_leaf(position, 2, &proof));
        assert!(!blob.verify_sub_leaf(SpoolIndex(4), 1, &proof));
    }

    #[test]
    fn sub_leaf_proof_rejects_malformed_paths_without_panicking() {
        let (blob, slices) = coded_blob();
        let position = SpoolIndex(3);
        let proof = blob
            .prove_sub_leaf(position, 1, &slices[3])
            .expect("proof for a held sub-leaf");

        let mut short_sub = proof.clone();
        short_sub.sub_proof.pop();
        assert!(!blob.verify_sub_leaf(position, 1, &short_sub));

        let mut empty_leaf = proof.clone();
        empty_leaf.sub_leaf.clear();
        assert!(!blob.verify_sub_leaf(position, 1, &empty_leaf));

        let mut long_leaf = proof;
        long_leaf.sub_leaf = vec![0u8; SUB_LEAF_BYTES + 1];
        assert!(!blob.verify_sub_leaf(position, 1, &long_leaf));
    }

    #[test]
    fn prove_sub_leaf_rejects_an_index_past_the_slice() {
        let (blob, slices) = coded_blob();
        let past_end = sub_leaf_count(slices[0].len());

        assert!(blob.prove_sub_leaf(SpoolIndex(0), past_end, &slices[0]).is_none());
        assert!(blob.prove_sub_leaf(SpoolIndex(MAX_GROUP_SIZE as u64), 0, &slices[0]).is_none());
    }
}