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

Minimal integer -(2**256) makes FunC compilation fail #339

Closed
anton-trunov opened this issue May 17, 2024 · 5 comments · Fixed by #352
Closed

Minimal integer -(2**256) makes FunC compilation fail #339

anton-trunov opened this issue May 17, 2024 · 5 comments · Fixed by #352
Assignees
Labels
bug Something isn't working or isn't right scope: const-eval The constant and partial evaluation optimization mechanisms
Milestone

Comments

@anton-trunov
Copy link
Member

The following snippet

contract Test {

    get fun minInt(): Int {
        // -(2**256)
        return -115792089237316195423570985008687907853269984665640564039457584007913129639936;
    }
}

produces the following FunC compilation error:

Func compilation error: /dist/tact_Test.code.fc:20:23: error: invalid integer constant `115792089237316195423570985008687907853269984665640564039457584007913129639936`
return ($self, (- 115792089237316195423570985008687907853269984665640564039457584007913129639936))

Reported by @novusnota.

@anton-trunov anton-trunov added the bug Something isn't working or isn't right label May 17, 2024
@novusnota
Copy link
Member

novusnota commented May 17, 2024

Additional context: tact-lang/tact-docs#206 (comment)

This works ($-2^{256} + 1$):

dump(
  -(pow2(255) - 1 + pow2(255))  // -(2**256 - 1)
);

This throws an exit code 4, while it should've fit ($-2^{256}$):

dump(
  -(pow2(255) - 1 + pow2(255)) // -(2**256 - 1) - 1
  - 1
);

Wolfram Alpha link

@anton-trunov anton-trunov added this to the v1.3.1 milestone May 17, 2024
@Gusarich
Copy link
Member

Gusarich commented May 17, 2024

in both cases (#339 (comment) and #339 (comment)) you compute positive number first and then negate it via - operator.

integers in TVM are signed 257-bit, which are numbers in range $-2^{256}$; $2^{256}-1$, so $2^{256}$ doesn't fit.

I guess we have to make some change in Tact's way of handling integer constants in order to fix the problem.

@Gusarich Gusarich self-assigned this May 17, 2024
@novusnota
Copy link
Member

you compute positive number first and then negate it via - operator.

Hmm, I definitely compute $2^{256} - 1$ first, such that it fits, then negate it — and the first example works. But the second unexpectedly fails.

I guess we have to make some change in Tact's way of handling integer constants in order to fix the problem.

May be the case indeed

@anton-trunov
Copy link
Member Author

-115792089237316195423570985008687907853269984665640564039457584007913129639936 is an integer literal (there is no negation per se) in Tact and it should be treated as such in FunC as well

@Gusarich
Copy link
Member

Gusarich commented May 17, 2024

interestingly, defining integer constants with these values works (for all of the snippets above), but trying to pass the value without an intermediate constant variable produces an error.

here's the result (in the first case integer literal is being generated correctly and in the second case incorrectly)

(tuple, int) $ConstantTester$_fun_something19(tuple $self) impure inline_ref {
    var ($self) = $self;
    return ($self, -115792089237316195423570985008687907853269984665640564039457584007913129639936);
}

(tuple, int) $ConstantTester$_fun_minInt1(tuple $self) impure inline_ref {
    var ($self) = $self;
    return ($self, (- 115792089237316195423570985008687907853269984665640564039457584007913129639936));
}

@anton-trunov anton-trunov added the scope: const-eval The constant and partial evaluation optimization mechanisms label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working or isn't right scope: const-eval The constant and partial evaluation optimization mechanisms
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants