nomt_core/
witness.rs

1//! Witnesses of NOMT sessions. These types encapsulate entire sets of reads and writes.
2
3use crate::{
4    proof::PathProof,
5    trie::{KeyPath, ValueHash},
6    trie_pos::TriePosition,
7};
8
9#[cfg(not(feature = "std"))]
10use alloc::vec::Vec;
11
12/// A witness that can be used to prove the correctness of state trie retrievals and updates.
13///
14/// Expected to be serializable.
15#[cfg_attr(
16    feature = "borsh",
17    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
18)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20pub struct Witness {
21    /// Various paths down the trie used as part of this witness.
22    /// Note that the paths are not necessarily in lexicographic order.
23    pub path_proofs: Vec<WitnessedPath>,
24    /// The operations witnessed by the paths.
25    pub operations: WitnessedOperations,
26}
27
28/// Operations provable by a corresponding witness.
29#[cfg_attr(
30    feature = "borsh",
31    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
32)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34pub struct WitnessedOperations {
35    /// Read operations.
36    pub reads: Vec<WitnessedRead>,
37    /// Write operations.
38    pub writes: Vec<WitnessedWrite>,
39}
40
41/// A path observed in the witness.
42#[cfg_attr(
43    feature = "borsh",
44    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
45)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47pub struct WitnessedPath {
48    /// Proof of a query path along the trie.
49    pub inner: PathProof,
50    /// The query path itself.
51    pub path: TriePosition,
52}
53
54/// A witness of a read value.
55#[cfg_attr(
56    feature = "borsh",
57    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
58)]
59#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
60pub struct WitnessedRead {
61    /// The key of the read value.
62    pub key: KeyPath,
63    /// The hash of the value witnessed. None means no value.
64    pub value: Option<ValueHash>,
65    /// The index of the path in the corresponding witness.
66    pub path_index: usize,
67}
68
69/// A witness of a write operation.
70#[cfg_attr(
71    feature = "borsh",
72    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
73)]
74#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
75pub struct WitnessedWrite {
76    /// The key of the written value.
77    pub key: KeyPath,
78    /// The hash of the written value. `None` means "delete".
79    pub value: Option<ValueHash>,
80    /// The index of the path in the corresponding witness.
81    pub path_index: usize,
82}