binius_field/arch/x86_64/
packed_aes_128.rs

1// Copyright 2024-2025 Irreducible Inc.
2
3use cfg_if::cfg_if;
4
5use super::m128::M128;
6use crate::{
7	aes_field::{
8		AESTowerField128b, AESTowerField16b, AESTowerField32b, AESTowerField64b, AESTowerField8b,
9	},
10	arch::{portable::packed::PackedPrimitiveType, SimdStrategy},
11	arithmetic_traits::{
12		impl_invert_with, impl_mul_alpha_with, impl_mul_with, impl_square_with,
13		impl_transformation_with_strategy,
14	},
15};
16
17// Define 128 bit packed field types
18pub type PackedAESBinaryField16x8b = PackedPrimitiveType<M128, AESTowerField8b>;
19pub type PackedAESBinaryField8x16b = PackedPrimitiveType<M128, AESTowerField16b>;
20pub type PackedAESBinaryField4x32b = PackedPrimitiveType<M128, AESTowerField32b>;
21pub type PackedAESBinaryField2x64b = PackedPrimitiveType<M128, AESTowerField64b>;
22pub type PackedAESBinaryField1x128b = PackedPrimitiveType<M128, AESTowerField128b>;
23
24// Define multiplication
25cfg_if! {
26	if #[cfg(target_feature = "gfni")] {
27		impl_mul_with!(PackedAESBinaryField16x8b @ crate::arch::GfniStrategy);
28	} else {
29		impl_mul_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
30	}
31}
32impl_mul_with!(PackedAESBinaryField8x16b @ SimdStrategy);
33impl_mul_with!(PackedAESBinaryField4x32b @ SimdStrategy);
34impl_mul_with!(PackedAESBinaryField2x64b @ SimdStrategy);
35impl_mul_with!(PackedAESBinaryField1x128b @ SimdStrategy);
36
37// Define square
38cfg_if! {
39	if #[cfg(target_feature = "gfni")] {
40		impl_square_with!(PackedAESBinaryField16x8b @ crate::arch::ReuseMultiplyStrategy);
41	} else {
42		impl_square_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
43	}
44}
45impl_square_with!(PackedAESBinaryField8x16b @ SimdStrategy);
46impl_square_with!(PackedAESBinaryField4x32b @ SimdStrategy);
47impl_square_with!(PackedAESBinaryField2x64b @ SimdStrategy);
48impl_square_with!(PackedAESBinaryField1x128b @ SimdStrategy);
49
50// Define invert
51cfg_if! {
52	if #[cfg(target_feature = "gfni")] {
53		impl_invert_with!(PackedAESBinaryField16x8b @ crate::arch::GfniStrategy);
54	} else {
55		impl_invert_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
56	}
57}
58impl_invert_with!(PackedAESBinaryField8x16b @ SimdStrategy);
59impl_invert_with!(PackedAESBinaryField4x32b @ SimdStrategy);
60impl_invert_with!(PackedAESBinaryField2x64b @ SimdStrategy);
61impl_invert_with!(PackedAESBinaryField1x128b @ SimdStrategy);
62
63// Define multiply by alpha
64cfg_if! {
65	if #[cfg(target_feature = "gfni")] {
66		impl_mul_alpha_with!(PackedAESBinaryField16x8b @ crate::arch::ReuseMultiplyStrategy);
67	} else {
68		impl_mul_alpha_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
69	}
70}
71impl_mul_alpha_with!(PackedAESBinaryField8x16b @ SimdStrategy);
72impl_mul_alpha_with!(PackedAESBinaryField4x32b @ SimdStrategy);
73impl_mul_alpha_with!(PackedAESBinaryField2x64b @ SimdStrategy);
74impl_mul_alpha_with!(PackedAESBinaryField1x128b @ SimdStrategy);
75
76// Define linear transformations
77cfg_if! {
78	if #[cfg(target_feature = "gfni")] {
79		use crate::arch::x86_64::gfni::gfni_arithmetics::impl_transformation_with_gfni_nxn;
80
81		impl_transformation_with_strategy!(PackedAESBinaryField16x8b, crate::arch::GfniStrategy);
82		impl_transformation_with_gfni_nxn!(PackedAESBinaryField8x16b, 2);
83		impl_transformation_with_gfni_nxn!(PackedAESBinaryField4x32b, 4);
84		impl_transformation_with_gfni_nxn!(PackedAESBinaryField2x64b, 8);
85		impl_transformation_with_gfni_nxn!(PackedAESBinaryField1x128b, 16);
86	} else {
87		impl_transformation_with_strategy!(PackedAESBinaryField16x8b, SimdStrategy);
88		impl_transformation_with_strategy!(PackedAESBinaryField8x16b, SimdStrategy);
89		impl_transformation_with_strategy!(PackedAESBinaryField4x32b, SimdStrategy);
90		impl_transformation_with_strategy!(PackedAESBinaryField2x64b, SimdStrategy);
91		impl_transformation_with_strategy!(PackedAESBinaryField1x128b, SimdStrategy);
92	}
93}