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

Instantiating a specific integer results in an overflow error, even though it clearly fits within the specified size #199

Closed
jules opened this issue Oct 31, 2022 · 4 comments · Fixed by #200

Comments

@jules
Copy link
Contributor

jules commented Oct 31, 2022

Version
1.6

Platform
Darwin computer.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000 x86_64

Description
Attempting to instantiate the number 258664426012969093929703085429980814127835149614277183275038967946009968870203535512256352201271898244626862047232 as a Uint<384, 6> results in overflow errors at compile time.

The following code:

use ruint::{uint, Uint};

fn main() {
    println!("{}", Uint::<384, 6>::MAX);
    let _a = uint!(258664426012969093929703085429980814127835149614277183275038967946009968870203535512256352201271898244626862047232_U384);
}

Resulted in this error:

jules@computer test-uint % cargo run
   Compiling test-uint v0.1.0 (/Users/jules/workspace/test-uint)
error: proc macro panicked
 --> src/main.rs:5:14
  |
5 |     let _a = uint!(258664426012969093929703085429980814127835149614277183275038967946009968870203535512256352201271898244626862047232_U384);
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: attempt to add with overflow

error: could not compile `test-uint` due to previous error

When putting the MAX and the given integer next to each other:
39402006196394479212279040100143613805079739270465446667948293404245721771497210611414266254884915640806627990306815

258664426012969093929703085429980814127835149614277183275038967946009968870203535512256352201271898244626862047232

It's clear that the integer should at the very least fit within the bounds of 384 bits.

So far from simple checks, the compiler will allow it if the number is decremented by 1, but still error if it is incremented by 1. I have not checked any other values for now, though I am capable of successfully instantiating Uints of the same bitsize with much higher values, without any issue.

@jules
Copy link
Contributor Author

jules commented Oct 31, 2022

Upon further inspection, it appears that the panic occurs here, when constructing the limbs.

When I add some before and after prints, this is what the compiler ends up spitting out..

         // Add digit to result
         println!("a");
         limbs[0] += digit; // Never carries
         println!("b");
// Previous output trimmed for brevity
...
a
b
a
b
a
b
a
error: proc macro panicked
 --> src/main.rs:5:28
  |
5 | ...> = uint!(258664426012969093929703085429980814127835149614277183275038967946009968870203535512256352201271898244626862047232_U384);
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: attempt to add with overflow

error: could not compile `test-uint` due to previous error

@jules
Copy link
Contributor Author

jules commented Oct 31, 2022

So it appears there's a minor chance that overflow does occur in this algorithm, in which case a simple fix could be to just use overflowing_add and check for overflow, incrementing the next limb by 1 if it overflows.

@recmo
Copy link
Owner

recmo commented Nov 1, 2022

Yes, the comment about carries is only valid for power-of-two bases. In fact it is possible to construct arbitrarily long carry propagation like so:

uint!(2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936576_U384)

Basically 2**(N*64) + [0..basis)

@recmo recmo closed this as completed in #200 Nov 1, 2022
recmo added a commit that referenced this issue Nov 1, 2022
Correct carry propagation when adding digits in macro (fixes #199)
@recmo
Copy link
Owner

recmo commented Nov 1, 2022

Version 1.7.0 is published that includes the fix.

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.

2 participants