Crate smchash

Crate smchash 

Source
Expand description

§smcHash

High-performance hash function optimized for modern CPUs.

§Features

  • Fast: Passes all 188 SMHasher3 quality tests
  • Cache-friendly: Processes 128 bytes (2 cache lines) per iteration
  • Parallel: 8 lanes for maximum ILP on ARM64
  • no_std compatible: Works in embedded environments
  • Built-in PRNG: smc_rand passes BigCrush and PractRand

§Quick Start

use smchash::{smchash, smchash_seeded, smc_rand};

// Basic hashing
let hash = smchash(b"Hello, World!");
assert_eq!(hash, 0x25bb0982c5c0de6e);

// Seeded hashing (different seed = different hash)
let hash1 = smchash_seeded(b"data", 1);
let hash2 = smchash_seeded(b"data", 2);
assert_ne!(hash1, hash2);

// PRNG (passes BigCrush/PractRand)
let mut seed = 42u64;
let r1 = smc_rand(&mut seed);
let r2 = smc_rand(&mut seed);
assert_ne!(r1, r2);

§Custom Secrets

For unique per-application hashing (e.g., HashDoS protection):

use smchash::smchash_secret;

// Your application's unique secrets (must be 9 elements)
let secret: [u64; 9] = [
    0x9ad1e8e2aa5a5c4b, 0xaaaad2335647d21b, 0xb8ac35e269d1b495,
    0xa98d653cb2b4c959, 0x71a5b853b43ca68b, 0x2b55934dc35c9655,
    0x746ae48ed4d41e4d, 0xa3d8c38e78aaa6a9, 0x1bca69c565658bc3,
];

let hash = smchash_secret(b"data", 0, &secret);

§Performance

Benchmarks on Apple M4 Max:

  • Small keys (≤16 bytes): ~2 GB/s
  • Large keys: ~15 GB/s

§Algorithm

  • 128-bit MUM (Multiply-XOR-Mix) construction
  • 8 parallel lanes for bulk processing
  • Secrets are odd, prime, 32 bits set, pairwise hamming distance = 32

§License

MIT License - Copyright 2025 ScaleCode Solutions

Functions§

smc_rand
Pseudo-random number generator.
smchash
Compute smcHash of the given data.
smchash_secret
Compute smcHash with custom secrets.
smchash_seeded
Compute smcHash with a custom seed.