use blst::blst_scalar;
use crate::{
error::{Error, Result},
Fr, G1Affine, G2Affine, DST, PK_SIZE, SIG_SIZE, SK_SIZE,
};
pub(crate) fn derivation_index_into_fr(index: &[u8]) -> Fr {
hash_to_field(index).unwrap_or_else(|_| {
derivation_index_into_fr(&[0u8; 32])
})
}
fn hash_to_field<T: AsRef<[u8]>>(msg: T) -> Result<Fr> {
let scalar = blst_scalar::hash_to(msg.as_ref(), DST).ok_or(Error::HashToFieldIsZero)?;
let fr = scalar.try_into();
fr.map_err(|_| Error::HashToFieldIsZero)
}
pub(crate) fn fr_from_bytes(bytes: [u8; SK_SIZE]) -> Result<Fr> {
let fr = Fr::from_bytes_be(&bytes);
if fr.is_none().into() {
return Err(Error::InvalidBytes);
};
Ok(fr.unwrap())
}
pub(crate) fn g1_from_bytes(bytes: [u8; PK_SIZE]) -> Result<G1Affine> {
let g1 = G1Affine::from_compressed(&bytes);
if g1.is_none().into() {
return Err(Error::InvalidBytes);
};
Ok(g1.unwrap())
}
pub(crate) fn g2_from_bytes(bytes: [u8; SIG_SIZE]) -> Result<G2Affine> {
let g2 = G2Affine::from_compressed(&bytes);
if g2.is_none().into() {
return Err(Error::InvalidBytes);
};
Ok(g2.unwrap())
}