ark_ff::fields

Trait PrimeField

Source
pub trait PrimeField:
    Field<BasePrimeField = Self>
    + FftField
    + FromStr
    + From<Self::BigInt>
    + Into<Self::BigInt>
    + From<BigUint>
    + Into<BigUint> {
    type BigInt: BigInteger;

    const MODULUS: Self::BigInt;
    const MODULUS_MINUS_ONE_DIV_TWO: Self::BigInt;
    const MODULUS_BIT_SIZE: u32;
    const TRACE: Self::BigInt;
    const TRACE_MINUS_ONE_DIV_TWO: Self::BigInt;

    // Required methods
    fn from_bigint(repr: Self::BigInt) -> Option<Self>;
    fn into_bigint(self) -> Self::BigInt;

    // Provided methods
    fn from_be_bytes_mod_order(bytes: &[u8]) -> Self { ... }
    fn from_le_bytes_mod_order(bytes: &[u8]) -> Self { ... }
}
Expand description

The interface for a prime field, i.e. the field of integers modulo a prime $p$. In the following example we’ll use the prime field underlying the BLS12-381 G1 curve.

use ark_ff::{BigInteger, Field, PrimeField, Zero};
use ark_std::{test_rng, One, UniformRand};
use ark_test_curves::bls12_381::Fq as F;

let mut rng = test_rng();
let a = F::rand(&mut rng);
// We can access the prime modulus associated with `F`:
let modulus = <F as PrimeField>::MODULUS;
assert_eq!(a.pow(&modulus), a); // the Euler-Fermat theorem tells us: a^{p-1} = 1 mod p

// We can convert field elements to integers in the range [0, MODULUS - 1]:
let one: num_bigint::BigUint = F::one().into();
assert_eq!(one, num_bigint::BigUint::one());

// We can construct field elements from an arbitrary sequence of bytes:
let n = F::from_le_bytes_mod_order(&modulus.to_bytes_le());
assert_eq!(n, F::zero());

Required Associated Constants§

Source

const MODULUS: Self::BigInt

The modulus p.

Source

const MODULUS_MINUS_ONE_DIV_TWO: Self::BigInt

The value (p - 1)/ 2.

Source

const MODULUS_BIT_SIZE: u32

The size of the modulus in bits.

Source

const TRACE: Self::BigInt

The trace of the field is defined as the smallest integer t such that by 2^s * t = p - 1, and t is coprime to 2.

Source

const TRACE_MINUS_ONE_DIV_TWO: Self::BigInt

The value (t - 1)/ 2.

Required Associated Types§

Source

type BigInt: BigInteger

A BigInteger type that can represent elements of this field.

Required Methods§

Source

fn from_bigint(repr: Self::BigInt) -> Option<Self>

Construct a prime field element from an integer in the range 0..(p - 1).

Source

fn into_bigint(self) -> Self::BigInt

Converts an element of the prime field into an integer in the range 0..(p - 1).

Provided Methods§

Source

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

Reads bytes in big-endian, and converts them to a field element. If the integer represented by bytes is larger than the modulus p, this method performs the appropriate reduction.

Source

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

Reads bytes in little-endian, and converts them to a field element. If the integer represented by bytes is larger than the modulus p, this method performs the appropriate reduction.

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.

Implementors§

Source§

impl<P: FpConfig<N>, const N: usize> PrimeField for Fp<P, N>