test: add more secp256k1 test vectors (#14756)
This commit is contained in:
parent
70c0775f0f
commit
0081563092
@ -27,12 +27,137 @@ type keyData struct {
|
||||
addr string
|
||||
}
|
||||
|
||||
/*
|
||||
The following code snippet has been used to generate test vectors. The purpose of these vectors are to check our
|
||||
implementation of secp256k1 against go-ethereum's one. It has been commented to avoid dependencies.
|
||||
|
||||
github.com/btcsuite/btcutil v1.0.2
|
||||
github.com/ethereum/go-ethereum v1.10.26
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
|
||||
|
||||
---
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/btcsuite/btcutil/base58"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"golang.org/x/crypto/ripemd160"
|
||||
)
|
||||
|
||||
func ethereumKeys() keyData {
|
||||
// Generate private key with the go-ethereum
|
||||
priv, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
encPriv := make([]byte, len(priv.D.Bytes())*2)
|
||||
hex.Encode(encPriv, priv.D.Bytes())
|
||||
|
||||
// Get go-ethereum public key
|
||||
ethPub, ok := priv.Public().(*ecdsa.PublicKey)
|
||||
if !ok {
|
||||
panic(err)
|
||||
}
|
||||
ethPublicKeyBytes := crypto.FromECDSAPub(ethPub)
|
||||
|
||||
// Format byte depending on the oddness of the Y coordinate.
|
||||
format := 0x02
|
||||
if ethPub.Y.Bit(0) != 0 {
|
||||
format = 0x03
|
||||
}
|
||||
|
||||
// Public key in the 33-byte compressed format.
|
||||
pub := ethPublicKeyBytes[:33]
|
||||
encPub := make([]byte, len(pub)*2)
|
||||
pub[0] = byte(format)
|
||||
hex.Encode(encPub, pub)
|
||||
|
||||
// Bitcoin style addresses
|
||||
sha := sha256.Sum256(pub)
|
||||
hasherRIPEMD160 := ripemd160.New()
|
||||
hasherRIPEMD160.Write(sha[:])
|
||||
addr := hasherRIPEMD160.Sum(nil)
|
||||
return keyData{
|
||||
priv: string(encPriv),
|
||||
pub: string(encPub),
|
||||
addr: base58.CheckEncode(addr[:], 0),
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
generateKeyForCheckingConsistency was used to create test vectors that matches consistency against prior versions.
|
||||
Here are the specific versions used to generate the vectors.
|
||||
|
||||
github.com/cosmos/btcutil v1.0.5
|
||||
github.com/cosmos/cosmos-sdk v0.46.8
|
||||
*/
|
||||
func generateKeyForCheckingConsistency() keyData {
|
||||
priv := secp256k1.GenPrivKey()
|
||||
encPriv := make([]byte, len(priv.Key)*2)
|
||||
hex.Encode(encPriv, priv.Key)
|
||||
pub := priv.PubKey()
|
||||
encPub := make([]byte, len(pub.Bytes())*2)
|
||||
hex.Encode(encPub, pub.Bytes())
|
||||
addr := pub.Address()
|
||||
return keyData{
|
||||
priv: string(encPriv),
|
||||
pub: string(encPub),
|
||||
addr: base58.CheckEncode(addr, 0),
|
||||
}
|
||||
}
|
||||
|
||||
var secpDataTable = []keyData{
|
||||
{
|
||||
priv: "a96e62ed3955e65be32703f12d87b6b5cf26039ecfa948dc5107a495418e5330",
|
||||
pub: "02950e1cdfcb133d6024109fd489f734eeb4502418e538c28481f22bce276f248c",
|
||||
addr: "1CKZ9Nx4zgds8tU7nJHotKSDr4a9bYJCa3",
|
||||
},
|
||||
// matches consistency against a prior version of this library. Generated with generateKeyForCheckingConsistency
|
||||
{
|
||||
priv: "9af074dc32fe3e7173802cd72dcb1110582879a1990c90bdac60f2739986aa06",
|
||||
pub: "0285592121e2a5e0eb970a1a9d1879c5fa7b33badf7dbb61c44b1bfced94649efb",
|
||||
addr: "1Q4mWVk2hotRVDEdGGtGf6waz622rEwvib",
|
||||
},
|
||||
// matches consistency against a prior version of this library. Generated with generateKeyForCheckingConsistency
|
||||
{
|
||||
priv: "ef9edc836bc4d47e9bc3cfab446836a737c41d60abb1d5f76a6d53ffe5b35f76",
|
||||
pub: "02f5bf88d72172cc2f9a52919b6b1b74a01ca606cad75d5f4f93aa1a6ff0374aaf",
|
||||
addr: "1KtiSApteeKdLi5cdZVpnkNW1t5Eteksvf",
|
||||
},
|
||||
// matches consistency against a prior version of this library. Generated with generateKeyForCheckingConsistency
|
||||
{
|
||||
priv: "ab7715a1dd7cea7898c45b1f291550b83a6897fbdf0ec48330dd50187059b74b",
|
||||
pub: "028f3003b3e6cb40897138dba5858207357a6d116cc5bf556c942cf6081b58d5fe",
|
||||
addr: "RnM1o5grgCHAmm45wt5vzGsQoCJdPK2n2",
|
||||
},
|
||||
// matches consistency against a prior version of this library. Generated with generateKeyForCheckingConsistency
|
||||
{
|
||||
priv: "db6b914d9a2d6ae4bab8f9b43de3b1e83940e1a309521128b13fdaf3cd15009a",
|
||||
pub: "022f8e4e07ae2705a3c425eafea16027041bcdc87a193b01ea6c36c1c7a0bfc300",
|
||||
addr: "16MpKTksSpGABuHqMqU9RPBz26DfwY8cLY",
|
||||
},
|
||||
// matches consistency against go-ethereum's implementation. Generated with ethereumKeys
|
||||
{
|
||||
priv: "42ba4249f6fd9f1e31f8876a8d3d3bdef989fcc906164290c0be237f69f53718",
|
||||
pub: "033c2f6ea7a678f0afbb43d0fe7a2b2706a75c2fdea08c3b90fd038c8219b42959",
|
||||
addr: "18iz5wdTdwzq6cGzoVhooZCPRAx61GfUMR",
|
||||
},
|
||||
// matches consistency against go-ethereum's implementation. Generated with ethereumKeys
|
||||
{
|
||||
priv: "86192b60369616574daabe8d7d6067f14ec3f0648cded5633c566c25c48e1f31",
|
||||
pub: "03ad9e97842d0f6f57804f29f55aac9bba207d2b24b98aaabc7d106250389e6d46",
|
||||
addr: "1K31NqmdMBZiLeUiP4kfjLNnWSmx17a9aE",
|
||||
},
|
||||
// matches consistency against go-ethereum's implementation. Generated with ethereumKeys
|
||||
{
|
||||
priv: "1856b3a581aa1bf83daf61b1f8f4bb52b5223033f710e61d7e0b3086f48ba09a",
|
||||
pub: "03d681bb11e5ebc14d5d2f72881cb0b2a693ef12bc72fe863f980fc6542eafbd40",
|
||||
addr: "1K29nsfH6qwmE3MzsoHpLcWLA4mQLstGgx",
|
||||
},
|
||||
}
|
||||
|
||||
func TestPubKeySecp256k1Address(t *testing.T) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user