pub struct FieldElement(/* private fields */);
Expand description
P-384 field element representing values in F_p
Internally stored as 12 little-endian 32-bit limbs for efficient arithmetic. All operations maintain the invariant that values are reduced modulo p.
Implementations§
Source§impl FieldElement
impl FieldElement
Sourcepub fn from_bytes(bytes: &[u8; 48]) -> Result<Self>
pub fn from_bytes(bytes: &[u8; 48]) -> Result<Self>
Create a field element from big-endian byte representation
Validates that the input represents a value less than the field modulus p. Returns an error if the value is >= p.
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Constant-time validation that the field element is in canonical form (< p)
Uses constant-time subtraction to check if self < p without branching. Returns true if the element is valid (< p), false otherwise.
Sourcepub fn add(&self, other: &Self) -> Self
pub fn add(&self, other: &Self) -> Self
Constant-time field addition: (self + other) mod p
Algorithm:
- Perform full 384-bit addition with carry detection
- Conditionally subtract p if result >= p
- Ensure result is in canonical form
Sourcepub fn sub(&self, other: &Self) -> Self
pub fn sub(&self, other: &Self) -> Self
Constant-time field subtraction: (self - other) mod p
Algorithm:
- Perform limb-wise subtraction
- If subtraction borrows, add p to get the correct positive result
Sourcepub fn mul(&self, other: &Self) -> Self
pub fn mul(&self, other: &Self) -> Self
Field multiplication: (self * other) mod p
Algorithm:
- Compute the full 768-bit product using schoolbook multiplication
- Perform carry propagation to get proper limb representation
- Apply Barrett reduction for P-384
Sourcepub fn square(&self) -> Self
pub fn square(&self) -> Self
Field squaring: self² mod p
Optimized version of multiplication for the case where both operands are the same. Currently implemented as self.mul(self) but could be optimized further with dedicated squaring algorithms.
Sourcepub fn invert(&self) -> Result<Self>
pub fn invert(&self) -> Result<Self>
Compute the modular multiplicative inverse using Fermat’s Little Theorem
For prime fields, a^(p-1) ≡ 1 (mod p), so a^(p-2) ≡ a^(-1) (mod p). Uses binary exponentiation (square-and-multiply) for efficiency.
Returns an error if attempting to invert zero (which has no inverse).
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Check if the field element represents zero
Constant-time check across all limbs to determine if the field element is the additive identity.
Sourcepub fn is_odd(&self) -> bool
pub fn is_odd(&self) -> bool
Return true
when the element is odd (LSB set)
Used for point compression to determine the sign of the y-coordinate. The parity is determined by the least significant bit of the canonical representation.
Sourcepub fn sqrt(&self) -> Option<Self>
pub fn sqrt(&self) -> Option<Self>
Modular square root using the (p+1)/4 shortcut (p ≡ 3 mod 4).
Because the P-384 prime satisfies p ≡ 3 (mod 4), we can compute sqrt(a) = a^((p+1)/4) mod p. This is more efficient than the general Tonelli-Shanks algorithm.
Returns None
when the input is a quadratic non-residue (i.e.,
when no square root exists in the field).
§Algorithm
For p ≡ 3 (mod 4), if a has a square root, then:
- sqrt(a) = ±a^((p+1)/4) mod p
- We return the principal square root (the smaller of the two)
Trait Implementations§
Source§impl Clone for FieldElement
impl Clone for FieldElement
Source§fn clone(&self) -> FieldElement
fn clone(&self) -> FieldElement
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more