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

u256 implementation #4794

Closed
wants to merge 9 commits into from
Closed

u256 implementation #4794

wants to merge 9 commits into from

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Jul 12, 2023

Description

This PR implements complete support for u256 through the stack:

  • Literals can be suffixed with u256.
  • Binary operations are converted to its specialized 256 fuel opcodes. Currently, no optimizations are done and all operands are indirect. Apart from shifts where the shift itself is not indirect.
  • This makes the struct U256 (capital U) obsolete, but it does not remove it yet.

u256 works as any other copy-able value. So passing by value is memcpy-ing all 32 bytes.
A particular complication is that all wide operations (WQOP, WQML, WQDIV etc...) have their write register as indirect. Which means that I have to allocate stack space for their result.

Single-Step Debugging

This PR also implements an option to run e2e tests using single-step debugging. It is a handy tool to understand what is happening. It prints the current instruction, the before and after of which affect register, and on an ad-hoc manner also the affected memory.

The worst part is that there is no way to set single-stepping. I had to use a transmute. This part is totally optional and I can remove this.

// Horrible, but there is not mut access to the debugger, which means there
// is no way to turn single stepping on.
let dbg: &mut Debugger = unsafe { std::mem::transmute(i.debugger()) };
dbg.set_single_stepping(true);

Given that this is completely apart. My suggestion is actually to detach this to a different PR.

Todo

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@xunilrj xunilrj changed the title literal_to_literal working u256 implementation Jul 20, 2023
@xunilrj xunilrj force-pushed the xunilrj/u256 branch 2 times, most recently from cf7dcb8 to bf3db1f Compare July 21, 2023 10:49
@bitzoic bitzoic mentioned this pull request Jul 24, 2023
7 tasks
@xunilrj xunilrj force-pushed the xunilrj/u256 branch 3 times, most recently from 74338ee to dee029c Compare July 24, 2023 14:13
@xunilrj xunilrj mentioned this pull request Jul 31, 2023
7 tasks
xunilrj added a commit that referenced this pull request Jul 31, 2023
## Description

This PR is part of #4794. It
implements the bare minimum to support `u256` constants across the
stack.

They are represented in sway in hex literals with `u256` suffixes. Hex
without suffix will still be `b256`, for backward compatibility until we
implement `u256` completely. Then we can decide what to do.

There are multiple places in the code that will fail if the literal does
not fit in u64. This will be fixed later in a specific PR for big u256.
Some places have temporary `unwrap()`, that will be removed later. I can
try to remove them now, if necessary.

I am leaving all documentation updates for when everything is
implemented.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Anton Trunov <[email protected]>
@xunilrj xunilrj mentioned this pull request Aug 1, 2023
7 tasks
xunilrj added a commit that referenced this pull request Aug 2, 2023
## Description

This PR is part of #4794. It
implements support for u256 add operators.

The IR generated is a vanilla `add` operator as any other integer.

```rust
script {
    entry fn main() -> u256, !1 {
        entry():
        v0 = const u256 0x0000000000000000000000000000000000000000000000000000000000000001, !2
        v1 = const u256 0x0000000000000000000000000000000000000000000000000000000000000002, !3
        v2 = call add_0(v0, v1), !4
        ret u256 v2
    }
}
```

This will then be transformed by `miscdemotion` pass to something like:

```rust
script {
    entry fn main() -> u256 {
         local u256 __wide_lhs
         local mut u256 __wide_result
         local u256 __wide_rhs
 
        entry():       
         v0 = get_local ptr u256, __wide_lhs, !0
         v1 = const u256 0x0000000000000000000000000000000000000000000000000000000000000001, !0
         store v1 to v0, !0
         v2 = get_local ptr u256, __wide_rhs, !0
         v3 = const u256 0x0000000000000000000000000000000000000000000000000000000000000002, !0
         store v3 to v2, !0
         v4 = get_local ptr u256, __wide_result, !0
         wide add v0, v2 to v4, !0
         v5 = load v4, !0
         ret u256 v5
    }
}
```

Mind the `wide add` here. It is a Fuel specific operation, giving space
to other targets to implement this differently.


## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
@xunilrj xunilrj mentioned this pull request Aug 3, 2023
12 tasks
xunilrj added a commit that referenced this pull request Aug 7, 2023
## Description

This PR is part of #4794 and
implements more operators for u256:
- [x] sub
- [x] mul
- [x] div
- [x] comparisions
- [x] mod

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Vaivaswatha N <[email protected]>
@xunilrj xunilrj mentioned this pull request Aug 7, 2023
7 tasks
@anton-trunov
Copy link
Contributor

Unless I'm missing something, the Todo needs to include const expressions evaluation. We want that for u256, right?

xunilrj added a commit that referenced this pull request Aug 8, 2023
## Description

This PR is part of #4794. It
implements the `not` operator for u256 and allows values bigger than
`u64`.

To support that it implements `U256` inside `sway-types`. For now, it is
a bare minimum wrapper around `BigUint`. We may use fuel macro in the
future, which implements all necessary functions.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Anton Trunov <[email protected]>
@xunilrj xunilrj mentioned this pull request Aug 9, 2023
7 tasks
xunilrj added a commit that referenced this pull request Aug 14, 2023
## Description

This PR is part of #4794.

It implements left and right shifts. These have a little difference
because the `rhs` is always `u64`, which means that the WQOP used the
`rhs` is not indirect.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
xunilrj added a commit that referenced this pull request Aug 23, 2023
## Description

This PR is part of #4794. It
implements const eval and optimizations for `u256`.


## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Joshua Batty <[email protected]>
Co-authored-by: IGI-111 <[email protected]>
xunilrj added a commit that referenced this pull request Aug 30, 2023
## Description

This PR is part of #4794. It
implements the missing operators: bitwise and comparison.


## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Anton Trunov <[email protected]>
@xunilrj xunilrj mentioned this pull request Sep 12, 2023
7 tasks
anton-trunov pushed a commit that referenced this pull request Sep 21, 2023
## Description

This PR is part of #4794 and
updates documentation for `u256` and improves test for edge cases.

Code also prints more info when tests run in `verbose` mode.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: IGI-111 <[email protected]>
@xunilrj xunilrj mentioned this pull request Sep 22, 2023
7 tasks
IGI-111 pushed a commit that referenced this pull request Sep 26, 2023
## Description

This PR is part of #4794. `u256`
was not being considered a reference type which was creating some issues
when using it in `storage`. This PR fixes this problem.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
@xunilrj xunilrj mentioned this pull request Sep 27, 2023
7 tasks
xunilrj added a commit that referenced this pull request Oct 3, 2023
## Description

This PR is part of #4794. It is the
minimum implementation for deprecation. Pretty much only what was needed
for deprecating `U256`.

It also minimally implements `#[allow(deprecated)]`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
xunilrj added a commit that referenced this pull request Oct 16, 2023
## Description

This PR is part of #4794. It fix an
issue necessary to use `u256` on configurables.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
IGI-111 pushed a commit that referenced this pull request Oct 24, 2023
## Description

This PR is part of #4794 and it
implements `pow` for u256. It is using the same algorithm as Rust stdlib
https://github.com/rust-lang/rust/blob/193e8a196b7700542473a477effd8c6c5786f8de/library/core/src/num/uint_macros.rs#L1976.

#4900 implements `pow` for `U256`
(upper case `U`) which is being deprecated.

Closes #4449

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Andrew O'Brien <[email protected]>
@xunilrj xunilrj closed this Oct 24, 2023
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 this pull request may close these issues.

2 participants