Merge pull request #3599 from Slava0135/fix-modmul

vm: fix modmul operation
This commit is contained in:
Anna Shaleva 2024-10-07 12:43:45 +03:00 committed by GitHub
commit 11151938b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 6 deletions

View file

@ -343,20 +343,22 @@ func TestOpcode(t *testing.T) {
src := `package foo src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/math" import "github.com/nspcc-dev/neo-go/pkg/interop/math"
func Main() []int { func Main() []int {
r := make([]int, 5) r := make([]int, 6)
r[0] = math.ModMul(3, 4, 5) r[0] = math.ModMul(3, 4, 5)
r[1] = math.ModMul(-3, 4, 5) r[1] = math.ModMul(-3, 4, 5)
r[2] = math.ModMul(3, 4, -5) r[2] = math.ModMul(3, 4, -5)
r[3] = math.ModMul(-3, 4, -5) r[3] = math.ModMul(-3, 4, -5)
r[4] = math.ModMul(0, 4, 5) r[4] = math.ModMul(0, 4, 5)
r[5] = math.ModMul(100, -1, -91)
return r return r
}` }`
eval(t, src, []stackitem.Item{ eval(t, src, []stackitem.Item{
stackitem.Make(2), stackitem.Make(2),
stackitem.Make(3), stackitem.Make(-2),
stackitem.Make(2), stackitem.Make(2),
stackitem.Make(3), stackitem.Make(-2),
stackitem.Make(0), stackitem.Make(0),
stackitem.Make(-9),
}) })
}) })
t.Run("MODPOW", func(t *testing.T) { t.Run("MODPOW", func(t *testing.T) {

View file

@ -1030,7 +1030,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
x1 := v.estack.Pop().BigInt() x1 := v.estack.Pop().BigInt()
res := new(big.Int).Mul(x1, x2) 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: case opcode.MODPOW:
modulus := v.estack.Pop().BigInt() modulus := v.estack.Pop().BigInt()

View file

@ -738,9 +738,10 @@ func TestMODMUL(t *testing.T) {
t.Run("bad, zero mod", getTestFuncForVM(prog, nil, 1, 2, 0)) 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, positive base", getTestFuncForVM(prog, 2, 3, 4, 5))
t.Run("good, zero base", getTestFuncForVM(prog, 0, 0, 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, 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) { func TestMODPOW(t *testing.T) {