Commit 9f736b2a authored by protolambda's avatar protolambda Committed by GitHub

op-chain-ops: alias functions without pointers, and geth uint256 type (#3617)

parent aeb8125b
...@@ -7,27 +7,27 @@ import ( ...@@ -7,27 +7,27 @@ import (
) )
var ( var (
uint160Max, _ = uint256.FromHex("0xffffffffffffffffffffffffffffffffffffffff") offsetAddr = common.HexToAddress("0x1111000000000000000000000000000000001111")
offset = new(uint256.Int).SetBytes(common.HexToAddress("0x1111000000000000000000000000000000001111").Bytes()) offsetU256 = new(uint256.Int).SetBytes20(offsetAddr[:])
) )
// ApplyL1ToL2Alias will apply the alias applied to L1 to L2 messages when it // ApplyL1ToL2Alias will apply the alias applied to L1 to L2 messages when it
// originates from a contract address // originates from a contract address
func ApplyL1ToL2Alias(address *common.Address) *common.Address { func ApplyL1ToL2Alias(address common.Address) common.Address {
input := new(uint256.Int).SetBytes(address.Bytes()) var input uint256.Int
output := new(uint256.Int).AddMod(input, offset, uint160Max) input.SetBytes20(address[:])
if output.Cmp(input) < 0 { input.Add(&input, offsetU256)
output = output.Sub(output, new(uint256.Int).SetUint64(1)) // clipping to bytes20 is the same as modulo 160 here, since the modulo is a multiple of 8 bits
} return input.Bytes20()
addr := common.BigToAddress(output.ToBig())
return &addr
} }
// UndoL1ToL2Alias will remove the alias applied to L1 to L2 messages when it // UndoL1ToL2Alias will remove the alias applied to L1 to L2 messages when it
// originates from a contract address // originates from a contract address
func UndoL1ToL2Alias(address *common.Address) *common.Address { func UndoL1ToL2Alias(address common.Address) common.Address {
input := new(uint256.Int).SetBytes(address.Bytes()) var input uint256.Int
output := new(uint256.Int).Sub(input, offset) input.SetBytes20(address[:])
addr := common.BigToAddress(output.ToBig()) input.Sub(&input, offsetU256)
return &addr // clipping to bytes20 is the same as modulo 160 here, since the modulo is a multiple of 8 bits.
// and underflows are not affected either.
return input.Bytes20()
} }
...@@ -4,17 +4,18 @@ import ( ...@@ -4,17 +4,18 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-chain-ops/crossdomain"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-chain-ops/crossdomain"
) )
func FuzzAliasing(f *testing.F) { func FuzzAliasing(f *testing.F) {
f.Fuzz(func(t *testing.T, address []byte) { f.Fuzz(func(t *testing.T, address []byte) {
addr := common.BytesToAddress(address) addr := common.BytesToAddress(address)
aliased := crossdomain.ApplyL1ToL2Alias(&addr) aliased := crossdomain.ApplyL1ToL2Alias(addr)
unaliased := crossdomain.UndoL1ToL2Alias(aliased) unaliased := crossdomain.UndoL1ToL2Alias(aliased)
require.Equal(t, addr, *unaliased) require.Equal(t, addr, unaliased)
}) })
} }
...@@ -51,10 +52,10 @@ func TestAliasing(t *testing.T) { ...@@ -51,10 +52,10 @@ func TestAliasing(t *testing.T) {
for i, test := range cases { for i, test := range cases {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) { t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
aliased := crossdomain.ApplyL1ToL2Alias(&test.Input) aliased := crossdomain.ApplyL1ToL2Alias(test.Input)
require.Equal(t, test.Output, *aliased) require.Equal(t, test.Output, aliased)
unaliased := crossdomain.UndoL1ToL2Alias(aliased) unaliased := crossdomain.UndoL1ToL2Alias(aliased)
require.Equal(t, test.Input, *unaliased) require.Equal(t, test.Input, unaliased)
}) })
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment