ring_math

Trait FieldElement

source
pub trait FieldElement:
    Add<Output = Self>
    + AddAssign
    + Div<Output = Self>
    + Mul<Output = Self>
    + MulAssign
    + Neg<Output = Self>
    + Sub<Output = Self>
    + SubAssign
    + FromStr
    + PartialEq
    + Clone
    + Hash
    + Debug
    + From<u64>
    + Display {
Show 17 methods // Required methods fn byte_len() -> usize; fn serialize(&self) -> String; fn deserialize(str: &str) -> Self; fn name_str() -> &'static str; fn from_bytes_le(bytes: &[u8]) -> Self; fn to_bytes_le(&self) -> Vec<u8>; // Provided methods fn zero() -> Self { ... } fn one() -> Self { ... } fn sample_rand<R>(src: &mut R) -> Self where R: Rng { ... } fn prime() -> BigUint { ... } fn from_usize(value: usize) -> Self { ... } fn to_biguint(&self) -> BigUint { ... } fn from_biguint(v: &BigUint) -> Self { ... } fn lower60_string(&self) -> String { ... } fn log_floor(&self, b: Self) -> u32 { ... } fn legendre(&self) -> i32 { ... } fn sqrt(&self) -> Self { ... }
}
Expand description

A generic representation of a scalar finite field element. For use in internal module logic. Supports field operations using builtin operators (*-+/) and other convenience traits. Handles serialization and deserialization to a reasonable string representation.

Required Methods§

source

fn byte_len() -> usize

Minimum number of bytes needed to represent an element.

source

fn serialize(&self) -> String

Get a valid string representation of the element.

source

fn deserialize(str: &str) -> Self

Parse an element from a supposedly valid string representation.

source

fn name_str() -> &'static str

A short string identifier for the field.

source

fn from_bytes_le(bytes: &[u8]) -> Self

Parse an element from a byte representation. Panics if the byte representation is too long. e.g. if the bytes represent a value > Self::prime().

source

fn to_bytes_le(&self) -> Vec<u8>

Convert a field element to a byte representation. The number of bytes may be variable, but is guaranteed to be accepted by from_bytes_le for the same curve.

Provided Methods§

source

fn zero() -> Self

Get the zero element.

source

fn one() -> Self

Get the one element.

source

fn sample_rand<R>(src: &mut R) -> Self
where R: Rng,

Sample a random element from the field using a supplied source of randomness. Requires the random feature to be enabled.

source

fn prime() -> BigUint

The prime modulus of the field as an arbitrary precision integer.

source

fn from_usize(value: usize) -> Self

Parse an element from a usize throws if the field size is smaller than the usize on the machine.

source

fn to_biguint(&self) -> BigUint

Get a num_bigint::BigUint representation for arbitrary precision operations.

source

fn from_biguint(v: &BigUint) -> Self

Convert a num_bigint::BigUint into a field element precision operations. Numbers will be converted % self.prime()

source

fn lower60_string(&self) -> String

A string representation of a field element using only the lower 60 bits of the element. A normal decimal representation will be given if it’s shorter than the lower 60 bit representation. This is a lossy representation.

source

fn log_floor(&self, b: Self) -> u32

Take a logarithm using a custom base and return the floored value. O(logb(n)) time complexity where n is the size of the element.

source

fn legendre(&self) -> i32

Calculate the legendre symbol for a field element. Used to determine if the element is a quadratic residue.

source

fn sqrt(&self) -> Self

Kumar 08 prime field square root implementation. Always returns the smaller root e.g. the positive root.

Object Safety§

This trait is not object safe.

Implementors§