use crate::multi_sig::PublicKey;
use blst::BLST_ERROR;
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
pub enum AtmsError {
#[error("Cannot register existing key.")]
RegisterExistingKey(PublicKey),
#[error("Proof of Merkle Tree membership is invalid.")]
InvalidMerkleProof,
#[error("Invalid key provided in the set of keys.")]
InvalidKey,
#[error("Key with invalid PoP provided in the set of keys.")]
InvalidPoP,
#[error("Trying to aggregate key from non-registered participant")]
NonRegisteredParticipant,
#[error("Submitted keys of non-signers contains duplicates.")]
FoundDuplicates(PublicKey),
#[error("Signatures do not exceed the required threshold {0}.")]
TooMuchOutstandingSigners(usize),
#[error("Invalid Signature.")]
InvalidSignature,
#[error("Given public key does not correspond to the aggregation of the set of public keys.")]
InvalidAggregation,
#[error("Invalid signature")]
SerializationError,
#[error("Unexpected point.")]
UnexpectedBlstTypes,
}
pub(crate) fn blst_err_to_atms(e: BLST_ERROR) -> Result<(), AtmsError> {
match e {
BLST_ERROR::BLST_SUCCESS => Ok(()),
BLST_ERROR::BLST_VERIFY_FAIL => Err(AtmsError::InvalidSignature),
BLST_ERROR::BLST_AGGR_TYPE_MISMATCH => Err(AtmsError::UnexpectedBlstTypes),
BLST_ERROR::BLST_PK_IS_INFINITY => Err(AtmsError::UnexpectedBlstTypes),
_ => Err(AtmsError::SerializationError),
}
}
#[derive(Debug, thiserror::Error)]
pub enum MerkleTreeError {
#[error("Invalid merkle path")]
InvalidPath,
#[error("Invalid sized bytes.")]
InvalidSizedBytes,
#[error("Index out of bounds")]
IndexOutOfBounds,
}
impl From<MerkleTreeError> for AtmsError {
fn from(e: MerkleTreeError) -> Self {
match e {
MerkleTreeError::InvalidPath => AtmsError::InvalidMerkleProof,
_ => AtmsError::SerializationError,
}
}
}