pub mod hash;
mod primitives;
pub trait SszEncode {
fn encode_ssz(&self) -> Vec<u8>;
fn encode_ssz_checked(&self) -> Result<Vec<u8>, String> {
Ok(self.encode_ssz())
}
fn encode_ssz_into(&self, out: &mut Vec<u8>) {
out.extend_from_slice(&self.encode_ssz());
}
unsafe fn write_fixed_ssz(&self, dst: *mut u8) {
let bytes = self.encode_ssz();
unsafe {
core::ptr::copy_nonoverlapping(bytes.as_ptr(), dst, bytes.len());
}
}
}
pub trait SszEncodeFixed: SszEncode + SszFixedLen {
#[inline]
fn encode_ssz_fixed_into(&self, out: &mut [u8]) {
let expected = Self::fixed_len();
debug_assert!(
out.len() == expected,
"fixed-size SSZ encode expects {} bytes, got {}",
expected,
out.len()
);
unsafe { self.write_fixed_ssz(out.as_mut_ptr()) };
}
}
impl<T> SszEncodeFixed for T where T: SszEncode + SszFixedLen {}
pub trait SszDecode: Sized {
fn decode_ssz(bytes: &[u8]) -> Result<Self, String>;
}
pub trait SszFixedLen {
fn fixed_len() -> usize;
fn tree_pack_basic() -> bool {
false
}
}
pub trait SszElement {
fn fixed_len_opt() -> Option<usize> {
None
}
fn tree_pack_basic() -> bool {
false
}
}
impl<T: SszFixedLen> SszElement for T {
fn fixed_len_opt() -> Option<usize> {
Some(T::fixed_len())
}
fn tree_pack_basic() -> bool {
T::tree_pack_basic()
}
}
pub trait HashTreeRoot {
fn hash_tree_root(&self) -> [u8; 32];
}