From c27bc602bd3ff9a8e888d499b64fb5ca798a9a00 Mon Sep 17 00:00:00 2001 From: Mike <45243121+tankf33der@users.noreply.github.com> Date: Wed, 21 May 2025 13:54:21 +0300 Subject: [PATCH] math.big: fix Karatsuba's add_in_place() function, add carry handler on exit (#24541) --- vlib/math/big/big_test.v | 1 + vlib/math/big/special_array_ops.v | 3 +++ vlib/math/big/special_array_ops_test.v | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/vlib/math/big/big_test.v b/vlib/math/big/big_test.v index 968dee119e..52e47f6ea9 100644 --- a/vlib/math/big/big_test.v +++ b/vlib/math/big/big_test.v @@ -374,6 +374,7 @@ const pow_test_data = [ PowTest{ 2, 291, '3978585891278293137243057985174566720803649206378781739523711815145275976100267004264448' }, PowTest{ 2, 13051, '55267956948146482939580690892144167523198080345777838207973892310290788736821578114330455608899403999039109810492889769112675765371767842822434294904209432777633054910495035098236995390194172097907719098491142950680893121170415521376299250142695723393227633360829843822866024641522563385984082711736391348667978440753913719338262829073043347247299392139063557930790932287363651577574134082253690763995791163252208415844758727346329281304595424562991322680879863770722706799961809876056054813814521080566806686543718660613598426151803438498674178140121315919034857314115503781267190616565875833106631659285123886667080327050174176636018357664222092316439777145540128647880814336995055642164733977751410563822190384321689041057299634727749812974144023207025653904438780374867988942510987313912358915642077631661267839921263001062107729173356916478717702317027506423498272334490092958377684733699662429071107215486273972618250534115869723283498114137145525373906475082833289104669492127120264073453925068828669125794548775264089233042803009595366761575381131720150020416662597433270991992738498853512916968911947303026796659580500368762437079002069661349769285545784372450396095376496629526120146056228969588758257289220372378463001533895184835727768908852564233327092981929411751106985223047814350329661006786989543912993534105514927316542621884655977620130711246926843567109456472355504038497809114457931293164130666363146093971664611970414478485192994816264742799354431903294883107927606299200191395563965045258006700385533997784569469661002395464091760511030079955508794507716517333922113296290807049250301483689712907877303498967096232669748824940257740874215641972642675515304831959934847559373172772025875939941676740275689817054162918846662455567085919928556650978239087803483530882408322645489433079587537294621171822004369279659604911446107164265409771411479042191057214450986176256578104398112366947709868794481776078240442638184240046590399307658325666682824402555546582169125084299171911366515003536406493880562467793738217233554231178610461637407108387422104493358650812565981939865004194724312308819851360505832016659131404286536166604042995985362843053197530692653760538388205506347898194204185268035169011980847365832662902833167362084522724032944503999606348026648228492541892525660287991575613769500299950657228084617208982541726107251394954812792421965613075539830944521759805581201863306109230481535735866977702920938163637191044218917218763676996478413703527738808285760128085267402333007136279283870165611587883351595579551530437487879301447035074519559776804970191650066388050122761542282202772884141794482588913487773583646082244648646898935682167457258599202486566745556266587599528210145725708324833027469913664602498185234663551855895508091756784116786760256088764788750159984191050146275837122761351322740020869396023016337700936855961260331330031678574208143275974341369638055331776401934895373348591440122433244331646746067969385005236926872015898048323445768868147713435873976237988151319143506783129013204847868879150394120372376677277312144782189330256123263292237715700439809503932239415509520929779856708018666527067600827221597869399175438321388641981165828368557419771140117333070030872143102607788421073131616871657178020327435898081376870580959579623464843234800465871796556467456341474610533391530095305196315887535933316836459037786625667180354374811843995688474490608169113180680143676925870456310071925085465713774179009107449991929491343540484244181606094673172473788660554675839157004904589906389801922612312350223513086754383162398737177774230411158150377305076748192396441454734336654918968445280484743879759859090769539002965788922965135895758979301734303194298909826556775859142046683115828058122817136236184480127270446495870009273426474586242661860904296452193946364822434632085953525238418258226591281397400949183681177585810194950485799542968112294068369136156497511211228725248' }, PowTest{ 324, 315, '66325146064916587705822805477951823674769212922003325230500180789514487101799702287247301347816140714887582527826252837635296749781071351621748491469338347097923896026211183517655658952346069454893422558286798338709431368762851475568899541999504754550056265493269010870696623999709399529395247064542825851568385196637089440522882877102429945439977107582295420418108331098961838419917230847980056560488541780255425015021238743932289115066701337398107639567748102191005710201353615093246958907555634902309636451244444952203735074916066229982498598205421944122042066749035283837586883383420374291325389757869347147357807188516650352693616763867685354382631931356465247637321960345782811272139101785279798666504361229957479336436466489780129445016691164329417001378480690804715301830926348058624' }, + PowTest{ '68973377060666327208300998134976245269331422613575559102002431189859063751444117873216302679385447519268197493634614796410525545235779538750217378947422409274212222649954053354836156', 40, '35258157541008739895331135586163782978774260107060350863053496129803595902499520940913245496659764125837663081609116693664859066121240222710455090772654236305161593582934388795922429749725757888593191060785569873278223392217676911081371575408698209198487574208133504092628962419627719435594361886665948512724378730193034883682168393683858735816474842100358088758399284368135640873191297579994494530855391610253450197112507337023973260390114533551942575098377704162567050376536430239009068399198141851190687786692855878006471015617730177140564368490211125304311492626663371442064896009452985346147238789927023952155765874191186509560387099879963058580458175561206837610198181475959286262651094184535935843622210514022579661392260730979033525048986082578378757345674162044090708247493623847265486608083275091608623071230279318646779316496782411478544030608676817377643527831062728898442877397153923451755987019024165148269190403334041384095965099409973404146520908147697493051017569834287683508859619614820718839901473820225092359044663170397185665222505521573782264521807940918163830187995079966398797834892374540329644161011839047312660408041075233827338195607721484103616473925700064580565891872491802099275924411222757546424787635592046987870959737279307319356334181603750301494352434527308393706898772379215484237398245418863149959467744064471526937704836205865867678040592702295144785801441309292006081240708835717111188736116077625711594150435701058724546928000620650885222066533349258673109739859759612800975406051665452593729572474029949396850025034721391155268272910460099672143460177826856963197730677959657469514888519160035227448297269382534435799272102328415501312356111831947752575670395475105022420187733034171502887359638152057876379771261999121543335479247633748144423214477698449199446169433490219980784078516768935903866137328947352936172402921848772002674322630245635026231629454706048753547546902225014320540104977155736944657399787940439035277957092279432489442361800063891355739406363001782622680936832010543170656715775452062089081852640884272056572152042908273232284891160722405506506925047900433631369352240575822425084000954380503324509277410828459755147240727523792066016952349310027090166657648299162430936093103120333183134330248812717816439403194436580527262279045316890050092161084522990205107250830319663209486820407463781212079072171125202655959768122491168090453120012969830867004575480551791284934079004033495761485325184899900038803474124601794212496136498280996063010402993132647266142814962192181619151827932124894236921088644522787656602462050326681805844438219230755707199226244584366270014878488134597806515533702003407174379138958404132228268468415855937695923909093324159910887747556292181906257010119373908909281031098335560145512862280651673879872429356095545191263033441020104833977186041802651653142739004325260453065487714226086302334925470018494814279647772696732751413492938129340193390870371914405132971752944771382425547764289488463024568565047205504469195296554979731615888034588174368312122402232599112594230523742220097645109619918997486658429331785718355443479680146829664055646997657939426194000103378913037286846713039443955792103079259450246916434767428300921600590858128004218244953067853605711134269179529138347808509232974732360192374463207085903368535685042319215946964616538267824251199799335578316284176334931243032435271890535306860625400974970800196067356950499429899623903038882907645186739157691937393868001954217068677405564876964259777068576274955960434304735331332727337942605330662700571524428729266514174137948899430401136194700301206200551515499664922638963028270893302781587131748991793976526030657152281703854200233227924967926571276661994874579099755225676717805006216569595520566828236321367761203653020465515401663214232518761474223110777886389720199103747449349499503400895567044624612530615359244422254804356461625965858659146045123680314611311068802319473327814245022783376966016661309117610975157629715943310597824424800462948268112526010321134293021657131436472752011710620253048339957169295446502441751862148811884121265645084802972835434430500666554560667426728304121819451042399317183881953483237660080276310900310609399239782487398889813072240297043891794998893001786233968869912478411537501683017172187740347782175947175330986012037410594509842385173934376073883704392390066195094586380644822566323337063806458420270229827713462151670514123181768373835781491371536706582982089663589356740349371026605036928057179599547510869011654375677928737943079864386098501933754834569076969459522907991122865834472316075011973279443310747045576723382195820376147552983791746734829752811505837765374961937584859088696438357582609441168579224373232538884336186944109656062668354958262471022386307605217481710783595873812958322692301637767626905751159343436670100901639626281237260581520402478166789720378200977977591043846005303066270994585484308872635398698206784922087791315869659668470550750672041440181846921937878737954369397741595712732557836089455119269549939304717990844018805341426733137219366976467073055703514345869726491666687917740218342964217322837128666044618309225468654622198231241735033740025000833039365889040210467381631582635008903317248408687515878944562462103899122853122282077536393859736678126798284896936745206987705406162201111434866485708471709121620748864731821841498499097932663197004613917962363847058188708935507962599345315904755192312885153454959868456584090394953490529701708225758187924021653717046235748081644499911269567590535014838235292295638849624380239182552648037518233234630924354382791335743589786044871629805970609844860052222008562149922586920233688221038240158947965090122365291307769963243469486471480976812917614890560320724900635565696327855056530135961774334692292338693195704454096038710058676180521224229217340225496509476508946611845834909161932831898098821430336348772505879016381899870043035683249316508108617843133439016918107763134987853201904389395477535249692909938456858665886155150195584515422711040259472814260855939483496834006050935205671281977481531908760417170523135516028527539444163160573194971206959625166833408764832574810255735634834363957899317195483566233828249891102020662519608925798051078615457621971822365495901117747375145048348614866478147644564817801751372420581094557535616477014993403293835622275791359197059201243272901787903487483825695988147058737185860181150381743018939858230502180587316663351024125733956328873574985787891695924518737336301071771593783442839325168387680903413878668606904467043759169383257006087574325785908578823397586231203787496335131359918746993718914006871027887598302839973090822047886906908431573569039736038250163656132301560418543891406517287043442644328121923996383111070947282060547074035037933135808930649423603110117360018001877349840800296558270695890794753087083525850557161190187608610387510161000273695896242816085985017009465480555075198416753288507556316216285114056684935945988548526597279887832288588727265282867869907650516070481891621446962371494964005298779179376792868773282807097284115930818770799203719816032512376608176221517386343418001080528568223383975802920001284696679468308086909698604190274805636772843836377041111569722570010440042124020486157215597592576' }, ] // vfmt on diff --git a/vlib/math/big/special_array_ops.v b/vlib/math/big/special_array_ops.v index d38c0a40ce..13f0ed429c 100644 --- a/vlib/math/big/special_array_ops.v +++ b/vlib/math/big/special_array_ops.v @@ -298,6 +298,9 @@ fn add_in_place(mut a []u32, b []u32) { carry = u32(partial >> 32) } } + if carry > 0 { + a << u32(carry) + } } // a := a - b supposed a >= b diff --git a/vlib/math/big/special_array_ops_test.v b/vlib/math/big/special_array_ops_test.v index 56e8945497..5e30c84be3 100644 --- a/vlib/math/big/special_array_ops_test.v +++ b/vlib/math/big/special_array_ops_test.v @@ -90,6 +90,15 @@ fn test_multiply_karatsuba_02() { assert c == expected.digits } +fn test_multiply_karatsuba_03() { + a := integer_from_string('9729117383001812976929423642') or { panic(err) } + b := integer_from_string('36889625830') or { panic(err) } + mut c := []u32{len: a.digits.len + b.digits.len + 1, init: 0} + karatsuba_multiply_digit_array(a.digits, b.digits, mut c) + expected := integer_from_string('358903499915085682930564860470935872860') or { panic(err) } + assert c == expected.digits +} + fn test_newton_divide_03() { a := [u32(0), 4] b := [u32(0), 1]