binius_field/
ghash.rs

1// Copyright 2023-2025 Irreducible Inc.
2
3//! Binary field implementation of GF(2^128) with a modulus of X^128 + X^7 + X^2 + X + 1.
4//! This is the GHASH field used in AES-GCM.
5
6use std::{
7	any::TypeId,
8	fmt::{self, Debug, Display, Formatter},
9	iter::{Product, Sum},
10	ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
11};
12
13use binius_utils::{
14	DeserializeBytes, SerializationError, SerializeBytes,
15	bytes::{Buf, BufMut},
16	iter::IterExtensions,
17};
18use bytemuck::{Pod, Zeroable};
19use rand::{
20	Rng,
21	distr::{Distribution, StandardUniform},
22};
23
24use super::{
25	arithmetic_traits::InvertOrZero,
26	binary_field::{BinaryField, BinaryField1b, TowerField},
27	extension::ExtensionField,
28	underlier::WithUnderlier,
29};
30use crate::{
31	AESTowerField8b, Field,
32	arch::packed_ghash_128::PackedBinaryGhash1x128b,
33	arithmetic_traits::Square,
34	binary_field_arithmetic::{
35		invert_or_zero_using_packed, multiple_using_packed, square_using_packed,
36	},
37	transpose::square_transforms_extension_field,
38	underlier::{Divisible, NumCast, U1, UnderlierWithBitOps},
39};
40
41#[derive(
42	Default,
43	Clone,
44	Copy,
45	PartialEq,
46	Eq,
47	PartialOrd,
48	Ord,
49	Hash,
50	Zeroable,
51	bytemuck::TransparentWrapper,
52)]
53#[repr(transparent)]
54pub struct BinaryField128bGhash(pub(crate) u128);
55
56impl BinaryField128bGhash {
57	#[inline]
58	pub const fn new(value: u128) -> Self {
59		Self(value)
60	}
61
62	#[inline]
63	pub const fn val(self) -> u128 {
64		self.0
65	}
66
67	#[inline]
68	pub fn mul_x(self) -> Self {
69		let val = self.to_underlier();
70		let shifted = val << 1;
71
72		// GHASH irreducible polynomial: x^128 + x^7 + x^2 + x + 1
73		// When the high bit is set, we need to XOR with the reduction polynomial 0x87
74		// All 1s if the top bit is set, all 0s otherwise
75		let mask = (val >> 127).wrapping_neg();
76		let result = shifted ^ (0x87 & mask);
77
78		Self::from_underlier(result)
79	}
80
81	#[inline]
82	pub fn mul_inv_x(self) -> Self {
83		let val = self.to_underlier();
84		let shifted = val >> 1;
85
86		// If low bit was set, we need to add compensation for the remainder
87		// When dividing by x with remainder 1, we add x^(-1) = x^127 to the result
88		// Since x^128 ≡ x^7 + x^2 + x + 1, we have x^127 ≡ x^6 + x + 1
89		// So 0x43 = x^6 + x + 1 (bits 6, 1, 0) and we set bit 127 for the x^127 term
90		// All 1s if the bottom bit is set, all 0s otherwise
91		let mask = (val & 1).wrapping_neg();
92		let result = shifted ^ (((1u128 << 127) | 0x43) & mask);
93
94		Self::from_underlier(result)
95	}
96}
97
98unsafe impl WithUnderlier for BinaryField128bGhash {
99	type Underlier = u128;
100}
101
102impl Neg for BinaryField128bGhash {
103	type Output = Self;
104
105	#[inline]
106	fn neg(self) -> Self::Output {
107		self
108	}
109}
110
111impl Add<Self> for BinaryField128bGhash {
112	type Output = Self;
113
114	#[allow(clippy::suspicious_arithmetic_impl)]
115	fn add(self, rhs: Self) -> Self::Output {
116		Self(self.0 ^ rhs.0)
117	}
118}
119
120impl Add<&Self> for BinaryField128bGhash {
121	type Output = Self;
122
123	#[allow(clippy::suspicious_arithmetic_impl)]
124	fn add(self, rhs: &Self) -> Self::Output {
125		Self(self.0 ^ rhs.0)
126	}
127}
128
129impl Sub<Self> for BinaryField128bGhash {
130	type Output = Self;
131
132	#[allow(clippy::suspicious_arithmetic_impl)]
133	fn sub(self, rhs: Self) -> Self::Output {
134		Self(self.0 ^ rhs.0)
135	}
136}
137
138impl Sub<&Self> for BinaryField128bGhash {
139	type Output = Self;
140
141	#[allow(clippy::suspicious_arithmetic_impl)]
142	fn sub(self, rhs: &Self) -> Self::Output {
143		Self(self.0 ^ rhs.0)
144	}
145}
146
147impl Mul<Self> for BinaryField128bGhash {
148	type Output = Self;
149
150	#[inline]
151	fn mul(self, rhs: Self) -> Self::Output {
152		multiple_using_packed::<PackedBinaryGhash1x128b>(self, rhs)
153	}
154}
155
156impl Mul<&Self> for BinaryField128bGhash {
157	type Output = Self;
158
159	#[inline]
160	fn mul(self, rhs: &Self) -> Self::Output {
161		self * *rhs
162	}
163}
164
165impl AddAssign<Self> for BinaryField128bGhash {
166	#[inline]
167	fn add_assign(&mut self, rhs: Self) {
168		*self = *self + rhs;
169	}
170}
171
172impl AddAssign<&Self> for BinaryField128bGhash {
173	#[inline]
174	fn add_assign(&mut self, rhs: &Self) {
175		*self = *self + rhs;
176	}
177}
178
179impl SubAssign<Self> for BinaryField128bGhash {
180	#[inline]
181	fn sub_assign(&mut self, rhs: Self) {
182		*self = *self - rhs;
183	}
184}
185
186impl SubAssign<&Self> for BinaryField128bGhash {
187	#[inline]
188	fn sub_assign(&mut self, rhs: &Self) {
189		*self = *self - rhs;
190	}
191}
192
193impl MulAssign<Self> for BinaryField128bGhash {
194	#[inline]
195	fn mul_assign(&mut self, rhs: Self) {
196		*self = *self * rhs;
197	}
198}
199
200impl MulAssign<&Self> for BinaryField128bGhash {
201	#[inline]
202	fn mul_assign(&mut self, rhs: &Self) {
203		*self = *self * rhs;
204	}
205}
206
207impl Sum<Self> for BinaryField128bGhash {
208	#[inline]
209	fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
210		iter.fold(Self::ZERO, |acc, x| acc + x)
211	}
212}
213
214impl<'a> Sum<&'a Self> for BinaryField128bGhash {
215	#[inline]
216	fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
217		iter.fold(Self::ZERO, |acc, x| acc + x)
218	}
219}
220
221impl Product<Self> for BinaryField128bGhash {
222	#[inline]
223	fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
224		iter.fold(Self::ONE, |acc, x| acc * x)
225	}
226}
227
228impl<'a> Product<&'a Self> for BinaryField128bGhash {
229	#[inline]
230	fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
231		iter.fold(Self::ONE, |acc, x| acc * x)
232	}
233}
234
235impl Square for BinaryField128bGhash {
236	#[inline]
237	fn square(self) -> Self {
238		square_using_packed::<PackedBinaryGhash1x128b>(self)
239	}
240}
241
242impl Field for BinaryField128bGhash {
243	const ZERO: Self = Self(0);
244	const ONE: Self = Self(1);
245	const CHARACTERISTIC: usize = 2;
246	const MULTIPLICATIVE_GENERATOR: Self = Self(0x494ef99794d5244f9152df59d87a9186);
247
248	fn double(&self) -> Self {
249		Self(0)
250	}
251}
252
253impl Distribution<BinaryField128bGhash> for StandardUniform {
254	fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BinaryField128bGhash {
255		BinaryField128bGhash(rng.random())
256	}
257}
258
259impl InvertOrZero for BinaryField128bGhash {
260	#[inline]
261	fn invert_or_zero(self) -> Self {
262		invert_or_zero_using_packed::<PackedBinaryGhash1x128b>(self)
263	}
264}
265
266impl From<u128> for BinaryField128bGhash {
267	#[inline]
268	fn from(value: u128) -> Self {
269		Self(value)
270	}
271}
272
273impl From<BinaryField128bGhash> for u128 {
274	#[inline]
275	fn from(value: BinaryField128bGhash) -> Self {
276		value.0
277	}
278}
279
280impl Display for BinaryField128bGhash {
281	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
282		write!(f, "0x{repr:0>32x}", repr = self.0)
283	}
284}
285
286impl Debug for BinaryField128bGhash {
287	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
288		write!(f, "BinaryField128bGhash({self})")
289	}
290}
291
292unsafe impl Pod for BinaryField128bGhash {}
293
294impl TryInto<BinaryField1b> for BinaryField128bGhash {
295	type Error = ();
296
297	#[inline]
298	fn try_into(self) -> Result<BinaryField1b, Self::Error> {
299		if self == Self::ZERO {
300			Ok(BinaryField1b::ZERO)
301		} else if self == Self::ONE {
302			Ok(BinaryField1b::ONE)
303		} else {
304			Err(())
305		}
306	}
307}
308
309impl From<BinaryField1b> for BinaryField128bGhash {
310	#[inline]
311	fn from(value: BinaryField1b) -> Self {
312		debug_assert_eq!(Self::ZERO, Self(0));
313
314		Self(Self::ONE.0 & u128::fill_with_bit(value.val().val()))
315	}
316}
317
318impl Add<BinaryField1b> for BinaryField128bGhash {
319	type Output = Self;
320
321	#[inline]
322	fn add(self, rhs: BinaryField1b) -> Self::Output {
323		self + Self::from(rhs)
324	}
325}
326
327impl Sub<BinaryField1b> for BinaryField128bGhash {
328	type Output = Self;
329
330	#[inline]
331	fn sub(self, rhs: BinaryField1b) -> Self::Output {
332		self - Self::from(rhs)
333	}
334}
335
336impl Mul<BinaryField1b> for BinaryField128bGhash {
337	type Output = Self;
338
339	#[inline]
340	#[allow(clippy::suspicious_arithmetic_impl)]
341	fn mul(self, rhs: BinaryField1b) -> Self::Output {
342		crate::tracing::trace_multiplication!(BinaryField128bGhash, BinaryField1b);
343
344		Self(self.0 & u128::fill_with_bit(u8::from(rhs.0)))
345	}
346}
347
348impl AddAssign<BinaryField1b> for BinaryField128bGhash {
349	#[inline]
350	fn add_assign(&mut self, rhs: BinaryField1b) {
351		*self = *self + rhs;
352	}
353}
354
355impl SubAssign<BinaryField1b> for BinaryField128bGhash {
356	#[inline]
357	fn sub_assign(&mut self, rhs: BinaryField1b) {
358		*self = *self - rhs;
359	}
360}
361
362impl MulAssign<BinaryField1b> for BinaryField128bGhash {
363	#[inline]
364	fn mul_assign(&mut self, rhs: BinaryField1b) {
365		*self = *self * rhs;
366	}
367}
368
369impl Add<BinaryField128bGhash> for BinaryField1b {
370	type Output = BinaryField128bGhash;
371
372	#[inline]
373	fn add(self, rhs: BinaryField128bGhash) -> Self::Output {
374		rhs + self
375	}
376}
377
378impl Sub<BinaryField128bGhash> for BinaryField1b {
379	type Output = BinaryField128bGhash;
380
381	#[inline]
382	fn sub(self, rhs: BinaryField128bGhash) -> Self::Output {
383		rhs - self
384	}
385}
386
387impl Mul<BinaryField128bGhash> for BinaryField1b {
388	type Output = BinaryField128bGhash;
389
390	#[inline]
391	fn mul(self, rhs: BinaryField128bGhash) -> Self::Output {
392		rhs * self
393	}
394}
395
396impl ExtensionField<BinaryField1b> for BinaryField128bGhash {
397	const LOG_DEGREE: usize = 7;
398
399	#[inline]
400	fn basis(i: usize) -> Self {
401		assert!(i < 128, "index {i} out of range for degree 128");
402		Self::new(1 << i)
403	}
404
405	#[inline]
406	fn from_bases_sparse(
407		base_elems: impl IntoIterator<Item = BinaryField1b>,
408		log_stride: usize,
409	) -> Self {
410		assert!(log_stride == 7, "log_stride must be 7 for BinaryField128bGhash");
411		let value = base_elems
412			.into_iter()
413			.enumerate()
414			.fold(0, |value, (i, elem)| value | (u128::from(elem.0) << i));
415		Self::new(value)
416	}
417
418	#[inline]
419	fn iter_bases(&self) -> impl Iterator<Item = BinaryField1b> {
420		Divisible::<U1>::value_iter(self.0).map_skippable(BinaryField1b::from)
421	}
422
423	#[inline]
424	fn into_iter_bases(self) -> impl Iterator<Item = BinaryField1b> {
425		Divisible::<U1>::value_iter(self.0).map_skippable(BinaryField1b::from)
426	}
427
428	#[inline]
429	unsafe fn get_base_unchecked(&self, i: usize) -> BinaryField1b {
430		BinaryField1b(U1::num_cast_from(self.0 >> i))
431	}
432
433	#[inline]
434	fn square_transpose(values: &mut [Self]) {
435		square_transforms_extension_field::<BinaryField1b, Self>(values)
436	}
437}
438
439impl SerializeBytes for BinaryField128bGhash {
440	fn serialize(&self, write_buf: impl BufMut) -> Result<(), SerializationError> {
441		self.0.serialize(write_buf)
442	}
443}
444
445impl DeserializeBytes for BinaryField128bGhash {
446	fn deserialize(read_buf: impl Buf) -> Result<Self, SerializationError>
447	where
448		Self: Sized,
449	{
450		Ok(Self(DeserializeBytes::deserialize(read_buf)?))
451	}
452}
453
454impl BinaryField for BinaryField128bGhash {}
455
456impl TowerField for BinaryField128bGhash {
457	fn min_tower_level(self) -> usize {
458		match self {
459			Self::ZERO | Self::ONE => 0,
460			_ => 7,
461		}
462	}
463
464	fn mul_primitive(self, _iota: usize) -> Self {
465		// This method could be implemented by multiplying by isomorphic alpha value
466		// But it's not being used as for now
467		unimplemented!()
468	}
469}
470
471impl From<AESTowerField8b> for BinaryField128bGhash {
472	fn from(value: AESTowerField8b) -> Self {
473		const LOOKUP_TABLE: [BinaryField128bGhash; 256] = [
474			BinaryField128bGhash(0x00000000000000000000000000000000),
475			BinaryField128bGhash(0x00000000000000000000000000000001),
476			BinaryField128bGhash(0x0dcb364640a222fe6b8330483c2e9849),
477			BinaryField128bGhash(0x0dcb364640a222fe6b8330483c2e9848),
478			BinaryField128bGhash(0x3d5bd35c94646a247573da4a5f7710ed),
479			BinaryField128bGhash(0x3d5bd35c94646a247573da4a5f7710ec),
480			BinaryField128bGhash(0x3090e51ad4c648da1ef0ea02635988a4),
481			BinaryField128bGhash(0x3090e51ad4c648da1ef0ea02635988a5),
482			BinaryField128bGhash(0x6d58c4e181f9199f41a12db1f974f3ac),
483			BinaryField128bGhash(0x6d58c4e181f9199f41a12db1f974f3ad),
484			BinaryField128bGhash(0x6093f2a7c15b3b612a221df9c55a6be5),
485			BinaryField128bGhash(0x6093f2a7c15b3b612a221df9c55a6be4),
486			BinaryField128bGhash(0x500317bd159d73bb34d2f7fba603e341),
487			BinaryField128bGhash(0x500317bd159d73bb34d2f7fba603e340),
488			BinaryField128bGhash(0x5dc821fb553f51455f51c7b39a2d7b08),
489			BinaryField128bGhash(0x5dc821fb553f51455f51c7b39a2d7b09),
490			BinaryField128bGhash(0xa72ec17764d7ced55e2f716f4ede412f),
491			BinaryField128bGhash(0xa72ec17764d7ced55e2f716f4ede412e),
492			BinaryField128bGhash(0xaae5f7312475ec2b35ac412772f0d966),
493			BinaryField128bGhash(0xaae5f7312475ec2b35ac412772f0d967),
494			BinaryField128bGhash(0x9a75122bf0b3a4f12b5cab2511a951c2),
495			BinaryField128bGhash(0x9a75122bf0b3a4f12b5cab2511a951c3),
496			BinaryField128bGhash(0x97be246db011860f40df9b6d2d87c98b),
497			BinaryField128bGhash(0x97be246db011860f40df9b6d2d87c98a),
498			BinaryField128bGhash(0xca760596e52ed74a1f8e5cdeb7aab283),
499			BinaryField128bGhash(0xca760596e52ed74a1f8e5cdeb7aab282),
500			BinaryField128bGhash(0xc7bd33d0a58cf5b4740d6c968b842aca),
501			BinaryField128bGhash(0xc7bd33d0a58cf5b4740d6c968b842acb),
502			BinaryField128bGhash(0xf72dd6ca714abd6e6afd8694e8dda26e),
503			BinaryField128bGhash(0xf72dd6ca714abd6e6afd8694e8dda26f),
504			BinaryField128bGhash(0xfae6e08c31e89f90017eb6dcd4f33a27),
505			BinaryField128bGhash(0xfae6e08c31e89f90017eb6dcd4f33a26),
506			BinaryField128bGhash(0x4d52354a3a3d8c865cb10fbabcf00118),
507			BinaryField128bGhash(0x4d52354a3a3d8c865cb10fbabcf00119),
508			BinaryField128bGhash(0x4099030c7a9fae7837323ff280de9951),
509			BinaryField128bGhash(0x4099030c7a9fae7837323ff280de9950),
510			BinaryField128bGhash(0x7009e616ae59e6a229c2d5f0e38711f5),
511			BinaryField128bGhash(0x7009e616ae59e6a229c2d5f0e38711f4),
512			BinaryField128bGhash(0x7dc2d050eefbc45c4241e5b8dfa989bc),
513			BinaryField128bGhash(0x7dc2d050eefbc45c4241e5b8dfa989bd),
514			BinaryField128bGhash(0x200af1abbbc495191d10220b4584f2b4),
515			BinaryField128bGhash(0x200af1abbbc495191d10220b4584f2b5),
516			BinaryField128bGhash(0x2dc1c7edfb66b7e77693124379aa6afd),
517			BinaryField128bGhash(0x2dc1c7edfb66b7e77693124379aa6afc),
518			BinaryField128bGhash(0x1d5122f72fa0ff3d6863f8411af3e259),
519			BinaryField128bGhash(0x1d5122f72fa0ff3d6863f8411af3e258),
520			BinaryField128bGhash(0x109a14b16f02ddc303e0c80926dd7a10),
521			BinaryField128bGhash(0x109a14b16f02ddc303e0c80926dd7a11),
522			BinaryField128bGhash(0xea7cf43d5eea4253029e7ed5f22e4037),
523			BinaryField128bGhash(0xea7cf43d5eea4253029e7ed5f22e4036),
524			BinaryField128bGhash(0xe7b7c27b1e4860ad691d4e9dce00d87e),
525			BinaryField128bGhash(0xe7b7c27b1e4860ad691d4e9dce00d87f),
526			BinaryField128bGhash(0xd7272761ca8e287777eda49fad5950da),
527			BinaryField128bGhash(0xd7272761ca8e287777eda49fad5950db),
528			BinaryField128bGhash(0xdaec11278a2c0a891c6e94d79177c893),
529			BinaryField128bGhash(0xdaec11278a2c0a891c6e94d79177c892),
530			BinaryField128bGhash(0x872430dcdf135bcc433f53640b5ab39b),
531			BinaryField128bGhash(0x872430dcdf135bcc433f53640b5ab39a),
532			BinaryField128bGhash(0x8aef069a9fb1793228bc632c37742bd2),
533			BinaryField128bGhash(0x8aef069a9fb1793228bc632c37742bd3),
534			BinaryField128bGhash(0xba7fe3804b7731e8364c892e542da376),
535			BinaryField128bGhash(0xba7fe3804b7731e8364c892e542da377),
536			BinaryField128bGhash(0xb7b4d5c60bd513165dcfb96668033b3f),
537			BinaryField128bGhash(0xb7b4d5c60bd513165dcfb96668033b3e),
538			BinaryField128bGhash(0x553e92e8bc0ae9a795ed1f57f3632d4d),
539			BinaryField128bGhash(0x553e92e8bc0ae9a795ed1f57f3632d4c),
540			BinaryField128bGhash(0x58f5a4aefca8cb59fe6e2f1fcf4db504),
541			BinaryField128bGhash(0x58f5a4aefca8cb59fe6e2f1fcf4db505),
542			BinaryField128bGhash(0x686541b4286e8383e09ec51dac143da0),
543			BinaryField128bGhash(0x686541b4286e8383e09ec51dac143da1),
544			BinaryField128bGhash(0x65ae77f268cca17d8b1df555903aa5e9),
545			BinaryField128bGhash(0x65ae77f268cca17d8b1df555903aa5e8),
546			BinaryField128bGhash(0x386656093df3f038d44c32e60a17dee1),
547			BinaryField128bGhash(0x386656093df3f038d44c32e60a17dee0),
548			BinaryField128bGhash(0x35ad604f7d51d2c6bfcf02ae363946a8),
549			BinaryField128bGhash(0x35ad604f7d51d2c6bfcf02ae363946a9),
550			BinaryField128bGhash(0x053d8555a9979a1ca13fe8ac5560ce0c),
551			BinaryField128bGhash(0x053d8555a9979a1ca13fe8ac5560ce0d),
552			BinaryField128bGhash(0x08f6b313e935b8e2cabcd8e4694e5645),
553			BinaryField128bGhash(0x08f6b313e935b8e2cabcd8e4694e5644),
554			BinaryField128bGhash(0xf210539fd8dd2772cbc26e38bdbd6c62),
555			BinaryField128bGhash(0xf210539fd8dd2772cbc26e38bdbd6c63),
556			BinaryField128bGhash(0xffdb65d9987f058ca0415e708193f42b),
557			BinaryField128bGhash(0xffdb65d9987f058ca0415e708193f42a),
558			BinaryField128bGhash(0xcf4b80c34cb94d56beb1b472e2ca7c8f),
559			BinaryField128bGhash(0xcf4b80c34cb94d56beb1b472e2ca7c8e),
560			BinaryField128bGhash(0xc280b6850c1b6fa8d532843adee4e4c6),
561			BinaryField128bGhash(0xc280b6850c1b6fa8d532843adee4e4c7),
562			BinaryField128bGhash(0x9f48977e59243eed8a63438944c99fce),
563			BinaryField128bGhash(0x9f48977e59243eed8a63438944c99fcf),
564			BinaryField128bGhash(0x9283a13819861c13e1e073c178e70787),
565			BinaryField128bGhash(0x9283a13819861c13e1e073c178e70786),
566			BinaryField128bGhash(0xa2134422cd4054c9ff1099c31bbe8f23),
567			BinaryField128bGhash(0xa2134422cd4054c9ff1099c31bbe8f22),
568			BinaryField128bGhash(0xafd872648de276379493a98b2790176a),
569			BinaryField128bGhash(0xafd872648de276379493a98b2790176b),
570			BinaryField128bGhash(0x186ca7a286376521c95c10ed4f932c55),
571			BinaryField128bGhash(0x186ca7a286376521c95c10ed4f932c54),
572			BinaryField128bGhash(0x15a791e4c69547dfa2df20a573bdb41c),
573			BinaryField128bGhash(0x15a791e4c69547dfa2df20a573bdb41d),
574			BinaryField128bGhash(0x253774fe12530f05bc2fcaa710e43cb8),
575			BinaryField128bGhash(0x253774fe12530f05bc2fcaa710e43cb9),
576			BinaryField128bGhash(0x28fc42b852f12dfbd7acfaef2ccaa4f1),
577			BinaryField128bGhash(0x28fc42b852f12dfbd7acfaef2ccaa4f0),
578			BinaryField128bGhash(0x7534634307ce7cbe88fd3d5cb6e7dff9),
579			BinaryField128bGhash(0x7534634307ce7cbe88fd3d5cb6e7dff8),
580			BinaryField128bGhash(0x78ff5505476c5e40e37e0d148ac947b0),
581			BinaryField128bGhash(0x78ff5505476c5e40e37e0d148ac947b1),
582			BinaryField128bGhash(0x486fb01f93aa169afd8ee716e990cf14),
583			BinaryField128bGhash(0x486fb01f93aa169afd8ee716e990cf15),
584			BinaryField128bGhash(0x45a48659d3083464960dd75ed5be575d),
585			BinaryField128bGhash(0x45a48659d3083464960dd75ed5be575c),
586			BinaryField128bGhash(0xbf4266d5e2e0abf497736182014d6d7a),
587			BinaryField128bGhash(0xbf4266d5e2e0abf497736182014d6d7b),
588			BinaryField128bGhash(0xb2895093a242890afcf051ca3d63f533),
589			BinaryField128bGhash(0xb2895093a242890afcf051ca3d63f532),
590			BinaryField128bGhash(0x8219b5897684c1d0e200bbc85e3a7d97),
591			BinaryField128bGhash(0x8219b5897684c1d0e200bbc85e3a7d96),
592			BinaryField128bGhash(0x8fd283cf3626e32e89838b806214e5de),
593			BinaryField128bGhash(0x8fd283cf3626e32e89838b806214e5df),
594			BinaryField128bGhash(0xd21aa2346319b26bd6d24c33f8399ed6),
595			BinaryField128bGhash(0xd21aa2346319b26bd6d24c33f8399ed7),
596			BinaryField128bGhash(0xdfd1947223bb9095bd517c7bc417069f),
597			BinaryField128bGhash(0xdfd1947223bb9095bd517c7bc417069e),
598			BinaryField128bGhash(0xef417168f77dd84fa3a19679a74e8e3b),
599			BinaryField128bGhash(0xef417168f77dd84fa3a19679a74e8e3a),
600			BinaryField128bGhash(0xe28a472eb7dffab1c822a6319b601672),
601			BinaryField128bGhash(0xe28a472eb7dffab1c822a6319b601673),
602			BinaryField128bGhash(0x93252331bf042b11512625b1f09fa87e),
603			BinaryField128bGhash(0x93252331bf042b11512625b1f09fa87f),
604			BinaryField128bGhash(0x9eee1577ffa609ef3aa515f9ccb13037),
605			BinaryField128bGhash(0x9eee1577ffa609ef3aa515f9ccb13036),
606			BinaryField128bGhash(0xae7ef06d2b6041352455fffbafe8b893),
607			BinaryField128bGhash(0xae7ef06d2b6041352455fffbafe8b892),
608			BinaryField128bGhash(0xa3b5c62b6bc263cb4fd6cfb393c620da),
609			BinaryField128bGhash(0xa3b5c62b6bc263cb4fd6cfb393c620db),
610			BinaryField128bGhash(0xfe7de7d03efd328e1087080009eb5bd2),
611			BinaryField128bGhash(0xfe7de7d03efd328e1087080009eb5bd3),
612			BinaryField128bGhash(0xf3b6d1967e5f10707b04384835c5c39b),
613			BinaryField128bGhash(0xf3b6d1967e5f10707b04384835c5c39a),
614			BinaryField128bGhash(0xc326348caa9958aa65f4d24a569c4b3f),
615			BinaryField128bGhash(0xc326348caa9958aa65f4d24a569c4b3e),
616			BinaryField128bGhash(0xceed02caea3b7a540e77e2026ab2d376),
617			BinaryField128bGhash(0xceed02caea3b7a540e77e2026ab2d377),
618			BinaryField128bGhash(0x340be246dbd3e5c40f0954debe41e951),
619			BinaryField128bGhash(0x340be246dbd3e5c40f0954debe41e950),
620			BinaryField128bGhash(0x39c0d4009b71c73a648a6496826f7118),
621			BinaryField128bGhash(0x39c0d4009b71c73a648a6496826f7119),
622			BinaryField128bGhash(0x0950311a4fb78fe07a7a8e94e136f9bc),
623			BinaryField128bGhash(0x0950311a4fb78fe07a7a8e94e136f9bd),
624			BinaryField128bGhash(0x049b075c0f15ad1e11f9bedcdd1861f5),
625			BinaryField128bGhash(0x049b075c0f15ad1e11f9bedcdd1861f4),
626			BinaryField128bGhash(0x595326a75a2afc5b4ea8796f47351afd),
627			BinaryField128bGhash(0x595326a75a2afc5b4ea8796f47351afc),
628			BinaryField128bGhash(0x549810e11a88dea5252b49277b1b82b4),
629			BinaryField128bGhash(0x549810e11a88dea5252b49277b1b82b5),
630			BinaryField128bGhash(0x6408f5fbce4e967f3bdba32518420a10),
631			BinaryField128bGhash(0x6408f5fbce4e967f3bdba32518420a11),
632			BinaryField128bGhash(0x69c3c3bd8eecb4815058936d246c9259),
633			BinaryField128bGhash(0x69c3c3bd8eecb4815058936d246c9258),
634			BinaryField128bGhash(0xde77167b8539a7970d972a0b4c6fa966),
635			BinaryField128bGhash(0xde77167b8539a7970d972a0b4c6fa967),
636			BinaryField128bGhash(0xd3bc203dc59b856966141a437041312f),
637			BinaryField128bGhash(0xd3bc203dc59b856966141a437041312e),
638			BinaryField128bGhash(0xe32cc527115dcdb378e4f0411318b98b),
639			BinaryField128bGhash(0xe32cc527115dcdb378e4f0411318b98a),
640			BinaryField128bGhash(0xeee7f36151ffef4d1367c0092f3621c2),
641			BinaryField128bGhash(0xeee7f36151ffef4d1367c0092f3621c3),
642			BinaryField128bGhash(0xb32fd29a04c0be084c3607bab51b5aca),
643			BinaryField128bGhash(0xb32fd29a04c0be084c3607bab51b5acb),
644			BinaryField128bGhash(0xbee4e4dc44629cf627b537f28935c283),
645			BinaryField128bGhash(0xbee4e4dc44629cf627b537f28935c282),
646			BinaryField128bGhash(0x8e7401c690a4d42c3945ddf0ea6c4a27),
647			BinaryField128bGhash(0x8e7401c690a4d42c3945ddf0ea6c4a26),
648			BinaryField128bGhash(0x83bf3780d006f6d252c6edb8d642d26e),
649			BinaryField128bGhash(0x83bf3780d006f6d252c6edb8d642d26f),
650			BinaryField128bGhash(0x7959d70ce1ee694253b85b6402b1e849),
651			BinaryField128bGhash(0x7959d70ce1ee694253b85b6402b1e848),
652			BinaryField128bGhash(0x7492e14aa14c4bbc383b6b2c3e9f7000),
653			BinaryField128bGhash(0x7492e14aa14c4bbc383b6b2c3e9f7001),
654			BinaryField128bGhash(0x44020450758a036626cb812e5dc6f8a4),
655			BinaryField128bGhash(0x44020450758a036626cb812e5dc6f8a5),
656			BinaryField128bGhash(0x49c93216352821984d48b16661e860ed),
657			BinaryField128bGhash(0x49c93216352821984d48b16661e860ec),
658			BinaryField128bGhash(0x140113ed601770dd121976d5fbc51be5),
659			BinaryField128bGhash(0x140113ed601770dd121976d5fbc51be4),
660			BinaryField128bGhash(0x19ca25ab20b55223799a469dc7eb83ac),
661			BinaryField128bGhash(0x19ca25ab20b55223799a469dc7eb83ad),
662			BinaryField128bGhash(0x295ac0b1f4731af9676aac9fa4b20b08),
663			BinaryField128bGhash(0x295ac0b1f4731af9676aac9fa4b20b09),
664			BinaryField128bGhash(0x2491f6f7b4d138070ce99cd7989c9341),
665			BinaryField128bGhash(0x2491f6f7b4d138070ce99cd7989c9340),
666			BinaryField128bGhash(0xc61bb1d9030ec2b6c4cb3ae603fc8533),
667			BinaryField128bGhash(0xc61bb1d9030ec2b6c4cb3ae603fc8532),
668			BinaryField128bGhash(0xcbd0879f43ace048af480aae3fd21d7a),
669			BinaryField128bGhash(0xcbd0879f43ace048af480aae3fd21d7b),
670			BinaryField128bGhash(0xfb406285976aa892b1b8e0ac5c8b95de),
671			BinaryField128bGhash(0xfb406285976aa892b1b8e0ac5c8b95df),
672			BinaryField128bGhash(0xf68b54c3d7c88a6cda3bd0e460a50d97),
673			BinaryField128bGhash(0xf68b54c3d7c88a6cda3bd0e460a50d96),
674			BinaryField128bGhash(0xab43753882f7db29856a1757fa88769f),
675			BinaryField128bGhash(0xab43753882f7db29856a1757fa88769e),
676			BinaryField128bGhash(0xa688437ec255f9d7eee9271fc6a6eed6),
677			BinaryField128bGhash(0xa688437ec255f9d7eee9271fc6a6eed7),
678			BinaryField128bGhash(0x9618a6641693b10df019cd1da5ff6672),
679			BinaryField128bGhash(0x9618a6641693b10df019cd1da5ff6673),
680			BinaryField128bGhash(0x9bd39022563193f39b9afd5599d1fe3b),
681			BinaryField128bGhash(0x9bd39022563193f39b9afd5599d1fe3a),
682			BinaryField128bGhash(0x613570ae67d90c639ae44b894d22c41c),
683			BinaryField128bGhash(0x613570ae67d90c639ae44b894d22c41d),
684			BinaryField128bGhash(0x6cfe46e8277b2e9df1677bc1710c5c55),
685			BinaryField128bGhash(0x6cfe46e8277b2e9df1677bc1710c5c54),
686			BinaryField128bGhash(0x5c6ea3f2f3bd6647ef9791c31255d4f1),
687			BinaryField128bGhash(0x5c6ea3f2f3bd6647ef9791c31255d4f0),
688			BinaryField128bGhash(0x51a595b4b31f44b98414a18b2e7b4cb8),
689			BinaryField128bGhash(0x51a595b4b31f44b98414a18b2e7b4cb9),
690			BinaryField128bGhash(0x0c6db44fe62015fcdb456638b45637b0),
691			BinaryField128bGhash(0x0c6db44fe62015fcdb456638b45637b1),
692			BinaryField128bGhash(0x01a68209a6823702b0c656708878aff9),
693			BinaryField128bGhash(0x01a68209a6823702b0c656708878aff8),
694			BinaryField128bGhash(0x3136671372447fd8ae36bc72eb21275d),
695			BinaryField128bGhash(0x3136671372447fd8ae36bc72eb21275c),
696			BinaryField128bGhash(0x3cfd515532e65d26c5b58c3ad70fbf14),
697			BinaryField128bGhash(0x3cfd515532e65d26c5b58c3ad70fbf15),
698			BinaryField128bGhash(0x8b49849339334e30987a355cbf0c842b),
699			BinaryField128bGhash(0x8b49849339334e30987a355cbf0c842a),
700			BinaryField128bGhash(0x8682b2d579916ccef3f9051483221c62),
701			BinaryField128bGhash(0x8682b2d579916ccef3f9051483221c63),
702			BinaryField128bGhash(0xb61257cfad572414ed09ef16e07b94c6),
703			BinaryField128bGhash(0xb61257cfad572414ed09ef16e07b94c7),
704			BinaryField128bGhash(0xbbd96189edf506ea868adf5edc550c8f),
705			BinaryField128bGhash(0xbbd96189edf506ea868adf5edc550c8e),
706			BinaryField128bGhash(0xe6114072b8ca57afd9db18ed46787787),
707			BinaryField128bGhash(0xe6114072b8ca57afd9db18ed46787786),
708			BinaryField128bGhash(0xebda7634f8687551b25828a57a56efce),
709			BinaryField128bGhash(0xebda7634f8687551b25828a57a56efcf),
710			BinaryField128bGhash(0xdb4a932e2cae3d8baca8c2a7190f676a),
711			BinaryField128bGhash(0xdb4a932e2cae3d8baca8c2a7190f676b),
712			BinaryField128bGhash(0xd681a5686c0c1f75c72bf2ef2521ff23),
713			BinaryField128bGhash(0xd681a5686c0c1f75c72bf2ef2521ff22),
714			BinaryField128bGhash(0x2c6745e45de480e5c6554433f1d2c504),
715			BinaryField128bGhash(0x2c6745e45de480e5c6554433f1d2c505),
716			BinaryField128bGhash(0x21ac73a21d46a21badd6747bcdfc5d4d),
717			BinaryField128bGhash(0x21ac73a21d46a21badd6747bcdfc5d4c),
718			BinaryField128bGhash(0x113c96b8c980eac1b3269e79aea5d5e9),
719			BinaryField128bGhash(0x113c96b8c980eac1b3269e79aea5d5e8),
720			BinaryField128bGhash(0x1cf7a0fe8922c83fd8a5ae31928b4da0),
721			BinaryField128bGhash(0x1cf7a0fe8922c83fd8a5ae31928b4da1),
722			BinaryField128bGhash(0x413f8105dc1d997a87f4698208a636a8),
723			BinaryField128bGhash(0x413f8105dc1d997a87f4698208a636a9),
724			BinaryField128bGhash(0x4cf4b7439cbfbb84ec7759ca3488aee1),
725			BinaryField128bGhash(0x4cf4b7439cbfbb84ec7759ca3488aee0),
726			BinaryField128bGhash(0x7c6452594879f35ef287b3c857d12645),
727			BinaryField128bGhash(0x7c6452594879f35ef287b3c857d12644),
728			BinaryField128bGhash(0x71af641f08dbd1a0990483806bffbe0c),
729			BinaryField128bGhash(0x71af641f08dbd1a0990483806bffbe0d),
730		];
731
732		LOOKUP_TABLE[value.0 as usize]
733	}
734}
735
736#[inline(always)]
737pub fn is_ghash_tower<F: TowerField>() -> bool {
738	TypeId::of::<F>() == TypeId::of::<BinaryField128bGhash>()
739		|| TypeId::of::<F>() == TypeId::of::<BinaryField1b>()
740}
741
742#[cfg(test)]
743mod tests {
744	use proptest::{prelude::any, proptest};
745
746	use super::*;
747	use crate::binary_field::tests::is_binary_field_valid_generator;
748
749	#[test]
750	fn test_ghash_mul() {
751		let a = BinaryField128bGhash(1u128);
752		let b = BinaryField128bGhash(1u128);
753		let c = a * b;
754
755		assert_eq!(c, BinaryField128bGhash::from(1u128));
756
757		let a = BinaryField128bGhash(1u128);
758		let b = BinaryField128bGhash(2u128);
759		let c = a * b;
760
761		assert_eq!(c, BinaryField128bGhash::from(2u128));
762
763		let a = BinaryField128bGhash(1u128);
764		let b = BinaryField128bGhash(1297182698762987u128);
765		let c = a * b;
766
767		assert_eq!(c, BinaryField128bGhash::from(1297182698762987u128));
768
769		let a = BinaryField128bGhash(2u128);
770		let b = BinaryField128bGhash(2u128);
771		let c = a * b;
772
773		assert_eq!(c, BinaryField128bGhash::from(4u128));
774
775		let a = BinaryField128bGhash(2u128);
776		let b = BinaryField128bGhash(3u128);
777		let c = a * b;
778
779		assert_eq!(c, BinaryField128bGhash::from(6u128));
780
781		let a = BinaryField128bGhash(3u128);
782		let b = BinaryField128bGhash(3u128);
783		let c = a * b;
784
785		assert_eq!(c, BinaryField128bGhash::from(5u128));
786
787		let a = BinaryField128bGhash(1u128 << 127);
788		let b = BinaryField128bGhash(2u128);
789		let c = a * b;
790
791		assert_eq!(c, BinaryField128bGhash::from(0b10000111));
792
793		let a = BinaryField128bGhash((1u128 << 127) + 1);
794		let b = BinaryField128bGhash(2u128);
795		let c = a * b;
796
797		assert_eq!(c, BinaryField128bGhash::from(0b10000101));
798
799		let a = BinaryField128bGhash(3u128 << 126);
800		let b = BinaryField128bGhash(2u128);
801		let c = a * b;
802
803		assert_eq!(c, BinaryField128bGhash::from(0b10000111 + (1u128 << 127)));
804
805		let a = BinaryField128bGhash(1u128 << 127);
806		let b = BinaryField128bGhash(4u128);
807		let c = a * b;
808
809		assert_eq!(c, BinaryField128bGhash::from(0b10000111 << 1));
810
811		let a = BinaryField128bGhash(1u128 << 127);
812		let b = BinaryField128bGhash(1u128 << 122);
813		let c = a * b;
814
815		assert_eq!(c, BinaryField128bGhash::from((0b00000111 << 121) + 0b10000111));
816	}
817
818	#[test]
819	fn test_multiplicative_generator() {
820		assert!(is_binary_field_valid_generator::<BinaryField128bGhash>());
821	}
822
823	#[test]
824	fn test_mul_x() {
825		let test_cases = [
826			0x0,                                    // Zero
827			0x1,                                    // One
828			0x2,                                    // Two
829			0x80000000000000000000000000000000u128, // High bit set
830			0x40000000000000000000000000000000u128, // Second highest bit
831			0xffffffffffffffffffffffffffffffffu128, // All bits set
832			0x87u128,                               // GHASH reduction polynomial
833			0x21ac73a21d46a21badd6747bcdfc5d4d,     // Random value
834		];
835
836		for &value in &test_cases {
837			let field_val = BinaryField128bGhash::new(value);
838			let mul_x_result = field_val.mul_x();
839			let regular_mul_result = field_val * BinaryField128bGhash::new(2u128);
840
841			assert_eq!(
842				mul_x_result, regular_mul_result,
843				"mul_x and regular multiplication by 2 differ for value {:#x}",
844				value
845			);
846		}
847	}
848
849	#[test]
850	fn test_mul_inv_x() {
851		let test_cases = [
852			0x0,                                    // Zero
853			0x1,                                    // One
854			0x2,                                    // Two
855			0x1u128,                                // Low bit set
856			0x3u128,                                // Two lowest bits set
857			0xffffffffffffffffffffffffffffffffu128, // All bits set
858			0x87u128,                               // GHASH reduction polynomial
859			0x21ac73a21d46a21badd6747bcdfc5d4d,     // Random value
860		];
861
862		for &value in &test_cases {
863			let field_val = BinaryField128bGhash::new(value);
864			let mul_inv_x_result = field_val.mul_inv_x();
865			let regular_mul_result = field_val
866				* BinaryField128bGhash::new(2u128)
867					.invert()
868					.expect("2 is invertible");
869
870			assert_eq!(
871				mul_inv_x_result, regular_mul_result,
872				"mul_inv_x and regular multiplication by 2 differ for value {:#x}",
873				value
874			);
875		}
876	}
877
878	proptest! {
879		#[test]
880		fn test_conversion_from_aes_consistency(a in any::<u8>(), b in any::<u8>()) {
881			let a_val = AESTowerField8b::new(a);
882			let b_val = AESTowerField8b::new(b);
883			let converted_a = BinaryField128bGhash::from(a_val);
884			let converted_b = BinaryField128bGhash::from(b_val);
885			assert_eq!(BinaryField128bGhash::from(a_val * b_val), converted_a * converted_b);
886		}
887	}
888}