Skip to content

Cryptography

Introduction

The Polkadot SDK uses cryptography to sign and verify transactions, to identify block authors and other session key holders, and to hash blocks and storage keys. The SDK is generic over the signature scheme: any scheme that implements the Pair trait from sp_core can be used.

This page describes the signature schemes and key types available in sp_core, how a runtime chooses which signatures it accepts, the hash functions used by the SDK, and how to work with keys from the command line with subkey. For background on the cryptography itself and why these schemes were chosen, see the Cryptography page on the Polkadot Wiki.

Signature Schemes

sp_core provides the following signature schemes. Each module defines a Pair, a Public key, and a Signature type:

  • sp_core::sr25519: Schnorr signatures on the Ristretto group (Schnorrkel), using the same underlying curve as Ed25519. It supports both hard and soft key derivation. The parachain template's Aura keys use sr25519 (sp_consensus_aura::sr25519::AuthorityId).
  • sp_core::ed25519: EdDSA signatures on Curve25519. It supports hard derivation only. GRANDPA keys use ed25519.
  • sp_core::ecdsa: ECDSA signatures on the secp256k1 curve, the same curve used by Bitcoin and Ethereum. It supports hard derivation only.

All three implement Pair, which provides methods such as generate, from_phrase, from_seed, and from_string to create a key pair, public to get the public key, sign to sign a message, and verify to check a signature. For example:

use sp_core::{sr25519, Pair};

// Create a key pair from a secret URI with a derivation path.
let pair = sr25519::Pair::from_string("//Alice", None).expect("valid secret URI");
let message = b"hello";
let signature = pair.sign(message);
assert!(sr25519::Pair::verify(&signature, message, &pair.public()));

sp_core also contains experimental bls and bandersnatch schemes, enabled with the bls-experimental and bandersnatch-experimental features.

Signatures in the Runtime

A runtime chooses which signatures it accepts on transactions through its Signature type, which must implement sp_runtime::traits::Verify. The account ID type is then derived from the signer. The parachain template uses MultiSignature, which accepts signatures from any of the three schemes:

pub type Signature = MultiSignature;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

MultiSignature has Ed25519, Sr25519, Ecdsa, and Eth variants, and the matching MultiSigner maps each public key to a 32-byte account ID. For sr25519 and ed25519, the account ID is the public key. For ECDSA, it is the Blake2 hash of the compressed public key, and the Eth variant derives the account ID from the Ethereum address instead. See Accounts for how account IDs are encoded as SS58 addresses.

Key Types

Nodes keep keys in a keystore, and each key is tagged with a four-byte KeyTypeId that says what the key is used for. The built-in identifiers in sp_core::crypto::key_types include the following:

Constant Identifier Used for
AURA aura Aura block authoring
BABE babe BABE block authoring
GRANDPA gran GRANDPA finality voting
BEEFY beef BEEFY
IM_ONLINE imon ImOnline heartbeats
AUTHORITY_DISCOVERY audi Authority discovery
ACCOUNT acco Controlling a runtime account
STAKING stak Staking
STATEMENT stmt Signing statements
DUMMY dumy Tests

A runtime declares which keys its session consists of with the impl_opaque_keys! macro. The parachain template has a single Aura key:

impl_opaque_keys! {
    pub struct SessionKeys {
        pub aura: Aura,
    }
}

Pallets that define their own keys use sp_application_crypto to wrap one of the sp_core schemes in an application-specific type bound to a KeyTypeId, so that keys for different purposes cannot be confused. For how collators generate and register their session keys, see Run a Collator.

Hash Functions

The SDK uses the Blake2 family as its default hash. The parachain template uses BlakeTwo256 (Blake2b with a 256-bit output) as the block and header hashing algorithm, and the SS58 address checksum is also derived with Blake2b.

The sp_crypto_hashing crate provides the hash functions used across the SDK, including blake2_128, blake2_256, twox_64, twox_128, twox_256, sha2_256, and keccak_256.

FRAME storage maps choose a hasher for their keys, such as Blake2_128Concat or Twox64Concat. The FRAME documentation classifies Blake2 hashers as cryptographically secure and Twox hashers as faster but not cryptographically secure. Use a cryptographically secure hasher when end users control the map keys. When they do not, or when the keys are cryptographically secure by definition (for example, an AccountId), a Twox hasher is sufficient.

Work with Keys Using Subkey

Subkey is the Polkadot SDK command-line tool for generating and inspecting keys. Most commands accept a --scheme option (sr25519, ed25519, or ecdsa; the default is sr25519). Run subkey --help for the full list of commands and options.

  • Generate a new key pair:

    subkey generate --scheme sr25519
    
  • Inspect a secret phrase, seed, or SS58 address to see the public key and account ID:

    subkey inspect "INSERT_SECRET_PHRASE"
    
  • Sign a message with a secret URI:

    echo -n "INSERT_MESSAGE" | subkey sign --suri "INSERT_SECRET_URI"
    
  • Verify a signature against an address:

    echo -n "INSERT_MESSAGE" | subkey verify INSERT_SIGNATURE INSERT_ADDRESS
    

See Validating Addresses for example subkey inspect output.

Last update: November 17, 2026
| Created: November 17, 2026