1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2024 Ulvetanna Inc.

use cfg_if::cfg_if;

use crate::{
	aes_field::{
		AESTowerField128b, AESTowerField16b, AESTowerField32b, AESTowerField64b, AESTowerField8b,
	},
	arch::{portable::packed::PackedPrimitiveType, SimdStrategy},
	arithmetic_traits::{
		impl_invert_with, impl_mul_alpha_with, impl_mul_with, impl_square_with,
		impl_transformation_with_strategy,
	},
};

use super::m128::M128;

// Define 128 bit packed field types
pub type PackedAESBinaryField16x8b = PackedPrimitiveType<M128, AESTowerField8b>;
pub type PackedAESBinaryField8x16b = PackedPrimitiveType<M128, AESTowerField16b>;
pub type PackedAESBinaryField4x32b = PackedPrimitiveType<M128, AESTowerField32b>;
pub type PackedAESBinaryField2x64b = PackedPrimitiveType<M128, AESTowerField64b>;
pub type PackedAESBinaryField1x128b = PackedPrimitiveType<M128, AESTowerField128b>;

// Define multiplication
cfg_if! {
	if #[cfg(target_feature = "gfni")] {
		impl_mul_with!(PackedAESBinaryField16x8b @ super::gfni::gfni_arithmetics::GfniAESTowerStrategy);
	} else {
		impl_mul_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
	}
}
impl_mul_with!(PackedAESBinaryField8x16b @ SimdStrategy);
impl_mul_with!(PackedAESBinaryField4x32b @ SimdStrategy);
impl_mul_with!(PackedAESBinaryField2x64b @ SimdStrategy);
impl_mul_with!(PackedAESBinaryField1x128b @ SimdStrategy);

// Define square
cfg_if! {
	if #[cfg(target_feature = "gfni")] {
		impl_square_with!(PackedAESBinaryField16x8b @ crate::arch::ReuseMultiplyStrategy);
	} else {
		impl_square_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
	}
}
impl_square_with!(PackedAESBinaryField8x16b @ SimdStrategy);
impl_square_with!(PackedAESBinaryField4x32b @ SimdStrategy);
impl_square_with!(PackedAESBinaryField2x64b @ SimdStrategy);
impl_square_with!(PackedAESBinaryField1x128b @ SimdStrategy);

// Define invert
cfg_if! {
	if #[cfg(target_feature = "gfni")] {
		impl_invert_with!(PackedAESBinaryField16x8b @ super::gfni::gfni_arithmetics::GfniAESTowerStrategy);
	} else {
		impl_invert_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
	}
}
impl_invert_with!(PackedAESBinaryField8x16b @ SimdStrategy);
impl_invert_with!(PackedAESBinaryField4x32b @ SimdStrategy);
impl_invert_with!(PackedAESBinaryField2x64b @ SimdStrategy);
impl_invert_with!(PackedAESBinaryField1x128b @ SimdStrategy);

// Define multiply by alpha
cfg_if! {
	if #[cfg(target_feature = "gfni")] {
		impl_mul_alpha_with!(PackedAESBinaryField16x8b @ crate::arch::ReuseMultiplyStrategy);
	} else {
		impl_mul_alpha_with!(PackedAESBinaryField16x8b @ crate::arch::PairwiseTableStrategy);
	}
}
impl_mul_alpha_with!(PackedAESBinaryField8x16b @ SimdStrategy);
impl_mul_alpha_with!(PackedAESBinaryField4x32b @ SimdStrategy);
impl_mul_alpha_with!(PackedAESBinaryField2x64b @ SimdStrategy);
impl_mul_alpha_with!(PackedAESBinaryField1x128b @ SimdStrategy);

// Define linear transformations
cfg_if! {
	if #[cfg(target_feature = "gfni")] {
		use crate::arch::x86_64::gfni::gfni_arithmetics::impl_transformation_with_gfni_nxn;
		use crate::arch::x86_64::gfni::gfni_arithmetics::impl_transformation_with_gfni;

		impl_transformation_with_gfni!(PackedAESBinaryField16x8b, super::gfni::gfni_arithmetics::GfniBinaryTowerStrategy);
		impl_transformation_with_gfni_nxn!(PackedAESBinaryField8x16b, 2);
		impl_transformation_with_gfni_nxn!(PackedAESBinaryField4x32b, 4);
		impl_transformation_with_gfni_nxn!(PackedAESBinaryField2x64b, 8);
	} else {
		impl_transformation_with_strategy!(PackedAESBinaryField16x8b, SimdStrategy);
		impl_transformation_with_strategy!(PackedAESBinaryField8x16b, SimdStrategy);
		impl_transformation_with_strategy!(PackedAESBinaryField4x32b, SimdStrategy);
		impl_transformation_with_strategy!(PackedAESBinaryField2x64b, SimdStrategy);
	}
}
impl_transformation_with_strategy!(PackedAESBinaryField1x128b, SimdStrategy);