在前文[secp256k1-py 安装以及命令行操作](https://steemit.com/python/@oflyhigh/secp256k1-py)中,我们介绍了secp256k1-py这个Python 库。 我们可以使用这个库用简单的命令行进行公钥私钥对的生成,以及对消息进行签名、校验等操作。  (图源 :[pixabay](https://pixabay.com)) 而在以前的学习中,我们曾经用ecdsa这个Python库来从私钥生成公钥,这节我们回顾一下这些知识,并对比一下ecdsa与secp256k1-py从私钥生成公钥的异同。 # 生成私钥 在之前的文章中,我们曾经说过,私钥就一串随机数,可以用抛硬币256次来生成。不过估计没谁会无聊到这种程度,所以在之前的文章中,我们用hashlib来生成私钥。 ``` import hashlib from binascii import hexlify, unhexlify s = hashlib.sha256(bytes('Hello World', 'utf-8')).digest() print(hexlify(s).decode('ascii')) ``` 上述代码生成如下私钥: >a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e 我们也可以使用 `python -m secp256k1 privkey` 来生成私钥,但是程序员嘛,对`Hello World`情有独钟,所以就用我们上边用`Hello World`生成的私钥好了。 # 生成公钥 在之前的文章中,我们得出过结论,公钥可以通过***`K=k∗G`***计算得出, 其中小***`k`***是我们的***`私钥`***,***`G`***是***`生成点`***,***`K`***是结果亦即***`公钥`***是***`曲线上的另外一个点`***。 另外,由于对于点P(x, y),根据椭圆曲线的方程,我们是可以通过x求得y的,所以公钥可以压缩表示。 #### ecdsa由私钥生成公钥 在之前的文章中,我们最终得出如下代码: ``` import ecdsa from binascii import hexlify, unhexlify secret = unhexlify('a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e') order = ecdsa.SigningKey.from_string(secret, curve=ecdsa.SECP256k1).curve.generator.order() p = ecdsa.SigningKey.from_string(secret, curve=ecdsa.SECP256k1).verifying_key.pubkey.point x_str = ecdsa.util.number_to_string(p.x(), order) y_str = ecdsa.util.number_to_string(p.y(), order) compressed = hexlify(bytes(chr(2 + (p.y() & 1)), 'ascii') + x_str).decode('ascii') uncompressed = hexlify(bytes(chr(4), 'ascii') + x_str + y_str).decode('ascii') print(compressed) print(uncompressed) ``` 输出如下:  #### secp256k1-py 命令行生成公钥 我们可以通过以下指令从私钥生成公钥: `python -m secp256k1 privkey -p -k a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e`  生成的压缩公钥和上边我们用ecdsa生成的完全一致。 #### secp256k1-py 代码生成公钥 ``` from secp256k1 import PrivateKey from binascii import hexlify, unhexlify privkey = PrivateKey(unhexlify('a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e')) compressed = hexlify(privkey.pubkey.serialize()).decode('ascii') uncompressed = hexlify(privkey.pubkey.serialize(compressed=False)).decode('ascii') print(compressed) print(uncompressed) ``` 输出如下:  可见和ecdsa生成的公钥完全一致。 # 结论 在程序中使用secp256k1-py可以更快捷地从私钥生成公钥,代码也更易于阅读和理解。 但是同样因为secp256k1-py的公钥生成逻辑和细节对用户完全透明,要学习相关知识的话,还是用ecdsa更好一些。 # 相关文章 * [secp256k1-py 安装以及命令行操作](https://steemit.com/python/@oflyhigh/secp256k1-py) * [温故而知新 /比特币(Bitcoin)有关的 Base58 & Base58Check、私钥(Private KEY)、公钥(Public KEY)、地址(Address)](https://steemit.com/cn/@oflyhigh/bitcoin-base58-and-base58check-private-key-public-key-address)
author | oflyhigh |
---|---|
permlink | ecdsa-secp256k1-py |
category | python |
json_metadata | {"tags":["python","secp256k1","secp256k1-py","cn-programming","cn"],"image":["https://steemitimages.com/DQmPde9zVwYvy87ZtbKCPanA1dTnShhpzhUPwxan2DmuxHT/image.png","https://steemitimages.com/DQmWDvERLdE5ZWZJn9vRjQWA4ZuEKCkAWG54LimJcYNCtKD/image.png","https://steemitimages.com/DQmR7x47tZeqQCV1VmBoVJHrPLpbbwY88yHsGoywicmxc77/image.png","https://steemitimages.com/DQmYD2wXWcJob67CwAVr1aaiS5pzmT2pWdva6Lx1vRUjqqy/image.png"],"links":["https://steemit.com/python/@oflyhigh/secp256k1-py","https://pixabay.com","https://steemit.com/cn/@oflyhigh/bitcoin-base58-and-base58check-private-key-public-key-address"],"app":"steemit/0.1","format":"markdown"} |
created | 2018-01-14 03:10:27 |
last_update | 2018-01-14 03:10:27 |
depth | 0 |
children | 8 |
last_payout | 2018-01-21 03:10:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 161.801 HBD |
curator_payout_value | 28.237 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,916 |
author_reputation | 6,311,489,499,323,278 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,365,226 |
net_rshares | 22,419,290,780,421 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
pharesim | 0 | 87,317,270,805 | 0.37% | ||
abit | 0 | 6,219,329,818,118 | 100% | ||
jademont | 0 | 207,503,259,783 | 100% | ||
mark-waser | 0 | 96,348,342,295 | 100% | ||
vi1son | 0 | 148,039,260,662 | 100% | ||
blockchainbilly | 0 | 19,938,910,153 | 50% | ||
arcange | 0 | 10,522,722,699 | 3% | ||
deanliu | 0 | 1,044,424,728,871 | 100% | ||
raphaelle | 0 | 1,902,577,664 | 3% | ||
joythewanderer | 0 | 391,728,505,023 | 60% | ||
ace108 | 0 | 235,232,913,101 | 25% | ||
tensaix2j | 0 | 6,270,260,826 | 100% | ||
magicmonk | 0 | 39,957,503,375 | 30% | ||
laoyao | 0 | 34,190,529,445 | 100% | ||
somebody | 0 | 1,294,714,847,645 | 100% | ||
midnightoil | 0 | 54,720,728,451 | 100% | ||
btsabc | 0 | 25,800,709,121 | 100% | ||
watch-chronolog | 0 | 1,475,881,069 | 5% | ||
xiaohui | 0 | 852,045,839,756 | 100% | ||
oflyhigh | 0 | 2,361,690,559,466 | 100% | ||
xiaokongcom | 0 | 10,308,404,793 | 100% | ||
yulan | 0 | 15,777,887,870 | 100% | ||
chinadaily | 0 | 207,738,812,701 | 100% | ||
helene | 0 | 455,892,382,025 | 100% | ||
ffcrossculture | 0 | 1,723,119,132 | 100% | ||
ethansteem | 0 | 203,506,530,266 | 100% | ||
wuyueling | 0 | 58,168,164 | 100% | ||
englishtchrivy | 0 | 97,757,890,159 | 37% | ||
davidjkelley | 0 | 5,787,704,489 | 100% | ||
digital-wisdom | 0 | 48,195,075,166 | 100% | ||
ethical-ai | 0 | 15,208,683,165 | 100% | ||
jwaser | 0 | 24,436,736,875 | 100% | ||
profitgenerator | 0 | 5,326,215,851 | 100% | ||
damarth | 0 | 189,379,773,696 | 3% | ||
bwaser | 0 | 6,263,049,387 | 100% | ||
ellepdub | 0 | 1,501,796,995 | 100% | ||
herpetologyguy | 0 | 246,678,823,069 | 100% | ||
morgan.waser | 0 | 13,398,296,568 | 100% | ||
handyman | 0 | 4,909,094,076 | 100% | ||
amylee | 0 | 89,220,561,893 | 100% | ||
mrtv2 | 0 | 117,216,377,021 | 100% | ||
strong-ai | 0 | 13,556,222,633 | 100% | ||
stacee | 0 | 127,907,562,552 | 100% | ||
steemtruth | 0 | 37,383,572,018 | 10% | ||
redes | 0 | 1,211,389,914,774 | 25% | ||
lalala | 0 | 42,904,185,577 | 100% | ||
joseluismejia | 0 | 3,456,040,260 | 100% | ||
dapeng | 0 | 40,966,537,545 | 25% | ||
devilwsy | 0 | 2,295,990,642 | 100% | ||
janiceting | 0 | 2,306,763,300 | 100% | ||
abraomarcos | 0 | 2,243,042,139 | 100% | ||
lydiachan | 0 | 22,573,779,847 | 100% | ||
technoprogressiv | 0 | 13,404,247,275 | 100% | ||
newhope | 0 | 1,854,724,403,986 | 24% | ||
dragon40 | 0 | 2,323,240,532 | 10% | ||
blackbunny | 0 | 68,338,665,453 | 100% | ||
bxt | 0 | 177,176,050,379 | 100% | ||
lingfei | 0 | 37,583,876,734 | 100% | ||
yyyy | 0 | 3,946,224,385 | 100% | ||
trafalgar | 0 | 759,149,724,730 | 3% | ||
austinsandersco | 0 | 774,790,186 | 70% | ||
htliao | 0 | 90,136,045,906 | 26% | ||
mandagoi | 0 | 18,334,716,529 | 21% | ||
ribalinux | 0 | 5,010,967,672 | 15% | ||
wylo | 0 | 617,754,967 | 100% | ||
susanlo | 0 | 37,108,476,636 | 100% | ||
jkkim | 0 | 263,774,105 | 10% | ||
ebejammin | 0 | 5,868,431,639 | 100% | ||
happychau123 | 0 | 50,662,554,324 | 100% | ||
linuslee0216 | 0 | 629,174,444 | 2.6% | ||
revelim | 0 | 12,195,532,574 | 30% | ||
cryptohustler | 0 | 14,471,667,151 | 100% | ||
drwom | 0 | 6,770,280,809 | 40% | ||
exec | 0 | 81,704,578,546 | 100% | ||
eval | 0 | 813,728,422 | 100% | ||
michaelwilshaw | 0 | 9,684,091,058 | 10% | ||
speeding | 0 | 3,628,753,731 | 100% | ||
moomoo | 0 | 3,313,626,195 | 100% | ||
shenchensucc | 0 | 14,596,492,289 | 100% | ||
walkinharmony | 0 | 16,213,907,223 | 50% | ||
asterix87 | 0 | 13,490,532,936 | 100% | ||
susanli3769 | 0 | 2,436,454,803 | 8% | ||
abetterworld | 0 | 2,790,074,764 | 100% | ||
raili | 0 | 9,166,478,931 | 100% | ||
fenghuang | 0 | 2,375,158,822,292 | 41% | ||
that1consultant | 0 | 2,330,834,767 | 100% | ||
sanzo | 0 | 322,801,500 | 100% | ||
davaowhenyo | 0 | 614,503,785 | 100% | ||
allenshayzar | 0 | 614,503,785 | 100% | ||
christianity101 | 0 | 455,763,094 | 77% | ||
raku | 0 | 617,220,000 | 100% | ||
resteeming | 0 | 614,860,000 | 100% | ||
ravenousappetite | 0 | 614,503,440 | 100% | ||
mydivathings | 0 | 232,245,446 | 100% | ||
aabb | 0 | 4,585,541,591 | 100% | ||
shirlam | 0 | 20,873,937,942 | 99% | ||
auntigormint | 0 | 605,282,500 | 100% | ||
mrliga | 0 | 12,370,867,388 | 100% | ||
angela.ghkh | 0 | 553,501,470 | 10% | ||
hannahwu | 0 | 3,368,414,749 | 26% | ||
imbritish | 0 | 368,548,015 | 66% | ||
sweethoney | 0 | 78,782,144 | 100% | ||
liangfengyouren | 0 | 1,403,719,860 | 50% | ||
idx | 0 | 21,394,035,284 | 100% | ||
jiangchen | 0 | 232,725,821 | 100% | ||
exprmnt | 0 | 614,860,000 | 100% | ||
fr3eze | 0 | 12,456,183,359 | 19% | ||
alli.top | 0 | 341,552,786 | 100% | ||
bearpaw | 0 | 2,479,842,961 | 100% | ||
vadimlasca | 0 | 807,297,232 | 100% | ||
kanxsh | 0 | 1,169,132,450 | 100% | ||
freedom-fighter | 0 | 614,503,785 | 100% | ||
annyblaq | 0 | 554,378,255 | 100% | ||
technologynepal | 0 | 617,220,000 | 100% | ||
chenlocus | 0 | 7,821,423,808 | 40% | ||
ytienchu | 0 | 389,490,746 | 26% | ||
tiffanyrej | 0 | 292,904,889 | 100% | ||
khalilad | 0 | 615,320,000 | 100% | ||
ms8988 | 0 | 618,149,359 | 100% | ||
xiaoshancun | 0 | 531,816,013 | 100% | ||
funzies | 0 | 978,186,169 | 100% | ||
wangwenjing | 0 | 1,212,707,518 | 10% | ||
ikonik | 0 | 592,947,752 | 100% | ||
vfxness | 0 | 7,435,148,935 | 100% | ||
lemminon | 0 | 615,320,000 | 100% | ||
heyeshuang | 0 | 636,999,780 | 100% | ||
grafinx | 0 | 4,918,649,661 | 100% | ||
razor80 | 0 | 611,826,474 | 100% | ||
fastiduos | 0 | 615,320,000 | 100% | ||
ivysrono | 0 | 43,786,620,145 | 100% | ||
weavingwords | 0 | 54,916,115,981 | 100% | ||
alanzheng | 0 | 69,095,652 | 2% | ||
nitro.live | 0 | 19,300,513,362 | 100% | ||
aboragabelgn | 0 | 611,427,500 | 100% | ||
tenzel | 0 | 583,142,406 | 100% | ||
rana420 | 0 | 304,240,055 | 100% | ||
chaerin | 0 | 659,650,889 | 100% | ||
brysj22952 | 0 | 616,539,960 | 100% | ||
cnbuddy | 0 | 3,237,404,732 | 0.64% | ||
deanyeong | 0 | 3,998,220,307 | 80% | ||
lebin | 0 | 24,761,872,127 | 50% | ||
znx | 0 | 13,191,867,103 | 100% | ||
antonsteemit | 0 | 2,615,969,022 | 100% | ||
saury | 0 | 259,305,253 | 100% | ||
candiru | 0 | 339,341,792 | 100% | ||
weiching | 0 | 29,694,364,646 | 19% | ||
itsok | 0 | 1,551,388,550 | 100% | ||
kidult00 | 0 | 425,336,047 | 100% | ||
kidult | 0 | 616,703,930 | 100% | ||
wxy2008 | 0 | 455,997,268 | 100% | ||
abu23 | 0 | 445,408,367 | 100% | ||
zhanxiao | 0 | 599,044,945 | 100% | ||
wilhb81 | 0 | 172,298,502 | 100% | ||
znzmdms | 0 | 344,120,000 | 100% | ||
putrabakri | 0 | 52,228,925 | 9% | ||
berbagi | 0 | 55,305,000 | 9% | ||
canggih | 0 | 55,305,000 | 9% | ||
dapur | 0 | 55,305,000 | 9% | ||
tokonline | 0 | 55,305,000 | 9% | ||
steemtutorial | 0 | 55,305,000 | 9% | ||
atbrittain | 0 | 474,114,105 | 100% | ||
andrewong | 0 | 568,412,500 | 100% | ||
treman26 | 0 | 436,295,000 | 100% | ||
exchangetimes | 0 | 457,802,500 | 100% | ||
nostalgic1212 | 0 | 296,314,755 | 100% | ||
sungbojus | 0 | 113,682,500 | 100% | ||
sunny.sam | 0 | 353,337,500 | 100% |
@oflyhigh please the English (Uk) version. Thanks
author | annyblaq |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180114t031747090z |
category | python |
json_metadata | {"tags":["python"],"users":["oflyhigh"],"app":"steemit/0.1"} |
created | 2018-01-14 03:17:54 |
last_update | 2018-01-14 03:17:54 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 03:17:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 49 |
author_reputation | 10,902,782,063 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,366,334 |
net_rshares | 0 |
Beautiful post. Good work friend. I follow you.
author | ansarmehmood |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180114t041338084z |
category | python |
json_metadata | {"tags":["python"],"app":"steemit/0.1"} |
created | 2018-01-14 04:15:09 |
last_update | 2018-01-14 04:15:09 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 04:15:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 47 |
author_reputation | 410,353,238,009 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,374,305 |
net_rshares | 0 |
https://steemit.com/cn/@crypto-king-pak/282kks 
author | crypto-king-pak |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180115t070436958z |
category | python |
json_metadata | {"tags":["python"],"image":["https://steemitimages.com/DQmchXoTgb6Dva1JbtRv7YXCE47C3fbiM6exwdFaYAykkzB/20180115_114704.jpg"],"links":["https://steemit.com/cn/@crypto-king-pak/282kks"],"app":"steemit/0.1"} |
created | 2018-01-15 07:04:39 |
last_update | 2018-01-15 07:04:39 |
depth | 1 |
children | 0 |
last_payout | 2018-01-22 07:04:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 164 |
author_reputation | 10,546,028,443 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,638,703 |
net_rshares | 0 |
thanks for this good post i gave upvote and follow you @oflyhigh please follow me friend @exchangetimes
author | exchangetimes |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180114t155316893z |
category | python |
json_metadata | {"tags":["python"],"users":["oflyhigh","exchangetimes"],"app":"steemit/0.1"} |
created | 2018-01-14 14:53:33 |
last_update | 2018-01-14 14:53:33 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 14:53:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 103 |
author_reputation | 70,569,246,143 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,476,503 |
net_rshares | 0 |
nice post dear
author | setuakter |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180114t045209136z |
category | python |
json_metadata | {"tags":["python"],"app":"steemit/0.1"} |
created | 2018-01-14 04:52:12 |
last_update | 2018-01-14 04:52:12 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 04:52:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 14 |
author_reputation | 173,872,091,830 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,379,514 |
net_rshares | 0 |
Please give us the English interpretation. Thanks.
author | sungbojus |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180114t033557676z |
category | python |
json_metadata | {"tags":["python"],"app":"steemit/0.1"} |
created | 2018-01-14 03:36:00 |
last_update | 2018-01-14 03:36:00 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 03:36:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 50 |
author_reputation | 574,145,539,121 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,368,789 |
net_rshares | 0 |
great work i vote to you
author | sunny.sam |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180114t034226913z |
category | python |
json_metadata | {"tags":["python"],"app":"steemit/0.1"} |
created | 2018-01-14 03:42:27 |
last_update | 2018-01-14 03:42:27 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 03:42:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 24 |
author_reputation | 190,775,267,304 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,369,752 |
net_rshares | 0 |
大神!每篇文章都有很用!!
author | zhanxiao |
---|---|
permlink | re-oflyhigh-ecdsa-secp256k1-py-20180114t031220093z |
category | python |
json_metadata | {"tags":["python"],"app":"steemit/0.1"} |
created | 2018-01-14 03:12:18 |
last_update | 2018-01-14 03:12:30 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 03:12:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 13 |
author_reputation | 63,512,851,996 |
root_title | 对比一下ecdsa与secp256k1-py从私钥生成公钥 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,365,507 |
net_rshares | 0 |