Map

Struct Map 

Source
pub struct Map<D, K, V>{ /* private fields */ }
Expand description

Immutable Map w/ Inclusion Proofs

§Usage

Using a Map should feel similar to using a HashMap. All maps begin by creating an empty map and then populating it with items. Each time you insert to a map, it creates a new map derived from the prior map. Once items are inserted, you can get an inclusion proof that demonstrates the presence of a key/value in a tree.

use warg_transparency::map::Map;
use sha2::Sha256;

let a = Map::<Sha256, &str, &str>::default();
let b = a.insert("foo", "bar");
let c = b.extend([("baz", "bat"), ("foo", "qux")]);

let proof = c.prove(&"foo").unwrap();
assert_eq!(c.root().clone(), proof.evaluate(&"foo", &"qux"));

§Design

A Map is a key/value store backed by a Merkle tree. Its values are stored in a binary tree. A cryptographic hash function is applied to the key and its resulting bits are interpreted as a path to descend the tree. This implies that the depth of the tree is n where n is the number of bits in the cryptographic hash function. Because this would cause the tree to have 2^(n+1) - 1 entries (far too many!), the tree is sparse and only creates nodes as necessary to represent the contents in the tree.

§Hashing Strategy

§Leaf Nodes

Leaf node hashes are calculated using the one-byte 0 prefix to prevent collisions with branch hashes.

hash(Leaf(value)) = hash(0x00 || <value>)

For leaf nodes that semantically “contain no value”, the <value> provided is empty i.e. the zero-length byte sequence.

§Branch Nodes

Branch node hashes are calculated using the one-byte 1 prefix to prevent collisions with branch hashes.

hash(Branch(left, right)) = hash(0x01 || hash(<left>) || hash(<right>))

Implementations§

Source§

impl<D, K, V> Map<D, K, V>

Source

pub fn root(&self) -> &Hash<D>

The hash of the root of the tree.

This uniquely identifies the map and its contents.

Source

pub fn len(&self) -> usize

The number of items in the map.

Source

pub fn is_empty(&self) -> bool

Whether or not the map is empty.

Source

pub fn prove(&self, key: K) -> Option<Proof<D, K, V>>

Gets the value for a given key and a proof of its presence in this map.

Source

pub fn insert(&self, key: K, val: V) -> Self

Insert a value into the map, creating a new map.

This replaces any existing items with the same key.

Source

pub fn extend(&self, iter: impl IntoIterator<Item = (K, V)>) -> Self

Inserts all key/value pairs into the map, creating a new map.

Trait Implementations§

Source§

impl<D, K, V> Clone for Map<D, K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate 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, K, V> Debug for Map<D, K, V>

Source§

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

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

impl<D, K, V> Default for Map<D, K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<D, K, V> Hash for Map<D, K, V>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<D, K, V> PartialEq for Map<D, K, V>

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, K, V> Eq for Map<D, K, V>

Auto Trait Implementations§

§

impl<D, K, V> Freeze for Map<D, K, V>

§

impl<D, K, V> RefUnwindSafe for Map<D, K, V>

§

impl<D, K, V> Send for Map<D, K, V>
where K: Send, V: Send,

§

impl<D, K, V> Sync for Map<D, K, V>
where K: Sync, V: Sync,

§

impl<D, K, V> Unpin for Map<D, K, V>

§

impl<D, K, V> UnwindSafe for Map<D, K, V>

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, dest: *mut u8)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.