Skip to content

Commit

Permalink
Update BLAKE3 for using the new impl handling
Browse files Browse the repository at this point in the history
This commit changes the BLAKE3 implementation handling and
also the calls to it from the ztest command.

Signed-off-by: Tino Reichardt <milky-zfs@mcmilk.de>
  • Loading branch information
mcmilk committed Aug 23, 2022
1 parent c002dc8 commit 62c3dd4
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 497 deletions.
6 changes: 4 additions & 2 deletions cmd/ztest.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
#include <libnvpair.h>
#include <libzutil.h>
#include <sys/crypto/icp.h>
#include <sys/zfs_impl.h>
#if (__GLIBC__ && !__UCLIBC__)
#include <execinfo.h> /* for backtrace() */
#endif
Expand Down Expand Up @@ -6389,6 +6390,7 @@ ztest_blake3(ztest_ds_t *zd, uint64_t id)
int i, *ptr;
uint32_t size;
BLAKE3_CTX ctx;
const zfs_impl_t *blake3 = zfs_impl_get_ops("blake3");

size = ztest_random_blocksize();
buf = umem_alloc(size, UMEM_NOFAIL);
Expand All @@ -6413,7 +6415,7 @@ ztest_blake3(ztest_ds_t *zd, uint64_t id)
void *res2 = &zc_res2;

/* BLAKE3_KEY_LEN = 32 */
VERIFY0(blake3_set_impl_name("generic"));
VERIFY0(blake3->setname("generic"));
templ = abd_checksum_blake3_tmpl_init(&salt);
Blake3_InitKeyed(&ctx, salt_ptr);
Blake3_Update(&ctx, buf, size);
Expand All @@ -6422,7 +6424,7 @@ ztest_blake3(ztest_ds_t *zd, uint64_t id)
ZIO_CHECKSUM_BSWAP(&zc_ref2);
abd_checksum_blake3_tmpl_free(templ);

VERIFY0(blake3_set_impl_name("cycle"));
VERIFY0(blake3->setname("cycle"));
while (run_count-- > 0) {

/* Test current implementation */
Expand Down
31 changes: 5 additions & 26 deletions include/sys/blake3.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
/*
* Based on BLAKE3 v1.3.1, https://github.com/BLAKE3-team/BLAKE3
* Copyright (c) 2019-2020 Samuel Neves and Jack O'Connor
* Copyright (c) 2021 Tino Reichardt <milky-zfs@mcmilk.de>
* Copyright (c) 2021-2022 Tino Reichardt <milky-zfs@mcmilk.de>
*/

#ifndef BLAKE3_H
#define BLAKE3_H
#ifndef _SYS_BLAKE3_H
#define _SYS_BLAKE3_H

#ifdef _KERNEL
#include <sys/types.h>
Expand Down Expand Up @@ -72,7 +72,7 @@ typedef struct {
*/
uint8_t cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN];

/* const blake3_impl_ops_t *ops */
/* const blake3_ops_t *ops */
const void *ops;
} BLAKE3_CTX;

Expand All @@ -97,29 +97,8 @@ extern void **blake3_per_cpu_ctx;
extern void blake3_per_cpu_ctx_init(void);
extern void blake3_per_cpu_ctx_fini(void);

/* return number of supported implementations */
extern int blake3_get_impl_count(void);

/* return id of selected implementation */
extern int blake3_get_impl_id(void);

/* return name of selected implementation */
extern const char *blake3_get_impl_name(void);

/* setup id as fastest implementation */
extern void blake3_set_impl_fastest(uint32_t id);

/* set implementation by id */
extern void blake3_set_impl_id(uint32_t id);

/* set implementation by name */
extern int blake3_set_impl_name(const char *name);

/* set startup implementation */
extern void blake3_setup_impl(void);

#ifdef __cplusplus
}
#endif

#endif /* BLAKE3_H */
#endif /* _SYS_BLAKE3_H */
16 changes: 8 additions & 8 deletions module/icp/algs/blake3/blake3.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static output_t make_output(const uint32_t input_cv[8],
* bytes. For that reason, chaining values in the CV stack are represented as
* bytes.
*/
static void output_chaining_value(const blake3_impl_ops_t *ops,
static void output_chaining_value(const blake3_ops_t *ops,
const output_t *ctx, uint8_t cv[32])
{
uint32_t cv_words[8];
Expand All @@ -139,7 +139,7 @@ static void output_chaining_value(const blake3_impl_ops_t *ops,
store_cv_words(cv, cv_words);
}

static void output_root_bytes(const blake3_impl_ops_t *ops, const output_t *ctx,
static void output_root_bytes(const blake3_ops_t *ops, const output_t *ctx,
uint64_t seek, uint8_t *out, size_t out_len)
{
uint64_t output_block_counter = seek / 64;
Expand All @@ -163,7 +163,7 @@ static void output_root_bytes(const blake3_impl_ops_t *ops, const output_t *ctx,
}
}

static void chunk_state_update(const blake3_impl_ops_t *ops,
static void chunk_state_update(const blake3_ops_t *ops,
blake3_chunk_state_t *ctx, const uint8_t *input, size_t input_len)
{
if (ctx->buf_len > 0) {
Expand Down Expand Up @@ -230,7 +230,7 @@ static size_t left_len(size_t content_len)
* number of chunks hashed. These chunks are never the root and never empty;
* those cases use a different codepath.
*/
static size_t compress_chunks_parallel(const blake3_impl_ops_t *ops,
static size_t compress_chunks_parallel(const blake3_ops_t *ops,
const uint8_t *input, size_t input_len, const uint32_t key[8],
uint64_t chunk_counter, uint8_t flags, uint8_t *out)
{
Expand Down Expand Up @@ -274,7 +274,7 @@ static size_t compress_chunks_parallel(const blake3_impl_ops_t *ops,
* return it as an additional output.) These parents are never the root and
* never empty; those cases use a different codepath.
*/
static size_t compress_parents_parallel(const blake3_impl_ops_t *ops,
static size_t compress_parents_parallel(const blake3_ops_t *ops,
const uint8_t *child_chaining_values, size_t num_chaining_values,
const uint32_t key[8], uint8_t flags, uint8_t *out)
{
Expand Down Expand Up @@ -320,7 +320,7 @@ static size_t compress_parents_parallel(const blake3_impl_ops_t *ops,
* of implementing this special rule? Because we don't want to limit SIMD or
* multi-threading parallelism for that update().
*/
static size_t blake3_compress_subtree_wide(const blake3_impl_ops_t *ops,
static size_t blake3_compress_subtree_wide(const blake3_ops_t *ops,
const uint8_t *input, size_t input_len, const uint32_t key[8],
uint64_t chunk_counter, uint8_t flags, uint8_t *out)
{
Expand Down Expand Up @@ -406,7 +406,7 @@ static size_t blake3_compress_subtree_wide(const blake3_impl_ops_t *ops,
* As with compress_subtree_wide(), this function is not used on inputs of 1
* chunk or less. That's a different codepath.
*/
static void compress_subtree_to_parent_node(const blake3_impl_ops_t *ops,
static void compress_subtree_to_parent_node(const blake3_ops_t *ops,
const uint8_t *input, size_t input_len, const uint32_t key[8],
uint64_t chunk_counter, uint8_t flags, uint8_t out[2 * BLAKE3_OUT_LEN])
{
Expand Down Expand Up @@ -434,7 +434,7 @@ static void hasher_init_base(BLAKE3_CTX *ctx, const uint32_t key[8],
memcpy(ctx->key, key, BLAKE3_KEY_LEN);
chunk_state_init(&ctx->chunk, key, flags);
ctx->cv_stack_len = 0;
ctx->ops = blake3_impl_get_ops();
ctx->ops = blake3_get_ops();
}

/*
Expand Down
7 changes: 4 additions & 3 deletions module/icp/algs/blake3/blake3_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,17 @@ static inline void blake3_hash_many_generic(const uint8_t * const *inputs,
}
}

static inline boolean_t blake3_is_generic_supported(void)
/* the generic implementation is always okay */
static boolean_t blake3_is_supported(void)
{
return (B_TRUE);
}

const blake3_impl_ops_t blake3_generic_impl = {
const blake3_ops_t blake3_generic_impl = {
.compress_in_place = blake3_compress_in_place_generic,
.compress_xof = blake3_compress_xof_generic,
.hash_many = blake3_hash_many_generic,
.is_supported = blake3_is_generic_supported,
.is_supported = blake3_is_supported,
.degree = 4,
.name = "generic"
};
Loading

0 comments on commit 62c3dd4

Please sign in to comment.