-
Notifications
You must be signed in to change notification settings - Fork 269
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
add mul
method for BigInteger
#772
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Left a couple of small comments
@@ -49,6 +50,33 @@ fn biginteger_arithmetic_test<B: BigInteger>(a: B, b: B, zero: B) { | |||
let mut a_plus_a = a; | |||
a_plus_a.add_with_carry(&a); // Won't assert anything about carry bit. | |||
assert_eq!(a_mul2, a_plus_a); | |||
|
|||
// a * 1 = a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add some tests to test-templates
, if appropriate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For these implementations around BigInt
, all unit tests can be found in this file:
- So I would say that for the moment there is not necessarily a need to outsource the tests to
test-templates
no? - I added a
max
parameter to try to test with a slightly wider coverage of all the multiplication features - If you want I can also add another function here that tests something specifically for multiplication? But it seems to me that with the tests here we are pretty well covered.
- To avoid mixing the PRs, if you want we can add a new issue to migrate all the tests that are here to
test-templates
and try to put an even broader conversion on all the functions of the implementation, I don't know if you see any interest in it?
Co-authored-by: Pratyush Mishra <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks great!
#[inline] | ||
fn mul_low(&mut self, other: &Self) { | ||
if self.is_zero() || other.is_zero() { | ||
*self = Self::zero(); | ||
return; | ||
} | ||
|
||
let mut res = Self::zero(); | ||
let mut carry = 0; | ||
|
||
for i in 0..N { | ||
for j in 0..(N - i) { | ||
res.0[i + j] = mac_with_carry!(res.0[i + j], self.0[i], other.0[j], &mut carry); | ||
} | ||
carry = 0; | ||
} | ||
|
||
*self = res | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[inline] | |
fn mul_low(&mut self, other: &Self) { | |
if self.is_zero() || other.is_zero() { | |
*self = Self::zero(); | |
return; | |
} | |
let mut res = Self::zero(); | |
let mut carry = 0; | |
for i in 0..N { | |
for j in 0..(N - i) { | |
res.0[i + j] = mac_with_carry!(res.0[i + j], self.0[i], other.0[j], &mut carry); | |
} | |
carry = 0; | |
} | |
*self = res | |
} | |
#[inline] | |
fn mul_low(&self, other: &Self) -> Self { | |
if self.is_zero() || other.is_zero() { | |
*self = Self::zero(); | |
return; | |
} | |
let mut res = Self::zero(); | |
let mut carry = 0; | |
for i in 0..N { | |
for j in 0..(N - i) { | |
res.0[i + j] = mac_with_carry!(res.0[i + j], self.0[i], other.0[j], &mut carry); | |
} | |
carry = 0; | |
} | |
res | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(For consistency, the operations should all return values. If we want in place operations, we should mark those explicitly)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure no problem, I've just followed the convention of other operations here like mul2
or muln
.
Just did a follow up PR here: #775
Description
This pull request introduces a new
mul
method for theBigInteger
trait. Themul
method allows efficient in-place multiplication of twoBigInteger
values, providing a convenient and performant way to compute the product of two instances.This enhancement complements the existing set of arithmetic operations supported by the
BigInteger
trait, contributing to the versatility and usability of the library.Should be part of #645.
Pending
section inCHANGELOG.md
Files changed
in the GitHub PR explorer