Skip to content

Commit

Permalink
Merge pull request #3599 from Slava0135/fix-modmul
Browse files Browse the repository at this point in the history
vm: fix modmul operation
  • Loading branch information
AnnaShaleva authored Oct 7, 2024
2 parents c960a7e + 9aca090 commit 1115193
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
8 changes: 5 additions & 3 deletions pkg/compiler/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,22 @@ func TestOpcode(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
func Main() []int {
r := make([]int, 5)
r := make([]int, 6)
r[0] = math.ModMul(3, 4, 5)
r[1] = math.ModMul(-3, 4, 5)
r[2] = math.ModMul(3, 4, -5)
r[3] = math.ModMul(-3, 4, -5)
r[4] = math.ModMul(0, 4, 5)
r[5] = math.ModMul(100, -1, -91)
return r
}`
eval(t, src, []stackitem.Item{
stackitem.Make(2),
stackitem.Make(3),
stackitem.Make(-2),
stackitem.Make(2),
stackitem.Make(3),
stackitem.Make(-2),
stackitem.Make(0),
stackitem.Make(-9),
})
})
t.Run("MODPOW", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
x1 := v.estack.Pop().BigInt()

res := new(big.Int).Mul(x1, x2)
v.estack.PushItem(stackitem.NewBigInteger(res.Mod(res, modulus)))
v.estack.PushItem(stackitem.NewBigInteger(res.Rem(res, modulus)))

case opcode.MODPOW:
modulus := v.estack.Pop().BigInt()
Expand Down
5 changes: 3 additions & 2 deletions pkg/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,10 @@ func TestMODMUL(t *testing.T) {
t.Run("bad, zero mod", getTestFuncForVM(prog, nil, 1, 2, 0))
t.Run("good, positive base", getTestFuncForVM(prog, 2, 3, 4, 5))
t.Run("good, zero base", getTestFuncForVM(prog, 0, 0, 4, 5))
t.Run("good, negative base", getTestFuncForVM(prog, 3, -3, 4, 5))
t.Run("good, negative base", getTestFuncForVM(prog, -2, -3, 4, 5))
t.Run("good, positive base, negative mod", getTestFuncForVM(prog, 2, 3, 4, -5))
t.Run("good, negative base, negative mod", getTestFuncForVM(prog, 3, -3, 4, -5))
t.Run("good, negative base, negative mod", getTestFuncForVM(prog, -2, -3, 4, -5))
t.Run("good, positive base, negative multiplier, negative mod", getTestFuncForVM(prog, -9, 100, -1, -91))
}

func TestMODPOW(t *testing.T) {
Expand Down

0 comments on commit 1115193

Please sign in to comment.