Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more secp256k1 test vectors #14756

Merged
merged 4 commits into from
Feb 9, 2023

Conversation

JulianToledano
Copy link
Contributor

Description

Closes:
#11343


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@JulianToledano JulianToledano marked this pull request as ready for review January 24, 2023 16:13
@JulianToledano JulianToledano requested a review from a team as a code owner January 24, 2023 16:13
@julienrbrt julienrbrt linked an issue Jan 24, 2023 that may be closed by this pull request
4 tasks
@github-prbot github-prbot requested review from a team, kocubinski and testinginprod and removed request for a team January 24, 2023 16:28
@tac0turtle
Copy link
Member

Would like @ValarDragon to sign off before merge

@ValarDragon
Copy link
Contributor

How are these vectors generated?

Whats something I should use to verify them? Happy to get a sage script going for it

@JulianToledano
Copy link
Contributor Author

How are these vectors generated?

Whats something I should use to verify them? Happy to get a sage script going for it

Those were generated with secp256k1.GenPrivKey().
This is the script used:

import (
	"encoding/hex"
	"fmt"
	"github.com/cosmos/btcutil/base58"
	"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
)

func main() {
	priv := secp256k1.GenPrivKey()
	encPriv := make([]byte, len(priv.Key)*2)
	hex.Encode(encPriv, priv.Key)
	fmt.Println(fmt.Sprintf("PrivKey: %v", string(encPriv)))
	pub := priv.PubKey()
	encPub := make([]byte, len(pub.Bytes())*2)
	hex.Encode(encPub, pub.Bytes())
	fmt.Println(fmt.Sprintf("PubKey: %v", string(encPub)))
	addr := pub.Address()
	fmt.Println(fmt.Sprintf("Address: %v", base58.CheckEncode(addr, 0)))
}

@tac0turtle
Copy link
Member

can we add a test or small function to do this in the code? that way if anything changes it's easy to fix it

@ValarDragon
Copy link
Contributor

Hrmm, ok.

However I personally wouldn't mark the issue as closed (or would make a second issue) for test vectors then.
These test vectors are then guaranteeing this code is deterministic / hasn't changed, but not guaranteeing that it matches any other implementation.
We should add a code comment on top of every test vector (or set of vectors), explaining how it is sourced. In this case, "matches consistency against a prior version of this library"

@JulianToledano
Copy link
Contributor Author

JulianToledano commented Jan 26, 2023

@ValarDragon would be enough to generate some test cases with go-ethereum? I have the following code snippet:

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() {
	// 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())
	fmt.Println(fmt.Sprintf("PrivKey: %v", string(encPriv)))

	// 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)
	fmt.Println(fmt.Sprintf("PubKey: %v", string(encPub)))

	//  Bitcoin style addresses
	sha := sha256.Sum256(pub)
	hasherRIPEMD160 := ripemd160.New()
	hasherRIPEMD160.Write(sha[:])
	addr := hasherRIPEMD160.Sum(nil)
	fmt.Println(fmt.Sprintf("Address: %v", base58.CheckEncode(addr[:], 0)))
}

@tac0turtle
Copy link
Member

Since we can rely on go-Ethereum as a dep we should comment out the test. But seems sufficient

@JulianToledano
Copy link
Contributor Author

go-ethereum test vectors added

@ainhoa-a ainhoa-a mentioned this pull request Feb 2, 2023
12 tasks
@JulianToledano
Copy link
Contributor Author

@tac0turtle did you have a chance to look at the last commit?

@tac0turtle
Copy link
Member

yes, but we cant update the pr in order to merge. Need you to update for us to merge

@tac0turtle tac0turtle enabled auto-merge (squash) February 9, 2023 12:41
@tac0turtle tac0turtle merged commit 0081563 into cosmos:main Feb 9, 2023
@JulianToledano JulianToledano deleted the feat/more-secp256k1-testvectors branch February 9, 2023 18:01
tsenart pushed a commit to meka-dev/cosmos-sdk that referenced this pull request Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add more test vectors to secp256k1 cgo
6 participants