as-blake
v0.1.1
Published
Portable, SIMD-accelerated BLAKE3 for AssemblyScript - SWAR and degree-4 SIMD, dispatched at compile time.
Maintainers
Readme
Portable BLAKE3 for AssemblyScript, with a 128-bit Wasm SIMD fast path for large inputs and a compact SWAR fallback for non-SIMD builds.
as-blake exposes BLAKE-family one-shot hashing, raw pointer hashing, keyed hashing, key derivation, and incremental streaming. When compiled with --enable simd, large messages use degree-4 SIMD: four BLAKE3 chunks are compressed in parallel inside one v128.
Install
npm install as-blakeCompile with SIMD for the fast path:
asc assembly/index.ts --enable simd --enable bulk-memoryCompile without SIMD for a SWAR-only binary:
asc assembly/index.ts --enable bulk-memoryThe package asconfig.json enables SIMD and bulk memory by default.
Usage
import { blake3 } from "as-blake";
const digest = blake3.hash(data); // ArrayBuffer -> 32-byte ArrayBufferFor hot paths, write into a caller-owned output buffer:
import { blake3 } from "as-blake";
blake3.hashUnsafe(inPtr, inLen, outPtr); // writes 32 bytes at outPtrIncremental hashing uses Hasher:
import { blake3 } from "as-blake";
const h = new blake3.Hasher();
h.update(ptr0, len0);
h.update(ptr1, len1);
h.finalize(outPtr);API
hash(data: ArrayBuffer): ArrayBuffer;
hashUnsafe(inPtr: usize, inLen: usize, outPtr: usize): void;
hashSWAR(inPtr: usize, inLen: usize, outPtr: usize): void;
hashKeyed(keyPtr: usize, inPtr: usize, inLen: usize, outPtr: usize): void;
deriveKey(
contextPtr: usize,
contextLen: usize,
materialPtr: usize,
materialLen: usize,
outPtr: usize,
): void;
class Hasher {
static create(): Hasher;
static createKeyed(keyPtr: usize): Hasher;
static createDeriveKey(contextPtr: usize, contextLen: usize): Hasher;
update(ptr: usize, len: usize): void;
finalize(outPtr: usize): void;
}Notes:
hashallocates and returns a 32-byte digest.hashUnsafe,hashKeyed, andderiveKeywrite a 32-byte digest tooutPtr.hashKeyedexpects a 32-byte key atkeyPtr.hashSWARforces the non-SIMD path even in a SIMD build.- One-shot scratch-backed functions are not reentrant. Use
Hasherfor independent concurrent instances. - Inputs are raw
(ptr, len)pairs into little-endian Wasm memory.
Performance
Throughput at 1 MiB on an AMD Ryzen 7800X3D:
| runtime | SWAR | SIMD | speedup | | --- | ---: | ---: | ---: | | V8 | 845 MB/s | 1926 MB/s | 2.3x | | WAVM | 957 MB/s | 2954 MB/s | 3.1x | | wazero | 896 MB/s | 1369 MB/s | 1.5x |
The SIMD path uses a degree-4 kernel above 4 KiB and a degree-2 kernel in the 2-4 KiB range. Smaller inputs use the SWAR stream.
Development
npm install
npm test
npm run verifyRun benchmarks:
npm run bench -- blake3-swar
npm run bench -- blake3-simd
npm run bench:summaryOptional runtime-specific benchmarks:
npm run bench -- --wavm blake3-simd
npm run bench -- --wazero blake3-simdLicense
This project is distributed under an open source license. Work on this project is done by passion, but if you want to support it financially, you can do so by making a donation to the project's GitHub Sponsors page.
You can view the full license using the following link: License
Contact
Please send all issues to GitHub Issues and to converse, please send me an email at [email protected]
- Email: Send me inquiries, questions, or requests at [email protected]
- GitHub: Visit the official GitHub repository Here
- Website: Visit my official website at jairus.dev
- Discord: Contact me at My Discord or on the AssemblyScript Discord Server
