sn_interface/messaging/system/
section_sig.rs1use crate::messaging::{
10 signature_aggregator::{AggregatorError, SignatureAggregator},
11 AuthorityProof,
12};
13use serde::{Deserialize, Serialize};
14use std::{
15 borrow::Borrow,
16 fmt::{self, Debug, Formatter},
17 ops::Deref,
18};
19use xor_name::Prefix;
20
21#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
23pub struct SectionSig {
24 pub public_key: bls::PublicKey,
26 pub signature: bls::Signature,
28}
29
30impl Debug for SectionSig {
31 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
32 f.debug_tuple("SectionSig").field(&self.public_key).finish()
33 }
34}
35
36impl SectionSig {
37 pub fn verify(&self, payload: &[u8]) -> bool {
39 self.public_key.verify(&self.signature, payload)
40 }
41
42 pub fn try_authorize(
44 aggregator: &mut SignatureAggregator,
45 share: SectionSigShare,
46 payload: impl AsRef<[u8]>,
47 ) -> Result<Option<AuthorityProof<Self>>, AggregatorError> {
48 match aggregator.try_aggregate(payload.as_ref(), share)? {
49 Some(sig) => Ok(Some(AuthorityProof(Self {
50 public_key: sig.public_key,
51 signature: sig.signature,
52 }))),
53 None => Ok(None),
54 }
55 }
56}
57
58#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
60pub struct SectionSigShare {
61 pub public_key_set: bls::PublicKeySet,
63 pub index: usize,
65 pub signature_share: bls::SignatureShare,
67}
68
69impl SectionSigShare {
70 pub fn new(
72 public_key_set: bls::PublicKeySet,
73 index: usize,
74 secret_key_share: &bls::SecretKeyShare,
75 payload: &[u8],
76 ) -> Self {
77 Self {
78 public_key_set,
79 index,
80 signature_share: secret_key_share.sign(payload),
81 }
82 }
83
84 pub fn verify(&self, payload: &[u8]) -> bool {
86 self.public_key_set
87 .public_key_share(self.index)
88 .verify(&self.signature_share, payload)
89 }
90}
91
92#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
94pub struct SectionSigned<T: Serialize> {
95 pub value: T,
97 pub sig: SectionSig,
99}
100
101impl<T> Borrow<Prefix> for SectionSigned<T>
102where
103 T: Borrow<Prefix> + Serialize,
104{
105 fn borrow(&self) -> &Prefix {
106 self.value.borrow()
107 }
108}
109
110impl<T: Serialize> Deref for SectionSigned<T> {
111 type Target = T;
112
113 fn deref(&self) -> &Self::Target {
114 &self.value
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use crate::test_utils::TestKeys;
121 use bls::SecretKey;
122
123 #[test]
124 fn verify_keyed_sig() {
125 let sk = SecretKey::random();
126 let data = "hello";
127 let sig = TestKeys::get_section_sig_bytes(&sk, data.as_bytes());
128 assert!(sig.verify(data.as_bytes()));
129 }
130}