aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2019-12-06 15:32:20 -0500
committerJack O'Connor <[email protected]>2019-12-06 15:32:20 -0500
commit912ae19bce02dc9d41f593c49d3bded70994b066 (patch)
tree90621c12f09c8e58d4621b802486fd785be0e5d0
parent47ef3ad01fec3bd02d9f8b2d3a04d8d5cdffbba0 (diff)
get rid of the bitflags dependency
-rw-r--r--Cargo.toml1
-rw-r--r--src/avx2.rs19
-rw-r--r--src/lib.rs74
-rw-r--r--src/platform.rs32
-rw-r--r--src/sse41.rs41
5 files changed, 74 insertions, 93 deletions
diff --git a/Cargo.toml b/Cargo.toml
index c219998..61d4f91 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,7 +12,6 @@ std = []
[dependencies]
arrayref = "0.3.5"
arrayvec = { version = "0.5.1", default-features = false, features = ["array-sizes-33-128"] }
-bitflags = "1.2.1"
constant_time_eq = "0.1.4"
rayon = { version = "1.2.1", optional = true }
diff --git a/src/avx2.rs b/src/avx2.rs
index 88cc157..a7a0de7 100644
--- a/src/avx2.rs
+++ b/src/avx2.rs
@@ -485,8 +485,7 @@ mod test {
let mut portable_out = [0; DEGREE * OUT_LEN];
for (parent, out) in parents.iter().zip(portable_out.chunks_exact_mut(OUT_LEN)) {
- let out_wide =
- portable::compress(&key, parent, BLOCK_LEN as u8, 0, Flags::PARENT.bits());
+ let out_wide = portable::compress(&key, parent, BLOCK_LEN as u8, 0, crate::PARENT);
out.copy_from_slice(&out_wide[..32]);
}
@@ -509,7 +508,7 @@ mod test {
0,
PARENT_OFFSET_DELTAS,
0,
- Flags::PARENT.bits(),
+ crate::PARENT,
0,
&mut simd_out,
);
@@ -548,19 +547,19 @@ mod test {
{
let mut cv = key;
for (block_index, block) in chunk.chunks_exact(BLOCK_LEN).enumerate() {
- let mut flags = Flags::KEYED_HASH;
+ let mut flags = crate::KEYED_HASH;
if block_index == 0 {
- flags |= Flags::CHUNK_START;
+ flags |= crate::CHUNK_START;
}
if block_index == CHUNK_LEN / BLOCK_LEN - 1 {
- flags |= Flags::CHUNK_END;
+ flags |= crate::CHUNK_END;
}
let out = portable::compress(
&cv,
array_ref!(block, 0, BLOCK_LEN),
BLOCK_LEN as u8,
initial_offset + (chunk_index * CHUNK_LEN) as u64,
- flags.bits(),
+ flags,
);
cv = *array_ref!(out, 0, 32);
}
@@ -585,9 +584,9 @@ mod test {
&key,
initial_offset,
CHUNK_OFFSET_DELTAS,
- Flags::KEYED_HASH.bits(),
- Flags::CHUNK_START.bits(),
- Flags::CHUNK_END.bits(),
+ crate::KEYED_HASH,
+ crate::CHUNK_START,
+ crate::CHUNK_END,
&mut simd_out,
);
}
diff --git a/src/lib.rs b/src/lib.rs
index c6181ea..17e65ad 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -71,16 +71,12 @@ const PARENT_OFFSET_DELTAS: &[u64; 16] = &[0; 16];
// chunk/parent, and chunk beginning/middle/end. These get set at the high end
// of the block flags word in the compression function, so their values start
// high and go down.
-bitflags::bitflags! {
- struct Flags: u8 {
- const CHUNK_START = 1 << 0;
- const CHUNK_END = 1 << 1;
- const PARENT = 1 << 2;
- const ROOT = 1 << 3;
- const KEYED_HASH = 1 << 4;
- const DERIVE_KEY = 1 << 5;
- }
-}
+const CHUNK_START: u8 = 1 << 0;
+const CHUNK_END: u8 = 1 << 1;
+const PARENT: u8 = 1 << 2;
+const ROOT: u8 = 1 << 3;
+const KEYED_HASH: u8 = 1 << 4;
+const DERIVE_KEY: u8 = 1 << 5;
fn offset_low(offset: u64) -> u32 {
offset as u32
@@ -162,7 +158,7 @@ struct Output {
block: [u8; 64],
block_len: u8,
offset: u64,
- flags: Flags,
+ flags: u8,
platform: Platform,
}
@@ -185,7 +181,7 @@ impl Output {
&self.block,
self.block_len,
0,
- self.flags | Flags::ROOT,
+ self.flags | ROOT,
);
Hash(*array_ref!(out, 0, 32))
}
@@ -199,7 +195,7 @@ impl Output {
&self.block,
self.block_len,
offset,
- self.flags | Flags::ROOT,
+ self.flags | ROOT,
);
out_block.copy_from_slice(&out_bytes[..out_block.len()]);
offset += 2 * OUT_LEN as u64;
@@ -214,12 +210,12 @@ struct ChunkState {
buf: [u8; BLOCK_LEN],
buf_len: u8,
blocks_compressed: u8,
- flags: Flags,
+ flags: u8,
platform: Platform,
}
impl ChunkState {
- fn new(key: &[u8; 32], offset: u64, flags: Flags, platform: Platform) -> Self {
+ fn new(key: &[u8; 32], offset: u64, flags: u8, platform: Platform) -> Self {
Self {
cv: *key,
offset,
@@ -252,11 +248,11 @@ impl ChunkState {
*input = &input[take..];
}
- fn start_flag(&self) -> Flags {
+ fn start_flag(&self) -> u8 {
if self.blocks_compressed == 0 {
- Flags::CHUNK_START
+ CHUNK_START
} else {
- Flags::empty()
+ 0
}
}
@@ -304,7 +300,7 @@ impl ChunkState {
}
fn output(&self) -> Output {
- let block_flags = self.flags | self.start_flag() | Flags::CHUNK_END;
+ let block_flags = self.flags | self.start_flag() | CHUNK_END;
Output {
input_chaining_value: self.cv,
block: self.buf,
@@ -387,7 +383,7 @@ fn compress_chunks_parallel(
input: &[u8],
key: &[u8; KEY_LEN],
offset: u64,
- flags: Flags,
+ flags: u8,
platform: Platform,
out: &mut [u8],
) -> usize {
@@ -406,8 +402,8 @@ fn compress_chunks_parallel(
offset,
CHUNK_OFFSET_DELTAS,
flags,
- Flags::CHUNK_START,
- Flags::CHUNK_END,
+ CHUNK_START,
+ CHUNK_END,
out,
);
@@ -434,7 +430,7 @@ fn compress_chunks_parallel(
fn compress_parents_parallel(
child_chaining_values: &[u8],
key: &[u8; KEY_LEN],
- flags: Flags,
+ flags: u8,
platform: Platform,
out: &mut [u8],
) -> usize {
@@ -455,9 +451,9 @@ fn compress_parents_parallel(
key,
0, // Parents always use offset 0.
PARENT_OFFSET_DELTAS,
- flags | Flags::PARENT,
- Flags::empty(), // Parents have no start flags.
- Flags::empty(), // Parents have no end flags.
+ flags | PARENT,
+ 0, // Parents have no start flags.
+ 0, // Parents have no end flags.
out,
);
@@ -488,7 +484,7 @@ fn compress_subtree_wide(
input: &[u8],
key: &[u8; KEY_LEN],
offset: u64,
- flags: Flags,
+ flags: u8,
platform: Platform,
out: &mut [u8],
) -> usize {
@@ -560,7 +556,7 @@ fn compress_subtree_to_parent_node(
input: &[u8],
key: &[u8; KEY_LEN],
offset: u64,
- flags: Flags,
+ flags: u8,
platform: Platform,
) -> [u8; BLOCK_LEN] {
debug_assert!(input.len() > CHUNK_LEN);
@@ -582,7 +578,7 @@ fn compress_subtree_to_parent_node(
// Hash a complete input all at once. Unlike compress_subtree_wide() and
// compress_subtree_to_parent_node(), this function handles the 1 chunk case.
-fn hash_all_at_once(input: &[u8], key: &[u8; KEY_LEN], flags: Flags) -> Output {
+fn hash_all_at_once(input: &[u8], key: &[u8; KEY_LEN], flags: u8) -> Output {
let platform = Platform::detect();
// If the whole subtree is one chunk, hash it directly with a ChunkState.
@@ -599,31 +595,31 @@ fn hash_all_at_once(input: &[u8], key: &[u8; KEY_LEN], flags: Flags) -> Output {
block: compress_subtree_to_parent_node(input, key, 0, flags, platform),
block_len: BLOCK_LEN as u8,
offset: 0,
- flags: flags | Flags::PARENT,
+ flags: flags | PARENT,
platform,
}
}
/// The default hash function.
pub fn hash(input: &[u8]) -> Hash {
- hash_all_at_once(input, IV_BYTES, Flags::empty()).root_hash()
+ hash_all_at_once(input, IV_BYTES, 0).root_hash()
}
/// The keyed hash function.
pub fn hash_keyed(key: &[u8; KEY_LEN], input: &[u8]) -> Hash {
- hash_all_at_once(input, key, Flags::KEYED_HASH).root_hash()
+ hash_all_at_once(input, key, KEYED_HASH).root_hash()
}
/// The key derivation function.
pub fn derive_key(key: &[u8; KEY_LEN], context: &[u8]) -> Hash {
- hash_all_at_once(context, key, Flags::DERIVE_KEY).root_hash()
+ hash_all_at_once(context, key, DERIVE_KEY).root_hash()
}
fn parent_node_output(
left_child: &[u8; 32],
right_child: &[u8; 32],
key: &[u8; KEY_LEN],
- flags: Flags,
+ flags: u8,
platform: Platform,
) -> Output {
let mut block = [0; BLOCK_LEN];
@@ -634,7 +630,7 @@ fn parent_node_output(
block,
block_len: BLOCK_LEN as u8,
offset: 0,
- flags: flags | Flags::PARENT,
+ flags: flags | PARENT,
platform,
}
}
@@ -650,7 +646,7 @@ pub struct Hasher {
}
impl Hasher {
- fn new_internal(key: &[u8; 32], flags: Flags) -> Self {
+ fn new_internal(key: &[u8; 32], flags: u8) -> Self {
Self {
key: *key,
chunk_state: ChunkState::new(key, 0, flags, Platform::detect()),
@@ -660,12 +656,12 @@ impl Hasher {
/// Construct a new `Hasher` for the regular hash function.
pub fn new() -> Self {
- Self::new_internal(IV_BYTES, Flags::empty())
+ Self::new_internal(IV_BYTES, 0)
}
/// Construct a new `Hasher` for the keyed hash function.
pub fn new_keyed(key: &[u8; KEY_LEN]) -> Self {
- Self::new_internal(key, Flags::KEYED_HASH)
+ Self::new_internal(key, KEYED_HASH)
}
/// Construct a new `Hasher` for the key derivation function.
@@ -676,7 +672,7 @@ impl Hasher {
///
/// [`derive_key`]: fn.derive_key.html
pub fn new_derive_key(key: &[u8; KEY_LEN]) -> Self {
- Self::new_internal(key, Flags::DERIVE_KEY)
+ Self::new_internal(key, DERIVE_KEY)
}
/// The total number of input bytes so far.
diff --git a/src/platform.rs b/src/platform.rs
index b931648..b7cd26e 100644
--- a/src/platform.rs
+++ b/src/platform.rs
@@ -1,4 +1,4 @@
-use crate::{portable, Flags, BLOCK_LEN, KEY_LEN};
+use crate::{portable, BLOCK_LEN, KEY_LEN};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::{avx2, sse41};
@@ -58,14 +58,14 @@ impl Platform {
block: &[u8; BLOCK_LEN],
block_len: u8,
offset: u64,
- flags: Flags,
+ flags: u8,
) -> [u8; 64] {
match self {
- Platform::Portable => portable::compress(cv, block, block_len, offset, flags.bits()),
+ Platform::Portable => portable::compress(cv, block, block_len, offset, flags),
// Safe because detect() checked for platform support.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Platform::SSE41 | Platform::AVX2 => unsafe {
- sse41::compress(cv, block, block_len, offset, flags.bits())
+ sse41::compress(cv, block, block_len, offset, flags)
},
}
}
@@ -86,9 +86,9 @@ impl Platform {
key: &[u8; KEY_LEN],
offset: u64,
offset_deltas: &[u64; 16],
- flags: Flags,
- flags_start: Flags,
- flags_end: Flags,
+ flags: u8,
+ flags_start: u8,
+ flags_end: u8,
out: &mut [u8],
) {
match self {
@@ -97,9 +97,9 @@ impl Platform {
key,
offset,
offset_deltas,
- flags.bits(),
- flags_start.bits(),
- flags_end.bits(),
+ flags,
+ flags_start,
+ flags_end,
out,
),
// Safe because detect() checked for platform support.
@@ -110,9 +110,9 @@ impl Platform {
key,
offset,
offset_deltas,
- flags.bits(),
- flags_start.bits(),
- flags_end.bits(),
+ flags,
+ flags_start,
+ flags_end,
out,
)
},
@@ -124,9 +124,9 @@ impl Platform {
key,
offset,
offset_deltas,
- flags.bits(),
- flags_start.bits(),
- flags_end.bits(),
+ flags,
+ flags_start,
+ flags_end,
out,
)
},
diff --git a/src/sse41.rs b/src/sse41.rs
index 69261df..371798a 100644
--- a/src/sse41.rs
+++ b/src/sse41.rs
@@ -713,25 +713,13 @@ mod test {
crate::test::paint_test_input(&mut block[..block_len as usize]);
// Use an offset with set bits in both 32-bit words.
let offset = ((5 * CHUNK_LEN as u64) << 32) + 6 * CHUNK_LEN as u64;
- let flags = Flags::CHUNK_END | Flags::ROOT;
+ let flags = crate::CHUNK_END | crate::ROOT;
- let portable_out = portable::compress(
- &initial_state,
- &block,
- block_len,
- offset as u64,
- flags.bits(),
- );
+ let portable_out =
+ portable::compress(&initial_state, &block, block_len, offset as u64, flags);
- let simd_out = unsafe {
- super::compress(
- &initial_state,
- &block,
- block_len,
- offset as u64,
- flags.bits(),
- )
- };
+ let simd_out =
+ unsafe { super::compress(&initial_state, &block, block_len, offset as u64, flags) };
assert_eq!(&portable_out[..], &simd_out[..]);
}
@@ -754,8 +742,7 @@ mod test {
let mut portable_out = [0; DEGREE * OUT_LEN];
for (parent, out) in parents.iter().zip(portable_out.chunks_exact_mut(OUT_LEN)) {
- let wide_out =
- portable::compress(&key, parent, BLOCK_LEN as u8, 0, Flags::PARENT.bits());
+ let wide_out = portable::compress(&key, parent, BLOCK_LEN as u8, 0, crate::PARENT);
out.copy_from_slice(&wide_out[..32]);
}
@@ -774,7 +761,7 @@ mod test {
0,
PARENT_OFFSET_DELTAS,
0,
- Flags::PARENT.bits(),
+ crate::PARENT,
0,
&mut simd_out,
);
@@ -809,19 +796,19 @@ mod test {
{
let mut cv = key;
for (block_index, block) in chunk.chunks_exact(BLOCK_LEN).enumerate() {
- let mut block_flags = Flags::KEYED_HASH;
+ let mut block_flags = crate::KEYED_HASH;
if block_index == 0 {
- block_flags |= Flags::CHUNK_START;
+ block_flags |= crate::CHUNK_START;
}
if block_index == CHUNK_LEN / BLOCK_LEN - 1 {
- block_flags |= Flags::CHUNK_END;
+ block_flags |= crate::CHUNK_END;
}
let out = portable::compress(
&cv,
array_ref!(block, 0, BLOCK_LEN),
BLOCK_LEN as u8,
initial_offset + (chunk_index * CHUNK_LEN) as u64,
- block_flags.bits(),
+ block_flags,
);
cv = *array_ref!(out, 0, 32);
}
@@ -842,9 +829,9 @@ mod test {
&key,
initial_offset,
CHUNK_OFFSET_DELTAS,
- Flags::KEYED_HASH.bits(),
- Flags::CHUNK_START.bits(),
- Flags::CHUNK_END.bits(),
+ crate::KEYED_HASH,
+ crate::CHUNK_START,
+ crate::CHUNK_END,
&mut simd_out,
);
}