1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::io;
use super::context_extension::ContextExtension;
use super::{Base16DecodedBytes, Base16EncodedBytes};
use crate::serialization::{
sigma_byte_reader::SigmaByteRead, sigma_byte_writer::SigmaByteWrite, SerializationError,
SigmaSerializable,
};
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "json",
serde(into = "Base16EncodedBytes", try_from = "Base16DecodedBytes")
)]
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum ProofBytes {
Empty,
Some(Vec<u8>),
}
impl Into<Base16EncodedBytes> for ProofBytes {
fn into(self) -> Base16EncodedBytes {
match self {
ProofBytes::Empty => Base16EncodedBytes::new(vec![].as_slice()),
ProofBytes::Some(bytes) => Base16EncodedBytes::new(&bytes),
}
}
}
impl From<Base16DecodedBytes> for ProofBytes {
fn from(v: Base16DecodedBytes) -> Self {
if v.0.is_empty() {
ProofBytes::Empty
} else {
ProofBytes::Some(v.0)
}
}
}
impl SigmaSerializable for ProofBytes {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> Result<(), io::Error> {
match self {
ProofBytes::Empty => w.put_u16(0)?,
ProofBytes::Some(bytes) => {
w.put_u16(bytes.len() as u16)?;
w.write_all(&bytes)?;
}
}
Ok(())
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SerializationError> {
let proof_len = r.get_u16()?;
Ok(if proof_len == 0 {
ProofBytes::Empty
} else {
let mut bytes = vec![0; proof_len as usize];
r.read_exact(&mut bytes)?;
ProofBytes::Some(bytes)
})
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
pub struct ProverResult {
#[cfg_attr(feature = "json", serde(rename = "proofBytes"))]
pub proof: ProofBytes,
#[cfg_attr(feature = "json", serde(rename = "extension"))]
pub extension: ContextExtension,
}
impl SigmaSerializable for ProverResult {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> Result<(), io::Error> {
self.proof.sigma_serialize(w)?;
self.extension.sigma_serialize(w)?;
Ok(())
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SerializationError> {
let proof = ProofBytes::sigma_parse(r)?;
let extension = ContextExtension::sigma_parse(r)?;
Ok(ProverResult { proof, extension })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::serialization::sigma_serialize_roundtrip;
use proptest::{collection::vec, prelude::*};
impl Arbitrary for ProofBytes {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
prop_oneof![
Just(ProofBytes::Empty),
(vec(any::<u8>(), 32..100))
.prop_map(ProofBytes::Some)
.boxed()
]
.boxed()
}
}
impl Arbitrary for ProverResult {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(any::<ProofBytes>(), any::<ContextExtension>())
.prop_map(|(proof, extension)| Self { proof, extension })
.boxed()
}
}
proptest! {
#[test]
fn ser_roundtrip(v in any::<ProverResult>()) {
prop_assert_eq![sigma_serialize_roundtrip(&v), v];
}
}
}