pub enum Proof {
    /// An inclusion proof for a Sparse Merkle Tree.
    Inclusion: InclusionProof,
    /// An exclusion proof for a Sparse Merkle Tree.
    Exclusion: ExclusionProof,
}
Expand description

A wrapper on inclusion and exclusion proofs for a Sparse Merkle Tree proof.

Variants

Inclusion: InclusionProof

An inclusion proof for a Sparse Merkle Tree.

Exclusion: ExclusionProof

An exclusion proof for a Sparse Merkle Tree.

Implementations

pub fn proof_set(self) -> Vec<b256>

Returns the underlying ProofSet of the Proof.

Returns

  • [ProofSet] - The underlying proof set that defines the proof.

Examples

use sway_libs::merkle::sparse::Proof;

fn foo() {
    let new_proof_set = ProofSet::new()
    let leaf = ExclusionLeaf::Placeholder;
    let new_proof = Proof::Exclusion(ExclusionProof::new(new_proof_set, leaf));
    let result_proof_set = new_proof.proof_set();
    assert(result_proof_set.len() == new_proof_set.len());
}

pub fn is_inclusion(self) -> bool

Returns whether the proof is the Inclusion variant.

Returns

  • [bool] - true if this is a Inclusion, otherwise false.

Examples

use sway_libs::merkle::sparse::Proof;

fn foo(proof: Proof) {
    assert(proof.is_inclusion());
}

pub fn is_exclusion(self) -> bool

Returns whether the proof is the Exclusion variant.

Returns

  • [bool] - true if this is a Exclusion, otherwise false.

Examples

use sway_libs::merkle::sparse::Proof;

fn foo(proof: Proof) {
    assert(proof.is_exclusion());
}

pub fn as_inclusion(self) -> Option<InclusionProof>

Returns the proof as the Inclusion variant.

Returns

  • [Option] - Some if this is a Inclusion, otherwise None.

Examples

use sway_libs::merkle::sparse::{Proof, InclusionProof};

fn foo(proof: Proof) {
    let in_proof = proof.as_inclusion();
    assert(in_proof.proof_set.len() != 0);
}

pub fn as_exclusion(self) -> Option<ExclusionProof>

Returns the proof as the Exclusion variant.

Returns

  • [Option] - Some if this is a Exclusion, otherwise None.

Examples

use sway_libs::merkle::sparse::{Proof, v};

fn foo(proof: Proof) {
    let ex_proof = proof.as_exclusion();
    assert(ex_proof.proof_set.len() != 0);
}

pub fn verify(
self,
root: MerkleRoot,
key: MerkleTreeKey,
leaf_data: Option<Bytes>,
) -> bool

Determines whether the computed root matches the root given on the proof.

Arguments

  • root: [MerkleRoot] - The pre-computed Sparse Merkle root that will be used to verify the leaf and proof.
  • key: [MerkleTreeKey] - The key associated with the particular leaf in the Sparse Merkle Tree.
  • leaf_data: [Option] - Some data that makes up the leaf on the Sparse Merkle Tree, None if this is an exclusion proof.

Returns

  • [bool] - true if the computed root matches the provided root, otherwise ‘false’.

Reverts

  • When leaf_data is None and this is an Inclusion proof.
  • When leaf_data is Some and this is an Exclusion proof.

Examples

use sway_libs::merkle::sparse::{Proof, MerkleTreeKey, InclusionProof};
use sway_libs::merkle::common::{ProofSet, MerkleRoot};
use std::bytes::Bytes;

fn foo() {
    let key = MerkleTreeKey::zero();
    let mut leaf = Bytes::new();
    leaf.push(1u8);
    let mut proof_set = ProofSet::new();
    proof_set.push(0xb51fc5c7f5b6393a5b13bb6068de2247ac09df1d3b1bec17627502cb1d1a6ac6);
    let proof = Proof::Inclusion(InclusionProof::new(proof_set));
    let root: MerkleRoot = 0xed84ee783dcb8999206160218e4fe8a1dc5ccb056e3b98f0a6fa633ca5896a47;

    assert(proof.verify(root, key, Some(leaf)) == true);
}

pub fn root(
self,
key: MerkleTreeKey,
leaf_data: Option<Bytes>,
) -> MerkleRoot

Computes the root of a Sparse Merkle Tree from the proof.

Arguments

  • key: [MerkleTreeKey] - The key associated with the particular leaf in the Sparse Merkle Tree.
  • leaf_data: [Option] - Some data that makes up the leaf on the Sparse Merkle Tree, None if this is an exclusion proof.

Returns

  • [MerkleRoot] - The computed merkle root.

Reverts

  • When leaf_data is None and this is an Inclusion proof.
  • When leaf_data is Some and this is an Exclusion proof.

Examples

use sway_libs::merkle::sparse::{Proof, MerkleTreeKey, InclusionProof};
use sway_libs::merkle::common::{ProofSet, MerkleRoot};

fn foo() {
    let key = MerkleTreeKey::zero();
    let mut leaf = Bytes::new();
    leaf.push(1u8);
    let mut proof_set = ProofSet::new();
    proof_set.push(0xb51fc5c7f5b6393a5b13bb6068de2247ac09df1d3b1bec17627502cb1d1a6ac6);
    let proof = Proof::Inclusion(InclusionProof::new(proof_set));

    let root: MerkleRoot = proof.root(key, Some(leaf));
    assert(root == 0xed84ee783dcb8999206160218e4fe8a1dc5ccb056e3b98f0a6fa633ca5896a47);
}

Trait Implementations

pub fn abi_encode(self, buffer: Buffer) -> Buffer

pub fn abi_decode(refmut buffer: BufferReader) -> Self

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

pub fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl PartialEq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}