pub enum ExponentiationAlgorithm {
Binary,
SlidingWindow(u32),
MontgomeryLadder,
FixedWindow(u32),
}Expand description
Exponentiation algorithms for modular exponentiation.
This enum defines different algorithms for computing base^exponent mod modulus. Each algorithm offers different trade-offs between performance, memory usage, and resistance to timing attacks. Choose based on your security requirements and performance constraints.
§Performance Characteristics
- Binary: Simple, good for small exponents, variable-time
- SlidingWindow: Good balance, configurable window size
- MontgomeryLadder: Constant-time, prevents timing attacks
- FixedWindow: Precomputed tables, fastest for repeated operations
§Security Considerations
- Use
MontgomeryLadderfor cryptographic applications requiring constant-time execution to prevent timing-based side-channel attacks BinaryandSlidingWindowmay leak exponent bits through timing
Variants§
Binary
Binary exponentiation using the square-and-multiply algorithm.
This is the simplest exponentiation method that processes each bit of the exponent individually. For each exponent bit:
- If bit is 1: square the result and multiply by base
- If bit is 0: just square the result
Performance: O(log exponent) operations Memory: O(1) additional space Security: Variable-time, may leak exponent through timing Best for: Small exponents, non-cryptographic use
SlidingWindow(u32)
Sliding window exponentiation with configurable window size.
Uses a sliding window approach to reduce the number of multiplications by precomputing powers of the base. The window size parameter controls the trade-off between precomputation time and multiplication count.
Performance: Better than binary for large exponents Memory: O(2^window_size) precomputed values Security: Variable-time, timing depends on exponent bits Best for: General-purpose exponentiation with known window size
MontgomeryLadder
Montgomery ladder exponentiation (constant-time).
A constant-time algorithm that always performs the same sequence of operations regardless of the exponent bits. This prevents timing-based side-channel attacks by ensuring both possible code paths (multiply or square) are always executed.
Performance: ~2x slower than binary exponentiation Memory: O(1) additional space Security: Constant-time, resistant to timing attacks Best for: Cryptographic applications, secure exponentiation
FixedWindow(u32)
Fixed window exponentiation with precomputed power table.
Precomputes all possible base^(odd values in window) and uses a fixed window size for exponent processing. Most efficient for repeated exponentiations with the same base or when precomputation cost can be amortized.
Performance: Fastest for repeated operations Memory: O(2^window_size) precomputed values Security: Variable-time, may leak exponent patterns Best for: Batch exponentiation, same base used repeatedly
Trait Implementations§
Source§impl Clone for ExponentiationAlgorithm
impl Clone for ExponentiationAlgorithm
Source§fn clone(&self) -> ExponentiationAlgorithm
fn clone(&self) -> ExponentiationAlgorithm
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more