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

CIP-31 - BLS12-381 Precompiles #1279

Closed
wants to merge 6 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
444 changes: 435 additions & 9 deletions core/vm/contracts.go

Large diffs are not rendered by default.

122 changes: 119 additions & 3 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package vm

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"reflect"
"testing"
Expand Down Expand Up @@ -138,6 +141,88 @@ type precompiledFailureTest struct {
name string
}

type precompiledTestFromJson struct {
Input, Expected string
Gas uint64
Name string
NoBenchmark bool // Benchmark primarily the worst-cases
}

// precompiledFailureTest defines the input/error pairs for precompiled
// contract failure tests.
type precompiledFailureTestFromJson struct {
Input string
ExpectedError string
Name string
}

func testJson(name, addr string, t *testing.T) {
tests, err := loadJson(name)
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
testPrecompiled(addr, test, t)
}
}

func testJsonFail(name, addr string, t *testing.T) {
tests, err := loadJsonFail(name)
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
testPrecompiledFailure(addr, test, t)
}
}

func loadJson(name string) ([]precompiledTest, error) {
data, err := ioutil.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name))
if err != nil {
return nil, err
}
var jsonTests []precompiledTestFromJson
err = json.Unmarshal(data, &jsonTests)
if err != nil {
return nil, err
}
testcases := make([]precompiledTest, len(jsonTests))
for i := 0; i < len(jsonTests); i++ {
jsonTest := jsonTests[i]
testcases[i] = precompiledTest{input: jsonTest.Input, expected: jsonTest.Expected, name: jsonTest.Name, errorExpected: false, noBenchmark: jsonTest.NoBenchmark}
}
return testcases, err
}

func loadJsonFail(name string) ([]precompiledFailureTest, error) {
data, err := ioutil.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name))
if err != nil {
return nil, err
}
var jsonTests []precompiledFailureTestFromJson
err = json.Unmarshal(data, &jsonTests)
if err != nil {
return nil, err
}
testcases := make([]precompiledFailureTest, len(jsonTests))
for i := 0; i < len(jsonTests); i++ {
jsonTest := jsonTests[i]
testcases[i] = precompiledFailureTest{input: jsonTest.Input, expectedError: errors.New(jsonTest.ExpectedError), name: jsonTest.Name}
}
err = json.Unmarshal(data, &testcases)
return testcases, err
}

func benchJson(name, addr string, b *testing.B) {
tests, err := loadJson(name)
if err != nil {
b.Fatal(err)
}
for _, test := range tests {
benchmarkPrecompiled(addr, test, b)
}
}

// modexpTests are the test and benchmark data for the modexp precompiled contract.
var modexpTests = []precompiledTest{
{
Expand Down Expand Up @@ -734,7 +819,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
}

func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
p := PrecompiledContractsIstanbul[common.HexToAddress(addr)]
p := PrecompiledContractsDonut[common.HexToAddress(addr)]
in := common.Hex2Bytes(test.input)
contract := NewContract(AccountRef(common.HexToAddress("1337")),
nil, new(big.Int), p.RequiredGas(in)-1)
Expand All @@ -752,7 +837,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
}

func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing.T) {
p := PrecompiledContractsIstanbul[common.HexToAddress(addr)]
p := PrecompiledContractsDonut[common.HexToAddress(addr)]
in := common.Hex2Bytes(test.input)
contract := NewContract(AccountRef(common.HexToAddress("31337")),
nil, new(big.Int), p.RequiredGas(in))
Expand All @@ -774,7 +859,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
if test.noBenchmark {
return
}
p := PrecompiledContractsIstanbul[common.HexToAddress(addr)]
p := PrecompiledContractsDonut[common.HexToAddress(addr)]
in := common.Hex2Bytes(test.input)
reqGas := p.RequiredGas(in)
contract := NewContract(AccountRef(common.HexToAddress("1337")),
Expand Down Expand Up @@ -983,3 +1068,34 @@ func TestGetVerifiedSealBitmap(t *testing.T) {
testPrecompiled("f4", test, t)
}
}

func TestPrecompiledBLS12381G1Add(t *testing.T) { testJson("blsG1Add", "f2", t) }
func TestPrecompiledBLS12381G1Mul(t *testing.T) { testJson("blsG1Mul", "f1", t) }
func TestPrecompiledBLS12381G1MultiExp(t *testing.T) { testJson("blsG1MultiExp", "f0", t) }
func TestPrecompiledBLS12381G2Add(t *testing.T) { testJson("blsG2Add", "ef", t) }
func TestPrecompiledBLS12381G2Mul(t *testing.T) { testJson("blsG2Mul", "ee", t) }
func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", "ed", t) }
func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "ec", t) }
func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "eb", t) }
func TestPrecompiledBLS12381MapG2(t *testing.T) { testJson("blsMapG2", "ea", t) }

func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add", "f2", b) }
func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "f1", b) }
func BenchmarkPrecompiledBLS12381G1MultiExp(b *testing.B) { benchJson("blsG1MultiExp", "f0", b) }
func BenchmarkPrecompiledBLS12381G2Add(b *testing.B) { benchJson("blsG2Add", "ef", b) }
func BenchmarkPrecompiledBLS12381G2Mul(b *testing.B) { benchJson("blsG2Mul", "ee", b) }
func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2MultiExp", "ed", b) }
func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "ec", b) }
func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "eb", b) }
func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "ea", b) }

// Failure tests
func TestPrecompiledBLS12381G1AddFail(t *testing.T) { testJsonFail("fail-blsG1Add", "f2", t) }
func TestPrecompiledBLS12381G1MulFail(t *testing.T) { testJsonFail("fail-blsG1Mul", "f1", t) }
func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) { testJsonFail("fail-blsG1MultiExp", "f0", t) }
func TestPrecompiledBLS12381G2AddFail(t *testing.T) { testJsonFail("fail-blsG2Add", "ef", t) }
func TestPrecompiledBLS12381G2MulFail(t *testing.T) { testJsonFail("fail-blsG2Mul", "ee", t) }
func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("fail-blsG2MultiExp", "ed", t) }
func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("fail-blsPairing", "ec", t) }
func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("fail-blsMapG1", "eb", t) }
func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("fail-blsMapG2", "ea", t) }
Loading