Skip to content

Commit

Permalink
Merge pull request #1463 from jhkimqd/jihwan/p256verify-test
Browse files Browse the repository at this point in the history
test: add unit test for p256verifyzkevm
  • Loading branch information
jhkimqd authored Nov 19, 2024
2 parents ebe2d7d + 5076ccc commit 09a92b5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
6 changes: 3 additions & 3 deletions core/vm/contracts_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ func (c *bigModExp_zkevm) Run(input []byte) ([]byte, error) {
baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64()
expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64()
modLen = new(big.Int).SetBytes(getData(input, 64, 32)).Uint64()
base = big.NewInt(0)
exp = big.NewInt(0)
mod = big.NewInt(0)
base = new(big.Int).SetBytes(getData(input, 0, baseLen))
exp = new(big.Int).SetBytes(getData(input, baseLen, expLen))
mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen))
)

// Extract `base`, `exp`, and `mod` with padding as needed
Expand Down
71 changes: 59 additions & 12 deletions core/vm/contracts_zkevm_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package vm

import (
"testing"
"bytes"
"fmt"
"math/big"
"testing"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/common"
)

var (
Expand All @@ -11,6 +16,54 @@ var (
big8194 = big.NewInt(0).Lsh(big.NewInt(1), 8194)
)

func uint64To32Bytes(input int) []byte {
bigInt := new(big.Int).SetUint64(uint64(input))
bytes := bigInt.Bytes()
result := make([]byte, 32)
copy(result[32-len(bytes):], bytes)
return result
}

func uint64ToDeterminedBytes(input *big.Int, length int) []byte {
bytes := input.Bytes()
result := make([]byte, length)
copy(result[length-len(bytes):], bytes)
return result
}

// This relies on PrecompiledContractsForkID13Durian.
func testPrecompiledZkevm(t *testing.T, addr string, test precompiledTest) {
p := PrecompiledContractsForkID13Durian[libcommon.HexToAddress(addr)]
in := libcommon.Hex2Bytes(test.Input)
gas := p.RequiredGas(in)
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
t.Parallel()
if res, _, err := RunPrecompiledContract(p, in, gas); err != nil {
t.Error(err)
} else if common.Bytes2Hex(res) != test.Expected {
t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
}
if expGas := test.Gas; expGas != gas {
t.Errorf("%v: gas wrong, expected %d, got %d", test.Name, expGas, gas)
}
// Verify that the precompile did not touch the input buffer
exp := libcommon.Hex2Bytes(test.Input)
if !bytes.Equal(in, exp) {
t.Errorf("Precompiled %v modified input data", addr)
}
})
}

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

func Test_ModExpZkevm_Gas(t *testing.T) {
modExp := bigModExp_zkevm{enabled: true, eip2565: true}

Expand Down Expand Up @@ -53,17 +106,11 @@ func Test_ModExpZkevm_Gas(t *testing.T) {
}
}

func uint64To32Bytes(input int) []byte {
bigInt := new(big.Int).SetUint64(uint64(input))
bytes := bigInt.Bytes()
result := make([]byte, 32)
copy(result[32-len(bytes):], bytes)
return result
func TestP256VerifyZkevm(t *testing.T) {
testJsonZkevm("p256Verify", "0x0000000000000000000000000000000000000100", t)
}

func uint64ToDeterminedBytes(input *big.Int, length int) []byte {
bytes := input.Bytes()
result := make([]byte, length)
copy(result[length-len(bytes):], bytes)
return result
// EIP2565 is enabled by default.
func TestBigModExpZkevm(t *testing.T) {
testJsonZkevm("modexp_eip2565", "0x0000000000000000000000000000000000000005", t)
}
35 changes: 35 additions & 0 deletions core/vm/testdata/precompiles/modexp_eip2565.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@
"Gas": 1360,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
"Expected": "",
"Name": "return_empty_byte_slice",
"Gas": 200,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002003ffff80",
"Expected": "3b01b01ac41f2d6e917c6d6a221ce793802469026d9ab7578fa2e79e4da6aaab",
"Name": "right_padding_mod",
"Gas": 200,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002003ffff800000000000000000000000000000000000000000000000000000000000000007",
"Expected": "3b01b01ac41f2d6e917c6d6a221ce793802469026d9ab7578fa2e79e4da6aaab",
"Name": "right_padding_mod_with_excess_data",
"Gas": 200,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002003ffff80",
"Expected": "0000000000000000000000000000000000000000000000000000000000000000",
"Name": "zero_base_right_pad",
"Gas": 200,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002003ffff8000000000000000000000000000000000000000000000000000000000000000",
"Expected": "3b01b01ac41f2d6e917c6d6a221ce793802469026d9ab7578fa2e79e4da6aaab",
"Name": "eip_example_3",
"Gas": 200,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040e09ad9675465c53a109fac66a445c91b292d2bb2c5268addb30cd82f80fcb0033ff97c80a5fc6f39193ae969c6ede6710a6b7ac27078a06d90ef1c72e5c85fb502fc9e1f6beb81516545975218075ec2af118cd8798df6e08a147c60fd6095ac2bb02c2908cf4dd7c81f11c289e4bce98f3553768f392a80ce22bf5c4f4a248c6b",
"Expected": "60008f1614cc01dcfb6bfb09c625cf90b47d4468db81b5f8b7a39d42f332eab9b2da8f2d95311648a8f243f4bb13cfb3d8f7f2a3c014122ebb3ed41b02783adc",
Expand Down

0 comments on commit 09a92b5

Please sign in to comment.