merkle_log

Struct MerkleLog

Source
pub struct MerkleLog<D: Digest<N>, N: Node> { /* private fields */ }
Expand description

A Merkle Tree-Structured Log is a potentially unbalanced merkle tree containing the entries of an append-only log (maximum 2^63 + 1 entries).

It extends the functionality of 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 digest::Output;
use sha2::Sha256;

let mut store = MemoryStore::default();

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

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

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

Implementations§

Source§

impl<D, N> MerkleLog<D, N>
where D: Digest<N>, N: Node,

Source

pub fn new(entry: impl AsRef<[u8]>) -> Self

Creates a new MerkleLog from the first log entry.

Source

pub const fn len(&self) -> u64

The size of the log.

Source

pub const fn head(&self) -> &N

The Node of the current head.

Source

pub const fn head_id(&self) -> TreeID

The unique TreeID of the current head.

Source

pub const fn root(&self) -> &N

The merkle root Node of the log.

Source

pub const fn root_id(&self) -> TreeID

The unique TreeID of the current root.

Source

pub const fn root_height(&self) -> u8

The unique TreeID of the current tree root.

Source

pub fn proving_ids(&self, entry_index: u64) -> impl Iterator<Item = TreeID>

Produces the TreeIDs whose values are required to produce a valid proof of inclusion for a particular leaf entry in the log, starting from the head.

§Examples
use merkle_log::{MemoryStore, MerkleLog, Store, TreeID};
use digest::Output;
use sha2::Sha256;

let mut store = MemoryStore::default();

let entry = b"hello";
let mut log = MerkleLog::<Sha256, [u8; 32]>::new(&entry);
store.set_leaf(log.head_id(), *log.head()).unwrap();

log.append(&entry, &mut store).unwrap(); // new size 2
log.append(&entry, &mut store).unwrap(); // new size 3
assert_eq!(log.proving_ids(1).collect::<Vec<_>>(), &[TreeID::from(0), TreeID::from(4)]);

log.append(&entry, &mut store).unwrap(); // new size 4
assert_eq!(log.proving_ids(1).collect::<Vec<_>>(), &[TreeID::from(0), TreeID::from(5)]);
assert_eq!(log.proving_ids(2).collect::<Vec<_>>(), &[TreeID::from(6), TreeID::from(1)]);
Source

pub fn prove<S: Store<N>>( &self, entry_index: u64, store: &S, ) -> Result<Proof<N>, Error>

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

Source

pub fn verify( &self, entry_index: u64, entry_node: &N, proof: &Proof<N>, ) -> Result<bool, Error>

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

Source

pub fn appending_ids(&self) -> impl Iterator<Item = TreeID>

Produces the TreeIDs whose values are required to append the next entry to log. See TreeID::appending_ids for additional doctests.

§Examples
use merkle_log::{MemoryStore, MerkleLog, Store, TreeID};
use digest::Output;
use sha2::Sha256;

let mut store = MemoryStore::default();

let entry = b"hello";
let mut log = MerkleLog::<Sha256, [u8; 32]>::new(&entry);
store.set_leaf(log.head_id(), *log.head()).unwrap();
assert_eq!(log.appending_ids().collect::<Vec<_>>(), &[TreeID::from(0)]);

log.append(&entry, &mut store).unwrap(); // new size 2
assert_eq!(log.appending_ids().collect::<Vec<_>>(), &[TreeID::from(1)]);

log.append(&entry, &mut store).unwrap(); // new size 3
assert_eq!(log.appending_ids().collect::<Vec<_>>(), &[TreeID::from(1), TreeID::from(4)]);

log.append(&entry, &mut store).unwrap(); // new size 4
assert_eq!(log.appending_ids().collect::<Vec<_>>(), &[TreeID::from(3)]);
Source

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

Appends a new entry to the log, returning the new permanent Nodes to store.

§Examples
use merkle_log::{MerkleLog, MemoryStore, Store, TreeID};
use digest::Output;
use sha2::Sha256;

let mut store = MemoryStore::default();

let mut entry = b"hello";
let mut log = MerkleLog::<Sha256, [u8; 32]>::new(&entry);
store.set_leaf(log.head_id(), *log.head()).unwrap();
assert_eq!(log.len(), 1);
assert_eq!(log.head_id(), TreeID::from(0));
assert_eq!(log.head(), store.get(&log.head_id()).unwrap());

log.append(b"world", &mut store).unwrap();
assert_eq!(log.len(), 2);
assert_eq!(log.head_id(), TreeID::from(2));
assert_eq!(log.root(), store.get(&TreeID::from(1)).unwrap());

Trait Implementations§

Source§

impl<D: Clone + Digest<N>, N: Clone + Node> Clone for MerkleLog<D, N>

Source§

fn clone(&self) -> MerkleLog<D, N>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<D: Debug + Digest<N>, N: Debug + Node> Debug for MerkleLog<D, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<D: Digest<N>, N: Node> PartialEq for MerkleLog<D, N>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<D: Digest<N> + Clone, N: Node> Copy for MerkleLog<D, N>

Source§

impl<D: Digest<N>, N: Node> Eq for MerkleLog<D, N>

Auto Trait Implementations§

§

impl<D, N> Freeze for MerkleLog<D, N>
where N: Freeze,

§

impl<D, N> RefUnwindSafe for MerkleLog<D, N>

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.