binius_field/
aes_field.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright 2024 Irreducible Inc.

use super::{
	arithmetic_traits::InvertOrZero,
	binary_field::{binary_field, impl_field_extension, BinaryField, BinaryField1b},
	binary_field_arithmetic::TowerFieldArithmetic,
	mul_by_binary_field_1b, BinaryField8b, Error,
};
use crate::{
	as_packed_field::{AsPackedField, PackScalar, PackedType},
	binary_field_arithmetic::{impl_arithmetic_using_packed, impl_mul_primitive},
	binary_tower,
	linear_transformation::{
		FieldLinearTransformation, PackedTransformationFactory, Transformation,
	},
	packed::PackedField,
	underlier::{WithUnderlier, U1},
	BinaryField128b, BinaryField16b, BinaryField32b, BinaryField64b, ExtensionField, Field,
	RepackedExtension, TowerField,
};
use bytemuck::{Pod, Zeroable};
use rand::RngCore;
use std::{
	array,
	fmt::{Debug, Display, Formatter},
	iter::{Product, Step, Sum},
	marker::PhantomData,
	ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

// These fields represent a tower based on AES GF(2^8) field (GF(256)/x^8+x^4+x^3+x+1)
// that is isomorphically included into binary tower, i.e.:
//  - AESTowerField16b is GF(2^16) / (x^2 + x * x_2 + 1) where `x_2` is 0x10 from
// BinaryField8b isomorphically projected to AESTowerField8b.
//  - AESTowerField32b is GF(2^32) / (x^2 + x * x_3 + 1), where `x_3` is 0x1000 from AESTowerField16b.
//  ...
binary_field!(pub AESTowerField8b(u8), 0xD0);
binary_field!(pub AESTowerField16b(u16), 0x4745);
binary_field!(pub AESTowerField32b(u32), 0xBD478FAB);
binary_field!(pub AESTowerField64b(u64), 0x0DE1555D2BD78EB4);
binary_field!(pub AESTowerField128b(u128), 0x6DB54066349EDB96C33A87244A742678);

unsafe impl Pod for AESTowerField8b {}
unsafe impl Pod for AESTowerField16b {}
unsafe impl Pod for AESTowerField32b {}
unsafe impl Pod for AESTowerField64b {}
unsafe impl Pod for AESTowerField128b {}

binary_tower!(
	AESTowerField8b(u8, BinaryField8b)
	< AESTowerField16b(u16, BinaryField16b)
	< AESTowerField32b(u32, BinaryField32b)
	< AESTowerField64b(u64, BinaryField64b)
	< AESTowerField128b(u128, BinaryField128b)
);

impl_field_extension!(BinaryField1b(U1) < @3 => AESTowerField8b(u8));
impl_field_extension!(BinaryField1b(U1) < @4 => AESTowerField16b(u16));
impl_field_extension!(BinaryField1b(U1) < @5 => AESTowerField32b(u32));
impl_field_extension!(BinaryField1b(U1) < @6 => AESTowerField64b(u64));
impl_field_extension!(BinaryField1b(U1) < @7 => AESTowerField128b(u128));

mul_by_binary_field_1b!(AESTowerField8b);
mul_by_binary_field_1b!(AESTowerField16b);
mul_by_binary_field_1b!(AESTowerField32b);
mul_by_binary_field_1b!(AESTowerField64b);
mul_by_binary_field_1b!(AESTowerField128b);

impl_arithmetic_using_packed!(AESTowerField8b);
impl_arithmetic_using_packed!(AESTowerField16b);
impl_arithmetic_using_packed!(AESTowerField32b);
impl_arithmetic_using_packed!(AESTowerField64b);
impl_arithmetic_using_packed!(AESTowerField128b);

impl TowerField for AESTowerField8b {
	type Canonical = BinaryField8b;

	fn mul_primitive(self, iota: usize) -> Result<Self, Error> {
		match iota {
			0..=1 => Ok(self * ISOMORPHIC_ALPHAS[iota]),
			2 => Ok(self.multiply_alpha()),
			_ => Err(Error::ExtensionDegreeMismatch),
		}
	}
}

pub const AES_TO_BINARY_LINEAR_TRANSFORMATION: FieldLinearTransformation<BinaryField8b> =
	FieldLinearTransformation::new_const(&[
		BinaryField8b(0x01),
		BinaryField8b(0x3c),
		BinaryField8b(0x8c),
		BinaryField8b(0x8a),
		BinaryField8b(0x59),
		BinaryField8b(0x7a),
		BinaryField8b(0x53),
		BinaryField8b(0x27),
	]);

impl From<AESTowerField8b> for BinaryField8b {
	fn from(value: AESTowerField8b) -> Self {
		AES_TO_BINARY_LINEAR_TRANSFORMATION.transform(&value)
	}
}

pub const BINARY_TO_AES_LINEAR_TRANSFORMATION: FieldLinearTransformation<AESTowerField8b> =
	FieldLinearTransformation::new_const(&[
		AESTowerField8b(0x01),
		AESTowerField8b(0xbc),
		AESTowerField8b(0xb0),
		AESTowerField8b(0xec),
		AESTowerField8b(0xd3),
		AESTowerField8b(0x8d),
		AESTowerField8b(0x2e),
		AESTowerField8b(0x58),
	]);

impl From<BinaryField8b> for AESTowerField8b {
	fn from(value: BinaryField8b) -> Self {
		BINARY_TO_AES_LINEAR_TRANSFORMATION.transform(&value)
	}
}

/// A 3- step transformation :
/// 1. Cast to base b-bit packed field
/// 2. Apply linear transformation between aes and binary b8 tower fields
/// 3. Cast back to the target field
struct SubfieldTransformer<IP, OP, T> {
	inner_transform: T,
	_ip_pd: PhantomData<IP>,
	_op_pd: PhantomData<OP>,
}

impl<IP, OP, T> SubfieldTransformer<IP, OP, T> {
	fn new(inner_transform: T) -> Self {
		Self {
			inner_transform,
			_ip_pd: PhantomData,
			_op_pd: PhantomData,
		}
	}
}

impl<IP, OP, IEP, OEP, T> Transformation<IEP, OEP> for SubfieldTransformer<IP, OP, T>
where
	IP: PackedField + WithUnderlier,
	OP: PackedField + WithUnderlier<Underlier = IP::Underlier>,
	IEP: RepackedExtension<IP, Scalar: ExtensionField<IP::Scalar>>
		+ WithUnderlier<Underlier = IP::Underlier>,
	OEP: RepackedExtension<OP, Scalar: ExtensionField<OP::Scalar>>
		+ WithUnderlier<Underlier = IP::Underlier>,
	T: Transformation<IP, OP>,
{
	fn transform(&self, input: &IEP) -> OEP {
		OEP::from_underlier(
			self.inner_transform
				.transform(&IP::from_underlier(input.to_underlier()))
				.to_underlier(),
		)
	}
}

/// Creates transformation object from AES tower to binary tower for packed field.
/// Note that creation of this object is not cheap, so it is better to create it once and reuse.
pub fn make_aes_to_binary_packed_transformer<IP, OP>() -> impl Transformation<IP, OP>
where
	IP: PackedField<Scalar: ExtensionField<AESTowerField8b>> + WithUnderlier,
	OP: PackedField<Scalar: ExtensionField<BinaryField8b>>
		+ WithUnderlier<Underlier = IP::Underlier>,
	IP::Underlier: PackScalar<
			AESTowerField8b,
			Packed: PackedTransformationFactory<PackedType<IP::Underlier, BinaryField8b>>,
		> + PackScalar<BinaryField8b>,
{
	SubfieldTransformer::<
		PackedType<IP::Underlier, AESTowerField8b>,
		PackedType<IP::Underlier, BinaryField8b>,
		_,
	>::new(PackedType::<IP::Underlier, AESTowerField8b>::make_packed_transformation(
		AES_TO_BINARY_LINEAR_TRANSFORMATION,
	))
}

/// Creates transformation object from AES tower to binary tower for packed field.
/// Note that creation of this object is not cheap, so it is better to create it once and reuse.
pub fn make_binary_to_aes_packed_transformer<IP, OP>() -> impl Transformation<IP, OP>
where
	IP: PackedField<Scalar: ExtensionField<BinaryField8b>> + WithUnderlier,
	OP: PackedField<Scalar: ExtensionField<AESTowerField8b>>
		+ WithUnderlier<Underlier = IP::Underlier>,
	IP::Underlier: PackScalar<
			BinaryField8b,
			Packed: PackedTransformationFactory<PackedType<IP::Underlier, AESTowerField8b>>,
		> + PackScalar<AESTowerField8b>,
{
	SubfieldTransformer::<
		PackedType<IP::Underlier, BinaryField8b>,
		PackedType<IP::Underlier, AESTowerField8b>,
		_,
	>::new(PackedType::<IP::Underlier, BinaryField8b>::make_packed_transformation(
		BINARY_TO_AES_LINEAR_TRANSFORMATION,
	))
}

/// Values isomorphic to 0x02, 0x04 and 0x10 in BinaryField8b
const ISOMORPHIC_ALPHAS: [AESTowerField8b; 3] = [
	AESTowerField8b(0xBC),
	AESTowerField8b(0xB0),
	AESTowerField8b(0xD3),
];

// MulPrimitive implementation for AES tower
impl_mul_primitive!(AESTowerField16b,
	mul_by 0 => ISOMORPHIC_ALPHAS[0],
	mul_by 1 => ISOMORPHIC_ALPHAS[1],
	repack 2 => AESTowerField8b,
	repack 3 => AESTowerField16b,
);
impl_mul_primitive!(AESTowerField32b,
	mul_by 0 => ISOMORPHIC_ALPHAS[0],
	mul_by 1 => ISOMORPHIC_ALPHAS[1],
	repack 2 => AESTowerField8b,
	repack 3 => AESTowerField16b,
	repack 4 => AESTowerField32b,
);
impl_mul_primitive!(AESTowerField64b,
	mul_by 0 => ISOMORPHIC_ALPHAS[0],
	mul_by 1 => ISOMORPHIC_ALPHAS[1],
	repack 2 => AESTowerField8b,
	repack 3 => AESTowerField16b,
	repack 4 => AESTowerField32b,
	repack 5 => AESTowerField64b,
);
impl_mul_primitive!(AESTowerField128b,
	mul_by 0 => ISOMORPHIC_ALPHAS[0],
	mul_by 1 => ISOMORPHIC_ALPHAS[1],
	repack 2 => AESTowerField8b,
	repack 3 => AESTowerField16b,
	repack 4 => AESTowerField32b,
	repack 5 => AESTowerField64b,
	repack 6 => AESTowerField128b,
);

/// We use this function to define isomorphisms between AES and binary tower fields.
/// Repack field as 8b packed field and apply isomorphism for each 8b element
fn convert_as_packed_8b<F1, F2, Scalar1, Scalar2>(val: F1) -> F2
where
	Scalar1: Field,
	Scalar2: Field + From<Scalar1>,
	F1: AsPackedField<Scalar1>,
	F2: AsPackedField<Scalar2>,
{
	assert_eq!(F1::Packed::WIDTH, F2::Packed::WIDTH);

	let val_repacked = val.to_packed();
	let converted_repacked = F2::Packed::from_fn(|i| val_repacked.get(i).into());

	F2::from_packed(converted_repacked)
}

macro_rules! impl_tower_field_conversion {
	($aes_field:ty, $binary_field:ty) => {
		impl From<$aes_field> for $binary_field {
			fn from(value: $aes_field) -> Self {
				convert_as_packed_8b::<_, _, AESTowerField8b, BinaryField8b>(value)
			}
		}

		impl From<$binary_field> for $aes_field {
			fn from(value: $binary_field) -> Self {
				convert_as_packed_8b::<_, _, BinaryField8b, AESTowerField8b>(value)
			}
		}
	};
}

impl_tower_field_conversion!(AESTowerField16b, BinaryField16b);
impl_tower_field_conversion!(AESTowerField32b, BinaryField32b);
impl_tower_field_conversion!(AESTowerField64b, BinaryField64b);
impl_tower_field_conversion!(AESTowerField128b, BinaryField128b);

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{
		binary_field::tests::is_binary_field_valid_generator, deserialize_canonical,
		serialize_canonical, PackedAESBinaryField16x32b, PackedAESBinaryField4x32b,
		PackedAESBinaryField8x32b, PackedBinaryField16x32b, PackedBinaryField4x32b,
		PackedBinaryField8x32b,
	};
	use bytes::BytesMut;

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

	fn check_square(f: impl Field) {
		assert_eq!(f.square(), f * f);
	}

	proptest! {
		#[test]
		fn test_square_8(a in any::<u8>()) {
			check_square(AESTowerField8b::from(a))
		}

		#[test]
		fn test_square_16(a in any::<u16>()) {
			check_square(AESTowerField16b::from(a))
		}

		#[test]
		fn test_square_32(a in any::<u32>()) {
			check_square(AESTowerField32b::from(a))
		}

		#[test]
		fn test_square_64(a in any::<u64>()) {
			check_square(AESTowerField64b::from(a))
		}

		#[test]
		fn test_square_128(a in any::<u128>()) {
			check_square(AESTowerField128b::from(a))
		}
	}

	fn check_invert(f: impl Field) {
		let inversed = f.invert();
		if f.is_zero() {
			assert!(inversed.is_none());
		} else {
			assert_eq!(inversed.unwrap() * f, Field::ONE);
		}
	}

	fn check_isomorphism_preserves_ops<F1: Field, F2: Field + From<F1>>(a: F1, b: F1) {
		assert_eq!(F2::from(a * b), F2::from(a) * F2::from(b));
		assert_eq!(F2::from(a + b), F2::from(a) + F2::from(b));
	}

	proptest! {
		#[test]
		fn test_invert_8(a in any::<u8>()) {
			check_invert(AESTowerField8b::from(a))
		}

		#[test]
		fn test_invert_16(a in any::<u16>()) {
			check_invert(AESTowerField16b::from(a))
		}

		#[test]
		fn test_invert_32(a in any::<u32>()) {
			check_invert(AESTowerField32b::from(a))
		}

		#[test]
		fn test_invert_64(a in any::<u64>()) {
			check_invert(AESTowerField64b::from(a))
		}

		#[test]
		fn test_invert_128(a in any::<u128>()) {
			check_invert(AESTowerField128b::from(a))
		}

		#[test]
		fn test_isomorphism_to_binary_tower8b_roundtrip(a in any::<u8>()) {
			let a_val = AESTowerField8b(a);
			let projected = BinaryField8b::from(a_val);
			let restored = AESTowerField8b::from(projected);
			assert_eq!(a_val, restored);
		}

		#[test]
		fn test_isomorphism_8b(a in any::<u8>(), b in any::<u8>()) {
			check_isomorphism_preserves_ops::<AESTowerField8b, BinaryField8b>(a.into(), b.into());
			check_isomorphism_preserves_ops::<BinaryField8b, AESTowerField8b>(a.into(), b.into());
		}

		#[test]
		fn test_isomorphism_16b(a in any::<u16>(), b in any::<u16>()) {
			check_isomorphism_preserves_ops::<AESTowerField16b, BinaryField16b>(a.into(), b.into());
			check_isomorphism_preserves_ops::<BinaryField16b, AESTowerField16b>(a.into(), b.into());
		}

		#[test]
		fn test_isomorphism_32b(a in any::<u32>(), b in any::<u32>()) {
			check_isomorphism_preserves_ops::<AESTowerField32b, BinaryField32b>(a.into(), b.into());
			check_isomorphism_preserves_ops::<BinaryField32b, AESTowerField32b>(a.into(), b.into());
		}

		#[test]
		fn test_isomorphism_64b(a in any::<u64>(), b in any::<u64>()) {
			check_isomorphism_preserves_ops::<AESTowerField64b, BinaryField64b>(a.into(), b.into());
			check_isomorphism_preserves_ops::<BinaryField64b, AESTowerField64b>(a.into(), b.into());
		}

		#[test]
		fn test_isomorphism_128b(a in any::<u128>(), b in any::<u128>()) {
			check_isomorphism_preserves_ops::<AESTowerField128b, BinaryField128b>(a.into(), b.into());
			check_isomorphism_preserves_ops::<BinaryField128b, AESTowerField128b>(a.into(), b.into());
		}
	}

	fn check_mul_by_one<F: Field>(f: F) {
		assert_eq!(F::ONE * f, f);
		assert_eq!(f * F::ONE, f);
	}

	fn check_commutative<F: Field>(f_1: F, f_2: F) {
		assert_eq!(f_1 * f_2, f_2 * f_1);
	}

	fn check_associativity_and_lineraity<F: Field>(f_1: F, f_2: F, f_3: F) {
		assert_eq!(f_1 * (f_2 * f_3), (f_1 * f_2) * f_3);
		assert_eq!(f_1 * (f_2 + f_3), f_1 * f_2 + f_1 * f_3);
	}

	fn check_mul<F: Field>(f_1: F, f_2: F, f_3: F) {
		check_mul_by_one(f_1);
		check_mul_by_one(f_2);
		check_mul_by_one(f_3);

		check_commutative(f_1, f_2);
		check_commutative(f_1, f_3);
		check_commutative(f_2, f_3);

		check_associativity_and_lineraity(f_1, f_2, f_3);
		check_associativity_and_lineraity(f_1, f_3, f_2);
		check_associativity_and_lineraity(f_2, f_1, f_3);
		check_associativity_and_lineraity(f_2, f_3, f_1);
		check_associativity_and_lineraity(f_3, f_1, f_2);
		check_associativity_and_lineraity(f_3, f_2, f_1);
	}

	proptest! {
		#[test]
		fn test_mul_8(a in any::<u8>(), b in any::<u8>(), c in any::<u8>()) {
			check_mul(AESTowerField8b::from(a), AESTowerField8b::from(b), AESTowerField8b::from(c))
		}

		#[test]
		fn test_mul_16(a in any::<u16>(), b in any::<u16>(), c in any::<u16>()) {
			check_mul(AESTowerField16b::from(a), AESTowerField16b::from(b), AESTowerField16b::from(c))
		}

		#[test]
		fn test_mul_32(a in any::<u32>(), b in any::<u32>(), c in any::<u32>()) {
			check_mul(AESTowerField32b::from(a), AESTowerField32b::from(b), AESTowerField32b::from(c))
		}

		#[test]
		fn test_mul_64(a in any::<u64>(), b in any::<u64>(), c in any::<u64>()) {
			check_mul(AESTowerField64b::from(a), AESTowerField64b::from(b), AESTowerField64b::from(c))
		}

		#[test]
		fn test_mul_128(a in any::<u128>(), b in any::<u128>(), c in any::<u128>()) {
			check_mul(AESTowerField128b::from(a), AESTowerField128b::from(b), AESTowerField128b::from(c))
		}

		#[test]
		fn test_conversion_roundtrip(a in any::<u8>()) {
			let a_val = AESTowerField8b(a);
			let converted = BinaryField8b::from(a_val);
			assert_eq!(a_val, AESTowerField8b::from(converted));
		}
	}

	#[test]
	fn test_multiplicative_generators() {
		assert!(is_binary_field_valid_generator::<AESTowerField8b>());
		assert!(is_binary_field_valid_generator::<AESTowerField16b>());
		assert!(is_binary_field_valid_generator::<AESTowerField32b>());
		assert!(is_binary_field_valid_generator::<AESTowerField64b>());
		assert!(is_binary_field_valid_generator::<AESTowerField128b>());
	}

	fn test_mul_primitive<F: TowerField + WithUnderlier<Underlier: From<u8>>>(val: F, iota: usize) {
		let result = val.mul_primitive(iota);
		let expected = match iota {
			0..=2 => {
				Ok(val
					* F::from_underlier(F::Underlier::from(ISOMORPHIC_ALPHAS[iota].to_underlier())))
			}
			_ => <F as ExtensionField<BinaryField1b>>::basis(1 << iota).map(|b| val * b),
		};
		assert_eq!(result.is_ok(), expected.is_ok());
		if result.is_ok() {
			assert_eq!(result.unwrap(), expected.unwrap());
		} else {
			assert!(matches!(result.unwrap_err(), Error::ExtensionDegreeMismatch));
		}
	}

	proptest! {
		#[test]
		fn test_mul_primitive_8b(val in 0u8.., iota in 3usize..8) {
			test_mul_primitive::<AESTowerField8b>(val.into(), iota)
		}

		#[test]
		fn test_mul_primitive_16b(val in 0u16.., iota in 3usize..8) {
			test_mul_primitive::<AESTowerField16b>(val.into(), iota)
		}

		#[test]
		fn test_mul_primitive_32b(val in 0u32.., iota in 3usize..8) {
			test_mul_primitive::<AESTowerField32b>(val.into(), iota)
		}

		#[test]
		fn test_mul_primitive_64b(val in 0u64.., iota in 3usize..8) {
			test_mul_primitive::<AESTowerField64b>(val.into(), iota)
		}

		#[test]
		fn test_mul_primitive_128b(val in 0u128.., iota in 3usize..8) {
			test_mul_primitive::<AESTowerField128b>(val.into(), iota)
		}
	}

	fn convert_pairwise<IP, OP>(val: IP) -> OP
	where
		IP: PackedField + WithUnderlier,
		OP: PackedField<Scalar: From<IP::Scalar>> + WithUnderlier<Underlier = IP::Underlier>,
	{
		OP::from_fn(|i| val.get(i).into())
	}

	proptest! {
		#[test]
		fn test_aes_to_binary_packed_transform_128(val in 0u128..) {
			let transform = make_aes_to_binary_packed_transformer::<PackedAESBinaryField4x32b, PackedBinaryField4x32b>();
			let input = PackedAESBinaryField4x32b::from(val);
			let result = transform.transform(&input);
			assert_eq!(result, convert_pairwise(input));
		}

		#[test]
		fn test_binary_to_aes_packed_transform_128(val in 0u128..) {
			let transform = make_binary_to_aes_packed_transformer::<PackedBinaryField4x32b, PackedAESBinaryField4x32b>();
			let input = PackedBinaryField4x32b::from(val);
			let result = transform.transform(&input);
			assert_eq!(result, convert_pairwise(input));
		}

		#[test]
		fn test_aes_to_binary_packed_transform_256(val in any::<[u128; 2]>()) {
			let transform = make_aes_to_binary_packed_transformer::<PackedAESBinaryField8x32b, PackedBinaryField8x32b>();
			let input = PackedAESBinaryField8x32b::from(val);
			let result = transform.transform(&input);
			assert_eq!(result, convert_pairwise(input));
		}

		#[test]
		fn test_binary_to_aes_packed_transform_256(val in any::<[u128; 2]>()) {
			let transform = make_binary_to_aes_packed_transformer::<PackedBinaryField8x32b, PackedAESBinaryField8x32b>();
			let input = PackedBinaryField8x32b::from(val);
			let result = transform.transform(&input);
			assert_eq!(result, convert_pairwise(input));
		}

		#[test]
		fn test_aes_to_binary_packed_transform_512(val in any::<[u128; 4]>()) {
			let transform = make_aes_to_binary_packed_transformer::<PackedAESBinaryField16x32b, PackedBinaryField16x32b>();
			let input = PackedAESBinaryField16x32b::from_underlier(val.into());
			let result = transform.transform(&input);
			assert_eq!(result, convert_pairwise(input));
		}

		#[test]
		fn test_binary_to_aes_packed_transform_512(val in any::<[u128; 4]>()) {
			let transform = make_binary_to_aes_packed_transformer::<PackedBinaryField16x32b, PackedAESBinaryField16x32b>();
			let input = PackedBinaryField16x32b::from_underlier(val.into());
			let result = transform.transform(&input);
			assert_eq!(result, convert_pairwise(input));
		}
	}

	#[test]
	fn test_canonical_serialization() {
		let mut buffer = BytesMut::new();
		let mut rng = thread_rng();
		let aes8 = <AESTowerField8b as Field>::random(&mut rng);
		let aes16 = <AESTowerField16b as Field>::random(&mut rng);
		let aes32 = <AESTowerField32b as Field>::random(&mut rng);
		let aes64 = <AESTowerField64b as Field>::random(&mut rng);
		let aes128 = <AESTowerField128b as Field>::random(&mut rng);

		serialize_canonical(aes8, &mut buffer).unwrap();
		serialize_canonical(aes16, &mut buffer).unwrap();
		serialize_canonical(aes32, &mut buffer).unwrap();
		serialize_canonical(aes64, &mut buffer).unwrap();
		serialize_canonical(aes128, &mut buffer).unwrap();

		serialize_canonical(aes128, &mut buffer).unwrap();

		let mut read_buffer = buffer.freeze();

		assert_eq!(deserialize_canonical::<AESTowerField8b, _>(&mut read_buffer).unwrap(), aes8);
		assert_eq!(deserialize_canonical::<AESTowerField16b, _>(&mut read_buffer).unwrap(), aes16);
		assert_eq!(deserialize_canonical::<AESTowerField32b, _>(&mut read_buffer).unwrap(), aes32);
		assert_eq!(deserialize_canonical::<AESTowerField64b, _>(&mut read_buffer).unwrap(), aes64);
		assert_eq!(
			deserialize_canonical::<AESTowerField128b, _>(&mut read_buffer).unwrap(),
			aes128
		);

		assert_eq!(
			deserialize_canonical::<BinaryField128b, _>(&mut read_buffer).unwrap(),
			aes128.into()
		)
	}
}