Skip to main content

Module crypto

Module crypto 

Source
Expand description

Cryptographic primitives for the Aptos SDK.

This module provides implementations of the signature schemes supported by Aptos, including Ed25519, Secp256k1, and Secp256r1 (P-256).

§Feature Flags

  • ed25519 (default): Ed25519 signatures
  • secp256k1 (default): Secp256k1 ECDSA signatures
  • secp256r1: Secp256r1 (P-256) ECDSA signatures
  • bls: BLS12-381 signatures

§Security Considerations

§Timing Attacks

The PartialEq implementations for cryptographic types use standard byte comparisons which may not be constant-time. This is generally acceptable because:

  • Public keys and signatures are not secret material
  • Signature verification in the underlying libraries uses constant-time operations for the actual cryptographic comparisons

If you need constant-time comparisons for specific use cases (e.g., comparing against expected signatures in tests), consider using the subtle crate’s ConstantTimeEq trait.

§Key Material Protection

Private key types implement Zeroize and ZeroizeOnDrop to clear sensitive key material from memory when dropped. The underlying cryptographic libraries (ed25519-dalek, k256, p256) also implement secure key handling.

§Example

use aptos_sdk::crypto::{Ed25519PrivateKey, Signer};

let private_key = Ed25519PrivateKey::generate();
let message = b"hello world";
let signature = private_key.sign(message);

let public_key = private_key.public_key();
assert!(public_key.verify(message, &signature).is_ok());

Structs§

AnyPublicKey
A public key that can be any supported signature scheme.
AnySignature
A signature that can be any supported signature scheme.
Bls12381PrivateKeybls
A BLS12-381 private key.
Bls12381ProofOfPossessionbls
A BLS12-381 proof of possession.
Bls12381PublicKeybls
A BLS12-381 public key.
Bls12381Signaturebls
A BLS12-381 signature.
Ed25519PrivateKeyed25519
An Ed25519 private key.
Ed25519PublicKeyed25519
An Ed25519 public key.
Ed25519Signatureed25519
An Ed25519 signature.
MultiEd25519PublicKeyed25519
A multi-Ed25519 public key.
MultiEd25519Signatureed25519
A multi-Ed25519 signature.
MultiKeyPublicKey
A multi-key public key supporting mixed signature schemes.
MultiKeySignature
A multi-key signature containing signatures from multiple signers.
Secp256k1PrivateKeysecp256k1
A Secp256k1 ECDSA private key.
Secp256k1PublicKeysecp256k1
A Secp256k1 ECDSA public key.
Secp256k1Signaturesecp256k1
A Secp256k1 ECDSA signature.
Secp256r1PrivateKeysecp256r1
A Secp256r1 (P-256) ECDSA private key.
Secp256r1PublicKeysecp256r1
A Secp256r1 (P-256) ECDSA public key.
Secp256r1Signaturesecp256r1
A Secp256r1 (P-256) ECDSA signature.

Enums§

AnyPublicKeyVariant
Supported signature schemes for multi-key.
HashFunction
Available hash functions.

Constants§

BLS12381_POP_LENGTHbls
BLS12-381 proof of possession length in bytes.
BLS12381_PRIVATE_KEY_LENGTHbls
BLS12-381 private key length in bytes.
BLS12381_PUBLIC_KEY_LENGTHbls
BLS12-381 public key length in bytes (compressed).
BLS12381_SIGNATURE_LENGTHbls
BLS12-381 signature length in bytes (compressed).
ED25519_PRIVATE_KEY_LENGTHed25519
Ed25519 private key length in bytes.
ED25519_PUBLIC_KEY_LENGTHed25519
Ed25519 public key length in bytes.
ED25519_SCHEME
The authentication key scheme byte for Ed25519 single-key accounts.
ED25519_SIGNATURE_LENGTHed25519
Ed25519 signature length in bytes.
KEYLESS_SCHEME
The authentication key scheme byte for keyless accounts.
MAX_NUM_OF_KEYSed25519
Maximum number of keys in a multi-Ed25519 account.
MIN_THRESHOLDed25519
Minimum threshold (at least 1 signature required).
MULTI_ED25519_SCHEME
The authentication key scheme byte for multi-Ed25519 accounts.
MULTI_KEY_MAX_NUM_OF_KEYS
Maximum number of keys in a multi-key account.
MULTI_KEY_MIN_THRESHOLD
Minimum threshold (at least 1 signature required).
MULTI_KEY_SCHEME
The authentication key scheme byte for multi-key accounts (unified).
SECP256K1_PRIVATE_KEY_LENGTHsecp256k1
Secp256k1 private key length in bytes.
SECP256K1_PUBLIC_KEY_LENGTHsecp256k1
Secp256k1 public key length in bytes (compressed).
SECP256K1_PUBLIC_KEY_UNCOMPRESSED_LENGTHsecp256k1
Secp256k1 uncompressed public key length in bytes.
SECP256K1_SIGNATURE_LENGTHsecp256k1
Secp256k1 signature length in bytes (DER encoded max).
SECP256R1_PRIVATE_KEY_LENGTHsecp256r1
Secp256r1 private key length in bytes.
SECP256R1_PUBLIC_KEY_LENGTHsecp256r1
Secp256r1 public key length in bytes (compressed).
SECP256R1_SIGNATURE_LENGTHsecp256r1
Secp256r1 signature length in bytes.
SINGLE_KEY_SCHEME
The authentication key scheme byte for single-key accounts (unified).

Traits§

PublicKey
A trait for public key types.
Signature
A trait for signature types.
Signer
A trait for types that can sign messages.
Verifier
A trait for types that can verify signatures.

Functions§

derive_address
Derives an account address from a public key and scheme.
derive_authentication_key
Derives an authentication key from a public key and scheme.
sha2_256
Computes the SHA2-256 hash of the input.
sha3_256
Computes the SHA3-256 hash of the input.
sha3_256_of
Computes the SHA3-256 hash of multiple byte slices.
signing_message
Computes a domain-separated hash for transaction signing.