mithril_stm/bls_multi_signature/
proof_of_possession.rs1use blst::{blst_p1, min_sig::Signature as BlstSig};
2
3use crate::bls_multi_signature::{
4 BlsSigningKey, POP,
5 helper::unsafe_helpers::{compress_p1, scalar_to_pk_in_g1, uncompress_p1},
6};
7use crate::error::{MultiSignatureError, blst_err_to_mithril};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct BlsProofOfPossession {
16 k1: BlstSig,
17 k2: blst_p1,
18}
19
20impl BlsProofOfPossession {
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.get(..48).ok_or(MultiSignatureError::SerializationError)?,
39 ) {
40 Ok(key) => key,
41 Err(e) => {
42 return Err(blst_err_to_mithril(e, None, None)
43 .expect_err("If it passed, blst returns and error different to SUCCESS."));
44 }
45 };
46
47 let k2 = uncompress_p1(bytes.get(48..96).ok_or(MultiSignatureError::SerializationError)?)?;
48
49 Ok(Self { k1, k2 })
50 }
51
52 pub(crate) fn get_k1(self) -> BlstSig {
53 self.k1
54 }
55
56 pub(crate) fn get_k2(self) -> blst_p1 {
57 self.k2
58 }
59}
60
61impl From<&BlsSigningKey> for BlsProofOfPossession {
62 fn from(sk: &BlsSigningKey) -> Self {
66 let k1 = sk.to_blst_secret_key().sign(POP, &[], &[]);
67 let k2 = scalar_to_pk_in_g1(&sk.to_blst_secret_key());
68 Self { k1, k2 }
69 }
70}