zebra_chain/orchard/
commitment.rs1use std::{fmt, io};
4
5use group::{ff::PrimeField, CurveAffine as _, GroupEncoding};
6use halo2::{
7 arithmetic::{Coordinates, CurveAffine},
8 pasta::pallas,
9};
10
11use crate::serialization::{
12 serde_helpers, ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize,
13};
14
15#[derive(Copy, Clone, Debug, PartialEq, Eq)]
17pub struct CommitmentRandomness(pallas::Scalar);
18
19#[derive(Clone, Copy, Deserialize, PartialEq, Eq, Serialize)]
24pub struct ValueCommitment(#[serde(with = "serde_helpers::Affine")] pub pallas::Affine);
25
26impl<'a> std::ops::Add<&'a ValueCommitment> for ValueCommitment {
27 type Output = Self;
28
29 fn add(self, rhs: &'a ValueCommitment) -> Self::Output {
30 self + *rhs
31 }
32}
33
34impl std::ops::Add<ValueCommitment> for ValueCommitment {
35 type Output = Self;
36
37 fn add(self, rhs: ValueCommitment) -> Self::Output {
38 ValueCommitment((self.0 + rhs.0).into())
39 }
40}
41
42impl std::ops::AddAssign<ValueCommitment> for ValueCommitment {
43 fn add_assign(&mut self, rhs: ValueCommitment) {
44 *self = *self + rhs
45 }
46}
47
48impl fmt::Debug for ValueCommitment {
49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 let mut d = f.debug_struct("ValueCommitment");
51
52 let option: Option<Coordinates<pallas::Affine>> = self.0.coordinates().into();
53
54 match option {
55 Some(coordinates) => d
56 .field("x", &hex::encode(coordinates.x().to_repr()))
57 .field("y", &hex::encode(coordinates.y().to_repr()))
58 .finish(),
59 None => d
60 .field("x", &hex::encode(pallas::Base::zero().to_repr()))
61 .field("y", &hex::encode(pallas::Base::zero().to_repr()))
62 .finish(),
63 }
64 }
65}
66
67impl From<pallas::Point> for ValueCommitment {
68 fn from(projective_point: pallas::Point) -> Self {
69 Self(pallas::Affine::from(projective_point))
70 }
71}
72
73impl From<ValueCommitment> for [u8; 32] {
77 fn from(cm: ValueCommitment) -> [u8; 32] {
78 cm.0.to_bytes()
79 }
80}
81
82impl<'a> std::ops::Sub<&'a ValueCommitment> for ValueCommitment {
83 type Output = Self;
84
85 fn sub(self, rhs: &'a ValueCommitment) -> Self::Output {
86 self - *rhs
87 }
88}
89
90impl std::ops::Sub<ValueCommitment> for ValueCommitment {
91 type Output = Self;
92
93 fn sub(self, rhs: ValueCommitment) -> Self::Output {
94 ValueCommitment((self.0 - rhs.0).into())
95 }
96}
97
98impl std::ops::SubAssign<ValueCommitment> for ValueCommitment {
99 fn sub_assign(&mut self, rhs: ValueCommitment) {
100 *self = *self - rhs;
101 }
102}
103
104impl std::iter::Sum for ValueCommitment {
105 fn sum<I>(iter: I) -> Self
106 where
107 I: Iterator<Item = Self>,
108 {
109 iter.fold(
110 ValueCommitment(pallas::Affine::identity()),
111 std::ops::Add::add,
112 )
113 }
114}
115
116impl TryFrom<[u8; 32]> for ValueCommitment {
120 type Error = &'static str;
121
122 fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
123 let possible_point = pallas::Affine::from_bytes(&bytes);
124
125 if possible_point.is_some().into() {
126 Ok(Self(possible_point.unwrap()))
127 } else {
128 Err("Invalid pallas::Affine value")
129 }
130 }
131}
132
133impl ZcashSerialize for ValueCommitment {
134 fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
135 writer.write_all(&<[u8; 32]>::from(*self)[..])?;
136 Ok(())
137 }
138}
139
140impl ZcashDeserialize for ValueCommitment {
141 fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
142 Self::try_from(reader.read_32_bytes()?).map_err(SerializationError::Parse)
143 }
144}
145
146#[cfg(test)]
147mod tests {
148
149 use std::ops::Neg;
150
151 use group::Group;
152
153 use super::*;
154
155 #[test]
156 fn add() {
157 let _init_guard = zebra_test::init();
158
159 let identity = ValueCommitment(pallas::Affine::identity());
160
161 let g = ValueCommitment(pallas::Affine::generator());
162
163 assert_eq!(identity + g, g);
164 }
165
166 #[test]
167 fn add_assign() {
168 let _init_guard = zebra_test::init();
169
170 let mut identity = ValueCommitment(pallas::Affine::identity());
171
172 let g = ValueCommitment(pallas::Affine::generator());
173
174 identity += g;
175 let new_g = identity;
176
177 assert_eq!(new_g, g);
178 }
179
180 #[test]
181 fn sub() {
182 let _init_guard = zebra_test::init();
183
184 let g_point = pallas::Affine::generator();
185
186 let identity = ValueCommitment(pallas::Affine::identity());
187
188 let g = ValueCommitment(g_point);
189
190 assert_eq!(identity - g, ValueCommitment(g_point.neg()));
191 }
192
193 #[test]
194 fn sub_assign() {
195 let _init_guard = zebra_test::init();
196
197 let g_point = pallas::Affine::generator();
198
199 let mut identity = ValueCommitment(pallas::Affine::identity());
200
201 let g = ValueCommitment(g_point);
202
203 identity -= g;
204 let new_g = identity;
205
206 assert_eq!(new_g, ValueCommitment(g_point.neg()));
207 }
208
209 #[test]
210 fn sum() {
211 let _init_guard = zebra_test::init();
212
213 let g_point = pallas::Affine::generator();
214
215 let g = ValueCommitment(g_point);
216 let other_g = ValueCommitment(g_point);
217
218 let sum: ValueCommitment = vec![g, other_g].into_iter().sum();
219
220 let doubled_g = ValueCommitment(g_point.to_curve().double().into());
221
222 assert_eq!(sum, doubled_g);
223 }
224}