Skip to main content

moonproof_prover/
versioned.rs

1//! Versioned proof types compatible with the official Midnight proof server.
2//!
3//! The Midnight SDK sends `ProofPreimageVersioned` (not bare `ProofPreimage`)
4//! and expects `ProofVersioned` (not bare `Proof`) in responses.
5//! These wrappers implement the same tagged serialization format.
6
7use std::borrow::Cow;
8use std::io::{Read, Write};
9use std::sync::Arc;
10
11use moonproof_ledger::serialize::{Deserializable, Serializable, Tagged};
12use moonproof_ledger::transient_crypto::proofs::{Proof, ProofPreimage};
13
14/// Versioned proof preimage — matches the official `ProofPreimageVersioned`.
15///
16/// Tag: `"proof-preimage-versioned"`
17/// Discriminant: 0 = invalid (V1, rejected), 1 = V2
18#[derive(Clone, Debug)]
19pub enum ProofPreimageVersioned {
20    V2(Arc<ProofPreimage>),
21}
22
23impl Serializable for ProofPreimageVersioned {
24    fn serialize(&self, writer: &mut impl Write) -> std::io::Result<()> {
25        match self {
26            ProofPreimageVersioned::V2(preimage) => {
27                Serializable::serialize(&1u8, writer)?;
28                preimage.serialize(writer)
29            }
30        }
31    }
32
33    fn serialized_size(&self) -> usize {
34        match self {
35            ProofPreimageVersioned::V2(preimage) => preimage.serialized_size() + 1,
36        }
37    }
38}
39
40impl Deserializable for ProofPreimageVersioned {
41    fn deserialize(reader: &mut impl Read, recursion_depth: u32) -> std::io::Result<Self> {
42        let discrim: u8 = Deserializable::deserialize(reader, recursion_depth)?;
43        match discrim {
44            0 => Err(std::io::Error::new(
45                std::io::ErrorKind::InvalidData,
46                format!("invalid old discriminant for ProofPreimageVersioned: {discrim}"),
47            )),
48            1 => Ok(ProofPreimageVersioned::V2(Arc::new(
49                ProofPreimage::deserialize(reader, recursion_depth)?,
50            ))),
51            _ => Err(std::io::Error::new(
52                std::io::ErrorKind::InvalidData,
53                format!("unknown discriminant for ProofPreimageVersioned: {discrim}"),
54            )),
55        }
56    }
57}
58
59impl Tagged for ProofPreimageVersioned {
60    fn tag() -> Cow<'static, str> {
61        "proof-preimage-versioned".into()
62    }
63    fn tag_unique_factor() -> String {
64        format!("[[],{}]", ProofPreimage::tag())
65    }
66}
67
68impl ProofPreimageVersioned {
69    /// Get the inner `ProofPreimage` reference.
70    pub fn inner(&self) -> &ProofPreimage {
71        match self {
72            ProofPreimageVersioned::V2(ppi) => ppi,
73        }
74    }
75
76    /// Get the key location from the inner preimage.
77    pub fn key_location(&self) -> &moonproof_ledger::transient_crypto::proofs::KeyLocation {
78        match self {
79            ProofPreimageVersioned::V2(ppi) => &ppi.key_location,
80        }
81    }
82}
83
84/// Versioned proof — matches the official `ProofVersioned`.
85///
86/// Tag: `"proof-versioned"`
87/// Discriminant: 0 = invalid (V1, rejected), 1 = V2
88#[derive(Clone, Debug)]
89pub enum ProofVersioned {
90    V2(Proof),
91}
92
93impl Serializable for ProofVersioned {
94    fn serialize(&self, writer: &mut impl Write) -> std::io::Result<()> {
95        match self {
96            ProofVersioned::V2(proof) => {
97                Serializable::serialize(&1u8, writer)?;
98                proof.serialize(writer)
99            }
100        }
101    }
102
103    fn serialized_size(&self) -> usize {
104        match self {
105            ProofVersioned::V2(proof) => proof.serialized_size() + 1,
106        }
107    }
108}
109
110impl Deserializable for ProofVersioned {
111    fn deserialize(reader: &mut impl Read, recursion_depth: u32) -> std::io::Result<Self> {
112        let discrim: u8 = Deserializable::deserialize(reader, recursion_depth)?;
113        match discrim {
114            0 => Err(std::io::Error::new(
115                std::io::ErrorKind::InvalidData,
116                format!("invalid old discriminant for ProofVersioned: {discrim}"),
117            )),
118            1 => Ok(ProofVersioned::V2(Proof::deserialize(
119                reader,
120                recursion_depth,
121            )?)),
122            _ => Err(std::io::Error::new(
123                std::io::ErrorKind::InvalidData,
124                format!("unknown discriminant for ProofVersioned: {discrim}"),
125            )),
126        }
127    }
128}
129
130impl Tagged for ProofVersioned {
131    fn tag() -> Cow<'static, str> {
132        "proof-versioned".into()
133    }
134    fn tag_unique_factor() -> String {
135        format!("[[],{}]", Proof::tag())
136    }
137}