-
Notifications
You must be signed in to change notification settings - Fork 14
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
polyval: Add runtime PCLMULQDQ detection #11
Closed
+140
−97
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/bash | ||
|
||
set -eux | ||
|
||
# Due to the fact that cargo does not disable default features when we use | ||
# cargo build --all --no-default-features we have to explicitly iterate over | ||
# all crates (see https://github.com/rust-lang/cargo/issues/4753 ) | ||
DIRS=`ls -d */` | ||
TARGET="thumbv7em-none-eabi" | ||
cargo clean | ||
|
||
for dir in $DIRS; do | ||
if [ $dir = "target/" ] | ||
then | ||
continue | ||
fi | ||
|
||
pushd $dir | ||
cargo build --no-default-features --verbose --target $TARGET || { | ||
echo $dir failed | ||
exit 1 | ||
} | ||
popd | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,23 @@ | |
//! | ||
//! [RFC 8452 Section 3]: https://tools.ietf.org/html/rfc8452#section-3 | ||
pub mod backend; | ||
#[cfg(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
mod pclmulqdq; | ||
mod soft; | ||
|
||
use self::backend::Backend; | ||
use core::ops::{Add, Mul}; | ||
#[cfg(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
use self::pclmulqdq::M128i; | ||
use self::soft::U64x2; | ||
|
||
/// Size of GF(2^128) in bytes (16-bytes). | ||
pub const FIELD_SIZE: usize = 16; | ||
|
@@ -27,29 +40,64 @@ pub type Block = [u8; FIELD_SIZE]; | |
|
||
/// POLYVAL field element. | ||
#[derive(Copy, Clone)] | ||
pub struct Element<B: Backend>(B); | ||
pub enum Element { | ||
#[cfg(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
/// (P)CLMUL(QDQ)-accelerated backend on supported x86 architectures | ||
Clmul(M128i), | ||
|
||
impl<B: Backend> Element<B> { | ||
/// Portable software fallback | ||
Soft(U64x2), | ||
} | ||
|
||
impl Element { | ||
/// Load a `FieldElement` from its bytestring representation. | ||
#[cfg(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
pub fn from_bytes(bytes: Block) -> Self { | ||
Element(bytes.into()) | ||
if cfg!(feature = "std") { | ||
if is_x86_feature_detected!("pclmulqdq") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite like that it does not work on |
||
Element::Clmul(bytes.into()) | ||
} else { | ||
Element::Soft(bytes.into()) | ||
} | ||
} else { | ||
Element::Clmul(bytes.into()) | ||
} | ||
} | ||
|
||
/// Serialize this `FieldElement` as a bytestring. | ||
pub fn to_bytes(self) -> Block { | ||
self.0.into() | ||
/// Load a `FieldElement` from its bytestring representation. | ||
#[cfg(not(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
)))] | ||
pub fn from_bytes(bytes: Block) -> Self { | ||
Element::Soft(bytes.into()) | ||
} | ||
} | ||
|
||
impl<B: Backend> Default for Element<B> { | ||
fn default() -> Self { | ||
Self::from_bytes(Block::default()) | ||
/// Serialize this `FieldElement` as a bytestring. | ||
pub fn to_bytes(self) -> Block { | ||
match self { | ||
#[cfg(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
Element::Clmul(m128i) => m128i.into(), | ||
Element::Soft(u64x2) => u64x2.into(), | ||
} | ||
} | ||
} | ||
|
||
#[allow(clippy::suspicious_arithmetic_impl)] | ||
impl<B: Backend> Add for Element<B> { | ||
type Output = Self; | ||
|
||
/// Adds two POLYVAL field elements. | ||
/// | ||
|
@@ -58,16 +106,21 @@ impl<B: Backend> Add for Element<B> { | |
/// > "The sum of any two elements in the field is the result of XORing them." | ||
/// | ||
/// [RFC 8452 Section 3]: https://tools.ietf.org/html/rfc8452#section-3 | ||
fn add(self, rhs: Self) -> Self { | ||
Element(self.0 + rhs.0) | ||
#[allow(clippy::should_implement_trait)] | ||
pub fn add(self, other: Block) -> Self { | ||
match self { | ||
#[cfg(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
Element::Clmul(m128i) => Element::Clmul(m128i + M128i::from(other)), | ||
Element::Soft(u64x2) => Element::Soft(u64x2 + U64x2::from(other)), | ||
} | ||
} | ||
} | ||
|
||
#[allow(clippy::suspicious_arithmetic_impl)] | ||
impl<B: Backend> Mul for Element<B> { | ||
type Output = Self; | ||
|
||
/// Computes POLYVAL multiplication over GF(2^128). | ||
/// Computes carryless POLYVAL multiplication over GF(2^128). | ||
/// | ||
/// From [RFC 8452 Section 3]: | ||
/// | ||
|
@@ -76,13 +129,22 @@ impl<B: Backend> Mul for Element<B> { | |
/// > irreducible polynomial." | ||
/// | ||
/// [RFC 8452 Section 3]: https://tools.ietf.org/html/rfc8452#section-3 | ||
fn mul(self, rhs: Self) -> Self { | ||
Element(self.0 * rhs.0) | ||
pub fn clmul(self, other: Block) -> Self { | ||
match self { | ||
#[cfg(all( | ||
target_feature = "pclmulqdq", | ||
target_feature = "sse2", | ||
target_feature = "sse4.1", | ||
any(target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
Element::Clmul(m128i) => Element::Clmul(m128i * M128i::from(other)), | ||
Element::Soft(u64x2) => Element::Soft(u64x2 * U64x2::from(other)), | ||
} | ||
} | ||
} | ||
|
||
impl<B: Backend> From<B> for Element<B> { | ||
fn from(element: B) -> Element<B> { | ||
Element(element) | ||
impl Default for Element { | ||
fn default() -> Self { | ||
Self::from_bytes(Block::default()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
//! | ||
//! Copyright (c) 2016 Thomas Pornin <[email protected]> | ||
use super::Backend; | ||
use crate::field::Block; | ||
use core::{ | ||
convert::TryInto, | ||
|
@@ -16,8 +15,6 @@ use core::{ | |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub struct U64x2(u64, u64); | ||
|
||
impl Backend for U64x2 {} | ||
|
||
impl From<Block> for U64x2 { | ||
fn from(bytes: Block) -> U64x2 { | ||
U64x2( | ||
|
@@ -29,14 +26,7 @@ impl From<Block> for U64x2 { | |
|
||
impl From<U64x2> for Block { | ||
fn from(u64x2: U64x2) -> Block { | ||
let x: u128 = u64x2.into(); | ||
x.to_le_bytes() | ||
} | ||
} | ||
|
||
impl From<U64x2> for u128 { | ||
fn from(u64x2: U64x2) -> u128 { | ||
u128::from(u64x2.0) | (u128::from(u64x2.1) << 64) | ||
(u128::from(u64x2.0) | (u128::from(u64x2.1) << 64)).to_le_bytes() | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conditional gating this implementation is using is gross. Open to alternatives... perhaps the
cfg-if
crate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried
cfg-if
. Unfortunately it doesn't seem to support nesting, and the resulting code ended up looking just as badThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made things a little better by using
cfg!
. Still a lot of annoying, redundant gating thoughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My kingdom for
cfg_alias
: rust-lang/cargo#7260 (comment)