<p>Probably not the right stack exchange for this question. It is only tangentially related to bitcoin. You probably want <a href="https://stackexchange.com/filters/169">Programming</a>, or <a href="https://cs.stackexchange.com/questions/tagged/machine-learning">Computer Science</a>.</p>

CC BY-SA 4.0
2021-11-24T19:25:06.197Z
2021-11-24T19:25:06.197Z
Johnathan J.
<p>You can check this github project for the midstate optimization code. The project is in C# but you can easily port to c++. Works well for existing blocks.</p>
<p><a href="https://github.com/aseemgautam/bitcoin-sha256" rel="nofollow noreferrer">https://github.com/aseemgautam/bitcoin-sha256</a></p>

CC BY-SA 4.0
2022-05-21T14:58:59.847Z
2022-05-21T14:58:59.847Z
A G
<p>I'm creating an AI controlled bitcoin miner but I'm having trouble manually &amp; locally creating a <strong>midstate</strong> function to be used for optimizing the mining process. Basically I've created independent functions that handle the steps involved in mining bitcoin manually.... <em>all except the <strong>midstate</strong> creation function</em> - <strong><code>inline std::string GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);</code></strong>.</p>
<p>I've searched everywhere and have been unable to find a detailed implementation guide and I've also been unsuccessful in doing it myself. The closest I came to understanding the logic behind it was <strong><a href="https://stackoverflow.com/questions/9245235/go-midstate-sha-256-hash/9322486#9322486">THIS</a></strong> - of which my <strong>untested</strong> attempt has been wiped along with my laptop and android. Just got a Desktop and I REALLY NEED <strong>HELP!!!!</strong></p>
<p>All I really need is an accurate implementation of the <strong>midstate</strong> function using <strong>C++</strong></p>
<pre><code>#include &lt;iostream&gt;
#include ALL LIBRARIES USED IN SOLUTION CODE

// DD - Declare and Define
// DD Variable, DD Function, DD Class, DD Struct ....
// Got the idea from Luffy from ONE PIECE - Gum Gum: Jet Pistol, Gum Gum: Gattling Gun ....
// Anyways .... MOVING ON


// Assume all data types are std::string
// This should help with faster debugging/testing of code/function

std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variables
  std::string midstate_hash;

  // Concatenate chunks of data -- 4 bytes, 32 bytes , 32 bytes
  midstate_hash = nVersion + nPrevHash + nMerkleRoot;

  // Then call sha256 to do whatever vodoo it does
  sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(midstate_hash);

  // ---- OR ----
  midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);


  // Then return string to caller function
  return midstate_hash;

}

// SAME FUNCTION BUT OPTIMIZED
std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variable, Concatenate data and assign variable to the result of midstate function
  std::string midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);

  return midstate_hash; // return midstate
}



// The implementation of the **sha256 midstate** generation can be done within same function, independent function e.g **SHA256_TRANSFORM_MIDSTATE()** or it can exist within an external header file.
// But please all header files used in creating a working function should be clearly stated and not be left to my imagination.. Please and THANKS in advance.


int main()
{
  std::string MIDSTATE, nVersion, nPrevHash, nMerkleRoot, nTime, nBits, nNonce;
  nVersion = 00000002;
  nPrevHash = &quot;8975c5797431243........&quot;;
  nMerkleRoot = &quot;ac363c68a97320f039ef4392ac2337754b65bfbb4790faf84d3a27c974c3ab31&quot;;
  nTime = &quot;3ab1dd30&quot;;
  nBits = &quot;1b9033c2&quot;;
  nNonce = &quot;001b32bc&quot;;

  MIDSTATE = GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);
  std::cout&lt;&lt; &quot;Midstate of block data is &quot;&lt;&lt; MIDSTATE &lt;&lt; std::endl;
  std::cout &lt;&lt; &quot;Begin mining attempts&quot; &lt;&lt; std::endl;

  return 0;

}
</code></pre>
<p>At the end of the day the output should have successfully and accurately generated a midstate which obviously should not remain the same when merkleRootHash is changed/updated.</p>
<p>That's it guys... It's really that simple. The code template is as straight forward as shown above. The rest of the program of course is as technical as you can imagine (lol...)</p>
<p>And Oh.. please do feel free to point out any and all possible issue(s)(and their solutions) that might come up when converting <strong><code>std::string</code></strong> to <strong><code>uint32_t, int32_t, uint256</code></strong> ..... I wouldn't mind optimizing my <strong>untested</strong> fix for the possible and inevitable <strong>incompatible conversion of string to BTYE</strong>   <em>error flags</em></p>
<p>This is really URGENT and extremely important to me. Thanks</p>
<p>ORIGINAL BITCOIN DATA TYPES</p>
<pre><code>int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;
</code></pre>
<p>A side question which can be ignored... P.S please don't ignore</p>
<p>Q. If a midstate is made up of only <strong><code>(nVersion &amp; nPrevHash)</code></strong> or <strong><code>(nPrevHash &amp; nMerkleRoot)</code></strong> - will it still be an accurate midstate if hashing function called
<strong><code>sha256(sha256(midstate, nMerkleRoot, nTime, nBits, nNonce));</code></strong> or <strong><code>sha256(sha256(nVersion, midstate, nTime, nBits, nNonce));</code></strong>. Would such a midtate be valid??</p>

<mining-theory><sha256><c++><midstate>
How do i create/implement a midstate function (One that can accurately calculate the midstate of already mined blocks)
AncientTides
# How do i create/implement a midstate function (One that can accurately calculate the midstate of already mined blocks)

(Asked by: username_0 on 2021-11-22)

I'm creating an AI controlled bitcoin miner but I'm having trouble manually & locally creating a **midstate** function to be used for optimizing the mining process. Basically I've created independent functions that handle the steps involved in mining bitcoin manually.... *all except the **midstate** creation function* - **`inline std::string GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);`**.

I've searched everywhere and have been unable to find a detailed implementation guide and I've also been unsuccessful in doing it myself. The closest I came to understanding the logic behind it was **[THIS](https://stackoverflow.com/questions/9245235/go-midstate-sha-256-hash/9322486#9322486)** - of which my **untested** attempt has been wiped along with my laptop and android. Just got a Desktop and I REALLY NEED **HELP!!!!**

All I really need is an accurate implementation of the **midstate** function using **C++**

```
#include <iostream>
#include ALL LIBRARIES USED IN SOLUTION CODE

// DD - Declare and Define
// DD Variable, DD Function, DD Class, DD Struct ....
// Got the idea from Luffy from ONE PIECE - Gum Gum: Jet Pistol, Gum Gum: Gattling Gun ....
// Anyways .... MOVING ON

// Assume all data types are std::string
// This should help with faster debugging/testing of code/function

std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variables
  std::string midstate_hash;

  // Concatenate chunks of data -- 4 bytes, 32 bytes , 32 bytes
  midstate_hash = nVersion + nPrevHash + nMerkleRoot;

  // Then call sha256 to do whatever vodoo it does
  sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(midstate_hash);

  // ---- OR ----
  midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);

  // Then return string to caller function
  return midstate_hash;

}

// SAME FUNCTION BUT OPTIMIZED
std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variable, Concatenate data and assign variable to the result of midstate function
  std::string midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);

  return midstate_hash; // return midstate
}

// The implementation of the **sha256 midstate** generation can be done within same function, independent function e.g **SHA256_TRANSFORM_MIDSTATE()** or it can exist within an external header file.
// But please all header files used in creating a working function should be clearly stated and not be left to my imagination.. Please and THANKS in advance.

int main()
{
  std::string MIDSTATE, nVersion, nPrevHash, nMerkleRoot, nTime, nBits, nNonce;
  nVersion = 00000002;
  nPrevHash = "8975c5797431243........";
  nMerkleRoot = "ac363c68a97320f039ef4392ac2337754b65bfbb4790faf84d3a27c974c3ab31";
  nTime = "3ab1dd30";
  nBits = "1b9033c2";
  nNonce = "001b32bc";

  MIDSTATE = GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);
  std::cout<< "Midstate of block data is "<< MIDSTATE << std::endl;
  std::cout << "Begin mining attempts" << std::endl;

  return 0;

}

```

At the end of the day the output should have successfully and accurately generated a midstate which obviously should not remain the same when merkleRootHash is changed/updated.

That's it guys... It's really that simple. The code template is as straight forward as shown above. The rest of the program of course is as technical as you can imagine (lol...)

And Oh.. please do feel free to point out any and all possible issue(s)(and their solutions) that might come up when converting **`std::string`** to **`uint32_t, int32_t, uint256`** ..... I wouldn't mind optimizing my **untested** fix for the possible and inevitable **incompatible conversion of string to BTYE** *error flags*

This is really URGENT and extremely important to me. Thanks

ORIGINAL BITCOIN DATA TYPES

```
int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;

```

A side question which can be ignored... P.S please don't ignore

Q. If a midstate is made up of only **`(nVersion & nPrevHash)`** or **`(nPrevHash & nMerkleRoot)`** - will it still be an accurate midstate if hashing function called
**`sha256(sha256(midstate, nMerkleRoot, nTime, nBits, nNonce));`** or **`sha256(sha256(nVersion, midstate, nTime, nBits, nNonce));`**. Would such a midtate be valid??

**Tags:** mining-theory, sha256, c++, midstate


---

## 2 answers

### Answer by username_1 on 2021-11-24. Score: 1

Probably not the right stack exchange for this question. It is only tangentially related to bitcoin. You probably want [Programming](https://stackexchange.com/filters/169), or [Computer Science](https://cs.stackexchange.com/questions/tagged/machine-learning).


---

### Answer by username_2 on 2022-05-21. Score: 0

You can check this github project for the midstate optimization code. The project is in C# but you can easily port to c++. Works well for existing blocks.

<https://github.com/aseemgautam/bitcoin-sha256>


---

