1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985 |
- /*
- mpi.c
- by Michael J. Fromberger <sting@linguist.dartmouth.edu>
- Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved
- Arbitrary precision integer arithmetic library
- $Id: mpi.c,v 1.2 2005/05/05 14:38:47 tom Exp $
- */
- #include "mpi.h"
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #if MP_DEBUG
- #include <stdio.h>
- #define DIAG(T,V) {fprintf(stderr,T);mp_print(V,stderr);fputc('\n',stderr);}
- #else
- #define DIAG(T,V)
- #endif
- /*
- If MP_LOGTAB is not defined, use the math library to compute the
- logarithms on the fly. Otherwise, use the static table below.
- Pick which works best for your system.
- */
- #if MP_LOGTAB
- /* {{{ s_logv_2[] - log table for 2 in various bases */
- /*
- A table of the logs of 2 for various bases (the 0 and 1 entries of
- this table are meaningless and should not be referenced).
- This table is used to compute output lengths for the mp_toradix()
- function. Since a number n in radix r takes up about log_r(n)
- digits, we estimate the output size by taking the least integer
- greater than log_r(n), where:
- log_r(n) = log_2(n) * log_r(2)
- This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
- which are the output bases supported.
- */
- #include "logtab.h"
- /* }}} */
- #define LOG_V_2(R) s_logv_2[(R)]
- #else
- #include <math.h>
- #define LOG_V_2(R) (log(2.0)/log(R))
- #endif
- /* Default precision for newly created mp_int's */
- static unsigned int s_mp_defprec = MP_DEFPREC;
- /* {{{ Digit arithmetic macros */
- /*
- When adding and multiplying digits, the results can be larger than
- can be contained in an mp_digit. Thus, an mp_word is used. These
- macros mask off the upper and lower digits of the mp_word (the
- mp_word may be more than 2 mp_digits wide, but we only concern
- ourselves with the low-order 2 mp_digits)
- If your mp_word DOES have more than 2 mp_digits, you need to
- uncomment the first line, and comment out the second.
- */
- /* #define CARRYOUT(W) (((W)>>DIGIT_BIT)&MP_DIGIT_MAX) */
- #define CARRYOUT(W) ((W)>>DIGIT_BIT)
- #define ACCUM(W) ((W)&MP_DIGIT_MAX)
- /* }}} */
- /* {{{ Comparison constants */
- #define MP_LT -1
- #define MP_EQ 0
- #define MP_GT 1
- /* }}} */
- /* {{{ Constant strings */
- /* Constant strings returned by mp_strerror() */
- static const char *mp_err_string[] = {
- "unknown result code", /* say what? */
- "boolean true", /* MP_OKAY, MP_YES */
- "boolean false", /* MP_NO */
- "out of memory", /* MP_MEM */
- "argument out of range", /* MP_RANGE */
- "invalid input parameter", /* MP_BADARG */
- "result is undefined" /* MP_UNDEF */
- };
- /* Value to digit maps for radix conversion */
- /* s_dmap_1 - standard digits and letters */
- static const char *s_dmap_1 =
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
- #if 0
- /* s_dmap_2 - base64 ordering for digits */
- static const char *s_dmap_2 =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- #endif
- /* }}} */
- /* {{{ Static function declarations */
- /*
- If MP_MACRO is false, these will be defined as actual functions;
- otherwise, suitable macro definitions will be used. This works
- around the fact that ANSI C89 doesn't support an 'inline' keyword
- (although I hear C9x will ... about bloody time). At present, the
- macro definitions are identical to the function bodies, but they'll
- expand in place, instead of generating a function call.
- I chose these particular functions to be made into macros because
- some profiling showed they are called a lot on a typical workload,
- and yet they are primarily housekeeping.
- */
- #if MP_MACRO == 0
- void s_mp_setz(mp_digit *dp, mp_size count); /* zero digits */
- void s_mp_copy(mp_digit *sp, mp_digit *dp, mp_size count); /* copy */
- void *s_mp_alloc(size_t nb, size_t ni); /* general allocator */
- void s_mp_free(void *ptr); /* general free function */
- #else
- /* Even if these are defined as macros, we need to respect the settings
- of the MP_MEMSET and MP_MEMCPY configuration options...
- */
- #if MP_MEMSET == 0
- #define s_mp_setz(dp, count) \
- {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=0;}
- #else
- #define s_mp_setz(dp, count) memset(dp, 0, (count) * sizeof(mp_digit))
- #endif /* MP_MEMSET */
- #if MP_MEMCPY == 0
- #define s_mp_copy(sp, dp, count) \
- {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=(sp)[ix];}
- #else
- #define s_mp_copy(sp, dp, count) memcpy(dp, sp, (count) * sizeof(mp_digit))
- #endif /* MP_MEMCPY */
- #define s_mp_alloc(nb, ni) calloc(nb, ni)
- #define s_mp_free(ptr) {if(ptr) free(ptr);}
- #endif /* MP_MACRO */
- mp_err s_mp_grow(mp_int *mp, mp_size min); /* increase allocated size */
- mp_err s_mp_pad(mp_int *mp, mp_size min); /* left pad with zeroes */
- void s_mp_clamp(mp_int *mp); /* clip leading zeroes */
- void s_mp_exch(mp_int *a, mp_int *b); /* swap a and b in place */
- mp_err s_mp_lshd(mp_int *mp, mp_size p); /* left-shift by p digits */
- void s_mp_rshd(mp_int *mp, mp_size p); /* right-shift by p digits */
- void s_mp_div_2d(mp_int *mp, mp_digit d); /* divide by 2^d in place */
- void s_mp_mod_2d(mp_int *mp, mp_digit d); /* modulo 2^d in place */
- mp_err s_mp_mul_2d(mp_int *mp, mp_digit d); /* multiply by 2^d in place*/
- void s_mp_div_2(mp_int *mp); /* divide by 2 in place */
- mp_err s_mp_mul_2(mp_int *mp); /* multiply by 2 in place */
- mp_digit s_mp_norm(mp_int *a, mp_int *b); /* normalize for division */
- mp_err s_mp_add_d(mp_int *mp, mp_digit d); /* unsigned digit addition */
- mp_err s_mp_sub_d(mp_int *mp, mp_digit d); /* unsigned digit subtract */
- mp_err s_mp_mul_d(mp_int *mp, mp_digit d); /* unsigned digit multiply */
- mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r);
- /* unsigned digit divide */
- mp_err s_mp_reduce(mp_int *x, mp_int *m, mp_int *mu);
- /* Barrett reduction */
- mp_err s_mp_add(mp_int *a, mp_int *b); /* magnitude addition */
- mp_err s_mp_sub(mp_int *a, mp_int *b); /* magnitude subtract */
- mp_err s_mp_mul(mp_int *a, mp_int *b); /* magnitude multiply */
- #if 0
- void s_mp_kmul(mp_digit *a, mp_digit *b, mp_digit *out, mp_size len);
- /* multiply buffers in place */
- #endif
- #if MP_SQUARE
- mp_err s_mp_sqr(mp_int *a); /* magnitude square */
- #else
- #define s_mp_sqr(a) s_mp_mul(a, a)
- #endif
- mp_err s_mp_div(mp_int *a, mp_int *b); /* magnitude divide */
- mp_err s_mp_2expt(mp_int *a, mp_digit k); /* a = 2^k */
- int s_mp_cmp(mp_int *a, mp_int *b); /* magnitude comparison */
- int s_mp_cmp_d(mp_int *a, mp_digit d); /* magnitude digit compare */
- int s_mp_ispow2(mp_int *v); /* is v a power of 2? */
- int s_mp_ispow2d(mp_digit d); /* is d a power of 2? */
- int s_mp_tovalue(char ch, int r); /* convert ch to value */
- char s_mp_todigit(int val, int r, int low); /* convert val to digit */
- int s_mp_outlen(int bits, int r); /* output length in bytes */
- /* }}} */
- /* {{{ Default precision manipulation */
- unsigned int mp_get_prec(void)
- {
- return s_mp_defprec;
- } /* end mp_get_prec() */
- void mp_set_prec(unsigned int prec)
- {
- if(prec == 0)
- s_mp_defprec = MP_DEFPREC;
- else
- s_mp_defprec = prec;
- } /* end mp_set_prec() */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ mp_init(mp) */
- /*
- mp_init(mp)
- Initialize a new zero-valued mp_int. Returns MP_OKAY if successful,
- MP_MEM if memory could not be allocated for the structure.
- */
- mp_err mp_init(mp_int *mp)
- {
- return mp_init_size(mp, s_mp_defprec);
- } /* end mp_init() */
- /* }}} */
- /* {{{ mp_init_array(mp[], count) */
- mp_err mp_init_array(mp_int mp[], int count)
- {
- mp_err res;
- int pos;
- ARGCHK(mp !=NULL && count > 0, MP_BADARG);
- for(pos = 0; pos < count; ++pos) {
- if((res = mp_init(&mp[pos])) != MP_OKAY)
- goto CLEANUP;
- }
- return MP_OKAY;
- CLEANUP:
- while(--pos >= 0)
- mp_clear(&mp[pos]);
- return res;
- } /* end mp_init_array() */
- /* }}} */
- /* {{{ mp_init_size(mp, prec) */
- /*
- mp_init_size(mp, prec)
- Initialize a new zero-valued mp_int with at least the given
- precision; returns MP_OKAY if successful, or MP_MEM if memory could
- not be allocated for the structure.
- */
- mp_err mp_init_size(mp_int *mp, mp_size prec)
- {
- ARGCHK(mp != NULL && prec > 0, MP_BADARG);
- if((DIGITS(mp) = s_mp_alloc(prec, sizeof(mp_digit))) == NULL)
- return MP_MEM;
- SIGN(mp) = MP_ZPOS;
- USED(mp) = 1;
- ALLOC(mp) = prec;
- return MP_OKAY;
- } /* end mp_init_size() */
- /* }}} */
- /* {{{ mp_init_copy(mp, from) */
- /*
- mp_init_copy(mp, from)
- Initialize mp as an exact copy of from. Returns MP_OKAY if
- successful, MP_MEM if memory could not be allocated for the new
- structure.
- */
- mp_err mp_init_copy(mp_int *mp, mp_int *from)
- {
- ARGCHK(mp != NULL && from != NULL, MP_BADARG);
- if(mp == from)
- return MP_OKAY;
- if((DIGITS(mp) = s_mp_alloc(USED(from), sizeof(mp_digit))) == NULL)
- return MP_MEM;
- s_mp_copy(DIGITS(from), DIGITS(mp), USED(from));
- USED(mp) = USED(from);
- ALLOC(mp) = USED(from);
- SIGN(mp) = SIGN(from);
- return MP_OKAY;
- } /* end mp_init_copy() */
- /* }}} */
- /* {{{ mp_copy(from, to) */
- /*
- mp_copy(from, to)
- Copies the mp_int 'from' to the mp_int 'to'. It is presumed that
- 'to' has already been initialized (if not, use mp_init_copy()
- instead). If 'from' and 'to' are identical, nothing happens.
- */
- mp_err mp_copy(mp_int *from, mp_int *to)
- {
- ARGCHK(from != NULL && to != NULL, MP_BADARG);
- if(from == to)
- return MP_OKAY;
- { /* copy */
- mp_digit *tmp;
- /*
- If the allocated buffer in 'to' already has enough space to hold
- all the used digits of 'from', we'll re-use it to avoid hitting
- the memory allocater more than necessary; otherwise, we'd have
- to grow anyway, so we just allocate a hunk and make the copy as
- usual
- */
- if(ALLOC(to) >= USED(from)) {
- s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from));
- s_mp_copy(DIGITS(from), DIGITS(to), USED(from));
-
- } else {
- if((tmp = s_mp_alloc(USED(from), sizeof(mp_digit))) == NULL)
- return MP_MEM;
- s_mp_copy(DIGITS(from), tmp, USED(from));
- if(DIGITS(to) != NULL) {
- #if MP_CRYPTO
- s_mp_setz(DIGITS(to), ALLOC(to));
- #endif
- s_mp_free(DIGITS(to));
- }
- DIGITS(to) = tmp;
- ALLOC(to) = USED(from);
- }
- /* Copy the precision and sign from the original */
- USED(to) = USED(from);
- SIGN(to) = SIGN(from);
- } /* end copy */
- return MP_OKAY;
- } /* end mp_copy() */
- /* }}} */
- /* {{{ mp_exch(mp1, mp2) */
- /*
- mp_exch(mp1, mp2)
- Exchange mp1 and mp2 without allocating any intermediate memory
- (well, unless you count the stack space needed for this call and the
- locals it creates...). This cannot fail.
- */
- void mp_exch(mp_int *mp1, mp_int *mp2)
- {
- #if MP_ARGCHK == 2
- assert(mp1 != NULL && mp2 != NULL);
- #else
- if(mp1 == NULL || mp2 == NULL)
- return;
- #endif
- s_mp_exch(mp1, mp2);
- } /* end mp_exch() */
- /* }}} */
- /* {{{ mp_clear(mp) */
- /*
- mp_clear(mp)
- Release the storage used by an mp_int, and void its fields so that
- if someone calls mp_clear() again for the same int later, we won't
- get tollchocked.
- */
- void mp_clear(mp_int *mp)
- {
- if(mp == NULL)
- return;
- if(DIGITS(mp) != NULL) {
- #if MP_CRYPTO
- s_mp_setz(DIGITS(mp), ALLOC(mp));
- #endif
- s_mp_free(DIGITS(mp));
- DIGITS(mp) = NULL;
- }
- USED(mp) = 0;
- ALLOC(mp) = 0;
- } /* end mp_clear() */
- /* }}} */
- /* {{{ mp_clear_array(mp[], count) */
- void mp_clear_array(mp_int mp[], int count)
- {
- ARGCHK(mp != NULL && count > 0, MP_BADARG);
- while(--count >= 0)
- mp_clear(&mp[count]);
- } /* end mp_clear_array() */
- /* }}} */
- /* {{{ mp_zero(mp) */
- /*
- mp_zero(mp)
- Set mp to zero. Does not change the allocated size of the structure,
- and therefore cannot fail (except on a bad argument, which we ignore)
- */
- void mp_zero(mp_int *mp)
- {
- if(mp == NULL)
- return;
- s_mp_setz(DIGITS(mp), ALLOC(mp));
- USED(mp) = 1;
- SIGN(mp) = MP_ZPOS;
- } /* end mp_zero() */
- /* }}} */
- /* {{{ mp_set(mp, d) */
- void mp_set(mp_int *mp, mp_digit d)
- {
- if(mp == NULL)
- return;
- mp_zero(mp);
- DIGIT(mp, 0) = d;
- } /* end mp_set() */
- /* }}} */
- /* {{{ mp_set_int(mp, z) */
- mp_err mp_set_int(mp_int *mp, long z)
- {
- int ix;
- unsigned long v = abs(z);
- mp_err res;
- ARGCHK(mp != NULL, MP_BADARG);
- mp_zero(mp);
- if(z == 0)
- return MP_OKAY; /* shortcut for zero */
- for(ix = sizeof(long) - 1; ix >= 0; ix--) {
- if((res = s_mp_mul_2d(mp, CHAR_BIT)) != MP_OKAY)
- return res;
- res = s_mp_add_d(mp,
- (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
- if(res != MP_OKAY)
- return res;
- }
- if(z < 0)
- SIGN(mp) = MP_NEG;
- return MP_OKAY;
- } /* end mp_set_int() */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ Digit arithmetic */
- /* {{{ mp_add_d(a, d, b) */
- /*
- mp_add_d(a, d, b)
- Compute the sum b = a + d, for a single digit d. Respects the sign of
- its primary addend (single digits are unsigned anyway).
- */
- mp_err mp_add_d(mp_int *a, mp_digit d, mp_int *b)
- {
- mp_err res = MP_OKAY;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- if((res = mp_copy(a, b)) != MP_OKAY)
- return res;
- if(SIGN(b) == MP_ZPOS) {
- res = s_mp_add_d(b, d);
- } else if(s_mp_cmp_d(b, d) >= 0) {
- res = s_mp_sub_d(b, d);
- } else {
- SIGN(b) = MP_ZPOS;
- DIGIT(b, 0) = d - DIGIT(b, 0);
- }
- return res;
- } /* end mp_add_d() */
- /* }}} */
- /* {{{ mp_sub_d(a, d, b) */
- /*
- mp_sub_d(a, d, b)
- Compute the difference b = a - d, for a single digit d. Respects the
- sign of its subtrahend (single digits are unsigned anyway).
- */
- mp_err mp_sub_d(mp_int *a, mp_digit d, mp_int *b)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- if((res = mp_copy(a, b)) != MP_OKAY)
- return res;
- if(SIGN(b) == MP_NEG) {
- if((res = s_mp_add_d(b, d)) != MP_OKAY)
- return res;
- } else if(s_mp_cmp_d(b, d) >= 0) {
- if((res = s_mp_sub_d(b, d)) != MP_OKAY)
- return res;
- } else {
- mp_neg(b, b);
- DIGIT(b, 0) = d - DIGIT(b, 0);
- SIGN(b) = MP_NEG;
- }
- if(s_mp_cmp_d(b, 0) == 0)
- SIGN(b) = MP_ZPOS;
- return MP_OKAY;
- } /* end mp_sub_d() */
- /* }}} */
- /* {{{ mp_mul_d(a, d, b) */
- /*
- mp_mul_d(a, d, b)
- Compute the product b = a * d, for a single digit d. Respects the sign
- of its multiplicand (single digits are unsigned anyway)
- */
- mp_err mp_mul_d(mp_int *a, mp_digit d, mp_int *b)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- if(d == 0) {
- mp_zero(b);
- return MP_OKAY;
- }
- if((res = mp_copy(a, b)) != MP_OKAY)
- return res;
- res = s_mp_mul_d(b, d);
- return res;
- } /* end mp_mul_d() */
- /* }}} */
- /* {{{ mp_mul_2(a, c) */
- mp_err mp_mul_2(mp_int *a, mp_int *c)
- {
- mp_err res;
- ARGCHK(a != NULL && c != NULL, MP_BADARG);
- if((res = mp_copy(a, c)) != MP_OKAY)
- return res;
- return s_mp_mul_2(c);
- } /* end mp_mul_2() */
- /* }}} */
- /* {{{ mp_div_d(a, d, q, r) */
- /*
- mp_div_d(a, d, q, r)
- Compute the quotient q = a / d and remainder r = a mod d, for a
- single digit d. Respects the sign of its divisor (single digits are
- unsigned anyway).
- */
- mp_err mp_div_d(mp_int *a, mp_digit d, mp_int *q, mp_digit *r)
- {
- mp_err res;
- mp_digit rem;
- int pow;
- ARGCHK(a != NULL, MP_BADARG);
- if(d == 0)
- return MP_RANGE;
- /* Shortcut for powers of two ... */
- if((pow = s_mp_ispow2d(d)) >= 0) {
- mp_digit mask;
- mask = (1 << pow) - 1;
- rem = DIGIT(a, 0) & mask;
- if(q) {
- mp_copy(a, q);
- s_mp_div_2d(q, pow);
- }
- if(r)
- *r = rem;
- return MP_OKAY;
- }
- /*
- If the quotient is actually going to be returned, we'll try to
- avoid hitting the memory allocator by copying the dividend into it
- and doing the division there. This can't be any _worse_ than
- always copying, and will sometimes be better (since it won't make
- another copy)
- If it's not going to be returned, we need to allocate a temporary
- to hold the quotient, which will just be discarded.
- */
- if(q) {
- if((res = mp_copy(a, q)) != MP_OKAY)
- return res;
- res = s_mp_div_d(q, d, &rem);
- if(s_mp_cmp_d(q, 0) == MP_EQ)
- SIGN(q) = MP_ZPOS;
- } else {
- mp_int qp;
- if((res = mp_init_copy(&qp, a)) != MP_OKAY)
- return res;
- res = s_mp_div_d(&qp, d, &rem);
- if(s_mp_cmp_d(&qp, 0) == 0)
- SIGN(&qp) = MP_ZPOS;
- mp_clear(&qp);
- }
- if(r)
- *r = rem;
- return res;
- } /* end mp_div_d() */
- /* }}} */
- /* {{{ mp_div_2(a, c) */
- /*
- mp_div_2(a, c)
- Compute c = a / 2, disregarding the remainder.
- */
- mp_err mp_div_2(mp_int *a, mp_int *c)
- {
- mp_err res;
- ARGCHK(a != NULL && c != NULL, MP_BADARG);
- if((res = mp_copy(a, c)) != MP_OKAY)
- return res;
- s_mp_div_2(c);
- return MP_OKAY;
- } /* end mp_div_2() */
- /* }}} */
- /* {{{ mp_expt_d(a, d, b) */
- mp_err mp_expt_d(mp_int *a, mp_digit d, mp_int *c)
- {
- mp_int s, x;
- mp_err res;
- ARGCHK(a != NULL && c != NULL, MP_BADARG);
- if((res = mp_init(&s)) != MP_OKAY)
- return res;
- if((res = mp_init_copy(&x, a)) != MP_OKAY)
- goto X;
- DIGIT(&s, 0) = 1;
- while(d != 0) {
- if(d & 1) {
- if((res = s_mp_mul(&s, &x)) != MP_OKAY)
- goto CLEANUP;
- }
- d >>= 1;
- if((res = s_mp_sqr(&x)) != MP_OKAY)
- goto CLEANUP;
- }
- s_mp_exch(&s, c);
- CLEANUP:
- mp_clear(&x);
- X:
- mp_clear(&s);
- return res;
- } /* end mp_expt_d() */
- /* }}} */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ Full arithmetic */
- /* {{{ mp_abs(a, b) */
- /*
- mp_abs(a, b)
- Compute b = |a|. 'a' and 'b' may be identical.
- */
- mp_err mp_abs(mp_int *a, mp_int *b)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- if((res = mp_copy(a, b)) != MP_OKAY)
- return res;
- SIGN(b) = MP_ZPOS;
- return MP_OKAY;
- } /* end mp_abs() */
- /* }}} */
- /* {{{ mp_neg(a, b) */
- /*
- mp_neg(a, b)
- Compute b = -a. 'a' and 'b' may be identical.
- */
- mp_err mp_neg(mp_int *a, mp_int *b)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- if((res = mp_copy(a, b)) != MP_OKAY)
- return res;
- if(s_mp_cmp_d(b, 0) == MP_EQ)
- SIGN(b) = MP_ZPOS;
- else
- SIGN(b) = (SIGN(b) == MP_NEG) ? MP_ZPOS : MP_NEG;
- return MP_OKAY;
- } /* end mp_neg() */
- /* }}} */
- /* {{{ mp_add(a, b, c) */
- /*
- mp_add(a, b, c)
- Compute c = a + b. All parameters may be identical.
- */
- mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
- {
- mp_err res;
- int cmp;
- ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
- if(SIGN(a) == SIGN(b)) { /* same sign: add values, keep sign */
- /* Commutativity of addition lets us do this in either order,
- so we avoid having to use a temporary even if the result
- is supposed to replace the output
- */
- if(c == b) {
- if((res = s_mp_add(c, a)) != MP_OKAY)
- return res;
- } else {
- if(c != a && (res = mp_copy(a, c)) != MP_OKAY)
- return res;
- if((res = s_mp_add(c, b)) != MP_OKAY)
- return res;
- }
- } else if((cmp = s_mp_cmp(a, b)) > 0) { /* different sign: a > b */
- /* If the output is going to be clobbered, we will use a temporary
- variable; otherwise, we'll do it without touching the memory
- allocator at all, if possible
- */
- if(c == b) {
- mp_int tmp;
- if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
- return res;
- if((res = s_mp_sub(&tmp, b)) != MP_OKAY) {
- mp_clear(&tmp);
- return res;
- }
- s_mp_exch(&tmp, c);
- mp_clear(&tmp);
- } else {
- if(c != a && (res = mp_copy(a, c)) != MP_OKAY)
- return res;
- if((res = s_mp_sub(c, b)) != MP_OKAY)
- return res;
- }
- } else if(cmp == 0) { /* different sign, a == b */
- mp_zero(c);
- return MP_OKAY;
- } else { /* different sign: a < b */
- /* See above... */
- if(c == a) {
- mp_int tmp;
- if((res = mp_init_copy(&tmp, b)) != MP_OKAY)
- return res;
- if((res = s_mp_sub(&tmp, a)) != MP_OKAY) {
- mp_clear(&tmp);
- return res;
- }
- s_mp_exch(&tmp, c);
- mp_clear(&tmp);
- } else {
- if(c != b && (res = mp_copy(b, c)) != MP_OKAY)
- return res;
- if((res = s_mp_sub(c, a)) != MP_OKAY)
- return res;
- }
- }
- if(USED(c) == 1 && DIGIT(c, 0) == 0)
- SIGN(c) = MP_ZPOS;
- return MP_OKAY;
- } /* end mp_add() */
- /* }}} */
- /* {{{ mp_sub(a, b, c) */
- /*
- mp_sub(a, b, c)
- Compute c = a - b. All parameters may be identical.
- */
- mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c)
- {
- mp_err res;
- int cmp;
- ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
- if(SIGN(a) != SIGN(b)) {
- if(c == a) {
- if((res = s_mp_add(c, b)) != MP_OKAY)
- return res;
- } else {
- if(c != b && ((res = mp_copy(b, c)) != MP_OKAY))
- return res;
- if((res = s_mp_add(c, a)) != MP_OKAY)
- return res;
- SIGN(c) = SIGN(a);
- }
- } else if((cmp = s_mp_cmp(a, b)) > 0) { /* Same sign, a > b */
- if(c == b) {
- mp_int tmp;
- if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
- return res;
- if((res = s_mp_sub(&tmp, b)) != MP_OKAY) {
- mp_clear(&tmp);
- return res;
- }
- s_mp_exch(&tmp, c);
- mp_clear(&tmp);
- } else {
- if(c != a && ((res = mp_copy(a, c)) != MP_OKAY))
- return res;
- if((res = s_mp_sub(c, b)) != MP_OKAY)
- return res;
- }
- } else if(cmp == 0) { /* Same sign, equal magnitude */
- mp_zero(c);
- return MP_OKAY;
- } else { /* Same sign, b > a */
- if(c == a) {
- mp_int tmp;
- if((res = mp_init_copy(&tmp, b)) != MP_OKAY)
- return res;
- if((res = s_mp_sub(&tmp, a)) != MP_OKAY) {
- mp_clear(&tmp);
- return res;
- }
- s_mp_exch(&tmp, c);
- mp_clear(&tmp);
- } else {
- if(c != b && ((res = mp_copy(b, c)) != MP_OKAY))
- return res;
- if((res = s_mp_sub(c, a)) != MP_OKAY)
- return res;
- }
- SIGN(c) = !SIGN(b);
- }
- if(USED(c) == 1 && DIGIT(c, 0) == 0)
- SIGN(c) = MP_ZPOS;
- return MP_OKAY;
- } /* end mp_sub() */
- /* }}} */
- /* {{{ mp_mul(a, b, c) */
- /*
- mp_mul(a, b, c)
- Compute c = a * b. All parameters may be identical.
- */
- mp_err mp_mul(mp_int *a, mp_int *b, mp_int *c)
- {
- mp_err res;
- mp_sign sgn;
- ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
- sgn = (SIGN(a) == SIGN(b)) ? MP_ZPOS : MP_NEG;
- if(c == b) {
- if((res = s_mp_mul(c, a)) != MP_OKAY)
- return res;
- } else {
- if((res = mp_copy(a, c)) != MP_OKAY)
- return res;
- if((res = s_mp_mul(c, b)) != MP_OKAY)
- return res;
- }
-
- if(sgn == MP_ZPOS || s_mp_cmp_d(c, 0) == MP_EQ)
- SIGN(c) = MP_ZPOS;
- else
- SIGN(c) = sgn;
-
- return MP_OKAY;
- } /* end mp_mul() */
- /* }}} */
- /* {{{ mp_mul_2d(a, d, c) */
- /*
- mp_mul_2d(a, d, c)
- Compute c = a * 2^d. a may be the same as c.
- */
- mp_err mp_mul_2d(mp_int *a, mp_digit d, mp_int *c)
- {
- mp_err res;
- ARGCHK(a != NULL && c != NULL, MP_BADARG);
- if((res = mp_copy(a, c)) != MP_OKAY)
- return res;
- if(d == 0)
- return MP_OKAY;
- return s_mp_mul_2d(c, d);
- } /* end mp_mul() */
- /* }}} */
- /* {{{ mp_sqr(a, b) */
- #if MP_SQUARE
- mp_err mp_sqr(mp_int *a, mp_int *b)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- if((res = mp_copy(a, b)) != MP_OKAY)
- return res;
- if((res = s_mp_sqr(b)) != MP_OKAY)
- return res;
- SIGN(b) = MP_ZPOS;
- return MP_OKAY;
- } /* end mp_sqr() */
- #endif
- /* }}} */
- /* {{{ mp_div(a, b, q, r) */
- /*
- mp_div(a, b, q, r)
- Compute q = a / b and r = a mod b. Input parameters may be re-used
- as output parameters. If q or r is NULL, that portion of the
- computation will be discarded (although it will still be computed)
- Pay no attention to the hacker behind the curtain.
- */
- mp_err mp_div(mp_int *a, mp_int *b, mp_int *q, mp_int *r)
- {
- mp_err res;
- mp_int qtmp, rtmp;
- int cmp;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- if(mp_cmp_z(b) == MP_EQ)
- return MP_RANGE;
- /* If a <= b, we can compute the solution without division, and
- avoid any memory allocation
- */
- if((cmp = s_mp_cmp(a, b)) < 0) {
- if(r) {
- if((res = mp_copy(a, r)) != MP_OKAY)
- return res;
- }
- if(q)
- mp_zero(q);
- return MP_OKAY;
- } else if(cmp == 0) {
- /* Set quotient to 1, with appropriate sign */
- if(q) {
- int qneg = (SIGN(a) != SIGN(b));
- mp_set(q, 1);
- if(qneg)
- SIGN(q) = MP_NEG;
- }
- if(r)
- mp_zero(r);
- return MP_OKAY;
- }
- /* If we get here, it means we actually have to do some division */
- /* Set up some temporaries... */
- if((res = mp_init_copy(&qtmp, a)) != MP_OKAY)
- return res;
- if((res = mp_init_copy(&rtmp, b)) != MP_OKAY)
- goto CLEANUP;
- if((res = s_mp_div(&qtmp, &rtmp)) != MP_OKAY)
- goto CLEANUP;
- /* Compute the signs for the output */
- SIGN(&rtmp) = SIGN(a); /* Sr = Sa */
- if(SIGN(a) == SIGN(b))
- SIGN(&qtmp) = MP_ZPOS; /* Sq = MP_ZPOS if Sa = Sb */
- else
- SIGN(&qtmp) = MP_NEG; /* Sq = MP_NEG if Sa != Sb */
- if(s_mp_cmp_d(&qtmp, 0) == MP_EQ)
- SIGN(&qtmp) = MP_ZPOS;
- if(s_mp_cmp_d(&rtmp, 0) == MP_EQ)
- SIGN(&rtmp) = MP_ZPOS;
- /* Copy output, if it is needed */
- if(q)
- s_mp_exch(&qtmp, q);
- if(r)
- s_mp_exch(&rtmp, r);
- CLEANUP:
- mp_clear(&rtmp);
- mp_clear(&qtmp);
- return res;
- } /* end mp_div() */
- /* }}} */
- /* {{{ mp_div_2d(a, d, q, r) */
- mp_err mp_div_2d(mp_int *a, mp_digit d, mp_int *q, mp_int *r)
- {
- mp_err res;
- ARGCHK(a != NULL, MP_BADARG);
- if(q) {
- if((res = mp_copy(a, q)) != MP_OKAY)
- return res;
- s_mp_div_2d(q, d);
- }
- if(r) {
- if((res = mp_copy(a, r)) != MP_OKAY)
- return res;
- s_mp_mod_2d(r, d);
- }
- return MP_OKAY;
- } /* end mp_div_2d() */
- /* }}} */
- /* {{{ mp_expt(a, b, c) */
- /*
- mp_expt(a, b, c)
- Compute c = a ** b, that is, raise a to the b power. Uses a
- standard iterative square-and-multiply technique.
- */
- mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
- {
- mp_int s, x;
- mp_err res;
- mp_digit d;
- int dig, bit;
- ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
- if(mp_cmp_z(b) < 0)
- return MP_RANGE;
- if((res = mp_init(&s)) != MP_OKAY)
- return res;
- mp_set(&s, 1);
- if((res = mp_init_copy(&x, a)) != MP_OKAY)
- goto X;
- /* Loop over low-order digits in ascending order */
- for(dig = 0; dig < (USED(b) - 1); dig++) {
- d = DIGIT(b, dig);
- /* Loop over bits of each non-maximal digit */
- for(bit = 0; bit < DIGIT_BIT; bit++) {
- if(d & 1) {
- if((res = s_mp_mul(&s, &x)) != MP_OKAY)
- goto CLEANUP;
- }
- d >>= 1;
-
- if((res = s_mp_sqr(&x)) != MP_OKAY)
- goto CLEANUP;
- }
- }
- /* Consider now the last digit... */
- d = DIGIT(b, dig);
- while(d) {
- if(d & 1) {
- if((res = s_mp_mul(&s, &x)) != MP_OKAY)
- goto CLEANUP;
- }
- d >>= 1;
- if((res = s_mp_sqr(&x)) != MP_OKAY)
- goto CLEANUP;
- }
-
- if(mp_iseven(b))
- SIGN(&s) = SIGN(a);
- res = mp_copy(&s, c);
- CLEANUP:
- mp_clear(&x);
- X:
- mp_clear(&s);
- return res;
- } /* end mp_expt() */
- /* }}} */
- /* {{{ mp_2expt(a, k) */
- /* Compute a = 2^k */
- mp_err mp_2expt(mp_int *a, mp_digit k)
- {
- ARGCHK(a != NULL, MP_BADARG);
- return s_mp_2expt(a, k);
- } /* end mp_2expt() */
- /* }}} */
- /* {{{ mp_mod(a, m, c) */
- /*
- mp_mod(a, m, c)
- Compute c = a (mod m). Result will always be 0 <= c < m.
- */
- mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c)
- {
- mp_err res;
- int mag;
- ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
- if(SIGN(m) == MP_NEG)
- return MP_RANGE;
- /*
- If |a| > m, we need to divide to get the remainder and take the
- absolute value.
- If |a| < m, we don't need to do any division, just copy and adjust
- the sign (if a is negative).
- If |a| == m, we can simply set the result to zero.
- This order is intended to minimize the average path length of the
- comparison chain on common workloads -- the most frequent cases are
- that |a| != m, so we do those first.
- */
- if((mag = s_mp_cmp(a, m)) > 0) {
- if((res = mp_div(a, m, NULL, c)) != MP_OKAY)
- return res;
-
- if(SIGN(c) == MP_NEG) {
- if((res = mp_add(c, m, c)) != MP_OKAY)
- return res;
- }
- } else if(mag < 0) {
- if((res = mp_copy(a, c)) != MP_OKAY)
- return res;
- if(mp_cmp_z(a) < 0) {
- if((res = mp_add(c, m, c)) != MP_OKAY)
- return res;
- }
-
- } else {
- mp_zero(c);
- }
- return MP_OKAY;
- } /* end mp_mod() */
- /* }}} */
- /* {{{ mp_mod_d(a, d, c) */
- /*
- mp_mod_d(a, d, c)
- Compute c = a (mod d). Result will always be 0 <= c < d
- */
- mp_err mp_mod_d(mp_int *a, mp_digit d, mp_digit *c)
- {
- mp_err res;
- mp_digit rem;
- ARGCHK(a != NULL && c != NULL, MP_BADARG);
- if(s_mp_cmp_d(a, d) > 0) {
- if((res = mp_div_d(a, d, NULL, &rem)) != MP_OKAY)
- return res;
- } else {
- if(SIGN(a) == MP_NEG)
- rem = d - DIGIT(a, 0);
- else
- rem = DIGIT(a, 0);
- }
- if(c)
- *c = rem;
- return MP_OKAY;
- } /* end mp_mod_d() */
- /* }}} */
- /* {{{ mp_sqrt(a, b) */
- /*
- mp_sqrt(a, b)
- Compute the integer square root of a, and store the result in b.
- Uses an integer-arithmetic version of Newton's iterative linear
- approximation technique to determine this value; the result has the
- following two properties:
- b^2 <= a
- (b+1)^2 >= a
- It is a range error to pass a negative value.
- */
- mp_err mp_sqrt(mp_int *a, mp_int *b)
- {
- mp_int x, t;
- mp_err res;
- ARGCHK(a != NULL && b != NULL, MP_BADARG);
- /* Cannot take square root of a negative value */
- if(SIGN(a) == MP_NEG)
- return MP_RANGE;
- /* Special cases for zero and one, trivial */
- if(mp_cmp_d(a, 0) == MP_EQ || mp_cmp_d(a, 1) == MP_EQ)
- return mp_copy(a, b);
-
- /* Initialize the temporaries we'll use below */
- if((res = mp_init_size(&t, USED(a))) != MP_OKAY)
- return res;
- /* Compute an initial guess for the iteration as a itself */
- if((res = mp_init_copy(&x, a)) != MP_OKAY)
- goto X;
- s_mp_rshd(&x, (USED(&x)/2)+1);
- mp_add_d(&x, 1, &x);
- for(;;) {
- /* t = (x * x) - a */
- mp_copy(&x, &t); /* can't fail, t is big enough for original x */
- if((res = mp_sqr(&t, &t)) != MP_OKAY ||
- (res = mp_sub(&t, a, &t)) != MP_OKAY)
- goto CLEANUP;
- /* t = t / 2x */
- s_mp_mul_2(&x);
- if((res = mp_div(&t, &x, &t, NULL)) != MP_OKAY)
- goto CLEANUP;
- s_mp_div_2(&x);
- /* Terminate the loop, if the quotient is zero */
- if(mp_cmp_z(&t) == MP_EQ)
- break;
- /* x = x - t */
- if((res = mp_sub(&x, &t, &x)) != MP_OKAY)
- goto CLEANUP;
- }
- /* Copy result to output parameter */
- mp_sub_d(&x, 1, &x);
- s_mp_exch(&x, b);
- CLEANUP:
- mp_clear(&x);
- X:
- mp_clear(&t);
- return res;
- } /* end mp_sqrt() */
- /* }}} */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ Modular arithmetic */
- #if MP_MODARITH
- /* {{{ mp_addmod(a, b, m, c) */
- /*
- mp_addmod(a, b, m, c)
- Compute c = (a + b) mod m
- */
- mp_err mp_addmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
- if((res = mp_add(a, b, c)) != MP_OKAY)
- return res;
- if((res = mp_mod(c, m, c)) != MP_OKAY)
- return res;
- return MP_OKAY;
- }
- /* }}} */
- /* {{{ mp_submod(a, b, m, c) */
- /*
- mp_submod(a, b, m, c)
- Compute c = (a - b) mod m
- */
- mp_err mp_submod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
- if((res = mp_sub(a, b, c)) != MP_OKAY)
- return res;
- if((res = mp_mod(c, m, c)) != MP_OKAY)
- return res;
- return MP_OKAY;
- }
- /* }}} */
- /* {{{ mp_mulmod(a, b, m, c) */
- /*
- mp_mulmod(a, b, m, c)
- Compute c = (a * b) mod m
- */
- mp_err mp_mulmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
- {
- mp_err res;
- ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
- if((res = mp_mul(a, b, c)) != MP_OKAY)
- return res;
- if((res = mp_mod(c, m, c)) != MP_OKAY)
- return res;
- return MP_OKAY;
- }
- /* }}} */
- /* {{{ mp_sqrmod(a, m, c) */
- #if MP_SQUARE
- mp_err mp_sqrmod(mp_int *a, mp_int *m, mp_int *c)
- {
- mp_err res;
- ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
- if((res = mp_sqr(a, c)) != MP_OKAY)
- return res;
- if((res = mp_mod(c, m, c)) != MP_OKAY)
- return res;
- return MP_OKAY;
- } /* end mp_sqrmod() */
- #endif
- /* }}} */
- /* {{{ mp_exptmod(a, b, m, c) */
- /*
- mp_exptmod(a, b, m, c)
- Compute c = (a ** b) mod m. Uses a standard square-and-multiply
- method with modular reductions at each step. (This is basically the
- same code as mp_expt(), except for the addition of the reductions)
-
- The modular reductions are done using Barrett's algorithm (see
- s_mp_reduce() below for details)
- */
- mp_err mp_exptmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
- {
- mp_int s, x, mu;
- mp_err res;
- mp_digit d, *db = DIGITS(b);
- mp_size ub = USED(b);
- int dig, bit;
- ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
- if(mp_cmp_z(b) < 0 || mp_cmp_z(m) <= 0)
- return MP_RANGE;
- if((res = mp_init(&s)) != MP_OKAY)
- return res;
- if((res = mp_init_copy(&x, a)) != MP_OKAY)
- goto X;
- if((res = mp_mod(&x, m, &x)) != MP_OKAY ||
- (res = mp_init(&mu)) != MP_OKAY)
- goto MU;
- mp_set(&s, 1);
- /* mu = b^2k / m */
- s_mp_add_d(&mu, 1);
- s_mp_lshd(&mu, 2 * USED(m));
- if((res = mp_div(&mu, m, &mu, NULL)) != MP_OKAY)
- goto CLEANUP;
- /* Loop over digits of b in ascending order, except highest order */
- for(dig = 0; dig < (ub - 1); dig++) {
- d = *db++;
- /* Loop over the bits of the lower-order digits */
- for(bit = 0; bit < DIGIT_BIT; bit++) {
- if(d & 1) {
- if((res = s_mp_mul(&s, &x)) != MP_OKAY)
- goto CLEANUP;
- if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
- goto CLEANUP;
- }
- d >>= 1;
- if((res = s_mp_sqr(&x)) != MP_OKAY)
- goto CLEANUP;
- if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
- goto CLEANUP;
- }
- }
- /* Now do the last digit... */
- d = *db;
- while(d) {
- if(d & 1) {
- if((res = s_mp_mul(&s, &x)) != MP_OKAY)
- goto CLEANUP;
- if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
- goto CLEANUP;
- }
- d >>= 1;
- if((res = s_mp_sqr(&x)) != MP_OKAY)
- goto CLEANUP;
- if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
- goto CLEANUP;
- }
- s_mp_exch(&s, c);
- CLEANUP:
- mp_clear(&mu);
- MU:
- mp_clear(&x);
- X:
- mp_clear(&s);
- return res;
- } /* end mp_exptmod() */
- /* }}} */
- /* {{{ mp_exptmod_d(a, d, m, c) */
- mp_err mp_exptmod_d(mp_int *a, mp_digit d, mp_int *m, mp_int *c)
- {
- mp_int s, x;
- mp_err res;
- ARGCHK(a != NULL && c != NULL, MP_BADARG);
- if((res = mp_init(&s)) != MP_OKAY)
- return res;
- if((res = mp_init_copy(&x, a)) != MP_OKAY)
- goto X;
- mp_set(&s, 1);
- while(d != 0) {
- if(d & 1) {
- if((res = s_mp_mul(&s, &x)) != MP_OKAY ||
- (res = mp_mod(&s, m, &s)) != MP_OKAY)
- goto CLEANUP;
- }
- d /= 2;
- if((res = s_mp_sqr(&x)) != MP_OKAY ||
- (res = mp_mod(&x, m, &x)) != MP_OKAY)
- goto CLEANUP;
- }
- s_mp_exch(&s, c);
- CLEANUP:
- mp_clear(&x);
- X:
- mp_clear(&s);
- return res;
- } /* end mp_exptmod_d() */
- /* }}} */
- #endif /* if MP_MODARITH */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ Comparison functions */
- /* {{{ mp_cmp_z(a) */
- /*
- mp_cmp_z(a)
- Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
- */
- int mp_cmp_z(mp_int *a)
- {
- if(SIGN(a) == MP_NEG)
- return MP_LT;
- else if(USED(a) == 1 && DIGIT(a, 0) == 0)
- return MP_EQ;
- else
- return MP_GT;
- } /* end mp_cmp_z() */
- /* }}} */
- /* {{{ mp_cmp_d(a, d) */
- /*
- mp_cmp_d(a, d)
- Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d
- */
- int mp_cmp_d(mp_int *a, mp_digit d)
- {
- ARGCHK(a != NULL, MP_EQ);
- if(SIGN(a) == MP_NEG)
- return MP_LT;
- return s_mp_cmp_d(a, d);
- } /* end mp_cmp_d() */
- /* }}} */
- /* {{{ mp_cmp(a, b) */
- int mp_cmp(mp_int *a, mp_int *b)
- {
- ARGCHK(a != NULL && b != NULL, MP_EQ);
- if(SIGN(a) == SIGN(b)) {
- int mag;
- if((mag = s_mp_cmp(a, b)) == MP_EQ)
- return MP_EQ;
- if(SIGN(a) == MP_ZPOS)
- return mag;
- else
- return -mag;
- } else if(SIGN(a) == MP_ZPOS) {
- return MP_GT;
- } else {
- return MP_LT;
- }
- } /* end mp_cmp() */
- /* }}} */
- /* {{{ mp_cmp_mag(a, b) */
- /*
- mp_cmp_mag(a, b)
- Compares |a| <=> |b|, and returns an appropriate comparison result
- */
- int mp_cmp_mag(mp_int *a, mp_int *b)
- {
- ARGCHK(a != NULL && b != NULL, MP_EQ);
- return s_mp_cmp(a, b);
- } /* end mp_cmp_mag() */
- /* }}} */
- /* {{{ mp_cmp_int(a, z) */
- /*
- This just converts z to an mp_int, and uses the existing comparison
- routines. This is sort of inefficient, but it's not clear to me how
- frequently this wil get used anyway. For small positive constants,
- you can always use mp_cmp_d(), and for zero, there is mp_cmp_z().
- */
- int mp_cmp_int(mp_int *a, long z)
- {
- mp_int tmp;
- int out;
- ARGCHK(a != NULL, MP_EQ);
-
- mp_init(&tmp); mp_set_int(&tmp, z);
- out = mp_cmp(a, &tmp);
- mp_clear(&tmp);
- return out;
- } /* end mp_cmp_int() */
- /* }}} */
- /* {{{ mp_isodd(a) */
- /*
- mp_isodd(a)
- Returns a true (non-zero) value if a is odd, false (zero) otherwise.
- */
- int mp_isodd(mp_int *a)
- {
- ARGCHK(a != NULL, 0);
- return (DIGIT(a, 0) & 1);
- } /* end mp_isodd() */
- /* }}} */
- /* {{{ mp_iseven(a) */
- int mp_iseven(mp_int *a)
- {
- return !mp_isodd(a);
- } /* end mp_iseven() */
- /* }}} */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ Number theoretic functions */
- #if MP_NUMTH
- /* {{{ mp_gcd(a, b, c) */
- /*
- Like the old mp_gcd() function, except computes the GCD using the
- binary algorithm due to Josef Stein in 1961 (via Knuth).
- */
- mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
- {
- mp_err res;
- mp_int u, v, t;
- mp_size k = 0;
- ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
- if(mp_cmp_z(a) == MP_EQ && mp_cmp_z(b) == MP_EQ)
- return MP_RANGE;
- if(mp_cmp_z(a) == MP_EQ) {
- return mp_copy(b, c);
- } else if(mp_cmp_z(b) == MP_EQ) {
- return mp_copy(a, c);
- }
- if((res = mp_init(&t)) != MP_OKAY)
- return res;
- if((res = mp_init_copy(&u, a)) != MP_OKAY)
- goto U;
- if((res = mp_init_copy(&v, b)) != MP_OKAY)
- goto V;
- SIGN(&u) = MP_ZPOS;
- SIGN(&v) = MP_ZPOS;
- /* Divide out common factors of 2 until at least 1 of a, b is even */
- while(mp_iseven(&u) && mp_iseven(&v)) {
- s_mp_div_2(&u);
- s_mp_div_2(&v);
- ++k;
- }
- /* Initialize t */
- if(mp_isodd(&u)) {
- if((res = mp_copy(&v, &t)) != MP_OKAY)
- goto CLEANUP;
-
- /* t = -v */
- if(SIGN(&v) == MP_ZPOS)
- SIGN(&t) = MP_NEG;
- else
- SIGN(&t) = MP_ZPOS;
-
- } else {
- if((res = mp_copy(&u, &t)) != MP_OKAY)
- goto CLEANUP;
- }
- for(;;) {
- while(mp_iseven(&t)) {
- s_mp_div_2(&t);
- }
- if(mp_cmp_z(&t) == MP_GT) {
- if((res = mp_copy(&t, &u)) != MP_OKAY)
- goto CLEANUP;
- } else {
- if((res = mp_copy(&t, &v)) != MP_OKAY)
- goto CLEANUP;
- /* v = -t */
- if(SIGN(&t) == MP_ZPOS)
- SIGN(&v) = MP_NEG;
- else
- SIGN(&v) = MP_ZPOS;
- }
- if((res = mp_sub(&u, &v, &t)) != MP_OKAY)
- goto CLEANUP;
- if(s_mp_cmp_d(&t, 0) == MP_EQ)
- break;
- }
- s_mp_2expt(&v, k); /* v = 2^k */
- res = mp_mul(&u, &v, c); /* c = u * v */
- CLEANUP:
- mp_clear(&v);
- V:
- mp_clear(&u);
- U:
- mp_clear(&t);
- return res;
- } /* end mp_bgcd() */
- /* }}} */
- /* {{{ mp_lcm(a, b, c) */
- /* We compute the least common multiple using the rule:
- ab = [a, b](a, b)
- ... by computing the product, and dividing out the gcd.
- */
- mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c)
- {
- mp_int gcd, prod;
- mp_err res;
- ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
- /* Set up temporaries */
- if((res = mp_init(&gcd)) != MP_OKAY)
- return res;
- if((res = mp_init(&prod)) != MP_OKAY)
- goto GCD;
- if((res = mp_mul(a, b, &prod)) != MP_OKAY)
- goto CLEANUP;
- if((res = mp_gcd(a, b, &gcd)) != MP_OKAY)
- goto CLEANUP;
- res = mp_div(&prod, &gcd, c, NULL);
- CLEANUP:
- mp_clear(&prod);
- GCD:
- mp_clear(&gcd);
- return res;
- } /* end mp_lcm() */
- /* }}} */
- /* {{{ mp_xgcd(a, b, g, x, y) */
- /*
- mp_xgcd(a, b, g, x, y)
- Compute g = (a, b) and values x and y satisfying Bezout's identity
- (that is, ax + by = g). This uses the extended binary GCD algorithm
- based on the Stein algorithm used for mp_gcd()
- */
- mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y)
- {
- mp_int gx, xc, yc, u, v, A, B, C, D;
- mp_int *clean[9];
- mp_err res;
- int last = -1;
- if(mp_cmp_z(b) == 0)
- return MP_RANGE;
- /* Initialize all these variables we need */
- if((res = mp_init(&u)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &u;
- if((res = mp_init(&v)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &v;
- if((res = mp_init(&gx)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &gx;
- if((res = mp_init(&A)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &A;
- if((res = mp_init(&B)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &B;
- if((res = mp_init(&C)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &C;
- if((res = mp_init(&D)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &D;
- if((res = mp_init_copy(&xc, a)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &xc;
- mp_abs(&xc, &xc);
- if((res = mp_init_copy(&yc, b)) != MP_OKAY) goto CLEANUP;
- clean[++last] = &yc;
- mp_abs(&yc, &yc);
- mp_set(&gx, 1);
- /* Divide by two until at least one of them is even */
- while(mp_iseven(&xc) && mp_iseven(&yc)) {
- s_mp_div_2(&xc);
- s_mp_div_2(&yc);
- if((res = s_mp_mul_2(&gx)) != MP_OKAY)
- goto CLEANUP;
- }
- mp_copy(&xc, &u);
- mp_copy(&yc, &v);
- mp_set(&A, 1); mp_set(&D, 1);
- /* Loop through binary GCD algorithm */
- for(;;) {
- while(mp_iseven(&u)) {
- s_mp_div_2(&u);
- if(mp_iseven(&A) && mp_iseven(&B)) {
- s_mp_div_2(&A); s_mp_div_2(&B);
- } else {
- if((res = mp_add(&A, &yc, &A)) != MP_OKAY) goto CLEANUP;
- s_mp_div_2(&A);
- if((res = mp_sub(&B, &xc, &B)) != MP_OKAY) goto CLEANUP;
- s_mp_div_2(&B);
- }
- }
- while(mp_iseven(&v)) {
- s_mp_div_2(&v);
- if(mp_iseven(&C) && mp_iseven(&D)) {
- s_mp_div_2(&C); s_mp_div_2(&D);
- } else {
- if((res = mp_add(&C, &yc, &C)) != MP_OKAY) goto CLEANUP;
- s_mp_div_2(&C);
- if((res = mp_sub(&D, &xc, &D)) != MP_OKAY) goto CLEANUP;
- s_mp_div_2(&D);
- }
- }
- if(mp_cmp(&u, &v) >= 0) {
- if((res = mp_sub(&u, &v, &u)) != MP_OKAY) goto CLEANUP;
- if((res = mp_sub(&A, &C, &A)) != MP_OKAY) goto CLEANUP;
- if((res = mp_sub(&B, &D, &B)) != MP_OKAY) goto CLEANUP;
- } else {
- if((res = mp_sub(&v, &u, &v)) != MP_OKAY) goto CLEANUP;
- if((res = mp_sub(&C, &A, &C)) != MP_OKAY) goto CLEANUP;
- if((res = mp_sub(&D, &B, &D)) != MP_OKAY) goto CLEANUP;
- }
- /* If we're done, copy results to output */
- if(mp_cmp_z(&u) == 0) {
- if(x)
- if((res = mp_copy(&C, x)) != MP_OKAY) goto CLEANUP;
- if(y)
- if((res = mp_copy(&D, y)) != MP_OKAY) goto CLEANUP;
-
- if(g)
- if((res = mp_mul(&gx, &v, g)) != MP_OKAY) goto CLEANUP;
- break;
- }
- }
- CLEANUP:
- while(last >= 0)
- mp_clear(clean[last--]);
- return res;
- } /* end mp_xgcd() */
- /* }}} */
- /* {{{ mp_invmod(a, m, c) */
- /*
- mp_invmod(a, m, c)
- Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
- This is equivalent to the question of whether (a, m) = 1. If not,
- MP_UNDEF is returned, and there is no inverse.
- */
- mp_err mp_invmod(mp_int *a, mp_int *m, mp_int *c)
- {
- mp_int g, x;
- mp_err res;
- ARGCHK(a && m && c, MP_BADARG);
- if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
- return MP_RANGE;
- if((res = mp_init(&g)) != MP_OKAY)
- return res;
- if((res = mp_init(&x)) != MP_OKAY)
- goto X;
- if((res = mp_xgcd(a, m, &g, &x, NULL)) != MP_OKAY)
- goto CLEANUP;
- if(mp_cmp_d(&g, 1) != MP_EQ) {
- res = MP_UNDEF;
- goto CLEANUP;
- }
- res = mp_mod(&x, m, c);
- SIGN(c) = SIGN(a);
- CLEANUP:
- mp_clear(&x);
- X:
- mp_clear(&g);
- return res;
- } /* end mp_invmod() */
- /* }}} */
- #endif /* if MP_NUMTH */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ mp_print(mp, ofp) */
- #if MP_IOFUNC
- /*
- mp_print(mp, ofp)
- Print a textual representation of the given mp_int on the output
- stream 'ofp'. Output is generated using the internal radix.
- */
- void mp_print(mp_int *mp, FILE *ofp)
- {
- int ix;
- if(mp == NULL || ofp == NULL)
- return;
- fputc((SIGN(mp) == MP_NEG) ? '-' : '+', ofp);
- for(ix = USED(mp) - 1; ix >= 0; ix--) {
- fprintf(ofp, DIGIT_FMT, DIGIT(mp, ix));
- }
- } /* end mp_print() */
- #endif /* if MP_IOFUNC */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* {{{ More I/O Functions */
- /* {{{ mp_read_signed_bin(mp, str, len) */
- /*
- mp_read_signed_bin(mp, str, len)
- Read in a raw value (base 256) into the given mp_int
- */
- mp_err mp_read_signed_bin(mp_int *mp, unsigned char *str, int len)
- {
- mp_err res;
- ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
- if((res = mp_read_unsigned_bin(mp, str + 1, len - 1)) == MP_OKAY) {
- /* Get sign from first byte */
- if(str[0])
- SIGN(mp) = MP_NEG;
- else
- SIGN(mp) = MP_ZPOS;
- }
- return res;
- } /* end mp_read_signed_bin() */
- /* }}} */
- /* {{{ mp_signed_bin_size(mp) */
- int mp_signed_bin_size(mp_int *mp)
- {
- ARGCHK(mp != NULL, 0);
- return mp_unsigned_bin_size(mp) + 1;
- } /* end mp_signed_bin_size() */
- /* }}} */
- /* {{{ mp_to_signed_bin(mp, str) */
- mp_err mp_to_signed_bin(mp_int *mp, unsigned char *str)
- {
- ARGCHK(mp != NULL && str != NULL, MP_BADARG);
- /* Caller responsible for allocating enough memory (use mp_raw_size(mp)) */
- str[0] = (char)SIGN(mp);
- return mp_to_unsigned_bin(mp, str + 1);
- } /* end mp_to_signed_bin() */
- /* }}} */
- /* {{{ mp_read_unsigned_bin(mp, str, len) */
- /*
- mp_read_unsigned_bin(mp, str, len)
- Read in an unsigned value (base 256) into the given mp_int
- */
- mp_err mp_read_unsigned_bin(mp_int *mp, unsigned char *str, int len)
- {
- int ix;
- mp_err res;
- ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
- mp_zero(mp);
- for(ix = 0; ix < len; ix++) {
- if((res = s_mp_mul_2d(mp, CHAR_BIT)) != MP_OKAY)
- return res;
- if((res = mp_add_d(mp, str[ix], mp)) != MP_OKAY)
- return res;
- }
-
- return MP_OKAY;
-
- } /* end mp_read_unsigned_bin() */
- /* }}} */
- /* {{{ mp_unsigned_bin_size(mp) */
- int mp_unsigned_bin_size(mp_int *mp)
- {
- mp_digit topdig;
- int count;
- ARGCHK(mp != NULL, 0);
- /* Special case for the value zero */
- if(USED(mp) == 1 && DIGIT(mp, 0) == 0)
- return 1;
- count = (USED(mp) - 1) * sizeof(mp_digit);
- topdig = DIGIT(mp, USED(mp) - 1);
- while(topdig != 0) {
- ++count;
- topdig >>= CHAR_BIT;
- }
- return count;
- } /* end mp_unsigned_bin_size() */
- /* }}} */
- /* {{{ mp_to_unsigned_bin(mp, str) */
- mp_err mp_to_unsigned_bin(mp_int *mp, unsigned char *str)
- {
- mp_digit *dp, *end, d;
- unsigned char *spos;
- ARGCHK(mp != NULL && str != NULL, MP_BADARG);
- dp = DIGITS(mp);
- end = dp + USED(mp) - 1;
- spos = str;
- /* Special case for zero, quick test */
- if(dp == end && *dp == 0) {
- *str = '\0';
- return MP_OKAY;
- }
- /* Generate digits in reverse order */
- while(dp < end) {
- int ix;
- d = *dp;
- for(ix = 0; ix < sizeof(mp_digit); ++ix) {
- *spos = d & UCHAR_MAX;
- d >>= CHAR_BIT;
- ++spos;
- }
- ++dp;
- }
- /* Now handle last digit specially, high order zeroes are not written */
- d = *end;
- while(d != 0) {
- *spos = d & UCHAR_MAX;
- d >>= CHAR_BIT;
- ++spos;
- }
- /* Reverse everything to get digits in the correct order */
- while(--spos > str) {
- unsigned char t = *str;
- *str = *spos;
- *spos = t;
- ++str;
- }
- return MP_OKAY;
- } /* end mp_to_unsigned_bin() */
- /* }}} */
- /* {{{ mp_count_bits(mp) */
- int mp_count_bits(mp_int *mp)
- {
- int len;
- mp_digit d;
- ARGCHK(mp != NULL, MP_BADARG);
- len = DIGIT_BIT * (USED(mp) - 1);
- d = DIGIT(mp, USED(mp) - 1);
- while(d != 0) {
- ++len;
- d >>= 1;
- }
- return len;
-
- } /* end mp_count_bits() */
- /* }}} */
- /* {{{ mp_read_radix(mp, str, radix) */
- /*
- mp_read_radix(mp, str, radix)
- Read an integer from the given string, and set mp to the resulting
- value. The input is presumed to be in base 10. Leading non-digit
- characters are ignored, and the function reads until a non-digit
- character or the end of the string.
- */
- mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix)
- {
- int ix = 0, val = 0;
- mp_err res;
- mp_sign sig = MP_ZPOS;
- ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
- MP_BADARG);
- mp_zero(mp);
- /* Skip leading non-digit characters until a digit or '-' or '+' */
- while(str[ix] &&
- (s_mp_tovalue(str[ix], radix) < 0) &&
- str[ix] != '-' &&
- str[ix] != '+') {
- ++ix;
- }
- if(str[ix] == '-') {
- sig = MP_NEG;
- ++ix;
- } else if(str[ix] == '+') {
- sig = MP_ZPOS; /* this is the default anyway... */
- ++ix;
- }
- while((val = s_mp_tovalue(str[ix], radix)) >= 0) {
- if((res = s_mp_mul_d(mp, radix)) != MP_OKAY)
- return res;
- if((res = s_mp_add_d(mp, val)) != MP_OKAY)
- return res;
- ++ix;
- }
- if(s_mp_cmp_d(mp, 0) == MP_EQ)
- SIGN(mp) = MP_ZPOS;
- else
- SIGN(mp) = sig;
- return MP_OKAY;
- } /* end mp_read_radix() */
- /* }}} */
- /* {{{ mp_radix_size(mp, radix) */
- int mp_radix_size(mp_int *mp, int radix)
- {
- int len;
- ARGCHK(mp != NULL, 0);
- len = s_mp_outlen(mp_count_bits(mp), radix) + 1; /* for NUL terminator */
- if(mp_cmp_z(mp) < 0)
- ++len; /* for sign */
- return len;
- } /* end mp_radix_size() */
- /* }}} */
- /* {{{ mp_value_radix_size(num, qty, radix) */
- /* num = number of digits
- qty = number of bits per digit
- radix = target base
-
- Return the number of digits in the specified radix that would be
- needed to express 'num' digits of 'qty' bits each.
- */
- int mp_value_radix_size(int num, int qty, int radix)
- {
- ARGCHK(num >= 0 && qty > 0 && radix >= 2 && radix <= MAX_RADIX, 0);
- return s_mp_outlen(num * qty, radix);
- } /* end mp_value_radix_size() */
- /* }}} */
- /* {{{ mp_toradix(mp, str, radix) */
- mp_err mp_toradix(mp_int *mp, unsigned char *str, int radix)
- {
- int ix, pos = 0;
- ARGCHK(mp != NULL && str != NULL, MP_BADARG);
- ARGCHK(radix > 1 && radix <= MAX_RADIX, MP_RANGE);
- if(mp_cmp_z(mp) == MP_EQ) {
- str[0] = '0';
- str[1] = '\0';
- } else {
- mp_err res;
- mp_int tmp;
- mp_sign sgn;
- mp_digit rem, rdx = (mp_digit)radix;
- char ch;
- if((res = mp_init_copy(&tmp, mp)) != MP_OKAY)
- return res;
- /* Save sign for later, and take absolute value */
- sgn = SIGN(&tmp); SIGN(&tmp) = MP_ZPOS;
- /* Generate output digits in reverse order */
- while(mp_cmp_z(&tmp) != 0) {
- if((res = s_mp_div_d(&tmp, rdx, &rem)) != MP_OKAY) {
- mp_clear(&tmp);
- return res;
- }
- /* Generate digits, use capital letters */
- ch = s_mp_todigit(rem, radix, 0);
- str[pos++] = ch;
- }
- /* Add - sign if original value was negative */
- if(sgn == MP_NEG)
- str[pos++] = '-';
- /* Add trailing NUL to end the string */
- str[pos--] = '\0';
- /* Reverse the digits and sign indicator */
- ix = 0;
- while(ix < pos) {
- char tmp = str[ix];
- str[ix] = str[pos];
- str[pos] = tmp;
- ++ix;
- --pos;
- }
-
- mp_clear(&tmp);
- }
- return MP_OKAY;
- } /* end mp_toradix() */
- /* }}} */
- /* {{{ mp_char2value(ch, r) */
- int mp_char2value(char ch, int r)
- {
- return s_mp_tovalue(ch, r);
- } /* end mp_tovalue() */
- /* }}} */
- /* }}} */
- /* {{{ mp_strerror(ec) */
- /*
- mp_strerror(ec)
- Return a string describing the meaning of error code 'ec'. The
- string returned is allocated in static memory, so the caller should
- not attempt to modify or free the memory associated with this
- string.
- */
- const char *mp_strerror(mp_err ec)
- {
- int aec = (ec < 0) ? -ec : ec;
- /* Code values are negative, so the senses of these comparisons
- are accurate */
- if(ec < MP_LAST_CODE || ec > MP_OKAY) {
- return mp_err_string[0]; /* unknown error code */
- } else {
- return mp_err_string[aec + 1];
- }
- } /* end mp_strerror() */
- /* }}} */
- /*========================================================================*/
- /*------------------------------------------------------------------------*/
- /* Static function definitions (internal use only) */
- /* {{{ Memory management */
- /* {{{ s_mp_grow(mp, min) */
- /* Make sure there are at least 'min' digits allocated to mp */
- mp_err s_mp_grow(mp_int *mp, mp_size min)
- {
- if(min > ALLOC(mp)) {
- mp_digit *tmp;
- /* Set min to next nearest default precision block size */
- min = ((min + (s_mp_defprec - 1)) / s_mp_defprec) * s_mp_defprec;
- if((tmp = s_mp_alloc(min, sizeof(mp_digit))) == NULL)
- return MP_MEM;
- s_mp_copy(DIGITS(mp), tmp, USED(mp));
- #if MP_CRYPTO
- s_mp_setz(DIGITS(mp), ALLOC(mp));
- #endif
- s_mp_free(DIGITS(mp));
- DIGITS(mp) = tmp;
- ALLOC(mp) = min;
- }
- return MP_OKAY;
- } /* end s_mp_grow() */
- /* }}} */
- /* {{{ s_mp_pad(mp, min) */
- /* Make sure the used size of mp is at least 'min', growing if needed */
- mp_err s_mp_pad(mp_int *mp, mp_size min)
- {
- if(min > USED(mp)) {
- mp_err res;
- /* Make sure there is room to increase precision */
- if(min > ALLOC(mp) && (res = s_mp_grow(mp, min)) != MP_OKAY)
- return res;
- /* Increase precision; should already be 0-filled */
- USED(mp) = min;
- }
- return MP_OKAY;
- } /* end s_mp_pad() */
- /* }}} */
- /* {{{ s_mp_setz(dp, count) */
- #if MP_MACRO == 0
- /* Set 'count' digits pointed to by dp to be zeroes */
- void s_mp_setz(mp_digit *dp, mp_size count)
- {
- #if MP_MEMSET == 0
- int ix;
- for(ix = 0; ix < count; ix++)
- dp[ix] = 0;
- #else
- memset(dp, 0, count * sizeof(mp_digit));
- #endif
- } /* end s_mp_setz() */
- #endif
- /* }}} */
- /* {{{ s_mp_copy(sp, dp, count) */
- #if MP_MACRO == 0
- /* Copy 'count' digits from sp to dp */
- void s_mp_copy(mp_digit *sp, mp_digit *dp, mp_size count)
- {
- #if MP_MEMCPY == 0
- int ix;
- for(ix = 0; ix < count; ix++)
- dp[ix] = sp[ix];
- #else
- memcpy(dp, sp, count * sizeof(mp_digit));
- #endif
- } /* end s_mp_copy() */
- #endif
- /* }}} */
- /* {{{ s_mp_alloc(nb, ni) */
- #if MP_MACRO == 0
- /* Allocate ni records of nb bytes each, and return a pointer to that */
- void *s_mp_alloc(size_t nb, size_t ni)
- {
- return calloc(nb, ni);
- } /* end s_mp_alloc() */
- #endif
- /* }}} */
- /* {{{ s_mp_free(ptr) */
- #if MP_MACRO == 0
- /* Free the memory pointed to by ptr */
- void s_mp_free(void *ptr)
- {
- if(ptr)
- free(ptr);
- } /* end s_mp_free() */
- #endif
- /* }}} */
- /* {{{ s_mp_clamp(mp) */
- /* Remove leading zeroes from the given value */
- void s_mp_clamp(mp_int *mp)
- {
- mp_size du = USED(mp);
- mp_digit *zp = DIGITS(mp) + du - 1;
- while(du > 1 && !*zp--)
- --du;
- USED(mp) = du;
- } /* end s_mp_clamp() */
- /* }}} */
- /* {{{ s_mp_exch(a, b) */
- /* Exchange the data for a and b; (b, a) = (a, b) */
- void s_mp_exch(mp_int *a, mp_int *b)
- {
- mp_int tmp;
- tmp = *a;
- *a = *b;
- *b = tmp;
- } /* end s_mp_exch() */
- /* }}} */
- /* }}} */
- /* {{{ Arithmetic helpers */
- /* {{{ s_mp_lshd(mp, p) */
- /*
- Shift mp leftward by p digits, growing if needed, and zero-filling
- the in-shifted digits at the right end. This is a convenient
- alternative to multiplication by powers of the radix
- */
- mp_err s_mp_lshd(mp_int *mp, mp_size p)
- {
- mp_err res;
- mp_size pos;
- mp_digit *dp;
- int ix;
- if(p == 0)
- return MP_OKAY;
- if((res = s_mp_pad(mp, USED(mp) + p)) != MP_OKAY)
- return res;
- pos = USED(mp) - 1;
- dp = DIGITS(mp);
- /* Shift all the significant figures over as needed */
- for(ix = pos - p; ix >= 0; ix--)
- dp[ix + p] = dp[ix];
- /* Fill the bottom digits with zeroes */
- for(ix = 0; ix < p; ix++)
- dp[ix] = 0;
- return MP_OKAY;
- } /* end s_mp_lshd() */
- /* }}} */
- /* {{{ s_mp_rshd(mp, p) */
- /*
- Shift mp rightward by p digits. Maintains the invariant that
- digits above the precision are all zero. Digits shifted off the
- end are lost. Cannot fail.
- */
- void s_mp_rshd(mp_int *mp, mp_size p)
- {
- mp_size ix;
- mp_digit *dp;
- if(p == 0)
- return;
- /* Shortcut when all digits are to be shifted off */
- if(p >= USED(mp)) {
- s_mp_setz(DIGITS(mp), ALLOC(mp));
- USED(mp) = 1;
- SIGN(mp) = MP_ZPOS;
- return;
- }
- /* Shift all the significant figures over as needed */
- dp = DIGITS(mp);
- for(ix = p; ix < USED(mp); ix++)
- dp[ix - p] = dp[ix];
- /* Fill the top digits with zeroes */
- ix -= p;
- while(ix < USED(mp))
- dp[ix++] = 0;
- /* Strip off any leading zeroes */
- s_mp_clamp(mp);
- } /* end s_mp_rshd() */
- /* }}} */
- /* {{{ s_mp_div_2(mp) */
- /* Divide by two -- take advantage of radix properties to do it fast */
- void s_mp_div_2(mp_int *mp)
- {
- s_mp_div_2d(mp, 1);
- } /* end s_mp_div_2() */
- /* }}} */
- /* {{{ s_mp_mul_2(mp) */
- mp_err s_mp_mul_2(mp_int *mp)
- {
- int ix;
- mp_digit kin = 0, kout, *dp = DIGITS(mp);
- mp_err res;
- /* Shift digits leftward by 1 bit */
- for(ix = 0; ix < USED(mp); ix++) {
- kout = (dp[ix] >> (DIGIT_BIT - 1)) & 1;
- dp[ix] = (dp[ix] << 1) | kin;
- kin = kout;
- }
- /* Deal with rollover from last digit */
- if(kin) {
- if(ix >= ALLOC(mp)) {
- if((res = s_mp_grow(mp, ALLOC(mp) + 1)) != MP_OKAY)
- return res;
- dp = DIGITS(mp);
- }
- dp[ix] = kin;
- USED(mp) += 1;
- }
- return MP_OKAY;
- } /* end s_mp_mul_2() */
- /* }}} */
- /* {{{ s_mp_mod_2d(mp, d) */
- /*
- Remainder the integer by 2^d, where d is a number of bits. This
- amounts to a bitwise AND of the value, and does not require the full
- division code
- */
- void s_mp_mod_2d(mp_int *mp, mp_digit d)
- {
- unsigned int ndig = (d / DIGIT_BIT), nbit = (d % DIGIT_BIT);
- unsigned int ix;
- mp_digit dmask, *dp = DIGITS(mp);
- if(ndig >= USED(mp))
- return;
- /* Flush all the bits above 2^d in its digit */
- dmask = (1 << nbit) - 1;
- dp[ndig] &= dmask;
- /* Flush all digits above the one with 2^d in it */
- for(ix = ndig + 1; ix < USED(mp); ix++)
- dp[ix] = 0;
- s_mp_clamp(mp);
- } /* end s_mp_mod_2d() */
- /* }}} */
- /* {{{ s_mp_mul_2d(mp, d) */
- /*
- Multiply by the integer 2^d, where d is a number of bits. This
- amounts to a bitwise shift of the value, and does not require the
- full multiplication code.
- */
- mp_err s_mp_mul_2d(mp_int *mp, mp_digit d)
- {
- mp_err res;
- mp_digit save, next, mask, *dp;
- mp_size used;
- int ix;
- if((res = s_mp_lshd(mp, d / DIGIT_BIT)) != MP_OKAY)
- return res;
- dp = DIGITS(mp); used = USED(mp);
- d %= DIGIT_BIT;
- mask = (1 << d) - 1;
- /* If the shift requires another digit, make sure we've got one to
- work with */
- if((dp[used - 1] >> (DIGIT_BIT - d)) & mask) {
- if((res = s_mp_grow(mp, used + 1)) != MP_OKAY)
- return res;
- dp = DIGITS(mp);
- }
- /* Do the shifting... */
- save = 0;
- for(ix = 0; ix < used; ix++) {
- next = (dp[ix] >> (DIGIT_BIT - d)) & mask;
- dp[ix] = (dp[ix] << d) | save;
- save = next;
- }
- /* If, at this point, we have a nonzero carryout into the next
- digit, we'll increase the size by one digit, and store it...
- */
- if(save) {
- dp[used] = save;
- USED(mp) += 1;
- }
- s_mp_clamp(mp);
- return MP_OKAY;
- } /* end s_mp_mul_2d() */
- /* }}} */
- /* {{{ s_mp_div_2d(mp, d) */
- /*
- Divide the integer by 2^d, where d is a number of bits. This
- amounts to a bitwise shift of the value, and does not require the
- full division code (used in Barrett reduction, see below)
- */
- void s_mp_div_2d(mp_int *mp, mp_digit d)
- {
- int ix;
- mp_digit save, next, mask, *dp = DIGITS(mp);
- s_mp_rshd(mp, d / DIGIT_BIT);
- d %= DIGIT_BIT;
- mask = (1 << d) - 1;
- save = 0;
- for(ix = USED(mp) - 1; ix >= 0; ix--) {
- next = dp[ix] & mask;
- dp[ix] = (dp[ix] >> d) | (save << (DIGIT_BIT - d));
- save = next;
- }
- s_mp_clamp(mp);
- } /* end s_mp_div_2d() */
- /* }}} */
- /* {{{ s_mp_norm(a, b) */
- /*
- s_mp_norm(a, b)
- Normalize a and b for division, where b is the divisor. In order
- that we might make good guesses for quotient digits, we want the
- leading digit of b to be at least half the radix, which we
- accomplish by multiplying a and b by a constant. This constant is
- returned (so that it can be divided back out of the remainder at the
- end of the division process).
- We multiply by the smallest power of 2 that gives us a leading digit
- at least half the radix. By choosing a power of 2, we simplify the
- multiplication and division steps to simple shifts.
- */
- mp_digit s_mp_norm(mp_int *a, mp_int *b)
- {
- mp_digit t, d = 0;
- t = DIGIT(b, USED(b) - 1);
- while(t < (RADIX / 2)) {
- t <<= 1;
- ++d;
- }
-
- if(d != 0) {
- s_mp_mul_2d(a, d);
- s_mp_mul_2d(b, d);
- }
- return d;
- } /* end s_mp_norm() */
- /* }}} */
- /* }}} */
- /* {{{ Primitive digit arithmetic */
- /* {{{ s_mp_add_d(mp, d) */
- /* Add d to |mp| in place */
- mp_err s_mp_add_d(mp_int *mp, mp_digit d) /* unsigned digit addition */
- {
- mp_word w, k = 0;
- mp_size ix = 1, used = USED(mp);
- mp_digit *dp = DIGITS(mp);
- w = dp[0] + d;
- dp[0] = ACCUM(w);
- k = CARRYOUT(w);
- while(ix < used && k) {
- w = dp[ix] + k;
- dp[ix] = ACCUM(w);
- k = CARRYOUT(w);
- ++ix;
- }
- if(k != 0) {
- mp_err res;
- if((res = s_mp_pad(mp, USED(mp) + 1)) != MP_OKAY)
- return res;
- DIGIT(mp, ix) = k;
- }
- return MP_OKAY;
- } /* end s_mp_add_d() */
- /* }}} */
- /* {{{ s_mp_sub_d(mp, d) */
- /* Subtract d from |mp| in place, assumes |mp| > d */
- mp_err s_mp_sub_d(mp_int *mp, mp_digit d) /* unsigned digit subtract */
- {
- mp_word w, b = 0;
- mp_size ix = 1, used = USED(mp);
- mp_digit *dp = DIGITS(mp);
- /* Compute initial subtraction */
- w = (RADIX + dp[0]) - d;
- b = CARRYOUT(w) ? 0 : 1;
- dp[0] = ACCUM(w);
- /* Propagate borrows leftward */
- while(b && ix < used) {
- w = (RADIX + dp[ix]) - b;
- b = CARRYOUT(w) ? 0 : 1;
- dp[ix] = ACCUM(w);
- ++ix;
- }
- /* Remove leading zeroes */
- s_mp_clamp(mp);
- /* If we have a borrow out, it's a violation of the input invariant */
- if(b)
- return MP_RANGE;
- else
- return MP_OKAY;
- } /* end s_mp_sub_d() */
- /* }}} */
- /* {{{ s_mp_mul_d(a, d) */
- /* Compute a = a * d, single digit multiplication */
- mp_err s_mp_mul_d(mp_int *a, mp_digit d)
- {
- mp_word w, k = 0;
- mp_size ix, max;
- mp_err res;
- mp_digit *dp = DIGITS(a);
- /*
- Single-digit multiplication will increase the precision of the
- output by at most one digit. However, we can detect when this
- will happen -- if the high-order digit of a, times d, gives a
- two-digit result, then the precision of the result will increase;
- otherwise it won't. We use this fact to avoid calling s_mp_pad()
- unless absolutely necessary.
- */
- max = USED(a);
- w = dp[max - 1] * d;
- if(CARRYOUT(w) != 0) {
- if((res = s_mp_pad(a, max + 1)) != MP_OKAY)
- return res;
- dp = DIGITS(a);
- }
- for(ix = 0; ix < max; ix++) {
- w = (dp[ix] * d) + k;
- dp[ix] = ACCUM(w);
- k = CARRYOUT(w);
- }
- /* If there is a precision increase, take care of it here; the above
- test guarantees we have enough storage to do this safely.
- */
- if(k) {
- dp[max] = k;
- USED(a) = max + 1;
- }
- s_mp_clamp(a);
- return MP_OKAY;
-
- } /* end s_mp_mul_d() */
- /* }}} */
- /* {{{ s_mp_div_d(mp, d, r) */
- /*
- s_mp_div_d(mp, d, r)
- Compute the quotient mp = mp / d and remainder r = mp mod d, for a
- single digit d. If r is null, the remainder will be discarded.
- */
- mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r)
- {
- mp_word w = 0, t;
- mp_int quot;
- mp_err res;
- mp_digit *dp = DIGITS(mp), *qp;
- int ix;
- if(d == 0)
- return MP_RANGE;
- /* Make room for the quotient */
- if((res = mp_init_size(", USED(mp))) != MP_OKAY)
- return res;
- USED(") = USED(mp); /* so clamping will work below */
- qp = DIGITS(");
- /* Divide without subtraction */
- for(ix = USED(mp) - 1; ix >= 0; ix--) {
- w = (w << DIGIT_BIT) | dp[ix];
- if(w >= d) {
- t = w / d;
- w = w % d;
- } else {
- t = 0;
- }
- qp[ix] = t;
- }
- /* Deliver the remainder, if desired */
- if(r)
- *r = w;
- s_mp_clamp(");
- mp_exch(", mp);
- mp_clear(");
- return MP_OKAY;
- } /* end s_mp_div_d() */
- /* }}} */
- /* }}} */
- /* {{{ Primitive full arithmetic */
- /* {{{ s_mp_add(a, b) */
- /* Compute a = |a| + |b| */
- mp_err s_mp_add(mp_int *a, mp_int *b) /* magnitude addition */
- {
- mp_word w = 0;
- mp_digit *pa, *pb;
- mp_size ix, used = USED(b);
- mp_err res;
- /* Make sure a has enough precision for the output value */
- if((used > USED(a)) && (res = s_mp_pad(a, used)) != MP_OKAY)
- return res;
- /*
- Add up all digits up to the precision of b. If b had initially
- the same precision as a, or greater, we took care of it by the
- padding step above, so there is no problem. If b had initially
- less precision, we'll have to make sure the carry out is duly
- propagated upward among the higher-order digits of the sum.
- */
- pa = DIGITS(a);
- pb = DIGITS(b);
- for(ix = 0; ix < used; ++ix) {
- w += *pa + *pb++;
- *pa++ = ACCUM(w);
- w = CARRYOUT(w);
- }
- /* If we run out of 'b' digits before we're actually done, make
- sure the carries get propagated upward...
- */
- used = USED(a);
- while(w && ix < used) {
- w += *pa;
- *pa++ = ACCUM(w);
- w = CARRYOUT(w);
- ++ix;
- }
- /* If there's an overall carry out, increase precision and include
- it. We could have done this initially, but why touch the memory
- allocator unless we're sure we have to?
- */
- if(w) {
- if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
- return res;
- DIGIT(a, ix) = w; /* pa may not be valid after s_mp_pad() call */
- }
- return MP_OKAY;
- } /* end s_mp_add() */
- /* }}} */
- /* {{{ s_mp_sub(a, b) */
- /* Compute a = |a| - |b|, assumes |a| >= |b| */
- mp_err s_mp_sub(mp_int *a, mp_int *b) /* magnitude subtract */
- {
- mp_word w = 0;
- mp_digit *pa, *pb;
- mp_size ix, used = USED(b);
- /*
- Subtract and propagate borrow. Up to the precision of b, this
- accounts for the digits of b; after that, we just make sure the
- carries get to the right place. This saves having to pad b out to
- the precision of a just to make the loops work right...
- */
- pa = DIGITS(a);
- pb = DIGITS(b);
- for(ix = 0; ix < used; ++ix) {
- w = (RADIX + *pa) - w - *pb++;
- *pa++ = ACCUM(w);
- w = CARRYOUT(w) ? 0 : 1;
- }
- used = USED(a);
- while(ix < used) {
- w = RADIX + *pa - w;
- *pa++ = ACCUM(w);
- w = CARRYOUT(w) ? 0 : 1;
- ++ix;
- }
- /* Clobber any leading zeroes we created */
- s_mp_clamp(a);
- /*
- If there was a borrow out, then |b| > |a| in violation
- of our input invariant. We've already done the work,
- but we'll at least complain about it...
- */
- if(w)
- return MP_RANGE;
- else
- return MP_OKAY;
- } /* end s_mp_sub() */
- /* }}} */
- mp_err s_mp_reduce(mp_int *x, mp_int *m, mp_int *mu)
- {
- mp_int q;
- mp_err res;
- mp_size um = USED(m);
- if((res = mp_init_copy(&q, x)) != MP_OKAY)
- return res;
- s_mp_rshd(&q, um - 1); /* q1 = x / b^(k-1) */
- s_mp_mul(&q, mu); /* q2 = q1 * mu */
- s_mp_rshd(&q, um + 1); /* q3 = q2 / b^(k+1) */
- /* x = x mod b^(k+1), quick (no division) */
- s_mp_mod_2d(x, (mp_digit)(DIGIT_BIT * (um + 1)));
- /* q = q * m mod b^(k+1), quick (no division), uses the short multiplier */
- #ifndef SHRT_MUL
- s_mp_mul(&q, m);
- s_mp_mod_2d(&q, (mp_digit)(DIGIT_BIT * (um + 1)));
- #else
- s_mp_mul_dig(&q, m, um + 1);
- #endif
- /* x = x - q */
- if((res = mp_sub(x, &q, x)) != MP_OKAY)
- goto CLEANUP;
- /* If x < 0, add b^(k+1) to it */
- if(mp_cmp_z(x) < 0) {
- mp_set(&q, 1);
- if((res = s_mp_lshd(&q, um + 1)) != MP_OKAY)
- goto CLEANUP;
- if((res = mp_add(x, &q, x)) != MP_OKAY)
- goto CLEANUP;
- }
- /* Back off if it's too big */
- while(mp_cmp(x, m) >= 0) {
- if((res = s_mp_sub(x, m)) != MP_OKAY)
- break;
- }
- CLEANUP:
- mp_clear(&q);
- return res;
- } /* end s_mp_reduce() */
- /* {{{ s_mp_mul(a, b) */
- /* Compute a = |a| * |b| */
- mp_err s_mp_mul(mp_int *a, mp_int *b)
- {
- mp_word w, k = 0;
- mp_int tmp;
- mp_err res;
- mp_size ix, jx, ua = USED(a), ub = USED(b);
- mp_digit *pa, *pb, *pt, *pbt;
- if((res = mp_init_size(&tmp, ua + ub)) != MP_OKAY)
- return res;
- /* This has the effect of left-padding with zeroes... */
- USED(&tmp) = ua + ub;
- /* We're going to need the base value each iteration */
- pbt = DIGITS(&tmp);
- /* Outer loop: Digits of b */
- pb = DIGITS(b);
- for(ix = 0; ix < ub; ++ix, ++pb) {
- if(*pb == 0)
- continue;
- /* Inner product: Digits of a */
- pa = DIGITS(a);
- for(jx = 0; jx < ua; ++jx, ++pa) {
- pt = pbt + ix + jx;
- w = *pb * *pa + k + *pt;
- *pt = ACCUM(w);
- k = CARRYOUT(w);
- }
- pbt[ix + jx] = k;
- k = 0;
- }
- s_mp_clamp(&tmp);
- s_mp_exch(&tmp, a);
- mp_clear(&tmp);
- return MP_OKAY;
- } /* end s_mp_mul() */
- /* }}} */
- /* {{{ s_mp_kmul(a, b, out, len) */
- #if 0
- void s_mp_kmul(mp_digit *a, mp_digit *b, mp_digit *out, mp_size len)
- {
- mp_word w, k = 0;
- mp_size ix, jx;
- mp_digit *pa, *pt;
- for(ix = 0; ix < len; ++ix, ++b) {
- if(*b == 0)
- continue;
-
- pa = a;
- for(jx = 0; jx < len; ++jx, ++pa) {
- pt = out + ix + jx;
- w = *b * *pa + k + *pt;
- *pt = ACCUM(w);
- k = CARRYOUT(w);
- }
- out[ix + jx] = k;
- k = 0;
- }
- } /* end s_mp_kmul() */
- #endif
- /* }}} */
- /* {{{ s_mp_sqr(a) */
- /*
- Computes the square of a, in place. This can be done more
- efficiently than a general multiplication, because many of the
- computation steps are redundant when squaring. The inner product
- step is a bit more complicated, but we save a fair number of
- iterations of the multiplication loop.
- */
- #if MP_SQUARE
- mp_err s_mp_sqr(mp_int *a)
- {
- mp_word w, k = 0;
- mp_int tmp;
- mp_err res;
- mp_size ix, jx, kx, used = USED(a);
- mp_digit *pa1, *pa2, *pt, *pbt;
- if((res = mp_init_size(&tmp, 2 * used)) != MP_OKAY)
- return res;
- /* Left-pad with zeroes */
- USED(&tmp) = 2 * used;
- /* We need the base value each time through the loop */
- pbt = DIGITS(&tmp);
- pa1 = DIGITS(a);
- for(ix = 0; ix < used; ++ix, ++pa1) {
- if(*pa1 == 0)
- continue;
- w = DIGIT(&tmp, ix + ix) + (*pa1 * *pa1);
- pbt[ix + ix] = ACCUM(w);
- k = CARRYOUT(w);
- /*
- The inner product is computed as:
- (C, S) = t[i,j] + 2 a[i] a[j] + C
- This can overflow what can be represented in an mp_word, and
- since C arithmetic does not provide any way to check for
- overflow, we have to check explicitly for overflow conditions
- before they happen.
- */
- for(jx = ix + 1, pa2 = DIGITS(a) + jx; jx < used; ++jx, ++pa2) {
- mp_word u = 0, v;
-
- /* Store this in a temporary to avoid indirections later */
- pt = pbt + ix + jx;
- /* Compute the multiplicative step */
- w = *pa1 * *pa2;
- /* If w is more than half MP_WORD_MAX, the doubling will
- overflow, and we need to record a carry out into the next
- word */
- u = (w >> (MP_WORD_BIT - 1)) & 1;
- /* Double what we've got, overflow will be ignored as defined
- for C arithmetic (we've already noted if it is to occur)
- */
- w *= 2;
- /* Compute the additive step */
- v = *pt + k;
- /* If we do not already have an overflow carry, check to see
- if the addition will cause one, and set the carry out if so
- */
- u |= ((MP_WORD_MAX - v) < w);
- /* Add in the rest, again ignoring overflow */
- w += v;
- /* Set the i,j digit of the output */
- *pt = ACCUM(w);
- /* Save carry information for the next iteration of the loop.
- This is why k must be an mp_word, instead of an mp_digit */
- k = CARRYOUT(w) | (u << DIGIT_BIT);
- } /* for(jx ...) */
- /* Set the last digit in the cycle and reset the carry */
- k = DIGIT(&tmp, ix + jx) + k;
- pbt[ix + jx] = ACCUM(k);
- k = CARRYOUT(k);
- /* If we are carrying out, propagate the carry to the next digit
- in the output. This may cascade, so we have to be somewhat
- circumspect -- but we will have enough precision in the output
- that we won't overflow
- */
- kx = 1;
- while(k) {
- k = pbt[ix + jx + kx] + 1;
- pbt[ix + jx + kx] = ACCUM(k);
- k = CARRYOUT(k);
- ++kx;
- }
- } /* for(ix ...) */
- s_mp_clamp(&tmp);
- s_mp_exch(&tmp, a);
- mp_clear(&tmp);
- return MP_OKAY;
- } /* end s_mp_sqr() */
- #endif
- /* }}} */
- /* {{{ s_mp_div(a, b) */
- /*
- s_mp_div(a, b)
- Compute a = a / b and b = a mod b. Assumes b > a.
- */
- mp_err s_mp_div(mp_int *a, mp_int *b)
- {
- mp_int quot, rem, t;
- mp_word q;
- mp_err res;
- mp_digit d;
- int ix;
- if(mp_cmp_z(b) == 0)
- return MP_RANGE;
- /* Shortcut if b is power of two */
- if((ix = s_mp_ispow2(b)) >= 0) {
- mp_copy(a, b); /* need this for remainder */
- s_mp_div_2d(a, (mp_digit)ix);
- s_mp_mod_2d(b, (mp_digit)ix);
- return MP_OKAY;
- }
- /* Allocate space to store the quotient */
- if((res = mp_init_size(", USED(a))) != MP_OKAY)
- return res;
- /* A working temporary for division */
- if((res = mp_init_size(&t, USED(a))) != MP_OKAY)
- goto T;
- /* Allocate space for the remainder */
- if((res = mp_init_size(&rem, USED(a))) != MP_OKAY)
- goto REM;
- /* Normalize to optimize guessing */
- d = s_mp_norm(a, b);
- /* Perform the division itself...woo! */
- ix = USED(a) - 1;
- while(ix >= 0) {
- /* Find a partial substring of a which is at least b */
- while(s_mp_cmp(&rem, b) < 0 && ix >= 0) {
- if((res = s_mp_lshd(&rem, 1)) != MP_OKAY)
- goto CLEANUP;
- if((res = s_mp_lshd(", 1)) != MP_OKAY)
- goto CLEANUP;
- DIGIT(&rem, 0) = DIGIT(a, ix);
- s_mp_clamp(&rem);
- --ix;
- }
- /* If we didn't find one, we're finished dividing */
- if(s_mp_cmp(&rem, b) < 0)
- break;
- /* Compute a guess for the next quotient digit */
- q = DIGIT(&rem, USED(&rem) - 1);
- if(q <= DIGIT(b, USED(b) - 1) && USED(&rem) > 1)
- q = (q << DIGIT_BIT) | DIGIT(&rem, USED(&rem) - 2);
- q /= DIGIT(b, USED(b) - 1);
- /* The guess can be as much as RADIX + 1 */
- if(q >= RADIX)
- q = RADIX - 1;
- /* See what that multiplies out to */
- mp_copy(b, &t);
- if((res = s_mp_mul_d(&t, q)) != MP_OKAY)
- goto CLEANUP;
- /*
- If it's too big, back it off. We should not have to do this
- more than once, or, in rare cases, twice. Knuth describes a
- method by which this could be reduced to a maximum of once, but
- I didn't implement that here.
- */
- while(s_mp_cmp(&t, &rem) > 0) {
- --q;
- s_mp_sub(&t, b);
- }
- /* At this point, q should be the right next digit */
- if((res = s_mp_sub(&rem, &t)) != MP_OKAY)
- goto CLEANUP;
- /*
- Include the digit in the quotient. We allocated enough memory
- for any quotient we could ever possibly get, so we should not
- have to check for failures here
- */
- DIGIT(", 0) = q;
- }
- /* Denormalize remainder */
- if(d != 0)
- s_mp_div_2d(&rem, d);
- s_mp_clamp(");
- s_mp_clamp(&rem);
- /* Copy quotient back to output */
- s_mp_exch(", a);
-
- /* Copy remainder back to output */
- s_mp_exch(&rem, b);
- CLEANUP:
- mp_clear(&rem);
- REM:
- mp_clear(&t);
- T:
- mp_clear(");
- return res;
- } /* end s_mp_div() */
- /* }}} */
- /* {{{ s_mp_2expt(a, k) */
- mp_err s_mp_2expt(mp_int *a, mp_digit k)
- {
- mp_err res;
- mp_size dig, bit;
- dig = k / DIGIT_BIT;
- bit = k % DIGIT_BIT;
- mp_zero(a);
- if((res = s_mp_pad(a, dig + 1)) != MP_OKAY)
- return res;
-
- DIGIT(a, dig) |= (1 << bit);
- return MP_OKAY;
- } /* end s_mp_2expt() */
- /* }}} */
- /* }}} */
- /* }}} */
- /* {{{ Primitive comparisons */
- /* {{{ s_mp_cmp(a, b) */
- /* Compare |a| <=> |b|, return 0 if equal, <0 if a<b, >0 if a>b */
- int s_mp_cmp(mp_int *a, mp_int *b)
- {
- mp_size ua = USED(a), ub = USED(b);
- if(ua > ub)
- return MP_GT;
- else if(ua < ub)
- return MP_LT;
- else {
- int ix = ua - 1;
- mp_digit *ap = DIGITS(a) + ix, *bp = DIGITS(b) + ix;
- while(ix >= 0) {
- if(*ap > *bp)
- return MP_GT;
- else if(*ap < *bp)
- return MP_LT;
- --ap; --bp; --ix;
- }
- return MP_EQ;
- }
- } /* end s_mp_cmp() */
- /* }}} */
- /* {{{ s_mp_cmp_d(a, d) */
- /* Compare |a| <=> d, return 0 if equal, <0 if a<d, >0 if a>d */
- int s_mp_cmp_d(mp_int *a, mp_digit d)
- {
- mp_size ua = USED(a);
- mp_digit *ap = DIGITS(a);
- if(ua > 1)
- return MP_GT;
- if(*ap < d)
- return MP_LT;
- else if(*ap > d)
- return MP_GT;
- else
- return MP_EQ;
- } /* end s_mp_cmp_d() */
- /* }}} */
- /* {{{ s_mp_ispow2(v) */
- /*
- Returns -1 if the value is not a power of two; otherwise, it returns
- k such that v = 2^k, i.e. lg(v).
- */
- int s_mp_ispow2(mp_int *v)
- {
- mp_digit d, *dp;
- mp_size uv = USED(v);
- int extra = 0, ix;
- d = DIGIT(v, uv - 1); /* most significant digit of v */
- while(d && ((d & 1) == 0)) {
- d >>= 1;
- ++extra;
- }
- if(d == 1) {
- ix = uv - 2;
- dp = DIGITS(v) + ix;
- while(ix >= 0) {
- if(*dp)
- return -1; /* not a power of two */
- --dp; --ix;
- }
- return ((uv - 1) * DIGIT_BIT) + extra;
- }
- return -1;
- } /* end s_mp_ispow2() */
- /* }}} */
- /* {{{ s_mp_ispow2d(d) */
- int s_mp_ispow2d(mp_digit d)
- {
- int pow = 0;
- while((d & 1) == 0) {
- ++pow; d >>= 1;
- }
- if(d == 1)
- return pow;
- return -1;
- } /* end s_mp_ispow2d() */
- /* }}} */
- /* }}} */
- /* {{{ Primitive I/O helpers */
- /* {{{ s_mp_tovalue(ch, r) */
- /*
- Convert the given character to its digit value, in the given radix.
- If the given character is not understood in the given radix, -1 is
- returned. Otherwise the digit's numeric value is returned.
- The results will be odd if you use a radix < 2 or > 62, you are
- expected to know what you're up to.
- */
- int s_mp_tovalue(char ch, int r)
- {
- int val, xch;
-
- if(r > 36)
- xch = ch;
- else
- xch = toupper(ch);
- if(isdigit(xch))
- val = xch - '0';
- else if(isupper(xch))
- val = xch - 'A' + 10;
- else if(islower(xch))
- val = xch - 'a' + 36;
- else if(xch == '+')
- val = 62;
- else if(xch == '/')
- val = 63;
- else
- return -1;
- if(val < 0 || val >= r)
- return -1;
- return val;
- } /* end s_mp_tovalue() */
- /* }}} */
- /* {{{ s_mp_todigit(val, r, low) */
- /*
- Convert val to a radix-r digit, if possible. If val is out of range
- for r, returns zero. Otherwise, returns an ASCII character denoting
- the value in the given radix.
- The results may be odd if you use a radix < 2 or > 64, you are
- expected to know what you're doing.
- */
-
- char s_mp_todigit(int val, int r, int low)
- {
- char ch;
- if(val < 0 || val >= r)
- return 0;
- ch = s_dmap_1[val];
- if(r <= 36 && low)
- ch = tolower(ch);
- return ch;
- } /* end s_mp_todigit() */
- /* }}} */
- /* {{{ s_mp_outlen(bits, radix) */
- /*
- Return an estimate for how long a string is needed to hold a radix
- r representation of a number with 'bits' significant bits.
- Does not include space for a sign or a NUL terminator.
- */
- int s_mp_outlen(int bits, int r)
- {
- return (int)((double)bits * LOG_V_2(r));
- } /* end s_mp_outlen() */
- /* }}} */
- /* }}} */
- /*------------------------------------------------------------------------*/
- /* HERE THERE BE DRAGONS */
- /* crc==4242132123, version==2, Sat Feb 02 06:43:52 2002 */
- /* $Source: /cvs/libtom/libtommath/mtest/mpi.c,v $ */
- /* $Revision: 1.2 $ */
- /* $Date: 2005/05/05 14:38:47 $ */
|