Skip to content

Commit

Permalink
added bls sig verify cost to genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
kitty committed Jul 15, 2021
1 parent e47c7f8 commit 082e071
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 49 deletions.
3 changes: 3 additions & 0 deletions proto/cosmos/auth/v1beta1/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package cosmos.auth.v1beta1;

import "cosmos_proto/cosmos.proto";
//import "cosmos.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";

Expand Down Expand Up @@ -47,4 +48,6 @@ message Params {
[(gogoproto.customname) = "SigVerifyCostED25519", (gogoproto.moretags) = "yaml:\"sig_verify_cost_ed25519\""];
uint64 sig_verify_cost_secp256k1 = 5
[(gogoproto.customname) = "SigVerifyCostSecp256k1", (gogoproto.moretags) = "yaml:\"sig_verify_cost_secp256k1\""];
uint64 sig_verify_cost_bls12381 = 6
[(gogoproto.customname) = "SigVerifyCostBls12381", (gogoproto.moretags) = "yaml:\"sig_verify_cost_bls12381\""];
}
4 changes: 1 addition & 3 deletions x/auth/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys/bls12381"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -389,9 +388,8 @@ func DefaultSigVerificationGasConsumer(
}
return nil

// todo: add gas option to params and genesis.json
case *bls12381.PubKey:
meter.ConsumeGas(6213, "ante verify: bls12381")
meter.ConsumeGas(params.SigVerifyCostBls12381, "ante verify: bls12381")
return nil

default:
Expand Down
14 changes: 13 additions & 1 deletion x/auth/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
TxSizeCostPerByte = "tx_size_cost_per_byte"
SigVerifyCostED25519 = "sig_verify_cost_ed25519"
SigVerifyCostSECP256K1 = "sig_verify_cost_secp256k1"
SigVerifyCostBLS12381 = "sig_verify_cost_bls12381"
)

// RandomGenesisAccounts defines the default RandomGenesisAccountsFn used on the SDK.
Expand Down Expand Up @@ -87,6 +88,11 @@ func GenSigVerifyCostSECP256K1(r *rand.Rand) uint64 {
return uint64(simulation.RandIntBetween(r, 500, 1000))
}

// GenSigVerifyCostBLS12381 randomized SigVerifyCostBLS12381
func GenSigVerifyCostBLS12381(r *rand.Rand) uint64 {
return uint64(simulation.RandIntBetween(r, 6000, 12000))
}

// RandomizedGenState generates a random GenesisState for auth
func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn types.RandomGenesisAccountsFn) {
var maxMemoChars uint64
Expand Down Expand Up @@ -119,8 +125,14 @@ func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn type
func(r *rand.Rand) { sigVerifyCostSECP256K1 = GenSigVerifyCostSECP256K1(r) },
)

var sigVerifyCostBLS12381 uint64
simState.AppParams.GetOrGenerate(
simState.Cdc, SigVerifyCostBLS12381, &sigVerifyCostBLS12381, simState.Rand,
func(r *rand.Rand) { sigVerifyCostBLS12381 = GenSigVerifyCostBLS12381(r) },
)

params := types.NewParams(maxMemoChars, txSigLimit, txSizeCostPerByte,
sigVerifyCostED25519, sigVerifyCostSECP256K1)
sigVerifyCostED25519, sigVerifyCostSECP256K1, sigVerifyCostBLS12381)
genesisAccs := randGenAccountsFn(simState)

authGenesis := types.NewGenesisState(params, genesisAccs)
Expand Down
128 changes: 84 additions & 44 deletions x/auth/types/auth.pb.go

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

24 changes: 23 additions & 1 deletion x/auth/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
DefaultTxSizeCostPerByte uint64 = 10
DefaultSigVerifyCostED25519 uint64 = 590
DefaultSigVerifyCostSecp256k1 uint64 = 1000
DefaultSigVerifyCostBls12381 uint64 = 6300
)

// Parameter keys
Expand All @@ -24,20 +25,22 @@ var (
KeyTxSizeCostPerByte = []byte("TxSizeCostPerByte")
KeySigVerifyCostED25519 = []byte("SigVerifyCostED25519")
KeySigVerifyCostSecp256k1 = []byte("SigVerifyCostSecp256k1")
KeySigVerifyCostBls12381 = []byte("SigVerifyCostBls12381")
)

var _ paramtypes.ParamSet = &Params{}

// NewParams creates a new Params object
func NewParams(
maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64,
maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64, sigVerifyCostBls12381 uint64,
) Params {
return Params{
MaxMemoCharacters: maxMemoCharacters,
TxSigLimit: txSigLimit,
TxSizeCostPerByte: txSizeCostPerByte,
SigVerifyCostED25519: sigVerifyCostED25519,
SigVerifyCostSecp256k1: sigVerifyCostSecp256k1,
SigVerifyCostBls12381: sigVerifyCostBls12381,
}
}

Expand All @@ -55,6 +58,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeyTxSizeCostPerByte, &p.TxSizeCostPerByte, validateTxSizeCostPerByte),
paramtypes.NewParamSetPair(KeySigVerifyCostED25519, &p.SigVerifyCostED25519, validateSigVerifyCostED25519),
paramtypes.NewParamSetPair(KeySigVerifyCostSecp256k1, &p.SigVerifyCostSecp256k1, validateSigVerifyCostSecp256k1),
paramtypes.NewParamSetPair(KeySigVerifyCostBls12381, &p.SigVerifyCostBls12381, validateSigVerifyCostBls12381),
}
}

Expand All @@ -66,6 +70,7 @@ func DefaultParams() Params {
TxSizeCostPerByte: DefaultTxSizeCostPerByte,
SigVerifyCostED25519: DefaultSigVerifyCostED25519,
SigVerifyCostSecp256k1: DefaultSigVerifyCostSecp256k1,
SigVerifyCostBls12381: DefaultSigVerifyCostBls12381,
}
}

Expand Down Expand Up @@ -114,6 +119,20 @@ func validateSigVerifyCostSecp256k1(i interface{}) error {
return nil
}


func validateSigVerifyCostBls12381(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v == 0 {
return fmt.Errorf("invalid BLS12381 signature verification cost: %d", v)
}

return nil
}

func validateMaxMemoCharacters(i interface{}) error {
v, ok := i.(uint64)
if !ok {
Expand Down Expand Up @@ -151,6 +170,9 @@ func (p Params) Validate() error {
if err := validateSigVerifyCostSecp256k1(p.SigVerifyCostSecp256k1); err != nil {
return err
}
if err := validateSigVerifyCostBls12381(p.SigVerifyCostBls12381); err != nil {
return err
}
if err := validateMaxMemoCharacters(p.MaxMemoCharacters); err != nil {
return err
}
Expand Down

0 comments on commit 082e071

Please sign in to comment.