zebra_chain/primitives/proofs/
halo2.rs

1use serde::{Deserialize, Serialize};
2use std::{fmt, io};
3
4use crate::serialization::{
5    zcash_serialize_bytes, SerializationError, ZcashDeserialize, ZcashSerialize,
6};
7
8/// An encoding of a Halo2 proof, as used in [Zcash][halo2].
9///
10/// Halo2 proofs in Zcash Orchard do not have a fixed size, hence the newtype
11/// around a vector of bytes.
12///
13/// [halo2]: https://zips.z.cash/protocol/nu5.pdf#halo2
14#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
15pub struct Halo2Proof(pub Vec<u8>);
16
17impl Halo2Proof {
18    /// Encode as bytes for RPC usage.
19    pub fn bytes_in_display_order(&self) -> Vec<u8> {
20        self.0.to_vec()
21    }
22}
23
24impl fmt::Debug for Halo2Proof {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        f.debug_tuple("Halo2Proof")
27            .field(&hex::encode(&self.0[..]))
28            .finish()
29    }
30}
31
32impl ZcashSerialize for Halo2Proof {
33    fn zcash_serialize<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
34        zcash_serialize_bytes(&self.0, writer)
35    }
36}
37
38impl ZcashDeserialize for Halo2Proof {
39    fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
40        let proof = Vec::zcash_deserialize(&mut reader)?;
41
42        Ok(Self(proof))
43    }
44}
45#[cfg(any(test, feature = "proptest-impl"))]
46use proptest::prelude::*;
47
48#[cfg(any(test, feature = "proptest-impl"))]
49impl Arbitrary for Halo2Proof {
50    type Parameters = ();
51
52    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
53        (any::<Vec<u8>>()).prop_map(Self).boxed()
54    }
55
56    type Strategy = BoxedStrategy<Self>;
57}