ic-auth 0.55.1

Canonical authentication, proof verification and bounded receiver/issuer state with optional stable upgrade checkpoints
Documentation
//! Retained host-owned work allowance; no ambient clock or persistence.

#[cfg(test)]
mod tests;

pub use crate::work_budget::BudgetError;
use crate::work_budget::WorkBucket;
use ic_auth_protocol_types::PortableMechanism;

///
/// VerificationCosts
///
/// Relative verification work costs, chosen from protected host measurements.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(
    feature = "receiver-stable",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct VerificationCosts {
    /// Units consumed by one attempted Ed25519 verification.
    pub ed25519: u32,
    /// Units consumed by one attempted certified verification.
    pub certified: u32,
}

///
/// VerificationBudget
///
/// One receiver-wide allowance shared across callers and proof mechanisms.
/// Starts empty and refills one unit per interval. Keep it across requests;
/// failed cryptography consumes credit. Hosts retain charges on normal refusal
/// and storage failure; an enclosing IC trap can roll back the whole message.
/// This bounds charged work, not ingress traffic or total instructions.
///

#[derive(Debug, Eq, PartialEq)]
pub struct VerificationBudget {
    bucket: WorkBucket,
    costs: VerificationCosts,
}

///
/// RetainedBudget
///
/// Stores verification costs and the work bucket across receiver upgrades.
///

#[cfg(feature = "receiver-stable")]
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(remote = "VerificationBudget")]
pub(crate) struct RetainedBudget {
    bucket: WorkBucket,
    costs: VerificationCosts,
}

impl VerificationBudget {
    #[cfg(feature = "receiver-stable")]
    pub(crate) fn matches(
        &self,
        costs: VerificationCosts,
        capacity: u32,
        interval: u64,
        now: u64,
    ) -> bool {
        self.costs == costs && self.bucket.matches(capacity, interval, now)
    }

    /// Configure positive costs within capacity and an initially empty bucket.
    pub fn new(
        capacity: u32,
        interval_ns: u64,
        costs: VerificationCosts,
        now_ns: u64,
    ) -> Result<Self, BudgetError> {
        if costs.ed25519 == 0
            || costs.certified == 0
            || costs.ed25519 > capacity
            || costs.certified > capacity
        {
            return Err(BudgetError::InvalidPolicy);
        }
        Ok(Self {
            bucket: WorkBucket::new(capacity, interval_ns, now_ns)?,
            costs,
        })
    }

    /// Retained units after the last attempt; this does not observe time/refill.
    #[must_use]
    pub const fn available(&self) -> u32 {
        self.bucket.available()
    }

    /// Most recent attempted-charge time, including exhausted attempts.
    #[must_use]
    pub const fn observed_at_ns(&self) -> u64 {
        self.bucket.observed_at_ns()
    }

    pub(super) fn charge(
        &mut self,
        now_ns: u64,
        mechanism: PortableMechanism,
    ) -> Result<(), BudgetError> {
        let cost = match mechanism {
            PortableMechanism::Ed25519 => self.costs.ed25519,
            PortableMechanism::Certified => self.costs.certified,
        };
        self.bucket.charge(now_ns, cost)
    }
}