1use anyhow::{Context, Ok};
2use ark_bls12_381::{Fr, G1Projective};
3use ark_ec::{CurveGroup, PrimeGroup};
4use ark_ff::PrimeField;
5use hkdf::Hkdf;
6use sha2::Sha256;
7use std::path::PathBuf;
8
9use crate::{
10 core::PublicKey,
11 util::{read_sk, write_sk},
12};
13
14pub fn keygen(ikm: &[u8], path: &PathBuf) -> anyhow::Result<()> {
15 let salt = b"BLS-SIG-KEYGEN-SALT-";
16 let hk = Hkdf::<Sha256>::new(Some(salt), ikm);
17
18 const L: usize = 48;
19
20 let mut okm = [0u8; L];
21 hk.expand(&[0x00], &mut okm).expect("HDKF failed!");
22
23 let mut bytes = [0u8; 32];
24 bytes.copy_from_slice(&okm[L - 32..]);
25
26 let sk = Fr::from_be_bytes_mod_order(&bytes);
27
28 write_sk(sk, path)
29}
30
31pub fn sk_to_pk(path: &PathBuf) -> anyhow::Result<PublicKey> {
32 let sk = read_sk(path).with_context(|| "Failed to get secret key from file.")?;
33 let pk_point = G1Projective::generator() * sk;
34 let pk = pk_point.into_affine();
35 Ok(pk)
36}