Skip to content

Commit 1fcf8e9

Browse files
committed
add un-optimized generic blake3 implementation..
haven't figured out how to add the SSE2/SSE4.1/AVX2/AVX512/Arm+Neon optimized versions when applicable.. and other issues too, like PHP_BLAKE3_SPEC going to ask for help upstream
1 parent 75a7e5c commit 1fcf8e9

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

ext/hash/blake3/blake3_dispatch.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#endif
1515
#endif
1616

17+
#define MAYBE_UNUSED(x) (void)((x))
18+
1719
#if defined(IS_X86)
1820
static uint64_t xgetbv() {
1921
#if defined(_MSC_VER)
@@ -137,6 +139,7 @@ void blake3_compress_in_place(uint32_t cv[8],
137139
uint8_t flags) {
138140
#if defined(IS_X86)
139141
const enum cpu_feature features = get_cpu_features();
142+
MAYBE_UNUSED(features);
140143
#if !defined(BLAKE3_NO_AVX512)
141144
if (features & AVX512VL) {
142145
blake3_compress_in_place_avx512(cv, block, block_len, counter, flags);
@@ -165,6 +168,7 @@ void blake3_compress_xof(const uint32_t cv[8],
165168
uint8_t out[64]) {
166169
#if defined(IS_X86)
167170
const enum cpu_feature features = get_cpu_features();
171+
MAYBE_UNUSED(features);
168172
#if !defined(BLAKE3_NO_AVX512)
169173
if (features & AVX512VL) {
170174
blake3_compress_xof_avx512(cv, block, block_len, counter, flags, out);
@@ -193,6 +197,7 @@ void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,
193197
uint8_t flags_start, uint8_t flags_end, uint8_t *out) {
194198
#if defined(IS_X86)
195199
const enum cpu_feature features = get_cpu_features();
200+
MAYBE_UNUSED(features);
196201
#if !defined(BLAKE3_NO_AVX512)
197202
if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) {
198203
blake3_hash_many_avx512(inputs, num_inputs, blocks, key, counter,
@@ -242,6 +247,7 @@ void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs,
242247
size_t blake3_simd_degree(void) {
243248
#if defined(IS_X86)
244249
const enum cpu_feature features = get_cpu_features();
250+
MAYBE_UNUSED(features);
245251
#if !defined(BLAKE3_NO_AVX512)
246252
if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) {
247253
return 16;
@@ -268,3 +274,4 @@ size_t blake3_simd_degree(void) {
268274
#endif
269275
return 1;
270276
}
277+

ext/hash/config.m4

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ if test "$PHP_MHASH" != "no"; then
1111
AC_DEFINE(PHP_MHASH_BC, 1, [ ])
1212
fi
1313

14+
1415
if test $ac_cv_c_bigendian_php = yes; then
16+
dnl todo: check if blake3/blake3_neon.c is applicable
1517
EXT_HASH_SHA3_SOURCES="hash_sha3.c"
1618
AC_DEFINE(HAVE_SLOW_HASH3, 1, [Define is hash3 algo is available])
1719
AC_MSG_WARN("Use SHA3 slow implementation on bigendian")
@@ -33,13 +35,23 @@ else
3335
PHP_ADD_BUILD_DIR(ext/hash/$SHA3_DIR, 1)
3436
fi
3537

38+
EXT_HASH_BLAKE3_SOURCES="hash_blake3.c blake3/blake3.c blake3/blake3_dispatch.c blake3/blake3_portable.c"
39+
dnl todo: add optimized versions of blake3 for sse2/sse4.1/avx2/avx512/arm+neon
40+
dnl ...when applicable...
41+
dnl EXT_HASH_BLAKE3_SOURCES="$EXT_HASH_BLAKE3_SOURCES blake3/blake3_sse2.c blake3/blake3_sse41.c blake3/blake3_avx2.c blake3/blake3_avx512.c"
42+
43+
PHP_HASH_CFLAGS="$PHP_HASH_CFLAGS -DBLAKE3_NO_SSE2 -DBLAKE3_NO_SSE41 -DBLAKE3_NO_AVX2 -DBLAKE3_NO_AVX512"
44+
45+
46+
3647
EXT_HASH_SOURCES="hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c \
3748
hash_tiger.c hash_gost.c hash_snefru.c hash_whirlpool.c hash_adler32.c \
38-
hash_crc32.c hash_fnv.c hash_joaat.c $EXT_HASH_SHA3_SOURCES"
49+
hash_crc32.c hash_fnv.c hash_joaat.c $EXT_HASH_SHA3_SOURCES \
50+
$EXT_HASH_BLAKE3_SOURCES"
3951
EXT_HASH_HEADERS="php_hash.h php_hash_md.h php_hash_sha.h php_hash_ripemd.h \
4052
php_hash_haval.h php_hash_tiger.h php_hash_gost.h php_hash_snefru.h \
4153
php_hash_whirlpool.h php_hash_adler32.h php_hash_crc32.h \
42-
php_hash_fnv.h php_hash_joaat.h php_hash_sha3.h"
54+
php_hash_fnv.h php_hash_joaat.h php_hash_sha3.h php_hash_blake3.h"
4355

4456
PHP_NEW_EXTENSION(hash, $EXT_HASH_SOURCES, 0,,$PHP_HASH_CFLAGS)
4557
PHP_INSTALL_HEADERS(ext/hash, $EXT_HASH_HEADERS)

ext/hash/hash.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,7 @@ PHP_MINIT_FUNCTION(hash)
15861586
php_hash_register_algo("fnv164", &php_hash_fnv164_ops);
15871587
php_hash_register_algo("fnv1a64", &php_hash_fnv1a64_ops);
15881588
php_hash_register_algo("joaat", &php_hash_joaat_ops);
1589+
php_hash_register_algo("blake3", &php_hash_blake3_ops);
15891590

15901591
PHP_HASH_HAVAL_REGISTER(3,128);
15911592
PHP_HASH_HAVAL_REGISTER(3,160);

ext/hash/hash_blake3.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "php_hash.h"
2+
#include "php_hash_blake3.h"
3+
#include "blake3/blake3.h"
4+
#include <string.h> // memcpy
5+
6+
PHP_HASH_API void PHP_BLAKE3Init(PHP_BLAKE3_CTX *context)
7+
{
8+
blake3_hasher_init(context);
9+
}
10+
11+
PHP_HASH_API void PHP_BLAKE3Update(PHP_BLAKE3_CTX *context, const unsigned char *input, size_t len)
12+
{
13+
blake3_hasher_update(context, input, len);
14+
}
15+
16+
PHP_HASH_API void PHP_BLAKE3Final(unsigned char digest[BLAKE3_OUT_LEN/*32*/], PHP_BLAKE3_CTX *context)
17+
{
18+
blake3_hasher_finalize(context, digest, BLAKE3_OUT_LEN);
19+
}
20+
21+
PHP_HASH_API int PHP_BLAKE3Copy(const php_hash_ops *ops, PHP_BLAKE3_CTX *orig_context, PHP_BLAKE3_CTX *copy_context)
22+
{
23+
memcpy(copy_context, orig_context, sizeof(*orig_context));
24+
return SUCCESS;
25+
}

ext/hash/php_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ extern const php_hash_ops php_hash_fnv1a32_ops;
105105
extern const php_hash_ops php_hash_fnv164_ops;
106106
extern const php_hash_ops php_hash_fnv1a64_ops;
107107
extern const php_hash_ops php_hash_joaat_ops;
108+
extern const php_hash_ops php_hash_blake3_ops;
108109

109110
#define PHP_HASH_HAVAL_OPS(p,b) extern const php_hash_ops php_hash_##p##haval##b##_ops;
110111

ext/hash/php_hash_blake3.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef PHP_HASH_BLAKE3_H
2+
#define PHP_HASH_BLAKE3_H
3+
4+
#include "ext/standard/basic_functions.h"
5+
#include "php_hash.h"
6+
#include "blake3/blake3.h"
7+
8+
9+
// typedef struct blake3_hasher PHP_BLAKE3_CTX;
10+
#define PHP_BLAKE3_CTX blake3_hasher
11+
// help: what is V supposed to be?
12+
#define PHP_BLAKE3_SPEC "l.I_HAVE_NO_IDEA_WHAT_THIS_IS_SUPPOSED_TO_BE"
13+
14+
PHP_HASH_API void PHP_BLAKE3Init(PHP_BLAKE3_CTX *context);
15+
PHP_HASH_API void PHP_BLAKE3Update(PHP_BLAKE3_CTX *context, const unsigned char *input, size_t len);
16+
PHP_HASH_API void PHP_BLAKE3Final(unsigned char digest[BLAKE3_OUT_LEN/*32*/], PHP_BLAKE3_CTX *context);
17+
PHP_HASH_API int PHP_BLAKE3Copy(const php_hash_ops *ops, PHP_BLAKE3_CTX *orig_context, PHP_BLAKE3_CTX *copy_context);
18+
19+
const php_hash_ops php_hash_blake3_ops = {
20+
"blake3",
21+
(php_hash_init_func_t) PHP_BLAKE3Init,
22+
(php_hash_update_func_t) PHP_BLAKE3Update,
23+
(php_hash_final_func_t) PHP_BLAKE3Final,
24+
(php_hash_copy_func_t) PHP_BLAKE3Copy,
25+
php_hash_serialize,
26+
php_hash_unserialize,
27+
PHP_BLAKE3_SPEC, // << don't know what this should be, hopefully a dev that knows can remove this comment
28+
BLAKE3_OUT_LEN /*32*/,
29+
BLAKE3_CHUNK_LEN /*1024*/,
30+
sizeof(*((PHP_BLAKE3_CTX*)NULL)), // << bet there's a better way to write that
31+
1
32+
};
33+
34+
#endif
35+

0 commit comments

Comments
 (0)