Macro define_ed25519_keypair

Source
macro_rules! define_ed25519_keypair {
    ($(#[ $docs_and_attrs:meta ])*
     $vis:vis $base_name:ident) => { ... };
}
Expand description

Create an ed25519 keypair wrapper given a visibility and a struct name.

§Syntax:

define_ed25519_keypair(<visibility> <prefix>)

This macro creates a struct tuple named <prefix>Keypair which contains the lower-level cryptographic keypair for an ed25519 keypair. It derives the deftly Ed25519Keypair template which in turn creates <prefix>PublicKey along a series of useful methods.

The keypair is NOT clonable by design in order to avoid duplicating secret key material.

§Example:

use tor_key_forge::define_ed25519_keypair;

define_ed25519_keypair!(NonPublicSigning);
define_ed25519_keypair!(pub PublicSigning);
define_ed25519_keypair!(pub(crate) CratePublicSigning);

The above results in NonPublicSigningKeypair and NonPublicSigningPublicKey struct being created and usable with a series of useful methods. Same for the other defines.

You can then use these objects like so:

use rand::Rng;
use tor_key_forge::Keygen;
use tor_key_forge::define_ed25519_keypair;
use tor_llcrypto::pk::ValidatableSignature;
use tor_llcrypto::pk::ed25519::Ed25519SigningKey;

define_ed25519_keypair!(
    /// Our signing key.
    MySigning
);

let signing_kp = MySigningKeypair::generate(&mut rng).expect("Invalid keygen");
let signing_pubkey = signing_kp.public();
// Lets sign this wonderful message.
let message = "Workers want rights, not your opinion".as_bytes();
let sig = signing_kp.sign(&message);

// You can then verify either directly with the keypair or the public key.
assert!(signing_kp.verify(sig, &message));
assert!(signing_pubkey.verify(sig, &message));