FieldElement

Struct FieldElement 

Source
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::ElementContainer

Implementations§

Source§

impl<F: FieldInternal> FieldElement<F>

Source

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.

Source

pub fn field(&self) -> F

Source

pub(crate) fn val(self) -> F::ElementContainer

Source

pub fn inv(self) -> Option<Self>

Source

pub fn frobenius(self) -> Self

Trait Implementations§

Source§

impl<F: Field> Add for FieldElement<F>

Source§

type Output = FieldElement<F>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<F: Field> AddAssign for FieldElement<F>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<F: Clone + FieldInternal> Clone for FieldElement<F>

Source§

fn clone(&self) -> FieldElement<F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug + FieldInternal> Debug for FieldElement<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: FieldInternal> Deref for FieldElement<F>

Source§

type Target = <F as FieldInternal>::ElementContainer

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<F: Field> Display for FieldElement<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Field> Div for FieldElement<F>

Source§

type Output = Option<FieldElement<F>>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<P: Prime> From<FieldElement<Fp<P>>> for u32

Source§

fn from(element: FieldElement<Fp<P>>) -> Self

Converts to this type from the input type.
Source§

impl<F: Hash + FieldInternal> Hash for FieldElement<F>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<F: Field> Mul for FieldElement<F>

Source§

type Output = FieldElement<F>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<F: Field> MulAssign for FieldElement<F>

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<F: Field> Neg for FieldElement<F>

Source§

type Output = FieldElement<F>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<F: PartialEq + FieldInternal> PartialEq for FieldElement<F>

Source§

fn eq(&self, other: &FieldElement<F>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F: Field> Sub for FieldElement<F>

Source§

type Output = FieldElement<F>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<F: Field> SubAssign for FieldElement<F>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<F: Copy + FieldInternal> Copy for FieldElement<F>

Source§

impl<F: Eq + FieldInternal> Eq for FieldElement<F>
where F::ElementContainer: Eq,

Source§

impl<F: FieldInternal> StructuralPartialEq for FieldElement<F>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V