| 0.3.2 |  | 
|---|---|
| 0.3.1 |  | 
| 0.2.1 |  | 
| 0.1.6 |  | 
| 0.1.4 |  | 
#57 in #diem
在 3 个 crate 中使用(通过 diem-framework-crypto)
29KB
581 行
密码操作派生宏
此 crate 包含四种类型的派生宏
- SilentDebug和- SilentDisplay宏旨在用于私钥类型,并省略其输入以提高保密性。
- Deref宏有助于派生新类型上的规范实例。
- 针对 diem_crypto::traits的派生宏,即ValidCryptoMaterial、PublicKey、PrivateKey、VerifyingKey、SigningKey和Signature,旨在在实现这些特质的简单类型联合上派生。
- 针对 diem_crypto::hash::CryptoHasher的派生宏,它定义了在diem_crypto::hash中描述的域分离哈希器结构(有关详细信息,请参阅那里)。此派生宏的唯一不同之处在于,它自动为您选择一个唯一的盐,使用 Serde 名称。对于容器Foo,这通常相当于define_hasher! { ( FooHasher, FOO_HASHER, b"Foo" ) }
签名特质的联合,详细说明
这些类型通常在您需要在运行时接受几种签名和验证方案时发挥作用(例如:BLS 或 EdDSA,请参阅下面)。在这种情况下,可以声明一个三联枚举类型,每个类型都描述这些替代方案的“求和类型”(积产品)。这实际上是一个签名方案本身(它具有规范签名、签名和验证密钥类型,并通过平凡调度验证所有预期的属性)。
以下宏在以下两个条件下允许您以平凡的方式定义此类联合
- 枚举的变体标签具有相同的名称,即如果BLS变体为SignatureUnion是SignatureUnion::BLS(BLS12381Signature),则BLS对应的PublicKeyUnion的变体也必须是PublicKeyUnion::BLS。
- 您需要为三个联合体分别指定关联类型PrivateKeyType、SignatureType和PublicKeyType。其中PrivateKeyType为VerifyingKeyMaterial和PublicKeyMaterial关联类型提供值,PublicKeyType为SigningKeyMaterial和PrivateKeyMaterial关联类型提供值,而SignatureType为SignatureMaterial关联类型提供值。
示例
# #[macro_use] extern crate crypto-derive;
use diem_crypto::{
    hash::HashValue,
    bls12381::{BLS12381PrivateKey, BLS12381PublicKey, BLS12381Signature},
    ed25519::{Ed25519PrivateKey, Ed25519PublicKey, Ed25519Signature},
};
use diem_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),
}
依赖项
~1.5MB
~35K SLoC