Merge pull request #826 from nspcc-dev/fix/mod

vm: use truncated division in MOD
This commit is contained in:
Roman Khimov 2020-04-06 10:03:40 +03:00 committed by GitHub
commit 48e619e26d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View file

@ -794,7 +794,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
a := v.estack.Pop().BigInt() a := v.estack.Pop().BigInt()
v.checkBigIntSize(a) v.checkBigIntSize(a)
v.estack.PushVal(new(big.Int).Mod(a, b)) v.estack.PushVal(new(big.Int).Rem(a, b))
case opcode.SHL, opcode.SHR: case opcode.SHL, opcode.SHR:
b := v.estack.Pop().BigInt().Int64() b := v.estack.Pop().BigInt().Int64()

View file

@ -844,11 +844,10 @@ func TestMULBigResult(t *testing.T) {
checkVMFailed(t, vm) checkVMFailed(t, vm)
} }
func TestDiv(t *testing.T) { func TestDivMod(t *testing.T) {
prog := makeProgram(opcode.DIV) runCase := func(op opcode.Opcode, p, q, result int64) func(t *testing.T) {
runCase := func(p, q, result int64) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
vm := load(prog) vm := load(makeProgram(op))
vm.estack.PushVal(p) vm.estack.PushVal(p)
vm.estack.PushVal(q) vm.estack.PushVal(q)
runVM(t, vm) runVM(t, vm)
@ -856,10 +855,19 @@ func TestDiv(t *testing.T) {
} }
} }
t.Run("positive/positive", runCase(5, 2, 2)) t.Run("DIV", func(t *testing.T) {
t.Run("positive/negative", runCase(5, -2, -2)) t.Run("positive/positive", runCase(opcode.DIV, 5, 2, 2))
t.Run("negative/positive", runCase(-5, 2, -2)) t.Run("positive/negative", runCase(opcode.DIV, 5, -2, -2))
t.Run("negative/negative", runCase(-5, -2, 2)) t.Run("negative/positive", runCase(opcode.DIV, -5, 2, -2))
t.Run("negative/negative", runCase(opcode.DIV, -5, -2, 2))
})
t.Run("MOD", func(t *testing.T) {
t.Run("positive/positive", runCase(opcode.MOD, 5, 2, 1))
t.Run("positive/negative", runCase(opcode.MOD, 5, -2, 1))
t.Run("negative/positive", runCase(opcode.MOD, -5, 2, -1))
t.Run("negative/negative", runCase(opcode.MOD, -5, -2, -1))
})
} }