mithril_stm/bls_multi_signature/
proof_of_possession.rs1use blst::{blst_p1, min_sig::Signature as BlstSig};
2
3use crate::bls_multi_signature::{
4 helper::unsafe_helpers::{compress_p1, scalar_to_pk_in_g1, uncompress_p1},
5 SigningKey, POP,
6};
7use crate::error::{blst_err_to_mithril, MultiSignatureError};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct ProofOfPossession {
16 k1: BlstSig,
17 k2: blst_p1,
18}
19
20impl ProofOfPossession {
21 pub fn to_bytes(self) -> [u8; 96] {
28 let mut pop_bytes = [0u8; 96];
29 pop_bytes[..48].copy_from_slice(&self.k1.to_bytes());
30
31 pop_bytes[48..].copy_from_slice(&compress_p1(&self.k2)[..]);
32 pop_bytes
33 }
34
35 pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> {
37 let k1 = match BlstSig::from_bytes(
38 bytes
39 .get(..48)
40 .ok_or(MultiSignatureError::SerializationError)?,
41 ) {
42 Ok(key) => key,
43 Err(e) => {
44 return Err(blst_err_to_mithril(e, None, None)
45 .expect_err("If it passed, blst returns and error different to SUCCESS."))
46 }
47 };
48
49 let k2 = uncompress_p1(
50 bytes
51 .get(48..96)
52 .ok_or(MultiSignatureError::SerializationError)?,
53 )?;
54
55 Ok(Self { k1, k2 })
56 }
57
58 pub(crate) fn to_k1(self) -> BlstSig {
59 self.k1
60 }
61
62 pub(crate) fn to_k2(self) -> blst_p1 {
63 self.k2
64 }
65}
66
67impl From<&SigningKey> for ProofOfPossession {
68 fn from(sk: &SigningKey) -> Self {
72 let k1 = sk.to_blst_sk().sign(POP, &[], &[]);
73 let k2 = scalar_to_pk_in_g1(&sk.to_blst_sk());
74 Self { k1, k2 }
75 }
76}