forked from tendermint/tendermint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make sure we create valid private keys: - genPrivKey samples and rejects invalid fieldelems (like libsecp256k1) - GenPrivKeySecp256k1 uses `(sha(secret) mod (n − 1)) + 1` - fix typo, rename test file: s/secpk256k1/secp256k1/ * Update crypto/secp256k1/secp256k1.go
- Loading branch information
Showing
5 changed files
with
151 additions
and
12 deletions.
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
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,39 @@ | ||
// +build libsecp256k1 | ||
|
||
package secp256k1 | ||
|
||
import ( | ||
"github.com/magiconair/properties/assert" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPrivKeySecp256k1SignVerify(t *testing.T) { | ||
msg := []byte("A.1.2 ECC Key Pair Generation by Testing Candidates") | ||
priv := GenPrivKey() | ||
tests := []struct { | ||
name string | ||
privKey PrivKeySecp256k1 | ||
wantSignErr bool | ||
wantVerifyPasses bool | ||
}{ | ||
{name: "valid sign-verify round", privKey: priv, wantSignErr: false, wantVerifyPasses: true}, | ||
{name: "invalid private key", privKey: [32]byte{}, wantSignErr: true, wantVerifyPasses: false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.privKey.Sign(msg) | ||
if tt.wantSignErr { | ||
require.Error(t, err) | ||
t.Logf("Got error: %s", err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.NotNil(t, got) | ||
|
||
pub := tt.privKey.PubKey() | ||
assert.Equal(t, tt.wantVerifyPasses, pub.VerifyBytes(msg, got)) | ||
}) | ||
} | ||
} |
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,45 @@ | ||
package secp256k1 | ||
|
||
import ( | ||
"bytes" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
underlyingSecp256k1 "github.com/btcsuite/btcd/btcec" | ||
) | ||
|
||
func Test_genPrivKey(t *testing.T) { | ||
|
||
empty := make([]byte, 32) | ||
oneB := big.NewInt(1).Bytes() | ||
onePadded := make([]byte, 32) | ||
copy(onePadded[32-len(oneB):32], oneB) | ||
t.Logf("one padded: %v, len=%v", onePadded, len(onePadded)) | ||
|
||
validOne := append(empty, onePadded...) | ||
tests := []struct { | ||
name string | ||
notSoRand []byte | ||
shouldPanic bool | ||
}{ | ||
{"empty bytes (panics because 1st 32 bytes are zero and 0 is not a valid field element)", empty, true}, | ||
{"curve order: N", underlyingSecp256k1.S256().N.Bytes(), true}, | ||
{"valid because 0 < 1 < N", validOne, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.shouldPanic { | ||
require.Panics(t, func() { | ||
genPrivKey(bytes.NewReader(tt.notSoRand)) | ||
}) | ||
return | ||
} | ||
got := genPrivKey(bytes.NewReader(tt.notSoRand)) | ||
fe := new(big.Int).SetBytes(got[:]) | ||
require.True(t, fe.Cmp(underlyingSecp256k1.S256().N) < 0) | ||
require.True(t, fe.Sign() > 0) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ import ( | |
"testing" | ||
|
||
secp256k1 "github.com/btcsuite/btcd/btcec" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
|
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