mithril_stm/bls_multi_signature/
verification_key.rs1use std::{
2 cmp::Ordering,
3 fmt::{Display, Formatter},
4 hash::{Hash, Hasher},
5 iter::Sum,
6};
7
8use blst::{
9 min_sig::{AggregatePublicKey, PublicKey as BlstVk},
10 BLST_ERROR,
11};
12use serde::{Deserialize, Serialize};
13
14use crate::bls_multi_signature::{
15 helper::unsafe_helpers::verify_pairing, ProofOfPossession, SigningKey, POP,
16};
17use crate::error::{blst_err_to_mithril, MultiSignatureError};
18
19#[derive(Debug, Clone, Copy, Default)]
22pub struct VerificationKey(pub BlstVk);
23
24impl VerificationKey {
25 pub fn to_bytes(self) -> [u8; 96] {
27 self.0.to_bytes()
28 }
29
30 pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> {
36 let bytes = bytes
37 .get(..96)
38 .ok_or(MultiSignatureError::SerializationError)?;
39 match BlstVk::key_validate(bytes) {
40 Ok(vk) => Ok(Self(vk)),
41 Err(e) => Err(blst_err_to_mithril(e, None, None)
42 .expect_err("If deserialization is not successful, blst returns and error different to SUCCESS."))
43 }
44 }
45
46 fn cmp_msp_mvk(&self, other: &VerificationKey) -> Ordering {
49 let self_bytes = self.to_bytes();
50 let other_bytes = other.to_bytes();
51 let mut result = Ordering::Equal;
52
53 for (i, j) in self_bytes.iter().zip(other_bytes.iter()) {
54 result = i.cmp(j);
55 if result != Ordering::Equal {
56 return result;
57 }
58 }
59
60 result
61 }
62
63 pub(crate) fn to_blst_vk(self) -> BlstVk {
64 self.0
65 }
66}
67
68impl Display for VerificationKey {
69 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70 write!(f, "{:?}", self.to_bytes())
71 }
72}
73
74impl Hash for VerificationKey {
75 fn hash<H: Hasher>(&self, state: &mut H) {
76 Hash::hash_slice(&self.to_bytes(), state)
77 }
78}
79
80impl PartialEq for VerificationKey {
81 fn eq(&self, other: &Self) -> bool {
82 self.0 == other.0
83 }
84}
85
86impl Eq for VerificationKey {}
87
88impl PartialOrd for VerificationKey {
89 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
90 Some(std::cmp::Ord::cmp(self, other))
91 }
92}
93
94impl Ord for VerificationKey {
95 fn cmp(&self, other: &Self) -> Ordering {
96 self.cmp_msp_mvk(other)
97 }
98}
99
100impl<'a> Sum<&'a Self> for VerificationKey {
101 fn sum<I>(iter: I) -> Self
102 where
103 I: Iterator<Item = &'a Self>,
104 {
105 let keys: Vec<&BlstVk> = iter.map(|x| &x.0).collect();
106
107 assert!(!keys.is_empty(), "One cannot add an empty vector");
108 let aggregate_key = AggregatePublicKey::aggregate(&keys, false)
109 .expect("An MspMvk is always a valid key. This function only fails if keys is empty or if the keys are invalid, none of which can happen.")
110 .to_public_key();
111
112 Self(aggregate_key)
113 }
114}
115
116impl From<&SigningKey> for VerificationKey {
117 fn from(sk: &SigningKey) -> Self {
121 VerificationKey(sk.to_blst_sk().sk_to_pk())
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
127pub struct VerificationKeyPoP {
128 pub vk: VerificationKey,
130 pub pop: ProofOfPossession,
132}
133
134impl VerificationKeyPoP {
135 pub fn check(&self) -> Result<(), MultiSignatureError> {
142 match self.vk.to_blst_vk().validate() {
143 Ok(_) => {
144 let result = verify_pairing(&self.vk, &self.pop);
145 if !(self
146 .pop
147 .to_k1()
148 .verify(false, POP, &[], &[], &self.vk.to_blst_vk(), false)
149 == BLST_ERROR::BLST_SUCCESS
150 && result)
151 {
152 return Err(MultiSignatureError::KeyInvalid(Box::new(*self)));
153 }
154 Ok(())
155 }
156 Err(e) => blst_err_to_mithril(e, None, Some(self.vk)),
157 }
158 }
159
160 pub fn to_bytes(self) -> [u8; 192] {
167 let mut vkpop_bytes = [0u8; 192];
168 vkpop_bytes[..96].copy_from_slice(&self.vk.to_bytes());
169 vkpop_bytes[96..].copy_from_slice(&self.pop.to_bytes());
170 vkpop_bytes
171 }
172
173 pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> {
175 let mvk = VerificationKey::from_bytes(
176 bytes
177 .get(..96)
178 .ok_or(MultiSignatureError::SerializationError)?,
179 )?;
180
181 let pop = ProofOfPossession::from_bytes(
182 bytes
183 .get(96..)
184 .ok_or(MultiSignatureError::SerializationError)?,
185 )?;
186
187 Ok(Self { vk: mvk, pop })
188 }
189}
190
191impl From<&SigningKey> for VerificationKeyPoP {
192 fn from(sk: &SigningKey) -> Self {
195 Self {
196 vk: sk.into(),
197 pop: sk.into(),
198 }
199 }
200}