pub struct FieldElement(/* private fields */);Expand description
P-256 field element representing values in F_p
Internally stored as 8 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; 32]) -> Result<Self>
pub fn from_bytes(bytes: &[u8; 32]) -> 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 256-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 512-bit product using schoolbook multiplication
- Perform carry propagation to get proper limb representation
- Apply NIST P-256 specific fast reduction (Solinas method)
The multiplication is performed in three phases to maintain clarity and correctness while achieving good performance.
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 if the field element is odd (least-significant bit 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>
Compute modular square root using exponentiation.
Because the P-256 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 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl ConditionallySelectable for FieldElement
impl ConditionallySelectable for FieldElement
Source§fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
Source§fn conditional_assign(&mut self, other: &Self, choice: Choice)
fn conditional_assign(&mut self, other: &Self, choice: Choice)
Source§fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
self and other if choice == 1; otherwise,
reassign both unto themselves. Read moreimpl Copy for FieldElement
Source§impl Debug for FieldElement
impl Debug for FieldElement
impl Eq for FieldElement
Source§impl PartialEq for FieldElement
impl PartialEq for FieldElement
Source§fn eq(&self, other: &FieldElement) -> bool
fn eq(&self, other: &FieldElement) -> bool
self and other values to be equal, and is used by ==.