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§
Sourceconst MODULUS: &'static str
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.
Sourceconst CAPACITY: u32
const CAPACITY: u32
How many bits of information can be reliably stored in the field element.
This is usually Self::NUM_BITS - 1
.
Sourceconst MULTIPLICATIVE_GENERATOR: Self
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
.
Sourceconst S: u32
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
.
Sourceconst ROOT_OF_UNITY: Self
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
.
Sourceconst ROOT_OF_UNITY_INV: Self
const ROOT_OF_UNITY_INV: Self
Inverse of Self::ROOT_OF_UNITY
.
Sourceconst DELTA: Self
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§
Required Methods§
Sourcefn from_repr(repr: Self::Repr) -> CtOption<Self>
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
.
Provided Methods§
Sourcefn from_str_vartime(s: &str) -> Option<Self>
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.
Sourcefn from_u128(v: u128) -> Self
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.
Sourcefn from_repr_vartime(repr: Self::Repr) -> Option<Self>
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.
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
impl PrimeField for Fp2
const NUM_BITS: u32 = 381u32
const CAPACITY: u32 = 380u32
const MODULUS: &'static str = "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
const ROOT_OF_UNITY: Fp2 = _
const ROOT_OF_UNITY_INV: Fp2 = _
const DELTA: Fp2 = _
const TWO_INV: Fp2 = _
const S: u32 = 0u32
type Repr = Fp2Bytes
fn from_repr(r: <Fp2 as PrimeField>::Repr) -> CtOption<Fp2>
fn to_repr(&self) -> <Fp2 as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fp
impl PrimeField for Fp
const MODULUS: &'static str = "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
const NUM_BITS: u32 = 381u32
const CAPACITY: u32 = 380u32
const TWO_INV: Fp = TWO_INV
const MULTIPLICATIVE_GENERATOR: Fp = GENERATOR
const S: u32 = 1u32
const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV
const DELTA: Fp = DELTA
type Repr = ReprFp
fn from_repr(r: <Fp as PrimeField>::Repr) -> CtOption<Fp>
fn to_repr(&self) -> <Fp as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Scalar
impl PrimeField for Scalar
const MODULUS: &'static str = "0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
const NUM_BITS: u32 = 255u32
const CAPACITY: u32 = 254u32
const TWO_INV: Scalar = TWO_INV
const MULTIPLICATIVE_GENERATOR: Scalar = GENERATOR
const S: u32 = 32u32
const ROOT_OF_UNITY: Scalar = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Scalar = ROOT_OF_UNITY_INV
const DELTA: Scalar = DELTA
type Repr = [u8; 32]
fn from_repr(r: <Scalar as PrimeField>::Repr) -> CtOption<Scalar>
fn to_repr(&self) -> <Scalar as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fq2
impl PrimeField for Fq2
const MODULUS: &'static str = "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"
const MULTIPLICATIVE_GENERATOR: Fq2 = _
const NUM_BITS: u32 = 254u32
const CAPACITY: u32 = 253u32
const S: u32 = 0u32
const ROOT_OF_UNITY: Fq2 = _
const ROOT_OF_UNITY_INV: Fq2 = _
const DELTA: Fq2 = _
const TWO_INV: Fq2 = _
type Repr = Fq2Bytes
fn from_repr(repr: <Fq2 as PrimeField>::Repr) -> CtOption<Fq2>
fn to_repr(&self) -> <Fq2 as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fq
impl PrimeField for Fq
const NUM_BITS: u32 = 254u32
const CAPACITY: u32 = 253u32
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fq = MULTIPLICATIVE_GENERATOR
const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV
const TWO_INV: Fq = TWO_INV
const DELTA: Fq = DELTA
const S: u32 = 0u32
type Repr = [u8; 32]
fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>
fn to_repr(&self) -> <Fq as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fr
impl PrimeField for Fr
const NUM_BITS: u32 = 254u32
const CAPACITY: u32 = 253u32
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fr = GENERATOR
const ROOT_OF_UNITY: Fr = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fr = ROOT_OF_UNITY_INV
const TWO_INV: Fr = TWO_INV
const DELTA: Fr = DELTA
const S: u32 = 28u32
type Repr = [u8; 32]
fn from_repr(repr: <Fr as PrimeField>::Repr) -> CtOption<Fr>
fn to_repr(&self) -> <Fr as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fq
impl PrimeField for Fq
const MODULUS: &'static str = MODULUS_STR
const NUM_BITS: u32 = 256u32
const CAPACITY: u32 = 255u32
const TWO_INV: Fq = TWO_INV
const MULTIPLICATIVE_GENERATOR: Fq = MULTIPLICATIVE_GENERATOR
const S: u32 = 2u32
const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV
const DELTA: Fq = DELTA
type Repr = [u8; 32]
fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>
fn to_repr(&self) -> <Fq as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fr
impl PrimeField for Fr
const MODULUS: &'static str = MODULUS_STR
const NUM_BITS: u32 = 256u32
const CAPACITY: u32 = 255u32
const TWO_INV: Fr = TWO_INV
const MULTIPLICATIVE_GENERATOR: Fr = MULTIPLICATIVE_GENERATOR
const S: u32 = 2u32
const ROOT_OF_UNITY: Fr = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fr = ROOT_OF_UNITY_INV
const DELTA: Fr = DELTA
type Repr = [u8; 32]
fn from_repr(repr: <Fr as PrimeField>::Repr) -> CtOption<Fr>
fn to_repr(&self) -> <Fr as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fp
impl PrimeField for Fp
const NUM_BITS: u32 = 446u32
const CAPACITY: u32 = 445u32
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fp = GENERATOR
const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV
const TWO_INV: Fp = TWO_INV
const DELTA: Fp = DELTA
const S: u32 = 32u32
type Repr = FpRepr
fn from_repr(repr: <Fp as PrimeField>::Repr) -> CtOption<Fp>
fn to_repr(&self) -> <Fp as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fq
impl PrimeField for Fq
const NUM_BITS: u32 = 446u32
const CAPACITY: u32 = 445u32
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fq = GENERATOR
const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV
const TWO_INV: Fq = TWO_INV
const DELTA: Fq = DELTA
const S: u32 = 32u32
type Repr = FqRepr
fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>
fn to_repr(&self) -> <Fq as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fp
impl PrimeField for Fp
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fp = MULTIPLICATIVE_GENERATOR
const TWO_INV: Fp = TWO_INV
const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV
const DELTA: Fp = DELTA
const NUM_BITS: u32 = 256u32
const CAPACITY: u32 = 255u32
const S: u32 = 1u32
type Repr = [u8; 32]
fn from_repr(repr: <Fp as PrimeField>::Repr) -> CtOption<Fp>
fn to_repr(&self) -> <Fp as PrimeField>::Repr
fn from_u128(v: u128) -> Fp
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fq
impl PrimeField for Fq
const NUM_BITS: u32 = 256u32
const CAPACITY: u32 = 255u32
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fq = GENERATOR
const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV
const TWO_INV: Fq = TWO_INV
const DELTA: Fq = DELTA
const S: u32 = 6u32
type Repr = [u8; 32]
fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>
fn to_repr(&self) -> <Fq as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fp
impl PrimeField for Fp
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fp = MULTIPLICATIVE_GENERATOR
const TWO_INV: Fp = TWO_INV
const ROOT_OF_UNITY: Fp = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fp = ROOT_OF_UNITY_INV
const DELTA: Fp = DELTA
const NUM_BITS: u32 = 256u32
const CAPACITY: u32 = 255u32
const S: u32 = 1u32
type Repr = [u8; 32]
fn from_repr(repr: <Fp as PrimeField>::Repr) -> CtOption<Fp>
fn to_repr(&self) -> <Fp as PrimeField>::Repr
fn from_u128(v: u128) -> Fp
fn is_odd(&self) -> Choice
Source§impl PrimeField for Fq
impl PrimeField for Fq
const NUM_BITS: u32 = 256u32
const CAPACITY: u32 = 255u32
const MODULUS: &'static str = MODULUS_STR
const MULTIPLICATIVE_GENERATOR: Fq = GENERATOR
const ROOT_OF_UNITY: Fq = ROOT_OF_UNITY
const ROOT_OF_UNITY_INV: Fq = ROOT_OF_UNITY_INV
const TWO_INV: Fq = TWO_INV
const DELTA: Fq = DELTA
const S: u32 = 4u32
type Repr = [u8; 32]
fn from_repr(repr: <Fq as PrimeField>::Repr) -> CtOption<Fq>
fn to_repr(&self) -> <Fq as PrimeField>::Repr
fn is_odd(&self) -> Choice
Source§impl PrimeField for Scalar
impl PrimeField for Scalar
Source§fn from_repr(
bytes: GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>,
) -> CtOption<Scalar>
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).