pub struct FieldElement(pub(super) MontyFieldElement<FieldParams, { FieldParams::LIMBS }>);Expand description
Element in the finite field modulo p = 2^{384} − 2^{128} − 2^{96} + 2^{32} − 1.
§Trait impls
Much of the important functionality is provided by traits from the [ff] crate:
- [
Field] represents elements of finite fields and provides:- [
Field::random] generate a random field element double,square, andinvertoperations- Bounds for [
Add], [Sub], [Mul], and [Neg] (and*Assignequivalents) - Bounds for [
ConditionallySelectable] from thesubtlecrate
- [
PrimeFieldrepresents elements of prime fields and provides:from_repr/to_reprfor converting field elements from/to big integers.MULTIPLICATIVE_GENERATORandROOT_OF_UNITYconstants.
Please see the documentation for the relevant traits for more information.
Tuple Fields§
§0: MontyFieldElement<FieldParams, { FieldParams::LIMBS }>Implementations§
Source§impl FieldElement
impl FieldElement
Sourcepub fn from_bytes(
repr: &MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>,
) -> CtOption<Self>
pub fn from_bytes( repr: &MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>, ) -> CtOption<Self>
Create a FieldElement from a canonical byte representation using the field’s configured byte order.
Sourcepub fn from_slice(slice: &[u8]) -> Option<Self>
pub fn from_slice(slice: &[u8]) -> Option<Self>
Decode FieldElement from a byte slice using the field’s configured byte order.
Sourcepub const fn from_hex_vartime(hex: &str) -> Self
pub const fn from_hex_vartime(hex: &str) -> Self
Decode a FieldElement from hex-encoded bytes using the field’s configured byte order.
This is primarily intended for defining constants using hex literals.
§Panics
- When hex is malformed
- When input is the wrong length
- If input overflows the modulus
Sourcepub fn from_uint(uint: &U384) -> CtOption<Self>
pub fn from_uint(uint: &U384) -> CtOption<Self>
Decode FieldElement
from U384 converting it into Montgomery form:
w * R^2 * R^-1 mod p = wR mod pSourcepub const fn from_u64(w: u64) -> Self
pub const fn from_u64(w: u64) -> Self
Convert a u64 into a FieldElement.
Sourcepub fn to_bytes(self) -> MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>
pub fn to_bytes(self) -> MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>
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 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.
Source§impl FieldElement
impl FieldElement
Sourcepub(crate) const fn from_uint_unchecked(w: U384) -> Self
pub(crate) const fn from_uint_unchecked(w: U384) -> Self
Decode FieldElement from U384 converting it into Montgomery form.
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_canonical(self) -> U384
pub const fn to_canonical(self) -> U384
Translate FieldElement out of the Montgomery domain, returning a U384 in canonical form.
Sourcepub fn invert_vartime(&self) -> CtOption<Self>
pub fn invert_vartime(&self) -> CtOption<Self>
Compute
FieldElement
inversion: 1 / self in variable-time.
Sourcepub const fn const_invert(&self) -> Self
pub const fn const_invert(&self) -> Self
Compute field inversion as a const fn. Panics if self is zero.
This is mainly intended for inverting constants at compile time.
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 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 ConstMontyParams<{ <$params>::LIMBS }> for FieldElement
impl ConstMontyParams<{ <$params>::LIMBS }> for FieldElement
Source§impl ConstantTimeEq for FieldElement
impl ConstantTimeEq for FieldElement
Source§impl ConstantTimeGreater for FieldElement
impl ConstantTimeGreater for FieldElement
Source§impl ConstantTimeLess for FieldElement
impl ConstantTimeLess for FieldElement
Source§impl CtEq for FieldElement
impl CtEq for FieldElement
Source§impl CtGt for FieldElement
impl CtGt for FieldElement
Source§impl CtLt for FieldElement
impl CtLt for FieldElement
Source§impl CtSelect for FieldElement
impl CtSelect for FieldElement
Source§impl Debug for FieldElement
impl Debug for FieldElement
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 MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>
impl From<&FieldElement> for MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>
Source§fn from(fe: &FieldElement) -> Self
fn from(fe: &FieldElement) -> Self
Source§impl From<&FieldElement> for U384
impl From<&FieldElement> for U384
Source§fn from(fe: &FieldElement) -> U384
fn from(fe: &FieldElement) -> U384
Source§impl From<FieldElement> for MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>
impl From<FieldElement> for MontyFieldBytes<FieldParams, { FieldParams::LIMBS }>
Source§fn from(fe: FieldElement) -> Self
fn from(fe: FieldElement) -> Self
Source§impl From<FieldElement> for MontyFieldElement<FieldParams, { FieldParams::LIMBS }>
impl From<FieldElement> for MontyFieldElement<FieldParams, { FieldParams::LIMBS }>
Source§fn from(
fe: FieldElement,
) -> MontyFieldElement<FieldParams, { FieldParams::LIMBS }>
fn from( fe: FieldElement, ) -> MontyFieldElement<FieldParams, { FieldParams::LIMBS }>
Source§impl From<FieldElement> for U384
impl From<FieldElement> for U384
Source§fn from(fe: FieldElement) -> U384
fn from(fe: FieldElement) -> U384
Source§impl From<MontyFieldElement<FieldParams, { <$params>::LIMBS }>> for FieldElement
impl From<MontyFieldElement<FieldParams, { <$params>::LIMBS }>> for FieldElement
Source§fn from(
fe: MontyFieldElement<FieldParams, { FieldParams::LIMBS }>,
) -> FieldElement
fn from( fe: MontyFieldElement<FieldParams, { FieldParams::LIMBS }>, ) -> 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: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Self, R::Error>
fn try_generate_from_rng<R: TryCryptoRng + ?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 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 Ord for FieldElement
impl Ord for FieldElement
Source§fn cmp(&self, other: &FieldElement) -> Ordering
fn cmp(&self, other: &FieldElement) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for FieldElement
impl PartialEq for FieldElement
Source§impl PartialOrd for FieldElement
impl PartialOrd for FieldElement
Source§impl PrimeField for FieldElement
impl PrimeField for FieldElement
Source§const MODULUS: &'static str
const MODULUS: &'static str
Source§const NUM_BITS: u32 = ::primefield::MontyFieldElement<FieldParams, { FieldParams::LIMBS }>::NUM_BITS
const NUM_BITS: u32 = ::primefield::MontyFieldElement<FieldParams, { FieldParams::LIMBS }>::NUM_BITS
Source§const CAPACITY: u32 = ::primefield::MontyFieldElement<FieldParams, { FieldParams::LIMBS }>::CAPACITY
const CAPACITY: u32 = ::primefield::MontyFieldElement<FieldParams, { FieldParams::LIMBS }>::CAPACITY
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, <FieldParams as MontyFieldParams<{ <$params>::LIMBS }>>::ByteSize>
type Repr = Array<u8, <FieldParams as MontyFieldParams<{ <$params>::LIMBS }>>::ByteSize>
Source§fn from_repr(bytes: Self::Repr) -> CtOption<Self>
fn from_repr(bytes: Self::Repr) -> CtOption<Self>
Source§fn to_repr(&self) -> Self::Repr
fn to_repr(&self) -> Self::Repr
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
const REPR_ENDIANNESS: ByteOrder
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.