# Kernel hash and Coin age comparison

Hi,

To be able to mine a block, a miner has to find a kernel hash that is lower then the coin age of one of his stakes. In short: SHA256(kernel) < stake.

Now, if I create a hash, it may look like this: 32b686fc00275e00daaefc338a356cbb1221b1715b442b6fcda795d10a91b80e (SHA256 hash of ‘peercoin’)
Suppose I have 1000 coins, and their age is 60 days, from their last transaction. That makes my stake 60 * 1000 = 60.000.

How does Peercoin determine that the above hash is lower then the stake?
32b686fc00275e00daaefc338a356cbb1221b1715b442b6fcda795d10a91b80e < 60.000 ?

Thanks,
Fevir

a hash is just a seq of 32 bytes, which can represent a big number. 0x32, the first byte, is hex for 50 in decimal. etc…
in javascript that would look like this:

var hashProofOfStakeInt = BigInteger.fromByteArrayUnsigned(hashProofOfStake);

this number can be compared to the target, which is in javascript:

var targetInt = bnCoinDayWeight.multiply(bnTargetPerCoinDay);

While bnTargetPerCoinDay is determined by current difficulty, your coin age does make bnCoinDayWeight bigger which helps to get targetInt > hashProofOfStakeInt

My excuses for explaining in code, but at some point it is easier to explain

```coinstake must meet hash target according to the protocol: kernel (input 0) must meet the formula hash(nStakeModifier + txPrev.block.nTime + txPrev.offset + txPrev.nTime + txPrev.vout.n + nTime) < bnTarget * nCoinDayWeight this ensures that the chance of getting a coinstake is proportional to the amount of coin age one owns. ```

Here is the corresponding code: https://github.com/ppcoin/ppcoin/blob/v0.4.0ppc/src/kernel.cpp#L313

And the related wiki page: https://wiki.peercointalk.org/index.php?title=CheckStakeKernelHash_function

Feel free to ask if you need some explanations about some part of the code.

EDIT: You can also try to ask your questions on freenode IRC #peercoin channel for live answers if one of us is online.

Not a Java person, but trying to understand.

So, var hashProofOfStakeInt – just creating a variable that will hold a certain value.
= BigInteger – a value is turned in to a BigInteger.
.fromByteArrayUnsigned(hashProofOfStake) – is the value that turns into a BigInteger, in this case, our hash is converted to a BigInteger. hashProofOfStake is calculated, as mably states above, in the ‘hash’.

I get the concept, but to what is the actual value of hashProofOfStakeInt, and how do we get there? Are you (thehuntergames) suggesting that the hash is hexed, so 32b686… becomes 50-107-134… (without the -)?

This one seems easy:
var targetInt = bnCoinDayWeight.multiply(bnTargetPerCoinDay);
So, a variable ‘targetInt’ is created, which is created by ‘coin day’ * ‘target’.

Then targetInt is compared to hashProofOfStakeInt.

@ mably, thanks, I did find those pages; as stated above, never programmed in Java before. I was hoping for an easy answer yes instead of base 10, it is using base 256.

Instead of 10 fingers we use 256.

2-6 in base 10 = 2 * 10^1 + 6 * 10^0 = 26 in radix 10

78-210-93 in base 256 = 78256^2 + 210256^1 + 93*256^0 = … you get the idea

Actually original peercoin code is written in C/C++.

[quote=“fevir, post:4, topic:3616”]So, var hashProofOfStakeInt – just creating a variable that will hold a certain value.
= BigInteger – a value is turned in to a BigInteger.
.fromByteArrayUnsigned(hashProofOfStake) – is the value that turns into a BigInteger, in this case, our hash is converted to a BigInteger. hashProofOfStake is calculated, as mably states above, in the ‘hash’.

I get the concept, but to what is the actual value of hashProofOfStakeInt, and how do we get there? Are you (thehuntergames) suggesting that the hash is hexed, so 32b686… becomes 50-107-134… (without the -)?[/quote]

The Hash function used here actually returns a uint256 (256 bits unsigned integer) value which can be easily converted to a CBigNum (BigInteger) as can be seen here.

You’re welcome.