| 0.2.7 |
|
|---|---|
| 0.2.6 |
|
| 0.2.2 |
|
| 0.1.7 |
|
| 0.1.0 |
|
#4 in #silent-debug
120 downloads per month
Used in 105 crates
(10 directly)
29KB
581 lines
Derive macros for crypto operations
This crate contains four types of derive macros:
- the
SilentDebugand SilentDisplay macros are meant to be used on private key types, and elide their input for confidentiality. - the
Derefmacro helps derive the canonical instances on new types. - the derive macros for
aptos_crypto::traits, namelyValidCryptoMaterial,PublicKey,PrivateKey,VerifyingKey,SigningKeyandSignatureare meant to be derived on simple unions of types implementing these traits. - the derive macro for
aptos_crypto::hash::CryptoHasher, which defines the domain-separation hasher structures described inaptos_crypto::hash(look there for details). This derive macro has for sole difference that it automatically picks a unique salt for you, using the Serde name. For a containerFoo, this is usually equivalent to:define_hasher! { ( FooHasher, FOO_HASHER, b"Foo" ) }
Unions of Signing Traits, in detail
Those types typically come into play when you need to accept several alternatives at runtime for several signature and verification schemes (ex: BLS or EdDSA, see below). In this case, it is possible to declare a triplet of enum types that each describe a 'sum type' (coproduct) of these alternatives. This happens to be a signing scheme itself (it has canonical signature, signing & verifying key types, and verifies all expected properties by trivial dispatch).
The macros below let you define this type of union trivially under two conditions:
- that the variant tags for the enum have the same name, i.e. if the BLS variant for the
SignatureUnionisSignatureUnion::BLS(BLS12381Signature), then the variant of thePublicKeyUnionfor BLS must also bePublicKeyUnion::BLS, - that you specify the associated types
PrivateKeyType,SignatureTypeandPublicKeyTypefor each of the three unions.PrivateKeyTypeprovides the value for theVerifyingKeyMaterialandPublicKeyMaterialassociated types,PublicKeyTypeprovides the valid for theSigningKeyMaterialandPrivateKeyMaterialassociated types andSignatureTypeprovides the value for theSignatureMaterialassociated type.
Example
# #[macro_use] extern crate crypto-derive;
use aptos_crypto::{
hash::HashValue,
bls12381::{BLS12381PrivateKey, BLS12381PublicKey, BLS12381Signature},
ed25519::{Ed25519PrivateKey, Ed25519PublicKey, Ed25519Signature},
};
use aptos_crypto_derive::{
SilentDebug, PrivateKey, PublicKey, Signature, SigningKey, ValidCryptoMaterial, VerifyingKey,
};
/// Generic public key enum
#[derive(
Debug, Clone, PartialEq, Eq, Hash, ValidCryptoMaterial, PublicKey, VerifyingKey,
)]
#[PrivateKeyType = "GenericPrivateKey"]
#[SignatureType = "GenericSignature"]
pub enum GenericPublicKey {
/// Ed25519 public key
Ed(Ed25519PublicKey),
/// BLS12-381 public key
BLS(BLS12381PublicKey),
}
/// Generic private key enum
#[derive(SilentDebug, ValidCryptoMaterial, PrivateKey, SigningKey)]
#[PublicKeyType = "GenericPublicKey"]
#[SignatureType = "GenericSignature"]
pub enum GenericPrivateKey {
/// Ed25519 private key
Ed(Ed25519PrivateKey),
/// BLS12-381 private key
BLS(BLS12381PrivateKey),
}
/// Generic signature enum
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Signature)]
#[PrivateKeyType = "GenericPrivateKey"]
#[PublicKeyType = "GenericPublicKey"]
pub enum GenericSignature {
/// Ed25519 signature
Ed(Ed25519Signature),
/// BLS12-381 signature
BLS(BLS12381Signature),
}
Dependencies
~1.5MB
~39K SLoC