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

AVM: uint256 opcodes #6083

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
125 changes: 125 additions & 0 deletions data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import (
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"

"github.com/holiman/uint256"
)

// The constants below control opcode evaluation and MAY NOT be changed without
Expand Down Expand Up @@ -1764,6 +1766,129 @@ func opPlus(cx *EvalContext) error {
return nil
}

func opAdd256(cx *EvalContext) error {
last := len(cx.Stack) - 1
prev := last - 1

if len(cx.Stack[prev].Bytes) > 32 || len(cx.Stack[last].Bytes) > 32 {
return errors.New("add256 requires inputs <= 32 byte")
}

a := new(uint256.Int).SetBytes(cx.Stack[prev].Bytes)
b := new(uint256.Int).SetBytes(cx.Stack[last].Bytes)
result, carry := new(uint256.Int).AddOverflow(a, b)

if carry {
return errors.New("add256 overflowed")
}

resultBytes := result.Bytes32()
cx.Stack[prev].Bytes = resultBytes[:]
cx.Stack = cx.Stack[:last]

return nil
}

func opSub256(cx *EvalContext) error {
last := len(cx.Stack) - 1
prev := last - 1

if len(cx.Stack[prev].Bytes) > 32 || len(cx.Stack[last].Bytes) > 32 {
return errors.New("sub256 requires inputs <= 32 byte")
}

a := new(uint256.Int).SetBytes(cx.Stack[prev].Bytes)
b := new(uint256.Int).SetBytes(cx.Stack[last].Bytes)
result, underflow := new(uint256.Int).SubOverflow(a, b)

if underflow {
return errors.New("sub256 underflowed")
}

resultBytes := result.Bytes32()
cx.Stack[prev].Bytes = resultBytes[:]
cx.Stack = cx.Stack[:last]

return nil
}

func opMul256(cx *EvalContext) error {
last := len(cx.Stack) - 1
prev := last - 1

if len(cx.Stack[prev].Bytes) > 32 || len(cx.Stack[last].Bytes) > 32 {
return errors.New("sub256 requires inputs <= 32 byte")
}

a := new(uint256.Int).SetBytes(cx.Stack[prev].Bytes)
b := new(uint256.Int).SetBytes(cx.Stack[last].Bytes)
result, overflow := new(uint256.Int).MulOverflow(a, b)

if overflow {
return errors.New("mul256 overflowed")
}

resultBytes := result.Bytes32()
cx.Stack[prev].Bytes = resultBytes[:]
cx.Stack = cx.Stack[:last]

return nil
}

func opDiv256(cx *EvalContext) error {
last := len(cx.Stack) - 1
prev := last - 1

if len(cx.Stack[prev].Bytes) > 32 || len(cx.Stack[last].Bytes) > 32 {
return errors.New("sub256 requires inputs <= 32 byte")
}

a := new(uint256.Int).SetBytes(cx.Stack[prev].Bytes)
b := new(uint256.Int).SetBytes(cx.Stack[last].Bytes)
result := new(uint256.Int).Div(a, b)

resultBytes := result.Bytes32()
cx.Stack[prev].Bytes = resultBytes[:]
cx.Stack = cx.Stack[:last]

return nil
}

func opMod256(cx *EvalContext) error {
last := len(cx.Stack) - 1
prev := last - 1

if len(cx.Stack[prev].Bytes) > 32 || len(cx.Stack[last].Bytes) > 32 {
return errors.New("sub256 requires inputs <= 32 byte")
}

a := new(uint256.Int).SetBytes(cx.Stack[prev].Bytes)
b := new(uint256.Int).SetBytes(cx.Stack[last].Bytes)

result := new(uint256.Int).Mod(a, b)

resultBytes := result.Bytes32()
cx.Stack[prev].Bytes = resultBytes[:]
cx.Stack = cx.Stack[:last]

return nil
}

func opSqrt256(cx *EvalContext) error {
last := len(cx.Stack) - 1

if len(cx.Stack[last].Bytes) > 32 {
return errors.New("sqrt256 requires input <= 32 byte")
}

a := new(uint256.Int).SetBytes(cx.Stack[last].Bytes)
result := new(uint256.Int).Sqrt(a)

resultBytes := result.Bytes32()
cx.Stack[last].Bytes = resultBytes[:]
return nil
}

func opAddw(cx *EvalContext) error {
last := len(cx.Stack) - 1
prev := last - 1
Expand Down
69 changes: 69 additions & 0 deletions data/transactions/logic/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6195,3 +6195,72 @@ func TestMaxTxGroup(t *testing.T) {

require.Equal(t, config.MaxTxGroupSize, maxTxGroupSize)
}

func TestAdd256(t *testing.T) {
partitiontest.PartitionTest(t)

t.Parallel()
testAccepts(t, "byte 0x01; byte 0x01; add256; byte 0x0000000000000000000000000000000000000000000000000000000000000002 ; ==", 11)
testPanics(t, "byte 0xF000000000000000000000000000000000000000000000000000000000000000; byte 0xF000000000000000000000000000000000000000000000000000000000000000; add256", 11, "overflowed")
}

func TestSub256(t *testing.T) {
partitiontest.PartitionTest(t)

t.Parallel()
testAccepts(t, "byte 0x02; byte 0x01; sub256; byte 0x0000000000000000000000000000000000000000000000000000000000000001 ; ==", 11)
testPanics(t, "byte 0x01; byte 0x02; sub256", 11, "underflowed")
}

func TestMul256(t *testing.T) {
partitiontest.PartitionTest(t)

t.Parallel()
testAccepts(t, "byte 0x02; byte 0x03; mul256; byte 0x0000000000000000000000000000000000000000000000000000000000000006 ; ==", 11)
testPanics(t, "byte 0xF000000000000000000000000000000000000000000000000000000000000000; byte 0x02; mul256", 11, "overflowed")
}

func TestDiv256(t *testing.T) {
partitiontest.PartitionTest(t)

t.Parallel()
testAccepts(t, "byte 0x06; byte 0x03; div256; byte 0x0000000000000000000000000000000000000000000000000000000000000002 ; ==", 11)
}

func TestMod256(t *testing.T) {
partitiontest.PartitionTest(t)

t.Parallel()
testAccepts(t, "byte 0x03; byte 0x02; mod256; byte 0x0000000000000000000000000000000000000000000000000000000000000001 ; ==", 11)
}

func TestSqrt256(t *testing.T) {
partitiontest.PartitionTest(t)

t.Parallel()
testAccepts(t, "byte 0x09; sqrt256; byte 0x0000000000000000000000000000000000000000000000000000000000000003 ; ==", 11)
}

func BenchmarkUint256Math(b *testing.B) {
u256 := "byte 0x1337;"

ops := []string{
"add256",
"sub256",
"mul256",
"div256",
"mod256",
}

for _, op := range ops {
b.Run(op, func(b *testing.B) {
b.ReportAllocs()
benchmarkOperation(b, "", u256+u256+op+"; pop", "int 1")
})
}

b.Run("sqrt256", func(b *testing.B) {
b.ReportAllocs()
benchmarkOperation(b, "", u256+"sqrt256; pop", "int 1")
})
}
8 changes: 8 additions & 0 deletions data/transactions/logic/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,14 @@ var OpSpecs = []OpSpec{
costByField("g", &EcGroups, []int{
BN254g1: 630, BN254g2: 3_300,
BLS12_381g1: 1_950, BLS12_381g2: 8_150})},

// uint256 math
{0xf0, "add256", opAdd256, proto("bb:b{32}"), 11, costly(2)},
{0xf1, "sub256", opSub256, proto("bb:b{32}"), 11, costly(2)},
{0xf2, "mul256", opMul256, proto("bb:b{32}"), 11, costly(2)},
{0xf3, "div256", opDiv256, proto("bb:b{32}"), 11, costly(2)},
{0xf4, "mod256", opMod256, proto("bb:b{32}"), 11, costly(2)},
{0xf5, "sqrt256", opSqrt256, proto("b:b{32}"), 11, costly(2)},
}

// OpcodesByVersion returns list of opcodes available in a specific version of TEAL
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/go-querystring v1.0.0
github.com/gorilla/mux v1.8.0
github.com/holiman/uint256 v1.3.0
github.com/ipfs/go-log v1.0.5
github.com/ipfs/go-log/v2 v2.5.1
github.com/jmoiron/sqlx v1.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/hashicorp/golang-lru/v2 v2.0.5 h1:wW7h1TG88eUIJ2i69gaE3uNVtEPIagzhGvHgwfx2Vm4=
github.com/hashicorp/golang-lru/v2 v2.0.5/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/holiman/uint256 v1.3.0 h1:4wdcm/tnd0xXdu7iS3ruNvxkWwrb4aeBQv19ayYn8F4=
github.com/holiman/uint256 v1.3.0/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
Expand Down
Loading