pub struct FieldElement<F: FieldInternal> {
field: F,
pub(super) value: F::ElementContainer,
}Expand description
An element of a field.
This contains the field itself so that it knows how to do arithmetic operations. We want this to be a struct rather than a trait, which means that we want the actual storage of the value to be managed by the field itself. Therefore, we have an internal field trait that knows about arithmetic operations and other implementation details, but these operations are only accessible from outside the crate using this struct.
It might seem wasteful to handle, say, FieldElement<Fp<P>>s rather than u32 in the API for
FqVector<Fp<P>>. However, this gives us type-level guarantees that the invariants of the
elements hold, i.e. in this case that its value is in the range 0..P. Moreover, this is bigger
than a bare F::ElementContainer only when the field has a positive memory footprint. The cases
we care most about, Fp<P2>, Fp<P3>, Fp<P5>, and Fp<P7>, are all ZSTs and therefore don’t
cause any overhead.
Fields§
§field: F§value: F::ElementContainerImplementations§
Source§impl<F: FieldInternal> FieldElement<F>
impl<F: FieldInternal> FieldElement<F>
Sourcepub(super) fn new(field: F, value: F::ElementContainer) -> Self
pub(super) fn new(field: F, value: F::ElementContainer) -> Self
Create a new field element. This is only visible to the field module, because the caller
is responsible for ensuring that the invariants of value hold.
Handling FieldElements in the API rather than the containers directly has the advantage of
being sure at compile-time that the invariants hold.
pub fn field(&self) -> F
pub(crate) fn val(self) -> F::ElementContainer
pub fn inv(self) -> Option<Self>
pub fn frobenius(self) -> Self
Trait Implementations§
Source§impl<F: Field> Add for FieldElement<F>
impl<F: Field> Add for FieldElement<F>
Source§impl<F: Field> AddAssign for FieldElement<F>
impl<F: Field> AddAssign for FieldElement<F>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<F: Clone + FieldInternal> Clone for FieldElement<F>where
F::ElementContainer: Clone,
impl<F: Clone + FieldInternal> Clone for FieldElement<F>where
F::ElementContainer: Clone,
Source§fn clone(&self) -> FieldElement<F>
fn clone(&self) -> FieldElement<F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<F: Debug + FieldInternal> Debug for FieldElement<F>where
F::ElementContainer: Debug,
impl<F: Debug + FieldInternal> Debug for FieldElement<F>where
F::ElementContainer: Debug,
Source§impl<F: FieldInternal> Deref for FieldElement<F>
impl<F: FieldInternal> Deref for FieldElement<F>
Source§type Target = <F as FieldInternal>::ElementContainer
type Target = <F as FieldInternal>::ElementContainer
Source§impl<F: Field> Display for FieldElement<F>
impl<F: Field> Display for FieldElement<F>
Source§impl<F: Field> Div for FieldElement<F>
impl<F: Field> Div for FieldElement<F>
Source§impl<P: Prime> From<FieldElement<Fp<P>>> for u32
impl<P: Prime> From<FieldElement<Fp<P>>> for u32
Source§fn from(element: FieldElement<Fp<P>>) -> Self
fn from(element: FieldElement<Fp<P>>) -> Self
Source§impl<F: Hash + FieldInternal> Hash for FieldElement<F>where
F::ElementContainer: Hash,
impl<F: Hash + FieldInternal> Hash for FieldElement<F>where
F::ElementContainer: Hash,
Source§impl<F: Field> Mul for FieldElement<F>
impl<F: Field> Mul for FieldElement<F>
Source§impl<F: Field> MulAssign for FieldElement<F>
impl<F: Field> MulAssign for FieldElement<F>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<F: Field> Neg for FieldElement<F>
impl<F: Field> Neg for FieldElement<F>
Source§impl<F: PartialEq + FieldInternal> PartialEq for FieldElement<F>where
F::ElementContainer: PartialEq,
impl<F: PartialEq + FieldInternal> PartialEq for FieldElement<F>where
F::ElementContainer: PartialEq,
Source§impl<F: Field> Sub for FieldElement<F>
impl<F: Field> Sub for FieldElement<F>
Source§impl<F: Field> SubAssign for FieldElement<F>
impl<F: Field> SubAssign for FieldElement<F>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreimpl<F: Copy + FieldInternal> Copy for FieldElement<F>where
F::ElementContainer: Copy,
impl<F: Eq + FieldInternal> Eq for FieldElement<F>where
F::ElementContainer: Eq,
impl<F: FieldInternal> StructuralPartialEq for FieldElement<F>
Auto Trait Implementations§
impl<F> Freeze for FieldElement<F>
impl<F> RefUnwindSafe for FieldElement<F>
impl<F> Send for FieldElement<F>
impl<F> Sync for FieldElement<F>
impl<F> Unpin for FieldElement<F>
impl<F> UnwindSafe for FieldElement<F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more