Struct merkle_log::MerkleLog[][src]

pub struct MerkleLog<D: Digest> { /* fields omitted */ }

A Merkle Tree-Structured Log is a potentially unbalanced merkle tree containing the entries of an append-only log.

It extends a traditional merkle tree by allowing for:

  • continually appending new entries (even when the length of the log is not a power of two)
  • providing proofs that a previous log head is a prefix of (contained within) the current log.

Example

use merkle_log::{MemoryStore, MerkleLog, Store};
use sha2::Sha256;

#[async_std::main]
async fn main() {
    let mut store = MemoryStore::default();

    // first entry
    let entry = b"hello";
    let mut log = MerkleLog::<Sha256>::new(&entry, &mut store).await.unwrap();
    let initial_head = *log.head();
    let initial_log = log.clone();

    // second entry
    let entry = b"world";
    log.append(entry, &mut store).await.unwrap();

    // prove existence of initial entry by its digest
    let proof = log.prove(0, &initial_head, &store).await.unwrap();
    assert!(log.verify(0, &initial_head, &proof));

    // prove that head is a prefix of the current log
    let (prefix_proof, head_proof) = log.prove_prefix(&initial_log, &store).await.unwrap();
    assert!(log.verify_prefix(&head_proof, &initial_log, &prefix_proof));
}

Implementations

impl<D: Digest> MerkleLog<D>[src]

pub async fn new<S: Store>(
    entry: impl AsRef<[u8]>,
    store: &mut S
) -> Result<Self, Error>
[src]

Creates a new MerkleLog from the first log entry.

pub fn root_id(&self) -> TreeID[src]

The unique TreeID of the current tree root. TreeID: crate::TreeID

pub fn head_id(&self) -> TreeID[src]

The unique TreeID of the current head. TreeID: crate::TreeID

pub fn size(&self) -> u64[src]

The size of the log.

pub fn head(&self) -> &Node[src]

The digest of the current head.

pub fn root(&self) -> &Node[src]

The merkle root of the log.

pub async fn prove<S: Store>(
    &self,
    entry_index: u64,
    entry_node: &Node,
    store: &S
) -> Result<Proof, Error>
[src]

Creates a proof that an entry is contained within the current log.

pub fn verify(&self, entry_index: u64, entry_node: &Node, proof: &Proof) -> bool[src]

Verifies a proof asserting that theentry_node exists at entry_index within the current log.

pub async fn prove_prefix<S: Store>(
    &self,
    prefix: &Self,
    store: &S
) -> Result<(Proof, Proof), Error>
[src]

Creates a proof that a prior log head is a prefix of the current log, as well as a proof that the current head is within the same log.

This involves producing a set of nodes, some of which are common to the subtree containing the prefix and the full tree containing both the prefix and the head. Verifying these proofs should convince the verifier that both the prefix and the head belong to the same log.

pub fn verify_prefix(
    &self,
    head_proof: &Proof,
    prefix: &Self,
    prefix_proof: &Proof
) -> bool
[src]

Verifies the prefix and head proofs against a known prefix and the current log.

pub async fn append<S: Store>(
    &mut self,
    entry: impl AsRef<[u8]>,
    store: &mut S
) -> Result<Node, Error>
[src]

Appends a new entry to the log.

Trait Implementations

impl<D: Clone + Digest> Clone for MerkleLog<D>[src]

impl<D: Clone + Digest> Copy for MerkleLog<D> where
    Output<D>: Copy
[src]

impl<D: Debug + Digest> Debug for MerkleLog<D>[src]

Auto Trait Implementations

impl<D> RefUnwindSafe for MerkleLog<D> where
    D: RefUnwindSafe

impl<D> Send for MerkleLog<D> where
    D: Send

impl<D> Sync for MerkleLog<D> where
    D: Sync

impl<D> Unpin for MerkleLog<D> where
    D: Unpin

impl<D> UnwindSafe for MerkleLog<D> where
    D: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.