在昨天的帖子中,学习了如何由私钥生成公钥.
有朋友私下问我,你咋整出俩公钥啊,一个压缩,一个未压缩,这又是什么鬼?我该用哪个公钥啊?别急,听我慢慢道来。
公钥压缩
为何要压缩
根据我们上节学习的成果,公钥(K)是通过私钥(k)乘以生成点(G)得到的,并且是椭圆曲线上的一个点P(x, y)
上图是我们用Hello World生成的私钥并计算出来的点P
表示成公钥其实就是把加上前缀04并把x和y连接起来
很长很长有木有,一共是520bits (8 + 256 + 256),使用16进制字符串表示,要130个字节!!
而这么长的内容在网上传输来传输去,传输无数次是很浪费资源的,于是就有了压缩的公钥
为何能压缩
通过以前的学习和上边的分析,我们知道公钥是椭圆曲线上的一个点P(x, y)
而按照椭圆曲线的方程,我们是可以通过x求得y的,所以我们只要保留x部分就可以了
机制就是这么简单!可见数学有多么重要,可惜是我从小学起就没有好好学 😭
同学们千万不要学我!
如何去压缩
以下图示很好的说明了公钥压缩的流程
公钥压缩的示意图( Source: 《Mastering Bitcoin》)
比特币地址
好了,公钥压缩问题已经理解了。
我们继续来进行最后一步,生成比特币地址!!!!
私钥、公钥都有了, 但是要玩转,让别人给我打钱,必须要有地址啊
木有地址,怎么骗钱???, 怎么收钱?
公钥到比特币地址的示意图( Source: 《Mastering Bitcoin》)
《Mastering Bitcoin》好书啊,再次强烈推荐!!
有了这个公钥到比特币地址的流程,生成比特币地址就相当容易了。
从steem python库中扒来ripemd160函数,hashlib真强大啊
def ripemd160(s):
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(unhexlify(s))
return ripemd160.digest()
连扒再改弄个了生成地址的函数,请忽略变量命名之类的毛病
def get_address(public_key):
pkbin = unhexlify(public_key)
addressbin = ripemd160(hexlify(hashlib.sha256(pkbin).digest()))
address = base58CheckEncode(0x00, hexlify(addressbin).decode('ascii'))
return address
运行我们代码,导入之前生成的两组公钥,得到两个地址:
Address(C): 1LGWpj3pqbzYWvTLEcBKP6CS5PQkxBgjXj
Address(U): 14EvJWPvFNLnzBBzhm1AG9a5b6XiSBFs6R
验证
导入WIF(未压缩)
在blockchain.info 在线钱包,导入WIF(未压缩)
5K5CpQZtUvPqwk2UE1FAiWC1nPV1WYqMnEhMYy2WTiMz3J1u9nm
再导出私钥,显示的地址是未压缩的,但是私钥自动给显示成WIF-Compressed了
Address(U): 14EvJWPvFNLnzBBzhm1AG9a5b6XiSBFs6R
导入WIF-Compressed
在blockchain.info 在线钱包,导入WIF-Compressed
L2mZAF2AEEfq2cuVAQ5XtBqcTLEdFRWSifT7i6Hf2uZckefDJfp5
再导出私钥,显示的地址是压缩的,私钥显示和我们导入的相同
Address(C): 1LGWpj3pqbzYWvTLEcBKP6CS5PQkxBgjXj
不同的私钥(压缩、未压缩) 对应不同的地址
由此可见,不同的私钥(压缩或者未压缩)对应不同的比特币地址,和我们程序中生成的地址结果一样
钱包里虽然把私钥都作为WIF-Compressed显示,但是对于压缩和未压缩的,是区别对待的。
关于压缩私钥,压缩地址,以及压缩公钥
无论是压缩私钥还是压缩地址,地址和私钥本身是不压缩的,只是代表对应的公钥是压缩的而已
总结
- 公钥压缩可以节省空间
- 公钥压缩的原理: 对应椭圆曲线上的点P(x, y) , 可以通过x求得点y
- 公钥压缩的实现,y是偶数用02做前缀,奇数03,接上x的16进制字符串表示
- 比特币地址由公钥按固定流程生成
- 压缩公钥生成对应压缩公钥的地址
- 未压缩公钥生成对应未压缩公钥的地址
- blockchain.info 导入相同私钥的WIF, WIF-Compressed,生成两个地址
- 地址和私钥本身是不压缩的,只是代表对应的公钥是压缩的而已
疑问,两个地址对应的是一个私钥(256bit随机数),那么我向一个地址打一笔钱过去,是两个地址同时显示有钱,还是只是我打钱的地址有钱呢?
我一会用我生成的这俩地址打一个比特币过去测试一下,你们别乱动啊。😭
今天就探索到这里。
免责声明,本文为个人理解,示例仅供参考
因使用文中代码或地址造成的损失,概不负责!
又一篇超級專業的文章!😆
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
过奖了,我也在学习和探索
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
嗯嗯,大家都加油!!>V<
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Good....
Upvote & follow me....
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
done, pls follow me too
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
great post,awesome work
thanks for sharing the info.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
请教一下,根据圆锥曲线y^2=x^3+7,一个确定的x不是应该生成两个y值吗(y1=-y2),那这个pubkey压缩的时候应该会有信息损失吧?不知道比特币系统是怎么解决的?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
nice post...upvoted and followed ..... please me too
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thank you for this useful info and article.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You are welcome! 😄
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
it's good that steemit can display translated page immediately , good to learn many things along the way..
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
學習了,多謝O哥。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
😄 互相学习
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
哥 下篇探讨下荣耀地址生成呗
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
荣耀地址是啥?😄 华为的荣耀吗?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
就是比特币地址靓号
有的人比特币地址是88888888888jdjdj 这种
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
牛
我第一次听说,了不起
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
nice lolo
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
内容太高级了, 不是很看得懂,反正觉得不错!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
谢谢夸奖 😄
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Very informative thanks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
我翻译了你的文章是出于好奇,我不后悔,有你在比特币做的事情,因为我从来没有想到😊。感谢您对这篇文章我是法国人,我看所有的新闻比特币。我给予好评的文章✌
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
谢谢
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
好专业啊!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
见笑了 😄
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
pls sir/ma @oflyhigh, pls i am greatly in need of your help.
my goal was jeopardize by @adm @idx @blacklist-a by flagging my post. pls i seek ur help sir/ma to pls help me out in any way you can help me
i will be very greatful
pls i need your reply
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I'm sorry there is nothing I can do to help.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I love Bitcoins!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
看不懂也要看,希望有一天能看懂。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
其实没啥用的啦,哈哈
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
能学一点是一点,咱是终身学习者。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
A very professional article to describe Bitcoin address
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
每天一小步,人生一大步。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
是的
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hello! I just upvoted you! I help new Steemit members! Upvote this comment and follow me! i will upvote your future posts! To any other visitor, upvote this post also to receive free UpVotes from me! Happy SteemIt!https://steemit.com/steemit/@rakiblove/today-529-75-profit-sativacoin-stv-cryptocurrency
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations! This post has been upvoted (100%) and reestem from the new curation trail @animus,
The goal is to help Steemit grow by supporting Minnows and creating a social network. It's a completely free and you can join https://streemian.com/profile/curationtrail/trailing/691 or delegate steem power (SP) to @animus
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
牛。反正看不懂。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
请在牛后边加个毛
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hello! I just upvoted you! I help new Steemit members! Upvote this comment and follow me! i will upvote your future posts! To any other visitor, upvote this post also to receive free UpVotes from me! Happy SteemIt!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey can you help me quickly ! A man give my real identity https://steemit.com/suisse/@monkrypto/on-me-downvote-sans-raison-2017730t829020z
and he DownVote all of my post... his name's Monkyy
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
hello, your post for I get it. I like it,I vote it
but no language
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit