openzeppelin_crypto/field/
prime.rs

1//! This module provides a generic interface for finite prime fields.
2
3use crate::{arithmetic::BigInteger, field::Field};
4
5/// Defines an abstract prime field.
6/// I.e., the field of integers of prime module [`Self::MODULUS`].
7pub trait PrimeField:
8    Field + From<<Self as PrimeField>::BigInt> + Into<<Self as PrimeField>::BigInt>
9{
10    /// A `BigInteger` type that can represent elements of this field.
11    type BigInt: BigInteger;
12
13    /// The modulus `p`.
14    const MODULUS: Self::BigInt;
15
16    /// The size of the modulus in bits.
17    const MODULUS_BIT_SIZE: usize;
18
19    /// [`Self::MODULUS`] has a spare bit in the most significant limb.
20    const HAS_MODULUS_SPARE_BIT: bool;
21
22    /// Returns the characteristic of the field,
23    /// in little-endian representation.
24    #[must_use]
25    fn characteristic() -> Self::BigInt {
26        Self::MODULUS
27    }
28
29    /// Construct a prime field element from a big integer.
30    fn from_bigint(repr: Self::BigInt) -> Self;
31
32    /// Converts an element of the prime field into an integer less than
33    /// [`Self::MODULUS`].
34    fn into_bigint(self) -> Self::BigInt;
35}