<p>Here's what I did to find out:</p>
<p>BIP32 says:</p>
<blockquote>
<h2>Master key generation</h2>
<p>The total number of possible extended keypairs
is almost 2512, but the produced keys are only 256 bits long, and
offer about half of that in terms of security. Therefore, master keys
are not generated directly, but instead from a potentially short seed
value.</p>
<ul>
<li>Generate a seed byte sequence S of a chosen length (between 128 and
512 bits; 256 bits is advised) from a (P)RNG.</li>
<li><strong>Calculate I = HMAC-SHA512(Key = &quot;Bitcoin seed&quot;, Data = S)</strong></li>
<li>Split I into two 32-byte sequences, IL and IR.</li>
<li>Use parse256(IL) as master secret key, and IR as master chain code.</li>
</ul>
<p>In case parse256(IL) is 0 or parse256(IL) ≥ n, the master key is invalid.</p>
</blockquote>
<p>(my emphasis)</p>
<p>So we need to find references to HMAC-SHA512 (or variations):</p>
<pre><code>
C:&gt; findstr /S  &quot;HMAC.SHA512&quot; *.cpp *.h
bitcoin-master\src\crypto\hmac_sha512.cpp:CHMAC_SHA512::CHMAC_SHA512(const unsigned char* key, size_t keylen)
bitcoin-master\src\crypto\hmac_sha512.cpp:void CHMAC_SHA512::Finalize(unsigned char hash[OUTPUT_SIZE])
bitcoin-master\src\crypto\hmac_sha512.h:#ifndef BITCOIN_CRYPTO_HMAC_SHA512_H
bitcoin-master\src\crypto\hmac_sha512.h:#define BITCOIN_CRYPTO_HMAC_SHA512_H
bitcoin-master\src\crypto\hmac_sha512.h:class CHMAC_SHA512
bitcoin-master\src\crypto\hmac_sha512.h:    CHMAC_SHA512(const unsigned char* key, size_t keylen);
bitcoin-master\src\crypto\hmac_sha512.h:    CHMAC_SHA512&amp; Write(const unsigned char* data, size_t len)
bitcoin-master\src\crypto\hmac_sha512.h:#endif // BITCOIN_CRYPTO_HMAC_SHA512_H
bitcoin-master\src\hash.cpp:    CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&amp;header, 1).Write(data, 32).Write(num, 4).Finalize(output);
bitcoin-master\src\key.cpp:    CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(seed.data(), seed.size()).Finalize(vout.data());
bitcoin-master\src\test\crypto_tests.cpp:    TestVector(CHMAC_SHA512(key.data(), key.size()), ParseHex(hexin), ParseHex(hexout));
bitcoin-master\src\test\fuzz\crypto.cpp:    CHMAC_SHA512 hmac_sha512{data.data(), data.size()};
bitcoin-master\src\test\fuzz\crypto.cpp:                        data.resize(CHMAC_SHA512::OUTPUT_SIZE);
</code></pre>
<p>So <code>key.cpp</code> seems to be calling (rather than defining) HMAC_SHA512 ...</p>
<p>I stopped there but you could make a note of the name of the method in <code>key.cpp</code> that calls HMAC_SHA512 and repeat the search for calls to <em>that</em> method until you find code whose method names, variable names and comments suggest you have reached the right place.</p>
<p>Obviously, on Linux  instead of <code>findstr /S</code> you'll use <code>grep -r</code> .</p>
<hr />
<p>Note that using any of this code is likely to be subject to the licence terms and conditions. These are set out in the file named COPYING - it is the MIT licence.</p>

CC BY-SA 4.0
2022-03-02T21:25:57.670Z
2022-03-02T22:38:09.330Z
2022-03-02T22:38:09.330Z
RedGrittyBrick
<p>I was looking through the repository but theres just too many folders to go through</p>
<p><a href="https://github.com/bitcoin/bitcoin" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin</a></p>
<p>Wheres the code bitcoin core uses to generate private keys?</p>

CC BY-SA 4.0
2022-03-02T21:24:15.310Z
I want to replicate that in a small program Im making
sonu ishaq
<private-key><source-code>
Where's the code that bitcoin core uses to generate private keys
sonu ishaq
# Where's the code that bitcoin core uses to generate private keys

(Asked by: username_0 on 2022-03-02)

I was looking through the repository but theres just too many folders to go through

<https://github.com/bitcoin/bitcoin>

Wheres the code bitcoin core uses to generate private keys?

**Tags:** private-key, source-code

> **Comments:**
> **username_0**: I want to replicate that in a small program Im making  

---

## 1 answers

### Answer by username_1 on 2022-03-02. Score: 1

Here's what I did to find out:

BIP32 says:

> ## Master key generation
>
> The total number of possible extended keypairs
> is almost 2512, but the produced keys are only 256 bits long, and
> offer about half of that in terms of security. Therefore, master keys
> are not generated directly, but instead from a potentially short seed
> value.
>
> * Generate a seed byte sequence S of a chosen length (between 128 and
>   512 bits; 256 bits is advised) from a (P)RNG.
> * **Calculate I = HMAC-SHA512(Key = "Bitcoin seed", Data = S)**
> * Split I into two 32-byte sequences, IL and IR.
> * Use parse256(IL) as master secret key, and IR as master chain code.
>
> In case parse256(IL) is 0 or parse256(IL) ≥ n, the master key is invalid.

(my emphasis)

So we need to find references to HMAC-SHA512 (or variations):

```

C:> findstr /S  "HMAC.SHA512" *.cpp *.h
bitcoin-master\src\crypto\hmac_sha512.cpp:CHMAC_SHA512::CHMAC_SHA512(const unsigned char* key, size_t keylen)
bitcoin-master\src\crypto\hmac_sha512.cpp:void CHMAC_SHA512::Finalize(unsigned char hash[OUTPUT_SIZE])
bitcoin-master\src\crypto\hmac_sha512.h:#ifndef BITCOIN_CRYPTO_HMAC_SHA512_H
bitcoin-master\src\crypto\hmac_sha512.h:#define BITCOIN_CRYPTO_HMAC_SHA512_H
bitcoin-master\src\crypto\hmac_sha512.h:class CHMAC_SHA512
bitcoin-master\src\crypto\hmac_sha512.h:    CHMAC_SHA512(const unsigned char* key, size_t keylen);
bitcoin-master\src\crypto\hmac_sha512.h:    CHMAC_SHA512& Write(const unsigned char* data, size_t len)
bitcoin-master\src\crypto\hmac_sha512.h:#endif // BITCOIN_CRYPTO_HMAC_SHA512_H
bitcoin-master\src\hash.cpp:    CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
bitcoin-master\src\key.cpp:    CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(seed.data(), seed.size()).Finalize(vout.data());
bitcoin-master\src\test\crypto_tests.cpp:    TestVector(CHMAC_SHA512(key.data(), key.size()), ParseHex(hexin), ParseHex(hexout));
bitcoin-master\src\test\fuzz\crypto.cpp:    CHMAC_SHA512 hmac_sha512{data.data(), data.size()};
bitcoin-master\src\test\fuzz\crypto.cpp:                        data.resize(CHMAC_SHA512::OUTPUT_SIZE);

```

So `key.cpp` seems to be calling (rather than defining) HMAC\_SHA512 ...

I stopped there but you could make a note of the name of the method in `key.cpp` that calls HMAC\_SHA512 and repeat the search for calls to *that* method until you find code whose method names, variable names and comments suggest you have reached the right place.

Obviously, on Linux instead of `findstr /S` you'll use `grep -r` .

---

Note that using any of this code is likely to be subject to the licence terms and conditions. These are set out in the file named COPYING - it is the MIT licence.


---

