Skip to main content

BFieldElement

Struct BFieldElement 

Source
pub struct BFieldElement(/* private fields */);
Expand description

Base field element ∈ ℤ_{2^64 - 2^32 + 1}.

In Montgomery representation. This implementation follows https://eprint.iacr.org/2022/274.pdf and https://github.com/novifinancial/winterfell/pull/101/files.

Implementations§

Source§

impl BFieldElement

Source

pub const BYTES: usize = 8

Source

pub const P: u64 = 0xffff_ffff_0000_0001

The base field’s prime, i.e., 2^64 - 2^32 + 1.

Source

pub const MAX: u64

Source

pub const MINUS_TWO_INVERSE: Self

-2^-1

Source

pub const fn new(value: u64) -> Self

Source

pub const fn value(&self) -> u64

Source

pub fn inverse(&self) -> Self

Source

pub const fn power_accumulator<const N: usize, const M: usize>( base: [Self; N], tail: [Self; N], ) -> [Self; N]

Square the base M times and multiply the result by the tail value

Source

pub const fn generator() -> Self

A generator for the entire base field.

Source

pub const fn lift(&self) -> XFieldElement

Turn this base field element into the corresponding extension field element.

Source

pub fn increment(&mut self)

Add BFieldElement::ONE to self.

Source

pub fn decrement(&mut self)

Subtract BFieldElement::ONE from self.

Source

pub const fn mod_pow(&self, exp: u64) -> Self

Source

pub const fn montyred(x: u128) -> u64

Montgomery reduction

Source

pub const fn raw_bytes(&self) -> [u8; 8]

Return the raw bytes or 8-bit chunks of the Montgomery representation, in little-endian byte order

Source

pub const fn from_raw_bytes(bytes: &[u8; 8]) -> Self

Take a slice of 8 bytes and interpret it as an integer in little-endian byte order, and cast it to a BFieldElement in Montgomery representation

Source

pub const fn raw_u16s(&self) -> [u16; 4]

Return the raw 16-bit chunks of the Montgomery representation, in little-endian chunk order

Source

pub const fn from_raw_u16s(chunks: &[u16; 4]) -> Self

Take a slice of 4 16-bit chunks and interpret it as an integer in little-endian chunk order, and cast it to a BFieldElement in Montgomery representation

Source

pub fn raw_u128(&self) -> u128

Source

pub const fn from_raw_u64(e: u64) -> BFieldElement

Source

pub const fn raw_u64(&self) -> u64

Source

pub const fn is_canonical(x: u64) -> bool

Trait Implementations§

Source§

impl Add for BFieldElement

Source§

type Output = BFieldElement

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<BFieldElement> for XFieldElement

Source§

type Output = XFieldElement

The resulting type after applying the + operator.
Source§

fn add(self, other: BFieldElement) -> Self

Performs the + operation. Read more
Source§

impl Add<XFieldElement> for BFieldElement

The bfe + xfe -> xfe instance belongs to BFieldElement.

Source§

type Output = XFieldElement

The resulting type after applying the + operator.
Source§

fn add(self, other: XFieldElement) -> XFieldElement

Performs the + operation. Read more
Source§

impl AddAssign for BFieldElement

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl AddAssign<BFieldElement> for XFieldElement

Source§

fn add_assign(&mut self, rhs: BFieldElement)

Performs the += operation. Read more
Source§

impl<'a> Arbitrary<'a> for BFieldElement

Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl BFieldCodec for BFieldElement

Source§

type Error = BFieldCodecError

Source§

fn decode(sequence: &[BFieldElement]) -> Result<Box<Self>, Self::Error>

Source§

fn encode(&self) -> Vec<BFieldElement>

Source§

fn static_length() -> Option<usize>

Returns the length in number of BFieldElements if it is known at compile-time. Otherwise, None.
Source§

impl Clone for BFieldElement

Source§

fn clone(&self) -> BFieldElement

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl ConstOne for BFieldElement

Source§

const ONE: Self

The multiplicative identity element of Self, 1.
Source§

impl ConstZero for BFieldElement

Source§

const ZERO: Self

The additive identity element of Self, 0.
Source§

impl Copy for BFieldElement

Source§

impl CyclicGroupGenerator for BFieldElement

Source§

impl Debug for BFieldElement

Source§

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

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

impl Default for BFieldElement

Source§

fn default() -> BFieldElement

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for BFieldElement

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for BFieldElement

Source§

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

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

impl Distribution<BFieldElement> for StandardUniform

Source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BFieldElement

Generate a random value of T, using rng as the source of randomness.
Source§

fn sample_iter<R>(self, rng: R) -> Iter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
Source§

fn map<F, S>(self, func: F) -> Map<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Map sampled values to type S Read more
Source§

impl Div for BFieldElement

Source§

type Output = BFieldElement

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self

Performs the / operation. Read more
Source§

impl Eq for BFieldElement

Source§

impl FiniteField for BFieldElement

Source§

fn batch_inversion(input: Vec<Self>) -> Vec<Self>

Montgomery Batch Inversion
Source§

fn square(self) -> Self

Source§

impl From<&BFieldElement> for u64

Source§

fn from(elem: &BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<&BFieldElement> for u128

Source§

fn from(elem: &BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<&BFieldElement> for i128

Source§

fn from(elem: &BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<&BFieldElement> for i64

Source§

fn from(elem: &BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<BFieldElement> for u64

Source§

fn from(elem: BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<BFieldElement> for u128

Source§

fn from(elem: BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<BFieldElement> for i128

Source§

fn from(elem: BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<BFieldElement> for i64

Source§

fn from(elem: BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<BFieldElement> for [u8; 8]

Convert a B-field element to a byte array. The client uses this for its database.

Source§

fn from(bfe: BFieldElement) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for BFieldElement

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for BFieldElement

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for BFieldElement

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for BFieldElement

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for BFieldElement

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for BFieldElement

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for BFieldElement

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for BFieldElement

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for BFieldElement

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for BFieldElement

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for BFieldElement

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl FromStr for BFieldElement

Source§

type Err = ParseBFieldElementError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl GetSize for BFieldElement

Source§

fn get_heap_size(&self) -> usize

Determines how many bytes this object occupies inside the heap. Read more
Source§

fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>( &self, tracker: TRACKER, ) -> (usize, TRACKER)

Determines how many bytes this object occupies inside the heap while using a tracker. Read more
Source§

fn get_stack_size() -> usize

Determines how may bytes this object occupies inside the stack. Read more
Source§

fn get_size(&self) -> usize

Determines the total size of the object. Read more
Source§

fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)
where T: GetSizeTracker,

Determines the total size of the object while using a tracker. Read more
Source§

impl Hash for BFieldElement

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 Inverse for BFieldElement

Source§

fn inverse(&self) -> Self

The multiplicative inverse: a * a.inverse() == 1 Read more
Source§

fn inverse_or_zero(&self) -> Self

Source§

impl LowerHex for BFieldElement

Source§

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

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

impl ModPowU32 for BFieldElement

Source§

fn mod_pow_u32(&self, exp: u32) -> Self

Source§

impl ModPowU64 for BFieldElement

Source§

fn mod_pow_u64(&self, pow: u64) -> Self

Source§

impl Mul for BFieldElement

Source§

type Output = BFieldElement

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<BFieldElement> for XFieldElement

XField * BField means scalar multiplication of the BFieldElement onto each coefficient of the XField.

Source§

type Output = XFieldElement

The resulting type after applying the * operator.
Source§

fn mul(self, other: BFieldElement) -> Self

Performs the * operation. Read more
Source§

impl<FF, FF2> Mul<Polynomial<'_, FF>> for BFieldElement
where FF: FiniteField + Mul<BFieldElement, Output = FF2>, FF2: 'static + FiniteField,

Source§

type Output = Polynomial<'static, FF2>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Polynomial<'_, FF>) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<XFieldElement> for BFieldElement

Source§

type Output = XFieldElement

The resulting type after applying the * operator.
Source§

fn mul(self, other: XFieldElement) -> XFieldElement

Performs the * operation. Read more
Source§

impl MulAssign for BFieldElement

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl MulAssign<BFieldElement> for XFieldElement

Source§

fn mul_assign(&mut self, rhs: BFieldElement)

Performs the *= operation. Read more
Source§

impl Neg for BFieldElement

Source§

type Output = BFieldElement

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl One for BFieldElement

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn is_one(&self) -> bool

Returns true if self is equal to the multiplicative identity. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

impl PartialEq for BFieldElement

Source§

fn eq(&self, other: &BFieldElement) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PrimitiveRootOfUnity for BFieldElement

Source§

impl Serialize for BFieldElement

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for BFieldElement

Source§

impl Sub for BFieldElement

Source§

type Output = BFieldElement

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<BFieldElement> for XFieldElement

Source§

type Output = XFieldElement

The resulting type after applying the - operator.
Source§

fn sub(self, other: BFieldElement) -> Self

Performs the - operation. Read more
Source§

impl Sub<XFieldElement> for BFieldElement

Source§

type Output = XFieldElement

The resulting type after applying the - operator.
Source§

fn sub(self, other: XFieldElement) -> XFieldElement

Performs the - operation. Read more
Source§

impl SubAssign for BFieldElement

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl SubAssign<BFieldElement> for XFieldElement

Source§

fn sub_assign(&mut self, rhs: BFieldElement)

Performs the -= operation. Read more
Source§

impl Sum for BFieldElement

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl TryFrom<&BFieldElement> for u8

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&BFieldElement> for i8

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&BFieldElement> for u16

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&BFieldElement> for i16

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&BFieldElement> for u32

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&BFieldElement> for i32

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&BFieldElement> for usize

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&BFieldElement> for isize

Source§

type Error = TryFromIntError

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

fn try_from(value: &BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&[u8]> for BFieldElement

Source§

type Error = ParseBFieldElementError

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

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for u8

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for i8

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for u16

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for i16

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for u32

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for i32

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for usize

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BFieldElement> for isize

Source§

type Error = TryFromIntError

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

fn try_from(value: BFieldElement) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<[u8; 8]> for BFieldElement

Source§

type Error = ParseBFieldElementError

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

fn try_from(array: [u8; 8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl UpperHex for BFieldElement

Source§

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

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

impl Zero for BFieldElement

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.