UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
bip32.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_BIP32_HPP
2#define SECP256K1_BIP32_HPP
3#pragma once
4
5// ============================================================================
6// BIP-32: Hierarchical Deterministic Key Derivation for secp256k1
7// ============================================================================
8// Implements BIP-32 (HD wallets):
9// - Extended key (xprv / xpub) derivation
10// - Normal child derivation (public derivable)
11// - Hardened child derivation (private only)
12// - Path parsing ("m/44'/0'/0'/0/0")
13//
14// Reference: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
15// ============================================================================
16
17#include <array>
18#include <cstdint>
19#include <string>
20#include <vector>
21#include "secp256k1/scalar.hpp"
22#include "secp256k1/point.hpp"
23
24namespace secp256k1 {
25
26// -- Extended Key -------------------------------------------------------------
27
28// BIP-32 extended key (private or public)
30 std::array<std::uint8_t, 32> key; // Private key or compressed X
31 std::array<std::uint8_t, 32> chain_code; // Chain code for derivation
32 std::uint8_t depth; // 0 for master, increments per level
33 std::uint32_t child_number; // Which child this is
34 std::array<std::uint8_t, 4> parent_fingerprint; // First 4 bytes of HASH160(parent pubkey)
35 bool is_private; // true = xprv, false = xpub
36 std::uint8_t pub_prefix = 0; // 0x02 or 0x03 when is_private==false
37
38 // Derive a child key at index.
39 // Hardened key: index >= 0x80000000 (or use derive_hardened())
40 // Normal: index < 0x80000000 (or use derive_normal())
41 //
42 // Returns {valid_child, success}
43 std::pair<ExtendedKey, bool> derive_child(std::uint32_t index) const;
44
45 // Convenience wrappers
46 std::pair<ExtendedKey, bool> derive_normal(std::uint32_t index) const {
47 return derive_child(index);
48 }
49 std::pair<ExtendedKey, bool> derive_hardened(std::uint32_t index) const {
50 return derive_child(index | 0x80000000u);
51 }
52
53 // Get the public key point from this extended key
55
56 // Get the private key scalar (only valid if is_private)
58
59 // Convert to the public extended key (strips private key)
61
62 // Serialize to 78 bytes (BIP-32 standard)
63 std::array<std::uint8_t, 78> serialize() const;
64
65 // Key fingerprint: first 4 bytes of HASH160(compressed pubkey)
66 std::array<std::uint8_t, 4> fingerprint() const;
67};
68
69// -- Master Key Generation ----------------------------------------------------
70
71// Generate master key from seed bytes (BIP-32).
72// Seed should be 16-64 bytes (BIP-39 uses 64).
73// Returns {ExtendedKey, success}
74std::pair<ExtendedKey, bool> bip32_master_key(const std::uint8_t* seed,
75 std::size_t seed_len);
76
77// -- Path Derivation ----------------------------------------------------------
78
79// Derive key from path string.
80// Path format: "m/44'/0'/0'/0/0" (apostrophe = hardened)
81// Returns {ExtendedKey, success}
82std::pair<ExtendedKey, bool> bip32_derive_path(const ExtendedKey& master,
83 const std::string& path);
84
85// -- HMAC-SHA512 (needed for BIP-32) ------------------------------------------
86// Exposed for testing. Computes HMAC-SHA512(key, data).
87
88std::array<std::uint8_t, 64> hmac_sha512(const std::uint8_t* key, std::size_t key_len,
89 const std::uint8_t* data, std::size_t data_len);
90
91} // namespace secp256k1
92
93#endif // SECP256K1_BIP32_HPP
std::pair< ExtendedKey, bool > bip32_master_key(const std::uint8_t *seed, std::size_t seed_len)
std::array< std::uint8_t, 64 > hmac_sha512(const std::uint8_t *key, std::size_t key_len, const std::uint8_t *data, std::size_t data_len)
std::pair< ExtendedKey, bool > bip32_derive_path(const ExtendedKey &master, const std::string &path)
fast::Scalar private_key() const
fast::Point public_key() const
std::array< std::uint8_t, 78 > serialize() const
std::uint8_t depth
Definition bip32.hpp:32
std::array< std::uint8_t, 32 > key
Definition bip32.hpp:30
ExtendedKey to_public() const
std::array< std::uint8_t, 4 > fingerprint() const
std::pair< ExtendedKey, bool > derive_child(std::uint32_t index) const
std::uint32_t child_number
Definition bip32.hpp:33
std::pair< ExtendedKey, bool > derive_hardened(std::uint32_t index) const
Definition bip32.hpp:49
std::array< std::uint8_t, 4 > parent_fingerprint
Definition bip32.hpp:34
std::pair< ExtendedKey, bool > derive_normal(std::uint32_t index) const
Definition bip32.hpp:46
std::array< std::uint8_t, 32 > chain_code
Definition bip32.hpp:31
std::uint8_t pub_prefix
Definition bip32.hpp:36