0
# Key Pair Generation
1
2
Key pair generation and management for sr25519, ed25519, and secp256k1 cryptographic schemes. Each scheme offers different properties and use cases within the Polkadot ecosystem.
3
4
## Capabilities
5
6
### Sr25519 Key Pairs
7
8
Sr25519 is the primary signature scheme used in Polkadot for account keys and validator signing.
9
10
```typescript { .api }
11
/**
12
* Generate sr25519 key pair from seed
13
* @param seed - 32-byte seed for key generation
14
* @returns Keypair with public and secret keys
15
*/
16
function sr25519PairFromSeed(seed: Uint8Array): Keypair;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { sr25519PairFromSeed, mnemonicToMiniSecret, mnemonicGenerate } from "@polkadot/util-crypto";
23
24
// From mnemonic
25
const mnemonic = mnemonicGenerate();
26
const seed = mnemonicToMiniSecret(mnemonic);
27
const pair = sr25519PairFromSeed(seed);
28
29
// From custom seed
30
const customSeed = new Uint8Array(32);
31
crypto.getRandomValues(customSeed);
32
const customPair = sr25519PairFromSeed(customSeed);
33
34
console.log(pair.publicKey.length); // 32
35
console.log(pair.secretKey.length); // 64
36
```
37
38
### Ed25519 Key Pairs
39
40
Ed25519 provides fast signing and verification with smaller signatures.
41
42
```typescript { .api }
43
/**
44
* Generate ed25519 key pair from seed
45
* @param seed - 32-byte seed for key generation
46
* @returns Keypair with public and secret keys
47
*/
48
function ed25519PairFromSeed(seed: Uint8Array): Keypair;
49
50
/**
51
* Generate ed25519 key pair from secret key
52
* @param secretKey - 32-byte secret key
53
* @returns Keypair with public and secret keys
54
*/
55
function ed25519PairFromSecret(secretKey: Uint8Array): Keypair;
56
57
/**
58
* Generate random ed25519 key pair
59
* @returns Keypair with random keys
60
*/
61
function ed25519PairFromRandom(): Keypair;
62
63
/**
64
* Generate ed25519 key pair from string with optional passphrase
65
* @param value - String value to derive from
66
* @param passphrase - Optional passphrase
67
* @returns Keypair derived from string
68
*/
69
function ed25519PairFromString(value: string, passphrase?: string): Keypair;
70
```
71
72
**Usage Example:**
73
74
```typescript
75
import {
76
ed25519PairFromSeed,
77
ed25519PairFromRandom,
78
ed25519PairFromString
79
} from "@polkadot/util-crypto";
80
81
// From seed
82
const seed = new Uint8Array(32);
83
const pair = ed25519PairFromSeed(seed);
84
85
// Random generation
86
const randomPair = ed25519PairFromRandom();
87
88
// From string
89
const stringPair = ed25519PairFromString("Alice", "optional-passphrase");
90
```
91
92
### Secp256k1 Key Pairs
93
94
Secp256k1 is used for Ethereum compatibility and Bitcoin-style signatures.
95
96
```typescript { .api }
97
/**
98
* Generate secp256k1 key pair from seed
99
* @param seed - 32-byte seed for key generation
100
* @returns Keypair with public and secret keys
101
*/
102
function secp256k1PairFromSeed(seed: Uint8Array): Keypair;
103
```
104
105
**Usage Example:**
106
107
```typescript
108
import { secp256k1PairFromSeed } from "@polkadot/util-crypto";
109
110
const seed = new Uint8Array(32);
111
const pair = secp256k1PairFromSeed(seed);
112
113
console.log(pair.publicKey.length); // 33 (compressed) or 65 (uncompressed)
114
console.log(pair.secretKey.length); // 32
115
```
116
117
## Key Pair Properties
118
119
### Sr25519 Properties
120
121
- **Public Key**: 32 bytes
122
- **Secret Key**: 64 bytes (32-byte seed + 32-byte nonce)
123
- **Features**: Supports soft derivation, VRF capabilities
124
- **Use Cases**: Account keys, validator signing, session keys
125
126
### Ed25519 Properties
127
128
- **Public Key**: 32 bytes
129
- **Secret Key**: 32 bytes (seed) or 64 bytes (expanded)
130
- **Features**: Fast verification, deterministic signatures
131
- **Use Cases**: High-performance signing, identity verification
132
133
### Secp256k1 Properties
134
135
- **Public Key**: 33 bytes (compressed) or 65 bytes (uncompressed)
136
- **Secret Key**: 32 bytes
137
- **Features**: Ethereum compatibility, ECDH support
138
- **Use Cases**: Ethereum integration, cross-chain compatibility
139
140
## Security Considerations
141
142
- Always use cryptographically secure random seeds
143
- Store secret keys securely and never expose them
144
- Use proper key derivation for hierarchical key management
145
- Validate public keys before use in cryptographic operations
146
- Consider the trade-offs between different signature schemes
147
148
## Performance Notes
149
150
- Sr25519: Moderate signing speed, fast verification
151
- Ed25519: Very fast signing and verification
152
- Secp256k1: Slower operations but broad compatibility
153
- All schemes support batch verification for multiple signatures