BLAKE3-team / BLAKE3

the official Rust and C implementations of the BLAKE3 cryptographic hash function
Apache License 2.0
4.71k stars 315 forks source link

Reference implementation of Blake3 for generic 16/32/64 bit CPUs? #364

Open G8TIC opened 7 months ago

G8TIC commented 7 months ago

While BLAKE3 is fantastic in terms of performance and optimisations across CPUs and operating systems with support for SSE, AVX, NEON etc. there doesn't appear to be a generic implementation, i.e. a non-optimised version that just works on any CPU, for example STM32, ESP32, ESP8266, ArmHF, i386 or anything else released in the last 25-30 years...

Can we have a "reference implementation" of BLAKE3 with just two files "blake3.c" and "blake3.h" that is written in pure C and compiles on any hardware with uint32_t and C99?

To me, the "refrence implementation" should come with a test harness and test vectors and should be the basis of an RFC for what looks to be a solid foundation. The optimised versions could be derivatives of the reference implementation...

A reference "baseline" implementation would be of huge benefit to low bandwidth and IoT type applications where the complexities of libraries and hand-crafted assembler optimisations are either not appropriate or warranted.

oconnor663 commented 7 months ago

The C and Rust implementations of BLAKE3 should both be portable to ~any CPU. The portable implementations are blake3_portable.c and portable.rs. You might also be interested in the reference_impl.

BurningEnlightenment commented 7 months ago

There is a pure C99 implementation in c/blake3_portable.c. It shares infrastructure/some code with the various SIMD implementations, but I don't see why that'd be a problem. Sure, it isn't condensed in one TU, but otherwise it should readily fulfill your requirements.

divinity76 commented 3 months ago

fwiw

cc -DBLAKE3_NO_AVX512 -DBLAKE3_NO_SSE41 -DBLAKE3_NO_AVX2 -DBLAKE3_NO_SSE2 -DBLAKE3_USE_NEON=0 c/blake3.c c/blake3_dispatch.c c/blake3_portable.c

should compile pretty much anywhere with a pure C implementation :)

(until BLAKE3_NO_RISCV_P comes along anyway.. maybe a BLAKE3_PORTABLE flag could be useful? idk)