1use super::Block;
2
3const MOD: u64 = 0b10000111; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
7cpufeatures::new!(target_feature_pclmulqdq, "pclmulqdq");
8
9impl Block {
10 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
14 #[inline]
15 pub fn clmul(&self, rhs: &Self) -> (Self, Self) {
16 if target_feature_pclmulqdq::get() {
17 unsafe {
19 let (low, high) = clmul::clmul128(self.into(), rhs.into());
20 (low.into(), high.into())
21 }
22 } else {
23 let (low, high) = scalar::clmul128(self.into(), rhs.into());
24 (low.into(), high.into())
25 }
26 }
27
28 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
32 #[inline]
33 pub fn clmul(&self, rhs: &Self) -> (Self, Self) {
34 let (low, high) = scalar::clmul128(self.into(), rhs.into());
35 (low.into(), high.into())
36 }
37
38 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
42 #[inline]
43 pub fn gf_mul(&self, rhs: &Self) -> Self {
44 if target_feature_pclmulqdq::get() {
45 unsafe { clmul::gf128_mul(self.into(), rhs.into()).into() }
47 } else {
48 scalar::gf128_mul(self.into(), rhs.into()).into()
49 }
50 }
51
52 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
56 #[inline]
57 pub fn gf_mul(&self, rhs: &Self) -> Self {
58 scalar::gf128_mul(self.into(), rhs.into()).into()
59 }
60
61 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
63 #[inline]
64 pub fn gf_reduce(low: &Self, high: &Self) -> Self {
65 if target_feature_pclmulqdq::get() {
66 unsafe { clmul::gf128_reduce(low.into(), high.into()).into() }
68 } else {
69 scalar::gf128_reduce(low.into(), high.into()).into()
70 }
71 }
72
73 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
75 #[inline]
76 pub fn gf_reduce(low: &Self, high: &Self) -> Self {
77 scalar::gf128_reduce(low.into(), high.into()).into()
78 }
79
80 #[inline]
81 pub fn gf_pow(&self, mut exp: u64) -> Block {
82 let mut s = Block::ONE;
83 let mut pow2 = *self;
84
85 while exp != 0 {
86 if exp & 1 != 0 {
87 s = s.gf_mul(&pow2);
88 }
89 pow2 = pow2.gf_mul(&pow2);
90 exp >>= 1;
91 }
92 s
93 }
94}
95
96#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
97mod clmul {
98 #[cfg(target_arch = "x86")]
99 use std::arch::x86::*;
100 #[cfg(target_arch = "x86_64")]
101 use std::arch::x86_64::*;
102
103 use super::MOD;
104
105 #[target_feature(enable = "pclmulqdq")]
106 #[inline]
107 pub fn gf128_mul(a: __m128i, b: __m128i) -> __m128i {
108 let (low, high) = clmul128(a, b);
109 gf128_reduce(low, high)
110 }
111
112 #[target_feature(enable = "pclmulqdq")]
116 #[inline]
117 pub fn clmul128(a: __m128i, b: __m128i) -> (__m128i, __m128i) {
118 let ab_low = _mm_clmulepi64_si128::<0x00>(a, b);
121 let ab_high = _mm_clmulepi64_si128::<0x11>(a, b);
122 let ab_lohi1 = _mm_clmulepi64_si128::<0x01>(a, b);
123 let ab_lohi2 = _mm_clmulepi64_si128::<0x10>(a, b);
124 let ab_mid = _mm_xor_si128(ab_lohi1, ab_lohi2);
125 let low = _mm_xor_si128(ab_low, _mm_slli_si128::<8>(ab_mid));
126 let high = _mm_xor_si128(ab_high, _mm_srli_si128::<8>(ab_mid));
127 (low, high)
128 }
129
130 #[target_feature(enable = "pclmulqdq")]
131 #[inline]
132 pub fn gf128_reduce(mut low: __m128i, mut high: __m128i) -> __m128i {
133 let modulus = [MOD, 0];
136 let modulus = unsafe { _mm_loadu_si64(modulus.as_ptr().cast()) };
138
139 let tmp = _mm_clmulepi64_si128::<0x01>(high, modulus);
140 let tmp_shifted = _mm_slli_si128::<8>(tmp);
141 low = _mm_xor_si128(low, tmp_shifted);
142 high = _mm_xor_si128(high, tmp_shifted);
143
144 let tmp = _mm_clmulepi64_si128::<0x01>(tmp, modulus);
146 low = _mm_xor_si128(low, tmp);
147
148 let tmp = _mm_clmulepi64_si128::<0x00>(high, modulus);
149 _mm_xor_si128(low, tmp)
150 }
151
152 #[cfg(all(test, target_feature = "pclmulqdq"))]
153 mod test {
154 use std::{arch::x86_64::__m128i, mem::transmute};
155
156 use crate::block::gf128::clmul::{clmul128, gf128_mul, gf128_reduce};
157
158 #[test]
159 fn test_gf128_mul_zero() {
160 unsafe {
161 let a = transmute(0x19831239123916248127031273012381_u128);
162 let b = transmute(0_u128);
163 let exp = 0_u128;
164 let mul = transmute(gf128_mul(a, b));
165 assert_eq!(exp, mul);
166 }
167 }
168
169 #[test]
170 fn test_gf128_mul_onw() {
171 unsafe {
172 let a = transmute(0x19831239123916248127031273012381_u128);
173 let b = transmute(0x1_u128);
174 let exp = 0x19831239123916248127031273012381_u128;
175 let mul = transmute(gf128_mul(a, b));
176 assert_eq!(exp, mul);
177 }
178 }
179
180 #[test]
181 fn test_gf128_mul() {
182 unsafe {
183 let a = transmute(0x19831239123916248127031273012381_u128);
184 let b = transmute(0xabcdef0123456789abcdef0123456789_u128);
185 let exp = 0x63a033d0ed643e85153c50f4268a7d9_u128;
186 let mul = transmute(gf128_mul(a, b));
187 assert_eq!(exp, mul);
188 }
189 }
190
191 #[test]
192 fn test_clmul128() {
193 unsafe {
194 let a: __m128i = transmute(0x19831239123916248127031273012381_u128);
195 let b: __m128i = transmute(0xabcdef0123456789abcdef0123456789_u128);
196 let (low, high) = clmul128(a, b);
197 let [low, high] = transmute([low, high]);
198 let exp_low: u128 = 0xa5de9b50e6db7b5147e92b99ee261809;
199 let exp_high: u128 = 0xf1d6d37d58114afed2addfedd7c77f7;
200 assert_eq!(exp_low, low);
201 assert_eq!(exp_high, high);
202 }
203 }
204
205 #[test]
206 fn test_gf128_reduce() {
207 unsafe {
208 let low: __m128i = transmute(0x0123456789abcdef0123456789abcdef_u128);
210 let high: __m128i = transmute(0xabcdef0123456789abcdef0123456789_u128);
211 let exp = 0xb4b548f1c3c23f86b4b548f1c3c21572_u128;
212 let res: u128 = transmute(gf128_reduce(low, high));
213
214 println!("res: {res:b}");
215 println!("exp: {exp:b}");
216 assert_eq!(exp, res);
217 }
218 }
219 }
220
221 #[cfg(all(is_nightly, test, target_feature = "pclmulqdq"))]
222 mod benches {
223 extern crate test;
224
225 use std::mem::transmute;
226
227 use criterion::black_box;
228 use rand::{Rng, rng};
229 use test::Bencher;
230
231 #[bench]
232 fn bench_gf128_mul(b: &mut Bencher) {
233 let [low, high] = unsafe { transmute(rng().random::<[u128; 2]>()) };
234 b.iter(|| black_box(unsafe { super::gf128_mul(black_box(low), black_box(high)) }));
235 }
236
237 #[bench]
238 fn bench_gf128_reduce(b: &mut Bencher) {
239 let [low, high] = unsafe { transmute(rng().random::<[u128; 2]>()) };
240 b.iter(|| black_box(unsafe { super::gf128_reduce(black_box(low), black_box(high)) }));
241 }
242 }
243}
244
245#[allow(dead_code)]
248mod scalar {
249 #[inline]
250 pub fn gf128_mul(a: u128, b: u128) -> u128 {
251 let (low, high) = clmul128(a, b);
252 gf128_reduce(low, high)
253 }
254
255 #[inline]
259 pub fn clmul128(a: u128, b: u128) -> (u128, u128) {
260 let (a_low, a_high) = (a as u64, (a >> 64) as u64);
261 let (b_low, b_high) = (b as u64, (b >> 64) as u64);
262
263 let ab_low = clmul64(a_low, b_low);
265 let ab_high = clmul64(a_high, b_high);
266 let ab_mid = clmul64(a_low ^ a_high, b_low ^ b_high) ^ ab_low ^ ab_high;
267 let low = ab_low ^ (ab_mid << 64);
268 let high = ab_high ^ (ab_mid >> 64);
269 (low, high)
270 }
271
272 #[inline]
283 fn clmul64(x: u64, y: u64) -> u128 {
284 let x0 = (x & 0x1111_1111_1111_1111) as u128;
285 let x1 = (x & 0x2222_2222_2222_2222) as u128;
286 let x2 = (x & 0x4444_4444_4444_4444) as u128;
287 let x3 = (x & 0x8888_8888_8888_8888) as u128;
288 let y0 = (y & 0x1111_1111_1111_1111) as u128;
289 let y1 = (y & 0x2222_2222_2222_2222) as u128;
290 let y2 = (y & 0x4444_4444_4444_4444) as u128;
291 let y3 = (y & 0x8888_8888_8888_8888) as u128;
292
293 let mut z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1);
294 let mut z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2);
295 let mut z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3);
296 let mut z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0);
297
298 z0 &= 0x1111_1111_1111_1111_1111_1111_1111_1111;
299 z1 &= 0x2222_2222_2222_2222_2222_2222_2222_2222;
300 z2 &= 0x4444_4444_4444_4444_4444_4444_4444_4444;
301 z3 &= 0x8888_8888_8888_8888_8888_8888_8888_8888;
302
303 z0 | z1 | z2 | z3
304 }
305
306 #[inline]
314 pub fn gf128_reduce(low: u128, high: u128) -> u128 {
315 #[inline]
319 fn shift_u128(x: u128, shift: u32) -> (u128, u128) {
320 let overflow = x >> (128 - shift);
322 let lower = x << shift;
323 (overflow, lower)
324 }
325
326 let (ov7, lo7) = shift_u128(high, 7);
332 let (ov2, lo2) = shift_u128(high, 2);
333 let (ov1, lo1) = shift_u128(high, 1);
334 let lo0 = high; let combined_low = lo7 ^ lo2 ^ lo1 ^ lo0;
338 let combined_overflow = ov7 ^ ov2 ^ ov1;
340
341 let reduced_overflow = (combined_overflow << 7)
345 ^ (combined_overflow << 2)
346 ^ (combined_overflow << 1)
347 ^ combined_overflow;
348
349 let poly_contrib = combined_low ^ reduced_overflow;
352
353 low ^ poly_contrib
355 }
356
357 #[cfg(test)]
358 mod tests {
359 use super::{clmul128, gf128_mul, gf128_reduce};
360
361 #[test]
362 fn test_gf128_mul_zero() {
363 let a = 0x19831239123916248127031273012381;
364 let b = 0;
365 let exp = 0;
366 let mul = gf128_mul(a, b);
367 assert_eq!(exp, mul);
368 }
369
370 #[test]
371 fn test_gf128_mul_one() {
372 let a = 0x19831239123916248127031273012381;
373 let b = 1;
374 let exp = 0x19831239123916248127031273012381;
375 let mul = gf128_mul(a, b);
376 assert_eq!(exp, mul);
377 }
378
379 #[test]
380 fn test_gf128_mul() {
381 let a = 0x19831239123916248127031273012381;
382 let b = 0xabcdef0123456789abcdef0123456789;
383 let exp = 0x63a033d0ed643e85153c50f4268a7d9;
384 let mul = gf128_mul(a, b);
385 assert_eq!(exp, mul);
386 }
387
388 #[test]
389 fn test_gf128_reduce_zero() {
390 assert_eq!(gf128_reduce(0, 0), 0);
391 }
392
393 #[test]
394 fn test_gf128_reduce_low_only() {
395 assert_eq!(gf128_reduce(1, 0), 1);
396 assert_eq!(gf128_reduce(0x87, 0), 0x87); assert_eq!(gf128_reduce(0xFFFFFFFFFFFFFFFF, 0), 0xFFFFFFFFFFFFFFFF);
398 }
399
400 #[test]
401 fn test_gf128_reduce_high_only() {
402 assert_eq!(gf128_reduce(0, 1), 0x87);
404 assert_eq!(gf128_reduce(0, 2), 0x87 << 1);
405 assert_eq!(gf128_reduce(0, 3), (0x87 << 1) ^ 0x87);
406
407 assert_eq!(gf128_reduce(0, 1 << 63), 0x87 << 63);
408 }
409
410 #[test]
411 fn test_gf128_reduce_overflow() {
412 let high = u128::MAX; let low = u128::MAX; assert_eq!(gf128_reduce(low, high), 0xffffffffffffffffffffffffffffc071);
415 }
416
417 #[test]
418 fn tests_gf128_reduce() {
419 let low = 0x0123456789abcdef0123456789abcdef;
421 let high = 0xabcdef0123456789abcdef0123456789;
422 let exp = 0xb4b548f1c3c23f86b4b548f1c3c21572;
423 let res = gf128_reduce(low, high);
424
425 println!("res: {res:b}");
426 println!("exp: {exp:b}");
427 assert_eq!(exp, res);
428 }
429
430 #[test]
431 fn test_clmul128() {
432 let a = 0x19831239123916248127031273012381;
433 let b = 0xabcdef0123456789abcdef0123456789;
434 let (low, high) = clmul128(a, b);
435 let exp_low = 0xa5de9b50e6db7b5147e92b99ee261809;
436 let exp_high = 0xf1d6d37d58114afed2addfedd7c77f7;
437 assert_eq!(exp_low, low);
438 assert_eq!(exp_high, high);
439 }
440 }
441
442 #[cfg(all(is_nightly, test))]
443 mod benches {
444 extern crate test;
445
446 use criterion::black_box;
447 use rand::{Rng, rng};
448 use test::Bencher;
449
450 #[bench]
451 fn bench_gf128_mul(b: &mut Bencher) {
452 let [low, high] = rng().random::<[u128; 2]>();
453 b.iter(|| black_box(super::gf128_mul(black_box(low), black_box(high))));
454 }
455
456 #[bench]
457 fn bench_gf128_reduce(b: &mut Bencher) {
458 let [low, high] = rng().random::<[u128; 2]>();
459 b.iter(|| black_box(super::gf128_reduce(black_box(low), black_box(high))));
460 }
461 }
462}
463
464#[cfg(all(test, not(miri), target_feature = "pclmulqdq"))]
467mod scalar_simd_tests {
468 use std::mem::transmute;
469
470 use rand::{Rng, rng};
471
472 use super::{clmul, scalar};
473
474 #[test]
475 fn test_clmul128() {
476 for _ in 0..1000 {
477 let (a, b) = rng().random::<(u128, u128)>();
478 unsafe {
479 let clmul_res = clmul::clmul128(transmute(a), transmute(b));
480 let scalar_res = scalar::clmul128(a, b);
481 assert_eq!(scalar_res.0, transmute(clmul_res.0));
482 }
483 }
484 }
485
486 #[test]
487 fn test_gf128_reduce() {
488 for _ in 0..1000 {
489 let (a, b) = rng().random::<(u128, u128)>();
490 unsafe {
491 let clmul_res = clmul::gf128_reduce(transmute(a), transmute(b));
492 let scalar_res = scalar::gf128_reduce(a, b);
493 assert_eq!(scalar_res, transmute(clmul_res));
494 }
495 }
496 }
497
498 #[test]
499 fn test_gf128_mul() {
500 for _ in 0..1000 {
501 let (a, b) = rng().random::<(u128, u128)>();
502 unsafe {
503 let clmul_res = clmul::gf128_mul(transmute(a), transmute(b));
504 let scalar_res = scalar::gf128_mul(a, b);
505 assert_eq!(scalar_res, transmute(clmul_res));
506 }
507 }
508 }
509}
510
511#[cfg(test)]
512mod tests {
513 use crate::Block;
514
515 #[test]
516 fn test_gf_pow() {
517 let b: Block = 24646523424323_u128.into();
518 assert_eq!(Block::ONE, b.gf_pow(0));
519 assert_eq!(b, b.gf_pow(1));
520 assert_eq!(b.gf_mul(&b), b.gf_pow(2));
521 assert_eq!(b.gf_mul(&b.gf_mul(&b)), b.gf_pow(3));
522 }
523}