Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while decoding "0xa0680017fff8000" from Cairo1-produced CASM #184

Closed
quasilyte opened this issue Jan 22, 2024 · 2 comments · Fixed by #185
Closed

Error while decoding "0xa0680017fff8000" from Cairo1-produced CASM #184

quasilyte opened this issue Jan 22, 2024 · 2 comments · Fixed by #185
Assignees

Comments

@quasilyte
Copy link
Contributor

quasilyte commented Jan 22, 2024

While decoding 0xa0680017fff8000 with assembler.DecodeInstruction:

flags: jnz opcode must have unconstrained res logic, no opcode, and no ap change

It could be the case that CASM changed a bit since Cairo0 times or our assembler needs to relax some of its restrictions.
I'll take a look at the Cairo1 compiler code to know what this new combination of flags should signify.

This issue will probably resolved in the same context as #183, but I may send a separate PR just for this fix (if any).

thoth disassembles it into an ordinary JNZ:

offset 0:          JNZ                 7                   # JMP 7

The input bytecode looks like this (we only care about the first value in this array, so the output is truncated):

{
  "prime": "0x800000000000011000000000000000000000000000000000000000000000001",
  "compiler_version": "2.4.1",
  "bytecode": [
    "0xa0680017fff8000",
    "0x7",

Instruction fields:

uint64 value: 722405534170316800
off_dst: -32768 (1000000000000000)
off_op0: 32767 (111111111111111)
off_op1: -32767 (1000000000000001)
dst_reg: 0 (0)
op0_reg: 1 (1)
op1_src: 1 (1)
res_logic: 0 (0)
pc_update: 4 (100)
ap_update: 2 (10)
opcode: 0 (0)
bits: 1000000000000000 111111111111111 1000000000000001 0 1 1 0 100 10 0
@quasilyte quasilyte self-assigned this Jan 22, 2024
@quasilyte
Copy link
Contributor Author

It looks like the ap_update check might be redundant.
thoth recognizes the ap_update as ADD1, but it doesn't fail the decompilation.

@quasilyte
Copy link
Contributor Author

It seems that ap update is permitted by jnz:
https://github.com/starkware-libs/cairo/blob/2ce586ec491d94022459fbff742f7838511a3e8b/crates/cairo-lang-casm/src/assembler_test.rs#L125

Let's take this cairo1 program for example:

fn f() -> bool {
  let a: i32 = 1;
  let b: i32 = 2;
  let x = a < b;
  return x;
}

We'll get this CASM for it:

[ap + 0] = 1, ap++;
[ap + 0] = 2, ap++;
[ap + -2] = [ap + 1] + [ap + -1], ap++;
%{ memory[ap + -1] = memory[ap + 0] < 4294967296 %}
jmp rel 7 if [ap + -1] != 0, ap++;
[ap + 0] = [ap + -1] + 340282366920938463463374607431768211456, ap++;
[ap + -1] = [[fp + -3] + 0];
jmp rel 10;
[ap + -1] = [[fp + -3] + 0];
ap += 1;
[ap + 0] = [fp + -3] + 1, ap++;
[ap + 0] = 0, ap++;
ret;
[ap + 0] = [fp + -3] + 1, ap++;
[ap + 0] = 1, ap++;
ret;

Note the conditional jump with , ap++ part.

quasilyte added a commit that referenced this issue Jan 22, 2024
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 added a commit that referenced this issue Jan 23, 2024
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
cicr99 pushed a commit that referenced this issue Jan 24, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant