# What is the precise nonce finding protocol?

There are many descriptions of what mining is, but none of them describe it exactly. From what I gather:

Mining means finding a nonce c such that for some information s the hash value h(s||c) begins with a certain number of zeros (h is sha-256, || concatenation).

The "information" is given here. The nonce is an integer that is incremented in the mining process. But it seems that it's not s||c that is hashed, but rather there is some double hashing going on, but of what, h(s)||c, h(s||c), h(s)||h(c)? I can't deduce it from the Python code in the previous link (NB: I'm code illiterate).

Also the nonce isn't just initialized at zero and then incremented until there's success:

To avoid risking wasting work in this way, there needs to be a random starting point, and so the work becomes to find H(s,x,c)/2^(n-k) = 0 where x is random (eg 128-bits to make it statistically infeasible for two users to maliciously or accidentally start at the same point), and c is the counter being varied, and s is the service string. (source)

But that's the article describing hashcash, and who knows bitcoin might not have implemented the hashcash protocol as it was proposed.

So my question is, what is the exact protocol? I'd go and read the source code if I could but like I said I'm not code literate. Surely the answer must be possible to formulate concisely, something like:

Compute h(s||c) where c runs over 0,1,... until value begins with 15 zeros.

or

Hash s and obtain x in manner X, then compute h(h(s)||c) where c runs over x,x+1,..., until value begins with 17 zeros.

Sorry if this has been asked before, if so clearly I'm googling the wrong words, thanks.

This mining simulator is a good visual of what is being hashed.

http://www.yogh.io/#mine:last

People are correct you don't have to change the Nonce it's just the fastest way to get a different hash output. You could very well keep the nonce at 1 and change the timestamp, or the list of included transactions, which would change the merkelroot.

What's being hashed is version + previous block hash + merkel root + time + bits (target) + nonce. This gets hashed twice sha256(sha256(blockheader))

The code and description here is pre https://en.bitcoin.it/wiki/Block_hashing_algorithm

• Thanks! I mark this as the answer since it answers my primary concern without the need for further clarifying – Erik Vesterlund Dec 23 '15 at 22:58
• The mining simulator is pretty cool! – Murch Feb 21 '16 at 11:47

There is no concatenation. The nonce is part of the block header. The block header is 80 bytes and is hashed twice. That means you first sha-256 hash the 80 byte block header, and then you hash the hash.

Starting different users in a pool at different nonce values would be useless. With modern hardware all possible nonce values are processed in a split second.

Instead different users are given a different template from which to generate different blocks (small variations of the same block). After a miner has tried all nonce values it generates a new block from the template and starts over.

Using templates ensures that each client can generate their own work without needing to talk to a mining pool server many times per second.

Using different templates for each user ensures that no two users in the pool will ever do the same work.

What part of the work does an ASIC chip do: https://bitcoin.stackexchange.com/a/41775/141

Mining protocols: https://bitcoin.stackexchange.com/a/13307/141

There is no precise nonce finding protocol.

The miner can arbitrarily choose a nonce c to perform the hashing operation.

Mining is a mathematical game where the goal is to make the result of the hash function smaller than a given number (this is what "a result starting with x zeros" is looking for). The number is directly based on the current Bitcoin network difficulty and changes every two weeks to keep average block finding time at 600 seconds.

Most nonce generators just increment by 1 but the key is where they start. If you are solo mining, you can pick a random number. If you are mining with multiple devices or you are a pool administrator, you have to divide the work to avoid calculating the same hash twice (make sure they never use the same c).

• What I meant was the synonymous word procedure, "A particular method for performing a task", so there is indeed a nonce finding protocol/procedure. – Erik Vesterlund Dec 21 '15 at 23:24
• precise is the operative word – Ron Dec 21 '15 at 23:37
• What do you mean? There is no ambiguity in the procedure. That all parameters are not decided beforehand doesn't mean the procedure is not precise. – Erik Vesterlund Dec 21 '15 at 23:45
• other than its size in bits, there is no protocol governing the generation of nonce values. Therefore there can be no precise procedure to generate it. Start at 0 and add 1, start at MAX and subtract 1... they're all valid. – Ron Dec 21 '15 at 23:47
• Yes but we're talking nonce finding, aren't we? Def: A nonce c is found if hash(hash(header||c)) < bound. – Erik Vesterlund Dec 21 '15 at 23:58

There is no "protocol" for finding a suitable nonce value. Any value that, after hashing, produces a result matching the current difficulty is acceptable. How a given miner finds that value is completely up to the miner. It's not of any benefit for all miners to start at the same value (0) and work upwards, because then all but the fastest miner is duplicating work that others have already just done (and by definition, the fastest miner will win). So it is beneficial for miners to start somewhere where they don't think anybody else will have looked yet.

• I think you misunderstand my admittedly poorly phrased question. What I want to know is what must the miner compute? "Any value that, after hashing, produces a result matching the current difficulty is acceptable" - it is the hashing part I'm asking about. What precisely is hashed? blockheader||nonceOfMinersChoice, or hash(blockheader)||nonceOfMinersChoice, or something else? – Erik Vesterlund Dec 20 '15 at 20:14
• @ErikVesterlund: The hash is computed as `h(s||c)` (which means "concatenate the header and the nonce, and then hash the result") just as the text you quoted says. In the case of Bitcoin, the hash function is `h(x) = SHA256(SHA256(x))`. – Greg Hewgill Dec 20 '15 at 20:21
• So to be clear, it is sha256(sha256(s||c)), not sha256(s||c) or sha256(sha256(s)||c)? – Erik Vesterlund Dec 20 '15 at 20:24
• @ErikVesterlund: Yes, that's right. – Greg Hewgill Dec 20 '15 at 20:50
• No, you don't concatenate the nonce after the first hashing and then hash again. The nonce is part of the header, so it part of the first hashing. – Dr.Haribo Dec 21 '15 at 20:45