Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using x86 intrinsics for carry-less multiplication #544

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/ff/galois_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
use bitvec::prelude::{BitArr, Lsb0};
use generic_array::GenericArray;
use typenum::{Unsigned, U1, U4, U5};
use core::arch::x86_64::{_mm_clmulepi64_si128, _mm_set_epi64x, _mm_extract_epi64};

// Bit store type definitions
type U8_1 = BitArr!(for 8, in u8, Lsb0);
Expand Down Expand Up @@ -171,12 +172,17 @@ macro_rules! bit_array_impl {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
debug_assert!(2 * $bits < u128::BITS);
let a = <Self as GaloisField>::as_u128(self);
let mut product = 0;
for i in 0..Self::BITS {
let bit = u128::from(rhs[i]);
product ^= bit * (a << i);
}
let a = _mm_set_epi64x(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be wrong but I think this wouldn't work on ARM because it uses different instruction set for carry-less multiplies. Lots of folks use M1 Mac (ARM) so we probably need to support both x86 and ARM and define 3 sets of multiplication instructions: no clmul support (basically what we had before), x86 with clmul support and ARM with clmul support.

In other words, something like this should be enough

#[cfg(all(
    target_arch = "x86_64",
    target_feature = "pclmulqdq"
))]
fn mul(a: 64, b: 64) -> u128 // or [u64; 2]
{
   ... Intel intrinsics  
}

#[cfg(all(
    target_arch="aarch64",
    target_feature="neon"
)]
fn mul(a: 64, b: 64) -> u128 // or [u64; 2]
{
   ... ARM intrinsics  
}

#[cfg(not(any(all(
    target_arch="aarch64",
    target_feature="neon"), all(
    target_arch = "x86_64",
    target_feature = "pclmulqdq"
)))]
fn mul(a: 64, b: 64) -> u128 // or [u64; 2] 
{
   no hardware support, standard multiply
}

some reference materials and I would be happy to help if needed:
rust-lang/stdarch#318
https://github.com/geky/gf256/blob/master/src/xmul.rs

0,
i64::try_from(<Self as GaloisField>::as_u128(self)).unwrap(),
);
let b = _mm_set_epi64x(
0,
i64::try_from(<Self as GaloisField>::as_u128(rhs)).unwrap(),
);
let intrinsic_result = _mm_clmulepi64_si128(a, b, 0);
let intermediate_result = _mm_extract_epi64(intrinsic_result, 0);
let mut product = u128::try_from(intermediate_result).unwrap();

let poly = <Self as GaloisField>::POLYNOMIAL;
while (u128::BITS - product.leading_zeros()) > $bits {
Expand Down