Skip to main content

Field

Trait Field 

Source
pub trait Field:
    'static
    + Debug
    + Default
    + Sized
    + Send
    + Sync
    + Copy
    + Clone
    + Eq
    + Ord
    + ConstantTimeEq
    + ConstantTimeGreater
    + ConstantTimeLess
    + ConditionallySelectable
    + Add<Output = Self>
    + for<'a> Add<&'a Self, Output = Self>
    + AddAssign<Self>
    + for<'a> AddAssign<&'a Self>
    + Neg<Output = Self>
    + Sub<Output = Self>
    + for<'a> Sub<&'a Self, Output = Self>
    + SubAssign<Self>
    + for<'a> SubAssign<&'a Self>
    + Mul<Output = Self>
    + for<'a> Mul<&'a Self, Output = Self>
    + MulAssign<Self>
    + for<'a> MulAssign<&'a Self>
    + Div<Output = Self>
    + for<'a> Div<&'a Self, Output = Self>
    + DivAssign<Self>
    + for<'a> DivAssign<&'a Self>
    + Sum
    + Product
    + Display
    + Binary
    + Octal
    + LowerHex
    + UpperHex
    + FromStr
    + From<u8>
    + From<u16>
    + TryFrom<usize, Error: Debug> {
    const LEN: usize;
    const ZERO: Self;
    const ONE: Self;
    const MAX: Self;
    const NUM_BITS: usize = _;
    const BITS: usize = Self::NUM_BITS;
Show 30 methods // Required methods fn is_odd(&self) -> Choice; fn try_random<R: TryCryptoRng>(rng: &mut R) -> Result<Self, R::Error>; fn random<R: CryptoRng>(rng: &mut R) -> Self; fn random_default() -> Self; fn invert(&self) -> CtOption<Self>; fn invert_vartime(&self) -> Option<Self>; fn pow(self, exp: Self) -> Self; fn pow_vartime(self, exp: Self) -> Self; fn div_int(&self, rhs: &Self) -> (Self, Self); fn try_from_le_bytes(bytes: &[u8]) -> CtOption<Self>; fn try_from_be_bytes(bytes: &[u8]) -> CtOption<Self>; fn from_str_radix(s: &str, radix: usize) -> Result<Self, Error>; fn to_str_radix( &self, radix: usize, pad_to: usize, upper_case: bool, ) -> String; fn try_to_u8(&self) -> Option<u8>; fn try_to_u16(&self) -> Option<u16>; // Provided methods fn zero() -> Self { ... } fn one() -> Self { ... } fn is_zero(&self) -> Choice { ... } fn is_even(&self) -> Choice { ... } fn double(&self) -> Self { ... } fn square(&self) -> Self { ... } fn cube(&self) -> Self { ... } fn invert_unwrap(&self) -> Self { ... } fn invert_or_zero(&self) -> Self { ... } fn pow_small(self, exp: usize) -> Self { ... } fn pow_small_vartime(self, exp: usize) -> Self { ... } fn pow_u32(self, exp: u32) -> Self { ... } fn pow_u32_vartime(self, exp: u32) -> Self { ... } fn pow_u64(self, exp: u64) -> Self { ... } fn pow_u64_vartime(self, exp: u64) -> Self { ... }
}
Expand description

A finite field.

NOTE: this is assumed to be a numeric finite field, ie. a range of the integers from 0 inclusive to N exclusive, with N being the cardinality of the field. For this reason this trait inherits several traits that are not necessarily part of algebraic fields, such as Ord for total ordering and several std::fmt::* traits for formatting numbers.

Required Associated Constants§

Source

const LEN: usize

The number of bytes required to represent a value.

Source

const ZERO: Self

The additive identity element.

Source

const ONE: Self

The multiplicative identity element.

Source

const MAX: Self

The largest value in the field.

Provided Associated Constants§

Source

const NUM_BITS: usize = _

The number of bits required to represent a value.

Source

const BITS: usize = Self::NUM_BITS

Same as Self::NUM_BITS. Provided for consistency with native Rust types.

Required Methods§

Source

fn is_odd(&self) -> Choice

Returns true iff the value is odd.

Source

fn try_random<R: TryCryptoRng>(rng: &mut R) -> Result<Self, R::Error>

Picks a uniformly distributed random scalar securely from the provided fallible CSPRNG.

Source

fn random<R: CryptoRng>(rng: &mut R) -> Self

Picks a uniformly distributed random scalar securely from the provided infallible CSPRNG.

Source

fn random_default() -> Self

Picks a uniformly distributed random scalar securely from the system’s default CSPRNG.

Source

fn invert(&self) -> CtOption<Self>

Returns the modular inverse of self, or Noneifself` is zero.

Source

fn invert_vartime(&self) -> Option<Self>

Returns the modular inverse of self, or Noneifself` is zero.

Source

fn pow(self, exp: Self) -> Self

Raises this value to exp, running exactly Self::NUM_BITS squares and multiplications so that a time observer cannot infer the exponent.

Source

fn pow_vartime(self, exp: Self) -> Self

Raises this value to exp.

Source

fn div_int(&self, rhs: &Self) -> (Self, Self)

Performs integer division by rhs and returns a (quotient, remainder) pair. Panics if rhs is zero.

Source

fn try_from_le_bytes(bytes: &[u8]) -> CtOption<Self>

Constructs a scalar from the little-endian byte representation of an integer.

The provided slice must have exactly Self::LEN bytes.

The function returns None if the integer lies outside the field range.

Source

fn try_from_be_bytes(bytes: &[u8]) -> CtOption<Self>

Constructs a scalar from the big-endian byte representation of an integer.

The provided slice must have exactly Self::LEN bytes.

The function returns None if the integer lies outside the field range.

Source

fn from_str_radix(s: &str, radix: usize) -> Result<Self, Error>

Parses a scalar from its text representation in the given radix.

Returns an error on invalid format or overflow. Panics if radix is less than 2 or greater than 36.

Source

fn to_str_radix(&self, radix: usize, pad_to: usize, upper_case: bool) -> String

Converts a scalar to its textual representation in the given radix.

The returned string will have at least pad_to characters, and will be padded with zeros if necessary.

When radix is greater than 10, the representation will start using alphabetic characters from A to Z for digits greater than 9, e.g. character A to F for hexadecimal numbers. The upper_case flag specifies whether those characters must be lower case or upper case.

This function panics if radix is less than 2 or greater than 36.

Source

fn try_to_u8(&self) -> Option<u8>

Returns this scalar as a u8, or None if the value exceeds the 8-bit range.

Source

fn try_to_u16(&self) -> Option<u16>

Returns this scalar as a u16, or None if the value exceeds the 16-bit range.

Provided Methods§

Source

fn zero() -> Self

Returns the element zero.

Source

fn one() -> Self

Returns the element one.

Source

fn is_zero(&self) -> Choice

Compares with zero.

Source

fn is_even(&self) -> Choice

Returns true iff the value is even.

Source

fn double(&self) -> Self

Returns this value doubled. self remains unchanged.

Source

fn square(&self) -> Self

Returns this value squared. self remains unchanged.

Source

fn cube(&self) -> Self

Returns this value raised to 3. self remains unchanged.

Source

fn invert_unwrap(&self) -> Self

Returns the modular inverse of self, assuming self is not zero and panicking otherwise.

Source

fn invert_or_zero(&self) -> Self

Returns the modular inverse of self, or zero if self is zero.

Source

fn pow_small(self, exp: usize) -> Self

Raises this value to exp, running exactly usize::BITS squares and multiplications so that a time observer cannot infer the exponent.

Unlike Field::pow, exp is a usize. That makes the algorithm significantly faster because the square-and-multiply loop runs only usize::BITS times rather than Field::BITS times, and bitwise operations on the exponent are native.

Source

fn pow_small_vartime(self, exp: usize) -> Self

Raises this value to exp.

Unlike Field::pow, exp is a usize. That makes the algorithm significantly faster because bitwise operations on the exponent are native.

Source

fn pow_u32(self, exp: u32) -> Self

Raises this value to exp, running exactly u32::BITS squares and multiplications so that an observer cannot infer the exponent.

Unlike Field::pow, exp is a u32. That makes the algorithm significantly faster because the square-and-multiply loop runs only 32 times rather than Field::BITS times, and bitwise operations on the exponent are native.

Source

fn pow_u32_vartime(self, exp: u32) -> Self

Raises this value to exp.

Unlike Field::pow_vartime, exp is a u32. That makes the algorithm significantly faster because bitwise operations on the exponent are native.

Source

fn pow_u64(self, exp: u64) -> Self

Raises this value to exp, running exactly u64::BITS squares and multiplications so that an observer cannot infer the exponent.

Unlike Field::pow, exp is a u64. That makes the algorithm significantly faster because the square-and-multiply loop runs only 64 times rather than Field::BITS times, and bitwise operations on the exponent are native.

Source

fn pow_u64_vartime(self, exp: u64) -> Self

Raises this value to exp.

Unlike Field::pow_vartime, exp is a u64. That makes the algorithm significantly faster because bitwise operations on the exponent are native.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Field for Scalar

Source§

const LEN: usize = 32

Source§

const ZERO: Self

Source§

const ONE: Self = Self::R

Source§

const MAX: Self