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

Miscompilation in the Cairo backend #3237

Closed
lukaszcz opened this issue Dec 5, 2024 · 0 comments · Fixed by #3238
Closed

Miscompilation in the Cairo backend #3237

lukaszcz opened this issue Dec 5, 2024 · 0 comments · Fixed by #3238

Comments

@lukaszcz
Copy link
Collaborator

lukaszcz commented Dec 5, 2024

The following is a minimal example of a program that gets miscompiled:

module optbug;

import Stdlib.Prelude open;

type Resource :=
  mkResource@{
    logic : Field;
    label : Field;
    quantity : Field;
    data : Field;
    eph : Bool;
    nonce : Field;
    npk : Field;
    rseed : Field;
  };

type LogicResult :=
  mkResult@{
    -- nullifier of input resource or commitment of output resource
    self_resource_id : Field;
  };

poseidonHashList' : List Field -> Field
  | [] := 0
  | (h :: t) := h +  poseidonHashList' t;

{-# inline: false #-}
poseidonHash2' (x y : Field) : Field := x + y;

main
  (self_resource : Resource)
  (resource_nf_key : Field)
  (merkle_path : List (Pair Field Bool))
  : LogicResult :=

  -- Check the self_resource resource commitment:
  let
    generated_npk : Field := poseidonHash2' resource_nf_key 0;

    is_output_resource :=
      case merkle_path of
        -- merkle_path can not be empty
        | nil := true
        | (_, is_left) :: t := is_left;

    actual_npk :=
      if
        | is_output_resource := Resource.npk self_resource
        | else := generated_npk;

    resource_cm :=
      poseidonHashList'
        [
          actual_npk;
          Resource.nonce self_resource;
          Resource.quantity self_resource;
        ];

    -- Generate the nullifier of self_resource resource
    resource_nullifier_ :=
      poseidonHashList'
        [
          resource_nf_key;
          resource_cm;
        ];
  in mkResult@{
       self_resource_id := resource_nullifier_;
     };

The problem is with the translation from JuvixReg to CASM. One sees that the generated CASM code is incorrect (inconsistent order of arguments passed to the next basic block in br branches). The JuvixReg code is still correct.

Related issue: anoma/aarm-cairo#37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant