Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/field/src/aes_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ mul_by_binary_field_1b!(AESTowerField8b);
impl_arithmetic_using_packed!(AESTowerField8b);

impl TowerField for AESTowerField8b {
type Canonical = AESTowerField8b;

fn min_tower_level(self) -> usize {
match self {
Self::ZERO | Self::ONE => 0,
Expand Down
46 changes: 4 additions & 42 deletions crates/field/src/arch/x86_64/gfni/gfni_arithmetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,12 @@ use crate::{
x86_64::{m128::m128_from_u128, simd::simd_arithmetic::TowerSimdType},
},
arithmetic_traits::{TaggedInvertOrZero, TaggedMul, TaggedPackedTransformationFactory},
is_aes_tower, is_canonical_tower,
is_aes_tower,
linear_transformation::{FieldLinearTransformation, Transformation},
packed::PackedBinaryField,
underlier::{Divisible, UnderlierType, WithUnderlier},
};

#[rustfmt::skip]
pub(super) const TOWER_TO_AES_MAP: i64 = u64::from_le_bytes([
0b00111110,
0b10011000,
0b01001110,
0b10010110,
0b11101010,
0b01101010,
0b01010000,
0b00110001,
]) as i64;

#[rustfmt::skip]
pub(super) const AES_TO_TOWER_MAP: i64 = u64::from_le_bytes([
0b00001100,
0b01110000,
0b10100010,
0b01110010,
0b00111110,
0b10000110,
0b11101000,
0b11010001,
]) as i64;

#[rustfmt::skip]
pub const IDENTITY_MAP: i64 = u64::from_le_bytes([
0b10000000,
Expand All @@ -60,12 +36,6 @@ pub(super) trait GfniType: Copy + TowerSimdType {
fn gf2p8affineinv_epi64_epi8(x: Self, a: Self) -> Self;
}

#[inline(always)]
pub(super) fn linear_transform<T: GfniType>(x: T, map: i64) -> T {
let map = T::set_epi_64(map);
T::gf2p8affine_epi64_epi8(x, map)
}

impl<U: GfniType + UnderlierType, Scalar: BinaryField> TaggedMul<GfniStrategy>
for PackedPrimitiveType<U, Scalar>
{
Expand All @@ -80,22 +50,14 @@ impl<U: GfniType + UnderlierType, Scalar: TowerField> TaggedInvertOrZero<GfniStr
{
#[inline(always)]
fn invert_or_zero(self) -> Self {
assert!(is_aes_tower::<Scalar>() || is_canonical_tower::<Scalar>());
assert!(is_aes_tower::<Scalar>());
assert!(Scalar::N_BITS == 8);

let val_gfni = if is_canonical_tower::<Scalar>() {
linear_transform(self.to_underlier(), TOWER_TO_AES_MAP)
} else {
self.to_underlier()
};
let val_gfni = self.to_underlier();

// Calculate inversion and linear transformation to the original field with a single
// instruction
let transform_after = if is_canonical_tower::<Scalar>() {
U::set_epi_64(AES_TO_TOWER_MAP)
} else {
U::set_epi_64(IDENTITY_MAP)
};
let transform_after = U::set_epi_64(IDENTITY_MAP);
let inv_gfni = U::gf2p8affineinv_epi64_epi8(val_gfni, transform_after);

inv_gfni.into()
Expand Down
16 changes: 1 addition & 15 deletions crates/field/src/binary_field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2023-2025 Irreducible Inc.

use std::{
any::TypeId,
fmt::{Debug, Display, Formatter},
iter::{Product, Sum},
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
Expand Down Expand Up @@ -31,18 +30,10 @@ pub trait BinaryField: ExtensionField<BinaryField1b> {
/// trait can be implemented on any binary field *isomorphic* to the canonical tower field.
///
/// [DP23]: https://eprint.iacr.org/2023/1784
pub trait TowerField: BinaryField + From<Self::Canonical>
where
Self::Canonical: From<Self>,
{
pub trait TowerField: BinaryField {
/// The level $\iota$ in the tower, where this field is isomorphic to $T_{\iota}$.
const TOWER_LEVEL: usize = Self::N_BITS.ilog2() as usize;

/// The canonical field isomorphic to this tower field.
/// Currently for every tower field, the canonical field is Fan-Paar's binary field of the same
/// degree.
type Canonical: TowerField + SerializeBytes + DeserializeBytes;

/// Returns the smallest valid `TOWER_LEVEL` in the tower that can fit the same value.
///
/// Since which `TOWER_LEVEL` values are valid depends on the tower,
Expand Down Expand Up @@ -534,11 +525,6 @@ pub(crate) use impl_field_extension;

binary_field!(pub BinaryField1b(U1), U1::new(0x1));

#[inline(always)]
pub fn is_canonical_tower<F: TowerField>() -> bool {
TypeId::of::<F::Canonical>() == TypeId::of::<F>()
}

macro_rules! serialize_deserialize {
($bin_type:ty) => {
impl SerializeBytes for $bin_type {
Expand Down
2 changes: 0 additions & 2 deletions crates/field/src/binary_field_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ pub(crate) use impl_arithmetic_using_packed;

// TODO: try to get rid of `TowerFieldArithmetic` and use `impl_arithmetic_using_packed` here
impl TowerField for BinaryField1b {
type Canonical = Self;

fn min_tower_level(self) -> usize {
0
}
Expand Down
2 changes: 0 additions & 2 deletions crates/field/src/ghash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,6 @@ impl BinaryField for BinaryField128bGhash {
}

impl TowerField for BinaryField128bGhash {
type Canonical = Self;

fn min_tower_level(self) -> usize {
match self {
Self::ZERO | Self::ONE => 0,
Expand Down
2 changes: 0 additions & 2 deletions crates/field/src/polyval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ impl BinaryField for BinaryField128bPolyval {
}

impl TowerField for BinaryField128bPolyval {
type Canonical = BinaryField128bPolyval;

fn min_tower_level(self) -> usize {
match self {
Self::ZERO | Self::ONE => 0,
Expand Down
Loading