pub struct FieldElement(/* private fields */);Expand description
Represents a field element of the base field (Fq) in the World ID Protocol.
The World ID Protocol uses the BabyJubJub curve throughout. Note the
base field of BabyJubJub is the scalar field of the BN254 curve.
This wrapper ensures consistent serialization and deserialization of field elements, where string-based serialization is done with hex encoding and binary serialization is done with byte vectors.
Implementations§
Source§impl FieldElement
impl FieldElement
Sourcepub fn to_be_bytes(&self) -> [u8; 32]
pub fn to_be_bytes(&self) -> [u8; 32]
Returns the 32-byte big-endian representation of this field element.
Sourcepub fn from_be_bytes(be_bytes: &[u8; 32]) -> Result<Self, PrimitiveError>
pub fn from_be_bytes(be_bytes: &[u8; 32]) -> Result<Self, PrimitiveError>
Constructs a field element from a 32-byte big-endian representation.
Unlike from_be_bytes_mod_order, this rejects values >= the field modulus.
§Errors
Returns PrimitiveError::NotInField if the value is >= the field modulus.
Sourcepub fn from_arbitrary_raw_bytes(bytes: &[u8]) -> Self
pub fn from_arbitrary_raw_bytes(bytes: &[u8]) -> Self
Takes arbitrary raw bytes, hashes them with a byte-friendly gas-efficient hash function and reduces it to a field element.
Trait Implementations§
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 Debug for FieldElement
impl Debug for FieldElement
Source§impl Default for FieldElement
impl Default for FieldElement
Source§fn default() -> FieldElement
fn default() -> FieldElement
Source§impl Deref for FieldElement
impl Deref for FieldElement
Source§impl DerefMut for FieldElement
impl DerefMut for FieldElement
Source§impl<'de> Deserialize<'de> for FieldElement
impl<'de> Deserialize<'de> for FieldElement
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for FieldElement
impl Display for FieldElement
Source§impl From<FieldElement> for U256
impl From<FieldElement> for U256
Source§fn from(value: FieldElement) -> Self
fn from(value: FieldElement) -> Self
Source§impl From<Fp<MontBackend<FrConfig, 4>, 4>> for FieldElement
impl From<Fp<MontBackend<FrConfig, 4>, 4>> for FieldElement
Source§impl From<RpId> for FieldElement
impl From<RpId> for FieldElement
Source§impl From<Uint<160, 3>> for FieldElement
impl From<Uint<160, 3>> for FieldElement
Source§impl From<u128> for FieldElement
impl From<u128> for FieldElement
Source§impl From<u64> for FieldElement
impl From<u64> for FieldElement
Source§impl FromStr for FieldElement
impl FromStr for FieldElement
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses a field element from a hex string (with optional “0x” prefix).
The value must be lower than the modulus and specifically for string encoding, proper padding is enforced (strictly 32 bytes). This is because some values in the Protocol are meant to be enforced uniqueness with, and this reduces the possibility of accidental string non-collisions.
Source§type Err = PrimitiveError
type Err = PrimitiveError
Source§impl Hash for FieldElement
impl Hash for FieldElement
Source§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 Serialize for FieldElement
impl Serialize for FieldElement
Source§impl TryFrom<Uint<256, 4>> for FieldElement
impl TryFrom<Uint<256, 4>> for FieldElement
impl Copy for FieldElement
impl Eq for FieldElement
impl StructuralPartialEq for FieldElement
Auto Trait Implementations§
impl Freeze for FieldElement
impl RefUnwindSafe for FieldElement
impl Send for FieldElement
impl Sync for FieldElement
impl Unpin for FieldElement
impl UnsafeUnpin for FieldElement
impl UnwindSafe for FieldElement
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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