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

polyval: Simplify implementation #12

Merged
merged 1 commit into from
Sep 19, 2019
Merged
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
47 changes: 32 additions & 15 deletions polyval/src/field.rs
Original file line number Diff line number Diff line change
@@ -14,11 +14,36 @@
//!
//! [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;

#[allow(unused_imports)]
use self::soft::U64x2;

#[cfg(not(all(
target_feature = "pclmulqdq",
target_feature = "sse2",
target_feature = "sse4.1",
any(target_arch = "x86", target_arch = "x86_64")
)))]
type M128i = U64x2;

/// Size of GF(2^128) in bytes (16-bytes).
pub const FIELD_SIZE: usize = 16;

@@ -27,9 +52,9 @@ pub type Block = [u8; FIELD_SIZE];

/// POLYVAL field element.
#[derive(Copy, Clone)]
pub struct Element<B: Backend>(B);
pub struct Element(M128i);

impl<B: Backend> Element<B> {
impl Element {
/// Load a `FieldElement` from its bytestring representation.
pub fn from_bytes(bytes: Block) -> Self {
Element(bytes.into())
@@ -41,14 +66,13 @@ impl<B: Backend> Element<B> {
}
}

impl<B: Backend> Default for Element<B> {
impl Default for Element {
fn default() -> Self {
Self::from_bytes(Block::default())
}
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl<B: Backend> Add for Element<B> {
impl Add for Element {
type Output = Self;

/// Adds two POLYVAL field elements.
@@ -63,8 +87,7 @@ impl<B: Backend> Add for Element<B> {
}
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl<B: Backend> Mul for Element<B> {
impl Mul for Element {
type Output = Self;

/// Computes POLYVAL multiplication over GF(2^128).
@@ -80,9 +103,3 @@ impl<B: Backend> Mul for Element<B> {
Element(self.0 * rhs.0)
}
}

impl<B: Backend> From<B> for Element<B> {
fn from(element: B) -> Element<B> {
Element(element)
}
}
31 changes: 0 additions & 31 deletions polyval/src/field/backend.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
//! Field arithmetic backends

#[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 super::Block;
use core::ops::{Add, Mul};

// TODO(tarcieri): runtime selection of PCLMULQDQ based on CPU features

#[cfg(all(
target_feature = "pclmulqdq",
target_feature = "sse2",
target_feature = "sse4.1",
any(target_arch = "x86", target_arch = "x86_64")
))]
pub(crate) use self::pclmulqdq::M128i;

#[allow(unused_imports)]
pub(crate) use self::soft::U64x2;

#[cfg(not(all(
target_feature = "pclmulqdq",
target_feature = "sse2",
target_feature = "sse4.1",
any(target_arch = "x86", target_arch = "x86_64")
)))]
pub(crate) type M128i = U64x2;

/// Field arithmetic backend
pub trait Backend:
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

use super::Backend;
use crate::field::Block;
use core::ops::{Add, Mul};

@@ -15,8 +14,6 @@ use core::ops::{Add, Mul};
#[derive(Copy, Clone)]
pub struct M128i(__m128i);

impl Backend for M128i {}

impl From<Block> for M128i {
// `_mm_loadu_si128` performs an unaligned load
#[allow(clippy::cast_ptr_alignment)]
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(
7 changes: 2 additions & 5 deletions polyval/src/lib.rs
Original file line number Diff line number Diff line change
@@ -53,19 +53,16 @@ pub use universal_hash;
use universal_hash::generic_array::{typenum::U16, GenericArray};
use universal_hash::{Output, UniversalHash};

// TODO(tarcieri): runtime selection of CLMUL vs soft backend when both are available
use field::backend::M128i;

/// **POLYVAL**: GHASH-like universal hash over GF(2^128).
#[allow(non_snake_case)]
#[derive(Clone)]
#[repr(align(16))]
pub struct Polyval {
/// GF(2^128) field element input blocks are multiplied by
H: field::Element<M128i>,
H: field::Element,

/// Field element representing the computed universal hash
S: field::Element<M128i>,
S: field::Element,
}

impl UniversalHash for Polyval {