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
use hex_fmt::HexFmt;
use serde::{Deserialize, Serialize};
use std::{fmt, hash::Hash};
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct SignatureShare {
pub index: usize,
pub share: bls::SignatureShare,
}
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, custom_debug::Debug)]
#[allow(clippy::large_enum_variant)]
pub enum Signature {
Ed25519(
#[debug(with = "Self::fmt_ed25519")]
#[serde(with = "serde_bytes")]
ed25519_dalek::Signature,
),
Bls(bls::Signature),
BlsShare(SignatureShare),
}
impl Signature {
pub fn into_bls(self) -> Option<bls::Signature> {
match self {
Self::Bls(sig) => Some(sig),
_ => None,
}
}
pub fn into_ed(self) -> Option<ed25519_dalek::Signature> {
match self {
Self::Ed25519(sig) => Some(sig),
_ => None,
}
}
pub(crate) fn fmt_ed25519(
sig: &ed25519_dalek::Signature,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
write!(f, "Signature({:0.10})", HexFmt(sig))
}
}
impl From<bls::Signature> for Signature {
fn from(sig: bls::Signature) -> Self {
Self::Bls(sig)
}
}
impl From<ed25519_dalek::Signature> for Signature {
fn from(sig: ed25519_dalek::Signature) -> Self {
Self::Ed25519(sig)
}
}
impl From<SignatureShare> for Signature {
fn from(sig: SignatureShare) -> Self {
Self::BlsShare(sig)
}
}
impl From<(usize, bls::SignatureShare)> for Signature {
fn from(sig: (usize, bls::SignatureShare)) -> Self {
let (index, share) = sig;
Self::BlsShare(SignatureShare { index, share })
}
}
#[cfg(test)]
mod tests {
use super::Signature;
#[test]
fn ed25519_rmp_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
let signature = gen_ed25519_sig();
let serialized = rmp_serde::to_vec(&signature)?;
assert_eq!(
rmp_serde::from_read::<_, Signature>(&serialized[..])?,
signature
);
Ok(())
}
#[test]
fn ed25519_rmp_fmt() -> Result<(), Box<dyn std::error::Error>> {
let signature = gen_ed25519_sig();
let serialized = rmp_serde::to_vec(&signature)?;
assert_eq!(serialized.len(), 68);
assert_eq!(serialized[0], 0x81);
assert_eq!(serialized[1], 0x00);
assert_eq!(serialized[2], 0xc4);
assert_eq!(serialized[3], 64);
assert_eq!(&serialized[4..], signature.into_ed().unwrap().as_ref());
Ok(())
}
fn gen_ed25519_sig() -> Signature {
let random_bytes: Vec<u8> = (0..ed25519::Signature::BYTE_SIZE)
.map(|_| rand::random::<u8>())
.collect();
let mut result = ed25519::Signature::from_bytes(&random_bytes);
while result.is_err() {
let random_bytes: Vec<u8> = (0..ed25519::Signature::BYTE_SIZE)
.map(|_| rand::random::<u8>())
.collect();
result = ed25519::Signature::from_bytes(&random_bytes);
}
Signature::Ed25519(result.unwrap())
}
}