Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/ffi_avx512.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};
use crate::{BlockBytes, CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};

// Unsafe because this may only be called on platforms supporting AVX-512.
pub unsafe fn compress_in_place(
cv: &mut CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand All @@ -22,11 +22,11 @@ pub unsafe fn compress_in_place(
// Unsafe because this may only be called on platforms supporting AVX-512.
pub unsafe fn compress_xof(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u8; 64] {
) -> BlockBytes {
unsafe {
let mut out = [0u8; 64];
ffi::blake3_compress_xof_avx512(
Expand Down Expand Up @@ -76,7 +76,7 @@ pub unsafe fn hash_many<const N: usize>(
#[cfg(unix)]
pub unsafe fn xof_many(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand Down
4 changes: 2 additions & 2 deletions src/ffi_neon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};
use crate::{BlockBytes, CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};

// Unsafe because this may only be called on platforms supporting NEON.
pub unsafe fn hash_many<const N: usize>(
Expand Down Expand Up @@ -44,7 +44,7 @@ pub extern "C" fn blake3_compress_in_place_portable(
unsafe {
crate::portable::compress_in_place(
&mut *(cv as *mut [u32; 8]),
&*(block as *const [u8; 64]),
&*(block as *const BlockBytes),
block_len,
counter,
flags,
Expand Down
8 changes: 4 additions & 4 deletions src/ffi_sse2.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};
use crate::{BlockBytes, CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};

// Unsafe because this may only be called on platforms supporting SSE2.
pub unsafe fn compress_in_place(
cv: &mut CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand All @@ -22,11 +22,11 @@ pub unsafe fn compress_in_place(
// Unsafe because this may only be called on platforms supporting SSE2.
pub unsafe fn compress_xof(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u8; 64] {
) -> BlockBytes {
unsafe {
let mut out = [0u8; 64];
ffi::blake3_compress_xof_sse2(
Expand Down
8 changes: 4 additions & 4 deletions src/ffi_sse41.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};
use crate::{BlockBytes, CVWords, IncrementCounter, BLOCK_LEN, OUT_LEN};

// Unsafe because this may only be called on platforms supporting SSE4.1.
pub unsafe fn compress_in_place(
cv: &mut CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand All @@ -22,11 +22,11 @@ pub unsafe fn compress_in_place(
// Unsafe because this may only be called on platforms supporting SSE4.1.
pub unsafe fn compress_xof(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u8; 64] {
) -> BlockBytes {
unsafe {
let mut out = [0u8; 64];
ffi::blake3_compress_xof_sse41(
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ const MAX_DEPTH: usize = 54; // 2^54 * CHUNK_LEN = 2^64
type CVWords = [u32; 8];
type CVBytes = [u8; 32]; // little-endian

type BlockBytes = [u8; BLOCK_LEN];
type BlockWords = [u32; 16];

const IV: &CVWords = &[
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
];
Expand Down Expand Up @@ -367,7 +370,7 @@ impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Formatting field as `&str` to reduce code size since the `Debug`
// dynamic dispatch table for `&str` is likely needed elsewhere already,
// but that for `ArrayString<[u8; 64]>` is not.
// but that for `ArrayString<BlockBytes>` is not.
let hex = self.to_hex();
let hex: &str = hex.as_str();

Expand All @@ -379,7 +382,7 @@ impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Formatting field as `&str` to reduce code size since the `Debug`
// dynamic dispatch table for `&str` is likely needed elsewhere already,
// but that for `ArrayString<[u8; 64]>` is not.
// but that for `ArrayString<BlockBytes>` is not.
let hex = self.to_hex();
let hex: &str = hex.as_str();

Expand Down Expand Up @@ -427,7 +430,7 @@ impl std::error::Error for HexError {}
#[derive(Clone)]
struct Output {
input_chaining_value: CVWords,
block: [u8; 64],
block: BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand Down Expand Up @@ -491,7 +494,7 @@ impl Zeroize for Output {
struct ChunkState {
cv: CVWords,
chunk_counter: u64,
buf: [u8; BLOCK_LEN],
buf: BlockBytes,
buf_len: u8,
blocks_compressed: u8,
flags: u8,
Expand Down Expand Up @@ -725,7 +728,7 @@ fn compress_parents_parallel(
let mut parents_exact = child_chaining_values.chunks_exact(BLOCK_LEN);
// Use MAX_SIMD_DEGREE_OR_2 rather than MAX_SIMD_DEGREE here, because of
// the requirements of compress_subtree_wide().
let mut parents_array = ArrayVec::<&[u8; BLOCK_LEN], MAX_SIMD_DEGREE_OR_2>::new();
let mut parents_array = ArrayVec::<&BlockBytes, MAX_SIMD_DEGREE_OR_2>::new();
for parent in &mut parents_exact {
parents_array.push(array_ref!(parent, 0, BLOCK_LEN));
}
Expand Down Expand Up @@ -847,7 +850,7 @@ fn compress_subtree_to_parent_node<J: join::Join>(
chunk_counter: u64,
flags: u8,
platform: Platform,
) -> [u8; BLOCK_LEN] {
) -> BlockBytes {
debug_assert!(input.len() > CHUNK_LEN);
let mut cv_array = [0; MAX_SIMD_DEGREE_OR_2 * OUT_LEN];
let mut num_cvs =
Expand Down Expand Up @@ -1689,7 +1692,7 @@ impl OutputReader {
// shorter than one block, and the case where our position_within_block is
// non-zero.
fn fill_one_block(&mut self, buf: &mut &mut [u8]) {
let output_block: [u8; BLOCK_LEN] = self.inner.root_output_block();
let output_block: BlockBytes = self.inner.root_output_block();
let output_bytes = &output_block[self.position_within_block as usize..];
let take = cmp::min(buf.len(), output_bytes.len());
buf[..take].copy_from_slice(&output_bytes[..take]);
Expand Down
16 changes: 8 additions & 8 deletions src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{portable, CVWords, IncrementCounter, BLOCK_LEN};
use crate::{portable, BlockBytes, BlockWords, CVWords, IncrementCounter, BLOCK_LEN};
use arrayref::{array_mut_ref, array_ref};

cfg_if::cfg_if! {
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Platform {
pub fn compress_in_place(
&self,
cv: &mut CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand Down Expand Up @@ -159,11 +159,11 @@ impl Platform {
pub fn compress_xof(
&self,
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u8; 64] {
) -> BlockBytes {
match self {
Platform::Portable => portable::compress_xof(cv, block, block_len, counter, flags),
// Safe because detect() checked for platform support.
Expand Down Expand Up @@ -315,7 +315,7 @@ impl Platform {
pub fn xof_many(
&self,
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
mut counter: u64,
flags: u8,
Expand All @@ -339,7 +339,7 @@ impl Platform {
// compress_xof. This is still faster than portable code.
for out_block in out.chunks_exact_mut(BLOCK_LEN) {
// TODO: Use array_chunks_mut here once that's stable.
let out_array: &mut [u8; BLOCK_LEN] = out_block.try_into().unwrap();
let out_array: &mut BlockBytes = out_block.try_into().unwrap();
*out_array = self.compress_xof(cv, block, block_len, counter, flags);
counter += 1;
}
Expand Down Expand Up @@ -485,7 +485,7 @@ pub fn words_from_le_bytes_32(bytes: &[u8; 32]) -> [u32; 8] {
}

#[inline(always)]
pub fn words_from_le_bytes_64(bytes: &[u8; 64]) -> [u32; 16] {
pub fn words_from_le_bytes_64(bytes: &BlockBytes) -> BlockWords {
let mut out = [0; 16];
out[0] = u32::from_le_bytes(*array_ref!(bytes, 0 * 4, 4));
out[1] = u32::from_le_bytes(*array_ref!(bytes, 1 * 4, 4));
Expand Down Expand Up @@ -521,7 +521,7 @@ pub fn le_bytes_from_words_32(words: &[u32; 8]) -> [u8; 32] {
}

#[inline(always)]
pub fn le_bytes_from_words_64(words: &[u32; 16]) -> [u8; 64] {
pub fn le_bytes_from_words_64(words: &BlockWords) -> BlockBytes {
let mut out = [0; 64];
*array_mut_ref!(out, 0 * 4, 4) = words[0].to_le_bytes();
*array_mut_ref!(out, 1 * 4, 4) = words[1].to_le_bytes();
Expand Down
18 changes: 9 additions & 9 deletions src/portable.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
counter_high, counter_low, CVBytes, CVWords, IncrementCounter, BLOCK_LEN, IV, MSG_SCHEDULE,
OUT_LEN,
counter_high, counter_low, BlockBytes, BlockWords, CVBytes, CVWords, IncrementCounter,
BLOCK_LEN, IV, MSG_SCHEDULE, OUT_LEN,
};
use arrayref::{array_mut_ref, array_ref};

#[inline(always)]
fn g(state: &mut [u32; 16], a: usize, b: usize, c: usize, d: usize, x: u32, y: u32) {
fn g(state: &mut BlockWords, a: usize, b: usize, c: usize, d: usize, x: u32, y: u32) {
state[a] = state[a].wrapping_add(state[b]).wrapping_add(x);
state[d] = (state[d] ^ state[a]).rotate_right(16);
state[c] = state[c].wrapping_add(state[d]);
Expand All @@ -17,7 +17,7 @@ fn g(state: &mut [u32; 16], a: usize, b: usize, c: usize, d: usize, x: u32, y: u
}

#[inline(always)]
fn round(state: &mut [u32; 16], msg: &[u32; 16], round: usize) {
fn round(state: &mut BlockWords, msg: &BlockWords, round: usize) {
// Select the message schedule based on the round.
let schedule = MSG_SCHEDULE[round];

Expand All @@ -37,11 +37,11 @@ fn round(state: &mut [u32; 16], msg: &[u32; 16], round: usize) {
#[inline(always)]
fn compress_pre(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u32; 16] {
) -> BlockWords {
let block_words = crate::platform::words_from_le_bytes_64(block);

let mut state = [
Expand Down Expand Up @@ -76,7 +76,7 @@ fn compress_pre(

pub fn compress_in_place(
cv: &mut CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand All @@ -95,11 +95,11 @@ pub fn compress_in_place(

pub fn compress_xof(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u8; 64] {
) -> BlockBytes {
let mut state = compress_pre(cv, block, block_len, counter, flags);
state[0] ^= state[8];
state[1] ^= state[9];
Expand Down
12 changes: 6 additions & 6 deletions src/rust_sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::arch::x86::*;
use core::arch::x86_64::*;

use crate::{
counter_high, counter_low, CVBytes, CVWords, IncrementCounter, BLOCK_LEN, IV, MSG_SCHEDULE,
OUT_LEN,
counter_high, counter_low, BlockBytes, CVBytes, CVWords, IncrementCounter, BLOCK_LEN, IV,
MSG_SCHEDULE, OUT_LEN,
};
use arrayref::{array_mut_ref, array_ref, mut_array_refs};

Expand Down Expand Up @@ -149,7 +149,7 @@ unsafe fn blend_epi16(a: __m128i, b: __m128i, imm8: i32) -> __m128i {
#[inline(always)]
unsafe fn compress_pre(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand Down Expand Up @@ -335,7 +335,7 @@ unsafe fn compress_pre(
#[target_feature(enable = "sse2")]
pub unsafe fn compress_in_place(
cv: &mut CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand All @@ -348,11 +348,11 @@ pub unsafe fn compress_in_place(
#[target_feature(enable = "sse2")]
pub unsafe fn compress_xof(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u8; 64] {
) -> BlockBytes {
let [mut row0, mut row1, mut row2, mut row3] =
compress_pre(cv, block, block_len, counter, flags);
row0 = xor(row0, row2);
Expand Down
12 changes: 6 additions & 6 deletions src/rust_sse41.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::arch::x86::*;
use core::arch::x86_64::*;

use crate::{
counter_high, counter_low, CVBytes, CVWords, IncrementCounter, BLOCK_LEN, IV, MSG_SCHEDULE,
OUT_LEN,
counter_high, counter_low, BlockBytes, CVBytes, CVWords, IncrementCounter, BLOCK_LEN, IV,
MSG_SCHEDULE, OUT_LEN,
};
use arrayref::{array_mut_ref, array_ref, mut_array_refs};

Expand Down Expand Up @@ -140,7 +140,7 @@ unsafe fn undiagonalize(row0: &mut __m128i, row2: &mut __m128i, row3: &mut __m12
#[inline(always)]
unsafe fn compress_pre(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand Down Expand Up @@ -326,7 +326,7 @@ unsafe fn compress_pre(
#[target_feature(enable = "sse4.1")]
pub unsafe fn compress_in_place(
cv: &mut CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
Expand All @@ -339,11 +339,11 @@ pub unsafe fn compress_in_place(
#[target_feature(enable = "sse4.1")]
pub unsafe fn compress_xof(
cv: &CVWords,
block: &[u8; BLOCK_LEN],
block: &BlockBytes,
block_len: u8,
counter: u64,
flags: u8,
) -> [u8; 64] {
) -> BlockBytes {
let [mut row0, mut row1, mut row2, mut row3] =
compress_pre(cv, block, block_len, counter, flags);
row0 = xor(row0, row2);
Expand Down
Loading