[−][src]Struct sp4r53::Proof
A proof that a list of hashes are leaves part of a Tree
.
Proofs can be created either using Proof::new()
or Tree::proove()
.
Implementations
impl Proof
[src]
pub fn new(tree: &Tree, hashes: &[Hash]) -> Result<Self, Error>
[src]
Tries to generate a new proof for the inclusion of the given hashes in a Tree
.
Note that if the tree is invalid, an error will be returned. You'll either need to flush
the tree or use Tree::proove()
instead of this method.
Example
use sp4r53::{blake3, Proof, Tree}; let foo = blake3::hash(b"foo"); let bar = blake3::hash(b"bar"); let baz = blake3::hash(b"baz"); let mut tree = Tree::new(); tree.insert(foo); tree.insert(bar); tree.insert(baz); assert!(Proof::new(&tree, &[foo, baz]).is_err()); tree.flush(); let proof = Proof::new(&tree, &[foo, baz]).unwrap(); let root = tree.root().unwrap(); assert_eq!(proof.verify(root), true); let hashes = proof.hashes(); assert_eq!(hashes.len(), 2); assert_eq!(hashes.contains(&foo), true); assert_eq!(hashes.contains(&bar), false); assert_eq!(hashes.contains(&baz), true);
pub fn from_bytes(buf: &[u8]) -> Result<Self, Error>
[src]
Tries to convert bytes returned by as_bytes()
back to a Proof
.
Example
use sp4r53::{blake3, Proof, Tree}; let foo = blake3::hash(b"foo"); let bar = blake3::hash(b"bar"); let baz = blake3::hash(b"baz"); let mut tree = Tree::new(); tree.insert(foo); tree.insert(bar); tree.insert(baz); let root = tree.flush(); let proof = tree.proove(&[foo, bar]).unwrap(); assert_eq!(proof.verify(root), true); let encoded = proof.as_bytes(); let decoded = Proof::from_bytes(&encoded).unwrap(); assert_eq!(decoded.verify(root), true);
pub fn as_bytes(&self) -> Vec<u8>
[src]
Converts the proof into bytes using an efficient-ish format.
Format
height = 255..0 as u8 (little endian)
tag = (empty_tag || branch_tag || hash_tag || leaf_tag) (empty_tag || branch_tag || hash_tag || leaf_tag)
empty_tag = 0000 as u4
branch_tag = 0001 as u4
hash_tag = 0010 as u4
leaf_tag = 0011 as u4
node = branch || hash || leaf || empty
branch = height tag node node
hash = [u8; 32]
leaf = [u8; 32]
empty = ()
Example
use sp4r53::{blake3, Proof, Tree}; let hash1 = blake3::Hash::from([0; 32]); let hash2 = blake3::Hash::from([255; 32]); let mut tree = Tree::new(); tree.insert(hash1); tree.insert(hash2); let proof = tree.proove(&[hash1]).unwrap(); let mut encoded = vec![0; 66]; encoded[0] = 255u8.to_le(); encoded[1] = 0b0011_0010; encoded[2..34].copy_from_slice(hash1.as_bytes()); encoded[34..66].copy_from_slice(hash2.as_bytes()); assert_eq!(proof.as_bytes(), encoded);
pub fn hashes(&self) -> Vec<Hash>
[src]
Lists the hashes of the leaves this Proof
proves the inclusion of (the hashes that were
passed to Proof::new()
or Tree::proove()
).
Example
use sp4r53::{blake3, Tree}; let foo = blake3::hash(b"foo"); let bar = blake3::hash(b"bar"); let baz = blake3::hash(b"baz"); let mut tree = Tree::new(); tree.insert(foo); tree.insert(bar); tree.insert(baz); let proof = tree.proove(&[foo, baz]).unwrap(); let hashes = proof.hashes(); assert_eq!(hashes.len(), 2); assert_eq!(hashes.contains(&foo), true); assert_eq!(hashes.contains(&bar), false); assert_eq!(hashes.contains(&baz), true);
pub fn verify(&self, root: Hash) -> bool
[src]
Verifies and returns whether the proof is valid for the given tree root hash.
Example
use sp4r53::{blake3, Tree}; let foo = blake3::hash(b"foo"); let bar = blake3::hash(b"bar"); let baz = blake3::hash(b"baz"); let mut tree = Tree::new(); tree.insert(foo); tree.insert(bar); tree.insert(baz); let root = tree.flush(); let proof = tree.proove(&[foo, bar]).unwrap(); assert_eq!(proof.verify(root), true);
Trait Implementations
impl Debug for Proof
[src]
impl Eq for Proof
[src]
impl PartialEq<Proof> for Proof
[src]
impl StructuralEq for Proof
[src]
impl StructuralPartialEq for Proof
[src]
Auto Trait Implementations
impl RefUnwindSafe for Proof
impl Send for Proof
impl Sync for Proof
impl Unpin for Proof
impl UnwindSafe for Proof
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,