clock_curve_math/extensible/
extensions.rs

1//! Future extension points for cryptographic operations.
2//!
3//! This module defines hooks for future cryptographic primitives that may
4//! require different arithmetic patterns or optimizations.
5
6#[cfg(feature = "alloc")]
7extern crate alloc;
8use super::field_parameters::FieldParameters;
9use crate::error::MathError;
10#[cfg(feature = "alloc")]
11use alloc::vec::Vec;
12
13/// Future extension points for cryptographic operations.
14///
15/// This trait defines hooks for future cryptographic primitives that may
16/// require different arithmetic patterns or optimizations. It provides a
17/// framework for extending the library with new cryptographic algorithms
18/// that operate on finite fields.
19///
20/// # Type Parameters
21/// - `Params`: The field parameters defining the mathematical field
22///
23/// # Examples
24/// Implementations can provide specialized cryptographic operations like
25/// pairing computations, zero-knowledge proof systems, or advanced
26/// elliptic curve operations that require custom field arithmetic.
27#[cfg(feature = "alloc")]
28pub trait CryptoExtension {
29    /// The field parameters type for this extension.
30    ///
31    /// This associated type specifies the mathematical field that the
32    /// extension operates on, including the modulus and Montgomery constants.
33    type Params: FieldParameters;
34
35    /// Performs a custom cryptographic operation on the input data.
36    ///
37    /// This method allows extensions to implement arbitrary cryptographic
38    /// primitives that may require specialized field arithmetic or
39    /// multi-step computations.
40    ///
41    /// # Arguments
42    /// * `input` - The input data for the cryptographic operation
43    ///
44    /// # Returns
45    /// The result of the cryptographic operation as a byte vector
46    fn custom_operation(&self, input: &[u8]) -> Result<Vec<u8>, MathError>;
47
48    /// Checks if this cryptographic extension is available and functional.
49    ///
50    /// Extensions may not be available due to missing hardware support,
51    /// insufficient memory, or other runtime constraints.
52    ///
53    /// # Returns
54    /// `true` if the extension is available for use, `false` otherwise
55    ///
56    /// # Note
57    /// The default implementation always returns `true`, assuming the
58    /// extension is always available. Override this method if availability
59    /// depends on runtime conditions.
60    fn is_available(&self) -> bool {
61        true
62    }
63
64    /// Returns the human-readable name of this cryptographic extension.
65    ///
66    /// This name can be used for logging, debugging, or user interface
67    /// purposes to identify which extension is being used.
68    ///
69    /// # Returns
70    /// A static string slice containing the extension name
71    fn name(&self) -> &'static str;
72}
73
74/// Placeholder implementation for future cryptographic extensions.
75///
76/// This struct serves as a stub for cryptographic extensions that have not
77/// yet been implemented. It provides the basic structure and interface that
78/// future extensions will follow, allowing the API to be designed ahead of
79/// full implementation.
80///
81/// # Type Parameters
82/// - `P`: The field parameters defining the mathematical field for operations
83///
84/// # Note
85/// This is a placeholder that returns `NotImplemented` errors for all operations.
86/// Real extensions should implement meaningful cryptographic algorithms.
87#[cfg(feature = "alloc")]
88#[derive(Clone, Debug)]
89pub struct FutureExtension<P: FieldParameters> {
90    /// Phantom data to tie the extension to its field parameters
91    _params: core::marker::PhantomData<P>,
92}
93
94#[cfg(feature = "alloc")]
95impl<P: FieldParameters> FutureExtension<P> {
96    /// Creates a new placeholder cryptographic extension.
97    ///
98    /// This constructor initializes a stub extension that can be used as
99    /// a placeholder until the actual cryptographic implementation is ready.
100    ///
101    /// # Returns
102    /// A new `FutureExtension` instance configured for the specified field parameters
103    pub fn new() -> Self {
104        Self {
105            _params: core::marker::PhantomData,
106        }
107    }
108}
109
110#[cfg(feature = "alloc")]
111impl<P: FieldParameters> CryptoExtension for FutureExtension<P> {
112    type Params = P;
113
114    /// Placeholder implementation that always returns NotImplemented.
115    ///
116    /// This method serves as a stub for future cryptographic operations.
117    /// Real implementations should perform meaningful cryptographic computations
118    /// on the input data using field arithmetic.
119    ///
120    /// # Arguments
121    /// * `_input` - Ignored input data (placeholder parameter)
122    ///
123    /// # Returns
124    /// Always returns `Err(MathError::NotImplemented)` as this is a placeholder
125    fn custom_operation(&self, _input: &[u8]) -> Result<Vec<u8>, MathError> {
126        // Placeholder implementation
127        Err(MathError::NotImplemented)
128    }
129
130    /// Returns the name identifier for this extension.
131    ///
132    /// # Returns
133    /// The static string "FutureExtension" identifying this placeholder implementation
134    fn name(&self) -> &'static str {
135        "FutureExtension"
136    }
137}
138
139#[cfg(feature = "alloc")]
140impl<P: FieldParameters> Default for FutureExtension<P> {
141    /// Creates a default placeholder cryptographic extension.
142    ///
143    /// This implementation allows `FutureExtension` to be used with `Default::default()`,
144    /// providing a convenient way to create placeholder extensions.
145    ///
146    /// # Returns
147    /// A new `FutureExtension` instance equivalent to calling `FutureExtension::new()`
148    fn default() -> Self {
149        Self::new()
150    }
151}