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
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Formatter};
use threshold_crypto as bls;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
pub struct Signed {
pub public_key: bls::PublicKey,
pub signature: bls::Signature,
}
impl Signed {
pub fn verify(&self, payload: &[u8]) -> bool {
self.public_key.verify(&self.signature, payload)
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SignedShare {
pub public_key_set: bls::PublicKeySet,
pub index: usize,
pub signature_share: bls::SignatureShare,
}
impl SignedShare {
pub fn new(
public_key_set: bls::PublicKeySet,
index: usize,
secret_key_share: &bls::SecretKeyShare,
payload: &[u8],
) -> Self {
Self {
public_key_set,
index,
signature_share: secret_key_share.sign(payload),
}
}
pub fn verify(&self, payload: &[u8]) -> bool {
self.public_key_set
.public_key_share(self.index)
.verify(&self.signature_share, payload)
}
}
impl Debug for SignedShare {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(
formatter,
"SignedShare {{ public_key: {:?}, index: {}, .. }}",
self.public_key_set.public_key(),
self.index
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use threshold_crypto::SecretKey;
#[test]
fn verify_signed() {
let sk = SecretKey::random();
let public_key = sk.public_key();
let data = "hello".to_string();
let signature = sk.sign(&data);
let signed = Signed {
public_key,
signature,
};
assert!(signed.verify(&data.as_bytes()));
}
}