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

fixed bitlen calc #18

Merged
merged 3 commits into from
Oct 9, 2024
Merged

fixed bitlen calc #18

merged 3 commits into from
Oct 9, 2024

Conversation

dovgopoly
Copy link
Contributor

Hi, I noticed a potential bug in the _add function when recalculating bitlen. Consider a scenario where the most significant word is already 1 and remains unchanged. In this case, the algorithm incorrectly calculates the result bitlen.

stack.add(
        "0x010000000000000000000000000000000000000000000000000000000000000001",
        "0x01"
);

The result bitlen is 258 but it's expected to be 257.

// if(msword==1 || msword>>(max_bitlen % 256)==1):
if or( eq(msword, 1), eq(shr(mod(max_bitlen,256),msword),1) ) {
// if(carry==1 || msword>>(max_bitlen % 256)==1):
if or( eq(carry, 1), eq(shr(mod(max_bitlen,256),msword),1) ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be fairly simple to illustrate with an example, right ?

Copy link
Contributor Author

@dovgopoly dovgopoly Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say we have two normalized big numbers a = [a2, a1] and b = [b1].
a1 = 0000000000000000000000000000000000000000000000000000000000000001
a2 = 0000000000000000000000000000000000000000000000000000000000000001
b1 = 0000000000000000000000000000000000000000000000000000000000000001
The result would be r = [r2, r1] = [a2, a1 + b1].
r1 = 0000000000000000000000000000000000000000000000000000000000000002
r2 = 0000000000000000000000000000000000000000000000000000000000000001

Here you set the most significant word of the result to 1 if an overflow occurs. But in some cases msword equals 1 even without an overflow, as shown in the example (r2). This is why I suggest using carry instead of msword to determine whether to increase the bit length.

Copy link
Collaborator

@riordant riordant Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your contribution!
would you mind adding a unit test (BigNumbers.t.sol) implementing this example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riordant Done

@riordant riordant force-pushed the master branch 4 times, most recently from 1e1aee1 to f382bcf Compare October 9, 2024 08:58
@riordant riordant closed this Oct 9, 2024
@riordant riordant reopened this Oct 9, 2024
@riordant riordant merged commit c77ffd7 into firoorg:master Oct 9, 2024
1 check passed
@riordant
Copy link
Collaborator

riordant commented Oct 9, 2024

@dovgopoly merged :)

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.

3 participants