binius_field/arch/portable/
packed_128.rs

1// Copyright 2024-2025 Irreducible Inc.
2
3use super::{
4	packed::{impl_broadcast, impl_ops_for_zero_height, PackedPrimitiveType},
5	packed_arithmetic::{alphas, impl_tower_constants},
6};
7use crate::{
8	arch::{PackedStrategy, PairwiseRecursiveStrategy, PairwiseStrategy, PairwiseTableStrategy},
9	arithmetic_traits::{
10		impl_invert_with, impl_mul_alpha_with, impl_mul_with, impl_square_with,
11		impl_transformation_with_strategy,
12	},
13	BinaryField128b, BinaryField16b, BinaryField1b, BinaryField2b, BinaryField32b, BinaryField4b,
14	BinaryField64b, BinaryField8b,
15};
16
17// Define 128 bit packed field types
18pub type PackedBinaryField128x1b = PackedPrimitiveType<u128, BinaryField1b>;
19pub type PackedBinaryField64x2b = PackedPrimitiveType<u128, BinaryField2b>;
20pub type PackedBinaryField32x4b = PackedPrimitiveType<u128, BinaryField4b>;
21pub type PackedBinaryField16x8b = PackedPrimitiveType<u128, BinaryField8b>;
22pub type PackedBinaryField8x16b = PackedPrimitiveType<u128, BinaryField16b>;
23pub type PackedBinaryField4x32b = PackedPrimitiveType<u128, BinaryField32b>;
24pub type PackedBinaryField2x64b = PackedPrimitiveType<u128, BinaryField64b>;
25pub type PackedBinaryField1x128b = PackedPrimitiveType<u128, BinaryField128b>;
26
27// Define broadcast
28impl_broadcast!(u128, BinaryField1b);
29impl_broadcast!(u128, BinaryField2b);
30impl_broadcast!(u128, BinaryField4b);
31impl_broadcast!(u128, BinaryField8b);
32impl_broadcast!(u128, BinaryField16b);
33impl_broadcast!(u128, BinaryField32b);
34impl_broadcast!(u128, BinaryField64b);
35impl_broadcast!(u128, BinaryField128b);
36
37// Define operations for height 0
38impl_ops_for_zero_height!(PackedBinaryField128x1b);
39
40// Define constants
41impl_tower_constants!(BinaryField1b, u128, { alphas!(u128, 0) });
42impl_tower_constants!(BinaryField2b, u128, { alphas!(u128, 1) });
43impl_tower_constants!(BinaryField4b, u128, { alphas!(u128, 2) });
44impl_tower_constants!(BinaryField8b, u128, { alphas!(u128, 3) });
45impl_tower_constants!(BinaryField16b, u128, { alphas!(u128, 4) });
46impl_tower_constants!(BinaryField32b, u128, { alphas!(u128, 5) });
47impl_tower_constants!(BinaryField64b, u128, { alphas!(u128, 6) });
48
49// Define multiplication
50impl_mul_with!(PackedBinaryField64x2b @ PackedStrategy);
51impl_mul_with!(PackedBinaryField32x4b @ PackedStrategy);
52impl_mul_with!(PackedBinaryField16x8b @ PackedStrategy);
53impl_mul_with!(PackedBinaryField8x16b @ PairwiseStrategy);
54impl_mul_with!(PackedBinaryField4x32b @ PairwiseStrategy);
55impl_mul_with!(PackedBinaryField2x64b @ PairwiseRecursiveStrategy);
56impl_mul_with!(PackedBinaryField1x128b @ PairwiseRecursiveStrategy);
57
58// Define square
59impl_square_with!(PackedBinaryField64x2b @ PackedStrategy);
60impl_square_with!(PackedBinaryField32x4b @ PackedStrategy);
61impl_square_with!(PackedBinaryField16x8b @ PackedStrategy);
62impl_square_with!(PackedBinaryField8x16b @ PairwiseRecursiveStrategy);
63impl_square_with!(PackedBinaryField4x32b @ PairwiseStrategy);
64impl_square_with!(PackedBinaryField2x64b @ PairwiseStrategy);
65impl_square_with!(PackedBinaryField1x128b @ PairwiseRecursiveStrategy);
66
67// Define invert
68impl_invert_with!(PackedBinaryField64x2b @ PackedStrategy);
69impl_invert_with!(PackedBinaryField32x4b @ PackedStrategy);
70impl_invert_with!(PackedBinaryField16x8b @ PairwiseTableStrategy);
71impl_invert_with!(PackedBinaryField8x16b @ PairwiseRecursiveStrategy);
72impl_invert_with!(PackedBinaryField4x32b @ PairwiseStrategy);
73impl_invert_with!(PackedBinaryField2x64b @ PairwiseRecursiveStrategy);
74impl_invert_with!(PackedBinaryField1x128b @ PairwiseRecursiveStrategy);
75
76// Define multiply by alpha
77impl_mul_alpha_with!(PackedBinaryField64x2b @ PackedStrategy);
78impl_mul_alpha_with!(PackedBinaryField32x4b @ PackedStrategy);
79impl_mul_alpha_with!(PackedBinaryField16x8b @ PackedStrategy);
80impl_mul_alpha_with!(PackedBinaryField8x16b @ PackedStrategy);
81impl_mul_alpha_with!(PackedBinaryField4x32b @ PackedStrategy);
82impl_mul_alpha_with!(PackedBinaryField2x64b @ PackedStrategy);
83impl_mul_alpha_with!(PackedBinaryField1x128b @ PairwiseRecursiveStrategy);
84
85// Define linear transformations
86impl_transformation_with_strategy!(PackedBinaryField128x1b, PackedStrategy);
87impl_transformation_with_strategy!(PackedBinaryField64x2b, PackedStrategy);
88impl_transformation_with_strategy!(PackedBinaryField32x4b, PackedStrategy);
89impl_transformation_with_strategy!(PackedBinaryField16x8b, PackedStrategy);
90impl_transformation_with_strategy!(PackedBinaryField8x16b, PackedStrategy);
91impl_transformation_with_strategy!(PackedBinaryField4x32b, PackedStrategy);
92impl_transformation_with_strategy!(PackedBinaryField2x64b, PairwiseStrategy);
93impl_transformation_with_strategy!(PackedBinaryField1x128b, PairwiseStrategy);