mithril_stm/bls_multi_signature/
signing_key.rs1use blst::min_sig::SecretKey as BlstSk;
2use rand_core::{CryptoRng, RngCore};
3
4use crate::bls_multi_signature::signature::BlsSignature;
5use crate::error::{blst_err_to_mithril, MultiSignatureError};
6
7#[derive(Debug, Clone)]
10pub struct BlsSigningKey(pub BlstSk);
11
12impl BlsSigningKey {
13 pub fn generate(rng: &mut (impl RngCore + CryptoRng)) -> Self {
15 let mut ikm = [0u8; 32];
16 rng.fill_bytes(&mut ikm);
17 BlsSigningKey(
18 BlstSk::key_gen(&ikm, &[])
19 .expect("Error occurs when the length of ikm < 32. This will not happen here."),
20 )
21 }
22
23 pub fn sign(&self, msg: &[u8]) -> BlsSignature {
25 BlsSignature(self.0.sign(msg, &[], &[]))
26 }
27
28 pub fn to_bytes(&self) -> [u8; 32] {
30 self.0.to_bytes()
31 }
32
33 pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> {
38 let bytes = bytes
39 .get(..32)
40 .ok_or(MultiSignatureError::SerializationError)?;
41 match BlstSk::from_bytes(bytes) {
42 Ok(sk) => Ok(Self(sk)),
43 Err(e) => Err(blst_err_to_mithril(e, None, None)
44 .expect_err("If deserialization is not successful, blst returns and error different to SUCCESS."))
45 }
46 }
47
48 pub(crate) fn to_blst_sk(&self) -> BlstSk {
49 self.0.clone()
50 }
51}