noah_algebra/
serialization.rs1use crate::{
2 bls12_381::{BLSFq, BLSGt, BLSScalar, BLSG1, BLSG2},
3 ed25519::{Ed25519Point, Ed25519Scalar},
4 jubjub::{JubjubPoint, JubjubScalar},
5 prelude::*,
6 ristretto::{CompressedEdwardsY, CompressedRistretto, RistrettoPoint, RistrettoScalar},
7 secp256k1::{SECP256K1Scalar, SECP256K1G1},
8 secq256k1::SECQ256K1Proof,
9 secq256k1::{SECQ256K1Scalar, SECQ256K1G1},
10 zorro::{ZorroFq, ZorroG1, ZorroProof, ZorroScalar},
11};
12use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate};
13use ark_std::io::Cursor;
14use bulletproofs::RangeProof;
15use serde::Serializer;
16
17macro_rules! to_from_bytes_scalar {
18 ($t:ident) => {
19 impl NoahFromToBytes for $t {
20 fn noah_to_bytes(&self) -> Vec<u8> {
21 let mut v = vec![];
22 v.extend_from_slice(&self.to_bytes()[..]);
23 v
24 }
25 fn noah_from_bytes(bytes: &[u8]) -> Result<$t> {
26 $t::from_bytes(bytes)
27 .map_err(|_| eg!(crate::errors::NoahError::DeserializationError))
28 }
29 }
30 };
31}
32
33to_from_bytes_scalar!(BLSScalar);
34to_from_bytes_scalar!(BLSFq);
35to_from_bytes_scalar!(JubjubScalar);
36to_from_bytes_scalar!(SECQ256K1Scalar);
37to_from_bytes_scalar!(SECP256K1Scalar);
38to_from_bytes_scalar!(ZorroScalar);
39to_from_bytes_scalar!(ZorroFq);
40to_from_bytes_scalar!(RistrettoScalar);
41to_from_bytes_scalar!(Ed25519Scalar);
42
43impl NoahFromToBytes for CompressedRistretto {
44 #[inline]
45 fn noah_to_bytes(&self) -> Vec<u8> {
46 self.0.as_bytes().to_vec()
47 }
48 #[inline]
49 fn noah_from_bytes(bytes: &[u8]) -> Result<Self> {
50 Ok(Self(
51 curve25519_dalek::ristretto::CompressedRistretto::from_slice(bytes),
52 ))
53 }
54}
55
56impl NoahFromToBytes for CompressedEdwardsY {
57 #[inline]
58 fn noah_to_bytes(&self) -> Vec<u8> {
59 self.0.as_bytes().to_vec()
60 }
61 #[inline]
62 fn noah_from_bytes(bytes: &[u8]) -> Result<Self> {
63 Ok(CompressedEdwardsY(
64 curve25519_dalek::edwards::CompressedEdwardsY::from_slice(bytes),
65 ))
66 }
67}
68
69serialize_deserialize!(BLSScalar);
70serialize_deserialize!(BLSFq);
71serialize_deserialize!(JubjubScalar);
72serialize_deserialize!(SECQ256K1Scalar);
73serialize_deserialize!(SECP256K1Scalar);
74serialize_deserialize!(ZorroScalar);
75serialize_deserialize!(ZorroFq);
76serialize_deserialize!(RistrettoScalar);
77serialize_deserialize!(Ed25519Scalar);
78serialize_deserialize!(CompressedRistretto);
79serialize_deserialize!(CompressedEdwardsY);
80
81macro_rules! to_from_bytes_group {
82 ($g:ident) => {
83 impl NoahFromToBytes for $g {
84 fn noah_to_bytes(&self) -> Vec<u8> {
85 self.to_compressed_bytes()
86 }
87 fn noah_from_bytes(bytes: &[u8]) -> Result<$g> {
88 $g::from_compressed_bytes(bytes)
89 .map_err(|_| eg!(crate::errors::NoahError::SerializationError))
90 }
91 }
92 };
93}
94
95to_from_bytes_group!(BLSG1);
96to_from_bytes_group!(BLSG2);
97to_from_bytes_group!(BLSGt);
98to_from_bytes_group!(JubjubPoint);
99to_from_bytes_group!(SECQ256K1G1);
100to_from_bytes_group!(SECP256K1G1);
101to_from_bytes_group!(ZorroG1);
102to_from_bytes_group!(RistrettoPoint);
103to_from_bytes_group!(Ed25519Point);
104
105serialize_deserialize!(BLSG1);
106serialize_deserialize!(BLSG2);
107serialize_deserialize!(BLSGt);
108serialize_deserialize!(JubjubPoint);
109serialize_deserialize!(SECQ256K1G1);
110serialize_deserialize!(SECP256K1G1);
111serialize_deserialize!(ZorroG1);
112serialize_deserialize!(RistrettoPoint);
113serialize_deserialize!(Ed25519Point);
114
115pub trait NoahFromToBytes: Sized {
117 fn noah_to_bytes(&self) -> Vec<u8>;
119 fn noah_from_bytes(bytes: &[u8]) -> Result<Self>;
121}
122
123impl NoahFromToBytes for RangeProof {
124 fn noah_to_bytes(&self) -> Vec<u8> {
125 let mut v = vec![];
126 v.extend_from_slice(&self.to_bytes()[..]);
127 v
128 }
129 fn noah_from_bytes(bytes: &[u8]) -> Result<RangeProof> {
130 RangeProof::from_bytes(bytes).map_err(|_| eg!(NoahError::DeserializationError))
131 }
132}
133
134impl NoahFromToBytes for bulletproofs::r1cs::R1CSProof {
135 fn noah_to_bytes(&self) -> Vec<u8> {
136 self.to_bytes()
137 }
138 fn noah_from_bytes(bytes: &[u8]) -> Result<bulletproofs::r1cs::R1CSProof> {
139 bulletproofs::r1cs::R1CSProof::from_bytes(bytes)
140 .map_err(|_| eg!(NoahError::DeserializationError))
141 }
142}
143
144impl NoahFromToBytes for SECQ256K1Proof {
145 fn noah_to_bytes(&self) -> Vec<u8> {
146 let mut cursor = Cursor::new(Vec::new());
147 self.serialize_with_mode(&mut cursor, Compress::Yes)
148 .unwrap();
149 cursor.into_inner()
150 }
151 fn noah_from_bytes(bytes: &[u8]) -> Result<SECQ256K1Proof> {
152 ark_bulletproofs::r1cs::R1CSProof::deserialize_with_mode(
153 bytes,
154 Compress::Yes,
155 Validate::Yes,
156 )
157 .map_err(|_| eg!(NoahError::DeserializationError))
158 }
159}
160
161impl NoahFromToBytes for ZorroProof {
162 fn noah_to_bytes(&self) -> Vec<u8> {
163 let mut cursor = Cursor::new(Vec::new());
164 self.serialize_with_mode(&mut cursor, Compress::Yes)
165 .unwrap();
166 cursor.into_inner()
167 }
168 fn noah_from_bytes(bytes: &[u8]) -> Result<ZorroProof> {
169 ark_bulletproofs::r1cs::R1CSProof::deserialize_with_mode(
170 bytes,
171 Compress::Yes,
172 Validate::Yes,
173 )
174 .map_err(|_| eg!(NoahError::DeserializationError))
175 }
176}
177
178impl NoahFromToBytes for x25519_dalek::PublicKey {
179 fn noah_to_bytes(&self) -> Vec<u8> {
180 let mut v = vec![];
181 v.extend_from_slice(self.as_bytes());
182 v
183 }
184 fn noah_from_bytes(bytes: &[u8]) -> Result<x25519_dalek::PublicKey> {
185 if bytes.len() < 32 {
186 return Err(eg!(NoahError::SerializationError));
187 }
188 let mut array = [0u8; 32];
189 array.copy_from_slice(&bytes[0..32]);
190 Ok(x25519_dalek::PublicKey::from(array))
191 }
192}
193
194impl NoahFromToBytes for x25519_dalek::StaticSecret {
195 fn noah_to_bytes(&self) -> Vec<u8> {
196 let mut v = vec![];
197 v.extend_from_slice(&self.to_bytes()[..]);
198 v
199 }
200 fn noah_from_bytes(bytes: &[u8]) -> Result<x25519_dalek::StaticSecret> {
201 if bytes.len() < 32 {
202 return Err(eg!(NoahError::SerializationError));
203 }
204 let mut array = [0u8; 32];
205 array.copy_from_slice(&bytes[0..32]);
206 Ok(x25519_dalek::StaticSecret::from(array))
207 }
208}
209
210pub mod noah_obj_serde {
212 use crate::serialization::NoahFromToBytes;
213 use crate::utils::{b64dec, b64enc};
214 use serde::de::SeqAccess;
215 use serde::de::Visitor;
216 use serde::Deserializer;
217 use serde::Serializer;
218
219 pub struct BytesVisitor;
221
222 impl<'de> Visitor<'de> for BytesVisitor {
223 type Value = Vec<u8>;
224
225 fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> core::fmt::Result {
226 formatter.write_str("a valid NoahFromTo Object")
227 }
228
229 fn visit_seq<V>(self, mut seq: V) -> Result<Vec<u8>, V::Error>
230 where
231 V: SeqAccess<'de>,
232 {
233 let mut vec: Vec<u8> = vec![];
234 while let Some(x) = seq.next_element().map_err(serde::de::Error::custom)? {
235 vec.push(x);
236 }
237 Ok(vec)
238 }
239
240 fn visit_bytes<E>(self, v: &[u8]) -> Result<Vec<u8>, E> {
241 let mut vec: Vec<u8> = vec![];
242 vec.extend_from_slice(v);
243 Ok(vec)
244 }
245
246 fn visit_str<E>(self, v: &str) -> Result<Vec<u8>, E>
247 where
248 E: serde::de::Error,
249 {
250 b64dec(v).map_err(serde::de::Error::custom)
251 }
252 }
253
254 pub fn serialize<S, T>(obj: &T, serializer: S) -> Result<S::Ok, S::Error>
256 where
257 S: Serializer,
258 T: NoahFromToBytes,
259 {
260 let bytes = obj.noah_to_bytes();
261 if serializer.is_human_readable() {
262 serializer.serialize_str(&b64enc(&bytes))
263 } else {
264 serializer.serialize_bytes(&bytes[..])
265 }
266 }
267
268 pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
270 where
271 D: Deserializer<'de>,
272 T: NoahFromToBytes,
273 {
274 if deserializer.is_human_readable() {
275 let bytes = deserializer.deserialize_str(BytesVisitor)?;
276 T::noah_from_bytes(bytes.as_slice()).map_err(serde::de::Error::custom)
277 } else {
278 let v = deserializer.deserialize_bytes(BytesVisitor)?;
279 T::noah_from_bytes(v.as_slice()).map_err(serde::de::Error::custom)
280 }
281 }
282}