pub trait FieldOps:
Sized
+ Clone
+ PartialEq
+ Eq
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Neg<Output = Self>
+ Default
+ ConditionallySelectable {
Show 27 methods
// Required methods
fn zero() -> Self;
fn one() -> Self;
fn is_zero(&self) -> Choice;
fn is_one(&self) -> Choice;
fn negate(&self) -> Self;
fn add(&self, rhs: &Self) -> Self;
fn sub(&self, rhs: &Self) -> Self;
fn mul(&self, rhs: &Self) -> Self;
fn square(&self) -> Self;
fn double(&self) -> Self;
fn invert(&self) -> CtOption<Self>;
fn frobenius(&self) -> Self;
fn norm(&self) -> Self;
fn trace(&self) -> Self;
fn sqrt(&self) -> CtOption<Self>;
fn legendre(&self) -> i8;
fn characteristic() -> Vec<u64>;
fn degree() -> u32;
fn from_u64(x: u64) -> Self;
// Provided methods
fn div(&self, rhs: &Self) -> CtOption<Self> { ... }
fn pow_vartime(&self, exp: &[u64]) -> Self { ... }
fn pow(&self, exp: &[u64]) -> Self { ... }
fn frobenius_pow(&self, k: u32) -> Self { ... }
fn inverse_and_sqrt(&self) -> (CtOption<Self>, CtOption<Self>) { ... }
fn inv_sqrt(&self) -> CtOption<Self> { ... }
fn invertme_sqrtother(&self, rhs: &Self) -> (CtOption<Self>, CtOption<Self>) { ... }
fn sqrt_ratio(&self, rhs: &Self) -> CtOption<Self> { ... }
}Expand description
Core arithmetic interface for field elements used throughout the library.
This trait abstracts the operations needed by the prime-field layer, extension fields, and higher-level elliptic-curve code.
It combines:
- basic ring-style operations (
+,-,*, unary-); - distinguished constants
zero()andone(); - predicates such as
is_zero()andis_one(); - field-specific operations such as inversion, Frobenius, norm, trace, square roots, and Legendre-symbol-style square testing;
- constant-time conditional selection via
subtle::ConditionallySelectable.
The trait is intentionally separate from FieldRandom, so downstream code
that only needs deterministic arithmetic does not need to depend on rand.
Scalars used by exponentiation methods are encoded as little-endian u64
limbs, matching the convention used elsewhere in the library.
Required Methods§
Sourcefn norm(&self) -> Self
fn norm(&self) -> Self
compute the norm of self down to $\mathbb{F}_p$ (as an
element of type Self)
Sourcefn trace(&self) -> Self
fn trace(&self) -> Self
compute the trace of self down to $\mathbb{F}_p$ (as an
element of type Self)
Sourcefn sqrt(&self) -> CtOption<Self>
fn sqrt(&self) -> CtOption<Self>
Returns a squareroot if it exists
Returns a squareroof of self if it exists in the finite
field $\mathbb{F}{p^M}$. The return type is CtOption<Self>
and it is not none if and only if the squareroot exists. This
is an implementation fo the Tonelli–Shanks algorithm in the
multiplicative group $\mathbb{F}{p^M}^*$
§Arguments
&self- Element of $\mathbb{F}_{p^M}$ (type: self)
§Returns
The square root of self (type: CtOption<Self>)
Sourcefn legendre(&self) -> i8
fn legendre(&self) -> i8
Computes the “Legendre symbol” i.e., if 0,1,-1 depending if
self is 0, a square or a nonsquare.
Sourcefn characteristic() -> Vec<u64>
fn characteristic() -> Vec<u64>
Returns the characteristic of the field.
Provided Methods§
Sourcefn pow_vartime(&self, exp: &[u64]) -> Self
fn pow_vartime(&self, exp: &[u64]) -> Self
self^exp using square-and multiply (litte-endian bit order)
It is constant time for fixed exp
§Arguments
&self- Finite field element (type: self)exp- Exponent (type: &u64)
§Returns
&self^exp (type: Self)
§Why <Self as FieldOps>::mul instead of result.mul(&base)
FieldOps requires Mul<Output = Self> as a supertrait, so Self
exposes two methods named mul:
<Self as Mul>::mul(self, rhs: Self) -> Self← operator, takes by value<Self as FieldOps>::mul(&self, rhs: &Self) -> Self← ours, takes by ref
Writing result.mul(&base) triggers method resolution, which picks
Mul::mul (the operator) because it was declared first in the supertrait
list. Mul::mul expects Self, not &Self → E0308.
Fully-qualified syntax <Self as FieldOps>::mul(...) bypasses method
resolution entirely and calls exactly the trait method we want.
Sourcefn pow(&self, exp: &[u64]) -> Self
fn pow(&self, exp: &[u64]) -> Self
self^pow in constant time using a Montgomery ladder
Uses a Montgomery ladder to compute self^exp
WARNING: Only constant time if the number of limbs of exp is
constant
§Arguments
&self- Element of $\mathbb{F}_p$ (type: self)exp- Exponent (type: &u64)
§Returns
The value self^pow (type: Self)
§Todo
Use subtle and conditional_swap to make true constant time
Sourcefn frobenius_pow(&self, k: u32) -> Self
fn frobenius_pow(&self, k: u32) -> Self
Compute self^{p^k} a power of the frobenius
Sourcefn inverse_and_sqrt(&self) -> (CtOption<Self>, CtOption<Self>)
fn inverse_and_sqrt(&self) -> (CtOption<Self>, CtOption<Self>)
Computes the inverse and square root of self
Computes simulaineously the inverse and square root of self.
§Arguments
&self- Element of $\mathbb{F}_{p^M}$ (type: self)
§Returns
The inverse and square root of self. The former is none if
and only if nonzero and the latter is not none if and only if
there exists a squareroot in $\mathbb{F}_{p^M}$
(type: (CtOption<Self>, CtOption<self>))
Sourcefn invertme_sqrtother(&self, rhs: &Self) -> (CtOption<Self>, CtOption<Self>)
fn invertme_sqrtother(&self, rhs: &Self) -> (CtOption<Self>, CtOption<Self>)
Computes the inverse of self and square root of rhs
Computes simulaineously the inverse of self and square root
of rhs.
§Arguments
&self- Element of $\mathbb{F}_{p^M}$ (type: self)rhs- Element of $\mathbb{F}_{p^M}$ (type: &Self)
§Returns
The inverse of self and square root fo rhs. Theq former is
none if and only if self is nonzero and the latter is not
none if and only if there exists a squareroot of rhs in $\mathbb{F}_{p^M}$
(type: (CtOption<Self>, CtOption<self>))
Sourcefn sqrt_ratio(&self, rhs: &Self) -> CtOption<Self>
fn sqrt_ratio(&self, rhs: &Self) -> CtOption<Self>
Computes the squareroot of a ratio self/rhs
§Arguments
&self- Element of $\mathbb{F}_{p^M}$ (type: self)rhs- Element of $\mathbb{F}_{p^M}$ (type: &Self)
§Returns
The squareroot of the ratio self/rhs is not none if and only
if rhs is invertible and the ratio has an $\mathbb{F}_{p^M}$ squareroot
(type: (CtOption<Self>, CtOption<self>))
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.