-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Migrate Tendermint PubKey types to the SDK #7047
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
73f7486
Move crypto packages from tm
sahith-narahari 1d3714f
Revert refactor changes
sahith-narahari aa40ce0
Fix lint
sahith-narahari eddbd66
Move to crypto/keys
sahith-narahari 7b6339e
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/mig…
sahith-narahari 70796ee
Update to tm 0.34-rc3
sahith-narahari 61e50a3
Update benchmark tests
sahith-narahari 0ed7dcb
Fix tests
sahith-narahari 0036cb2
Merge branch 'master' into sahith/migrate-tm-keys
sahith-narahari 9412451
Merge branch 'master' into sahith/migrate-tm-keys
aaronc bee04f6
Update to tm master
sahith-narahari 2c00861
Merge branch 'sahith/migrate-tm-keys' of github.com:cosmos/cosmos-sdk…
sahith-narahari 7a089a3
Merge branch 'master' into sahith/migrate-tm-keys
sahith-narahari 18dac82
Merge branch 'master' into sahith/migrate-tm-keys
mergify[bot] f9de5ed
Merge branch 'master' into sahith/migrate-tm-keys
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package benchmarking | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/tendermint/tendermint/crypto" | ||
) | ||
|
||
// The code in this file is adapted from agl/ed25519. | ||
// As such it is under the following license. | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found at the bottom of this file. | ||
|
||
type zeroReader struct{} | ||
|
||
func (zeroReader) Read(buf []byte) (int, error) { | ||
for i := range buf { | ||
buf[i] = 0 | ||
} | ||
return len(buf), nil | ||
} | ||
|
||
// BenchmarkKeyGeneration benchmarks the given key generation algorithm using | ||
// a dummy reader. | ||
func BenchmarkKeyGeneration(b *testing.B, generateKey func(reader io.Reader) crypto.PrivKey) { | ||
var zero zeroReader | ||
for i := 0; i < b.N; i++ { | ||
generateKey(zero) | ||
} | ||
} | ||
|
||
// BenchmarkSigning benchmarks the given signing algorithm using | ||
// the provided privkey. | ||
func BenchmarkSigning(b *testing.B, priv crypto.PrivKey) { | ||
message := []byte("Hello, world!") | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
priv.Sign(message) | ||
} | ||
} | ||
|
||
// BenchmarkVerification benchmarks the given verification algorithm using | ||
// the provided privkey on a constant message. | ||
func BenchmarkVerification(b *testing.B, priv crypto.PrivKey) { | ||
pub := priv.PubKey() | ||
// use a short message, so this time doesn't get dominated by hashing. | ||
message := []byte("Hello, world!") | ||
signature, err := priv.Sign(message) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
pub.VerifyBytes(message, signature) | ||
} | ||
} | ||
|
||
// Below is the aforementioned license. | ||
|
||
// Copyright (c) 2012 The Go Authors. All rights reserved. | ||
|
||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are | ||
// met: | ||
|
||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following disclaimer | ||
// in the documentation and/or other materials provided with the | ||
// distribution. | ||
// * Neither the name of Google Inc. nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
|
||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ed25519 | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/tendermint/tendermint/crypto" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/benchmarking" | ||
) | ||
|
||
func BenchmarkKeyGeneration(b *testing.B) { | ||
benchmarkKeygenWrapper := func(reader io.Reader) crypto.PrivKey { | ||
return genPrivKey(reader) | ||
} | ||
benchmarking.BenchmarkKeyGeneration(b, benchmarkKeygenWrapper) | ||
} | ||
|
||
func BenchmarkSigning(b *testing.B) { | ||
priv := GenPrivKey() | ||
benchmarking.BenchmarkSigning(b, priv) | ||
} | ||
|
||
func BenchmarkVerification(b *testing.B) { | ||
priv := GenPrivKey() | ||
benchmarking.BenchmarkVerification(b, priv) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package ed25519 | ||
|
||
import ( | ||
"bytes" | ||
"crypto/subtle" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/tendermint/go-amino" | ||
"golang.org/x/crypto/ed25519" | ||
|
||
"github.com/tendermint/tendermint/crypto" | ||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
) | ||
|
||
// ------------------------------------- | ||
|
||
var _ crypto.PrivKey = PrivKeyEd25519{} | ||
|
||
const ( | ||
PrivKeyAminoName = "tendermint/PrivKeyEd25519" | ||
PubKeyAminoName = "tendermint/PubKeyEd25519" | ||
// Size of an Edwards25519 signature. Namely the size of a compressed | ||
// Edwards25519 point, and a field element. Both of which are 32 bytes. | ||
SignatureSize = 64 | ||
) | ||
|
||
var cdc = amino.NewCodec() | ||
|
||
func init() { | ||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil) | ||
cdc.RegisterConcrete(PubKeyEd25519{}, | ||
PubKeyAminoName, nil) | ||
|
||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil) | ||
cdc.RegisterConcrete(PrivKeyEd25519{}, | ||
PrivKeyAminoName, nil) | ||
} | ||
|
||
// PrivKeyEd25519 implements crypto.PrivKey. | ||
type PrivKeyEd25519 [64]byte | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you will not want sized byte arrays in the sdk, this was discussed in an issue, cant recall which. the current tendermint impl uses unsized to make it easier but this does bring an extra need for carfulness. |
||
|
||
// Bytes marshals the privkey using amino encoding. | ||
func (privKey PrivKeyEd25519) Bytes() []byte { | ||
return cdc.MustMarshalBinaryBare(privKey) | ||
} | ||
|
||
// Sign produces a signature on the provided message. | ||
// This assumes the privkey is wellformed in the golang format. | ||
// The first 32 bytes should be random, | ||
// corresponding to the normal ed25519 private key. | ||
// The latter 32 bytes should be the compressed public key. | ||
// If these conditions aren't met, Sign will panic or produce an | ||
// incorrect signature. | ||
func (privKey PrivKeyEd25519) Sign(msg []byte) ([]byte, error) { | ||
signatureBytes := ed25519.Sign(privKey[:], msg) | ||
return signatureBytes, nil | ||
} | ||
|
||
// PubKey gets the corresponding public key from the private key. | ||
func (privKey PrivKeyEd25519) PubKey() crypto.PubKey { | ||
privKeyBytes := [64]byte(privKey) | ||
initialized := false | ||
// If the latter 32 bytes of the privkey are all zero, compute the pubkey | ||
// otherwise privkey is initialized and we can use the cached value inside | ||
// of the private key. | ||
for _, v := range privKeyBytes[32:] { | ||
if v != 0 { | ||
initialized = true | ||
break | ||
} | ||
} | ||
|
||
if !initialized { | ||
panic("Expected PrivKeyEd25519 to include concatenated pubkey bytes") | ||
} | ||
|
||
var pubkeyBytes [PubKeyEd25519Size]byte | ||
copy(pubkeyBytes[:], privKeyBytes[32:]) | ||
return PubKeyEd25519(pubkeyBytes) | ||
} | ||
|
||
// Equals - you probably don't need to use this. | ||
// Runs in constant time based on length of the keys. | ||
func (privKey PrivKeyEd25519) Equals(other crypto.PrivKey) bool { | ||
if otherEd, ok := other.(PrivKeyEd25519); ok { | ||
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1 | ||
} | ||
|
||
return false | ||
} | ||
|
||
// GenPrivKey generates a new ed25519 private key. | ||
// It uses OS randomness in conjunction with the current global random seed | ||
// in tendermint/libs/common to generate the private key. | ||
func GenPrivKey() PrivKeyEd25519 { | ||
return genPrivKey(crypto.CReader()) | ||
} | ||
|
||
// genPrivKey generates a new ed25519 private key using the provided reader. | ||
func genPrivKey(rand io.Reader) PrivKeyEd25519 { | ||
seed := make([]byte, 32) | ||
_, err := io.ReadFull(rand, seed) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
privKey := ed25519.NewKeyFromSeed(seed) | ||
var privKeyEd PrivKeyEd25519 | ||
copy(privKeyEd[:], privKey) | ||
return privKeyEd | ||
} | ||
|
||
// GenPrivKeyFromSecret hashes the secret with SHA2, and uses | ||
// that 32 byte output to create the private key. | ||
// NOTE: secret should be the output of a KDF like bcrypt, | ||
// if it's derived from user input. | ||
func GenPrivKeyFromSecret(secret []byte) PrivKeyEd25519 { | ||
seed := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes. | ||
|
||
privKey := ed25519.NewKeyFromSeed(seed) | ||
var privKeyEd PrivKeyEd25519 | ||
copy(privKeyEd[:], privKey) | ||
return privKeyEd | ||
} | ||
|
||
// ------------------------------------- | ||
|
||
var _ crypto.PubKey = PubKeyEd25519{} | ||
|
||
// PubKeyEd25519Size is the number of bytes in an Ed25519 signature. | ||
const PubKeyEd25519Size = 32 | ||
|
||
// PubKeyEd25519 implements crypto.PubKey for the Ed25519 signature scheme. | ||
type PubKeyEd25519 [PubKeyEd25519Size]byte | ||
|
||
// Address is the SHA256-20 of the raw pubkey bytes. | ||
func (pubKey PubKeyEd25519) Address() crypto.Address { | ||
return tmhash.SumTruncated(pubKey[:]) | ||
} | ||
|
||
// Bytes marshals the PubKey using amino encoding. | ||
func (pubKey PubKeyEd25519) Bytes() []byte { | ||
bz, err := cdc.MarshalBinaryBare(pubKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return bz | ||
} | ||
|
||
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig []byte) bool { | ||
// make sure we use the same algorithm to sign | ||
if len(sig) != SignatureSize { | ||
return false | ||
} | ||
return ed25519.Verify(pubKey[:], msg, sig) | ||
} | ||
|
||
func (pubKey PubKeyEd25519) String() string { | ||
return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:]) | ||
} | ||
|
||
func (pubKey PubKeyEd25519) Equals(other crypto.PubKey) bool { | ||
if otherEd, ok := other.(PubKeyEd25519); ok { | ||
return bytes.Equal(pubKey[:], otherEd[:]) | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ed25519_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/tendermint/tendermint/crypto" | ||
"github.com/tendermint/tendermint/crypto/ed25519" | ||
) | ||
|
||
func TestSignAndValidateEd25519(t *testing.T) { | ||
|
||
privKey := ed25519.GenPrivKey() | ||
pubKey := privKey.PubKey() | ||
|
||
msg := crypto.CRandBytes(128) | ||
sig, err := privKey.Sign(msg) | ||
require.Nil(t, err) | ||
|
||
// Test the signature | ||
assert.True(t, pubKey.VerifyBytes(msg, sig)) | ||
|
||
// Mutate the signature, just one bit. | ||
// TODO: Replace this with a much better fuzzer, tendermint/ed25519/issues/10 | ||
sig[7] ^= byte(0x01) | ||
|
||
assert.False(t, pubKey.VerifyBytes(msg, sig)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package secp256k1 | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/tendermint/tendermint/crypto" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/benchmarking" | ||
) | ||
|
||
func BenchmarkKeyGeneration(b *testing.B) { | ||
benchmarkKeygenWrapper := func(reader io.Reader) crypto.PrivKey { | ||
return genPrivKey(reader) | ||
} | ||
benchmarking.BenchmarkKeyGeneration(b, benchmarkKeygenWrapper) | ||
} | ||
|
||
func BenchmarkSigning(b *testing.B) { | ||
priv := GenPrivKey() | ||
benchmarking.BenchmarkSigning(b, priv) | ||
} | ||
|
||
func BenchmarkVerification(b *testing.B) { | ||
priv := GenPrivKey() | ||
benchmarking.BenchmarkVerification(b, priv) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe | ||
|
||
*~ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crypto/
already has some folders (hd, keyring, ledger). Would it make more sense to put all these files intocrypto/pubkey/
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crypto/(key, keys)
because it houses private keys as wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't have a preference here, either way seems fine to me. Will move to
crypto/keys
if that seems betterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer
keys
thankey
.