RSPOW
A simple multi-algorithm proof-of-work library for Rust.
Algorithms
- SHA-256
- SHA-512
- RIPEMD-320
- Scrypt
- Argon2id
API references are available at docs.rs/rspow.
Difficulty Modes
RSPOW supports two difficulty modes:
- AsciiZeroPrefix (default): the hash must start with
difficultybytes of ASCII'0'(0x30).- Expected attempts grow by ~256 per additional byte.
- Simple to explain, but coarse-grained and often too steep for memory-hard hashes.
- LeadingZeroBits: the hash must have at least
difficultyleading zero bits.- Expected attempts ≈
2^difficulty. - Fine-grained control suitable for tuning across a wide range.
- Expected attempts ≈
Notes:
PoW::calculate_target()returns the ASCII'0'prefix and is meaningful only forAsciiZeroPrefix.- In
LeadingZeroBitsmode, thetargetslice is ignored; pass an empty slice for clarity.
Examples
ASCII '0' prefix (default)
use ;
let data = "hello world";
let difficulty = 2; // requires prefix "00"
let algorithm = Sha2_512;
let pow = new.unwrap;
let target = pow.calculate_target; // [0x30; difficulty]
let = pow.calculate_pow;
assert!;
assert!;
Leading zero bits (fine-grained)
use ;
let data = "hello world";
let bits = 12; // expected attempts ~ 2^12 = 4096
let algorithm = Sha2_256;
let pow = with_mode.unwrap;
let = pow.calculate_pow; // target is ignored in bits mode
assert!;
assert!;
Argon2id with custom parameters
use ;
let data = b"hello world";
// Example parameters only; tune for your threat model.
let params = new.unwrap;
let algorithm = Argon2id;
// Prefer LeadingZeroBits for smoother tuning with memory-hard functions
let bits = 8; // expected attempts ~ 256
let pow = with_mode.unwrap;
let = pow.calculate_pow;
assert!;
Tuning Guidance
- LeadingZeroBits: each additional bit doubles expected attempts; choose
bitsto match your time budget. - AsciiZeroPrefix: each additional byte multiplies attempts by ~256; easy but coarse.
- Memory-hard algorithms (e.g., Argon2id, Scrypt) may make multi-byte ASCII prefix targets impractical; prefer
LeadingZeroBits.
Compatibility
- Existing code using
PoW::newandcalculate_target()keeps the legacy behavior by default. - New code is encouraged to adopt
DifficultyMode::LeadingZeroBitsfor precise difficulty control.