This crate provides a collection of useful cryptographic primitives and components for use in higher-level Blockchain Commons projects. All the types are CBOR serializable, and many can also be serialized to and from URs (Uniform Resources).
# Build the crate
cargo build
# Run tests
cargo test
cargo test --test <test_name>
cargo test --doc
# Check code quality
cargo clippy -- -D warnings
# Build documentation
cargo doc --no-deps --target-dir cargo-docs- Production quality - Write code as you would for a real-world implementation
- Clean code - Fix all compiler errors and Clippy lints
- Security focus - Cryptographic primitives must adhere to best practices and be thoroughly tested
- Don't mark tasks as complete until all tests pass
- Security-critical components require comprehensive test coverage
Many of the types in this crate are implementations of specifications in the bc-research repository that is linked to this workspace. Always look for more detailed specifications in that repository.
This repository relies heavily on the public API of the dcbor crate, which is in this workspace. You can research the actual code there or examine its cargo documentation in dcbor/cargo-docs.
The bc-ur crate is also in this workspace and provides the UR serialization/deserialization functionality. You can research the actual code there.
| Type | Description |
|---|---|
Digest |
A cryptographic digest/hash of data |
ARID, UUID, XID |
Unique identifiers for various purposes |
Salt, Nonce |
Cryptographic primitives for random or pseudorandom data |
SymmetricKey, EncryptedMessage |
Types for symmetric encryption |
ECPrivateKey, ECPublicKey |
Elliptic curve cryptography keys |
Ed25519PrivateKey, Ed25519PublicKey |
Ed25519 cryptographic keys |
X25519PrivateKey, X25519PublicKey |
X25519 key agreement keys |
SigningPrivateKey, SigningPublicKey |
Keys for digital signatures |
MLDSAPrivateKey, MLDSAPublicKey |
Post-quantum digital signature keys |
MLKEMPrivateKey, MLKEMPublicKey |
Post-quantum key encapsulation keys |
These documents are essential for understanding the cryptographic primitives and standards implemented in this crate.
🚨 NOTE: Always refer to the relevant standards and specifications when documenting cryptographic primitives.
| Title | Description |
|---|---|
| CBOR RFC 8949 | The CBOR data format specification |
| UR (Uniform Resources) | Blockchain Commons specification for encoding structured binary data |
| SSKR (Sharded Secret Key Reconstruction) | Specification for secret sharing |
| Post-quantum cryptography | NIST standards for quantum-resistant cryptography |
- Goal: Enhance the documentation for the
bc-componentscrate to improve usability and understanding. Document all public API elements, including structs, enums, and functions.
- Comprehensive: All public API elements have documentation
- Contextual: Documentation explains both "what" and "why"
- Practical: Examples demonstrate real-world usage
- Consistent: Uniform style and detail level across the codebase
- Accessible: Explanations suitable for developers not familiar with Rust, and Rust engineers not familiar with cryptography
- Searchable: Proper cross-references and keyword usage
- Validated: Examples compile and work correctly
- Doc Example Best Practices:
- Handle errors properly in examples that return
Result - Use
no_runfor examples that can't be directly compiled/run in doc tests - Check constructors for type initialization in examples - some types may lack
Defaultimplementation - For internal/implementation types that users shouldn't directly interact with, clearly mark them as such in the documentation
- Show typical usage patterns for each type, not all possible ways to use it
- For cryptographic operations, include examples of key generation, signing/verification, encryption/decryption as appropriate
- Refer to unit tests for examples of how to use the types
- When demonstrating UR encoding/decoding, demonstrate using string representations
- 🚨 CRITICAL: ALL trait implementations (
impl Trait for Type) MUST have a single-line doc comment explaining the implementation's purpose - this includes common traits likeDefault,From,TryFrom, and any custom traits
- Handle errors properly in examples that return
🚨 CRITICAL: Always perform these quality checks with EVERY documentation task BEFORE considering it complete:
-
Fix all doc tests:
# Run from the bc-components directory, not the workspace root cd /path/to/bc-components && cargo test --doc
Ensure all doc tests pass, and fix any failures immediately.
-
Fix all Clippy lints:
# Run from the bc-components directory, not the workspace root cd /path/to/bc-components && cargo clippy -- -D warnings
Address any Clippy warnings introduced by documentation changes.
🔴 MANDATORY: YOU MUST RUN THESE CHECKS YOURSELF after making changes, without waiting to be prompted. Documentation is not complete until all tests pass. NEVER mark a task as complete without running and passing these checks.
This section inventories all public API items that need documentation, ordered from simplest with least dependencies to most complex.
- ✅
Digest(digest.rs) - A cryptographic digest/hash of data - ✅
DigestProvider(digest_provider.rs) - An interface for providing digests - ✅
Nonce(nonce.rs) - A nonce (number used once) for cryptographic operations - ✅
Salt(salt.rs) - A salt for cryptographic operations - ✅
Seed(seed.rs) - A seed for deterministic key generation
- ✅
ARID(id/arid.rs) - An Apparently Random Identifier - ✅
UUID(id/uuid.rs) - A Universally Unique Identifier - ✅
XID(id/xid.rs) - An eXtensible Identifier - ✅
URI(id/uri.rs) - A Uniform Resource Identifier - ✅
XIDProvider(id/mod.rs) - Interface for providing XIDs
- ✅
SymmetricKey(symmetric/symmetric_key.rs) - A key for symmetric encryption - ✅
AuthenticationTag(symmetric/authentication_tag.rs) - Authentication tag for authenticated encryption - ✅
EncryptedMessage(symmetric/encrypted_message.rs) - A symmetrically-encrypted message
- ✅
Ed25519PrivateKey, ✅Ed25519PublicKey(ed25519/ed25519_private_key.rs,ed25519/ed25519_public_key.rs) - Ed25519 keys - ✅
X25519PrivateKey, ✅X25519PublicKey(x25519/x25519_private_key.rs,x25519/x25519_public_key.rs) - X25519 keys
- ✅ All the types in the
ec_keymodule (ec_key/) - ECDSA keys and signatures
- ✅
MLDSALevel, ✅MLDSAPrivateKey, ✅MLDSAPublicKey, ✅MLDSASignature(mldsa/) - ML-DSA post-quantum signatures - ✅
MLKEMLevel, ✅MLKEMPrivateKey, ✅MLKEMPublicKey, ✅MLKEMCiphertext(mlkem/) - ML-KEM post-quantum key encapsulation
- ✅
SigningPrivateKey, ✅SigningPublicKey(signing/signing_private_key.rs,signing/signing_public_key.rs) - Keys for digital signatures - ✅
Signature(signing/signature.rs) - A digital signature - ✅
Signer, ✅Verifier(signing/signer.rs) - Interfaces for signing and verification - ✅
SignatureScheme(signing/signature_scheme.rs) - Enumeration of signature schemes
- ✅
EncapsulationPrivateKey, ✅EncapsulationPublicKey(encapsulation/encapsulation_private_key.rs,encapsulation/encapsulation_public_key.rs) - KEM keys - ✅
EncapsulationCiphertext(encapsulation/encapsulation_ciphertext.rs) - Ciphertext produced by KEM - ✅
SealedMessage(encapsulation/sealed_message.rs) - A sealed message using KEM - ✅
EncapsulationScheme(encapsulation/encapsulation_scheme.rs) - Enum of encapsulation schemes - ✅
Encrypter, ✅Decrypter(encrypter.rs) - Interfaces for public key encryption/decryption
- ✅
SSKRGroupSpec, ✅SSKRSpec, ✅SSKRSecret, ✅SSKRShare(sskr_mod.rs) - SSKR secret sharing
- ✅
HKDFRng(hkdf_rng.rs) - Random number generator based on HKDF - ✅
keypair,keypair_using(keypair.rs) - Functions for generating keypairs
- ✅
Compressed(compressed.rs) - A compressed binary blob
The following insights about the API design of this crate have been learned during documentation:
-
Cryptographic Flexibility: The crate provides a uniform interface to different cryptographic schemes, allowing flexibility and future-proofing as cryptographic standards evolve.
-
Post-Quantum Support: The inclusion of ML-DSA and ML-KEM types demonstrates a forward-looking approach to cryptography in a post-quantum computing world.
-
UR Serialization: Many types support serialization to Uniform Resources (URs), enabling interoperability with other Blockchain Commons tools and libraries.
-
CBOR Encoding: Deterministic CBOR encoding ensures consistent serialization across platforms.
-
Multiplatform Support: The crate is designed to be used across different platforms and environments.
-
Error Handling: Comprehensive error handling for cryptographic operations ensures robust behavior.
-
Testability: Extensive test coverage, particularly important for cryptographic code.
-
SSH Key Support: Integration with SSH key formats for practical application interoperability.
These are the key insights gained during the documentation process:
-
Trait Implementation Documentation: All trait implementations benefit from having a single-line doc comment that explains the purpose of the implementation. This significantly improves code readability and maintainability, especially for codebases with many trait implementations.
-
CBOR and UR Patterns: There is a consistent pattern across types for CBOR serialization and UR encoding. Most types in the crate follow the pattern:
- Implement
CBORTaggedto define tags - Implement
CBORTaggedEncodablefor serialization - Implement
CBORTaggedDecodablefor deserialization - Implement
From<Type> for CBORandTryFrom<CBOR> for Typefor conversions
- Implement
-
Type Conversion Patterns: Many types implement similar conversion patterns:
From<&Type> for Typefor cloning from referencesFrom<Type> for Vec<u8>andFrom<&Type> for Vec<u8>for byte conversionsAsRef<[u8]>for providing byte slice referencesAsRef<Self>for self-references
-
Doc Testing Requirements: Doctest examples need to be carefully constructed to compile and run successfully. This includes properly importing types, specifying correct type hints, and ensuring examples are self-contained.
-
Cryptographic Primitive Relationships: Core cryptographic primitives have clear roles with distinct purposes:
Digest: Immutable cryptographic hash used for verification and identificationNonce: Fixed-size random number used once to prevent replay attacksSalt: Variable-length random data used to decorrelate informationSeed: Source of entropy for deterministic key generation with metadata
- None planned at this time.