[][src]Struct p0w::Tree

pub struct Tree { /* fields omitted */ }

A Merkle tree used to generate a Proofs

Implementations

impl Tree[src]

pub fn new<Desc: Into<Vec<u8>>>(desc: Desc, levels: usize) -> Self[src]

Creates a new Merkle tree with the specified service description and number of levels (including the root).

Increasing the number of levels will also increase the time spent computing the tree (ie. this is the way to increase the 'difficulty').

Panics

This function will panic if levels is less than 2.

Example

use p0w::Tree;

let tree = Tree::new("foobar", 8);
let proofs = tree.gen_proofs();

assert!(proofs.verify().is_ok());

pub fn par_new<Desc: Into<Vec<u8>>>(desc: Desc, levels: usize) -> Self[src]

Creates a new Merkle tree with the specified service description and number of levels (including the root) using rayon to parallelize the hash operations.

Increasing the number of levels will also increase the time spent computing the tree (ie. this is the way to increase the 'difficulty').

Panics

This function will panic if levels is less than 2.

Example

use p0w::Tree;

let tree = Tree::par_new("foobar", 8);
let proofs = tree.gen_proofs();

assert!(proofs.verify().is_ok());

pub fn description(&self) -> &[u8][src]

Returns the tree's service description (as passed to Tree::new()).

Example

use p0w::Tree;

let tree = Tree::new("foobar", 8);
assert_eq!(tree.description(), b"foobar");

pub fn levels(&self) -> usize[src]

Returns the number of levels (including the root) this tree contains (as passed to Tree::new()).

Example

use p0w::Tree;

let tree = Tree::new("foobar", 8);
assert_eq!(tree.levels(), 8);

pub fn as_nodes(&self) -> &[Hash][src]

Returns the tree's nodes in order (level by level).

Example

use p0w::Tree;

let tree = Tree::new("foobar", 8);

let nodes = tree.as_nodes();
assert_eq!(nodes.len(), 255);

pub fn gen_proofs(&self) -> Proofs[src]

Generates a new Proofs containing 16 * levels (as passed to Tree::new()) evenly-distributed, randomly selected leaves along with all internal nodes required to prove the validity of the tree.

This method selects the leaves in a random but evenly-distributed manner by seeding a ChaCha20Rng instance with the tree's root node, and generating 8 * levels numbers in the range 0..(leaves / 8).

This method is equivalent to calling Tree::gen_proofs_with(8 * levels).

Panics

This method panics if levels is less than 7.

Example

use p0w::Tree;

let tree = Tree::new("foobar", 8);
let proofs = tree.gen_proofs();

assert_eq!(proofs.proofs(), 64);

pub fn gen_proofs_with(&self, proofs: usize) -> Proofs[src]

Generates a new Proofs containing 2 * proofs evenly-distributed, randomly selected leaves along with all internal nodes required to prove the validity of the tree.

This method selects the leaves in a random but evenly-distributed manner by seeding a ChaCha20Rng instance with the tree's root node, and generating proofs numbers in the range 0..(leaves / proofs).

It is recommended that proofs is at least 8 times greater than the number of levels in the tree (as passed to Tree::new()).

Panics

This method panics if proofs is 0 or greater than the number of leaves the tree contains (leaves = 2 ^ (levels - 1)).

Example

use p0w::Tree;

let tree = Tree::new("foobar", 8);
let proofs = tree.gen_proofs_with(64);

assert_eq!(proofs.proofs(), 64);

Trait Implementations

impl Debug for Tree[src]

Auto Trait Implementations

impl RefUnwindSafe for Tree

impl Send for Tree

impl Sync for Tree

impl Unpin for Tree

impl UnwindSafe for Tree

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, 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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,