macula_crypto_nif (macula v1.4.30)
View SourceCryptographic operations for Macula mesh.
This module provides Ed25519 key generation, signing, and verification, BLAKE3 and SHA-256 hashing, and base64 encoding. It uses Rust NIFs when available, falling back to pure Erlang implementations otherwise.
NIF vs Erlang
The Rust NIFs provide significant performance improvements: - Key generation: ~10x faster - Signing: ~5x faster - Verification: ~8x faster - BLAKE3: ~20x faster - SHA-256: ~3x faster
The pure Erlang fallbacks ensure the module works even when NIFs cannot be loaded (e.g., different architecture, missing Rust toolchain).
Summary
Functions
Decode URL-safe base64 data. Returns {ok, Data} or {error, invalid_base64}.
Encode data as URL-safe base64 (no padding).
Compute BLAKE3 hash. Returns 32-byte hash binary.
Compute BLAKE3 hash and return as hex string. Returns 64-character hex string.
Compute BLAKE3 hash of multiple chunks (streaming). Returns 32-byte hash binary.
Verify data matches expected BLAKE3 hash.
Generate a new Ed25519 keypair. Returns {ok, {PublicKey, PrivateKey}} where both are 32-byte binaries.
Check if the NIF is loaded.
Constant-time comparison of two binaries. Important for security - prevents timing attacks.
Compute SHA-256 hash. Returns 32-byte hash binary.
Compute SHA-256 hash and encode as URL-safe base64. Returns base64-encoded string (no padding).
Sign a message with an Ed25519 private key. Returns {ok, Signature} where Signature is a 64-byte binary, or {error, invalid_private_key}.
Verify an Ed25519 signature. Returns true if valid, false otherwise.
Functions
Decode URL-safe base64 data. Returns {ok, Data} or {error, invalid_base64}.
Encode data as URL-safe base64 (no padding).
Compute BLAKE3 hash. Returns 32-byte hash binary.
Compute BLAKE3 hash and return as hex string. Returns 64-character hex string.
Compute BLAKE3 hash of multiple chunks (streaming). Returns 32-byte hash binary.
Verify data matches expected BLAKE3 hash.
Generate a new Ed25519 keypair. Returns {ok, {PublicKey, PrivateKey}} where both are 32-byte binaries.
-spec is_nif_loaded() -> boolean().
Check if the NIF is loaded.
Constant-time comparison of two binaries. Important for security - prevents timing attacks.
Compute SHA-256 hash. Returns 32-byte hash binary.
Compute SHA-256 hash and encode as URL-safe base64. Returns base64-encoded string (no padding).
-spec sign(Message :: binary(), PrivateKey :: binary()) -> {ok, Signature :: binary()} | {error, invalid_private_key}.
Sign a message with an Ed25519 private key. Returns {ok, Signature} where Signature is a 64-byte binary, or {error, invalid_private_key}.
Verify an Ed25519 signature. Returns true if valid, false otherwise.