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

Use embedded lib key type in sdk pub keys instead of bytes. #7672

Merged
merged 9 commits into from
Oct 28, 2020

Conversation

robert-zaremba
Copy link
Collaborator

@robert-zaremba robert-zaremba commented Oct 26, 2020

Description

When doing the conversion of validators public key from string to Any I noticed that we are doing unnecessary memory allocation for ed25519 keys and we can simplify by using the ed25519.PrivateKey directly in protobuf ( ed25519.PrivateKey = []byte).

This changeset:

  • adds cross library signature tests for ed25519
  • adds cross library signature tests for secp256k1 (note: signature validation was not simple - requires assumption about signature)
  • uses ed25519 from stdlib:
    In Go 1.13 the new crypto/ed25519 package implements the Ed25519 signature scheme.
    This functionality was previously provided by the golang.org/x/crypto/ed25519 package,
    which becomes a wrapper for crypto/ed25519 when used with Go 1.13+.
  • uses ed25519.PrivateKey direactly as "our" private key reducing the code (by delegating directly to the stdlib code!) an unnecessary memory allocations.

closes: #7643


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer
  • Review Codecov Report in the comment section below once CI passes

@codecov
Copy link

codecov bot commented Oct 26, 2020

Codecov Report

Merging #7672 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #7672   +/-   ##
=======================================
  Coverage   54.10%   54.11%           
=======================================
  Files         611      611           
  Lines       38571    38571           
=======================================
+ Hits        20869    20871    +2     
+ Misses      15574    15573    -1     
+ Partials     2128     2127    -1     

@@ -1,6 +1,7 @@
package ed25519_test
package ed25519
Copy link
Contributor

Choose a reason for hiding this comment

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

why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

to avoid weird package renames. It simplifies reading the code.
Also, we only have unit tests here. Using ed25519_test is good for integration tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a weird package rename. It actually helps the reader to have _test packages marked consistently.

is good for integration tests.

Why? How? In which cases?

Please revert this unnecesary change.

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW there are plenty of good reasons to hide internals when testing Go packages, that's why _test packages are idiomatic in Go. We've made efforts in the past to make tests live within their respective _test packages. This is a step backwards.

Copy link
Collaborator Author

@robert-zaremba robert-zaremba Oct 26, 2020

Choose a reason for hiding this comment

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

Testing in same package is the Go idiomatic way. The go test roughly mentions about _test package. The testing godoc doesn't mention it at all, same go wiki - learn testing. Go code review guidelines mention _test packages only in one place - to use it to avoid circular dependencies - they suggest to use dot import for that.

In all projects I was working (many) there was a preference to test in the package (not _test). Personally I'm advocating to use use _test packages for integration tests to have better feeling of a black box testing. This also gives a better separation between unit tests and other tests.

Copy link
Contributor

@alexanderbez alexanderbez Oct 27, 2020

Choose a reason for hiding this comment

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

It's not about which projects use and don't use _test. We should strive to use when possible for better isolation. If the change isn't necessary, then we shouldn't change it.

Copy link
Collaborator Author

@robert-zaremba robert-zaremba Oct 27, 2020

Choose a reason for hiding this comment

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

I wanted to point out that it's a usual Go way for unit tests.
So, how about this:

@alessio , @aaronc - If you prefer to keep the _test name, how about using the dot import (ref: same comment above) to avoid this name clashes ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I wanted to point out that it's a usual Go way for unit tests.

And I would like to point out that:

Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary.

Per this piece of documentation here, that incidentally is hosted at tip.golang.org.

how about using the dot import

No, it adds no value, plus it would make the imported symbols unpredictable.

How about reverting this change?

Copy link
Collaborator Author

@robert-zaremba robert-zaremba Oct 27, 2020

Choose a reason for hiding this comment

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

Per this piece of documentation here, that incidentally is hosted at tip.golang.org.

I already linked go test docs (using the source file) in one of my comments above. I don't see any relevant change in tip. If there is any, would you mind to pointing it out?
NOTE: I compared, there is no change in tip

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How about reverting this change?

OK, my motivation was to use the more adopted way for Go unit tests. If this is not an SDK way, no problem, reverting.

@@ -1,14 +1,14 @@
package ed25519

import (
"crypto/ed25519"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good BTW

Copy link
Collaborator Author

@robert-zaremba robert-zaremba Oct 26, 2020

Choose a reason for hiding this comment

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

yes, I made a note in the commit. Here are the details: https://golang.org/doc/go1.13#crypto/ed25519
We shouldn't use golang.org/x/crypto/ed25519 package.

Copy link
Contributor

@alessio alessio left a comment

Choose a reason for hiding this comment

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

Few changes are requested.

@robert-zaremba
Copy link
Collaborator Author

robert-zaremba commented Oct 27, 2020

I've rolled back the package rename from ed25519 to ed25519_test.

Copy link
Contributor

@blushi blushi left a comment

Choose a reason for hiding this comment

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

The changes look ok to me but could you resolve merge conflicts?

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

the code lgtm, though I would prefer to approve once the tests pass.

crypto/keys/secp256k1/secp256k1_test.go Outdated Show resolved Hide resolved
fadeev and others added 8 commits October 28, 2020 10:46
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
In Go 1.13 the new crypto/ed25519 package implements the Ed25519 signature scheme.
This functionality was previously provided by the golang.org/x/crypto/ed25519 package,
which becomes a wrapper for crypto/ed25519 when used with Go 1.13+.
@robert-zaremba
Copy link
Collaborator Author

Tests were passing, but there were conflicts after the nights merge. Now solved.

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

utACK, thanks!

@robert-zaremba robert-zaremba added the A:automerge Automatically merge PR once all prerequisites pass. label Oct 28, 2020
@mergify mergify bot merged commit bd9af94 into master Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge Automatically merge PR once all prerequisites pass.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use embedded lib key type in sdk pub keys instead of bytes.
7 participants