diff --git a/pkg/compiler/syscall_test.go b/pkg/compiler/syscall_test.go index b6d735f0a..5ce03d9e2 100644 --- a/pkg/compiler/syscall_test.go +++ b/pkg/compiler/syscall_test.go @@ -117,4 +117,20 @@ func TestOpcode(t *testing.T) { }` eval(t, src, big.NewInt(42)) }) + t.Run("POW", func(t *testing.T) { + src := `package foo + import "github.com/nspcc-dev/neo-go/pkg/interop/math" + func Main() int { + return math.Pow(2, math.Pow(3, 2)) + }` + eval(t, src, big.NewInt(512)) + }) + t.Run("SRQT", func(t *testing.T) { + src := `package foo + import "github.com/nspcc-dev/neo-go/pkg/interop/math" + func Main() int { + return math.Sqrt(math.Sqrt(101)) // == sqrt(10) == 3 + }` + eval(t, src, big.NewInt(3)) + }) } diff --git a/pkg/interop/math/math.go b/pkg/interop/math/math.go new file mode 100644 index 000000000..86cbd020d --- /dev/null +++ b/pkg/interop/math/math.go @@ -0,0 +1,14 @@ +package math + +import "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal" + +// Pow returns a^b using POW VM opcode. +// b must be >= 0 and <= 2^31-1. +func Pow(a, b int) int { + return neogointernal.Opcode2("POW", a, b).(int) +} + +// Sqrt returns positive square root of x rounded down. +func Sqrt(x int) int { + return neogointernal.Opcode1("SQRT", x).(int) +}