「Block hashing algorithm」の版間の差分

提供: tezos-wiki
移動先: 案内検索
(1版 をインポートしました)
1行目: 1行目:
Bitcoin mining uses the [[hashcash]] proof of work function; the hashcash algorithm requires the following parameters: a service string, a nonce, and a counter.  In bitcoin the service string is encoded in the block header data structure, and includes a version field, the hash of the previous block, the root hash of the merkle tree of all transactions in the block, the current time, and the difficulty.  Bitcoin stores the nonce in the extraNonce field which is part of the coinbase transaction, which is stored as the left most leaf node in the merkle tree (the coinbase is the special first transaction in the block).  The counter parameter is small at 32-bits so each time it wraps the extraNonce field must be incremented (or otherwise changed) to avoid repeating work.
+
Bitcoinマイニングでは、[[hashcash]]実証関数を使用します。ハッシュキャッシュアルゴリズムには、サービスストリング、ナンス、およびカウンターというパラメーターが必要です。ビット列では、サービス文字列はブロックヘッダデータ構造で符号化され、バージョンフィールド、前のブロックのハッシュ、ブロック内のすべてのトランザクションのメルクルツリーのルートハッシュ、現在の時間、および難易度が含まれます。 Bitcoinは、コーンベーストランザクションの一部であるextraNonceフィールドにナンスを格納します。これは、Merkleツリーの最左端のノードとして格納されます(コインベースは、ブロック内の特別な最初のトランザクションです)。カウンタパラメータは32ビットで小さいため、ラップするたびにrepeatNonceフィールドを増やす(または変更して)作業を繰り返さないようにする必要があります。
The basics of the hashcash algorithm are quite easy to understand and it is described in more detail [[hashcash|here]].
+
ハッシュキャッシュアルゴリズムの基本は理解しやすいので、これについては[[hashcash | here]]で詳しく説明しています。
When mining bitcoin, the hashcash algorithm repeatedly hashes the block header while incrementing the counter & extraNonce fields.  Incrementing the extraNonce field entails recomputing the merkle tree, as the coinbase transaction is the left most leaf node.  The block is also occasionally updated as you are working on it.
+
ビットコインをマイニングするとき、ハッシュキャッシュアルゴリズムはブロックヘッダーを繰り返しハッシュし、counter&extraNonceフィールドをインクリメントします。 extraNonceフィールドをインクリメントするには、コインベースのトランザクションが左端のリーフノードであるため、Merkleツリーを再計算する必要があります。ブロックは、作業中に時折更新されることもあります。
  
A block header contains these fields:
+
ブロックヘッダーには次のフィールドがあります。
{| class="wikitable"
+
{| class = "wikitable"
|-
+
| -  
! Field
+
!フィールド
! Purpose
+
!目的
! Updated when...
+
!いつ更新...
! Size (Bytes)
+
!サイズ(バイト)
|-
+
| -  
|Version
+
|バージョン
|Block version number
+
|ブロックバージョン番号
|You upgrade the software and it specifies a new version
+
|ソフトウェアをアップグレードし、新しいバージョンを指定する
|4
+
| 4
|-
+
| -  
|hashPrevBlock
+
| hashPrevBlock
|256-bit hash of the previous block header
+
|前のブロックヘッダーの256ビットのハッシュ
|A new block comes in
+
|新しいブロックが来る
|32
+
| 32
|-d
+
| -d
|hashMerkleRoot
+
| hashMerkleRoot
|256-bit hash based on all of the transactions in the block
+
|ブロック内のすべてのトランザクションに基づく256ビットのハッシュ
|A transaction is accepted
+
|取引が受け入れられる
|32
+
| 32
|-
+
| -  
|Time
+
|時間
|Current timestamp as seconds since 1970-01-01T00:00 UTC
+
|現在のタイムスタンプ(1970-01-01T00:UTCからの秒数)
|Every few seconds
+
|数秒ごと
|4
+
| 4
|-
+
| -  
|Bits
+
|ビット
|Current [[target]] in compact format
+
|現在の[[ターゲット]]をコンパクト形式で
|The [[difficulty]] is adjusted
+
| [難しさ]を調整しました
|4
+
| 4
|-
+
| -  
|Nonce
+
|ノンス
|32-bit number (starts at 0)
+
| 32ビット数(0から始まる)
|A hash is tried (increments)
+
|ハッシュが試行されます(増分)
|4
+
| 4
 
|}
 
|}
  
The body of the block contains the transactions. These are hashed only indirectly through the Merkle root. Because transactions aren't hashed directly, hashing a block with 1 transaction takes exactly the same amount of effort as hashing a block with 10,000 transactions.
+
ブロックの本体にはトランザクションが含まれています。これらは、Merkleルートを介して間接的にのみハッシュされます。トランザクションは直接ハッシングされないので、1つのトランザクションでブロックをハッシュすることは、10,000のトランザクションでブロックをハッシュすることとまったく同じ努力を要する。
  
The compact format of target is a special kind of floating-point encoding using 3 bytes mantissa, the leading byte as exponent (where only the 5 lowest bits are used) and its base is 256.
+
コンパクトなフォーマットのターゲットは、3バイトの仮数を使用する特別な種類の浮動小数点符号化で、指数として先頭のバイト(5つの最下位ビットのみが使用されます)とその底は256です。
Most of these fields will be the same for all users. There might be some minor variation in the timestamps. The nonce will usually be different, but it increases in a strictly linear way. "Nonce" starts at 0 and is incremented for each hash. Whenever Nonce overflows (which it does frequently), the extraNonce portion of the generation transaction is incremented, which changes the Merkle root.
+
これらのフィールドのほとんどは、すべてのユーザーで同じになります。タイムスタンプに多少のばらつきがあるかもしれません。ナンスは通常異なるが、厳密に線形に増加する。 「Nonce」は0から始まり、各ハッシュに対してインクリメントされます。 Nonceがオーバーフローすると(頻繁に発生する)、生成トランザクションのextraNonce部分がインクリメントされ、Merkleルートが変更されます。
  
Moreover, it is extremely unlikely for two people to have the same Merkle root because the first transaction in your block is a generation "sent" to one of ''your'' unique Bitcoin addresses. Since your block is different from everyone else's blocks, you are (nearly) guaranteed to produce different hashes. Every hash you calculate has the same chance of winning as every other hash calculated by the network.
+
さらに、ブロック内の最初のトランザクションが "あなたの"独自のBitcoinアドレスの1つに "送信"されているため、2人が同じMerkleルートを持つことはほとんどありません。あなたのブロックは他のブロックとは異なるので、あなたは(ほとんど)異なるハッシュを生成することが保証されています。あなたが計算するすべてのハッシュは、ネットワークによって計算される他のすべてのハッシュと同じ勝利のチャンスを持ちます。
 +
BitcoinではSHA256(SHA256(Block_Header))を使用していますが、バイトオーダーについては注意が必要です。
  
Bitcoin uses: SHA256(SHA256(Block_Header)) but you have to be careful about byte-order.
+
たとえば、このPythonコードは、2011年6月現在で最小のハッシュを持つブロックのハッシュを計算します[http://blockexplorer.com/block/00000000000000e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1dブロック125552]。ヘッダーは上記の6つのフィールドから作成され、16進表記でリトルエンディアン値として連結されます。
 
+
<source lang = "python">
For example, this python code will calculate the hash of the block with the smallest hash as of June 2011, [http://blockexplorer.com/block/00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d Block 125552].  The header is built from the six fields described above, concatenated together as little-endian values in hex notation:
 
<source lang="python">
 
 
>>> import hashlib
 
>>> import hashlib
>>> header_hex = ("01000000" +
+
>>> header_hex ="01000000" +
"81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
+
 "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
"e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
+
 "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
"c7f5d74d" +
+
 "c7f5d74d" +
"f2b9441a" +
+
 "f2b9441a" +
"42a14695")
+
 "42a14695"
>>> header_bin = header_hex.decode('hex')
+
>>> header_bin = header_hex.decode( 'hex'
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
+
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest())。digest()
>>> hash.encode('hex_codec')
+
>>> hash.encode( 'hex_codec'
 
'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000'
 
'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000'
>>> hash[::-1].encode('hex_codec')
+
>>> hash [:: - 1] .encode( 'hex_codec'
 
'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'
 
'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'
</source>
+
</ source>
  
==Endianess==
+
==エンディアン==
Note that the hash, which is a 256-bit number, has lots of leading zero bytes when stored or printed as a big-endian hexadecimal constant, but it has trailing zero bytes when stored or printed in little-endian. For example, if interpreted as a string and the lowest (or start of) the string address keeps lowest significant byte, it is little-endian.
+
256ビットの数値であるハッシュは、ビッグエンディアンの16進定数として格納または印刷されるときに先行するゼロバイトがたくさんあることに注意してください。リトルエンディアンで保存または印刷すると、末尾にゼロバイトがあります。たとえば、文字列として解釈され、文字列アドレスの最下位(または先頭)が最下位バイトを保持する場合、リトルエンディアンです。
  
The output of [http://blockexplorer.com blockexplorer] displays the hash values as big-endian numbers; notation for numbers is usual (leading digits are the most significant digits read from left to right).
+
[http://blockexplorer.com blockexplorer]の出力は、ハッシュ値をビッグエンディアン数で表示します。数字の表記は通常です(先頭の数字は左から右へ読む最上位の数字です)。
  
For another example, [http://pastebin.com/bW3fQA2a here] is a version in plain C without any optimization, threading or error checking.
+
別の例として、[http://pastebin.com/bW3fQA2a here]は、最適化、スレッド化、またはエラーチェックを行わずにプレーンCのバージョンです。
  
Here is the same example in plain PHP without any optimization.
+
以下は、最適化のないプレーンなPHPの同じ例です。
<source lang="php">
+
<source lang = "php">
<?
+
<
  //This reverses and then swaps every other char
+
  //これは、他のすべての文字を反転してからスワップします。
  function SwapOrder($in){
+
  関数SwapOrder($ in){
      $Split = str_split(strrev($in));
+
      $スプリット= str_split(strrev($ in));
      $x='';
+
      $ x = '';
      for ($i = 0; $i < count($Split); $i+=2) {
+
      for($ i = 0; $ i <count($ Split); $ i + = 2){
          $x .= $Split[$i+1].$Split[$i];
+
          $ x。= $分割[$ i + 1]$分割[$ i];
      }  
+
      }
      return $x;
+
      $ xを返します。
  }
+
  }
 
+
  
  //makes the littleEndian
+
  // littleEndianを作る
  function littleEndian($value){
+
  関数littleEndian($ value){
      return implode (unpack('H*',pack("V*",$value)));
+
      アンパックを返す(unpack( 'H *'、pack( "V *"$ value)));
  }
+
  }
 
+
  
  $version = littleEndian(1);
+
  $ version = littleEndian(1);
  $prevBlockHash = SwapOrder('00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81');
+
  $ prevBlockHash = SwapOrder( '00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81';
  $rootHash = SwapOrder('2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3');
+
  $ rootHash = SwapOrder( '2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3';
  $time = littleEndian(1305998791);
+
  $ time = littleEndian(1305998791);
  $bits = littleEndian(440711666);  
+
  $ bits = littleEndian(440711666);
  $nonce = littleEndian(2504433986);  
+
  $ nonce = littleEndian(2504433986);
 
+
  
  //concat it all
+
  //すべてを連結する
  $header_hex = $version . $prevBlockHash . $rootHash . $time . $bits . $nonce;
+
  $ header_hex = $ versionです。 $ prevBlockHash。 $ rootHash。 $時間。 $ビット。 $ nonce;
 
+
  
  //convert from hex to binary
+
  // 16進数から2進数に変換する
  $header_bin = hex2bin($header_hex);
+
  $ header_bin = hex2bin($ header_hex);
  //hash it then convert from hex to binary
+
  //ハッシュしてから、16進数から2進数に変換する
  $pass1 = hex2bin(  hash('sha256', $header_bin )  );
+
  $ pass1 = hex2bin(hash( 'sha256'$ header_bin));
  //Hash it for the seconded time
+
  //二度目にハッシュします
  $pass2 = hash('sha256', $pass1);
+
  $ pass2 = hash( 'sha256'$ pass1);
  //fix the order
+
  //注文を修正する
  $FinalHash = SwapOrder($pass2);
+
  $ FinalHash = SwapOrder($ pass2);
 
+
  
  echo  $FinalHash;
+
  エコー$ファイナルハッシュ;
?>
+
>
</source>
+
</ source>
  
[[Category:Technical]]
+
[[Category:技術]]

2018年4月13日 (金) 02:06時点における版

Bitcoinマイニングでは、hashcash実証関数を使用します。ハッシュキャッシュアルゴリズムには、サービスストリング、ナンス、およびカウンターというパラメーターが必要です。ビット列では、サービス文字列はブロックヘッダデータ構造で符号化され、バージョンフィールド、前のブロックのハッシュ、ブロック内のすべてのトランザクションのメルクルツリーのルートハッシュ、現在の時間、および難易度が含まれます。 Bitcoinは、コーンベーストランザクションの一部であるextraNonceフィールドにナンスを格納します。これは、Merkleツリーの最左端のノードとして格納されます(コインベースは、ブロック内の特別な最初のトランザクションです)。カウンタパラメータは32ビットで小さいため、ラップするたびにrepeatNonceフィールドを増やす(または変更して)作業を繰り返さないようにする必要があります。 ハッシュキャッシュアルゴリズムの基本は理解しやすいので、これについては hereで詳しく説明しています。 ビットコインをマイニングするとき、ハッシュキャッシュアルゴリズムはブロックヘッダーを繰り返しハッシュし、counter&extraNonceフィールドをインクリメントします。 extraNonceフィールドをインクリメントするには、コインベースのトランザクションが左端のリーフノードであるため、Merkleツリーを再計算する必要があります。ブロックは、作業中に時折更新されることもあります。

ブロックヘッダーには次のフィールドがあります。

-

!フィールド !目的 !いつ更新... !サイズ(バイト)

- バージョン ブロックバージョン番号 ソフトウェアをアップグレードし、新しいバージョンを指定する 4 - hashPrevBlock 前のブロックヘッダーの256ビットのハッシュ 新しいブロックが来る 32 -d hashMerkleRoot ブロック内のすべてのトランザクションに基づく256ビットのハッシュ 取引が受け入れられる 32 - 時間 現在のタイムスタンプ(1970-01-01T00:UTCからの秒数) 数秒ごと 4 - ビット 現在のターゲットをコンパクト形式で [難しさ]を調整しました 4 - ノンス 32ビット数(0から始まる) ハッシュが試行されます(増分) 4

ブロックの本体にはトランザクションが含まれています。これらは、Merkleルートを介して間接的にのみハッシュされます。トランザクションは直接ハッシングされないので、1つのトランザクションでブロックをハッシュすることは、10,000のトランザクションでブロックをハッシュすることとまったく同じ努力を要する。

コンパクトなフォーマットのターゲットは、3バイトの仮数を使用する特別な種類の浮動小数点符号化で、指数として先頭のバイト(5つの最下位ビットのみが使用されます)とその底は256です。 これらのフィールドのほとんどは、すべてのユーザーで同じになります。タイムスタンプに多少のばらつきがあるかもしれません。ナンスは通常異なるが、厳密に線形に増加する。 「Nonce」は0から始まり、各ハッシュに対してインクリメントされます。 Nonceがオーバーフローすると(頻繁に発生する)、生成トランザクションのextraNonce部分がインクリメントされ、Merkleルートが変更されます。

さらに、ブロック内の最初のトランザクションが "あなたの"独自のBitcoinアドレスの1つに "送信"されているため、2人が同じMerkleルートを持つことはほとんどありません。あなたのブロックは他のブロックとは異なるので、あなたは(ほとんど)異なるハッシュを生成することが保証されています。あなたが計算するすべてのハッシュは、ネットワークによって計算される他のすべてのハッシュと同じ勝利のチャンスを持ちます。 BitcoinではSHA256(SHA256(Block_Header))を使用していますが、バイトオーダーについては注意が必要です。

たとえば、このPythonコードは、2011年6月現在で最小のハッシュを持つブロックのハッシュを計算します[1]。ヘッダーは上記の6つのフィールドから作成され、16進表記でリトルエンディアン値として連結されます。 <source lang = "python"> >>> import hashlib >>> header_hex =( "01000000" +  "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +  "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +  "c7f5d74d" +  "f2b9441a" +  "42a14695") >>> header_bin = header_hex.decode( 'hex') >>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest())。digest() >>> hash.encode( 'hex_codec') '1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000' >>> hash [:: - 1] .encode( 'hex_codec') '00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d' </ source>

エンディアン

256ビットの数値であるハッシュは、ビッグエンディアンの16進定数として格納または印刷されるときに先行するゼロバイトがたくさんあることに注意してください。リトルエンディアンで保存または印刷すると、末尾にゼロバイトがあります。たとえば、文字列として解釈され、文字列アドレスの最下位(または先頭)が最下位バイトを保持する場合、リトルエンディアンです。

blockexplorerの出力は、ハッシュ値をビッグエンディアン数で表示します。数字の表記は通常です(先頭の数字は左から右へ読む最上位の数字です)。

別の例として、hereは、最適化、スレッド化、またはエラーチェックを行わずにプレーンCのバージョンです。

以下は、最適化のないプレーンなPHPの同じ例です。 <source lang = "php"> <?   //これは、他のすべての文字を反転してからスワップします。   関数SwapOrder($ in){       $スプリット= str_split(strrev($ in));       $ x = ;       for($ i = 0; $ i <count($ Split); $ i + = 2){           $ x。= $分割[$ i + 1]。$分割[$ i];       }       $ xを返します。   }      // littleEndianを作る   関数littleEndian($ value){       アンパックを返す(unpack( 'H *'、pack( "V *"、$ value)));   }      $ version = littleEndian(1);   $ prevBlockHash = SwapOrder( '00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81');   $ rootHash = SwapOrder( '2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3');   $ time = littleEndian(1305998791);   $ bits = littleEndian(440711666);   $ nonce = littleEndian(2504433986);      //すべてを連結する   $ header_hex = $ versionです。 $ prevBlockHash。 $ rootHash。 $時間。 $ビット。 $ nonce;      // 16進数から2進数に変換する   $ header_bin = hex2bin($ header_hex);   //ハッシュしてから、16進数から2進数に変換する   $ pass1 = hex2bin(hash( 'sha256'、$ header_bin));   //二度目にハッシュします   $ pass2 = hash( 'sha256'、$ pass1);   //注文を修正する   $ FinalHash = SwapOrder($ pass2);      エコー$ファイナルハッシュ; ?> </ source>

Category:技術