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

btcec: add deep copy method for public keys #2288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ziggie1984
Copy link
Contributor

@ziggie1984 ziggie1984 commented Dec 10, 2024

We use a lot of pointers to the PublicKey type, so it comes very handy to have a deep copy method available.

@coveralls
Copy link

coveralls commented Dec 10, 2024

Pull Request Test Coverage Report for Build 12273833480

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.004%) to 57.284%

Files with Coverage Reduction New Missed Lines %
peer/peer.go 1 74.52%
Totals Coverage Status
Change from base Build 12253655920: 0.004%
Covered Lines: 29917
Relevant Lines: 52226

💛 - Coveralls

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

btcec/pubkey.go Outdated
@@ -52,6 +52,13 @@ func NewPublicKey(x, y *FieldVal) *PublicKey {
return secp.NewPublicKey(x, y)
}

// CopyPublicKey returns a deep copy of the public key.
func CopyPublicKey(p *PublicKey) *PublicKey {
pk := secp.JacobianPoint{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use var pk secp.JacobianPoint instead?

Copy link
Member

@Roasbeef Roasbeef Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to copy things this way. You need to call ToAffine when coming back from the Jacobian coords.

Here's some code examples:

After computations, we always transport/handle in affine form.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a property based test here. I wager it would've caught this issue.

The property we care about here is: pub == copy(pub).

If you look at what ToAffine does, it actually will arrive at new values for x and y, as it's a distinct coordinate system: https://github.com/decred/dcrd/blob/08d8572807872f2b9737f8a118b16c320a04b077/dcrec/secp256k1/curve.go#L136-L149

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same about ToAffine(), but since we use AsJacobian() right before that, those should be affine coordinates already or does that no hold?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would still work because we are not doing any operations on it so z is set to 1 anyways and Affine and Jacobian are the same coordinates but technically it's wrong but it just happens to work for this particular cpy method. Will correct it.

btcec/pubkey.go Outdated
@@ -52,6 +52,13 @@ func NewPublicKey(x, y *FieldVal) *PublicKey {
return secp.NewPublicKey(x, y)
}

// CopyPublicKey returns a deep copy of the public key.
func CopyPublicKey(p *PublicKey) *PublicKey {
pk := secp.JacobianPoint{}
Copy link
Member

@Roasbeef Roasbeef Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to copy things this way. You need to call ToAffine when coming back from the Jacobian coords.

Here's some code examples:

After computations, we always transport/handle in affine form.

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, LGTM 🎉

btcec/pubkey_test.go Outdated Show resolved Hide resolved
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.

4 participants