Skip to main content

ed25519_simd/
cache.rs

1use crate::edwards::PointTable;
2use crate::input::{PUBLIC_KEY_LEN, VerifyInput};
3use crate::verifier::{VerificationPolicy, Verifier};
4use crate::wide::avx512ifma;
5
6pub(crate) mod private {
7    /// Prevents downstream crates from implementing sealed cache policies.
8    pub trait Sealed {}
9}
10
11/// A decoded public key and its precomputed multiplication table.
12///
13/// A later signature from the same key can start its scalar
14/// ladder with these cached multiples instead of rebuilding them.
15#[derive(Clone, Debug)]
16pub struct CachedPublicKey {
17    pub(crate) encoded: [u8; PUBLIC_KEY_LEN],
18    pub(crate) table: PointTable,
19}
20
21impl CachedPublicKey {
22    /// Build a cached public key from its encoded bytes.
23    pub fn from_encoded(encoded: [u8; PUBLIC_KEY_LEN]) -> Option<Self> {
24        avx512ifma::decode_public_key_table(&encoded).map(|table| Self { encoded, table })
25    }
26}
27
28/// Storage policy for verifier-decoded public keys.
29///
30/// [`NullKeyCache`] retains nothing; [`HotKeyCache`](crate::HotKeyCache)
31/// retains repeated keys across batches. This trait is sealed; downstream
32/// crates can select a provided policy but cannot implement their own.
33pub trait KeyCache: private::Sealed {
34    /// Policy/cache-specific reusable verification state.
35    #[doc(hidden)]
36    type Queues<P: VerificationPolicy>: core::fmt::Debug + Default;
37
38    /// Borrow a cached key, or `None` if it is absent. Implementations may
39    /// update recency state through interior mutability.
40    fn get(&self, encoded: &[u8; PUBLIC_KEY_LEN]) -> Option<&CachedPublicKey>;
41
42    /// Optionally retain an already-decoded key for later chunks or batches.
43    /// The default implementation leaves the cache unchanged.
44    fn insert(&mut self, _key: CachedPublicKey) {}
45
46    /// Dispatch into a cache-capability-specific batch driver.
47    #[doc(hidden)]
48    fn dispatch_verify_batch<P: VerificationPolicy>(
49        verifier: &mut Verifier<P, Self>,
50        inputs: &[VerifyInput<'_>],
51        out: &mut [bool],
52    ) where
53        Self: Sized;
54}
55
56/// Zero-sized verification state for a cache that cannot hit.
57#[doc(hidden)]
58#[derive(Debug, Default)]
59pub struct NoQueues;
60
61/// A [`KeyCache`] that retains no decoded keys.
62#[derive(Clone, Copy, Debug, Default)]
63pub struct NullKeyCache;
64
65impl NullKeyCache {
66    /// Create the no-op cache.
67    pub fn new() -> Self {
68        Self
69    }
70}
71
72impl private::Sealed for NullKeyCache {}
73
74impl KeyCache for NullKeyCache {
75    type Queues<P: VerificationPolicy> = NoQueues;
76
77    #[inline]
78    fn get(&self, _encoded: &[u8; PUBLIC_KEY_LEN]) -> Option<&CachedPublicKey> {
79        None
80    }
81
82    #[inline]
83    fn dispatch_verify_batch<P: VerificationPolicy>(
84        verifier: &mut Verifier<P, Self>,
85        inputs: &[VerifyInput<'_>],
86        out: &mut [bool],
87    ) {
88        P::dispatch_uncached_verify_batch(verifier, inputs, out);
89    }
90}