Co-authored-by: Marko <marko@baricevic.me> Co-authored-by: sontrinh16 <trinhleson2000@gmail.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
co-authored by
Marko
sontrinh16
Julien Robert
parent
0d34054516
commit
c8ef0ef3ff
@@ -1,6 +1,8 @@
|
||||
package codec
|
||||
|
||||
import (
|
||||
"github.com/cometbft/cometbft/crypto/bls12381"
|
||||
|
||||
"cosmossdk.io/core/registry"
|
||||
|
||||
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
|
||||
@@ -18,7 +20,7 @@ func RegisterCrypto(registrar registry.AminoRegistrar) {
|
||||
ed25519.PubKeyName)
|
||||
registrar.RegisterConcrete(&secp256k1.PubKey{},
|
||||
secp256k1.PubKeyName)
|
||||
registrar.RegisterConcrete(&bls12_381.PubKey{}, bls12_381.PubKeyName)
|
||||
registrar.RegisterConcrete(&bls12_381.PubKey{}, bls12381.PubKeyName)
|
||||
registrar.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
|
||||
kmultisig.PubKeyAminoRoute)
|
||||
registrar.RegisterInterface((*cryptotypes.PrivKey)(nil), nil)
|
||||
@@ -26,5 +28,5 @@ func RegisterCrypto(registrar registry.AminoRegistrar) {
|
||||
ed25519.PrivKeyName)
|
||||
registrar.RegisterConcrete(&secp256k1.PrivKey{},
|
||||
secp256k1.PrivKeyName)
|
||||
registrar.RegisterConcrete(&bls12_381.PrivKey{}, bls12_381.PrivKeyName)
|
||||
registrar.RegisterConcrete(&bls12_381.PrivKey{}, bls12381.PrivKeyName)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package codec
|
||||
|
||||
import (
|
||||
"github.com/cometbft/cometbft/crypto/bls12381"
|
||||
|
||||
"cosmossdk.io/errors"
|
||||
|
||||
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
|
||||
@@ -29,7 +31,7 @@ func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) {
|
||||
return &secp256k1.PubKey{
|
||||
Key: pk.Value,
|
||||
}, nil
|
||||
case bls12_381.PubKeyName:
|
||||
case bls12381.PubKeyName:
|
||||
return &bls12_381.PubKey{
|
||||
Key: pk.Value,
|
||||
}, nil
|
||||
@@ -60,7 +62,7 @@ func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) {
|
||||
}, nil
|
||||
case *bls12_381.PubKey:
|
||||
return cryptokeys.JSONPubkey{
|
||||
KeyType: bls12_381.PubKeyName,
|
||||
KeyType: bls12381.PubKeyName,
|
||||
Value: pk.Bytes(),
|
||||
}, nil
|
||||
default:
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package bls12_381
|
||||
|
||||
const (
|
||||
// PrivKeyName is the name of the private key as it is stored in the keystore.
|
||||
PrivKeyName = "cometbft/PrivKeyBls12_381"
|
||||
// PubKeyName is the name of the public key as it is stored in the keystore.
|
||||
PubKeyName = "cometbft/PubKeyBls12_381"
|
||||
// PubKeySize is the size, in bytes, of public keys as used in this package.
|
||||
PubKeySize = 32
|
||||
// PrivKeySize is the size, in bytes, of private keys as used in this package.
|
||||
PrivKeySize = 64
|
||||
// SignatureLength defines the byte length of a BLS signature.
|
||||
SignatureLength = 96
|
||||
// SeedSize is the size, in bytes, of private key seeds. These are the
|
||||
// private key representations used by RFC 8032.
|
||||
SeedSize = 32
|
||||
// MaxMsgLen defines the maximum length of the message bytes as passed to Sign.
|
||||
MaxMsgLen = 32
|
||||
// KeyType is the type of key this package provides.
|
||||
KeyType = "bls12381"
|
||||
)
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cometbft/cometbft/crypto"
|
||||
bls "github.com/cometbft/cometbft/crypto/bls12381"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
@@ -54,7 +55,7 @@ func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {
|
||||
|
||||
// Type returns the type.
|
||||
func (PrivKey) Type() string {
|
||||
return KeyType
|
||||
return bls.KeyType
|
||||
}
|
||||
|
||||
// Sign signs the given byte array. If msg is larger than
|
||||
@@ -70,7 +71,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) {
|
||||
|
||||
// UnmarshalAmino overrides Amino binary marshaling.
|
||||
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
|
||||
if len(bz) != PrivKeySize {
|
||||
if len(bz) != bls.PrivKeySize {
|
||||
return errors.New("invalid privkey size")
|
||||
}
|
||||
privKey.Key = bz
|
||||
@@ -119,7 +120,7 @@ func (pubKey PubKey) Bytes() []byte {
|
||||
|
||||
// Type returns the key's type.
|
||||
func (PubKey) Type() string {
|
||||
return KeyType
|
||||
return bls.KeyType
|
||||
}
|
||||
|
||||
// Equals returns true if the other's type is the same and their bytes are deeply equal.
|
||||
|
||||
@@ -10,8 +10,7 @@ import (
|
||||
|
||||
"github.com/cometbft/cometbft/crypto"
|
||||
"github.com/cometbft/cometbft/crypto/tmhash"
|
||||
|
||||
bls12381 "github.com/cosmos/crypto/curves/bls12381"
|
||||
"github.com/cometbft/cometbft/crypto/bls12381"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
@@ -32,20 +31,20 @@ var (
|
||||
|
||||
// NewPrivateKeyFromBytes build a new key from the given bytes.
|
||||
func NewPrivateKeyFromBytes(bz []byte) (PrivKey, error) {
|
||||
secretKey, err := bls12381.SecretKeyFromBytes(bz)
|
||||
secretKey, err := bls12381.NewPrivateKeyFromBytes(bz)
|
||||
if err != nil {
|
||||
return PrivKey{}, err
|
||||
}
|
||||
return PrivKey{
|
||||
Key: secretKey.Marshal(),
|
||||
Key: secretKey.Bytes(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GenPrivKey generates a new key.
|
||||
func GenPrivKey() (PrivKey, error) {
|
||||
secretKey, err := bls12381.RandKey()
|
||||
secretKey, err := bls12381.GenPrivKey()
|
||||
return PrivKey{
|
||||
Key: secretKey.Marshal(),
|
||||
Key: secretKey.Bytes(),
|
||||
}, err
|
||||
}
|
||||
|
||||
@@ -57,13 +56,13 @@ func (privKey PrivKey) Bytes() []byte {
|
||||
// PubKey returns the private key's public key. If the privkey is not valid
|
||||
// it returns a nil value.
|
||||
func (privKey PrivKey) PubKey() cryptotypes.PubKey {
|
||||
secretKey, err := bls12381.SecretKeyFromBytes(privKey.Key)
|
||||
secretKey, err := bls12381.NewPrivateKeyFromBytes(privKey.Key)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &PubKey{
|
||||
Key: secretKey.PublicKey().Marshal(),
|
||||
Key: secretKey.PubKey().Bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,24 +73,23 @@ func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {
|
||||
|
||||
// Type returns the type.
|
||||
func (PrivKey) Type() string {
|
||||
return KeyType
|
||||
return bls12381.KeyType
|
||||
}
|
||||
|
||||
// Sign signs the given byte array. If msg is larger than
|
||||
// MaxMsgLen, SHA256 sum will be signed instead of the raw bytes.
|
||||
func (privKey PrivKey) Sign(msg []byte) ([]byte, error) {
|
||||
secretKey, err := bls12381.SecretKeyFromBytes(privKey.Key)
|
||||
secretKey, err := bls12381.NewPrivateKeyFromBytes(privKey.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(msg) > MaxMsgLen {
|
||||
if len(msg) > bls12381.MaxMsgLen {
|
||||
hash := sha256.Sum256(msg)
|
||||
sig := secretKey.Sign(hash[:])
|
||||
return sig.Marshal(), nil
|
||||
return secretKey.Sign(hash[:])
|
||||
}
|
||||
sig := secretKey.Sign(msg)
|
||||
return sig.Marshal(), nil
|
||||
|
||||
return secretKey.Sign(msg)
|
||||
}
|
||||
|
||||
// MarshalAmino overrides Amino binary marshaling.
|
||||
@@ -101,7 +99,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) {
|
||||
|
||||
// UnmarshalAmino overrides Amino binary marshaling.
|
||||
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
|
||||
if len(bz) != PrivKeySize {
|
||||
if len(bz) != bls12381.PrivKeySize {
|
||||
return errors.New("invalid privkey size")
|
||||
}
|
||||
privKey.Key = bz
|
||||
@@ -135,8 +133,8 @@ var _ cryptotypes.PubKey = &PubKey{}
|
||||
//
|
||||
// The function will panic if the public key is invalid.
|
||||
func (pubKey PubKey) Address() crypto.Address {
|
||||
pk, _ := bls12381.PublicKeyFromBytes(pubKey.Key)
|
||||
if len(pk.Marshal()) != PubKeySize {
|
||||
pk, _ := bls12381.NewPublicKeyFromBytes(pubKey.Key)
|
||||
if len(pk.Bytes()) != bls12381.PubKeySize {
|
||||
panic("pubkey is incorrect size")
|
||||
}
|
||||
return crypto.Address(tmhash.SumTruncated(pubKey.Key))
|
||||
@@ -144,26 +142,21 @@ func (pubKey PubKey) Address() crypto.Address {
|
||||
|
||||
// VerifySignature verifies the given signature.
|
||||
func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
|
||||
if len(sig) != SignatureLength {
|
||||
if len(sig) != bls12381.SignatureLength {
|
||||
return false
|
||||
}
|
||||
|
||||
pubK, err := bls12381.PublicKeyFromBytes(pubKey.Key)
|
||||
pubK, err := bls12381.NewPublicKeyFromBytes(pubKey.Key)
|
||||
if err != nil { // invalid pubkey
|
||||
return false
|
||||
}
|
||||
|
||||
if len(msg) > MaxMsgLen {
|
||||
if len(msg) > bls12381.MaxMsgLen {
|
||||
hash := sha256.Sum256(msg)
|
||||
msg = hash[:]
|
||||
}
|
||||
|
||||
ok, err := bls12381.VerifySignature(sig, [MaxMsgLen]byte(msg[:MaxMsgLen]), pubK)
|
||||
if err != nil { // bad signature
|
||||
return false
|
||||
}
|
||||
|
||||
return ok
|
||||
return pubK.VerifySignature(msg, sig)
|
||||
}
|
||||
|
||||
// Bytes returns the byte format.
|
||||
@@ -173,7 +166,7 @@ func (pubKey PubKey) Bytes() []byte {
|
||||
|
||||
// Type returns the key's type.
|
||||
func (PubKey) Type() string {
|
||||
return KeyType
|
||||
return bls12381.KeyType
|
||||
}
|
||||
|
||||
// Equals returns true if the other's type is the same and their bytes are deeply equal.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
bls "github.com/cometbft/cometbft/crypto/bls12381"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
@@ -26,7 +28,7 @@ func (pk JSONPubkey) Address() types.Address {
|
||||
Key: pk.Value,
|
||||
}
|
||||
return secp256k1.Address()
|
||||
case bls12_381.PubKeyName:
|
||||
case bls.PubKeyName:
|
||||
bls12_381 := bls12_381.PubKey{
|
||||
Key: pk.Value,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package multisig
|
||||
|
||||
import (
|
||||
"github.com/cometbft/cometbft/crypto/bls12381"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
||||
@@ -26,7 +28,7 @@ func init() {
|
||||
AminoCdc.RegisterConcrete(&secp256k1.PubKey{},
|
||||
secp256k1.PubKeyName)
|
||||
AminoCdc.RegisterConcrete(&bls12_381.PubKey{},
|
||||
bls12_381.PubKeyName)
|
||||
bls12381.PubKeyName)
|
||||
AminoCdc.RegisterConcrete(&LegacyAminoPubKey{},
|
||||
PubKeyAminoRoute)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user