Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Feb 26, 2024
1 parent b3225c1 commit 96aef8c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 33 deletions.
80 changes: 72 additions & 8 deletions crypto/keys/bls12_381/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

package bls
package bls12_381

import (
"bytes"
fmt "fmt"

"github.com/minio/sha256-simd"

errorsmod "cosmossdk.io/errors"

"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types/errors"

"github.com/itsdevbear/comet-bls12-381/bls/blst"
"github.com/itsdevbear/comet-bls12-381/bls/params"
Expand All @@ -47,6 +52,11 @@ const (
KeyType = "bls12_381"
)

var (
_ cryptotypes.PrivKey = &PrivKey{}
_ codec.AminoMarshaler = &PrivKey{}
)

// -------------------------------------.
const (
PrivKeyName = "cometbft/PrivKeyBLS12_381"
Expand Down Expand Up @@ -116,6 +126,33 @@ func (privKey PrivKey) Sign(digestBz []byte) ([]byte, error) {
return sig.Marshal(), nil
}

// MarshalAmino overrides Amino binary marshaling.
func (privKey PrivKey) MarshalAmino() ([]byte, error) {
return privKey.Key, nil
}

// UnmarshalAmino overrides Amino binary marshaling.
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
if len(bz) != PrivKeySize {
return fmt.Errorf("invalid privkey size")
}
privKey.Key = bz

return nil
}

// MarshalAminoJSON overrides Amino JSON marshaling.
func (privKey PrivKey) MarshalAminoJSON() ([]byte, error) {
// When we marshal to Amino JSON, we don't marshal the "key" field itself,
// just its contents (i.e. the key bytes).
return privKey.MarshalAmino()
}

// UnmarshalAminoJSON overrides Amino JSON marshaling.
func (privKey *PrivKey) UnmarshalAminoJSON(bz []byte) error {
return privKey.UnmarshalAmino(bz)
}

// ===============================================================================================
// Public Key
// ===============================================================================================
Expand All @@ -129,7 +166,7 @@ var _ cryptotypes.PubKey = &PubKey{}

// Address returns the address of the ECDSA public key.
// The function will return an empty address if the public key is invalid.
func (pubKey PubKey) Address() crypto.Address {
func (pubKey *PubKey) Address() crypto.Address {
pk, _ := blst.PublicKeyFromBytes(pubKey.Key)
if len(pk.Marshal()) != PubKeySize {
panic("pubkey is incorrect size")

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
Expand All @@ -138,7 +175,7 @@ func (pubKey PubKey) Address() crypto.Address {
return crypto.Address(tmhash.SumTruncated(pubKey.Key))
}

func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
func (pubKey *PubKey) VerifySignature(msg, sig []byte) bool {
if len(sig) != params.BLSSignatureLength {
return false
}
Expand All @@ -157,20 +194,47 @@ func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
}

// Bytes returns the pubkey byte format.
func (pubKey PubKey) Bytes() []byte {
func (pubKey *PubKey) Bytes() []byte {
return pubKey.Key
}

func (pubKey PubKey) String() string {
return string(pubKey.Bytes())
func (pubKey *PubKey) String() string {
return fmt.Sprintf("PubKeyBLS12_381{%X}", pubKey.Key)
}

// Type returns eth_bls12_381.
func (pubKey PubKey) Type() string {
func (pubKey *PubKey) Type() string {
return KeyType
}

// Equals returns true if the pubkey type is the same and their bytes are deeply equal.
func (pubKey PubKey) Equals(other cryptotypes.PubKey) bool {
func (pubKey *PubKey) Equals(other cryptotypes.PubKey) bool {
return pubKey.Type() == other.Type() && bytes.Equal(pubKey.Bytes(), other.Bytes())
}

// MarshalAmino overrides Amino binary marshaling.
func (pubKey *PubKey) MarshalAmino() ([]byte, error) {
return pubKey.Key, nil
}

// UnmarshalAmino overrides Amino binary marshaling.
func (pubKey *PubKey) UnmarshalAmino(bz []byte) error {
if len(bz) != PubKeySize {
return errorsmod.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size")
}
pubKey.Key = bz

return nil
}

// MarshalAminoJSON overrides Amino JSON marshaling.
func (pubKey *PubKey) MarshalAminoJSON() ([]byte, error) {
// When we marshal to Amino JSON, we don't marshal the "key" field itself,
// just its contents (i.e. the key bytes).
return pubKey.MarshalAmino()
}

// UnmarshalAminoJSON overrides Amino JSON marshaling.
func (pubKey *PubKey) UnmarshalAminoJSON(bz []byte) error {
return pubKey.UnmarshalAmino(bz)
}
52 changes: 27 additions & 25 deletions crypto/keys/bls12_381/keys.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 96aef8c

Please sign in to comment.