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
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Pending

- [\#772](https://github.com/arkworks-rs/algebra/pull/772) (`ark-ff`) Implementation of `mul` method for `BigInteger`.

### Breaking changes

- [\#577](https://github.com/arkworks-rs/algebra/pull/577) (`ark-ff`, `ark-ec`) Add `AdditiveGroup`, a trait for additive groups (equipped with scalar field).
Expand Down
41 changes: 41 additions & 0 deletions ff/src/biginteger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,26 @@ impl<const N: usize> BigInteger for BigInt<N> {
}
}

#[inline]
fn mul(&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
}

tcoratger marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
fn div2(&mut self) {
let mut t = 0;
Expand Down Expand Up @@ -1041,6 +1061,27 @@ pub trait BigInteger:
#[deprecated(since = "0.4.2", note = "please use the operator `<<` instead")]
fn muln(&mut self, amt: u32);

/// Multiplies this [`BigInteger`] by another `BigInteger`, storing the result in `self`.
/// Overflow is ignored.
///
/// # Example
///
/// ```
/// use ark_ff::{biginteger::BigInteger64 as B, BigInteger as _};
///
/// // Basic
/// let mut a = B::from(42u64);
/// let b = B::from(3u64);
/// a.mul(&b);
/// assert_eq!(a, B::from(126u64));
///
/// // Edge-Case
/// let mut zero = B::from(0u64);
/// zero.mul(&B::from(5u64));
/// assert_eq!(zero, B::from(0u64));
/// ```
fn mul(&mut self, other: &Self);

/// Performs a rightwise bitshift of this number, effectively dividing
/// it by 2.
/// # Example
Expand Down
28 changes: 28 additions & 0 deletions ff/src/biginteger/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{biginteger::BigInteger, UniformRand};
use num_bigint::BigUint;
// use std::println;
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

// Test elementary math operations for BigInteger.
fn biginteger_arithmetic_test<B: BigInteger>(a: B, b: B, zero: B) {
Expand Down Expand Up @@ -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?

let mut a_mul = a;
a_mul.mul(&B::from(1u64));
assert_eq!(a_mul, a);

// a * 2 = a
a_mul.mul(&B::from(2u64));
assert_eq!(a_mul, a_plus_a);

// a * 2 * b = b * 2 * a
a_mul.mul(&b);
let mut b_mul = b;
b_mul.mul(&B::from(2u64));
b_mul.mul(&a);
assert_eq!(a_mul, b_mul);

// a * 2 * b * 0 = 0
a_mul.mul(&zero);
assert!(a_mul.is_zero());

// a * 2 * ... * 2 = a * 2^n
let mut a_mul_n = a;
for _ in 0..20 {
a_mul_n.mul(&B::from(2u64));
}
assert_eq!(a_mul_n, a << 20);
}

fn biginteger_shr<B: BigInteger>() {
Expand Down
Loading