Author

Topic: Trying to understanding Bitcoin mining algorithm (Read 9057 times)

full member
Activity: 453
Merit: 101
RISE WITH RAYS FOR THE FUTURE
December 02, 2011, 09:56:35 AM
#10
Thank you very much.
Got it with this code:

        public string ComputeHash(string input, HashAlgorithm algorithm)
        {
            Byte[] inputBytes = Encoding.UTF8.GetBytes(input);

            Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
            HashAlgorithm hashing;
            hashing = new SHA256Managed();
            textBox3.Text = BitConverter.ToString(hashing.ComputeHash(hashedBytes));
            return BitConverter.ToString(hashedBytes);
        }
foo
sr. member
Activity: 409
Merit: 250
You can't do the second hash with a string as input, you need to use a number. In this case the number is 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824.
full member
Activity: 453
Merit: 101
RISE WITH RAYS FOR THE FUTURE
Its me again!

Have readed this page:
https://en.bitcoin.it/wiki/Protocol_specification
Got text:
"Example of double-SHA-256 encoding of string "hello":
hello
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round of sha-256)
9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50 (second round of sha-256)"

Got after first round :
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
second
a6ac4251402d8b9cbac7af2abeb7f2cd3fc96c3a1d3df508e26870dc91a079a4
or
d7914fe546b684688bb95f4f888a92dfc680603a75f23eb823658031fff766d9

Code:
public string ComputeHash(string input, HashAlgorithm algorithm)
        {
            Byte[] inputBytes = Encoding.UTF8.GetBytes(input);

            Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);

            return BitConverter.ToString(hashedBytes);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = ComputeHash(textBox1.Text, new SHA256Cng());
            textBox2.Text = textBox2.Text.ToLower();
            textBox2.Text = textBox2.Text.Replace("-", "");
     
            textBox3.Text = ComputeHash(textBox2.Text, new SHA256Cng());
            textBox3.Text = textBox3.Text.ToLower();
            textBox3.Text = textBox3.Text.Replace("-", "");
        }

Whats wrong?
donator
Activity: 1218
Merit: 1079
Gerald Davis
Ok. 2nd problem seems to be solved.

Quote
{
  "midstate": "eb5a28fb7c4ee0b602ecf3ddd31bd6591b9b4d2f53767cd7433fa557cf1c2127",
  "data": "00000001acb8f97aae2fae30ec96b2bf67c7e064d134e80dcd46f52f00000dbe000000000473187 52323d170d19c090d9ea8684da21be77bad127dffc3fcfda5cb29469e4eb050191a0df0ca000000 0000000080000000000000000000000000000000000000000000000000000000000000000000000 0000000000080020000",
  "hash1": "0000000000000000000000000000000000000000000000000000000000000000000000800000000 0000000000000000000000000000000000000000000010000",
  "target": "0000000000000000000000000000000000000000000000caf00d000000000000"
}

We take our encrypted "answer" and compare it to reversed "target", in that case:
0000000000000000000000000000000000000000000000caf00d000000000000
In answer any behind first 12 zeroes can in this case be anything?

And now to first question.
There is nonce, and its increasing all the time.
In first try its 00000001, second 00000002 etc.
In examples while they encrypt it with sha256 they convert nonce, but i cannot find in which format they convert it. In example before converting nonce was 00000000 and just before hashing its in format like 2e6b0a1d. I just dont understand which format it must be converted.

Tried to understand WIKI but there are 2 variable: i am stupid  or its not explained very good in wiki.

Thanks for answers.

I believe the blockexplorer shows the target reversed.

So 0000000000000000000000000000000000000000000000caf00d000000000000

becomes
00000000.0000d00f.ac000000.00000000.00000000.00000000.00000000.00000000
valid hash must be equall to or smaller than this hexaecimal number.

To simplify the matching we can ignore the target and look only for smaller values.
00000000.0000d00f.acffffff.ffffff.ffffff.ffffff.ffffff.ffffff
valid has must be smaller than this

only these values matter   
00000000.0000d00f.acffffff.   
if the thist three segements are below this the hash is valid.

I am not sure what you mean by they convert the nonce.  The noce is a hexadecimal number.  Every single one is tried from 00000000 to ffffff.
Once all nonce values are tried the block header is changed (time if nothing else) and entire nonce range is tried again (00000000 to ffffff).


legendary
Activity: 1050
Merit: 1000
You are WRONG!
I am coding on C# by the way.
DON'T!
full member
Activity: 453
Merit: 101
RISE WITH RAYS FOR THE FUTURE
Ok. 2nd problem seems to be solved.

Quote
{
  "midstate": "eb5a28fb7c4ee0b602ecf3ddd31bd6591b9b4d2f53767cd7433fa557cf1c2127",
  "data": "00000001acb8f97aae2fae30ec96b2bf67c7e064d134e80dcd46f52f00000dbe000000000473187 52323d170d19c090d9ea8684da21be77bad127dffc3fcfda5cb29469e4eb050191a0df0ca000000 0000000080000000000000000000000000000000000000000000000000000000000000000000000 0000000000080020000",
  "hash1": "0000000000000000000000000000000000000000000000000000000000000000000000800000000 0000000000000000000000000000000000000000000010000",
  "target": "0000000000000000000000000000000000000000000000caf00d000000000000"
}

We take our encrypted "answer" and compare it to reversed "target", in that case:
0000000000000000000000000000000000000000000000caf00d000000000000
In answer any behind first 12 zeroes can in this case be anything?

And now to first question.
There is nonce, and its increasing all the time.
In first try its 00000001, second 00000002 etc.
In examples while they encrypt it with sha256 they convert nonce, but i cannot find in which format they convert it. In example before converting nonce was 00000000 and just before hashing its in format like 2e6b0a1d. I just dont understand which format it must be converted.

Tried to understand WIKI but there are 2 variable: i am stupid  or its not explained very good in wiki.

Thanks for answers.
donator
Activity: 1218
Merit: 1079
Gerald Davis
1)To what type i must convert nonce 00000000 to get 2e6b0a1d???

Not sure what you are asking here.  A miner will start at nonce 0 and try all 2^32 possibilities until it reaches 4294967295.  This allows multiple hashes per blockheader change.  Once all nonces have been tried



Quote
2) In some examples i have seen target: ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000 How i know is my solution smaller than target?

You reverse it again and compare the values (they are hexadecimal)

ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000 becomes

00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff

These would be smaller
00000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffe
000000008e93ed87e827f989e2b4343c43434d434efff
00000000.... anything else here is less.....

These would be too large
00000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffff
0f28ebb00ffffffffffffffffffffffffffffffffffffffffffffffffffffffff
00000001 .... anything else here is too large ...

Since only the most significant digits matter generally you can break the hash into 32 bit "cunks" and just compare them.

So
00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff

00000000.ffffffff.ffffffff.ffffffff.ffffffff.ffffffff.ffffffff.ffffffff

so to match your example target the first 32bit must be 00000000.  Anything other than 0 is too large.
Since second 32bit segment is ffffffff then any value is good (no need to check 2nd to 8th segments).

On the other hand if you target was
fffffffffffffffffffffffffffffffffffffffffffffffffffffffa00000000

which reverses to
00000000.afffffff.ffffffff.ffffffff.ffffffff.ffffffff.ffffffff.ffffffff

then you would need to check that first segment is <= 00000000
and second segment <= afffffff
legendary
Activity: 1204
Merit: 1015
Does the wiki answer your question?
https://en.bitcoin.it/wiki/Difficulty
full member
Activity: 453
Merit: 101
RISE WITH RAYS FOR THE FUTURE
No one knows?
full member
Activity: 453
Merit: 101
RISE WITH RAYS FOR THE FUTURE
Hi!
I actually wanted to write to Russian Coders section, but becouse of restriction have to ask here:

I try to simulate mining. I am not very good coder, but anyway.
@ the moment as i have read:

We take: Version, Prev. Block hash, Merkle,Date, Difficulty,Nonce.
Revers every of them, put in same "string", double hash SHA256, reverse again and check is it smaller than difficulty?

Problems i have:
1)To what type i must convert nonce 00000000 to get 2e6b0a1d???

2) In some examples i have seen target: ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000 How i know is my solution smaller than target?

I am coding on C# by the way.

Thanks
Jump to: