zebra_chain/serialization/
serde_helpers.rs

1use group::{ff::PrimeField, GroupEncoding};
2use halo2::pasta::pallas;
3
4#[derive(Deserialize, Serialize)]
5#[serde(remote = "jubjub::AffinePoint")]
6pub struct AffinePoint {
7    #[serde(getter = "jubjub::AffinePoint::to_bytes")]
8    bytes: [u8; 32],
9}
10
11impl From<AffinePoint> for jubjub::AffinePoint {
12    fn from(local: AffinePoint) -> Self {
13        jubjub::AffinePoint::from_bytes(local.bytes).unwrap()
14    }
15}
16
17#[derive(Deserialize, Serialize)]
18#[serde(remote = "jubjub::Fq")]
19pub struct Fq {
20    #[serde(getter = "jubjub::Fq::to_bytes")]
21    bytes: [u8; 32],
22}
23
24impl From<Fq> for jubjub::Fq {
25    fn from(local: Fq) -> Self {
26        jubjub::Fq::from_bytes(&local.bytes).unwrap()
27    }
28}
29
30#[derive(Deserialize, Serialize)]
31#[serde(remote = "pallas::Affine")]
32pub struct Affine {
33    #[serde(getter = "pallas::Affine::to_bytes")]
34    bytes: [u8; 32],
35}
36
37impl From<Affine> for pallas::Affine {
38    fn from(local: Affine) -> Self {
39        pallas::Affine::from_bytes(&local.bytes).unwrap()
40    }
41}
42
43#[derive(Deserialize, Serialize)]
44#[serde(remote = "pallas::Scalar")]
45pub struct Scalar {
46    #[serde(getter = "pallas::Scalar::to_repr")]
47    bytes: [u8; 32],
48}
49
50impl From<Scalar> for pallas::Scalar {
51    fn from(local: Scalar) -> Self {
52        pallas::Scalar::from_repr(local.bytes).unwrap()
53    }
54}
55
56#[derive(Deserialize, Serialize)]
57#[serde(remote = "pallas::Base")]
58pub struct Base {
59    #[serde(getter = "pallas::Base::to_repr")]
60    bytes: [u8; 32],
61}
62
63impl From<Base> for pallas::Base {
64    fn from(local: Base) -> Self {
65        pallas::Base::from_repr(local.bytes).unwrap()
66    }
67}
68
69#[derive(Deserialize, Serialize)]
70#[serde(remote = "sapling_crypto::value::ValueCommitment")]
71pub struct ValueCommitment {
72    #[serde(getter = "sapling_crypto::value::ValueCommitment::to_bytes")]
73    bytes: [u8; 32],
74}
75
76impl From<ValueCommitment> for sapling_crypto::value::ValueCommitment {
77    fn from(local: ValueCommitment) -> Self {
78        sapling_crypto::value::ValueCommitment::from_bytes_not_small_order(&local.bytes).unwrap()
79    }
80}
81
82#[derive(Deserialize, Serialize)]
83#[serde(remote = "sapling_crypto::note::ExtractedNoteCommitment")]
84pub struct SaplingExtractedNoteCommitment {
85    #[serde(getter = "SaplingExtractedNoteCommitment::as_serializable_bytes")]
86    bytes: [u8; 32],
87}
88
89impl From<SaplingExtractedNoteCommitment> for sapling_crypto::note::ExtractedNoteCommitment {
90    fn from(local: SaplingExtractedNoteCommitment) -> Self {
91        sapling_crypto::note::ExtractedNoteCommitment::from_bytes(&local.bytes).unwrap()
92    }
93}
94
95impl SaplingExtractedNoteCommitment {
96    fn as_serializable_bytes(remote: &sapling_crypto::note::ExtractedNoteCommitment) -> [u8; 32] {
97        remote.to_bytes()
98    }
99}
100
101#[derive(Deserialize, Serialize)]
102#[serde(remote = "sapling_crypto::Node")]
103pub struct Node {
104    #[serde(getter = "Node::as_serializable_bytes")]
105    bytes: [u8; 32],
106}
107
108impl From<Node> for sapling_crypto::Node {
109    fn from(local: Node) -> Self {
110        sapling_crypto::Node::from_bytes(local.bytes).unwrap()
111    }
112}
113
114impl Node {
115    fn as_serializable_bytes(remote: &sapling_crypto::Node) -> [u8; 32] {
116        remote.to_bytes()
117    }
118}