Bitcoin Forum
December 07, 2016, 04:40:23 PM *
News: Latest stable version of Bitcoin Core: 0.13.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Trying to understanding Bitcoin mining algorithm  (Read 8335 times)
a2offrb
Full Member
***
Offline Offline

Activity: 150



View Profile
October 24, 2011, 08:59:07 AM
 #1

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
1481128823
Hero Member
*
Offline Offline

Posts: 1481128823

View Profile Personal Message (Offline)

Ignore
1481128823
Reply with quote  #2

1481128823
Report to moderator
1481128823
Hero Member
*
Offline Offline

Posts: 1481128823

View Profile Personal Message (Offline)

Ignore
1481128823
Reply with quote  #2

1481128823
Report to moderator
1481128823
Hero Member
*
Offline Offline

Posts: 1481128823

View Profile Personal Message (Offline)

Ignore
1481128823
Reply with quote  #2

1481128823
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481128823
Hero Member
*
Offline Offline

Posts: 1481128823

View Profile Personal Message (Offline)

Ignore
1481128823
Reply with quote  #2

1481128823
Report to moderator
a2offrb
Full Member
***
Offline Offline

Activity: 150



View Profile
October 31, 2011, 09:47:45 AM
 #2

No one knows?
Maged
Legendary
*
Offline Offline

Activity: 1260


View Profile
October 31, 2011, 03:09:53 PM
 #3

Does the wiki answer your question?
https://en.bitcoin.it/wiki/Difficulty

DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218


Gerald Davis


View Profile
October 31, 2011, 05:56:23 PM
 #4

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
a2offrb
Full Member
***
Offline Offline

Activity: 150



View Profile
November 01, 2011, 08:09:24 PM
 #5

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.
kokjo
Legendary
*
Offline Offline

Activity: 1050

You are WRONG!


View Profile
November 01, 2011, 08:12:32 PM
 #6

I am coding on C# by the way.
DON'T!

"The whole problem with the world is that fools and fanatics are always so certain of themselves and wiser people so full of doubts." -Bertrand Russell
DeathAndTaxes
Donator
Legendary
*
Offline Offline

Activity: 1218


Gerald Davis


View Profile
November 01, 2011, 08:48:31 PM
 #7

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).


a2offrb
Full Member
***
Offline Offline

Activity: 150



View Profile
December 01, 2011, 09:36:08 PM
 #8

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?
foo
Sr. Member
****
Offline Offline

Activity: 409



View Profile
December 02, 2011, 11:36:05 AM
 #9

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.

I know this because Tyler knows this.
a2offrb
Full Member
***
Offline Offline

Activity: 150



View Profile
December 02, 2011, 02:56:35 PM
 #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);
        }
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!