espresso_types/v0/
nsproof.rs

1use hotshot_types::{
2    data::{VidCommitment, VidCommon},
3    vid::avidm::AvidMShare,
4};
5use serde::{Deserialize, Serialize};
6
7use crate::{
8    v0::{NamespaceId, NsIndex, NsPayload, NsTable, Payload, Transaction},
9    v0_1::ADVZNsProof,
10    v0_3::{AvidMIncorrectEncodingNsProof, AvidMNsProof},
11    v0_6::AvidmGf2NsProof,
12};
13
14#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
15pub struct NamespaceProofQueryData {
16    pub proof: Option<NsProof>,
17    pub transactions: Vec<Transaction>,
18}
19
20#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
21pub struct ADVZNamespaceProofQueryData {
22    pub proof: Option<ADVZNsProof>,
23    pub transactions: Vec<Transaction>,
24}
25
26/// Each variant represents a specific version of a namespace proof.
27#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28pub enum NsProof {
29    /// V0 proof for ADVZ
30    V0(ADVZNsProof),
31    /// V1 proof for AvidM, contains only correct encoding proof
32    V1(AvidMNsProof),
33    /// Incorrect encoding proof for AvidM (only supported after API version 1.1)
34    V1IncorrectEncoding(AvidMIncorrectEncodingNsProof),
35    /// V2 proof for AvidmGf2
36    V2(AvidmGf2NsProof),
37}
38
39impl NsProof {
40    pub fn new(payload: &Payload, index: &NsIndex, common: &VidCommon) -> Option<NsProof> {
41        match common {
42            VidCommon::V0(common) => Some(NsProof::V0(ADVZNsProof::new(payload, index, common)?)),
43            VidCommon::V1(common) => Some(NsProof::V1(AvidMNsProof::new(payload, index, common)?)),
44            VidCommon::V2(common) => {
45                Some(NsProof::V2(AvidmGf2NsProof::new(payload, index, common)?))
46            },
47        }
48    }
49
50    pub fn v1_1_new_with_incorrect_encoding(
51        shares: &[AvidMShare],
52        ns_table: &NsTable,
53        index: &NsIndex,
54        commit: &VidCommitment,
55        common: &VidCommon,
56    ) -> Option<NsProof> {
57        match common {
58            VidCommon::V1(common) => Some(NsProof::V1IncorrectEncoding(
59                AvidMIncorrectEncodingNsProof::new(shares, ns_table, index, commit, common)?,
60            )),
61            _ => None,
62        }
63    }
64
65    pub fn verify(
66        &self,
67        ns_table: &NsTable,
68        commit: &VidCommitment,
69        common: &VidCommon,
70    ) -> Option<(Vec<Transaction>, NamespaceId)> {
71        match (self, common) {
72            (Self::V0(proof), VidCommon::V0(common)) => proof.verify(ns_table, commit, common),
73            (Self::V1(proof), VidCommon::V1(common)) => proof.verify(ns_table, commit, common),
74            (Self::V1IncorrectEncoding(proof), VidCommon::V1(common)) => {
75                proof.verify(ns_table, commit, common)
76            },
77            (Self::V2(proof), VidCommon::V2(_)) => proof.verify(ns_table, commit, common),
78            _ => {
79                tracing::error!("Incompatible version of VidCommon and NsProof.");
80                None
81            },
82        }
83    }
84
85    pub fn export_all_txs(&self, ns_id: &NamespaceId) -> Vec<Transaction> {
86        match self {
87            Self::V0(proof) => proof.export_all_txs(ns_id),
88            Self::V1(AvidMNsProof(proof)) => {
89                NsPayload::from_bytes_slice(&proof.ns_payload).export_all_txs(ns_id)
90            },
91            Self::V1IncorrectEncoding(_) => vec![],
92            Self::V2(AvidmGf2NsProof(proof)) => {
93                NsPayload::from_bytes_slice(&proof.ns_payload).export_all_txs(ns_id)
94            },
95        }
96    }
97}