UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
bip39.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_BIP39_HPP
2#define SECP256K1_BIP39_HPP
3#pragma once
4
5// ============================================================================
6// BIP-39: Mnemonic Code for Generating Deterministic Keys
7// ============================================================================
8// Implements BIP-39 mnemonic seed phrase generation and validation:
9// - Entropy (128-256 bits) -> mnemonic word sequence (12-24 words)
10// - Mnemonic + passphrase -> 512-bit seed (PBKDF2-HMAC-SHA512, 2048 rounds)
11// - Mnemonic validation (checksum, word lookup)
12//
13// The seed output is compatible with BIP-32 master key derivation:
14// auto [mnemonic, ok1] = bip39_generate(16); // 12 words
15// auto [seed, ok2] = bip39_mnemonic_to_seed(mnemonic, "");
16// auto [master, ok3] = bip32_master_key(seed.data(), 64);
17//
18// Reference: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
19// ============================================================================
20
21#include <array>
22#include <cstdint>
23#include <cstddef>
24#include <string>
25#include <utility>
26
27namespace secp256k1 {
28
29// -- Mnemonic Generation ------------------------------------------------------
30
31// Generate a BIP-39 mnemonic from entropy.
32// entropy_bytes: 16 (12 words), 20 (15), 24 (18), 28 (21), 32 (24)
33// Returns {mnemonic_string, success}
34// Uses OS CSPRNG for entropy if entropy_in == nullptr.
35std::pair<std::string, bool>
36bip39_generate(std::size_t entropy_bytes,
37 const std::uint8_t* entropy_in = nullptr);
38
39// -- Mnemonic Validation ------------------------------------------------------
40
41// Validate a BIP-39 mnemonic: word count, word membership, checksum.
42// Returns true if the mnemonic is valid.
43bool bip39_validate(const std::string& mnemonic);
44
45// -- Seed Derivation ----------------------------------------------------------
46
47// Derive 512-bit seed from mnemonic + passphrase.
48// Uses PBKDF2-HMAC-SHA512 with 2048 iterations.
49// salt = "mnemonic" + passphrase (UTF-8)
50// Returns {64-byte seed, success}
51std::pair<std::array<std::uint8_t, 64>, bool>
52bip39_mnemonic_to_seed(const std::string& mnemonic,
53 const std::string& passphrase = "");
54
55// -- Mnemonic <-> Entropy Roundtrip -------------------------------------------
56
57// Decode mnemonic back to entropy bytes.
58// Returns {entropy_bytes, entropy_length, success}
60 std::array<std::uint8_t, 32> data{}; // max 256-bit entropy
61 std::size_t length = 0; // actual byte count (16-32)
62};
63std::pair<Bip39Entropy, bool>
64bip39_mnemonic_to_entropy(const std::string& mnemonic);
65
66// -- PBKDF2-HMAC-SHA512 (exposed for testing) --------------------------------
67
68void pbkdf2_hmac_sha512(const std::uint8_t* password, std::size_t password_len,
69 const std::uint8_t* salt, std::size_t salt_len,
70 std::uint32_t iterations,
71 std::uint8_t* output, std::size_t output_len);
72
73// -- Wordlist Access ----------------------------------------------------------
74
75// Get the English BIP-39 wordlist (2048 words, sorted).
76const char* const* bip39_wordlist_english();
77
78} // namespace secp256k1
79
80#endif // SECP256K1_BIP39_HPP
std::pair< Bip39Entropy, bool > bip39_mnemonic_to_entropy(const std::string &mnemonic)
std::pair< std::string, bool > bip39_generate(std::size_t entropy_bytes, const std::uint8_t *entropy_in=nullptr)
bool bip39_validate(const std::string &mnemonic)
std::pair< std::array< std::uint8_t, 64 >, bool > bip39_mnemonic_to_seed(const std::string &mnemonic, const std::string &passphrase="")
void pbkdf2_hmac_sha512(const std::uint8_t *password, std::size_t password_len, const std::uint8_t *salt, std::size_t salt_len, std::uint32_t iterations, std::uint8_t *output, std::size_t output_len)
const char *const * bip39_wordlist_english()
std::size_t length
Definition bip39.hpp:61
std::array< std::uint8_t, 32 > data
Definition bip39.hpp:60