トランザクションのバージョン

トランザクションのバージョンは通常1で、16進数の01000000で表記される。リトルエンディアン

class Tx:

    def __init__(self, version, tx_ins, tx_outs, locktime, testnet=False):
        self.version = version
        self.tx_ins = tx_ins 
        self.tx_outs = tx_outs 
        self.locktime = locktime
        self.testnet = testnet

    def __repr__(self):
        tx_ins = ''
        for tx_in in self.tx_ins:
            tx_ins += tx_in.__repr__() + '\n'
        tx_outs = ''
        for tx_out in self.tx_outs:
            tx_outs += tx_out.__repr__() + '\n'
        return 'tx: {}\nversion: {}\ntx_ins:\n{}tx_outs:\nlocktime: {}'.format(
            self.id(),
            self.version,
            tx_ins,
            tx_outs,
            self.locktime,
        )
    
    def id(self):
        return self.hash().hex()

    def hash(self):
        return hash256(self.serialize())[::-1]

    @classmethod
    def parse(cls, s, testnet=False):
        version = little_endian_to_int(s.read(4))
        return cls(version, None, None, None, testnet=testnet)

inputs, outputs, locktimeはNoneとしておく。

秘密鍵からbitcoinテストネットのアドレス作成

ビットコインの秘密鍵は256ビットの16進数

ビットコインの秘密鍵は256ビット(32バイト、16進数で64文字)で、1から2^256の範囲であればほぼすべての数字が秘密鍵として利用

from ecc import S256Point, PrivateKey
from helper import hash160, little_endian_to_int

import os

private_key = os.urandom(32)
p = little_endian_to_int(hash160(private_key))
print(p)

privateKey = PrivateKey(p)
wallet = privateKey.point.address(testnet=True)
print(wallet)

little_endian_to_int でint型にしてアドレスを作成する

ウォレットインポート形式(WIF)

秘密鍵は256ビットの数値である。
WIFは人間が読めるように秘密鍵をシリアライズした形式。アドレスと同じようにBase58エンコーディングを用いる。

1. メインネット用の秘密鍵は0x80、テストネット用は0xefのプレフィックスで開始
2. 秘密鍵を32バイトのビッグエンディアンでエンコード
3. 公開鍵アドレス用のSECフォーマットが圧縮形式の場合、末尾に0x01を追加
4. 1.のプレフィックス、2.のシリアライズした秘密鍵、3.のサフィックスを順に結合
5. 4.の結果にhash256を実行し、先頭の4バイトを取得
6. 4.と5.を結合させて、Base58でエンコード

    def wif(self, compressed=True, testnet=False):
        secret_bytes = self.secret.to_bytes(32, 'big')
        if testnet:
            prefix = b'\xef'
        else:
            prefix = b'\x80'
        if compressed:
            suffix = b'\x01'
        else:
            suffix = b''
        return encode_base58_checksum(prefix + secret_bytes + suffix)

秘密鍵を”5003″とする場合のWIF

from ecc import S256Point, PrivateKey
import helper

privateKey = PrivateKey(5003)
wif = privateKey.wif(compressed=True, testnet=True)
print(wif)

$ python3 main.py
cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN8rFTv2sfUK

ビットコインのアドレス形式

アドレスを短くして安全性を高めるにはripemd160ハッシュを使用する。33バイトから20バイトへ短縮できる。

1. メインネットアドレスは先頭を0x00、テストネットは0x6fで開始する。
2. SECフォーマット(圧縮・非圧縮)を取り出し、sha256とripemd160ハッシュ操作を行う。この組み合わせをhash160操作と呼ぶ。
3. 1のプレフィックスと2のハッシュ操作を結合する。
4. 3の結果にhash256を行い、先頭の4バイトを取得する
5. 3と4を結合させてBase58でエンコードする

4の結果をチェックサムと呼ぶ

def encode_base58_checksum(b):
    return encode_base58(b + hash256(b)[:4])
import helper from encode_base58_checksum, hash160

def encode_base58_checksum(b):
    return encode_base58(b + hash256(b)[:4])

def hash160(s):
    return hashlib.new('ripemd160', hashlib.sha256(s).digest()).digest()
    def hash160(self, compressed=True):
        return hash160(self.sec(compressed))

    def address(self, compressed=True, testnet=False):
        h160 = self.hash160(compressed)
        if testnet:
            prefix = b'\x6f'
        else:
            prefix = b'\x00'
        return encode_base58_checksum(prefix + h160)

秘密鍵を”5002″とする場合のアドレスの求め方

from ecc import S256Point, PrivateKey

privateKey = PrivateKey(5002)
address = privateKey.point.address(compressed=False, testnet=True)
print(address)

$ python3 main.py
mmTPbXQFxboEtNRkwfh6K51jvdtHLxGeMA

圧縮・非圧縮やメインネット、テストネットは引数で切り替える
address = privateKey.point.address(compressed=True, testnet=False)

スカラー倍算のコーディング

    def __rmul__(self, coefficient):
        product = self.__class__(Nonee, None, self.a, self.b)
        for _ in range(coefficient):
            product += self
        return product

coefficient の和訳は係数です。係数の数だけ点を加算します。

ビットの倍算だと以下のようになる。2進展開。

    def __rmul__(self, coefficient):
        coef = coefficient 
        current = self 
        result = self.__class__(Nonee, None, self.a, self.b)
        while coef:
            if coef & 1:
                result += current
            current += current
        return result

点と曲線の確認をunittestで実行する

ecc.py

from unittest import TestCase

class ECCTest(TestCase):

    def test_on_curve(self):
        prime = 223
        a = FieldElement(0, prime)
        b = FieldElement(7, prime)  
        valid_points = ((192, 105),(17,56),(1,193))
        invalid_points = ((200, 119),(42, 99))
        for x_raw, y_raw in valid_points:
            x = FieldElement(x_raw, prime)
            y = FieldElement(y_raw, prime)
            Point(x, y, a, b)
        for x_raw, y_raw in invalid_points:
            x = FieldElement(x_raw, prime)
            y = FieldElement(y_raw, prime)
            with self.assertRaises(ValueError):
                Point(x, y, a, b)

helper.py

from unittest import TestSuite, TextTestRunner

def run(test):
    suite = TestSuite()
    suite.addTest(test)
    TextTestRunner().run(suite)

main.py

import ecc
from helper import run

run(ecc.ECCTest('test_on_curve'))

$ python3 main.py
.
———————————————————————-
Ran 1 test in 0.000s

OK

有限体とは

位数が有限である体
四則演算が定義され閉じている有限集

整数の集合は無限の要素だが、有限体は要素が有限で、四則演算(足し算・引き算・掛け算・割り算)が閉じている
=> 閉じているとは、演算結果が、有理数でなくなるということがない
=> 演算を施した結果がふたたびもとの集合に属する
素数 P で割った余り

これ、初手でいきなりつまづくな…

pythonとhmac-sha512

HMACは暗号ハッシュ関数を使用してメッセージ認証を行う仕組み

import hmac
import hashlib

key=b"secret2"
text=b"foo bar"
signature=hmac.new(key,text,hashlib.md5).hexdigest()
print(signature)

b””としないと、エラーになる。
self._hmac = _hashopenssl.hmac_new(key, msg, digestmod=digestmod)
TypeError: Strings must be encoded before hashing

HD Wallet

import os
import binascii
import ecdsa
import hmac
import hashlib

seed = os.urandom(32)
root_key = b'Bitcoin Seed'

def hmac_sha512(data, keymessage):
    hash = hmac.new(data, keymessage, hashlib.sha512).digest()
    return hash

def create_pubkey(private_key):
    publickey = ecdsa.SigningKey.from_string(private_key, curve=ecdsa.SECP256k1).verifying_key.to_string()
    return publickey

master = hmac_sha512(seed, root_key)
master_secretkey = master[:32]
master_chaincode = master[32:]

master_publickey = create_pubkey(master_secretkey)
master_publickey_integer = int.from_bytes(master_publickey[32:], byteorder="big")

if master_publickey_integer % 2 == 0:
    master_publickey_x = b"\x02" + master_publickey[:32]
else:
    master_publickey_x = b"\x03" + master_publickey[:32]

print("マスター秘密鍵")
print(binascii.hexlify(master_secretkey))
print("\n")
print("マスターチェーンコード")
print(binascii.hexlify(master_chaincode))
print("\n")
print("マスター公開鍵")
print(binascii.hexlify(master_publickey_x))

index = 0
index_bytes = index.to_bytes(8, "big")
data = master_publickey_x + index_bytes
result_hmac512 = hmac_sha512(data, master_chaincode)

sum_integer = int.from_bytes(master_secretkey, "big") + int.from_bytes(result_hmac512[:32], "big")

p = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1
child_secretkey = (sum_integer % p).to_bytes(32, "big")

print("\n")
print("子秘密鍵")
print(binascii.hexlify(child_secretkey))

bitcoin アドレスの生成

import os
import ecdsa
import hashlib
import base58
from Crypto.Hash import RIPEMD160

private_key = os.urandom(32)
public_key = ecdsa.SigningKey.from_string(private_key, curve=ecdsa.SECP256k1).verifying_key.to_string()

prefix_and_pubkey = b"\x04" + public_key

intermediate = hashlib.sha256(prefix_and_pubkey).digest()
ripemd160 = RIPEMD160.new()
ripemd160.update(intermediate)
hash160 = ripemd160.digest()

prefix_and_hash160 = b"\x00" + hash160

double_hash = hashlib.sha256(hashlib.sha256(prefix_and_hash160).digest()).digest()
checksum = double_hash[:4]
pre_address = prefix_and_hash160 + checksum

address = base58.b58encode(pre_address)
print(address.decode())

hashlibで以下のように書くとエラーになる。
ripemd160 = hashlib.new(‘ripemd160’)

$ python3 address.py
Traceback (most recent call last):
File “/usr/lib/python3.10/hashlib.py”, line 160, in __hash_new
return _hashlib.new(name, data, **kwargs)
ValueError: [digital envelope routines] unsupported

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/vagrant/dev/work/address.py”, line 12, in
ripemd160 = hashlib.new(‘ripemd160’)
File “/usr/lib/python3.10/hashlib.py”, line 166, in __hash_new
return __get_builtin_constructor(name)(data)
File “/usr/lib/python3.10/hashlib.py”, line 123, in __get_builtin_constructor
raise ValueError(‘unsupported hash type ‘ + name)
ValueError: unsupported hash type ripemd160

従って、pycryptodomeをインストールして、ripemd160を使う
$ pip install pycryptodome