binius_field/arch/x86_64/
packed_128.rs

1// Copyright 2024-2025 Irreducible Inc.
2
3use cfg_if::cfg_if;
4
5use super::m128::M128;
6use crate::{
7	arch::{
8		portable::{
9			packed::{impl_ops_for_zero_height, PackedPrimitiveType},
10			packed_arithmetic::{alphas, impl_tower_constants},
11		},
12		PackedStrategy, SimdStrategy,
13	},
14	arithmetic_traits::{
15		impl_invert_with, impl_mul_alpha_with, impl_mul_with, impl_square_with,
16		impl_transformation_with_strategy,
17	},
18	BinaryField128b, BinaryField16b, BinaryField1b, BinaryField2b, BinaryField32b, BinaryField4b,
19	BinaryField64b, BinaryField8b,
20};
21
22// Define 128 bit packed field types
23pub type PackedBinaryField128x1b = PackedPrimitiveType<M128, BinaryField1b>;
24pub type PackedBinaryField64x2b = PackedPrimitiveType<M128, BinaryField2b>;
25pub type PackedBinaryField32x4b = PackedPrimitiveType<M128, BinaryField4b>;
26pub type PackedBinaryField16x8b = PackedPrimitiveType<M128, BinaryField8b>;
27pub type PackedBinaryField8x16b = PackedPrimitiveType<M128, BinaryField16b>;
28pub type PackedBinaryField4x32b = PackedPrimitiveType<M128, BinaryField32b>;
29pub type PackedBinaryField2x64b = PackedPrimitiveType<M128, BinaryField64b>;
30pub type PackedBinaryField1x128b = PackedPrimitiveType<M128, BinaryField128b>;
31
32// Define operations for zero height
33impl_ops_for_zero_height!(PackedBinaryField128x1b);
34
35// Define constants
36impl_tower_constants!(BinaryField1b, M128, { M128::from_u128(alphas!(u128, 0)) });
37impl_tower_constants!(BinaryField2b, M128, { M128::from_u128(alphas!(u128, 1)) });
38impl_tower_constants!(BinaryField4b, M128, { M128::from_u128(alphas!(u128, 2)) });
39impl_tower_constants!(BinaryField8b, M128, { M128::from_u128(alphas!(u128, 3)) });
40impl_tower_constants!(BinaryField16b, M128, { M128::from_u128(alphas!(u128, 4)) });
41impl_tower_constants!(BinaryField32b, M128, { M128::from_u128(alphas!(u128, 5)) });
42impl_tower_constants!(BinaryField64b, M128, { M128::from_u128(alphas!(u128, 6)) });
43
44// Define multiplication
45impl_mul_with!(PackedBinaryField64x2b @ PackedStrategy);
46impl_mul_with!(PackedBinaryField32x4b @ PackedStrategy);
47cfg_if! {
48	if #[cfg(target_feature = "gfni")] {
49		impl_mul_with!(PackedBinaryField16x8b @ crate::arch::AESIsomorphicStrategy);
50		impl_mul_with!(PackedBinaryField8x16b @ crate::arch::AESIsomorphicStrategy);
51		impl_mul_with!(PackedBinaryField4x32b @ crate::arch::AESIsomorphicStrategy);
52		impl_mul_with!(PackedBinaryField2x64b @ crate::arch::AESIsomorphicStrategy);
53		impl_mul_with!(PackedBinaryField1x128b @ crate::arch::AESIsomorphicStrategy);
54	} else {
55		impl_mul_with!(PackedBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
56		impl_mul_with!(PackedBinaryField8x16b @ SimdStrategy);
57		impl_mul_with!(PackedBinaryField4x32b @ SimdStrategy);
58		impl_mul_with!(PackedBinaryField2x64b @ SimdStrategy);
59		impl_mul_with!(PackedBinaryField1x128b @ SimdStrategy);
60
61	}
62}
63
64// Define square
65impl_square_with!(PackedBinaryField64x2b @ PackedStrategy);
66impl_square_with!(PackedBinaryField32x4b @ PackedStrategy);
67cfg_if! {
68	if #[cfg(target_feature = "gfni")] {
69		impl_square_with!(PackedBinaryField16x8b @ crate::arch::AESIsomorphicStrategy);
70		impl_square_with!(PackedBinaryField8x16b @ crate::arch::AESIsomorphicStrategy);
71		impl_square_with!(PackedBinaryField4x32b @ crate::arch::AESIsomorphicStrategy);
72		impl_square_with!(PackedBinaryField2x64b @ crate::arch::AESIsomorphicStrategy);
73		impl_square_with!(PackedBinaryField1x128b @ crate::arch::AESIsomorphicStrategy);
74	} else {
75		impl_square_with!(PackedBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
76		impl_square_with!(PackedBinaryField8x16b @ SimdStrategy);
77		impl_square_with!(PackedBinaryField4x32b @ SimdStrategy);
78		impl_square_with!(PackedBinaryField2x64b @ SimdStrategy);
79		impl_square_with!(PackedBinaryField1x128b @ SimdStrategy);
80	}
81}
82
83// Define invert
84impl_invert_with!(PackedBinaryField64x2b @ PackedStrategy);
85impl_invert_with!(PackedBinaryField32x4b @ PackedStrategy);
86cfg_if! {
87	if #[cfg(target_feature = "gfni")] {
88		impl_invert_with!(PackedBinaryField16x8b @ crate::arch::GfniStrategy);
89		impl_invert_with!(PackedBinaryField8x16b @ crate::arch::AESIsomorphicStrategy);
90		impl_invert_with!(PackedBinaryField4x32b @ crate::arch::AESIsomorphicStrategy);
91		impl_invert_with!(PackedBinaryField2x64b @ crate::arch::AESIsomorphicStrategy);
92		impl_invert_with!(PackedBinaryField1x128b @ crate::arch::AESIsomorphicStrategy);
93	} else {
94		impl_invert_with!(PackedBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
95		impl_invert_with!(PackedBinaryField8x16b @ SimdStrategy);
96		impl_invert_with!(PackedBinaryField4x32b @ SimdStrategy);
97		impl_invert_with!(PackedBinaryField2x64b @ SimdStrategy);
98		impl_invert_with!(PackedBinaryField1x128b @ SimdStrategy);
99	}
100}
101
102// Define multiply by alpha
103impl_mul_alpha_with!(PackedBinaryField64x2b @ PackedStrategy);
104impl_mul_alpha_with!(PackedBinaryField32x4b @ PackedStrategy);
105cfg_if! {
106	if #[cfg(target_feature = "gfni")] {
107		impl_mul_alpha_with!(PackedBinaryField16x8b @ crate::arch::ReuseMultiplyStrategy);
108	} else {
109		impl_mul_alpha_with!(PackedBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
110	}
111}
112impl_mul_alpha_with!(PackedBinaryField8x16b @ SimdStrategy);
113impl_mul_alpha_with!(PackedBinaryField4x32b @ SimdStrategy);
114impl_mul_alpha_with!(PackedBinaryField2x64b @ SimdStrategy);
115impl_mul_alpha_with!(PackedBinaryField1x128b @ SimdStrategy);
116
117// Define linear transformations
118impl_transformation_with_strategy!(PackedBinaryField128x1b, SimdStrategy);
119impl_transformation_with_strategy!(PackedBinaryField64x2b, SimdStrategy);
120impl_transformation_with_strategy!(PackedBinaryField32x4b, SimdStrategy);
121cfg_if! {
122	if #[cfg(target_feature = "gfni")] {
123		use crate::arch::x86_64::gfni::gfni_arithmetics::impl_transformation_with_gfni_nxn;
124
125		impl_transformation_with_strategy!(PackedBinaryField16x8b, crate::arch::GfniStrategy);
126		impl_transformation_with_gfni_nxn!(PackedBinaryField8x16b, 2);
127		impl_transformation_with_gfni_nxn!(PackedBinaryField4x32b, 4);
128		impl_transformation_with_gfni_nxn!(PackedBinaryField2x64b, 8);
129		impl_transformation_with_gfni_nxn!(PackedBinaryField1x128b, 16);
130	} else {
131		impl_transformation_with_strategy!(PackedBinaryField16x8b, SimdStrategy);
132		impl_transformation_with_strategy!(PackedBinaryField8x16b, SimdStrategy);
133		impl_transformation_with_strategy!(PackedBinaryField4x32b, SimdStrategy);
134		impl_transformation_with_strategy!(PackedBinaryField2x64b, SimdStrategy);
135		impl_transformation_with_strategy!(PackedBinaryField1x128b, SimdStrategy);
136	}
137}