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,
};
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Pod, Zeroable)]
#[cfg_attr(feature = "wincode", derive(Serialize, Deserialize))]
pub struct BlobEncoding {
pub size: StorageUnits,
pub commitment: Hash,
pub stripe_size: StorageUnits,
pub stripe_count: StripeCount,
pub leaves: [Hash; MAX_GROUP_SIZE],
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SubLeafProof {
pub sub_leaf: Vec<u8>,
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
}
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))
}
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])
}
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
}
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,
})
}
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;
}
if proof.sub_proof.len() != SUB_TREE_HEIGHT {
return false;
}
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,
)
}
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<()> {
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;
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);
}
#[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));
}
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)
}
#[test]
fn full_root() {
let (blob, _) = coded_blob();
assert_eq!(blob.commitment_root(TAPENET_PROFILE), Some(blob.commitment));
}
#[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));
}
#[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);
}
#[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));
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());
}
}