This document describes the architecture and design principles of the HPCrypt cryptography library.
HPCrypt follows these core principles:
- Security First: Prioritize correctness and security over performance
- No Unsafe Code: 100% safe Rust implementations
- no_std Compatible: Support embedded and bare-metal environments
- Modular Design: Each crate focuses on a specific domain
- Pay for What You Use: Fine-grained feature flags and minimal dependencies
- Well Documented: Production-ready documentation and examples
- Thoroughly Tested: Comprehensive test coverage with KAT vectors
HPCrypt uses a multi-crate workspace structure:
Layer 0 (Primitives):
├── hpcrypt-core # Error types, utilities
├── hpcrypt-hash # Hash functions (SHA-2, SHA-3, BLAKE2/3)
├── hpcrypt-curves # Elliptic curves (NIST, secp256k1, Ed25519, X25519)
├── hpcrypt-rng # Random number generation
└── hpcrypt-cipher # Block ciphers and modes (AES, ChaCha20)
Layer 1 (Cryptographic Schemes):
├── hpcrypt-mac # MACs (HMAC, CMAC, Poly1305, GHASH, KMAC)
├── hpcrypt-aead # Authenticated encryption (AES-GCM, ChaCha20-Poly1305, Ascon)
├── hpcrypt-kdf # Key derivation (HKDF, PBKDF2, Argon2, scrypt)
├── hpcrypt-signatures # Digital signatures (ECDSA, EdDSA)
├── hpcrypt-rsa # RSA signatures and encryption
├── hpcrypt-mlkem # Post-quantum KEM (FIPS 203)
├── hpcrypt-mldsa # Post-quantum signatures (FIPS 204)
├── hpcrypt-slhdsa # Post-quantum hash-based signatures (FIPS 205)
└── hpcrypt-threshold # Shamir's Secret Sharing
Layer 2 (Protocols):
├── hpcrypt-ecies # Elliptic Curve Integrated Encryption
├── hpcrypt-hpke # Hybrid Public Key Encryption (RFC 9180)
├── hpcrypt-fpe # Format-Preserving Encryption (FF1)
├── hpcrypt-pake # Password-Authenticated Key Exchange (OPAQUE)
└── hpcrypt-srp # Secure Remote Password (SRP-6a)
Layer 3 (Umbrella):
└── hpcrypt # Re-exports all crates with feature gates
- Layered Architecture: Dependencies flow downward only - no circular dependencies
- Separation of Concerns: Encryption (cipher), authentication (mac), and combined (aead) are separate
- Self-Contained Primitives: Layer 0 has no internal dependencies
- Protocol Independence: High-level protocols can be used standalone
- Minimal External Dependencies: Only essential crates (
subtle,zeroize,sha3,getrandom,num-bigint)
std: Standard library support (default)alloc: Heap allocation without stdserde: Serialization support
ascon-only: Minimal AEAD for embedded (hpcrypt-aead)os-rng: OS random number generation (default)quic-header-protection: QUIC protocol support (hpcrypt-kdf)- Algorithm-specific flags:
p256,secp256k1,ed25519, etc.
# Minimal embedded: Only Ascon AEAD
hpcrypt-aead = { version = "0.1", default-features = false, features = ["ascon-only"] }
# Umbrella with symmetric crypto only
hpcrypt = { version = "0.1", features = ["symmetric"] }
# Everything
hpcrypt = { version = "0.1", features = ["full"] }The codebase includes multiple layers of testing:
- Unit Tests: Inline tests in each module
- Integration Tests: Public API testing
- Known Answer Tests (KAT): From authoritative sources
- Test Vector Sources:
- Wycheproof (Google's crypto test suite)
- NIST CAVP (Cryptographic Algorithm Validation Program)
- RFC test vectors (protocol-specific)
crate/
├── src/*.rs # Unit tests
├── tests/ # Integration and KAT tests
│ ├── wycheproof_*.rs
│ ├── cavp_*.rs
│ └── rfc_*.rs
└── benches/ # Performance benchmarks
- Release builds use LTO and aggressive optimization (
opt-level = 3) - Multiple implementation variants for critical operations (MACs have 30+ variants)
- Hardware acceleration where available (AVX2, PCLMULQDQ on x86/x86_64)
- Graceful fallback to portable implementations
- Field arithmetic (Montgomery/Barrett reduction)
- NTT for lattice crypto
- GHASH/POLYVAL (hardware-accelerated polynomial evaluation)
- Hash function compression loops
- Constant-time operations for sensitive data (via
subtlecrate) - No secret-dependent branches or memory access
- Multiple constant-time implementation variants tested
- Automatic zeroing on drop (via
zeroize) - Constant-time equality checks
- Protected memory for private keys
- NIST FIPS: ML-KEM (203), ML-DSA (204), SLH-DSA (205)
- NIST SP: AES-GCM (800-38D), FF1 (800-38G)
- RFCs: HPKE (9180), OPAQUE (9497), ChaCha20-Poly1305 (8439), and more
- PKCS/SEC: RSA (PKCS#1 v2.2), ECIES (SEC 1 v2.0)
When adding functionality:
- Respect the layer structure
- Minimize external dependencies
- Add comprehensive tests with KAT vectors
- Include benchmarks
- Document security properties
- Design for no_std first
- NIST FIPS 203/204/205 - Post-quantum cryptography
- RFC 9180 - HPKE
- RFC 8439 - ChaCha20-Poly1305
- RFC 5869 - HKDF
- RFC 9497 - OPAQUE
- Wycheproof - Google's test suite
- NIST CAVP - Algorithm validation
For questions or design discussions, please open an issue on GitHub.