binius_field/arch/portable/
packed_4.rs

1// Copyright 2024-2025 Irreducible Inc.
2
3use super::{
4	packed::{impl_broadcast, impl_ops_for_zero_height, PackedPrimitiveType},
5	packed_arithmetic::TowerConstants,
6	reuse_multiply_arithmetic::Alpha,
7};
8use crate::{
9	arch::{PackedStrategy, PairwiseRecursiveStrategy, PairwiseStrategy, ReuseMultiplyStrategy},
10	arithmetic_traits::{
11		impl_invert_with, impl_mul_alpha_with, impl_mul_with, impl_square_with,
12		impl_transformation_with_strategy,
13	},
14	underlier::{UnderlierType, U4},
15	BinaryField1b, BinaryField2b, BinaryField4b,
16};
17
18// Define 4 bit packed field types
19pub type PackedBinaryField4x1b = PackedPrimitiveType<U4, BinaryField1b>;
20pub type PackedBinaryField2x2b = PackedPrimitiveType<U4, BinaryField2b>;
21pub type PackedBinaryField1x4b = PackedPrimitiveType<U4, BinaryField4b>;
22
23// Define broadcast
24impl_broadcast!(U4, BinaryField1b);
25impl_broadcast!(U4, BinaryField2b);
26impl_broadcast!(U4, BinaryField4b);
27
28// Define operations for height 0
29impl_ops_for_zero_height!(PackedBinaryField4x1b);
30
31// Define constants
32impl TowerConstants<U4> for BinaryField1b {
33	const ALPHAS_ODD: U4 = U4::new(<Self as TowerConstants<u8>>::ALPHAS_ODD);
34}
35impl TowerConstants<U4> for BinaryField2b {
36	const ALPHAS_ODD: U4 = U4::new(<Self as TowerConstants<u8>>::ALPHAS_ODD);
37}
38impl TowerConstants<U4> for BinaryField4b {
39	const ALPHAS_ODD: U4 = U4::new(<Self as TowerConstants<u8>>::ALPHAS_ODD);
40}
41
42// Define multiplication
43impl_mul_with!(PackedBinaryField2x2b @ PackedStrategy);
44impl_mul_with!(PackedBinaryField1x4b @ PackedStrategy);
45
46// Define square
47impl_square_with!(PackedBinaryField2x2b @ PackedStrategy);
48impl_square_with!(PackedBinaryField1x4b @ ReuseMultiplyStrategy);
49
50// Define invert
51impl_invert_with!(PackedBinaryField2x2b @ PairwiseRecursiveStrategy);
52impl_invert_with!(PackedBinaryField1x4b @ PairwiseRecursiveStrategy);
53
54// Define multiply by alpha
55impl_mul_alpha_with!(PackedBinaryField2x2b @ PackedStrategy);
56impl_mul_alpha_with!(PackedBinaryField1x4b @ ReuseMultiplyStrategy);
57
58impl Alpha for PackedBinaryField1x4b {
59	#[inline]
60	fn alpha() -> Self {
61		Self::from_underlier(U4::new_unchecked(0x04))
62	}
63}
64
65// Define linear transformations
66impl_transformation_with_strategy!(PackedBinaryField4x1b, PackedStrategy);
67impl_transformation_with_strategy!(PackedBinaryField2x2b, PackedStrategy);
68impl_transformation_with_strategy!(PackedBinaryField1x4b, PairwiseStrategy);