snark_verifier::util::arithmetic

Trait PrimeField

Source
pub trait PrimeField: Field + From<u64> {
    type Repr: Copy + Default + Send + Sync + 'static + AsRef<[u8]> + AsMut<[u8]>;

    const MODULUS: &'static str;
    const NUM_BITS: u32;
    const CAPACITY: u32;
    const TWO_INV: Self;
    const MULTIPLICATIVE_GENERATOR: Self;
    const S: u32;
    const ROOT_OF_UNITY: Self;
    const ROOT_OF_UNITY_INV: Self;
    const DELTA: Self;

    // Required methods
    fn from_repr(repr: Self::Repr) -> CtOption<Self>;
    fn to_repr(&self) -> Self::Repr;
    fn is_odd(&self) -> Choice;

    // Provided methods
    fn from_str_vartime(s: &str) -> Option<Self> { ... }
    fn from_u128(v: u128) -> Self { ... }
    fn from_repr_vartime(repr: Self::Repr) -> Option<Self> { ... }
    fn is_even(&self) -> Choice { ... }
}
Expand description

This represents an element of a non-binary prime field.

Required Associated Constants§

Source

const MODULUS: &'static str

Modulus of the field written as a string for debugging purposes.

The encoding of the modulus is implementation-specific. Generic users of the PrimeField trait should treat this string as opaque.

Source

const NUM_BITS: u32

How many bits are needed to represent an element of this field.

Source

const CAPACITY: u32

How many bits of information can be reliably stored in the field element.

This is usually Self::NUM_BITS - 1.

Source

const TWO_INV: Self

Inverse of $2$ in the field.

Source

const MULTIPLICATIVE_GENERATOR: Self

A fixed multiplicative generator of modulus - 1 order. This element must also be a quadratic nonresidue.

It can be calculated using SageMath as GF(modulus).primitive_element().

Implementations of this trait MUST ensure that this is the generator used to derive Self::ROOT_OF_UNITY.

Source

const S: u32

An integer s satisfying the equation 2^s * t = modulus - 1 with t odd.

This is the number of leading zero bits in the little-endian bit representation of modulus - 1.

Source

const ROOT_OF_UNITY: Self

The 2^s root of unity.

It can be calculated by exponentiating Self::MULTIPLICATIVE_GENERATOR by t, where t = (modulus - 1) >> Self::S.

Source

const ROOT_OF_UNITY_INV: Self

Inverse of Self::ROOT_OF_UNITY.

Source

const DELTA: Self

Generator of the t-order multiplicative subgroup.

It can be calculated by exponentiating Self::MULTIPLICATIVE_GENERATOR by 2^s, where s is Self::S.

Required Associated Types§

Source

type Repr: Copy + Default + Send + Sync + 'static + AsRef<[u8]> + AsMut<[u8]>

The prime field can be converted back and forth into this binary representation.

Required Methods§

Source

fn from_repr(repr: Self::Repr) -> CtOption<Self>

Attempts to convert a byte representation of a field element into an element of this prime field, failing if the input is not canonical (is not smaller than the field’s modulus).

The byte representation is interpreted with the same endianness as elements returned by PrimeField::to_repr.

Source

fn to_repr(&self) -> Self::Repr

Converts an element of the prime field into the standard byte representation for this field.

The endianness of the byte representation is implementation-specific. Generic encodings of field elements should be treated as opaque.

Source

fn is_odd(&self) -> Choice

Returns true iff this element is odd.

Provided Methods§

Source

fn from_str_vartime(s: &str) -> Option<Self>

Interpret a string of numbers as a (congruent) prime field element. Does not accept unnecessary leading zeroes or a blank string.

§Security

This method provides no constant-time guarantees.

Source

fn from_u128(v: u128) -> Self

Obtains a field element congruent to the integer v.

For fields where Self::CAPACITY >= 128, this is injective and will produce a unique field element.

For fields where Self::CAPACITY < 128, this is surjective; some field elements will be produced by multiple values of v.

If you want to deterministically sample a field element representing a value, use FromUniformBytes instead.

Source

fn from_repr_vartime(repr: Self::Repr) -> Option<Self>

Attempts to convert a byte representation of a field element into an element of this prime field, failing if the input is not canonical (is not smaller than the field’s modulus).

The byte representation is interpreted with the same endianness as elements returned by PrimeField::to_repr.

§Security

This method provides no constant-time guarantees. Implementors of the PrimeField trait may optimise this method using non-constant-time logic.

Source

fn is_even(&self) -> Choice

Returns true iff this element is even.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl PrimeField for Fp2

Source§

const NUM_BITS: u32 = 381u32

Source§

const CAPACITY: u32 = 380u32

Source§

const MODULUS: &'static str = "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"

Source§

const ROOT_OF_UNITY: Fp2 = _

Source§

const ROOT_OF_UNITY_INV: Fp2 = _

Source§

const DELTA: Fp2 = _

Source§

const TWO_INV: Fp2 = _

Source§

const S: u32 = 0u32

Source§

type Repr = Fp2Bytes

Source§

fn from_repr(r: <Fp2 as PrimeField>::Repr) -> CtOption<Fp2>

Source§

fn to_repr(&self) -> <Fp2 as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fp

Source§

const MODULUS: &'static str = "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"

Source§

const NUM_BITS: u32 = 381u32

Source§

const CAPACITY: u32 = 380u32

Source§

const TWO_INV: Fp = TWO_INV

Source§

const MULTIPLICATIVE_GENERATOR: Fp = GENERATOR

Source§

const S: u32 = 1u32

Source§

const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV

Source§

const DELTA: Fp = DELTA

Source§

type Repr = ReprFp

Source§

fn from_repr(r: <Fp as PrimeField>::Repr) -> CtOption<Fp>

Source§

fn to_repr(&self) -> <Fp as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Scalar

Source§

const MODULUS: &'static str = "0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"

Source§

const NUM_BITS: u32 = 255u32

Source§

const CAPACITY: u32 = 254u32

Source§

const TWO_INV: Scalar = TWO_INV

Source§

const MULTIPLICATIVE_GENERATOR: Scalar = GENERATOR

Source§

const S: u32 = 32u32

Source§

const ROOT_OF_UNITY: Scalar = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Scalar = ROOT_OF_UNITY_INV

Source§

const DELTA: Scalar = DELTA

Source§

type Repr = [u8; 32]

Source§

fn from_repr(r: <Scalar as PrimeField>::Repr) -> CtOption<Scalar>

Source§

fn to_repr(&self) -> <Scalar as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fq2

Source§

const MODULUS: &'static str = "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"

Source§

const MULTIPLICATIVE_GENERATOR: Fq2 = _

Source§

const NUM_BITS: u32 = 254u32

Source§

const CAPACITY: u32 = 253u32

Source§

const S: u32 = 0u32

Source§

const ROOT_OF_UNITY: Fq2 = _

Source§

const ROOT_OF_UNITY_INV: Fq2 = _

Source§

const DELTA: Fq2 = _

Source§

const TWO_INV: Fq2 = _

Source§

type Repr = Fq2Bytes

Source§

fn from_repr(repr: <Fq2 as PrimeField>::Repr) -> CtOption<Fq2>

Source§

fn to_repr(&self) -> <Fq2 as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fq

Source§

const NUM_BITS: u32 = 254u32

Source§

const CAPACITY: u32 = 253u32

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fq = MULTIPLICATIVE_GENERATOR

Source§

const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV

Source§

const TWO_INV: Fq = TWO_INV

Source§

const DELTA: Fq = DELTA

Source§

const S: u32 = 0u32

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>

Source§

fn to_repr(&self) -> <Fq as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fr

Source§

const NUM_BITS: u32 = 254u32

Source§

const CAPACITY: u32 = 253u32

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fr = GENERATOR

Source§

const ROOT_OF_UNITY: Fr = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fr = ROOT_OF_UNITY_INV

Source§

const TWO_INV: Fr = TWO_INV

Source§

const DELTA: Fr = DELTA

Source§

const S: u32 = 28u32

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fr as PrimeField>::Repr) -> CtOption<Fr>

Source§

fn to_repr(&self) -> <Fr as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fq

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const NUM_BITS: u32 = 256u32

Source§

const CAPACITY: u32 = 255u32

Source§

const TWO_INV: Fq = TWO_INV

Source§

const MULTIPLICATIVE_GENERATOR: Fq = MULTIPLICATIVE_GENERATOR

Source§

const S: u32 = 2u32

Source§

const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV

Source§

const DELTA: Fq = DELTA

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>

Source§

fn to_repr(&self) -> <Fq as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fr

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const NUM_BITS: u32 = 256u32

Source§

const CAPACITY: u32 = 255u32

Source§

const TWO_INV: Fr = TWO_INV

Source§

const MULTIPLICATIVE_GENERATOR: Fr = MULTIPLICATIVE_GENERATOR

Source§

const S: u32 = 2u32

Source§

const ROOT_OF_UNITY: Fr = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fr = ROOT_OF_UNITY_INV

Source§

const DELTA: Fr = DELTA

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fr as PrimeField>::Repr) -> CtOption<Fr>

Source§

fn to_repr(&self) -> <Fr as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fp

Source§

const NUM_BITS: u32 = 446u32

Source§

const CAPACITY: u32 = 445u32

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fp = GENERATOR

Source§

const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV

Source§

const TWO_INV: Fp = TWO_INV

Source§

const DELTA: Fp = DELTA

Source§

const S: u32 = 32u32

Source§

type Repr = FpRepr

Source§

fn from_repr(repr: <Fp as PrimeField>::Repr) -> CtOption<Fp>

Source§

fn to_repr(&self) -> <Fp as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fq

Source§

const NUM_BITS: u32 = 446u32

Source§

const CAPACITY: u32 = 445u32

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fq = GENERATOR

Source§

const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV

Source§

const TWO_INV: Fq = TWO_INV

Source§

const DELTA: Fq = DELTA

Source§

const S: u32 = 32u32

Source§

type Repr = FqRepr

Source§

fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>

Source§

fn to_repr(&self) -> <Fq as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fp

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fp = MULTIPLICATIVE_GENERATOR

Source§

const TWO_INV: Fp = TWO_INV

Source§

const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV

Source§

const DELTA: Fp = DELTA

Source§

const NUM_BITS: u32 = 256u32

Source§

const CAPACITY: u32 = 255u32

Source§

const S: u32 = 1u32

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fp as PrimeField>::Repr) -> CtOption<Fp>

Source§

fn to_repr(&self) -> <Fp as PrimeField>::Repr

Source§

fn from_u128(v: u128) -> Fp

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fq

Source§

const NUM_BITS: u32 = 256u32

Source§

const CAPACITY: u32 = 255u32

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fq = GENERATOR

Source§

const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV

Source§

const TWO_INV: Fq = TWO_INV

Source§

const DELTA: Fq = DELTA

Source§

const S: u32 = 6u32

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>

Source§

fn to_repr(&self) -> <Fq as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fp

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fp = MULTIPLICATIVE_GENERATOR

Source§

const TWO_INV: Fp = TWO_INV

Source§

const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV

Source§

const DELTA: Fp = DELTA

Source§

const NUM_BITS: u32 = 256u32

Source§

const CAPACITY: u32 = 255u32

Source§

const S: u32 = 1u32

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fp as PrimeField>::Repr) -> CtOption<Fp>

Source§

fn to_repr(&self) -> <Fp as PrimeField>::Repr

Source§

fn from_u128(v: u128) -> Fp

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fq

Source§

const NUM_BITS: u32 = 256u32

Source§

const CAPACITY: u32 = 255u32

Source§

const MODULUS: &'static str = MODULUS_STR

Source§

const MULTIPLICATIVE_GENERATOR: Fq = GENERATOR

Source§

const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV

Source§

const TWO_INV: Fq = TWO_INV

Source§

const DELTA: Fq = DELTA

Source§

const S: u32 = 4u32

Source§

type Repr = [u8; 32]

Source§

fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>

Source§

fn to_repr(&self) -> <Fq as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Scalar

Source§

fn from_repr( bytes: GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>, ) -> CtOption<Scalar>

Attempts to parse the given byte array as an SEC1-encoded scalar.

Returns None if the byte array does not contain a big-endian integer in the range [0, p).

Source§

const MODULUS: &'static str = ORDER_HEX

Source§

const NUM_BITS: u32 = 256u32

Source§

const CAPACITY: u32 = 255u32

Source§

const TWO_INV: Scalar = _

Source§

const MULTIPLICATIVE_GENERATOR: Scalar = _

Source§

const S: u32 = 6u32

Source§

const ROOT_OF_UNITY: Scalar = _

Source§

const ROOT_OF_UNITY_INV: Scalar = _

Source§

const DELTA: Scalar = _

Source§

type Repr = GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>

Source§

fn to_repr(&self) -> GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fp

Source§

const MODULUS: &'static str = "0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001"

Source§

const TWO_INV: Fp = _

Source§

const NUM_BITS: u32 = 255u32

Source§

const CAPACITY: u32 = 254u32

Source§

const MULTIPLICATIVE_GENERATOR: Fp = GENERATOR

Source§

const S: u32 = 32u32

Source§

const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fp = _

Source§

const DELTA: Fp = DELTA

Source§

type Repr = [u8; 32]

Source§

fn from_u128(v: u128) -> Fp

Source§

fn from_repr(repr: <Fp as PrimeField>::Repr) -> CtOption<Fp>

Source§

fn to_repr(&self) -> <Fp as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Source§

impl PrimeField for Fq

Source§

const MODULUS: &'static str = "0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001"

Source§

const NUM_BITS: u32 = 255u32

Source§

const CAPACITY: u32 = 254u32

Source§

const TWO_INV: Fq = _

Source§

const MULTIPLICATIVE_GENERATOR: Fq = GENERATOR

Source§

const S: u32 = 32u32

Source§

const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY

Source§

const ROOT_OF_UNITY_INV: Fq = _

Source§

const DELTA: Fq = DELTA

Source§

type Repr = [u8; 32]

Source§

fn from_u128(v: u128) -> Fq

Source§

fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>

Source§

fn to_repr(&self) -> <Fq as PrimeField>::Repr

Source§

fn is_odd(&self) -> Choice

Implementors§