Skip to content

Commit a74c51b

Browse files
committed
LibCrypto: Remove unused big numbers modular functions
Remove `Mod`, `ModularInverse`, `ModularPower` and `LCM` as they are unused.
1 parent f2eeb22 commit a74c51b

File tree

3 files changed

+1
-212
lines changed

3 files changed

+1
-212
lines changed

Libraries/LibCrypto/NumberTheory/ModularFunctions.cpp

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,6 @@
1212

1313
namespace Crypto::NumberTheory {
1414

15-
UnsignedBigInteger Mod(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
16-
{
17-
UnsignedBigInteger result;
18-
result.set_to(a);
19-
result.set_to(result.divided_by(b).remainder);
20-
return result;
21-
}
22-
23-
UnsignedBigInteger ModularInverse(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
24-
{
25-
if (b == 1)
26-
return { 1 };
27-
28-
UnsignedBigInteger result;
29-
UnsignedBigInteger temp_y;
30-
UnsignedBigInteger temp_gcd;
31-
UnsignedBigInteger temp_quotient;
32-
UnsignedBigInteger temp_1;
33-
UnsignedBigInteger temp_2;
34-
UnsignedBigInteger temp_shift;
35-
UnsignedBigInteger temp_r;
36-
UnsignedBigInteger temp_s;
37-
UnsignedBigInteger temp_t;
38-
39-
UnsignedBigIntegerAlgorithms::modular_inverse_without_allocation(a, b, result, temp_y, temp_gcd, temp_quotient, temp_1, temp_2, temp_shift, temp_r, temp_s, temp_t);
40-
41-
return result;
42-
}
43-
44-
UnsignedBigInteger ModularPower(UnsignedBigInteger const& b, UnsignedBigInteger const& e, UnsignedBigInteger const& m)
45-
{
46-
if (m == 1)
47-
return 0;
48-
49-
if (m.is_odd()) {
50-
UnsignedBigInteger temp_z0 { 0 };
51-
UnsignedBigInteger temp_rr { 0 };
52-
UnsignedBigInteger temp_one { 0 };
53-
UnsignedBigInteger temp_z { 0 };
54-
UnsignedBigInteger temp_zz { 0 };
55-
UnsignedBigInteger temp_x { 0 };
56-
UnsignedBigInteger temp_extra { 0 };
57-
58-
UnsignedBigInteger result;
59-
UnsignedBigIntegerAlgorithms::montgomery_modular_power_with_minimal_allocations(b, e, m, temp_z0, temp_rr, temp_one, temp_z, temp_zz, temp_x, temp_extra, result);
60-
return result;
61-
}
62-
63-
UnsignedBigInteger ep { e };
64-
UnsignedBigInteger base { b };
65-
66-
UnsignedBigInteger result;
67-
UnsignedBigInteger temp_1;
68-
UnsignedBigInteger temp_multiply;
69-
UnsignedBigInteger temp_quotient;
70-
UnsignedBigInteger temp_remainder;
71-
72-
UnsignedBigIntegerAlgorithms::destructive_modular_power_without_allocation(ep, base, m, temp_1, temp_multiply, temp_quotient, temp_remainder, result);
73-
74-
return result;
75-
}
76-
7715
UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
7816
{
7917
UnsignedBigInteger temp_a { a };
@@ -87,31 +25,4 @@ UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
8725
return output;
8826
}
8927

90-
UnsignedBigInteger LCM(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
91-
{
92-
UnsignedBigInteger temp_a { a };
93-
UnsignedBigInteger temp_b { b };
94-
UnsignedBigInteger temp_1;
95-
UnsignedBigInteger temp_2;
96-
UnsignedBigInteger temp_3;
97-
UnsignedBigInteger temp_quotient;
98-
UnsignedBigInteger temp_remainder;
99-
UnsignedBigInteger gcd_output;
100-
UnsignedBigInteger output { 0 };
101-
102-
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder, gcd_output);
103-
if (gcd_output == 0) {
104-
dbgln_if(NT_DEBUG, "GCD is zero");
105-
return output;
106-
}
107-
108-
// output = (a / gcd_output) * b
109-
UnsignedBigIntegerAlgorithms::divide_without_allocation(a, gcd_output, temp_quotient, temp_remainder);
110-
UnsignedBigIntegerAlgorithms::multiply_without_allocation(temp_quotient, b, temp_1, output);
111-
112-
dbgln_if(NT_DEBUG, "quot: {} rem: {} out: {}", temp_quotient, temp_remainder, output);
113-
114-
return output;
115-
}
116-
11728
}

Libraries/LibCrypto/NumberTheory/ModularFunctions.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010

1111
namespace Crypto::NumberTheory {
1212

13-
UnsignedBigInteger Mod(UnsignedBigInteger const& a, UnsignedBigInteger const& b);
14-
UnsignedBigInteger ModularInverse(UnsignedBigInteger const& a_, UnsignedBigInteger const& b);
15-
UnsignedBigInteger ModularPower(UnsignedBigInteger const& b, UnsignedBigInteger const& e, UnsignedBigInteger const& m);
16-
1713
// Note: This function _will_ generate extremely huge numbers, and in doing so,
1814
// it will allocate and free a lot of memory!
1915
// Please use |ModularPower| if your use-case is modexp.
@@ -39,6 +35,5 @@ static IntegerType Power(IntegerType const& b, IntegerType const& e)
3935
}
4036

4137
UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b);
42-
UnsignedBigInteger LCM(UnsignedBigInteger const& a, UnsignedBigInteger const& b);
4338

4439
}

Tests/LibCrypto/TestBigInteger.cpp

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h>
1010
#include <LibCrypto/BigInt/SignedBigInteger.h>
1111
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
12-
#include <LibCrypto/NumberTheory/ModularFunctions.h>
1312
#include <LibTest/TestCase.h>
13+
1414
#include <math.h>
1515

1616
static Crypto::UnsignedBigInteger bigint_fibonacci(size_t n)
@@ -260,123 +260,6 @@ TEST_CASE(test_unsigned_bigint_base10_to_string)
260260
EXPECT_EQ(result, "57195071295721390579057195715793");
261261
}
262262

263-
TEST_CASE(test_bigint_modular_inverse)
264-
{
265-
auto result = Crypto::NumberTheory::ModularInverse(7, 87);
266-
EXPECT_EQ(result, 25);
267-
268-
// RSA-like calculations (non-prime modulus)
269-
// 256 bits
270-
auto result0 = Crypto::NumberTheory::ModularInverse("65537"_bigint, "7716818999704200055673002605512017774829533873852931754420182187116755406508851421710377874835807810150544004124020368281638431187393087109588616395722976"_bigint);
271-
EXPECT_EQ(result0, "6957112022178657251467710742735822058162610570160374638904992058315050936014396238029779769209358140634220249380773356423403675888538086147825555026035553"_bigint);
272-
273-
// 512 bits
274-
auto result1 = Crypto::NumberTheory::ModularInverse("65537"_bigint, "66371585251075966819781098993500728937583856843831372038905151148345437332287092304882812087499010029105588098364783005919549558874442528396629248591406931414614111891501372333038520092291512484438801203423887203269149674846124095871663987547448839320258336408613886916453844596419759100107324930878071769740"_bigint);
275-
EXPECT_EQ(result1, "26054622179142032720028508076442212084428946778480090764681215551421076128717366124902270573494164075542052047036494993565348604622774660543816175267575966621965870525545200512871843484053034799993241047965063186879250098185242452576259203314665246947408123972479812452501763277722372741633903726089081777013"_bigint);
276-
277-
// 1024 bits
278-
auto result2 = Crypto::NumberTheory::ModularInverse("65537"_bigint, "15138018815872997670379340569590053786751606702300795170195880218956355437896550248537760818855924336022497803648355813501714375226639621651553768492566347398869904156530722997508431839019744455406614130583767126628559642684420295498410584657359791127851130600248257172505371207271304207113156882020325681053619922800978652053485848563399633561547330701503189380714480104549363705442836720246845910476607566548831148092234175836086100548136352482086041752239158391127234701836987492763766422215181929557528346258876471603164358341122158423252911442143627060117356562382539931055979839928020375814577774568506219095460"_bigint);
279-
EXPECT_EQ(result2, "352944027811067647898738611629058427852304118911692860827613485123904223707309287574434266615985662838432895066522539680342700540859443396609154496797860427323087928211223350781892424890095206186754144857591836206851688878370908212484113910561145014928308094010701389437847432819789627667865537264858898647327940583790765221748422671237234540519772362358619915066782513690761367501055197957446641610208834119453346877106578279102485033455183279561583102635479714079717024343606159710438913791366678187343078155600092293050263813498247677964057687773249647494687288513671987040199233950440440274115001289968681855713"_bigint);
280-
281-
// 2048 bits
282-
auto result3 = Crypto::NumberTheory::ModularInverse("65537"_bigint, "523474308603627394504956180621539730601163404544670078344572546811775850669696720017356530287979625576623354887741212994543899068001220583437221973327752079153585098984263865181019654102487287512742287583901185619943683690635892036920956164864785078974721208937251159192154678447234191958275430233568974368064153896258338157469723619961352235804796084551641896006827645045906990423304676288895690876254935487456610269572418962650650646690483258846109000171328266193988292013017586921119096421585767248613790649741313360067618201749482055683058067852760706162692126354831896695191672470846960268467251962491660154005556677209860743434696351971155125630603082354855591129257818487022326288868392996237441507506020729258165681956915422119008555908702541877086255318047295376505201886687588318922810022094799926224262663342802397393873785019139429897232975310359190270883355499980682538341383918065122655507451050546937038544941011313947405743092260202204107637846238518077467613057097554476001838993189751185435880317537273891467684330982378878693444450893688310488368914140946077563025119239896138217432169087237109636595561779480434253413579986644072788364909696328314076474006110809917696250643811113150325166438321806889977329096600"_bigint);
283-
EXPECT_EQ(result3, "240127075385672984131139625830070783237907982221133353148189335410568341527428666156244401941613614961167400369106979053892812269120049657443477793981296225881475026790422579290126094592109424058098042199594448071964950528580600611958965243821505925343196113711042336371725072831518096843639993577853488509194999139161304606985554742922290191265996073819003163398587965470117671744141606775913928846496667921317852122223410154174992910744403897198385261335591218191096175027653809536744181084305551380061284286787205754668550681282247875856383030865885608272716379977803550823924611280514398989134855055135065370857211199581305881103457229188227055584369447256267812626743332730752890660577238791001818881550170150963398307775313919391546061252167851998883746488646960356804185182713413302894188591089552011567206439844281374992020196210238318522369271354430754186391905586095171569497490344824935263935189296620116395162680037583825943495347400986600883286030356418038099224122793594156156724989735012128839569555916857118867097884284041934024459778861054849599643478734444083949177169533378055193717492397723564200451231728283569509748271283984325804303130753631049728871294775611922359924670108389072405289815451858958044897456873"_bigint);
284-
285-
// Prime modulus
286-
// 256 bits
287-
auto result4 = Crypto::NumberTheory::ModularInverse("79065576377430658630291493727884901955697921969202460485568061955796483998089"_bigint, "105236333148230907525852233540677623156492475210517338560791379084799836582587"_bigint);
288-
EXPECT_EQ(result4, "93504545219772953643321957341999793447107631393924073671776287172945600034443"_bigint);
289-
290-
// 512 bits
291-
auto result5 = Crypto::NumberTheory::ModularInverse("6732413992718219635342848318074302303731222168385940253721776224551974038416513462421454674844777721589563127965274488341922551419528552939608455047714128"_bigint, "11522413189509252702442551731783393581283708206969207645140596867187940532466129960582867971721932546048110673296094625661627355203044884987258434322393611"_bigint);
292-
EXPECT_EQ(result5, "11152730475146621030888388443393672975086889576414759677260744095766476531703359323453287638858041043666073703397243706949753685433502205695232485731849432"_bigint);
293-
294-
// 1024 bits
295-
auto result6 = Crypto::NumberTheory::ModularInverse("74263833960189886466939196560269216955870235656416128238251461763825971916420974189969964837983352188966833052749715539825280552531258436173317484112004327881741531787519471213020298642984697548930887036556763982001107471012474873100069623257613164741565312643996566523133343615723683010756027848816042939202"_bigint, "95381964444589883427387341140753255405844325814158762996484790475715776875097467150855290612578232487289384615394165716659709100194630793773552674979686871441395261056953751419334210618336786252840280983695277648363095334709545375311967459037971278965116324165577308183006400447807648095049414919774916252747"_bigint);
296-
EXPECT_EQ(result6, "58709722343881170435829301168583511620090591717154752336044125040931850388422639576614097557227300205781894345595418512100748823628637201919915110093901598005111776632116568475789059078360021536835127742733773460624284681421890935681567846755324337116900649074136799388542272888156479298282951539364264931616"_bigint);
297-
298-
// 2048 bits
299-
auto result7 = Crypto::NumberTheory::ModularInverse("2083841562885492721290501151318058444158766003544222347122338319668970762119890042933475358898503059392439888781978346524976708635055122364241675726844930777696927712106305827918390408155067866218977660488635746552929258625544335318963328074495878439935663659069731717795216882935427203069231010795298950025561648743468756200717796561939220399337004980456668273620158478615916791124020696059432601192990947530965055857904582283829896086691653209249081553530465663724181700972927069397922147671340499270418643905380501155480764913403727582416414800901222394379992981688837765818280499497151738855424231982306618396076"_bigint, "16224364484369166277359386410182421629585266346687261081219199035627872465058014536404366328330233633748201670077151313307023144281234188494904998208639551259034363175330775169605905250528606169313713885192955997968412296964554695990505670926075345389730833276243454625387707778469967380099142375244892915645788614606443180803179195164798643205708829861402784554710221097157040790522116753790155662203858533778060827797234218324190122635514071740918420043227885163450453517325211468174509897086842869675754300089020572195273927710496253921910012981005407132203227555676309198192189264516679445448908377225879137304001"_bigint);
300-
EXPECT_EQ(result7, "1920241917211855356722925925154440229550377096185083909958775862353126205660695403426655365321463320876264364542077391170885582314150929024605918556565268345499952616868512453484734433431514794042936426911598410457811519189984561227978039512706300456181926682048163061548216104149539350320019907684566461197120360812572564919099529762677479436223515410468281993579286727653390573176288887687204943283770190210493492026862067176323654605190038514894818679839404911730667301011930597975461644362994301634764766641419232360033891763076329125623575026815152128746383453332269905123747535275999442797020400268408062413004"_bigint);
301-
}
302-
303-
TEST_CASE(test_bigint_even_simple_modular_power)
304-
{
305-
Crypto::UnsignedBigInteger base { 7 };
306-
Crypto::UnsignedBigInteger exponent { 2 };
307-
Crypto::UnsignedBigInteger modulo { 10 };
308-
auto result = Crypto::NumberTheory::ModularPower(base, exponent, modulo);
309-
EXPECT_EQ(result.words(), Vector<u32> { 9 });
310-
}
311-
312-
TEST_CASE(test_bigint_odd_simple_modular_power)
313-
{
314-
Crypto::UnsignedBigInteger base { 10 };
315-
Crypto::UnsignedBigInteger exponent { 2 };
316-
Crypto::UnsignedBigInteger modulo { 9 };
317-
auto result = Crypto::NumberTheory::ModularPower(base, exponent, modulo);
318-
EXPECT_EQ(result.words(), Vector<u32> { 1 });
319-
}
320-
321-
TEST_CASE(test_bigint_large_even_fibonacci_modular_power)
322-
{
323-
Crypto::UnsignedBigInteger base = bigint_fibonacci(200);
324-
Crypto::UnsignedBigInteger exponent = bigint_fibonacci(100);
325-
Crypto::UnsignedBigInteger modulo = bigint_fibonacci(150);
326-
// Result according to Wolfram Alpha : 7195284628716783672927396027925
327-
auto result = Crypto::NumberTheory::ModularPower(base, exponent, modulo);
328-
Vector<u32> expected_result { 2042093077, 1351416233, 3510104665, 90 };
329-
EXPECT_EQ(result.words(), expected_result);
330-
}
331-
332-
TEST_CASE(test_bigint_large_odd_fibonacci_modular_power)
333-
{
334-
Crypto::UnsignedBigInteger base = bigint_fibonacci(200);
335-
Crypto::UnsignedBigInteger exponent = bigint_fibonacci(100);
336-
Crypto::UnsignedBigInteger modulo = bigint_fibonacci(149);
337-
// Result according to Wolfram Alpha : 1136278609611966596838389694992
338-
auto result = Crypto::NumberTheory::ModularPower(base, exponent, modulo);
339-
Vector<u32> expected_result { 2106049040, 2169509253, 1468244710, 14 };
340-
EXPECT_EQ(result.words(), expected_result);
341-
}
342-
343-
TEST_CASE(test_bigint_large_odd_fibonacci_with_carry_modular_power)
344-
{
345-
Crypto::UnsignedBigInteger base = bigint_fibonacci(200);
346-
Crypto::UnsignedBigInteger exponent = bigint_fibonacci(100);
347-
Crypto::UnsignedBigInteger modulo = bigint_fibonacci(185);
348-
// Result according to Wolfram Alpha : 55094573983071006678665780782730672080
349-
auto result = Crypto::NumberTheory::ModularPower(base, exponent, modulo);
350-
Vector<u32> expected_result { 1988720592, 2097784252, 347129583, 695391288 };
351-
EXPECT_EQ(result.words(), expected_result);
352-
}
353-
354-
TEST_CASE(test_bigint_modular_power_extra_tests)
355-
{
356-
struct {
357-
Crypto::UnsignedBigInteger base;
358-
Crypto::UnsignedBigInteger exp;
359-
Crypto::UnsignedBigInteger mod;
360-
Crypto::UnsignedBigInteger expected;
361-
} mod_pow_tests[] = {
362-
{ "2988348162058574136915891421498819466320163312926952423791023078876139"_bigint, "2351399303373464486466122544523690094744975233415544072992656881240319"_bigint, "10000"_bigint, "3059"_bigint },
363-
{ "24231"_bigint, "12448"_bigint, "14679"_bigint, "4428"_bigint },
364-
{ "1005404"_bigint, "8352654"_bigint, "8161408"_bigint, "2605696"_bigint },
365-
{ "3665005778"_bigint, "3244425589"_bigint, "565668506"_bigint, "524766494"_bigint },
366-
{ "10662083169959689657"_bigint, "11605678468317533000"_bigint, "1896834583057209739"_bigint, "1292743154593945858"_bigint },
367-
{ "99667739213529524852296932424683448520"_bigint, "123394910770101395416306279070921784207"_bigint, "238026722756504133786938677233768788719"_bigint, "197165477545023317459748215952393063201"_bigint },
368-
{ "49368547511968178788919424448914214709244872098814465088945281575062739912239"_bigint, "25201856190991298572337188495596990852134236115562183449699512394891190792064"_bigint, "45950460777961491021589776911422805972195170308651734432277141467904883064645"_bigint, "39917885806532796066922509794537889114718612292469285403012781055544152450051"_bigint },
369-
{ "48399385336454791246880286907257136254351739111892925951016159217090949616810"_bigint, "5758661760571644379364752528081901787573279669668889744323710906207949658569"_bigint, "32812120644405991429173950312949738783216437173380339653152625840449006970808"_bigint, "7948464125034399875323770213514649646309423451213282653637296324080400293584"_bigint },
370-
};
371-
372-
for (auto test_case : mod_pow_tests) {
373-
auto actual = Crypto::NumberTheory::ModularPower(
374-
test_case.base, test_case.exp, test_case.mod);
375-
376-
EXPECT_EQ(actual, test_case.expected);
377-
}
378-
}
379-
380263
TEST_CASE(test_bigint_import_big_endian_decode_encode_roundtrip)
381264
{
382265
u8 random_bytes[128];

0 commit comments

Comments
 (0)