pub struct FieldElement(pub(crate) [u64; 9]);Expand description
Element of the secp521r1 base field used for curve coordinates.
Tuple Fields§
§0: [u64; 9]Implementations§
Source§impl FieldElement
 
impl FieldElement
Sourcepub fn from_bytes(repr: &FieldBytes) -> CtOption<Self>
 
pub fn from_bytes(repr: &FieldBytes) -> CtOption<Self>
Create a FieldElement from a canonical big-endian representation.
Sourcepub fn from_slice(slice: &[u8]) -> Result<Self>
 
pub fn from_slice(slice: &[u8]) -> Result<Self>
Decode FieldElement from a big endian byte slice.
Sourcepub(crate) const fn from_hex(hex: &str) -> Self
 
pub(crate) const fn from_hex(hex: &str) -> Self
Parse a FieldElement from big endian hex-encoded bytes.
Does not perform a check that the field element does not overflow the order.
This method is primarily intended for defining internal constants.
Sourcepub const fn from_u64(w: u64) -> Self
 
pub const fn from_u64(w: u64) -> Self
Convert a u64 into a FieldElement.
Sourcepub(crate) const fn from_uint_unchecked(w: U576) -> Self
 
pub(crate) const fn from_uint_unchecked(w: U576) -> Self
Decode FieldElement from U576.
Does not perform a check that the field element does not overflow the order.
Used incorrectly this can lead to invalid results!
Sourcepub fn to_bytes(self) -> FieldBytes
 
pub fn to_bytes(self) -> FieldBytes
Returns the big-endian encoding of this FieldElement.
Sourcepub fn is_odd(&self) -> Choice
 
pub fn is_odd(&self) -> Choice
Determine if this FieldElement is odd in the SEC1 sense: self mod 2 == 1.
§Returns
If odd, return Choice(1).  Otherwise, return Choice(0).
Sourcepub fn is_even(&self) -> Choice
 
pub fn is_even(&self) -> Choice
Determine if this FieldElement is even in the SEC1 sense: self mod 2 == 0.
§Returns
If even, return Choice(1).  Otherwise, return Choice(0).
Sourcepub fn is_zero(&self) -> Choice
 
pub fn is_zero(&self) -> Choice
Determine if this FieldElement is zero.
§Returns
If zero, return Choice(1).  Otherwise, return Choice(0).
Sourcepub(crate) const fn add_loose(&self, rhs: &Self) -> LooseFieldElement
 
pub(crate) const fn add_loose(&self, rhs: &Self) -> LooseFieldElement
Add elements.
Sourcepub(crate) const fn double_loose(&self) -> LooseFieldElement
 
pub(crate) const fn double_loose(&self) -> LooseFieldElement
Double element (add it to itself).
Sourcepub(crate) const fn sub_loose(&self, rhs: &Self) -> LooseFieldElement
 
pub(crate) const fn sub_loose(&self, rhs: &Self) -> LooseFieldElement
Subtract elements, returning a loose field element.
Sourcepub(crate) const fn neg_loose(&self) -> LooseFieldElement
 
pub(crate) const fn neg_loose(&self) -> LooseFieldElement
Negate element, returning a loose field element.
Sourcepub const fn pow_vartime(&self, exp: &[u64]) -> Self
 
pub const fn pow_vartime(&self, exp: &[u64]) -> Self
Returns self^exp, where exp is a little-endian integer exponent.
This operation is variable time with respect to the exponent.
If the exponent is fixed, this operation is effectively constant time.
Sourcepub fn invert(&self) -> CtOption<Self>
 
pub fn invert(&self) -> CtOption<Self>
Compute FieldElement inversion: 1 / self.
Sourceconst fn invert_unchecked(&self) -> Self
 
const fn invert_unchecked(&self) -> Self
Returns the multiplicative inverse of self.
Does not check that self is non-zero.
Sourcepub fn sqrt(&self) -> CtOption<Self>
 
pub fn sqrt(&self) -> CtOption<Self>
Returns the square root of self mod p, or None if no square root
exists.
§Implementation details
If x has a sqrt, then due to Euler’s criterion this implies x(p - 1)/2 = 1.
- x(p + 1)/2 = x.
 - There’s a special property due to p ≡ 3 (mod 4) which implies (p + 1)/4 is an integer.
 - We can rewrite 
1.as x((p+1)/4)2 - x(p+1)/4 is the square root.
 - This is simplified as (2251 - 1 + 1) /4 = 2519
 - Hence, x2519 is the square root iff result.square() == self
 
Sourcepub(crate) const fn relax(&self) -> LooseFieldElement
 
pub(crate) const fn relax(&self) -> LooseFieldElement
Relax a tight field element into a loose one.
Trait Implementations§
Source§impl Add<&FieldElement> for &FieldElement
 
impl Add<&FieldElement> for &FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
+ operator.Source§fn add(self, rhs: &FieldElement) -> FieldElement
 
fn add(self, rhs: &FieldElement) -> FieldElement
+ operation. Read moreSource§impl Add<&FieldElement> for FieldElement
 
impl Add<&FieldElement> for FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
+ operator.Source§fn add(self, rhs: &FieldElement) -> FieldElement
 
fn add(self, rhs: &FieldElement) -> FieldElement
+ operation. Read moreSource§impl Add for FieldElement
 
impl Add for FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
+ operator.Source§fn add(self, rhs: FieldElement) -> FieldElement
 
fn add(self, rhs: FieldElement) -> FieldElement
+ operation. Read moreSource§impl AddAssign<&FieldElement> for FieldElement
 
impl AddAssign<&FieldElement> for FieldElement
Source§fn add_assign(&mut self, other: &FieldElement)
 
fn add_assign(&mut self, other: &FieldElement)
+= operation. Read moreSource§impl AddAssign for FieldElement
 
impl AddAssign for FieldElement
Source§fn add_assign(&mut self, other: FieldElement)
 
fn add_assign(&mut self, other: FieldElement)
+= operation. Read moreSource§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 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 moreSource§impl ConstantTimeEq for FieldElement
 
impl ConstantTimeEq for FieldElement
Source§impl Debug for FieldElement
 
impl Debug for FieldElement
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
 
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formatting machinery for FieldElement
§Why
let fe1 = FieldElement([9, 0, 0, 0, 0, 0, 0, 0, 0]);
let fe2 = FieldElement([
    8,
    0,
    288230376151711744,
    288230376151711743,
    288230376151711743,
    288230376151711743,
    288230376151711743,
    288230376151711743,
    144115188075855871,
]);For the above example, deriving core::fmt::Debug will result in returning 2 different
strings, which are in reality the same due to p521’s unsaturated math, instead print the
output as a hex string in big-endian.
This makes debugging easier.
Source§impl Default for FieldElement
 
impl Default for FieldElement
Source§impl Field for FieldElement
 
impl Field for FieldElement
Source§fn random(rng: impl RngCore) -> Self
 
fn random(rng: impl RngCore) -> Self
Source§fn invert(&self) -> CtOption<Self>
 
fn invert(&self) -> CtOption<Self>
Source§fn sqrt(&self) -> CtOption<Self>
 
fn sqrt(&self) -> CtOption<Self>
Source§fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self)
 
fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self)
Source§fn is_zero_vartime(&self) -> bool
 
fn is_zero_vartime(&self) -> bool
Source§impl From<&FieldElement> for LooseFieldElement
 
impl From<&FieldElement> for LooseFieldElement
Source§fn from(tight: &FieldElement) -> LooseFieldElement
 
fn from(tight: &FieldElement) -> LooseFieldElement
Source§impl From<&LooseFieldElement> for FieldElement
 
impl From<&LooseFieldElement> for FieldElement
Source§fn from(loose: &LooseFieldElement) -> FieldElement
 
fn from(loose: &LooseFieldElement) -> FieldElement
Source§impl From<FieldElement> for LooseFieldElement
 
impl From<FieldElement> for LooseFieldElement
Source§fn from(tight: FieldElement) -> LooseFieldElement
 
fn from(tight: FieldElement) -> LooseFieldElement
Source§impl From<LooseFieldElement> for FieldElement
 
impl From<LooseFieldElement> for FieldElement
Source§fn from(loose: LooseFieldElement) -> FieldElement
 
fn from(loose: LooseFieldElement) -> FieldElement
Source§impl From<u128> for FieldElement
 
impl From<u128> for FieldElement
Source§fn from(n: u128) -> FieldElement
 
fn from(n: u128) -> FieldElement
Source§impl From<u32> for FieldElement
 
impl From<u32> for FieldElement
Source§fn from(n: u32) -> FieldElement
 
fn from(n: u32) -> FieldElement
Source§impl From<u64> for FieldElement
 
impl From<u64> for FieldElement
Source§fn from(n: u64) -> FieldElement
 
fn from(n: u64) -> FieldElement
Source§impl Mul<&FieldElement> for &FieldElement
 
impl Mul<&FieldElement> for &FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
* operator.Source§fn mul(self, rhs: &FieldElement) -> FieldElement
 
fn mul(self, rhs: &FieldElement) -> FieldElement
* operation. Read moreSource§impl Mul<&FieldElement> for FieldElement
 
impl Mul<&FieldElement> for FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
* operator.Source§fn mul(self, rhs: &FieldElement) -> FieldElement
 
fn mul(self, rhs: &FieldElement) -> FieldElement
* operation. Read moreSource§impl Mul for FieldElement
 
impl Mul for FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
* operator.Source§fn mul(self, rhs: FieldElement) -> FieldElement
 
fn mul(self, rhs: FieldElement) -> FieldElement
* operation. Read moreSource§impl MulAssign<&FieldElement> for FieldElement
 
impl MulAssign<&FieldElement> for FieldElement
Source§fn mul_assign(&mut self, other: &FieldElement)
 
fn mul_assign(&mut self, other: &FieldElement)
*= operation. Read moreSource§impl MulAssign for FieldElement
 
impl MulAssign for FieldElement
Source§fn mul_assign(&mut self, other: FieldElement)
 
fn mul_assign(&mut self, other: FieldElement)
*= operation. Read moreSource§impl Neg for FieldElement
 
impl Neg for FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
- operator.Source§fn neg(self) -> FieldElement
 
fn neg(self) -> FieldElement
- operation. Read moreSource§impl PartialEq for FieldElement
 
impl PartialEq for FieldElement
Source§impl PrimeField for FieldElement
 
impl PrimeField for FieldElement
Source§const MODULUS: &'static str = MODULUS_HEX
 
const MODULUS: &'static str = MODULUS_HEX
Source§const CAPACITY: u32 = 520u32
 
const CAPACITY: u32 = 520u32
Source§const MULTIPLICATIVE_GENERATOR: Self
 
const MULTIPLICATIVE_GENERATOR: Self
modulus - 1 order. This element must also be
a quadratic nonresidue. Read moreSource§const ROOT_OF_UNITY: Self
 
const ROOT_OF_UNITY: Self
2^s root of unity. Read moreSource§const ROOT_OF_UNITY_INV: Self
 
const ROOT_OF_UNITY_INV: Self
Self::ROOT_OF_UNITY.Source§type Repr = GenericArray<u8, <NistP521 as Curve>::FieldBytesSize>
 
type Repr = GenericArray<u8, <NistP521 as Curve>::FieldBytesSize>
Source§fn from_repr(bytes: FieldBytes) -> CtOption<Self>
 
fn from_repr(bytes: FieldBytes) -> CtOption<Self>
Source§fn to_repr(&self) -> FieldBytes
 
fn to_repr(&self) -> FieldBytes
Source§fn from_str_vartime(s: &str) -> Option<Self>
 
fn from_str_vartime(s: &str) -> Option<Self>
Source§impl<'a> Product<&'a FieldElement> for FieldElement
 
impl<'a> Product<&'a FieldElement> for FieldElement
Source§impl Product for FieldElement
 
impl Product for FieldElement
Source§impl Sub<&FieldElement> for &FieldElement
 
impl Sub<&FieldElement> for &FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
- operator.Source§fn sub(self, rhs: &FieldElement) -> FieldElement
 
fn sub(self, rhs: &FieldElement) -> FieldElement
- operation. Read moreSource§impl Sub<&FieldElement> for FieldElement
 
impl Sub<&FieldElement> for FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
- operator.Source§fn sub(self, rhs: &FieldElement) -> FieldElement
 
fn sub(self, rhs: &FieldElement) -> FieldElement
- operation. Read moreSource§impl Sub for FieldElement
 
impl Sub for FieldElement
Source§type Output = FieldElement
 
type Output = FieldElement
- operator.Source§fn sub(self, rhs: FieldElement) -> FieldElement
 
fn sub(self, rhs: FieldElement) -> FieldElement
- operation. Read moreSource§impl SubAssign<&FieldElement> for FieldElement
 
impl SubAssign<&FieldElement> for FieldElement
Source§fn sub_assign(&mut self, other: &FieldElement)
 
fn sub_assign(&mut self, other: &FieldElement)
-= operation. Read moreSource§impl SubAssign for FieldElement
 
impl SubAssign for FieldElement
Source§fn sub_assign(&mut self, other: FieldElement)
 
fn sub_assign(&mut self, other: FieldElement)
-= operation. Read moreSource§impl<'a> Sum<&'a FieldElement> for FieldElement
 
impl<'a> Sum<&'a FieldElement> for FieldElement
Source§fn sum<I: Iterator<Item = &'a FieldElement>>(iter: I) -> Self
 
fn sum<I: Iterator<Item = &'a FieldElement>>(iter: I) -> Self
Self from the elements by “summing up”
the items.