ExponentiationAlgorithm

Enum ExponentiationAlgorithm 

Source
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 MontgomeryLadder for cryptographic applications requiring constant-time execution to prevent timing-based side-channel attacks
  • Binary and SlidingWindow may 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

Source§

fn clone(&self) -> ExponentiationAlgorithm

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ExponentiationAlgorithm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ExponentiationAlgorithm

Source§

fn eq(&self, other: &ExponentiationAlgorithm) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for ExponentiationAlgorithm

Source§

impl Eq for ExponentiationAlgorithm

Source§

impl StructuralPartialEq for ExponentiationAlgorithm

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.