Skip to content

Commit

Permalink
Merge pull request #12 from RustCrypto/polyval/simplify-implementation
Browse files Browse the repository at this point in the history
polyval: Simplify implementation
  • Loading branch information
tarcieri committed Sep 19, 2019
2 parents 1e7d0bd + f6a33ae commit 0f3a9bf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 57 deletions.
47 changes: 32 additions & 15 deletions polyval/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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())
Expand All @@ -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.
Expand All @@ -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).
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//!
//! Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>

use super::Backend;
use crate::field::Block;
use core::{
convert::TryInto,
Expand All @@ -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(
Expand Down
7 changes: 2 additions & 5 deletions polyval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0f3a9bf

Please sign in to comment.