Published Date : 2021年12月31日9:51

028 Pythonでビットコインを学ぶ (BASE58の仕組みをPythonと図を使って理解してみよう パート3)
028 Use python to learn bitcoin (Understand how BASE 58 works in using Python and diagrams Part 3)


This blog has an English translation


ニコニコ動画にアップした動画のまとめ記事です。

This is a summary blog post about a video I uploaded to NicoNico.

細かい部分は動画を参考にしてください。

Please refer to the video for details.


目次

Table of Contents




① 動画の説明
① Video Description



00:00 BASE58の仕組みをPythonを使って理解してみよう。パート3

00:00 Understand how BASE 58 works in using Python. Part 3

00:05 前回のパート2ではdivmodメソッドを使って、ある数値をBase58用にエンコードする手順を紹介しました。

00:05 In Part 2, I showed you how to use the divmod method to encode a number for Base 58.

00:10 では実際にBase58を使ってある数値をエンコードしていきましょう。数値の変数と基数の変数を用意します。

00:10 Now let's actually encode a number using Base 58. Prepare numeric and radix variables.

00:23 商がゼロになるまでワイル文とdivmodメソッドを使って計算していきましょう。print関数を使ってその様子を確かめます。

00:23 Until the quotient is zero, we will calculate it using the while statement and divmod methiod. Use the print function to see what happens.

while quotient > 0:
    quotient, remainder = divmod(quotient, base)
    print(quotient, remainder)

00:34 結果はパート1で見せた図のようになりました。

00:34 The calculation results are like the diagram I showed you in Part 1.

00:43 では計算された剰余の値を格納するリストを作成しましょう。

00:43 Now, let's create a list to store the computed remainder values.

list_of_remainders = []

00:48 そして、商がゼロになるまで計算していき、剰余の値を先ほどのリストに格納していくだけです。

00:48 Then, we just compute until the quotient is zero and store the remainder value in the list we created earlier.

quotient, remainder = int_value, 0
while quotient > 0:
    quotient, remainder = divmod(quotient, base)
    list_of_remainders.append(remainder)

01:05 剰余の値が格納されたリストの順序を逆にします。

01:05 Reverses the order of the list containing the remainder values.

for r in reversed(list_of_remainders):
    print(r)

01:25 それから、For文を使って剰余の値をBase58の文字列のインデックス番号として使い表示させます。

01:25 It then uses the For statement to display the BASE 58 string using the remainder as the index number.

for r in reversed(list_of_remainders):
    print(base58_chars[r])

01:49 Pythonのリスト内包表記とjoinメソッドを使って簡潔にエンコード文字列を表示させてみましょう。

01:49 Let's use Python's list comprehension and join methods to display the encoding string succinctly.

''.join([base58_chars[r] for r in reversed(list_of_remainders)])

02:24 これである整数値から、Base58のエンコード文字列を作成できました。

02:24 We have now created a Base 58 encoded string from an integer value.

03:11 では次にBase58のエンコード文字列から元の整数値に戻すデコード作業をしてみましょう。

03:11 Now let's decode the Base 58 encoded string back to its original integer value.

03:16 今度はエンコード時にリストの順序を逆にしたので、デコード時も順序を逆にして、findメソッドを使用して文字からインデックス番号を調べます。

03:16 Now that we have reversed the order of the list during encoding, we also reversed the order during decoding and used the find method to find the index number from the character.

03:25 これらのインデックス番号がエンコード作業時に計算された剰余の数となります。

03:25 These index numbers are the number of remainder value calculated during the encoding process.

03:33 これらのインデックス番号を使って、パート1の図で説明した様に、最後の文字から順番にその対応する数値に対して基数となる58の累乗を掛けていきます。

03:33 These index numbers are then used to multiply the corresponding number by the base power of 58, starting with the last character, as described in the illustration in Part 1.

04:15 そして先ほどデコード作業を行った全ての数値を足し合わせると、エンコードされる前の数値に戻すことができます。

04:15 You can then add up all the numbers you just decoded and return them to the numbers they were before they were encoded.

4:30 Part4へ続く。

4:30 Continued to Part 4.



以上です。お疲れ様です。

That's all. Thank you for your hard work.