Skip to content

Commit

Permalink
parity: add parity eip2537 configurator implementation
Browse files Browse the repository at this point in the history
Install support for OpenEthereum data type for
EIP2537 configuration feature.

EIP2537 adds 9 precompiles for BLS12-381 curve.

Signed-off-by: meows <[email protected]>

parity: (lint) goimports -w

Signed-off-by: meows <[email protected]>

parity: !squashme fix typo

Signed-off-by: meows <[email protected]>
  • Loading branch information
meowsbits committed Jul 27, 2020
1 parent 4b8d352 commit 5089bec
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 20 deletions.
18 changes: 18 additions & 0 deletions params/types/parity/parity.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ type ParityChainSpecPricing struct {

// Blake2F is the price per round of Blake2 compression
Blake2F *ParityChainSpecBlakePricing `json:"blake2_f,omitempty"`

BLS12ConstOperation *ParityChainSpecBLS12ConstOperationPricing `json:"bls12_const_operations,omitempty"`
BLS12G1MultiExp *ParityChainSpecBLS12MultiExpPricing `json:"bls12_g1_multiexp,omitempty"`
BLS12G2MultiExp *ParityChainSpecBLS12MultiExpPricing `json:"bls12_g2_multiexp,omitempty"`
BLS12Pairing *ParityChainSpecBLS12PairingPricing `json:"bls12_pairing,omitempty"`
}

type ParityChainSpecLinearPricing struct {
Expand Down Expand Up @@ -341,6 +346,19 @@ type ParityChainSpecBlakePricing struct {
GasPerRound uint64 `json:"gas_per_round"`
}

type ParityChainSpecBLS12ConstOperationPricing struct {
Price uint64 `json:"price"`
}

type ParityChainSpecBLS12MultiExpPricing struct {
Base uint64 `json:"base"`
}

type ParityChainSpecBLS12PairingPricing struct {
Base uint64 `json:"base"`
Pair uint64 `json:"pair"`
}

func (spec *ParityChainSpec) GetPrecompile(address common.Address, pricing ParityChainSpecPricing) *ParityU64 {
if spec.Accounts == nil {
return nil
Expand Down
178 changes: 158 additions & 20 deletions params/types/parity/parity_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,30 +459,168 @@ func (spec *ParityChainSpec) SetEIP1108Transition(n *uint64) error {
return nil
}

func (c *ParityChainSpec) GetECIP1080Transition() *uint64 {
return c.Params.ECIP1080Transition.Uint64P()
}

func (c *ParityChainSpec) SetECIP1080Transition(n *uint64) error {
c.Params.ECIP1080Transition = new(ParityU64).SetUint64(n)
return nil
}

func (c *ParityChainSpec) GetEIP1706Transition() *uint64 {
return c.Params.EIP1706Transition.Uint64P() // FIXME when+if upstream implements
}
func (spec *ParityChainSpec) GetECIP1080Transition() *uint64 {
return spec.Params.ECIP1080Transition.Uint64P()
}

func (spec *ParityChainSpec) SetECIP1080Transition(n *uint64) error {
spec.Params.ECIP1080Transition = new(ParityU64).SetUint64(n)
return nil
}

func (spec *ParityChainSpec) GetEIP1706Transition() *uint64 {
return spec.Params.EIP1706Transition.Uint64P() // FIXME when+if upstream implements
}

func (spec *ParityChainSpec) SetEIP1706Transition(n *uint64) error {
spec.Params.EIP1706Transition = new(ParityU64).SetUint64(n)
return nil
}

// GetEIP2537Transition returns the EIP2537 activation, if any.
// https://eips.ethereum.org/EIPS/eip-2537
func (spec *ParityChainSpec) GetEIP2537Transition() *uint64 {
var eip2537Precompiles = map[string]*uint64{
"bls12_381_g1_add": spec.GetPrecompile(common.BytesToAddress([]byte{0xa}),
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 600,
},
}).Uint64P(),
"bls12_381_g1_mul": spec.GetPrecompile(common.BytesToAddress([]byte{0xb}),
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 12000,
},
}).Uint64P(),
"bls12_381_g1_multiexp": spec.GetPrecompile(common.BytesToAddress([]byte{0xc}),
ParityChainSpecPricing{
BLS12G1MultiExp: &ParityChainSpecBLS12MultiExpPricing{
Base: 12000,
},
}).Uint64P(),
"bls12_381_g2_add": spec.GetPrecompile(common.BytesToAddress([]byte{0xd}),
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 4500,
},
}).Uint64P(),
"bls12_381_g2_mul": spec.GetPrecompile(common.BytesToAddress([]byte{0xe}),
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 55000,
},
}).Uint64P(),
"bls12_381_g2_multiexp": spec.GetPrecompile(common.BytesToAddress([]byte{0xf}),
ParityChainSpecPricing{
BLS12G2MultiExp: &ParityChainSpecBLS12MultiExpPricing{
Base: 55000,
},
}).Uint64P(),
"bls12_381_pairing": spec.GetPrecompile(common.BytesToAddress([]byte{0x10}),
ParityChainSpecPricing{
BLS12Pairing: &ParityChainSpecBLS12PairingPricing{
Base: 115000,
Pair: 23000,
},
}).Uint64P(),
"bls12_381_fp_to_g1": spec.GetPrecompile(common.BytesToAddress([]byte{0x11}),
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 5500,
},
}).Uint64P(),
"bls12_381_fp2_to_g2": spec.GetPrecompile(common.BytesToAddress([]byte{0x12}),
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 110000,
},
}).Uint64P(),
}

func (c *ParityChainSpec) SetEIP1706Transition(n *uint64) error {
c.Params.EIP1706Transition = new(ParityU64).SetUint64(n)
return nil
var activation *uint64
for _, v := range eip2537Precompiles {
if v == nil {
return nil
}
if activation == nil {
activation = v
continue
}
if *v != *activation {
return nil
}
}
return activation
}

func (c *ParityChainSpec) GetEIP2537Transition() *uint64 {
return c.Params.EIP2537Transition.Uint64P() // FIXME when+if upstream implements
}
func (spec *ParityChainSpec) SetEIP2537Transition(n *uint64) error {
type setter struct {
name string
pricing ParityChainSpecPricing
}
var eip2537Precompiles = []setter{
{"bls12_381_g1_add",
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 600,
},
}},
{"bls12_381_g1_mul",
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 12000,
},
}},
{"bls12_381_g1_multiexp",
ParityChainSpecPricing{
BLS12G1MultiExp: &ParityChainSpecBLS12MultiExpPricing{
Base: 12000,
},
}},
{"bls12_381_g2_add",
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 4500,
},
}},
{"bls12_381_g2_mul",
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 55000,
},
}},
{"bls12_381_g2_multiexp",
ParityChainSpecPricing{
BLS12G2MultiExp: &ParityChainSpecBLS12MultiExpPricing{
Base: 55000,
},
}},
{"bls12_381_pairing",
ParityChainSpecPricing{
BLS12Pairing: &ParityChainSpecBLS12PairingPricing{
Base: 115000,
Pair: 23000,
},
}},
{"bls12_381_fp_to_g1",
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 5500,
},
}},
{"bls12_381_fp2_to_g2",
ParityChainSpecPricing{
BLS12ConstOperation: &ParityChainSpecBLS12ConstOperationPricing{
Price: 110000,
},
}},
}

func (c *ParityChainSpec) SetEIP2537Transition(n *uint64) error {
c.Params.EIP2537Transition = new(ParityU64).SetUint64(n)
var addr uint8 = 0xa // EIP2537 BLS precompiles occupy 0xa:0x12 (9 total)
for i, v := range eip2537Precompiles {
spec.SetPrecompile2(common.BytesToAddress([]byte{addr + uint8(i)}), v.name, n, v.pricing)
}
return nil
}

Expand Down
48 changes: 48 additions & 0 deletions params/types/parity/parity_configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package parity

import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -57,3 +59,49 @@ func TestParityChainSpec_GetSetUint64(t *testing.T) {
t.Error("not right answer")
}
}

func TestParityChainSpec_GetEIP2537(t *testing.T) {
specFile := "../../parity.json.d/foundation.json"
b, err := ioutil.ReadFile(specFile)
if err != nil {
t.Fatalf("read file: %v", err)
}
spec := &ParityChainSpec{}
err = json.Unmarshal(b, spec)
if err != nil {
t.Fatalf("unmarshal: %v", err)
}
activateNumberRaw := "0x7fffffffffffff"
var activateNumber = new(math.HexOrDecimal256)
err = activateNumber.UnmarshalText([]byte(activateNumberRaw))
if err != nil {
t.Fatalf("unmarshal text: %v", err)
}
t.Logf("activate number: %v raw=%v", activateNumber.ToInt(), activateNumberRaw)

eip2537activation := spec.GetEIP2537Transition()
if eip2537activation == nil {
t.Fatal("nil activation")
}
if *eip2537activation != activateNumber.ToInt().Uint64() {
t.Fatal("wrong activation")
}
}

func TestParityChainSpec_SetEIP2537(t *testing.T) {
spec := &ParityChainSpec{}
activateAt := uint64(42)
err := spec.SetEIP2537Transition(&activateAt)
if err != nil {
t.Fatalf("set: %v", err)
}
got := spec.GetEIP2537Transition()
if got == nil || *got != activateAt {
b, err := json.MarshalIndent(spec, "", " ")
if err != nil {
t.Fatalf("marshal: %v", err)
}
t.Log(string(b))
t.Fatalf("empty got")
}
}

0 comments on commit 5089bec

Please sign in to comment.