FieldElement

Struct FieldElement 

Source
pub struct FieldElement { /* private fields */ }
Expand description

FieldElement represents an element of the field modulo p = 2^255 - 19.

Values are stored in Montgomery form for efficient multiplication.

Implementations§

Source§

impl FieldElement

Source

pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self, MathError>

Create a FieldElement from bytes (canonical representation).

The bytes must represent a value in the range [0, p). Values outside this range will result in an error.

§Examples
use clock_curve_math::FieldElement;

let bytes = [1u8; 32]; // Little-endian representation of a small number
let element = FieldElement::from_bytes(&bytes).unwrap();
assert_eq!(element.to_bytes(), bytes);
§Errors

Returns crate::error::MathError::InvalidBytes if the byte representation is not in [0, p).

Source

pub fn to_bytes(&self) -> [u8; 32]

Convert to bytes (canonical representation).

Returns the canonical little-endian byte representation of the field element. The result is guaranteed to be in the range [0, p).

§Examples
use clock_curve_math::FieldElement;

let element = FieldElement::from_u64(42);
let bytes = element.to_bytes();
let reconstructed = FieldElement::from_bytes(&bytes).unwrap();
assert_eq!(element, reconstructed);
Source

pub fn from_u64(value: u64) -> Self

Create a FieldElement from a u64 value.

The value is automatically reduced modulo p if necessary.

§Examples
use clock_curve_math::FieldElement;

let zero = FieldElement::from_u64(0);
let one = FieldElement::from_u64(1);
let large = FieldElement::from_u64(u64::MAX); // Will be reduced mod p
Source

pub fn from_bigint(value: &BigInt) -> Result<Self, MathError>

Create a FieldElement from a BigInt value with validation.

The value must be in the range [0, p) where p is the field modulus.

§Examples
use clock_curve_math::{FieldElement, BigInt};

let value = BigInt::from_u64(42);
let element = FieldElement::from_bigint(&value).unwrap();
§Errors

Returns crate::error::MathError::InvalidFieldElement if the value is not in [0, p).

Source

pub fn to_bigint(&self) -> BigInt

Convert a FieldElement to a BigInt.

Returns the Montgomery representation of the field element as a BigInt. To get the regular representation, use from_montgomery on the result.

§Examples
use clock_curve_math::{FieldElement, BigInt};

let element = FieldElement::from_u64(42);
let bigint = element.to_bigint();
let regular = clock_curve_math::montgomery::from_montgomery_p(&bigint);
assert_eq!(regular, BigInt::from_u64(42));
Source

pub fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self

Constant-time conditional selection.

Returns a if flag == 1, b if flag == 0. Executes in constant time regardless of the flag value.

§Safety

flag should be either 0 or 1 for correct behavior.

§Examples
use clock_curve_math::FieldElement;

let a = FieldElement::from_u64(10);
let b = FieldElement::from_u64(20);

let selected = FieldElement::conditional_select(&a, &b, clock_curve_math::ct::Choice::from_bool(true)); // selects a
assert_eq!(selected, a);

let selected = FieldElement::conditional_select(&a, &b, clock_curve_math::ct::Choice::from_bool(false)); // selects b
assert_eq!(selected, b);
Source

pub fn conditional_swap(a: &mut Self, b: &mut Self, flag: u64)

Constant-time conditional swap.

Swaps a and b if flag == 1, leaves them unchanged if flag == 0. Executes in constant time regardless of the flag value.

§Safety

flag should be either 0 or 1 for correct behavior.

§Examples
use clock_curve_math::FieldElement;

let mut a = FieldElement::from_u64(10);
let mut b = FieldElement::from_u64(20);

FieldElement::conditional_swap(&mut a, &mut b, 0);
assert_eq!(a, FieldElement::from_u64(10)); // unchanged
assert_eq!(b, FieldElement::from_u64(20)); // unchanged

FieldElement::conditional_swap(&mut a, &mut b, 1);
assert_eq!(a, FieldElement::from_u64(20)); // swapped
assert_eq!(b, FieldElement::from_u64(10)); // swapped
Source

pub fn is_valid(&self) -> bool

Check if the value is valid (in range [0, p)).

Returns true if the field element represents a value in [0, p). Field elements created through the public API are always valid.

§Examples
use clock_curve_math::FieldElement;

let element = FieldElement::from_u64(42);
assert!(element.is_valid());
Source

pub fn neg(&self) -> Self

Compute the additive inverse (negation).

Returns -self mod p, which satisfies self + (-self) ≡ 0 mod p.

§Examples
use clock_curve_math::{FieldElement, FieldOps};

let a = FieldElement::from_u64(5);
let neg_a = a.neg();
let sum = a.add(&neg_a);
assert_eq!(sum, FieldElement::from_u64(0));
Source

pub fn is_zero(&self) -> bool

Check if this field element is zero.

Returns true if the field element represents the additive identity (zero).

§Examples
use clock_curve_math::FieldElement;

let zero = FieldElement::from_u64(0);
let nonzero = FieldElement::from_u64(42);

assert!(zero.is_zero());
assert!(!nonzero.is_zero());

Trait Implementations§

Source§

impl AdvancedComputation for FieldElement

Source§

fn evaluate_polynomial(&self, coeffs: &[Self], x: &Self) -> Self

Compute result of a polynomial evaluation. Read more
Source§

fn gcd_extended(&self, other: &Self) -> Self

Compute the greatest common divisor of two elements. Read more
Source§

impl Clone for FieldElement

Source§

fn clone(&self) -> FieldElement

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 ConfigurableArithmetic for FieldElement

Source§

fn mul_with_config(&self, rhs: &Self, config: &MultiplicationConfig) -> Self

Multiply with configuration.
Source§

fn pow_with_config(&self, exp: &BigInt, config: &ExponentiationConfig) -> Self

Compute power with configuration.
Source§

impl Debug for FieldElement

Source§

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

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

impl Default for FieldElement

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl FieldExtensions for FieldElement

Source§

fn try_sqrt(&self) -> Option<Self>

Attempt to compute the square root. Read more
Source§

fn legendre(&self) -> i32

Compute the Legendre symbol (a/p). Read more
Source§

fn order(&self) -> Option<BigInt>

Get the order of this element (if it generates the field). Read more
Source§

fn is_quadratic_residue(&self) -> bool

Check if this element is a quadratic residue.
Source§

impl FieldOps for FieldElement

Source§

fn add(&self, rhs: &Self) -> Self

Modular addition. Read more
Source§

fn sub(&self, rhs: &Self) -> Self

Modular subtraction. Read more
Source§

fn mul(&self, rhs: &Self) -> Self

Montgomery multiplication. Read more
Source§

fn square(&self) -> Self

Squaring (optimized). Read more
Source§

fn inv(&self) -> Self

Constant-time modular inverse. Read more
Source§

fn pow(&self, exp: &BigInt) -> Self

Modular exponentiation. Read more
Source§

fn pow_checked(&self, exp: &BigInt) -> Result<Self, MathError>

Modular exponentiation with input validation. Read more
Source§

impl FutureOperations for FieldElement

Source§

fn batch_op_reserved(&self, _op: u32) -> Result<Self, MathError>
where Self: Sized,

Reserved for future batch operations.
Source§

fn crypto_op_reserved( &self, _op: u32, _params: &[u8], ) -> Result<Vec<u8>, MathError>

Reserved for future cryptographic operations.
Source§

fn supports_future_op(&self, op: u32) -> bool

Check if a future operation is supported.
Source§

impl HardwareAcceleration for FieldElement

Source§

fn hardware_accelerated(&self, operation: HardwareOperation) -> bool

Check if hardware acceleration is available for this operation.
Source§

impl MemoryOptimization for FieldElement

Source§

fn preferred_alignment(&self) -> usize
where Self: Sized,

Get the preferred memory alignment for this type.
Source§

impl PartialEq for FieldElement

Source§

fn eq(&self, other: &FieldElement) -> 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 FieldElement

Source§

impl Eq for FieldElement

Source§

impl StructuralPartialEq for FieldElement

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.