clock_curve_math/bigint/
arithmetic.rs

1//! BigInt arithmetic operations trait.
2
3use core::cmp::Ordering;
4
5use super::BigInt;
6
7/// Trait for big integer arithmetic operations.
8///
9/// All operations must execute in constant time for security-critical use cases.
10pub trait BigIntOps {
11    /// Constant-time addition.
12    fn add(&self, rhs: &Self) -> Self;
13
14    /// Constant-time subtraction.
15    fn sub(&self, rhs: &Self) -> Self;
16
17    /// Constant-time multiplication.
18    ///
19    /// Note: This may produce a double-width result that needs reduction.
20    fn mul(&self, rhs: &Self) -> Self;
21
22    /// Wide multiplication returning the full 512-bit result as (low, high) BigInts.
23    fn mul_wide(&self, rhs: &Self) -> (Self, Self)
24    where
25        Self: Sized;
26
27    /// Constant-time left shift.
28    fn shl(&self, bits: u32) -> Self;
29
30    /// Constant-time right shift.
31    fn shr(&self, bits: u32) -> Self;
32
33    /// Constant-time comparison.
34    fn cmp(&self, rhs: &Self) -> Ordering;
35
36    /// Constant-time zero check.
37    fn is_zero(&self) -> bool;
38}
39
40impl BigIntOps for BigInt {
41    fn add(&self, rhs: &Self) -> Self {
42        self.add(rhs)
43    }
44
45    fn sub(&self, rhs: &Self) -> Self {
46        self.sub(rhs)
47    }
48
49    fn mul(&self, rhs: &Self) -> Self {
50        self.mul(rhs)
51    }
52
53    fn mul_wide(&self, rhs: &Self) -> (Self, Self) {
54        self.mul_wide(rhs)
55    }
56
57    fn shl(&self, bits: u32) -> Self {
58        self.shl(bits)
59    }
60
61    fn shr(&self, bits: u32) -> Self {
62        self.shr(bits)
63    }
64
65    fn cmp(&self, rhs: &Self) -> Ordering {
66        self.cmp(rhs)
67    }
68
69    fn is_zero(&self) -> bool {
70        self.is_zero()
71    }
72}