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

Remove bit precision from the public API #46

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

dvdplm
Copy link
Contributor

@dvdplm dvdplm commented Sep 19, 2024

An attempt to provide a more ergonomic API that works for both heap and stack allocated uints.

Before we had:

// `…where T: Integer + RandomBits`
let n = random_odd_integer::<T>(rng, NonZeroU32::new(bit_length).unwrap(), bits_precision);
let p = generate_prime_with_rng::<U1024>(&mut rng, 1024, 1024);

With this PR:

let n = random_odd_integer::<T>(rng, NonZeroU32::new(bit_length).unwrap());
let p = generate_prime_with_rng::<U1024>(&mut rng, 1024);

I think this is a more ergonomic public API.

Perhaps more contentiously, this PR also switches to prefer passing numeric parameters by value rather than by reference.

If reviewers think this makes the PR too noisy to review, I can split out those changes.

Copy link

codecov bot commented Sep 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.33%. Comparing base (4a4696e) to head (e8b70ba).

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #46   +/-   ##
=======================================
  Coverage   99.32%   99.33%           
=======================================
  Files           9        9           
  Lines        1340     1347    +7     
=======================================
+ Hits         1331     1338    +7     
  Misses          9        9           

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

@@ -8,29 +8,21 @@ use crate::{

/// Provides a generic way to access methods for random prime number generation
/// and primality checking, wrapping the standalone functions ([`is_prime_with_rng`] etc).
pub trait RandomPrimeWithRng {
pub trait RandomPrimeWithRng: Zeroize {
Copy link
Member

Choose a reason for hiding this comment

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

Why an additional Zeroize requirement?

@@ -59,7 +69,7 @@ impl<T: Integer + RandomMod> MillerRabin<T> {
}

/// Perform a Miller-Rabin check with a given base.
pub fn test(&self, base: &T) -> Primality {
pub fn test(&self, base: T) -> Primality {
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't seem like taking base by value here reduces the amount of cloning.

///
/// For example, a U512 type occupies 8 64-bit words, but the number `7` contained in such a type
/// has a bit length of 3 because 7 is `b111`.
pub fn bits(&self) -> u32 {
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 make it pub(crate) for now, since it's only used internally.

@@ -9,25 +9,26 @@ use rand_core::CryptoRngCore;

use crate::hazmat::precomputed::{SmallPrime, RECIPROCALS, SMALL_PRIMES};

/// Returns a random odd integer with given bit length
/// (that is, with both `0` and `bit_length-1` bits set).
/// Returns a random odd integer with given bit length (that is, with both `0` and `bit_length-1`
Copy link
Member

Choose a reason for hiding this comment

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

This is not going to affect merging this PR, but I just want to note that the original formatting was intentional - it's easier to read text that is 50-70 characters wide, so I try to add line breaks at meaningful points in the sentences.

/// If the Lucas test is inconclusive, run a Miller-Rabin with random base and unless this second
/// M-R test finds it's composite, then conclude that it's prime.
fn _is_prime_with_rng<T: Integer + RandomMod>(rng: &mut impl CryptoRngCore, num: Odd<T>) -> bool {
let candidate = num.clone();
Copy link
Member

Choose a reason for hiding this comment

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

Probably a little cleaner would be to call the argument candidate right in the method signature.

@fjarri
Copy link
Member

fjarri commented Oct 7, 2024

The original intent of the API was that someone who was using boxed uints could independently select the allocation size of the result. But I suppose given how many times we clone within the library already, and how much cloning will be going on later in most applications, one more reallocation to widen the result is not going to make much of a difference.

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