This commit is contained in:
wborgeaud 2021-12-13 14:35:05 +01:00
parent aed4de0293
commit e6c3f35431
7 changed files with 23 additions and 22 deletions

View File

@ -50,8 +50,6 @@ impl<F: Extendable<2>> From<F> for QuadraticExtension<F> {
}
impl<F: Extendable<2>> Field for QuadraticExtension<F> {
type PrimeField = F;
const ZERO: Self = Self([F::ZERO; 2]);
const ONE: Self = Self([F::ONE, F::ZERO]);
const TWO: Self = Self([F::TWO, F::ZERO]);
@ -63,6 +61,7 @@ impl<F: Extendable<2>> Field for QuadraticExtension<F> {
// long as `F::TWO_ADICITY >= 2`, `p` can be written as `4n + 1`, so `p + 1` can be written as
// `2(2n + 1)`, which has a 2-adicity of 1.
const TWO_ADICITY: usize = F::TWO_ADICITY + 1;
const CHARACTERISTIC_TWO_ADICITY: usize = F::CHARACTERISTIC_TWO_ADICITY;
const MULTIPLICATIVE_GROUP_GENERATOR: Self = Self(F::EXT_MULTIPLICATIVE_GROUP_GENERATOR);
const POWER_OF_TWO_GENERATOR: Self = Self(F::EXT_POWER_OF_TWO_GENERATOR);

View File

@ -51,8 +51,6 @@ impl<F: Extendable<4>> From<F> for QuarticExtension<F> {
}
impl<F: Extendable<4>> Field for QuarticExtension<F> {
type PrimeField = F;
const ZERO: Self = Self([F::ZERO; 4]);
const ONE: Self = Self([F::ONE, F::ZERO, F::ZERO, F::ZERO]);
const TWO: Self = Self([F::TWO, F::ZERO, F::ZERO, F::ZERO]);
@ -65,6 +63,7 @@ impl<F: Extendable<4>> Field for QuarticExtension<F> {
// `2(2n + 1)`, which has a 2-adicity of 1. A similar argument can show that `p^2 + 1` also has
// a 2-adicity of 1.
const TWO_ADICITY: usize = F::TWO_ADICITY + 2;
const CHARACTERISTIC_TWO_ADICITY: usize = F::CHARACTERISTIC_TWO_ADICITY;
const MULTIPLICATIVE_GROUP_GENERATOR: Self = Self(F::EXT_MULTIPLICATIVE_GROUP_GENERATOR);
const POWER_OF_TWO_GENERATOR: Self = Self(F::EXT_POWER_OF_TWO_GENERATOR);

View File

@ -42,8 +42,6 @@ pub trait Field:
+ Serialize
+ DeserializeOwned
{
type PrimeField: PrimeField;
const ZERO: Self;
const ONE: Self;
const TWO: Self;
@ -54,6 +52,9 @@ pub trait Field:
/// The 2-adicity of this field's multiplicative group.
const TWO_ADICITY: usize;
/// The 2-adicity of this field's multiplicative group.
const CHARACTERISTIC_TWO_ADICITY: usize;
/// Generator of the entire multiplicative group, i.e. all non-zero elements.
const MULTIPLICATIVE_GROUP_GENERATOR: Self;
/// Generator of a multiplicative subgroup of order `2^TWO_ADICITY`.
@ -212,17 +213,17 @@ pub trait Field:
// TWO_ADICITY. Can remove the branch and simplify if that
// saving isn't worth it.
if exp > Self::PrimeField::TWO_ADICITY {
if exp > Self::CHARACTERISTIC_TWO_ADICITY {
// NB: This should be a compile-time constant
let inverse_2_pow_adicity: Self =
Self::from_canonical_u64(p - ((p - 1) >> Self::PrimeField::TWO_ADICITY));
Self::from_canonical_u64(p - ((p - 1) >> Self::CHARACTERISTIC_TWO_ADICITY));
let mut res = inverse_2_pow_adicity;
let mut e = exp - Self::PrimeField::TWO_ADICITY;
let mut e = exp - Self::CHARACTERISTIC_TWO_ADICITY;
while e > Self::PrimeField::TWO_ADICITY {
while e > Self::CHARACTERISTIC_TWO_ADICITY {
res *= inverse_2_pow_adicity;
e -= Self::PrimeField::TWO_ADICITY;
e -= Self::CHARACTERISTIC_TWO_ADICITY;
}
res * Self::from_canonical_u64(p - ((p - 1) >> e))
} else {
@ -404,7 +405,7 @@ pub trait Field:
}
/// A finite field of prime order less than 2^64.
pub trait PrimeField: Field<PrimeField = Self> {
pub trait PrimeField: Field {
const ORDER: u64;
/// The number of bits required to encode any field element.
@ -449,6 +450,15 @@ pub trait PrimeField: Field<PrimeField = Self> {
}
}
pub trait SmallCharacteristicField: Field {
const SMALLCHAR: u64;
#[inline]
fn inverse_2exp(exp: usize) -> Self {
todo!()
}
}
/// An iterator over the powers of a certain base element `b`: `b^0, b^1, b^2, ...`.
#[derive(Clone)]
pub struct Powers<F: Field> {

View File

@ -62,8 +62,6 @@ impl Debug for GoldilocksField {
}
impl Field for GoldilocksField {
type PrimeField = Self;
const ZERO: Self = Self(0);
const ONE: Self = Self(1);
const TWO: Self = Self(2);
@ -71,6 +69,7 @@ impl Field for GoldilocksField {
const CHARACTERISTIC: u64 = Self::ORDER;
const TWO_ADICITY: usize = 32;
const CHARACTERISTIC_TWO_ADICITY: usize = Self::TWO_ADICITY;
// Sage: `g = GF(p).multiplicative_generator()`
const MULTIPLICATIVE_GROUP_GENERATOR: Self = Self(7);

View File

@ -39,7 +39,6 @@ where
Self::Scalar: Sub<Self, Output = Self>,
{
type Scalar: Field;
type PackedPrimeField: PackedField<Scalar = <Self::Scalar as Field>::PrimeField>;
const WIDTH: usize;
const ZERO: Self;
@ -102,7 +101,6 @@ where
unsafe impl<F: Field> PackedField for F {
type Scalar = Self;
type PackedPrimeField = F::PrimeField;
const WIDTH: usize = 1;
const ZERO: Self = <F as Field>::ZERO;

View File

@ -68,9 +68,6 @@ impl Debug for Secp256K1Base {
}
impl Field for Secp256K1Base {
// TODO: fix
type PrimeField = GoldilocksField;
const ZERO: Self = Self([0; 4]);
const ONE: Self = Self([1, 0, 0, 0]);
const TWO: Self = Self([2, 0, 0, 0]);
@ -84,6 +81,7 @@ impl Field for Secp256K1Base {
// TODO: fix
const CHARACTERISTIC: u64 = 0;
const TWO_ADICITY: usize = 1;
const CHARACTERISTIC_TWO_ADICITY: usize = Self::TWO_ADICITY;
// Sage: `g = GF(p).multiplicative_generator()`
const MULTIPLICATIVE_GROUP_GENERATOR: Self = Self([5, 0, 0, 0]);

View File

@ -71,9 +71,6 @@ impl Debug for Secp256K1Scalar {
}
impl Field for Secp256K1Scalar {
// TODO: fix
type PrimeField = GoldilocksField;
const ZERO: Self = Self([0; 4]);
const ONE: Self = Self([1, 0, 0, 0]);
const TWO: Self = Self([2, 0, 0, 0]);
@ -88,6 +85,7 @@ impl Field for Secp256K1Scalar {
const CHARACTERISTIC: u64 = 0;
const TWO_ADICITY: usize = 6;
const CHARACTERISTIC_TWO_ADICITY: usize = 6;
// Sage: `g = GF(p).multiplicative_generator()`
const MULTIPLICATIVE_GROUP_GENERATOR: Self = Self([7, 0, 0, 0]);