binius_field/
packed_polyval.rs

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2024-2025 Irreducible Inc.

pub use crate::arch::{
	packed_polyval_128::PackedBinaryPolyval1x128b, packed_polyval_256::PackedBinaryPolyval2x128b,
	packed_polyval_512::PackedBinaryPolyval4x128b,
};

#[cfg(test)]
mod test_utils {
	/// Test if `mult_func` operation is a valid multiply operation on the given values for
	/// all possible packed fields defined on 8-512 bits.
	macro_rules! define_multiply_tests {
		($mult_func:path, $constraint:ty) => {
			$crate::packed_binary_field::test_utils::define_check_packed_mul!(
				$mult_func,
				$constraint
			);

			proptest! {
				#[test]
				fn test_mul_packed_128(a_val in any::<u128>(), b_val in any::<u128>()) {
					TestMult::<$crate::arch::packed_polyval_128::PackedBinaryPolyval1x128b>::test_mul(
						a_val.into(),
						b_val.into(),
					);
				}

				#[test]
				fn test_mul_packed_256(a_val in any::<[u128; 2]>(), b_val in any::<[u128; 2]>()) {
					TestMult::<$crate::arch::packed_polyval_256::PackedBinaryPolyval2x128b>::test_mul(
						a_val.into(),
						b_val.into(),
					);
				}

				#[test]
				fn test_mul_packed_512(a_val in any::<[u128; 4]>(), b_val in any::<[u128; 4]>()) {
					TestMult::<$crate::arch::packed_polyval_512::PackedBinaryPolyval4x128b>::test_mul(
						a_val.into(),
						b_val.into(),
					);
				}
			}
		};
	}

	/// Test if `square_func` operation is a valid square operation on the given value for
	/// all possible packed fields.
	macro_rules! define_square_tests {
		($square_func:path, $constraint:ident) => {
			$crate::packed_binary_field::test_utils::define_check_packed_square!(
				$square_func,
				$constraint
			);

			proptest! {
				#[test]
				fn test_square_packed_128(a_val in any::<u128>()) {
					TestSquare::<$crate::arch::packed_polyval_128::PackedBinaryPolyval1x128b>::test_square(a_val.into());
				}

				#[test]
				fn test_square_packed_256(a_val in any::<[u128; 2]>()) {
					TestSquare::<$crate::arch::packed_polyval_256::PackedBinaryPolyval2x128b>::test_square(a_val.into());
				}

				#[test]
				fn test_square_packed_512(a_val in any::<[u128; 4]>()) {
					TestSquare::<$crate::arch::packed_polyval_512::PackedBinaryPolyval4x128b>::test_square(a_val.into());
				}
			}
		};
	}

	/// Test if `invert_func` operation is a valid invert operation on the given value for
	/// all possible packed fields.
	macro_rules! define_invert_tests {
		($invert_func:path, $constraint:ident) => {
			$crate::packed_binary_field::test_utils::define_check_packed_inverse!(
				$invert_func,
				$constraint
			);

			proptest! {
				#[test]
				fn test_invert_packed_128(a_val in any::<u128>()) {
					TestInvert::<$crate::arch::packed_polyval_128::PackedBinaryPolyval1x128b>::test_invert(a_val.into());
				}

				#[test]
				fn test_invert_packed_256(a_val in any::<[u128; 2]>()) {
					TestInvert::<$crate::arch::packed_polyval_256::PackedBinaryPolyval2x128b>::test_invert(a_val.into());
				}

				#[test]
				fn test_invert_packed_512(a_val in any::<[u128; 4]>()) {
					TestInvert::<$crate::arch::packed_polyval_512::PackedBinaryPolyval4x128b>::test_invert(a_val.into());
				}
			}
		};
	}

	macro_rules! define_transformation_tests {
		($constraint:path) => {
			$crate::packed_binary_field::test_utils::define_check_packed_transformation!(
				$constraint
			);

			proptest::proptest! {
				#[test]
				fn test_transformation_packed_128(a_val in proptest::prelude::any::<u128>()) {
					TestTransformation::<$crate::arch::packed_polyval_128::PackedBinaryPolyval1x128b>::test_transformation(a_val.into());
				}

				#[test]
				fn test_transformation_packed_256(a_val in proptest::prelude::any::<[u128; 2]>()) {
					TestTransformation::<$crate::arch::packed_polyval_256::PackedBinaryPolyval2x128b>::test_transformation(a_val.into());
				}

				#[test]
				fn test_transformation_packed_512(a_val in proptest::prelude::any::<[u128; 4]>()) {
					TestTransformation::<$crate::arch::packed_polyval_512::PackedBinaryPolyval4x128b>::test_transformation(a_val.into());
				}
			}
		};
	}

	pub(crate) use define_invert_tests;
	pub(crate) use define_multiply_tests;
	pub(crate) use define_square_tests;
	pub(crate) use define_transformation_tests;
}

#[cfg(test)]
mod tests {
	use std::ops::Mul;

	use proptest::{arbitrary::any, proptest};

	use super::test_utils::{
		define_invert_tests, define_multiply_tests, define_square_tests,
		define_transformation_tests,
	};
	use crate::{
		arch::{
			packed_polyval_128::PackedBinaryPolyval1x128b,
			packed_polyval_256::PackedBinaryPolyval2x128b,
			packed_polyval_512::PackedBinaryPolyval4x128b,
		},
		linear_transformation::PackedTransformationFactory,
		test_utils::implements_transformation_factory,
		underlier::WithUnderlier,
		BinaryField128bPolyval, PackedBinaryField1x128b, PackedBinaryField2x128b,
		PackedBinaryField4x128b, PackedField,
	};

	fn check_get_set<const WIDTH: usize, PT>(a: [u128; WIDTH], b: [u128; WIDTH])
	where
		PT: PackedField<Scalar = BinaryField128bPolyval>
			+ WithUnderlier<Underlier: From<[u128; WIDTH]>>,
	{
		let mut val = PT::from_underlier(a.into());
		for i in 0..WIDTH {
			assert_eq!(val.get(i), BinaryField128bPolyval::from(a[i]));
			val.set(i, BinaryField128bPolyval::from(b[i]));
			assert_eq!(val.get(i), BinaryField128bPolyval::from(b[i]));
		}
	}

	proptest! {
		#[test]
		fn test_get_set_256(a in any::<[u128; 2]>(), b in any::<[u128; 2]>()) {
			check_get_set::<2, PackedBinaryPolyval2x128b>(a, b);
		}

		#[test]
		fn test_get_set_512(a in any::<[u128; 4]>(), b in any::<[u128; 4]>()) {
			check_get_set::<4, PackedBinaryPolyval4x128b>(a, b);
		}
	}

	define_multiply_tests!(Mul::mul, PackedField);

	define_square_tests!(PackedField::square, PackedField);

	define_invert_tests!(PackedField::invert_or_zero, PackedField);

	#[allow(unused)]
	trait SelfTransformationFactory: PackedTransformationFactory<Self> {}

	impl<T: PackedTransformationFactory<T>> SelfTransformationFactory for T {}

	define_transformation_tests!(SelfTransformationFactory);

	/// Compile-time test to ensure packed fields implement `PackedTransformationFactory`.
	#[allow(unused)]
	const fn test_implement_transformation_factory() {
		// 128 bit packed polyval
		implements_transformation_factory::<PackedBinaryPolyval1x128b, PackedBinaryPolyval1x128b>();
		implements_transformation_factory::<PackedBinaryField1x128b, PackedBinaryPolyval1x128b>();

		// 256 bit packed polyval
		implements_transformation_factory::<PackedBinaryPolyval2x128b, PackedBinaryPolyval2x128b>();
		implements_transformation_factory::<PackedBinaryField2x128b, PackedBinaryPolyval2x128b>();

		// 512 bit packed polyval
		implements_transformation_factory::<PackedBinaryPolyval4x128b, PackedBinaryPolyval4x128b>();
		implements_transformation_factory::<PackedBinaryField4x128b, PackedBinaryPolyval4x128b>();
	}
}