Skip to main content

multiversx_bls/
secret_key.rs

1use crate::BlsError;
2use crate::bls_api::*;
3use crate::constants::MCLBN_FR_UNIT_SIZE;
4use crate::g1::G1;
5use crate::g2::G2;
6use crate::init::{INIT, init_library};
7
8/// secret key type
9#[derive(Default, Debug, Clone, Copy, Eq)]
10#[repr(C)]
11pub struct SecretKey {
12    d: [u64; MCLBN_FR_UNIT_SIZE],
13}
14
15impl PartialEq for SecretKey {
16    /// return true if `self` is equal to `rhs`
17    fn eq(&self, rhs: &Self) -> bool {
18        INIT.call_once(init_library);
19        unsafe { blsSecretKeyIsEqual(self, rhs) == 1 }
20    }
21}
22
23impl SecretKey {
24    /// init secret key by CSPRNG
25    pub fn set_by_csprng(&mut self) {
26        INIT.call_once(init_library);
27        unsafe { blsSecretKeySetByCSPRNG(self) }
28        let ret = unsafe { mclBnFr_isZero(self) };
29        if ret == 1 {
30            panic!("zero secretkey")
31        }
32    }
33
34    /// set hexadecimal string `s` to `self`
35    pub fn set_hex_str(&mut self, s: &str) -> bool {
36        INIT.call_once(init_library);
37        unsafe { blsSecretKeySetHexStr(self, s.as_ptr(), s.len()) == 0 }
38    }
39
40    /// return the secret key set by hexadecimal string `s`
41    pub fn from_hex_str(s: &str) -> Result<SecretKey, BlsError> {
42        let mut v = SecretKey::default();
43        if v.set_hex_str(s) {
44            return Ok(v);
45        }
46        Err(BlsError::InvalidData)
47    }
48
49    /// return the public key corresponding to `self`
50    pub fn get_public_key(&self) -> G2 {
51        INIT.call_once(init_library);
52        let mut v = G2::default();
53        unsafe {
54            blsGetPublicKey(&mut v, self);
55        }
56        v
57    }
58
59    /// return the signature of `msg`
60    /// * `msg` - message
61    pub fn sign(&self, msg: &[u8]) -> G1 {
62        INIT.call_once(init_library);
63        let mut v = G1::default();
64        unsafe { blsSign(&mut v, self, msg.as_ptr(), msg.len()) }
65        v
66    }
67
68    /// return true if `buf` is deserialized successfully
69    /// * `buf` - serialized data by `serialize`
70    pub fn deserialize(&mut self, buf: &[u8]) -> bool {
71        INIT.call_once(init_library);
72
73        let n = unsafe { blsSecretKeyDeserialize(self, buf.as_ptr(), buf.len()) };
74        n > 0 && n == buf.len()
75    }
76
77    /// return deserialized `buf`
78    pub fn from_serialized(buf: &[u8]) -> Result<Self, crate::BlsError> {
79        let mut v = Self::default();
80        if v.deserialize(buf) {
81            return Ok(v);
82        }
83
84        Err(crate::BlsError::InvalidData)
85    }
86
87    /// return serialized byte array
88    pub fn serialize(&self) -> Result<Vec<u8>, BlsError> {
89        INIT.call_once(init_library);
90
91        let size = unsafe { mclBn_getFrByteSize() };
92        let mut buf = vec![0u8; size];
93
94        let n = unsafe { blsSecretKeySerialize(buf.as_mut_ptr(), size, self) };
95        if n == 0 {
96            return Err(BlsError::SerializeError);
97        }
98
99        buf.truncate(n);
100
101        Ok(buf)
102    }
103
104    pub fn is_zero(&self) -> bool {
105        INIT.call_once(init_library);
106        unsafe { mclBnFr_isZero(self) == 1 }
107    }
108
109    pub fn is_valid(&self) -> bool {
110        INIT.call_once(init_library);
111        unsafe { mclBnFr_isValid(self) == 1 }
112    }
113}