amadeus_utils/
types.rs

1/// Primitive types used in Amadeus
2
3/// First 4 bytes of the BLAKE3 hash (for optimisation purposes)
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, bincode::Encode, bincode::Decode)]
5pub struct B3f4([u8; 4]);
6
7impl B3f4 {
8    pub fn new(b3: &[u8; 32]) -> Self {
9        B3f4([b3[0], b3[1], b3[2], b3[3]])
10    }
11}
12
13impl serde::Serialize for B3f4 {
14    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
15    where
16        S: serde::Serializer,
17    {
18        serde_bytes::Bytes::new(&self.0).serialize(serializer)
19    }
20}
21
22impl<'de> serde::Deserialize<'de> for B3f4 {
23    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
24    where
25        D: serde::Deserializer<'de>,
26    {
27        let bytes = serde_bytes::ByteBuf::deserialize(deserializer)?;
28        let array: [u8; 4] = bytes.into_vec().try_into().map_err(|_| serde::de::Error::custom("expected 4 bytes"))?;
29        Ok(B3f4(array))
30    }
31}
32
33impl From<[u8; 4]> for B3f4 {
34    fn from(bytes: [u8; 4]) -> Self {
35        B3f4(bytes)
36    }
37}
38
39impl From<B3f4> for [u8; 4] {
40    fn from(b3f4: B3f4) -> Self {
41        b3f4.0
42    }
43}
44
45impl AsRef<[u8; 4]> for B3f4 {
46    fn as_ref(&self) -> &[u8; 4] {
47        &self.0
48    }
49}
50
51impl std::ops::Deref for B3f4 {
52    type Target = [u8; 4];
53
54    fn deref(&self) -> &Self::Target {
55        &self.0
56    }
57}
58
59/// BLS12-381 signature
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, bincode::Encode, bincode::Decode)]
61pub struct Signature([u8; 96]);
62
63impl Signature {
64    pub const fn new(bytes: [u8; 96]) -> Self {
65        Signature(bytes)
66    }
67}
68
69impl serde::Serialize for Signature {
70    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
71    where
72        S: serde::Serializer,
73    {
74        serde_bytes::Bytes::new(&self.0).serialize(serializer)
75    }
76}
77
78impl<'de> serde::Deserialize<'de> for Signature {
79    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
80    where
81        D: serde::Deserializer<'de>,
82    {
83        let bytes = serde_bytes::ByteBuf::deserialize(deserializer)?;
84        let array: [u8; 96] = bytes.into_vec().try_into().map_err(|_| serde::de::Error::custom("expected 96 bytes"))?;
85        Ok(Signature(array))
86    }
87}
88
89impl From<[u8; 96]> for Signature {
90    fn from(bytes: [u8; 96]) -> Self {
91        Signature(bytes)
92    }
93}
94
95impl From<Signature> for [u8; 96] {
96    fn from(sig: Signature) -> Self {
97        sig.0
98    }
99}
100
101impl AsRef<[u8; 96]> for Signature {
102    fn as_ref(&self) -> &[u8; 96] {
103        &self.0
104    }
105}
106
107impl AsRef<[u8]> for Signature {
108    fn as_ref(&self) -> &[u8] {
109        &self.0
110    }
111}
112
113impl std::ops::Deref for Signature {
114    type Target = [u8; 96];
115
116    fn deref(&self) -> &Self::Target {
117        &self.0
118    }
119}
120
121impl TryFrom<&[u8]> for Signature {
122    type Error = std::array::TryFromSliceError;
123    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
124        Ok(Signature(<[u8; 96]>::try_from(slice)?))
125    }
126}
127
128impl TryFrom<Vec<u8>> for Signature {
129    type Error = Vec<u8>;
130    fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
131        Ok(Signature(vec.try_into()?))
132    }
133}
134
135impl PartialEq<[u8]> for Signature {
136    fn eq(&self, other: &[u8]) -> bool {
137        &self.0[..] == other
138    }
139}
140
141impl PartialEq<Signature> for [u8] {
142    fn eq(&self, other: &Signature) -> bool {
143        self == &other.0[..]
144    }
145}
146
147impl PartialEq<&[u8]> for Signature {
148    fn eq(&self, other: &&[u8]) -> bool {
149        &self.0[..] == *other
150    }
151}
152
153impl std::borrow::Borrow<[u8]> for Signature {
154    fn borrow(&self) -> &[u8] {
155        &self.0
156    }
157}
158
159impl std::borrow::Borrow<[u8; 96]> for Signature {
160    fn borrow(&self) -> &[u8; 96] {
161        &self.0
162    }
163}
164
165/// Blake3/SHA256 32byte hash
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, bincode::Encode, bincode::Decode)]
167pub struct Hash([u8; 32]);
168
169impl Hash {
170    pub const fn new(bytes: [u8; 32]) -> Self {
171        Hash(bytes)
172    }
173}
174
175impl serde::Serialize for Hash {
176    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
177    where
178        S: serde::Serializer,
179    {
180        serde_bytes::Bytes::new(&self.0).serialize(serializer)
181    }
182}
183
184impl<'de> serde::Deserialize<'de> for Hash {
185    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
186    where
187        D: serde::Deserializer<'de>,
188    {
189        let bytes = serde_bytes::ByteBuf::deserialize(deserializer)?;
190        let array: [u8; 32] = bytes.into_vec().try_into().map_err(|_| serde::de::Error::custom("expected 32 bytes"))?;
191        Ok(Hash(array))
192    }
193}
194
195impl From<[u8; 32]> for Hash {
196    fn from(bytes: [u8; 32]) -> Self {
197        Hash(bytes)
198    }
199}
200
201impl From<Hash> for [u8; 32] {
202    fn from(hash: Hash) -> Self {
203        hash.0
204    }
205}
206
207impl AsRef<[u8; 32]> for Hash {
208    fn as_ref(&self) -> &[u8; 32] {
209        &self.0
210    }
211}
212
213impl AsRef<[u8]> for Hash {
214    fn as_ref(&self) -> &[u8] {
215        &self.0
216    }
217}
218
219impl std::ops::Deref for Hash {
220    type Target = [u8; 32];
221
222    fn deref(&self) -> &Self::Target {
223        &self.0
224    }
225}
226
227impl TryFrom<&[u8]> for Hash {
228    type Error = std::array::TryFromSliceError;
229    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
230        Ok(Hash(<[u8; 32]>::try_from(slice)?))
231    }
232}
233
234impl TryFrom<Vec<u8>> for Hash {
235    type Error = Vec<u8>;
236    fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
237        Ok(Hash(vec.try_into()?))
238    }
239}
240
241impl PartialEq<[u8]> for Hash {
242    fn eq(&self, other: &[u8]) -> bool {
243        &self.0[..] == other
244    }
245}
246
247impl PartialEq<Hash> for [u8] {
248    fn eq(&self, other: &Hash) -> bool {
249        self == &other.0[..]
250    }
251}
252
253impl PartialEq<&[u8]> for Hash {
254    fn eq(&self, other: &&[u8]) -> bool {
255        &self.0[..] == *other
256    }
257}
258
259impl PartialEq<[u8; 32]> for Hash {
260    fn eq(&self, other: &[u8; 32]) -> bool {
261        &self.0 == other
262    }
263}
264
265impl PartialEq<Hash> for [u8; 32] {
266    fn eq(&self, other: &Hash) -> bool {
267        self == &other.0
268    }
269}
270
271impl std::borrow::Borrow<[u8]> for Hash {
272    fn borrow(&self) -> &[u8] {
273        &self.0
274    }
275}
276
277impl std::borrow::Borrow<[u8; 32]> for Hash {
278    fn borrow(&self) -> &[u8; 32] {
279        &self.0
280    }
281}
282
283/// BLS12-381 public key
284#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, bincode::Encode, bincode::Decode)]
285pub struct PublicKey([u8; 48]);
286
287impl PublicKey {
288    pub const fn new(bytes: [u8; 48]) -> Self {
289        PublicKey(bytes)
290    }
291}
292
293impl serde::Serialize for PublicKey {
294    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
295    where
296        S: serde::Serializer,
297    {
298        serde_bytes::Bytes::new(&self.0).serialize(serializer)
299    }
300}
301
302impl<'de> serde::Deserialize<'de> for PublicKey {
303    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
304    where
305        D: serde::Deserializer<'de>,
306    {
307        let bytes = serde_bytes::ByteBuf::deserialize(deserializer)?;
308        let array: [u8; 48] = bytes.into_vec().try_into().map_err(|_| serde::de::Error::custom("expected 48 bytes"))?;
309        Ok(PublicKey(array))
310    }
311}
312
313impl From<[u8; 48]> for PublicKey {
314    fn from(bytes: [u8; 48]) -> Self {
315        PublicKey(bytes)
316    }
317}
318
319impl From<PublicKey> for [u8; 48] {
320    fn from(pk: PublicKey) -> Self {
321        pk.0
322    }
323}
324
325impl AsRef<[u8; 48]> for PublicKey {
326    fn as_ref(&self) -> &[u8; 48] {
327        &self.0
328    }
329}
330
331impl AsRef<[u8]> for PublicKey {
332    fn as_ref(&self) -> &[u8] {
333        &self.0
334    }
335}
336
337impl std::ops::Deref for PublicKey {
338    type Target = [u8; 48];
339
340    fn deref(&self) -> &Self::Target {
341        &self.0
342    }
343}
344
345impl TryFrom<&[u8]> for PublicKey {
346    type Error = std::array::TryFromSliceError;
347    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
348        Ok(PublicKey(<[u8; 48]>::try_from(slice)?))
349    }
350}
351
352impl TryFrom<Vec<u8>> for PublicKey {
353    type Error = Vec<u8>;
354    fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
355        Ok(PublicKey(vec.try_into()?))
356    }
357}
358
359impl PartialEq<[u8]> for PublicKey {
360    fn eq(&self, other: &[u8]) -> bool {
361        &self.0[..] == other
362    }
363}
364
365impl PartialEq<PublicKey> for [u8] {
366    fn eq(&self, other: &PublicKey) -> bool {
367        self == &other.0[..]
368    }
369}
370
371impl PartialEq<&[u8]> for PublicKey {
372    fn eq(&self, other: &&[u8]) -> bool {
373        &self.0[..] == *other
374    }
375}
376
377impl std::borrow::Borrow<[u8]> for PublicKey {
378    fn borrow(&self) -> &[u8] {
379        &self.0
380    }
381}
382
383impl std::borrow::Borrow<[u8; 48]> for PublicKey {
384    fn borrow(&self) -> &[u8; 48] {
385        &self.0
386    }
387}