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

add mul method for BigInteger #772

Merged
merged 9 commits into from
Feb 5, 2024
Merged

Conversation

tcoratger
Copy link
Contributor

@tcoratger tcoratger commented Feb 2, 2024

Description

This pull request introduces a new mul method for the BigInteger trait. The mul method allows efficient in-place multiplication of two BigInteger 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.


  • Targeted PR against correct branch (master)
  • Linked to GitHub issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • Wrote unit tests
  • Updated relevant documentation in the code
  • Added a relevant changelog entry to the Pending section in CHANGELOG.md
  • Re-reviewed Files changed in the GitHub PR explorer

@tcoratger tcoratger requested review from a team as code owners February 2, 2024 12:31
@tcoratger tcoratger requested review from z-tech, Pratyush and mmagician and removed request for a team February 2, 2024 12:31
ff/src/biginteger/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

@Pratyush Pratyush left a 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

ff/src/biginteger/tests.rs Outdated Show resolved Hide resolved
@@ -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
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 also add some tests to test-templates, if appropriate?

Copy link
Contributor Author

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?

Copy link
Member

@Pratyush Pratyush left a 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!

@Pratyush Pratyush added this pull request to the merge queue Feb 5, 2024
Merged via the queue into arkworks-rs:master with commit fc3f661 Feb 5, 2024
37 checks passed
Comment on lines +446 to +464
#[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
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#[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
}

Copy link
Member

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)

Copy link
Contributor Author

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

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