Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secp256k1 precompiles #123

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/types/suave_structs.go

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

39 changes: 32 additions & 7 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package vm

import (
"crypto/rand"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
suave "github.com/ethereum/go-ethereum/suave/core"
Expand All @@ -29,13 +32,13 @@ func (b *suaveRuntime) confidentialInputs() ([]byte, error) {

/* Confidential store precompiles */

func (b *suaveRuntime) confidentialStore(bidId types.BidId, key string, data []byte) error {
bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidId)
func (b *suaveRuntime) confidentialStore(bidID types.BidId, key string, data []byte) error {
bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidID)
if err != nil {
return suave.ErrBidNotFound
}

log.Info("confStore", "bidId", bidId, "key", key)
log.Info("confStore", "bidId", bidID, "key", key)

caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialStoreAddr, bid)
if err != nil {
Expand All @@ -46,16 +49,16 @@ func (b *suaveRuntime) confidentialStore(bidId types.BidId, key string, data []b
confStorePrecompileStoreMeter.Mark(int64(len(data)))
}

_, err = b.suaveContext.Backend.ConfidentialStore.Store(bidId, caller, key, data)
_, err = b.suaveContext.Backend.ConfidentialStore.Store(bidID, caller, key, data)
if err != nil {
return err
}

return nil
}

func (b *suaveRuntime) confidentialRetrieve(bidId types.BidId, key string) ([]byte, error) {
bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidId)
func (b *suaveRuntime) confidentialRetrieve(bidID types.BidId, key string) ([]byte, error) {
bid, err := b.suaveContext.Backend.ConfidentialStore.FetchBidById(bidID)
if err != nil {
return nil, suave.ErrBidNotFound
}
Expand All @@ -65,7 +68,7 @@ func (b *suaveRuntime) confidentialRetrieve(bidId types.BidId, key string) ([]by
return nil, err
}

data, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bidId, caller, key)
data, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(bidID, caller, key)
if err != nil {
return []byte(err.Error()), err
}
Expand Down Expand Up @@ -109,6 +112,28 @@ func (b *suaveRuntime) fetchBids(targetBlock uint64, namespace string) ([]types.
return bids, nil
}

func (b *suaveRuntime) randomUint() (*big.Int, error) {
metachris marked this conversation as resolved.
Show resolved Hide resolved
maxU256 := new(big.Int)
maxU256.SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16)
num, err := rand.Int(rand.Reader, maxU256)
if err != nil {
return nil, err
}
return num, nil
}

func (b *suaveRuntime) secp256k1Sign(msg, key []byte) ([]byte, error) {
return secp256k1.Sign(msg, key)
}

func (b *suaveRuntime) secp256k1RecoverPubkey(msg, sig []byte) ([]byte, error) {
return secp256k1.RecoverPubkey(msg, sig)
}

func (b *suaveRuntime) secp256k1VerifySignature(pubkey, msg, sig []byte) (bool, error) {
return secp256k1.VerifySignature(pubkey, msg, sig), nil
}

func mustParseAbi(data string) abi.ABI {
inoutAbi, err := abi.JSON(strings.NewReader(data))
if err != nil {
Expand Down
181 changes: 179 additions & 2 deletions core/vm/contracts_suave_runtime_adapter.go

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

17 changes: 17 additions & 0 deletions core/vm/contracts_suave_runtime_adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vm

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -67,6 +68,22 @@ func (m *mockRuntime) submitEthBlockBidToRelay(relayUrl string, builderBid []byt
return []byte{0x1}, nil
}

func (m *mockRuntime) randomUint() (*big.Int, error) {
return big.NewInt(0x9001), nil
}

func (m *mockRuntime) secp256k1RecoverPubkey(_msg []byte, _sig []byte) ([]byte, error) {
return []byte{0x1}, nil
}

func (m *mockRuntime) secp256k1VerifySignature(_msg []byte, _sig []byte, _pubkey []byte) (bool, error) {
return true, nil
}

func (m *mockRuntime) secp256k1Sign(_msg []byte, _key []byte) ([]byte, error) {
return []byte{0x1}, nil
}

func TestRuntimeAdapter(t *testing.T) {
adapter := &SuaveRuntimeAdapter{
impl: &mockRuntime{},
Expand Down
Loading
Loading