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

Feat: Schönhage–Strassen Algorithm Implementation #60

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
16 changes: 11 additions & 5 deletions big-int/src/operators/multiply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ namespace libbig
largeInt x2 = (f == x.number.length()) ? x:largeInt(x.number.substr(x.number.length() - f, x.number.length()));
largeInt y2 = (f == y.number.length()) ? y:largeInt(y.number.substr(y.number.length() - f, y.number.length()));

const largeInt x3 = x1 + x2;
const largeInt y3 = y1 + y2;
largeInt x3 = x1 + x2;
largeInt y3 = y1 + y2;

largeInt x1y1 = simple_multiplication(x1, y1);
largeInt x2y2 = simple_multiplication(x2, y2);
largeInt x3y3 = simple_multiplication(x3, y3);
largeInt x1y1 = x1 * y1;
largeInt x2y2 = x2 * y2;
largeInt x3y3 = x3 * y3;

largeInt xy = append_zeroes(x1y1, 2*f) + append_zeroes((x3y3 - x1y1 - x2y2), f) + x2y2;

Expand All @@ -124,5 +124,11 @@ namespace libbig
return remove_zeroes(xy);
}

largeInt largeInt::operator*(int next_number) {
return *this * largeInt(next_number);
}

largeInt largeInt::operator*(int64_t next_number) {
return *this * largeInt(std::to_string(next_number));
}
} // namespace libbig
128 changes: 128 additions & 0 deletions tests/operators/multiply_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,133 @@ int main()
c = a * b;
assert(c == libbig::largeInt("7000770444056069403046119404826740387252166452093943636486324185225836788920060077258109995450330997820727632474615791121779806923541276193853962113432983583426568371447855101334504301369704676060587738461922443343618487980096828486083202559107333726983966552151422667448209619250487990929538296732965889159956209059231075"));

/// Tests for multiplication of largeInt to largeInt (largeInt * int)
/// Test 1
a = libbig::largeInt("1");
int d { 1 };
c = a * d;
assert(c == libbig::largeInt("1"));

/// Test 2
a = libbig::largeInt("999");
d = 99999;
c = a * d;
assert(c == libbig::largeInt("99899001"));

/// Test 3
a = libbig::largeInt("32516889472103571029388908423975");
d = 0;
c = a * d;
assert(c == libbig::largeInt("0"));

/// Test 4
a = libbig::largeInt("6666666666666");
d = 2147000000;
c = a * d;
assert(c == libbig::largeInt("14313333333331902000000"));

// Test 5
a = libbig::largeInt("33");
d = -2;
c = a * d;
assert(c == libbig::largeInt("-66"));

// Test 6
a = libbig::largeInt("-0");
d = 100;
c = a * d;
assert(c == libbig::largeInt("0"));

// Test 7
a = libbig::largeInt("-100");
d = -100;
c = a * d;
assert(c == libbig::largeInt("10000"));

// Test 8
a = libbig::largeInt("0000000000000000000000000000000000000000000000000000");
d = 1111111111;
c = a * d;
assert(c == libbig::largeInt("0"));

// Test 9
a = libbig::largeInt("824358293475908745902350926589023459023485089347892650234650273456207349236502347524357832456789235623489759235623789456782394567892359478235239423569782356234659837562378965278956237489235");
d = 2147111111;
c = a * d;
assert(c == libbig::largeInt("1769988851367122479149013395500437599508978045281688053754054359337011171465551557147132045207848595192391874649522705358582133685907728887725045238091914980923198260535639031943130151995891211390085"));

// Test 10
a = libbig::largeInt("824358293475908745902350926589023459023485089347892650234650273456207349236502347524357832456789235623489759235623789456782394567892359478235239423569782356234659837562378965278956237489235");
d = -0;
c = a * d;
assert(c == libbig::largeInt("0"));

/// Tests for multiplication of largeInt to largeInt (largeInt * int64_t(long long))
/// Test 1
a = libbig::largeInt("1");
int64_t e { 1 };
c = a * e;
assert(c == libbig::largeInt("1"));

/// Test 2
a = libbig::largeInt("999");
e = 99999;
c = a * e;
assert(c == libbig::largeInt("99899001"));

/// Test 3
a = libbig::largeInt("32516889472103571029388908423975");
e = 0;
c = a * e;
assert(c == libbig::largeInt("0"));

/// Test 4
a = libbig::largeInt("6666666666666");
e = 2147000000;
c = a * e;
assert(c == libbig::largeInt("14313333333331902000000"));

// Test 5
a = libbig::largeInt("33");
e = -2;
c = a * e;
assert(c == libbig::largeInt("-66"));

// Test 6
a = libbig::largeInt("-0");
e = 100;
c = a * e;
assert(c == libbig::largeInt("0"));

// Test 7
a = libbig::largeInt("-100");
e = -100;
c = a * e;
assert(c == libbig::largeInt("10000"));

// Test 8
a = libbig::largeInt("0000000000000000000000000000000000000000000000000000");
e = 1111111111;
c = a * e;
assert(c == libbig::largeInt("0"));

// Test 9
a = libbig::largeInt("824358293475908745902350926589023459023485089347892650234650273456207349236502347524357832456789235623489759235623789456782394567892359478235239423569782356234659837562378965278956237489235");
e = 2147111111;
c = a * e;
assert(c == libbig::largeInt("1769988851367122479149013395500437599508978045281688053754054359337011171465551557147132045207848595192391874649522705358582133685907728887725045238091914980923198260535639031943130151995891211390085"));

// Test 10
a = libbig::largeInt("824358293475908745902350926589023459023485089347892650234650273456207349236502347524357832456789235623489759235623789456782394567892359478235239423569782356234659837562378965278956237489235");
e = -0;
c = a * e;
assert(c == libbig::largeInt("0"));

// Test 1
a = libbig::largeInt("824358293475908745902350926589023459023485089347892650234650273456207349236502347524357832456789235623489759235623789456782394567892359478235239423569782356234659837562378965278956237489235");
e = 1265387145178649877;
c = a * e;
assert(c == libbig::largeInt("1043132387585823801991307287703268230197717355664659943770242721775843792669363443147242163866322648004732006134963833033002779153256893032797217422608922304113441680757314641399751543910917847399909621574095"));

return 0;
}