Trait ark_ff::prelude::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};
use ark_std::{test_rng, One, UniformRand, Zero};
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 Types§
sourcetype BigInt: BigInteger
type BigInt: BigInteger
A BigInteger
type that can represent elements of this field.
Required Associated Constants§
sourceconst MODULUS_MINUS_ONE_DIV_TWO: Self::BigInt
const MODULUS_MINUS_ONE_DIV_TWO: Self::BigInt
The value (p - 1)/ 2
.
sourceconst MODULUS_BIT_SIZE: u32
const MODULUS_BIT_SIZE: u32
The size of the modulus in bits.
sourceconst TRACE: Self::BigInt
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.
sourceconst TRACE_MINUS_ONE_DIV_TWO: Self::BigInt
const TRACE_MINUS_ONE_DIV_TWO: Self::BigInt
The value (t - 1)/ 2
.
Required Methods§
sourcefn from_bigint(repr: Self::BigInt) -> Option<Self>
fn from_bigint(repr: Self::BigInt) -> Option<Self>
Construct a prime field element from an integer in the range 0..(p - 1).
sourcefn into_bigint(self) -> Self::BigInt
fn into_bigint(self) -> Self::BigInt
Converts an element of the prime field into an integer in the range 0..(p - 1).
Provided Methods§
sourcefn from_be_bytes_mod_order(bytes: &[u8]) -> Self
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.
sourcefn from_le_bytes_mod_order(bytes: &[u8]) -> Self
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.