cosmos-sdk/crypto/keys/secp256r1/privkey_internal_test.go

116 lines
2.7 KiB
Go
Raw Normal View History

crypto: add secp256r1 (#8559) * Optimize secp256k1 hashing * Add ADR-028 related functions * Update ed25519 * fix errors/handle * fix build * fix build * Add tests and update function names * wip * Use LengthPrefix for composed addresses * add tests for NewComposed * add module hash function * fix append * rollback ed25519 ADR-28 update * rollback ed25519 ADR-28 test * Adding Module tests and convert tests to test suite * convert store_key_test.go to test suite * rollback test check comment * any.pb.go update * generated proto files * wip * renames * wip2 * add String method to PBBytes * wip3 * add pubkey tests * adding cryptotypes.PrivKey methods * re-enable test * fix equals test * fix ecdsa object receiver * add ProtoMarshaler implementation and tests * move code to init and add interface registry * add bytes tests * merge Unmarshal with UnmarshalAmino * implement ProtoMarshaler to ecdsaSK * remove bytes.go * add private key marshaling tests * break tests into 2 suites * add signature tests * remove TODO * remove bytes.proto * adding changelog * Update CHANGELOG.md * Update crypto/keys/ecdsa/ecdsa_privkey.go * Update crypto/keys/ecdsa/ecdsa_pubkey.go * comments: add dot (.) at the end * update comments * update commented code * rename files * remove Amino methods * use 2 spaces in protocgen.sh * rollback changes in protocgen.sh * add MessageName * rework ecdsa proto structure * move ecdsa to internal package * add secp256r1 proto * refactore proto definition for secp256r1 * fix err check * update comments * create const for fieldSize+1 * simplify the PubKey.String test * Apply suggestions from code review Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * Update doc comments: SDK Interface -> sdk.Interface * rename init.go to doc.go * Add PubKey.Type() test * Revert "Update doc comments: SDK Interface -> sdk.Interface" This reverts commit 01f2b4f5efcd79a452483bcda152db54a8fbfee2. * Use cryptotypes.Address instead of tmcrypto * Revert "Use cryptotypes.Address instead of tmcrypto" This reverts commit 15b866ae67bdb7ca4872f4089fcab19f9e2e3608. This issue will be solved in https://github.com/cosmos/cosmos-sdk/issues/8775 * add link to ANSI X9.62 * move init.go -> doc.go * use proto.MessageName() Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>
2021-03-04 04:29:48 -08:00
package secp256r1
import (
"testing"
Remove bech32 PubKey support (#7477) * Move PubKey bech32 to legacy package and migrate the usage where possible * update /server * wip * move proto json encoding helper functions to internal * update internal/marshal * wip * update sections which needs legacybech32 * update validators output * fix conflicts * slashing update * add more tests and helper function for ANY JSON serialization * update slashing * Update function documentation * Rename code any-marshal helper functions * Update pubkey unpacking test * Update test comments * solve TestDecodeStore * solve legacytx issues * all code compiles * keyring tests * keyring cleanup * remove AssertMsg * fix some tests * fix add_ledger_test.go * update cli tests * debug cli test * rename clashed bech32 names * linter fixes * update tmservice tests * linter: update legacy deprecated checks * fix names * linting * legacybech32 pubkey type rename * fix staking client * fix test compilation * fix TestGetCmdQuerySigningInfo * rename NewIfcJSONAnyMarshaler * keyring: remove duplicated information from multinfo structure * todo cleanups * Update Changelog * remove some legacybech32 from tests * remove todos * remove printlnJSON from /server CLI and amino encoding * remove protocdc.MarshalJSON * client/show remove duplicated function * remove protocdc package * comment update * remove legacybech32.MustMarshalPubKey from a test * add todo * fix TestPublicKeyUnsafe test * review update * fix bech32 UnmarshalPubKey * Use codec.MarshalIfcJSON * fix linter issues * merging conflict: fix codec.Unmarshal calls * cleanups * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Reword changelog updates * use pubkey.String for comparison in Test_runAddCmdLedgerWithCustomCoinType * Update GetCmdQuerySigningInfo example * cli: update keys add docs * Add errors AsOf and errors.ErrIO type * restore multisigPubKeyInfo structure bring it back to multiInfo struct * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update crypto/keys/ed25519/ed25519_test.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * move pubkey any marshaling tests * Apply suggestions from code review Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * review updates * adding missing return * errors: use IsOf instead of AsOf * keyring: add a correct check for key not found in keyring.Get * add checkKeyNotFound * fix linter issues * fix: keyring key not found check * fix keyring tests * fix linting issues * cli tests * fix: 'simd keys show <key> -p' * fix: TestVerifyMultisignature * rename keyring Bech32... functions to Mk... * fix RunAddCmd * Update pubkey display * wip * add more tests * udate keyring output tests * remove todo from ledger tests * rename MkKeyOutput * Changelog update * solve liner issues * add link to github issue Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
2021-03-25 07:53:22 -07:00
proto "github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"
crypto: add secp256r1 (#8559) * Optimize secp256k1 hashing * Add ADR-028 related functions * Update ed25519 * fix errors/handle * fix build * fix build * Add tests and update function names * wip * Use LengthPrefix for composed addresses * add tests for NewComposed * add module hash function * fix append * rollback ed25519 ADR-28 update * rollback ed25519 ADR-28 test * Adding Module tests and convert tests to test suite * convert store_key_test.go to test suite * rollback test check comment * any.pb.go update * generated proto files * wip * renames * wip2 * add String method to PBBytes * wip3 * add pubkey tests * adding cryptotypes.PrivKey methods * re-enable test * fix equals test * fix ecdsa object receiver * add ProtoMarshaler implementation and tests * move code to init and add interface registry * add bytes tests * merge Unmarshal with UnmarshalAmino * implement ProtoMarshaler to ecdsaSK * remove bytes.go * add private key marshaling tests * break tests into 2 suites * add signature tests * remove TODO * remove bytes.proto * adding changelog * Update CHANGELOG.md * Update crypto/keys/ecdsa/ecdsa_privkey.go * Update crypto/keys/ecdsa/ecdsa_pubkey.go * comments: add dot (.) at the end * update comments * update commented code * rename files * remove Amino methods * use 2 spaces in protocgen.sh * rollback changes in protocgen.sh * add MessageName * rework ecdsa proto structure * move ecdsa to internal package * add secp256r1 proto * refactore proto definition for secp256r1 * fix err check * update comments * create const for fieldSize+1 * simplify the PubKey.String test * Apply suggestions from code review Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * Update doc comments: SDK Interface -> sdk.Interface * rename init.go to doc.go * Add PubKey.Type() test * Revert "Update doc comments: SDK Interface -> sdk.Interface" This reverts commit 01f2b4f5efcd79a452483bcda152db54a8fbfee2. * Use cryptotypes.Address instead of tmcrypto * Revert "Use cryptotypes.Address instead of tmcrypto" This reverts commit 15b866ae67bdb7ca4872f4089fcab19f9e2e3608. This issue will be solved in https://github.com/cosmos/cosmos-sdk/issues/8775 * add link to ANSI X9.62 * move init.go -> doc.go * use proto.MessageName() Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>
2021-03-04 04:29:48 -08:00
"github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
var _ cryptotypes.PrivKey = &PrivKey{}
func TestSKSuite(t *testing.T) {
suite.Run(t, new(SKSuite))
}
type SKSuite struct{ CommonSuite }
func (suite *SKSuite) TestString() {
suite.Require().Equal("secp256r1{-}", suite.sk.String())
}
func (suite *SKSuite) TestEquals() {
require := suite.Require()
skOther, err := GenPrivKey()
require.NoError(err)
require.False(suite.sk.Equals(skOther))
skOther2 := &PrivKey{skOther.Secret}
require.True(skOther.Equals(skOther2))
require.True(skOther2.Equals(skOther), "Equals must be reflexive")
}
func (suite *SKSuite) TestPubKey() {
pk := suite.sk.PubKey()
suite.True(suite.sk.(*PrivKey).Secret.PublicKey.Equal(&pk.(*PubKey).Key.PublicKey))
}
func (suite *SKSuite) Bytes() {
bz := suite.sk.Bytes()
suite.Len(bz, fieldSize)
var sk *PrivKey
suite.Nil(sk.Bytes())
}
func (suite *SKSuite) TestMarshalProto() {
require := suite.Require()
/**** test structure marshalling ****/
var sk PrivKey
bz, err := proto.Marshal(suite.sk)
require.NoError(err)
require.NoError(proto.Unmarshal(bz, &sk))
require.True(sk.Equals(suite.sk))
/**** test structure marshalling with codec ****/
sk = PrivKey{}
registry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
bz, err = cdc.MarshalBinaryBare(suite.sk.(*PrivKey))
require.NoError(err)
require.NoError(cdc.UnmarshalBinaryBare(bz, &sk))
require.True(sk.Equals(suite.sk))
const bufSize = 100
bz2 := make([]byte, bufSize)
skCpy := suite.sk.(*PrivKey)
_, err = skCpy.MarshalTo(bz2)
require.NoError(err)
require.Len(bz2, bufSize)
require.Equal(bz, bz2[:sk.Size()])
bz2 = make([]byte, bufSize)
_, err = skCpy.MarshalToSizedBuffer(bz2)
require.NoError(err)
require.Len(bz2, bufSize)
require.Equal(bz, bz2[(bufSize-sk.Size()):])
}
func (suite *SKSuite) TestSign() {
require := suite.Require()
msg := crypto.CRandBytes(1000)
sig, err := suite.sk.Sign(msg)
require.NoError(err)
sigCpy := make([]byte, len(sig))
copy(sigCpy, sig)
require.True(suite.pk.VerifySignature(msg, sigCpy))
// Mutate the signature
for i := range sig {
sigCpy[i] ^= byte(i + 1)
require.False(suite.pk.VerifySignature(msg, sigCpy))
}
// Mutate the message
msg[1] ^= byte(2)
require.False(suite.pk.VerifySignature(msg, sig))
}
func (suite *SKSuite) TestSize() {
require := suite.Require()
var pk ecdsaSK
require.Equal(pk.Size(), len(suite.sk.Bytes()))
var nilPk *ecdsaSK
require.Equal(0, nilPk.Size(), "nil value must have zero size")
}