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

feat(integer): add unsigned_oveflowing_add #674

Merged
merged 1 commit into from
Nov 14, 2023
Merged

Conversation

tmontaigu
Copy link
Contributor

No description provided.

@cla-bot cla-bot bot added the cla-signed label Nov 8, 2023
Copy link

github-actions bot commented Nov 8, 2023

@slab-ci cpu_fast_test

@tmontaigu tmontaigu force-pushed the unsigned-overflowing-add branch from 635d9f0 to c168f51 Compare November 8, 2023 19:23
Copy link

github-actions bot commented Nov 8, 2023

@slab-ci cpu_fast_test

Copy link
Member

@IceTDrinker IceTDrinker left a comment

Choose a reason for hiding this comment

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

Big doubt on the add under modulus which makes me wonder if sub under modulus is correct 🤔

Comment on lines 76 to 79
fn overflowing_add_under_modulus(lhs: u64, rhs: u64, modulus: u64) -> (u64, bool) {
let result = lhs.wrapping_add(rhs);
(result % modulus, result >= modulus)
}
Copy link
Member

Choose a reason for hiding this comment

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

let's say your modulus is 2^63 + 1

I take 2^63 + 2^63 result is 2^64 or 0 in a u64, the computation overflowed but we would not see it here, am I correct ?

Maybe the sub under modulus is wrong as well 🤔

Copy link
Member

Choose a reason for hiding this comment

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

fn overflowing_sub_under_modulus(lhs: u64, rhs: u64, modulus: u64) -> (u64, bool) {
    let overflowed = rhs > lhs;
    let result = if overflowed {
        todo!() // maybe use wrapping_sub_custom mod from the unsigned integer trait
    } else {
        lhs - rhs
    };
    (result, overflowed)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, but these functions are written only for the tests where we don't go beyond a modulus of 2^16

Copy link
Member

Choose a reason for hiding this comment

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

understood, could you add the assert in the two functions to avoid issues on the value ranges ? or update them so that we don't mistakenly use them in contexts where they are not meant to be used ?

Copy link
Member

Choose a reason for hiding this comment

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

and sorry for being annoying with this but I just battled against moduli 😅 I'd rather we don't have weird surprises

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fn overflowing_add_under_modulus(lhs: u64, rhs: u64, modulus: u64) -> (u64, bool) {
    let (result, overflowed) = lhs.overflowing_add(rhs);
    (result % modulus, overfowed || result >= modulus)
}

should work for all moduluses

Copy link
Member

Choose a reason for hiding this comment

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

indeed should be good

Copy link
Contributor Author

@tmontaigu tmontaigu Nov 10, 2023

Choose a reason for hiding this comment

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

Well maybe not for non-power of two modulus that have their sum overflow u64, but we don't have that

Copy link
Member

Choose a reason for hiding this comment

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

could you take a look for the sub as well ?

@tmontaigu tmontaigu force-pushed the unsigned-overflowing-add branch from c168f51 to 34f38e8 Compare November 10, 2023 15:06
Copy link

@slab-ci cpu_fast_test

@tmontaigu tmontaigu force-pushed the unsigned-overflowing-add branch from 34f38e8 to 72f1aee Compare November 14, 2023 09:45
Copy link

@slab-ci cpu_fast_test

Copy link
Member

@IceTDrinker IceTDrinker left a comment

Choose a reason for hiding this comment

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

Thanks !

Copy link

Pull Request has been approved 🎉
Launching full test suite...
@slab-ci cpu_test
@slab-ci cpu_integer_test
@slab-ci cpu_multi_bit_test
@slab-ci cpu_wasm_test
@slab-ci csprng_randomness_testing

@tmontaigu tmontaigu merged commit f02f1fb into main Nov 14, 2023
19 checks passed
@tmontaigu tmontaigu deleted the unsigned-overflowing-add branch November 14, 2023 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants