Skip to main content

BigIntError

Enum BigIntError 

Source
#[non_exhaustive]
pub enum BigIntError { Overflow, InvalidEncoding, DivisionByZero, InvalidModulus, InvalidState, }
Expand description

Errors that can occur during BigInt operations.

All variants are designed to be constant-time and provide minimal information to prevent side-channel attacks while still being useful for debugging.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Overflow

Operation would exceed the maximum capacity.

This error occurs when a BigInt operation would require more limbs than the configured maximum capacity. This prevents unbounded memory allocation and ensures constant-time behavior.

§Examples

use clock_bigint::{BigInt, add_magnitude};

// Create BigInts with values that need 1 limb each
let a = BigInt::from_u64(u64::MAX, 10);
let b = BigInt::from_u64(u64::MAX, 10);

// Try to add into a result with insufficient capacity (only 1 limb max)
let mut result = BigInt::new(1); // Only 1 limb capacity
let add_result = add_magnitude(&a, &b, &mut result);
assert!(add_result.is_err());
§

InvalidEncoding

Invalid encoding detected (non-canonical form).

This error occurs when attempting to decode malformed or non-canonical binary representations of BigInt values. The encoding format requires specific structure and canonical forms for security and correctness.

§Examples

use clock_bigint::BigInt;

// Empty data cannot be decoded
let result = clock_bigint::decode(&[]);
assert!(matches!(result, Err(clock_bigint::BigIntError::InvalidEncoding)));

// Invalid length prefix
let invalid_data = vec![0u8; 10]; // Too short for valid encoding
let result = clock_bigint::decode(&invalid_data);
assert!(matches!(result, Err(clock_bigint::BigIntError::InvalidEncoding)));
§

DivisionByZero

Division by zero attempted.

This error occurs when attempting to divide by zero or compute modulo zero. These operations are mathematically undefined.

§Examples

use clock_bigint::{BigInt, div};

let a = BigInt::from_u64(10, 10);
let zero = BigInt::from_u64(0, 10);

// Division by zero
let result = div(&a, &zero);
assert!(matches!(result, Err(clock_bigint::BigIntError::DivisionByZero)));
§

InvalidModulus

Invalid modulus for Montgomery arithmetic.

This error occurs when attempting to use Montgomery arithmetic with an unsupported modulus. Montgomery operations require the modulus to be odd and non-zero for the algorithms to work correctly.

§Examples

use clock_bigint::{BigInt, MontgomeryContext};

let even_modulus = BigInt::from_u64(10, 10); // Even modulus

// Creating Montgomery context with even modulus fails
let result = MontgomeryContext::new(even_modulus);
assert!(matches!(result, Err(clock_bigint::BigIntError::InvalidModulus)));

let zero_modulus = BigInt::from_u64(0, 10); // Zero modulus
let result = MontgomeryContext::new(zero_modulus);
assert!(matches!(result, Err(clock_bigint::BigIntError::InvalidModulus)));
§

InvalidState

Invalid internal state.

This error occurs when an operation is attempted but the BigInt is in an invalid internal state, such as having non-canonical representation when canonical form is required.

§Examples

use clock_bigint::BigInt;

let mut bi = BigInt::from_u64(10, 10);
// Manually corrupt internal state (normally not possible through public API)
// This would cause InvalidState if the corruption were detected

Trait Implementations§

Source§

impl Clone for BigIntError

Source§

fn clone(&self) -> BigIntError

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for BigIntError

Source§

impl Debug for BigIntError

Source§

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

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

impl Display for BigIntError

Source§

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

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

impl Eq for BigIntError

Source§

impl PartialEq for BigIntError

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 StructuralPartialEq for BigIntError

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.