Skip to content

Commit

Permalink
dbl: Refactor to avoid unsafe (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
aewag authored Dec 21, 2021
1 parent cb6cc8f commit 9701c71
Showing 1 changed file with 48 additions and 30 deletions.
78 changes: 48 additions & 30 deletions dbl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![forbid(unsafe_code)]
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
Expand All @@ -10,7 +11,7 @@ extern crate generic_array;
use generic_array::typenum::{U16, U32, U8};
use generic_array::GenericArray;

use core::mem;
use core::{convert::TryInto, mem::size_of};

const C64: u64 = 0b1_1011;
const C128: u64 = 0b1000_0111;
Expand All @@ -37,38 +38,36 @@ pub trait Dbl {
}

impl Dbl for GenericArray<u8, U8> {
#[inline]
fn dbl(self) -> Self {
let mut val: u64 = unsafe { mem::transmute_copy(&self) };
val = val.to_be();
let mut val = u64::from_be_bytes(self.into());

let a = val >> 63;
val <<= 1;
val ^= a * C64;
unsafe { mem::transmute(val.to_be()) }

val.to_be_bytes().into()
}

#[inline]
fn inv_dbl(self) -> Self {
let mut val: u64 = unsafe { mem::transmute_copy(&self) };
val = val.to_be();
let mut val = u64::from_be_bytes(self.into());

let a = val & 1;
val >>= 1;
val ^= a * ((1 << 63) ^ (C64 >> 1));

unsafe { mem::transmute(val.to_be()) }
}
}

#[inline(always)]
fn to_be(val: &mut [u64]) {
for v in val.iter_mut() {
*v = v.to_be();
val.to_be_bytes().into()
}
}

impl Dbl for GenericArray<u8, U16> {
#[inline]
fn dbl(self) -> Self {
let mut val: [u64; 2] = unsafe { mem::transmute_copy(&self) };
to_be(&mut val);
let mut val = [
u64::from_be_bytes(self[..8].try_into().unwrap()),
u64::from_be_bytes(self[8..].try_into().unwrap()),
];

let b = val[1] >> 63;
let a = val[0] >> 63;
Expand All @@ -78,13 +77,18 @@ impl Dbl for GenericArray<u8, U16> {
val[1] <<= 1;
val[1] ^= a * C128;

to_be(&mut val);
unsafe { mem::transmute(val) }
let mut res = Self::default();
res[..8].copy_from_slice(&val[0].to_be_bytes());
res[8..].copy_from_slice(&val[1].to_be_bytes());
res
}

#[inline]
fn inv_dbl(self) -> Self {
let mut val: [u64; 2] = unsafe { mem::transmute_copy(&self) };
to_be(&mut val);
let mut val = [
u64::from_be_bytes(self[..8].try_into().unwrap()),
u64::from_be_bytes(self[8..].try_into().unwrap()),
];

let a = (val[0] & 1) << 63;
let b = val[1] & 1;
Expand All @@ -95,15 +99,20 @@ impl Dbl for GenericArray<u8, U16> {
val[0] ^= b * (1 << 63);
val[1] ^= b * (C128 >> 1);

to_be(&mut val);
unsafe { mem::transmute(val) }
let mut res = Self::default();
res[..8].copy_from_slice(&val[0].to_be_bytes());
res[8..].copy_from_slice(&val[1].to_be_bytes());
res
}
}

impl Dbl for GenericArray<u8, U32> {
#[inline]
fn dbl(self) -> Self {
let mut val: [u64; 4] = unsafe { mem::transmute_copy(&self) };
to_be(&mut val);
let mut val = [0u64; 4];
for (s, v) in self.chunks_exact(size_of::<u64>()).zip(val.iter_mut()) {
*v = u64::from_be_bytes(s.try_into().unwrap());
}

let a = val[0] >> 63;
let b = val[1] >> 63;
Expand All @@ -119,13 +128,19 @@ impl Dbl for GenericArray<u8, U32> {
val[3] <<= 1;
val[3] ^= a * C256;

to_be(&mut val);
unsafe { mem::transmute(val) }
let mut val_u8 = [0u8; 32];
for (vu8, v) in val_u8.chunks_exact_mut(size_of::<u64>()).zip(val.iter()) {
vu8.copy_from_slice(&v.to_be_bytes());
}
val_u8.into()
}

#[inline]
fn inv_dbl(self) -> Self {
let mut val: [u64; 4] = unsafe { mem::transmute_copy(&self) };
to_be(&mut val);
let mut val = [0u64; 4];
for (s, v) in self.chunks_exact(size_of::<u64>()).zip(val.iter_mut()) {
*v = u64::from_be_bytes(s.try_into().unwrap());
}

let a = (val[0] & 1) << 63;
let b = (val[1] & 1) << 63;
Expand All @@ -143,7 +158,10 @@ impl Dbl for GenericArray<u8, U32> {
val[0] ^= d * (1 << 63);
val[3] ^= d * (C256 >> 1);

to_be(&mut val);
unsafe { mem::transmute(val) }
let mut val_u8 = [0u8; 32];
for (vu8, v) in val_u8.chunks_exact_mut(size_of::<u64>()).zip(val.iter()) {
vu8.copy_from_slice(&v.to_be_bytes());
}
val_u8.into()
}
}

0 comments on commit 9701c71

Please sign in to comment.