From e6c3f354313b57e1b85e85d36635a49da235bc40 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 13 Dec 2021 14:35:05 +0100 Subject: [PATCH] working --- src/field/extension_field/quadratic.rs | 3 +-- src/field/extension_field/quartic.rs | 3 +-- src/field/field_types.rs | 26 ++++++++++++++++++-------- src/field/goldilocks_field.rs | 3 +-- src/field/packed_field.rs | 2 -- src/field/secp256k1_base.rs | 4 +--- src/field/secp256k1_scalar.rs | 4 +--- 7 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/field/extension_field/quadratic.rs b/src/field/extension_field/quadratic.rs index b724095a..dfb861c2 100644 --- a/src/field/extension_field/quadratic.rs +++ b/src/field/extension_field/quadratic.rs @@ -50,8 +50,6 @@ impl> From for QuadraticExtension { } impl> Field for QuadraticExtension { - 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> Field for QuadraticExtension { // 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); diff --git a/src/field/extension_field/quartic.rs b/src/field/extension_field/quartic.rs index 0d221401..1a34d40a 100644 --- a/src/field/extension_field/quartic.rs +++ b/src/field/extension_field/quartic.rs @@ -51,8 +51,6 @@ impl> From for QuarticExtension { } impl> Field for QuarticExtension { - 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> Field for QuarticExtension { // `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); diff --git a/src/field/field_types.rs b/src/field/field_types.rs index a3affc13..dec22c9d 100644 --- a/src/field/field_types.rs +++ b/src/field/field_types.rs @@ -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 { +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 { } } +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 { diff --git a/src/field/goldilocks_field.rs b/src/field/goldilocks_field.rs index 058b6db8..7bdd3c77 100644 --- a/src/field/goldilocks_field.rs +++ b/src/field/goldilocks_field.rs @@ -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); diff --git a/src/field/packed_field.rs b/src/field/packed_field.rs index f2b0c83e..00b99d6c 100644 --- a/src/field/packed_field.rs +++ b/src/field/packed_field.rs @@ -39,7 +39,6 @@ where Self::Scalar: Sub, { type Scalar: Field; - type PackedPrimeField: PackedField::PrimeField>; const WIDTH: usize; const ZERO: Self; @@ -102,7 +101,6 @@ where unsafe impl PackedField for F { type Scalar = Self; - type PackedPrimeField = F::PrimeField; const WIDTH: usize = 1; const ZERO: Self = ::ZERO; diff --git a/src/field/secp256k1_base.rs b/src/field/secp256k1_base.rs index b3fb0148..32615187 100644 --- a/src/field/secp256k1_base.rs +++ b/src/field/secp256k1_base.rs @@ -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]); diff --git a/src/field/secp256k1_scalar.rs b/src/field/secp256k1_scalar.rs index f4f2e6ab..44907b7a 100644 --- a/src/field/secp256k1_scalar.rs +++ b/src/field/secp256k1_scalar.rs @@ -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]);