4 releases

0.0.1-sol5 May 1, 2020
0.0.1-sol4 Oct 25, 2019
0.0.0 Aug 6, 2019
0.0.0-sol9 Jul 26, 2019
0.0.0-sol15 Jul 27, 2019

#32 in #libra

Download history 59/week @ 2024-03-24 108/week @ 2024-03-31 41/week @ 2024-04-07 46/week @ 2024-04-14 58/week @ 2024-04-21 56/week @ 2024-04-28 42/week @ 2024-05-05 53/week @ 2024-05-12 49/week @ 2024-05-19 53/week @ 2024-05-26 39/week @ 2024-06-02 26/week @ 2024-06-09 49/week @ 2024-06-16 48/week @ 2024-06-23 12/week @ 2024-06-30 19/week @ 2024-07-07

130 downloads per month
Used in 23 crates (2 directly)

Apache-2.0

19KB
367 lines

This crate contains three types of derive macros:

  • the SilentDebug and SilentDisplay macros are meant to be used on private key types, and elide their input for confidentiality.
  • the Deref macro helps derive the canonical instances on new types.
  • the derive macros for solana_libra_crypto::traits, namely ValidKey, PublicKey, PrivateKey, VerifyingKey, SigningKey and Signature are meant to be derived on simple unions of types implementing these traits.

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 SignatureUnion is SignatureUnion::BLS(BLS12381Signature), then the variant of the PublicKeyUnion for BLS must also be PublicKeyUnion::BLS,
  • that you specify the associated types PrivateKeyType, SignatureType and PublicKeyType for each of the three unions. PrivateKeyType provides the value for the VerifyingKeyMaterial and PublicKeyMaterial associated types, PublicKeyType provides the valid for the SigningKeyMaterial and PrivateKeyMaterial associated types and SignatureType provides the value for the SignatureMaterial associated type.

Example

# #[macro_use] extern crate solana_libra_crypto_derive;
use solana_libra_crypto::{
    hash::HashValue,
    bls12381::{BLS12381PrivateKey, BLS12381PublicKey, BLS12381Signature},
    ed25519::{Ed25519PrivateKey, Ed25519PublicKey, Ed25519Signature},
};
use solana_libra_crypto_derive::{
    SilentDebug, PrivateKey, PublicKey, Signature, SigningKey, ValidKey, VerifyingKey,
};

# fn main() {
/// Generic public key enum
#[derive(
    Debug, Clone, PartialEq, Eq, Hash, ValidKey, 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, ValidKey, 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
~35K SLoC