Skip to content

Masm for Rust's signed overflowing_div can produce wrong result #966

@mooori

Description

@mooori

Packages versions

next branch

Bug description

For example (-1i8).overflowing_div(-2i8):

Rust result: (0, false)
Rust result as bytes: [0, 0] // expected
Vm out bytes: [1, 0] // actual

Rust source

pub fn f(a: i8, b: i8) -> (i8, bool) {
    a.overflowing_div(b)
}

Potential explanation

Based on the generated test_overflowing_div_i8.hir:

  • The generated hir entrypoint has i32 arguments for what are originally i8 operands
  • Perhaps the operands have been zero-extended instead of sign-extended
    • -1i8 = 0xFF: zext yields 255i32, sext yields -1i32
    • -2i8 = 0xFE: zext yields 254i32, sext yields -2i32
    • 255/254 = 1 instead of -1/-2 = 0

How can this be reproduced?

UPDATE_EXPECT=1 cargo nextest run -p miden-integration-tests -- rust_masm_tests::instructions::test_overflowing_div_i8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions