1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#[cfg(feature = "serde")]
macro_rules! serde_boilerplate { ($t:ty) => {
impl ::serde::Serialize for $t {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
serializer.serialize_bytes(&self.to_bytes()[..])
}
}
impl<'d> ::serde::Deserialize<'d> for $t {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: ::serde::Deserializer<'d> {
struct MyVisitor;
impl<'d> ::serde::de::Visitor<'d> for MyVisitor {
type Value = $t;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
formatter.write_str(Self::Value::DESCRIPTION)
}
fn visit_bytes<E>(self, bytes: &[u8]) -> Result<$t, E> where E: ::serde::de::Error {
Self::Value::from_bytes(bytes).map_err(crate::errors::serde_error_from_signature_error)
}
}
deserializer.deserialize_bytes(MyVisitor)
}
}
} }
#[cfg(not(feature = "serde"))]
macro_rules! serde_boilerplate { ($t:ty) => { } }
#[cfg(all(test, feature = "serde"))]
mod test {
use std::vec::Vec;
use bincode::{serialize, serialized_size, deserialize};
use curve25519_dalek::ristretto::{CompressedRistretto};
use crate::*;
static COMPRESSED_PUBLIC_KEY : CompressedRistretto = CompressedRistretto([
208, 120, 140, 129, 177, 179, 237, 159,
252, 160, 028, 013, 206, 005, 211, 241,
192, 218, 001, 097, 130, 241, 020, 169,
119, 046, 246, 029, 079, 080, 077, 084]);
static ED25519_SECRET_KEY: MiniSecretKey = MiniSecretKey([
062, 070, 027, 163, 092, 182, 011, 003,
077, 234, 098, 004, 011, 127, 079, 228,
243, 187, 150, 073, 201, 137, 076, 022,
085, 251, 152, 002, 241, 042, 072, 054, ]);
static SIGNATURE_BYTES: [u8; SIGNATURE_LENGTH] = [
010, 126, 151, 143, 157, 064, 047, 001,
196, 140, 179, 058, 226, 152, 018, 102,
160, 123, 080, 016, 210, 086, 196, 028,
053, 231, 012, 157, 169, 019, 158, 063,
045, 154, 238, 007, 053, 185, 227, 229,
079, 108, 213, 080, 124, 252, 084, 167,
216, 085, 134, 144, 129, 149, 041, 081,
063, 120, 126, 100, 092, 059, 050, 138, ];
#[test]
fn serialize_deserialize_signature() {
let signature: Signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();
let encoded_signature: Vec<u8> = serialize(&signature).unwrap();
let decoded_signature: Signature = deserialize(&encoded_signature).unwrap();
assert_eq!(signature, decoded_signature);
}
#[test]
fn serialize_deserialize_public_key() {
let public_key = PublicKey::from_compressed(COMPRESSED_PUBLIC_KEY).unwrap();
let encoded_public_key: Vec<u8> = serialize(&public_key).unwrap();
let decoded_public_key: PublicKey = deserialize(&encoded_public_key).unwrap();
assert_eq!(public_key, decoded_public_key);
}
#[test]
fn serialize_deserialize_mini_secret_key() {
let encoded_secret_key: Vec<u8> = serialize(&ED25519_SECRET_KEY).unwrap();
let decoded_secret_key: MiniSecretKey = deserialize(&encoded_secret_key).unwrap();
for i in 0..32 {
assert_eq!(ED25519_SECRET_KEY.0[i], decoded_secret_key.0[i]);
}
}
#[test]
fn serialize_public_key_size() {
let public_key = PublicKey::from_compressed(COMPRESSED_PUBLIC_KEY).unwrap();
assert_eq!(serialized_size(&public_key).unwrap(), 32+8);
}
#[test]
fn serialize_signature_size() {
let signature: Signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();
assert_eq!(serialized_size(&signature).unwrap(), 64+8);
}
#[test]
fn serialize_secret_key_size() {
assert_eq!(serialized_size(&ED25519_SECRET_KEY).unwrap(), 32+8);
let secret_key = ED25519_SECRET_KEY.expand(ExpansionMode::Ed25519);
assert_eq!(serialized_size(&secret_key).unwrap(), 64+8);
let secret_key = ED25519_SECRET_KEY.expand(ExpansionMode::Uniform);
assert_eq!(serialized_size(&secret_key).unwrap(), 64+8);
}
}