attrkey 0.1.0-alpha.1

Pure Rust implementation of a Selection-Sensitive Attribute-Based Key Derivation Scheme.
Documentation
use core::fmt::{Display, Formatter, Result};
use std::error::Error;

/// An enum representing all errors for the Selection-Sensitive Attribute-Based
/// Key Derivation Scheme.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ArrkeyError {
    /// Number of attributes is two few, at least two are required.
    AttributesTooFew,

    /// Memory cost is too small for Argon2.
    MemoryTooSmall,

    /// Number of iterations (time cost) is too small for Argon2.
    TimeTooSmall,

    /// Default degree of parallelism is too low for Argon2.
    ThreadsTooFew,

    /// Default degree of parallelism is too high for Argon2.
    ThreadsTooMany,

    /// Proof-of-work constraint is too small for annihilation.
    ConstraintTooSmall,

    /// An Argon2 password hashing failure prevented hardening attributes.
    HardenFail,

    /// Too few attributes selected, at least one attribute must be selected.
    IndicesTooFew,

    /// Too many attributes selected, selection cannot include all attributes.
    IndicesTooMany,

    /// One more more selected attribute indices are out of bounds.
    IndexOutOfBounds,

    /// Computing an annihilation key from selected and remaining attributes
    /// failed.
    Annihilation,
}

impl Display for ArrkeyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.write_str(match self {
            Self::AttributesTooFew => "not enough attributes",
            Self::MemoryTooSmall => "memory cost is too small",
            Self::TimeTooSmall => "time cost is too small",
            Self::ThreadsTooFew => "not enough threads",
            Self::ThreadsTooMany => "too many threads",
            Self::ConstraintTooSmall => "constraint is too small",
            Self::HardenFail => "failed to harden attributes",
            Self::IndicesTooFew => "too few selected attributes",
            Self::IndicesTooMany => "too many selected attributes",
            Self::IndexOutOfBounds => "out of bound selection index",
            Self::Annihilation => "key pair annihilation failed",
        })
    }
}

impl Error for ArrkeyError {}

#[test]
fn test_display() {
    assert_eq!(
        ArrkeyError::AttributesTooFew.to_string(),
        "not enough attributes"
    );

    assert_eq!(
        ArrkeyError::MemoryTooSmall.to_string(),
        "memory cost is too small"
    );

    assert_eq!(
        ArrkeyError::TimeTooSmall.to_string(),
        "time cost is too small"
    );

    assert_eq!(ArrkeyError::ThreadsTooFew.to_string(), "not enough threads");

    assert_eq!(ArrkeyError::ThreadsTooMany.to_string(), "too many threads");

    assert_eq!(
        ArrkeyError::ConstraintTooSmall.to_string(),
        "constraint is too small"
    );

    assert_eq!(
        ArrkeyError::HardenFail.to_string(),
        "failed to harden attributes"
    );

    assert_eq!(
        ArrkeyError::IndicesTooFew.to_string(),
        "too few selected attributes"
    );

    assert_eq!(
        ArrkeyError::IndicesTooMany.to_string(),
        "too many selected attributes"
    );

    assert_eq!(
        ArrkeyError::IndexOutOfBounds.to_string(),
        "out of bound selection index"
    );

    assert_eq!(
        ArrkeyError::Annihilation.to_string(),
        "key pair annihilation failed"
    );
}