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

Boxed uintlike coverage #428

Closed

Conversation

xuganyu96
Copy link
Contributor

This is an attempt to implement for BoxedUint the missing arithmetic needed for crypto-primes (see entropyxyz/crypto-primes#37 (comment)).

Comment on lines 25 to 33
let (half, is_odd) = a.shr1_with_carry();
let half_modulus = modulus.shr1();

let if_even = half.clone();
let if_odd = half
.wrapping_add(&half_modulus)
.wrapping_add(&BoxedUint::one_with_precision(a.bits_precision()));

BoxedUint::conditional_select(&if_even, &if_odd, is_odd)
Copy link
Member

Choose a reason for hiding this comment

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

You can cut down on a number of allocations here:

Suggested change
let (half, is_odd) = a.shr1_with_carry();
let half_modulus = modulus.shr1();
let if_even = half.clone();
let if_odd = half
.wrapping_add(&half_modulus)
.wrapping_add(&BoxedUint::one_with_precision(a.bits_precision()));
BoxedUint::conditional_select(&if_even, &if_odd, is_odd)
let (mut ret, is_odd) = a.shr1_with_carry();
let half_modulus = modulus.shr1();
let if_odd = ret
.wrapping_add(&half_modulus)
.wrapping_add(&BoxedUint::one());
ret.conditional_assign(&if_odd, is_odd);
ret

Cargo.toml Outdated
@@ -39,7 +39,7 @@ rand_core = { version = "0.6", features = ["std"] }
rand_chacha = "0.3"

[features]
default = ["rand"]
default = ["rand", "alloc"]
Copy link
Member

Choose a reason for hiding this comment

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

Most of the downstream crates which use this crate don't need the alloc feature. It's really just for rsa/dsa.

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