cairo icon indicating copy to clipboard operation
cairo copied to clipboard

bug: Multiple division assignment failing

Open neotheprogramist opened this issue 1 year ago • 10 comments

Bug Report

Cairo version:

v2.4.4

Current behavior:

When repeating the assignment let value = 1 / 1; 255 times or more in the code, the following error is encountered: Error: #3585->#3586: Got 'Offset overflow' error while moving [3].

Expected behavior:

There should be no error or limit imposed on the number of times a division assignment can be executed in the code.

Steps to reproduce:

  1. Clone the repository:
    git clone https://github.com/neotheprogramist/cairo-playground
    
  2. Run the command:
    scarb cairo-run
    
    At this stage, the program should work correctly and return 1.
  3. Open src/gen.cairo and uncomment line 265.
  4. Run the command again:
    scarb cairo-run
    
    This time, the program fails and returns the error:
    Error: #3585->#3586: Got 'Offset overflow' error while moving [3].
    

Related code:

https://github.com/neotheprogramist/cairo-playground

Other information:

The issue does not occur when performing the operation in a loop, regardless the n

let randoms1 = array![rand(), rand(), ..., rand()];
let randoms2 = array![rand(), rand(), ..., rand()];
let mut value = 0;
let mut i = 0;
loop {
    if i == n {
        break;
    }
    value = randoms1[i] / randoms2[i];
    i += 1;
};

neotheprogramist avatar Jan 18 '24 11:01 neotheprogramist

This has nothing to do with the number of divisions happening in the code - this is all about the number of variables you define in the same scope - this division imposes a large AP-change, so you are bound by the number of such changes you perform.

Do you have an actual real world example where you ran into it?

orizi avatar Jan 18 '24 18:01 orizi

@orizi Yes here I've needed to introduce a loop to do workaround.

neotheprogramist avatar Jan 18 '24 18:01 neotheprogramist

In any case - even in generated code - i'd recommend against creating huge blocks of code especially if it creates thousands and thousands of variables that are all usable in the same scope.

orizi avatar Jan 18 '24 21:01 orizi

@orizi Sure, we've already parsed that and created a loop - let's treat this issue only as a notification that such restriction exists though.

neotheprogramist avatar Jan 19 '24 12:01 neotheprogramist

Hi Ori, I'm trying to write some tests for Fq6 (cubic over quadratic) such that I have Fq6 comprised of 3 Fq2 and finally the quadratic Fq2 of 2 Fq (represented by u256).

I have these tests,

#[test]
#[available_gas(5000000)]
fn mul() {
    // Having both of these causes this error:
    // #24531->#24532: Got 'Offset overflow' error while moving [62].
    let a = fq6(34, 645, 20, 55, 140, 105);
    let b = fq6(25, 45, 11, 43, 86, 101);
    let c = fq6(9, 600, 31, 12, 54, 4);

    // Having two of these cause the overflow error
    let ab = a * b;
    let bc = b * c;

    // This line line after the two above causes
    // Failed to cast from 63344.
    assert(ab * c == a * bc, 'incorrect mul');
}

I move ab and bc computation to separate scope without sharing any vars between them and it works with assert commented out. But of assert I need to share the previous result and I'm not able to avoid the error.

shramee avatar Jan 22 '24 15:01 shramee

i'm assuming fq6 is huge - can you try adding core::internal::revoke_ap_tracking() as the first statement in it?

orizi avatar Jan 22 '24 15:01 orizi

Results in same error...

shramee avatar Jan 22 '24 16:01 shramee

can you add the same for your Mul::mul implementation?

orizi avatar Jan 22 '24 16:01 orizi

Aah that did it!!!! ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ Thanks so much!!

image

shramee avatar Jan 22 '24 17:01 shramee

glad to help - i guess we need to track this and auto add this sort of thing. as you seem to run into it more often lately.

orizi avatar Jan 22 '24 17:01 orizi