pub struct Elem(/* private fields */);
Expand description
The BabyBear class is an element of the finite field F_p, where P is the prime number 15*2^27 + 1. Put another way, Fp is basically integer arithmetic modulo P.
The Fp
datatype is the core type of all of the operations done within the
zero knowledge proofs, and is the smallest ‘addressable’ datatype, and the
base type of which all composite types are built. In many ways, one can
imagine it as the word size of a very strange architecture.
This specific prime P was chosen to:
- Be less than 2^31 so that it fits within a 32 bit word and doesn’t overflow on addition.
- Otherwise have as large a power of 2 in the factors of P-1 as possible.
This last property is useful for number theoretical transforms (the fast fourier transform equivalent on finite fields). See risc0_zkp::core::ntt for details.
The Fp class wraps all the standard arithmetic operations to make the finite field elements look basically like ordinary numbers (which they mostly are).
Implementations§
Source§impl Elem
impl Elem
Trait Implementations§
Source§impl AddAssign<Elem> for ExtElem
impl AddAssign<Elem> for ExtElem
Source§fn add_assign(&mut self, rhs: Elem)
fn add_assign(&mut self, rhs: Elem)
Promoting addition case for BabyBear Elem
Source§impl AddAssign for Elem
impl AddAssign for Elem
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Simple addition case for Baby Bear Elem
Source§impl CheckedBitPattern for Elem
impl CheckedBitPattern for Elem
Source§fn is_valid_bit_pattern(bits: &u32) -> bool
fn is_valid_bit_pattern(bits: &u32) -> bool
Checks that the u32 is less than the modulus.
Source§type Bits = u32
type Bits = u32
Self
must have the same layout as the specified Bits
except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern
.Source§impl Elem for Elem
impl Elem for Elem
Source§fn inv(self) -> Self
fn inv(self) -> Self
Compute the multiplicative inverse of x
, or 1 / x
in finite field
terms. Since we know by Fermat’s Little Theorem that
x ^ (P - 1) == 1 % P
for any x != 0
,
it follows that x * x ^ (P - 2) == 1 % P
for x != 0
.
That is, x ^ (P - 2)
is the multiplicative inverse of x
.
Note that if computed this way, the inverse of zero comes out as zero,
which we allow because it is convenient in many cases.
Source§const INVALID: Self
const INVALID: Self
Source§fn to_u32_words(&self) -> Vec<u32>
fn to_u32_words(&self) -> Vec<u32>
Source§fn from_u32_words(val: &[u32]) -> Self
fn from_u32_words(val: &[u32]) -> Self
Source§fn is_valid(&self) -> bool
fn is_valid(&self) -> bool
Source§fn is_reduced(&self) -> bool
fn is_reduced(&self) -> bool
Source§fn valid_or_zero(&self) -> Self
fn valid_or_zero(&self) -> Self
Source§fn ensure_valid(&self) -> &Self
fn ensure_valid(&self) -> &Self
Source§fn ensure_reduced(&self) -> &Self
fn ensure_reduced(&self) -> &Self
Source§fn as_u32_slice(elems: &[Self]) -> &[u32]
fn as_u32_slice(elems: &[Self]) -> &[u32]
Source§fn as_u32_slice_unchecked(elems: &[Self]) -> &[u32]
fn as_u32_slice_unchecked(elems: &[Self]) -> &[u32]
Source§fn from_u32_slice(u32s: &[u32]) -> &[Self]
fn from_u32_slice(u32s: &[u32]) -> &[Self]
Source§impl MulAssign<Elem> for ExtElem
impl MulAssign<Elem> for ExtElem
Source§fn mul_assign(&mut self, rhs: Elem)
fn mul_assign(&mut self, rhs: Elem)
Simple multiplication case by a Baby Bear Elem
Source§impl MulAssign for Elem
impl MulAssign for Elem
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Simple multiplication case for Baby Bear Elem
Source§impl Ord for Elem
impl Ord for Elem
Source§impl PartialOrd for Elem
impl PartialOrd for Elem
Source§impl RootsOfUnity for Elem
impl RootsOfUnity for Elem
Source§impl SubAssign<Elem> for ExtElem
impl SubAssign<Elem> for ExtElem
Source§fn sub_assign(&mut self, rhs: Elem)
fn sub_assign(&mut self, rhs: Elem)
Promoting subtraction case for BabyBear Elem
Source§impl SubAssign for Elem
impl SubAssign for Elem
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Simple subtraction case for Baby Bear Elem
impl Copy for Elem
impl Eq for Elem
impl NoUninit for Elem
Auto Trait Implementations§
impl Freeze for Elem
impl RefUnwindSafe for Elem
impl Send for Elem
impl Sync for Elem
impl Unpin for Elem
impl UnwindSafe for Elem
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,
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