Skip to content

Conversation

@Fethbita
Copy link
Contributor

@Fethbita Fethbita commented Dec 5, 2025

I only added the LCM for Uint for now, I think it should not be difficult to add it to other types as well. I have never used the Int type so if that is different, might take some time.

@Fethbita Fethbita changed the title Adds LCM for Uint Add LCM for Uint Dec 5, 2025
@codecov
Copy link

codecov bot commented Dec 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.83%. Comparing base (59b58ec) to head (bb886aa).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1028      +/-   ##
==========================================
- Coverage   79.87%   79.83%   -0.04%     
==========================================
  Files         163      164       +1     
  Lines       17737    17801      +64     
==========================================
+ Hits        14167    14212      +45     
- Misses       3570     3589      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Fethbita Fethbita mentioned this pull request Dec 5, 2025
42 tasks
@tarcieri tarcieri requested review from fjarri and tarcieri December 10, 2025 15:15
assert_eq!(lhs.lcm_uint(&rhs), target);
}

fn run_tests<const LIMBS: usize, const WIDE_LIMBS: usize>()
Copy link
Contributor

Choose a reason for hiding this comment

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

What's our policy on proptests? Seems like it would be useful to have one here.

Copy link
Member

Choose a reason for hiding this comment

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

I'm definitely a fan of proptests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By proptest, do you mean this proptest? I have never used it before, so what kind of tests would you like here? Generate 2 primes (it seems difficult to get it to generate primes) and calculate that the LCM output equals to multiple of the inputs? Or would it be used only for detecting whether the function crashes with any kind of input?

Copy link
Contributor

@fjarri fjarri Dec 12, 2025

Choose a reason for hiding this comment

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

I think there are two types of tests possible here. First, with primes; but you don't need filtering for those, just use random bytes as a seed for a random prime. Second, edge cases, for which you don't need primes, just random numbers. num_bigint's lcm can be used as the reference.

Copy link
Member

Choose a reason for hiding this comment

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

@Fethbita you can look here for some example protests: https://github.com/RustCrypto/crypto-bigint/blob/master/tests/uint.rs

I guess I would be okay with not having them for the initial PR, but it would definitely be good to have them as a followup, whoever authors it. It's something I can look at.

Truly random primes seem like they might need a circular dependency on crypto-primes which is doable (it can be added as a dev-dependency, I think) but a little tricky.

Another option is to write the test with a canned set of primes and select from them randomly, though if the solution space is small enough it could just be brute forced every test run and kind of defeats the point of proptest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a single test for comparing with num_bigint, using random values.

src/uint/lcm.rs Outdated

impl<const LIMBS: usize> Uint<LIMBS> {
/// Compute the least common multiple of `self` and `rhs`.
pub const fn lcm_uint<const WIDE_LIMBS: usize>(&self, rhs: &Self) -> Uint<WIDE_LIMBS>
Copy link
Member

@tarcieri tarcieri Dec 14, 2025

Choose a reason for hiding this comment

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

I'm not sure I like this _uint naming scheme, which I guess was introduced in #923

It feels to me like _uint should be the "default" for Self/Uint, so this is just fn lcm

cc @andrewwhitehead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, removed the _uint.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's kinda the same issue here as in the other PR. If there's an LCM trait, then the inherent method can clobber the trait method, making combinations like Uint/Int more difficult to use in non-generic contexts.

Copy link
Member

Choose a reason for hiding this comment

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

@andrewwhitehead that's fine so long as they have the same type signature. You can always call the inherent method with UFCS.

If they have different type signatures, that's bad.

@tarcieri tarcieri merged commit 5216e23 into RustCrypto:master Dec 18, 2025
27 checks passed
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.

4 participants