pub struct FieldElement(pub(crate) fiat_p521_tight_field_element);Expand description
Element of the secp521r1 base field used for curve coordinates.
Tuple Fields§
§0: fiat_p521_tight_field_elementImplementations§
Source§impl FieldElement
impl FieldElement
const LIMBS: usize = 9
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.
This method is primarily intended for defining internal constants.
§Panics
- if the input in hex is not the correct length
- if the given value when decoded from hex overflows the modulus
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: Uint) -> Self
pub(crate) const fn from_uint_unchecked(w: Uint) -> Self
Decode FieldElement from Uint.
Does not perform a check that the field element does not overflow the order.
Used incorrectly this can lead to invalid results!
Sourcepub const fn to_bytes(self) -> FieldBytes
pub const 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 const fn add_loose(&self, rhs: &Self) -> LooseFieldElement
pub const fn add_loose(&self, rhs: &Self) -> LooseFieldElement
Add elements.
Sourcepub const fn double_loose(&self) -> LooseFieldElement
pub const fn double_loose(&self) -> LooseFieldElement
Double element (add it to itself).
Sourcepub const fn sub_loose(&self, rhs: &Self) -> LooseFieldElement
pub const fn sub_loose(&self, rhs: &Self) -> LooseFieldElement
Subtract elements, returning a loose field element.
Sourcepub const fn neg_loose(&self) -> LooseFieldElement
pub const fn neg_loose(&self) -> LooseFieldElement
Negate element, returning a loose field element.
Sourcepub const fn pow_vartime<const RHS_LIMBS: usize>(
&self,
exp: &Uint<RHS_LIMBS>,
) -> Self
pub const fn pow_vartime<const RHS_LIMBS: usize>( &self, exp: &Uint<RHS_LIMBS>, ) -> Self
Returns self^exp, where exp is a little-endian integer exponent.
This operation is variable time with respect to the exponent exp.
If the exponent is fixed, this operation is constant time.
Sourcepub const fn sqn_vartime(&self, n: usize) -> Self
pub const fn sqn_vartime(&self, n: usize) -> Self
Returns self^(2^n) mod p.
This operation is variable time with respect to the exponent n.
If the exponent is fixed, this operation is constant time.
Sourcepub fn invert(&self) -> CtOption<Self>
pub fn invert(&self) -> CtOption<Self>
Compute FieldElement inversion: 1 / self.
Sourcepub fn invert_vartime(&self) -> CtOption<Self>
pub fn invert_vartime(&self) -> CtOption<Self>
Compute FieldElement inversion: 1 / self in variable-time.
Sourceconst fn invert_unwrap(&self) -> Self
const fn invert_unwrap(&self) -> Self
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 const fn relax(&self) -> LooseFieldElement
pub 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 AsRef<fiat_p521_tight_field_element> for FieldElement
impl AsRef<fiat_p521_tight_field_element> for FieldElement
Source§fn as_ref(&self) -> &fiat_p521_tight_field_element
fn as_ref(&self) -> &fiat_p521_tight_field_element
Source§impl BatchInvert for FieldElement
impl BatchInvert for FieldElement
Source§fn batch_invert_in_place(
elements: &mut [Self],
scratch_space: &mut [Self],
) -> Self
fn batch_invert_in_place( elements: &mut [Self], scratch_space: &mut [Self], ) -> Self
elements (when non-zero). Zero-valued elements are
left as zero. Read moreSource§fn batch_invert_in_place_vartime(
elements: &mut [Self],
scratch_space: &mut [Self],
) -> Self
fn batch_invert_in_place_vartime( elements: &mut [Self], scratch_space: &mut [Self], ) -> Self
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 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 try_random<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error>
fn try_random<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error>
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 random<R>(rng: &mut R) -> Self
fn random<R>(rng: &mut R) -> 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 Generate for FieldElement
impl Generate for FieldElement
Source§fn try_generate_from_rng<R: TryRng + ?Sized>(
rng: &mut R,
) -> Result<Self, R::Error>
fn try_generate_from_rng<R: TryRng + ?Sized>( rng: &mut R, ) -> Result<Self, R::Error>
TryCryptoRng. Read moreSource§fn generate_from_rng<R>(rng: &mut R) -> Self
fn generate_from_rng<R>(rng: &mut R) -> Self
CryptoRng.Source§impl Invert for FieldElement
impl Invert for 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 = 520
const CAPACITY: u32 = 520
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 = Array<u8, <NistP521 as Curve>::FieldBytesSize>
type Repr = Array<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 PrimeFieldExt for FieldElement
impl PrimeFieldExt for FieldElement
Source§const REPR_ENDIANNESS: ByteOrder = ByteOrder::BigEndian
const REPR_ENDIANNESS: ByteOrder = ByteOrder::BigEndian
ff::PrimeField::Repr.Source§fn to_be_repr(&self) -> Self::Repr
fn to_be_repr(&self) -> Self::Repr
self using a big endian representation.Source§fn to_le_repr(&self) -> Self::Repr
fn to_le_repr(&self) -> Self::Repr
self using a little endian representation.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 Retrieve for FieldElement
impl Retrieve 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.