This is a C/C++ programming question related to CPU mining. In the source code for CPU miners wolf-xmr-miner, cpuminer-multi et al there is a function called scanhash_cryptonight that has this the following prototype:

int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
                         uint32_t max_nonce, uint64_t *hashes_done);

The parameters pdata and ptarget are pointers to members of a struct that looks like this:

struct work {
    uint32_t data[32];
    uint32_t target[8];

    /* ... */

Now when scanhash_cryptonight is called there is this nonceptr variable that is initialized like this (link to scanhash function):

uint32_t *nonceptr = (uint32_t*) (((char*)pdata) + 39); 

My question is: what is the value of nonceptr used for and what is the casting in the expresssion doing?


nonceptr is a pointer to the place where the nonce is in the block header (pointed to by pdata). It is defined as a pointer to an unsigned 32-bit integer because the nonce is 32 bits long, and it is used when mining to easily change the nonce (e.g. *nonceptr = new_nonce;).

  • This is correct. Also, to make sure I understand: is it true that the nonce is a 32-bit unsigned int that is stored at offset 39 in block_hashing_blob? It's incremented by miners and used in the cryptonight PoW hash to find a block that is below a certain difficulty target. – moo Dec 20 '17 at 1:33
  • Yes the nonce is a 32-bit unsigned integer currently located at offset 39 in blockhashing_blob. However, there are some variable size integers before the nonce in the block header (version numbers, timestamp), so in the future if at least one of them gets big enough for its serialization to require more bytes to be stored, the offset of the nonce will increase. – glv Dec 20 '17 at 9:10
  • One more thing, can you refer me to any reading that will help me reach the same level of understanding that you have? i want to build an open source miner in C over the next few months because i am dissatisfied with how hacky other miners are (many of them are just forked from the same hacky code and adapted) atm, i am struggling through the moner-project's source. anyway, thanks (again) and hope i see you around. – moo Dec 21 '17 at 3:07
  • 1
    There are the MRL papers (getmonero.org/resources/research-lab), Moneropedia (getmonero.org/resources/moneropedia), some StackExchange answers (like monero.stackexchange.com/questions/3958/…)... – glv Dec 21 '17 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.