比特币地址生成

一个比特币钱包中包含一系列的密钥对,每个密钥对包括一个私钥和一个公钥。私钥(k)是一个数字,通常是随机选出的。有了私钥,我们就可以使用椭圆曲线乘法这个单向加密函数产生一个公钥(K)。有了公钥(K),我们就可以使用一个单向加密哈希函数生成比特币地址(A)。在本节中,我们将从生成私钥开始,讲述如何使用椭圆曲线运算将私钥生成公钥,并最终由公钥生成比特币地址。私钥、公钥和比特币地址之间的关系如下图所示。
这里写图片描述

比特币使用了secp256k1标准所定义的一条特殊的椭圆曲线和一系列数学常数。该标准由美国国家标准与技术研究院(NIST)设立。secp256k1曲线由下述函数定义,该函数可产生一条椭圆曲线:
y^2 = (x^3 + 7)} over (Fp)

y^2 mod p = (x^3 + 7) mod p
上述mod p(素数p取模)表明该曲线是在素数阶p的有限域内,也写作Fp,其中p = 2^256 – 2^32 – 2^9 – 2^8 – 2^7 – 2^6 – 2^4 – 1,这是一个非常大的素数。

比特币地址可由公钥经过单向的加密哈希算法得到。哈希算法是一种单向函数,接收任意长度的输入产生指纹摘要。加密哈希函数在比特币中被广泛使用:比特币地址、脚本地址以及在挖矿中的工作量证明算法。由公钥生成比特币地址时使用的算法是Secure Hash Algorithm (SHA)和the RACE Integrity Primitives Evaluation Message Digest (RIPEMD),特别是SHA256和RIPEMD160。
以公钥 K 为输入,计算其SHA256哈希值,并以此结果计算RIPEMD160 哈希值,得到一个长度为160比特(20字节)的数字:
A = RIPEMD160(SHA256(K))
公式中,K是公钥,A是生成的比特币地址。
这里写图片描述

python 实现:

import bitcoin
#Generate a random private key
valid_private_key=False
while not valid_private_key:
    private_key = bitcoin.random_key()
    decoded_private_key = bitcoin.decode_privkey(private_key,'hex')
    compressed_private_key = private_key + '01'
    valid_private_key=0<decoded_private_key<bitcoin.N

print("Private Key (hex) is:",private_key)
print("Private Key (decimal) is:",decoded_private_key)
print("Private Key Compressed (hex) is:",compressed_private_key)

#Convert private key to WIF format bin_to_b58chech(encode(priv,256,32))+b'\x01',128+int(vbyte)

wif_encoding_private_key=bitcoin.encode_privkey(decoded_private_key,'wif')
wif_compressed_private_key=bitcoin.encode_privkey(decoded_private_key,'wif_compressed')

print("Private Key(WIF) is :",wif_encoding_private_key)
print("Private Key(WIF-Compressed) is:",wif_encoding_private_key)


#Multiply the EC generator point G with the private key to get a public key point

public_key=bitcoin.privkey_to_pubkey(decoded_private_key)
print("Public key (x,y) coordinates is:",public_key)

#Encode as hex,prefix 04

hex_encoded_public_key=bitcoin.encode_pubkey(public_key,'hex')
print("Public key(hex) is:", hex_encoded_public_key)

#Compress public key

hex_compressed_public_key=bitcoin.encode_pubkey(public_key,'hex_compressed')
print("Public Key (hex) is:", hex_compressed_public_key)

#Generate compressd bitcoin address

print("Compressed BitCoin Address (b58Check) is:",bitcoin.pubkey_to_address(hex_compressed_public_key))

运行结果:

('Private Key (hex) is:', 'cdc251339ae7f5eb9307b2fabf99fa0c559f4a830d9775da750680aeb7736984')
('Private Key (decimal) is:', 93067462722861502220508048909367986477516452871578598536429701079758554753412L)
('Private Key Compressed (hex) is:', 'cdc251339ae7f5eb9307b2fabf99fa0c559f4a830d9775da750680aeb773698401')
('Private Key(WIF) is :', '5KNuRLDhiciuru7gnsFzbvquhhnh146TQHGZq2zngn3pxEuwdhT')
('Private Key(WIF-Compressed) is:', '5KNuRLDhiciuru7gnsFzbvquhhnh146TQHGZq2zngn3pxEuwdhT')
('Public key (x,y) coordinates is:', (99721318612079371418755876162091289597888001042842450951215822209800054135238L, 113456397613655834157807616351733448095224302432153543297669122135832329444447L))
('Public key(hex) is:', '04dc784423817f472991548cc7a279a446c845a250cad3db86783f2fe5226e59c6fad60b9ea2544f9237e62b4d65f61ad9906143eba74ba2b427ee90f7534bdc5f')
('Public Key (hex) is:', '03dc784423817f472991548cc7a279a446c845a250cad3db86783f2fe5226e59c6')
('Compressed BitCoin Address (b58Check) is:', '1sBU8e7yewpdJzojPba3RqCYWZBXohVe1')

http://zhibimo.com/read/wang-miao/mastering-bitcoin/Chapter04.html

猜你喜欢

转载自blog.csdn.net/niekai01/article/details/80070269