Skip to content

Commit

Permalink
pkg/assembler: allow JNZ with ap_change (#185)
Browse files Browse the repository at this point in the history
The cairo1 compiler generates jnz with ap++ for some
range-check related code. To support this code in our
assembler, the ap_change check should be removed.

See the referenced issue for more context.

Fixes #184
quasilyte authored Jan 24, 2024
1 parent 69b6036 commit 8be1de8
Showing 2 changed files with 7 additions and 11 deletions.
10 changes: 6 additions & 4 deletions pkg/assembler/instruction.go
Original file line number Diff line number Diff line change
@@ -332,13 +332,15 @@ func decodeInstructionFlags(instruction *Instruction, flags uint16) error {
}
instruction.Opcode = Opcode(opcode)

// for pc udpate Jnz, res should be unconstrainded, no opcode, and ap should update with Imm
// for pc udpate Jnz, res should be unconstrainded, no opcode;
// it used to have an ap update check, but new Cairo1 compiler
// emits Jnz with ap_add1 sometimes
// See #184
if instruction.PcUpdate == PcUpdateJnz &&
(instruction.Res != Unconstrained ||
instruction.Opcode != OpCodeNop ||
instruction.ApUpdate != SameAp) {
instruction.Opcode != OpCodeNop) {
return fmt.Errorf(
"jnz opcode must have unconstrained res logic, no opcode, and no ap change",
"jnz opcode must have unconstrained res logic and no opcode",
)
}

8 changes: 1 addition & 7 deletions pkg/assembler/instruction_test.go
Original file line number Diff line number Diff line change
@@ -214,9 +214,8 @@ func TestInvalidOpcode(t *testing.T) {
func TestPcUpdateJnzInvalid(t *testing.T) {
instructionInvalidRes := new(f.Element).SetBytes([]byte{0x06, 0x27, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00})
instructionInvalidOpcode := new(f.Element).SetBytes([]byte{0x16, 0x07, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00})
instructionInvalidApUpdate := new(f.Element).SetBytes([]byte{0x06, 0x07, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00})

expectedError := "jnz opcode must have unconstrained res logic, no opcode, and no ap change"
expectedError := "jnz opcode must have unconstrained res logic and no opcode"

_, err := DecodeInstruction(instructionInvalidRes)

@@ -227,11 +226,6 @@ func TestPcUpdateJnzInvalid(t *testing.T) {

require.Error(t, err)
assert.ErrorContains(t, err, expectedError)

_, err = DecodeInstruction(instructionInvalidApUpdate)

require.Error(t, err)
assert.ErrorContains(t, err, expectedError)
}

func TestCallInvalidApUpdate(t *testing.T) {

0 comments on commit 8be1de8

Please sign in to comment.