package btcec

package:btcec 

github.com/btcsuite/btcd/btcec

btcec实现了对比特币所需的椭圆曲线elliptic curve的支持。比特币使用椭圆曲线密码using koblitz curves(特别是secp256k1)进行加密。

这个包提供了数据结构、实现密码/椭圆曲线接口的方法,这样可以使用标准的crypto/ecdsa包操作这些曲线。此外,还提供了用于从标准格式解析签名和公钥的功能。它是专为btcd设计的,但也可以用于其他椭圆曲线密码。

btcec.go

提供了基于Jacobian坐标系的操作。对于在曲线上给的一个点(x,y),在Jacobian坐标系表示为(x1, y1, z1),其中x = x1/z1²,y=y1/z1³.

//实现了一个可以与crypto/elliptic中的ECC曲线接口适配的koblitz曲线
type KoblitzCurve struct

ciphering.go

//使用Diffie-Hellman密钥交换(ECDH【RFC4753】)方法【RFC5903中陈述仅返回X】,给定私钥和公钥生成一个共享secret
func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte

//使用AES-256-CBC进行加密
func Encrypt(pubkey *PublicKey, in []byte) ([]byte, error)
func Decrypt(priv *PrivateKey, in []byte) ([]byte, error)

signature.go

//对ecdsa signature的封装
type Signature struct {
    R *big.Int
    S *big.Int
}

//调用ecdsa.Verify进行验证。使用公钥验证哈希的签名。如果签名有效,则返回true,否则为false。
func (sig *Signature) Verify(hash []byte, pubKey *PublicKey) bool
//从[]byte生成Signature对象
func parseSig(sigStr []byte, curve elliptic.Curve, der bool) (*Signature, error)

//在给定的消息散列“msg”上,recoverKeyFromSignature从签名“sig”中恢复了一个公钥
 
 
func recoverKeyFromSignature(curve *KoblitzCurve, sig *Signature, msg []byte, iter int, doChecks bool) (*PublicKey, error)

//这个方法可以产生一个紧凑(compact)的签名
func SignCompact(curve *KoblitzCurve, key *PrivateKey,
    hash []byte, isCompressedKey bool) ([]byte, error)

func RecoverCompact(curve *KoblitzCurve, signature, hash []byte) (*PublicKey, bool, error)

//根据RFC6979和bip62生成一个确定性的ECDSA签名。
func signRFC6979(privateKey *PrivateKey, hash []byte) (*Signature, error)

pubkey.go

对ecdsa.PublicKey的封装

//serialized 公钥的长度值
const (
    PubKeyBytesLenCompressed   = 33
    PubKeyBytesLenUncompressed = 65
    PubKeyBytesLenHybrid       = 65 //混合
)

//给定曲线curve及曲线上的一个点x 
func decompressPoint(curve *KoblitzCurve, x *big.Int, ybit bool) (*big.Int, error)

//基于koblitz曲线从一个bytestring pubKeyStr解析得到ecdsa.PublicKey,验证它是否有效。它支持压缩的、未压缩的和混合的签名格式。
func ParsePubKey(pubKeyStr []byte, curve *KoblitzCurve) (key *PublicKey, err error)
//一个例子

key: []byte{0x06, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb,
            0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07,
            0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59,
            0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
            0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb,
            0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17, 0xb4, 0x48,
            0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47, 0xd0, 0x8f, 0xfb,
            0x10, 0xd4, 0xb8,
        },
pk, err := ParsePubKey(key, S256()) //pk-PublicKey

privkey.go

生成私钥的方法及对ecdsa.PrivateKey进行封装。

相关函数:

//返回一个基于“曲线”的私钥和公钥,pk:private key

func PrivKeyFromBytes(curve elliptic.Curve, pk []byte) (*PrivateKey, *PublicKey)

//对ecdsa.GenerateKey进行包装,返回私钥

func NewPrivateKey(curve elliptic.Curve) (*PrivateKey, error)

使用私钥签名,为所提供的hash散列生成一个ECDSA签名。生成的签名是确定的(相同的消息和相同的私钥产生相同的签名),规范参照RFC6979和BIP0062。

func (p *PrivateKey) Sign(hash []byte) (*Signature, error) {
    return signRFC6979(p, hash)
}

将私钥序列化为一个大端big-endian二进制编码的数字,其长度为32字节。

func (p *PrivateKey) Serialize() []byte


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值