Skip to content

Commit

Permalink
add derive key
Browse files Browse the repository at this point in the history
Signed-off-by: Pantelis Antoniou <[email protected]>
  • Loading branch information
pantoniou committed Sep 7, 2023
1 parent 3938e8e commit 0cda9a9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
39 changes: 17 additions & 22 deletions src/blake3/blake3_host_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "blake3_impl.h"
#include "blake3_internal.h"

// turns out it's not competitive
#define BLAKE3_CPUSIMD_DISABLE

static const blake3_backend *blake3_select_function(uint64_t selectable_backends, blake3_func_id fid);

static uint64_t supported_cpusimd_backend(void);
Expand Down Expand Up @@ -375,15 +378,6 @@ static blake3_backend blake3_backends[B3BID_COUNT] = {
},
};

struct cpusimd_data {
unsigned int num_cpus;
unsigned int simd_cpus;
unsigned int be_simd_degree_mult;
const blake3_backend *be_best;
char *description;
blake3_thread_pool *tp;
};

static uint64_t supported_cpusimd_backend(void)
{
const blake3_backend *be = &blake3_backends[B3BID_CPUSIMD];
Expand All @@ -397,6 +391,17 @@ static uint64_t detected_cpusimd_backend(void)
return be->info.simd_degree ? B3BF_CPUSIMD : 0;
}

#ifndef BLAKE3_CPUSIMD_DISABLE

struct cpusimd_data {
unsigned int num_cpus;
unsigned int simd_cpus;
unsigned int be_simd_degree_mult;
const blake3_backend *be_best;
char *description;
blake3_thread_pool *tp;
};

typedef struct _blake3_hash_many_common_state {
blake3_hash_many_f hash_many;
size_t blocks;
Expand Down Expand Up @@ -498,19 +503,12 @@ static void blake3_hash_many_cpusimd(
outi += BLAKE3_OUT_LEN * stepi;
}

#if 0
for (i = 0, s = states; i < states_count; i++, s++) {
c = s->common;
c->hash_many(s->inputs, s->num_inputs, c->blocks, c->key, s->counter, c->increment_counter, c->flags, c->flags_start, c->flags_end, s->out);
}
#else
if (!d->tp) {
d->tp = blake3_thread_pool_create(d->simd_cpus);
assert(d->tp);
}

blake3_thread_arg_array_join(d->tp, blake3_cpusimd_hash_many_thread, NULL, states, sizeof(states[0]), states_count);
#endif

#ifdef CPUSIMD_CHECK
for (i = 0; i < count; i += stepi) {
Expand All @@ -523,9 +521,7 @@ static void blake3_hash_many_cpusimd(
#endif
}

static bool disable_cpusimd = true;

#define CPUSIMD_MULT 16
#define CPUSIMD_MULT 2

static void __attribute__((constructor))
cpusimd_setup(void)
Expand All @@ -538,9 +534,6 @@ cpusimd_setup(void)
uint64_t supported_backends, detected_backends, selectable_backends;
ssize_t n;

if (disable_cpusimd)
return;

scval = sysconf(_SC_NPROCESSORS_ONLN);
assert(scval > 0);

Expand Down Expand Up @@ -619,6 +612,8 @@ cpusimd_cleanup(void)
free(d);
}

#endif

static void dump_backends(blake3_host_state *hs, uint64_t backends)
{
const blake3_backend *be;
Expand Down
3 changes: 1 addition & 2 deletions src/blake3/blake3_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,7 @@ void blake3_thread_work_join(blake3_thread_pool *tp, const blake3_thread_work *w
/* finally wait for all threaded work to complete */
for (i = 0; i < thread_work_count; i++) {
t = threads[i];
if (!t)
continue; /* thread might be cancelled */
assert(t);
blake3_thread_wait_work(t);
blake3_thread_pool_unreserve(tp, t);
}
Expand Down
34 changes: 25 additions & 9 deletions src/internal/fy-b3sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ void test_thread_latency(unsigned int num_threads)
#define OPT_MT_DEGREE 132
#define OPT_LIST_BACKENDS 133
#define OPT_NO_CPUSIMD 134
#define OPT_DERIVE_KEY 135
#define OPT_TEST 196

static struct option lopts[] = {
Expand All @@ -277,25 +278,30 @@ static struct option lopts[] = {
{"list-backends", no_argument, 0, OPT_LIST_BACKENDS },
{"test", no_argument, 0, OPT_TEST },
{"no-cpusimd", no_argument, 0, OPT_NO_CPUSIMD },
{"derive-key", required_argument, 0, OPT_DERIVE_KEY },
{"debug", no_argument, 0, 'd' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 },
};

static void display_usage(FILE *fp, char *progname)
{
fprintf(fp, "Usage: %s [options] [files]\n", progname);
fprintf(fp, "\nOptions:\n\n");
fprintf(fp, "Usage:\n\t%s [options] [args]\n", progname);
fprintf(fp, "\noptions:\n");
fprintf(fp, "\t--derive-key <context> : Key derivation mode, with the given context string\n");
fprintf(fp, "\t--num-threads <n> : Number of threads to use (default: number of CPUs * 3 / 2)\n");
fprintf(fp, "\t--bufsz-log2 <n> : Number of bits for the bufsz (default: 16)\n");
fprintf(fp, "\t--list-backends : List available backends\n");
fprintf(fp, "\t--backend <arg>, -b <arg> : Backend selection\n");
fprintf(fp, "\t--no-mmap : Disable file mmap\n");
fprintf(fp, "\t--no-mthread : Disable multithreading\n");
fprintf(fp, "\t--mt-degree <n> : Set the multi-thread degree (default 128)\n");
fprintf(fp, "\t--list-backends : List available backends\n");
fprintf(fp, "\t--no-cpusimd : Disable CPUSIMD\n");
fprintf(fp, "\t--bufsz-log2 <n> : Number of bits for the bufsz (default: 20 - 1MB)\n");
fprintf(fp, "\t--debug : Enable debug messages\n");
fprintf(fp, "\t--help, -h : Display help message\n");
fprintf(fp, "\n");
fprintf(fp, "\nargs:\n");
fprintf(fp, "\t<file>... Files to hash or checkfiles to check.\n\tIf no file given (or file is '-' hash stdin\n");

if (fp == stderr)
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -329,15 +335,16 @@ int main(int argc, char *argv[])
uint8_t output[BLAKE3_OUT_LEN];
const char *filename;
uint8_t *buf;
int bufsz_log2 = 16; /* 64K */
int bufsz_log2 = 20; /* 1M */
size_t bufsz;
int mt_degree = 64;
size_t rdn, filesize, offset;
FILE *fp = NULL;
int i, opt, lidx, rc, num_inputs, num_ok, fd = -1, exitcode;
int i, j, opt, lidx, rc, num_inputs, num_ok, fd = -1, exitcode;
bool no_mmap = false, no_mthread = false, debug = false, no_cpusimd = false;
int num_threads = 0;
const char *backend = "";
const char *context = NULL;
bool do_test = false;

exitcode = EXIT_FAILURE;
Expand Down Expand Up @@ -381,6 +388,9 @@ int main(int argc, char *argv[])
case OPT_NO_CPUSIMD:
no_cpusimd = true;
break;
case OPT_DERIVE_KEY:
context = optarg;
break;

case OPT_TEST:
do_test = true;
Expand Down Expand Up @@ -431,7 +441,10 @@ int main(int argc, char *argv[])
goto err_out;
}

hasher = blake3_hasher_create(host_state, NULL, NULL, 0);
if (context)
hasher = blake3_hasher_create(host_state, NULL, context, 0);
else
hasher = blake3_hasher_create(host_state, NULL, NULL, 0);
if (!hasher) {
fprintf(stderr, "unable to create blake3 hasher\n");
goto err_out;
Expand All @@ -442,6 +455,9 @@ int main(int argc, char *argv[])

filename = argv[i];

if (debug)
fprintf(stderr, "processing file %s\n", filename);

if (!strcmp(filename, "-")) {
fp = stdin;
} else {
Expand Down Expand Up @@ -521,8 +537,8 @@ int main(int argc, char *argv[])
blake3_hasher_finalize(hasher, output, BLAKE3_OUT_LEN);

// Print the hash as hexadecimal.
for (i = 0; i < BLAKE3_OUT_LEN; i++)
printf("%02x", output[i]);
for (j = 0; j < BLAKE3_OUT_LEN; j++)
printf("%02x", output[j]);
printf(" %s\n", filename);

if (fp) {
Expand Down

0 comments on commit 0cda9a9

Please sign in to comment.