Skip to content

Commit

Permalink
Create a "fake-SIMD" type to work around rust-lang/rust#23037
Browse files Browse the repository at this point in the history
  • Loading branch information
Palmer Cox committed Mar 8, 2015
1 parent 2edc96f commit 8b7861c
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 22 deletions.
20 changes: 10 additions & 10 deletions src/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::cmp;
use std::simd::u32x4;

use buffer::{BufferResult, RefReadBuffer, RefWriteBuffer};
use symmetriccipher::{Encryptor, Decryptor, SynchronousStreamCipher, SymmetricCipherError};
use cryptoutil::{read_u32_le, symm_enc_or_dec, write_u32_le, xor_keystream};
use simd::u32x4;

#[derive(Copy)]
struct ChaChaState {
Expand Down Expand Up @@ -56,13 +56,13 @@ macro_rules! state_to_buffer {

macro_rules! round{
($state: expr) => {{
$state.a += $state.b;
$state.a = $state.a + $state.b;
rotate!($state.d, $state.a, S16);
$state.c += $state.d;
$state.c = $state.c + $state.d;
rotate!($state.b, $state.c, S12);
$state.a += $state.b;
$state.a = $state.a + $state.b;
rotate!($state.d, $state.a, S8);
$state.c += $state.d;
$state.c = $state.c + $state.d;
rotate!($state.b, $state.c, S7);
}}
}
Expand Down Expand Up @@ -217,14 +217,14 @@ impl ChaCha20 {
round!(state);
swizzle!(state.d, state.c, state.b);
}
state.a += self.state.a;
state.b += self.state.b;
state.c += self.state.c;
state.d += self.state.d;
state.a = state.a + self.state.a;
state.b = state.b + self.state.b;
state.c = state.c + self.state.c;
state.d = state.d + self.state.d;

state_to_buffer!(state, self.output);

self.state.d += u32x4(1, 0, 0, 0);
self.state.d = self.state.d + u32x4(1, 0, 0, 0);
let u32x4(c12, _, _, _) = self.state.d;
if c12 == 0 {
// we could increment the other counter word with an 8 byte nonce
Expand Down
13 changes: 7 additions & 6 deletions src/ghash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
use std::ops::BitXor;
use std::mem;
use std::simd;
use std::slice::bytes::copy_memory;

use cryptoutil::{read_u32_be, write_u32_be};
use mac::{Mac, MacResult};
use simd;

// A struct representing an element in GF(2^128)
// x^0 is the msb, while x^127 is the lsb
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Gf128 {

// Multiply the element by x modulo x^128
// This is equivalent to a rightshift in the bit representation
#[cfg(target_arch = "x86_64")]
#[cfg(all(target_arch = "x86_64",ndebug))]
fn times_x(mut self) -> Gf128 {
unsafe {
asm!("
Expand All @@ -70,7 +70,7 @@ impl Gf128 {
self
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(any(not(target_arch = "x86_64"),not(ndebug)))]
fn times_x(self) -> Gf128 {
let simd::u32x4(a, b, c, d) = self.d;
Gf128::new(a >> 1 | b << 31, b >> 1 | c << 31, c >> 1 | d << 31, d >> 1)
Expand All @@ -96,7 +96,7 @@ impl Gf128 {
}

// This XORs the value of y with x if the LSB of self is set, otherwise y is returned
#[cfg(target_arch = "x86_64")]
#[cfg(all(target_arch = "x86_64",ndebug))]
fn cond_xor(self, x: Gf128, mut y: Gf128) -> Gf128 {
let lsb = simd::u32x4(1, 0, 0, 0);
unsafe {
Expand All @@ -113,10 +113,11 @@ impl Gf128 {
y
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(any(not(target_arch = "x86_64"),not(ndebug)))]
fn cond_xor(self, x: Gf128, y: Gf128) -> Gf128 {
use simd::SimdExt;
let lsb = simd::u32x4(1, 0, 0, 0);
let simd::u32x4(m, _, _, _) = (self.d & lsb) == lsb;
let simd::u32x4(m, _, _, _) = (self.d & lsb).simd_eq(lsb);
let mask = simd::u32x4(m, m, m, m);
Gf128 { d: (x.d & mask) ^ y.d }
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod salsa20;
pub mod scrypt;
pub mod sha1;
pub mod sha2;
mod simd;
pub mod sosemanuk;
pub mod symmetriccipher;
pub mod util;
Expand Down
8 changes: 4 additions & 4 deletions src/salsa20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use buffer::{BufferResult, RefReadBuffer, RefWriteBuffer};
use symmetriccipher::{Encryptor, Decryptor, SynchronousStreamCipher, SymmetricCipherError};
use cryptoutil::{read_u32_le, symm_enc_or_dec, write_u32_le, xor_keystream};
use simd::u32x4;

use std::cmp;
use std::simd::u32x4;

#[derive(Copy)]
struct SalsaState {
Expand Down Expand Up @@ -59,7 +59,7 @@ macro_rules! add_rotate_xor {
let v = $a + $b;
let r = S32 - $shift;
let right = v >> r;
$dst ^= (v << $shift) ^ right
$dst = $dst ^ (v << $shift) ^ right
}}
}

Expand Down Expand Up @@ -177,10 +177,10 @@ impl Salsa20 {
write_u32_le(&mut self.output[i*4..(i+1)*4], lens[i]);
}

self.state.b += u32x4(1, 0, 0, 0);
self.state.b = self.state.b + u32x4(1, 0, 0, 0);
let u32x4(_, _, _, ctr_lo) = self.state.b;
if ctr_lo == 0 {
self.state.a += u32x4(0, 1, 0, 0);
self.state.a = self.state.a + u32x4(0, 1, 0, 0);
}

self.offset = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ algorithms, but some, like "parity" is only found in SHA-1.
*/

use std::num::Int;
use std::simd::u32x4;
use digest::Digest;
use cryptoutil::{write_u32_be, read_u32v_be, add_bytes_to_bits, FixedBuffer, FixedBuffer64, StandardPadding};
use simd::u32x4;

const STATE_LEN: usize = 5;
const BLOCK_LEN: usize = 16;
Expand Down
3 changes: 2 additions & 1 deletion src/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ assert_eq!(hex.as_slice(),
*/

use std::simd::{u32x4, u64x2};
use std::num::Int;
use digest::Digest;
use cryptoutil::{write_u32_be, read_u32v_be,
write_u64_be, read_u64v_be,
add_bytes_to_bits, add_bytes_to_bits_tuple,
FixedBuffer, FixedBuffer128, FixedBuffer64, StandardPadding};

use simd::{u32x4, u64x2};

const STATE_LEN: usize = 8;
const BLOCK_LEN: usize = 16;

Expand Down
141 changes: 141 additions & 0 deletions src/simd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(not(ndebug))]
pub use self::fake::*;

#[cfg(ndebug)]
pub use self::real::*;

pub trait SimdExt {
fn simd_eq(self, rhs: Self) -> Self;
}

#[cfg(not(ndebug))]
impl SimdExt for fake::u32x4 {
fn simd_eq(self, rhs: Self) -> Self {
if self == rhs {
fake::u32x4(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff)
} else {
fake::u32x4(0, 0, 0, 0)
}
}
}

#[cfg(ndebug)]
impl SimdExt for real::u32x4 {
fn simd_eq(self, rhs: Self) -> Self {
self == rhs
}
}

#[cfg(not(ndebug))]
mod fake {
use std::ops::{Add, BitAnd, BitOr, BitXor, Shl, Shr, Sub};

#[derive(Copy, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub struct u32x4(pub u32, pub u32, pub u32, pub u32);

impl Add for u32x4 {
type Output = u32x4;

fn add(self, rhs: u32x4) -> u32x4 {
u32x4(
self.0.wrapping_add(rhs.0),
self.1.wrapping_add(rhs.1),
self.2.wrapping_add(rhs.2),
self.3.wrapping_add(rhs.3))
}
}

impl Sub for u32x4 {
type Output = u32x4;

fn sub(self, rhs: u32x4) -> u32x4 {
u32x4(
self.0.wrapping_sub(rhs.0),
self.1.wrapping_sub(rhs.1),
self.2.wrapping_sub(rhs.2),
self.3.wrapping_sub(rhs.3))
}
}

impl BitAnd for u32x4 {
type Output = u32x4;

fn bitand(self, rhs: u32x4) -> u32x4 {
u32x4(self.0 & rhs.0, self.1 & rhs.1, self.2 & rhs.2, self.3 & rhs.3)
}
}

impl BitOr for u32x4 {
type Output = u32x4;

fn bitor(self, rhs: u32x4) -> u32x4 {
u32x4(self.0 | rhs.0, self.1 | rhs.1, self.2 | rhs.2, self.3 | rhs.3)
}
}

impl BitXor for u32x4 {
type Output = u32x4;

fn bitxor(self, rhs: u32x4) -> u32x4 {
u32x4(self.0 ^ rhs.0, self.1 ^ rhs.1, self.2 ^ rhs.2, self.3 ^ rhs.3)
}
}

impl Shl<usize> for u32x4 {
type Output = u32x4;

fn shl(self, amt: usize) -> u32x4 {
u32x4(self.0 << amt, self.1 << amt, self.2 << amt, self.3 << amt)
}
}

impl Shl<u32x4> for u32x4 {
type Output = u32x4;

fn shl(self, rhs: u32x4) -> u32x4 {
u32x4(self.0 << rhs.0, self.1 << rhs.1, self.2 << rhs.2, self.3 << rhs.3)
}
}

impl Shr<usize> for u32x4 {
type Output = u32x4;

fn shr(self, amt: usize) -> u32x4 {
u32x4(self.0 >> amt, self.1 >> amt, self.2 >> amt, self.3 >> amt)
}
}

impl Shr<u32x4> for u32x4 {
type Output = u32x4;

fn shr(self, rhs: u32x4) -> u32x4 {
u32x4(self.0 >> rhs.0, self.1 >> rhs.1, self.2 >> rhs.2, self.3 >> rhs.3)
}
}

#[derive(Copy)]
#[allow(non_camel_case_types)]
pub struct u64x2(pub u64, pub u64);

impl Add for u64x2 {
type Output = u64x2;

fn add(self, rhs: u64x2) -> u64x2 {
u64x2(self.0.wrapping_add(rhs.0), self.1.wrapping_add(rhs.1))
}
}
}

#[cfg(ndebug)]
mod real {
pub use std::simd::u32x4;
pub use std::simd::u64x2;
}

0 comments on commit 8b7861c

Please sign in to comment.