commonware_cryptography

Trait Scheme

source
pub trait Scheme:
    Send
    + Sync
    + Clone
    + 'static {
    // Required methods
    fn new<R: Rng + CryptoRng>(rng: &mut R) -> Self;
    fn from(private_key: PrivateKey) -> Option<Self>;
    fn from_seed(seed: u64) -> Self;
    fn private_key(&self) -> PrivateKey;
    fn public_key(&self) -> PublicKey;
    fn validate(public_key: &PublicKey) -> bool;
    fn sign(&mut self, namespace: &[u8], message: &[u8]) -> Signature;
    fn verify(
        namespace: &[u8],
        message: &[u8],
        public_key: &PublicKey,
        signature: &Signature,
    ) -> bool;
}
Expand description

Interface that commonware crates rely on for most cryptographic operations.

Required Methods§

source

fn new<R: Rng + CryptoRng>(rng: &mut R) -> Self

Returns a new instance of the scheme.

source

fn from(private_key: PrivateKey) -> Option<Self>

Returns a new instance of the scheme from a secret key.

source

fn from_seed(seed: u64) -> Self

Returns a new instance of the scheme from a provided seed.

§Warning

This function is insecure and should only be used for examples and testing.

source

fn private_key(&self) -> PrivateKey

Returns the serialized private key of the signer.

source

fn public_key(&self) -> PublicKey

Returns the serialized public key of the signer.

source

fn validate(public_key: &PublicKey) -> bool

Verify that a public key is well-formatted.

source

fn sign(&mut self, namespace: &[u8], message: &[u8]) -> Signature

Sign the given message.

The message should not be hashed prior to calling this function. If a particular scheme requires a payload to be hashed before it is signed, it will be done internally.

To protect against replay attacks, it is required to provide a namespace to prefix any message. This ensures that a signature meant for one context cannot be used unexpectedly in another (i.e. signing a message on the network layer can’t accidentally spend funds on the execution layer).

source

fn verify( namespace: &[u8], message: &[u8], public_key: &PublicKey, signature: &Signature, ) -> bool

Check that a signature is valid for the given message and public key.

The message should not be hashed prior to calling this function. If a particular scheme requires a payload to be hashed before it is signed, it will be done internally.

Because namespace is prepended to message before signing, the namespace provided here must match the namespace provided during signing.

Object Safety§

This trait is not object safe.

Implementors§