Welcome to little lamb

Code » limb » commit 3f2cefe

blake3: Tweak interface

author Olivier Brunel
2023-03-26 13:42:41 UTC
committer Olivier Brunel
2023-03-26 14:31:10 UTC
parent 0ad440d8eaf5a25f2ccd8c9bd5fb76e70713676d

blake3: Tweak interface

doc/blake3.h.0.md +0 -4
doc/{blake3.3.md => blake3_init.3.md} +4 -10
include/limb/blake3.h +4 -6
meta/libs/limb +0 -1
src/blake3/blake3.c +0 -14
src/blake3/blake3_impl.c +10 -3
src/blake3/blake3_portable.c +1 -0

diff --git a/doc/blake3.h.0.md b/doc/blake3.h.0.md
index b4390e2..514fad6 100644
--- a/doc/blake3.h.0.md
+++ b/doc/blake3.h.0.md
@@ -35,7 +35,3 @@ The following functions are defined :
 
 : [blake3_final](3)
 :: To obtain the hash from a blake3 context.
-
-: [blake3](3)
-:: Convenience function to do all of the above at once, when the entire data is
-:: available in a single block.
diff --git a/doc/blake3.3.md b/doc/blake3_init.3.md
similarity index 62%
rename from doc/blake3.3.md
rename to doc/blake3_init.3.md
index ef1e155..2ab24e8 100644
--- a/doc/blake3.3.md
+++ b/doc/blake3_init.3.md
@@ -1,5 +1,5 @@
 % limb manual
-% blake3(3)
+% blake3_init(3)
 % limb 0.0.2
 % 2023-02-03
 
@@ -13,11 +13,9 @@ given block of data
     #include <limb/blake3.h>
 
 ```pre hl
-void blake3_init(blake3_ctx *<em>ctx</em>);
-void blake3_update(const void *<em>msg</em>, size_t <em>size</em>, blake3_ctx *<em>ctx</em>);
-void blake3_final(unsigned char * restrict <em>md</em>, blake3_ctx *<em>ctx</em>);
-
-void blake3(unsigned char * restrict <em>md</em>, const void *<em>msg</em>, size_t <em>size</em>);
+void blake3_init(void *<em>ctx</em>);
+void blake3_update(const void *<em>msg</em>, size_t <em>size</em>, void *<em>ctx</em>);
+void blake3_final(void * restrict <em>md</em>, void *<em>ctx</em>);
 ```
 
 # DESCRIPTION
@@ -31,7 +29,3 @@ function repeatedly as many times as needed.
 
 The `blake3_final`() function stores the calculated hash from `ctx` in binary
 form into `md`, which must be able to store 32 bytes.
-
-As a convenience, the `blake3`() function allows to compute the BLAKE3 digest of
-a given `msg` of length `size` in one call. The 32 bytes digest will be stored
-in `md`.
diff --git a/include/limb/blake3.h b/include/limb/blake3.h
index 9870e59..e5289e3 100644
--- a/include/limb/blake3.h
+++ b/include/limb/blake3.h
@@ -4,7 +4,7 @@
 #ifndef LIMB_BLAKE3_H
 #define LIMB_BLAKE3_H
 
-#include <string.h> /* size_t */
+#include <stddef.h> /* size_t */
 #include "limb/int.h"
 
 #define BLAKE3_KEY_LEN 32
@@ -37,10 +37,8 @@ typedef struct {
   u8 cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN];
 } blake3_ctx;
 
-extern void blake3_init(blake3_ctx *ctx);
-extern void blake3_update(const void *msg, size_t len, blake3_ctx *ctx);
-extern void blake3_final(unsigned char * restrict md, blake3_ctx *ctx);
-
-extern void blake3(unsigned char * restrict md, const void *msg, size_t size);
+extern void blake3_init(void *ctx);
+extern void blake3_update(const void *msg, size_t mlen, void *ctx);
+extern void blake3_final(void * restrict md, void *ctx);
 
 #endif /* LIMB_BLAKE3_H */
diff --git a/meta/libs/limb b/meta/libs/limb
index 1adf17e..53d195c 100644
--- a/meta/libs/limb
+++ b/meta/libs/limb
@@ -107,6 +107,5 @@ obj/blake3/blake3_impl.o
 obj/blake3/blake3_dispatch.o
 obj/blake3/blake3_portable.o
 $$(BLAKE3_OPTIMIZ)
-obj/blake3/blake3.o
 # skalibs dependency
 skalibs
diff --git a/src/blake3/blake3.c b/src/blake3/blake3.c
deleted file mode 100644
index fe1e8e8..0000000
--- a/src/blake3/blake3.c
+++ /dev/null
@@ -1,14 +0,0 @@
-/* This file is part of limb                           https://lila.oss/limb
- * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "blake3.h"
-
-void
-blake3(unsigned char * restrict md, const void *msg, size_t size)
-{
-    blake3_ctx ctx;
-
-    blake3_init(&ctx);
-    blake3_update(msg, size, &ctx);
-    blake3_final(md, &ctx);
-}
diff --git a/src/blake3/blake3_impl.c b/src/blake3/blake3_impl.c
index 1b13ad6..bc6372c 100644
--- a/src/blake3/blake3_impl.c
+++ b/src/blake3/blake3_impl.c
@@ -5,6 +5,7 @@
  * Copyright (C) 2019-2020 Samuel Neves and Jack O'Connor */
 /* SPDX-License-Identifier: CC0-1.0 OR Apache-2.0 */
 #include <assert.h>
+#include <string.h>
 #include "blake3.h"
 
 INLINE void
@@ -376,8 +377,10 @@ compress_subtree_to_parent_node(const u8 *input, size_t input_len, const u32 key
 }
 
 void
-blake3_init(blake3_ctx *ctx)
+blake3_init(void *ctx_)
 {
+    blake3_ctx *ctx = ctx_;
+
     memcpy(ctx->key, IV, BLAKE3_KEY_LEN);
     chunk_state_init(&ctx->chunk, IV, 0);
     ctx->cv_stack_len = 0;
@@ -448,8 +451,10 @@ hasher_push_cv(blake3_ctx *ctx, u8 new_cv[BLAKE3_OUT_LEN], u64 chunk_counter)
 }
 
 void
-blake3_update(const void *input, size_t input_len, blake3_ctx *ctx)
+blake3_update(const void *input, size_t input_len, void *ctx_)
 {
+    blake3_ctx *ctx = ctx_;
+
     /* Explicitly checking for zero avoids causing UB by passing a null pointer
      * to memcpy. This comes up in practice with things like:
      *   std::vector<u8> v;
@@ -561,8 +566,10 @@ blake3_update(const void *input, size_t input_len, blake3_ctx *ctx)
 }
 
 void
-blake3_final(unsigned char * restrict out, blake3_ctx *ctx)
+blake3_final(void * restrict out, void *ctx_)
 {
+    blake3_ctx *ctx = ctx_;
+
     /* If the subtree stack is empty, then the current chunk is the root. */
     if (ctx->cv_stack_len == 0) {
         output_t output = chunk_state_output(&ctx->chunk);
diff --git a/src/blake3/blake3_portable.c b/src/blake3/blake3_portable.c
index 70c03fb..5e0f5bb 100644
--- a/src/blake3/blake3_portable.c
+++ b/src/blake3/blake3_portable.c
@@ -4,6 +4,7 @@
  *  https://github.com/BLAKE3-team/BLAKE3
  * Copyright (C) 2019-2020 Samuel Neves and Jack O'Connor */
 /* SPDX-License-Identifier: CC0-1.0 OR Apache-2.0 */
+#include <string.h>
 #include "blake3.h"
 
 INLINE u32