bdl.js 516 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. web3.Utils = require("./index.js");
  3. },{"./index.js":2}],2:[function(require,module,exports){
  4. (function (Buffer){
  5. const createKeccakHash = require('keccak')
  6. const secp256k1 = require('secp256k1')
  7. const assert = require('assert')
  8. const rlp = require('rlp')
  9. const BN = require('bn.js')
  10. const createHash = require('create-hash')
  11. Object.assign(exports, require('ethjs-util'))
  12. /**
  13. * the max integer that this VM can handle (a ```BN```)
  14. * @var {BN} MAX_INTEGER
  15. */
  16. exports.MAX_INTEGER = new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)
  17. /**
  18. * 2^256 (a ```BN```)
  19. * @var {BN} TWO_POW256
  20. */
  21. exports.TWO_POW256 = new BN('10000000000000000000000000000000000000000000000000000000000000000', 16)
  22. /**
  23. * SHA3-256 hash of null (a ```String```)
  24. * @var {String} SHA3_NULL_S
  25. */
  26. exports.SHA3_NULL_S = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
  27. /**
  28. * SHA3-256 hash of null (a ```Buffer```)
  29. * @var {Buffer} SHA3_NULL
  30. */
  31. exports.SHA3_NULL = Buffer.from(exports.SHA3_NULL_S, 'hex')
  32. /**
  33. * SHA3-256 of an RLP of an empty array (a ```String```)
  34. * @var {String} SHA3_RLP_ARRAY_S
  35. */
  36. exports.SHA3_RLP_ARRAY_S = '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'
  37. /**
  38. * SHA3-256 of an RLP of an empty array (a ```Buffer```)
  39. * @var {Buffer} SHA3_RLP_ARRAY
  40. */
  41. exports.SHA3_RLP_ARRAY = Buffer.from(exports.SHA3_RLP_ARRAY_S, 'hex')
  42. /**
  43. * SHA3-256 hash of the RLP of null (a ```String```)
  44. * @var {String} SHA3_RLP_S
  45. */
  46. exports.SHA3_RLP_S = '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'
  47. /**
  48. * SHA3-256 hash of the RLP of null (a ```Buffer```)
  49. * @var {Buffer} SHA3_RLP
  50. */
  51. exports.SHA3_RLP = Buffer.from(exports.SHA3_RLP_S, 'hex')
  52. /**
  53. * [`BN`](https://github.com/indutny/bn.js)
  54. * @var {Function}
  55. */
  56. exports.BN = BN
  57. /**
  58. * [`rlp`](https://github.com/ethereumjs/rlp)
  59. * @var {Function}
  60. */
  61. exports.rlp = rlp
  62. /**
  63. * [`secp256k1`](https://github.com/cryptocoinjs/secp256k1-node/)
  64. * @var {Object}
  65. */
  66. exports.secp256k1 = secp256k1
  67. /**
  68. * Returns a buffer filled with 0s
  69. * @method zeros
  70. * @param {Number} bytes the number of bytes the buffer should be
  71. * @return {Buffer}
  72. */
  73. exports.zeros = function (bytes) {
  74. return Buffer.allocUnsafe(bytes).fill(0)
  75. }
  76. /**
  77. * Left Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
  78. * Or it truncates the beginning if it exceeds.
  79. * @method lsetLength
  80. * @param {Buffer|Array} msg the value to pad
  81. * @param {Number} length the number of bytes the output should be
  82. * @param {Boolean} [right=false] whether to start padding form the left or right
  83. * @return {Buffer|Array}
  84. */
  85. exports.setLengthLeft = exports.setLength = function (msg, length, right) {
  86. var buf = exports.zeros(length)
  87. msg = exports.toBuffer(msg)
  88. if (right) {
  89. if (msg.length < length) {
  90. msg.copy(buf)
  91. return buf
  92. }
  93. return msg.slice(0, length)
  94. } else {
  95. if (msg.length < length) {
  96. msg.copy(buf, length - msg.length)
  97. return buf
  98. }
  99. return msg.slice(-length)
  100. }
  101. }
  102. /**
  103. * Right Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
  104. * Or it truncates the beginning if it exceeds.
  105. * @param {Buffer|Array} msg the value to pad
  106. * @param {Number} length the number of bytes the output should be
  107. * @return {Buffer|Array}
  108. */
  109. exports.setLengthRight = function (msg, length) {
  110. return exports.setLength(msg, length, true)
  111. }
  112. /**
  113. * Trims leading zeros from a `Buffer` or an `Array`
  114. * @param {Buffer|Array|String} a
  115. * @return {Buffer|Array|String}
  116. */
  117. exports.unpad = exports.stripZeros = function (a) {
  118. a = exports.stripHexPrefix(a)
  119. var first = a[0]
  120. while (a.length > 0 && first.toString() === '0') {
  121. a = a.slice(1)
  122. first = a[0]
  123. }
  124. return a
  125. }
  126. /**
  127. * Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method.
  128. * @param {*} v the value
  129. */
  130. exports.toBuffer = function (v) {
  131. if (!Buffer.isBuffer(v)) {
  132. if (Array.isArray(v)) {
  133. v = Buffer.from(v)
  134. } else if (typeof v === 'string') {
  135. if (exports.isHexPrefixed(v)) {
  136. v = Buffer.from(exports.padToEven(exports.stripHexPrefix(v)), 'hex')
  137. } else {
  138. v = Buffer.from(v)
  139. }
  140. } else if (typeof v === 'number') {
  141. v = exports.intToBuffer(v)
  142. } else if (v === null || v === undefined) {
  143. v = Buffer.allocUnsafe(0)
  144. } else if (v.toArray) {
  145. // converts a BN to a Buffer
  146. v = Buffer.from(v.toArray())
  147. } else {
  148. throw new Error('invalid type')
  149. }
  150. }
  151. return v
  152. }
  153. /**
  154. * Converts a `Buffer` to a `Number`
  155. * @param {Buffer} buf
  156. * @return {Number}
  157. * @throws If the input number exceeds 53 bits.
  158. */
  159. exports.bufferToInt = function (buf) {
  160. return new BN(exports.toBuffer(buf)).toNumber()
  161. }
  162. /**
  163. * Converts a `Buffer` into a hex `String`
  164. * @param {Buffer} buf
  165. * @return {String}
  166. */
  167. exports.bufferToHex = function (buf) {
  168. buf = exports.toBuffer(buf)
  169. return '0x' + buf.toString('hex')
  170. }
  171. /**
  172. * Interprets a `Buffer` as a signed integer and returns a `BN`. Assumes 256-bit numbers.
  173. * @param {Buffer} num
  174. * @return {BN}
  175. */
  176. exports.fromSigned = function (num) {
  177. return new BN(num).fromTwos(256)
  178. }
  179. /**
  180. * Converts a `BN` to an unsigned integer and returns it as a `Buffer`. Assumes 256-bit numbers.
  181. * @param {BN} num
  182. * @return {Buffer}
  183. */
  184. exports.toUnsigned = function (num) {
  185. return Buffer.from(num.toTwos(256).toArray())
  186. }
  187. /**
  188. * Creates SHA-3 hash of the input
  189. * @param {Buffer|Array|String|Number} a the input data
  190. * @param {Number} [bits=256] the SHA width
  191. * @return {Buffer}
  192. */
  193. exports.sha3 = function (a, bits) {
  194. a = exports.toBuffer(a)
  195. if (!bits) bits = 256
  196. return createKeccakHash('keccak' + bits).update(a).digest()
  197. }
  198. /**
  199. * Creates SHA256 hash of the input
  200. * @param {Buffer|Array|String|Number} a the input data
  201. * @return {Buffer}
  202. */
  203. exports.sha256 = function (a) {
  204. a = exports.toBuffer(a)
  205. return createHash('sha256').update(a).digest()
  206. }
  207. /**
  208. * Creates RIPEMD160 hash of the input
  209. * @param {Buffer|Array|String|Number} a the input data
  210. * @param {Boolean} padded whether it should be padded to 256 bits or not
  211. * @return {Buffer}
  212. */
  213. exports.ripemd160 = function (a, padded) {
  214. a = exports.toBuffer(a)
  215. var hash = createHash('rmd160').update(a).digest()
  216. if (padded === true) {
  217. return exports.setLength(hash, 32)
  218. } else {
  219. return hash
  220. }
  221. }
  222. /**
  223. * Creates SHA-3 hash of the RLP encoded version of the input
  224. * @param {Buffer|Array|String|Number} a the input data
  225. * @return {Buffer}
  226. */
  227. exports.rlphash = function (a) {
  228. return exports.sha3(rlp.encode(a))
  229. }
  230. /**
  231. * Checks if the private key satisfies the rules of the curve secp256k1.
  232. * @param {Buffer} privateKey
  233. * @return {Boolean}
  234. */
  235. exports.isValidPrivate = function (privateKey) {
  236. return secp256k1.privateKeyVerify(privateKey)
  237. }
  238. /**
  239. * Checks if the public key satisfies the rules of the curve secp256k1
  240. * and the requirements of Ethereum.
  241. * @param {Buffer} publicKey The two points of an uncompressed key, unless sanitize is enabled
  242. * @param {Boolean} [sanitize=false] Accept public keys in other formats
  243. * @return {Boolean}
  244. */
  245. exports.isValidPublic = function (publicKey, sanitize) {
  246. if (publicKey.length === 64) {
  247. // Convert to SEC1 for secp256k1
  248. return secp256k1.publicKeyVerify(Buffer.concat([ Buffer.from([4]), publicKey ]))
  249. }
  250. if (!sanitize) {
  251. return false
  252. }
  253. return secp256k1.publicKeyVerify(publicKey)
  254. }
  255. /**
  256. * Returns the ethereum address of a given public key.
  257. * Accepts "Ethereum public keys" and SEC1 encoded keys.
  258. * @param {Buffer} pubKey The two points of an uncompressed key, unless sanitize is enabled
  259. * @param {Boolean} [sanitize=false] Accept public keys in other formats
  260. * @return {Buffer}
  261. */
  262. exports.pubToAddress = exports.publicToAddress = function (pubKey, sanitize) {
  263. pubKey = exports.toBuffer(pubKey)
  264. if (sanitize && (pubKey.length !== 64)) {
  265. pubKey = secp256k1.publicKeyConvert(pubKey, false).slice(1)
  266. }
  267. assert(pubKey.length === 64)
  268. // Only take the lower 160bits of the hash
  269. return exports.sha3(pubKey).slice(-20)
  270. }
  271. /**
  272. * Returns the ethereum public key of a given private key
  273. * @param {Buffer} privateKey A private key must be 256 bits wide
  274. * @return {Buffer}
  275. */
  276. var privateToPublic = exports.privateToPublic = function (privateKey) {
  277. privateKey = exports.toBuffer(privateKey)
  278. // skip the type flag and use the X, Y points
  279. return secp256k1.publicKeyCreate(privateKey, false).slice(1)
  280. }
  281. /**
  282. * Converts a public key to the Ethereum format.
  283. * @param {Buffer} publicKey
  284. * @return {Buffer}
  285. */
  286. exports.importPublic = function (publicKey) {
  287. publicKey = exports.toBuffer(publicKey)
  288. if (publicKey.length !== 64) {
  289. publicKey = secp256k1.publicKeyConvert(publicKey, false).slice(1)
  290. }
  291. return publicKey
  292. }
  293. /**
  294. * ECDSA sign
  295. * @param {Buffer} msgHash
  296. * @param {Buffer} privateKey
  297. * @return {Object}
  298. */
  299. exports.ecsign = function (msgHash, privateKey) {
  300. var sig = secp256k1.sign(msgHash, privateKey)
  301. var ret = {}
  302. ret.r = sig.signature.slice(0, 32)
  303. ret.s = sig.signature.slice(32, 64)
  304. ret.v = sig.recovery + 27
  305. return ret
  306. }
  307. /**
  308. * Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call.
  309. * The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign`
  310. * call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
  311. * used to produce the signature.
  312. * @param message
  313. * @returns {Buffer} hash
  314. */
  315. exports.hashPersonalMessage = function (message) {
  316. var prefix = exports.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString())
  317. return exports.sha3(Buffer.concat([prefix, message]))
  318. }
  319. /**
  320. * ECDSA public key recovery from signature
  321. * @param {Buffer} msgHash
  322. * @param {Number} v
  323. * @param {Buffer} r
  324. * @param {Buffer} s
  325. * @return {Buffer} publicKey
  326. */
  327. exports.ecrecover = function (msgHash, v, r, s) {
  328. var signature = Buffer.concat([exports.setLength(r, 32), exports.setLength(s, 32)], 64)
  329. var recovery = v - 27
  330. if (recovery !== 0 && recovery !== 1) {
  331. throw new Error('Invalid signature v value')
  332. }
  333. var senderPubKey = secp256k1.recover(msgHash, signature, recovery)
  334. return secp256k1.publicKeyConvert(senderPubKey, false).slice(1)
  335. }
  336. /**
  337. * Convert signature parameters into the format of `eth_sign` RPC method
  338. * @param {Number} v
  339. * @param {Buffer} r
  340. * @param {Buffer} s
  341. * @return {String} sig
  342. */
  343. exports.toRpcSig = function (v, r, s) {
  344. // NOTE: with potential introduction of chainId this might need to be updated
  345. if (v !== 27 && v !== 28) {
  346. throw new Error('Invalid recovery id')
  347. }
  348. // geth (and the RPC eth_sign method) uses the 65 byte format used by Bitcoin
  349. // FIXME: this might change in the future - https://github.com/ethereum/go-ethereum/issues/2053
  350. return exports.bufferToHex(Buffer.concat([
  351. exports.setLengthLeft(r, 32),
  352. exports.setLengthLeft(s, 32),
  353. exports.toBuffer(v - 27)
  354. ]))
  355. }
  356. /**
  357. * Convert signature format of the `eth_sign` RPC method to signature parameters
  358. * NOTE: all because of a bug in geth: https://github.com/ethereum/go-ethereum/issues/2053
  359. * @param {String} sig
  360. * @return {Object}
  361. */
  362. exports.fromRpcSig = function (sig) {
  363. sig = exports.toBuffer(sig)
  364. // NOTE: with potential introduction of chainId this might need to be updated
  365. if (sig.length !== 65) {
  366. throw new Error('Invalid signature length')
  367. }
  368. var v = sig[64]
  369. // support both versions of `eth_sign` responses
  370. if (v < 27) {
  371. v += 27
  372. }
  373. return {
  374. v: v,
  375. r: sig.slice(0, 32),
  376. s: sig.slice(32, 64)
  377. }
  378. }
  379. /**
  380. * Returns the ethereum address of a given private key
  381. * @param {Buffer} privateKey A private key must be 256 bits wide
  382. * @return {Buffer}
  383. */
  384. exports.privateToAddress = function (privateKey) {
  385. return exports.publicToAddress(privateToPublic(privateKey))
  386. }
  387. /**
  388. * Checks if the address is a valid. Accepts checksummed addresses too
  389. * @param {String} address
  390. * @return {Boolean}
  391. */
  392. exports.isValidAddress = function (address) {
  393. return /^0x[0-9a-fA-F]{40}$/i.test(address)
  394. }
  395. /**
  396. * Returns a checksummed address
  397. * @param {String} address
  398. * @return {String}
  399. */
  400. exports.toChecksumAddress = function (address) {
  401. address = exports.stripHexPrefix(address).toLowerCase()
  402. var hash = exports.sha3(address).toString('hex')
  403. var ret = '0x'
  404. for (var i = 0; i < address.length; i++) {
  405. if (parseInt(hash[i], 16) >= 8) {
  406. ret += address[i].toUpperCase()
  407. } else {
  408. ret += address[i]
  409. }
  410. }
  411. return ret
  412. }
  413. /**
  414. * Checks if the address is a valid checksummed address
  415. * @param {Buffer} address
  416. * @return {Boolean}
  417. */
  418. exports.isValidChecksumAddress = function (address) {
  419. return exports.isValidAddress(address) && (exports.toChecksumAddress(address) === address)
  420. }
  421. /**
  422. * Generates an address of a newly created contract
  423. * @param {Buffer} from the address which is creating this new address
  424. * @param {Buffer} nonce the nonce of the from account
  425. * @return {Buffer}
  426. */
  427. exports.generateAddress = function (from, nonce) {
  428. from = exports.toBuffer(from)
  429. nonce = new BN(nonce)
  430. if (nonce.isZero()) {
  431. // in RLP we want to encode null in the case of zero nonce
  432. // read the RLP documentation for an answer if you dare
  433. nonce = null
  434. } else {
  435. nonce = Buffer.from(nonce.toArray())
  436. }
  437. // Only take the lower 160bits of the hash
  438. return exports.rlphash([from, nonce]).slice(-20)
  439. }
  440. /**
  441. * Returns true if the supplied address belongs to a precompiled account
  442. * @param {Buffer|String} address
  443. * @return {Boolean}
  444. */
  445. exports.isPrecompiled = function (address) {
  446. var a = exports.unpad(address)
  447. return a.length === 1 && a[0] > 0 && a[0] < 5
  448. }
  449. /**
  450. * Adds "0x" to a given `String` if it does not already start with "0x"
  451. * @param {String} str
  452. * @return {String}
  453. */
  454. exports.addHexPrefix = function (str) {
  455. if (typeof str !== 'string') {
  456. return str
  457. }
  458. return exports.isHexPrefixed(str) ? str : '0x' + str
  459. }
  460. /**
  461. * Validate ECDSA signature
  462. * @method isValidSignature
  463. * @param {Buffer} v
  464. * @param {Buffer} r
  465. * @param {Buffer} s
  466. * @param {Boolean} [homestead=true]
  467. * @return {Boolean}
  468. */
  469. exports.isValidSignature = function (v, r, s, homestead) {
  470. const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
  471. const SECP256K1_N = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16)
  472. if (r.length !== 32 || s.length !== 32) {
  473. return false
  474. }
  475. if (v !== 27 && v !== 28) {
  476. return false
  477. }
  478. r = new BN(r)
  479. s = new BN(s)
  480. if (r.isZero() || r.gt(SECP256K1_N) || s.isZero() || s.gt(SECP256K1_N)) {
  481. return false
  482. }
  483. if ((homestead === false) && (new BN(s).cmp(SECP256K1_N_DIV_2) === 1)) {
  484. return false
  485. }
  486. return true
  487. }
  488. /**
  489. * Converts a `Buffer` or `Array` to JSON
  490. * @param {Buffer|Array} ba
  491. * @return {Array|String|null}
  492. */
  493. exports.baToJSON = function (ba) {
  494. if (Buffer.isBuffer(ba)) {
  495. return '0x' + ba.toString('hex')
  496. } else if (ba instanceof Array) {
  497. var array = []
  498. for (var i = 0; i < ba.length; i++) {
  499. array.push(exports.baToJSON(ba[i]))
  500. }
  501. return array
  502. }
  503. }
  504. /**
  505. * Defines properties on a `Object`. It make the assumption that underlying data is binary.
  506. * @param {Object} self the `Object` to define properties on
  507. * @param {Array} fields an array fields to define. Fields can contain:
  508. * * `name` - the name of the properties
  509. * * `length` - the number of bytes the field can have
  510. * * `allowLess` - if the field can be less than the length
  511. * * `allowEmpty`
  512. * @param {*} data data to be validated against the definitions
  513. */
  514. exports.defineProperties = function (self, fields, data) {
  515. self.raw = []
  516. self._fields = []
  517. // attach the `toJSON`
  518. self.toJSON = function (label) {
  519. if (label) {
  520. var obj = {}
  521. self._fields.forEach(function (field) {
  522. obj[field] = '0x' + self[field].toString('hex')
  523. })
  524. return obj
  525. }
  526. return exports.baToJSON(this.raw)
  527. }
  528. self.serialize = function serialize () {
  529. return rlp.encode(self.raw)
  530. }
  531. fields.forEach(function (field, i) {
  532. self._fields.push(field.name)
  533. function getter () {
  534. return self.raw[i]
  535. }
  536. function setter (v) {
  537. v = exports.toBuffer(v)
  538. if (v.toString('hex') === '00' && !field.allowZero) {
  539. v = Buffer.allocUnsafe(0)
  540. }
  541. if (field.allowLess && field.length) {
  542. v = exports.stripZeros(v)
  543. assert(field.length >= v.length, 'The field ' + field.name + ' must not have more ' + field.length + ' bytes')
  544. } else if (!(field.allowZero && v.length === 0) && field.length) {
  545. assert(field.length === v.length, 'The field ' + field.name + ' must have byte length of ' + field.length)
  546. }
  547. self.raw[i] = v
  548. }
  549. Object.defineProperty(self, field.name, {
  550. enumerable: true,
  551. configurable: true,
  552. get: getter,
  553. set: setter
  554. })
  555. if (field.default) {
  556. self[field.name] = field.default
  557. }
  558. // attach alias
  559. if (field.alias) {
  560. Object.defineProperty(self, field.alias, {
  561. enumerable: false,
  562. configurable: true,
  563. set: setter,
  564. get: getter
  565. })
  566. }
  567. })
  568. // if the constuctor is passed data
  569. if (data) {
  570. if (typeof data === 'string') {
  571. data = Buffer.from(exports.stripHexPrefix(data), 'hex')
  572. }
  573. if (Buffer.isBuffer(data)) {
  574. data = rlp.decode(data)
  575. }
  576. if (Array.isArray(data)) {
  577. if (data.length > self._fields.length) {
  578. throw (new Error('wrong number of fields in data'))
  579. }
  580. // make sure all the items are buffers
  581. data.forEach(function (d, i) {
  582. self[self._fields[i]] = exports.toBuffer(d)
  583. })
  584. } else if (typeof data === 'object') {
  585. const keys = Object.keys(data)
  586. fields.forEach(function (field) {
  587. if (keys.indexOf(field.name) !== -1) self[field.name] = data[field.name]
  588. if (keys.indexOf(field.alias) !== -1) self[field.alias] = data[field.alias]
  589. })
  590. } else {
  591. throw new Error('invalid data')
  592. }
  593. }
  594. }
  595. }).call(this,require("buffer").Buffer)
  596. },{"assert":61,"bn.js":4,"buffer":65,"create-hash":7,"ethjs-util":26,"keccak":36,"rlp":45,"secp256k1":46}],3:[function(require,module,exports){
  597. (function (Buffer){
  598. // Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki
  599. // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
  600. // NOTE: SIGHASH byte ignored AND restricted, truncate before use
  601. function check (buffer) {
  602. if (buffer.length < 8) return false
  603. if (buffer.length > 72) return false
  604. if (buffer[0] !== 0x30) return false
  605. if (buffer[1] !== buffer.length - 2) return false
  606. if (buffer[2] !== 0x02) return false
  607. var lenR = buffer[3]
  608. if (lenR === 0) return false
  609. if (5 + lenR >= buffer.length) return false
  610. if (buffer[4 + lenR] !== 0x02) return false
  611. var lenS = buffer[5 + lenR]
  612. if (lenS === 0) return false
  613. if ((6 + lenR + lenS) !== buffer.length) return false
  614. if (buffer[4] & 0x80) return false
  615. if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) return false
  616. if (buffer[lenR + 6] & 0x80) return false
  617. if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) return false
  618. return true
  619. }
  620. function decode (buffer) {
  621. if (buffer.length < 8) throw new Error('DER sequence length is too short')
  622. if (buffer.length > 72) throw new Error('DER sequence length is too long')
  623. if (buffer[0] !== 0x30) throw new Error('Expected DER sequence')
  624. if (buffer[1] !== buffer.length - 2) throw new Error('DER sequence length is invalid')
  625. if (buffer[2] !== 0x02) throw new Error('Expected DER integer')
  626. var lenR = buffer[3]
  627. if (lenR === 0) throw new Error('R length is zero')
  628. if (5 + lenR >= buffer.length) throw new Error('R length is too long')
  629. if (buffer[4 + lenR] !== 0x02) throw new Error('Expected DER integer (2)')
  630. var lenS = buffer[5 + lenR]
  631. if (lenS === 0) throw new Error('S length is zero')
  632. if ((6 + lenR + lenS) !== buffer.length) throw new Error('S length is invalid')
  633. if (buffer[4] & 0x80) throw new Error('R value is negative')
  634. if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) throw new Error('R value excessively padded')
  635. if (buffer[lenR + 6] & 0x80) throw new Error('S value is negative')
  636. if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) throw new Error('S value excessively padded')
  637. // non-BIP66 - extract R, S values
  638. return {
  639. r: buffer.slice(4, 4 + lenR),
  640. s: buffer.slice(6 + lenR)
  641. }
  642. }
  643. /*
  644. * Expects r and s to be positive DER integers.
  645. *
  646. * The DER format uses the most significant bit as a sign bit (& 0x80).
  647. * If the significant bit is set AND the integer is positive, a 0x00 is prepended.
  648. *
  649. * Examples:
  650. *
  651. * 0 => 0x00
  652. * 1 => 0x01
  653. * -1 => 0xff
  654. * 127 => 0x7f
  655. * -127 => 0x81
  656. * 128 => 0x0080
  657. * -128 => 0x80
  658. * 255 => 0x00ff
  659. * -255 => 0xff01
  660. * 16300 => 0x3fac
  661. * -16300 => 0xc054
  662. * 62300 => 0x00f35c
  663. * -62300 => 0xff0ca4
  664. */
  665. function encode (r, s) {
  666. var lenR = r.length
  667. var lenS = s.length
  668. if (lenR === 0) throw new Error('R length is zero')
  669. if (lenS === 0) throw new Error('S length is zero')
  670. if (lenR > 33) throw new Error('R length is too long')
  671. if (lenS > 33) throw new Error('S length is too long')
  672. if (r[0] & 0x80) throw new Error('R value is negative')
  673. if (s[0] & 0x80) throw new Error('S value is negative')
  674. if (lenR > 1 && (r[0] === 0x00) && !(r[1] & 0x80)) throw new Error('R value excessively padded')
  675. if (lenS > 1 && (s[0] === 0x00) && !(s[1] & 0x80)) throw new Error('S value excessively padded')
  676. var signature = new Buffer(6 + lenR + lenS)
  677. // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
  678. signature[0] = 0x30
  679. signature[1] = signature.length - 2
  680. signature[2] = 0x02
  681. signature[3] = r.length
  682. r.copy(signature, 4)
  683. signature[4 + lenR] = 0x02
  684. signature[5 + lenR] = s.length
  685. s.copy(signature, 6 + lenR)
  686. return signature
  687. }
  688. module.exports = {
  689. check: check,
  690. decode: decode,
  691. encode: encode
  692. }
  693. }).call(this,require("buffer").Buffer)
  694. },{"buffer":65}],4:[function(require,module,exports){
  695. (function (module, exports) {
  696. 'use strict';
  697. // Utils
  698. function assert (val, msg) {
  699. if (!val) throw new Error(msg || 'Assertion failed');
  700. }
  701. // Could use `inherits` module, but don't want to move from single file
  702. // architecture yet.
  703. function inherits (ctor, superCtor) {
  704. ctor.super_ = superCtor;
  705. var TempCtor = function () {};
  706. TempCtor.prototype = superCtor.prototype;
  707. ctor.prototype = new TempCtor();
  708. ctor.prototype.constructor = ctor;
  709. }
  710. // BN
  711. function BN (number, base, endian) {
  712. if (BN.isBN(number)) {
  713. return number;
  714. }
  715. this.negative = 0;
  716. this.words = null;
  717. this.length = 0;
  718. // Reduction context
  719. this.red = null;
  720. if (number !== null) {
  721. if (base === 'le' || base === 'be') {
  722. endian = base;
  723. base = 10;
  724. }
  725. this._init(number || 0, base || 10, endian || 'be');
  726. }
  727. }
  728. if (typeof module === 'object') {
  729. module.exports = BN;
  730. } else {
  731. exports.BN = BN;
  732. }
  733. BN.BN = BN;
  734. BN.wordSize = 26;
  735. var Buffer;
  736. try {
  737. Buffer = require('buf' + 'fer').Buffer;
  738. } catch (e) {
  739. }
  740. BN.isBN = function isBN (num) {
  741. if (num instanceof BN) {
  742. return true;
  743. }
  744. return num !== null && typeof num === 'object' &&
  745. num.constructor.wordSize === BN.wordSize && Array.isArray(num.words);
  746. };
  747. BN.max = function max (left, right) {
  748. if (left.cmp(right) > 0) return left;
  749. return right;
  750. };
  751. BN.min = function min (left, right) {
  752. if (left.cmp(right) < 0) return left;
  753. return right;
  754. };
  755. BN.prototype._init = function init (number, base, endian) {
  756. if (typeof number === 'number') {
  757. return this._initNumber(number, base, endian);
  758. }
  759. if (typeof number === 'object') {
  760. return this._initArray(number, base, endian);
  761. }
  762. if (base === 'hex') {
  763. base = 16;
  764. }
  765. assert(base === (base | 0) && base >= 2 && base <= 36);
  766. number = number.toString().replace(/\s+/g, '');
  767. var start = 0;
  768. if (number[0] === '-') {
  769. start++;
  770. }
  771. if (base === 16) {
  772. this._parseHex(number, start);
  773. } else {
  774. this._parseBase(number, base, start);
  775. }
  776. if (number[0] === '-') {
  777. this.negative = 1;
  778. }
  779. this.strip();
  780. if (endian !== 'le') return;
  781. this._initArray(this.toArray(), base, endian);
  782. };
  783. BN.prototype._initNumber = function _initNumber (number, base, endian) {
  784. if (number < 0) {
  785. this.negative = 1;
  786. number = -number;
  787. }
  788. if (number < 0x4000000) {
  789. this.words = [ number & 0x3ffffff ];
  790. this.length = 1;
  791. } else if (number < 0x10000000000000) {
  792. this.words = [
  793. number & 0x3ffffff,
  794. (number / 0x4000000) & 0x3ffffff
  795. ];
  796. this.length = 2;
  797. } else {
  798. assert(number < 0x20000000000000); // 2 ^ 53 (unsafe)
  799. this.words = [
  800. number & 0x3ffffff,
  801. (number / 0x4000000) & 0x3ffffff,
  802. 1
  803. ];
  804. this.length = 3;
  805. }
  806. if (endian !== 'le') return;
  807. // Reverse the bytes
  808. this._initArray(this.toArray(), base, endian);
  809. };
  810. BN.prototype._initArray = function _initArray (number, base, endian) {
  811. // Perhaps a Uint8Array
  812. assert(typeof number.length === 'number');
  813. if (number.length <= 0) {
  814. this.words = [ 0 ];
  815. this.length = 1;
  816. return this;
  817. }
  818. this.length = Math.ceil(number.length / 3);
  819. this.words = new Array(this.length);
  820. for (var i = 0; i < this.length; i++) {
  821. this.words[i] = 0;
  822. }
  823. var j, w;
  824. var off = 0;
  825. if (endian === 'be') {
  826. for (i = number.length - 1, j = 0; i >= 0; i -= 3) {
  827. w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16);
  828. this.words[j] |= (w << off) & 0x3ffffff;
  829. this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
  830. off += 24;
  831. if (off >= 26) {
  832. off -= 26;
  833. j++;
  834. }
  835. }
  836. } else if (endian === 'le') {
  837. for (i = 0, j = 0; i < number.length; i += 3) {
  838. w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16);
  839. this.words[j] |= (w << off) & 0x3ffffff;
  840. this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
  841. off += 24;
  842. if (off >= 26) {
  843. off -= 26;
  844. j++;
  845. }
  846. }
  847. }
  848. return this.strip();
  849. };
  850. function parseHex (str, start, end) {
  851. var r = 0;
  852. var len = Math.min(str.length, end);
  853. for (var i = start; i < len; i++) {
  854. var c = str.charCodeAt(i) - 48;
  855. r <<= 4;
  856. // 'a' - 'f'
  857. if (c >= 49 && c <= 54) {
  858. r |= c - 49 + 0xa;
  859. // 'A' - 'F'
  860. } else if (c >= 17 && c <= 22) {
  861. r |= c - 17 + 0xa;
  862. // '0' - '9'
  863. } else {
  864. r |= c & 0xf;
  865. }
  866. }
  867. return r;
  868. }
  869. BN.prototype._parseHex = function _parseHex (number, start) {
  870. // Create possibly bigger array to ensure that it fits the number
  871. this.length = Math.ceil((number.length - start) / 6);
  872. this.words = new Array(this.length);
  873. for (var i = 0; i < this.length; i++) {
  874. this.words[i] = 0;
  875. }
  876. var j, w;
  877. // Scan 24-bit chunks and add them to the number
  878. var off = 0;
  879. for (i = number.length - 6, j = 0; i >= start; i -= 6) {
  880. w = parseHex(number, i, i + 6);
  881. this.words[j] |= (w << off) & 0x3ffffff;
  882. // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb
  883. this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
  884. off += 24;
  885. if (off >= 26) {
  886. off -= 26;
  887. j++;
  888. }
  889. }
  890. if (i + 6 !== start) {
  891. w = parseHex(number, start, i + 6);
  892. this.words[j] |= (w << off) & 0x3ffffff;
  893. this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
  894. }
  895. this.strip();
  896. };
  897. function parseBase (str, start, end, mul) {
  898. var r = 0;
  899. var len = Math.min(str.length, end);
  900. for (var i = start; i < len; i++) {
  901. var c = str.charCodeAt(i) - 48;
  902. r *= mul;
  903. // 'a'
  904. if (c >= 49) {
  905. r += c - 49 + 0xa;
  906. // 'A'
  907. } else if (c >= 17) {
  908. r += c - 17 + 0xa;
  909. // '0' - '9'
  910. } else {
  911. r += c;
  912. }
  913. }
  914. return r;
  915. }
  916. BN.prototype._parseBase = function _parseBase (number, base, start) {
  917. // Initialize as zero
  918. this.words = [ 0 ];
  919. this.length = 1;
  920. // Find length of limb in base
  921. for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) {
  922. limbLen++;
  923. }
  924. limbLen--;
  925. limbPow = (limbPow / base) | 0;
  926. var total = number.length - start;
  927. var mod = total % limbLen;
  928. var end = Math.min(total, total - mod) + start;
  929. var word = 0;
  930. for (var i = start; i < end; i += limbLen) {
  931. word = parseBase(number, i, i + limbLen, base);
  932. this.imuln(limbPow);
  933. if (this.words[0] + word < 0x4000000) {
  934. this.words[0] += word;
  935. } else {
  936. this._iaddn(word);
  937. }
  938. }
  939. if (mod !== 0) {
  940. var pow = 1;
  941. word = parseBase(number, i, number.length, base);
  942. for (i = 0; i < mod; i++) {
  943. pow *= base;
  944. }
  945. this.imuln(pow);
  946. if (this.words[0] + word < 0x4000000) {
  947. this.words[0] += word;
  948. } else {
  949. this._iaddn(word);
  950. }
  951. }
  952. };
  953. BN.prototype.copy = function copy (dest) {
  954. dest.words = new Array(this.length);
  955. for (var i = 0; i < this.length; i++) {
  956. dest.words[i] = this.words[i];
  957. }
  958. dest.length = this.length;
  959. dest.negative = this.negative;
  960. dest.red = this.red;
  961. };
  962. BN.prototype.clone = function clone () {
  963. var r = new BN(null);
  964. this.copy(r);
  965. return r;
  966. };
  967. BN.prototype._expand = function _expand (size) {
  968. while (this.length < size) {
  969. this.words[this.length++] = 0;
  970. }
  971. return this;
  972. };
  973. // Remove leading `0` from `this`
  974. BN.prototype.strip = function strip () {
  975. while (this.length > 1 && this.words[this.length - 1] === 0) {
  976. this.length--;
  977. }
  978. return this._normSign();
  979. };
  980. BN.prototype._normSign = function _normSign () {
  981. // -0 = 0
  982. if (this.length === 1 && this.words[0] === 0) {
  983. this.negative = 0;
  984. }
  985. return this;
  986. };
  987. BN.prototype.inspect = function inspect () {
  988. return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>';
  989. };
  990. /*
  991. var zeros = [];
  992. var groupSizes = [];
  993. var groupBases = [];
  994. var s = '';
  995. var i = -1;
  996. while (++i < BN.wordSize) {
  997. zeros[i] = s;
  998. s += '0';
  999. }
  1000. groupSizes[0] = 0;
  1001. groupSizes[1] = 0;
  1002. groupBases[0] = 0;
  1003. groupBases[1] = 0;
  1004. var base = 2 - 1;
  1005. while (++base < 36 + 1) {
  1006. var groupSize = 0;
  1007. var groupBase = 1;
  1008. while (groupBase < (1 << BN.wordSize) / base) {
  1009. groupBase *= base;
  1010. groupSize += 1;
  1011. }
  1012. groupSizes[base] = groupSize;
  1013. groupBases[base] = groupBase;
  1014. }
  1015. */
  1016. var zeros = [
  1017. '',
  1018. '0',
  1019. '00',
  1020. '000',
  1021. '0000',
  1022. '00000',
  1023. '000000',
  1024. '0000000',
  1025. '00000000',
  1026. '000000000',
  1027. '0000000000',
  1028. '00000000000',
  1029. '000000000000',
  1030. '0000000000000',
  1031. '00000000000000',
  1032. '000000000000000',
  1033. '0000000000000000',
  1034. '00000000000000000',
  1035. '000000000000000000',
  1036. '0000000000000000000',
  1037. '00000000000000000000',
  1038. '000000000000000000000',
  1039. '0000000000000000000000',
  1040. '00000000000000000000000',
  1041. '000000000000000000000000',
  1042. '0000000000000000000000000'
  1043. ];
  1044. var groupSizes = [
  1045. 0, 0,
  1046. 25, 16, 12, 11, 10, 9, 8,
  1047. 8, 7, 7, 7, 7, 6, 6,
  1048. 6, 6, 6, 6, 6, 5, 5,
  1049. 5, 5, 5, 5, 5, 5, 5,
  1050. 5, 5, 5, 5, 5, 5, 5
  1051. ];
  1052. var groupBases = [
  1053. 0, 0,
  1054. 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216,
  1055. 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625,
  1056. 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632,
  1057. 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149,
  1058. 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176
  1059. ];
  1060. BN.prototype.toString = function toString (base, padding) {
  1061. base = base || 10;
  1062. padding = padding | 0 || 1;
  1063. var out;
  1064. if (base === 16 || base === 'hex') {
  1065. out = '';
  1066. var off = 0;
  1067. var carry = 0;
  1068. for (var i = 0; i < this.length; i++) {
  1069. var w = this.words[i];
  1070. var word = (((w << off) | carry) & 0xffffff).toString(16);
  1071. carry = (w >>> (24 - off)) & 0xffffff;
  1072. if (carry !== 0 || i !== this.length - 1) {
  1073. out = zeros[6 - word.length] + word + out;
  1074. } else {
  1075. out = word + out;
  1076. }
  1077. off += 2;
  1078. if (off >= 26) {
  1079. off -= 26;
  1080. i--;
  1081. }
  1082. }
  1083. if (carry !== 0) {
  1084. out = carry.toString(16) + out;
  1085. }
  1086. while (out.length % padding !== 0) {
  1087. out = '0' + out;
  1088. }
  1089. if (this.negative !== 0) {
  1090. out = '-' + out;
  1091. }
  1092. return out;
  1093. }
  1094. if (base === (base | 0) && base >= 2 && base <= 36) {
  1095. // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base));
  1096. var groupSize = groupSizes[base];
  1097. // var groupBase = Math.pow(base, groupSize);
  1098. var groupBase = groupBases[base];
  1099. out = '';
  1100. var c = this.clone();
  1101. c.negative = 0;
  1102. while (!c.isZero()) {
  1103. var r = c.modn(groupBase).toString(base);
  1104. c = c.idivn(groupBase);
  1105. if (!c.isZero()) {
  1106. out = zeros[groupSize - r.length] + r + out;
  1107. } else {
  1108. out = r + out;
  1109. }
  1110. }
  1111. if (this.isZero()) {
  1112. out = '0' + out;
  1113. }
  1114. while (out.length % padding !== 0) {
  1115. out = '0' + out;
  1116. }
  1117. if (this.negative !== 0) {
  1118. out = '-' + out;
  1119. }
  1120. return out;
  1121. }
  1122. assert(false, 'Base should be between 2 and 36');
  1123. };
  1124. BN.prototype.toNumber = function toNumber () {
  1125. var ret = this.words[0];
  1126. if (this.length === 2) {
  1127. ret += this.words[1] * 0x4000000;
  1128. } else if (this.length === 3 && this.words[2] === 0x01) {
  1129. // NOTE: at this stage it is known that the top bit is set
  1130. ret += 0x10000000000000 + (this.words[1] * 0x4000000);
  1131. } else if (this.length > 2) {
  1132. assert(false, 'Number can only safely store up to 53 bits');
  1133. }
  1134. return (this.negative !== 0) ? -ret : ret;
  1135. };
  1136. BN.prototype.toJSON = function toJSON () {
  1137. return this.toString(16);
  1138. };
  1139. BN.prototype.toBuffer = function toBuffer (endian, length) {
  1140. assert(typeof Buffer !== 'undefined');
  1141. return this.toArrayLike(Buffer, endian, length);
  1142. };
  1143. BN.prototype.toArray = function toArray (endian, length) {
  1144. return this.toArrayLike(Array, endian, length);
  1145. };
  1146. BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) {
  1147. var byteLength = this.byteLength();
  1148. var reqLength = length || Math.max(1, byteLength);
  1149. assert(byteLength <= reqLength, 'byte array longer than desired length');
  1150. assert(reqLength > 0, 'Requested array length <= 0');
  1151. this.strip();
  1152. var littleEndian = endian === 'le';
  1153. var res = new ArrayType(reqLength);
  1154. var b, i;
  1155. var q = this.clone();
  1156. if (!littleEndian) {
  1157. // Assume big-endian
  1158. for (i = 0; i < reqLength - byteLength; i++) {
  1159. res[i] = 0;
  1160. }
  1161. for (i = 0; !q.isZero(); i++) {
  1162. b = q.andln(0xff);
  1163. q.iushrn(8);
  1164. res[reqLength - i - 1] = b;
  1165. }
  1166. } else {
  1167. for (i = 0; !q.isZero(); i++) {
  1168. b = q.andln(0xff);
  1169. q.iushrn(8);
  1170. res[i] = b;
  1171. }
  1172. for (; i < reqLength; i++) {
  1173. res[i] = 0;
  1174. }
  1175. }
  1176. return res;
  1177. };
  1178. if (Math.clz32) {
  1179. BN.prototype._countBits = function _countBits (w) {
  1180. return 32 - Math.clz32(w);
  1181. };
  1182. } else {
  1183. BN.prototype._countBits = function _countBits (w) {
  1184. var t = w;
  1185. var r = 0;
  1186. if (t >= 0x1000) {
  1187. r += 13;
  1188. t >>>= 13;
  1189. }
  1190. if (t >= 0x40) {
  1191. r += 7;
  1192. t >>>= 7;
  1193. }
  1194. if (t >= 0x8) {
  1195. r += 4;
  1196. t >>>= 4;
  1197. }
  1198. if (t >= 0x02) {
  1199. r += 2;
  1200. t >>>= 2;
  1201. }
  1202. return r + t;
  1203. };
  1204. }
  1205. BN.prototype._zeroBits = function _zeroBits (w) {
  1206. // Short-cut
  1207. if (w === 0) return 26;
  1208. var t = w;
  1209. var r = 0;
  1210. if ((t & 0x1fff) === 0) {
  1211. r += 13;
  1212. t >>>= 13;
  1213. }
  1214. if ((t & 0x7f) === 0) {
  1215. r += 7;
  1216. t >>>= 7;
  1217. }
  1218. if ((t & 0xf) === 0) {
  1219. r += 4;
  1220. t >>>= 4;
  1221. }
  1222. if ((t & 0x3) === 0) {
  1223. r += 2;
  1224. t >>>= 2;
  1225. }
  1226. if ((t & 0x1) === 0) {
  1227. r++;
  1228. }
  1229. return r;
  1230. };
  1231. // Return number of used bits in a BN
  1232. BN.prototype.bitLength = function bitLength () {
  1233. var w = this.words[this.length - 1];
  1234. var hi = this._countBits(w);
  1235. return (this.length - 1) * 26 + hi;
  1236. };
  1237. function toBitArray (num) {
  1238. var w = new Array(num.bitLength());
  1239. for (var bit = 0; bit < w.length; bit++) {
  1240. var off = (bit / 26) | 0;
  1241. var wbit = bit % 26;
  1242. w[bit] = (num.words[off] & (1 << wbit)) >>> wbit;
  1243. }
  1244. return w;
  1245. }
  1246. // Number of trailing zero bits
  1247. BN.prototype.zeroBits = function zeroBits () {
  1248. if (this.isZero()) return 0;
  1249. var r = 0;
  1250. for (var i = 0; i < this.length; i++) {
  1251. var b = this._zeroBits(this.words[i]);
  1252. r += b;
  1253. if (b !== 26) break;
  1254. }
  1255. return r;
  1256. };
  1257. BN.prototype.byteLength = function byteLength () {
  1258. return Math.ceil(this.bitLength() / 8);
  1259. };
  1260. BN.prototype.toTwos = function toTwos (width) {
  1261. if (this.negative !== 0) {
  1262. return this.abs().inotn(width).iaddn(1);
  1263. }
  1264. return this.clone();
  1265. };
  1266. BN.prototype.fromTwos = function fromTwos (width) {
  1267. if (this.testn(width - 1)) {
  1268. return this.notn(width).iaddn(1).ineg();
  1269. }
  1270. return this.clone();
  1271. };
  1272. BN.prototype.isNeg = function isNeg () {
  1273. return this.negative !== 0;
  1274. };
  1275. // Return negative clone of `this`
  1276. BN.prototype.neg = function neg () {
  1277. return this.clone().ineg();
  1278. };
  1279. BN.prototype.ineg = function ineg () {
  1280. if (!this.isZero()) {
  1281. this.negative ^= 1;
  1282. }
  1283. return this;
  1284. };
  1285. // Or `num` with `this` in-place
  1286. BN.prototype.iuor = function iuor (num) {
  1287. while (this.length < num.length) {
  1288. this.words[this.length++] = 0;
  1289. }
  1290. for (var i = 0; i < num.length; i++) {
  1291. this.words[i] = this.words[i] | num.words[i];
  1292. }
  1293. return this.strip();
  1294. };
  1295. BN.prototype.ior = function ior (num) {
  1296. assert((this.negative | num.negative) === 0);
  1297. return this.iuor(num);
  1298. };
  1299. // Or `num` with `this`
  1300. BN.prototype.or = function or (num) {
  1301. if (this.length > num.length) return this.clone().ior(num);
  1302. return num.clone().ior(this);
  1303. };
  1304. BN.prototype.uor = function uor (num) {
  1305. if (this.length > num.length) return this.clone().iuor(num);
  1306. return num.clone().iuor(this);
  1307. };
  1308. // And `num` with `this` in-place
  1309. BN.prototype.iuand = function iuand (num) {
  1310. // b = min-length(num, this)
  1311. var b;
  1312. if (this.length > num.length) {
  1313. b = num;
  1314. } else {
  1315. b = this;
  1316. }
  1317. for (var i = 0; i < b.length; i++) {
  1318. this.words[i] = this.words[i] & num.words[i];
  1319. }
  1320. this.length = b.length;
  1321. return this.strip();
  1322. };
  1323. BN.prototype.iand = function iand (num) {
  1324. assert((this.negative | num.negative) === 0);
  1325. return this.iuand(num);
  1326. };
  1327. // And `num` with `this`
  1328. BN.prototype.and = function and (num) {
  1329. if (this.length > num.length) return this.clone().iand(num);
  1330. return num.clone().iand(this);
  1331. };
  1332. BN.prototype.uand = function uand (num) {
  1333. if (this.length > num.length) return this.clone().iuand(num);
  1334. return num.clone().iuand(this);
  1335. };
  1336. // Xor `num` with `this` in-place
  1337. BN.prototype.iuxor = function iuxor (num) {
  1338. // a.length > b.length
  1339. var a;
  1340. var b;
  1341. if (this.length > num.length) {
  1342. a = this;
  1343. b = num;
  1344. } else {
  1345. a = num;
  1346. b = this;
  1347. }
  1348. for (var i = 0; i < b.length; i++) {
  1349. this.words[i] = a.words[i] ^ b.words[i];
  1350. }
  1351. if (this !== a) {
  1352. for (; i < a.length; i++) {
  1353. this.words[i] = a.words[i];
  1354. }
  1355. }
  1356. this.length = a.length;
  1357. return this.strip();
  1358. };
  1359. BN.prototype.ixor = function ixor (num) {
  1360. assert((this.negative | num.negative) === 0);
  1361. return this.iuxor(num);
  1362. };
  1363. // Xor `num` with `this`
  1364. BN.prototype.xor = function xor (num) {
  1365. if (this.length > num.length) return this.clone().ixor(num);
  1366. return num.clone().ixor(this);
  1367. };
  1368. BN.prototype.uxor = function uxor (num) {
  1369. if (this.length > num.length) return this.clone().iuxor(num);
  1370. return num.clone().iuxor(this);
  1371. };
  1372. // Not ``this`` with ``width`` bitwidth
  1373. BN.prototype.inotn = function inotn (width) {
  1374. assert(typeof width === 'number' && width >= 0);
  1375. var bytesNeeded = Math.ceil(width / 26) | 0;
  1376. var bitsLeft = width % 26;
  1377. // Extend the buffer with leading zeroes
  1378. this._expand(bytesNeeded);
  1379. if (bitsLeft > 0) {
  1380. bytesNeeded--;
  1381. }
  1382. // Handle complete words
  1383. for (var i = 0; i < bytesNeeded; i++) {
  1384. this.words[i] = ~this.words[i] & 0x3ffffff;
  1385. }
  1386. // Handle the residue
  1387. if (bitsLeft > 0) {
  1388. this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft));
  1389. }
  1390. // And remove leading zeroes
  1391. return this.strip();
  1392. };
  1393. BN.prototype.notn = function notn (width) {
  1394. return this.clone().inotn(width);
  1395. };
  1396. // Set `bit` of `this`
  1397. BN.prototype.setn = function setn (bit, val) {
  1398. assert(typeof bit === 'number' && bit >= 0);
  1399. var off = (bit / 26) | 0;
  1400. var wbit = bit % 26;
  1401. this._expand(off + 1);
  1402. if (val) {
  1403. this.words[off] = this.words[off] | (1 << wbit);
  1404. } else {
  1405. this.words[off] = this.words[off] & ~(1 << wbit);
  1406. }
  1407. return this.strip();
  1408. };
  1409. // Add `num` to `this` in-place
  1410. BN.prototype.iadd = function iadd (num) {
  1411. var r;
  1412. // negative + positive
  1413. if (this.negative !== 0 && num.negative === 0) {
  1414. this.negative = 0;
  1415. r = this.isub(num);
  1416. this.negative ^= 1;
  1417. return this._normSign();
  1418. // positive + negative
  1419. } else if (this.negative === 0 && num.negative !== 0) {
  1420. num.negative = 0;
  1421. r = this.isub(num);
  1422. num.negative = 1;
  1423. return r._normSign();
  1424. }
  1425. // a.length > b.length
  1426. var a, b;
  1427. if (this.length > num.length) {
  1428. a = this;
  1429. b = num;
  1430. } else {
  1431. a = num;
  1432. b = this;
  1433. }
  1434. var carry = 0;
  1435. for (var i = 0; i < b.length; i++) {
  1436. r = (a.words[i] | 0) + (b.words[i] | 0) + carry;
  1437. this.words[i] = r & 0x3ffffff;
  1438. carry = r >>> 26;
  1439. }
  1440. for (; carry !== 0 && i < a.length; i++) {
  1441. r = (a.words[i] | 0) + carry;
  1442. this.words[i] = r & 0x3ffffff;
  1443. carry = r >>> 26;
  1444. }
  1445. this.length = a.length;
  1446. if (carry !== 0) {
  1447. this.words[this.length] = carry;
  1448. this.length++;
  1449. // Copy the rest of the words
  1450. } else if (a !== this) {
  1451. for (; i < a.length; i++) {
  1452. this.words[i] = a.words[i];
  1453. }
  1454. }
  1455. return this;
  1456. };
  1457. // Add `num` to `this`
  1458. BN.prototype.add = function add (num) {
  1459. var res;
  1460. if (num.negative !== 0 && this.negative === 0) {
  1461. num.negative = 0;
  1462. res = this.sub(num);
  1463. num.negative ^= 1;
  1464. return res;
  1465. } else if (num.negative === 0 && this.negative !== 0) {
  1466. this.negative = 0;
  1467. res = num.sub(this);
  1468. this.negative = 1;
  1469. return res;
  1470. }
  1471. if (this.length > num.length) return this.clone().iadd(num);
  1472. return num.clone().iadd(this);
  1473. };
  1474. // Subtract `num` from `this` in-place
  1475. BN.prototype.isub = function isub (num) {
  1476. // this - (-num) = this + num
  1477. if (num.negative !== 0) {
  1478. num.negative = 0;
  1479. var r = this.iadd(num);
  1480. num.negative = 1;
  1481. return r._normSign();
  1482. // -this - num = -(this + num)
  1483. } else if (this.negative !== 0) {
  1484. this.negative = 0;
  1485. this.iadd(num);
  1486. this.negative = 1;
  1487. return this._normSign();
  1488. }
  1489. // At this point both numbers are positive
  1490. var cmp = this.cmp(num);
  1491. // Optimization - zeroify
  1492. if (cmp === 0) {
  1493. this.negative = 0;
  1494. this.length = 1;
  1495. this.words[0] = 0;
  1496. return this;
  1497. }
  1498. // a > b
  1499. var a, b;
  1500. if (cmp > 0) {
  1501. a = this;
  1502. b = num;
  1503. } else {
  1504. a = num;
  1505. b = this;
  1506. }
  1507. var carry = 0;
  1508. for (var i = 0; i < b.length; i++) {
  1509. r = (a.words[i] | 0) - (b.words[i] | 0) + carry;
  1510. carry = r >> 26;
  1511. this.words[i] = r & 0x3ffffff;
  1512. }
  1513. for (; carry !== 0 && i < a.length; i++) {
  1514. r = (a.words[i] | 0) + carry;
  1515. carry = r >> 26;
  1516. this.words[i] = r & 0x3ffffff;
  1517. }
  1518. // Copy rest of the words
  1519. if (carry === 0 && i < a.length && a !== this) {
  1520. for (; i < a.length; i++) {
  1521. this.words[i] = a.words[i];
  1522. }
  1523. }
  1524. this.length = Math.max(this.length, i);
  1525. if (a !== this) {
  1526. this.negative = 1;
  1527. }
  1528. return this.strip();
  1529. };
  1530. // Subtract `num` from `this`
  1531. BN.prototype.sub = function sub (num) {
  1532. return this.clone().isub(num);
  1533. };
  1534. function smallMulTo (self, num, out) {
  1535. out.negative = num.negative ^ self.negative;
  1536. var len = (self.length + num.length) | 0;
  1537. out.length = len;
  1538. len = (len - 1) | 0;
  1539. // Peel one iteration (compiler can't do it, because of code complexity)
  1540. var a = self.words[0] | 0;
  1541. var b = num.words[0] | 0;
  1542. var r = a * b;
  1543. var lo = r & 0x3ffffff;
  1544. var carry = (r / 0x4000000) | 0;
  1545. out.words[0] = lo;
  1546. for (var k = 1; k < len; k++) {
  1547. // Sum all words with the same `i + j = k` and accumulate `ncarry`,
  1548. // note that ncarry could be >= 0x3ffffff
  1549. var ncarry = carry >>> 26;
  1550. var rword = carry & 0x3ffffff;
  1551. var maxJ = Math.min(k, num.length - 1);
  1552. for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
  1553. var i = (k - j) | 0;
  1554. a = self.words[i] | 0;
  1555. b = num.words[j] | 0;
  1556. r = a * b + rword;
  1557. ncarry += (r / 0x4000000) | 0;
  1558. rword = r & 0x3ffffff;
  1559. }
  1560. out.words[k] = rword | 0;
  1561. carry = ncarry | 0;
  1562. }
  1563. if (carry !== 0) {
  1564. out.words[k] = carry | 0;
  1565. } else {
  1566. out.length--;
  1567. }
  1568. return out.strip();
  1569. }
  1570. // TODO(indutny): it may be reasonable to omit it for users who don't need
  1571. // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit
  1572. // multiplication (like elliptic secp256k1).
  1573. var comb10MulTo = function comb10MulTo (self, num, out) {
  1574. var a = self.words;
  1575. var b = num.words;
  1576. var o = out.words;
  1577. var c = 0;
  1578. var lo;
  1579. var mid;
  1580. var hi;
  1581. var a0 = a[0] | 0;
  1582. var al0 = a0 & 0x1fff;
  1583. var ah0 = a0 >>> 13;
  1584. var a1 = a[1] | 0;
  1585. var al1 = a1 & 0x1fff;
  1586. var ah1 = a1 >>> 13;
  1587. var a2 = a[2] | 0;
  1588. var al2 = a2 & 0x1fff;
  1589. var ah2 = a2 >>> 13;
  1590. var a3 = a[3] | 0;
  1591. var al3 = a3 & 0x1fff;
  1592. var ah3 = a3 >>> 13;
  1593. var a4 = a[4] | 0;
  1594. var al4 = a4 & 0x1fff;
  1595. var ah4 = a4 >>> 13;
  1596. var a5 = a[5] | 0;
  1597. var al5 = a5 & 0x1fff;
  1598. var ah5 = a5 >>> 13;
  1599. var a6 = a[6] | 0;
  1600. var al6 = a6 & 0x1fff;
  1601. var ah6 = a6 >>> 13;
  1602. var a7 = a[7] | 0;
  1603. var al7 = a7 & 0x1fff;
  1604. var ah7 = a7 >>> 13;
  1605. var a8 = a[8] | 0;
  1606. var al8 = a8 & 0x1fff;
  1607. var ah8 = a8 >>> 13;
  1608. var a9 = a[9] | 0;
  1609. var al9 = a9 & 0x1fff;
  1610. var ah9 = a9 >>> 13;
  1611. var b0 = b[0] | 0;
  1612. var bl0 = b0 & 0x1fff;
  1613. var bh0 = b0 >>> 13;
  1614. var b1 = b[1] | 0;
  1615. var bl1 = b1 & 0x1fff;
  1616. var bh1 = b1 >>> 13;
  1617. var b2 = b[2] | 0;
  1618. var bl2 = b2 & 0x1fff;
  1619. var bh2 = b2 >>> 13;
  1620. var b3 = b[3] | 0;
  1621. var bl3 = b3 & 0x1fff;
  1622. var bh3 = b3 >>> 13;
  1623. var b4 = b[4] | 0;
  1624. var bl4 = b4 & 0x1fff;
  1625. var bh4 = b4 >>> 13;
  1626. var b5 = b[5] | 0;
  1627. var bl5 = b5 & 0x1fff;
  1628. var bh5 = b5 >>> 13;
  1629. var b6 = b[6] | 0;
  1630. var bl6 = b6 & 0x1fff;
  1631. var bh6 = b6 >>> 13;
  1632. var b7 = b[7] | 0;
  1633. var bl7 = b7 & 0x1fff;
  1634. var bh7 = b7 >>> 13;
  1635. var b8 = b[8] | 0;
  1636. var bl8 = b8 & 0x1fff;
  1637. var bh8 = b8 >>> 13;
  1638. var b9 = b[9] | 0;
  1639. var bl9 = b9 & 0x1fff;
  1640. var bh9 = b9 >>> 13;
  1641. out.negative = self.negative ^ num.negative;
  1642. out.length = 19;
  1643. /* k = 0 */
  1644. lo = Math.imul(al0, bl0);
  1645. mid = Math.imul(al0, bh0);
  1646. mid = (mid + Math.imul(ah0, bl0)) | 0;
  1647. hi = Math.imul(ah0, bh0);
  1648. var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1649. c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0;
  1650. w0 &= 0x3ffffff;
  1651. /* k = 1 */
  1652. lo = Math.imul(al1, bl0);
  1653. mid = Math.imul(al1, bh0);
  1654. mid = (mid + Math.imul(ah1, bl0)) | 0;
  1655. hi = Math.imul(ah1, bh0);
  1656. lo = (lo + Math.imul(al0, bl1)) | 0;
  1657. mid = (mid + Math.imul(al0, bh1)) | 0;
  1658. mid = (mid + Math.imul(ah0, bl1)) | 0;
  1659. hi = (hi + Math.imul(ah0, bh1)) | 0;
  1660. var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1661. c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0;
  1662. w1 &= 0x3ffffff;
  1663. /* k = 2 */
  1664. lo = Math.imul(al2, bl0);
  1665. mid = Math.imul(al2, bh0);
  1666. mid = (mid + Math.imul(ah2, bl0)) | 0;
  1667. hi = Math.imul(ah2, bh0);
  1668. lo = (lo + Math.imul(al1, bl1)) | 0;
  1669. mid = (mid + Math.imul(al1, bh1)) | 0;
  1670. mid = (mid + Math.imul(ah1, bl1)) | 0;
  1671. hi = (hi + Math.imul(ah1, bh1)) | 0;
  1672. lo = (lo + Math.imul(al0, bl2)) | 0;
  1673. mid = (mid + Math.imul(al0, bh2)) | 0;
  1674. mid = (mid + Math.imul(ah0, bl2)) | 0;
  1675. hi = (hi + Math.imul(ah0, bh2)) | 0;
  1676. var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1677. c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0;
  1678. w2 &= 0x3ffffff;
  1679. /* k = 3 */
  1680. lo = Math.imul(al3, bl0);
  1681. mid = Math.imul(al3, bh0);
  1682. mid = (mid + Math.imul(ah3, bl0)) | 0;
  1683. hi = Math.imul(ah3, bh0);
  1684. lo = (lo + Math.imul(al2, bl1)) | 0;
  1685. mid = (mid + Math.imul(al2, bh1)) | 0;
  1686. mid = (mid + Math.imul(ah2, bl1)) | 0;
  1687. hi = (hi + Math.imul(ah2, bh1)) | 0;
  1688. lo = (lo + Math.imul(al1, bl2)) | 0;
  1689. mid = (mid + Math.imul(al1, bh2)) | 0;
  1690. mid = (mid + Math.imul(ah1, bl2)) | 0;
  1691. hi = (hi + Math.imul(ah1, bh2)) | 0;
  1692. lo = (lo + Math.imul(al0, bl3)) | 0;
  1693. mid = (mid + Math.imul(al0, bh3)) | 0;
  1694. mid = (mid + Math.imul(ah0, bl3)) | 0;
  1695. hi = (hi + Math.imul(ah0, bh3)) | 0;
  1696. var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1697. c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0;
  1698. w3 &= 0x3ffffff;
  1699. /* k = 4 */
  1700. lo = Math.imul(al4, bl0);
  1701. mid = Math.imul(al4, bh0);
  1702. mid = (mid + Math.imul(ah4, bl0)) | 0;
  1703. hi = Math.imul(ah4, bh0);
  1704. lo = (lo + Math.imul(al3, bl1)) | 0;
  1705. mid = (mid + Math.imul(al3, bh1)) | 0;
  1706. mid = (mid + Math.imul(ah3, bl1)) | 0;
  1707. hi = (hi + Math.imul(ah3, bh1)) | 0;
  1708. lo = (lo + Math.imul(al2, bl2)) | 0;
  1709. mid = (mid + Math.imul(al2, bh2)) | 0;
  1710. mid = (mid + Math.imul(ah2, bl2)) | 0;
  1711. hi = (hi + Math.imul(ah2, bh2)) | 0;
  1712. lo = (lo + Math.imul(al1, bl3)) | 0;
  1713. mid = (mid + Math.imul(al1, bh3)) | 0;
  1714. mid = (mid + Math.imul(ah1, bl3)) | 0;
  1715. hi = (hi + Math.imul(ah1, bh3)) | 0;
  1716. lo = (lo + Math.imul(al0, bl4)) | 0;
  1717. mid = (mid + Math.imul(al0, bh4)) | 0;
  1718. mid = (mid + Math.imul(ah0, bl4)) | 0;
  1719. hi = (hi + Math.imul(ah0, bh4)) | 0;
  1720. var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1721. c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0;
  1722. w4 &= 0x3ffffff;
  1723. /* k = 5 */
  1724. lo = Math.imul(al5, bl0);
  1725. mid = Math.imul(al5, bh0);
  1726. mid = (mid + Math.imul(ah5, bl0)) | 0;
  1727. hi = Math.imul(ah5, bh0);
  1728. lo = (lo + Math.imul(al4, bl1)) | 0;
  1729. mid = (mid + Math.imul(al4, bh1)) | 0;
  1730. mid = (mid + Math.imul(ah4, bl1)) | 0;
  1731. hi = (hi + Math.imul(ah4, bh1)) | 0;
  1732. lo = (lo + Math.imul(al3, bl2)) | 0;
  1733. mid = (mid + Math.imul(al3, bh2)) | 0;
  1734. mid = (mid + Math.imul(ah3, bl2)) | 0;
  1735. hi = (hi + Math.imul(ah3, bh2)) | 0;
  1736. lo = (lo + Math.imul(al2, bl3)) | 0;
  1737. mid = (mid + Math.imul(al2, bh3)) | 0;
  1738. mid = (mid + Math.imul(ah2, bl3)) | 0;
  1739. hi = (hi + Math.imul(ah2, bh3)) | 0;
  1740. lo = (lo + Math.imul(al1, bl4)) | 0;
  1741. mid = (mid + Math.imul(al1, bh4)) | 0;
  1742. mid = (mid + Math.imul(ah1, bl4)) | 0;
  1743. hi = (hi + Math.imul(ah1, bh4)) | 0;
  1744. lo = (lo + Math.imul(al0, bl5)) | 0;
  1745. mid = (mid + Math.imul(al0, bh5)) | 0;
  1746. mid = (mid + Math.imul(ah0, bl5)) | 0;
  1747. hi = (hi + Math.imul(ah0, bh5)) | 0;
  1748. var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1749. c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0;
  1750. w5 &= 0x3ffffff;
  1751. /* k = 6 */
  1752. lo = Math.imul(al6, bl0);
  1753. mid = Math.imul(al6, bh0);
  1754. mid = (mid + Math.imul(ah6, bl0)) | 0;
  1755. hi = Math.imul(ah6, bh0);
  1756. lo = (lo + Math.imul(al5, bl1)) | 0;
  1757. mid = (mid + Math.imul(al5, bh1)) | 0;
  1758. mid = (mid + Math.imul(ah5, bl1)) | 0;
  1759. hi = (hi + Math.imul(ah5, bh1)) | 0;
  1760. lo = (lo + Math.imul(al4, bl2)) | 0;
  1761. mid = (mid + Math.imul(al4, bh2)) | 0;
  1762. mid = (mid + Math.imul(ah4, bl2)) | 0;
  1763. hi = (hi + Math.imul(ah4, bh2)) | 0;
  1764. lo = (lo + Math.imul(al3, bl3)) | 0;
  1765. mid = (mid + Math.imul(al3, bh3)) | 0;
  1766. mid = (mid + Math.imul(ah3, bl3)) | 0;
  1767. hi = (hi + Math.imul(ah3, bh3)) | 0;
  1768. lo = (lo + Math.imul(al2, bl4)) | 0;
  1769. mid = (mid + Math.imul(al2, bh4)) | 0;
  1770. mid = (mid + Math.imul(ah2, bl4)) | 0;
  1771. hi = (hi + Math.imul(ah2, bh4)) | 0;
  1772. lo = (lo + Math.imul(al1, bl5)) | 0;
  1773. mid = (mid + Math.imul(al1, bh5)) | 0;
  1774. mid = (mid + Math.imul(ah1, bl5)) | 0;
  1775. hi = (hi + Math.imul(ah1, bh5)) | 0;
  1776. lo = (lo + Math.imul(al0, bl6)) | 0;
  1777. mid = (mid + Math.imul(al0, bh6)) | 0;
  1778. mid = (mid + Math.imul(ah0, bl6)) | 0;
  1779. hi = (hi + Math.imul(ah0, bh6)) | 0;
  1780. var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1781. c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0;
  1782. w6 &= 0x3ffffff;
  1783. /* k = 7 */
  1784. lo = Math.imul(al7, bl0);
  1785. mid = Math.imul(al7, bh0);
  1786. mid = (mid + Math.imul(ah7, bl0)) | 0;
  1787. hi = Math.imul(ah7, bh0);
  1788. lo = (lo + Math.imul(al6, bl1)) | 0;
  1789. mid = (mid + Math.imul(al6, bh1)) | 0;
  1790. mid = (mid + Math.imul(ah6, bl1)) | 0;
  1791. hi = (hi + Math.imul(ah6, bh1)) | 0;
  1792. lo = (lo + Math.imul(al5, bl2)) | 0;
  1793. mid = (mid + Math.imul(al5, bh2)) | 0;
  1794. mid = (mid + Math.imul(ah5, bl2)) | 0;
  1795. hi = (hi + Math.imul(ah5, bh2)) | 0;
  1796. lo = (lo + Math.imul(al4, bl3)) | 0;
  1797. mid = (mid + Math.imul(al4, bh3)) | 0;
  1798. mid = (mid + Math.imul(ah4, bl3)) | 0;
  1799. hi = (hi + Math.imul(ah4, bh3)) | 0;
  1800. lo = (lo + Math.imul(al3, bl4)) | 0;
  1801. mid = (mid + Math.imul(al3, bh4)) | 0;
  1802. mid = (mid + Math.imul(ah3, bl4)) | 0;
  1803. hi = (hi + Math.imul(ah3, bh4)) | 0;
  1804. lo = (lo + Math.imul(al2, bl5)) | 0;
  1805. mid = (mid + Math.imul(al2, bh5)) | 0;
  1806. mid = (mid + Math.imul(ah2, bl5)) | 0;
  1807. hi = (hi + Math.imul(ah2, bh5)) | 0;
  1808. lo = (lo + Math.imul(al1, bl6)) | 0;
  1809. mid = (mid + Math.imul(al1, bh6)) | 0;
  1810. mid = (mid + Math.imul(ah1, bl6)) | 0;
  1811. hi = (hi + Math.imul(ah1, bh6)) | 0;
  1812. lo = (lo + Math.imul(al0, bl7)) | 0;
  1813. mid = (mid + Math.imul(al0, bh7)) | 0;
  1814. mid = (mid + Math.imul(ah0, bl7)) | 0;
  1815. hi = (hi + Math.imul(ah0, bh7)) | 0;
  1816. var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1817. c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0;
  1818. w7 &= 0x3ffffff;
  1819. /* k = 8 */
  1820. lo = Math.imul(al8, bl0);
  1821. mid = Math.imul(al8, bh0);
  1822. mid = (mid + Math.imul(ah8, bl0)) | 0;
  1823. hi = Math.imul(ah8, bh0);
  1824. lo = (lo + Math.imul(al7, bl1)) | 0;
  1825. mid = (mid + Math.imul(al7, bh1)) | 0;
  1826. mid = (mid + Math.imul(ah7, bl1)) | 0;
  1827. hi = (hi + Math.imul(ah7, bh1)) | 0;
  1828. lo = (lo + Math.imul(al6, bl2)) | 0;
  1829. mid = (mid + Math.imul(al6, bh2)) | 0;
  1830. mid = (mid + Math.imul(ah6, bl2)) | 0;
  1831. hi = (hi + Math.imul(ah6, bh2)) | 0;
  1832. lo = (lo + Math.imul(al5, bl3)) | 0;
  1833. mid = (mid + Math.imul(al5, bh3)) | 0;
  1834. mid = (mid + Math.imul(ah5, bl3)) | 0;
  1835. hi = (hi + Math.imul(ah5, bh3)) | 0;
  1836. lo = (lo + Math.imul(al4, bl4)) | 0;
  1837. mid = (mid + Math.imul(al4, bh4)) | 0;
  1838. mid = (mid + Math.imul(ah4, bl4)) | 0;
  1839. hi = (hi + Math.imul(ah4, bh4)) | 0;
  1840. lo = (lo + Math.imul(al3, bl5)) | 0;
  1841. mid = (mid + Math.imul(al3, bh5)) | 0;
  1842. mid = (mid + Math.imul(ah3, bl5)) | 0;
  1843. hi = (hi + Math.imul(ah3, bh5)) | 0;
  1844. lo = (lo + Math.imul(al2, bl6)) | 0;
  1845. mid = (mid + Math.imul(al2, bh6)) | 0;
  1846. mid = (mid + Math.imul(ah2, bl6)) | 0;
  1847. hi = (hi + Math.imul(ah2, bh6)) | 0;
  1848. lo = (lo + Math.imul(al1, bl7)) | 0;
  1849. mid = (mid + Math.imul(al1, bh7)) | 0;
  1850. mid = (mid + Math.imul(ah1, bl7)) | 0;
  1851. hi = (hi + Math.imul(ah1, bh7)) | 0;
  1852. lo = (lo + Math.imul(al0, bl8)) | 0;
  1853. mid = (mid + Math.imul(al0, bh8)) | 0;
  1854. mid = (mid + Math.imul(ah0, bl8)) | 0;
  1855. hi = (hi + Math.imul(ah0, bh8)) | 0;
  1856. var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1857. c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0;
  1858. w8 &= 0x3ffffff;
  1859. /* k = 9 */
  1860. lo = Math.imul(al9, bl0);
  1861. mid = Math.imul(al9, bh0);
  1862. mid = (mid + Math.imul(ah9, bl0)) | 0;
  1863. hi = Math.imul(ah9, bh0);
  1864. lo = (lo + Math.imul(al8, bl1)) | 0;
  1865. mid = (mid + Math.imul(al8, bh1)) | 0;
  1866. mid = (mid + Math.imul(ah8, bl1)) | 0;
  1867. hi = (hi + Math.imul(ah8, bh1)) | 0;
  1868. lo = (lo + Math.imul(al7, bl2)) | 0;
  1869. mid = (mid + Math.imul(al7, bh2)) | 0;
  1870. mid = (mid + Math.imul(ah7, bl2)) | 0;
  1871. hi = (hi + Math.imul(ah7, bh2)) | 0;
  1872. lo = (lo + Math.imul(al6, bl3)) | 0;
  1873. mid = (mid + Math.imul(al6, bh3)) | 0;
  1874. mid = (mid + Math.imul(ah6, bl3)) | 0;
  1875. hi = (hi + Math.imul(ah6, bh3)) | 0;
  1876. lo = (lo + Math.imul(al5, bl4)) | 0;
  1877. mid = (mid + Math.imul(al5, bh4)) | 0;
  1878. mid = (mid + Math.imul(ah5, bl4)) | 0;
  1879. hi = (hi + Math.imul(ah5, bh4)) | 0;
  1880. lo = (lo + Math.imul(al4, bl5)) | 0;
  1881. mid = (mid + Math.imul(al4, bh5)) | 0;
  1882. mid = (mid + Math.imul(ah4, bl5)) | 0;
  1883. hi = (hi + Math.imul(ah4, bh5)) | 0;
  1884. lo = (lo + Math.imul(al3, bl6)) | 0;
  1885. mid = (mid + Math.imul(al3, bh6)) | 0;
  1886. mid = (mid + Math.imul(ah3, bl6)) | 0;
  1887. hi = (hi + Math.imul(ah3, bh6)) | 0;
  1888. lo = (lo + Math.imul(al2, bl7)) | 0;
  1889. mid = (mid + Math.imul(al2, bh7)) | 0;
  1890. mid = (mid + Math.imul(ah2, bl7)) | 0;
  1891. hi = (hi + Math.imul(ah2, bh7)) | 0;
  1892. lo = (lo + Math.imul(al1, bl8)) | 0;
  1893. mid = (mid + Math.imul(al1, bh8)) | 0;
  1894. mid = (mid + Math.imul(ah1, bl8)) | 0;
  1895. hi = (hi + Math.imul(ah1, bh8)) | 0;
  1896. lo = (lo + Math.imul(al0, bl9)) | 0;
  1897. mid = (mid + Math.imul(al0, bh9)) | 0;
  1898. mid = (mid + Math.imul(ah0, bl9)) | 0;
  1899. hi = (hi + Math.imul(ah0, bh9)) | 0;
  1900. var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1901. c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0;
  1902. w9 &= 0x3ffffff;
  1903. /* k = 10 */
  1904. lo = Math.imul(al9, bl1);
  1905. mid = Math.imul(al9, bh1);
  1906. mid = (mid + Math.imul(ah9, bl1)) | 0;
  1907. hi = Math.imul(ah9, bh1);
  1908. lo = (lo + Math.imul(al8, bl2)) | 0;
  1909. mid = (mid + Math.imul(al8, bh2)) | 0;
  1910. mid = (mid + Math.imul(ah8, bl2)) | 0;
  1911. hi = (hi + Math.imul(ah8, bh2)) | 0;
  1912. lo = (lo + Math.imul(al7, bl3)) | 0;
  1913. mid = (mid + Math.imul(al7, bh3)) | 0;
  1914. mid = (mid + Math.imul(ah7, bl3)) | 0;
  1915. hi = (hi + Math.imul(ah7, bh3)) | 0;
  1916. lo = (lo + Math.imul(al6, bl4)) | 0;
  1917. mid = (mid + Math.imul(al6, bh4)) | 0;
  1918. mid = (mid + Math.imul(ah6, bl4)) | 0;
  1919. hi = (hi + Math.imul(ah6, bh4)) | 0;
  1920. lo = (lo + Math.imul(al5, bl5)) | 0;
  1921. mid = (mid + Math.imul(al5, bh5)) | 0;
  1922. mid = (mid + Math.imul(ah5, bl5)) | 0;
  1923. hi = (hi + Math.imul(ah5, bh5)) | 0;
  1924. lo = (lo + Math.imul(al4, bl6)) | 0;
  1925. mid = (mid + Math.imul(al4, bh6)) | 0;
  1926. mid = (mid + Math.imul(ah4, bl6)) | 0;
  1927. hi = (hi + Math.imul(ah4, bh6)) | 0;
  1928. lo = (lo + Math.imul(al3, bl7)) | 0;
  1929. mid = (mid + Math.imul(al3, bh7)) | 0;
  1930. mid = (mid + Math.imul(ah3, bl7)) | 0;
  1931. hi = (hi + Math.imul(ah3, bh7)) | 0;
  1932. lo = (lo + Math.imul(al2, bl8)) | 0;
  1933. mid = (mid + Math.imul(al2, bh8)) | 0;
  1934. mid = (mid + Math.imul(ah2, bl8)) | 0;
  1935. hi = (hi + Math.imul(ah2, bh8)) | 0;
  1936. lo = (lo + Math.imul(al1, bl9)) | 0;
  1937. mid = (mid + Math.imul(al1, bh9)) | 0;
  1938. mid = (mid + Math.imul(ah1, bl9)) | 0;
  1939. hi = (hi + Math.imul(ah1, bh9)) | 0;
  1940. var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1941. c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0;
  1942. w10 &= 0x3ffffff;
  1943. /* k = 11 */
  1944. lo = Math.imul(al9, bl2);
  1945. mid = Math.imul(al9, bh2);
  1946. mid = (mid + Math.imul(ah9, bl2)) | 0;
  1947. hi = Math.imul(ah9, bh2);
  1948. lo = (lo + Math.imul(al8, bl3)) | 0;
  1949. mid = (mid + Math.imul(al8, bh3)) | 0;
  1950. mid = (mid + Math.imul(ah8, bl3)) | 0;
  1951. hi = (hi + Math.imul(ah8, bh3)) | 0;
  1952. lo = (lo + Math.imul(al7, bl4)) | 0;
  1953. mid = (mid + Math.imul(al7, bh4)) | 0;
  1954. mid = (mid + Math.imul(ah7, bl4)) | 0;
  1955. hi = (hi + Math.imul(ah7, bh4)) | 0;
  1956. lo = (lo + Math.imul(al6, bl5)) | 0;
  1957. mid = (mid + Math.imul(al6, bh5)) | 0;
  1958. mid = (mid + Math.imul(ah6, bl5)) | 0;
  1959. hi = (hi + Math.imul(ah6, bh5)) | 0;
  1960. lo = (lo + Math.imul(al5, bl6)) | 0;
  1961. mid = (mid + Math.imul(al5, bh6)) | 0;
  1962. mid = (mid + Math.imul(ah5, bl6)) | 0;
  1963. hi = (hi + Math.imul(ah5, bh6)) | 0;
  1964. lo = (lo + Math.imul(al4, bl7)) | 0;
  1965. mid = (mid + Math.imul(al4, bh7)) | 0;
  1966. mid = (mid + Math.imul(ah4, bl7)) | 0;
  1967. hi = (hi + Math.imul(ah4, bh7)) | 0;
  1968. lo = (lo + Math.imul(al3, bl8)) | 0;
  1969. mid = (mid + Math.imul(al3, bh8)) | 0;
  1970. mid = (mid + Math.imul(ah3, bl8)) | 0;
  1971. hi = (hi + Math.imul(ah3, bh8)) | 0;
  1972. lo = (lo + Math.imul(al2, bl9)) | 0;
  1973. mid = (mid + Math.imul(al2, bh9)) | 0;
  1974. mid = (mid + Math.imul(ah2, bl9)) | 0;
  1975. hi = (hi + Math.imul(ah2, bh9)) | 0;
  1976. var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  1977. c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0;
  1978. w11 &= 0x3ffffff;
  1979. /* k = 12 */
  1980. lo = Math.imul(al9, bl3);
  1981. mid = Math.imul(al9, bh3);
  1982. mid = (mid + Math.imul(ah9, bl3)) | 0;
  1983. hi = Math.imul(ah9, bh3);
  1984. lo = (lo + Math.imul(al8, bl4)) | 0;
  1985. mid = (mid + Math.imul(al8, bh4)) | 0;
  1986. mid = (mid + Math.imul(ah8, bl4)) | 0;
  1987. hi = (hi + Math.imul(ah8, bh4)) | 0;
  1988. lo = (lo + Math.imul(al7, bl5)) | 0;
  1989. mid = (mid + Math.imul(al7, bh5)) | 0;
  1990. mid = (mid + Math.imul(ah7, bl5)) | 0;
  1991. hi = (hi + Math.imul(ah7, bh5)) | 0;
  1992. lo = (lo + Math.imul(al6, bl6)) | 0;
  1993. mid = (mid + Math.imul(al6, bh6)) | 0;
  1994. mid = (mid + Math.imul(ah6, bl6)) | 0;
  1995. hi = (hi + Math.imul(ah6, bh6)) | 0;
  1996. lo = (lo + Math.imul(al5, bl7)) | 0;
  1997. mid = (mid + Math.imul(al5, bh7)) | 0;
  1998. mid = (mid + Math.imul(ah5, bl7)) | 0;
  1999. hi = (hi + Math.imul(ah5, bh7)) | 0;
  2000. lo = (lo + Math.imul(al4, bl8)) | 0;
  2001. mid = (mid + Math.imul(al4, bh8)) | 0;
  2002. mid = (mid + Math.imul(ah4, bl8)) | 0;
  2003. hi = (hi + Math.imul(ah4, bh8)) | 0;
  2004. lo = (lo + Math.imul(al3, bl9)) | 0;
  2005. mid = (mid + Math.imul(al3, bh9)) | 0;
  2006. mid = (mid + Math.imul(ah3, bl9)) | 0;
  2007. hi = (hi + Math.imul(ah3, bh9)) | 0;
  2008. var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  2009. c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0;
  2010. w12 &= 0x3ffffff;
  2011. /* k = 13 */
  2012. lo = Math.imul(al9, bl4);
  2013. mid = Math.imul(al9, bh4);
  2014. mid = (mid + Math.imul(ah9, bl4)) | 0;
  2015. hi = Math.imul(ah9, bh4);
  2016. lo = (lo + Math.imul(al8, bl5)) | 0;
  2017. mid = (mid + Math.imul(al8, bh5)) | 0;
  2018. mid = (mid + Math.imul(ah8, bl5)) | 0;
  2019. hi = (hi + Math.imul(ah8, bh5)) | 0;
  2020. lo = (lo + Math.imul(al7, bl6)) | 0;
  2021. mid = (mid + Math.imul(al7, bh6)) | 0;
  2022. mid = (mid + Math.imul(ah7, bl6)) | 0;
  2023. hi = (hi + Math.imul(ah7, bh6)) | 0;
  2024. lo = (lo + Math.imul(al6, bl7)) | 0;
  2025. mid = (mid + Math.imul(al6, bh7)) | 0;
  2026. mid = (mid + Math.imul(ah6, bl7)) | 0;
  2027. hi = (hi + Math.imul(ah6, bh7)) | 0;
  2028. lo = (lo + Math.imul(al5, bl8)) | 0;
  2029. mid = (mid + Math.imul(al5, bh8)) | 0;
  2030. mid = (mid + Math.imul(ah5, bl8)) | 0;
  2031. hi = (hi + Math.imul(ah5, bh8)) | 0;
  2032. lo = (lo + Math.imul(al4, bl9)) | 0;
  2033. mid = (mid + Math.imul(al4, bh9)) | 0;
  2034. mid = (mid + Math.imul(ah4, bl9)) | 0;
  2035. hi = (hi + Math.imul(ah4, bh9)) | 0;
  2036. var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  2037. c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0;
  2038. w13 &= 0x3ffffff;
  2039. /* k = 14 */
  2040. lo = Math.imul(al9, bl5);
  2041. mid = Math.imul(al9, bh5);
  2042. mid = (mid + Math.imul(ah9, bl5)) | 0;
  2043. hi = Math.imul(ah9, bh5);
  2044. lo = (lo + Math.imul(al8, bl6)) | 0;
  2045. mid = (mid + Math.imul(al8, bh6)) | 0;
  2046. mid = (mid + Math.imul(ah8, bl6)) | 0;
  2047. hi = (hi + Math.imul(ah8, bh6)) | 0;
  2048. lo = (lo + Math.imul(al7, bl7)) | 0;
  2049. mid = (mid + Math.imul(al7, bh7)) | 0;
  2050. mid = (mid + Math.imul(ah7, bl7)) | 0;
  2051. hi = (hi + Math.imul(ah7, bh7)) | 0;
  2052. lo = (lo + Math.imul(al6, bl8)) | 0;
  2053. mid = (mid + Math.imul(al6, bh8)) | 0;
  2054. mid = (mid + Math.imul(ah6, bl8)) | 0;
  2055. hi = (hi + Math.imul(ah6, bh8)) | 0;
  2056. lo = (lo + Math.imul(al5, bl9)) | 0;
  2057. mid = (mid + Math.imul(al5, bh9)) | 0;
  2058. mid = (mid + Math.imul(ah5, bl9)) | 0;
  2059. hi = (hi + Math.imul(ah5, bh9)) | 0;
  2060. var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  2061. c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0;
  2062. w14 &= 0x3ffffff;
  2063. /* k = 15 */
  2064. lo = Math.imul(al9, bl6);
  2065. mid = Math.imul(al9, bh6);
  2066. mid = (mid + Math.imul(ah9, bl6)) | 0;
  2067. hi = Math.imul(ah9, bh6);
  2068. lo = (lo + Math.imul(al8, bl7)) | 0;
  2069. mid = (mid + Math.imul(al8, bh7)) | 0;
  2070. mid = (mid + Math.imul(ah8, bl7)) | 0;
  2071. hi = (hi + Math.imul(ah8, bh7)) | 0;
  2072. lo = (lo + Math.imul(al7, bl8)) | 0;
  2073. mid = (mid + Math.imul(al7, bh8)) | 0;
  2074. mid = (mid + Math.imul(ah7, bl8)) | 0;
  2075. hi = (hi + Math.imul(ah7, bh8)) | 0;
  2076. lo = (lo + Math.imul(al6, bl9)) | 0;
  2077. mid = (mid + Math.imul(al6, bh9)) | 0;
  2078. mid = (mid + Math.imul(ah6, bl9)) | 0;
  2079. hi = (hi + Math.imul(ah6, bh9)) | 0;
  2080. var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  2081. c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0;
  2082. w15 &= 0x3ffffff;
  2083. /* k = 16 */
  2084. lo = Math.imul(al9, bl7);
  2085. mid = Math.imul(al9, bh7);
  2086. mid = (mid + Math.imul(ah9, bl7)) | 0;
  2087. hi = Math.imul(ah9, bh7);
  2088. lo = (lo + Math.imul(al8, bl8)) | 0;
  2089. mid = (mid + Math.imul(al8, bh8)) | 0;
  2090. mid = (mid + Math.imul(ah8, bl8)) | 0;
  2091. hi = (hi + Math.imul(ah8, bh8)) | 0;
  2092. lo = (lo + Math.imul(al7, bl9)) | 0;
  2093. mid = (mid + Math.imul(al7, bh9)) | 0;
  2094. mid = (mid + Math.imul(ah7, bl9)) | 0;
  2095. hi = (hi + Math.imul(ah7, bh9)) | 0;
  2096. var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  2097. c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0;
  2098. w16 &= 0x3ffffff;
  2099. /* k = 17 */
  2100. lo = Math.imul(al9, bl8);
  2101. mid = Math.imul(al9, bh8);
  2102. mid = (mid + Math.imul(ah9, bl8)) | 0;
  2103. hi = Math.imul(ah9, bh8);
  2104. lo = (lo + Math.imul(al8, bl9)) | 0;
  2105. mid = (mid + Math.imul(al8, bh9)) | 0;
  2106. mid = (mid + Math.imul(ah8, bl9)) | 0;
  2107. hi = (hi + Math.imul(ah8, bh9)) | 0;
  2108. var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  2109. c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0;
  2110. w17 &= 0x3ffffff;
  2111. /* k = 18 */
  2112. lo = Math.imul(al9, bl9);
  2113. mid = Math.imul(al9, bh9);
  2114. mid = (mid + Math.imul(ah9, bl9)) | 0;
  2115. hi = Math.imul(ah9, bh9);
  2116. var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
  2117. c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0;
  2118. w18 &= 0x3ffffff;
  2119. o[0] = w0;
  2120. o[1] = w1;
  2121. o[2] = w2;
  2122. o[3] = w3;
  2123. o[4] = w4;
  2124. o[5] = w5;
  2125. o[6] = w6;
  2126. o[7] = w7;
  2127. o[8] = w8;
  2128. o[9] = w9;
  2129. o[10] = w10;
  2130. o[11] = w11;
  2131. o[12] = w12;
  2132. o[13] = w13;
  2133. o[14] = w14;
  2134. o[15] = w15;
  2135. o[16] = w16;
  2136. o[17] = w17;
  2137. o[18] = w18;
  2138. if (c !== 0) {
  2139. o[19] = c;
  2140. out.length++;
  2141. }
  2142. return out;
  2143. };
  2144. // Polyfill comb
  2145. if (!Math.imul) {
  2146. comb10MulTo = smallMulTo;
  2147. }
  2148. function bigMulTo (self, num, out) {
  2149. out.negative = num.negative ^ self.negative;
  2150. out.length = self.length + num.length;
  2151. var carry = 0;
  2152. var hncarry = 0;
  2153. for (var k = 0; k < out.length - 1; k++) {
  2154. // Sum all words with the same `i + j = k` and accumulate `ncarry`,
  2155. // note that ncarry could be >= 0x3ffffff
  2156. var ncarry = hncarry;
  2157. hncarry = 0;
  2158. var rword = carry & 0x3ffffff;
  2159. var maxJ = Math.min(k, num.length - 1);
  2160. for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
  2161. var i = k - j;
  2162. var a = self.words[i] | 0;
  2163. var b = num.words[j] | 0;
  2164. var r = a * b;
  2165. var lo = r & 0x3ffffff;
  2166. ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0;
  2167. lo = (lo + rword) | 0;
  2168. rword = lo & 0x3ffffff;
  2169. ncarry = (ncarry + (lo >>> 26)) | 0;
  2170. hncarry += ncarry >>> 26;
  2171. ncarry &= 0x3ffffff;
  2172. }
  2173. out.words[k] = rword;
  2174. carry = ncarry;
  2175. ncarry = hncarry;
  2176. }
  2177. if (carry !== 0) {
  2178. out.words[k] = carry;
  2179. } else {
  2180. out.length--;
  2181. }
  2182. return out.strip();
  2183. }
  2184. function jumboMulTo (self, num, out) {
  2185. var fftm = new FFTM();
  2186. return fftm.mulp(self, num, out);
  2187. }
  2188. BN.prototype.mulTo = function mulTo (num, out) {
  2189. var res;
  2190. var len = this.length + num.length;
  2191. if (this.length === 10 && num.length === 10) {
  2192. res = comb10MulTo(this, num, out);
  2193. } else if (len < 63) {
  2194. res = smallMulTo(this, num, out);
  2195. } else if (len < 1024) {
  2196. res = bigMulTo(this, num, out);
  2197. } else {
  2198. res = jumboMulTo(this, num, out);
  2199. }
  2200. return res;
  2201. };
  2202. // Cooley-Tukey algorithm for FFT
  2203. // slightly revisited to rely on looping instead of recursion
  2204. function FFTM (x, y) {
  2205. this.x = x;
  2206. this.y = y;
  2207. }
  2208. FFTM.prototype.makeRBT = function makeRBT (N) {
  2209. var t = new Array(N);
  2210. var l = BN.prototype._countBits(N) - 1;
  2211. for (var i = 0; i < N; i++) {
  2212. t[i] = this.revBin(i, l, N);
  2213. }
  2214. return t;
  2215. };
  2216. // Returns binary-reversed representation of `x`
  2217. FFTM.prototype.revBin = function revBin (x, l, N) {
  2218. if (x === 0 || x === N - 1) return x;
  2219. var rb = 0;
  2220. for (var i = 0; i < l; i++) {
  2221. rb |= (x & 1) << (l - i - 1);
  2222. x >>= 1;
  2223. }
  2224. return rb;
  2225. };
  2226. // Performs "tweedling" phase, therefore 'emulating'
  2227. // behaviour of the recursive algorithm
  2228. FFTM.prototype.permute = function permute (rbt, rws, iws, rtws, itws, N) {
  2229. for (var i = 0; i < N; i++) {
  2230. rtws[i] = rws[rbt[i]];
  2231. itws[i] = iws[rbt[i]];
  2232. }
  2233. };
  2234. FFTM.prototype.transform = function transform (rws, iws, rtws, itws, N, rbt) {
  2235. this.permute(rbt, rws, iws, rtws, itws, N);
  2236. for (var s = 1; s < N; s <<= 1) {
  2237. var l = s << 1;
  2238. var rtwdf = Math.cos(2 * Math.PI / l);
  2239. var itwdf = Math.sin(2 * Math.PI / l);
  2240. for (var p = 0; p < N; p += l) {
  2241. var rtwdf_ = rtwdf;
  2242. var itwdf_ = itwdf;
  2243. for (var j = 0; j < s; j++) {
  2244. var re = rtws[p + j];
  2245. var ie = itws[p + j];
  2246. var ro = rtws[p + j + s];
  2247. var io = itws[p + j + s];
  2248. var rx = rtwdf_ * ro - itwdf_ * io;
  2249. io = rtwdf_ * io + itwdf_ * ro;
  2250. ro = rx;
  2251. rtws[p + j] = re + ro;
  2252. itws[p + j] = ie + io;
  2253. rtws[p + j + s] = re - ro;
  2254. itws[p + j + s] = ie - io;
  2255. /* jshint maxdepth : false */
  2256. if (j !== l) {
  2257. rx = rtwdf * rtwdf_ - itwdf * itwdf_;
  2258. itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_;
  2259. rtwdf_ = rx;
  2260. }
  2261. }
  2262. }
  2263. }
  2264. };
  2265. FFTM.prototype.guessLen13b = function guessLen13b (n, m) {
  2266. var N = Math.max(m, n) | 1;
  2267. var odd = N & 1;
  2268. var i = 0;
  2269. for (N = N / 2 | 0; N; N = N >>> 1) {
  2270. i++;
  2271. }
  2272. return 1 << i + 1 + odd;
  2273. };
  2274. FFTM.prototype.conjugate = function conjugate (rws, iws, N) {
  2275. if (N <= 1) return;
  2276. for (var i = 0; i < N / 2; i++) {
  2277. var t = rws[i];
  2278. rws[i] = rws[N - i - 1];
  2279. rws[N - i - 1] = t;
  2280. t = iws[i];
  2281. iws[i] = -iws[N - i - 1];
  2282. iws[N - i - 1] = -t;
  2283. }
  2284. };
  2285. FFTM.prototype.normalize13b = function normalize13b (ws, N) {
  2286. var carry = 0;
  2287. for (var i = 0; i < N / 2; i++) {
  2288. var w = Math.round(ws[2 * i + 1] / N) * 0x2000 +
  2289. Math.round(ws[2 * i] / N) +
  2290. carry;
  2291. ws[i] = w & 0x3ffffff;
  2292. if (w < 0x4000000) {
  2293. carry = 0;
  2294. } else {
  2295. carry = w / 0x4000000 | 0;
  2296. }
  2297. }
  2298. return ws;
  2299. };
  2300. FFTM.prototype.convert13b = function convert13b (ws, len, rws, N) {
  2301. var carry = 0;
  2302. for (var i = 0; i < len; i++) {
  2303. carry = carry + (ws[i] | 0);
  2304. rws[2 * i] = carry & 0x1fff; carry = carry >>> 13;
  2305. rws[2 * i + 1] = carry & 0x1fff; carry = carry >>> 13;
  2306. }
  2307. // Pad with zeroes
  2308. for (i = 2 * len; i < N; ++i) {
  2309. rws[i] = 0;
  2310. }
  2311. assert(carry === 0);
  2312. assert((carry & ~0x1fff) === 0);
  2313. };
  2314. FFTM.prototype.stub = function stub (N) {
  2315. var ph = new Array(N);
  2316. for (var i = 0; i < N; i++) {
  2317. ph[i] = 0;
  2318. }
  2319. return ph;
  2320. };
  2321. FFTM.prototype.mulp = function mulp (x, y, out) {
  2322. var N = 2 * this.guessLen13b(x.length, y.length);
  2323. var rbt = this.makeRBT(N);
  2324. var _ = this.stub(N);
  2325. var rws = new Array(N);
  2326. var rwst = new Array(N);
  2327. var iwst = new Array(N);
  2328. var nrws = new Array(N);
  2329. var nrwst = new Array(N);
  2330. var niwst = new Array(N);
  2331. var rmws = out.words;
  2332. rmws.length = N;
  2333. this.convert13b(x.words, x.length, rws, N);
  2334. this.convert13b(y.words, y.length, nrws, N);
  2335. this.transform(rws, _, rwst, iwst, N, rbt);
  2336. this.transform(nrws, _, nrwst, niwst, N, rbt);
  2337. for (var i = 0; i < N; i++) {
  2338. var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i];
  2339. iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i];
  2340. rwst[i] = rx;
  2341. }
  2342. this.conjugate(rwst, iwst, N);
  2343. this.transform(rwst, iwst, rmws, _, N, rbt);
  2344. this.conjugate(rmws, _, N);
  2345. this.normalize13b(rmws, N);
  2346. out.negative = x.negative ^ y.negative;
  2347. out.length = x.length + y.length;
  2348. return out.strip();
  2349. };
  2350. // Multiply `this` by `num`
  2351. BN.prototype.mul = function mul (num) {
  2352. var out = new BN(null);
  2353. out.words = new Array(this.length + num.length);
  2354. return this.mulTo(num, out);
  2355. };
  2356. // Multiply employing FFT
  2357. BN.prototype.mulf = function mulf (num) {
  2358. var out = new BN(null);
  2359. out.words = new Array(this.length + num.length);
  2360. return jumboMulTo(this, num, out);
  2361. };
  2362. // In-place Multiplication
  2363. BN.prototype.imul = function imul (num) {
  2364. return this.clone().mulTo(num, this);
  2365. };
  2366. BN.prototype.imuln = function imuln (num) {
  2367. assert(typeof num === 'number');
  2368. assert(num < 0x4000000);
  2369. // Carry
  2370. var carry = 0;
  2371. for (var i = 0; i < this.length; i++) {
  2372. var w = (this.words[i] | 0) * num;
  2373. var lo = (w & 0x3ffffff) + (carry & 0x3ffffff);
  2374. carry >>= 26;
  2375. carry += (w / 0x4000000) | 0;
  2376. // NOTE: lo is 27bit maximum
  2377. carry += lo >>> 26;
  2378. this.words[i] = lo & 0x3ffffff;
  2379. }
  2380. if (carry !== 0) {
  2381. this.words[i] = carry;
  2382. this.length++;
  2383. }
  2384. return this;
  2385. };
  2386. BN.prototype.muln = function muln (num) {
  2387. return this.clone().imuln(num);
  2388. };
  2389. // `this` * `this`
  2390. BN.prototype.sqr = function sqr () {
  2391. return this.mul(this);
  2392. };
  2393. // `this` * `this` in-place
  2394. BN.prototype.isqr = function isqr () {
  2395. return this.imul(this.clone());
  2396. };
  2397. // Math.pow(`this`, `num`)
  2398. BN.prototype.pow = function pow (num) {
  2399. var w = toBitArray(num);
  2400. if (w.length === 0) return new BN(1);
  2401. // Skip leading zeroes
  2402. var res = this;
  2403. for (var i = 0; i < w.length; i++, res = res.sqr()) {
  2404. if (w[i] !== 0) break;
  2405. }
  2406. if (++i < w.length) {
  2407. for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) {
  2408. if (w[i] === 0) continue;
  2409. res = res.mul(q);
  2410. }
  2411. }
  2412. return res;
  2413. };
  2414. // Shift-left in-place
  2415. BN.prototype.iushln = function iushln (bits) {
  2416. assert(typeof bits === 'number' && bits >= 0);
  2417. var r = bits % 26;
  2418. var s = (bits - r) / 26;
  2419. var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r);
  2420. var i;
  2421. if (r !== 0) {
  2422. var carry = 0;
  2423. for (i = 0; i < this.length; i++) {
  2424. var newCarry = this.words[i] & carryMask;
  2425. var c = ((this.words[i] | 0) - newCarry) << r;
  2426. this.words[i] = c | carry;
  2427. carry = newCarry >>> (26 - r);
  2428. }
  2429. if (carry) {
  2430. this.words[i] = carry;
  2431. this.length++;
  2432. }
  2433. }
  2434. if (s !== 0) {
  2435. for (i = this.length - 1; i >= 0; i--) {
  2436. this.words[i + s] = this.words[i];
  2437. }
  2438. for (i = 0; i < s; i++) {
  2439. this.words[i] = 0;
  2440. }
  2441. this.length += s;
  2442. }
  2443. return this.strip();
  2444. };
  2445. BN.prototype.ishln = function ishln (bits) {
  2446. // TODO(indutny): implement me
  2447. assert(this.negative === 0);
  2448. return this.iushln(bits);
  2449. };
  2450. // Shift-right in-place
  2451. // NOTE: `hint` is a lowest bit before trailing zeroes
  2452. // NOTE: if `extended` is present - it will be filled with destroyed bits
  2453. BN.prototype.iushrn = function iushrn (bits, hint, extended) {
  2454. assert(typeof bits === 'number' && bits >= 0);
  2455. var h;
  2456. if (hint) {
  2457. h = (hint - (hint % 26)) / 26;
  2458. } else {
  2459. h = 0;
  2460. }
  2461. var r = bits % 26;
  2462. var s = Math.min((bits - r) / 26, this.length);
  2463. var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
  2464. var maskedWords = extended;
  2465. h -= s;
  2466. h = Math.max(0, h);
  2467. // Extended mode, copy masked part
  2468. if (maskedWords) {
  2469. for (var i = 0; i < s; i++) {
  2470. maskedWords.words[i] = this.words[i];
  2471. }
  2472. maskedWords.length = s;
  2473. }
  2474. if (s === 0) {
  2475. // No-op, we should not move anything at all
  2476. } else if (this.length > s) {
  2477. this.length -= s;
  2478. for (i = 0; i < this.length; i++) {
  2479. this.words[i] = this.words[i + s];
  2480. }
  2481. } else {
  2482. this.words[0] = 0;
  2483. this.length = 1;
  2484. }
  2485. var carry = 0;
  2486. for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) {
  2487. var word = this.words[i] | 0;
  2488. this.words[i] = (carry << (26 - r)) | (word >>> r);
  2489. carry = word & mask;
  2490. }
  2491. // Push carried bits as a mask
  2492. if (maskedWords && carry !== 0) {
  2493. maskedWords.words[maskedWords.length++] = carry;
  2494. }
  2495. if (this.length === 0) {
  2496. this.words[0] = 0;
  2497. this.length = 1;
  2498. }
  2499. return this.strip();
  2500. };
  2501. BN.prototype.ishrn = function ishrn (bits, hint, extended) {
  2502. // TODO(indutny): implement me
  2503. assert(this.negative === 0);
  2504. return this.iushrn(bits, hint, extended);
  2505. };
  2506. // Shift-left
  2507. BN.prototype.shln = function shln (bits) {
  2508. return this.clone().ishln(bits);
  2509. };
  2510. BN.prototype.ushln = function ushln (bits) {
  2511. return this.clone().iushln(bits);
  2512. };
  2513. // Shift-right
  2514. BN.prototype.shrn = function shrn (bits) {
  2515. return this.clone().ishrn(bits);
  2516. };
  2517. BN.prototype.ushrn = function ushrn (bits) {
  2518. return this.clone().iushrn(bits);
  2519. };
  2520. // Test if n bit is set
  2521. BN.prototype.testn = function testn (bit) {
  2522. assert(typeof bit === 'number' && bit >= 0);
  2523. var r = bit % 26;
  2524. var s = (bit - r) / 26;
  2525. var q = 1 << r;
  2526. // Fast case: bit is much higher than all existing words
  2527. if (this.length <= s) return false;
  2528. // Check bit and return
  2529. var w = this.words[s];
  2530. return !!(w & q);
  2531. };
  2532. // Return only lowers bits of number (in-place)
  2533. BN.prototype.imaskn = function imaskn (bits) {
  2534. assert(typeof bits === 'number' && bits >= 0);
  2535. var r = bits % 26;
  2536. var s = (bits - r) / 26;
  2537. assert(this.negative === 0, 'imaskn works only with positive numbers');
  2538. if (this.length <= s) {
  2539. return this;
  2540. }
  2541. if (r !== 0) {
  2542. s++;
  2543. }
  2544. this.length = Math.min(s, this.length);
  2545. if (r !== 0) {
  2546. var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
  2547. this.words[this.length - 1] &= mask;
  2548. }
  2549. return this.strip();
  2550. };
  2551. // Return only lowers bits of number
  2552. BN.prototype.maskn = function maskn (bits) {
  2553. return this.clone().imaskn(bits);
  2554. };
  2555. // Add plain number `num` to `this`
  2556. BN.prototype.iaddn = function iaddn (num) {
  2557. assert(typeof num === 'number');
  2558. assert(num < 0x4000000);
  2559. if (num < 0) return this.isubn(-num);
  2560. // Possible sign change
  2561. if (this.negative !== 0) {
  2562. if (this.length === 1 && (this.words[0] | 0) < num) {
  2563. this.words[0] = num - (this.words[0] | 0);
  2564. this.negative = 0;
  2565. return this;
  2566. }
  2567. this.negative = 0;
  2568. this.isubn(num);
  2569. this.negative = 1;
  2570. return this;
  2571. }
  2572. // Add without checks
  2573. return this._iaddn(num);
  2574. };
  2575. BN.prototype._iaddn = function _iaddn (num) {
  2576. this.words[0] += num;
  2577. // Carry
  2578. for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) {
  2579. this.words[i] -= 0x4000000;
  2580. if (i === this.length - 1) {
  2581. this.words[i + 1] = 1;
  2582. } else {
  2583. this.words[i + 1]++;
  2584. }
  2585. }
  2586. this.length = Math.max(this.length, i + 1);
  2587. return this;
  2588. };
  2589. // Subtract plain number `num` from `this`
  2590. BN.prototype.isubn = function isubn (num) {
  2591. assert(typeof num === 'number');
  2592. assert(num < 0x4000000);
  2593. if (num < 0) return this.iaddn(-num);
  2594. if (this.negative !== 0) {
  2595. this.negative = 0;
  2596. this.iaddn(num);
  2597. this.negative = 1;
  2598. return this;
  2599. }
  2600. this.words[0] -= num;
  2601. if (this.length === 1 && this.words[0] < 0) {
  2602. this.words[0] = -this.words[0];
  2603. this.negative = 1;
  2604. } else {
  2605. // Carry
  2606. for (var i = 0; i < this.length && this.words[i] < 0; i++) {
  2607. this.words[i] += 0x4000000;
  2608. this.words[i + 1] -= 1;
  2609. }
  2610. }
  2611. return this.strip();
  2612. };
  2613. BN.prototype.addn = function addn (num) {
  2614. return this.clone().iaddn(num);
  2615. };
  2616. BN.prototype.subn = function subn (num) {
  2617. return this.clone().isubn(num);
  2618. };
  2619. BN.prototype.iabs = function iabs () {
  2620. this.negative = 0;
  2621. return this;
  2622. };
  2623. BN.prototype.abs = function abs () {
  2624. return this.clone().iabs();
  2625. };
  2626. BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) {
  2627. var len = num.length + shift;
  2628. var i;
  2629. this._expand(len);
  2630. var w;
  2631. var carry = 0;
  2632. for (i = 0; i < num.length; i++) {
  2633. w = (this.words[i + shift] | 0) + carry;
  2634. var right = (num.words[i] | 0) * mul;
  2635. w -= right & 0x3ffffff;
  2636. carry = (w >> 26) - ((right / 0x4000000) | 0);
  2637. this.words[i + shift] = w & 0x3ffffff;
  2638. }
  2639. for (; i < this.length - shift; i++) {
  2640. w = (this.words[i + shift] | 0) + carry;
  2641. carry = w >> 26;
  2642. this.words[i + shift] = w & 0x3ffffff;
  2643. }
  2644. if (carry === 0) return this.strip();
  2645. // Subtraction overflow
  2646. assert(carry === -1);
  2647. carry = 0;
  2648. for (i = 0; i < this.length; i++) {
  2649. w = -(this.words[i] | 0) + carry;
  2650. carry = w >> 26;
  2651. this.words[i] = w & 0x3ffffff;
  2652. }
  2653. this.negative = 1;
  2654. return this.strip();
  2655. };
  2656. BN.prototype._wordDiv = function _wordDiv (num, mode) {
  2657. var shift = this.length - num.length;
  2658. var a = this.clone();
  2659. var b = num;
  2660. // Normalize
  2661. var bhi = b.words[b.length - 1] | 0;
  2662. var bhiBits = this._countBits(bhi);
  2663. shift = 26 - bhiBits;
  2664. if (shift !== 0) {
  2665. b = b.ushln(shift);
  2666. a.iushln(shift);
  2667. bhi = b.words[b.length - 1] | 0;
  2668. }
  2669. // Initialize quotient
  2670. var m = a.length - b.length;
  2671. var q;
  2672. if (mode !== 'mod') {
  2673. q = new BN(null);
  2674. q.length = m + 1;
  2675. q.words = new Array(q.length);
  2676. for (var i = 0; i < q.length; i++) {
  2677. q.words[i] = 0;
  2678. }
  2679. }
  2680. var diff = a.clone()._ishlnsubmul(b, 1, m);
  2681. if (diff.negative === 0) {
  2682. a = diff;
  2683. if (q) {
  2684. q.words[m] = 1;
  2685. }
  2686. }
  2687. for (var j = m - 1; j >= 0; j--) {
  2688. var qj = (a.words[b.length + j] | 0) * 0x4000000 +
  2689. (a.words[b.length + j - 1] | 0);
  2690. // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max
  2691. // (0x7ffffff)
  2692. qj = Math.min((qj / bhi) | 0, 0x3ffffff);
  2693. a._ishlnsubmul(b, qj, j);
  2694. while (a.negative !== 0) {
  2695. qj--;
  2696. a.negative = 0;
  2697. a._ishlnsubmul(b, 1, j);
  2698. if (!a.isZero()) {
  2699. a.negative ^= 1;
  2700. }
  2701. }
  2702. if (q) {
  2703. q.words[j] = qj;
  2704. }
  2705. }
  2706. if (q) {
  2707. q.strip();
  2708. }
  2709. a.strip();
  2710. // Denormalize
  2711. if (mode !== 'div' && shift !== 0) {
  2712. a.iushrn(shift);
  2713. }
  2714. return {
  2715. div: q || null,
  2716. mod: a
  2717. };
  2718. };
  2719. // NOTE: 1) `mode` can be set to `mod` to request mod only,
  2720. // to `div` to request div only, or be absent to
  2721. // request both div & mod
  2722. // 2) `positive` is true if unsigned mod is requested
  2723. BN.prototype.divmod = function divmod (num, mode, positive) {
  2724. assert(!num.isZero());
  2725. if (this.isZero()) {
  2726. return {
  2727. div: new BN(0),
  2728. mod: new BN(0)
  2729. };
  2730. }
  2731. var div, mod, res;
  2732. if (this.negative !== 0 && num.negative === 0) {
  2733. res = this.neg().divmod(num, mode);
  2734. if (mode !== 'mod') {
  2735. div = res.div.neg();
  2736. }
  2737. if (mode !== 'div') {
  2738. mod = res.mod.neg();
  2739. if (positive && mod.negative !== 0) {
  2740. mod.iadd(num);
  2741. }
  2742. }
  2743. return {
  2744. div: div,
  2745. mod: mod
  2746. };
  2747. }
  2748. if (this.negative === 0 && num.negative !== 0) {
  2749. res = this.divmod(num.neg(), mode);
  2750. if (mode !== 'mod') {
  2751. div = res.div.neg();
  2752. }
  2753. return {
  2754. div: div,
  2755. mod: res.mod
  2756. };
  2757. }
  2758. if ((this.negative & num.negative) !== 0) {
  2759. res = this.neg().divmod(num.neg(), mode);
  2760. if (mode !== 'div') {
  2761. mod = res.mod.neg();
  2762. if (positive && mod.negative !== 0) {
  2763. mod.isub(num);
  2764. }
  2765. }
  2766. return {
  2767. div: res.div,
  2768. mod: mod
  2769. };
  2770. }
  2771. // Both numbers are positive at this point
  2772. // Strip both numbers to approximate shift value
  2773. if (num.length > this.length || this.cmp(num) < 0) {
  2774. return {
  2775. div: new BN(0),
  2776. mod: this
  2777. };
  2778. }
  2779. // Very short reduction
  2780. if (num.length === 1) {
  2781. if (mode === 'div') {
  2782. return {
  2783. div: this.divn(num.words[0]),
  2784. mod: null
  2785. };
  2786. }
  2787. if (mode === 'mod') {
  2788. return {
  2789. div: null,
  2790. mod: new BN(this.modn(num.words[0]))
  2791. };
  2792. }
  2793. return {
  2794. div: this.divn(num.words[0]),
  2795. mod: new BN(this.modn(num.words[0]))
  2796. };
  2797. }
  2798. return this._wordDiv(num, mode);
  2799. };
  2800. // Find `this` / `num`
  2801. BN.prototype.div = function div (num) {
  2802. return this.divmod(num, 'div', false).div;
  2803. };
  2804. // Find `this` % `num`
  2805. BN.prototype.mod = function mod (num) {
  2806. return this.divmod(num, 'mod', false).mod;
  2807. };
  2808. BN.prototype.umod = function umod (num) {
  2809. return this.divmod(num, 'mod', true).mod;
  2810. };
  2811. // Find Round(`this` / `num`)
  2812. BN.prototype.divRound = function divRound (num) {
  2813. var dm = this.divmod(num);
  2814. // Fast case - exact division
  2815. if (dm.mod.isZero()) return dm.div;
  2816. var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
  2817. var half = num.ushrn(1);
  2818. var r2 = num.andln(1);
  2819. var cmp = mod.cmp(half);
  2820. // Round down
  2821. if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div;
  2822. // Round up
  2823. return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
  2824. };
  2825. BN.prototype.modn = function modn (num) {
  2826. assert(num <= 0x3ffffff);
  2827. var p = (1 << 26) % num;
  2828. var acc = 0;
  2829. for (var i = this.length - 1; i >= 0; i--) {
  2830. acc = (p * acc + (this.words[i] | 0)) % num;
  2831. }
  2832. return acc;
  2833. };
  2834. // In-place division by number
  2835. BN.prototype.idivn = function idivn (num) {
  2836. assert(num <= 0x3ffffff);
  2837. var carry = 0;
  2838. for (var i = this.length - 1; i >= 0; i--) {
  2839. var w = (this.words[i] | 0) + carry * 0x4000000;
  2840. this.words[i] = (w / num) | 0;
  2841. carry = w % num;
  2842. }
  2843. return this.strip();
  2844. };
  2845. BN.prototype.divn = function divn (num) {
  2846. return this.clone().idivn(num);
  2847. };
  2848. BN.prototype.egcd = function egcd (p) {
  2849. assert(p.negative === 0);
  2850. assert(!p.isZero());
  2851. var x = this;
  2852. var y = p.clone();
  2853. if (x.negative !== 0) {
  2854. x = x.umod(p);
  2855. } else {
  2856. x = x.clone();
  2857. }
  2858. // A * x + B * y = x
  2859. var A = new BN(1);
  2860. var B = new BN(0);
  2861. // C * x + D * y = y
  2862. var C = new BN(0);
  2863. var D = new BN(1);
  2864. var g = 0;
  2865. while (x.isEven() && y.isEven()) {
  2866. x.iushrn(1);
  2867. y.iushrn(1);
  2868. ++g;
  2869. }
  2870. var yp = y.clone();
  2871. var xp = x.clone();
  2872. while (!x.isZero()) {
  2873. for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
  2874. if (i > 0) {
  2875. x.iushrn(i);
  2876. while (i-- > 0) {
  2877. if (A.isOdd() || B.isOdd()) {
  2878. A.iadd(yp);
  2879. B.isub(xp);
  2880. }
  2881. A.iushrn(1);
  2882. B.iushrn(1);
  2883. }
  2884. }
  2885. for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
  2886. if (j > 0) {
  2887. y.iushrn(j);
  2888. while (j-- > 0) {
  2889. if (C.isOdd() || D.isOdd()) {
  2890. C.iadd(yp);
  2891. D.isub(xp);
  2892. }
  2893. C.iushrn(1);
  2894. D.iushrn(1);
  2895. }
  2896. }
  2897. if (x.cmp(y) >= 0) {
  2898. x.isub(y);
  2899. A.isub(C);
  2900. B.isub(D);
  2901. } else {
  2902. y.isub(x);
  2903. C.isub(A);
  2904. D.isub(B);
  2905. }
  2906. }
  2907. return {
  2908. a: C,
  2909. b: D,
  2910. gcd: y.iushln(g)
  2911. };
  2912. };
  2913. // This is reduced incarnation of the binary EEA
  2914. // above, designated to invert members of the
  2915. // _prime_ fields F(p) at a maximal speed
  2916. BN.prototype._invmp = function _invmp (p) {
  2917. assert(p.negative === 0);
  2918. assert(!p.isZero());
  2919. var a = this;
  2920. var b = p.clone();
  2921. if (a.negative !== 0) {
  2922. a = a.umod(p);
  2923. } else {
  2924. a = a.clone();
  2925. }
  2926. var x1 = new BN(1);
  2927. var x2 = new BN(0);
  2928. var delta = b.clone();
  2929. while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
  2930. for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
  2931. if (i > 0) {
  2932. a.iushrn(i);
  2933. while (i-- > 0) {
  2934. if (x1.isOdd()) {
  2935. x1.iadd(delta);
  2936. }
  2937. x1.iushrn(1);
  2938. }
  2939. }
  2940. for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
  2941. if (j > 0) {
  2942. b.iushrn(j);
  2943. while (j-- > 0) {
  2944. if (x2.isOdd()) {
  2945. x2.iadd(delta);
  2946. }
  2947. x2.iushrn(1);
  2948. }
  2949. }
  2950. if (a.cmp(b) >= 0) {
  2951. a.isub(b);
  2952. x1.isub(x2);
  2953. } else {
  2954. b.isub(a);
  2955. x2.isub(x1);
  2956. }
  2957. }
  2958. var res;
  2959. if (a.cmpn(1) === 0) {
  2960. res = x1;
  2961. } else {
  2962. res = x2;
  2963. }
  2964. if (res.cmpn(0) < 0) {
  2965. res.iadd(p);
  2966. }
  2967. return res;
  2968. };
  2969. BN.prototype.gcd = function gcd (num) {
  2970. if (this.isZero()) return num.abs();
  2971. if (num.isZero()) return this.abs();
  2972. var a = this.clone();
  2973. var b = num.clone();
  2974. a.negative = 0;
  2975. b.negative = 0;
  2976. // Remove common factor of two
  2977. for (var shift = 0; a.isEven() && b.isEven(); shift++) {
  2978. a.iushrn(1);
  2979. b.iushrn(1);
  2980. }
  2981. do {
  2982. while (a.isEven()) {
  2983. a.iushrn(1);
  2984. }
  2985. while (b.isEven()) {
  2986. b.iushrn(1);
  2987. }
  2988. var r = a.cmp(b);
  2989. if (r < 0) {
  2990. // Swap `a` and `b` to make `a` always bigger than `b`
  2991. var t = a;
  2992. a = b;
  2993. b = t;
  2994. } else if (r === 0 || b.cmpn(1) === 0) {
  2995. break;
  2996. }
  2997. a.isub(b);
  2998. } while (true);
  2999. return b.iushln(shift);
  3000. };
  3001. // Invert number in the field F(num)
  3002. BN.prototype.invm = function invm (num) {
  3003. return this.egcd(num).a.umod(num);
  3004. };
  3005. BN.prototype.isEven = function isEven () {
  3006. return (this.words[0] & 1) === 0;
  3007. };
  3008. BN.prototype.isOdd = function isOdd () {
  3009. return (this.words[0] & 1) === 1;
  3010. };
  3011. // And first word and num
  3012. BN.prototype.andln = function andln (num) {
  3013. return this.words[0] & num;
  3014. };
  3015. // Increment at the bit position in-line
  3016. BN.prototype.bincn = function bincn (bit) {
  3017. assert(typeof bit === 'number');
  3018. var r = bit % 26;
  3019. var s = (bit - r) / 26;
  3020. var q = 1 << r;
  3021. // Fast case: bit is much higher than all existing words
  3022. if (this.length <= s) {
  3023. this._expand(s + 1);
  3024. this.words[s] |= q;
  3025. return this;
  3026. }
  3027. // Add bit and propagate, if needed
  3028. var carry = q;
  3029. for (var i = s; carry !== 0 && i < this.length; i++) {
  3030. var w = this.words[i] | 0;
  3031. w += carry;
  3032. carry = w >>> 26;
  3033. w &= 0x3ffffff;
  3034. this.words[i] = w;
  3035. }
  3036. if (carry !== 0) {
  3037. this.words[i] = carry;
  3038. this.length++;
  3039. }
  3040. return this;
  3041. };
  3042. BN.prototype.isZero = function isZero () {
  3043. return this.length === 1 && this.words[0] === 0;
  3044. };
  3045. BN.prototype.cmpn = function cmpn (num) {
  3046. var negative = num < 0;
  3047. if (this.negative !== 0 && !negative) return -1;
  3048. if (this.negative === 0 && negative) return 1;
  3049. this.strip();
  3050. var res;
  3051. if (this.length > 1) {
  3052. res = 1;
  3053. } else {
  3054. if (negative) {
  3055. num = -num;
  3056. }
  3057. assert(num <= 0x3ffffff, 'Number is too big');
  3058. var w = this.words[0] | 0;
  3059. res = w === num ? 0 : w < num ? -1 : 1;
  3060. }
  3061. if (this.negative !== 0) return -res | 0;
  3062. return res;
  3063. };
  3064. // Compare two numbers and return:
  3065. // 1 - if `this` > `num`
  3066. // 0 - if `this` == `num`
  3067. // -1 - if `this` < `num`
  3068. BN.prototype.cmp = function cmp (num) {
  3069. if (this.negative !== 0 && num.negative === 0) return -1;
  3070. if (this.negative === 0 && num.negative !== 0) return 1;
  3071. var res = this.ucmp(num);
  3072. if (this.negative !== 0) return -res | 0;
  3073. return res;
  3074. };
  3075. // Unsigned comparison
  3076. BN.prototype.ucmp = function ucmp (num) {
  3077. // At this point both numbers have the same sign
  3078. if (this.length > num.length) return 1;
  3079. if (this.length < num.length) return -1;
  3080. var res = 0;
  3081. for (var i = this.length - 1; i >= 0; i--) {
  3082. var a = this.words[i] | 0;
  3083. var b = num.words[i] | 0;
  3084. if (a === b) continue;
  3085. if (a < b) {
  3086. res = -1;
  3087. } else if (a > b) {
  3088. res = 1;
  3089. }
  3090. break;
  3091. }
  3092. return res;
  3093. };
  3094. BN.prototype.gtn = function gtn (num) {
  3095. return this.cmpn(num) === 1;
  3096. };
  3097. BN.prototype.gt = function gt (num) {
  3098. return this.cmp(num) === 1;
  3099. };
  3100. BN.prototype.gten = function gten (num) {
  3101. return this.cmpn(num) >= 0;
  3102. };
  3103. BN.prototype.gte = function gte (num) {
  3104. return this.cmp(num) >= 0;
  3105. };
  3106. BN.prototype.ltn = function ltn (num) {
  3107. return this.cmpn(num) === -1;
  3108. };
  3109. BN.prototype.lt = function lt (num) {
  3110. return this.cmp(num) === -1;
  3111. };
  3112. BN.prototype.lten = function lten (num) {
  3113. return this.cmpn(num) <= 0;
  3114. };
  3115. BN.prototype.lte = function lte (num) {
  3116. return this.cmp(num) <= 0;
  3117. };
  3118. BN.prototype.eqn = function eqn (num) {
  3119. return this.cmpn(num) === 0;
  3120. };
  3121. BN.prototype.eq = function eq (num) {
  3122. return this.cmp(num) === 0;
  3123. };
  3124. //
  3125. // A reduce context, could be using montgomery or something better, depending
  3126. // on the `m` itself.
  3127. //
  3128. BN.red = function red (num) {
  3129. return new Red(num);
  3130. };
  3131. BN.prototype.toRed = function toRed (ctx) {
  3132. assert(!this.red, 'Already a number in reduction context');
  3133. assert(this.negative === 0, 'red works only with positives');
  3134. return ctx.convertTo(this)._forceRed(ctx);
  3135. };
  3136. BN.prototype.fromRed = function fromRed () {
  3137. assert(this.red, 'fromRed works only with numbers in reduction context');
  3138. return this.red.convertFrom(this);
  3139. };
  3140. BN.prototype._forceRed = function _forceRed (ctx) {
  3141. this.red = ctx;
  3142. return this;
  3143. };
  3144. BN.prototype.forceRed = function forceRed (ctx) {
  3145. assert(!this.red, 'Already a number in reduction context');
  3146. return this._forceRed(ctx);
  3147. };
  3148. BN.prototype.redAdd = function redAdd (num) {
  3149. assert(this.red, 'redAdd works only with red numbers');
  3150. return this.red.add(this, num);
  3151. };
  3152. BN.prototype.redIAdd = function redIAdd (num) {
  3153. assert(this.red, 'redIAdd works only with red numbers');
  3154. return this.red.iadd(this, num);
  3155. };
  3156. BN.prototype.redSub = function redSub (num) {
  3157. assert(this.red, 'redSub works only with red numbers');
  3158. return this.red.sub(this, num);
  3159. };
  3160. BN.prototype.redISub = function redISub (num) {
  3161. assert(this.red, 'redISub works only with red numbers');
  3162. return this.red.isub(this, num);
  3163. };
  3164. BN.prototype.redShl = function redShl (num) {
  3165. assert(this.red, 'redShl works only with red numbers');
  3166. return this.red.shl(this, num);
  3167. };
  3168. BN.prototype.redMul = function redMul (num) {
  3169. assert(this.red, 'redMul works only with red numbers');
  3170. this.red._verify2(this, num);
  3171. return this.red.mul(this, num);
  3172. };
  3173. BN.prototype.redIMul = function redIMul (num) {
  3174. assert(this.red, 'redMul works only with red numbers');
  3175. this.red._verify2(this, num);
  3176. return this.red.imul(this, num);
  3177. };
  3178. BN.prototype.redSqr = function redSqr () {
  3179. assert(this.red, 'redSqr works only with red numbers');
  3180. this.red._verify1(this);
  3181. return this.red.sqr(this);
  3182. };
  3183. BN.prototype.redISqr = function redISqr () {
  3184. assert(this.red, 'redISqr works only with red numbers');
  3185. this.red._verify1(this);
  3186. return this.red.isqr(this);
  3187. };
  3188. // Square root over p
  3189. BN.prototype.redSqrt = function redSqrt () {
  3190. assert(this.red, 'redSqrt works only with red numbers');
  3191. this.red._verify1(this);
  3192. return this.red.sqrt(this);
  3193. };
  3194. BN.prototype.redInvm = function redInvm () {
  3195. assert(this.red, 'redInvm works only with red numbers');
  3196. this.red._verify1(this);
  3197. return this.red.invm(this);
  3198. };
  3199. // Return negative clone of `this` % `red modulo`
  3200. BN.prototype.redNeg = function redNeg () {
  3201. assert(this.red, 'redNeg works only with red numbers');
  3202. this.red._verify1(this);
  3203. return this.red.neg(this);
  3204. };
  3205. BN.prototype.redPow = function redPow (num) {
  3206. assert(this.red && !num.red, 'redPow(normalNum)');
  3207. this.red._verify1(this);
  3208. return this.red.pow(this, num);
  3209. };
  3210. // Prime numbers with efficient reduction
  3211. var primes = {
  3212. k256: null,
  3213. p224: null,
  3214. p192: null,
  3215. p25519: null
  3216. };
  3217. // Pseudo-Mersenne prime
  3218. function MPrime (name, p) {
  3219. // P = 2 ^ N - K
  3220. this.name = name;
  3221. this.p = new BN(p, 16);
  3222. this.n = this.p.bitLength();
  3223. this.k = new BN(1).iushln(this.n).isub(this.p);
  3224. this.tmp = this._tmp();
  3225. }
  3226. MPrime.prototype._tmp = function _tmp () {
  3227. var tmp = new BN(null);
  3228. tmp.words = new Array(Math.ceil(this.n / 13));
  3229. return tmp;
  3230. };
  3231. MPrime.prototype.ireduce = function ireduce (num) {
  3232. // Assumes that `num` is less than `P^2`
  3233. // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P)
  3234. var r = num;
  3235. var rlen;
  3236. do {
  3237. this.split(r, this.tmp);
  3238. r = this.imulK(r);
  3239. r = r.iadd(this.tmp);
  3240. rlen = r.bitLength();
  3241. } while (rlen > this.n);
  3242. var cmp = rlen < this.n ? -1 : r.ucmp(this.p);
  3243. if (cmp === 0) {
  3244. r.words[0] = 0;
  3245. r.length = 1;
  3246. } else if (cmp > 0) {
  3247. r.isub(this.p);
  3248. } else {
  3249. r.strip();
  3250. }
  3251. return r;
  3252. };
  3253. MPrime.prototype.split = function split (input, out) {
  3254. input.iushrn(this.n, 0, out);
  3255. };
  3256. MPrime.prototype.imulK = function imulK (num) {
  3257. return num.imul(this.k);
  3258. };
  3259. function K256 () {
  3260. MPrime.call(
  3261. this,
  3262. 'k256',
  3263. 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f');
  3264. }
  3265. inherits(K256, MPrime);
  3266. K256.prototype.split = function split (input, output) {
  3267. // 256 = 9 * 26 + 22
  3268. var mask = 0x3fffff;
  3269. var outLen = Math.min(input.length, 9);
  3270. for (var i = 0; i < outLen; i++) {
  3271. output.words[i] = input.words[i];
  3272. }
  3273. output.length = outLen;
  3274. if (input.length <= 9) {
  3275. input.words[0] = 0;
  3276. input.length = 1;
  3277. return;
  3278. }
  3279. // Shift by 9 limbs
  3280. var prev = input.words[9];
  3281. output.words[output.length++] = prev & mask;
  3282. for (i = 10; i < input.length; i++) {
  3283. var next = input.words[i] | 0;
  3284. input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22);
  3285. prev = next;
  3286. }
  3287. prev >>>= 22;
  3288. input.words[i - 10] = prev;
  3289. if (prev === 0 && input.length > 10) {
  3290. input.length -= 10;
  3291. } else {
  3292. input.length -= 9;
  3293. }
  3294. };
  3295. K256.prototype.imulK = function imulK (num) {
  3296. // K = 0x1000003d1 = [ 0x40, 0x3d1 ]
  3297. num.words[num.length] = 0;
  3298. num.words[num.length + 1] = 0;
  3299. num.length += 2;
  3300. // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390
  3301. var lo = 0;
  3302. for (var i = 0; i < num.length; i++) {
  3303. var w = num.words[i] | 0;
  3304. lo += w * 0x3d1;
  3305. num.words[i] = lo & 0x3ffffff;
  3306. lo = w * 0x40 + ((lo / 0x4000000) | 0);
  3307. }
  3308. // Fast length reduction
  3309. if (num.words[num.length - 1] === 0) {
  3310. num.length--;
  3311. if (num.words[num.length - 1] === 0) {
  3312. num.length--;
  3313. }
  3314. }
  3315. return num;
  3316. };
  3317. function P224 () {
  3318. MPrime.call(
  3319. this,
  3320. 'p224',
  3321. 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001');
  3322. }
  3323. inherits(P224, MPrime);
  3324. function P192 () {
  3325. MPrime.call(
  3326. this,
  3327. 'p192',
  3328. 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff');
  3329. }
  3330. inherits(P192, MPrime);
  3331. function P25519 () {
  3332. // 2 ^ 255 - 19
  3333. MPrime.call(
  3334. this,
  3335. '25519',
  3336. '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed');
  3337. }
  3338. inherits(P25519, MPrime);
  3339. P25519.prototype.imulK = function imulK (num) {
  3340. // K = 0x13
  3341. var carry = 0;
  3342. for (var i = 0; i < num.length; i++) {
  3343. var hi = (num.words[i] | 0) * 0x13 + carry;
  3344. var lo = hi & 0x3ffffff;
  3345. hi >>>= 26;
  3346. num.words[i] = lo;
  3347. carry = hi;
  3348. }
  3349. if (carry !== 0) {
  3350. num.words[num.length++] = carry;
  3351. }
  3352. return num;
  3353. };
  3354. // Exported mostly for testing purposes, use plain name instead
  3355. BN._prime = function prime (name) {
  3356. // Cached version of prime
  3357. if (primes[name]) return primes[name];
  3358. var prime;
  3359. if (name === 'k256') {
  3360. prime = new K256();
  3361. } else if (name === 'p224') {
  3362. prime = new P224();
  3363. } else if (name === 'p192') {
  3364. prime = new P192();
  3365. } else if (name === 'p25519') {
  3366. prime = new P25519();
  3367. } else {
  3368. throw new Error('Unknown prime ' + name);
  3369. }
  3370. primes[name] = prime;
  3371. return prime;
  3372. };
  3373. //
  3374. // Base reduction engine
  3375. //
  3376. function Red (m) {
  3377. if (typeof m === 'string') {
  3378. var prime = BN._prime(m);
  3379. this.m = prime.p;
  3380. this.prime = prime;
  3381. } else {
  3382. assert(m.gtn(1), 'modulus must be greater than 1');
  3383. this.m = m;
  3384. this.prime = null;
  3385. }
  3386. }
  3387. Red.prototype._verify1 = function _verify1 (a) {
  3388. assert(a.negative === 0, 'red works only with positives');
  3389. assert(a.red, 'red works only with red numbers');
  3390. };
  3391. Red.prototype._verify2 = function _verify2 (a, b) {
  3392. assert((a.negative | b.negative) === 0, 'red works only with positives');
  3393. assert(a.red && a.red === b.red,
  3394. 'red works only with red numbers');
  3395. };
  3396. Red.prototype.imod = function imod (a) {
  3397. if (this.prime) return this.prime.ireduce(a)._forceRed(this);
  3398. return a.umod(this.m)._forceRed(this);
  3399. };
  3400. Red.prototype.neg = function neg (a) {
  3401. if (a.isZero()) {
  3402. return a.clone();
  3403. }
  3404. return this.m.sub(a)._forceRed(this);
  3405. };
  3406. Red.prototype.add = function add (a, b) {
  3407. this._verify2(a, b);
  3408. var res = a.add(b);
  3409. if (res.cmp(this.m) >= 0) {
  3410. res.isub(this.m);
  3411. }
  3412. return res._forceRed(this);
  3413. };
  3414. Red.prototype.iadd = function iadd (a, b) {
  3415. this._verify2(a, b);
  3416. var res = a.iadd(b);
  3417. if (res.cmp(this.m) >= 0) {
  3418. res.isub(this.m);
  3419. }
  3420. return res;
  3421. };
  3422. Red.prototype.sub = function sub (a, b) {
  3423. this._verify2(a, b);
  3424. var res = a.sub(b);
  3425. if (res.cmpn(0) < 0) {
  3426. res.iadd(this.m);
  3427. }
  3428. return res._forceRed(this);
  3429. };
  3430. Red.prototype.isub = function isub (a, b) {
  3431. this._verify2(a, b);
  3432. var res = a.isub(b);
  3433. if (res.cmpn(0) < 0) {
  3434. res.iadd(this.m);
  3435. }
  3436. return res;
  3437. };
  3438. Red.prototype.shl = function shl (a, num) {
  3439. this._verify1(a);
  3440. return this.imod(a.ushln(num));
  3441. };
  3442. Red.prototype.imul = function imul (a, b) {
  3443. this._verify2(a, b);
  3444. return this.imod(a.imul(b));
  3445. };
  3446. Red.prototype.mul = function mul (a, b) {
  3447. this._verify2(a, b);
  3448. return this.imod(a.mul(b));
  3449. };
  3450. Red.prototype.isqr = function isqr (a) {
  3451. return this.imul(a, a.clone());
  3452. };
  3453. Red.prototype.sqr = function sqr (a) {
  3454. return this.mul(a, a);
  3455. };
  3456. Red.prototype.sqrt = function sqrt (a) {
  3457. if (a.isZero()) return a.clone();
  3458. var mod3 = this.m.andln(3);
  3459. assert(mod3 % 2 === 1);
  3460. // Fast case
  3461. if (mod3 === 3) {
  3462. var pow = this.m.add(new BN(1)).iushrn(2);
  3463. return this.pow(a, pow);
  3464. }
  3465. // Tonelli-Shanks algorithm (Totally unoptimized and slow)
  3466. //
  3467. // Find Q and S, that Q * 2 ^ S = (P - 1)
  3468. var q = this.m.subn(1);
  3469. var s = 0;
  3470. while (!q.isZero() && q.andln(1) === 0) {
  3471. s++;
  3472. q.iushrn(1);
  3473. }
  3474. assert(!q.isZero());
  3475. var one = new BN(1).toRed(this);
  3476. var nOne = one.redNeg();
  3477. // Find quadratic non-residue
  3478. // NOTE: Max is such because of generalized Riemann hypothesis.
  3479. var lpow = this.m.subn(1).iushrn(1);
  3480. var z = this.m.bitLength();
  3481. z = new BN(2 * z * z).toRed(this);
  3482. while (this.pow(z, lpow).cmp(nOne) !== 0) {
  3483. z.redIAdd(nOne);
  3484. }
  3485. var c = this.pow(z, q);
  3486. var r = this.pow(a, q.addn(1).iushrn(1));
  3487. var t = this.pow(a, q);
  3488. var m = s;
  3489. while (t.cmp(one) !== 0) {
  3490. var tmp = t;
  3491. for (var i = 0; tmp.cmp(one) !== 0; i++) {
  3492. tmp = tmp.redSqr();
  3493. }
  3494. assert(i < m);
  3495. var b = this.pow(c, new BN(1).iushln(m - i - 1));
  3496. r = r.redMul(b);
  3497. c = b.redSqr();
  3498. t = t.redMul(c);
  3499. m = i;
  3500. }
  3501. return r;
  3502. };
  3503. Red.prototype.invm = function invm (a) {
  3504. var inv = a._invmp(this.m);
  3505. if (inv.negative !== 0) {
  3506. inv.negative = 0;
  3507. return this.imod(inv).redNeg();
  3508. } else {
  3509. return this.imod(inv);
  3510. }
  3511. };
  3512. Red.prototype.pow = function pow (a, num) {
  3513. if (num.isZero()) return new BN(1);
  3514. if (num.cmpn(1) === 0) return a.clone();
  3515. var windowSize = 4;
  3516. var wnd = new Array(1 << windowSize);
  3517. wnd[0] = new BN(1).toRed(this);
  3518. wnd[1] = a;
  3519. for (var i = 2; i < wnd.length; i++) {
  3520. wnd[i] = this.mul(wnd[i - 1], a);
  3521. }
  3522. var res = wnd[0];
  3523. var current = 0;
  3524. var currentLen = 0;
  3525. var start = num.bitLength() % 26;
  3526. if (start === 0) {
  3527. start = 26;
  3528. }
  3529. for (i = num.length - 1; i >= 0; i--) {
  3530. var word = num.words[i];
  3531. for (var j = start - 1; j >= 0; j--) {
  3532. var bit = (word >> j) & 1;
  3533. if (res !== wnd[0]) {
  3534. res = this.sqr(res);
  3535. }
  3536. if (bit === 0 && current === 0) {
  3537. currentLen = 0;
  3538. continue;
  3539. }
  3540. current <<= 1;
  3541. current |= bit;
  3542. currentLen++;
  3543. if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue;
  3544. res = this.mul(res, wnd[current]);
  3545. currentLen = 0;
  3546. current = 0;
  3547. }
  3548. start = 26;
  3549. }
  3550. return res;
  3551. };
  3552. Red.prototype.convertTo = function convertTo (num) {
  3553. var r = num.umod(this.m);
  3554. return r === num ? r.clone() : r;
  3555. };
  3556. Red.prototype.convertFrom = function convertFrom (num) {
  3557. var res = num.clone();
  3558. res.red = null;
  3559. return res;
  3560. };
  3561. //
  3562. // Montgomery method engine
  3563. //
  3564. BN.mont = function mont (num) {
  3565. return new Mont(num);
  3566. };
  3567. function Mont (m) {
  3568. Red.call(this, m);
  3569. this.shift = this.m.bitLength();
  3570. if (this.shift % 26 !== 0) {
  3571. this.shift += 26 - (this.shift % 26);
  3572. }
  3573. this.r = new BN(1).iushln(this.shift);
  3574. this.r2 = this.imod(this.r.sqr());
  3575. this.rinv = this.r._invmp(this.m);
  3576. this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
  3577. this.minv = this.minv.umod(this.r);
  3578. this.minv = this.r.sub(this.minv);
  3579. }
  3580. inherits(Mont, Red);
  3581. Mont.prototype.convertTo = function convertTo (num) {
  3582. return this.imod(num.ushln(this.shift));
  3583. };
  3584. Mont.prototype.convertFrom = function convertFrom (num) {
  3585. var r = this.imod(num.mul(this.rinv));
  3586. r.red = null;
  3587. return r;
  3588. };
  3589. Mont.prototype.imul = function imul (a, b) {
  3590. if (a.isZero() || b.isZero()) {
  3591. a.words[0] = 0;
  3592. a.length = 1;
  3593. return a;
  3594. }
  3595. var t = a.imul(b);
  3596. var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
  3597. var u = t.isub(c).iushrn(this.shift);
  3598. var res = u;
  3599. if (u.cmp(this.m) >= 0) {
  3600. res = u.isub(this.m);
  3601. } else if (u.cmpn(0) < 0) {
  3602. res = u.iadd(this.m);
  3603. }
  3604. return res._forceRed(this);
  3605. };
  3606. Mont.prototype.mul = function mul (a, b) {
  3607. if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this);
  3608. var t = a.mul(b);
  3609. var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
  3610. var u = t.isub(c).iushrn(this.shift);
  3611. var res = u;
  3612. if (u.cmp(this.m) >= 0) {
  3613. res = u.isub(this.m);
  3614. } else if (u.cmpn(0) < 0) {
  3615. res = u.iadd(this.m);
  3616. }
  3617. return res._forceRed(this);
  3618. };
  3619. Mont.prototype.invm = function invm (a) {
  3620. // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R
  3621. var res = this.imod(a._invmp(this.m).mul(this.r2));
  3622. return res._forceRed(this);
  3623. };
  3624. })(typeof module === 'undefined' || module, this);
  3625. },{}],5:[function(require,module,exports){
  3626. var r;
  3627. module.exports = function rand(len) {
  3628. if (!r)
  3629. r = new Rand(null);
  3630. return r.generate(len);
  3631. };
  3632. function Rand(rand) {
  3633. this.rand = rand;
  3634. }
  3635. module.exports.Rand = Rand;
  3636. Rand.prototype.generate = function generate(len) {
  3637. return this._rand(len);
  3638. };
  3639. // Emulate crypto API using randy
  3640. Rand.prototype._rand = function _rand(n) {
  3641. if (this.rand.getBytes)
  3642. return this.rand.getBytes(n);
  3643. var res = new Uint8Array(n);
  3644. for (var i = 0; i < res.length; i++)
  3645. res[i] = this.rand.getByte();
  3646. return res;
  3647. };
  3648. if (typeof self === 'object') {
  3649. if (self.crypto && self.crypto.getRandomValues) {
  3650. // Modern browsers
  3651. Rand.prototype._rand = function _rand(n) {
  3652. var arr = new Uint8Array(n);
  3653. self.crypto.getRandomValues(arr);
  3654. return arr;
  3655. };
  3656. } else if (self.msCrypto && self.msCrypto.getRandomValues) {
  3657. // IE
  3658. Rand.prototype._rand = function _rand(n) {
  3659. var arr = new Uint8Array(n);
  3660. self.msCrypto.getRandomValues(arr);
  3661. return arr;
  3662. };
  3663. // Safari's WebWorkers do not have `crypto`
  3664. } else if (typeof window === 'object') {
  3665. // Old junk
  3666. Rand.prototype._rand = function() {
  3667. throw new Error('Not implemented yet');
  3668. };
  3669. }
  3670. } else {
  3671. // Node.js or Web worker with no crypto support
  3672. try {
  3673. var crypto = require('crypto');
  3674. if (typeof crypto.randomBytes !== 'function')
  3675. throw new Error('Not supported');
  3676. Rand.prototype._rand = function _rand(n) {
  3677. return crypto.randomBytes(n);
  3678. };
  3679. } catch (e) {
  3680. }
  3681. }
  3682. },{"crypto":63}],6:[function(require,module,exports){
  3683. (function (Buffer){
  3684. var Transform = require('stream').Transform
  3685. var inherits = require('inherits')
  3686. var StringDecoder = require('string_decoder').StringDecoder
  3687. module.exports = CipherBase
  3688. inherits(CipherBase, Transform)
  3689. function CipherBase (hashMode) {
  3690. Transform.call(this)
  3691. this.hashMode = typeof hashMode === 'string'
  3692. if (this.hashMode) {
  3693. this[hashMode] = this._finalOrDigest
  3694. } else {
  3695. this.final = this._finalOrDigest
  3696. }
  3697. this._decoder = null
  3698. this._encoding = null
  3699. }
  3700. CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
  3701. if (typeof data === 'string') {
  3702. data = new Buffer(data, inputEnc)
  3703. }
  3704. var outData = this._update(data)
  3705. if (this.hashMode) {
  3706. return this
  3707. }
  3708. if (outputEnc) {
  3709. outData = this._toString(outData, outputEnc)
  3710. }
  3711. return outData
  3712. }
  3713. CipherBase.prototype.setAutoPadding = function () {}
  3714. CipherBase.prototype.getAuthTag = function () {
  3715. throw new Error('trying to get auth tag in unsupported state')
  3716. }
  3717. CipherBase.prototype.setAuthTag = function () {
  3718. throw new Error('trying to set auth tag in unsupported state')
  3719. }
  3720. CipherBase.prototype.setAAD = function () {
  3721. throw new Error('trying to set aad in unsupported state')
  3722. }
  3723. CipherBase.prototype._transform = function (data, _, next) {
  3724. var err
  3725. try {
  3726. if (this.hashMode) {
  3727. this._update(data)
  3728. } else {
  3729. this.push(this._update(data))
  3730. }
  3731. } catch (e) {
  3732. err = e
  3733. } finally {
  3734. next(err)
  3735. }
  3736. }
  3737. CipherBase.prototype._flush = function (done) {
  3738. var err
  3739. try {
  3740. this.push(this._final())
  3741. } catch (e) {
  3742. err = e
  3743. } finally {
  3744. done(err)
  3745. }
  3746. }
  3747. CipherBase.prototype._finalOrDigest = function (outputEnc) {
  3748. var outData = this._final() || new Buffer('')
  3749. if (outputEnc) {
  3750. outData = this._toString(outData, outputEnc, true)
  3751. }
  3752. return outData
  3753. }
  3754. CipherBase.prototype._toString = function (value, enc, fin) {
  3755. if (!this._decoder) {
  3756. this._decoder = new StringDecoder(enc)
  3757. this._encoding = enc
  3758. }
  3759. if (this._encoding !== enc) {
  3760. throw new Error('can\'t switch encodings')
  3761. }
  3762. var out = this._decoder.write(value)
  3763. if (fin) {
  3764. out += this._decoder.end()
  3765. }
  3766. return out
  3767. }
  3768. }).call(this,require("buffer").Buffer)
  3769. },{"buffer":65,"inherits":34,"stream":85,"string_decoder":86}],7:[function(require,module,exports){
  3770. (function (Buffer){
  3771. 'use strict';
  3772. var inherits = require('inherits')
  3773. var md5 = require('./md5')
  3774. var rmd160 = require('ripemd160')
  3775. var sha = require('sha.js')
  3776. var Base = require('cipher-base')
  3777. function HashNoConstructor(hash) {
  3778. Base.call(this, 'digest')
  3779. this._hash = hash
  3780. this.buffers = []
  3781. }
  3782. inherits(HashNoConstructor, Base)
  3783. HashNoConstructor.prototype._update = function (data) {
  3784. this.buffers.push(data)
  3785. }
  3786. HashNoConstructor.prototype._final = function () {
  3787. var buf = Buffer.concat(this.buffers)
  3788. var r = this._hash(buf)
  3789. this.buffers = null
  3790. return r
  3791. }
  3792. function Hash(hash) {
  3793. Base.call(this, 'digest')
  3794. this._hash = hash
  3795. }
  3796. inherits(Hash, Base)
  3797. Hash.prototype._update = function (data) {
  3798. this._hash.update(data)
  3799. }
  3800. Hash.prototype._final = function () {
  3801. return this._hash.digest()
  3802. }
  3803. module.exports = function createHash (alg) {
  3804. alg = alg.toLowerCase()
  3805. if ('md5' === alg) return new HashNoConstructor(md5)
  3806. if ('rmd160' === alg || 'ripemd160' === alg) return new HashNoConstructor(rmd160)
  3807. return new Hash(sha(alg))
  3808. }
  3809. }).call(this,require("buffer").Buffer)
  3810. },{"./md5":9,"buffer":65,"cipher-base":6,"inherits":34,"ripemd160":44,"sha.js":53}],8:[function(require,module,exports){
  3811. (function (Buffer){
  3812. 'use strict';
  3813. var intSize = 4;
  3814. var zeroBuffer = new Buffer(intSize); zeroBuffer.fill(0);
  3815. var chrsz = 8;
  3816. function toArray(buf, bigEndian) {
  3817. if ((buf.length % intSize) !== 0) {
  3818. var len = buf.length + (intSize - (buf.length % intSize));
  3819. buf = Buffer.concat([buf, zeroBuffer], len);
  3820. }
  3821. var arr = [];
  3822. var fn = bigEndian ? buf.readInt32BE : buf.readInt32LE;
  3823. for (var i = 0; i < buf.length; i += intSize) {
  3824. arr.push(fn.call(buf, i));
  3825. }
  3826. return arr;
  3827. }
  3828. function toBuffer(arr, size, bigEndian) {
  3829. var buf = new Buffer(size);
  3830. var fn = bigEndian ? buf.writeInt32BE : buf.writeInt32LE;
  3831. for (var i = 0; i < arr.length; i++) {
  3832. fn.call(buf, arr[i], i * 4, true);
  3833. }
  3834. return buf;
  3835. }
  3836. function hash(buf, fn, hashSize, bigEndian) {
  3837. if (!Buffer.isBuffer(buf)) buf = new Buffer(buf);
  3838. var arr = fn(toArray(buf, bigEndian), buf.length * chrsz);
  3839. return toBuffer(arr, hashSize, bigEndian);
  3840. }
  3841. exports.hash = hash;
  3842. }).call(this,require("buffer").Buffer)
  3843. },{"buffer":65}],9:[function(require,module,exports){
  3844. 'use strict';
  3845. /*
  3846. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  3847. * Digest Algorithm, as defined in RFC 1321.
  3848. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
  3849. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  3850. * Distributed under the BSD License
  3851. * See http://pajhome.org.uk/crypt/md5 for more info.
  3852. */
  3853. var helpers = require('./helpers');
  3854. /*
  3855. * Calculate the MD5 of an array of little-endian words, and a bit length
  3856. */
  3857. function core_md5(x, len)
  3858. {
  3859. /* append padding */
  3860. x[len >> 5] |= 0x80 << ((len) % 32);
  3861. x[(((len + 64) >>> 9) << 4) + 14] = len;
  3862. var a = 1732584193;
  3863. var b = -271733879;
  3864. var c = -1732584194;
  3865. var d = 271733878;
  3866. for(var i = 0; i < x.length; i += 16)
  3867. {
  3868. var olda = a;
  3869. var oldb = b;
  3870. var oldc = c;
  3871. var oldd = d;
  3872. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  3873. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  3874. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  3875. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  3876. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  3877. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  3878. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  3879. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  3880. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  3881. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  3882. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  3883. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  3884. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  3885. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  3886. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  3887. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  3888. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  3889. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  3890. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  3891. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  3892. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  3893. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  3894. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  3895. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  3896. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  3897. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  3898. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  3899. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  3900. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  3901. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  3902. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  3903. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  3904. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  3905. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  3906. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  3907. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  3908. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  3909. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  3910. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  3911. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  3912. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  3913. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  3914. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  3915. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  3916. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  3917. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  3918. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  3919. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  3920. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  3921. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  3922. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  3923. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  3924. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  3925. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  3926. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  3927. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  3928. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  3929. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  3930. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  3931. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  3932. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  3933. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  3934. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  3935. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  3936. a = safe_add(a, olda);
  3937. b = safe_add(b, oldb);
  3938. c = safe_add(c, oldc);
  3939. d = safe_add(d, oldd);
  3940. }
  3941. return Array(a, b, c, d);
  3942. }
  3943. /*
  3944. * These functions implement the four basic operations the algorithm uses.
  3945. */
  3946. function md5_cmn(q, a, b, x, s, t)
  3947. {
  3948. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  3949. }
  3950. function md5_ff(a, b, c, d, x, s, t)
  3951. {
  3952. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  3953. }
  3954. function md5_gg(a, b, c, d, x, s, t)
  3955. {
  3956. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  3957. }
  3958. function md5_hh(a, b, c, d, x, s, t)
  3959. {
  3960. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  3961. }
  3962. function md5_ii(a, b, c, d, x, s, t)
  3963. {
  3964. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  3965. }
  3966. /*
  3967. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  3968. * to work around bugs in some JS interpreters.
  3969. */
  3970. function safe_add(x, y)
  3971. {
  3972. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  3973. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  3974. return (msw << 16) | (lsw & 0xFFFF);
  3975. }
  3976. /*
  3977. * Bitwise rotate a 32-bit number to the left.
  3978. */
  3979. function bit_rol(num, cnt)
  3980. {
  3981. return (num << cnt) | (num >>> (32 - cnt));
  3982. }
  3983. module.exports = function md5(buf) {
  3984. return helpers.hash(buf, core_md5, 16);
  3985. };
  3986. },{"./helpers":8}],10:[function(require,module,exports){
  3987. 'use strict';
  3988. var elliptic = exports;
  3989. elliptic.version = require('../package.json').version;
  3990. elliptic.utils = require('./elliptic/utils');
  3991. elliptic.rand = require('brorand');
  3992. elliptic.curve = require('./elliptic/curve');
  3993. elliptic.curves = require('./elliptic/curves');
  3994. // Protocols
  3995. elliptic.ec = require('./elliptic/ec');
  3996. elliptic.eddsa = require('./elliptic/eddsa');
  3997. },{"../package.json":25,"./elliptic/curve":13,"./elliptic/curves":16,"./elliptic/ec":17,"./elliptic/eddsa":20,"./elliptic/utils":24,"brorand":5}],11:[function(require,module,exports){
  3998. 'use strict';
  3999. var BN = require('bn.js');
  4000. var elliptic = require('../../elliptic');
  4001. var utils = elliptic.utils;
  4002. var getNAF = utils.getNAF;
  4003. var getJSF = utils.getJSF;
  4004. var assert = utils.assert;
  4005. function BaseCurve(type, conf) {
  4006. this.type = type;
  4007. this.p = new BN(conf.p, 16);
  4008. // Use Montgomery, when there is no fast reduction for the prime
  4009. this.red = conf.prime ? BN.red(conf.prime) : BN.mont(this.p);
  4010. // Useful for many curves
  4011. this.zero = new BN(0).toRed(this.red);
  4012. this.one = new BN(1).toRed(this.red);
  4013. this.two = new BN(2).toRed(this.red);
  4014. // Curve configuration, optional
  4015. this.n = conf.n && new BN(conf.n, 16);
  4016. this.g = conf.g && this.pointFromJSON(conf.g, conf.gRed);
  4017. // Temporary arrays
  4018. this._wnafT1 = new Array(4);
  4019. this._wnafT2 = new Array(4);
  4020. this._wnafT3 = new Array(4);
  4021. this._wnafT4 = new Array(4);
  4022. // Generalized Greg Maxwell's trick
  4023. var adjustCount = this.n && this.p.div(this.n);
  4024. if (!adjustCount || adjustCount.cmpn(100) > 0) {
  4025. this.redN = null;
  4026. } else {
  4027. this._maxwellTrick = true;
  4028. this.redN = this.n.toRed(this.red);
  4029. }
  4030. }
  4031. module.exports = BaseCurve;
  4032. BaseCurve.prototype.point = function point() {
  4033. throw new Error('Not implemented');
  4034. };
  4035. BaseCurve.prototype.validate = function validate() {
  4036. throw new Error('Not implemented');
  4037. };
  4038. BaseCurve.prototype._fixedNafMul = function _fixedNafMul(p, k) {
  4039. assert(p.precomputed);
  4040. var doubles = p._getDoubles();
  4041. var naf = getNAF(k, 1);
  4042. var I = (1 << (doubles.step + 1)) - (doubles.step % 2 === 0 ? 2 : 1);
  4043. I /= 3;
  4044. // Translate into more windowed form
  4045. var repr = [];
  4046. for (var j = 0; j < naf.length; j += doubles.step) {
  4047. var nafW = 0;
  4048. for (var k = j + doubles.step - 1; k >= j; k--)
  4049. nafW = (nafW << 1) + naf[k];
  4050. repr.push(nafW);
  4051. }
  4052. var a = this.jpoint(null, null, null);
  4053. var b = this.jpoint(null, null, null);
  4054. for (var i = I; i > 0; i--) {
  4055. for (var j = 0; j < repr.length; j++) {
  4056. var nafW = repr[j];
  4057. if (nafW === i)
  4058. b = b.mixedAdd(doubles.points[j]);
  4059. else if (nafW === -i)
  4060. b = b.mixedAdd(doubles.points[j].neg());
  4061. }
  4062. a = a.add(b);
  4063. }
  4064. return a.toP();
  4065. };
  4066. BaseCurve.prototype._wnafMul = function _wnafMul(p, k) {
  4067. var w = 4;
  4068. // Precompute window
  4069. var nafPoints = p._getNAFPoints(w);
  4070. w = nafPoints.wnd;
  4071. var wnd = nafPoints.points;
  4072. // Get NAF form
  4073. var naf = getNAF(k, w);
  4074. // Add `this`*(N+1) for every w-NAF index
  4075. var acc = this.jpoint(null, null, null);
  4076. for (var i = naf.length - 1; i >= 0; i--) {
  4077. // Count zeroes
  4078. for (var k = 0; i >= 0 && naf[i] === 0; i--)
  4079. k++;
  4080. if (i >= 0)
  4081. k++;
  4082. acc = acc.dblp(k);
  4083. if (i < 0)
  4084. break;
  4085. var z = naf[i];
  4086. assert(z !== 0);
  4087. if (p.type === 'affine') {
  4088. // J +- P
  4089. if (z > 0)
  4090. acc = acc.mixedAdd(wnd[(z - 1) >> 1]);
  4091. else
  4092. acc = acc.mixedAdd(wnd[(-z - 1) >> 1].neg());
  4093. } else {
  4094. // J +- J
  4095. if (z > 0)
  4096. acc = acc.add(wnd[(z - 1) >> 1]);
  4097. else
  4098. acc = acc.add(wnd[(-z - 1) >> 1].neg());
  4099. }
  4100. }
  4101. return p.type === 'affine' ? acc.toP() : acc;
  4102. };
  4103. BaseCurve.prototype._wnafMulAdd = function _wnafMulAdd(defW,
  4104. points,
  4105. coeffs,
  4106. len,
  4107. jacobianResult) {
  4108. var wndWidth = this._wnafT1;
  4109. var wnd = this._wnafT2;
  4110. var naf = this._wnafT3;
  4111. // Fill all arrays
  4112. var max = 0;
  4113. for (var i = 0; i < len; i++) {
  4114. var p = points[i];
  4115. var nafPoints = p._getNAFPoints(defW);
  4116. wndWidth[i] = nafPoints.wnd;
  4117. wnd[i] = nafPoints.points;
  4118. }
  4119. // Comb small window NAFs
  4120. for (var i = len - 1; i >= 1; i -= 2) {
  4121. var a = i - 1;
  4122. var b = i;
  4123. if (wndWidth[a] !== 1 || wndWidth[b] !== 1) {
  4124. naf[a] = getNAF(coeffs[a], wndWidth[a]);
  4125. naf[b] = getNAF(coeffs[b], wndWidth[b]);
  4126. max = Math.max(naf[a].length, max);
  4127. max = Math.max(naf[b].length, max);
  4128. continue;
  4129. }
  4130. var comb = [
  4131. points[a], /* 1 */
  4132. null, /* 3 */
  4133. null, /* 5 */
  4134. points[b] /* 7 */
  4135. ];
  4136. // Try to avoid Projective points, if possible
  4137. if (points[a].y.cmp(points[b].y) === 0) {
  4138. comb[1] = points[a].add(points[b]);
  4139. comb[2] = points[a].toJ().mixedAdd(points[b].neg());
  4140. } else if (points[a].y.cmp(points[b].y.redNeg()) === 0) {
  4141. comb[1] = points[a].toJ().mixedAdd(points[b]);
  4142. comb[2] = points[a].add(points[b].neg());
  4143. } else {
  4144. comb[1] = points[a].toJ().mixedAdd(points[b]);
  4145. comb[2] = points[a].toJ().mixedAdd(points[b].neg());
  4146. }
  4147. var index = [
  4148. -3, /* -1 -1 */
  4149. -1, /* -1 0 */
  4150. -5, /* -1 1 */
  4151. -7, /* 0 -1 */
  4152. 0, /* 0 0 */
  4153. 7, /* 0 1 */
  4154. 5, /* 1 -1 */
  4155. 1, /* 1 0 */
  4156. 3 /* 1 1 */
  4157. ];
  4158. var jsf = getJSF(coeffs[a], coeffs[b]);
  4159. max = Math.max(jsf[0].length, max);
  4160. naf[a] = new Array(max);
  4161. naf[b] = new Array(max);
  4162. for (var j = 0; j < max; j++) {
  4163. var ja = jsf[0][j] | 0;
  4164. var jb = jsf[1][j] | 0;
  4165. naf[a][j] = index[(ja + 1) * 3 + (jb + 1)];
  4166. naf[b][j] = 0;
  4167. wnd[a] = comb;
  4168. }
  4169. }
  4170. var acc = this.jpoint(null, null, null);
  4171. var tmp = this._wnafT4;
  4172. for (var i = max; i >= 0; i--) {
  4173. var k = 0;
  4174. while (i >= 0) {
  4175. var zero = true;
  4176. for (var j = 0; j < len; j++) {
  4177. tmp[j] = naf[j][i] | 0;
  4178. if (tmp[j] !== 0)
  4179. zero = false;
  4180. }
  4181. if (!zero)
  4182. break;
  4183. k++;
  4184. i--;
  4185. }
  4186. if (i >= 0)
  4187. k++;
  4188. acc = acc.dblp(k);
  4189. if (i < 0)
  4190. break;
  4191. for (var j = 0; j < len; j++) {
  4192. var z = tmp[j];
  4193. var p;
  4194. if (z === 0)
  4195. continue;
  4196. else if (z > 0)
  4197. p = wnd[j][(z - 1) >> 1];
  4198. else if (z < 0)
  4199. p = wnd[j][(-z - 1) >> 1].neg();
  4200. if (p.type === 'affine')
  4201. acc = acc.mixedAdd(p);
  4202. else
  4203. acc = acc.add(p);
  4204. }
  4205. }
  4206. // Zeroify references
  4207. for (var i = 0; i < len; i++)
  4208. wnd[i] = null;
  4209. if (jacobianResult)
  4210. return acc;
  4211. else
  4212. return acc.toP();
  4213. };
  4214. function BasePoint(curve, type) {
  4215. this.curve = curve;
  4216. this.type = type;
  4217. this.precomputed = null;
  4218. }
  4219. BaseCurve.BasePoint = BasePoint;
  4220. BasePoint.prototype.eq = function eq(/*other*/) {
  4221. throw new Error('Not implemented');
  4222. };
  4223. BasePoint.prototype.validate = function validate() {
  4224. return this.curve.validate(this);
  4225. };
  4226. BaseCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
  4227. bytes = utils.toArray(bytes, enc);
  4228. var len = this.p.byteLength();
  4229. // uncompressed, hybrid-odd, hybrid-even
  4230. if ((bytes[0] === 0x04 || bytes[0] === 0x06 || bytes[0] === 0x07) &&
  4231. bytes.length - 1 === 2 * len) {
  4232. if (bytes[0] === 0x06)
  4233. assert(bytes[bytes.length - 1] % 2 === 0);
  4234. else if (bytes[0] === 0x07)
  4235. assert(bytes[bytes.length - 1] % 2 === 1);
  4236. var res = this.point(bytes.slice(1, 1 + len),
  4237. bytes.slice(1 + len, 1 + 2 * len));
  4238. return res;
  4239. } else if ((bytes[0] === 0x02 || bytes[0] === 0x03) &&
  4240. bytes.length - 1 === len) {
  4241. return this.pointFromX(bytes.slice(1, 1 + len), bytes[0] === 0x03);
  4242. }
  4243. throw new Error('Unknown point format');
  4244. };
  4245. BasePoint.prototype.encodeCompressed = function encodeCompressed(enc) {
  4246. return this.encode(enc, true);
  4247. };
  4248. BasePoint.prototype._encode = function _encode(compact) {
  4249. var len = this.curve.p.byteLength();
  4250. var x = this.getX().toArray('be', len);
  4251. if (compact)
  4252. return [ this.getY().isEven() ? 0x02 : 0x03 ].concat(x);
  4253. return [ 0x04 ].concat(x, this.getY().toArray('be', len)) ;
  4254. };
  4255. BasePoint.prototype.encode = function encode(enc, compact) {
  4256. return utils.encode(this._encode(compact), enc);
  4257. };
  4258. BasePoint.prototype.precompute = function precompute(power) {
  4259. if (this.precomputed)
  4260. return this;
  4261. var precomputed = {
  4262. doubles: null,
  4263. naf: null,
  4264. beta: null
  4265. };
  4266. precomputed.naf = this._getNAFPoints(8);
  4267. precomputed.doubles = this._getDoubles(4, power);
  4268. precomputed.beta = this._getBeta();
  4269. this.precomputed = precomputed;
  4270. return this;
  4271. };
  4272. BasePoint.prototype._hasDoubles = function _hasDoubles(k) {
  4273. if (!this.precomputed)
  4274. return false;
  4275. var doubles = this.precomputed.doubles;
  4276. if (!doubles)
  4277. return false;
  4278. return doubles.points.length >= Math.ceil((k.bitLength() + 1) / doubles.step);
  4279. };
  4280. BasePoint.prototype._getDoubles = function _getDoubles(step, power) {
  4281. if (this.precomputed && this.precomputed.doubles)
  4282. return this.precomputed.doubles;
  4283. var doubles = [ this ];
  4284. var acc = this;
  4285. for (var i = 0; i < power; i += step) {
  4286. for (var j = 0; j < step; j++)
  4287. acc = acc.dbl();
  4288. doubles.push(acc);
  4289. }
  4290. return {
  4291. step: step,
  4292. points: doubles
  4293. };
  4294. };
  4295. BasePoint.prototype._getNAFPoints = function _getNAFPoints(wnd) {
  4296. if (this.precomputed && this.precomputed.naf)
  4297. return this.precomputed.naf;
  4298. var res = [ this ];
  4299. var max = (1 << wnd) - 1;
  4300. var dbl = max === 1 ? null : this.dbl();
  4301. for (var i = 1; i < max; i++)
  4302. res[i] = res[i - 1].add(dbl);
  4303. return {
  4304. wnd: wnd,
  4305. points: res
  4306. };
  4307. };
  4308. BasePoint.prototype._getBeta = function _getBeta() {
  4309. return null;
  4310. };
  4311. BasePoint.prototype.dblp = function dblp(k) {
  4312. var r = this;
  4313. for (var i = 0; i < k; i++)
  4314. r = r.dbl();
  4315. return r;
  4316. };
  4317. },{"../../elliptic":10,"bn.js":4}],12:[function(require,module,exports){
  4318. 'use strict';
  4319. var curve = require('../curve');
  4320. var elliptic = require('../../elliptic');
  4321. var BN = require('bn.js');
  4322. var inherits = require('inherits');
  4323. var Base = curve.base;
  4324. var assert = elliptic.utils.assert;
  4325. function EdwardsCurve(conf) {
  4326. // NOTE: Important as we are creating point in Base.call()
  4327. this.twisted = (conf.a | 0) !== 1;
  4328. this.mOneA = this.twisted && (conf.a | 0) === -1;
  4329. this.extended = this.mOneA;
  4330. Base.call(this, 'edwards', conf);
  4331. this.a = new BN(conf.a, 16).umod(this.red.m);
  4332. this.a = this.a.toRed(this.red);
  4333. this.c = new BN(conf.c, 16).toRed(this.red);
  4334. this.c2 = this.c.redSqr();
  4335. this.d = new BN(conf.d, 16).toRed(this.red);
  4336. this.dd = this.d.redAdd(this.d);
  4337. assert(!this.twisted || this.c.fromRed().cmpn(1) === 0);
  4338. this.oneC = (conf.c | 0) === 1;
  4339. }
  4340. inherits(EdwardsCurve, Base);
  4341. module.exports = EdwardsCurve;
  4342. EdwardsCurve.prototype._mulA = function _mulA(num) {
  4343. if (this.mOneA)
  4344. return num.redNeg();
  4345. else
  4346. return this.a.redMul(num);
  4347. };
  4348. EdwardsCurve.prototype._mulC = function _mulC(num) {
  4349. if (this.oneC)
  4350. return num;
  4351. else
  4352. return this.c.redMul(num);
  4353. };
  4354. // Just for compatibility with Short curve
  4355. EdwardsCurve.prototype.jpoint = function jpoint(x, y, z, t) {
  4356. return this.point(x, y, z, t);
  4357. };
  4358. EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd) {
  4359. x = new BN(x, 16);
  4360. if (!x.red)
  4361. x = x.toRed(this.red);
  4362. var x2 = x.redSqr();
  4363. var rhs = this.c2.redSub(this.a.redMul(x2));
  4364. var lhs = this.one.redSub(this.c2.redMul(this.d).redMul(x2));
  4365. var y2 = rhs.redMul(lhs.redInvm());
  4366. var y = y2.redSqrt();
  4367. if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
  4368. throw new Error('invalid point');
  4369. var isOdd = y.fromRed().isOdd();
  4370. if (odd && !isOdd || !odd && isOdd)
  4371. y = y.redNeg();
  4372. return this.point(x, y);
  4373. };
  4374. EdwardsCurve.prototype.pointFromY = function pointFromY(y, odd) {
  4375. y = new BN(y, 16);
  4376. if (!y.red)
  4377. y = y.toRed(this.red);
  4378. // x^2 = (y^2 - 1) / (d y^2 + 1)
  4379. var y2 = y.redSqr();
  4380. var lhs = y2.redSub(this.one);
  4381. var rhs = y2.redMul(this.d).redAdd(this.one);
  4382. var x2 = lhs.redMul(rhs.redInvm());
  4383. if (x2.cmp(this.zero) === 0) {
  4384. if (odd)
  4385. throw new Error('invalid point');
  4386. else
  4387. return this.point(this.zero, y);
  4388. }
  4389. var x = x2.redSqrt();
  4390. if (x.redSqr().redSub(x2).cmp(this.zero) !== 0)
  4391. throw new Error('invalid point');
  4392. if (x.isOdd() !== odd)
  4393. x = x.redNeg();
  4394. return this.point(x, y);
  4395. };
  4396. EdwardsCurve.prototype.validate = function validate(point) {
  4397. if (point.isInfinity())
  4398. return true;
  4399. // Curve: A * X^2 + Y^2 = C^2 * (1 + D * X^2 * Y^2)
  4400. point.normalize();
  4401. var x2 = point.x.redSqr();
  4402. var y2 = point.y.redSqr();
  4403. var lhs = x2.redMul(this.a).redAdd(y2);
  4404. var rhs = this.c2.redMul(this.one.redAdd(this.d.redMul(x2).redMul(y2)));
  4405. return lhs.cmp(rhs) === 0;
  4406. };
  4407. function Point(curve, x, y, z, t) {
  4408. Base.BasePoint.call(this, curve, 'projective');
  4409. if (x === null && y === null && z === null) {
  4410. this.x = this.curve.zero;
  4411. this.y = this.curve.one;
  4412. this.z = this.curve.one;
  4413. this.t = this.curve.zero;
  4414. this.zOne = true;
  4415. } else {
  4416. this.x = new BN(x, 16);
  4417. this.y = new BN(y, 16);
  4418. this.z = z ? new BN(z, 16) : this.curve.one;
  4419. this.t = t && new BN(t, 16);
  4420. if (!this.x.red)
  4421. this.x = this.x.toRed(this.curve.red);
  4422. if (!this.y.red)
  4423. this.y = this.y.toRed(this.curve.red);
  4424. if (!this.z.red)
  4425. this.z = this.z.toRed(this.curve.red);
  4426. if (this.t && !this.t.red)
  4427. this.t = this.t.toRed(this.curve.red);
  4428. this.zOne = this.z === this.curve.one;
  4429. // Use extended coordinates
  4430. if (this.curve.extended && !this.t) {
  4431. this.t = this.x.redMul(this.y);
  4432. if (!this.zOne)
  4433. this.t = this.t.redMul(this.z.redInvm());
  4434. }
  4435. }
  4436. }
  4437. inherits(Point, Base.BasePoint);
  4438. EdwardsCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
  4439. return Point.fromJSON(this, obj);
  4440. };
  4441. EdwardsCurve.prototype.point = function point(x, y, z, t) {
  4442. return new Point(this, x, y, z, t);
  4443. };
  4444. Point.fromJSON = function fromJSON(curve, obj) {
  4445. return new Point(curve, obj[0], obj[1], obj[2]);
  4446. };
  4447. Point.prototype.inspect = function inspect() {
  4448. if (this.isInfinity())
  4449. return '<EC Point Infinity>';
  4450. return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
  4451. ' y: ' + this.y.fromRed().toString(16, 2) +
  4452. ' z: ' + this.z.fromRed().toString(16, 2) + '>';
  4453. };
  4454. Point.prototype.isInfinity = function isInfinity() {
  4455. // XXX This code assumes that zero is always zero in red
  4456. return this.x.cmpn(0) === 0 &&
  4457. this.y.cmp(this.z) === 0;
  4458. };
  4459. Point.prototype._extDbl = function _extDbl() {
  4460. // hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
  4461. // #doubling-dbl-2008-hwcd
  4462. // 4M + 4S
  4463. // A = X1^2
  4464. var a = this.x.redSqr();
  4465. // B = Y1^2
  4466. var b = this.y.redSqr();
  4467. // C = 2 * Z1^2
  4468. var c = this.z.redSqr();
  4469. c = c.redIAdd(c);
  4470. // D = a * A
  4471. var d = this.curve._mulA(a);
  4472. // E = (X1 + Y1)^2 - A - B
  4473. var e = this.x.redAdd(this.y).redSqr().redISub(a).redISub(b);
  4474. // G = D + B
  4475. var g = d.redAdd(b);
  4476. // F = G - C
  4477. var f = g.redSub(c);
  4478. // H = D - B
  4479. var h = d.redSub(b);
  4480. // X3 = E * F
  4481. var nx = e.redMul(f);
  4482. // Y3 = G * H
  4483. var ny = g.redMul(h);
  4484. // T3 = E * H
  4485. var nt = e.redMul(h);
  4486. // Z3 = F * G
  4487. var nz = f.redMul(g);
  4488. return this.curve.point(nx, ny, nz, nt);
  4489. };
  4490. Point.prototype._projDbl = function _projDbl() {
  4491. // hyperelliptic.org/EFD/g1p/auto-twisted-projective.html
  4492. // #doubling-dbl-2008-bbjlp
  4493. // #doubling-dbl-2007-bl
  4494. // and others
  4495. // Generally 3M + 4S or 2M + 4S
  4496. // B = (X1 + Y1)^2
  4497. var b = this.x.redAdd(this.y).redSqr();
  4498. // C = X1^2
  4499. var c = this.x.redSqr();
  4500. // D = Y1^2
  4501. var d = this.y.redSqr();
  4502. var nx;
  4503. var ny;
  4504. var nz;
  4505. if (this.curve.twisted) {
  4506. // E = a * C
  4507. var e = this.curve._mulA(c);
  4508. // F = E + D
  4509. var f = e.redAdd(d);
  4510. if (this.zOne) {
  4511. // X3 = (B - C - D) * (F - 2)
  4512. nx = b.redSub(c).redSub(d).redMul(f.redSub(this.curve.two));
  4513. // Y3 = F * (E - D)
  4514. ny = f.redMul(e.redSub(d));
  4515. // Z3 = F^2 - 2 * F
  4516. nz = f.redSqr().redSub(f).redSub(f);
  4517. } else {
  4518. // H = Z1^2
  4519. var h = this.z.redSqr();
  4520. // J = F - 2 * H
  4521. var j = f.redSub(h).redISub(h);
  4522. // X3 = (B-C-D)*J
  4523. nx = b.redSub(c).redISub(d).redMul(j);
  4524. // Y3 = F * (E - D)
  4525. ny = f.redMul(e.redSub(d));
  4526. // Z3 = F * J
  4527. nz = f.redMul(j);
  4528. }
  4529. } else {
  4530. // E = C + D
  4531. var e = c.redAdd(d);
  4532. // H = (c * Z1)^2
  4533. var h = this.curve._mulC(this.c.redMul(this.z)).redSqr();
  4534. // J = E - 2 * H
  4535. var j = e.redSub(h).redSub(h);
  4536. // X3 = c * (B - E) * J
  4537. nx = this.curve._mulC(b.redISub(e)).redMul(j);
  4538. // Y3 = c * E * (C - D)
  4539. ny = this.curve._mulC(e).redMul(c.redISub(d));
  4540. // Z3 = E * J
  4541. nz = e.redMul(j);
  4542. }
  4543. return this.curve.point(nx, ny, nz);
  4544. };
  4545. Point.prototype.dbl = function dbl() {
  4546. if (this.isInfinity())
  4547. return this;
  4548. // Double in extended coordinates
  4549. if (this.curve.extended)
  4550. return this._extDbl();
  4551. else
  4552. return this._projDbl();
  4553. };
  4554. Point.prototype._extAdd = function _extAdd(p) {
  4555. // hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
  4556. // #addition-add-2008-hwcd-3
  4557. // 8M
  4558. // A = (Y1 - X1) * (Y2 - X2)
  4559. var a = this.y.redSub(this.x).redMul(p.y.redSub(p.x));
  4560. // B = (Y1 + X1) * (Y2 + X2)
  4561. var b = this.y.redAdd(this.x).redMul(p.y.redAdd(p.x));
  4562. // C = T1 * k * T2
  4563. var c = this.t.redMul(this.curve.dd).redMul(p.t);
  4564. // D = Z1 * 2 * Z2
  4565. var d = this.z.redMul(p.z.redAdd(p.z));
  4566. // E = B - A
  4567. var e = b.redSub(a);
  4568. // F = D - C
  4569. var f = d.redSub(c);
  4570. // G = D + C
  4571. var g = d.redAdd(c);
  4572. // H = B + A
  4573. var h = b.redAdd(a);
  4574. // X3 = E * F
  4575. var nx = e.redMul(f);
  4576. // Y3 = G * H
  4577. var ny = g.redMul(h);
  4578. // T3 = E * H
  4579. var nt = e.redMul(h);
  4580. // Z3 = F * G
  4581. var nz = f.redMul(g);
  4582. return this.curve.point(nx, ny, nz, nt);
  4583. };
  4584. Point.prototype._projAdd = function _projAdd(p) {
  4585. // hyperelliptic.org/EFD/g1p/auto-twisted-projective.html
  4586. // #addition-add-2008-bbjlp
  4587. // #addition-add-2007-bl
  4588. // 10M + 1S
  4589. // A = Z1 * Z2
  4590. var a = this.z.redMul(p.z);
  4591. // B = A^2
  4592. var b = a.redSqr();
  4593. // C = X1 * X2
  4594. var c = this.x.redMul(p.x);
  4595. // D = Y1 * Y2
  4596. var d = this.y.redMul(p.y);
  4597. // E = d * C * D
  4598. var e = this.curve.d.redMul(c).redMul(d);
  4599. // F = B - E
  4600. var f = b.redSub(e);
  4601. // G = B + E
  4602. var g = b.redAdd(e);
  4603. // X3 = A * F * ((X1 + Y1) * (X2 + Y2) - C - D)
  4604. var tmp = this.x.redAdd(this.y).redMul(p.x.redAdd(p.y)).redISub(c).redISub(d);
  4605. var nx = a.redMul(f).redMul(tmp);
  4606. var ny;
  4607. var nz;
  4608. if (this.curve.twisted) {
  4609. // Y3 = A * G * (D - a * C)
  4610. ny = a.redMul(g).redMul(d.redSub(this.curve._mulA(c)));
  4611. // Z3 = F * G
  4612. nz = f.redMul(g);
  4613. } else {
  4614. // Y3 = A * G * (D - C)
  4615. ny = a.redMul(g).redMul(d.redSub(c));
  4616. // Z3 = c * F * G
  4617. nz = this.curve._mulC(f).redMul(g);
  4618. }
  4619. return this.curve.point(nx, ny, nz);
  4620. };
  4621. Point.prototype.add = function add(p) {
  4622. if (this.isInfinity())
  4623. return p;
  4624. if (p.isInfinity())
  4625. return this;
  4626. if (this.curve.extended)
  4627. return this._extAdd(p);
  4628. else
  4629. return this._projAdd(p);
  4630. };
  4631. Point.prototype.mul = function mul(k) {
  4632. if (this._hasDoubles(k))
  4633. return this.curve._fixedNafMul(this, k);
  4634. else
  4635. return this.curve._wnafMul(this, k);
  4636. };
  4637. Point.prototype.mulAdd = function mulAdd(k1, p, k2) {
  4638. return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, false);
  4639. };
  4640. Point.prototype.jmulAdd = function jmulAdd(k1, p, k2) {
  4641. return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, true);
  4642. };
  4643. Point.prototype.normalize = function normalize() {
  4644. if (this.zOne)
  4645. return this;
  4646. // Normalize coordinates
  4647. var zi = this.z.redInvm();
  4648. this.x = this.x.redMul(zi);
  4649. this.y = this.y.redMul(zi);
  4650. if (this.t)
  4651. this.t = this.t.redMul(zi);
  4652. this.z = this.curve.one;
  4653. this.zOne = true;
  4654. return this;
  4655. };
  4656. Point.prototype.neg = function neg() {
  4657. return this.curve.point(this.x.redNeg(),
  4658. this.y,
  4659. this.z,
  4660. this.t && this.t.redNeg());
  4661. };
  4662. Point.prototype.getX = function getX() {
  4663. this.normalize();
  4664. return this.x.fromRed();
  4665. };
  4666. Point.prototype.getY = function getY() {
  4667. this.normalize();
  4668. return this.y.fromRed();
  4669. };
  4670. Point.prototype.eq = function eq(other) {
  4671. return this === other ||
  4672. this.getX().cmp(other.getX()) === 0 &&
  4673. this.getY().cmp(other.getY()) === 0;
  4674. };
  4675. Point.prototype.eqXToP = function eqXToP(x) {
  4676. var rx = x.toRed(this.curve.red).redMul(this.z);
  4677. if (this.x.cmp(rx) === 0)
  4678. return true;
  4679. var xc = x.clone();
  4680. var t = this.curve.redN.redMul(this.z);
  4681. for (;;) {
  4682. xc.iadd(this.curve.n);
  4683. if (xc.cmp(this.curve.p) >= 0)
  4684. return false;
  4685. rx.redIAdd(t);
  4686. if (this.x.cmp(rx) === 0)
  4687. return true;
  4688. }
  4689. return false;
  4690. };
  4691. // Compatibility with BaseCurve
  4692. Point.prototype.toP = Point.prototype.normalize;
  4693. Point.prototype.mixedAdd = Point.prototype.add;
  4694. },{"../../elliptic":10,"../curve":13,"bn.js":4,"inherits":34}],13:[function(require,module,exports){
  4695. 'use strict';
  4696. var curve = exports;
  4697. curve.base = require('./base');
  4698. curve.short = require('./short');
  4699. curve.mont = require('./mont');
  4700. curve.edwards = require('./edwards');
  4701. },{"./base":11,"./edwards":12,"./mont":14,"./short":15}],14:[function(require,module,exports){
  4702. 'use strict';
  4703. var curve = require('../curve');
  4704. var BN = require('bn.js');
  4705. var inherits = require('inherits');
  4706. var Base = curve.base;
  4707. var elliptic = require('../../elliptic');
  4708. var utils = elliptic.utils;
  4709. function MontCurve(conf) {
  4710. Base.call(this, 'mont', conf);
  4711. this.a = new BN(conf.a, 16).toRed(this.red);
  4712. this.b = new BN(conf.b, 16).toRed(this.red);
  4713. this.i4 = new BN(4).toRed(this.red).redInvm();
  4714. this.two = new BN(2).toRed(this.red);
  4715. this.a24 = this.i4.redMul(this.a.redAdd(this.two));
  4716. }
  4717. inherits(MontCurve, Base);
  4718. module.exports = MontCurve;
  4719. MontCurve.prototype.validate = function validate(point) {
  4720. var x = point.normalize().x;
  4721. var x2 = x.redSqr();
  4722. var rhs = x2.redMul(x).redAdd(x2.redMul(this.a)).redAdd(x);
  4723. var y = rhs.redSqrt();
  4724. return y.redSqr().cmp(rhs) === 0;
  4725. };
  4726. function Point(curve, x, z) {
  4727. Base.BasePoint.call(this, curve, 'projective');
  4728. if (x === null && z === null) {
  4729. this.x = this.curve.one;
  4730. this.z = this.curve.zero;
  4731. } else {
  4732. this.x = new BN(x, 16);
  4733. this.z = new BN(z, 16);
  4734. if (!this.x.red)
  4735. this.x = this.x.toRed(this.curve.red);
  4736. if (!this.z.red)
  4737. this.z = this.z.toRed(this.curve.red);
  4738. }
  4739. }
  4740. inherits(Point, Base.BasePoint);
  4741. MontCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
  4742. return this.point(utils.toArray(bytes, enc), 1);
  4743. };
  4744. MontCurve.prototype.point = function point(x, z) {
  4745. return new Point(this, x, z);
  4746. };
  4747. MontCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
  4748. return Point.fromJSON(this, obj);
  4749. };
  4750. Point.prototype.precompute = function precompute() {
  4751. // No-op
  4752. };
  4753. Point.prototype._encode = function _encode() {
  4754. return this.getX().toArray('be', this.curve.p.byteLength());
  4755. };
  4756. Point.fromJSON = function fromJSON(curve, obj) {
  4757. return new Point(curve, obj[0], obj[1] || curve.one);
  4758. };
  4759. Point.prototype.inspect = function inspect() {
  4760. if (this.isInfinity())
  4761. return '<EC Point Infinity>';
  4762. return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
  4763. ' z: ' + this.z.fromRed().toString(16, 2) + '>';
  4764. };
  4765. Point.prototype.isInfinity = function isInfinity() {
  4766. // XXX This code assumes that zero is always zero in red
  4767. return this.z.cmpn(0) === 0;
  4768. };
  4769. Point.prototype.dbl = function dbl() {
  4770. // http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#doubling-dbl-1987-m-3
  4771. // 2M + 2S + 4A
  4772. // A = X1 + Z1
  4773. var a = this.x.redAdd(this.z);
  4774. // AA = A^2
  4775. var aa = a.redSqr();
  4776. // B = X1 - Z1
  4777. var b = this.x.redSub(this.z);
  4778. // BB = B^2
  4779. var bb = b.redSqr();
  4780. // C = AA - BB
  4781. var c = aa.redSub(bb);
  4782. // X3 = AA * BB
  4783. var nx = aa.redMul(bb);
  4784. // Z3 = C * (BB + A24 * C)
  4785. var nz = c.redMul(bb.redAdd(this.curve.a24.redMul(c)));
  4786. return this.curve.point(nx, nz);
  4787. };
  4788. Point.prototype.add = function add() {
  4789. throw new Error('Not supported on Montgomery curve');
  4790. };
  4791. Point.prototype.diffAdd = function diffAdd(p, diff) {
  4792. // http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#diffadd-dadd-1987-m-3
  4793. // 4M + 2S + 6A
  4794. // A = X2 + Z2
  4795. var a = this.x.redAdd(this.z);
  4796. // B = X2 - Z2
  4797. var b = this.x.redSub(this.z);
  4798. // C = X3 + Z3
  4799. var c = p.x.redAdd(p.z);
  4800. // D = X3 - Z3
  4801. var d = p.x.redSub(p.z);
  4802. // DA = D * A
  4803. var da = d.redMul(a);
  4804. // CB = C * B
  4805. var cb = c.redMul(b);
  4806. // X5 = Z1 * (DA + CB)^2
  4807. var nx = diff.z.redMul(da.redAdd(cb).redSqr());
  4808. // Z5 = X1 * (DA - CB)^2
  4809. var nz = diff.x.redMul(da.redISub(cb).redSqr());
  4810. return this.curve.point(nx, nz);
  4811. };
  4812. Point.prototype.mul = function mul(k) {
  4813. var t = k.clone();
  4814. var a = this; // (N / 2) * Q + Q
  4815. var b = this.curve.point(null, null); // (N / 2) * Q
  4816. var c = this; // Q
  4817. for (var bits = []; t.cmpn(0) !== 0; t.iushrn(1))
  4818. bits.push(t.andln(1));
  4819. for (var i = bits.length - 1; i >= 0; i--) {
  4820. if (bits[i] === 0) {
  4821. // N * Q + Q = ((N / 2) * Q + Q)) + (N / 2) * Q
  4822. a = a.diffAdd(b, c);
  4823. // N * Q = 2 * ((N / 2) * Q + Q))
  4824. b = b.dbl();
  4825. } else {
  4826. // N * Q = ((N / 2) * Q + Q) + ((N / 2) * Q)
  4827. b = a.diffAdd(b, c);
  4828. // N * Q + Q = 2 * ((N / 2) * Q + Q)
  4829. a = a.dbl();
  4830. }
  4831. }
  4832. return b;
  4833. };
  4834. Point.prototype.mulAdd = function mulAdd() {
  4835. throw new Error('Not supported on Montgomery curve');
  4836. };
  4837. Point.prototype.jumlAdd = function jumlAdd() {
  4838. throw new Error('Not supported on Montgomery curve');
  4839. };
  4840. Point.prototype.eq = function eq(other) {
  4841. return this.getX().cmp(other.getX()) === 0;
  4842. };
  4843. Point.prototype.normalize = function normalize() {
  4844. this.x = this.x.redMul(this.z.redInvm());
  4845. this.z = this.curve.one;
  4846. return this;
  4847. };
  4848. Point.prototype.getX = function getX() {
  4849. // Normalize coordinates
  4850. this.normalize();
  4851. return this.x.fromRed();
  4852. };
  4853. },{"../../elliptic":10,"../curve":13,"bn.js":4,"inherits":34}],15:[function(require,module,exports){
  4854. 'use strict';
  4855. var curve = require('../curve');
  4856. var elliptic = require('../../elliptic');
  4857. var BN = require('bn.js');
  4858. var inherits = require('inherits');
  4859. var Base = curve.base;
  4860. var assert = elliptic.utils.assert;
  4861. function ShortCurve(conf) {
  4862. Base.call(this, 'short', conf);
  4863. this.a = new BN(conf.a, 16).toRed(this.red);
  4864. this.b = new BN(conf.b, 16).toRed(this.red);
  4865. this.tinv = this.two.redInvm();
  4866. this.zeroA = this.a.fromRed().cmpn(0) === 0;
  4867. this.threeA = this.a.fromRed().sub(this.p).cmpn(-3) === 0;
  4868. // If the curve is endomorphic, precalculate beta and lambda
  4869. this.endo = this._getEndomorphism(conf);
  4870. this._endoWnafT1 = new Array(4);
  4871. this._endoWnafT2 = new Array(4);
  4872. }
  4873. inherits(ShortCurve, Base);
  4874. module.exports = ShortCurve;
  4875. ShortCurve.prototype._getEndomorphism = function _getEndomorphism(conf) {
  4876. // No efficient endomorphism
  4877. if (!this.zeroA || !this.g || !this.n || this.p.modn(3) !== 1)
  4878. return;
  4879. // Compute beta and lambda, that lambda * P = (beta * Px; Py)
  4880. var beta;
  4881. var lambda;
  4882. if (conf.beta) {
  4883. beta = new BN(conf.beta, 16).toRed(this.red);
  4884. } else {
  4885. var betas = this._getEndoRoots(this.p);
  4886. // Choose the smallest beta
  4887. beta = betas[0].cmp(betas[1]) < 0 ? betas[0] : betas[1];
  4888. beta = beta.toRed(this.red);
  4889. }
  4890. if (conf.lambda) {
  4891. lambda = new BN(conf.lambda, 16);
  4892. } else {
  4893. // Choose the lambda that is matching selected beta
  4894. var lambdas = this._getEndoRoots(this.n);
  4895. if (this.g.mul(lambdas[0]).x.cmp(this.g.x.redMul(beta)) === 0) {
  4896. lambda = lambdas[0];
  4897. } else {
  4898. lambda = lambdas[1];
  4899. assert(this.g.mul(lambda).x.cmp(this.g.x.redMul(beta)) === 0);
  4900. }
  4901. }
  4902. // Get basis vectors, used for balanced length-two representation
  4903. var basis;
  4904. if (conf.basis) {
  4905. basis = conf.basis.map(function(vec) {
  4906. return {
  4907. a: new BN(vec.a, 16),
  4908. b: new BN(vec.b, 16)
  4909. };
  4910. });
  4911. } else {
  4912. basis = this._getEndoBasis(lambda);
  4913. }
  4914. return {
  4915. beta: beta,
  4916. lambda: lambda,
  4917. basis: basis
  4918. };
  4919. };
  4920. ShortCurve.prototype._getEndoRoots = function _getEndoRoots(num) {
  4921. // Find roots of for x^2 + x + 1 in F
  4922. // Root = (-1 +- Sqrt(-3)) / 2
  4923. //
  4924. var red = num === this.p ? this.red : BN.mont(num);
  4925. var tinv = new BN(2).toRed(red).redInvm();
  4926. var ntinv = tinv.redNeg();
  4927. var s = new BN(3).toRed(red).redNeg().redSqrt().redMul(tinv);
  4928. var l1 = ntinv.redAdd(s).fromRed();
  4929. var l2 = ntinv.redSub(s).fromRed();
  4930. return [ l1, l2 ];
  4931. };
  4932. ShortCurve.prototype._getEndoBasis = function _getEndoBasis(lambda) {
  4933. // aprxSqrt >= sqrt(this.n)
  4934. var aprxSqrt = this.n.ushrn(Math.floor(this.n.bitLength() / 2));
  4935. // 3.74
  4936. // Run EGCD, until r(L + 1) < aprxSqrt
  4937. var u = lambda;
  4938. var v = this.n.clone();
  4939. var x1 = new BN(1);
  4940. var y1 = new BN(0);
  4941. var x2 = new BN(0);
  4942. var y2 = new BN(1);
  4943. // NOTE: all vectors are roots of: a + b * lambda = 0 (mod n)
  4944. var a0;
  4945. var b0;
  4946. // First vector
  4947. var a1;
  4948. var b1;
  4949. // Second vector
  4950. var a2;
  4951. var b2;
  4952. var prevR;
  4953. var i = 0;
  4954. var r;
  4955. var x;
  4956. while (u.cmpn(0) !== 0) {
  4957. var q = v.div(u);
  4958. r = v.sub(q.mul(u));
  4959. x = x2.sub(q.mul(x1));
  4960. var y = y2.sub(q.mul(y1));
  4961. if (!a1 && r.cmp(aprxSqrt) < 0) {
  4962. a0 = prevR.neg();
  4963. b0 = x1;
  4964. a1 = r.neg();
  4965. b1 = x;
  4966. } else if (a1 && ++i === 2) {
  4967. break;
  4968. }
  4969. prevR = r;
  4970. v = u;
  4971. u = r;
  4972. x2 = x1;
  4973. x1 = x;
  4974. y2 = y1;
  4975. y1 = y;
  4976. }
  4977. a2 = r.neg();
  4978. b2 = x;
  4979. var len1 = a1.sqr().add(b1.sqr());
  4980. var len2 = a2.sqr().add(b2.sqr());
  4981. if (len2.cmp(len1) >= 0) {
  4982. a2 = a0;
  4983. b2 = b0;
  4984. }
  4985. // Normalize signs
  4986. if (a1.negative) {
  4987. a1 = a1.neg();
  4988. b1 = b1.neg();
  4989. }
  4990. if (a2.negative) {
  4991. a2 = a2.neg();
  4992. b2 = b2.neg();
  4993. }
  4994. return [
  4995. { a: a1, b: b1 },
  4996. { a: a2, b: b2 }
  4997. ];
  4998. };
  4999. ShortCurve.prototype._endoSplit = function _endoSplit(k) {
  5000. var basis = this.endo.basis;
  5001. var v1 = basis[0];
  5002. var v2 = basis[1];
  5003. var c1 = v2.b.mul(k).divRound(this.n);
  5004. var c2 = v1.b.neg().mul(k).divRound(this.n);
  5005. var p1 = c1.mul(v1.a);
  5006. var p2 = c2.mul(v2.a);
  5007. var q1 = c1.mul(v1.b);
  5008. var q2 = c2.mul(v2.b);
  5009. // Calculate answer
  5010. var k1 = k.sub(p1).sub(p2);
  5011. var k2 = q1.add(q2).neg();
  5012. return { k1: k1, k2: k2 };
  5013. };
  5014. ShortCurve.prototype.pointFromX = function pointFromX(x, odd) {
  5015. x = new BN(x, 16);
  5016. if (!x.red)
  5017. x = x.toRed(this.red);
  5018. var y2 = x.redSqr().redMul(x).redIAdd(x.redMul(this.a)).redIAdd(this.b);
  5019. var y = y2.redSqrt();
  5020. if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
  5021. throw new Error('invalid point');
  5022. // XXX Is there any way to tell if the number is odd without converting it
  5023. // to non-red form?
  5024. var isOdd = y.fromRed().isOdd();
  5025. if (odd && !isOdd || !odd && isOdd)
  5026. y = y.redNeg();
  5027. return this.point(x, y);
  5028. };
  5029. ShortCurve.prototype.validate = function validate(point) {
  5030. if (point.inf)
  5031. return true;
  5032. var x = point.x;
  5033. var y = point.y;
  5034. var ax = this.a.redMul(x);
  5035. var rhs = x.redSqr().redMul(x).redIAdd(ax).redIAdd(this.b);
  5036. return y.redSqr().redISub(rhs).cmpn(0) === 0;
  5037. };
  5038. ShortCurve.prototype._endoWnafMulAdd =
  5039. function _endoWnafMulAdd(points, coeffs, jacobianResult) {
  5040. var npoints = this._endoWnafT1;
  5041. var ncoeffs = this._endoWnafT2;
  5042. for (var i = 0; i < points.length; i++) {
  5043. var split = this._endoSplit(coeffs[i]);
  5044. var p = points[i];
  5045. var beta = p._getBeta();
  5046. if (split.k1.negative) {
  5047. split.k1.ineg();
  5048. p = p.neg(true);
  5049. }
  5050. if (split.k2.negative) {
  5051. split.k2.ineg();
  5052. beta = beta.neg(true);
  5053. }
  5054. npoints[i * 2] = p;
  5055. npoints[i * 2 + 1] = beta;
  5056. ncoeffs[i * 2] = split.k1;
  5057. ncoeffs[i * 2 + 1] = split.k2;
  5058. }
  5059. var res = this._wnafMulAdd(1, npoints, ncoeffs, i * 2, jacobianResult);
  5060. // Clean-up references to points and coefficients
  5061. for (var j = 0; j < i * 2; j++) {
  5062. npoints[j] = null;
  5063. ncoeffs[j] = null;
  5064. }
  5065. return res;
  5066. };
  5067. function Point(curve, x, y, isRed) {
  5068. Base.BasePoint.call(this, curve, 'affine');
  5069. if (x === null && y === null) {
  5070. this.x = null;
  5071. this.y = null;
  5072. this.inf = true;
  5073. } else {
  5074. this.x = new BN(x, 16);
  5075. this.y = new BN(y, 16);
  5076. // Force redgomery representation when loading from JSON
  5077. if (isRed) {
  5078. this.x.forceRed(this.curve.red);
  5079. this.y.forceRed(this.curve.red);
  5080. }
  5081. if (!this.x.red)
  5082. this.x = this.x.toRed(this.curve.red);
  5083. if (!this.y.red)
  5084. this.y = this.y.toRed(this.curve.red);
  5085. this.inf = false;
  5086. }
  5087. }
  5088. inherits(Point, Base.BasePoint);
  5089. ShortCurve.prototype.point = function point(x, y, isRed) {
  5090. return new Point(this, x, y, isRed);
  5091. };
  5092. ShortCurve.prototype.pointFromJSON = function pointFromJSON(obj, red) {
  5093. return Point.fromJSON(this, obj, red);
  5094. };
  5095. Point.prototype._getBeta = function _getBeta() {
  5096. if (!this.curve.endo)
  5097. return;
  5098. var pre = this.precomputed;
  5099. if (pre && pre.beta)
  5100. return pre.beta;
  5101. var beta = this.curve.point(this.x.redMul(this.curve.endo.beta), this.y);
  5102. if (pre) {
  5103. var curve = this.curve;
  5104. var endoMul = function(p) {
  5105. return curve.point(p.x.redMul(curve.endo.beta), p.y);
  5106. };
  5107. pre.beta = beta;
  5108. beta.precomputed = {
  5109. beta: null,
  5110. naf: pre.naf && {
  5111. wnd: pre.naf.wnd,
  5112. points: pre.naf.points.map(endoMul)
  5113. },
  5114. doubles: pre.doubles && {
  5115. step: pre.doubles.step,
  5116. points: pre.doubles.points.map(endoMul)
  5117. }
  5118. };
  5119. }
  5120. return beta;
  5121. };
  5122. Point.prototype.toJSON = function toJSON() {
  5123. if (!this.precomputed)
  5124. return [ this.x, this.y ];
  5125. return [ this.x, this.y, this.precomputed && {
  5126. doubles: this.precomputed.doubles && {
  5127. step: this.precomputed.doubles.step,
  5128. points: this.precomputed.doubles.points.slice(1)
  5129. },
  5130. naf: this.precomputed.naf && {
  5131. wnd: this.precomputed.naf.wnd,
  5132. points: this.precomputed.naf.points.slice(1)
  5133. }
  5134. } ];
  5135. };
  5136. Point.fromJSON = function fromJSON(curve, obj, red) {
  5137. if (typeof obj === 'string')
  5138. obj = JSON.parse(obj);
  5139. var res = curve.point(obj[0], obj[1], red);
  5140. if (!obj[2])
  5141. return res;
  5142. function obj2point(obj) {
  5143. return curve.point(obj[0], obj[1], red);
  5144. }
  5145. var pre = obj[2];
  5146. res.precomputed = {
  5147. beta: null,
  5148. doubles: pre.doubles && {
  5149. step: pre.doubles.step,
  5150. points: [ res ].concat(pre.doubles.points.map(obj2point))
  5151. },
  5152. naf: pre.naf && {
  5153. wnd: pre.naf.wnd,
  5154. points: [ res ].concat(pre.naf.points.map(obj2point))
  5155. }
  5156. };
  5157. return res;
  5158. };
  5159. Point.prototype.inspect = function inspect() {
  5160. if (this.isInfinity())
  5161. return '<EC Point Infinity>';
  5162. return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
  5163. ' y: ' + this.y.fromRed().toString(16, 2) + '>';
  5164. };
  5165. Point.prototype.isInfinity = function isInfinity() {
  5166. return this.inf;
  5167. };
  5168. Point.prototype.add = function add(p) {
  5169. // O + P = P
  5170. if (this.inf)
  5171. return p;
  5172. // P + O = P
  5173. if (p.inf)
  5174. return this;
  5175. // P + P = 2P
  5176. if (this.eq(p))
  5177. return this.dbl();
  5178. // P + (-P) = O
  5179. if (this.neg().eq(p))
  5180. return this.curve.point(null, null);
  5181. // P + Q = O
  5182. if (this.x.cmp(p.x) === 0)
  5183. return this.curve.point(null, null);
  5184. var c = this.y.redSub(p.y);
  5185. if (c.cmpn(0) !== 0)
  5186. c = c.redMul(this.x.redSub(p.x).redInvm());
  5187. var nx = c.redSqr().redISub(this.x).redISub(p.x);
  5188. var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
  5189. return this.curve.point(nx, ny);
  5190. };
  5191. Point.prototype.dbl = function dbl() {
  5192. if (this.inf)
  5193. return this;
  5194. // 2P = O
  5195. var ys1 = this.y.redAdd(this.y);
  5196. if (ys1.cmpn(0) === 0)
  5197. return this.curve.point(null, null);
  5198. var a = this.curve.a;
  5199. var x2 = this.x.redSqr();
  5200. var dyinv = ys1.redInvm();
  5201. var c = x2.redAdd(x2).redIAdd(x2).redIAdd(a).redMul(dyinv);
  5202. var nx = c.redSqr().redISub(this.x.redAdd(this.x));
  5203. var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
  5204. return this.curve.point(nx, ny);
  5205. };
  5206. Point.prototype.getX = function getX() {
  5207. return this.x.fromRed();
  5208. };
  5209. Point.prototype.getY = function getY() {
  5210. return this.y.fromRed();
  5211. };
  5212. Point.prototype.mul = function mul(k) {
  5213. k = new BN(k, 16);
  5214. if (this._hasDoubles(k))
  5215. return this.curve._fixedNafMul(this, k);
  5216. else if (this.curve.endo)
  5217. return this.curve._endoWnafMulAdd([ this ], [ k ]);
  5218. else
  5219. return this.curve._wnafMul(this, k);
  5220. };
  5221. Point.prototype.mulAdd = function mulAdd(k1, p2, k2) {
  5222. var points = [ this, p2 ];
  5223. var coeffs = [ k1, k2 ];
  5224. if (this.curve.endo)
  5225. return this.curve._endoWnafMulAdd(points, coeffs);
  5226. else
  5227. return this.curve._wnafMulAdd(1, points, coeffs, 2);
  5228. };
  5229. Point.prototype.jmulAdd = function jmulAdd(k1, p2, k2) {
  5230. var points = [ this, p2 ];
  5231. var coeffs = [ k1, k2 ];
  5232. if (this.curve.endo)
  5233. return this.curve._endoWnafMulAdd(points, coeffs, true);
  5234. else
  5235. return this.curve._wnafMulAdd(1, points, coeffs, 2, true);
  5236. };
  5237. Point.prototype.eq = function eq(p) {
  5238. return this === p ||
  5239. this.inf === p.inf &&
  5240. (this.inf || this.x.cmp(p.x) === 0 && this.y.cmp(p.y) === 0);
  5241. };
  5242. Point.prototype.neg = function neg(_precompute) {
  5243. if (this.inf)
  5244. return this;
  5245. var res = this.curve.point(this.x, this.y.redNeg());
  5246. if (_precompute && this.precomputed) {
  5247. var pre = this.precomputed;
  5248. var negate = function(p) {
  5249. return p.neg();
  5250. };
  5251. res.precomputed = {
  5252. naf: pre.naf && {
  5253. wnd: pre.naf.wnd,
  5254. points: pre.naf.points.map(negate)
  5255. },
  5256. doubles: pre.doubles && {
  5257. step: pre.doubles.step,
  5258. points: pre.doubles.points.map(negate)
  5259. }
  5260. };
  5261. }
  5262. return res;
  5263. };
  5264. Point.prototype.toJ = function toJ() {
  5265. if (this.inf)
  5266. return this.curve.jpoint(null, null, null);
  5267. var res = this.curve.jpoint(this.x, this.y, this.curve.one);
  5268. return res;
  5269. };
  5270. function JPoint(curve, x, y, z) {
  5271. Base.BasePoint.call(this, curve, 'jacobian');
  5272. if (x === null && y === null && z === null) {
  5273. this.x = this.curve.one;
  5274. this.y = this.curve.one;
  5275. this.z = new BN(0);
  5276. } else {
  5277. this.x = new BN(x, 16);
  5278. this.y = new BN(y, 16);
  5279. this.z = new BN(z, 16);
  5280. }
  5281. if (!this.x.red)
  5282. this.x = this.x.toRed(this.curve.red);
  5283. if (!this.y.red)
  5284. this.y = this.y.toRed(this.curve.red);
  5285. if (!this.z.red)
  5286. this.z = this.z.toRed(this.curve.red);
  5287. this.zOne = this.z === this.curve.one;
  5288. }
  5289. inherits(JPoint, Base.BasePoint);
  5290. ShortCurve.prototype.jpoint = function jpoint(x, y, z) {
  5291. return new JPoint(this, x, y, z);
  5292. };
  5293. JPoint.prototype.toP = function toP() {
  5294. if (this.isInfinity())
  5295. return this.curve.point(null, null);
  5296. var zinv = this.z.redInvm();
  5297. var zinv2 = zinv.redSqr();
  5298. var ax = this.x.redMul(zinv2);
  5299. var ay = this.y.redMul(zinv2).redMul(zinv);
  5300. return this.curve.point(ax, ay);
  5301. };
  5302. JPoint.prototype.neg = function neg() {
  5303. return this.curve.jpoint(this.x, this.y.redNeg(), this.z);
  5304. };
  5305. JPoint.prototype.add = function add(p) {
  5306. // O + P = P
  5307. if (this.isInfinity())
  5308. return p;
  5309. // P + O = P
  5310. if (p.isInfinity())
  5311. return this;
  5312. // 12M + 4S + 7A
  5313. var pz2 = p.z.redSqr();
  5314. var z2 = this.z.redSqr();
  5315. var u1 = this.x.redMul(pz2);
  5316. var u2 = p.x.redMul(z2);
  5317. var s1 = this.y.redMul(pz2.redMul(p.z));
  5318. var s2 = p.y.redMul(z2.redMul(this.z));
  5319. var h = u1.redSub(u2);
  5320. var r = s1.redSub(s2);
  5321. if (h.cmpn(0) === 0) {
  5322. if (r.cmpn(0) !== 0)
  5323. return this.curve.jpoint(null, null, null);
  5324. else
  5325. return this.dbl();
  5326. }
  5327. var h2 = h.redSqr();
  5328. var h3 = h2.redMul(h);
  5329. var v = u1.redMul(h2);
  5330. var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
  5331. var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
  5332. var nz = this.z.redMul(p.z).redMul(h);
  5333. return this.curve.jpoint(nx, ny, nz);
  5334. };
  5335. JPoint.prototype.mixedAdd = function mixedAdd(p) {
  5336. // O + P = P
  5337. if (this.isInfinity())
  5338. return p.toJ();
  5339. // P + O = P
  5340. if (p.isInfinity())
  5341. return this;
  5342. // 8M + 3S + 7A
  5343. var z2 = this.z.redSqr();
  5344. var u1 = this.x;
  5345. var u2 = p.x.redMul(z2);
  5346. var s1 = this.y;
  5347. var s2 = p.y.redMul(z2).redMul(this.z);
  5348. var h = u1.redSub(u2);
  5349. var r = s1.redSub(s2);
  5350. if (h.cmpn(0) === 0) {
  5351. if (r.cmpn(0) !== 0)
  5352. return this.curve.jpoint(null, null, null);
  5353. else
  5354. return this.dbl();
  5355. }
  5356. var h2 = h.redSqr();
  5357. var h3 = h2.redMul(h);
  5358. var v = u1.redMul(h2);
  5359. var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
  5360. var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
  5361. var nz = this.z.redMul(h);
  5362. return this.curve.jpoint(nx, ny, nz);
  5363. };
  5364. JPoint.prototype.dblp = function dblp(pow) {
  5365. if (pow === 0)
  5366. return this;
  5367. if (this.isInfinity())
  5368. return this;
  5369. if (!pow)
  5370. return this.dbl();
  5371. if (this.curve.zeroA || this.curve.threeA) {
  5372. var r = this;
  5373. for (var i = 0; i < pow; i++)
  5374. r = r.dbl();
  5375. return r;
  5376. }
  5377. // 1M + 2S + 1A + N * (4S + 5M + 8A)
  5378. // N = 1 => 6M + 6S + 9A
  5379. var a = this.curve.a;
  5380. var tinv = this.curve.tinv;
  5381. var jx = this.x;
  5382. var jy = this.y;
  5383. var jz = this.z;
  5384. var jz4 = jz.redSqr().redSqr();
  5385. // Reuse results
  5386. var jyd = jy.redAdd(jy);
  5387. for (var i = 0; i < pow; i++) {
  5388. var jx2 = jx.redSqr();
  5389. var jyd2 = jyd.redSqr();
  5390. var jyd4 = jyd2.redSqr();
  5391. var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
  5392. var t1 = jx.redMul(jyd2);
  5393. var nx = c.redSqr().redISub(t1.redAdd(t1));
  5394. var t2 = t1.redISub(nx);
  5395. var dny = c.redMul(t2);
  5396. dny = dny.redIAdd(dny).redISub(jyd4);
  5397. var nz = jyd.redMul(jz);
  5398. if (i + 1 < pow)
  5399. jz4 = jz4.redMul(jyd4);
  5400. jx = nx;
  5401. jz = nz;
  5402. jyd = dny;
  5403. }
  5404. return this.curve.jpoint(jx, jyd.redMul(tinv), jz);
  5405. };
  5406. JPoint.prototype.dbl = function dbl() {
  5407. if (this.isInfinity())
  5408. return this;
  5409. if (this.curve.zeroA)
  5410. return this._zeroDbl();
  5411. else if (this.curve.threeA)
  5412. return this._threeDbl();
  5413. else
  5414. return this._dbl();
  5415. };
  5416. JPoint.prototype._zeroDbl = function _zeroDbl() {
  5417. var nx;
  5418. var ny;
  5419. var nz;
  5420. // Z = 1
  5421. if (this.zOne) {
  5422. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html
  5423. // #doubling-mdbl-2007-bl
  5424. // 1M + 5S + 14A
  5425. // XX = X1^2
  5426. var xx = this.x.redSqr();
  5427. // YY = Y1^2
  5428. var yy = this.y.redSqr();
  5429. // YYYY = YY^2
  5430. var yyyy = yy.redSqr();
  5431. // S = 2 * ((X1 + YY)^2 - XX - YYYY)
  5432. var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
  5433. s = s.redIAdd(s);
  5434. // M = 3 * XX + a; a = 0
  5435. var m = xx.redAdd(xx).redIAdd(xx);
  5436. // T = M ^ 2 - 2*S
  5437. var t = m.redSqr().redISub(s).redISub(s);
  5438. // 8 * YYYY
  5439. var yyyy8 = yyyy.redIAdd(yyyy);
  5440. yyyy8 = yyyy8.redIAdd(yyyy8);
  5441. yyyy8 = yyyy8.redIAdd(yyyy8);
  5442. // X3 = T
  5443. nx = t;
  5444. // Y3 = M * (S - T) - 8 * YYYY
  5445. ny = m.redMul(s.redISub(t)).redISub(yyyy8);
  5446. // Z3 = 2*Y1
  5447. nz = this.y.redAdd(this.y);
  5448. } else {
  5449. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html
  5450. // #doubling-dbl-2009-l
  5451. // 2M + 5S + 13A
  5452. // A = X1^2
  5453. var a = this.x.redSqr();
  5454. // B = Y1^2
  5455. var b = this.y.redSqr();
  5456. // C = B^2
  5457. var c = b.redSqr();
  5458. // D = 2 * ((X1 + B)^2 - A - C)
  5459. var d = this.x.redAdd(b).redSqr().redISub(a).redISub(c);
  5460. d = d.redIAdd(d);
  5461. // E = 3 * A
  5462. var e = a.redAdd(a).redIAdd(a);
  5463. // F = E^2
  5464. var f = e.redSqr();
  5465. // 8 * C
  5466. var c8 = c.redIAdd(c);
  5467. c8 = c8.redIAdd(c8);
  5468. c8 = c8.redIAdd(c8);
  5469. // X3 = F - 2 * D
  5470. nx = f.redISub(d).redISub(d);
  5471. // Y3 = E * (D - X3) - 8 * C
  5472. ny = e.redMul(d.redISub(nx)).redISub(c8);
  5473. // Z3 = 2 * Y1 * Z1
  5474. nz = this.y.redMul(this.z);
  5475. nz = nz.redIAdd(nz);
  5476. }
  5477. return this.curve.jpoint(nx, ny, nz);
  5478. };
  5479. JPoint.prototype._threeDbl = function _threeDbl() {
  5480. var nx;
  5481. var ny;
  5482. var nz;
  5483. // Z = 1
  5484. if (this.zOne) {
  5485. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html
  5486. // #doubling-mdbl-2007-bl
  5487. // 1M + 5S + 15A
  5488. // XX = X1^2
  5489. var xx = this.x.redSqr();
  5490. // YY = Y1^2
  5491. var yy = this.y.redSqr();
  5492. // YYYY = YY^2
  5493. var yyyy = yy.redSqr();
  5494. // S = 2 * ((X1 + YY)^2 - XX - YYYY)
  5495. var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
  5496. s = s.redIAdd(s);
  5497. // M = 3 * XX + a
  5498. var m = xx.redAdd(xx).redIAdd(xx).redIAdd(this.curve.a);
  5499. // T = M^2 - 2 * S
  5500. var t = m.redSqr().redISub(s).redISub(s);
  5501. // X3 = T
  5502. nx = t;
  5503. // Y3 = M * (S - T) - 8 * YYYY
  5504. var yyyy8 = yyyy.redIAdd(yyyy);
  5505. yyyy8 = yyyy8.redIAdd(yyyy8);
  5506. yyyy8 = yyyy8.redIAdd(yyyy8);
  5507. ny = m.redMul(s.redISub(t)).redISub(yyyy8);
  5508. // Z3 = 2 * Y1
  5509. nz = this.y.redAdd(this.y);
  5510. } else {
  5511. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  5512. // 3M + 5S
  5513. // delta = Z1^2
  5514. var delta = this.z.redSqr();
  5515. // gamma = Y1^2
  5516. var gamma = this.y.redSqr();
  5517. // beta = X1 * gamma
  5518. var beta = this.x.redMul(gamma);
  5519. // alpha = 3 * (X1 - delta) * (X1 + delta)
  5520. var alpha = this.x.redSub(delta).redMul(this.x.redAdd(delta));
  5521. alpha = alpha.redAdd(alpha).redIAdd(alpha);
  5522. // X3 = alpha^2 - 8 * beta
  5523. var beta4 = beta.redIAdd(beta);
  5524. beta4 = beta4.redIAdd(beta4);
  5525. var beta8 = beta4.redAdd(beta4);
  5526. nx = alpha.redSqr().redISub(beta8);
  5527. // Z3 = (Y1 + Z1)^2 - gamma - delta
  5528. nz = this.y.redAdd(this.z).redSqr().redISub(gamma).redISub(delta);
  5529. // Y3 = alpha * (4 * beta - X3) - 8 * gamma^2
  5530. var ggamma8 = gamma.redSqr();
  5531. ggamma8 = ggamma8.redIAdd(ggamma8);
  5532. ggamma8 = ggamma8.redIAdd(ggamma8);
  5533. ggamma8 = ggamma8.redIAdd(ggamma8);
  5534. ny = alpha.redMul(beta4.redISub(nx)).redISub(ggamma8);
  5535. }
  5536. return this.curve.jpoint(nx, ny, nz);
  5537. };
  5538. JPoint.prototype._dbl = function _dbl() {
  5539. var a = this.curve.a;
  5540. // 4M + 6S + 10A
  5541. var jx = this.x;
  5542. var jy = this.y;
  5543. var jz = this.z;
  5544. var jz4 = jz.redSqr().redSqr();
  5545. var jx2 = jx.redSqr();
  5546. var jy2 = jy.redSqr();
  5547. var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
  5548. var jxd4 = jx.redAdd(jx);
  5549. jxd4 = jxd4.redIAdd(jxd4);
  5550. var t1 = jxd4.redMul(jy2);
  5551. var nx = c.redSqr().redISub(t1.redAdd(t1));
  5552. var t2 = t1.redISub(nx);
  5553. var jyd8 = jy2.redSqr();
  5554. jyd8 = jyd8.redIAdd(jyd8);
  5555. jyd8 = jyd8.redIAdd(jyd8);
  5556. jyd8 = jyd8.redIAdd(jyd8);
  5557. var ny = c.redMul(t2).redISub(jyd8);
  5558. var nz = jy.redAdd(jy).redMul(jz);
  5559. return this.curve.jpoint(nx, ny, nz);
  5560. };
  5561. JPoint.prototype.trpl = function trpl() {
  5562. if (!this.curve.zeroA)
  5563. return this.dbl().add(this);
  5564. // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#tripling-tpl-2007-bl
  5565. // 5M + 10S + ...
  5566. // XX = X1^2
  5567. var xx = this.x.redSqr();
  5568. // YY = Y1^2
  5569. var yy = this.y.redSqr();
  5570. // ZZ = Z1^2
  5571. var zz = this.z.redSqr();
  5572. // YYYY = YY^2
  5573. var yyyy = yy.redSqr();
  5574. // M = 3 * XX + a * ZZ2; a = 0
  5575. var m = xx.redAdd(xx).redIAdd(xx);
  5576. // MM = M^2
  5577. var mm = m.redSqr();
  5578. // E = 6 * ((X1 + YY)^2 - XX - YYYY) - MM
  5579. var e = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
  5580. e = e.redIAdd(e);
  5581. e = e.redAdd(e).redIAdd(e);
  5582. e = e.redISub(mm);
  5583. // EE = E^2
  5584. var ee = e.redSqr();
  5585. // T = 16*YYYY
  5586. var t = yyyy.redIAdd(yyyy);
  5587. t = t.redIAdd(t);
  5588. t = t.redIAdd(t);
  5589. t = t.redIAdd(t);
  5590. // U = (M + E)^2 - MM - EE - T
  5591. var u = m.redIAdd(e).redSqr().redISub(mm).redISub(ee).redISub(t);
  5592. // X3 = 4 * (X1 * EE - 4 * YY * U)
  5593. var yyu4 = yy.redMul(u);
  5594. yyu4 = yyu4.redIAdd(yyu4);
  5595. yyu4 = yyu4.redIAdd(yyu4);
  5596. var nx = this.x.redMul(ee).redISub(yyu4);
  5597. nx = nx.redIAdd(nx);
  5598. nx = nx.redIAdd(nx);
  5599. // Y3 = 8 * Y1 * (U * (T - U) - E * EE)
  5600. var ny = this.y.redMul(u.redMul(t.redISub(u)).redISub(e.redMul(ee)));
  5601. ny = ny.redIAdd(ny);
  5602. ny = ny.redIAdd(ny);
  5603. ny = ny.redIAdd(ny);
  5604. // Z3 = (Z1 + E)^2 - ZZ - EE
  5605. var nz = this.z.redAdd(e).redSqr().redISub(zz).redISub(ee);
  5606. return this.curve.jpoint(nx, ny, nz);
  5607. };
  5608. JPoint.prototype.mul = function mul(k, kbase) {
  5609. k = new BN(k, kbase);
  5610. return this.curve._wnafMul(this, k);
  5611. };
  5612. JPoint.prototype.eq = function eq(p) {
  5613. if (p.type === 'affine')
  5614. return this.eq(p.toJ());
  5615. if (this === p)
  5616. return true;
  5617. // x1 * z2^2 == x2 * z1^2
  5618. var z2 = this.z.redSqr();
  5619. var pz2 = p.z.redSqr();
  5620. if (this.x.redMul(pz2).redISub(p.x.redMul(z2)).cmpn(0) !== 0)
  5621. return false;
  5622. // y1 * z2^3 == y2 * z1^3
  5623. var z3 = z2.redMul(this.z);
  5624. var pz3 = pz2.redMul(p.z);
  5625. return this.y.redMul(pz3).redISub(p.y.redMul(z3)).cmpn(0) === 0;
  5626. };
  5627. JPoint.prototype.eqXToP = function eqXToP(x) {
  5628. var zs = this.z.redSqr();
  5629. var rx = x.toRed(this.curve.red).redMul(zs);
  5630. if (this.x.cmp(rx) === 0)
  5631. return true;
  5632. var xc = x.clone();
  5633. var t = this.curve.redN.redMul(zs);
  5634. for (;;) {
  5635. xc.iadd(this.curve.n);
  5636. if (xc.cmp(this.curve.p) >= 0)
  5637. return false;
  5638. rx.redIAdd(t);
  5639. if (this.x.cmp(rx) === 0)
  5640. return true;
  5641. }
  5642. return false;
  5643. };
  5644. JPoint.prototype.inspect = function inspect() {
  5645. if (this.isInfinity())
  5646. return '<EC JPoint Infinity>';
  5647. return '<EC JPoint x: ' + this.x.toString(16, 2) +
  5648. ' y: ' + this.y.toString(16, 2) +
  5649. ' z: ' + this.z.toString(16, 2) + '>';
  5650. };
  5651. JPoint.prototype.isInfinity = function isInfinity() {
  5652. // XXX This code assumes that zero is always zero in red
  5653. return this.z.cmpn(0) === 0;
  5654. };
  5655. },{"../../elliptic":10,"../curve":13,"bn.js":4,"inherits":34}],16:[function(require,module,exports){
  5656. 'use strict';
  5657. var curves = exports;
  5658. var hash = require('hash.js');
  5659. var elliptic = require('../elliptic');
  5660. var assert = elliptic.utils.assert;
  5661. function PresetCurve(options) {
  5662. if (options.type === 'short')
  5663. this.curve = new elliptic.curve.short(options);
  5664. else if (options.type === 'edwards')
  5665. this.curve = new elliptic.curve.edwards(options);
  5666. else
  5667. this.curve = new elliptic.curve.mont(options);
  5668. this.g = this.curve.g;
  5669. this.n = this.curve.n;
  5670. this.hash = options.hash;
  5671. assert(this.g.validate(), 'Invalid curve');
  5672. assert(this.g.mul(this.n).isInfinity(), 'Invalid curve, G*N != O');
  5673. }
  5674. curves.PresetCurve = PresetCurve;
  5675. function defineCurve(name, options) {
  5676. Object.defineProperty(curves, name, {
  5677. configurable: true,
  5678. enumerable: true,
  5679. get: function() {
  5680. var curve = new PresetCurve(options);
  5681. Object.defineProperty(curves, name, {
  5682. configurable: true,
  5683. enumerable: true,
  5684. value: curve
  5685. });
  5686. return curve;
  5687. }
  5688. });
  5689. }
  5690. defineCurve('p192', {
  5691. type: 'short',
  5692. prime: 'p192',
  5693. p: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff',
  5694. a: 'ffffffff ffffffff ffffffff fffffffe ffffffff fffffffc',
  5695. b: '64210519 e59c80e7 0fa7e9ab 72243049 feb8deec c146b9b1',
  5696. n: 'ffffffff ffffffff ffffffff 99def836 146bc9b1 b4d22831',
  5697. hash: hash.sha256,
  5698. gRed: false,
  5699. g: [
  5700. '188da80e b03090f6 7cbf20eb 43a18800 f4ff0afd 82ff1012',
  5701. '07192b95 ffc8da78 631011ed 6b24cdd5 73f977a1 1e794811'
  5702. ]
  5703. });
  5704. defineCurve('p224', {
  5705. type: 'short',
  5706. prime: 'p224',
  5707. p: 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001',
  5708. a: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe',
  5709. b: 'b4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4',
  5710. n: 'ffffffff ffffffff ffffffff ffff16a2 e0b8f03e 13dd2945 5c5c2a3d',
  5711. hash: hash.sha256,
  5712. gRed: false,
  5713. g: [
  5714. 'b70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21',
  5715. 'bd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34'
  5716. ]
  5717. });
  5718. defineCurve('p256', {
  5719. type: 'short',
  5720. prime: null,
  5721. p: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff',
  5722. a: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff fffffffc',
  5723. b: '5ac635d8 aa3a93e7 b3ebbd55 769886bc 651d06b0 cc53b0f6 3bce3c3e 27d2604b',
  5724. n: 'ffffffff 00000000 ffffffff ffffffff bce6faad a7179e84 f3b9cac2 fc632551',
  5725. hash: hash.sha256,
  5726. gRed: false,
  5727. g: [
  5728. '6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296',
  5729. '4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5'
  5730. ]
  5731. });
  5732. defineCurve('p384', {
  5733. type: 'short',
  5734. prime: null,
  5735. p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  5736. 'fffffffe ffffffff 00000000 00000000 ffffffff',
  5737. a: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  5738. 'fffffffe ffffffff 00000000 00000000 fffffffc',
  5739. b: 'b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112 0314088f ' +
  5740. '5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef',
  5741. n: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff c7634d81 ' +
  5742. 'f4372ddf 581a0db2 48b0a77a ecec196a ccc52973',
  5743. hash: hash.sha384,
  5744. gRed: false,
  5745. g: [
  5746. 'aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98 59f741e0 82542a38 ' +
  5747. '5502f25d bf55296c 3a545e38 72760ab7',
  5748. '3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c e9da3113 b5f0b8c0 ' +
  5749. '0a60b1ce 1d7e819d 7a431d7c 90ea0e5f'
  5750. ]
  5751. });
  5752. defineCurve('p521', {
  5753. type: 'short',
  5754. prime: null,
  5755. p: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  5756. 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  5757. 'ffffffff ffffffff ffffffff ffffffff ffffffff',
  5758. a: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  5759. 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  5760. 'ffffffff ffffffff ffffffff ffffffff fffffffc',
  5761. b: '00000051 953eb961 8e1c9a1f 929a21a0 b68540ee a2da725b ' +
  5762. '99b315f3 b8b48991 8ef109e1 56193951 ec7e937b 1652c0bd ' +
  5763. '3bb1bf07 3573df88 3d2c34f1 ef451fd4 6b503f00',
  5764. n: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
  5765. 'ffffffff ffffffff fffffffa 51868783 bf2f966b 7fcc0148 ' +
  5766. 'f709a5d0 3bb5c9b8 899c47ae bb6fb71e 91386409',
  5767. hash: hash.sha512,
  5768. gRed: false,
  5769. g: [
  5770. '000000c6 858e06b7 0404e9cd 9e3ecb66 2395b442 9c648139 ' +
  5771. '053fb521 f828af60 6b4d3dba a14b5e77 efe75928 fe1dc127 ' +
  5772. 'a2ffa8de 3348b3c1 856a429b f97e7e31 c2e5bd66',
  5773. '00000118 39296a78 9a3bc004 5c8a5fb4 2c7d1bd9 98f54449 ' +
  5774. '579b4468 17afbd17 273e662c 97ee7299 5ef42640 c550b901 ' +
  5775. '3fad0761 353c7086 a272c240 88be9476 9fd16650'
  5776. ]
  5777. });
  5778. defineCurve('curve25519', {
  5779. type: 'mont',
  5780. prime: 'p25519',
  5781. p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
  5782. a: '76d06',
  5783. b: '1',
  5784. n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
  5785. hash: hash.sha256,
  5786. gRed: false,
  5787. g: [
  5788. '9'
  5789. ]
  5790. });
  5791. defineCurve('ed25519', {
  5792. type: 'edwards',
  5793. prime: 'p25519',
  5794. p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
  5795. a: '-1',
  5796. c: '1',
  5797. // -121665 * (121666^(-1)) (mod P)
  5798. d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
  5799. n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
  5800. hash: hash.sha256,
  5801. gRed: false,
  5802. g: [
  5803. '216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a',
  5804. // 4/5
  5805. '6666666666666666666666666666666666666666666666666666666666666658'
  5806. ]
  5807. });
  5808. var pre;
  5809. try {
  5810. pre = require('./precomputed/secp256k1');
  5811. } catch (e) {
  5812. pre = undefined;
  5813. }
  5814. defineCurve('secp256k1', {
  5815. type: 'short',
  5816. prime: 'k256',
  5817. p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f',
  5818. a: '0',
  5819. b: '7',
  5820. n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141',
  5821. h: '1',
  5822. hash: hash.sha256,
  5823. // Precomputed endomorphism
  5824. beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee',
  5825. lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72',
  5826. basis: [
  5827. {
  5828. a: '3086d221a7d46bcde86c90e49284eb15',
  5829. b: '-e4437ed6010e88286f547fa90abfe4c3'
  5830. },
  5831. {
  5832. a: '114ca50f7a8e2f3f657c1108d9d44cfd8',
  5833. b: '3086d221a7d46bcde86c90e49284eb15'
  5834. }
  5835. ],
  5836. gRed: false,
  5837. g: [
  5838. '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
  5839. '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
  5840. pre
  5841. ]
  5842. });
  5843. },{"../elliptic":10,"./precomputed/secp256k1":23,"hash.js":27}],17:[function(require,module,exports){
  5844. 'use strict';
  5845. var BN = require('bn.js');
  5846. var HmacDRBG = require('hmac-drbg');
  5847. var elliptic = require('../../elliptic');
  5848. var utils = elliptic.utils;
  5849. var assert = utils.assert;
  5850. var KeyPair = require('./key');
  5851. var Signature = require('./signature');
  5852. function EC(options) {
  5853. if (!(this instanceof EC))
  5854. return new EC(options);
  5855. // Shortcut `elliptic.ec(curve-name)`
  5856. if (typeof options === 'string') {
  5857. assert(elliptic.curves.hasOwnProperty(options), 'Unknown curve ' + options);
  5858. options = elliptic.curves[options];
  5859. }
  5860. // Shortcut for `elliptic.ec(elliptic.curves.curveName)`
  5861. if (options instanceof elliptic.curves.PresetCurve)
  5862. options = { curve: options };
  5863. this.curve = options.curve.curve;
  5864. this.n = this.curve.n;
  5865. this.nh = this.n.ushrn(1);
  5866. this.g = this.curve.g;
  5867. // Point on curve
  5868. this.g = options.curve.g;
  5869. this.g.precompute(options.curve.n.bitLength() + 1);
  5870. // Hash for function for DRBG
  5871. this.hash = options.hash || options.curve.hash;
  5872. }
  5873. module.exports = EC;
  5874. EC.prototype.keyPair = function keyPair(options) {
  5875. return new KeyPair(this, options);
  5876. };
  5877. EC.prototype.keyFromPrivate = function keyFromPrivate(priv, enc) {
  5878. return KeyPair.fromPrivate(this, priv, enc);
  5879. };
  5880. EC.prototype.keyFromPublic = function keyFromPublic(pub, enc) {
  5881. return KeyPair.fromPublic(this, pub, enc);
  5882. };
  5883. EC.prototype.genKeyPair = function genKeyPair(options) {
  5884. if (!options)
  5885. options = {};
  5886. // Instantiate Hmac_DRBG
  5887. var drbg = new HmacDRBG({
  5888. hash: this.hash,
  5889. pers: options.pers,
  5890. persEnc: options.persEnc || 'utf8',
  5891. entropy: options.entropy || elliptic.rand(this.hash.hmacStrength),
  5892. entropyEnc: options.entropy && options.entropyEnc || 'utf8',
  5893. nonce: this.n.toArray()
  5894. });
  5895. var bytes = this.n.byteLength();
  5896. var ns2 = this.n.sub(new BN(2));
  5897. do {
  5898. var priv = new BN(drbg.generate(bytes));
  5899. if (priv.cmp(ns2) > 0)
  5900. continue;
  5901. priv.iaddn(1);
  5902. return this.keyFromPrivate(priv);
  5903. } while (true);
  5904. };
  5905. EC.prototype._truncateToN = function truncateToN(msg, truncOnly) {
  5906. var delta = msg.byteLength() * 8 - this.n.bitLength();
  5907. if (delta > 0)
  5908. msg = msg.ushrn(delta);
  5909. if (!truncOnly && msg.cmp(this.n) >= 0)
  5910. return msg.sub(this.n);
  5911. else
  5912. return msg;
  5913. };
  5914. EC.prototype.sign = function sign(msg, key, enc, options) {
  5915. if (typeof enc === 'object') {
  5916. options = enc;
  5917. enc = null;
  5918. }
  5919. if (!options)
  5920. options = {};
  5921. key = this.keyFromPrivate(key, enc);
  5922. msg = this._truncateToN(new BN(msg, 16));
  5923. // Zero-extend key to provide enough entropy
  5924. var bytes = this.n.byteLength();
  5925. var bkey = key.getPrivate().toArray('be', bytes);
  5926. // Zero-extend nonce to have the same byte size as N
  5927. var nonce = msg.toArray('be', bytes);
  5928. // Instantiate Hmac_DRBG
  5929. var drbg = new HmacDRBG({
  5930. hash: this.hash,
  5931. entropy: bkey,
  5932. nonce: nonce,
  5933. pers: options.pers,
  5934. persEnc: options.persEnc || 'utf8'
  5935. });
  5936. // Number of bytes to generate
  5937. var ns1 = this.n.sub(new BN(1));
  5938. for (var iter = 0; true; iter++) {
  5939. var k = options.k ?
  5940. options.k(iter) :
  5941. new BN(drbg.generate(this.n.byteLength()));
  5942. k = this._truncateToN(k, true);
  5943. if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0)
  5944. continue;
  5945. var kp = this.g.mul(k);
  5946. if (kp.isInfinity())
  5947. continue;
  5948. var kpX = kp.getX();
  5949. var r = kpX.umod(this.n);
  5950. if (r.cmpn(0) === 0)
  5951. continue;
  5952. var s = k.invm(this.n).mul(r.mul(key.getPrivate()).iadd(msg));
  5953. s = s.umod(this.n);
  5954. if (s.cmpn(0) === 0)
  5955. continue;
  5956. var recoveryParam = (kp.getY().isOdd() ? 1 : 0) |
  5957. (kpX.cmp(r) !== 0 ? 2 : 0);
  5958. // Use complement of `s`, if it is > `n / 2`
  5959. if (options.canonical && s.cmp(this.nh) > 0) {
  5960. s = this.n.sub(s);
  5961. recoveryParam ^= 1;
  5962. }
  5963. return new Signature({ r: r, s: s, recoveryParam: recoveryParam });
  5964. }
  5965. };
  5966. EC.prototype.verify = function verify(msg, signature, key, enc) {
  5967. msg = this._truncateToN(new BN(msg, 16));
  5968. key = this.keyFromPublic(key, enc);
  5969. signature = new Signature(signature, 'hex');
  5970. // Perform primitive values validation
  5971. var r = signature.r;
  5972. var s = signature.s;
  5973. if (r.cmpn(1) < 0 || r.cmp(this.n) >= 0)
  5974. return false;
  5975. if (s.cmpn(1) < 0 || s.cmp(this.n) >= 0)
  5976. return false;
  5977. // Validate signature
  5978. var sinv = s.invm(this.n);
  5979. var u1 = sinv.mul(msg).umod(this.n);
  5980. var u2 = sinv.mul(r).umod(this.n);
  5981. if (!this.curve._maxwellTrick) {
  5982. var p = this.g.mulAdd(u1, key.getPublic(), u2);
  5983. if (p.isInfinity())
  5984. return false;
  5985. return p.getX().umod(this.n).cmp(r) === 0;
  5986. }
  5987. // NOTE: Greg Maxwell's trick, inspired by:
  5988. // https://git.io/vad3K
  5989. var p = this.g.jmulAdd(u1, key.getPublic(), u2);
  5990. if (p.isInfinity())
  5991. return false;
  5992. // Compare `p.x` of Jacobian point with `r`,
  5993. // this will do `p.x == r * p.z^2` instead of multiplying `p.x` by the
  5994. // inverse of `p.z^2`
  5995. return p.eqXToP(r);
  5996. };
  5997. EC.prototype.recoverPubKey = function(msg, signature, j, enc) {
  5998. assert((3 & j) === j, 'The recovery param is more than two bits');
  5999. signature = new Signature(signature, enc);
  6000. var n = this.n;
  6001. var e = new BN(msg);
  6002. var r = signature.r;
  6003. var s = signature.s;
  6004. // A set LSB signifies that the y-coordinate is odd
  6005. var isYOdd = j & 1;
  6006. var isSecondKey = j >> 1;
  6007. if (r.cmp(this.curve.p.umod(this.curve.n)) >= 0 && isSecondKey)
  6008. throw new Error('Unable to find sencond key candinate');
  6009. // 1.1. Let x = r + jn.
  6010. if (isSecondKey)
  6011. r = this.curve.pointFromX(r.add(this.curve.n), isYOdd);
  6012. else
  6013. r = this.curve.pointFromX(r, isYOdd);
  6014. var rInv = signature.r.invm(n);
  6015. var s1 = n.sub(e).mul(rInv).umod(n);
  6016. var s2 = s.mul(rInv).umod(n);
  6017. // 1.6.1 Compute Q = r^-1 (sR - eG)
  6018. // Q = r^-1 (sR + -eG)
  6019. return this.g.mulAdd(s1, r, s2);
  6020. };
  6021. EC.prototype.getKeyRecoveryParam = function(e, signature, Q, enc) {
  6022. signature = new Signature(signature, enc);
  6023. if (signature.recoveryParam !== null)
  6024. return signature.recoveryParam;
  6025. for (var i = 0; i < 4; i++) {
  6026. var Qprime;
  6027. try {
  6028. Qprime = this.recoverPubKey(e, signature, i);
  6029. } catch (e) {
  6030. continue;
  6031. }
  6032. if (Qprime.eq(Q))
  6033. return i;
  6034. }
  6035. throw new Error('Unable to find valid recovery factor');
  6036. };
  6037. },{"../../elliptic":10,"./key":18,"./signature":19,"bn.js":4,"hmac-drbg":33}],18:[function(require,module,exports){
  6038. 'use strict';
  6039. var BN = require('bn.js');
  6040. var elliptic = require('../../elliptic');
  6041. var utils = elliptic.utils;
  6042. var assert = utils.assert;
  6043. function KeyPair(ec, options) {
  6044. this.ec = ec;
  6045. this.priv = null;
  6046. this.pub = null;
  6047. // KeyPair(ec, { priv: ..., pub: ... })
  6048. if (options.priv)
  6049. this._importPrivate(options.priv, options.privEnc);
  6050. if (options.pub)
  6051. this._importPublic(options.pub, options.pubEnc);
  6052. }
  6053. module.exports = KeyPair;
  6054. KeyPair.fromPublic = function fromPublic(ec, pub, enc) {
  6055. if (pub instanceof KeyPair)
  6056. return pub;
  6057. return new KeyPair(ec, {
  6058. pub: pub,
  6059. pubEnc: enc
  6060. });
  6061. };
  6062. KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) {
  6063. if (priv instanceof KeyPair)
  6064. return priv;
  6065. return new KeyPair(ec, {
  6066. priv: priv,
  6067. privEnc: enc
  6068. });
  6069. };
  6070. KeyPair.prototype.validate = function validate() {
  6071. var pub = this.getPublic();
  6072. if (pub.isInfinity())
  6073. return { result: false, reason: 'Invalid public key' };
  6074. if (!pub.validate())
  6075. return { result: false, reason: 'Public key is not a point' };
  6076. if (!pub.mul(this.ec.curve.n).isInfinity())
  6077. return { result: false, reason: 'Public key * N != O' };
  6078. return { result: true, reason: null };
  6079. };
  6080. KeyPair.prototype.getPublic = function getPublic(compact, enc) {
  6081. // compact is optional argument
  6082. if (typeof compact === 'string') {
  6083. enc = compact;
  6084. compact = null;
  6085. }
  6086. if (!this.pub)
  6087. this.pub = this.ec.g.mul(this.priv);
  6088. if (!enc)
  6089. return this.pub;
  6090. return this.pub.encode(enc, compact);
  6091. };
  6092. KeyPair.prototype.getPrivate = function getPrivate(enc) {
  6093. if (enc === 'hex')
  6094. return this.priv.toString(16, 2);
  6095. else
  6096. return this.priv;
  6097. };
  6098. KeyPair.prototype._importPrivate = function _importPrivate(key, enc) {
  6099. this.priv = new BN(key, enc || 16);
  6100. // Ensure that the priv won't be bigger than n, otherwise we may fail
  6101. // in fixed multiplication method
  6102. this.priv = this.priv.umod(this.ec.curve.n);
  6103. };
  6104. KeyPair.prototype._importPublic = function _importPublic(key, enc) {
  6105. if (key.x || key.y) {
  6106. // Montgomery points only have an `x` coordinate.
  6107. // Weierstrass/Edwards points on the other hand have both `x` and
  6108. // `y` coordinates.
  6109. if (this.ec.curve.type === 'mont') {
  6110. assert(key.x, 'Need x coordinate');
  6111. } else if (this.ec.curve.type === 'short' ||
  6112. this.ec.curve.type === 'edwards') {
  6113. assert(key.x && key.y, 'Need both x and y coordinate');
  6114. }
  6115. this.pub = this.ec.curve.point(key.x, key.y);
  6116. return;
  6117. }
  6118. this.pub = this.ec.curve.decodePoint(key, enc);
  6119. };
  6120. // ECDH
  6121. KeyPair.prototype.derive = function derive(pub) {
  6122. return pub.mul(this.priv).getX();
  6123. };
  6124. // ECDSA
  6125. KeyPair.prototype.sign = function sign(msg, enc, options) {
  6126. return this.ec.sign(msg, this, enc, options);
  6127. };
  6128. KeyPair.prototype.verify = function verify(msg, signature) {
  6129. return this.ec.verify(msg, signature, this);
  6130. };
  6131. KeyPair.prototype.inspect = function inspect() {
  6132. return '<Key priv: ' + (this.priv && this.priv.toString(16, 2)) +
  6133. ' pub: ' + (this.pub && this.pub.inspect()) + ' >';
  6134. };
  6135. },{"../../elliptic":10,"bn.js":4}],19:[function(require,module,exports){
  6136. 'use strict';
  6137. var BN = require('bn.js');
  6138. var elliptic = require('../../elliptic');
  6139. var utils = elliptic.utils;
  6140. var assert = utils.assert;
  6141. function Signature(options, enc) {
  6142. if (options instanceof Signature)
  6143. return options;
  6144. if (this._importDER(options, enc))
  6145. return;
  6146. assert(options.r && options.s, 'Signature without r or s');
  6147. this.r = new BN(options.r, 16);
  6148. this.s = new BN(options.s, 16);
  6149. if (options.recoveryParam === undefined)
  6150. this.recoveryParam = null;
  6151. else
  6152. this.recoveryParam = options.recoveryParam;
  6153. }
  6154. module.exports = Signature;
  6155. function Position() {
  6156. this.place = 0;
  6157. }
  6158. function getLength(buf, p) {
  6159. var initial = buf[p.place++];
  6160. if (!(initial & 0x80)) {
  6161. return initial;
  6162. }
  6163. var octetLen = initial & 0xf;
  6164. var val = 0;
  6165. for (var i = 0, off = p.place; i < octetLen; i++, off++) {
  6166. val <<= 8;
  6167. val |= buf[off];
  6168. }
  6169. p.place = off;
  6170. return val;
  6171. }
  6172. function rmPadding(buf) {
  6173. var i = 0;
  6174. var len = buf.length - 1;
  6175. while (!buf[i] && !(buf[i + 1] & 0x80) && i < len) {
  6176. i++;
  6177. }
  6178. if (i === 0) {
  6179. return buf;
  6180. }
  6181. return buf.slice(i);
  6182. }
  6183. Signature.prototype._importDER = function _importDER(data, enc) {
  6184. data = utils.toArray(data, enc);
  6185. var p = new Position();
  6186. if (data[p.place++] !== 0x30) {
  6187. return false;
  6188. }
  6189. var len = getLength(data, p);
  6190. if ((len + p.place) !== data.length) {
  6191. return false;
  6192. }
  6193. if (data[p.place++] !== 0x02) {
  6194. return false;
  6195. }
  6196. var rlen = getLength(data, p);
  6197. var r = data.slice(p.place, rlen + p.place);
  6198. p.place += rlen;
  6199. if (data[p.place++] !== 0x02) {
  6200. return false;
  6201. }
  6202. var slen = getLength(data, p);
  6203. if (data.length !== slen + p.place) {
  6204. return false;
  6205. }
  6206. var s = data.slice(p.place, slen + p.place);
  6207. if (r[0] === 0 && (r[1] & 0x80)) {
  6208. r = r.slice(1);
  6209. }
  6210. if (s[0] === 0 && (s[1] & 0x80)) {
  6211. s = s.slice(1);
  6212. }
  6213. this.r = new BN(r);
  6214. this.s = new BN(s);
  6215. this.recoveryParam = null;
  6216. return true;
  6217. };
  6218. function constructLength(arr, len) {
  6219. if (len < 0x80) {
  6220. arr.push(len);
  6221. return;
  6222. }
  6223. var octets = 1 + (Math.log(len) / Math.LN2 >>> 3);
  6224. arr.push(octets | 0x80);
  6225. while (--octets) {
  6226. arr.push((len >>> (octets << 3)) & 0xff);
  6227. }
  6228. arr.push(len);
  6229. }
  6230. Signature.prototype.toDER = function toDER(enc) {
  6231. var r = this.r.toArray();
  6232. var s = this.s.toArray();
  6233. // Pad values
  6234. if (r[0] & 0x80)
  6235. r = [ 0 ].concat(r);
  6236. // Pad values
  6237. if (s[0] & 0x80)
  6238. s = [ 0 ].concat(s);
  6239. r = rmPadding(r);
  6240. s = rmPadding(s);
  6241. while (!s[0] && !(s[1] & 0x80)) {
  6242. s = s.slice(1);
  6243. }
  6244. var arr = [ 0x02 ];
  6245. constructLength(arr, r.length);
  6246. arr = arr.concat(r);
  6247. arr.push(0x02);
  6248. constructLength(arr, s.length);
  6249. var backHalf = arr.concat(s);
  6250. var res = [ 0x30 ];
  6251. constructLength(res, backHalf.length);
  6252. res = res.concat(backHalf);
  6253. return utils.encode(res, enc);
  6254. };
  6255. },{"../../elliptic":10,"bn.js":4}],20:[function(require,module,exports){
  6256. 'use strict';
  6257. var hash = require('hash.js');
  6258. var elliptic = require('../../elliptic');
  6259. var utils = elliptic.utils;
  6260. var assert = utils.assert;
  6261. var parseBytes = utils.parseBytes;
  6262. var KeyPair = require('./key');
  6263. var Signature = require('./signature');
  6264. function EDDSA(curve) {
  6265. assert(curve === 'ed25519', 'only tested with ed25519 so far');
  6266. if (!(this instanceof EDDSA))
  6267. return new EDDSA(curve);
  6268. var curve = elliptic.curves[curve].curve;
  6269. this.curve = curve;
  6270. this.g = curve.g;
  6271. this.g.precompute(curve.n.bitLength() + 1);
  6272. this.pointClass = curve.point().constructor;
  6273. this.encodingLength = Math.ceil(curve.n.bitLength() / 8);
  6274. this.hash = hash.sha512;
  6275. }
  6276. module.exports = EDDSA;
  6277. /**
  6278. * @param {Array|String} message - message bytes
  6279. * @param {Array|String|KeyPair} secret - secret bytes or a keypair
  6280. * @returns {Signature} - signature
  6281. */
  6282. EDDSA.prototype.sign = function sign(message, secret) {
  6283. message = parseBytes(message);
  6284. var key = this.keyFromSecret(secret);
  6285. var r = this.hashInt(key.messagePrefix(), message);
  6286. var R = this.g.mul(r);
  6287. var Rencoded = this.encodePoint(R);
  6288. var s_ = this.hashInt(Rencoded, key.pubBytes(), message)
  6289. .mul(key.priv());
  6290. var S = r.add(s_).umod(this.curve.n);
  6291. return this.makeSignature({ R: R, S: S, Rencoded: Rencoded });
  6292. };
  6293. /**
  6294. * @param {Array} message - message bytes
  6295. * @param {Array|String|Signature} sig - sig bytes
  6296. * @param {Array|String|Point|KeyPair} pub - public key
  6297. * @returns {Boolean} - true if public key matches sig of message
  6298. */
  6299. EDDSA.prototype.verify = function verify(message, sig, pub) {
  6300. message = parseBytes(message);
  6301. sig = this.makeSignature(sig);
  6302. var key = this.keyFromPublic(pub);
  6303. var h = this.hashInt(sig.Rencoded(), key.pubBytes(), message);
  6304. var SG = this.g.mul(sig.S());
  6305. var RplusAh = sig.R().add(key.pub().mul(h));
  6306. return RplusAh.eq(SG);
  6307. };
  6308. EDDSA.prototype.hashInt = function hashInt() {
  6309. var hash = this.hash();
  6310. for (var i = 0; i < arguments.length; i++)
  6311. hash.update(arguments[i]);
  6312. return utils.intFromLE(hash.digest()).umod(this.curve.n);
  6313. };
  6314. EDDSA.prototype.keyFromPublic = function keyFromPublic(pub) {
  6315. return KeyPair.fromPublic(this, pub);
  6316. };
  6317. EDDSA.prototype.keyFromSecret = function keyFromSecret(secret) {
  6318. return KeyPair.fromSecret(this, secret);
  6319. };
  6320. EDDSA.prototype.makeSignature = function makeSignature(sig) {
  6321. if (sig instanceof Signature)
  6322. return sig;
  6323. return new Signature(this, sig);
  6324. };
  6325. /**
  6326. * * https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03#section-5.2
  6327. *
  6328. * EDDSA defines methods for encoding and decoding points and integers. These are
  6329. * helper convenience methods, that pass along to utility functions implied
  6330. * parameters.
  6331. *
  6332. */
  6333. EDDSA.prototype.encodePoint = function encodePoint(point) {
  6334. var enc = point.getY().toArray('le', this.encodingLength);
  6335. enc[this.encodingLength - 1] |= point.getX().isOdd() ? 0x80 : 0;
  6336. return enc;
  6337. };
  6338. EDDSA.prototype.decodePoint = function decodePoint(bytes) {
  6339. bytes = utils.parseBytes(bytes);
  6340. var lastIx = bytes.length - 1;
  6341. var normed = bytes.slice(0, lastIx).concat(bytes[lastIx] & ~0x80);
  6342. var xIsOdd = (bytes[lastIx] & 0x80) !== 0;
  6343. var y = utils.intFromLE(normed);
  6344. return this.curve.pointFromY(y, xIsOdd);
  6345. };
  6346. EDDSA.prototype.encodeInt = function encodeInt(num) {
  6347. return num.toArray('le', this.encodingLength);
  6348. };
  6349. EDDSA.prototype.decodeInt = function decodeInt(bytes) {
  6350. return utils.intFromLE(bytes);
  6351. };
  6352. EDDSA.prototype.isPoint = function isPoint(val) {
  6353. return val instanceof this.pointClass;
  6354. };
  6355. },{"../../elliptic":10,"./key":21,"./signature":22,"hash.js":27}],21:[function(require,module,exports){
  6356. 'use strict';
  6357. var elliptic = require('../../elliptic');
  6358. var utils = elliptic.utils;
  6359. var assert = utils.assert;
  6360. var parseBytes = utils.parseBytes;
  6361. var cachedProperty = utils.cachedProperty;
  6362. /**
  6363. * @param {EDDSA} eddsa - instance
  6364. * @param {Object} params - public/private key parameters
  6365. *
  6366. * @param {Array<Byte>} [params.secret] - secret seed bytes
  6367. * @param {Point} [params.pub] - public key point (aka `A` in eddsa terms)
  6368. * @param {Array<Byte>} [params.pub] - public key point encoded as bytes
  6369. *
  6370. */
  6371. function KeyPair(eddsa, params) {
  6372. this.eddsa = eddsa;
  6373. this._secret = parseBytes(params.secret);
  6374. if (eddsa.isPoint(params.pub))
  6375. this._pub = params.pub;
  6376. else
  6377. this._pubBytes = parseBytes(params.pub);
  6378. }
  6379. KeyPair.fromPublic = function fromPublic(eddsa, pub) {
  6380. if (pub instanceof KeyPair)
  6381. return pub;
  6382. return new KeyPair(eddsa, { pub: pub });
  6383. };
  6384. KeyPair.fromSecret = function fromSecret(eddsa, secret) {
  6385. if (secret instanceof KeyPair)
  6386. return secret;
  6387. return new KeyPair(eddsa, { secret: secret });
  6388. };
  6389. KeyPair.prototype.secret = function secret() {
  6390. return this._secret;
  6391. };
  6392. cachedProperty(KeyPair, 'pubBytes', function pubBytes() {
  6393. return this.eddsa.encodePoint(this.pub());
  6394. });
  6395. cachedProperty(KeyPair, 'pub', function pub() {
  6396. if (this._pubBytes)
  6397. return this.eddsa.decodePoint(this._pubBytes);
  6398. return this.eddsa.g.mul(this.priv());
  6399. });
  6400. cachedProperty(KeyPair, 'privBytes', function privBytes() {
  6401. var eddsa = this.eddsa;
  6402. var hash = this.hash();
  6403. var lastIx = eddsa.encodingLength - 1;
  6404. var a = hash.slice(0, eddsa.encodingLength);
  6405. a[0] &= 248;
  6406. a[lastIx] &= 127;
  6407. a[lastIx] |= 64;
  6408. return a;
  6409. });
  6410. cachedProperty(KeyPair, 'priv', function priv() {
  6411. return this.eddsa.decodeInt(this.privBytes());
  6412. });
  6413. cachedProperty(KeyPair, 'hash', function hash() {
  6414. return this.eddsa.hash().update(this.secret()).digest();
  6415. });
  6416. cachedProperty(KeyPair, 'messagePrefix', function messagePrefix() {
  6417. return this.hash().slice(this.eddsa.encodingLength);
  6418. });
  6419. KeyPair.prototype.sign = function sign(message) {
  6420. assert(this._secret, 'KeyPair can only verify');
  6421. return this.eddsa.sign(message, this);
  6422. };
  6423. KeyPair.prototype.verify = function verify(message, sig) {
  6424. return this.eddsa.verify(message, sig, this);
  6425. };
  6426. KeyPair.prototype.getSecret = function getSecret(enc) {
  6427. assert(this._secret, 'KeyPair is public only');
  6428. return utils.encode(this.secret(), enc);
  6429. };
  6430. KeyPair.prototype.getPublic = function getPublic(enc) {
  6431. return utils.encode(this.pubBytes(), enc);
  6432. };
  6433. module.exports = KeyPair;
  6434. },{"../../elliptic":10}],22:[function(require,module,exports){
  6435. 'use strict';
  6436. var BN = require('bn.js');
  6437. var elliptic = require('../../elliptic');
  6438. var utils = elliptic.utils;
  6439. var assert = utils.assert;
  6440. var cachedProperty = utils.cachedProperty;
  6441. var parseBytes = utils.parseBytes;
  6442. /**
  6443. * @param {EDDSA} eddsa - eddsa instance
  6444. * @param {Array<Bytes>|Object} sig -
  6445. * @param {Array<Bytes>|Point} [sig.R] - R point as Point or bytes
  6446. * @param {Array<Bytes>|bn} [sig.S] - S scalar as bn or bytes
  6447. * @param {Array<Bytes>} [sig.Rencoded] - R point encoded
  6448. * @param {Array<Bytes>} [sig.Sencoded] - S scalar encoded
  6449. */
  6450. function Signature(eddsa, sig) {
  6451. this.eddsa = eddsa;
  6452. if (typeof sig !== 'object')
  6453. sig = parseBytes(sig);
  6454. if (Array.isArray(sig)) {
  6455. sig = {
  6456. R: sig.slice(0, eddsa.encodingLength),
  6457. S: sig.slice(eddsa.encodingLength)
  6458. };
  6459. }
  6460. assert(sig.R && sig.S, 'Signature without R or S');
  6461. if (eddsa.isPoint(sig.R))
  6462. this._R = sig.R;
  6463. if (sig.S instanceof BN)
  6464. this._S = sig.S;
  6465. this._Rencoded = Array.isArray(sig.R) ? sig.R : sig.Rencoded;
  6466. this._Sencoded = Array.isArray(sig.S) ? sig.S : sig.Sencoded;
  6467. }
  6468. cachedProperty(Signature, 'S', function S() {
  6469. return this.eddsa.decodeInt(this.Sencoded());
  6470. });
  6471. cachedProperty(Signature, 'R', function R() {
  6472. return this.eddsa.decodePoint(this.Rencoded());
  6473. });
  6474. cachedProperty(Signature, 'Rencoded', function Rencoded() {
  6475. return this.eddsa.encodePoint(this.R());
  6476. });
  6477. cachedProperty(Signature, 'Sencoded', function Sencoded() {
  6478. return this.eddsa.encodeInt(this.S());
  6479. });
  6480. Signature.prototype.toBytes = function toBytes() {
  6481. return this.Rencoded().concat(this.Sencoded());
  6482. };
  6483. Signature.prototype.toHex = function toHex() {
  6484. return utils.encode(this.toBytes(), 'hex').toUpperCase();
  6485. };
  6486. module.exports = Signature;
  6487. },{"../../elliptic":10,"bn.js":4}],23:[function(require,module,exports){
  6488. module.exports = {
  6489. doubles: {
  6490. step: 4,
  6491. points: [
  6492. [
  6493. 'e60fce93b59e9ec53011aabc21c23e97b2a31369b87a5ae9c44ee89e2a6dec0a',
  6494. 'f7e3507399e595929db99f34f57937101296891e44d23f0be1f32cce69616821'
  6495. ],
  6496. [
  6497. '8282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508',
  6498. '11f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf'
  6499. ],
  6500. [
  6501. '175e159f728b865a72f99cc6c6fc846de0b93833fd2222ed73fce5b551e5b739',
  6502. 'd3506e0d9e3c79eba4ef97a51ff71f5eacb5955add24345c6efa6ffee9fed695'
  6503. ],
  6504. [
  6505. '363d90d447b00c9c99ceac05b6262ee053441c7e55552ffe526bad8f83ff4640',
  6506. '4e273adfc732221953b445397f3363145b9a89008199ecb62003c7f3bee9de9'
  6507. ],
  6508. [
  6509. '8b4b5f165df3c2be8c6244b5b745638843e4a781a15bcd1b69f79a55dffdf80c',
  6510. '4aad0a6f68d308b4b3fbd7813ab0da04f9e336546162ee56b3eff0c65fd4fd36'
  6511. ],
  6512. [
  6513. '723cbaa6e5db996d6bf771c00bd548c7b700dbffa6c0e77bcb6115925232fcda',
  6514. '96e867b5595cc498a921137488824d6e2660a0653779494801dc069d9eb39f5f'
  6515. ],
  6516. [
  6517. 'eebfa4d493bebf98ba5feec812c2d3b50947961237a919839a533eca0e7dd7fa',
  6518. '5d9a8ca3970ef0f269ee7edaf178089d9ae4cdc3a711f712ddfd4fdae1de8999'
  6519. ],
  6520. [
  6521. '100f44da696e71672791d0a09b7bde459f1215a29b3c03bfefd7835b39a48db0',
  6522. 'cdd9e13192a00b772ec8f3300c090666b7ff4a18ff5195ac0fbd5cd62bc65a09'
  6523. ],
  6524. [
  6525. 'e1031be262c7ed1b1dc9227a4a04c017a77f8d4464f3b3852c8acde6e534fd2d',
  6526. '9d7061928940405e6bb6a4176597535af292dd419e1ced79a44f18f29456a00d'
  6527. ],
  6528. [
  6529. 'feea6cae46d55b530ac2839f143bd7ec5cf8b266a41d6af52d5e688d9094696d',
  6530. 'e57c6b6c97dce1bab06e4e12bf3ecd5c981c8957cc41442d3155debf18090088'
  6531. ],
  6532. [
  6533. 'da67a91d91049cdcb367be4be6ffca3cfeed657d808583de33fa978bc1ec6cb1',
  6534. '9bacaa35481642bc41f463f7ec9780e5dec7adc508f740a17e9ea8e27a68be1d'
  6535. ],
  6536. [
  6537. '53904faa0b334cdda6e000935ef22151ec08d0f7bb11069f57545ccc1a37b7c0',
  6538. '5bc087d0bc80106d88c9eccac20d3c1c13999981e14434699dcb096b022771c8'
  6539. ],
  6540. [
  6541. '8e7bcd0bd35983a7719cca7764ca906779b53a043a9b8bcaeff959f43ad86047',
  6542. '10b7770b2a3da4b3940310420ca9514579e88e2e47fd68b3ea10047e8460372a'
  6543. ],
  6544. [
  6545. '385eed34c1cdff21e6d0818689b81bde71a7f4f18397e6690a841e1599c43862',
  6546. '283bebc3e8ea23f56701de19e9ebf4576b304eec2086dc8cc0458fe5542e5453'
  6547. ],
  6548. [
  6549. '6f9d9b803ecf191637c73a4413dfa180fddf84a5947fbc9c606ed86c3fac3a7',
  6550. '7c80c68e603059ba69b8e2a30e45c4d47ea4dd2f5c281002d86890603a842160'
  6551. ],
  6552. [
  6553. '3322d401243c4e2582a2147c104d6ecbf774d163db0f5e5313b7e0e742d0e6bd',
  6554. '56e70797e9664ef5bfb019bc4ddaf9b72805f63ea2873af624f3a2e96c28b2a0'
  6555. ],
  6556. [
  6557. '85672c7d2de0b7da2bd1770d89665868741b3f9af7643397721d74d28134ab83',
  6558. '7c481b9b5b43b2eb6374049bfa62c2e5e77f17fcc5298f44c8e3094f790313a6'
  6559. ],
  6560. [
  6561. '948bf809b1988a46b06c9f1919413b10f9226c60f668832ffd959af60c82a0a',
  6562. '53a562856dcb6646dc6b74c5d1c3418c6d4dff08c97cd2bed4cb7f88d8c8e589'
  6563. ],
  6564. [
  6565. '6260ce7f461801c34f067ce0f02873a8f1b0e44dfc69752accecd819f38fd8e8',
  6566. 'bc2da82b6fa5b571a7f09049776a1ef7ecd292238051c198c1a84e95b2b4ae17'
  6567. ],
  6568. [
  6569. 'e5037de0afc1d8d43d8348414bbf4103043ec8f575bfdc432953cc8d2037fa2d',
  6570. '4571534baa94d3b5f9f98d09fb990bddbd5f5b03ec481f10e0e5dc841d755bda'
  6571. ],
  6572. [
  6573. 'e06372b0f4a207adf5ea905e8f1771b4e7e8dbd1c6a6c5b725866a0ae4fce725',
  6574. '7a908974bce18cfe12a27bb2ad5a488cd7484a7787104870b27034f94eee31dd'
  6575. ],
  6576. [
  6577. '213c7a715cd5d45358d0bbf9dc0ce02204b10bdde2a3f58540ad6908d0559754',
  6578. '4b6dad0b5ae462507013ad06245ba190bb4850f5f36a7eeddff2c27534b458f2'
  6579. ],
  6580. [
  6581. '4e7c272a7af4b34e8dbb9352a5419a87e2838c70adc62cddf0cc3a3b08fbd53c',
  6582. '17749c766c9d0b18e16fd09f6def681b530b9614bff7dd33e0b3941817dcaae6'
  6583. ],
  6584. [
  6585. 'fea74e3dbe778b1b10f238ad61686aa5c76e3db2be43057632427e2840fb27b6',
  6586. '6e0568db9b0b13297cf674deccb6af93126b596b973f7b77701d3db7f23cb96f'
  6587. ],
  6588. [
  6589. '76e64113f677cf0e10a2570d599968d31544e179b760432952c02a4417bdde39',
  6590. 'c90ddf8dee4e95cf577066d70681f0d35e2a33d2b56d2032b4b1752d1901ac01'
  6591. ],
  6592. [
  6593. 'c738c56b03b2abe1e8281baa743f8f9a8f7cc643df26cbee3ab150242bcbb891',
  6594. '893fb578951ad2537f718f2eacbfbbbb82314eef7880cfe917e735d9699a84c3'
  6595. ],
  6596. [
  6597. 'd895626548b65b81e264c7637c972877d1d72e5f3a925014372e9f6588f6c14b',
  6598. 'febfaa38f2bc7eae728ec60818c340eb03428d632bb067e179363ed75d7d991f'
  6599. ],
  6600. [
  6601. 'b8da94032a957518eb0f6433571e8761ceffc73693e84edd49150a564f676e03',
  6602. '2804dfa44805a1e4d7c99cc9762808b092cc584d95ff3b511488e4e74efdf6e7'
  6603. ],
  6604. [
  6605. 'e80fea14441fb33a7d8adab9475d7fab2019effb5156a792f1a11778e3c0df5d',
  6606. 'eed1de7f638e00771e89768ca3ca94472d155e80af322ea9fcb4291b6ac9ec78'
  6607. ],
  6608. [
  6609. 'a301697bdfcd704313ba48e51d567543f2a182031efd6915ddc07bbcc4e16070',
  6610. '7370f91cfb67e4f5081809fa25d40f9b1735dbf7c0a11a130c0d1a041e177ea1'
  6611. ],
  6612. [
  6613. '90ad85b389d6b936463f9d0512678de208cc330b11307fffab7ac63e3fb04ed4',
  6614. 'e507a3620a38261affdcbd9427222b839aefabe1582894d991d4d48cb6ef150'
  6615. ],
  6616. [
  6617. '8f68b9d2f63b5f339239c1ad981f162ee88c5678723ea3351b7b444c9ec4c0da',
  6618. '662a9f2dba063986de1d90c2b6be215dbbea2cfe95510bfdf23cbf79501fff82'
  6619. ],
  6620. [
  6621. 'e4f3fb0176af85d65ff99ff9198c36091f48e86503681e3e6686fd5053231e11',
  6622. '1e63633ad0ef4f1c1661a6d0ea02b7286cc7e74ec951d1c9822c38576feb73bc'
  6623. ],
  6624. [
  6625. '8c00fa9b18ebf331eb961537a45a4266c7034f2f0d4e1d0716fb6eae20eae29e',
  6626. 'efa47267fea521a1a9dc343a3736c974c2fadafa81e36c54e7d2a4c66702414b'
  6627. ],
  6628. [
  6629. 'e7a26ce69dd4829f3e10cec0a9e98ed3143d084f308b92c0997fddfc60cb3e41',
  6630. '2a758e300fa7984b471b006a1aafbb18d0a6b2c0420e83e20e8a9421cf2cfd51'
  6631. ],
  6632. [
  6633. 'b6459e0ee3662ec8d23540c223bcbdc571cbcb967d79424f3cf29eb3de6b80ef',
  6634. '67c876d06f3e06de1dadf16e5661db3c4b3ae6d48e35b2ff30bf0b61a71ba45'
  6635. ],
  6636. [
  6637. 'd68a80c8280bb840793234aa118f06231d6f1fc67e73c5a5deda0f5b496943e8',
  6638. 'db8ba9fff4b586d00c4b1f9177b0e28b5b0e7b8f7845295a294c84266b133120'
  6639. ],
  6640. [
  6641. '324aed7df65c804252dc0270907a30b09612aeb973449cea4095980fc28d3d5d',
  6642. '648a365774b61f2ff130c0c35aec1f4f19213b0c7e332843967224af96ab7c84'
  6643. ],
  6644. [
  6645. '4df9c14919cde61f6d51dfdbe5fee5dceec4143ba8d1ca888e8bd373fd054c96',
  6646. '35ec51092d8728050974c23a1d85d4b5d506cdc288490192ebac06cad10d5d'
  6647. ],
  6648. [
  6649. '9c3919a84a474870faed8a9c1cc66021523489054d7f0308cbfc99c8ac1f98cd',
  6650. 'ddb84f0f4a4ddd57584f044bf260e641905326f76c64c8e6be7e5e03d4fc599d'
  6651. ],
  6652. [
  6653. '6057170b1dd12fdf8de05f281d8e06bb91e1493a8b91d4cc5a21382120a959e5',
  6654. '9a1af0b26a6a4807add9a2daf71df262465152bc3ee24c65e899be932385a2a8'
  6655. ],
  6656. [
  6657. 'a576df8e23a08411421439a4518da31880cef0fba7d4df12b1a6973eecb94266',
  6658. '40a6bf20e76640b2c92b97afe58cd82c432e10a7f514d9f3ee8be11ae1b28ec8'
  6659. ],
  6660. [
  6661. '7778a78c28dec3e30a05fe9629de8c38bb30d1f5cf9a3a208f763889be58ad71',
  6662. '34626d9ab5a5b22ff7098e12f2ff580087b38411ff24ac563b513fc1fd9f43ac'
  6663. ],
  6664. [
  6665. '928955ee637a84463729fd30e7afd2ed5f96274e5ad7e5cb09eda9c06d903ac',
  6666. 'c25621003d3f42a827b78a13093a95eeac3d26efa8a8d83fc5180e935bcd091f'
  6667. ],
  6668. [
  6669. '85d0fef3ec6db109399064f3a0e3b2855645b4a907ad354527aae75163d82751',
  6670. '1f03648413a38c0be29d496e582cf5663e8751e96877331582c237a24eb1f962'
  6671. ],
  6672. [
  6673. 'ff2b0dce97eece97c1c9b6041798b85dfdfb6d8882da20308f5404824526087e',
  6674. '493d13fef524ba188af4c4dc54d07936c7b7ed6fb90e2ceb2c951e01f0c29907'
  6675. ],
  6676. [
  6677. '827fbbe4b1e880ea9ed2b2e6301b212b57f1ee148cd6dd28780e5e2cf856e241',
  6678. 'c60f9c923c727b0b71bef2c67d1d12687ff7a63186903166d605b68baec293ec'
  6679. ],
  6680. [
  6681. 'eaa649f21f51bdbae7be4ae34ce6e5217a58fdce7f47f9aa7f3b58fa2120e2b3',
  6682. 'be3279ed5bbbb03ac69a80f89879aa5a01a6b965f13f7e59d47a5305ba5ad93d'
  6683. ],
  6684. [
  6685. 'e4a42d43c5cf169d9391df6decf42ee541b6d8f0c9a137401e23632dda34d24f',
  6686. '4d9f92e716d1c73526fc99ccfb8ad34ce886eedfa8d8e4f13a7f7131deba9414'
  6687. ],
  6688. [
  6689. '1ec80fef360cbdd954160fadab352b6b92b53576a88fea4947173b9d4300bf19',
  6690. 'aeefe93756b5340d2f3a4958a7abbf5e0146e77f6295a07b671cdc1cc107cefd'
  6691. ],
  6692. [
  6693. '146a778c04670c2f91b00af4680dfa8bce3490717d58ba889ddb5928366642be',
  6694. 'b318e0ec3354028add669827f9d4b2870aaa971d2f7e5ed1d0b297483d83efd0'
  6695. ],
  6696. [
  6697. 'fa50c0f61d22e5f07e3acebb1aa07b128d0012209a28b9776d76a8793180eef9',
  6698. '6b84c6922397eba9b72cd2872281a68a5e683293a57a213b38cd8d7d3f4f2811'
  6699. ],
  6700. [
  6701. 'da1d61d0ca721a11b1a5bf6b7d88e8421a288ab5d5bba5220e53d32b5f067ec2',
  6702. '8157f55a7c99306c79c0766161c91e2966a73899d279b48a655fba0f1ad836f1'
  6703. ],
  6704. [
  6705. 'a8e282ff0c9706907215ff98e8fd416615311de0446f1e062a73b0610d064e13',
  6706. '7f97355b8db81c09abfb7f3c5b2515888b679a3e50dd6bd6cef7c73111f4cc0c'
  6707. ],
  6708. [
  6709. '174a53b9c9a285872d39e56e6913cab15d59b1fa512508c022f382de8319497c',
  6710. 'ccc9dc37abfc9c1657b4155f2c47f9e6646b3a1d8cb9854383da13ac079afa73'
  6711. ],
  6712. [
  6713. '959396981943785c3d3e57edf5018cdbe039e730e4918b3d884fdff09475b7ba',
  6714. '2e7e552888c331dd8ba0386a4b9cd6849c653f64c8709385e9b8abf87524f2fd'
  6715. ],
  6716. [
  6717. 'd2a63a50ae401e56d645a1153b109a8fcca0a43d561fba2dbb51340c9d82b151',
  6718. 'e82d86fb6443fcb7565aee58b2948220a70f750af484ca52d4142174dcf89405'
  6719. ],
  6720. [
  6721. '64587e2335471eb890ee7896d7cfdc866bacbdbd3839317b3436f9b45617e073',
  6722. 'd99fcdd5bf6902e2ae96dd6447c299a185b90a39133aeab358299e5e9faf6589'
  6723. ],
  6724. [
  6725. '8481bde0e4e4d885b3a546d3e549de042f0aa6cea250e7fd358d6c86dd45e458',
  6726. '38ee7b8cba5404dd84a25bf39cecb2ca900a79c42b262e556d64b1b59779057e'
  6727. ],
  6728. [
  6729. '13464a57a78102aa62b6979ae817f4637ffcfed3c4b1ce30bcd6303f6caf666b',
  6730. '69be159004614580ef7e433453ccb0ca48f300a81d0942e13f495a907f6ecc27'
  6731. ],
  6732. [
  6733. 'bc4a9df5b713fe2e9aef430bcc1dc97a0cd9ccede2f28588cada3a0d2d83f366',
  6734. 'd3a81ca6e785c06383937adf4b798caa6e8a9fbfa547b16d758d666581f33c1'
  6735. ],
  6736. [
  6737. '8c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa',
  6738. '40a30463a3305193378fedf31f7cc0eb7ae784f0451cb9459e71dc73cbef9482'
  6739. ],
  6740. [
  6741. '8ea9666139527a8c1dd94ce4f071fd23c8b350c5a4bb33748c4ba111faccae0',
  6742. '620efabbc8ee2782e24e7c0cfb95c5d735b783be9cf0f8e955af34a30e62b945'
  6743. ],
  6744. [
  6745. 'dd3625faef5ba06074669716bbd3788d89bdde815959968092f76cc4eb9a9787',
  6746. '7a188fa3520e30d461da2501045731ca941461982883395937f68d00c644a573'
  6747. ],
  6748. [
  6749. 'f710d79d9eb962297e4f6232b40e8f7feb2bc63814614d692c12de752408221e',
  6750. 'ea98e67232d3b3295d3b535532115ccac8612c721851617526ae47a9c77bfc82'
  6751. ]
  6752. ]
  6753. },
  6754. naf: {
  6755. wnd: 7,
  6756. points: [
  6757. [
  6758. 'f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9',
  6759. '388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672'
  6760. ],
  6761. [
  6762. '2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4',
  6763. 'd8ac222636e5e3d6d4dba9dda6c9c426f788271bab0d6840dca87d3aa6ac62d6'
  6764. ],
  6765. [
  6766. '5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc',
  6767. '6aebca40ba255960a3178d6d861a54dba813d0b813fde7b5a5082628087264da'
  6768. ],
  6769. [
  6770. 'acd484e2f0c7f65309ad178a9f559abde09796974c57e714c35f110dfc27ccbe',
  6771. 'cc338921b0a7d9fd64380971763b61e9add888a4375f8e0f05cc262ac64f9c37'
  6772. ],
  6773. [
  6774. '774ae7f858a9411e5ef4246b70c65aac5649980be5c17891bbec17895da008cb',
  6775. 'd984a032eb6b5e190243dd56d7b7b365372db1e2dff9d6a8301d74c9c953c61b'
  6776. ],
  6777. [
  6778. 'f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8',
  6779. 'ab0902e8d880a89758212eb65cdaf473a1a06da521fa91f29b5cb52db03ed81'
  6780. ],
  6781. [
  6782. 'd7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e',
  6783. '581e2872a86c72a683842ec228cc6defea40af2bd896d3a5c504dc9ff6a26b58'
  6784. ],
  6785. [
  6786. 'defdea4cdb677750a420fee807eacf21eb9898ae79b9768766e4faa04a2d4a34',
  6787. '4211ab0694635168e997b0ead2a93daeced1f4a04a95c0f6cfb199f69e56eb77'
  6788. ],
  6789. [
  6790. '2b4ea0a797a443d293ef5cff444f4979f06acfebd7e86d277475656138385b6c',
  6791. '85e89bc037945d93b343083b5a1c86131a01f60c50269763b570c854e5c09b7a'
  6792. ],
  6793. [
  6794. '352bbf4a4cdd12564f93fa332ce333301d9ad40271f8107181340aef25be59d5',
  6795. '321eb4075348f534d59c18259dda3e1f4a1b3b2e71b1039c67bd3d8bcf81998c'
  6796. ],
  6797. [
  6798. '2fa2104d6b38d11b0230010559879124e42ab8dfeff5ff29dc9cdadd4ecacc3f',
  6799. '2de1068295dd865b64569335bd5dd80181d70ecfc882648423ba76b532b7d67'
  6800. ],
  6801. [
  6802. '9248279b09b4d68dab21a9b066edda83263c3d84e09572e269ca0cd7f5453714',
  6803. '73016f7bf234aade5d1aa71bdea2b1ff3fc0de2a887912ffe54a32ce97cb3402'
  6804. ],
  6805. [
  6806. 'daed4f2be3a8bf278e70132fb0beb7522f570e144bf615c07e996d443dee8729',
  6807. 'a69dce4a7d6c98e8d4a1aca87ef8d7003f83c230f3afa726ab40e52290be1c55'
  6808. ],
  6809. [
  6810. 'c44d12c7065d812e8acf28d7cbb19f9011ecd9e9fdf281b0e6a3b5e87d22e7db',
  6811. '2119a460ce326cdc76c45926c982fdac0e106e861edf61c5a039063f0e0e6482'
  6812. ],
  6813. [
  6814. '6a245bf6dc698504c89a20cfded60853152b695336c28063b61c65cbd269e6b4',
  6815. 'e022cf42c2bd4a708b3f5126f16a24ad8b33ba48d0423b6efd5e6348100d8a82'
  6816. ],
  6817. [
  6818. '1697ffa6fd9de627c077e3d2fe541084ce13300b0bec1146f95ae57f0d0bd6a5',
  6819. 'b9c398f186806f5d27561506e4557433a2cf15009e498ae7adee9d63d01b2396'
  6820. ],
  6821. [
  6822. '605bdb019981718b986d0f07e834cb0d9deb8360ffb7f61df982345ef27a7479',
  6823. '2972d2de4f8d20681a78d93ec96fe23c26bfae84fb14db43b01e1e9056b8c49'
  6824. ],
  6825. [
  6826. '62d14dab4150bf497402fdc45a215e10dcb01c354959b10cfe31c7e9d87ff33d',
  6827. '80fc06bd8cc5b01098088a1950eed0db01aa132967ab472235f5642483b25eaf'
  6828. ],
  6829. [
  6830. '80c60ad0040f27dade5b4b06c408e56b2c50e9f56b9b8b425e555c2f86308b6f',
  6831. '1c38303f1cc5c30f26e66bad7fe72f70a65eed4cbe7024eb1aa01f56430bd57a'
  6832. ],
  6833. [
  6834. '7a9375ad6167ad54aa74c6348cc54d344cc5dc9487d847049d5eabb0fa03c8fb',
  6835. 'd0e3fa9eca8726909559e0d79269046bdc59ea10c70ce2b02d499ec224dc7f7'
  6836. ],
  6837. [
  6838. 'd528ecd9b696b54c907a9ed045447a79bb408ec39b68df504bb51f459bc3ffc9',
  6839. 'eecf41253136e5f99966f21881fd656ebc4345405c520dbc063465b521409933'
  6840. ],
  6841. [
  6842. '49370a4b5f43412ea25f514e8ecdad05266115e4a7ecb1387231808f8b45963',
  6843. '758f3f41afd6ed428b3081b0512fd62a54c3f3afbb5b6764b653052a12949c9a'
  6844. ],
  6845. [
  6846. '77f230936ee88cbbd73df930d64702ef881d811e0e1498e2f1c13eb1fc345d74',
  6847. '958ef42a7886b6400a08266e9ba1b37896c95330d97077cbbe8eb3c7671c60d6'
  6848. ],
  6849. [
  6850. 'f2dac991cc4ce4b9ea44887e5c7c0bce58c80074ab9d4dbaeb28531b7739f530',
  6851. 'e0dedc9b3b2f8dad4da1f32dec2531df9eb5fbeb0598e4fd1a117dba703a3c37'
  6852. ],
  6853. [
  6854. '463b3d9f662621fb1b4be8fbbe2520125a216cdfc9dae3debcba4850c690d45b',
  6855. '5ed430d78c296c3543114306dd8622d7c622e27c970a1de31cb377b01af7307e'
  6856. ],
  6857. [
  6858. 'f16f804244e46e2a09232d4aff3b59976b98fac14328a2d1a32496b49998f247',
  6859. 'cedabd9b82203f7e13d206fcdf4e33d92a6c53c26e5cce26d6579962c4e31df6'
  6860. ],
  6861. [
  6862. 'caf754272dc84563b0352b7a14311af55d245315ace27c65369e15f7151d41d1',
  6863. 'cb474660ef35f5f2a41b643fa5e460575f4fa9b7962232a5c32f908318a04476'
  6864. ],
  6865. [
  6866. '2600ca4b282cb986f85d0f1709979d8b44a09c07cb86d7c124497bc86f082120',
  6867. '4119b88753c15bd6a693b03fcddbb45d5ac6be74ab5f0ef44b0be9475a7e4b40'
  6868. ],
  6869. [
  6870. '7635ca72d7e8432c338ec53cd12220bc01c48685e24f7dc8c602a7746998e435',
  6871. '91b649609489d613d1d5e590f78e6d74ecfc061d57048bad9e76f302c5b9c61'
  6872. ],
  6873. [
  6874. '754e3239f325570cdbbf4a87deee8a66b7f2b33479d468fbc1a50743bf56cc18',
  6875. '673fb86e5bda30fb3cd0ed304ea49a023ee33d0197a695d0c5d98093c536683'
  6876. ],
  6877. [
  6878. 'e3e6bd1071a1e96aff57859c82d570f0330800661d1c952f9fe2694691d9b9e8',
  6879. '59c9e0bba394e76f40c0aa58379a3cb6a5a2283993e90c4167002af4920e37f5'
  6880. ],
  6881. [
  6882. '186b483d056a033826ae73d88f732985c4ccb1f32ba35f4b4cc47fdcf04aa6eb',
  6883. '3b952d32c67cf77e2e17446e204180ab21fb8090895138b4a4a797f86e80888b'
  6884. ],
  6885. [
  6886. 'df9d70a6b9876ce544c98561f4be4f725442e6d2b737d9c91a8321724ce0963f',
  6887. '55eb2dafd84d6ccd5f862b785dc39d4ab157222720ef9da217b8c45cf2ba2417'
  6888. ],
  6889. [
  6890. '5edd5cc23c51e87a497ca815d5dce0f8ab52554f849ed8995de64c5f34ce7143',
  6891. 'efae9c8dbc14130661e8cec030c89ad0c13c66c0d17a2905cdc706ab7399a868'
  6892. ],
  6893. [
  6894. '290798c2b6476830da12fe02287e9e777aa3fba1c355b17a722d362f84614fba',
  6895. 'e38da76dcd440621988d00bcf79af25d5b29c094db2a23146d003afd41943e7a'
  6896. ],
  6897. [
  6898. 'af3c423a95d9f5b3054754efa150ac39cd29552fe360257362dfdecef4053b45',
  6899. 'f98a3fd831eb2b749a93b0e6f35cfb40c8cd5aa667a15581bc2feded498fd9c6'
  6900. ],
  6901. [
  6902. '766dbb24d134e745cccaa28c99bf274906bb66b26dcf98df8d2fed50d884249a',
  6903. '744b1152eacbe5e38dcc887980da38b897584a65fa06cedd2c924f97cbac5996'
  6904. ],
  6905. [
  6906. '59dbf46f8c94759ba21277c33784f41645f7b44f6c596a58ce92e666191abe3e',
  6907. 'c534ad44175fbc300f4ea6ce648309a042ce739a7919798cd85e216c4a307f6e'
  6908. ],
  6909. [
  6910. 'f13ada95103c4537305e691e74e9a4a8dd647e711a95e73cb62dc6018cfd87b8',
  6911. 'e13817b44ee14de663bf4bc808341f326949e21a6a75c2570778419bdaf5733d'
  6912. ],
  6913. [
  6914. '7754b4fa0e8aced06d4167a2c59cca4cda1869c06ebadfb6488550015a88522c',
  6915. '30e93e864e669d82224b967c3020b8fa8d1e4e350b6cbcc537a48b57841163a2'
  6916. ],
  6917. [
  6918. '948dcadf5990e048aa3874d46abef9d701858f95de8041d2a6828c99e2262519',
  6919. 'e491a42537f6e597d5d28a3224b1bc25df9154efbd2ef1d2cbba2cae5347d57e'
  6920. ],
  6921. [
  6922. '7962414450c76c1689c7b48f8202ec37fb224cf5ac0bfa1570328a8a3d7c77ab',
  6923. '100b610ec4ffb4760d5c1fc133ef6f6b12507a051f04ac5760afa5b29db83437'
  6924. ],
  6925. [
  6926. '3514087834964b54b15b160644d915485a16977225b8847bb0dd085137ec47ca',
  6927. 'ef0afbb2056205448e1652c48e8127fc6039e77c15c2378b7e7d15a0de293311'
  6928. ],
  6929. [
  6930. 'd3cc30ad6b483e4bc79ce2c9dd8bc54993e947eb8df787b442943d3f7b527eaf',
  6931. '8b378a22d827278d89c5e9be8f9508ae3c2ad46290358630afb34db04eede0a4'
  6932. ],
  6933. [
  6934. '1624d84780732860ce1c78fcbfefe08b2b29823db913f6493975ba0ff4847610',
  6935. '68651cf9b6da903e0914448c6cd9d4ca896878f5282be4c8cc06e2a404078575'
  6936. ],
  6937. [
  6938. '733ce80da955a8a26902c95633e62a985192474b5af207da6df7b4fd5fc61cd4',
  6939. 'f5435a2bd2badf7d485a4d8b8db9fcce3e1ef8e0201e4578c54673bc1dc5ea1d'
  6940. ],
  6941. [
  6942. '15d9441254945064cf1a1c33bbd3b49f8966c5092171e699ef258dfab81c045c',
  6943. 'd56eb30b69463e7234f5137b73b84177434800bacebfc685fc37bbe9efe4070d'
  6944. ],
  6945. [
  6946. 'a1d0fcf2ec9de675b612136e5ce70d271c21417c9d2b8aaaac138599d0717940',
  6947. 'edd77f50bcb5a3cab2e90737309667f2641462a54070f3d519212d39c197a629'
  6948. ],
  6949. [
  6950. 'e22fbe15c0af8ccc5780c0735f84dbe9a790badee8245c06c7ca37331cb36980',
  6951. 'a855babad5cd60c88b430a69f53a1a7a38289154964799be43d06d77d31da06'
  6952. ],
  6953. [
  6954. '311091dd9860e8e20ee13473c1155f5f69635e394704eaa74009452246cfa9b3',
  6955. '66db656f87d1f04fffd1f04788c06830871ec5a64feee685bd80f0b1286d8374'
  6956. ],
  6957. [
  6958. '34c1fd04d301be89b31c0442d3e6ac24883928b45a9340781867d4232ec2dbdf',
  6959. '9414685e97b1b5954bd46f730174136d57f1ceeb487443dc5321857ba73abee'
  6960. ],
  6961. [
  6962. 'f219ea5d6b54701c1c14de5b557eb42a8d13f3abbcd08affcc2a5e6b049b8d63',
  6963. '4cb95957e83d40b0f73af4544cccf6b1f4b08d3c07b27fb8d8c2962a400766d1'
  6964. ],
  6965. [
  6966. 'd7b8740f74a8fbaab1f683db8f45de26543a5490bca627087236912469a0b448',
  6967. 'fa77968128d9c92ee1010f337ad4717eff15db5ed3c049b3411e0315eaa4593b'
  6968. ],
  6969. [
  6970. '32d31c222f8f6f0ef86f7c98d3a3335ead5bcd32abdd94289fe4d3091aa824bf',
  6971. '5f3032f5892156e39ccd3d7915b9e1da2e6dac9e6f26e961118d14b8462e1661'
  6972. ],
  6973. [
  6974. '7461f371914ab32671045a155d9831ea8793d77cd59592c4340f86cbc18347b5',
  6975. '8ec0ba238b96bec0cbdddcae0aa442542eee1ff50c986ea6b39847b3cc092ff6'
  6976. ],
  6977. [
  6978. 'ee079adb1df1860074356a25aa38206a6d716b2c3e67453d287698bad7b2b2d6',
  6979. '8dc2412aafe3be5c4c5f37e0ecc5f9f6a446989af04c4e25ebaac479ec1c8c1e'
  6980. ],
  6981. [
  6982. '16ec93e447ec83f0467b18302ee620f7e65de331874c9dc72bfd8616ba9da6b5',
  6983. '5e4631150e62fb40d0e8c2a7ca5804a39d58186a50e497139626778e25b0674d'
  6984. ],
  6985. [
  6986. 'eaa5f980c245f6f038978290afa70b6bd8855897f98b6aa485b96065d537bd99',
  6987. 'f65f5d3e292c2e0819a528391c994624d784869d7e6ea67fb18041024edc07dc'
  6988. ],
  6989. [
  6990. '78c9407544ac132692ee1910a02439958ae04877151342ea96c4b6b35a49f51',
  6991. 'f3e0319169eb9b85d5404795539a5e68fa1fbd583c064d2462b675f194a3ddb4'
  6992. ],
  6993. [
  6994. '494f4be219a1a77016dcd838431aea0001cdc8ae7a6fc688726578d9702857a5',
  6995. '42242a969283a5f339ba7f075e36ba2af925ce30d767ed6e55f4b031880d562c'
  6996. ],
  6997. [
  6998. 'a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5',
  6999. '204b5d6f84822c307e4b4a7140737aec23fc63b65b35f86a10026dbd2d864e6b'
  7000. ],
  7001. [
  7002. 'c41916365abb2b5d09192f5f2dbeafec208f020f12570a184dbadc3e58595997',
  7003. '4f14351d0087efa49d245b328984989d5caf9450f34bfc0ed16e96b58fa9913'
  7004. ],
  7005. [
  7006. '841d6063a586fa475a724604da03bc5b92a2e0d2e0a36acfe4c73a5514742881',
  7007. '73867f59c0659e81904f9a1c7543698e62562d6744c169ce7a36de01a8d6154'
  7008. ],
  7009. [
  7010. '5e95bb399a6971d376026947f89bde2f282b33810928be4ded112ac4d70e20d5',
  7011. '39f23f366809085beebfc71181313775a99c9aed7d8ba38b161384c746012865'
  7012. ],
  7013. [
  7014. '36e4641a53948fd476c39f8a99fd974e5ec07564b5315d8bf99471bca0ef2f66',
  7015. 'd2424b1b1abe4eb8164227b085c9aa9456ea13493fd563e06fd51cf5694c78fc'
  7016. ],
  7017. [
  7018. '336581ea7bfbbb290c191a2f507a41cf5643842170e914faeab27c2c579f726',
  7019. 'ead12168595fe1be99252129b6e56b3391f7ab1410cd1e0ef3dcdcabd2fda224'
  7020. ],
  7021. [
  7022. '8ab89816dadfd6b6a1f2634fcf00ec8403781025ed6890c4849742706bd43ede',
  7023. '6fdcef09f2f6d0a044e654aef624136f503d459c3e89845858a47a9129cdd24e'
  7024. ],
  7025. [
  7026. '1e33f1a746c9c5778133344d9299fcaa20b0938e8acff2544bb40284b8c5fb94',
  7027. '60660257dd11b3aa9c8ed618d24edff2306d320f1d03010e33a7d2057f3b3b6'
  7028. ],
  7029. [
  7030. '85b7c1dcb3cec1b7ee7f30ded79dd20a0ed1f4cc18cbcfcfa410361fd8f08f31',
  7031. '3d98a9cdd026dd43f39048f25a8847f4fcafad1895d7a633c6fed3c35e999511'
  7032. ],
  7033. [
  7034. '29df9fbd8d9e46509275f4b125d6d45d7fbe9a3b878a7af872a2800661ac5f51',
  7035. 'b4c4fe99c775a606e2d8862179139ffda61dc861c019e55cd2876eb2a27d84b'
  7036. ],
  7037. [
  7038. 'a0b1cae06b0a847a3fea6e671aaf8adfdfe58ca2f768105c8082b2e449fce252',
  7039. 'ae434102edde0958ec4b19d917a6a28e6b72da1834aff0e650f049503a296cf2'
  7040. ],
  7041. [
  7042. '4e8ceafb9b3e9a136dc7ff67e840295b499dfb3b2133e4ba113f2e4c0e121e5',
  7043. 'cf2174118c8b6d7a4b48f6d534ce5c79422c086a63460502b827ce62a326683c'
  7044. ],
  7045. [
  7046. 'd24a44e047e19b6f5afb81c7ca2f69080a5076689a010919f42725c2b789a33b',
  7047. '6fb8d5591b466f8fc63db50f1c0f1c69013f996887b8244d2cdec417afea8fa3'
  7048. ],
  7049. [
  7050. 'ea01606a7a6c9cdd249fdfcfacb99584001edd28abbab77b5104e98e8e3b35d4',
  7051. '322af4908c7312b0cfbfe369f7a7b3cdb7d4494bc2823700cfd652188a3ea98d'
  7052. ],
  7053. [
  7054. 'af8addbf2b661c8a6c6328655eb96651252007d8c5ea31be4ad196de8ce2131f',
  7055. '6749e67c029b85f52a034eafd096836b2520818680e26ac8f3dfbcdb71749700'
  7056. ],
  7057. [
  7058. 'e3ae1974566ca06cc516d47e0fb165a674a3dabcfca15e722f0e3450f45889',
  7059. '2aeabe7e4531510116217f07bf4d07300de97e4874f81f533420a72eeb0bd6a4'
  7060. ],
  7061. [
  7062. '591ee355313d99721cf6993ffed1e3e301993ff3ed258802075ea8ced397e246',
  7063. 'b0ea558a113c30bea60fc4775460c7901ff0b053d25ca2bdeee98f1a4be5d196'
  7064. ],
  7065. [
  7066. '11396d55fda54c49f19aa97318d8da61fa8584e47b084945077cf03255b52984',
  7067. '998c74a8cd45ac01289d5833a7beb4744ff536b01b257be4c5767bea93ea57a4'
  7068. ],
  7069. [
  7070. '3c5d2a1ba39c5a1790000738c9e0c40b8dcdfd5468754b6405540157e017aa7a',
  7071. 'b2284279995a34e2f9d4de7396fc18b80f9b8b9fdd270f6661f79ca4c81bd257'
  7072. ],
  7073. [
  7074. 'cc8704b8a60a0defa3a99a7299f2e9c3fbc395afb04ac078425ef8a1793cc030',
  7075. 'bdd46039feed17881d1e0862db347f8cf395b74fc4bcdc4e940b74e3ac1f1b13'
  7076. ],
  7077. [
  7078. 'c533e4f7ea8555aacd9777ac5cad29b97dd4defccc53ee7ea204119b2889b197',
  7079. '6f0a256bc5efdf429a2fb6242f1a43a2d9b925bb4a4b3a26bb8e0f45eb596096'
  7080. ],
  7081. [
  7082. 'c14f8f2ccb27d6f109f6d08d03cc96a69ba8c34eec07bbcf566d48e33da6593',
  7083. 'c359d6923bb398f7fd4473e16fe1c28475b740dd098075e6c0e8649113dc3a38'
  7084. ],
  7085. [
  7086. 'a6cbc3046bc6a450bac24789fa17115a4c9739ed75f8f21ce441f72e0b90e6ef',
  7087. '21ae7f4680e889bb130619e2c0f95a360ceb573c70603139862afd617fa9b9f'
  7088. ],
  7089. [
  7090. '347d6d9a02c48927ebfb86c1359b1caf130a3c0267d11ce6344b39f99d43cc38',
  7091. '60ea7f61a353524d1c987f6ecec92f086d565ab687870cb12689ff1e31c74448'
  7092. ],
  7093. [
  7094. 'da6545d2181db8d983f7dcb375ef5866d47c67b1bf31c8cf855ef7437b72656a',
  7095. '49b96715ab6878a79e78f07ce5680c5d6673051b4935bd897fea824b77dc208a'
  7096. ],
  7097. [
  7098. 'c40747cc9d012cb1a13b8148309c6de7ec25d6945d657146b9d5994b8feb1111',
  7099. '5ca560753be2a12fc6de6caf2cb489565db936156b9514e1bb5e83037e0fa2d4'
  7100. ],
  7101. [
  7102. '4e42c8ec82c99798ccf3a610be870e78338c7f713348bd34c8203ef4037f3502',
  7103. '7571d74ee5e0fb92a7a8b33a07783341a5492144cc54bcc40a94473693606437'
  7104. ],
  7105. [
  7106. '3775ab7089bc6af823aba2e1af70b236d251cadb0c86743287522a1b3b0dedea',
  7107. 'be52d107bcfa09d8bcb9736a828cfa7fac8db17bf7a76a2c42ad961409018cf7'
  7108. ],
  7109. [
  7110. 'cee31cbf7e34ec379d94fb814d3d775ad954595d1314ba8846959e3e82f74e26',
  7111. '8fd64a14c06b589c26b947ae2bcf6bfa0149ef0be14ed4d80f448a01c43b1c6d'
  7112. ],
  7113. [
  7114. 'b4f9eaea09b6917619f6ea6a4eb5464efddb58fd45b1ebefcdc1a01d08b47986',
  7115. '39e5c9925b5a54b07433a4f18c61726f8bb131c012ca542eb24a8ac07200682a'
  7116. ],
  7117. [
  7118. 'd4263dfc3d2df923a0179a48966d30ce84e2515afc3dccc1b77907792ebcc60e',
  7119. '62dfaf07a0f78feb30e30d6295853ce189e127760ad6cf7fae164e122a208d54'
  7120. ],
  7121. [
  7122. '48457524820fa65a4f8d35eb6930857c0032acc0a4a2de422233eeda897612c4',
  7123. '25a748ab367979d98733c38a1fa1c2e7dc6cc07db2d60a9ae7a76aaa49bd0f77'
  7124. ],
  7125. [
  7126. 'dfeeef1881101f2cb11644f3a2afdfc2045e19919152923f367a1767c11cceda',
  7127. 'ecfb7056cf1de042f9420bab396793c0c390bde74b4bbdff16a83ae09a9a7517'
  7128. ],
  7129. [
  7130. '6d7ef6b17543f8373c573f44e1f389835d89bcbc6062ced36c82df83b8fae859',
  7131. 'cd450ec335438986dfefa10c57fea9bcc521a0959b2d80bbf74b190dca712d10'
  7132. ],
  7133. [
  7134. 'e75605d59102a5a2684500d3b991f2e3f3c88b93225547035af25af66e04541f',
  7135. 'f5c54754a8f71ee540b9b48728473e314f729ac5308b06938360990e2bfad125'
  7136. ],
  7137. [
  7138. 'eb98660f4c4dfaa06a2be453d5020bc99a0c2e60abe388457dd43fefb1ed620c',
  7139. '6cb9a8876d9cb8520609af3add26cd20a0a7cd8a9411131ce85f44100099223e'
  7140. ],
  7141. [
  7142. '13e87b027d8514d35939f2e6892b19922154596941888336dc3563e3b8dba942',
  7143. 'fef5a3c68059a6dec5d624114bf1e91aac2b9da568d6abeb2570d55646b8adf1'
  7144. ],
  7145. [
  7146. 'ee163026e9fd6fe017c38f06a5be6fc125424b371ce2708e7bf4491691e5764a',
  7147. '1acb250f255dd61c43d94ccc670d0f58f49ae3fa15b96623e5430da0ad6c62b2'
  7148. ],
  7149. [
  7150. 'b268f5ef9ad51e4d78de3a750c2dc89b1e626d43505867999932e5db33af3d80',
  7151. '5f310d4b3c99b9ebb19f77d41c1dee018cf0d34fd4191614003e945a1216e423'
  7152. ],
  7153. [
  7154. 'ff07f3118a9df035e9fad85eb6c7bfe42b02f01ca99ceea3bf7ffdba93c4750d',
  7155. '438136d603e858a3a5c440c38eccbaddc1d2942114e2eddd4740d098ced1f0d8'
  7156. ],
  7157. [
  7158. '8d8b9855c7c052a34146fd20ffb658bea4b9f69e0d825ebec16e8c3ce2b526a1',
  7159. 'cdb559eedc2d79f926baf44fb84ea4d44bcf50fee51d7ceb30e2e7f463036758'
  7160. ],
  7161. [
  7162. '52db0b5384dfbf05bfa9d472d7ae26dfe4b851ceca91b1eba54263180da32b63',
  7163. 'c3b997d050ee5d423ebaf66a6db9f57b3180c902875679de924b69d84a7b375'
  7164. ],
  7165. [
  7166. 'e62f9490d3d51da6395efd24e80919cc7d0f29c3f3fa48c6fff543becbd43352',
  7167. '6d89ad7ba4876b0b22c2ca280c682862f342c8591f1daf5170e07bfd9ccafa7d'
  7168. ],
  7169. [
  7170. '7f30ea2476b399b4957509c88f77d0191afa2ff5cb7b14fd6d8e7d65aaab1193',
  7171. 'ca5ef7d4b231c94c3b15389a5f6311e9daff7bb67b103e9880ef4bff637acaec'
  7172. ],
  7173. [
  7174. '5098ff1e1d9f14fb46a210fada6c903fef0fb7b4a1dd1d9ac60a0361800b7a00',
  7175. '9731141d81fc8f8084d37c6e7542006b3ee1b40d60dfe5362a5b132fd17ddc0'
  7176. ],
  7177. [
  7178. '32b78c7de9ee512a72895be6b9cbefa6e2f3c4ccce445c96b9f2c81e2778ad58',
  7179. 'ee1849f513df71e32efc3896ee28260c73bb80547ae2275ba497237794c8753c'
  7180. ],
  7181. [
  7182. 'e2cb74fddc8e9fbcd076eef2a7c72b0ce37d50f08269dfc074b581550547a4f7',
  7183. 'd3aa2ed71c9dd2247a62df062736eb0baddea9e36122d2be8641abcb005cc4a4'
  7184. ],
  7185. [
  7186. '8438447566d4d7bedadc299496ab357426009a35f235cb141be0d99cd10ae3a8',
  7187. 'c4e1020916980a4da5d01ac5e6ad330734ef0d7906631c4f2390426b2edd791f'
  7188. ],
  7189. [
  7190. '4162d488b89402039b584c6fc6c308870587d9c46f660b878ab65c82c711d67e',
  7191. '67163e903236289f776f22c25fb8a3afc1732f2b84b4e95dbda47ae5a0852649'
  7192. ],
  7193. [
  7194. '3fad3fa84caf0f34f0f89bfd2dcf54fc175d767aec3e50684f3ba4a4bf5f683d',
  7195. 'cd1bc7cb6cc407bb2f0ca647c718a730cf71872e7d0d2a53fa20efcdfe61826'
  7196. ],
  7197. [
  7198. '674f2600a3007a00568c1a7ce05d0816c1fb84bf1370798f1c69532faeb1a86b',
  7199. '299d21f9413f33b3edf43b257004580b70db57da0b182259e09eecc69e0d38a5'
  7200. ],
  7201. [
  7202. 'd32f4da54ade74abb81b815ad1fb3b263d82d6c692714bcff87d29bd5ee9f08f',
  7203. 'f9429e738b8e53b968e99016c059707782e14f4535359d582fc416910b3eea87'
  7204. ],
  7205. [
  7206. '30e4e670435385556e593657135845d36fbb6931f72b08cb1ed954f1e3ce3ff6',
  7207. '462f9bce619898638499350113bbc9b10a878d35da70740dc695a559eb88db7b'
  7208. ],
  7209. [
  7210. 'be2062003c51cc3004682904330e4dee7f3dcd10b01e580bf1971b04d4cad297',
  7211. '62188bc49d61e5428573d48a74e1c655b1c61090905682a0d5558ed72dccb9bc'
  7212. ],
  7213. [
  7214. '93144423ace3451ed29e0fb9ac2af211cb6e84a601df5993c419859fff5df04a',
  7215. '7c10dfb164c3425f5c71a3f9d7992038f1065224f72bb9d1d902a6d13037b47c'
  7216. ],
  7217. [
  7218. 'b015f8044f5fcbdcf21ca26d6c34fb8197829205c7b7d2a7cb66418c157b112c',
  7219. 'ab8c1e086d04e813744a655b2df8d5f83b3cdc6faa3088c1d3aea1454e3a1d5f'
  7220. ],
  7221. [
  7222. 'd5e9e1da649d97d89e4868117a465a3a4f8a18de57a140d36b3f2af341a21b52',
  7223. '4cb04437f391ed73111a13cc1d4dd0db1693465c2240480d8955e8592f27447a'
  7224. ],
  7225. [
  7226. 'd3ae41047dd7ca065dbf8ed77b992439983005cd72e16d6f996a5316d36966bb',
  7227. 'bd1aeb21ad22ebb22a10f0303417c6d964f8cdd7df0aca614b10dc14d125ac46'
  7228. ],
  7229. [
  7230. '463e2763d885f958fc66cdd22800f0a487197d0a82e377b49f80af87c897b065',
  7231. 'bfefacdb0e5d0fd7df3a311a94de062b26b80c61fbc97508b79992671ef7ca7f'
  7232. ],
  7233. [
  7234. '7985fdfd127c0567c6f53ec1bb63ec3158e597c40bfe747c83cddfc910641917',
  7235. '603c12daf3d9862ef2b25fe1de289aed24ed291e0ec6708703a5bd567f32ed03'
  7236. ],
  7237. [
  7238. '74a1ad6b5f76e39db2dd249410eac7f99e74c59cb83d2d0ed5ff1543da7703e9',
  7239. 'cc6157ef18c9c63cd6193d83631bbea0093e0968942e8c33d5737fd790e0db08'
  7240. ],
  7241. [
  7242. '30682a50703375f602d416664ba19b7fc9bab42c72747463a71d0896b22f6da3',
  7243. '553e04f6b018b4fa6c8f39e7f311d3176290d0e0f19ca73f17714d9977a22ff8'
  7244. ],
  7245. [
  7246. '9e2158f0d7c0d5f26c3791efefa79597654e7a2b2464f52b1ee6c1347769ef57',
  7247. '712fcdd1b9053f09003a3481fa7762e9ffd7c8ef35a38509e2fbf2629008373'
  7248. ],
  7249. [
  7250. '176e26989a43c9cfeba4029c202538c28172e566e3c4fce7322857f3be327d66',
  7251. 'ed8cc9d04b29eb877d270b4878dc43c19aefd31f4eee09ee7b47834c1fa4b1c3'
  7252. ],
  7253. [
  7254. '75d46efea3771e6e68abb89a13ad747ecf1892393dfc4f1b7004788c50374da8',
  7255. '9852390a99507679fd0b86fd2b39a868d7efc22151346e1a3ca4726586a6bed8'
  7256. ],
  7257. [
  7258. '809a20c67d64900ffb698c4c825f6d5f2310fb0451c869345b7319f645605721',
  7259. '9e994980d9917e22b76b061927fa04143d096ccc54963e6a5ebfa5f3f8e286c1'
  7260. ],
  7261. [
  7262. '1b38903a43f7f114ed4500b4eac7083fdefece1cf29c63528d563446f972c180',
  7263. '4036edc931a60ae889353f77fd53de4a2708b26b6f5da72ad3394119daf408f9'
  7264. ]
  7265. ]
  7266. }
  7267. };
  7268. },{}],24:[function(require,module,exports){
  7269. 'use strict';
  7270. var utils = exports;
  7271. var BN = require('bn.js');
  7272. var minAssert = require('minimalistic-assert');
  7273. var minUtils = require('minimalistic-crypto-utils');
  7274. utils.assert = minAssert;
  7275. utils.toArray = minUtils.toArray;
  7276. utils.zero2 = minUtils.zero2;
  7277. utils.toHex = minUtils.toHex;
  7278. utils.encode = minUtils.encode;
  7279. // Represent num in a w-NAF form
  7280. function getNAF(num, w) {
  7281. var naf = [];
  7282. var ws = 1 << (w + 1);
  7283. var k = num.clone();
  7284. while (k.cmpn(1) >= 0) {
  7285. var z;
  7286. if (k.isOdd()) {
  7287. var mod = k.andln(ws - 1);
  7288. if (mod > (ws >> 1) - 1)
  7289. z = (ws >> 1) - mod;
  7290. else
  7291. z = mod;
  7292. k.isubn(z);
  7293. } else {
  7294. z = 0;
  7295. }
  7296. naf.push(z);
  7297. // Optimization, shift by word if possible
  7298. var shift = (k.cmpn(0) !== 0 && k.andln(ws - 1) === 0) ? (w + 1) : 1;
  7299. for (var i = 1; i < shift; i++)
  7300. naf.push(0);
  7301. k.iushrn(shift);
  7302. }
  7303. return naf;
  7304. }
  7305. utils.getNAF = getNAF;
  7306. // Represent k1, k2 in a Joint Sparse Form
  7307. function getJSF(k1, k2) {
  7308. var jsf = [
  7309. [],
  7310. []
  7311. ];
  7312. k1 = k1.clone();
  7313. k2 = k2.clone();
  7314. var d1 = 0;
  7315. var d2 = 0;
  7316. while (k1.cmpn(-d1) > 0 || k2.cmpn(-d2) > 0) {
  7317. // First phase
  7318. var m14 = (k1.andln(3) + d1) & 3;
  7319. var m24 = (k2.andln(3) + d2) & 3;
  7320. if (m14 === 3)
  7321. m14 = -1;
  7322. if (m24 === 3)
  7323. m24 = -1;
  7324. var u1;
  7325. if ((m14 & 1) === 0) {
  7326. u1 = 0;
  7327. } else {
  7328. var m8 = (k1.andln(7) + d1) & 7;
  7329. if ((m8 === 3 || m8 === 5) && m24 === 2)
  7330. u1 = -m14;
  7331. else
  7332. u1 = m14;
  7333. }
  7334. jsf[0].push(u1);
  7335. var u2;
  7336. if ((m24 & 1) === 0) {
  7337. u2 = 0;
  7338. } else {
  7339. var m8 = (k2.andln(7) + d2) & 7;
  7340. if ((m8 === 3 || m8 === 5) && m14 === 2)
  7341. u2 = -m24;
  7342. else
  7343. u2 = m24;
  7344. }
  7345. jsf[1].push(u2);
  7346. // Second phase
  7347. if (2 * d1 === u1 + 1)
  7348. d1 = 1 - d1;
  7349. if (2 * d2 === u2 + 1)
  7350. d2 = 1 - d2;
  7351. k1.iushrn(1);
  7352. k2.iushrn(1);
  7353. }
  7354. return jsf;
  7355. }
  7356. utils.getJSF = getJSF;
  7357. function cachedProperty(obj, name, computer) {
  7358. var key = '_' + name;
  7359. obj.prototype[name] = function cachedProperty() {
  7360. return this[key] !== undefined ? this[key] :
  7361. this[key] = computer.call(this);
  7362. };
  7363. }
  7364. utils.cachedProperty = cachedProperty;
  7365. function parseBytes(bytes) {
  7366. return typeof bytes === 'string' ? utils.toArray(bytes, 'hex') :
  7367. bytes;
  7368. }
  7369. utils.parseBytes = parseBytes;
  7370. function intFromLE(bytes) {
  7371. return new BN(bytes, 'hex', 'le');
  7372. }
  7373. utils.intFromLE = intFromLE;
  7374. },{"bn.js":4,"minimalistic-assert":42,"minimalistic-crypto-utils":43}],25:[function(require,module,exports){
  7375. module.exports={
  7376. "_args": [
  7377. [
  7378. {
  7379. "raw": "elliptic@^6.2.3",
  7380. "scope": null,
  7381. "escapedName": "elliptic",
  7382. "name": "elliptic",
  7383. "rawSpec": "^6.2.3",
  7384. "spec": ">=6.2.3 <7.0.0",
  7385. "type": "range"
  7386. },
  7387. "/home/ethvm/Desktop/Embark/eth-utils/ethereumjs-util/node_modules/secp256k1"
  7388. ]
  7389. ],
  7390. "_from": "elliptic@>=6.2.3 <7.0.0",
  7391. "_id": "[email protected]",
  7392. "_inCache": true,
  7393. "_location": "/elliptic",
  7394. "_nodeVersion": "7.0.0",
  7395. "_npmOperationalInternal": {
  7396. "host": "packages-18-east.internal.npmjs.com",
  7397. "tmp": "tmp/elliptic-6.4.0.tgz_1487798866428_0.30510620190761983"
  7398. },
  7399. "_npmUser": {
  7400. "name": "indutny",
  7401. "email": "[email protected]"
  7402. },
  7403. "_npmVersion": "3.10.8",
  7404. "_phantomChildren": {},
  7405. "_requested": {
  7406. "raw": "elliptic@^6.2.3",
  7407. "scope": null,
  7408. "escapedName": "elliptic",
  7409. "name": "elliptic",
  7410. "rawSpec": "^6.2.3",
  7411. "spec": ">=6.2.3 <7.0.0",
  7412. "type": "range"
  7413. },
  7414. "_requiredBy": [
  7415. "/browserify-sign",
  7416. "/create-ecdh",
  7417. "/secp256k1"
  7418. ],
  7419. "_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.4.0.tgz",
  7420. "_shasum": "cac9af8762c85836187003c8dfe193e5e2eae5df",
  7421. "_shrinkwrap": null,
  7422. "_spec": "elliptic@^6.2.3",
  7423. "_where": "/home/ethvm/Desktop/Embark/eth-utils/ethereumjs-util/node_modules/secp256k1",
  7424. "author": {
  7425. "name": "Fedor Indutny",
  7426. "email": "[email protected]"
  7427. },
  7428. "bugs": {
  7429. "url": "https://github.com/indutny/elliptic/issues"
  7430. },
  7431. "dependencies": {
  7432. "bn.js": "^4.4.0",
  7433. "brorand": "^1.0.1",
  7434. "hash.js": "^1.0.0",
  7435. "hmac-drbg": "^1.0.0",
  7436. "inherits": "^2.0.1",
  7437. "minimalistic-assert": "^1.0.0",
  7438. "minimalistic-crypto-utils": "^1.0.0"
  7439. },
  7440. "description": "EC cryptography",
  7441. "devDependencies": {
  7442. "brfs": "^1.4.3",
  7443. "coveralls": "^2.11.3",
  7444. "grunt": "^0.4.5",
  7445. "grunt-browserify": "^5.0.0",
  7446. "grunt-cli": "^1.2.0",
  7447. "grunt-contrib-connect": "^1.0.0",
  7448. "grunt-contrib-copy": "^1.0.0",
  7449. "grunt-contrib-uglify": "^1.0.1",
  7450. "grunt-mocha-istanbul": "^3.0.1",
  7451. "grunt-saucelabs": "^8.6.2",
  7452. "istanbul": "^0.4.2",
  7453. "jscs": "^2.9.0",
  7454. "jshint": "^2.6.0",
  7455. "mocha": "^2.1.0"
  7456. },
  7457. "directories": {},
  7458. "dist": {
  7459. "shasum": "cac9af8762c85836187003c8dfe193e5e2eae5df",
  7460. "tarball": "https://registry.npmjs.org/elliptic/-/elliptic-6.4.0.tgz"
  7461. },
  7462. "files": [
  7463. "lib"
  7464. ],
  7465. "gitHead": "6b0d2b76caae91471649c8e21f0b1d3ba0f96090",
  7466. "homepage": "https://github.com/indutny/elliptic",
  7467. "keywords": [
  7468. "EC",
  7469. "Elliptic",
  7470. "curve",
  7471. "Cryptography"
  7472. ],
  7473. "license": "MIT",
  7474. "main": "lib/elliptic.js",
  7475. "maintainers": [
  7476. {
  7477. "name": "indutny",
  7478. "email": "[email protected]"
  7479. }
  7480. ],
  7481. "name": "elliptic",
  7482. "optionalDependencies": {},
  7483. "readme": "ERROR: No README data found!",
  7484. "repository": {
  7485. "type": "git",
  7486. "url": "git+ssh://[email protected]/indutny/elliptic.git"
  7487. },
  7488. "scripts": {
  7489. "jscs": "jscs benchmarks/*.js lib/*.js lib/**/*.js lib/**/**/*.js test/index.js",
  7490. "jshint": "jscs benchmarks/*.js lib/*.js lib/**/*.js lib/**/**/*.js test/index.js",
  7491. "lint": "npm run jscs && npm run jshint",
  7492. "test": "npm run lint && npm run unit",
  7493. "unit": "istanbul test _mocha --reporter=spec test/index.js",
  7494. "version": "grunt dist && git add dist/"
  7495. },
  7496. "version": "6.4.0"
  7497. }
  7498. },{}],26:[function(require,module,exports){
  7499. (function (Buffer){
  7500. 'use strict';
  7501. var isHexPrefixed = require('is-hex-prefixed');
  7502. var stripHexPrefix = require('strip-hex-prefix');
  7503. /**
  7504. * Pads a `String` to have an even length
  7505. * @param {String} value
  7506. * @return {String} output
  7507. */
  7508. function padToEven(value) {
  7509. var a = value; // eslint-disable-line
  7510. if (typeof a !== 'string') {
  7511. throw new Error('[ethjs-util] while padding to even, value must be string, is currently ' + typeof a + ', while padToEven.');
  7512. }
  7513. if (a.length % 2) {
  7514. a = '0' + a;
  7515. }
  7516. return a;
  7517. }
  7518. /**
  7519. * Converts a `Number` into a hex `String`
  7520. * @param {Number} i
  7521. * @return {String}
  7522. */
  7523. function intToHex(i) {
  7524. var hex = i.toString(16); // eslint-disable-line
  7525. return '0x' + padToEven(hex);
  7526. }
  7527. /**
  7528. * Converts an `Number` to a `Buffer`
  7529. * @param {Number} i
  7530. * @return {Buffer}
  7531. */
  7532. function intToBuffer(i) {
  7533. var hex = intToHex(i);
  7534. return new Buffer(hex.slice(2), 'hex');
  7535. }
  7536. /**
  7537. * Get the binary size of a string
  7538. * @param {String} str
  7539. * @return {Number}
  7540. */
  7541. function getBinarySize(str) {
  7542. if (typeof str !== 'string') {
  7543. throw new Error('[ethjs-util] while getting binary size, method getBinarySize requires input \'str\' to be type String, got \'' + typeof str + '\'.');
  7544. }
  7545. return Buffer.byteLength(str, 'utf8');
  7546. }
  7547. /**
  7548. * Returns TRUE if the first specified array contains all elements
  7549. * from the second one. FALSE otherwise.
  7550. *
  7551. * @param {array} superset
  7552. * @param {array} subset
  7553. *
  7554. * @returns {boolean}
  7555. */
  7556. function arrayContainsArray(superset, subset, some) {
  7557. if (Array.isArray(superset) !== true) {
  7558. throw new Error('[ethjs-util] method arrayContainsArray requires input \'superset\' to be an array got type \'' + typeof superset + '\'');
  7559. }
  7560. if (Array.isArray(subset) !== true) {
  7561. throw new Error('[ethjs-util] method arrayContainsArray requires input \'subset\' to be an array got type \'' + typeof subset + '\'');
  7562. }
  7563. return subset[Boolean(some) && 'some' || 'every'](function (value) {
  7564. return superset.indexOf(value) >= 0;
  7565. });
  7566. }
  7567. /**
  7568. * Should be called to get utf8 from it's hex representation
  7569. *
  7570. * @method toUtf8
  7571. * @param {String} string in hex
  7572. * @returns {String} ascii string representation of hex value
  7573. */
  7574. function toUtf8(hex) {
  7575. var bufferValue = new Buffer(padToEven(stripHexPrefix(hex).replace(/^0+|0+$/g, '')), 'hex');
  7576. return bufferValue.toString('utf8');
  7577. }
  7578. /**
  7579. * Should be called to get ascii from it's hex representation
  7580. *
  7581. * @method toAscii
  7582. * @param {String} string in hex
  7583. * @returns {String} ascii string representation of hex value
  7584. */
  7585. function toAscii(hex) {
  7586. var str = ''; // eslint-disable-line
  7587. var i = 0,
  7588. l = hex.length; // eslint-disable-line
  7589. if (hex.substring(0, 2) === '0x') {
  7590. i = 2;
  7591. }
  7592. for (; i < l; i += 2) {
  7593. var code = parseInt(hex.substr(i, 2), 16);
  7594. str += String.fromCharCode(code);
  7595. }
  7596. return str;
  7597. }
  7598. /**
  7599. * Should be called to get hex representation (prefixed by 0x) of utf8 string
  7600. *
  7601. * @method fromUtf8
  7602. * @param {String} string
  7603. * @param {Number} optional padding
  7604. * @returns {String} hex representation of input string
  7605. */
  7606. function fromUtf8(stringValue) {
  7607. var str = new Buffer(stringValue, 'utf8');
  7608. return '0x' + padToEven(str.toString('hex')).replace(/^0+|0+$/g, '');
  7609. }
  7610. /**
  7611. * Should be called to get hex representation (prefixed by 0x) of ascii string
  7612. *
  7613. * @method fromAscii
  7614. * @param {String} string
  7615. * @param {Number} optional padding
  7616. * @returns {String} hex representation of input string
  7617. */
  7618. function fromAscii(stringValue) {
  7619. var hex = ''; // eslint-disable-line
  7620. for (var i = 0; i < stringValue.length; i++) {
  7621. // eslint-disable-line
  7622. var code = stringValue.charCodeAt(i);
  7623. var n = code.toString(16);
  7624. hex += n.length < 2 ? '0' + n : n;
  7625. }
  7626. return '0x' + hex;
  7627. }
  7628. /**
  7629. * getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3]
  7630. *
  7631. * @method getKeys get specific key from inner object array of objects
  7632. * @param {String} params
  7633. * @param {String} key
  7634. * @param {Boolean} allowEmpty
  7635. * @returns {Array} output just a simple array of output keys
  7636. */
  7637. function getKeys(params, key, allowEmpty) {
  7638. if (!Array.isArray(params)) {
  7639. throw new Error('[ethjs-util] method getKeys expecting type Array as \'params\' input, got \'' + typeof params + '\'');
  7640. }
  7641. if (typeof key !== 'string') {
  7642. throw new Error('[ethjs-util] method getKeys expecting type String for input \'key\' got \'' + typeof key + '\'.');
  7643. }
  7644. var result = []; // eslint-disable-line
  7645. for (var i = 0; i < params.length; i++) {
  7646. // eslint-disable-line
  7647. var value = params[i][key]; // eslint-disable-line
  7648. if (allowEmpty && !value) {
  7649. value = '';
  7650. } else if (typeof value !== 'string') {
  7651. throw new Error('invalid abi');
  7652. }
  7653. result.push(value);
  7654. }
  7655. return result;
  7656. }
  7657. /**
  7658. * Is the string a hex string.
  7659. *
  7660. * @method check if string is hex string of specific length
  7661. * @param {String} value
  7662. * @param {Number} length
  7663. * @returns {Boolean} output the string is a hex string
  7664. */
  7665. function isHexString(value, length) {
  7666. if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
  7667. return false;
  7668. }
  7669. if (length && value.length !== 2 + 2 * length) {
  7670. return false;
  7671. }
  7672. return true;
  7673. }
  7674. module.exports = {
  7675. arrayContainsArray: arrayContainsArray,
  7676. intToBuffer: intToBuffer,
  7677. getBinarySize: getBinarySize,
  7678. isHexPrefixed: isHexPrefixed,
  7679. stripHexPrefix: stripHexPrefix,
  7680. padToEven: padToEven,
  7681. intToHex: intToHex,
  7682. fromAscii: fromAscii,
  7683. fromUtf8: fromUtf8,
  7684. toAscii: toAscii,
  7685. toUtf8: toUtf8,
  7686. getKeys: getKeys,
  7687. isHexString: isHexString
  7688. };
  7689. }).call(this,require("buffer").Buffer)
  7690. },{"buffer":65,"is-hex-prefixed":35,"strip-hex-prefix":60}],27:[function(require,module,exports){
  7691. var hash = exports;
  7692. hash.utils = require('./hash/utils');
  7693. hash.common = require('./hash/common');
  7694. hash.sha = require('./hash/sha');
  7695. hash.ripemd = require('./hash/ripemd');
  7696. hash.hmac = require('./hash/hmac');
  7697. // Proxy hash functions to the main object
  7698. hash.sha1 = hash.sha.sha1;
  7699. hash.sha256 = hash.sha.sha256;
  7700. hash.sha224 = hash.sha.sha224;
  7701. hash.sha384 = hash.sha.sha384;
  7702. hash.sha512 = hash.sha.sha512;
  7703. hash.ripemd160 = hash.ripemd.ripemd160;
  7704. },{"./hash/common":28,"./hash/hmac":29,"./hash/ripemd":30,"./hash/sha":31,"./hash/utils":32}],28:[function(require,module,exports){
  7705. var hash = require('../hash');
  7706. var utils = hash.utils;
  7707. var assert = utils.assert;
  7708. function BlockHash() {
  7709. this.pending = null;
  7710. this.pendingTotal = 0;
  7711. this.blockSize = this.constructor.blockSize;
  7712. this.outSize = this.constructor.outSize;
  7713. this.hmacStrength = this.constructor.hmacStrength;
  7714. this.padLength = this.constructor.padLength / 8;
  7715. this.endian = 'big';
  7716. this._delta8 = this.blockSize / 8;
  7717. this._delta32 = this.blockSize / 32;
  7718. }
  7719. exports.BlockHash = BlockHash;
  7720. BlockHash.prototype.update = function update(msg, enc) {
  7721. // Convert message to array, pad it, and join into 32bit blocks
  7722. msg = utils.toArray(msg, enc);
  7723. if (!this.pending)
  7724. this.pending = msg;
  7725. else
  7726. this.pending = this.pending.concat(msg);
  7727. this.pendingTotal += msg.length;
  7728. // Enough data, try updating
  7729. if (this.pending.length >= this._delta8) {
  7730. msg = this.pending;
  7731. // Process pending data in blocks
  7732. var r = msg.length % this._delta8;
  7733. this.pending = msg.slice(msg.length - r, msg.length);
  7734. if (this.pending.length === 0)
  7735. this.pending = null;
  7736. msg = utils.join32(msg, 0, msg.length - r, this.endian);
  7737. for (var i = 0; i < msg.length; i += this._delta32)
  7738. this._update(msg, i, i + this._delta32);
  7739. }
  7740. return this;
  7741. };
  7742. BlockHash.prototype.digest = function digest(enc) {
  7743. this.update(this._pad());
  7744. assert(this.pending === null);
  7745. return this._digest(enc);
  7746. };
  7747. BlockHash.prototype._pad = function pad() {
  7748. var len = this.pendingTotal;
  7749. var bytes = this._delta8;
  7750. var k = bytes - ((len + this.padLength) % bytes);
  7751. var res = new Array(k + this.padLength);
  7752. res[0] = 0x80;
  7753. for (var i = 1; i < k; i++)
  7754. res[i] = 0;
  7755. // Append length
  7756. len <<= 3;
  7757. if (this.endian === 'big') {
  7758. for (var t = 8; t < this.padLength; t++)
  7759. res[i++] = 0;
  7760. res[i++] = 0;
  7761. res[i++] = 0;
  7762. res[i++] = 0;
  7763. res[i++] = 0;
  7764. res[i++] = (len >>> 24) & 0xff;
  7765. res[i++] = (len >>> 16) & 0xff;
  7766. res[i++] = (len >>> 8) & 0xff;
  7767. res[i++] = len & 0xff;
  7768. } else {
  7769. res[i++] = len & 0xff;
  7770. res[i++] = (len >>> 8) & 0xff;
  7771. res[i++] = (len >>> 16) & 0xff;
  7772. res[i++] = (len >>> 24) & 0xff;
  7773. res[i++] = 0;
  7774. res[i++] = 0;
  7775. res[i++] = 0;
  7776. res[i++] = 0;
  7777. for (var t = 8; t < this.padLength; t++)
  7778. res[i++] = 0;
  7779. }
  7780. return res;
  7781. };
  7782. },{"../hash":27}],29:[function(require,module,exports){
  7783. var hmac = exports;
  7784. var hash = require('../hash');
  7785. var utils = hash.utils;
  7786. var assert = utils.assert;
  7787. function Hmac(hash, key, enc) {
  7788. if (!(this instanceof Hmac))
  7789. return new Hmac(hash, key, enc);
  7790. this.Hash = hash;
  7791. this.blockSize = hash.blockSize / 8;
  7792. this.outSize = hash.outSize / 8;
  7793. this.inner = null;
  7794. this.outer = null;
  7795. this._init(utils.toArray(key, enc));
  7796. }
  7797. module.exports = Hmac;
  7798. Hmac.prototype._init = function init(key) {
  7799. // Shorten key, if needed
  7800. if (key.length > this.blockSize)
  7801. key = new this.Hash().update(key).digest();
  7802. assert(key.length <= this.blockSize);
  7803. // Add padding to key
  7804. for (var i = key.length; i < this.blockSize; i++)
  7805. key.push(0);
  7806. for (var i = 0; i < key.length; i++)
  7807. key[i] ^= 0x36;
  7808. this.inner = new this.Hash().update(key);
  7809. // 0x36 ^ 0x5c = 0x6a
  7810. for (var i = 0; i < key.length; i++)
  7811. key[i] ^= 0x6a;
  7812. this.outer = new this.Hash().update(key);
  7813. };
  7814. Hmac.prototype.update = function update(msg, enc) {
  7815. this.inner.update(msg, enc);
  7816. return this;
  7817. };
  7818. Hmac.prototype.digest = function digest(enc) {
  7819. this.outer.update(this.inner.digest());
  7820. return this.outer.digest(enc);
  7821. };
  7822. },{"../hash":27}],30:[function(require,module,exports){
  7823. var hash = require('../hash');
  7824. var utils = hash.utils;
  7825. var rotl32 = utils.rotl32;
  7826. var sum32 = utils.sum32;
  7827. var sum32_3 = utils.sum32_3;
  7828. var sum32_4 = utils.sum32_4;
  7829. var BlockHash = hash.common.BlockHash;
  7830. function RIPEMD160() {
  7831. if (!(this instanceof RIPEMD160))
  7832. return new RIPEMD160();
  7833. BlockHash.call(this);
  7834. this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 ];
  7835. this.endian = 'little';
  7836. }
  7837. utils.inherits(RIPEMD160, BlockHash);
  7838. exports.ripemd160 = RIPEMD160;
  7839. RIPEMD160.blockSize = 512;
  7840. RIPEMD160.outSize = 160;
  7841. RIPEMD160.hmacStrength = 192;
  7842. RIPEMD160.padLength = 64;
  7843. RIPEMD160.prototype._update = function update(msg, start) {
  7844. var A = this.h[0];
  7845. var B = this.h[1];
  7846. var C = this.h[2];
  7847. var D = this.h[3];
  7848. var E = this.h[4];
  7849. var Ah = A;
  7850. var Bh = B;
  7851. var Ch = C;
  7852. var Dh = D;
  7853. var Eh = E;
  7854. for (var j = 0; j < 80; j++) {
  7855. var T = sum32(
  7856. rotl32(
  7857. sum32_4(A, f(j, B, C, D), msg[r[j] + start], K(j)),
  7858. s[j]),
  7859. E);
  7860. A = E;
  7861. E = D;
  7862. D = rotl32(C, 10);
  7863. C = B;
  7864. B = T;
  7865. T = sum32(
  7866. rotl32(
  7867. sum32_4(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j] + start], Kh(j)),
  7868. sh[j]),
  7869. Eh);
  7870. Ah = Eh;
  7871. Eh = Dh;
  7872. Dh = rotl32(Ch, 10);
  7873. Ch = Bh;
  7874. Bh = T;
  7875. }
  7876. T = sum32_3(this.h[1], C, Dh);
  7877. this.h[1] = sum32_3(this.h[2], D, Eh);
  7878. this.h[2] = sum32_3(this.h[3], E, Ah);
  7879. this.h[3] = sum32_3(this.h[4], A, Bh);
  7880. this.h[4] = sum32_3(this.h[0], B, Ch);
  7881. this.h[0] = T;
  7882. };
  7883. RIPEMD160.prototype._digest = function digest(enc) {
  7884. if (enc === 'hex')
  7885. return utils.toHex32(this.h, 'little');
  7886. else
  7887. return utils.split32(this.h, 'little');
  7888. };
  7889. function f(j, x, y, z) {
  7890. if (j <= 15)
  7891. return x ^ y ^ z;
  7892. else if (j <= 31)
  7893. return (x & y) | ((~x) & z);
  7894. else if (j <= 47)
  7895. return (x | (~y)) ^ z;
  7896. else if (j <= 63)
  7897. return (x & z) | (y & (~z));
  7898. else
  7899. return x ^ (y | (~z));
  7900. }
  7901. function K(j) {
  7902. if (j <= 15)
  7903. return 0x00000000;
  7904. else if (j <= 31)
  7905. return 0x5a827999;
  7906. else if (j <= 47)
  7907. return 0x6ed9eba1;
  7908. else if (j <= 63)
  7909. return 0x8f1bbcdc;
  7910. else
  7911. return 0xa953fd4e;
  7912. }
  7913. function Kh(j) {
  7914. if (j <= 15)
  7915. return 0x50a28be6;
  7916. else if (j <= 31)
  7917. return 0x5c4dd124;
  7918. else if (j <= 47)
  7919. return 0x6d703ef3;
  7920. else if (j <= 63)
  7921. return 0x7a6d76e9;
  7922. else
  7923. return 0x00000000;
  7924. }
  7925. var r = [
  7926. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  7927. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  7928. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  7929. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  7930. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
  7931. ];
  7932. var rh = [
  7933. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  7934. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  7935. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  7936. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  7937. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
  7938. ];
  7939. var s = [
  7940. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  7941. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  7942. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  7943. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  7944. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
  7945. ];
  7946. var sh = [
  7947. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  7948. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  7949. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  7950. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  7951. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
  7952. ];
  7953. },{"../hash":27}],31:[function(require,module,exports){
  7954. var hash = require('../hash');
  7955. var utils = hash.utils;
  7956. var assert = utils.assert;
  7957. var rotr32 = utils.rotr32;
  7958. var rotl32 = utils.rotl32;
  7959. var sum32 = utils.sum32;
  7960. var sum32_4 = utils.sum32_4;
  7961. var sum32_5 = utils.sum32_5;
  7962. var rotr64_hi = utils.rotr64_hi;
  7963. var rotr64_lo = utils.rotr64_lo;
  7964. var shr64_hi = utils.shr64_hi;
  7965. var shr64_lo = utils.shr64_lo;
  7966. var sum64 = utils.sum64;
  7967. var sum64_hi = utils.sum64_hi;
  7968. var sum64_lo = utils.sum64_lo;
  7969. var sum64_4_hi = utils.sum64_4_hi;
  7970. var sum64_4_lo = utils.sum64_4_lo;
  7971. var sum64_5_hi = utils.sum64_5_hi;
  7972. var sum64_5_lo = utils.sum64_5_lo;
  7973. var BlockHash = hash.common.BlockHash;
  7974. var sha256_K = [
  7975. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  7976. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  7977. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  7978. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  7979. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  7980. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  7981. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  7982. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  7983. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  7984. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  7985. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  7986. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  7987. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  7988. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  7989. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  7990. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  7991. ];
  7992. var sha512_K = [
  7993. 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
  7994. 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
  7995. 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
  7996. 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
  7997. 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
  7998. 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
  7999. 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
  8000. 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
  8001. 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
  8002. 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
  8003. 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
  8004. 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
  8005. 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
  8006. 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
  8007. 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
  8008. 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
  8009. 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
  8010. 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
  8011. 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
  8012. 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
  8013. 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
  8014. 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
  8015. 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
  8016. 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
  8017. 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
  8018. 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
  8019. 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
  8020. 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
  8021. 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
  8022. 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
  8023. 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
  8024. 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
  8025. 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
  8026. 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
  8027. 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
  8028. 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
  8029. 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
  8030. 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
  8031. 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
  8032. 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
  8033. ];
  8034. var sha1_K = [
  8035. 0x5A827999, 0x6ED9EBA1,
  8036. 0x8F1BBCDC, 0xCA62C1D6
  8037. ];
  8038. function SHA256() {
  8039. if (!(this instanceof SHA256))
  8040. return new SHA256();
  8041. BlockHash.call(this);
  8042. this.h = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  8043. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ];
  8044. this.k = sha256_K;
  8045. this.W = new Array(64);
  8046. }
  8047. utils.inherits(SHA256, BlockHash);
  8048. exports.sha256 = SHA256;
  8049. SHA256.blockSize = 512;
  8050. SHA256.outSize = 256;
  8051. SHA256.hmacStrength = 192;
  8052. SHA256.padLength = 64;
  8053. SHA256.prototype._update = function _update(msg, start) {
  8054. var W = this.W;
  8055. for (var i = 0; i < 16; i++)
  8056. W[i] = msg[start + i];
  8057. for (; i < W.length; i++)
  8058. W[i] = sum32_4(g1_256(W[i - 2]), W[i - 7], g0_256(W[i - 15]), W[i - 16]);
  8059. var a = this.h[0];
  8060. var b = this.h[1];
  8061. var c = this.h[2];
  8062. var d = this.h[3];
  8063. var e = this.h[4];
  8064. var f = this.h[5];
  8065. var g = this.h[6];
  8066. var h = this.h[7];
  8067. assert(this.k.length === W.length);
  8068. for (var i = 0; i < W.length; i++) {
  8069. var T1 = sum32_5(h, s1_256(e), ch32(e, f, g), this.k[i], W[i]);
  8070. var T2 = sum32(s0_256(a), maj32(a, b, c));
  8071. h = g;
  8072. g = f;
  8073. f = e;
  8074. e = sum32(d, T1);
  8075. d = c;
  8076. c = b;
  8077. b = a;
  8078. a = sum32(T1, T2);
  8079. }
  8080. this.h[0] = sum32(this.h[0], a);
  8081. this.h[1] = sum32(this.h[1], b);
  8082. this.h[2] = sum32(this.h[2], c);
  8083. this.h[3] = sum32(this.h[3], d);
  8084. this.h[4] = sum32(this.h[4], e);
  8085. this.h[5] = sum32(this.h[5], f);
  8086. this.h[6] = sum32(this.h[6], g);
  8087. this.h[7] = sum32(this.h[7], h);
  8088. };
  8089. SHA256.prototype._digest = function digest(enc) {
  8090. if (enc === 'hex')
  8091. return utils.toHex32(this.h, 'big');
  8092. else
  8093. return utils.split32(this.h, 'big');
  8094. };
  8095. function SHA224() {
  8096. if (!(this instanceof SHA224))
  8097. return new SHA224();
  8098. SHA256.call(this);
  8099. this.h = [ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
  8100. 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 ];
  8101. }
  8102. utils.inherits(SHA224, SHA256);
  8103. exports.sha224 = SHA224;
  8104. SHA224.blockSize = 512;
  8105. SHA224.outSize = 224;
  8106. SHA224.hmacStrength = 192;
  8107. SHA224.padLength = 64;
  8108. SHA224.prototype._digest = function digest(enc) {
  8109. // Just truncate output
  8110. if (enc === 'hex')
  8111. return utils.toHex32(this.h.slice(0, 7), 'big');
  8112. else
  8113. return utils.split32(this.h.slice(0, 7), 'big');
  8114. };
  8115. function SHA512() {
  8116. if (!(this instanceof SHA512))
  8117. return new SHA512();
  8118. BlockHash.call(this);
  8119. this.h = [ 0x6a09e667, 0xf3bcc908,
  8120. 0xbb67ae85, 0x84caa73b,
  8121. 0x3c6ef372, 0xfe94f82b,
  8122. 0xa54ff53a, 0x5f1d36f1,
  8123. 0x510e527f, 0xade682d1,
  8124. 0x9b05688c, 0x2b3e6c1f,
  8125. 0x1f83d9ab, 0xfb41bd6b,
  8126. 0x5be0cd19, 0x137e2179 ];
  8127. this.k = sha512_K;
  8128. this.W = new Array(160);
  8129. }
  8130. utils.inherits(SHA512, BlockHash);
  8131. exports.sha512 = SHA512;
  8132. SHA512.blockSize = 1024;
  8133. SHA512.outSize = 512;
  8134. SHA512.hmacStrength = 192;
  8135. SHA512.padLength = 128;
  8136. SHA512.prototype._prepareBlock = function _prepareBlock(msg, start) {
  8137. var W = this.W;
  8138. // 32 x 32bit words
  8139. for (var i = 0; i < 32; i++)
  8140. W[i] = msg[start + i];
  8141. for (; i < W.length; i += 2) {
  8142. var c0_hi = g1_512_hi(W[i - 4], W[i - 3]); // i - 2
  8143. var c0_lo = g1_512_lo(W[i - 4], W[i - 3]);
  8144. var c1_hi = W[i - 14]; // i - 7
  8145. var c1_lo = W[i - 13];
  8146. var c2_hi = g0_512_hi(W[i - 30], W[i - 29]); // i - 15
  8147. var c2_lo = g0_512_lo(W[i - 30], W[i - 29]);
  8148. var c3_hi = W[i - 32]; // i - 16
  8149. var c3_lo = W[i - 31];
  8150. W[i] = sum64_4_hi(c0_hi, c0_lo,
  8151. c1_hi, c1_lo,
  8152. c2_hi, c2_lo,
  8153. c3_hi, c3_lo);
  8154. W[i + 1] = sum64_4_lo(c0_hi, c0_lo,
  8155. c1_hi, c1_lo,
  8156. c2_hi, c2_lo,
  8157. c3_hi, c3_lo);
  8158. }
  8159. };
  8160. SHA512.prototype._update = function _update(msg, start) {
  8161. this._prepareBlock(msg, start);
  8162. var W = this.W;
  8163. var ah = this.h[0];
  8164. var al = this.h[1];
  8165. var bh = this.h[2];
  8166. var bl = this.h[3];
  8167. var ch = this.h[4];
  8168. var cl = this.h[5];
  8169. var dh = this.h[6];
  8170. var dl = this.h[7];
  8171. var eh = this.h[8];
  8172. var el = this.h[9];
  8173. var fh = this.h[10];
  8174. var fl = this.h[11];
  8175. var gh = this.h[12];
  8176. var gl = this.h[13];
  8177. var hh = this.h[14];
  8178. var hl = this.h[15];
  8179. assert(this.k.length === W.length);
  8180. for (var i = 0; i < W.length; i += 2) {
  8181. var c0_hi = hh;
  8182. var c0_lo = hl;
  8183. var c1_hi = s1_512_hi(eh, el);
  8184. var c1_lo = s1_512_lo(eh, el);
  8185. var c2_hi = ch64_hi(eh, el, fh, fl, gh, gl);
  8186. var c2_lo = ch64_lo(eh, el, fh, fl, gh, gl);
  8187. var c3_hi = this.k[i];
  8188. var c3_lo = this.k[i + 1];
  8189. var c4_hi = W[i];
  8190. var c4_lo = W[i + 1];
  8191. var T1_hi = sum64_5_hi(c0_hi, c0_lo,
  8192. c1_hi, c1_lo,
  8193. c2_hi, c2_lo,
  8194. c3_hi, c3_lo,
  8195. c4_hi, c4_lo);
  8196. var T1_lo = sum64_5_lo(c0_hi, c0_lo,
  8197. c1_hi, c1_lo,
  8198. c2_hi, c2_lo,
  8199. c3_hi, c3_lo,
  8200. c4_hi, c4_lo);
  8201. var c0_hi = s0_512_hi(ah, al);
  8202. var c0_lo = s0_512_lo(ah, al);
  8203. var c1_hi = maj64_hi(ah, al, bh, bl, ch, cl);
  8204. var c1_lo = maj64_lo(ah, al, bh, bl, ch, cl);
  8205. var T2_hi = sum64_hi(c0_hi, c0_lo, c1_hi, c1_lo);
  8206. var T2_lo = sum64_lo(c0_hi, c0_lo, c1_hi, c1_lo);
  8207. hh = gh;
  8208. hl = gl;
  8209. gh = fh;
  8210. gl = fl;
  8211. fh = eh;
  8212. fl = el;
  8213. eh = sum64_hi(dh, dl, T1_hi, T1_lo);
  8214. el = sum64_lo(dl, dl, T1_hi, T1_lo);
  8215. dh = ch;
  8216. dl = cl;
  8217. ch = bh;
  8218. cl = bl;
  8219. bh = ah;
  8220. bl = al;
  8221. ah = sum64_hi(T1_hi, T1_lo, T2_hi, T2_lo);
  8222. al = sum64_lo(T1_hi, T1_lo, T2_hi, T2_lo);
  8223. }
  8224. sum64(this.h, 0, ah, al);
  8225. sum64(this.h, 2, bh, bl);
  8226. sum64(this.h, 4, ch, cl);
  8227. sum64(this.h, 6, dh, dl);
  8228. sum64(this.h, 8, eh, el);
  8229. sum64(this.h, 10, fh, fl);
  8230. sum64(this.h, 12, gh, gl);
  8231. sum64(this.h, 14, hh, hl);
  8232. };
  8233. SHA512.prototype._digest = function digest(enc) {
  8234. if (enc === 'hex')
  8235. return utils.toHex32(this.h, 'big');
  8236. else
  8237. return utils.split32(this.h, 'big');
  8238. };
  8239. function SHA384() {
  8240. if (!(this instanceof SHA384))
  8241. return new SHA384();
  8242. SHA512.call(this);
  8243. this.h = [ 0xcbbb9d5d, 0xc1059ed8,
  8244. 0x629a292a, 0x367cd507,
  8245. 0x9159015a, 0x3070dd17,
  8246. 0x152fecd8, 0xf70e5939,
  8247. 0x67332667, 0xffc00b31,
  8248. 0x8eb44a87, 0x68581511,
  8249. 0xdb0c2e0d, 0x64f98fa7,
  8250. 0x47b5481d, 0xbefa4fa4 ];
  8251. }
  8252. utils.inherits(SHA384, SHA512);
  8253. exports.sha384 = SHA384;
  8254. SHA384.blockSize = 1024;
  8255. SHA384.outSize = 384;
  8256. SHA384.hmacStrength = 192;
  8257. SHA384.padLength = 128;
  8258. SHA384.prototype._digest = function digest(enc) {
  8259. if (enc === 'hex')
  8260. return utils.toHex32(this.h.slice(0, 12), 'big');
  8261. else
  8262. return utils.split32(this.h.slice(0, 12), 'big');
  8263. };
  8264. function SHA1() {
  8265. if (!(this instanceof SHA1))
  8266. return new SHA1();
  8267. BlockHash.call(this);
  8268. this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe,
  8269. 0x10325476, 0xc3d2e1f0 ];
  8270. this.W = new Array(80);
  8271. }
  8272. utils.inherits(SHA1, BlockHash);
  8273. exports.sha1 = SHA1;
  8274. SHA1.blockSize = 512;
  8275. SHA1.outSize = 160;
  8276. SHA1.hmacStrength = 80;
  8277. SHA1.padLength = 64;
  8278. SHA1.prototype._update = function _update(msg, start) {
  8279. var W = this.W;
  8280. for (var i = 0; i < 16; i++)
  8281. W[i] = msg[start + i];
  8282. for(; i < W.length; i++)
  8283. W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  8284. var a = this.h[0];
  8285. var b = this.h[1];
  8286. var c = this.h[2];
  8287. var d = this.h[3];
  8288. var e = this.h[4];
  8289. for (var i = 0; i < W.length; i++) {
  8290. var s = ~~(i / 20);
  8291. var t = sum32_5(rotl32(a, 5), ft_1(s, b, c, d), e, W[i], sha1_K[s]);
  8292. e = d;
  8293. d = c;
  8294. c = rotl32(b, 30);
  8295. b = a;
  8296. a = t;
  8297. }
  8298. this.h[0] = sum32(this.h[0], a);
  8299. this.h[1] = sum32(this.h[1], b);
  8300. this.h[2] = sum32(this.h[2], c);
  8301. this.h[3] = sum32(this.h[3], d);
  8302. this.h[4] = sum32(this.h[4], e);
  8303. };
  8304. SHA1.prototype._digest = function digest(enc) {
  8305. if (enc === 'hex')
  8306. return utils.toHex32(this.h, 'big');
  8307. else
  8308. return utils.split32(this.h, 'big');
  8309. };
  8310. function ch32(x, y, z) {
  8311. return (x & y) ^ ((~x) & z);
  8312. }
  8313. function maj32(x, y, z) {
  8314. return (x & y) ^ (x & z) ^ (y & z);
  8315. }
  8316. function p32(x, y, z) {
  8317. return x ^ y ^ z;
  8318. }
  8319. function s0_256(x) {
  8320. return rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22);
  8321. }
  8322. function s1_256(x) {
  8323. return rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25);
  8324. }
  8325. function g0_256(x) {
  8326. return rotr32(x, 7) ^ rotr32(x, 18) ^ (x >>> 3);
  8327. }
  8328. function g1_256(x) {
  8329. return rotr32(x, 17) ^ rotr32(x, 19) ^ (x >>> 10);
  8330. }
  8331. function ft_1(s, x, y, z) {
  8332. if (s === 0)
  8333. return ch32(x, y, z);
  8334. if (s === 1 || s === 3)
  8335. return p32(x, y, z);
  8336. if (s === 2)
  8337. return maj32(x, y, z);
  8338. }
  8339. function ch64_hi(xh, xl, yh, yl, zh, zl) {
  8340. var r = (xh & yh) ^ ((~xh) & zh);
  8341. if (r < 0)
  8342. r += 0x100000000;
  8343. return r;
  8344. }
  8345. function ch64_lo(xh, xl, yh, yl, zh, zl) {
  8346. var r = (xl & yl) ^ ((~xl) & zl);
  8347. if (r < 0)
  8348. r += 0x100000000;
  8349. return r;
  8350. }
  8351. function maj64_hi(xh, xl, yh, yl, zh, zl) {
  8352. var r = (xh & yh) ^ (xh & zh) ^ (yh & zh);
  8353. if (r < 0)
  8354. r += 0x100000000;
  8355. return r;
  8356. }
  8357. function maj64_lo(xh, xl, yh, yl, zh, zl) {
  8358. var r = (xl & yl) ^ (xl & zl) ^ (yl & zl);
  8359. if (r < 0)
  8360. r += 0x100000000;
  8361. return r;
  8362. }
  8363. function s0_512_hi(xh, xl) {
  8364. var c0_hi = rotr64_hi(xh, xl, 28);
  8365. var c1_hi = rotr64_hi(xl, xh, 2); // 34
  8366. var c2_hi = rotr64_hi(xl, xh, 7); // 39
  8367. var r = c0_hi ^ c1_hi ^ c2_hi;
  8368. if (r < 0)
  8369. r += 0x100000000;
  8370. return r;
  8371. }
  8372. function s0_512_lo(xh, xl) {
  8373. var c0_lo = rotr64_lo(xh, xl, 28);
  8374. var c1_lo = rotr64_lo(xl, xh, 2); // 34
  8375. var c2_lo = rotr64_lo(xl, xh, 7); // 39
  8376. var r = c0_lo ^ c1_lo ^ c2_lo;
  8377. if (r < 0)
  8378. r += 0x100000000;
  8379. return r;
  8380. }
  8381. function s1_512_hi(xh, xl) {
  8382. var c0_hi = rotr64_hi(xh, xl, 14);
  8383. var c1_hi = rotr64_hi(xh, xl, 18);
  8384. var c2_hi = rotr64_hi(xl, xh, 9); // 41
  8385. var r = c0_hi ^ c1_hi ^ c2_hi;
  8386. if (r < 0)
  8387. r += 0x100000000;
  8388. return r;
  8389. }
  8390. function s1_512_lo(xh, xl) {
  8391. var c0_lo = rotr64_lo(xh, xl, 14);
  8392. var c1_lo = rotr64_lo(xh, xl, 18);
  8393. var c2_lo = rotr64_lo(xl, xh, 9); // 41
  8394. var r = c0_lo ^ c1_lo ^ c2_lo;
  8395. if (r < 0)
  8396. r += 0x100000000;
  8397. return r;
  8398. }
  8399. function g0_512_hi(xh, xl) {
  8400. var c0_hi = rotr64_hi(xh, xl, 1);
  8401. var c1_hi = rotr64_hi(xh, xl, 8);
  8402. var c2_hi = shr64_hi(xh, xl, 7);
  8403. var r = c0_hi ^ c1_hi ^ c2_hi;
  8404. if (r < 0)
  8405. r += 0x100000000;
  8406. return r;
  8407. }
  8408. function g0_512_lo(xh, xl) {
  8409. var c0_lo = rotr64_lo(xh, xl, 1);
  8410. var c1_lo = rotr64_lo(xh, xl, 8);
  8411. var c2_lo = shr64_lo(xh, xl, 7);
  8412. var r = c0_lo ^ c1_lo ^ c2_lo;
  8413. if (r < 0)
  8414. r += 0x100000000;
  8415. return r;
  8416. }
  8417. function g1_512_hi(xh, xl) {
  8418. var c0_hi = rotr64_hi(xh, xl, 19);
  8419. var c1_hi = rotr64_hi(xl, xh, 29); // 61
  8420. var c2_hi = shr64_hi(xh, xl, 6);
  8421. var r = c0_hi ^ c1_hi ^ c2_hi;
  8422. if (r < 0)
  8423. r += 0x100000000;
  8424. return r;
  8425. }
  8426. function g1_512_lo(xh, xl) {
  8427. var c0_lo = rotr64_lo(xh, xl, 19);
  8428. var c1_lo = rotr64_lo(xl, xh, 29); // 61
  8429. var c2_lo = shr64_lo(xh, xl, 6);
  8430. var r = c0_lo ^ c1_lo ^ c2_lo;
  8431. if (r < 0)
  8432. r += 0x100000000;
  8433. return r;
  8434. }
  8435. },{"../hash":27}],32:[function(require,module,exports){
  8436. var utils = exports;
  8437. var inherits = require('inherits');
  8438. function toArray(msg, enc) {
  8439. if (Array.isArray(msg))
  8440. return msg.slice();
  8441. if (!msg)
  8442. return [];
  8443. var res = [];
  8444. if (typeof msg === 'string') {
  8445. if (!enc) {
  8446. for (var i = 0; i < msg.length; i++) {
  8447. var c = msg.charCodeAt(i);
  8448. var hi = c >> 8;
  8449. var lo = c & 0xff;
  8450. if (hi)
  8451. res.push(hi, lo);
  8452. else
  8453. res.push(lo);
  8454. }
  8455. } else if (enc === 'hex') {
  8456. msg = msg.replace(/[^a-z0-9]+/ig, '');
  8457. if (msg.length % 2 !== 0)
  8458. msg = '0' + msg;
  8459. for (var i = 0; i < msg.length; i += 2)
  8460. res.push(parseInt(msg[i] + msg[i + 1], 16));
  8461. }
  8462. } else {
  8463. for (var i = 0; i < msg.length; i++)
  8464. res[i] = msg[i] | 0;
  8465. }
  8466. return res;
  8467. }
  8468. utils.toArray = toArray;
  8469. function toHex(msg) {
  8470. var res = '';
  8471. for (var i = 0; i < msg.length; i++)
  8472. res += zero2(msg[i].toString(16));
  8473. return res;
  8474. }
  8475. utils.toHex = toHex;
  8476. function htonl(w) {
  8477. var res = (w >>> 24) |
  8478. ((w >>> 8) & 0xff00) |
  8479. ((w << 8) & 0xff0000) |
  8480. ((w & 0xff) << 24);
  8481. return res >>> 0;
  8482. }
  8483. utils.htonl = htonl;
  8484. function toHex32(msg, endian) {
  8485. var res = '';
  8486. for (var i = 0; i < msg.length; i++) {
  8487. var w = msg[i];
  8488. if (endian === 'little')
  8489. w = htonl(w);
  8490. res += zero8(w.toString(16));
  8491. }
  8492. return res;
  8493. }
  8494. utils.toHex32 = toHex32;
  8495. function zero2(word) {
  8496. if (word.length === 1)
  8497. return '0' + word;
  8498. else
  8499. return word;
  8500. }
  8501. utils.zero2 = zero2;
  8502. function zero8(word) {
  8503. if (word.length === 7)
  8504. return '0' + word;
  8505. else if (word.length === 6)
  8506. return '00' + word;
  8507. else if (word.length === 5)
  8508. return '000' + word;
  8509. else if (word.length === 4)
  8510. return '0000' + word;
  8511. else if (word.length === 3)
  8512. return '00000' + word;
  8513. else if (word.length === 2)
  8514. return '000000' + word;
  8515. else if (word.length === 1)
  8516. return '0000000' + word;
  8517. else
  8518. return word;
  8519. }
  8520. utils.zero8 = zero8;
  8521. function join32(msg, start, end, endian) {
  8522. var len = end - start;
  8523. assert(len % 4 === 0);
  8524. var res = new Array(len / 4);
  8525. for (var i = 0, k = start; i < res.length; i++, k += 4) {
  8526. var w;
  8527. if (endian === 'big')
  8528. w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3];
  8529. else
  8530. w = (msg[k + 3] << 24) | (msg[k + 2] << 16) | (msg[k + 1] << 8) | msg[k];
  8531. res[i] = w >>> 0;
  8532. }
  8533. return res;
  8534. }
  8535. utils.join32 = join32;
  8536. function split32(msg, endian) {
  8537. var res = new Array(msg.length * 4);
  8538. for (var i = 0, k = 0; i < msg.length; i++, k += 4) {
  8539. var m = msg[i];
  8540. if (endian === 'big') {
  8541. res[k] = m >>> 24;
  8542. res[k + 1] = (m >>> 16) & 0xff;
  8543. res[k + 2] = (m >>> 8) & 0xff;
  8544. res[k + 3] = m & 0xff;
  8545. } else {
  8546. res[k + 3] = m >>> 24;
  8547. res[k + 2] = (m >>> 16) & 0xff;
  8548. res[k + 1] = (m >>> 8) & 0xff;
  8549. res[k] = m & 0xff;
  8550. }
  8551. }
  8552. return res;
  8553. }
  8554. utils.split32 = split32;
  8555. function rotr32(w, b) {
  8556. return (w >>> b) | (w << (32 - b));
  8557. }
  8558. utils.rotr32 = rotr32;
  8559. function rotl32(w, b) {
  8560. return (w << b) | (w >>> (32 - b));
  8561. }
  8562. utils.rotl32 = rotl32;
  8563. function sum32(a, b) {
  8564. return (a + b) >>> 0;
  8565. }
  8566. utils.sum32 = sum32;
  8567. function sum32_3(a, b, c) {
  8568. return (a + b + c) >>> 0;
  8569. }
  8570. utils.sum32_3 = sum32_3;
  8571. function sum32_4(a, b, c, d) {
  8572. return (a + b + c + d) >>> 0;
  8573. }
  8574. utils.sum32_4 = sum32_4;
  8575. function sum32_5(a, b, c, d, e) {
  8576. return (a + b + c + d + e) >>> 0;
  8577. }
  8578. utils.sum32_5 = sum32_5;
  8579. function assert(cond, msg) {
  8580. if (!cond)
  8581. throw new Error(msg || 'Assertion failed');
  8582. }
  8583. utils.assert = assert;
  8584. utils.inherits = inherits;
  8585. function sum64(buf, pos, ah, al) {
  8586. var bh = buf[pos];
  8587. var bl = buf[pos + 1];
  8588. var lo = (al + bl) >>> 0;
  8589. var hi = (lo < al ? 1 : 0) + ah + bh;
  8590. buf[pos] = hi >>> 0;
  8591. buf[pos + 1] = lo;
  8592. }
  8593. exports.sum64 = sum64;
  8594. function sum64_hi(ah, al, bh, bl) {
  8595. var lo = (al + bl) >>> 0;
  8596. var hi = (lo < al ? 1 : 0) + ah + bh;
  8597. return hi >>> 0;
  8598. };
  8599. exports.sum64_hi = sum64_hi;
  8600. function sum64_lo(ah, al, bh, bl) {
  8601. var lo = al + bl;
  8602. return lo >>> 0;
  8603. };
  8604. exports.sum64_lo = sum64_lo;
  8605. function sum64_4_hi(ah, al, bh, bl, ch, cl, dh, dl) {
  8606. var carry = 0;
  8607. var lo = al;
  8608. lo = (lo + bl) >>> 0;
  8609. carry += lo < al ? 1 : 0;
  8610. lo = (lo + cl) >>> 0;
  8611. carry += lo < cl ? 1 : 0;
  8612. lo = (lo + dl) >>> 0;
  8613. carry += lo < dl ? 1 : 0;
  8614. var hi = ah + bh + ch + dh + carry;
  8615. return hi >>> 0;
  8616. };
  8617. exports.sum64_4_hi = sum64_4_hi;
  8618. function sum64_4_lo(ah, al, bh, bl, ch, cl, dh, dl) {
  8619. var lo = al + bl + cl + dl;
  8620. return lo >>> 0;
  8621. };
  8622. exports.sum64_4_lo = sum64_4_lo;
  8623. function sum64_5_hi(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
  8624. var carry = 0;
  8625. var lo = al;
  8626. lo = (lo + bl) >>> 0;
  8627. carry += lo < al ? 1 : 0;
  8628. lo = (lo + cl) >>> 0;
  8629. carry += lo < cl ? 1 : 0;
  8630. lo = (lo + dl) >>> 0;
  8631. carry += lo < dl ? 1 : 0;
  8632. lo = (lo + el) >>> 0;
  8633. carry += lo < el ? 1 : 0;
  8634. var hi = ah + bh + ch + dh + eh + carry;
  8635. return hi >>> 0;
  8636. };
  8637. exports.sum64_5_hi = sum64_5_hi;
  8638. function sum64_5_lo(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
  8639. var lo = al + bl + cl + dl + el;
  8640. return lo >>> 0;
  8641. };
  8642. exports.sum64_5_lo = sum64_5_lo;
  8643. function rotr64_hi(ah, al, num) {
  8644. var r = (al << (32 - num)) | (ah >>> num);
  8645. return r >>> 0;
  8646. };
  8647. exports.rotr64_hi = rotr64_hi;
  8648. function rotr64_lo(ah, al, num) {
  8649. var r = (ah << (32 - num)) | (al >>> num);
  8650. return r >>> 0;
  8651. };
  8652. exports.rotr64_lo = rotr64_lo;
  8653. function shr64_hi(ah, al, num) {
  8654. return ah >>> num;
  8655. };
  8656. exports.shr64_hi = shr64_hi;
  8657. function shr64_lo(ah, al, num) {
  8658. var r = (ah << (32 - num)) | (al >>> num);
  8659. return r >>> 0;
  8660. };
  8661. exports.shr64_lo = shr64_lo;
  8662. },{"inherits":34}],33:[function(require,module,exports){
  8663. 'use strict';
  8664. var hash = require('hash.js');
  8665. var utils = require('minimalistic-crypto-utils');
  8666. var assert = require('minimalistic-assert');
  8667. function HmacDRBG(options) {
  8668. if (!(this instanceof HmacDRBG))
  8669. return new HmacDRBG(options);
  8670. this.hash = options.hash;
  8671. this.predResist = !!options.predResist;
  8672. this.outLen = this.hash.outSize;
  8673. this.minEntropy = options.minEntropy || this.hash.hmacStrength;
  8674. this.reseed = null;
  8675. this.reseedInterval = null;
  8676. this.K = null;
  8677. this.V = null;
  8678. var entropy = utils.toArray(options.entropy, options.entropyEnc || 'hex');
  8679. var nonce = utils.toArray(options.nonce, options.nonceEnc || 'hex');
  8680. var pers = utils.toArray(options.pers, options.persEnc || 'hex');
  8681. assert(entropy.length >= (this.minEntropy / 8),
  8682. 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits');
  8683. this._init(entropy, nonce, pers);
  8684. }
  8685. module.exports = HmacDRBG;
  8686. HmacDRBG.prototype._init = function init(entropy, nonce, pers) {
  8687. var seed = entropy.concat(nonce).concat(pers);
  8688. this.K = new Array(this.outLen / 8);
  8689. this.V = new Array(this.outLen / 8);
  8690. for (var i = 0; i < this.V.length; i++) {
  8691. this.K[i] = 0x00;
  8692. this.V[i] = 0x01;
  8693. }
  8694. this._update(seed);
  8695. this.reseed = 1;
  8696. this.reseedInterval = 0x1000000000000; // 2^48
  8697. };
  8698. HmacDRBG.prototype._hmac = function hmac() {
  8699. return new hash.hmac(this.hash, this.K);
  8700. };
  8701. HmacDRBG.prototype._update = function update(seed) {
  8702. var kmac = this._hmac()
  8703. .update(this.V)
  8704. .update([ 0x00 ]);
  8705. if (seed)
  8706. kmac = kmac.update(seed);
  8707. this.K = kmac.digest();
  8708. this.V = this._hmac().update(this.V).digest();
  8709. if (!seed)
  8710. return;
  8711. this.K = this._hmac()
  8712. .update(this.V)
  8713. .update([ 0x01 ])
  8714. .update(seed)
  8715. .digest();
  8716. this.V = this._hmac().update(this.V).digest();
  8717. };
  8718. HmacDRBG.prototype.reseed = function reseed(entropy, entropyEnc, add, addEnc) {
  8719. // Optional entropy enc
  8720. if (typeof entropyEnc !== 'string') {
  8721. addEnc = add;
  8722. add = entropyEnc;
  8723. entropyEnc = null;
  8724. }
  8725. entropy = utils.toArray(entropy, entropyEnc);
  8726. add = utils.toArray(add, addEnc);
  8727. assert(entropy.length >= (this.minEntropy / 8),
  8728. 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits');
  8729. this._update(entropy.concat(add || []));
  8730. this.reseed = 1;
  8731. };
  8732. HmacDRBG.prototype.generate = function generate(len, enc, add, addEnc) {
  8733. if (this.reseed > this.reseedInterval)
  8734. throw new Error('Reseed is required');
  8735. // Optional encoding
  8736. if (typeof enc !== 'string') {
  8737. addEnc = add;
  8738. add = enc;
  8739. enc = null;
  8740. }
  8741. // Optional additional data
  8742. if (add) {
  8743. add = utils.toArray(add, addEnc || 'hex');
  8744. this._update(add);
  8745. }
  8746. var temp = [];
  8747. while (temp.length < len) {
  8748. this.V = this._hmac().update(this.V).digest();
  8749. temp = temp.concat(this.V);
  8750. }
  8751. var res = temp.slice(0, len);
  8752. this._update(add);
  8753. this.reseed++;
  8754. return utils.encode(res, enc);
  8755. };
  8756. },{"hash.js":27,"minimalistic-assert":42,"minimalistic-crypto-utils":43}],34:[function(require,module,exports){
  8757. if (typeof Object.create === 'function') {
  8758. // implementation from standard node.js 'util' module
  8759. module.exports = function inherits(ctor, superCtor) {
  8760. ctor.super_ = superCtor
  8761. ctor.prototype = Object.create(superCtor.prototype, {
  8762. constructor: {
  8763. value: ctor,
  8764. enumerable: false,
  8765. writable: true,
  8766. configurable: true
  8767. }
  8768. });
  8769. };
  8770. } else {
  8771. // old school shim for old browsers
  8772. module.exports = function inherits(ctor, superCtor) {
  8773. ctor.super_ = superCtor
  8774. var TempCtor = function () {}
  8775. TempCtor.prototype = superCtor.prototype
  8776. ctor.prototype = new TempCtor()
  8777. ctor.prototype.constructor = ctor
  8778. }
  8779. }
  8780. },{}],35:[function(require,module,exports){
  8781. /**
  8782. * Returns a `Boolean` on whether or not the a `String` starts with '0x'
  8783. * @param {String} str the string input value
  8784. * @return {Boolean} a boolean if it is or is not hex prefixed
  8785. * @throws if the str input is not a string
  8786. */
  8787. module.exports = function isHexPrefixed(str) {
  8788. if (typeof str !== 'string') {
  8789. throw new Error("[is-hex-prefixed] value must be type 'string', is currently type " + (typeof str) + ", while checking isHexPrefixed.");
  8790. }
  8791. return str.slice(0, 2) === '0x';
  8792. }
  8793. },{}],36:[function(require,module,exports){
  8794. 'use strict'
  8795. module.exports = require('./lib/api')(require('./lib/keccak'))
  8796. },{"./lib/api":37,"./lib/keccak":41}],37:[function(require,module,exports){
  8797. 'use strict'
  8798. var createKeccak = require('./keccak')
  8799. var createShake = require('./shake')
  8800. module.exports = function (KeccakState) {
  8801. var Keccak = createKeccak(KeccakState)
  8802. var Shake = createShake(KeccakState)
  8803. return function (algorithm, options) {
  8804. var hash = typeof algorithm === 'string' ? algorithm.toLowerCase() : algorithm
  8805. switch (hash) {
  8806. case 'keccak224': return new Keccak(1152, 448, null, 224, options)
  8807. case 'keccak256': return new Keccak(1088, 512, null, 256, options)
  8808. case 'keccak384': return new Keccak(832, 768, null, 384, options)
  8809. case 'keccak512': return new Keccak(576, 1024, null, 512, options)
  8810. case 'sha3-224': return new Keccak(1152, 448, 0x06, 224, options)
  8811. case 'sha3-256': return new Keccak(1088, 512, 0x06, 256, options)
  8812. case 'sha3-384': return new Keccak(832, 768, 0x06, 384, options)
  8813. case 'sha3-512': return new Keccak(576, 1024, 0x06, 512, options)
  8814. case 'shake128': return new Shake(1344, 256, 0x1f, options)
  8815. case 'shake256': return new Shake(1088, 512, 0x1f, options)
  8816. default: throw new Error('Invald algorithm: ' + algorithm)
  8817. }
  8818. }
  8819. }
  8820. },{"./keccak":38,"./shake":39}],38:[function(require,module,exports){
  8821. (function (Buffer){
  8822. 'use strict'
  8823. var Transform = require('stream').Transform
  8824. var inherits = require('inherits')
  8825. module.exports = function (KeccakState) {
  8826. function Keccak (rate, capacity, delimitedSuffix, hashBitLength, options) {
  8827. Transform.call(this, options)
  8828. this._rate = rate
  8829. this._capacity = capacity
  8830. this._delimitedSuffix = delimitedSuffix
  8831. this._hashBitLength = hashBitLength
  8832. this._options = options
  8833. this._state = new KeccakState()
  8834. this._state.initialize(rate, capacity)
  8835. this._finalized = false
  8836. }
  8837. inherits(Keccak, Transform)
  8838. Keccak.prototype._transform = function (chunk, encoding, callback) {
  8839. var error = null
  8840. try {
  8841. this.update(chunk, encoding)
  8842. } catch (err) {
  8843. error = err
  8844. }
  8845. callback(error)
  8846. }
  8847. Keccak.prototype._flush = function (callback) {
  8848. var error = null
  8849. try {
  8850. this.push(this.digest())
  8851. } catch (err) {
  8852. error = err
  8853. }
  8854. callback(error)
  8855. }
  8856. Keccak.prototype.update = function (data, encoding) {
  8857. if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
  8858. if (this._finalized) throw new Error('Digest already called')
  8859. if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding)
  8860. this._state.absorb(data)
  8861. return this
  8862. }
  8863. Keccak.prototype.digest = function (encoding) {
  8864. if (this._finalized) throw new Error('Digest already called')
  8865. this._finalized = true
  8866. if (this._delimitedSuffix) this._state.absorbLastFewBits(this._delimitedSuffix)
  8867. var digest = this._state.squeeze(this._hashBitLength / 8)
  8868. if (encoding !== undefined) digest = digest.toString(encoding)
  8869. this._resetState()
  8870. return digest
  8871. }
  8872. // remove result from memory
  8873. Keccak.prototype._resetState = function () {
  8874. this._state.initialize(this._rate, this._capacity)
  8875. return this
  8876. }
  8877. // because sometimes we need hash right now and little later
  8878. Keccak.prototype._clone = function () {
  8879. var clone = new Keccak(this._rate, this._capacity, this._delimitedSuffix, this._hashBitLength, this._options)
  8880. this._state.copy(clone._state)
  8881. clone._finalized = this._finalized
  8882. return clone
  8883. }
  8884. return Keccak
  8885. }
  8886. }).call(this,require("buffer").Buffer)
  8887. },{"buffer":65,"inherits":34,"stream":85}],39:[function(require,module,exports){
  8888. (function (Buffer){
  8889. 'use strict'
  8890. var Transform = require('stream').Transform
  8891. var inherits = require('inherits')
  8892. module.exports = function (KeccakState) {
  8893. function Shake (rate, capacity, delimitedSuffix, options) {
  8894. Transform.call(this, options)
  8895. this._rate = rate
  8896. this._capacity = capacity
  8897. this._delimitedSuffix = delimitedSuffix
  8898. this._options = options
  8899. this._state = new KeccakState()
  8900. this._state.initialize(rate, capacity)
  8901. this._finalized = false
  8902. }
  8903. inherits(Shake, Transform)
  8904. Shake.prototype._transform = function (chunk, encoding, callback) {
  8905. var error = null
  8906. try {
  8907. this.update(chunk, encoding)
  8908. } catch (err) {
  8909. error = err
  8910. }
  8911. callback(error)
  8912. }
  8913. Shake.prototype._flush = function () {}
  8914. Shake.prototype._read = function (size) {
  8915. this.push(this.squeeze(size))
  8916. }
  8917. Shake.prototype.update = function (data, encoding) {
  8918. if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
  8919. if (this._finalized) throw new Error('Squeeze already called')
  8920. if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding)
  8921. this._state.absorb(data)
  8922. return this
  8923. }
  8924. Shake.prototype.squeeze = function (dataByteLength, encoding) {
  8925. if (!this._finalized) {
  8926. this._finalized = true
  8927. this._state.absorbLastFewBits(this._delimitedSuffix)
  8928. }
  8929. var data = this._state.squeeze(dataByteLength)
  8930. if (encoding !== undefined) data = data.toString(encoding)
  8931. return data
  8932. }
  8933. Shake.prototype._resetState = function () {
  8934. this._state.initialize(this._rate, this._capacity)
  8935. return this
  8936. }
  8937. Shake.prototype._clone = function () {
  8938. var clone = new Shake(this._rate, this._capacity, this._delimitedSuffix, this._options)
  8939. this._state.copy(clone._state)
  8940. clone._finalized = this._finalized
  8941. return clone
  8942. }
  8943. return Shake
  8944. }
  8945. }).call(this,require("buffer").Buffer)
  8946. },{"buffer":65,"inherits":34,"stream":85}],40:[function(require,module,exports){
  8947. 'use strict'
  8948. var P1600_ROUND_CONSTANTS = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648]
  8949. exports.p1600 = function (s) {
  8950. for (var round = 0; round < 24; ++round) {
  8951. // theta
  8952. var lo0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40]
  8953. var hi0 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41]
  8954. var lo1 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42]
  8955. var hi1 = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43]
  8956. var lo2 = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44]
  8957. var hi2 = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45]
  8958. var lo3 = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46]
  8959. var hi3 = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47]
  8960. var lo4 = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48]
  8961. var hi4 = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49]
  8962. var lo = lo4 ^ (lo1 << 1 | hi1 >>> 31)
  8963. var hi = hi4 ^ (hi1 << 1 | lo1 >>> 31)
  8964. var t1slo0 = s[0] ^ lo
  8965. var t1shi0 = s[1] ^ hi
  8966. var t1slo5 = s[10] ^ lo
  8967. var t1shi5 = s[11] ^ hi
  8968. var t1slo10 = s[20] ^ lo
  8969. var t1shi10 = s[21] ^ hi
  8970. var t1slo15 = s[30] ^ lo
  8971. var t1shi15 = s[31] ^ hi
  8972. var t1slo20 = s[40] ^ lo
  8973. var t1shi20 = s[41] ^ hi
  8974. lo = lo0 ^ (lo2 << 1 | hi2 >>> 31)
  8975. hi = hi0 ^ (hi2 << 1 | lo2 >>> 31)
  8976. var t1slo1 = s[2] ^ lo
  8977. var t1shi1 = s[3] ^ hi
  8978. var t1slo6 = s[12] ^ lo
  8979. var t1shi6 = s[13] ^ hi
  8980. var t1slo11 = s[22] ^ lo
  8981. var t1shi11 = s[23] ^ hi
  8982. var t1slo16 = s[32] ^ lo
  8983. var t1shi16 = s[33] ^ hi
  8984. var t1slo21 = s[42] ^ lo
  8985. var t1shi21 = s[43] ^ hi
  8986. lo = lo1 ^ (lo3 << 1 | hi3 >>> 31)
  8987. hi = hi1 ^ (hi3 << 1 | lo3 >>> 31)
  8988. var t1slo2 = s[4] ^ lo
  8989. var t1shi2 = s[5] ^ hi
  8990. var t1slo7 = s[14] ^ lo
  8991. var t1shi7 = s[15] ^ hi
  8992. var t1slo12 = s[24] ^ lo
  8993. var t1shi12 = s[25] ^ hi
  8994. var t1slo17 = s[34] ^ lo
  8995. var t1shi17 = s[35] ^ hi
  8996. var t1slo22 = s[44] ^ lo
  8997. var t1shi22 = s[45] ^ hi
  8998. lo = lo2 ^ (lo4 << 1 | hi4 >>> 31)
  8999. hi = hi2 ^ (hi4 << 1 | lo4 >>> 31)
  9000. var t1slo3 = s[6] ^ lo
  9001. var t1shi3 = s[7] ^ hi
  9002. var t1slo8 = s[16] ^ lo
  9003. var t1shi8 = s[17] ^ hi
  9004. var t1slo13 = s[26] ^ lo
  9005. var t1shi13 = s[27] ^ hi
  9006. var t1slo18 = s[36] ^ lo
  9007. var t1shi18 = s[37] ^ hi
  9008. var t1slo23 = s[46] ^ lo
  9009. var t1shi23 = s[47] ^ hi
  9010. lo = lo3 ^ (lo0 << 1 | hi0 >>> 31)
  9011. hi = hi3 ^ (hi0 << 1 | lo0 >>> 31)
  9012. var t1slo4 = s[8] ^ lo
  9013. var t1shi4 = s[9] ^ hi
  9014. var t1slo9 = s[18] ^ lo
  9015. var t1shi9 = s[19] ^ hi
  9016. var t1slo14 = s[28] ^ lo
  9017. var t1shi14 = s[29] ^ hi
  9018. var t1slo19 = s[38] ^ lo
  9019. var t1shi19 = s[39] ^ hi
  9020. var t1slo24 = s[48] ^ lo
  9021. var t1shi24 = s[49] ^ hi
  9022. // rho & pi
  9023. var t2slo0 = t1slo0
  9024. var t2shi0 = t1shi0
  9025. var t2slo16 = (t1shi5 << 4 | t1slo5 >>> 28)
  9026. var t2shi16 = (t1slo5 << 4 | t1shi5 >>> 28)
  9027. var t2slo7 = (t1slo10 << 3 | t1shi10 >>> 29)
  9028. var t2shi7 = (t1shi10 << 3 | t1slo10 >>> 29)
  9029. var t2slo23 = (t1shi15 << 9 | t1slo15 >>> 23)
  9030. var t2shi23 = (t1slo15 << 9 | t1shi15 >>> 23)
  9031. var t2slo14 = (t1slo20 << 18 | t1shi20 >>> 14)
  9032. var t2shi14 = (t1shi20 << 18 | t1slo20 >>> 14)
  9033. var t2slo10 = (t1slo1 << 1 | t1shi1 >>> 31)
  9034. var t2shi10 = (t1shi1 << 1 | t1slo1 >>> 31)
  9035. var t2slo1 = (t1shi6 << 12 | t1slo6 >>> 20)
  9036. var t2shi1 = (t1slo6 << 12 | t1shi6 >>> 20)
  9037. var t2slo17 = (t1slo11 << 10 | t1shi11 >>> 22)
  9038. var t2shi17 = (t1shi11 << 10 | t1slo11 >>> 22)
  9039. var t2slo8 = (t1shi16 << 13 | t1slo16 >>> 19)
  9040. var t2shi8 = (t1slo16 << 13 | t1shi16 >>> 19)
  9041. var t2slo24 = (t1slo21 << 2 | t1shi21 >>> 30)
  9042. var t2shi24 = (t1shi21 << 2 | t1slo21 >>> 30)
  9043. var t2slo20 = (t1shi2 << 30 | t1slo2 >>> 2)
  9044. var t2shi20 = (t1slo2 << 30 | t1shi2 >>> 2)
  9045. var t2slo11 = (t1slo7 << 6 | t1shi7 >>> 26)
  9046. var t2shi11 = (t1shi7 << 6 | t1slo7 >>> 26)
  9047. var t2slo2 = (t1shi12 << 11 | t1slo12 >>> 21)
  9048. var t2shi2 = (t1slo12 << 11 | t1shi12 >>> 21)
  9049. var t2slo18 = (t1slo17 << 15 | t1shi17 >>> 17)
  9050. var t2shi18 = (t1shi17 << 15 | t1slo17 >>> 17)
  9051. var t2slo9 = (t1shi22 << 29 | t1slo22 >>> 3)
  9052. var t2shi9 = (t1slo22 << 29 | t1shi22 >>> 3)
  9053. var t2slo5 = (t1slo3 << 28 | t1shi3 >>> 4)
  9054. var t2shi5 = (t1shi3 << 28 | t1slo3 >>> 4)
  9055. var t2slo21 = (t1shi8 << 23 | t1slo8 >>> 9)
  9056. var t2shi21 = (t1slo8 << 23 | t1shi8 >>> 9)
  9057. var t2slo12 = (t1slo13 << 25 | t1shi13 >>> 7)
  9058. var t2shi12 = (t1shi13 << 25 | t1slo13 >>> 7)
  9059. var t2slo3 = (t1slo18 << 21 | t1shi18 >>> 11)
  9060. var t2shi3 = (t1shi18 << 21 | t1slo18 >>> 11)
  9061. var t2slo19 = (t1shi23 << 24 | t1slo23 >>> 8)
  9062. var t2shi19 = (t1slo23 << 24 | t1shi23 >>> 8)
  9063. var t2slo15 = (t1slo4 << 27 | t1shi4 >>> 5)
  9064. var t2shi15 = (t1shi4 << 27 | t1slo4 >>> 5)
  9065. var t2slo6 = (t1slo9 << 20 | t1shi9 >>> 12)
  9066. var t2shi6 = (t1shi9 << 20 | t1slo9 >>> 12)
  9067. var t2slo22 = (t1shi14 << 7 | t1slo14 >>> 25)
  9068. var t2shi22 = (t1slo14 << 7 | t1shi14 >>> 25)
  9069. var t2slo13 = (t1slo19 << 8 | t1shi19 >>> 24)
  9070. var t2shi13 = (t1shi19 << 8 | t1slo19 >>> 24)
  9071. var t2slo4 = (t1slo24 << 14 | t1shi24 >>> 18)
  9072. var t2shi4 = (t1shi24 << 14 | t1slo24 >>> 18)
  9073. // chi
  9074. s[0] = t2slo0 ^ (~t2slo1 & t2slo2)
  9075. s[1] = t2shi0 ^ (~t2shi1 & t2shi2)
  9076. s[10] = t2slo5 ^ (~t2slo6 & t2slo7)
  9077. s[11] = t2shi5 ^ (~t2shi6 & t2shi7)
  9078. s[20] = t2slo10 ^ (~t2slo11 & t2slo12)
  9079. s[21] = t2shi10 ^ (~t2shi11 & t2shi12)
  9080. s[30] = t2slo15 ^ (~t2slo16 & t2slo17)
  9081. s[31] = t2shi15 ^ (~t2shi16 & t2shi17)
  9082. s[40] = t2slo20 ^ (~t2slo21 & t2slo22)
  9083. s[41] = t2shi20 ^ (~t2shi21 & t2shi22)
  9084. s[2] = t2slo1 ^ (~t2slo2 & t2slo3)
  9085. s[3] = t2shi1 ^ (~t2shi2 & t2shi3)
  9086. s[12] = t2slo6 ^ (~t2slo7 & t2slo8)
  9087. s[13] = t2shi6 ^ (~t2shi7 & t2shi8)
  9088. s[22] = t2slo11 ^ (~t2slo12 & t2slo13)
  9089. s[23] = t2shi11 ^ (~t2shi12 & t2shi13)
  9090. s[32] = t2slo16 ^ (~t2slo17 & t2slo18)
  9091. s[33] = t2shi16 ^ (~t2shi17 & t2shi18)
  9092. s[42] = t2slo21 ^ (~t2slo22 & t2slo23)
  9093. s[43] = t2shi21 ^ (~t2shi22 & t2shi23)
  9094. s[4] = t2slo2 ^ (~t2slo3 & t2slo4)
  9095. s[5] = t2shi2 ^ (~t2shi3 & t2shi4)
  9096. s[14] = t2slo7 ^ (~t2slo8 & t2slo9)
  9097. s[15] = t2shi7 ^ (~t2shi8 & t2shi9)
  9098. s[24] = t2slo12 ^ (~t2slo13 & t2slo14)
  9099. s[25] = t2shi12 ^ (~t2shi13 & t2shi14)
  9100. s[34] = t2slo17 ^ (~t2slo18 & t2slo19)
  9101. s[35] = t2shi17 ^ (~t2shi18 & t2shi19)
  9102. s[44] = t2slo22 ^ (~t2slo23 & t2slo24)
  9103. s[45] = t2shi22 ^ (~t2shi23 & t2shi24)
  9104. s[6] = t2slo3 ^ (~t2slo4 & t2slo0)
  9105. s[7] = t2shi3 ^ (~t2shi4 & t2shi0)
  9106. s[16] = t2slo8 ^ (~t2slo9 & t2slo5)
  9107. s[17] = t2shi8 ^ (~t2shi9 & t2shi5)
  9108. s[26] = t2slo13 ^ (~t2slo14 & t2slo10)
  9109. s[27] = t2shi13 ^ (~t2shi14 & t2shi10)
  9110. s[36] = t2slo18 ^ (~t2slo19 & t2slo15)
  9111. s[37] = t2shi18 ^ (~t2shi19 & t2shi15)
  9112. s[46] = t2slo23 ^ (~t2slo24 & t2slo20)
  9113. s[47] = t2shi23 ^ (~t2shi24 & t2shi20)
  9114. s[8] = t2slo4 ^ (~t2slo0 & t2slo1)
  9115. s[9] = t2shi4 ^ (~t2shi0 & t2shi1)
  9116. s[18] = t2slo9 ^ (~t2slo5 & t2slo6)
  9117. s[19] = t2shi9 ^ (~t2shi5 & t2shi6)
  9118. s[28] = t2slo14 ^ (~t2slo10 & t2slo11)
  9119. s[29] = t2shi14 ^ (~t2shi10 & t2shi11)
  9120. s[38] = t2slo19 ^ (~t2slo15 & t2slo16)
  9121. s[39] = t2shi19 ^ (~t2shi15 & t2shi16)
  9122. s[48] = t2slo24 ^ (~t2slo20 & t2slo21)
  9123. s[49] = t2shi24 ^ (~t2shi20 & t2shi21)
  9124. // iota
  9125. s[0] ^= P1600_ROUND_CONSTANTS[round * 2]
  9126. s[1] ^= P1600_ROUND_CONSTANTS[round * 2 + 1]
  9127. }
  9128. }
  9129. },{}],41:[function(require,module,exports){
  9130. (function (Buffer){
  9131. 'use strict'
  9132. var keccakState = require('./keccak-state-unroll')
  9133. function Keccak () {
  9134. // much faster than `new Array(50)`
  9135. this.state = [
  9136. 0, 0, 0, 0, 0,
  9137. 0, 0, 0, 0, 0,
  9138. 0, 0, 0, 0, 0,
  9139. 0, 0, 0, 0, 0,
  9140. 0, 0, 0, 0, 0
  9141. ]
  9142. this.blockSize = null
  9143. this.count = 0
  9144. this.squeezing = false
  9145. }
  9146. Keccak.prototype.initialize = function (rate, capacity) {
  9147. for (var i = 0; i < 50; ++i) this.state[i] = 0
  9148. this.blockSize = rate / 8
  9149. this.count = 0
  9150. this.squeezing = false
  9151. }
  9152. Keccak.prototype.absorb = function (data) {
  9153. for (var i = 0; i < data.length; ++i) {
  9154. this.state[~~(this.count / 4)] ^= data[i] << (8 * (this.count % 4))
  9155. this.count += 1
  9156. if (this.count === this.blockSize) {
  9157. keccakState.p1600(this.state)
  9158. this.count = 0
  9159. }
  9160. }
  9161. }
  9162. Keccak.prototype.absorbLastFewBits = function (bits) {
  9163. this.state[~~(this.count / 4)] ^= bits << (8 * (this.count % 4))
  9164. if ((bits & 0x80) !== 0 && this.count === (this.blockSize - 1)) keccakState.p1600(this.state)
  9165. this.state[~~((this.blockSize - 1) / 4)] ^= 0x80 << (8 * ((this.blockSize - 1) % 4))
  9166. keccakState.p1600(this.state)
  9167. this.count = 0
  9168. this.squeezing = true
  9169. }
  9170. Keccak.prototype.squeeze = function (length) {
  9171. if (!this.squeezing) this.absorbLastFewBits(0x01)
  9172. var output = new Buffer(length)
  9173. for (var i = 0; i < length; ++i) {
  9174. output[i] = (this.state[~~(this.count / 4)] >>> (8 * (this.count % 4))) & 0xff
  9175. this.count += 1
  9176. if (this.count === this.blockSize) {
  9177. keccakState.p1600(this.state)
  9178. this.count = 0
  9179. }
  9180. }
  9181. return output
  9182. }
  9183. Keccak.prototype.copy = function (dest) {
  9184. for (var i = 0; i < 50; ++i) dest.state[i] = this.state[i]
  9185. dest.blockSize = this.blockSize
  9186. dest.count = this.count
  9187. dest.squeezing = this.squeezing
  9188. }
  9189. module.exports = Keccak
  9190. }).call(this,require("buffer").Buffer)
  9191. },{"./keccak-state-unroll":40,"buffer":65}],42:[function(require,module,exports){
  9192. module.exports = assert;
  9193. function assert(val, msg) {
  9194. if (!val)
  9195. throw new Error(msg || 'Assertion failed');
  9196. }
  9197. assert.equal = function assertEqual(l, r, msg) {
  9198. if (l != r)
  9199. throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r));
  9200. };
  9201. },{}],43:[function(require,module,exports){
  9202. 'use strict';
  9203. var utils = exports;
  9204. function toArray(msg, enc) {
  9205. if (Array.isArray(msg))
  9206. return msg.slice();
  9207. if (!msg)
  9208. return [];
  9209. var res = [];
  9210. if (typeof msg !== 'string') {
  9211. for (var i = 0; i < msg.length; i++)
  9212. res[i] = msg[i] | 0;
  9213. return res;
  9214. }
  9215. if (enc === 'hex') {
  9216. msg = msg.replace(/[^a-z0-9]+/ig, '');
  9217. if (msg.length % 2 !== 0)
  9218. msg = '0' + msg;
  9219. for (var i = 0; i < msg.length; i += 2)
  9220. res.push(parseInt(msg[i] + msg[i + 1], 16));
  9221. } else {
  9222. for (var i = 0; i < msg.length; i++) {
  9223. var c = msg.charCodeAt(i);
  9224. var hi = c >> 8;
  9225. var lo = c & 0xff;
  9226. if (hi)
  9227. res.push(hi, lo);
  9228. else
  9229. res.push(lo);
  9230. }
  9231. }
  9232. return res;
  9233. }
  9234. utils.toArray = toArray;
  9235. function zero2(word) {
  9236. if (word.length === 1)
  9237. return '0' + word;
  9238. else
  9239. return word;
  9240. }
  9241. utils.zero2 = zero2;
  9242. function toHex(msg) {
  9243. var res = '';
  9244. for (var i = 0; i < msg.length; i++)
  9245. res += zero2(msg[i].toString(16));
  9246. return res;
  9247. }
  9248. utils.toHex = toHex;
  9249. utils.encode = function encode(arr, enc) {
  9250. if (enc === 'hex')
  9251. return toHex(arr);
  9252. else
  9253. return arr;
  9254. };
  9255. },{}],44:[function(require,module,exports){
  9256. (function (Buffer){
  9257. /*
  9258. CryptoJS v3.1.2
  9259. code.google.com/p/crypto-js
  9260. (c) 2009-2013 by Jeff Mott. All rights reserved.
  9261. code.google.com/p/crypto-js/wiki/License
  9262. */
  9263. /** @preserve
  9264. (c) 2012 by Cédric Mesnil. All rights reserved.
  9265. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  9266. - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9267. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  9268. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9269. */
  9270. // constants table
  9271. var zl = [
  9272. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  9273. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  9274. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  9275. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  9276. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
  9277. ]
  9278. var zr = [
  9279. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  9280. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  9281. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  9282. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  9283. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
  9284. ]
  9285. var sl = [
  9286. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  9287. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  9288. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  9289. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  9290. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
  9291. ]
  9292. var sr = [
  9293. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  9294. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  9295. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  9296. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  9297. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
  9298. ]
  9299. var hl = [0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]
  9300. var hr = [0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]
  9301. function bytesToWords (bytes) {
  9302. var words = []
  9303. for (var i = 0, b = 0; i < bytes.length; i++, b += 8) {
  9304. words[b >>> 5] |= bytes[i] << (24 - b % 32)
  9305. }
  9306. return words
  9307. }
  9308. function wordsToBytes (words) {
  9309. var bytes = []
  9310. for (var b = 0; b < words.length * 32; b += 8) {
  9311. bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF)
  9312. }
  9313. return bytes
  9314. }
  9315. function processBlock (H, M, offset) {
  9316. // swap endian
  9317. for (var i = 0; i < 16; i++) {
  9318. var offset_i = offset + i
  9319. var M_offset_i = M[offset_i]
  9320. // Swap
  9321. M[offset_i] = (
  9322. (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
  9323. (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
  9324. )
  9325. }
  9326. // Working variables
  9327. var al, bl, cl, dl, el
  9328. var ar, br, cr, dr, er
  9329. ar = al = H[0]
  9330. br = bl = H[1]
  9331. cr = cl = H[2]
  9332. dr = dl = H[3]
  9333. er = el = H[4]
  9334. // computation
  9335. var t
  9336. for (i = 0; i < 80; i += 1) {
  9337. t = (al + M[offset + zl[i]]) | 0
  9338. if (i < 16) {
  9339. t += f1(bl, cl, dl) + hl[0]
  9340. } else if (i < 32) {
  9341. t += f2(bl, cl, dl) + hl[1]
  9342. } else if (i < 48) {
  9343. t += f3(bl, cl, dl) + hl[2]
  9344. } else if (i < 64) {
  9345. t += f4(bl, cl, dl) + hl[3]
  9346. } else {// if (i<80) {
  9347. t += f5(bl, cl, dl) + hl[4]
  9348. }
  9349. t = t | 0
  9350. t = rotl(t, sl[i])
  9351. t = (t + el) | 0
  9352. al = el
  9353. el = dl
  9354. dl = rotl(cl, 10)
  9355. cl = bl
  9356. bl = t
  9357. t = (ar + M[offset + zr[i]]) | 0
  9358. if (i < 16) {
  9359. t += f5(br, cr, dr) + hr[0]
  9360. } else if (i < 32) {
  9361. t += f4(br, cr, dr) + hr[1]
  9362. } else if (i < 48) {
  9363. t += f3(br, cr, dr) + hr[2]
  9364. } else if (i < 64) {
  9365. t += f2(br, cr, dr) + hr[3]
  9366. } else {// if (i<80) {
  9367. t += f1(br, cr, dr) + hr[4]
  9368. }
  9369. t = t | 0
  9370. t = rotl(t, sr[i])
  9371. t = (t + er) | 0
  9372. ar = er
  9373. er = dr
  9374. dr = rotl(cr, 10)
  9375. cr = br
  9376. br = t
  9377. }
  9378. // intermediate hash value
  9379. t = (H[1] + cl + dr) | 0
  9380. H[1] = (H[2] + dl + er) | 0
  9381. H[2] = (H[3] + el + ar) | 0
  9382. H[3] = (H[4] + al + br) | 0
  9383. H[4] = (H[0] + bl + cr) | 0
  9384. H[0] = t
  9385. }
  9386. function f1 (x, y, z) {
  9387. return ((x) ^ (y) ^ (z))
  9388. }
  9389. function f2 (x, y, z) {
  9390. return (((x) & (y)) | ((~x) & (z)))
  9391. }
  9392. function f3 (x, y, z) {
  9393. return (((x) | (~(y))) ^ (z))
  9394. }
  9395. function f4 (x, y, z) {
  9396. return (((x) & (z)) | ((y) & (~(z))))
  9397. }
  9398. function f5 (x, y, z) {
  9399. return ((x) ^ ((y) | (~(z))))
  9400. }
  9401. function rotl (x, n) {
  9402. return (x << n) | (x >>> (32 - n))
  9403. }
  9404. function ripemd160 (message) {
  9405. var H = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
  9406. if (typeof message === 'string') {
  9407. message = new Buffer(message, 'utf8')
  9408. }
  9409. var m = bytesToWords(message)
  9410. var nBitsLeft = message.length * 8
  9411. var nBitsTotal = message.length * 8
  9412. // Add padding
  9413. m[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32)
  9414. m[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  9415. (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
  9416. (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
  9417. )
  9418. for (var i = 0; i < m.length; i += 16) {
  9419. processBlock(H, m, i)
  9420. }
  9421. // swap endian
  9422. for (i = 0; i < 5; i++) {
  9423. // shortcut
  9424. var H_i = H[i]
  9425. // Swap
  9426. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
  9427. (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00)
  9428. }
  9429. var digestbytes = wordsToBytes(H)
  9430. return new Buffer(digestbytes)
  9431. }
  9432. module.exports = ripemd160
  9433. }).call(this,require("buffer").Buffer)
  9434. },{"buffer":65}],45:[function(require,module,exports){
  9435. (function (Buffer){
  9436. const assert = require('assert')
  9437. /**
  9438. * RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP
  9439. * This function takes in a data, convert it to buffer if not, and a length for recursion
  9440. *
  9441. * @param {Buffer,String,Integer,Array} data - will be converted to buffer
  9442. * @returns {Buffer} - returns buffer of encoded data
  9443. **/
  9444. exports.encode = function (input) {
  9445. if (input instanceof Array) {
  9446. var output = []
  9447. for (var i = 0; i < input.length; i++) {
  9448. output.push(exports.encode(input[i]))
  9449. }
  9450. var buf = Buffer.concat(output)
  9451. return Buffer.concat([encodeLength(buf.length, 192), buf])
  9452. } else {
  9453. input = toBuffer(input)
  9454. if (input.length === 1 && input[0] < 128) {
  9455. return input
  9456. } else {
  9457. return Buffer.concat([encodeLength(input.length, 128), input])
  9458. }
  9459. }
  9460. }
  9461. function safeParseInt (v, base) {
  9462. if (v.slice(0, 2) === '00') {
  9463. throw (new Error('invalid RLP: extra zeros'))
  9464. }
  9465. return parseInt(v, base)
  9466. }
  9467. function encodeLength (len, offset) {
  9468. if (len < 56) {
  9469. return new Buffer([len + offset])
  9470. } else {
  9471. var hexLength = intToHex(len)
  9472. var lLength = hexLength.length / 2
  9473. var firstByte = intToHex(offset + 55 + lLength)
  9474. return new Buffer(firstByte + hexLength, 'hex')
  9475. }
  9476. }
  9477. /**
  9478. * RLP Decoding based on: {@link https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP|RLP}
  9479. * @param {Buffer,String,Integer,Array} data - will be converted to buffer
  9480. * @returns {Array} - returns decode Array of Buffers containg the original message
  9481. **/
  9482. exports.decode = function (input, stream) {
  9483. if (!input || input.length === 0) {
  9484. return new Buffer([])
  9485. }
  9486. input = toBuffer(input)
  9487. var decoded = _decode(input)
  9488. if (stream) {
  9489. return decoded
  9490. }
  9491. assert.equal(decoded.remainder.length, 0, 'invalid remainder')
  9492. return decoded.data
  9493. }
  9494. exports.getLength = function (input) {
  9495. if (!input || input.length === 0) {
  9496. return new Buffer([])
  9497. }
  9498. input = toBuffer(input)
  9499. var firstByte = input[0]
  9500. if (firstByte <= 0x7f) {
  9501. return input.length
  9502. } else if (firstByte <= 0xb7) {
  9503. return firstByte - 0x7f
  9504. } else if (firstByte <= 0xbf) {
  9505. return firstByte - 0xb6
  9506. } else if (firstByte <= 0xf7) {
  9507. // a list between 0-55 bytes long
  9508. return firstByte - 0xbf
  9509. } else {
  9510. // a list over 55 bytes long
  9511. var llength = firstByte - 0xf6
  9512. var length = safeParseInt(input.slice(1, llength).toString('hex'), 16)
  9513. return llength + length
  9514. }
  9515. }
  9516. function _decode (input) {
  9517. var length, llength, data, innerRemainder, d
  9518. var decoded = []
  9519. var firstByte = input[0]
  9520. if (firstByte <= 0x7f) {
  9521. // a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding.
  9522. return {
  9523. data: input.slice(0, 1),
  9524. remainder: input.slice(1)
  9525. }
  9526. } else if (firstByte <= 0xb7) {
  9527. // string is 0-55 bytes long. A single byte with value 0x80 plus the length of the string followed by the string
  9528. // The range of the first byte is [0x80, 0xb7]
  9529. length = firstByte - 0x7f
  9530. // set 0x80 null to 0
  9531. if (firstByte === 0x80) {
  9532. data = new Buffer([])
  9533. } else {
  9534. data = input.slice(1, length)
  9535. }
  9536. if (length === 2 && data[0] < 0x80) {
  9537. throw new Error('invalid rlp encoding: byte must be less 0x80')
  9538. }
  9539. return {
  9540. data: data,
  9541. remainder: input.slice(length)
  9542. }
  9543. } else if (firstByte <= 0xbf) {
  9544. llength = firstByte - 0xb6
  9545. length = safeParseInt(input.slice(1, llength).toString('hex'), 16)
  9546. data = input.slice(llength, length + llength)
  9547. if (data.length < length) {
  9548. throw (new Error('invalid RLP'))
  9549. }
  9550. return {
  9551. data: data,
  9552. remainder: input.slice(length + llength)
  9553. }
  9554. } else if (firstByte <= 0xf7) {
  9555. // a list between 0-55 bytes long
  9556. length = firstByte - 0xbf
  9557. innerRemainder = input.slice(1, length)
  9558. while (innerRemainder.length) {
  9559. d = _decode(innerRemainder)
  9560. decoded.push(d.data)
  9561. innerRemainder = d.remainder
  9562. }
  9563. return {
  9564. data: decoded,
  9565. remainder: input.slice(length)
  9566. }
  9567. } else {
  9568. // a list over 55 bytes long
  9569. llength = firstByte - 0xf6
  9570. length = safeParseInt(input.slice(1, llength).toString('hex'), 16)
  9571. var totalLength = llength + length
  9572. if (totalLength > input.length) {
  9573. throw new Error('invalid rlp: total length is larger than the data')
  9574. }
  9575. innerRemainder = input.slice(llength, totalLength)
  9576. if (innerRemainder.length === 0) {
  9577. throw new Error('invalid rlp, List has a invalid length')
  9578. }
  9579. while (innerRemainder.length) {
  9580. d = _decode(innerRemainder)
  9581. decoded.push(d.data)
  9582. innerRemainder = d.remainder
  9583. }
  9584. return {
  9585. data: decoded,
  9586. remainder: input.slice(totalLength)
  9587. }
  9588. }
  9589. }
  9590. function isHexPrefixed (str) {
  9591. return str.slice(0, 2) === '0x'
  9592. }
  9593. // Removes 0x from a given String
  9594. function stripHexPrefix (str) {
  9595. if (typeof str !== 'string') {
  9596. return str
  9597. }
  9598. return isHexPrefixed(str) ? str.slice(2) : str
  9599. }
  9600. function intToHex (i) {
  9601. var hex = i.toString(16)
  9602. if (hex.length % 2) {
  9603. hex = '0' + hex
  9604. }
  9605. return hex
  9606. }
  9607. function padToEven (a) {
  9608. if (a.length % 2) a = '0' + a
  9609. return a
  9610. }
  9611. function intToBuffer (i) {
  9612. var hex = intToHex(i)
  9613. return new Buffer(hex, 'hex')
  9614. }
  9615. function toBuffer (v) {
  9616. if (!Buffer.isBuffer(v)) {
  9617. if (typeof v === 'string') {
  9618. if (isHexPrefixed(v)) {
  9619. v = new Buffer(padToEven(stripHexPrefix(v)), 'hex')
  9620. } else {
  9621. v = new Buffer(v)
  9622. }
  9623. } else if (typeof v === 'number') {
  9624. if (!v) {
  9625. v = new Buffer([])
  9626. } else {
  9627. v = intToBuffer(v)
  9628. }
  9629. } else if (v === null || v === undefined) {
  9630. v = new Buffer([])
  9631. } else if (v.toArray) {
  9632. // converts a BN to a Buffer
  9633. v = new Buffer(v.toArray())
  9634. } else {
  9635. throw new Error('invalid type')
  9636. }
  9637. }
  9638. return v
  9639. }
  9640. }).call(this,require("buffer").Buffer)
  9641. },{"assert":61,"buffer":65}],46:[function(require,module,exports){
  9642. 'use strict'
  9643. module.exports = require('./lib')(require('./lib/elliptic'))
  9644. },{"./lib":50,"./lib/elliptic":49}],47:[function(require,module,exports){
  9645. (function (Buffer){
  9646. 'use strict'
  9647. var toString = Object.prototype.toString
  9648. // TypeError
  9649. exports.isArray = function (value, message) {
  9650. if (!Array.isArray(value)) throw TypeError(message)
  9651. }
  9652. exports.isBoolean = function (value, message) {
  9653. if (toString.call(value) !== '[object Boolean]') throw TypeError(message)
  9654. }
  9655. exports.isBuffer = function (value, message) {
  9656. if (!Buffer.isBuffer(value)) throw TypeError(message)
  9657. }
  9658. exports.isFunction = function (value, message) {
  9659. if (toString.call(value) !== '[object Function]') throw TypeError(message)
  9660. }
  9661. exports.isNumber = function (value, message) {
  9662. if (toString.call(value) !== '[object Number]') throw TypeError(message)
  9663. }
  9664. exports.isObject = function (value, message) {
  9665. if (toString.call(value) !== '[object Object]') throw TypeError(message)
  9666. }
  9667. // RangeError
  9668. exports.isBufferLength = function (buffer, length, message) {
  9669. if (buffer.length !== length) throw RangeError(message)
  9670. }
  9671. exports.isBufferLength2 = function (buffer, length1, length2, message) {
  9672. if (buffer.length !== length1 && buffer.length !== length2) throw RangeError(message)
  9673. }
  9674. exports.isLengthGTZero = function (value, message) {
  9675. if (value.length === 0) throw RangeError(message)
  9676. }
  9677. exports.isNumberInInterval = function (number, x, y, message) {
  9678. if (number <= x || number >= y) throw RangeError(message)
  9679. }
  9680. }).call(this,{"isBuffer":require("../../../../../../../../../usr/lib/node_modules/browserify/node_modules/is-buffer/index.js")})
  9681. },{"../../../../../../../../../usr/lib/node_modules/browserify/node_modules/is-buffer/index.js":70}],48:[function(require,module,exports){
  9682. (function (Buffer){
  9683. 'use strict'
  9684. var bip66 = require('bip66')
  9685. var EC_PRIVKEY_EXPORT_DER_COMPRESSED = new Buffer([
  9686. // begin
  9687. 0x30, 0x81, 0xd3, 0x02, 0x01, 0x01, 0x04, 0x20,
  9688. // private key
  9689. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9690. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9691. // middle
  9692. 0xa0, 0x81, 0x85, 0x30, 0x81, 0x82, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48,
  9693. 0xcE, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9694. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9695. 0xff, 0xff, 0xfE, 0xff, 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04,
  9696. 0x21, 0x02, 0x79, 0xbE, 0x66, 0x7E, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xcE, 0x87,
  9697. 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xcE, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8,
  9698. 0x17, 0x98, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9699. 0xff, 0xff, 0xff, 0xff, 0xfE, 0xba, 0xaE, 0xdc, 0xE6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5E,
  9700. 0x8c, 0xd0, 0x36, 0x41, 0x41, 0x02, 0x01, 0x01, 0xa1, 0x24, 0x03, 0x22, 0x00,
  9701. // public key
  9702. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9703. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9704. 0x00
  9705. ])
  9706. var EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED = new Buffer([
  9707. // begin
  9708. 0x30, 0x82, 0x01, 0x13, 0x02, 0x01, 0x01, 0x04, 0x20,
  9709. // private key
  9710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9712. // middle
  9713. 0xa0, 0x81, 0xa5, 0x30, 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48,
  9714. 0xcE, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9715. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9716. 0xff, 0xff, 0xfE, 0xff, 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04,
  9717. 0x41, 0x04, 0x79, 0xbE, 0x66, 0x7E, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xcE, 0x87,
  9718. 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xcE, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8,
  9719. 0x17, 0x98, 0x48, 0x3a, 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb, 0xfc, 0x0E, 0x11,
  9720. 0x08, 0xa8, 0xfd, 0x17, 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10,
  9721. 0xd4, 0xb8, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9722. 0xff, 0xff, 0xff, 0xff, 0xfE, 0xba, 0xaE, 0xdc, 0xE6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5E,
  9723. 0x8c, 0xd0, 0x36, 0x41, 0x41, 0x02, 0x01, 0x01, 0xa1, 0x44, 0x03, 0x42, 0x00,
  9724. // public key
  9725. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9726. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9727. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9728. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9729. 0x00
  9730. ])
  9731. var ZERO_BUFFER_32 = new Buffer([
  9732. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9733. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  9734. ])
  9735. exports.privateKeyExport = function (privateKey, publicKey, compressed) {
  9736. var result = new Buffer(compressed ? EC_PRIVKEY_EXPORT_DER_COMPRESSED : EC_PRIVKEY_EXPORT_DER_UNCOMPRESSED)
  9737. privateKey.copy(result, compressed ? 8 : 9)
  9738. publicKey.copy(result, compressed ? 181 : 214)
  9739. return result
  9740. }
  9741. exports.privateKeyImport = function (privateKey) {
  9742. var length = privateKey.length
  9743. // sequence header
  9744. var index = 0
  9745. if (length < index + 1 || privateKey[index] !== 0x30) return
  9746. index += 1
  9747. // sequence length constructor
  9748. if (length < index + 1 || !(privateKey[index] & 0x80)) return
  9749. var lenb = privateKey[index] & 0x7f
  9750. index += 1
  9751. if (lenb < 1 || lenb > 2) return
  9752. if (length < index + lenb) return
  9753. // sequence length
  9754. var len = privateKey[index + lenb - 1] | (lenb > 1 ? privateKey[index + lenb - 2] << 8 : 0)
  9755. index += lenb
  9756. if (length < index + len) return
  9757. // sequence element 0: version number (=1)
  9758. if (length < index + 3 ||
  9759. privateKey[index] !== 0x02 ||
  9760. privateKey[index + 1] !== 0x01 ||
  9761. privateKey[index + 2] !== 0x01) {
  9762. return
  9763. }
  9764. index += 3
  9765. // sequence element 1: octet string, up to 32 bytes
  9766. if (length < index + 2 ||
  9767. privateKey[index] !== 0x04 ||
  9768. privateKey[index + 1] > 0x20 ||
  9769. length < index + 2 + privateKey[index + 1]) {
  9770. return
  9771. }
  9772. return privateKey.slice(index + 2, index + 2 + privateKey[index + 1])
  9773. }
  9774. exports.signatureExport = function (sigObj) {
  9775. var r = Buffer.concat([new Buffer([0]), sigObj.r])
  9776. for (var lenR = 33, posR = 0; lenR > 1 && r[posR] === 0x00 && !(r[posR + 1] & 0x80); --lenR, ++posR);
  9777. var s = Buffer.concat([new Buffer([0]), sigObj.s])
  9778. for (var lenS = 33, posS = 0; lenS > 1 && s[posS] === 0x00 && !(s[posS + 1] & 0x80); --lenS, ++posS);
  9779. return bip66.encode(r.slice(posR), s.slice(posS))
  9780. }
  9781. exports.signatureImport = function (sig) {
  9782. var r = new Buffer(ZERO_BUFFER_32)
  9783. var s = new Buffer(ZERO_BUFFER_32)
  9784. try {
  9785. var sigObj = bip66.decode(sig)
  9786. if (sigObj.r.length === 33 && sigObj.r[0] === 0x00) sigObj.r = sigObj.r.slice(1)
  9787. if (sigObj.r.length > 32) throw new Error('R length is too long')
  9788. if (sigObj.s.length === 33 && sigObj.s[0] === 0x00) sigObj.s = sigObj.s.slice(1)
  9789. if (sigObj.s.length > 32) throw new Error('S length is too long')
  9790. } catch (err) {
  9791. return
  9792. }
  9793. sigObj.r.copy(r, 32 - sigObj.r.length)
  9794. sigObj.s.copy(s, 32 - sigObj.s.length)
  9795. return { r: r, s: s }
  9796. }
  9797. exports.signatureImportLax = function (sig) {
  9798. var r = new Buffer(ZERO_BUFFER_32)
  9799. var s = new Buffer(ZERO_BUFFER_32)
  9800. var length = sig.length
  9801. var index = 0
  9802. // sequence tag byte
  9803. if (sig[index++] !== 0x30) return
  9804. // sequence length byte
  9805. var lenbyte = sig[index++]
  9806. if (lenbyte & 0x80) {
  9807. index += lenbyte - 0x80
  9808. if (index > length) return
  9809. }
  9810. // sequence tag byte for r
  9811. if (sig[index++] !== 0x02) return
  9812. // length for r
  9813. var rlen = sig[index++]
  9814. if (rlen & 0x80) {
  9815. lenbyte = rlen - 0x80
  9816. if (index + lenbyte > length) return
  9817. for (; lenbyte > 0 && sig[index] === 0x00; index += 1, lenbyte -= 1);
  9818. for (rlen = 0; lenbyte > 0; index += 1, lenbyte -= 1) rlen = (rlen << 8) + sig[index]
  9819. }
  9820. if (rlen > length - index) return
  9821. var rindex = index
  9822. index += rlen
  9823. // sequence tag byte for s
  9824. if (sig[index++] !== 0x02) return
  9825. // length for s
  9826. var slen = sig[index++]
  9827. if (slen & 0x80) {
  9828. lenbyte = slen - 0x80
  9829. if (index + lenbyte > length) return
  9830. for (; lenbyte > 0 && sig[index] === 0x00; index += 1, lenbyte -= 1);
  9831. for (slen = 0; lenbyte > 0; index += 1, lenbyte -= 1) slen = (slen << 8) + sig[index]
  9832. }
  9833. if (slen > length - index) return
  9834. var sindex = index
  9835. index += slen
  9836. // ignore leading zeros in r
  9837. for (; rlen > 0 && sig[rindex] === 0x00; rlen -= 1, rindex += 1);
  9838. // copy r value
  9839. if (rlen > 32) return
  9840. var rvalue = sig.slice(rindex, rindex + rlen)
  9841. rvalue.copy(r, 32 - rvalue.length)
  9842. // ignore leading zeros in s
  9843. for (; slen > 0 && sig[sindex] === 0x00; slen -= 1, sindex += 1);
  9844. // copy s value
  9845. if (slen > 32) return
  9846. var svalue = sig.slice(sindex, sindex + slen)
  9847. svalue.copy(s, 32 - svalue.length)
  9848. return { r: r, s: s }
  9849. }
  9850. }).call(this,require("buffer").Buffer)
  9851. },{"bip66":3,"buffer":65}],49:[function(require,module,exports){
  9852. (function (Buffer){
  9853. 'use strict'
  9854. var createHash = require('create-hash')
  9855. var BN = require('bn.js')
  9856. var EC = require('elliptic').ec
  9857. var messages = require('../messages.json')
  9858. var ec = new EC('secp256k1')
  9859. var ecparams = ec.curve
  9860. function loadCompressedPublicKey (first, xBuffer) {
  9861. var x = new BN(xBuffer)
  9862. // overflow
  9863. if (x.cmp(ecparams.p) >= 0) return null
  9864. x = x.toRed(ecparams.red)
  9865. // compute corresponding Y
  9866. var y = x.redSqr().redIMul(x).redIAdd(ecparams.b).redSqrt()
  9867. if ((first === 0x03) !== y.isOdd()) y = y.redNeg()
  9868. return ec.keyPair({ pub: { x: x, y: y } })
  9869. }
  9870. function loadUncompressedPublicKey (first, xBuffer, yBuffer) {
  9871. var x = new BN(xBuffer)
  9872. var y = new BN(yBuffer)
  9873. // overflow
  9874. if (x.cmp(ecparams.p) >= 0 || y.cmp(ecparams.p) >= 0) return null
  9875. x = x.toRed(ecparams.red)
  9876. y = y.toRed(ecparams.red)
  9877. // is odd flag
  9878. if ((first === 0x06 || first === 0x07) && y.isOdd() !== (first === 0x07)) return null
  9879. // x*x*x + b = y*y
  9880. var x3 = x.redSqr().redIMul(x)
  9881. if (!y.redSqr().redISub(x3.redIAdd(ecparams.b)).isZero()) return null
  9882. return ec.keyPair({ pub: { x: x, y: y } })
  9883. }
  9884. function loadPublicKey (publicKey) {
  9885. var first = publicKey[0]
  9886. switch (first) {
  9887. case 0x02:
  9888. case 0x03:
  9889. if (publicKey.length !== 33) return null
  9890. return loadCompressedPublicKey(first, publicKey.slice(1, 33))
  9891. case 0x04:
  9892. case 0x06:
  9893. case 0x07:
  9894. if (publicKey.length !== 65) return null
  9895. return loadUncompressedPublicKey(first, publicKey.slice(1, 33), publicKey.slice(33, 65))
  9896. default:
  9897. return null
  9898. }
  9899. }
  9900. exports.privateKeyVerify = function (privateKey) {
  9901. var bn = new BN(privateKey)
  9902. return bn.cmp(ecparams.n) < 0 && !bn.isZero()
  9903. }
  9904. exports.privateKeyExport = function (privateKey, compressed) {
  9905. var d = new BN(privateKey)
  9906. if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.EC_PRIVATE_KEY_EXPORT_DER_FAIL)
  9907. return new Buffer(ec.keyFromPrivate(privateKey).getPublic(compressed, true))
  9908. }
  9909. exports.privateKeyTweakAdd = function (privateKey, tweak) {
  9910. var bn = new BN(tweak)
  9911. if (bn.cmp(ecparams.n) >= 0) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
  9912. bn.iadd(new BN(privateKey))
  9913. if (bn.cmp(ecparams.n) >= 0) bn.isub(ecparams.n)
  9914. if (bn.isZero()) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_ADD_FAIL)
  9915. return bn.toArrayLike(Buffer, 'be', 32)
  9916. }
  9917. exports.privateKeyTweakMul = function (privateKey, tweak) {
  9918. var bn = new BN(tweak)
  9919. if (bn.cmp(ecparams.n) >= 0 || bn.isZero()) throw new Error(messages.EC_PRIVATE_KEY_TWEAK_MUL_FAIL)
  9920. bn.imul(new BN(privateKey))
  9921. if (bn.cmp(ecparams.n)) bn = bn.umod(ecparams.n)
  9922. return bn.toArrayLike(Buffer, 'be', 32)
  9923. }
  9924. exports.publicKeyCreate = function (privateKey, compressed) {
  9925. var d = new BN(privateKey)
  9926. if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.EC_PUBLIC_KEY_CREATE_FAIL)
  9927. return new Buffer(ec.keyFromPrivate(privateKey).getPublic(compressed, true))
  9928. }
  9929. exports.publicKeyConvert = function (publicKey, compressed) {
  9930. var pair = loadPublicKey(publicKey)
  9931. if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
  9932. return new Buffer(pair.getPublic(compressed, true))
  9933. }
  9934. exports.publicKeyVerify = function (publicKey) {
  9935. return loadPublicKey(publicKey) !== null
  9936. }
  9937. exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) {
  9938. var pair = loadPublicKey(publicKey)
  9939. if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
  9940. tweak = new BN(tweak)
  9941. if (tweak.cmp(ecparams.n) >= 0) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)
  9942. return new Buffer(ecparams.g.mul(tweak).add(pair.pub).encode(true, compressed))
  9943. }
  9944. exports.publicKeyTweakMul = function (publicKey, tweak, compressed) {
  9945. var pair = loadPublicKey(publicKey)
  9946. if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
  9947. tweak = new BN(tweak)
  9948. if (tweak.cmp(ecparams.n) >= 0 || tweak.isZero()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_MUL_FAIL)
  9949. return new Buffer(pair.pub.mul(tweak).encode(true, compressed))
  9950. }
  9951. exports.publicKeyCombine = function (publicKeys, compressed) {
  9952. var pairs = new Array(publicKeys.length)
  9953. for (var i = 0; i < publicKeys.length; ++i) {
  9954. pairs[i] = loadPublicKey(publicKeys[i])
  9955. if (pairs[i] === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
  9956. }
  9957. var point = pairs[0].pub
  9958. for (var j = 1; j < pairs.length; ++j) point = point.add(pairs[j].pub)
  9959. if (point.isInfinity()) throw new Error(messages.EC_PUBLIC_KEY_COMBINE_FAIL)
  9960. return new Buffer(point.encode(true, compressed))
  9961. }
  9962. exports.signatureNormalize = function (signature) {
  9963. var r = new BN(signature.slice(0, 32))
  9964. var s = new BN(signature.slice(32, 64))
  9965. if (r.cmp(ecparams.n) >= 0 || s.cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
  9966. var result = new Buffer(signature)
  9967. if (s.cmp(ec.nh) === 1) ecparams.n.sub(s).toArrayLike(Buffer, 'be', 32).copy(result, 32)
  9968. return result
  9969. }
  9970. exports.signatureExport = function (signature) {
  9971. var r = signature.slice(0, 32)
  9972. var s = signature.slice(32, 64)
  9973. if (new BN(r).cmp(ecparams.n) >= 0 || new BN(s).cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
  9974. return { r: r, s: s }
  9975. }
  9976. exports.signatureImport = function (sigObj) {
  9977. var r = new BN(sigObj.r)
  9978. if (r.cmp(ecparams.n) >= 0) r = new BN(0)
  9979. var s = new BN(sigObj.s)
  9980. if (s.cmp(ecparams.n) >= 0) s = new BN(0)
  9981. return Buffer.concat([
  9982. r.toArrayLike(Buffer, 'be', 32),
  9983. s.toArrayLike(Buffer, 'be', 32)
  9984. ])
  9985. }
  9986. exports.sign = function (message, privateKey, noncefn, data) {
  9987. if (typeof noncefn === 'function') {
  9988. var getNonce = noncefn
  9989. noncefn = function (counter) {
  9990. var nonce = getNonce(message, privateKey, null, data, counter)
  9991. if (!Buffer.isBuffer(nonce) || nonce.length !== 32) throw new Error(messages.ECDSA_SIGN_FAIL)
  9992. return new BN(nonce)
  9993. }
  9994. }
  9995. var d = new BN(privateKey)
  9996. if (d.cmp(ecparams.n) >= 0 || d.isZero()) throw new Error(messages.ECDSA_SIGN_FAIL)
  9997. var result = ec.sign(message, privateKey, { canonical: true, k: noncefn, pers: data })
  9998. return {
  9999. signature: Buffer.concat([
  10000. result.r.toArrayLike(Buffer, 'be', 32),
  10001. result.s.toArrayLike(Buffer, 'be', 32)
  10002. ]),
  10003. recovery: result.recoveryParam
  10004. }
  10005. }
  10006. exports.verify = function (message, signature, publicKey) {
  10007. var sigObj = {r: signature.slice(0, 32), s: signature.slice(32, 64)}
  10008. var sigr = new BN(sigObj.r)
  10009. var sigs = new BN(sigObj.s)
  10010. if (sigr.cmp(ecparams.n) >= 0 || sigs.cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
  10011. if (sigs.cmp(ec.nh) === 1 || sigr.isZero() || sigs.isZero()) return false
  10012. var pair = loadPublicKey(publicKey)
  10013. if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
  10014. return ec.verify(message, sigObj, {x: pair.pub.x, y: pair.pub.y})
  10015. }
  10016. exports.recover = function (message, signature, recovery, compressed) {
  10017. var sigObj = {r: signature.slice(0, 32), s: signature.slice(32, 64)}
  10018. var sigr = new BN(sigObj.r)
  10019. var sigs = new BN(sigObj.s)
  10020. if (sigr.cmp(ecparams.n) >= 0 || sigs.cmp(ecparams.n) >= 0) throw new Error(messages.ECDSA_SIGNATURE_PARSE_FAIL)
  10021. try {
  10022. if (sigr.isZero() || sigs.isZero()) throw new Error()
  10023. var point = ec.recoverPubKey(message, sigObj, recovery)
  10024. return new Buffer(point.encode(true, compressed))
  10025. } catch (err) {
  10026. throw new Error(messages.ECDSA_RECOVER_FAIL)
  10027. }
  10028. }
  10029. exports.ecdh = function (publicKey, privateKey) {
  10030. var shared = exports.ecdhUnsafe(publicKey, privateKey, true)
  10031. return createHash('sha256').update(shared).digest()
  10032. }
  10033. exports.ecdhUnsafe = function (publicKey, privateKey, compressed) {
  10034. var pair = loadPublicKey(publicKey)
  10035. if (pair === null) throw new Error(messages.EC_PUBLIC_KEY_PARSE_FAIL)
  10036. var scalar = new BN(privateKey)
  10037. if (scalar.cmp(ecparams.n) >= 0 || scalar.isZero()) throw new Error(messages.ECDH_FAIL)
  10038. return new Buffer(pair.pub.mul(scalar).encode(true, compressed))
  10039. }
  10040. }).call(this,require("buffer").Buffer)
  10041. },{"../messages.json":51,"bn.js":4,"buffer":65,"create-hash":7,"elliptic":10}],50:[function(require,module,exports){
  10042. 'use strict'
  10043. var assert = require('./assert')
  10044. var der = require('./der')
  10045. var messages = require('./messages.json')
  10046. function initCompressedValue (value, defaultValue) {
  10047. if (value === undefined) return defaultValue
  10048. assert.isBoolean(value, messages.COMPRESSED_TYPE_INVALID)
  10049. return value
  10050. }
  10051. module.exports = function (secp256k1) {
  10052. return {
  10053. privateKeyVerify: function (privateKey) {
  10054. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10055. return privateKey.length === 32 && secp256k1.privateKeyVerify(privateKey)
  10056. },
  10057. privateKeyExport: function (privateKey, compressed) {
  10058. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10059. assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
  10060. compressed = initCompressedValue(compressed, true)
  10061. var publicKey = secp256k1.privateKeyExport(privateKey, compressed)
  10062. return der.privateKeyExport(privateKey, publicKey, compressed)
  10063. },
  10064. privateKeyImport: function (privateKey) {
  10065. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10066. privateKey = der.privateKeyImport(privateKey)
  10067. if (privateKey && privateKey.length === 32 && secp256k1.privateKeyVerify(privateKey)) return privateKey
  10068. throw new Error(messages.EC_PRIVATE_KEY_IMPORT_DER_FAIL)
  10069. },
  10070. privateKeyTweakAdd: function (privateKey, tweak) {
  10071. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10072. assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
  10073. assert.isBuffer(tweak, messages.TWEAK_TYPE_INVALID)
  10074. assert.isBufferLength(tweak, 32, messages.TWEAK_LENGTH_INVALID)
  10075. return secp256k1.privateKeyTweakAdd(privateKey, tweak)
  10076. },
  10077. privateKeyTweakMul: function (privateKey, tweak) {
  10078. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10079. assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
  10080. assert.isBuffer(tweak, messages.TWEAK_TYPE_INVALID)
  10081. assert.isBufferLength(tweak, 32, messages.TWEAK_LENGTH_INVALID)
  10082. return secp256k1.privateKeyTweakMul(privateKey, tweak)
  10083. },
  10084. publicKeyCreate: function (privateKey, compressed) {
  10085. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10086. assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
  10087. compressed = initCompressedValue(compressed, true)
  10088. return secp256k1.publicKeyCreate(privateKey, compressed)
  10089. },
  10090. publicKeyConvert: function (publicKey, compressed) {
  10091. assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10092. assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
  10093. compressed = initCompressedValue(compressed, true)
  10094. return secp256k1.publicKeyConvert(publicKey, compressed)
  10095. },
  10096. publicKeyVerify: function (publicKey) {
  10097. assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10098. return secp256k1.publicKeyVerify(publicKey)
  10099. },
  10100. publicKeyTweakAdd: function (publicKey, tweak, compressed) {
  10101. assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10102. assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
  10103. assert.isBuffer(tweak, messages.TWEAK_TYPE_INVALID)
  10104. assert.isBufferLength(tweak, 32, messages.TWEAK_LENGTH_INVALID)
  10105. compressed = initCompressedValue(compressed, true)
  10106. return secp256k1.publicKeyTweakAdd(publicKey, tweak, compressed)
  10107. },
  10108. publicKeyTweakMul: function (publicKey, tweak, compressed) {
  10109. assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10110. assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
  10111. assert.isBuffer(tweak, messages.TWEAK_TYPE_INVALID)
  10112. assert.isBufferLength(tweak, 32, messages.TWEAK_LENGTH_INVALID)
  10113. compressed = initCompressedValue(compressed, true)
  10114. return secp256k1.publicKeyTweakMul(publicKey, tweak, compressed)
  10115. },
  10116. publicKeyCombine: function (publicKeys, compressed) {
  10117. assert.isArray(publicKeys, messages.EC_PUBLIC_KEYS_TYPE_INVALID)
  10118. assert.isLengthGTZero(publicKeys, messages.EC_PUBLIC_KEYS_LENGTH_INVALID)
  10119. for (var i = 0; i < publicKeys.length; ++i) {
  10120. assert.isBuffer(publicKeys[i], messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10121. assert.isBufferLength2(publicKeys[i], 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
  10122. }
  10123. compressed = initCompressedValue(compressed, true)
  10124. return secp256k1.publicKeyCombine(publicKeys, compressed)
  10125. },
  10126. signatureNormalize: function (signature) {
  10127. assert.isBuffer(signature, messages.ECDSA_SIGNATURE_TYPE_INVALID)
  10128. assert.isBufferLength(signature, 64, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
  10129. return secp256k1.signatureNormalize(signature)
  10130. },
  10131. signatureExport: function (signature) {
  10132. assert.isBuffer(signature, messages.ECDSA_SIGNATURE_TYPE_INVALID)
  10133. assert.isBufferLength(signature, 64, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
  10134. var sigObj = secp256k1.signatureExport(signature)
  10135. return der.signatureExport(sigObj)
  10136. },
  10137. signatureImport: function (sig) {
  10138. assert.isBuffer(sig, messages.ECDSA_SIGNATURE_TYPE_INVALID)
  10139. assert.isLengthGTZero(sig, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
  10140. var sigObj = der.signatureImport(sig)
  10141. if (sigObj) return secp256k1.signatureImport(sigObj)
  10142. throw new Error(messages.ECDSA_SIGNATURE_PARSE_DER_FAIL)
  10143. },
  10144. signatureImportLax: function (sig) {
  10145. assert.isBuffer(sig, messages.ECDSA_SIGNATURE_TYPE_INVALID)
  10146. assert.isLengthGTZero(sig, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
  10147. var sigObj = der.signatureImportLax(sig)
  10148. if (sigObj) return secp256k1.signatureImport(sigObj)
  10149. throw new Error(messages.ECDSA_SIGNATURE_PARSE_DER_FAIL)
  10150. },
  10151. sign: function (message, privateKey, options) {
  10152. assert.isBuffer(message, messages.MSG32_TYPE_INVALID)
  10153. assert.isBufferLength(message, 32, messages.MSG32_LENGTH_INVALID)
  10154. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10155. assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
  10156. var data = null
  10157. var noncefn = null
  10158. if (options !== undefined) {
  10159. assert.isObject(options, messages.OPTIONS_TYPE_INVALID)
  10160. if (options.data !== undefined) {
  10161. assert.isBuffer(options.data, messages.OPTIONS_DATA_TYPE_INVALID)
  10162. assert.isBufferLength(options.data, 32, messages.OPTIONS_DATA_LENGTH_INVALID)
  10163. data = options.data
  10164. }
  10165. if (options.noncefn !== undefined) {
  10166. assert.isFunction(options.noncefn, messages.OPTIONS_NONCEFN_TYPE_INVALID)
  10167. noncefn = options.noncefn
  10168. }
  10169. }
  10170. return secp256k1.sign(message, privateKey, noncefn, data)
  10171. },
  10172. verify: function (message, signature, publicKey) {
  10173. assert.isBuffer(message, messages.MSG32_TYPE_INVALID)
  10174. assert.isBufferLength(message, 32, messages.MSG32_LENGTH_INVALID)
  10175. assert.isBuffer(signature, messages.ECDSA_SIGNATURE_TYPE_INVALID)
  10176. assert.isBufferLength(signature, 64, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
  10177. assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10178. assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
  10179. return secp256k1.verify(message, signature, publicKey)
  10180. },
  10181. recover: function (message, signature, recovery, compressed) {
  10182. assert.isBuffer(message, messages.MSG32_TYPE_INVALID)
  10183. assert.isBufferLength(message, 32, messages.MSG32_LENGTH_INVALID)
  10184. assert.isBuffer(signature, messages.ECDSA_SIGNATURE_TYPE_INVALID)
  10185. assert.isBufferLength(signature, 64, messages.ECDSA_SIGNATURE_LENGTH_INVALID)
  10186. assert.isNumber(recovery, messages.RECOVERY_ID_TYPE_INVALID)
  10187. assert.isNumberInInterval(recovery, -1, 4, messages.RECOVERY_ID_VALUE_INVALID)
  10188. compressed = initCompressedValue(compressed, true)
  10189. return secp256k1.recover(message, signature, recovery, compressed)
  10190. },
  10191. ecdh: function (publicKey, privateKey) {
  10192. assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10193. assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
  10194. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10195. assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
  10196. return secp256k1.ecdh(publicKey, privateKey)
  10197. },
  10198. ecdhUnsafe: function (publicKey, privateKey, compressed) {
  10199. assert.isBuffer(publicKey, messages.EC_PUBLIC_KEY_TYPE_INVALID)
  10200. assert.isBufferLength2(publicKey, 33, 65, messages.EC_PUBLIC_KEY_LENGTH_INVALID)
  10201. assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  10202. assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID)
  10203. compressed = initCompressedValue(compressed, true)
  10204. return secp256k1.ecdhUnsafe(publicKey, privateKey, compressed)
  10205. }
  10206. }
  10207. }
  10208. },{"./assert":47,"./der":48,"./messages.json":51}],51:[function(require,module,exports){
  10209. module.exports={
  10210. "COMPRESSED_TYPE_INVALID": "compressed should be a boolean",
  10211. "EC_PRIVATE_KEY_TYPE_INVALID": "private key should be a Buffer",
  10212. "EC_PRIVATE_KEY_LENGTH_INVALID": "private key length is invalid",
  10213. "EC_PRIVATE_KEY_TWEAK_ADD_FAIL": "tweak out of range or resulting private key is invalid",
  10214. "EC_PRIVATE_KEY_TWEAK_MUL_FAIL": "tweak out of range",
  10215. "EC_PRIVATE_KEY_EXPORT_DER_FAIL": "couldn't export to DER format",
  10216. "EC_PRIVATE_KEY_IMPORT_DER_FAIL": "couldn't import from DER format",
  10217. "EC_PUBLIC_KEYS_TYPE_INVALID": "public keys should be an Array",
  10218. "EC_PUBLIC_KEYS_LENGTH_INVALID": "public keys Array should have at least 1 element",
  10219. "EC_PUBLIC_KEY_TYPE_INVALID": "public key should be a Buffer",
  10220. "EC_PUBLIC_KEY_LENGTH_INVALID": "public key length is invalid",
  10221. "EC_PUBLIC_KEY_PARSE_FAIL": "the public key could not be parsed or is invalid",
  10222. "EC_PUBLIC_KEY_CREATE_FAIL": "private was invalid, try again",
  10223. "EC_PUBLIC_KEY_TWEAK_ADD_FAIL": "tweak out of range or resulting public key is invalid",
  10224. "EC_PUBLIC_KEY_TWEAK_MUL_FAIL": "tweak out of range",
  10225. "EC_PUBLIC_KEY_COMBINE_FAIL": "the sum of the public keys is not valid",
  10226. "ECDH_FAIL": "scalar was invalid (zero or overflow)",
  10227. "ECDSA_SIGNATURE_TYPE_INVALID": "signature should be a Buffer",
  10228. "ECDSA_SIGNATURE_LENGTH_INVALID": "signature length is invalid",
  10229. "ECDSA_SIGNATURE_PARSE_FAIL": "couldn't parse signature",
  10230. "ECDSA_SIGNATURE_PARSE_DER_FAIL": "couldn't parse DER signature",
  10231. "ECDSA_SIGNATURE_SERIALIZE_DER_FAIL": "couldn't serialize signature to DER format",
  10232. "ECDSA_SIGN_FAIL": "nonce generation function failed or private key is invalid",
  10233. "ECDSA_RECOVER_FAIL": "couldn't recover public key from signature",
  10234. "MSG32_TYPE_INVALID": "message should be a Buffer",
  10235. "MSG32_LENGTH_INVALID": "message length is invalid",
  10236. "OPTIONS_TYPE_INVALID": "options should be an Object",
  10237. "OPTIONS_DATA_TYPE_INVALID": "options.data should be a Buffer",
  10238. "OPTIONS_DATA_LENGTH_INVALID": "options.data length is invalid",
  10239. "OPTIONS_NONCEFN_TYPE_INVALID": "options.noncefn should be a Function",
  10240. "RECOVERY_ID_TYPE_INVALID": "recovery should be a Number",
  10241. "RECOVERY_ID_VALUE_INVALID": "recovery should have value between -1 and 4",
  10242. "TWEAK_TYPE_INVALID": "tweak should be a Buffer",
  10243. "TWEAK_LENGTH_INVALID": "tweak length is invalid"
  10244. }
  10245. },{}],52:[function(require,module,exports){
  10246. (function (Buffer){
  10247. // prototype class for hash functions
  10248. function Hash (blockSize, finalSize) {
  10249. this._block = new Buffer(blockSize)
  10250. this._finalSize = finalSize
  10251. this._blockSize = blockSize
  10252. this._len = 0
  10253. this._s = 0
  10254. }
  10255. Hash.prototype.update = function (data, enc) {
  10256. if (typeof data === 'string') {
  10257. enc = enc || 'utf8'
  10258. data = new Buffer(data, enc)
  10259. }
  10260. var l = this._len += data.length
  10261. var s = this._s || 0
  10262. var f = 0
  10263. var buffer = this._block
  10264. while (s < l) {
  10265. var t = Math.min(data.length, f + this._blockSize - (s % this._blockSize))
  10266. var ch = (t - f)
  10267. for (var i = 0; i < ch; i++) {
  10268. buffer[(s % this._blockSize) + i] = data[i + f]
  10269. }
  10270. s += ch
  10271. f += ch
  10272. if ((s % this._blockSize) === 0) {
  10273. this._update(buffer)
  10274. }
  10275. }
  10276. this._s = s
  10277. return this
  10278. }
  10279. Hash.prototype.digest = function (enc) {
  10280. // Suppose the length of the message M, in bits, is l
  10281. var l = this._len * 8
  10282. // Append the bit 1 to the end of the message
  10283. this._block[this._len % this._blockSize] = 0x80
  10284. // and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize
  10285. this._block.fill(0, this._len % this._blockSize + 1)
  10286. if (l % (this._blockSize * 8) >= this._finalSize * 8) {
  10287. this._update(this._block)
  10288. this._block.fill(0)
  10289. }
  10290. // to this append the block which is equal to the number l written in binary
  10291. // TODO: handle case where l is > Math.pow(2, 29)
  10292. this._block.writeInt32BE(l, this._blockSize - 4)
  10293. var hash = this._update(this._block) || this._hash()
  10294. return enc ? hash.toString(enc) : hash
  10295. }
  10296. Hash.prototype._update = function () {
  10297. throw new Error('_update must be implemented by subclass')
  10298. }
  10299. module.exports = Hash
  10300. }).call(this,require("buffer").Buffer)
  10301. },{"buffer":65}],53:[function(require,module,exports){
  10302. var exports = module.exports = function SHA (algorithm) {
  10303. algorithm = algorithm.toLowerCase()
  10304. var Algorithm = exports[algorithm]
  10305. if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)')
  10306. return new Algorithm()
  10307. }
  10308. exports.sha = require('./sha')
  10309. exports.sha1 = require('./sha1')
  10310. exports.sha224 = require('./sha224')
  10311. exports.sha256 = require('./sha256')
  10312. exports.sha384 = require('./sha384')
  10313. exports.sha512 = require('./sha512')
  10314. },{"./sha":54,"./sha1":55,"./sha224":56,"./sha256":57,"./sha384":58,"./sha512":59}],54:[function(require,module,exports){
  10315. (function (Buffer){
  10316. /*
  10317. * A JavaScript implementation of the Secure Hash Algorithm, SHA-0, as defined
  10318. * in FIPS PUB 180-1
  10319. * This source code is derived from sha1.js of the same repository.
  10320. * The difference between SHA-0 and SHA-1 is just a bitwise rotate left
  10321. * operation was added.
  10322. */
  10323. var inherits = require('inherits')
  10324. var Hash = require('./hash')
  10325. var K = [
  10326. 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
  10327. ]
  10328. var W = new Array(80)
  10329. function Sha () {
  10330. this.init()
  10331. this._w = W
  10332. Hash.call(this, 64, 56)
  10333. }
  10334. inherits(Sha, Hash)
  10335. Sha.prototype.init = function () {
  10336. this._a = 0x67452301
  10337. this._b = 0xefcdab89
  10338. this._c = 0x98badcfe
  10339. this._d = 0x10325476
  10340. this._e = 0xc3d2e1f0
  10341. return this
  10342. }
  10343. function rotl5 (num) {
  10344. return (num << 5) | (num >>> 27)
  10345. }
  10346. function rotl30 (num) {
  10347. return (num << 30) | (num >>> 2)
  10348. }
  10349. function ft (s, b, c, d) {
  10350. if (s === 0) return (b & c) | ((~b) & d)
  10351. if (s === 2) return (b & c) | (b & d) | (c & d)
  10352. return b ^ c ^ d
  10353. }
  10354. Sha.prototype._update = function (M) {
  10355. var W = this._w
  10356. var a = this._a | 0
  10357. var b = this._b | 0
  10358. var c = this._c | 0
  10359. var d = this._d | 0
  10360. var e = this._e | 0
  10361. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  10362. for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]
  10363. for (var j = 0; j < 80; ++j) {
  10364. var s = ~~(j / 20)
  10365. var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
  10366. e = d
  10367. d = c
  10368. c = rotl30(b)
  10369. b = a
  10370. a = t
  10371. }
  10372. this._a = (a + this._a) | 0
  10373. this._b = (b + this._b) | 0
  10374. this._c = (c + this._c) | 0
  10375. this._d = (d + this._d) | 0
  10376. this._e = (e + this._e) | 0
  10377. }
  10378. Sha.prototype._hash = function () {
  10379. var H = new Buffer(20)
  10380. H.writeInt32BE(this._a | 0, 0)
  10381. H.writeInt32BE(this._b | 0, 4)
  10382. H.writeInt32BE(this._c | 0, 8)
  10383. H.writeInt32BE(this._d | 0, 12)
  10384. H.writeInt32BE(this._e | 0, 16)
  10385. return H
  10386. }
  10387. module.exports = Sha
  10388. }).call(this,require("buffer").Buffer)
  10389. },{"./hash":52,"buffer":65,"inherits":34}],55:[function(require,module,exports){
  10390. (function (Buffer){
  10391. /*
  10392. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  10393. * in FIPS PUB 180-1
  10394. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  10395. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  10396. * Distributed under the BSD License
  10397. * See http://pajhome.org.uk/crypt/md5 for details.
  10398. */
  10399. var inherits = require('inherits')
  10400. var Hash = require('./hash')
  10401. var K = [
  10402. 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
  10403. ]
  10404. var W = new Array(80)
  10405. function Sha1 () {
  10406. this.init()
  10407. this._w = W
  10408. Hash.call(this, 64, 56)
  10409. }
  10410. inherits(Sha1, Hash)
  10411. Sha1.prototype.init = function () {
  10412. this._a = 0x67452301
  10413. this._b = 0xefcdab89
  10414. this._c = 0x98badcfe
  10415. this._d = 0x10325476
  10416. this._e = 0xc3d2e1f0
  10417. return this
  10418. }
  10419. function rotl1 (num) {
  10420. return (num << 1) | (num >>> 31)
  10421. }
  10422. function rotl5 (num) {
  10423. return (num << 5) | (num >>> 27)
  10424. }
  10425. function rotl30 (num) {
  10426. return (num << 30) | (num >>> 2)
  10427. }
  10428. function ft (s, b, c, d) {
  10429. if (s === 0) return (b & c) | ((~b) & d)
  10430. if (s === 2) return (b & c) | (b & d) | (c & d)
  10431. return b ^ c ^ d
  10432. }
  10433. Sha1.prototype._update = function (M) {
  10434. var W = this._w
  10435. var a = this._a | 0
  10436. var b = this._b | 0
  10437. var c = this._c | 0
  10438. var d = this._d | 0
  10439. var e = this._e | 0
  10440. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  10441. for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16])
  10442. for (var j = 0; j < 80; ++j) {
  10443. var s = ~~(j / 20)
  10444. var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
  10445. e = d
  10446. d = c
  10447. c = rotl30(b)
  10448. b = a
  10449. a = t
  10450. }
  10451. this._a = (a + this._a) | 0
  10452. this._b = (b + this._b) | 0
  10453. this._c = (c + this._c) | 0
  10454. this._d = (d + this._d) | 0
  10455. this._e = (e + this._e) | 0
  10456. }
  10457. Sha1.prototype._hash = function () {
  10458. var H = new Buffer(20)
  10459. H.writeInt32BE(this._a | 0, 0)
  10460. H.writeInt32BE(this._b | 0, 4)
  10461. H.writeInt32BE(this._c | 0, 8)
  10462. H.writeInt32BE(this._d | 0, 12)
  10463. H.writeInt32BE(this._e | 0, 16)
  10464. return H
  10465. }
  10466. module.exports = Sha1
  10467. }).call(this,require("buffer").Buffer)
  10468. },{"./hash":52,"buffer":65,"inherits":34}],56:[function(require,module,exports){
  10469. (function (Buffer){
  10470. /**
  10471. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  10472. * in FIPS 180-2
  10473. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  10474. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  10475. *
  10476. */
  10477. var inherits = require('inherits')
  10478. var Sha256 = require('./sha256')
  10479. var Hash = require('./hash')
  10480. var W = new Array(64)
  10481. function Sha224 () {
  10482. this.init()
  10483. this._w = W // new Array(64)
  10484. Hash.call(this, 64, 56)
  10485. }
  10486. inherits(Sha224, Sha256)
  10487. Sha224.prototype.init = function () {
  10488. this._a = 0xc1059ed8
  10489. this._b = 0x367cd507
  10490. this._c = 0x3070dd17
  10491. this._d = 0xf70e5939
  10492. this._e = 0xffc00b31
  10493. this._f = 0x68581511
  10494. this._g = 0x64f98fa7
  10495. this._h = 0xbefa4fa4
  10496. return this
  10497. }
  10498. Sha224.prototype._hash = function () {
  10499. var H = new Buffer(28)
  10500. H.writeInt32BE(this._a, 0)
  10501. H.writeInt32BE(this._b, 4)
  10502. H.writeInt32BE(this._c, 8)
  10503. H.writeInt32BE(this._d, 12)
  10504. H.writeInt32BE(this._e, 16)
  10505. H.writeInt32BE(this._f, 20)
  10506. H.writeInt32BE(this._g, 24)
  10507. return H
  10508. }
  10509. module.exports = Sha224
  10510. }).call(this,require("buffer").Buffer)
  10511. },{"./hash":52,"./sha256":57,"buffer":65,"inherits":34}],57:[function(require,module,exports){
  10512. (function (Buffer){
  10513. /**
  10514. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  10515. * in FIPS 180-2
  10516. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  10517. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  10518. *
  10519. */
  10520. var inherits = require('inherits')
  10521. var Hash = require('./hash')
  10522. var K = [
  10523. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  10524. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  10525. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  10526. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  10527. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  10528. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  10529. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  10530. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  10531. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  10532. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  10533. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  10534. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  10535. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  10536. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  10537. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  10538. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
  10539. ]
  10540. var W = new Array(64)
  10541. function Sha256 () {
  10542. this.init()
  10543. this._w = W // new Array(64)
  10544. Hash.call(this, 64, 56)
  10545. }
  10546. inherits(Sha256, Hash)
  10547. Sha256.prototype.init = function () {
  10548. this._a = 0x6a09e667
  10549. this._b = 0xbb67ae85
  10550. this._c = 0x3c6ef372
  10551. this._d = 0xa54ff53a
  10552. this._e = 0x510e527f
  10553. this._f = 0x9b05688c
  10554. this._g = 0x1f83d9ab
  10555. this._h = 0x5be0cd19
  10556. return this
  10557. }
  10558. function ch (x, y, z) {
  10559. return z ^ (x & (y ^ z))
  10560. }
  10561. function maj (x, y, z) {
  10562. return (x & y) | (z & (x | y))
  10563. }
  10564. function sigma0 (x) {
  10565. return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
  10566. }
  10567. function sigma1 (x) {
  10568. return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
  10569. }
  10570. function gamma0 (x) {
  10571. return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
  10572. }
  10573. function gamma1 (x) {
  10574. return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
  10575. }
  10576. Sha256.prototype._update = function (M) {
  10577. var W = this._w
  10578. var a = this._a | 0
  10579. var b = this._b | 0
  10580. var c = this._c | 0
  10581. var d = this._d | 0
  10582. var e = this._e | 0
  10583. var f = this._f | 0
  10584. var g = this._g | 0
  10585. var h = this._h | 0
  10586. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  10587. for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0
  10588. for (var j = 0; j < 64; ++j) {
  10589. var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0
  10590. var T2 = (sigma0(a) + maj(a, b, c)) | 0
  10591. h = g
  10592. g = f
  10593. f = e
  10594. e = (d + T1) | 0
  10595. d = c
  10596. c = b
  10597. b = a
  10598. a = (T1 + T2) | 0
  10599. }
  10600. this._a = (a + this._a) | 0
  10601. this._b = (b + this._b) | 0
  10602. this._c = (c + this._c) | 0
  10603. this._d = (d + this._d) | 0
  10604. this._e = (e + this._e) | 0
  10605. this._f = (f + this._f) | 0
  10606. this._g = (g + this._g) | 0
  10607. this._h = (h + this._h) | 0
  10608. }
  10609. Sha256.prototype._hash = function () {
  10610. var H = new Buffer(32)
  10611. H.writeInt32BE(this._a, 0)
  10612. H.writeInt32BE(this._b, 4)
  10613. H.writeInt32BE(this._c, 8)
  10614. H.writeInt32BE(this._d, 12)
  10615. H.writeInt32BE(this._e, 16)
  10616. H.writeInt32BE(this._f, 20)
  10617. H.writeInt32BE(this._g, 24)
  10618. H.writeInt32BE(this._h, 28)
  10619. return H
  10620. }
  10621. module.exports = Sha256
  10622. }).call(this,require("buffer").Buffer)
  10623. },{"./hash":52,"buffer":65,"inherits":34}],58:[function(require,module,exports){
  10624. (function (Buffer){
  10625. var inherits = require('inherits')
  10626. var SHA512 = require('./sha512')
  10627. var Hash = require('./hash')
  10628. var W = new Array(160)
  10629. function Sha384 () {
  10630. this.init()
  10631. this._w = W
  10632. Hash.call(this, 128, 112)
  10633. }
  10634. inherits(Sha384, SHA512)
  10635. Sha384.prototype.init = function () {
  10636. this._ah = 0xcbbb9d5d
  10637. this._bh = 0x629a292a
  10638. this._ch = 0x9159015a
  10639. this._dh = 0x152fecd8
  10640. this._eh = 0x67332667
  10641. this._fh = 0x8eb44a87
  10642. this._gh = 0xdb0c2e0d
  10643. this._hh = 0x47b5481d
  10644. this._al = 0xc1059ed8
  10645. this._bl = 0x367cd507
  10646. this._cl = 0x3070dd17
  10647. this._dl = 0xf70e5939
  10648. this._el = 0xffc00b31
  10649. this._fl = 0x68581511
  10650. this._gl = 0x64f98fa7
  10651. this._hl = 0xbefa4fa4
  10652. return this
  10653. }
  10654. Sha384.prototype._hash = function () {
  10655. var H = new Buffer(48)
  10656. function writeInt64BE (h, l, offset) {
  10657. H.writeInt32BE(h, offset)
  10658. H.writeInt32BE(l, offset + 4)
  10659. }
  10660. writeInt64BE(this._ah, this._al, 0)
  10661. writeInt64BE(this._bh, this._bl, 8)
  10662. writeInt64BE(this._ch, this._cl, 16)
  10663. writeInt64BE(this._dh, this._dl, 24)
  10664. writeInt64BE(this._eh, this._el, 32)
  10665. writeInt64BE(this._fh, this._fl, 40)
  10666. return H
  10667. }
  10668. module.exports = Sha384
  10669. }).call(this,require("buffer").Buffer)
  10670. },{"./hash":52,"./sha512":59,"buffer":65,"inherits":34}],59:[function(require,module,exports){
  10671. (function (Buffer){
  10672. var inherits = require('inherits')
  10673. var Hash = require('./hash')
  10674. var K = [
  10675. 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
  10676. 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
  10677. 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
  10678. 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
  10679. 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
  10680. 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
  10681. 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
  10682. 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
  10683. 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
  10684. 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
  10685. 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
  10686. 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
  10687. 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
  10688. 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
  10689. 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
  10690. 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
  10691. 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
  10692. 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
  10693. 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
  10694. 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
  10695. 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
  10696. 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
  10697. 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
  10698. 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
  10699. 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
  10700. 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
  10701. 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
  10702. 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
  10703. 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
  10704. 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
  10705. 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
  10706. 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
  10707. 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
  10708. 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
  10709. 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
  10710. 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
  10711. 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
  10712. 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
  10713. 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
  10714. 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
  10715. ]
  10716. var W = new Array(160)
  10717. function Sha512 () {
  10718. this.init()
  10719. this._w = W
  10720. Hash.call(this, 128, 112)
  10721. }
  10722. inherits(Sha512, Hash)
  10723. Sha512.prototype.init = function () {
  10724. this._ah = 0x6a09e667
  10725. this._bh = 0xbb67ae85
  10726. this._ch = 0x3c6ef372
  10727. this._dh = 0xa54ff53a
  10728. this._eh = 0x510e527f
  10729. this._fh = 0x9b05688c
  10730. this._gh = 0x1f83d9ab
  10731. this._hh = 0x5be0cd19
  10732. this._al = 0xf3bcc908
  10733. this._bl = 0x84caa73b
  10734. this._cl = 0xfe94f82b
  10735. this._dl = 0x5f1d36f1
  10736. this._el = 0xade682d1
  10737. this._fl = 0x2b3e6c1f
  10738. this._gl = 0xfb41bd6b
  10739. this._hl = 0x137e2179
  10740. return this
  10741. }
  10742. function Ch (x, y, z) {
  10743. return z ^ (x & (y ^ z))
  10744. }
  10745. function maj (x, y, z) {
  10746. return (x & y) | (z & (x | y))
  10747. }
  10748. function sigma0 (x, xl) {
  10749. return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25)
  10750. }
  10751. function sigma1 (x, xl) {
  10752. return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23)
  10753. }
  10754. function Gamma0 (x, xl) {
  10755. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7)
  10756. }
  10757. function Gamma0l (x, xl) {
  10758. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25)
  10759. }
  10760. function Gamma1 (x, xl) {
  10761. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6)
  10762. }
  10763. function Gamma1l (x, xl) {
  10764. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26)
  10765. }
  10766. function getCarry (a, b) {
  10767. return (a >>> 0) < (b >>> 0) ? 1 : 0
  10768. }
  10769. Sha512.prototype._update = function (M) {
  10770. var W = this._w
  10771. var ah = this._ah | 0
  10772. var bh = this._bh | 0
  10773. var ch = this._ch | 0
  10774. var dh = this._dh | 0
  10775. var eh = this._eh | 0
  10776. var fh = this._fh | 0
  10777. var gh = this._gh | 0
  10778. var hh = this._hh | 0
  10779. var al = this._al | 0
  10780. var bl = this._bl | 0
  10781. var cl = this._cl | 0
  10782. var dl = this._dl | 0
  10783. var el = this._el | 0
  10784. var fl = this._fl | 0
  10785. var gl = this._gl | 0
  10786. var hl = this._hl | 0
  10787. for (var i = 0; i < 32; i += 2) {
  10788. W[i] = M.readInt32BE(i * 4)
  10789. W[i + 1] = M.readInt32BE(i * 4 + 4)
  10790. }
  10791. for (; i < 160; i += 2) {
  10792. var xh = W[i - 15 * 2]
  10793. var xl = W[i - 15 * 2 + 1]
  10794. var gamma0 = Gamma0(xh, xl)
  10795. var gamma0l = Gamma0l(xl, xh)
  10796. xh = W[i - 2 * 2]
  10797. xl = W[i - 2 * 2 + 1]
  10798. var gamma1 = Gamma1(xh, xl)
  10799. var gamma1l = Gamma1l(xl, xh)
  10800. // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
  10801. var Wi7h = W[i - 7 * 2]
  10802. var Wi7l = W[i - 7 * 2 + 1]
  10803. var Wi16h = W[i - 16 * 2]
  10804. var Wi16l = W[i - 16 * 2 + 1]
  10805. var Wil = (gamma0l + Wi7l) | 0
  10806. var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0
  10807. Wil = (Wil + gamma1l) | 0
  10808. Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0
  10809. Wil = (Wil + Wi16l) | 0
  10810. Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0
  10811. W[i] = Wih
  10812. W[i + 1] = Wil
  10813. }
  10814. for (var j = 0; j < 160; j += 2) {
  10815. Wih = W[j]
  10816. Wil = W[j + 1]
  10817. var majh = maj(ah, bh, ch)
  10818. var majl = maj(al, bl, cl)
  10819. var sigma0h = sigma0(ah, al)
  10820. var sigma0l = sigma0(al, ah)
  10821. var sigma1h = sigma1(eh, el)
  10822. var sigma1l = sigma1(el, eh)
  10823. // t1 = h + sigma1 + ch + K[j] + W[j]
  10824. var Kih = K[j]
  10825. var Kil = K[j + 1]
  10826. var chh = Ch(eh, fh, gh)
  10827. var chl = Ch(el, fl, gl)
  10828. var t1l = (hl + sigma1l) | 0
  10829. var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0
  10830. t1l = (t1l + chl) | 0
  10831. t1h = (t1h + chh + getCarry(t1l, chl)) | 0
  10832. t1l = (t1l + Kil) | 0
  10833. t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0
  10834. t1l = (t1l + Wil) | 0
  10835. t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0
  10836. // t2 = sigma0 + maj
  10837. var t2l = (sigma0l + majl) | 0
  10838. var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0
  10839. hh = gh
  10840. hl = gl
  10841. gh = fh
  10842. gl = fl
  10843. fh = eh
  10844. fl = el
  10845. el = (dl + t1l) | 0
  10846. eh = (dh + t1h + getCarry(el, dl)) | 0
  10847. dh = ch
  10848. dl = cl
  10849. ch = bh
  10850. cl = bl
  10851. bh = ah
  10852. bl = al
  10853. al = (t1l + t2l) | 0
  10854. ah = (t1h + t2h + getCarry(al, t1l)) | 0
  10855. }
  10856. this._al = (this._al + al) | 0
  10857. this._bl = (this._bl + bl) | 0
  10858. this._cl = (this._cl + cl) | 0
  10859. this._dl = (this._dl + dl) | 0
  10860. this._el = (this._el + el) | 0
  10861. this._fl = (this._fl + fl) | 0
  10862. this._gl = (this._gl + gl) | 0
  10863. this._hl = (this._hl + hl) | 0
  10864. this._ah = (this._ah + ah + getCarry(this._al, al)) | 0
  10865. this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0
  10866. this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0
  10867. this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0
  10868. this._eh = (this._eh + eh + getCarry(this._el, el)) | 0
  10869. this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0
  10870. this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0
  10871. this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0
  10872. }
  10873. Sha512.prototype._hash = function () {
  10874. var H = new Buffer(64)
  10875. function writeInt64BE (h, l, offset) {
  10876. H.writeInt32BE(h, offset)
  10877. H.writeInt32BE(l, offset + 4)
  10878. }
  10879. writeInt64BE(this._ah, this._al, 0)
  10880. writeInt64BE(this._bh, this._bl, 8)
  10881. writeInt64BE(this._ch, this._cl, 16)
  10882. writeInt64BE(this._dh, this._dl, 24)
  10883. writeInt64BE(this._eh, this._el, 32)
  10884. writeInt64BE(this._fh, this._fl, 40)
  10885. writeInt64BE(this._gh, this._gl, 48)
  10886. writeInt64BE(this._hh, this._hl, 56)
  10887. return H
  10888. }
  10889. module.exports = Sha512
  10890. }).call(this,require("buffer").Buffer)
  10891. },{"./hash":52,"buffer":65,"inherits":34}],60:[function(require,module,exports){
  10892. var isHexPrefixed = require('is-hex-prefixed');
  10893. /**
  10894. * Removes '0x' from a given `String` is present
  10895. * @param {String} str the string value
  10896. * @return {String|Optional} a string by pass if necessary
  10897. */
  10898. module.exports = function stripHexPrefix(str) {
  10899. if (typeof str !== 'string') {
  10900. return str;
  10901. }
  10902. return isHexPrefixed(str) ? str.slice(2) : str;
  10903. }
  10904. },{"is-hex-prefixed":35}],61:[function(require,module,exports){
  10905. (function (global){
  10906. 'use strict';
  10907. // compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
  10908. // original notice:
  10909. /*!
  10910. * The buffer module from node.js, for the browser.
  10911. *
  10912. * @author Feross Aboukhadijeh <[email protected]> <http://feross.org>
  10913. * @license MIT
  10914. */
  10915. function compare(a, b) {
  10916. if (a === b) {
  10917. return 0;
  10918. }
  10919. var x = a.length;
  10920. var y = b.length;
  10921. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  10922. if (a[i] !== b[i]) {
  10923. x = a[i];
  10924. y = b[i];
  10925. break;
  10926. }
  10927. }
  10928. if (x < y) {
  10929. return -1;
  10930. }
  10931. if (y < x) {
  10932. return 1;
  10933. }
  10934. return 0;
  10935. }
  10936. function isBuffer(b) {
  10937. if (global.Buffer && typeof global.Buffer.isBuffer === 'function') {
  10938. return global.Buffer.isBuffer(b);
  10939. }
  10940. return !!(b != null && b._isBuffer);
  10941. }
  10942. // based on node assert, original notice:
  10943. // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
  10944. //
  10945. // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
  10946. //
  10947. // Originally from narwhal.js (http://narwhaljs.org)
  10948. // Copyright (c) 2009 Thomas Robinson <280north.com>
  10949. //
  10950. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10951. // of this software and associated documentation files (the 'Software'), to
  10952. // deal in the Software without restriction, including without limitation the
  10953. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10954. // sell copies of the Software, and to permit persons to whom the Software is
  10955. // furnished to do so, subject to the following conditions:
  10956. //
  10957. // The above copyright notice and this permission notice shall be included in
  10958. // all copies or substantial portions of the Software.
  10959. //
  10960. // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  10961. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10962. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10963. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  10964. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  10965. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10966. var util = require('util/');
  10967. var hasOwn = Object.prototype.hasOwnProperty;
  10968. var pSlice = Array.prototype.slice;
  10969. var functionsHaveNames = (function () {
  10970. return function foo() {}.name === 'foo';
  10971. }());
  10972. function pToString (obj) {
  10973. return Object.prototype.toString.call(obj);
  10974. }
  10975. function isView(arrbuf) {
  10976. if (isBuffer(arrbuf)) {
  10977. return false;
  10978. }
  10979. if (typeof global.ArrayBuffer !== 'function') {
  10980. return false;
  10981. }
  10982. if (typeof ArrayBuffer.isView === 'function') {
  10983. return ArrayBuffer.isView(arrbuf);
  10984. }
  10985. if (!arrbuf) {
  10986. return false;
  10987. }
  10988. if (arrbuf instanceof DataView) {
  10989. return true;
  10990. }
  10991. if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) {
  10992. return true;
  10993. }
  10994. return false;
  10995. }
  10996. // 1. The assert module provides functions that throw
  10997. // AssertionError's when particular conditions are not met. The
  10998. // assert module must conform to the following interface.
  10999. var assert = module.exports = ok;
  11000. // 2. The AssertionError is defined in assert.
  11001. // new assert.AssertionError({ message: message,
  11002. // actual: actual,
  11003. // expected: expected })
  11004. var regex = /\s*function\s+([^\(\s]*)\s*/;
  11005. // based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js
  11006. function getName(func) {
  11007. if (!util.isFunction(func)) {
  11008. return;
  11009. }
  11010. if (functionsHaveNames) {
  11011. return func.name;
  11012. }
  11013. var str = func.toString();
  11014. var match = str.match(regex);
  11015. return match && match[1];
  11016. }
  11017. assert.AssertionError = function AssertionError(options) {
  11018. this.name = 'AssertionError';
  11019. this.actual = options.actual;
  11020. this.expected = options.expected;
  11021. this.operator = options.operator;
  11022. if (options.message) {
  11023. this.message = options.message;
  11024. this.generatedMessage = false;
  11025. } else {
  11026. this.message = getMessage(this);
  11027. this.generatedMessage = true;
  11028. }
  11029. var stackStartFunction = options.stackStartFunction || fail;
  11030. if (Error.captureStackTrace) {
  11031. Error.captureStackTrace(this, stackStartFunction);
  11032. } else {
  11033. // non v8 browsers so we can have a stacktrace
  11034. var err = new Error();
  11035. if (err.stack) {
  11036. var out = err.stack;
  11037. // try to strip useless frames
  11038. var fn_name = getName(stackStartFunction);
  11039. var idx = out.indexOf('\n' + fn_name);
  11040. if (idx >= 0) {
  11041. // once we have located the function frame
  11042. // we need to strip out everything before it (and its line)
  11043. var next_line = out.indexOf('\n', idx + 1);
  11044. out = out.substring(next_line + 1);
  11045. }
  11046. this.stack = out;
  11047. }
  11048. }
  11049. };
  11050. // assert.AssertionError instanceof Error
  11051. util.inherits(assert.AssertionError, Error);
  11052. function truncate(s, n) {
  11053. if (typeof s === 'string') {
  11054. return s.length < n ? s : s.slice(0, n);
  11055. } else {
  11056. return s;
  11057. }
  11058. }
  11059. function inspect(something) {
  11060. if (functionsHaveNames || !util.isFunction(something)) {
  11061. return util.inspect(something);
  11062. }
  11063. var rawname = getName(something);
  11064. var name = rawname ? ': ' + rawname : '';
  11065. return '[Function' + name + ']';
  11066. }
  11067. function getMessage(self) {
  11068. return truncate(inspect(self.actual), 128) + ' ' +
  11069. self.operator + ' ' +
  11070. truncate(inspect(self.expected), 128);
  11071. }
  11072. // At present only the three keys mentioned above are used and
  11073. // understood by the spec. Implementations or sub modules can pass
  11074. // other keys to the AssertionError's constructor - they will be
  11075. // ignored.
  11076. // 3. All of the following functions must throw an AssertionError
  11077. // when a corresponding condition is not met, with a message that
  11078. // may be undefined if not provided. All assertion methods provide
  11079. // both the actual and expected values to the assertion error for
  11080. // display purposes.
  11081. function fail(actual, expected, message, operator, stackStartFunction) {
  11082. throw new assert.AssertionError({
  11083. message: message,
  11084. actual: actual,
  11085. expected: expected,
  11086. operator: operator,
  11087. stackStartFunction: stackStartFunction
  11088. });
  11089. }
  11090. // EXTENSION! allows for well behaved errors defined elsewhere.
  11091. assert.fail = fail;
  11092. // 4. Pure assertion tests whether a value is truthy, as determined
  11093. // by !!guard.
  11094. // assert.ok(guard, message_opt);
  11095. // This statement is equivalent to assert.equal(true, !!guard,
  11096. // message_opt);. To test strictly for the value true, use
  11097. // assert.strictEqual(true, guard, message_opt);.
  11098. function ok(value, message) {
  11099. if (!value) fail(value, true, message, '==', assert.ok);
  11100. }
  11101. assert.ok = ok;
  11102. // 5. The equality assertion tests shallow, coercive equality with
  11103. // ==.
  11104. // assert.equal(actual, expected, message_opt);
  11105. assert.equal = function equal(actual, expected, message) {
  11106. if (actual != expected) fail(actual, expected, message, '==', assert.equal);
  11107. };
  11108. // 6. The non-equality assertion tests for whether two objects are not equal
  11109. // with != assert.notEqual(actual, expected, message_opt);
  11110. assert.notEqual = function notEqual(actual, expected, message) {
  11111. if (actual == expected) {
  11112. fail(actual, expected, message, '!=', assert.notEqual);
  11113. }
  11114. };
  11115. // 7. The equivalence assertion tests a deep equality relation.
  11116. // assert.deepEqual(actual, expected, message_opt);
  11117. assert.deepEqual = function deepEqual(actual, expected, message) {
  11118. if (!_deepEqual(actual, expected, false)) {
  11119. fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  11120. }
  11121. };
  11122. assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
  11123. if (!_deepEqual(actual, expected, true)) {
  11124. fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual);
  11125. }
  11126. };
  11127. function _deepEqual(actual, expected, strict, memos) {
  11128. // 7.1. All identical values are equivalent, as determined by ===.
  11129. if (actual === expected) {
  11130. return true;
  11131. } else if (isBuffer(actual) && isBuffer(expected)) {
  11132. return compare(actual, expected) === 0;
  11133. // 7.2. If the expected value is a Date object, the actual value is
  11134. // equivalent if it is also a Date object that refers to the same time.
  11135. } else if (util.isDate(actual) && util.isDate(expected)) {
  11136. return actual.getTime() === expected.getTime();
  11137. // 7.3 If the expected value is a RegExp object, the actual value is
  11138. // equivalent if it is also a RegExp object with the same source and
  11139. // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
  11140. } else if (util.isRegExp(actual) && util.isRegExp(expected)) {
  11141. return actual.source === expected.source &&
  11142. actual.global === expected.global &&
  11143. actual.multiline === expected.multiline &&
  11144. actual.lastIndex === expected.lastIndex &&
  11145. actual.ignoreCase === expected.ignoreCase;
  11146. // 7.4. Other pairs that do not both pass typeof value == 'object',
  11147. // equivalence is determined by ==.
  11148. } else if ((actual === null || typeof actual !== 'object') &&
  11149. (expected === null || typeof expected !== 'object')) {
  11150. return strict ? actual === expected : actual == expected;
  11151. // If both values are instances of typed arrays, wrap their underlying
  11152. // ArrayBuffers in a Buffer each to increase performance
  11153. // This optimization requires the arrays to have the same type as checked by
  11154. // Object.prototype.toString (aka pToString). Never perform binary
  11155. // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their
  11156. // bit patterns are not identical.
  11157. } else if (isView(actual) && isView(expected) &&
  11158. pToString(actual) === pToString(expected) &&
  11159. !(actual instanceof Float32Array ||
  11160. actual instanceof Float64Array)) {
  11161. return compare(new Uint8Array(actual.buffer),
  11162. new Uint8Array(expected.buffer)) === 0;
  11163. // 7.5 For all other Object pairs, including Array objects, equivalence is
  11164. // determined by having the same number of owned properties (as verified
  11165. // with Object.prototype.hasOwnProperty.call), the same set of keys
  11166. // (although not necessarily the same order), equivalent values for every
  11167. // corresponding key, and an identical 'prototype' property. Note: this
  11168. // accounts for both named and indexed properties on Arrays.
  11169. } else if (isBuffer(actual) !== isBuffer(expected)) {
  11170. return false;
  11171. } else {
  11172. memos = memos || {actual: [], expected: []};
  11173. var actualIndex = memos.actual.indexOf(actual);
  11174. if (actualIndex !== -1) {
  11175. if (actualIndex === memos.expected.indexOf(expected)) {
  11176. return true;
  11177. }
  11178. }
  11179. memos.actual.push(actual);
  11180. memos.expected.push(expected);
  11181. return objEquiv(actual, expected, strict, memos);
  11182. }
  11183. }
  11184. function isArguments(object) {
  11185. return Object.prototype.toString.call(object) == '[object Arguments]';
  11186. }
  11187. function objEquiv(a, b, strict, actualVisitedObjects) {
  11188. if (a === null || a === undefined || b === null || b === undefined)
  11189. return false;
  11190. // if one is a primitive, the other must be same
  11191. if (util.isPrimitive(a) || util.isPrimitive(b))
  11192. return a === b;
  11193. if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
  11194. return false;
  11195. var aIsArgs = isArguments(a);
  11196. var bIsArgs = isArguments(b);
  11197. if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
  11198. return false;
  11199. if (aIsArgs) {
  11200. a = pSlice.call(a);
  11201. b = pSlice.call(b);
  11202. return _deepEqual(a, b, strict);
  11203. }
  11204. var ka = objectKeys(a);
  11205. var kb = objectKeys(b);
  11206. var key, i;
  11207. // having the same number of owned properties (keys incorporates
  11208. // hasOwnProperty)
  11209. if (ka.length !== kb.length)
  11210. return false;
  11211. //the same set of keys (although not necessarily the same order),
  11212. ka.sort();
  11213. kb.sort();
  11214. //~~~cheap key test
  11215. for (i = ka.length - 1; i >= 0; i--) {
  11216. if (ka[i] !== kb[i])
  11217. return false;
  11218. }
  11219. //equivalent values for every corresponding key, and
  11220. //~~~possibly expensive deep test
  11221. for (i = ka.length - 1; i >= 0; i--) {
  11222. key = ka[i];
  11223. if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects))
  11224. return false;
  11225. }
  11226. return true;
  11227. }
  11228. // 8. The non-equivalence assertion tests for any deep inequality.
  11229. // assert.notDeepEqual(actual, expected, message_opt);
  11230. assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
  11231. if (_deepEqual(actual, expected, false)) {
  11232. fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
  11233. }
  11234. };
  11235. assert.notDeepStrictEqual = notDeepStrictEqual;
  11236. function notDeepStrictEqual(actual, expected, message) {
  11237. if (_deepEqual(actual, expected, true)) {
  11238. fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual);
  11239. }
  11240. }
  11241. // 9. The strict equality assertion tests strict equality, as determined by ===.
  11242. // assert.strictEqual(actual, expected, message_opt);
  11243. assert.strictEqual = function strictEqual(actual, expected, message) {
  11244. if (actual !== expected) {
  11245. fail(actual, expected, message, '===', assert.strictEqual);
  11246. }
  11247. };
  11248. // 10. The strict non-equality assertion tests for strict inequality, as
  11249. // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
  11250. assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
  11251. if (actual === expected) {
  11252. fail(actual, expected, message, '!==', assert.notStrictEqual);
  11253. }
  11254. };
  11255. function expectedException(actual, expected) {
  11256. if (!actual || !expected) {
  11257. return false;
  11258. }
  11259. if (Object.prototype.toString.call(expected) == '[object RegExp]') {
  11260. return expected.test(actual);
  11261. }
  11262. try {
  11263. if (actual instanceof expected) {
  11264. return true;
  11265. }
  11266. } catch (e) {
  11267. // Ignore. The instanceof check doesn't work for arrow functions.
  11268. }
  11269. if (Error.isPrototypeOf(expected)) {
  11270. return false;
  11271. }
  11272. return expected.call({}, actual) === true;
  11273. }
  11274. function _tryBlock(block) {
  11275. var error;
  11276. try {
  11277. block();
  11278. } catch (e) {
  11279. error = e;
  11280. }
  11281. return error;
  11282. }
  11283. function _throws(shouldThrow, block, expected, message) {
  11284. var actual;
  11285. if (typeof block !== 'function') {
  11286. throw new TypeError('"block" argument must be a function');
  11287. }
  11288. if (typeof expected === 'string') {
  11289. message = expected;
  11290. expected = null;
  11291. }
  11292. actual = _tryBlock(block);
  11293. message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
  11294. (message ? ' ' + message : '.');
  11295. if (shouldThrow && !actual) {
  11296. fail(actual, expected, 'Missing expected exception' + message);
  11297. }
  11298. var userProvidedMessage = typeof message === 'string';
  11299. var isUnwantedException = !shouldThrow && util.isError(actual);
  11300. var isUnexpectedException = !shouldThrow && actual && !expected;
  11301. if ((isUnwantedException &&
  11302. userProvidedMessage &&
  11303. expectedException(actual, expected)) ||
  11304. isUnexpectedException) {
  11305. fail(actual, expected, 'Got unwanted exception' + message);
  11306. }
  11307. if ((shouldThrow && actual && expected &&
  11308. !expectedException(actual, expected)) || (!shouldThrow && actual)) {
  11309. throw actual;
  11310. }
  11311. }
  11312. // 11. Expected to throw an error:
  11313. // assert.throws(block, Error_opt, message_opt);
  11314. assert.throws = function(block, /*optional*/error, /*optional*/message) {
  11315. _throws(true, block, error, message);
  11316. };
  11317. // EXTENSION! This is annoying to write outside this module.
  11318. assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
  11319. _throws(false, block, error, message);
  11320. };
  11321. assert.ifError = function(err) { if (err) throw err; };
  11322. var objectKeys = Object.keys || function (obj) {
  11323. var keys = [];
  11324. for (var key in obj) {
  11325. if (hasOwn.call(obj, key)) keys.push(key);
  11326. }
  11327. return keys;
  11328. };
  11329. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  11330. },{"util/":90}],62:[function(require,module,exports){
  11331. 'use strict'
  11332. exports.byteLength = byteLength
  11333. exports.toByteArray = toByteArray
  11334. exports.fromByteArray = fromByteArray
  11335. var lookup = []
  11336. var revLookup = []
  11337. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
  11338. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  11339. for (var i = 0, len = code.length; i < len; ++i) {
  11340. lookup[i] = code[i]
  11341. revLookup[code.charCodeAt(i)] = i
  11342. }
  11343. revLookup['-'.charCodeAt(0)] = 62
  11344. revLookup['_'.charCodeAt(0)] = 63
  11345. function placeHoldersCount (b64) {
  11346. var len = b64.length
  11347. if (len % 4 > 0) {
  11348. throw new Error('Invalid string. Length must be a multiple of 4')
  11349. }
  11350. // the number of equal signs (place holders)
  11351. // if there are two placeholders, than the two characters before it
  11352. // represent one byte
  11353. // if there is only one, then the three characters before it represent 2 bytes
  11354. // this is just a cheap hack to not do indexOf twice
  11355. return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0
  11356. }
  11357. function byteLength (b64) {
  11358. // base64 is 4/3 + up to two characters of the original data
  11359. return b64.length * 3 / 4 - placeHoldersCount(b64)
  11360. }
  11361. function toByteArray (b64) {
  11362. var i, j, l, tmp, placeHolders, arr
  11363. var len = b64.length
  11364. placeHolders = placeHoldersCount(b64)
  11365. arr = new Arr(len * 3 / 4 - placeHolders)
  11366. // if there are placeholders, only get up to the last complete 4 chars
  11367. l = placeHolders > 0 ? len - 4 : len
  11368. var L = 0
  11369. for (i = 0, j = 0; i < l; i += 4, j += 3) {
  11370. tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]
  11371. arr[L++] = (tmp >> 16) & 0xFF
  11372. arr[L++] = (tmp >> 8) & 0xFF
  11373. arr[L++] = tmp & 0xFF
  11374. }
  11375. if (placeHolders === 2) {
  11376. tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
  11377. arr[L++] = tmp & 0xFF
  11378. } else if (placeHolders === 1) {
  11379. tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)
  11380. arr[L++] = (tmp >> 8) & 0xFF
  11381. arr[L++] = tmp & 0xFF
  11382. }
  11383. return arr
  11384. }
  11385. function tripletToBase64 (num) {
  11386. return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
  11387. }
  11388. function encodeChunk (uint8, start, end) {
  11389. var tmp
  11390. var output = []
  11391. for (var i = start; i < end; i += 3) {
  11392. tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
  11393. output.push(tripletToBase64(tmp))
  11394. }
  11395. return output.join('')
  11396. }
  11397. function fromByteArray (uint8) {
  11398. var tmp
  11399. var len = uint8.length
  11400. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  11401. var output = ''
  11402. var parts = []
  11403. var maxChunkLength = 16383 // must be multiple of 3
  11404. // go through the array every three bytes, we'll deal with trailing stuff later
  11405. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  11406. parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
  11407. }
  11408. // pad the end with zeros, but make sure to not forget the extra bytes
  11409. if (extraBytes === 1) {
  11410. tmp = uint8[len - 1]
  11411. output += lookup[tmp >> 2]
  11412. output += lookup[(tmp << 4) & 0x3F]
  11413. output += '=='
  11414. } else if (extraBytes === 2) {
  11415. tmp = (uint8[len - 2] << 8) + (uint8[len - 1])
  11416. output += lookup[tmp >> 10]
  11417. output += lookup[(tmp >> 4) & 0x3F]
  11418. output += lookup[(tmp << 2) & 0x3F]
  11419. output += '='
  11420. }
  11421. parts.push(output)
  11422. return parts.join('')
  11423. }
  11424. },{}],63:[function(require,module,exports){
  11425. },{}],64:[function(require,module,exports){
  11426. (function (global){
  11427. 'use strict';
  11428. var buffer = require('buffer');
  11429. var Buffer = buffer.Buffer;
  11430. var SlowBuffer = buffer.SlowBuffer;
  11431. var MAX_LEN = buffer.kMaxLength || 2147483647;
  11432. exports.alloc = function alloc(size, fill, encoding) {
  11433. if (typeof Buffer.alloc === 'function') {
  11434. return Buffer.alloc(size, fill, encoding);
  11435. }
  11436. if (typeof encoding === 'number') {
  11437. throw new TypeError('encoding must not be number');
  11438. }
  11439. if (typeof size !== 'number') {
  11440. throw new TypeError('size must be a number');
  11441. }
  11442. if (size > MAX_LEN) {
  11443. throw new RangeError('size is too large');
  11444. }
  11445. var enc = encoding;
  11446. var _fill = fill;
  11447. if (_fill === undefined) {
  11448. enc = undefined;
  11449. _fill = 0;
  11450. }
  11451. var buf = new Buffer(size);
  11452. if (typeof _fill === 'string') {
  11453. var fillBuf = new Buffer(_fill, enc);
  11454. var flen = fillBuf.length;
  11455. var i = -1;
  11456. while (++i < size) {
  11457. buf[i] = fillBuf[i % flen];
  11458. }
  11459. } else {
  11460. buf.fill(_fill);
  11461. }
  11462. return buf;
  11463. }
  11464. exports.allocUnsafe = function allocUnsafe(size) {
  11465. if (typeof Buffer.allocUnsafe === 'function') {
  11466. return Buffer.allocUnsafe(size);
  11467. }
  11468. if (typeof size !== 'number') {
  11469. throw new TypeError('size must be a number');
  11470. }
  11471. if (size > MAX_LEN) {
  11472. throw new RangeError('size is too large');
  11473. }
  11474. return new Buffer(size);
  11475. }
  11476. exports.from = function from(value, encodingOrOffset, length) {
  11477. if (typeof Buffer.from === 'function' && (!global.Uint8Array || Uint8Array.from !== Buffer.from)) {
  11478. return Buffer.from(value, encodingOrOffset, length);
  11479. }
  11480. if (typeof value === 'number') {
  11481. throw new TypeError('"value" argument must not be a number');
  11482. }
  11483. if (typeof value === 'string') {
  11484. return new Buffer(value, encodingOrOffset);
  11485. }
  11486. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  11487. var offset = encodingOrOffset;
  11488. if (arguments.length === 1) {
  11489. return new Buffer(value);
  11490. }
  11491. if (typeof offset === 'undefined') {
  11492. offset = 0;
  11493. }
  11494. var len = length;
  11495. if (typeof len === 'undefined') {
  11496. len = value.byteLength - offset;
  11497. }
  11498. if (offset >= value.byteLength) {
  11499. throw new RangeError('\'offset\' is out of bounds');
  11500. }
  11501. if (len > value.byteLength - offset) {
  11502. throw new RangeError('\'length\' is out of bounds');
  11503. }
  11504. return new Buffer(value.slice(offset, offset + len));
  11505. }
  11506. if (Buffer.isBuffer(value)) {
  11507. var out = new Buffer(value.length);
  11508. value.copy(out, 0, 0, value.length);
  11509. return out;
  11510. }
  11511. if (value) {
  11512. if (Array.isArray(value) || (typeof ArrayBuffer !== 'undefined' && value.buffer instanceof ArrayBuffer) || 'length' in value) {
  11513. return new Buffer(value);
  11514. }
  11515. if (value.type === 'Buffer' && Array.isArray(value.data)) {
  11516. return new Buffer(value.data);
  11517. }
  11518. }
  11519. throw new TypeError('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.');
  11520. }
  11521. exports.allocUnsafeSlow = function allocUnsafeSlow(size) {
  11522. if (typeof Buffer.allocUnsafeSlow === 'function') {
  11523. return Buffer.allocUnsafeSlow(size);
  11524. }
  11525. if (typeof size !== 'number') {
  11526. throw new TypeError('size must be a number');
  11527. }
  11528. if (size >= MAX_LEN) {
  11529. throw new RangeError('size is too large');
  11530. }
  11531. return new SlowBuffer(size);
  11532. }
  11533. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  11534. },{"buffer":65}],65:[function(require,module,exports){
  11535. /*!
  11536. * The buffer module from node.js, for the browser.
  11537. *
  11538. * @author Feross Aboukhadijeh <[email protected]> <http://feross.org>
  11539. * @license MIT
  11540. */
  11541. /* eslint-disable no-proto */
  11542. 'use strict'
  11543. var base64 = require('base64-js')
  11544. var ieee754 = require('ieee754')
  11545. exports.Buffer = Buffer
  11546. exports.SlowBuffer = SlowBuffer
  11547. exports.INSPECT_MAX_BYTES = 50
  11548. var K_MAX_LENGTH = 0x7fffffff
  11549. exports.kMaxLength = K_MAX_LENGTH
  11550. /**
  11551. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  11552. * === true Use Uint8Array implementation (fastest)
  11553. * === false Print warning and recommend using `buffer` v4.x which has an Object
  11554. * implementation (most compatible, even IE6)
  11555. *
  11556. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  11557. * Opera 11.6+, iOS 4.2+.
  11558. *
  11559. * We report that the browser does not support typed arrays if the are not subclassable
  11560. * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
  11561. * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
  11562. * for __proto__ and has a buggy typed array implementation.
  11563. */
  11564. Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
  11565. if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
  11566. typeof console.error === 'function') {
  11567. console.error(
  11568. 'This browser lacks typed array (Uint8Array) support which is required by ' +
  11569. '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
  11570. )
  11571. }
  11572. function typedArraySupport () {
  11573. // Can typed array instances can be augmented?
  11574. try {
  11575. var arr = new Uint8Array(1)
  11576. arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}
  11577. return arr.foo() === 42
  11578. } catch (e) {
  11579. return false
  11580. }
  11581. }
  11582. function createBuffer (length) {
  11583. if (length > K_MAX_LENGTH) {
  11584. throw new RangeError('Invalid typed array length')
  11585. }
  11586. // Return an augmented `Uint8Array` instance
  11587. var buf = new Uint8Array(length)
  11588. buf.__proto__ = Buffer.prototype
  11589. return buf
  11590. }
  11591. /**
  11592. * The Buffer constructor returns instances of `Uint8Array` that have their
  11593. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  11594. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  11595. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  11596. * returns a single octet.
  11597. *
  11598. * The `Uint8Array` prototype remains unmodified.
  11599. */
  11600. function Buffer (arg, encodingOrOffset, length) {
  11601. // Common case.
  11602. if (typeof arg === 'number') {
  11603. if (typeof encodingOrOffset === 'string') {
  11604. throw new Error(
  11605. 'If encoding is specified then the first argument must be a string'
  11606. )
  11607. }
  11608. return allocUnsafe(arg)
  11609. }
  11610. return from(arg, encodingOrOffset, length)
  11611. }
  11612. // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
  11613. if (typeof Symbol !== 'undefined' && Symbol.species &&
  11614. Buffer[Symbol.species] === Buffer) {
  11615. Object.defineProperty(Buffer, Symbol.species, {
  11616. value: null,
  11617. configurable: true,
  11618. enumerable: false,
  11619. writable: false
  11620. })
  11621. }
  11622. Buffer.poolSize = 8192 // not used by this implementation
  11623. function from (value, encodingOrOffset, length) {
  11624. if (typeof value === 'number') {
  11625. throw new TypeError('"value" argument must not be a number')
  11626. }
  11627. if (value instanceof ArrayBuffer) {
  11628. return fromArrayBuffer(value, encodingOrOffset, length)
  11629. }
  11630. if (typeof value === 'string') {
  11631. return fromString(value, encodingOrOffset)
  11632. }
  11633. return fromObject(value)
  11634. }
  11635. /**
  11636. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  11637. * if value is a number.
  11638. * Buffer.from(str[, encoding])
  11639. * Buffer.from(array)
  11640. * Buffer.from(buffer)
  11641. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  11642. **/
  11643. Buffer.from = function (value, encodingOrOffset, length) {
  11644. return from(value, encodingOrOffset, length)
  11645. }
  11646. // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
  11647. // https://github.com/feross/buffer/pull/148
  11648. Buffer.prototype.__proto__ = Uint8Array.prototype
  11649. Buffer.__proto__ = Uint8Array
  11650. function assertSize (size) {
  11651. if (typeof size !== 'number') {
  11652. throw new TypeError('"size" argument must be a number')
  11653. } else if (size < 0) {
  11654. throw new RangeError('"size" argument must not be negative')
  11655. }
  11656. }
  11657. function alloc (size, fill, encoding) {
  11658. assertSize(size)
  11659. if (size <= 0) {
  11660. return createBuffer(size)
  11661. }
  11662. if (fill !== undefined) {
  11663. // Only pay attention to encoding if it's a string. This
  11664. // prevents accidentally sending in a number that would
  11665. // be interpretted as a start offset.
  11666. return typeof encoding === 'string'
  11667. ? createBuffer(size).fill(fill, encoding)
  11668. : createBuffer(size).fill(fill)
  11669. }
  11670. return createBuffer(size)
  11671. }
  11672. /**
  11673. * Creates a new filled Buffer instance.
  11674. * alloc(size[, fill[, encoding]])
  11675. **/
  11676. Buffer.alloc = function (size, fill, encoding) {
  11677. return alloc(size, fill, encoding)
  11678. }
  11679. function allocUnsafe (size) {
  11680. assertSize(size)
  11681. return createBuffer(size < 0 ? 0 : checked(size) | 0)
  11682. }
  11683. /**
  11684. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  11685. * */
  11686. Buffer.allocUnsafe = function (size) {
  11687. return allocUnsafe(size)
  11688. }
  11689. /**
  11690. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  11691. */
  11692. Buffer.allocUnsafeSlow = function (size) {
  11693. return allocUnsafe(size)
  11694. }
  11695. function fromString (string, encoding) {
  11696. if (typeof encoding !== 'string' || encoding === '') {
  11697. encoding = 'utf8'
  11698. }
  11699. if (!Buffer.isEncoding(encoding)) {
  11700. throw new TypeError('"encoding" must be a valid string encoding')
  11701. }
  11702. var length = byteLength(string, encoding) | 0
  11703. var buf = createBuffer(length)
  11704. var actual = buf.write(string, encoding)
  11705. if (actual !== length) {
  11706. // Writing a hex string, for example, that contains invalid characters will
  11707. // cause everything after the first invalid character to be ignored. (e.g.
  11708. // 'abxxcd' will be treated as 'ab')
  11709. buf = buf.slice(0, actual)
  11710. }
  11711. return buf
  11712. }
  11713. function fromArrayLike (array) {
  11714. var length = array.length < 0 ? 0 : checked(array.length) | 0
  11715. var buf = createBuffer(length)
  11716. for (var i = 0; i < length; i += 1) {
  11717. buf[i] = array[i] & 255
  11718. }
  11719. return buf
  11720. }
  11721. function fromArrayBuffer (array, byteOffset, length) {
  11722. if (byteOffset < 0 || array.byteLength < byteOffset) {
  11723. throw new RangeError('\'offset\' is out of bounds')
  11724. }
  11725. if (array.byteLength < byteOffset + (length || 0)) {
  11726. throw new RangeError('\'length\' is out of bounds')
  11727. }
  11728. var buf
  11729. if (byteOffset === undefined && length === undefined) {
  11730. buf = new Uint8Array(array)
  11731. } else if (length === undefined) {
  11732. buf = new Uint8Array(array, byteOffset)
  11733. } else {
  11734. buf = new Uint8Array(array, byteOffset, length)
  11735. }
  11736. // Return an augmented `Uint8Array` instance
  11737. buf.__proto__ = Buffer.prototype
  11738. return buf
  11739. }
  11740. function fromObject (obj) {
  11741. if (Buffer.isBuffer(obj)) {
  11742. var len = checked(obj.length) | 0
  11743. var buf = createBuffer(len)
  11744. if (buf.length === 0) {
  11745. return buf
  11746. }
  11747. obj.copy(buf, 0, 0, len)
  11748. return buf
  11749. }
  11750. if (obj) {
  11751. if (ArrayBuffer.isView(obj) || 'length' in obj) {
  11752. if (typeof obj.length !== 'number' || isnan(obj.length)) {
  11753. return createBuffer(0)
  11754. }
  11755. return fromArrayLike(obj)
  11756. }
  11757. if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
  11758. return fromArrayLike(obj.data)
  11759. }
  11760. }
  11761. throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
  11762. }
  11763. function checked (length) {
  11764. // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
  11765. // length is NaN (which is otherwise coerced to zero.)
  11766. if (length >= K_MAX_LENGTH) {
  11767. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  11768. 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
  11769. }
  11770. return length | 0
  11771. }
  11772. function SlowBuffer (length) {
  11773. if (+length != length) { // eslint-disable-line eqeqeq
  11774. length = 0
  11775. }
  11776. return Buffer.alloc(+length)
  11777. }
  11778. Buffer.isBuffer = function isBuffer (b) {
  11779. return b != null && b._isBuffer === true
  11780. }
  11781. Buffer.compare = function compare (a, b) {
  11782. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  11783. throw new TypeError('Arguments must be Buffers')
  11784. }
  11785. if (a === b) return 0
  11786. var x = a.length
  11787. var y = b.length
  11788. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  11789. if (a[i] !== b[i]) {
  11790. x = a[i]
  11791. y = b[i]
  11792. break
  11793. }
  11794. }
  11795. if (x < y) return -1
  11796. if (y < x) return 1
  11797. return 0
  11798. }
  11799. Buffer.isEncoding = function isEncoding (encoding) {
  11800. switch (String(encoding).toLowerCase()) {
  11801. case 'hex':
  11802. case 'utf8':
  11803. case 'utf-8':
  11804. case 'ascii':
  11805. case 'latin1':
  11806. case 'binary':
  11807. case 'base64':
  11808. case 'ucs2':
  11809. case 'ucs-2':
  11810. case 'utf16le':
  11811. case 'utf-16le':
  11812. return true
  11813. default:
  11814. return false
  11815. }
  11816. }
  11817. Buffer.concat = function concat (list, length) {
  11818. if (!Array.isArray(list)) {
  11819. throw new TypeError('"list" argument must be an Array of Buffers')
  11820. }
  11821. if (list.length === 0) {
  11822. return Buffer.alloc(0)
  11823. }
  11824. var i
  11825. if (length === undefined) {
  11826. length = 0
  11827. for (i = 0; i < list.length; ++i) {
  11828. length += list[i].length
  11829. }
  11830. }
  11831. var buffer = Buffer.allocUnsafe(length)
  11832. var pos = 0
  11833. for (i = 0; i < list.length; ++i) {
  11834. var buf = list[i]
  11835. if (!Buffer.isBuffer(buf)) {
  11836. throw new TypeError('"list" argument must be an Array of Buffers')
  11837. }
  11838. buf.copy(buffer, pos)
  11839. pos += buf.length
  11840. }
  11841. return buffer
  11842. }
  11843. function byteLength (string, encoding) {
  11844. if (Buffer.isBuffer(string)) {
  11845. return string.length
  11846. }
  11847. if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer) {
  11848. return string.byteLength
  11849. }
  11850. if (typeof string !== 'string') {
  11851. string = '' + string
  11852. }
  11853. var len = string.length
  11854. if (len === 0) return 0
  11855. // Use a for loop to avoid recursion
  11856. var loweredCase = false
  11857. for (;;) {
  11858. switch (encoding) {
  11859. case 'ascii':
  11860. case 'latin1':
  11861. case 'binary':
  11862. return len
  11863. case 'utf8':
  11864. case 'utf-8':
  11865. case undefined:
  11866. return utf8ToBytes(string).length
  11867. case 'ucs2':
  11868. case 'ucs-2':
  11869. case 'utf16le':
  11870. case 'utf-16le':
  11871. return len * 2
  11872. case 'hex':
  11873. return len >>> 1
  11874. case 'base64':
  11875. return base64ToBytes(string).length
  11876. default:
  11877. if (loweredCase) return utf8ToBytes(string).length // assume utf8
  11878. encoding = ('' + encoding).toLowerCase()
  11879. loweredCase = true
  11880. }
  11881. }
  11882. }
  11883. Buffer.byteLength = byteLength
  11884. function slowToString (encoding, start, end) {
  11885. var loweredCase = false
  11886. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  11887. // property of a typed array.
  11888. // This behaves neither like String nor Uint8Array in that we set start/end
  11889. // to their upper/lower bounds if the value passed is out of range.
  11890. // undefined is handled specially as per ECMA-262 6th Edition,
  11891. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  11892. if (start === undefined || start < 0) {
  11893. start = 0
  11894. }
  11895. // Return early if start > this.length. Done here to prevent potential uint32
  11896. // coercion fail below.
  11897. if (start > this.length) {
  11898. return ''
  11899. }
  11900. if (end === undefined || end > this.length) {
  11901. end = this.length
  11902. }
  11903. if (end <= 0) {
  11904. return ''
  11905. }
  11906. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  11907. end >>>= 0
  11908. start >>>= 0
  11909. if (end <= start) {
  11910. return ''
  11911. }
  11912. if (!encoding) encoding = 'utf8'
  11913. while (true) {
  11914. switch (encoding) {
  11915. case 'hex':
  11916. return hexSlice(this, start, end)
  11917. case 'utf8':
  11918. case 'utf-8':
  11919. return utf8Slice(this, start, end)
  11920. case 'ascii':
  11921. return asciiSlice(this, start, end)
  11922. case 'latin1':
  11923. case 'binary':
  11924. return latin1Slice(this, start, end)
  11925. case 'base64':
  11926. return base64Slice(this, start, end)
  11927. case 'ucs2':
  11928. case 'ucs-2':
  11929. case 'utf16le':
  11930. case 'utf-16le':
  11931. return utf16leSlice(this, start, end)
  11932. default:
  11933. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  11934. encoding = (encoding + '').toLowerCase()
  11935. loweredCase = true
  11936. }
  11937. }
  11938. }
  11939. // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
  11940. // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
  11941. // reliably in a browserify context because there could be multiple different
  11942. // copies of the 'buffer' package in use. This method works even for Buffer
  11943. // instances that were created from another copy of the `buffer` package.
  11944. // See: https://github.com/feross/buffer/issues/154
  11945. Buffer.prototype._isBuffer = true
  11946. function swap (b, n, m) {
  11947. var i = b[n]
  11948. b[n] = b[m]
  11949. b[m] = i
  11950. }
  11951. Buffer.prototype.swap16 = function swap16 () {
  11952. var len = this.length
  11953. if (len % 2 !== 0) {
  11954. throw new RangeError('Buffer size must be a multiple of 16-bits')
  11955. }
  11956. for (var i = 0; i < len; i += 2) {
  11957. swap(this, i, i + 1)
  11958. }
  11959. return this
  11960. }
  11961. Buffer.prototype.swap32 = function swap32 () {
  11962. var len = this.length
  11963. if (len % 4 !== 0) {
  11964. throw new RangeError('Buffer size must be a multiple of 32-bits')
  11965. }
  11966. for (var i = 0; i < len; i += 4) {
  11967. swap(this, i, i + 3)
  11968. swap(this, i + 1, i + 2)
  11969. }
  11970. return this
  11971. }
  11972. Buffer.prototype.swap64 = function swap64 () {
  11973. var len = this.length
  11974. if (len % 8 !== 0) {
  11975. throw new RangeError('Buffer size must be a multiple of 64-bits')
  11976. }
  11977. for (var i = 0; i < len; i += 8) {
  11978. swap(this, i, i + 7)
  11979. swap(this, i + 1, i + 6)
  11980. swap(this, i + 2, i + 5)
  11981. swap(this, i + 3, i + 4)
  11982. }
  11983. return this
  11984. }
  11985. Buffer.prototype.toString = function toString () {
  11986. var length = this.length
  11987. if (length === 0) return ''
  11988. if (arguments.length === 0) return utf8Slice(this, 0, length)
  11989. return slowToString.apply(this, arguments)
  11990. }
  11991. Buffer.prototype.equals = function equals (b) {
  11992. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  11993. if (this === b) return true
  11994. return Buffer.compare(this, b) === 0
  11995. }
  11996. Buffer.prototype.inspect = function inspect () {
  11997. var str = ''
  11998. var max = exports.INSPECT_MAX_BYTES
  11999. if (this.length > 0) {
  12000. str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
  12001. if (this.length > max) str += ' ... '
  12002. }
  12003. return '<Buffer ' + str + '>'
  12004. }
  12005. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  12006. if (!Buffer.isBuffer(target)) {
  12007. throw new TypeError('Argument must be a Buffer')
  12008. }
  12009. if (start === undefined) {
  12010. start = 0
  12011. }
  12012. if (end === undefined) {
  12013. end = target ? target.length : 0
  12014. }
  12015. if (thisStart === undefined) {
  12016. thisStart = 0
  12017. }
  12018. if (thisEnd === undefined) {
  12019. thisEnd = this.length
  12020. }
  12021. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  12022. throw new RangeError('out of range index')
  12023. }
  12024. if (thisStart >= thisEnd && start >= end) {
  12025. return 0
  12026. }
  12027. if (thisStart >= thisEnd) {
  12028. return -1
  12029. }
  12030. if (start >= end) {
  12031. return 1
  12032. }
  12033. start >>>= 0
  12034. end >>>= 0
  12035. thisStart >>>= 0
  12036. thisEnd >>>= 0
  12037. if (this === target) return 0
  12038. var x = thisEnd - thisStart
  12039. var y = end - start
  12040. var len = Math.min(x, y)
  12041. var thisCopy = this.slice(thisStart, thisEnd)
  12042. var targetCopy = target.slice(start, end)
  12043. for (var i = 0; i < len; ++i) {
  12044. if (thisCopy[i] !== targetCopy[i]) {
  12045. x = thisCopy[i]
  12046. y = targetCopy[i]
  12047. break
  12048. }
  12049. }
  12050. if (x < y) return -1
  12051. if (y < x) return 1
  12052. return 0
  12053. }
  12054. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  12055. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  12056. //
  12057. // Arguments:
  12058. // - buffer - a Buffer to search
  12059. // - val - a string, Buffer, or number
  12060. // - byteOffset - an index into `buffer`; will be clamped to an int32
  12061. // - encoding - an optional encoding, relevant is val is a string
  12062. // - dir - true for indexOf, false for lastIndexOf
  12063. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  12064. // Empty buffer means no match
  12065. if (buffer.length === 0) return -1
  12066. // Normalize byteOffset
  12067. if (typeof byteOffset === 'string') {
  12068. encoding = byteOffset
  12069. byteOffset = 0
  12070. } else if (byteOffset > 0x7fffffff) {
  12071. byteOffset = 0x7fffffff
  12072. } else if (byteOffset < -0x80000000) {
  12073. byteOffset = -0x80000000
  12074. }
  12075. byteOffset = +byteOffset // Coerce to Number.
  12076. if (isNaN(byteOffset)) {
  12077. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  12078. byteOffset = dir ? 0 : (buffer.length - 1)
  12079. }
  12080. // Normalize byteOffset: negative offsets start from the end of the buffer
  12081. if (byteOffset < 0) byteOffset = buffer.length + byteOffset
  12082. if (byteOffset >= buffer.length) {
  12083. if (dir) return -1
  12084. else byteOffset = buffer.length - 1
  12085. } else if (byteOffset < 0) {
  12086. if (dir) byteOffset = 0
  12087. else return -1
  12088. }
  12089. // Normalize val
  12090. if (typeof val === 'string') {
  12091. val = Buffer.from(val, encoding)
  12092. }
  12093. // Finally, search either indexOf (if dir is true) or lastIndexOf
  12094. if (Buffer.isBuffer(val)) {
  12095. // Special case: looking for empty string/buffer always fails
  12096. if (val.length === 0) {
  12097. return -1
  12098. }
  12099. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  12100. } else if (typeof val === 'number') {
  12101. val = val & 0xFF // Search for a byte value [0-255]
  12102. if (typeof Uint8Array.prototype.indexOf === 'function') {
  12103. if (dir) {
  12104. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  12105. } else {
  12106. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  12107. }
  12108. }
  12109. return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
  12110. }
  12111. throw new TypeError('val must be string, number or Buffer')
  12112. }
  12113. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  12114. var indexSize = 1
  12115. var arrLength = arr.length
  12116. var valLength = val.length
  12117. if (encoding !== undefined) {
  12118. encoding = String(encoding).toLowerCase()
  12119. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  12120. encoding === 'utf16le' || encoding === 'utf-16le') {
  12121. if (arr.length < 2 || val.length < 2) {
  12122. return -1
  12123. }
  12124. indexSize = 2
  12125. arrLength /= 2
  12126. valLength /= 2
  12127. byteOffset /= 2
  12128. }
  12129. }
  12130. function read (buf, i) {
  12131. if (indexSize === 1) {
  12132. return buf[i]
  12133. } else {
  12134. return buf.readUInt16BE(i * indexSize)
  12135. }
  12136. }
  12137. var i
  12138. if (dir) {
  12139. var foundIndex = -1
  12140. for (i = byteOffset; i < arrLength; i++) {
  12141. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  12142. if (foundIndex === -1) foundIndex = i
  12143. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  12144. } else {
  12145. if (foundIndex !== -1) i -= i - foundIndex
  12146. foundIndex = -1
  12147. }
  12148. }
  12149. } else {
  12150. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
  12151. for (i = byteOffset; i >= 0; i--) {
  12152. var found = true
  12153. for (var j = 0; j < valLength; j++) {
  12154. if (read(arr, i + j) !== read(val, j)) {
  12155. found = false
  12156. break
  12157. }
  12158. }
  12159. if (found) return i
  12160. }
  12161. }
  12162. return -1
  12163. }
  12164. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  12165. return this.indexOf(val, byteOffset, encoding) !== -1
  12166. }
  12167. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  12168. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  12169. }
  12170. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  12171. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  12172. }
  12173. function hexWrite (buf, string, offset, length) {
  12174. offset = Number(offset) || 0
  12175. var remaining = buf.length - offset
  12176. if (!length) {
  12177. length = remaining
  12178. } else {
  12179. length = Number(length)
  12180. if (length > remaining) {
  12181. length = remaining
  12182. }
  12183. }
  12184. // must be an even number of digits
  12185. var strLen = string.length
  12186. if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
  12187. if (length > strLen / 2) {
  12188. length = strLen / 2
  12189. }
  12190. for (var i = 0; i < length; ++i) {
  12191. var parsed = parseInt(string.substr(i * 2, 2), 16)
  12192. if (isNaN(parsed)) return i
  12193. buf[offset + i] = parsed
  12194. }
  12195. return i
  12196. }
  12197. function utf8Write (buf, string, offset, length) {
  12198. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  12199. }
  12200. function asciiWrite (buf, string, offset, length) {
  12201. return blitBuffer(asciiToBytes(string), buf, offset, length)
  12202. }
  12203. function latin1Write (buf, string, offset, length) {
  12204. return asciiWrite(buf, string, offset, length)
  12205. }
  12206. function base64Write (buf, string, offset, length) {
  12207. return blitBuffer(base64ToBytes(string), buf, offset, length)
  12208. }
  12209. function ucs2Write (buf, string, offset, length) {
  12210. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  12211. }
  12212. Buffer.prototype.write = function write (string, offset, length, encoding) {
  12213. // Buffer#write(string)
  12214. if (offset === undefined) {
  12215. encoding = 'utf8'
  12216. length = this.length
  12217. offset = 0
  12218. // Buffer#write(string, encoding)
  12219. } else if (length === undefined && typeof offset === 'string') {
  12220. encoding = offset
  12221. length = this.length
  12222. offset = 0
  12223. // Buffer#write(string, offset[, length][, encoding])
  12224. } else if (isFinite(offset)) {
  12225. offset = offset >>> 0
  12226. if (isFinite(length)) {
  12227. length = length >>> 0
  12228. if (encoding === undefined) encoding = 'utf8'
  12229. } else {
  12230. encoding = length
  12231. length = undefined
  12232. }
  12233. } else {
  12234. throw new Error(
  12235. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  12236. )
  12237. }
  12238. var remaining = this.length - offset
  12239. if (length === undefined || length > remaining) length = remaining
  12240. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  12241. throw new RangeError('Attempt to write outside buffer bounds')
  12242. }
  12243. if (!encoding) encoding = 'utf8'
  12244. var loweredCase = false
  12245. for (;;) {
  12246. switch (encoding) {
  12247. case 'hex':
  12248. return hexWrite(this, string, offset, length)
  12249. case 'utf8':
  12250. case 'utf-8':
  12251. return utf8Write(this, string, offset, length)
  12252. case 'ascii':
  12253. return asciiWrite(this, string, offset, length)
  12254. case 'latin1':
  12255. case 'binary':
  12256. return latin1Write(this, string, offset, length)
  12257. case 'base64':
  12258. // Warning: maxLength not taken into account in base64Write
  12259. return base64Write(this, string, offset, length)
  12260. case 'ucs2':
  12261. case 'ucs-2':
  12262. case 'utf16le':
  12263. case 'utf-16le':
  12264. return ucs2Write(this, string, offset, length)
  12265. default:
  12266. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  12267. encoding = ('' + encoding).toLowerCase()
  12268. loweredCase = true
  12269. }
  12270. }
  12271. }
  12272. Buffer.prototype.toJSON = function toJSON () {
  12273. return {
  12274. type: 'Buffer',
  12275. data: Array.prototype.slice.call(this._arr || this, 0)
  12276. }
  12277. }
  12278. function base64Slice (buf, start, end) {
  12279. if (start === 0 && end === buf.length) {
  12280. return base64.fromByteArray(buf)
  12281. } else {
  12282. return base64.fromByteArray(buf.slice(start, end))
  12283. }
  12284. }
  12285. function utf8Slice (buf, start, end) {
  12286. end = Math.min(buf.length, end)
  12287. var res = []
  12288. var i = start
  12289. while (i < end) {
  12290. var firstByte = buf[i]
  12291. var codePoint = null
  12292. var bytesPerSequence = (firstByte > 0xEF) ? 4
  12293. : (firstByte > 0xDF) ? 3
  12294. : (firstByte > 0xBF) ? 2
  12295. : 1
  12296. if (i + bytesPerSequence <= end) {
  12297. var secondByte, thirdByte, fourthByte, tempCodePoint
  12298. switch (bytesPerSequence) {
  12299. case 1:
  12300. if (firstByte < 0x80) {
  12301. codePoint = firstByte
  12302. }
  12303. break
  12304. case 2:
  12305. secondByte = buf[i + 1]
  12306. if ((secondByte & 0xC0) === 0x80) {
  12307. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  12308. if (tempCodePoint > 0x7F) {
  12309. codePoint = tempCodePoint
  12310. }
  12311. }
  12312. break
  12313. case 3:
  12314. secondByte = buf[i + 1]
  12315. thirdByte = buf[i + 2]
  12316. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  12317. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  12318. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  12319. codePoint = tempCodePoint
  12320. }
  12321. }
  12322. break
  12323. case 4:
  12324. secondByte = buf[i + 1]
  12325. thirdByte = buf[i + 2]
  12326. fourthByte = buf[i + 3]
  12327. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  12328. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  12329. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  12330. codePoint = tempCodePoint
  12331. }
  12332. }
  12333. }
  12334. }
  12335. if (codePoint === null) {
  12336. // we did not generate a valid codePoint so insert a
  12337. // replacement char (U+FFFD) and advance only 1 byte
  12338. codePoint = 0xFFFD
  12339. bytesPerSequence = 1
  12340. } else if (codePoint > 0xFFFF) {
  12341. // encode to utf16 (surrogate pair dance)
  12342. codePoint -= 0x10000
  12343. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  12344. codePoint = 0xDC00 | codePoint & 0x3FF
  12345. }
  12346. res.push(codePoint)
  12347. i += bytesPerSequence
  12348. }
  12349. return decodeCodePointsArray(res)
  12350. }
  12351. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  12352. // the lowest limit is Chrome, with 0x10000 args.
  12353. // We go 1 magnitude less, for safety
  12354. var MAX_ARGUMENTS_LENGTH = 0x1000
  12355. function decodeCodePointsArray (codePoints) {
  12356. var len = codePoints.length
  12357. if (len <= MAX_ARGUMENTS_LENGTH) {
  12358. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  12359. }
  12360. // Decode in chunks to avoid "call stack size exceeded".
  12361. var res = ''
  12362. var i = 0
  12363. while (i < len) {
  12364. res += String.fromCharCode.apply(
  12365. String,
  12366. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  12367. )
  12368. }
  12369. return res
  12370. }
  12371. function asciiSlice (buf, start, end) {
  12372. var ret = ''
  12373. end = Math.min(buf.length, end)
  12374. for (var i = start; i < end; ++i) {
  12375. ret += String.fromCharCode(buf[i] & 0x7F)
  12376. }
  12377. return ret
  12378. }
  12379. function latin1Slice (buf, start, end) {
  12380. var ret = ''
  12381. end = Math.min(buf.length, end)
  12382. for (var i = start; i < end; ++i) {
  12383. ret += String.fromCharCode(buf[i])
  12384. }
  12385. return ret
  12386. }
  12387. function hexSlice (buf, start, end) {
  12388. var len = buf.length
  12389. if (!start || start < 0) start = 0
  12390. if (!end || end < 0 || end > len) end = len
  12391. var out = ''
  12392. for (var i = start; i < end; ++i) {
  12393. out += toHex(buf[i])
  12394. }
  12395. return out
  12396. }
  12397. function utf16leSlice (buf, start, end) {
  12398. var bytes = buf.slice(start, end)
  12399. var res = ''
  12400. for (var i = 0; i < bytes.length; i += 2) {
  12401. res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
  12402. }
  12403. return res
  12404. }
  12405. Buffer.prototype.slice = function slice (start, end) {
  12406. var len = this.length
  12407. start = ~~start
  12408. end = end === undefined ? len : ~~end
  12409. if (start < 0) {
  12410. start += len
  12411. if (start < 0) start = 0
  12412. } else if (start > len) {
  12413. start = len
  12414. }
  12415. if (end < 0) {
  12416. end += len
  12417. if (end < 0) end = 0
  12418. } else if (end > len) {
  12419. end = len
  12420. }
  12421. if (end < start) end = start
  12422. var newBuf = this.subarray(start, end)
  12423. // Return an augmented `Uint8Array` instance
  12424. newBuf.__proto__ = Buffer.prototype
  12425. return newBuf
  12426. }
  12427. /*
  12428. * Need to make sure that buffer isn't trying to write out of bounds.
  12429. */
  12430. function checkOffset (offset, ext, length) {
  12431. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  12432. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  12433. }
  12434. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  12435. offset = offset >>> 0
  12436. byteLength = byteLength >>> 0
  12437. if (!noAssert) checkOffset(offset, byteLength, this.length)
  12438. var val = this[offset]
  12439. var mul = 1
  12440. var i = 0
  12441. while (++i < byteLength && (mul *= 0x100)) {
  12442. val += this[offset + i] * mul
  12443. }
  12444. return val
  12445. }
  12446. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  12447. offset = offset >>> 0
  12448. byteLength = byteLength >>> 0
  12449. if (!noAssert) {
  12450. checkOffset(offset, byteLength, this.length)
  12451. }
  12452. var val = this[offset + --byteLength]
  12453. var mul = 1
  12454. while (byteLength > 0 && (mul *= 0x100)) {
  12455. val += this[offset + --byteLength] * mul
  12456. }
  12457. return val
  12458. }
  12459. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  12460. offset = offset >>> 0
  12461. if (!noAssert) checkOffset(offset, 1, this.length)
  12462. return this[offset]
  12463. }
  12464. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  12465. offset = offset >>> 0
  12466. if (!noAssert) checkOffset(offset, 2, this.length)
  12467. return this[offset] | (this[offset + 1] << 8)
  12468. }
  12469. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  12470. offset = offset >>> 0
  12471. if (!noAssert) checkOffset(offset, 2, this.length)
  12472. return (this[offset] << 8) | this[offset + 1]
  12473. }
  12474. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  12475. offset = offset >>> 0
  12476. if (!noAssert) checkOffset(offset, 4, this.length)
  12477. return ((this[offset]) |
  12478. (this[offset + 1] << 8) |
  12479. (this[offset + 2] << 16)) +
  12480. (this[offset + 3] * 0x1000000)
  12481. }
  12482. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  12483. offset = offset >>> 0
  12484. if (!noAssert) checkOffset(offset, 4, this.length)
  12485. return (this[offset] * 0x1000000) +
  12486. ((this[offset + 1] << 16) |
  12487. (this[offset + 2] << 8) |
  12488. this[offset + 3])
  12489. }
  12490. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  12491. offset = offset >>> 0
  12492. byteLength = byteLength >>> 0
  12493. if (!noAssert) checkOffset(offset, byteLength, this.length)
  12494. var val = this[offset]
  12495. var mul = 1
  12496. var i = 0
  12497. while (++i < byteLength && (mul *= 0x100)) {
  12498. val += this[offset + i] * mul
  12499. }
  12500. mul *= 0x80
  12501. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  12502. return val
  12503. }
  12504. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  12505. offset = offset >>> 0
  12506. byteLength = byteLength >>> 0
  12507. if (!noAssert) checkOffset(offset, byteLength, this.length)
  12508. var i = byteLength
  12509. var mul = 1
  12510. var val = this[offset + --i]
  12511. while (i > 0 && (mul *= 0x100)) {
  12512. val += this[offset + --i] * mul
  12513. }
  12514. mul *= 0x80
  12515. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  12516. return val
  12517. }
  12518. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  12519. offset = offset >>> 0
  12520. if (!noAssert) checkOffset(offset, 1, this.length)
  12521. if (!(this[offset] & 0x80)) return (this[offset])
  12522. return ((0xff - this[offset] + 1) * -1)
  12523. }
  12524. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  12525. offset = offset >>> 0
  12526. if (!noAssert) checkOffset(offset, 2, this.length)
  12527. var val = this[offset] | (this[offset + 1] << 8)
  12528. return (val & 0x8000) ? val | 0xFFFF0000 : val
  12529. }
  12530. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  12531. offset = offset >>> 0
  12532. if (!noAssert) checkOffset(offset, 2, this.length)
  12533. var val = this[offset + 1] | (this[offset] << 8)
  12534. return (val & 0x8000) ? val | 0xFFFF0000 : val
  12535. }
  12536. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  12537. offset = offset >>> 0
  12538. if (!noAssert) checkOffset(offset, 4, this.length)
  12539. return (this[offset]) |
  12540. (this[offset + 1] << 8) |
  12541. (this[offset + 2] << 16) |
  12542. (this[offset + 3] << 24)
  12543. }
  12544. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  12545. offset = offset >>> 0
  12546. if (!noAssert) checkOffset(offset, 4, this.length)
  12547. return (this[offset] << 24) |
  12548. (this[offset + 1] << 16) |
  12549. (this[offset + 2] << 8) |
  12550. (this[offset + 3])
  12551. }
  12552. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  12553. offset = offset >>> 0
  12554. if (!noAssert) checkOffset(offset, 4, this.length)
  12555. return ieee754.read(this, offset, true, 23, 4)
  12556. }
  12557. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  12558. offset = offset >>> 0
  12559. if (!noAssert) checkOffset(offset, 4, this.length)
  12560. return ieee754.read(this, offset, false, 23, 4)
  12561. }
  12562. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  12563. offset = offset >>> 0
  12564. if (!noAssert) checkOffset(offset, 8, this.length)
  12565. return ieee754.read(this, offset, true, 52, 8)
  12566. }
  12567. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  12568. offset = offset >>> 0
  12569. if (!noAssert) checkOffset(offset, 8, this.length)
  12570. return ieee754.read(this, offset, false, 52, 8)
  12571. }
  12572. function checkInt (buf, value, offset, ext, max, min) {
  12573. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  12574. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  12575. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  12576. }
  12577. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  12578. value = +value
  12579. offset = offset >>> 0
  12580. byteLength = byteLength >>> 0
  12581. if (!noAssert) {
  12582. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  12583. checkInt(this, value, offset, byteLength, maxBytes, 0)
  12584. }
  12585. var mul = 1
  12586. var i = 0
  12587. this[offset] = value & 0xFF
  12588. while (++i < byteLength && (mul *= 0x100)) {
  12589. this[offset + i] = (value / mul) & 0xFF
  12590. }
  12591. return offset + byteLength
  12592. }
  12593. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  12594. value = +value
  12595. offset = offset >>> 0
  12596. byteLength = byteLength >>> 0
  12597. if (!noAssert) {
  12598. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  12599. checkInt(this, value, offset, byteLength, maxBytes, 0)
  12600. }
  12601. var i = byteLength - 1
  12602. var mul = 1
  12603. this[offset + i] = value & 0xFF
  12604. while (--i >= 0 && (mul *= 0x100)) {
  12605. this[offset + i] = (value / mul) & 0xFF
  12606. }
  12607. return offset + byteLength
  12608. }
  12609. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  12610. value = +value
  12611. offset = offset >>> 0
  12612. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  12613. this[offset] = (value & 0xff)
  12614. return offset + 1
  12615. }
  12616. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  12617. value = +value
  12618. offset = offset >>> 0
  12619. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  12620. this[offset] = (value & 0xff)
  12621. this[offset + 1] = (value >>> 8)
  12622. return offset + 2
  12623. }
  12624. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  12625. value = +value
  12626. offset = offset >>> 0
  12627. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  12628. this[offset] = (value >>> 8)
  12629. this[offset + 1] = (value & 0xff)
  12630. return offset + 2
  12631. }
  12632. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  12633. value = +value
  12634. offset = offset >>> 0
  12635. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  12636. this[offset + 3] = (value >>> 24)
  12637. this[offset + 2] = (value >>> 16)
  12638. this[offset + 1] = (value >>> 8)
  12639. this[offset] = (value & 0xff)
  12640. return offset + 4
  12641. }
  12642. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  12643. value = +value
  12644. offset = offset >>> 0
  12645. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  12646. this[offset] = (value >>> 24)
  12647. this[offset + 1] = (value >>> 16)
  12648. this[offset + 2] = (value >>> 8)
  12649. this[offset + 3] = (value & 0xff)
  12650. return offset + 4
  12651. }
  12652. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  12653. value = +value
  12654. offset = offset >>> 0
  12655. if (!noAssert) {
  12656. var limit = Math.pow(2, (8 * byteLength) - 1)
  12657. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  12658. }
  12659. var i = 0
  12660. var mul = 1
  12661. var sub = 0
  12662. this[offset] = value & 0xFF
  12663. while (++i < byteLength && (mul *= 0x100)) {
  12664. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  12665. sub = 1
  12666. }
  12667. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  12668. }
  12669. return offset + byteLength
  12670. }
  12671. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  12672. value = +value
  12673. offset = offset >>> 0
  12674. if (!noAssert) {
  12675. var limit = Math.pow(2, (8 * byteLength) - 1)
  12676. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  12677. }
  12678. var i = byteLength - 1
  12679. var mul = 1
  12680. var sub = 0
  12681. this[offset + i] = value & 0xFF
  12682. while (--i >= 0 && (mul *= 0x100)) {
  12683. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  12684. sub = 1
  12685. }
  12686. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  12687. }
  12688. return offset + byteLength
  12689. }
  12690. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  12691. value = +value
  12692. offset = offset >>> 0
  12693. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  12694. if (value < 0) value = 0xff + value + 1
  12695. this[offset] = (value & 0xff)
  12696. return offset + 1
  12697. }
  12698. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  12699. value = +value
  12700. offset = offset >>> 0
  12701. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  12702. this[offset] = (value & 0xff)
  12703. this[offset + 1] = (value >>> 8)
  12704. return offset + 2
  12705. }
  12706. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  12707. value = +value
  12708. offset = offset >>> 0
  12709. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  12710. this[offset] = (value >>> 8)
  12711. this[offset + 1] = (value & 0xff)
  12712. return offset + 2
  12713. }
  12714. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  12715. value = +value
  12716. offset = offset >>> 0
  12717. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  12718. this[offset] = (value & 0xff)
  12719. this[offset + 1] = (value >>> 8)
  12720. this[offset + 2] = (value >>> 16)
  12721. this[offset + 3] = (value >>> 24)
  12722. return offset + 4
  12723. }
  12724. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  12725. value = +value
  12726. offset = offset >>> 0
  12727. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  12728. if (value < 0) value = 0xffffffff + value + 1
  12729. this[offset] = (value >>> 24)
  12730. this[offset + 1] = (value >>> 16)
  12731. this[offset + 2] = (value >>> 8)
  12732. this[offset + 3] = (value & 0xff)
  12733. return offset + 4
  12734. }
  12735. function checkIEEE754 (buf, value, offset, ext, max, min) {
  12736. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  12737. if (offset < 0) throw new RangeError('Index out of range')
  12738. }
  12739. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  12740. value = +value
  12741. offset = offset >>> 0
  12742. if (!noAssert) {
  12743. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  12744. }
  12745. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  12746. return offset + 4
  12747. }
  12748. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  12749. return writeFloat(this, value, offset, true, noAssert)
  12750. }
  12751. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  12752. return writeFloat(this, value, offset, false, noAssert)
  12753. }
  12754. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  12755. value = +value
  12756. offset = offset >>> 0
  12757. if (!noAssert) {
  12758. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  12759. }
  12760. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  12761. return offset + 8
  12762. }
  12763. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  12764. return writeDouble(this, value, offset, true, noAssert)
  12765. }
  12766. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  12767. return writeDouble(this, value, offset, false, noAssert)
  12768. }
  12769. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  12770. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  12771. if (!start) start = 0
  12772. if (!end && end !== 0) end = this.length
  12773. if (targetStart >= target.length) targetStart = target.length
  12774. if (!targetStart) targetStart = 0
  12775. if (end > 0 && end < start) end = start
  12776. // Copy 0 bytes; we're done
  12777. if (end === start) return 0
  12778. if (target.length === 0 || this.length === 0) return 0
  12779. // Fatal error conditions
  12780. if (targetStart < 0) {
  12781. throw new RangeError('targetStart out of bounds')
  12782. }
  12783. if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
  12784. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  12785. // Are we oob?
  12786. if (end > this.length) end = this.length
  12787. if (target.length - targetStart < end - start) {
  12788. end = target.length - targetStart + start
  12789. }
  12790. var len = end - start
  12791. var i
  12792. if (this === target && start < targetStart && targetStart < end) {
  12793. // descending copy from end
  12794. for (i = len - 1; i >= 0; --i) {
  12795. target[i + targetStart] = this[i + start]
  12796. }
  12797. } else if (len < 1000) {
  12798. // ascending copy from start
  12799. for (i = 0; i < len; ++i) {
  12800. target[i + targetStart] = this[i + start]
  12801. }
  12802. } else {
  12803. Uint8Array.prototype.set.call(
  12804. target,
  12805. this.subarray(start, start + len),
  12806. targetStart
  12807. )
  12808. }
  12809. return len
  12810. }
  12811. // Usage:
  12812. // buffer.fill(number[, offset[, end]])
  12813. // buffer.fill(buffer[, offset[, end]])
  12814. // buffer.fill(string[, offset[, end]][, encoding])
  12815. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  12816. // Handle string cases:
  12817. if (typeof val === 'string') {
  12818. if (typeof start === 'string') {
  12819. encoding = start
  12820. start = 0
  12821. end = this.length
  12822. } else if (typeof end === 'string') {
  12823. encoding = end
  12824. end = this.length
  12825. }
  12826. if (val.length === 1) {
  12827. var code = val.charCodeAt(0)
  12828. if (code < 256) {
  12829. val = code
  12830. }
  12831. }
  12832. if (encoding !== undefined && typeof encoding !== 'string') {
  12833. throw new TypeError('encoding must be a string')
  12834. }
  12835. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  12836. throw new TypeError('Unknown encoding: ' + encoding)
  12837. }
  12838. } else if (typeof val === 'number') {
  12839. val = val & 255
  12840. }
  12841. // Invalid ranges are not set to a default, so can range check early.
  12842. if (start < 0 || this.length < start || this.length < end) {
  12843. throw new RangeError('Out of range index')
  12844. }
  12845. if (end <= start) {
  12846. return this
  12847. }
  12848. start = start >>> 0
  12849. end = end === undefined ? this.length : end >>> 0
  12850. if (!val) val = 0
  12851. var i
  12852. if (typeof val === 'number') {
  12853. for (i = start; i < end; ++i) {
  12854. this[i] = val
  12855. }
  12856. } else {
  12857. var bytes = Buffer.isBuffer(val)
  12858. ? val
  12859. : new Buffer(val, encoding)
  12860. var len = bytes.length
  12861. for (i = 0; i < end - start; ++i) {
  12862. this[i + start] = bytes[i % len]
  12863. }
  12864. }
  12865. return this
  12866. }
  12867. // HELPER FUNCTIONS
  12868. // ================
  12869. var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
  12870. function base64clean (str) {
  12871. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  12872. str = stringtrim(str).replace(INVALID_BASE64_RE, '')
  12873. // Node converts strings with length < 2 to ''
  12874. if (str.length < 2) return ''
  12875. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  12876. while (str.length % 4 !== 0) {
  12877. str = str + '='
  12878. }
  12879. return str
  12880. }
  12881. function stringtrim (str) {
  12882. if (str.trim) return str.trim()
  12883. return str.replace(/^\s+|\s+$/g, '')
  12884. }
  12885. function toHex (n) {
  12886. if (n < 16) return '0' + n.toString(16)
  12887. return n.toString(16)
  12888. }
  12889. function utf8ToBytes (string, units) {
  12890. units = units || Infinity
  12891. var codePoint
  12892. var length = string.length
  12893. var leadSurrogate = null
  12894. var bytes = []
  12895. for (var i = 0; i < length; ++i) {
  12896. codePoint = string.charCodeAt(i)
  12897. // is surrogate component
  12898. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  12899. // last char was a lead
  12900. if (!leadSurrogate) {
  12901. // no lead yet
  12902. if (codePoint > 0xDBFF) {
  12903. // unexpected trail
  12904. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  12905. continue
  12906. } else if (i + 1 === length) {
  12907. // unpaired lead
  12908. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  12909. continue
  12910. }
  12911. // valid lead
  12912. leadSurrogate = codePoint
  12913. continue
  12914. }
  12915. // 2 leads in a row
  12916. if (codePoint < 0xDC00) {
  12917. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  12918. leadSurrogate = codePoint
  12919. continue
  12920. }
  12921. // valid surrogate pair
  12922. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  12923. } else if (leadSurrogate) {
  12924. // valid bmp char, but last char was a lead
  12925. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  12926. }
  12927. leadSurrogate = null
  12928. // encode utf8
  12929. if (codePoint < 0x80) {
  12930. if ((units -= 1) < 0) break
  12931. bytes.push(codePoint)
  12932. } else if (codePoint < 0x800) {
  12933. if ((units -= 2) < 0) break
  12934. bytes.push(
  12935. codePoint >> 0x6 | 0xC0,
  12936. codePoint & 0x3F | 0x80
  12937. )
  12938. } else if (codePoint < 0x10000) {
  12939. if ((units -= 3) < 0) break
  12940. bytes.push(
  12941. codePoint >> 0xC | 0xE0,
  12942. codePoint >> 0x6 & 0x3F | 0x80,
  12943. codePoint & 0x3F | 0x80
  12944. )
  12945. } else if (codePoint < 0x110000) {
  12946. if ((units -= 4) < 0) break
  12947. bytes.push(
  12948. codePoint >> 0x12 | 0xF0,
  12949. codePoint >> 0xC & 0x3F | 0x80,
  12950. codePoint >> 0x6 & 0x3F | 0x80,
  12951. codePoint & 0x3F | 0x80
  12952. )
  12953. } else {
  12954. throw new Error('Invalid code point')
  12955. }
  12956. }
  12957. return bytes
  12958. }
  12959. function asciiToBytes (str) {
  12960. var byteArray = []
  12961. for (var i = 0; i < str.length; ++i) {
  12962. // Node's code seems to be doing this and not & 0x7F..
  12963. byteArray.push(str.charCodeAt(i) & 0xFF)
  12964. }
  12965. return byteArray
  12966. }
  12967. function utf16leToBytes (str, units) {
  12968. var c, hi, lo
  12969. var byteArray = []
  12970. for (var i = 0; i < str.length; ++i) {
  12971. if ((units -= 2) < 0) break
  12972. c = str.charCodeAt(i)
  12973. hi = c >> 8
  12974. lo = c % 256
  12975. byteArray.push(lo)
  12976. byteArray.push(hi)
  12977. }
  12978. return byteArray
  12979. }
  12980. function base64ToBytes (str) {
  12981. return base64.toByteArray(base64clean(str))
  12982. }
  12983. function blitBuffer (src, dst, offset, length) {
  12984. for (var i = 0; i < length; ++i) {
  12985. if ((i + offset >= dst.length) || (i >= src.length)) break
  12986. dst[i + offset] = src[i]
  12987. }
  12988. return i
  12989. }
  12990. function isnan (val) {
  12991. return val !== val // eslint-disable-line no-self-compare
  12992. }
  12993. },{"base64-js":62,"ieee754":68}],66:[function(require,module,exports){
  12994. (function (Buffer){
  12995. // Copyright Joyent, Inc. and other Node contributors.
  12996. //
  12997. // Permission is hereby granted, free of charge, to any person obtaining a
  12998. // copy of this software and associated documentation files (the
  12999. // "Software"), to deal in the Software without restriction, including
  13000. // without limitation the rights to use, copy, modify, merge, publish,
  13001. // distribute, sublicense, and/or sell copies of the Software, and to permit
  13002. // persons to whom the Software is furnished to do so, subject to the
  13003. // following conditions:
  13004. //
  13005. // The above copyright notice and this permission notice shall be included
  13006. // in all copies or substantial portions of the Software.
  13007. //
  13008. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13009. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13010. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  13011. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  13012. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  13013. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  13014. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  13015. // NOTE: These type checking functions intentionally don't use `instanceof`
  13016. // because it is fragile and can be easily faked with `Object.create()`.
  13017. function isArray(arg) {
  13018. if (Array.isArray) {
  13019. return Array.isArray(arg);
  13020. }
  13021. return objectToString(arg) === '[object Array]';
  13022. }
  13023. exports.isArray = isArray;
  13024. function isBoolean(arg) {
  13025. return typeof arg === 'boolean';
  13026. }
  13027. exports.isBoolean = isBoolean;
  13028. function isNull(arg) {
  13029. return arg === null;
  13030. }
  13031. exports.isNull = isNull;
  13032. function isNullOrUndefined(arg) {
  13033. return arg == null;
  13034. }
  13035. exports.isNullOrUndefined = isNullOrUndefined;
  13036. function isNumber(arg) {
  13037. return typeof arg === 'number';
  13038. }
  13039. exports.isNumber = isNumber;
  13040. function isString(arg) {
  13041. return typeof arg === 'string';
  13042. }
  13043. exports.isString = isString;
  13044. function isSymbol(arg) {
  13045. return typeof arg === 'symbol';
  13046. }
  13047. exports.isSymbol = isSymbol;
  13048. function isUndefined(arg) {
  13049. return arg === void 0;
  13050. }
  13051. exports.isUndefined = isUndefined;
  13052. function isRegExp(re) {
  13053. return objectToString(re) === '[object RegExp]';
  13054. }
  13055. exports.isRegExp = isRegExp;
  13056. function isObject(arg) {
  13057. return typeof arg === 'object' && arg !== null;
  13058. }
  13059. exports.isObject = isObject;
  13060. function isDate(d) {
  13061. return objectToString(d) === '[object Date]';
  13062. }
  13063. exports.isDate = isDate;
  13064. function isError(e) {
  13065. return (objectToString(e) === '[object Error]' || e instanceof Error);
  13066. }
  13067. exports.isError = isError;
  13068. function isFunction(arg) {
  13069. return typeof arg === 'function';
  13070. }
  13071. exports.isFunction = isFunction;
  13072. function isPrimitive(arg) {
  13073. return arg === null ||
  13074. typeof arg === 'boolean' ||
  13075. typeof arg === 'number' ||
  13076. typeof arg === 'string' ||
  13077. typeof arg === 'symbol' || // ES6 symbol
  13078. typeof arg === 'undefined';
  13079. }
  13080. exports.isPrimitive = isPrimitive;
  13081. exports.isBuffer = Buffer.isBuffer;
  13082. function objectToString(o) {
  13083. return Object.prototype.toString.call(o);
  13084. }
  13085. }).call(this,{"isBuffer":require("../../is-buffer/index.js")})
  13086. },{"../../is-buffer/index.js":70}],67:[function(require,module,exports){
  13087. // Copyright Joyent, Inc. and other Node contributors.
  13088. //
  13089. // Permission is hereby granted, free of charge, to any person obtaining a
  13090. // copy of this software and associated documentation files (the
  13091. // "Software"), to deal in the Software without restriction, including
  13092. // without limitation the rights to use, copy, modify, merge, publish,
  13093. // distribute, sublicense, and/or sell copies of the Software, and to permit
  13094. // persons to whom the Software is furnished to do so, subject to the
  13095. // following conditions:
  13096. //
  13097. // The above copyright notice and this permission notice shall be included
  13098. // in all copies or substantial portions of the Software.
  13099. //
  13100. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13101. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13102. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  13103. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  13104. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  13105. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  13106. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  13107. function EventEmitter() {
  13108. this._events = this._events || {};
  13109. this._maxListeners = this._maxListeners || undefined;
  13110. }
  13111. module.exports = EventEmitter;
  13112. // Backwards-compat with node 0.10.x
  13113. EventEmitter.EventEmitter = EventEmitter;
  13114. EventEmitter.prototype._events = undefined;
  13115. EventEmitter.prototype._maxListeners = undefined;
  13116. // By default EventEmitters will print a warning if more than 10 listeners are
  13117. // added to it. This is a useful default which helps finding memory leaks.
  13118. EventEmitter.defaultMaxListeners = 10;
  13119. // Obviously not all Emitters should be limited to 10. This function allows
  13120. // that to be increased. Set to zero for unlimited.
  13121. EventEmitter.prototype.setMaxListeners = function(n) {
  13122. if (!isNumber(n) || n < 0 || isNaN(n))
  13123. throw TypeError('n must be a positive number');
  13124. this._maxListeners = n;
  13125. return this;
  13126. };
  13127. EventEmitter.prototype.emit = function(type) {
  13128. var er, handler, len, args, i, listeners;
  13129. if (!this._events)
  13130. this._events = {};
  13131. // If there is no 'error' event listener then throw.
  13132. if (type === 'error') {
  13133. if (!this._events.error ||
  13134. (isObject(this._events.error) && !this._events.error.length)) {
  13135. er = arguments[1];
  13136. if (er instanceof Error) {
  13137. throw er; // Unhandled 'error' event
  13138. } else {
  13139. // At least give some kind of context to the user
  13140. var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
  13141. err.context = er;
  13142. throw err;
  13143. }
  13144. }
  13145. }
  13146. handler = this._events[type];
  13147. if (isUndefined(handler))
  13148. return false;
  13149. if (isFunction(handler)) {
  13150. switch (arguments.length) {
  13151. // fast cases
  13152. case 1:
  13153. handler.call(this);
  13154. break;
  13155. case 2:
  13156. handler.call(this, arguments[1]);
  13157. break;
  13158. case 3:
  13159. handler.call(this, arguments[1], arguments[2]);
  13160. break;
  13161. // slower
  13162. default:
  13163. args = Array.prototype.slice.call(arguments, 1);
  13164. handler.apply(this, args);
  13165. }
  13166. } else if (isObject(handler)) {
  13167. args = Array.prototype.slice.call(arguments, 1);
  13168. listeners = handler.slice();
  13169. len = listeners.length;
  13170. for (i = 0; i < len; i++)
  13171. listeners[i].apply(this, args);
  13172. }
  13173. return true;
  13174. };
  13175. EventEmitter.prototype.addListener = function(type, listener) {
  13176. var m;
  13177. if (!isFunction(listener))
  13178. throw TypeError('listener must be a function');
  13179. if (!this._events)
  13180. this._events = {};
  13181. // To avoid recursion in the case that type === "newListener"! Before
  13182. // adding it to the listeners, first emit "newListener".
  13183. if (this._events.newListener)
  13184. this.emit('newListener', type,
  13185. isFunction(listener.listener) ?
  13186. listener.listener : listener);
  13187. if (!this._events[type])
  13188. // Optimize the case of one listener. Don't need the extra array object.
  13189. this._events[type] = listener;
  13190. else if (isObject(this._events[type]))
  13191. // If we've already got an array, just append.
  13192. this._events[type].push(listener);
  13193. else
  13194. // Adding the second element, need to change to array.
  13195. this._events[type] = [this._events[type], listener];
  13196. // Check for listener leak
  13197. if (isObject(this._events[type]) && !this._events[type].warned) {
  13198. if (!isUndefined(this._maxListeners)) {
  13199. m = this._maxListeners;
  13200. } else {
  13201. m = EventEmitter.defaultMaxListeners;
  13202. }
  13203. if (m && m > 0 && this._events[type].length > m) {
  13204. this._events[type].warned = true;
  13205. console.error('(node) warning: possible EventEmitter memory ' +
  13206. 'leak detected. %d listeners added. ' +
  13207. 'Use emitter.setMaxListeners() to increase limit.',
  13208. this._events[type].length);
  13209. if (typeof console.trace === 'function') {
  13210. // not supported in IE 10
  13211. console.trace();
  13212. }
  13213. }
  13214. }
  13215. return this;
  13216. };
  13217. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  13218. EventEmitter.prototype.once = function(type, listener) {
  13219. if (!isFunction(listener))
  13220. throw TypeError('listener must be a function');
  13221. var fired = false;
  13222. function g() {
  13223. this.removeListener(type, g);
  13224. if (!fired) {
  13225. fired = true;
  13226. listener.apply(this, arguments);
  13227. }
  13228. }
  13229. g.listener = listener;
  13230. this.on(type, g);
  13231. return this;
  13232. };
  13233. // emits a 'removeListener' event iff the listener was removed
  13234. EventEmitter.prototype.removeListener = function(type, listener) {
  13235. var list, position, length, i;
  13236. if (!isFunction(listener))
  13237. throw TypeError('listener must be a function');
  13238. if (!this._events || !this._events[type])
  13239. return this;
  13240. list = this._events[type];
  13241. length = list.length;
  13242. position = -1;
  13243. if (list === listener ||
  13244. (isFunction(list.listener) && list.listener === listener)) {
  13245. delete this._events[type];
  13246. if (this._events.removeListener)
  13247. this.emit('removeListener', type, listener);
  13248. } else if (isObject(list)) {
  13249. for (i = length; i-- > 0;) {
  13250. if (list[i] === listener ||
  13251. (list[i].listener && list[i].listener === listener)) {
  13252. position = i;
  13253. break;
  13254. }
  13255. }
  13256. if (position < 0)
  13257. return this;
  13258. if (list.length === 1) {
  13259. list.length = 0;
  13260. delete this._events[type];
  13261. } else {
  13262. list.splice(position, 1);
  13263. }
  13264. if (this._events.removeListener)
  13265. this.emit('removeListener', type, listener);
  13266. }
  13267. return this;
  13268. };
  13269. EventEmitter.prototype.removeAllListeners = function(type) {
  13270. var key, listeners;
  13271. if (!this._events)
  13272. return this;
  13273. // not listening for removeListener, no need to emit
  13274. if (!this._events.removeListener) {
  13275. if (arguments.length === 0)
  13276. this._events = {};
  13277. else if (this._events[type])
  13278. delete this._events[type];
  13279. return this;
  13280. }
  13281. // emit removeListener for all listeners on all events
  13282. if (arguments.length === 0) {
  13283. for (key in this._events) {
  13284. if (key === 'removeListener') continue;
  13285. this.removeAllListeners(key);
  13286. }
  13287. this.removeAllListeners('removeListener');
  13288. this._events = {};
  13289. return this;
  13290. }
  13291. listeners = this._events[type];
  13292. if (isFunction(listeners)) {
  13293. this.removeListener(type, listeners);
  13294. } else if (listeners) {
  13295. // LIFO order
  13296. while (listeners.length)
  13297. this.removeListener(type, listeners[listeners.length - 1]);
  13298. }
  13299. delete this._events[type];
  13300. return this;
  13301. };
  13302. EventEmitter.prototype.listeners = function(type) {
  13303. var ret;
  13304. if (!this._events || !this._events[type])
  13305. ret = [];
  13306. else if (isFunction(this._events[type]))
  13307. ret = [this._events[type]];
  13308. else
  13309. ret = this._events[type].slice();
  13310. return ret;
  13311. };
  13312. EventEmitter.prototype.listenerCount = function(type) {
  13313. if (this._events) {
  13314. var evlistener = this._events[type];
  13315. if (isFunction(evlistener))
  13316. return 1;
  13317. else if (evlistener)
  13318. return evlistener.length;
  13319. }
  13320. return 0;
  13321. };
  13322. EventEmitter.listenerCount = function(emitter, type) {
  13323. return emitter.listenerCount(type);
  13324. };
  13325. function isFunction(arg) {
  13326. return typeof arg === 'function';
  13327. }
  13328. function isNumber(arg) {
  13329. return typeof arg === 'number';
  13330. }
  13331. function isObject(arg) {
  13332. return typeof arg === 'object' && arg !== null;
  13333. }
  13334. function isUndefined(arg) {
  13335. return arg === void 0;
  13336. }
  13337. },{}],68:[function(require,module,exports){
  13338. exports.read = function (buffer, offset, isLE, mLen, nBytes) {
  13339. var e, m
  13340. var eLen = nBytes * 8 - mLen - 1
  13341. var eMax = (1 << eLen) - 1
  13342. var eBias = eMax >> 1
  13343. var nBits = -7
  13344. var i = isLE ? (nBytes - 1) : 0
  13345. var d = isLE ? -1 : 1
  13346. var s = buffer[offset + i]
  13347. i += d
  13348. e = s & ((1 << (-nBits)) - 1)
  13349. s >>= (-nBits)
  13350. nBits += eLen
  13351. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  13352. m = e & ((1 << (-nBits)) - 1)
  13353. e >>= (-nBits)
  13354. nBits += mLen
  13355. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  13356. if (e === 0) {
  13357. e = 1 - eBias
  13358. } else if (e === eMax) {
  13359. return m ? NaN : ((s ? -1 : 1) * Infinity)
  13360. } else {
  13361. m = m + Math.pow(2, mLen)
  13362. e = e - eBias
  13363. }
  13364. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  13365. }
  13366. exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
  13367. var e, m, c
  13368. var eLen = nBytes * 8 - mLen - 1
  13369. var eMax = (1 << eLen) - 1
  13370. var eBias = eMax >> 1
  13371. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
  13372. var i = isLE ? 0 : (nBytes - 1)
  13373. var d = isLE ? 1 : -1
  13374. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
  13375. value = Math.abs(value)
  13376. if (isNaN(value) || value === Infinity) {
  13377. m = isNaN(value) ? 1 : 0
  13378. e = eMax
  13379. } else {
  13380. e = Math.floor(Math.log(value) / Math.LN2)
  13381. if (value * (c = Math.pow(2, -e)) < 1) {
  13382. e--
  13383. c *= 2
  13384. }
  13385. if (e + eBias >= 1) {
  13386. value += rt / c
  13387. } else {
  13388. value += rt * Math.pow(2, 1 - eBias)
  13389. }
  13390. if (value * c >= 2) {
  13391. e++
  13392. c /= 2
  13393. }
  13394. if (e + eBias >= eMax) {
  13395. m = 0
  13396. e = eMax
  13397. } else if (e + eBias >= 1) {
  13398. m = (value * c - 1) * Math.pow(2, mLen)
  13399. e = e + eBias
  13400. } else {
  13401. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
  13402. e = 0
  13403. }
  13404. }
  13405. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  13406. e = (e << mLen) | m
  13407. eLen += mLen
  13408. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  13409. buffer[offset + i - d] |= s * 128
  13410. }
  13411. },{}],69:[function(require,module,exports){
  13412. arguments[4][34][0].apply(exports,arguments)
  13413. },{"dup":34}],70:[function(require,module,exports){
  13414. /*!
  13415. * Determine if an object is a Buffer
  13416. *
  13417. * @author Feross Aboukhadijeh <[email protected]> <http://feross.org>
  13418. * @license MIT
  13419. */
  13420. // The _isBuffer check is for Safari 5-7 support, because it's missing
  13421. // Object.prototype.constructor. Remove this eventually
  13422. module.exports = function (obj) {
  13423. return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer)
  13424. }
  13425. function isBuffer (obj) {
  13426. return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
  13427. }
  13428. // For Node v0.10 support. Remove this eventually.
  13429. function isSlowBuffer (obj) {
  13430. return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0))
  13431. }
  13432. },{}],71:[function(require,module,exports){
  13433. var toString = {}.toString;
  13434. module.exports = Array.isArray || function (arr) {
  13435. return toString.call(arr) == '[object Array]';
  13436. };
  13437. },{}],72:[function(require,module,exports){
  13438. (function (process){
  13439. 'use strict';
  13440. if (!process.version ||
  13441. process.version.indexOf('v0.') === 0 ||
  13442. process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) {
  13443. module.exports = nextTick;
  13444. } else {
  13445. module.exports = process.nextTick;
  13446. }
  13447. function nextTick(fn, arg1, arg2, arg3) {
  13448. if (typeof fn !== 'function') {
  13449. throw new TypeError('"callback" argument must be a function');
  13450. }
  13451. var len = arguments.length;
  13452. var args, i;
  13453. switch (len) {
  13454. case 0:
  13455. case 1:
  13456. return process.nextTick(fn);
  13457. case 2:
  13458. return process.nextTick(function afterTickOne() {
  13459. fn.call(null, arg1);
  13460. });
  13461. case 3:
  13462. return process.nextTick(function afterTickTwo() {
  13463. fn.call(null, arg1, arg2);
  13464. });
  13465. case 4:
  13466. return process.nextTick(function afterTickThree() {
  13467. fn.call(null, arg1, arg2, arg3);
  13468. });
  13469. default:
  13470. args = new Array(len - 1);
  13471. i = 0;
  13472. while (i < args.length) {
  13473. args[i++] = arguments[i];
  13474. }
  13475. return process.nextTick(function afterTick() {
  13476. fn.apply(null, args);
  13477. });
  13478. }
  13479. }
  13480. }).call(this,require('_process'))
  13481. },{"_process":73}],73:[function(require,module,exports){
  13482. // shim for using process in browser
  13483. var process = module.exports = {};
  13484. // cached from whatever global is present so that test runners that stub it
  13485. // don't break things. But we need to wrap it in a try catch in case it is
  13486. // wrapped in strict mode code which doesn't define any globals. It's inside a
  13487. // function because try/catches deoptimize in certain engines.
  13488. var cachedSetTimeout;
  13489. var cachedClearTimeout;
  13490. function defaultSetTimout() {
  13491. throw new Error('setTimeout has not been defined');
  13492. }
  13493. function defaultClearTimeout () {
  13494. throw new Error('clearTimeout has not been defined');
  13495. }
  13496. (function () {
  13497. try {
  13498. if (typeof setTimeout === 'function') {
  13499. cachedSetTimeout = setTimeout;
  13500. } else {
  13501. cachedSetTimeout = defaultSetTimout;
  13502. }
  13503. } catch (e) {
  13504. cachedSetTimeout = defaultSetTimout;
  13505. }
  13506. try {
  13507. if (typeof clearTimeout === 'function') {
  13508. cachedClearTimeout = clearTimeout;
  13509. } else {
  13510. cachedClearTimeout = defaultClearTimeout;
  13511. }
  13512. } catch (e) {
  13513. cachedClearTimeout = defaultClearTimeout;
  13514. }
  13515. } ())
  13516. function runTimeout(fun) {
  13517. if (cachedSetTimeout === setTimeout) {
  13518. //normal enviroments in sane situations
  13519. return setTimeout(fun, 0);
  13520. }
  13521. // if setTimeout wasn't available but was latter defined
  13522. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  13523. cachedSetTimeout = setTimeout;
  13524. return setTimeout(fun, 0);
  13525. }
  13526. try {
  13527. // when when somebody has screwed with setTimeout but no I.E. maddness
  13528. return cachedSetTimeout(fun, 0);
  13529. } catch(e){
  13530. try {
  13531. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  13532. return cachedSetTimeout.call(null, fun, 0);
  13533. } catch(e){
  13534. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  13535. return cachedSetTimeout.call(this, fun, 0);
  13536. }
  13537. }
  13538. }
  13539. function runClearTimeout(marker) {
  13540. if (cachedClearTimeout === clearTimeout) {
  13541. //normal enviroments in sane situations
  13542. return clearTimeout(marker);
  13543. }
  13544. // if clearTimeout wasn't available but was latter defined
  13545. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  13546. cachedClearTimeout = clearTimeout;
  13547. return clearTimeout(marker);
  13548. }
  13549. try {
  13550. // when when somebody has screwed with setTimeout but no I.E. maddness
  13551. return cachedClearTimeout(marker);
  13552. } catch (e){
  13553. try {
  13554. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  13555. return cachedClearTimeout.call(null, marker);
  13556. } catch (e){
  13557. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  13558. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  13559. return cachedClearTimeout.call(this, marker);
  13560. }
  13561. }
  13562. }
  13563. var queue = [];
  13564. var draining = false;
  13565. var currentQueue;
  13566. var queueIndex = -1;
  13567. function cleanUpNextTick() {
  13568. if (!draining || !currentQueue) {
  13569. return;
  13570. }
  13571. draining = false;
  13572. if (currentQueue.length) {
  13573. queue = currentQueue.concat(queue);
  13574. } else {
  13575. queueIndex = -1;
  13576. }
  13577. if (queue.length) {
  13578. drainQueue();
  13579. }
  13580. }
  13581. function drainQueue() {
  13582. if (draining) {
  13583. return;
  13584. }
  13585. var timeout = runTimeout(cleanUpNextTick);
  13586. draining = true;
  13587. var len = queue.length;
  13588. while(len) {
  13589. currentQueue = queue;
  13590. queue = [];
  13591. while (++queueIndex < len) {
  13592. if (currentQueue) {
  13593. currentQueue[queueIndex].run();
  13594. }
  13595. }
  13596. queueIndex = -1;
  13597. len = queue.length;
  13598. }
  13599. currentQueue = null;
  13600. draining = false;
  13601. runClearTimeout(timeout);
  13602. }
  13603. process.nextTick = function (fun) {
  13604. var args = new Array(arguments.length - 1);
  13605. if (arguments.length > 1) {
  13606. for (var i = 1; i < arguments.length; i++) {
  13607. args[i - 1] = arguments[i];
  13608. }
  13609. }
  13610. queue.push(new Item(fun, args));
  13611. if (queue.length === 1 && !draining) {
  13612. runTimeout(drainQueue);
  13613. }
  13614. };
  13615. // v8 likes predictible objects
  13616. function Item(fun, array) {
  13617. this.fun = fun;
  13618. this.array = array;
  13619. }
  13620. Item.prototype.run = function () {
  13621. this.fun.apply(null, this.array);
  13622. };
  13623. process.title = 'browser';
  13624. process.browser = true;
  13625. process.env = {};
  13626. process.argv = [];
  13627. process.version = ''; // empty string to avoid regexp issues
  13628. process.versions = {};
  13629. function noop() {}
  13630. process.on = noop;
  13631. process.addListener = noop;
  13632. process.once = noop;
  13633. process.off = noop;
  13634. process.removeListener = noop;
  13635. process.removeAllListeners = noop;
  13636. process.emit = noop;
  13637. process.binding = function (name) {
  13638. throw new Error('process.binding is not supported');
  13639. };
  13640. process.cwd = function () { return '/' };
  13641. process.chdir = function (dir) {
  13642. throw new Error('process.chdir is not supported');
  13643. };
  13644. process.umask = function() { return 0; };
  13645. },{}],74:[function(require,module,exports){
  13646. module.exports = require("./lib/_stream_duplex.js")
  13647. },{"./lib/_stream_duplex.js":75}],75:[function(require,module,exports){
  13648. // a duplex stream is just a stream that is both readable and writable.
  13649. // Since JS doesn't have multiple prototypal inheritance, this class
  13650. // prototypally inherits from Readable, and then parasitically from
  13651. // Writable.
  13652. 'use strict';
  13653. /*<replacement>*/
  13654. var objectKeys = Object.keys || function (obj) {
  13655. var keys = [];
  13656. for (var key in obj) {
  13657. keys.push(key);
  13658. }return keys;
  13659. };
  13660. /*</replacement>*/
  13661. module.exports = Duplex;
  13662. /*<replacement>*/
  13663. var processNextTick = require('process-nextick-args');
  13664. /*</replacement>*/
  13665. /*<replacement>*/
  13666. var util = require('core-util-is');
  13667. util.inherits = require('inherits');
  13668. /*</replacement>*/
  13669. var Readable = require('./_stream_readable');
  13670. var Writable = require('./_stream_writable');
  13671. util.inherits(Duplex, Readable);
  13672. var keys = objectKeys(Writable.prototype);
  13673. for (var v = 0; v < keys.length; v++) {
  13674. var method = keys[v];
  13675. if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
  13676. }
  13677. function Duplex(options) {
  13678. if (!(this instanceof Duplex)) return new Duplex(options);
  13679. Readable.call(this, options);
  13680. Writable.call(this, options);
  13681. if (options && options.readable === false) this.readable = false;
  13682. if (options && options.writable === false) this.writable = false;
  13683. this.allowHalfOpen = true;
  13684. if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
  13685. this.once('end', onend);
  13686. }
  13687. // the no-half-open enforcer
  13688. function onend() {
  13689. // if we allow half-open state, or if the writable side ended,
  13690. // then we're ok.
  13691. if (this.allowHalfOpen || this._writableState.ended) return;
  13692. // no more data can be written.
  13693. // But allow more writes to happen in this tick.
  13694. processNextTick(onEndNT, this);
  13695. }
  13696. function onEndNT(self) {
  13697. self.end();
  13698. }
  13699. function forEach(xs, f) {
  13700. for (var i = 0, l = xs.length; i < l; i++) {
  13701. f(xs[i], i);
  13702. }
  13703. }
  13704. },{"./_stream_readable":77,"./_stream_writable":79,"core-util-is":66,"inherits":69,"process-nextick-args":72}],76:[function(require,module,exports){
  13705. // a passthrough stream.
  13706. // basically just the most minimal sort of Transform stream.
  13707. // Every written chunk gets output as-is.
  13708. 'use strict';
  13709. module.exports = PassThrough;
  13710. var Transform = require('./_stream_transform');
  13711. /*<replacement>*/
  13712. var util = require('core-util-is');
  13713. util.inherits = require('inherits');
  13714. /*</replacement>*/
  13715. util.inherits(PassThrough, Transform);
  13716. function PassThrough(options) {
  13717. if (!(this instanceof PassThrough)) return new PassThrough(options);
  13718. Transform.call(this, options);
  13719. }
  13720. PassThrough.prototype._transform = function (chunk, encoding, cb) {
  13721. cb(null, chunk);
  13722. };
  13723. },{"./_stream_transform":78,"core-util-is":66,"inherits":69}],77:[function(require,module,exports){
  13724. (function (process){
  13725. 'use strict';
  13726. module.exports = Readable;
  13727. /*<replacement>*/
  13728. var processNextTick = require('process-nextick-args');
  13729. /*</replacement>*/
  13730. /*<replacement>*/
  13731. var isArray = require('isarray');
  13732. /*</replacement>*/
  13733. /*<replacement>*/
  13734. var Duplex;
  13735. /*</replacement>*/
  13736. Readable.ReadableState = ReadableState;
  13737. /*<replacement>*/
  13738. var EE = require('events').EventEmitter;
  13739. var EElistenerCount = function (emitter, type) {
  13740. return emitter.listeners(type).length;
  13741. };
  13742. /*</replacement>*/
  13743. /*<replacement>*/
  13744. var Stream;
  13745. (function () {
  13746. try {
  13747. Stream = require('st' + 'ream');
  13748. } catch (_) {} finally {
  13749. if (!Stream) Stream = require('events').EventEmitter;
  13750. }
  13751. })();
  13752. /*</replacement>*/
  13753. var Buffer = require('buffer').Buffer;
  13754. /*<replacement>*/
  13755. var bufferShim = require('buffer-shims');
  13756. /*</replacement>*/
  13757. /*<replacement>*/
  13758. var util = require('core-util-is');
  13759. util.inherits = require('inherits');
  13760. /*</replacement>*/
  13761. /*<replacement>*/
  13762. var debugUtil = require('util');
  13763. var debug = void 0;
  13764. if (debugUtil && debugUtil.debuglog) {
  13765. debug = debugUtil.debuglog('stream');
  13766. } else {
  13767. debug = function () {};
  13768. }
  13769. /*</replacement>*/
  13770. var BufferList = require('./internal/streams/BufferList');
  13771. var StringDecoder;
  13772. util.inherits(Readable, Stream);
  13773. function prependListener(emitter, event, fn) {
  13774. // Sadly this is not cacheable as some libraries bundle their own
  13775. // event emitter implementation with them.
  13776. if (typeof emitter.prependListener === 'function') {
  13777. return emitter.prependListener(event, fn);
  13778. } else {
  13779. // This is a hack to make sure that our error handler is attached before any
  13780. // userland ones. NEVER DO THIS. This is here only because this code needs
  13781. // to continue to work with older versions of Node.js that do not include
  13782. // the prependListener() method. The goal is to eventually remove this hack.
  13783. if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
  13784. }
  13785. }
  13786. function ReadableState(options, stream) {
  13787. Duplex = Duplex || require('./_stream_duplex');
  13788. options = options || {};
  13789. // object stream flag. Used to make read(n) ignore n and to
  13790. // make all the buffer merging and length checks go away
  13791. this.objectMode = !!options.objectMode;
  13792. if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
  13793. // the point at which it stops calling _read() to fill the buffer
  13794. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  13795. var hwm = options.highWaterMark;
  13796. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  13797. this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
  13798. // cast to ints.
  13799. this.highWaterMark = ~ ~this.highWaterMark;
  13800. // A linked list is used to store data chunks instead of an array because the
  13801. // linked list can remove elements from the beginning faster than
  13802. // array.shift()
  13803. this.buffer = new BufferList();
  13804. this.length = 0;
  13805. this.pipes = null;
  13806. this.pipesCount = 0;
  13807. this.flowing = null;
  13808. this.ended = false;
  13809. this.endEmitted = false;
  13810. this.reading = false;
  13811. // a flag to be able to tell if the onwrite cb is called immediately,
  13812. // or on a later tick. We set this to true at first, because any
  13813. // actions that shouldn't happen until "later" should generally also
  13814. // not happen before the first write call.
  13815. this.sync = true;
  13816. // whenever we return null, then we set a flag to say
  13817. // that we're awaiting a 'readable' event emission.
  13818. this.needReadable = false;
  13819. this.emittedReadable = false;
  13820. this.readableListening = false;
  13821. this.resumeScheduled = false;
  13822. // Crypto is kind of old and crusty. Historically, its default string
  13823. // encoding is 'binary' so we have to make this configurable.
  13824. // Everything else in the universe uses 'utf8', though.
  13825. this.defaultEncoding = options.defaultEncoding || 'utf8';
  13826. // when piping, we only care about 'readable' events that happen
  13827. // after read()ing all the bytes and not getting any pushback.
  13828. this.ranOut = false;
  13829. // the number of writers that are awaiting a drain event in .pipe()s
  13830. this.awaitDrain = 0;
  13831. // if true, a maybeReadMore has been scheduled
  13832. this.readingMore = false;
  13833. this.decoder = null;
  13834. this.encoding = null;
  13835. if (options.encoding) {
  13836. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  13837. this.decoder = new StringDecoder(options.encoding);
  13838. this.encoding = options.encoding;
  13839. }
  13840. }
  13841. function Readable(options) {
  13842. Duplex = Duplex || require('./_stream_duplex');
  13843. if (!(this instanceof Readable)) return new Readable(options);
  13844. this._readableState = new ReadableState(options, this);
  13845. // legacy
  13846. this.readable = true;
  13847. if (options && typeof options.read === 'function') this._read = options.read;
  13848. Stream.call(this);
  13849. }
  13850. // Manually shove something into the read() buffer.
  13851. // This returns true if the highWaterMark has not been hit yet,
  13852. // similar to how Writable.write() returns true if you should
  13853. // write() some more.
  13854. Readable.prototype.push = function (chunk, encoding) {
  13855. var state = this._readableState;
  13856. if (!state.objectMode && typeof chunk === 'string') {
  13857. encoding = encoding || state.defaultEncoding;
  13858. if (encoding !== state.encoding) {
  13859. chunk = bufferShim.from(chunk, encoding);
  13860. encoding = '';
  13861. }
  13862. }
  13863. return readableAddChunk(this, state, chunk, encoding, false);
  13864. };
  13865. // Unshift should *always* be something directly out of read()
  13866. Readable.prototype.unshift = function (chunk) {
  13867. var state = this._readableState;
  13868. return readableAddChunk(this, state, chunk, '', true);
  13869. };
  13870. Readable.prototype.isPaused = function () {
  13871. return this._readableState.flowing === false;
  13872. };
  13873. function readableAddChunk(stream, state, chunk, encoding, addToFront) {
  13874. var er = chunkInvalid(state, chunk);
  13875. if (er) {
  13876. stream.emit('error', er);
  13877. } else if (chunk === null) {
  13878. state.reading = false;
  13879. onEofChunk(stream, state);
  13880. } else if (state.objectMode || chunk && chunk.length > 0) {
  13881. if (state.ended && !addToFront) {
  13882. var e = new Error('stream.push() after EOF');
  13883. stream.emit('error', e);
  13884. } else if (state.endEmitted && addToFront) {
  13885. var _e = new Error('stream.unshift() after end event');
  13886. stream.emit('error', _e);
  13887. } else {
  13888. var skipAdd;
  13889. if (state.decoder && !addToFront && !encoding) {
  13890. chunk = state.decoder.write(chunk);
  13891. skipAdd = !state.objectMode && chunk.length === 0;
  13892. }
  13893. if (!addToFront) state.reading = false;
  13894. // Don't add to the buffer if we've decoded to an empty string chunk and
  13895. // we're not in object mode
  13896. if (!skipAdd) {
  13897. // if we want the data now, just emit it.
  13898. if (state.flowing && state.length === 0 && !state.sync) {
  13899. stream.emit('data', chunk);
  13900. stream.read(0);
  13901. } else {
  13902. // update the buffer info.
  13903. state.length += state.objectMode ? 1 : chunk.length;
  13904. if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
  13905. if (state.needReadable) emitReadable(stream);
  13906. }
  13907. }
  13908. maybeReadMore(stream, state);
  13909. }
  13910. } else if (!addToFront) {
  13911. state.reading = false;
  13912. }
  13913. return needMoreData(state);
  13914. }
  13915. // if it's past the high water mark, we can push in some more.
  13916. // Also, if we have no data yet, we can stand some
  13917. // more bytes. This is to work around cases where hwm=0,
  13918. // such as the repl. Also, if the push() triggered a
  13919. // readable event, and the user called read(largeNumber) such that
  13920. // needReadable was set, then we ought to push more, so that another
  13921. // 'readable' event will be triggered.
  13922. function needMoreData(state) {
  13923. return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
  13924. }
  13925. // backwards compatibility.
  13926. Readable.prototype.setEncoding = function (enc) {
  13927. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  13928. this._readableState.decoder = new StringDecoder(enc);
  13929. this._readableState.encoding = enc;
  13930. return this;
  13931. };
  13932. // Don't raise the hwm > 8MB
  13933. var MAX_HWM = 0x800000;
  13934. function computeNewHighWaterMark(n) {
  13935. if (n >= MAX_HWM) {
  13936. n = MAX_HWM;
  13937. } else {
  13938. // Get the next highest power of 2 to prevent increasing hwm excessively in
  13939. // tiny amounts
  13940. n--;
  13941. n |= n >>> 1;
  13942. n |= n >>> 2;
  13943. n |= n >>> 4;
  13944. n |= n >>> 8;
  13945. n |= n >>> 16;
  13946. n++;
  13947. }
  13948. return n;
  13949. }
  13950. // This function is designed to be inlinable, so please take care when making
  13951. // changes to the function body.
  13952. function howMuchToRead(n, state) {
  13953. if (n <= 0 || state.length === 0 && state.ended) return 0;
  13954. if (state.objectMode) return 1;
  13955. if (n !== n) {
  13956. // Only flow one buffer at a time
  13957. if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
  13958. }
  13959. // If we're asking for more than the current hwm, then raise the hwm.
  13960. if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
  13961. if (n <= state.length) return n;
  13962. // Don't have enough
  13963. if (!state.ended) {
  13964. state.needReadable = true;
  13965. return 0;
  13966. }
  13967. return state.length;
  13968. }
  13969. // you can override either this method, or the async _read(n) below.
  13970. Readable.prototype.read = function (n) {
  13971. debug('read', n);
  13972. n = parseInt(n, 10);
  13973. var state = this._readableState;
  13974. var nOrig = n;
  13975. if (n !== 0) state.emittedReadable = false;
  13976. // if we're doing read(0) to trigger a readable event, but we
  13977. // already have a bunch of data in the buffer, then just trigger
  13978. // the 'readable' event and move on.
  13979. if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
  13980. debug('read: emitReadable', state.length, state.ended);
  13981. if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
  13982. return null;
  13983. }
  13984. n = howMuchToRead(n, state);
  13985. // if we've ended, and we're now clear, then finish it up.
  13986. if (n === 0 && state.ended) {
  13987. if (state.length === 0) endReadable(this);
  13988. return null;
  13989. }
  13990. // All the actual chunk generation logic needs to be
  13991. // *below* the call to _read. The reason is that in certain
  13992. // synthetic stream cases, such as passthrough streams, _read
  13993. // may be a completely synchronous operation which may change
  13994. // the state of the read buffer, providing enough data when
  13995. // before there was *not* enough.
  13996. //
  13997. // So, the steps are:
  13998. // 1. Figure out what the state of things will be after we do
  13999. // a read from the buffer.
  14000. //
  14001. // 2. If that resulting state will trigger a _read, then call _read.
  14002. // Note that this may be asynchronous, or synchronous. Yes, it is
  14003. // deeply ugly to write APIs this way, but that still doesn't mean
  14004. // that the Readable class should behave improperly, as streams are
  14005. // designed to be sync/async agnostic.
  14006. // Take note if the _read call is sync or async (ie, if the read call
  14007. // has returned yet), so that we know whether or not it's safe to emit
  14008. // 'readable' etc.
  14009. //
  14010. // 3. Actually pull the requested chunks out of the buffer and return.
  14011. // if we need a readable event, then we need to do some reading.
  14012. var doRead = state.needReadable;
  14013. debug('need readable', doRead);
  14014. // if we currently have less than the highWaterMark, then also read some
  14015. if (state.length === 0 || state.length - n < state.highWaterMark) {
  14016. doRead = true;
  14017. debug('length less than watermark', doRead);
  14018. }
  14019. // however, if we've ended, then there's no point, and if we're already
  14020. // reading, then it's unnecessary.
  14021. if (state.ended || state.reading) {
  14022. doRead = false;
  14023. debug('reading or ended', doRead);
  14024. } else if (doRead) {
  14025. debug('do read');
  14026. state.reading = true;
  14027. state.sync = true;
  14028. // if the length is currently zero, then we *need* a readable event.
  14029. if (state.length === 0) state.needReadable = true;
  14030. // call internal read method
  14031. this._read(state.highWaterMark);
  14032. state.sync = false;
  14033. // If _read pushed data synchronously, then `reading` will be false,
  14034. // and we need to re-evaluate how much data we can return to the user.
  14035. if (!state.reading) n = howMuchToRead(nOrig, state);
  14036. }
  14037. var ret;
  14038. if (n > 0) ret = fromList(n, state);else ret = null;
  14039. if (ret === null) {
  14040. state.needReadable = true;
  14041. n = 0;
  14042. } else {
  14043. state.length -= n;
  14044. }
  14045. if (state.length === 0) {
  14046. // If we have nothing in the buffer, then we want to know
  14047. // as soon as we *do* get something into the buffer.
  14048. if (!state.ended) state.needReadable = true;
  14049. // If we tried to read() past the EOF, then emit end on the next tick.
  14050. if (nOrig !== n && state.ended) endReadable(this);
  14051. }
  14052. if (ret !== null) this.emit('data', ret);
  14053. return ret;
  14054. };
  14055. function chunkInvalid(state, chunk) {
  14056. var er = null;
  14057. if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) {
  14058. er = new TypeError('Invalid non-string/buffer chunk');
  14059. }
  14060. return er;
  14061. }
  14062. function onEofChunk(stream, state) {
  14063. if (state.ended) return;
  14064. if (state.decoder) {
  14065. var chunk = state.decoder.end();
  14066. if (chunk && chunk.length) {
  14067. state.buffer.push(chunk);
  14068. state.length += state.objectMode ? 1 : chunk.length;
  14069. }
  14070. }
  14071. state.ended = true;
  14072. // emit 'readable' now to make sure it gets picked up.
  14073. emitReadable(stream);
  14074. }
  14075. // Don't emit readable right away in sync mode, because this can trigger
  14076. // another read() call => stack overflow. This way, it might trigger
  14077. // a nextTick recursion warning, but that's not so bad.
  14078. function emitReadable(stream) {
  14079. var state = stream._readableState;
  14080. state.needReadable = false;
  14081. if (!state.emittedReadable) {
  14082. debug('emitReadable', state.flowing);
  14083. state.emittedReadable = true;
  14084. if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream);
  14085. }
  14086. }
  14087. function emitReadable_(stream) {
  14088. debug('emit readable');
  14089. stream.emit('readable');
  14090. flow(stream);
  14091. }
  14092. // at this point, the user has presumably seen the 'readable' event,
  14093. // and called read() to consume some data. that may have triggered
  14094. // in turn another _read(n) call, in which case reading = true if
  14095. // it's in progress.
  14096. // However, if we're not ended, or reading, and the length < hwm,
  14097. // then go ahead and try to read some more preemptively.
  14098. function maybeReadMore(stream, state) {
  14099. if (!state.readingMore) {
  14100. state.readingMore = true;
  14101. processNextTick(maybeReadMore_, stream, state);
  14102. }
  14103. }
  14104. function maybeReadMore_(stream, state) {
  14105. var len = state.length;
  14106. while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
  14107. debug('maybeReadMore read 0');
  14108. stream.read(0);
  14109. if (len === state.length)
  14110. // didn't get any data, stop spinning.
  14111. break;else len = state.length;
  14112. }
  14113. state.readingMore = false;
  14114. }
  14115. // abstract method. to be overridden in specific implementation classes.
  14116. // call cb(er, data) where data is <= n in length.
  14117. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  14118. // arbitrary, and perhaps not very meaningful.
  14119. Readable.prototype._read = function (n) {
  14120. this.emit('error', new Error('_read() is not implemented'));
  14121. };
  14122. Readable.prototype.pipe = function (dest, pipeOpts) {
  14123. var src = this;
  14124. var state = this._readableState;
  14125. switch (state.pipesCount) {
  14126. case 0:
  14127. state.pipes = dest;
  14128. break;
  14129. case 1:
  14130. state.pipes = [state.pipes, dest];
  14131. break;
  14132. default:
  14133. state.pipes.push(dest);
  14134. break;
  14135. }
  14136. state.pipesCount += 1;
  14137. debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
  14138. var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
  14139. var endFn = doEnd ? onend : cleanup;
  14140. if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn);
  14141. dest.on('unpipe', onunpipe);
  14142. function onunpipe(readable) {
  14143. debug('onunpipe');
  14144. if (readable === src) {
  14145. cleanup();
  14146. }
  14147. }
  14148. function onend() {
  14149. debug('onend');
  14150. dest.end();
  14151. }
  14152. // when the dest drains, it reduces the awaitDrain counter
  14153. // on the source. This would be more elegant with a .once()
  14154. // handler in flow(), but adding and removing repeatedly is
  14155. // too slow.
  14156. var ondrain = pipeOnDrain(src);
  14157. dest.on('drain', ondrain);
  14158. var cleanedUp = false;
  14159. function cleanup() {
  14160. debug('cleanup');
  14161. // cleanup event handlers once the pipe is broken
  14162. dest.removeListener('close', onclose);
  14163. dest.removeListener('finish', onfinish);
  14164. dest.removeListener('drain', ondrain);
  14165. dest.removeListener('error', onerror);
  14166. dest.removeListener('unpipe', onunpipe);
  14167. src.removeListener('end', onend);
  14168. src.removeListener('end', cleanup);
  14169. src.removeListener('data', ondata);
  14170. cleanedUp = true;
  14171. // if the reader is waiting for a drain event from this
  14172. // specific writer, then it would cause it to never start
  14173. // flowing again.
  14174. // So, if this is awaiting a drain, then we just call it now.
  14175. // If we don't know, then assume that we are waiting for one.
  14176. if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
  14177. }
  14178. // If the user pushes more data while we're writing to dest then we'll end up
  14179. // in ondata again. However, we only want to increase awaitDrain once because
  14180. // dest will only emit one 'drain' event for the multiple writes.
  14181. // => Introduce a guard on increasing awaitDrain.
  14182. var increasedAwaitDrain = false;
  14183. src.on('data', ondata);
  14184. function ondata(chunk) {
  14185. debug('ondata');
  14186. increasedAwaitDrain = false;
  14187. var ret = dest.write(chunk);
  14188. if (false === ret && !increasedAwaitDrain) {
  14189. // If the user unpiped during `dest.write()`, it is possible
  14190. // to get stuck in a permanently paused state if that write
  14191. // also returned false.
  14192. // => Check whether `dest` is still a piping destination.
  14193. if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
  14194. debug('false write response, pause', src._readableState.awaitDrain);
  14195. src._readableState.awaitDrain++;
  14196. increasedAwaitDrain = true;
  14197. }
  14198. src.pause();
  14199. }
  14200. }
  14201. // if the dest has an error, then stop piping into it.
  14202. // however, don't suppress the throwing behavior for this.
  14203. function onerror(er) {
  14204. debug('onerror', er);
  14205. unpipe();
  14206. dest.removeListener('error', onerror);
  14207. if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
  14208. }
  14209. // Make sure our error handler is attached before userland ones.
  14210. prependListener(dest, 'error', onerror);
  14211. // Both close and finish should trigger unpipe, but only once.
  14212. function onclose() {
  14213. dest.removeListener('finish', onfinish);
  14214. unpipe();
  14215. }
  14216. dest.once('close', onclose);
  14217. function onfinish() {
  14218. debug('onfinish');
  14219. dest.removeListener('close', onclose);
  14220. unpipe();
  14221. }
  14222. dest.once('finish', onfinish);
  14223. function unpipe() {
  14224. debug('unpipe');
  14225. src.unpipe(dest);
  14226. }
  14227. // tell the dest that it's being piped to
  14228. dest.emit('pipe', src);
  14229. // start the flow if it hasn't been started already.
  14230. if (!state.flowing) {
  14231. debug('pipe resume');
  14232. src.resume();
  14233. }
  14234. return dest;
  14235. };
  14236. function pipeOnDrain(src) {
  14237. return function () {
  14238. var state = src._readableState;
  14239. debug('pipeOnDrain', state.awaitDrain);
  14240. if (state.awaitDrain) state.awaitDrain--;
  14241. if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
  14242. state.flowing = true;
  14243. flow(src);
  14244. }
  14245. };
  14246. }
  14247. Readable.prototype.unpipe = function (dest) {
  14248. var state = this._readableState;
  14249. // if we're not piping anywhere, then do nothing.
  14250. if (state.pipesCount === 0) return this;
  14251. // just one destination. most common case.
  14252. if (state.pipesCount === 1) {
  14253. // passed in one, but it's not the right one.
  14254. if (dest && dest !== state.pipes) return this;
  14255. if (!dest) dest = state.pipes;
  14256. // got a match.
  14257. state.pipes = null;
  14258. state.pipesCount = 0;
  14259. state.flowing = false;
  14260. if (dest) dest.emit('unpipe', this);
  14261. return this;
  14262. }
  14263. // slow case. multiple pipe destinations.
  14264. if (!dest) {
  14265. // remove all.
  14266. var dests = state.pipes;
  14267. var len = state.pipesCount;
  14268. state.pipes = null;
  14269. state.pipesCount = 0;
  14270. state.flowing = false;
  14271. for (var i = 0; i < len; i++) {
  14272. dests[i].emit('unpipe', this);
  14273. }return this;
  14274. }
  14275. // try to find the right one.
  14276. var index = indexOf(state.pipes, dest);
  14277. if (index === -1) return this;
  14278. state.pipes.splice(index, 1);
  14279. state.pipesCount -= 1;
  14280. if (state.pipesCount === 1) state.pipes = state.pipes[0];
  14281. dest.emit('unpipe', this);
  14282. return this;
  14283. };
  14284. // set up data events if they are asked for
  14285. // Ensure readable listeners eventually get something
  14286. Readable.prototype.on = function (ev, fn) {
  14287. var res = Stream.prototype.on.call(this, ev, fn);
  14288. if (ev === 'data') {
  14289. // Start flowing on next tick if stream isn't explicitly paused
  14290. if (this._readableState.flowing !== false) this.resume();
  14291. } else if (ev === 'readable') {
  14292. var state = this._readableState;
  14293. if (!state.endEmitted && !state.readableListening) {
  14294. state.readableListening = state.needReadable = true;
  14295. state.emittedReadable = false;
  14296. if (!state.reading) {
  14297. processNextTick(nReadingNextTick, this);
  14298. } else if (state.length) {
  14299. emitReadable(this, state);
  14300. }
  14301. }
  14302. }
  14303. return res;
  14304. };
  14305. Readable.prototype.addListener = Readable.prototype.on;
  14306. function nReadingNextTick(self) {
  14307. debug('readable nexttick read 0');
  14308. self.read(0);
  14309. }
  14310. // pause() and resume() are remnants of the legacy readable stream API
  14311. // If the user uses them, then switch into old mode.
  14312. Readable.prototype.resume = function () {
  14313. var state = this._readableState;
  14314. if (!state.flowing) {
  14315. debug('resume');
  14316. state.flowing = true;
  14317. resume(this, state);
  14318. }
  14319. return this;
  14320. };
  14321. function resume(stream, state) {
  14322. if (!state.resumeScheduled) {
  14323. state.resumeScheduled = true;
  14324. processNextTick(resume_, stream, state);
  14325. }
  14326. }
  14327. function resume_(stream, state) {
  14328. if (!state.reading) {
  14329. debug('resume read 0');
  14330. stream.read(0);
  14331. }
  14332. state.resumeScheduled = false;
  14333. state.awaitDrain = 0;
  14334. stream.emit('resume');
  14335. flow(stream);
  14336. if (state.flowing && !state.reading) stream.read(0);
  14337. }
  14338. Readable.prototype.pause = function () {
  14339. debug('call pause flowing=%j', this._readableState.flowing);
  14340. if (false !== this._readableState.flowing) {
  14341. debug('pause');
  14342. this._readableState.flowing = false;
  14343. this.emit('pause');
  14344. }
  14345. return this;
  14346. };
  14347. function flow(stream) {
  14348. var state = stream._readableState;
  14349. debug('flow', state.flowing);
  14350. while (state.flowing && stream.read() !== null) {}
  14351. }
  14352. // wrap an old-style stream as the async data source.
  14353. // This is *not* part of the readable stream interface.
  14354. // It is an ugly unfortunate mess of history.
  14355. Readable.prototype.wrap = function (stream) {
  14356. var state = this._readableState;
  14357. var paused = false;
  14358. var self = this;
  14359. stream.on('end', function () {
  14360. debug('wrapped end');
  14361. if (state.decoder && !state.ended) {
  14362. var chunk = state.decoder.end();
  14363. if (chunk && chunk.length) self.push(chunk);
  14364. }
  14365. self.push(null);
  14366. });
  14367. stream.on('data', function (chunk) {
  14368. debug('wrapped data');
  14369. if (state.decoder) chunk = state.decoder.write(chunk);
  14370. // don't skip over falsy values in objectMode
  14371. if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
  14372. var ret = self.push(chunk);
  14373. if (!ret) {
  14374. paused = true;
  14375. stream.pause();
  14376. }
  14377. });
  14378. // proxy all the other methods.
  14379. // important when wrapping filters and duplexes.
  14380. for (var i in stream) {
  14381. if (this[i] === undefined && typeof stream[i] === 'function') {
  14382. this[i] = function (method) {
  14383. return function () {
  14384. return stream[method].apply(stream, arguments);
  14385. };
  14386. }(i);
  14387. }
  14388. }
  14389. // proxy certain important events.
  14390. var events = ['error', 'close', 'destroy', 'pause', 'resume'];
  14391. forEach(events, function (ev) {
  14392. stream.on(ev, self.emit.bind(self, ev));
  14393. });
  14394. // when we try to consume some more bytes, simply unpause the
  14395. // underlying stream.
  14396. self._read = function (n) {
  14397. debug('wrapped _read', n);
  14398. if (paused) {
  14399. paused = false;
  14400. stream.resume();
  14401. }
  14402. };
  14403. return self;
  14404. };
  14405. // exposed for testing purposes only.
  14406. Readable._fromList = fromList;
  14407. // Pluck off n bytes from an array of buffers.
  14408. // Length is the combined lengths of all the buffers in the list.
  14409. // This function is designed to be inlinable, so please take care when making
  14410. // changes to the function body.
  14411. function fromList(n, state) {
  14412. // nothing buffered
  14413. if (state.length === 0) return null;
  14414. var ret;
  14415. if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
  14416. // read it all, truncate the list
  14417. if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length);
  14418. state.buffer.clear();
  14419. } else {
  14420. // read part of list
  14421. ret = fromListPartial(n, state.buffer, state.decoder);
  14422. }
  14423. return ret;
  14424. }
  14425. // Extracts only enough buffered data to satisfy the amount requested.
  14426. // This function is designed to be inlinable, so please take care when making
  14427. // changes to the function body.
  14428. function fromListPartial(n, list, hasStrings) {
  14429. var ret;
  14430. if (n < list.head.data.length) {
  14431. // slice is the same for buffers and strings
  14432. ret = list.head.data.slice(0, n);
  14433. list.head.data = list.head.data.slice(n);
  14434. } else if (n === list.head.data.length) {
  14435. // first chunk is a perfect match
  14436. ret = list.shift();
  14437. } else {
  14438. // result spans more than one buffer
  14439. ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
  14440. }
  14441. return ret;
  14442. }
  14443. // Copies a specified amount of characters from the list of buffered data
  14444. // chunks.
  14445. // This function is designed to be inlinable, so please take care when making
  14446. // changes to the function body.
  14447. function copyFromBufferString(n, list) {
  14448. var p = list.head;
  14449. var c = 1;
  14450. var ret = p.data;
  14451. n -= ret.length;
  14452. while (p = p.next) {
  14453. var str = p.data;
  14454. var nb = n > str.length ? str.length : n;
  14455. if (nb === str.length) ret += str;else ret += str.slice(0, n);
  14456. n -= nb;
  14457. if (n === 0) {
  14458. if (nb === str.length) {
  14459. ++c;
  14460. if (p.next) list.head = p.next;else list.head = list.tail = null;
  14461. } else {
  14462. list.head = p;
  14463. p.data = str.slice(nb);
  14464. }
  14465. break;
  14466. }
  14467. ++c;
  14468. }
  14469. list.length -= c;
  14470. return ret;
  14471. }
  14472. // Copies a specified amount of bytes from the list of buffered data chunks.
  14473. // This function is designed to be inlinable, so please take care when making
  14474. // changes to the function body.
  14475. function copyFromBuffer(n, list) {
  14476. var ret = bufferShim.allocUnsafe(n);
  14477. var p = list.head;
  14478. var c = 1;
  14479. p.data.copy(ret);
  14480. n -= p.data.length;
  14481. while (p = p.next) {
  14482. var buf = p.data;
  14483. var nb = n > buf.length ? buf.length : n;
  14484. buf.copy(ret, ret.length - n, 0, nb);
  14485. n -= nb;
  14486. if (n === 0) {
  14487. if (nb === buf.length) {
  14488. ++c;
  14489. if (p.next) list.head = p.next;else list.head = list.tail = null;
  14490. } else {
  14491. list.head = p;
  14492. p.data = buf.slice(nb);
  14493. }
  14494. break;
  14495. }
  14496. ++c;
  14497. }
  14498. list.length -= c;
  14499. return ret;
  14500. }
  14501. function endReadable(stream) {
  14502. var state = stream._readableState;
  14503. // If we get here before consuming all the bytes, then that is a
  14504. // bug in node. Should never happen.
  14505. if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
  14506. if (!state.endEmitted) {
  14507. state.ended = true;
  14508. processNextTick(endReadableNT, state, stream);
  14509. }
  14510. }
  14511. function endReadableNT(state, stream) {
  14512. // Check that we didn't get one last unshift.
  14513. if (!state.endEmitted && state.length === 0) {
  14514. state.endEmitted = true;
  14515. stream.readable = false;
  14516. stream.emit('end');
  14517. }
  14518. }
  14519. function forEach(xs, f) {
  14520. for (var i = 0, l = xs.length; i < l; i++) {
  14521. f(xs[i], i);
  14522. }
  14523. }
  14524. function indexOf(xs, x) {
  14525. for (var i = 0, l = xs.length; i < l; i++) {
  14526. if (xs[i] === x) return i;
  14527. }
  14528. return -1;
  14529. }
  14530. }).call(this,require('_process'))
  14531. },{"./_stream_duplex":75,"./internal/streams/BufferList":80,"_process":73,"buffer":65,"buffer-shims":64,"core-util-is":66,"events":67,"inherits":69,"isarray":71,"process-nextick-args":72,"string_decoder/":86,"util":63}],78:[function(require,module,exports){
  14532. // a transform stream is a readable/writable stream where you do
  14533. // something with the data. Sometimes it's called a "filter",
  14534. // but that's not a great name for it, since that implies a thing where
  14535. // some bits pass through, and others are simply ignored. (That would
  14536. // be a valid example of a transform, of course.)
  14537. //
  14538. // While the output is causally related to the input, it's not a
  14539. // necessarily symmetric or synchronous transformation. For example,
  14540. // a zlib stream might take multiple plain-text writes(), and then
  14541. // emit a single compressed chunk some time in the future.
  14542. //
  14543. // Here's how this works:
  14544. //
  14545. // The Transform stream has all the aspects of the readable and writable
  14546. // stream classes. When you write(chunk), that calls _write(chunk,cb)
  14547. // internally, and returns false if there's a lot of pending writes
  14548. // buffered up. When you call read(), that calls _read(n) until
  14549. // there's enough pending readable data buffered up.
  14550. //
  14551. // In a transform stream, the written data is placed in a buffer. When
  14552. // _read(n) is called, it transforms the queued up data, calling the
  14553. // buffered _write cb's as it consumes chunks. If consuming a single
  14554. // written chunk would result in multiple output chunks, then the first
  14555. // outputted bit calls the readcb, and subsequent chunks just go into
  14556. // the read buffer, and will cause it to emit 'readable' if necessary.
  14557. //
  14558. // This way, back-pressure is actually determined by the reading side,
  14559. // since _read has to be called to start processing a new chunk. However,
  14560. // a pathological inflate type of transform can cause excessive buffering
  14561. // here. For example, imagine a stream where every byte of input is
  14562. // interpreted as an integer from 0-255, and then results in that many
  14563. // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
  14564. // 1kb of data being output. In this case, you could write a very small
  14565. // amount of input, and end up with a very large amount of output. In
  14566. // such a pathological inflating mechanism, there'd be no way to tell
  14567. // the system to stop doing the transform. A single 4MB write could
  14568. // cause the system to run out of memory.
  14569. //
  14570. // However, even in such a pathological case, only a single written chunk
  14571. // would be consumed, and then the rest would wait (un-transformed) until
  14572. // the results of the previous transformed chunk were consumed.
  14573. 'use strict';
  14574. module.exports = Transform;
  14575. var Duplex = require('./_stream_duplex');
  14576. /*<replacement>*/
  14577. var util = require('core-util-is');
  14578. util.inherits = require('inherits');
  14579. /*</replacement>*/
  14580. util.inherits(Transform, Duplex);
  14581. function TransformState(stream) {
  14582. this.afterTransform = function (er, data) {
  14583. return afterTransform(stream, er, data);
  14584. };
  14585. this.needTransform = false;
  14586. this.transforming = false;
  14587. this.writecb = null;
  14588. this.writechunk = null;
  14589. this.writeencoding = null;
  14590. }
  14591. function afterTransform(stream, er, data) {
  14592. var ts = stream._transformState;
  14593. ts.transforming = false;
  14594. var cb = ts.writecb;
  14595. if (!cb) return stream.emit('error', new Error('no writecb in Transform class'));
  14596. ts.writechunk = null;
  14597. ts.writecb = null;
  14598. if (data !== null && data !== undefined) stream.push(data);
  14599. cb(er);
  14600. var rs = stream._readableState;
  14601. rs.reading = false;
  14602. if (rs.needReadable || rs.length < rs.highWaterMark) {
  14603. stream._read(rs.highWaterMark);
  14604. }
  14605. }
  14606. function Transform(options) {
  14607. if (!(this instanceof Transform)) return new Transform(options);
  14608. Duplex.call(this, options);
  14609. this._transformState = new TransformState(this);
  14610. var stream = this;
  14611. // start out asking for a readable event once data is transformed.
  14612. this._readableState.needReadable = true;
  14613. // we have implemented the _read method, and done the other things
  14614. // that Readable wants before the first _read call, so unset the
  14615. // sync guard flag.
  14616. this._readableState.sync = false;
  14617. if (options) {
  14618. if (typeof options.transform === 'function') this._transform = options.transform;
  14619. if (typeof options.flush === 'function') this._flush = options.flush;
  14620. }
  14621. // When the writable side finishes, then flush out anything remaining.
  14622. this.once('prefinish', function () {
  14623. if (typeof this._flush === 'function') this._flush(function (er, data) {
  14624. done(stream, er, data);
  14625. });else done(stream);
  14626. });
  14627. }
  14628. Transform.prototype.push = function (chunk, encoding) {
  14629. this._transformState.needTransform = false;
  14630. return Duplex.prototype.push.call(this, chunk, encoding);
  14631. };
  14632. // This is the part where you do stuff!
  14633. // override this function in implementation classes.
  14634. // 'chunk' is an input chunk.
  14635. //
  14636. // Call `push(newChunk)` to pass along transformed output
  14637. // to the readable side. You may call 'push' zero or more times.
  14638. //
  14639. // Call `cb(err)` when you are done with this chunk. If you pass
  14640. // an error, then that'll put the hurt on the whole operation. If you
  14641. // never call cb(), then you'll never get another chunk.
  14642. Transform.prototype._transform = function (chunk, encoding, cb) {
  14643. throw new Error('_transform() is not implemented');
  14644. };
  14645. Transform.prototype._write = function (chunk, encoding, cb) {
  14646. var ts = this._transformState;
  14647. ts.writecb = cb;
  14648. ts.writechunk = chunk;
  14649. ts.writeencoding = encoding;
  14650. if (!ts.transforming) {
  14651. var rs = this._readableState;
  14652. if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
  14653. }
  14654. };
  14655. // Doesn't matter what the args are here.
  14656. // _transform does all the work.
  14657. // That we got here means that the readable side wants more data.
  14658. Transform.prototype._read = function (n) {
  14659. var ts = this._transformState;
  14660. if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
  14661. ts.transforming = true;
  14662. this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
  14663. } else {
  14664. // mark that we need a transform, so that any data that comes in
  14665. // will get processed, now that we've asked for it.
  14666. ts.needTransform = true;
  14667. }
  14668. };
  14669. function done(stream, er, data) {
  14670. if (er) return stream.emit('error', er);
  14671. if (data !== null && data !== undefined) stream.push(data);
  14672. // if there's nothing in the write buffer, then that means
  14673. // that nothing more will ever be provided
  14674. var ws = stream._writableState;
  14675. var ts = stream._transformState;
  14676. if (ws.length) throw new Error('Calling transform done when ws.length != 0');
  14677. if (ts.transforming) throw new Error('Calling transform done when still transforming');
  14678. return stream.push(null);
  14679. }
  14680. },{"./_stream_duplex":75,"core-util-is":66,"inherits":69}],79:[function(require,module,exports){
  14681. (function (process){
  14682. // A bit simpler than readable streams.
  14683. // Implement an async ._write(chunk, encoding, cb), and it'll handle all
  14684. // the drain event emission and buffering.
  14685. 'use strict';
  14686. module.exports = Writable;
  14687. /*<replacement>*/
  14688. var processNextTick = require('process-nextick-args');
  14689. /*</replacement>*/
  14690. /*<replacement>*/
  14691. var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : processNextTick;
  14692. /*</replacement>*/
  14693. /*<replacement>*/
  14694. var Duplex;
  14695. /*</replacement>*/
  14696. Writable.WritableState = WritableState;
  14697. /*<replacement>*/
  14698. var util = require('core-util-is');
  14699. util.inherits = require('inherits');
  14700. /*</replacement>*/
  14701. /*<replacement>*/
  14702. var internalUtil = {
  14703. deprecate: require('util-deprecate')
  14704. };
  14705. /*</replacement>*/
  14706. /*<replacement>*/
  14707. var Stream;
  14708. (function () {
  14709. try {
  14710. Stream = require('st' + 'ream');
  14711. } catch (_) {} finally {
  14712. if (!Stream) Stream = require('events').EventEmitter;
  14713. }
  14714. })();
  14715. /*</replacement>*/
  14716. var Buffer = require('buffer').Buffer;
  14717. /*<replacement>*/
  14718. var bufferShim = require('buffer-shims');
  14719. /*</replacement>*/
  14720. util.inherits(Writable, Stream);
  14721. function nop() {}
  14722. function WriteReq(chunk, encoding, cb) {
  14723. this.chunk = chunk;
  14724. this.encoding = encoding;
  14725. this.callback = cb;
  14726. this.next = null;
  14727. }
  14728. function WritableState(options, stream) {
  14729. Duplex = Duplex || require('./_stream_duplex');
  14730. options = options || {};
  14731. // object stream flag to indicate whether or not this stream
  14732. // contains buffers or objects.
  14733. this.objectMode = !!options.objectMode;
  14734. if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode;
  14735. // the point at which write() starts returning false
  14736. // Note: 0 is a valid value, means that we always return false if
  14737. // the entire buffer is not flushed immediately on write()
  14738. var hwm = options.highWaterMark;
  14739. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  14740. this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
  14741. // cast to ints.
  14742. this.highWaterMark = ~ ~this.highWaterMark;
  14743. // drain event flag.
  14744. this.needDrain = false;
  14745. // at the start of calling end()
  14746. this.ending = false;
  14747. // when end() has been called, and returned
  14748. this.ended = false;
  14749. // when 'finish' is emitted
  14750. this.finished = false;
  14751. // should we decode strings into buffers before passing to _write?
  14752. // this is here so that some node-core streams can optimize string
  14753. // handling at a lower level.
  14754. var noDecode = options.decodeStrings === false;
  14755. this.decodeStrings = !noDecode;
  14756. // Crypto is kind of old and crusty. Historically, its default string
  14757. // encoding is 'binary' so we have to make this configurable.
  14758. // Everything else in the universe uses 'utf8', though.
  14759. this.defaultEncoding = options.defaultEncoding || 'utf8';
  14760. // not an actual buffer we keep track of, but a measurement
  14761. // of how much we're waiting to get pushed to some underlying
  14762. // socket or file.
  14763. this.length = 0;
  14764. // a flag to see when we're in the middle of a write.
  14765. this.writing = false;
  14766. // when true all writes will be buffered until .uncork() call
  14767. this.corked = 0;
  14768. // a flag to be able to tell if the onwrite cb is called immediately,
  14769. // or on a later tick. We set this to true at first, because any
  14770. // actions that shouldn't happen until "later" should generally also
  14771. // not happen before the first write call.
  14772. this.sync = true;
  14773. // a flag to know if we're processing previously buffered items, which
  14774. // may call the _write() callback in the same tick, so that we don't
  14775. // end up in an overlapped onwrite situation.
  14776. this.bufferProcessing = false;
  14777. // the callback that's passed to _write(chunk,cb)
  14778. this.onwrite = function (er) {
  14779. onwrite(stream, er);
  14780. };
  14781. // the callback that the user supplies to write(chunk,encoding,cb)
  14782. this.writecb = null;
  14783. // the amount that is being written when _write is called.
  14784. this.writelen = 0;
  14785. this.bufferedRequest = null;
  14786. this.lastBufferedRequest = null;
  14787. // number of pending user-supplied write callbacks
  14788. // this must be 0 before 'finish' can be emitted
  14789. this.pendingcb = 0;
  14790. // emit prefinish if the only thing we're waiting for is _write cbs
  14791. // This is relevant for synchronous Transform streams
  14792. this.prefinished = false;
  14793. // True if the error was already emitted and should not be thrown again
  14794. this.errorEmitted = false;
  14795. // count buffered requests
  14796. this.bufferedRequestCount = 0;
  14797. // allocate the first CorkedRequest, there is always
  14798. // one allocated and free to use, and we maintain at most two
  14799. this.corkedRequestsFree = new CorkedRequest(this);
  14800. }
  14801. WritableState.prototype.getBuffer = function getBuffer() {
  14802. var current = this.bufferedRequest;
  14803. var out = [];
  14804. while (current) {
  14805. out.push(current);
  14806. current = current.next;
  14807. }
  14808. return out;
  14809. };
  14810. (function () {
  14811. try {
  14812. Object.defineProperty(WritableState.prototype, 'buffer', {
  14813. get: internalUtil.deprecate(function () {
  14814. return this.getBuffer();
  14815. }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.')
  14816. });
  14817. } catch (_) {}
  14818. })();
  14819. // Test _writableState for inheritance to account for Duplex streams,
  14820. // whose prototype chain only points to Readable.
  14821. var realHasInstance;
  14822. if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') {
  14823. realHasInstance = Function.prototype[Symbol.hasInstance];
  14824. Object.defineProperty(Writable, Symbol.hasInstance, {
  14825. value: function (object) {
  14826. if (realHasInstance.call(this, object)) return true;
  14827. return object && object._writableState instanceof WritableState;
  14828. }
  14829. });
  14830. } else {
  14831. realHasInstance = function (object) {
  14832. return object instanceof this;
  14833. };
  14834. }
  14835. function Writable(options) {
  14836. Duplex = Duplex || require('./_stream_duplex');
  14837. // Writable ctor is applied to Duplexes, too.
  14838. // `realHasInstance` is necessary because using plain `instanceof`
  14839. // would return false, as no `_writableState` property is attached.
  14840. // Trying to use the custom `instanceof` for Writable here will also break the
  14841. // Node.js LazyTransform implementation, which has a non-trivial getter for
  14842. // `_writableState` that would lead to infinite recursion.
  14843. if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) {
  14844. return new Writable(options);
  14845. }
  14846. this._writableState = new WritableState(options, this);
  14847. // legacy.
  14848. this.writable = true;
  14849. if (options) {
  14850. if (typeof options.write === 'function') this._write = options.write;
  14851. if (typeof options.writev === 'function') this._writev = options.writev;
  14852. }
  14853. Stream.call(this);
  14854. }
  14855. // Otherwise people can pipe Writable streams, which is just wrong.
  14856. Writable.prototype.pipe = function () {
  14857. this.emit('error', new Error('Cannot pipe, not readable'));
  14858. };
  14859. function writeAfterEnd(stream, cb) {
  14860. var er = new Error('write after end');
  14861. // TODO: defer error events consistently everywhere, not just the cb
  14862. stream.emit('error', er);
  14863. processNextTick(cb, er);
  14864. }
  14865. // If we get something that is not a buffer, string, null, or undefined,
  14866. // and we're not in objectMode, then that's an error.
  14867. // Otherwise stream chunks are all considered to be of length=1, and the
  14868. // watermarks determine how many objects to keep in the buffer, rather than
  14869. // how many bytes or characters.
  14870. function validChunk(stream, state, chunk, cb) {
  14871. var valid = true;
  14872. var er = false;
  14873. // Always throw error if a null is written
  14874. // if we are not in object mode then throw
  14875. // if it is not a buffer, string, or undefined.
  14876. if (chunk === null) {
  14877. er = new TypeError('May not write null values to stream');
  14878. } else if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
  14879. er = new TypeError('Invalid non-string/buffer chunk');
  14880. }
  14881. if (er) {
  14882. stream.emit('error', er);
  14883. processNextTick(cb, er);
  14884. valid = false;
  14885. }
  14886. return valid;
  14887. }
  14888. Writable.prototype.write = function (chunk, encoding, cb) {
  14889. var state = this._writableState;
  14890. var ret = false;
  14891. if (typeof encoding === 'function') {
  14892. cb = encoding;
  14893. encoding = null;
  14894. }
  14895. if (Buffer.isBuffer(chunk)) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
  14896. if (typeof cb !== 'function') cb = nop;
  14897. if (state.ended) writeAfterEnd(this, cb);else if (validChunk(this, state, chunk, cb)) {
  14898. state.pendingcb++;
  14899. ret = writeOrBuffer(this, state, chunk, encoding, cb);
  14900. }
  14901. return ret;
  14902. };
  14903. Writable.prototype.cork = function () {
  14904. var state = this._writableState;
  14905. state.corked++;
  14906. };
  14907. Writable.prototype.uncork = function () {
  14908. var state = this._writableState;
  14909. if (state.corked) {
  14910. state.corked--;
  14911. if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
  14912. }
  14913. };
  14914. Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
  14915. // node::ParseEncoding() requires lower case.
  14916. if (typeof encoding === 'string') encoding = encoding.toLowerCase();
  14917. if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding);
  14918. this._writableState.defaultEncoding = encoding;
  14919. return this;
  14920. };
  14921. function decodeChunk(state, chunk, encoding) {
  14922. if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
  14923. chunk = bufferShim.from(chunk, encoding);
  14924. }
  14925. return chunk;
  14926. }
  14927. // if we're already writing something, then just put this
  14928. // in the queue, and wait our turn. Otherwise, call _write
  14929. // If we return false, then we need a drain event, so set that flag.
  14930. function writeOrBuffer(stream, state, chunk, encoding, cb) {
  14931. chunk = decodeChunk(state, chunk, encoding);
  14932. if (Buffer.isBuffer(chunk)) encoding = 'buffer';
  14933. var len = state.objectMode ? 1 : chunk.length;
  14934. state.length += len;
  14935. var ret = state.length < state.highWaterMark;
  14936. // we must ensure that previous needDrain will not be reset to false.
  14937. if (!ret) state.needDrain = true;
  14938. if (state.writing || state.corked) {
  14939. var last = state.lastBufferedRequest;
  14940. state.lastBufferedRequest = new WriteReq(chunk, encoding, cb);
  14941. if (last) {
  14942. last.next = state.lastBufferedRequest;
  14943. } else {
  14944. state.bufferedRequest = state.lastBufferedRequest;
  14945. }
  14946. state.bufferedRequestCount += 1;
  14947. } else {
  14948. doWrite(stream, state, false, len, chunk, encoding, cb);
  14949. }
  14950. return ret;
  14951. }
  14952. function doWrite(stream, state, writev, len, chunk, encoding, cb) {
  14953. state.writelen = len;
  14954. state.writecb = cb;
  14955. state.writing = true;
  14956. state.sync = true;
  14957. if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
  14958. state.sync = false;
  14959. }
  14960. function onwriteError(stream, state, sync, er, cb) {
  14961. --state.pendingcb;
  14962. if (sync) processNextTick(cb, er);else cb(er);
  14963. stream._writableState.errorEmitted = true;
  14964. stream.emit('error', er);
  14965. }
  14966. function onwriteStateUpdate(state) {
  14967. state.writing = false;
  14968. state.writecb = null;
  14969. state.length -= state.writelen;
  14970. state.writelen = 0;
  14971. }
  14972. function onwrite(stream, er) {
  14973. var state = stream._writableState;
  14974. var sync = state.sync;
  14975. var cb = state.writecb;
  14976. onwriteStateUpdate(state);
  14977. if (er) onwriteError(stream, state, sync, er, cb);else {
  14978. // Check if we're actually ready to finish, but don't emit yet
  14979. var finished = needFinish(state);
  14980. if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
  14981. clearBuffer(stream, state);
  14982. }
  14983. if (sync) {
  14984. /*<replacement>*/
  14985. asyncWrite(afterWrite, stream, state, finished, cb);
  14986. /*</replacement>*/
  14987. } else {
  14988. afterWrite(stream, state, finished, cb);
  14989. }
  14990. }
  14991. }
  14992. function afterWrite(stream, state, finished, cb) {
  14993. if (!finished) onwriteDrain(stream, state);
  14994. state.pendingcb--;
  14995. cb();
  14996. finishMaybe(stream, state);
  14997. }
  14998. // Must force callback to be called on nextTick, so that we don't
  14999. // emit 'drain' before the write() consumer gets the 'false' return
  15000. // value, and has a chance to attach a 'drain' listener.
  15001. function onwriteDrain(stream, state) {
  15002. if (state.length === 0 && state.needDrain) {
  15003. state.needDrain = false;
  15004. stream.emit('drain');
  15005. }
  15006. }
  15007. // if there's something in the buffer waiting, then process it
  15008. function clearBuffer(stream, state) {
  15009. state.bufferProcessing = true;
  15010. var entry = state.bufferedRequest;
  15011. if (stream._writev && entry && entry.next) {
  15012. // Fast case, write everything using _writev()
  15013. var l = state.bufferedRequestCount;
  15014. var buffer = new Array(l);
  15015. var holder = state.corkedRequestsFree;
  15016. holder.entry = entry;
  15017. var count = 0;
  15018. while (entry) {
  15019. buffer[count] = entry;
  15020. entry = entry.next;
  15021. count += 1;
  15022. }
  15023. doWrite(stream, state, true, state.length, buffer, '', holder.finish);
  15024. // doWrite is almost always async, defer these to save a bit of time
  15025. // as the hot path ends with doWrite
  15026. state.pendingcb++;
  15027. state.lastBufferedRequest = null;
  15028. if (holder.next) {
  15029. state.corkedRequestsFree = holder.next;
  15030. holder.next = null;
  15031. } else {
  15032. state.corkedRequestsFree = new CorkedRequest(state);
  15033. }
  15034. } else {
  15035. // Slow case, write chunks one-by-one
  15036. while (entry) {
  15037. var chunk = entry.chunk;
  15038. var encoding = entry.encoding;
  15039. var cb = entry.callback;
  15040. var len = state.objectMode ? 1 : chunk.length;
  15041. doWrite(stream, state, false, len, chunk, encoding, cb);
  15042. entry = entry.next;
  15043. // if we didn't call the onwrite immediately, then
  15044. // it means that we need to wait until it does.
  15045. // also, that means that the chunk and cb are currently
  15046. // being processed, so move the buffer counter past them.
  15047. if (state.writing) {
  15048. break;
  15049. }
  15050. }
  15051. if (entry === null) state.lastBufferedRequest = null;
  15052. }
  15053. state.bufferedRequestCount = 0;
  15054. state.bufferedRequest = entry;
  15055. state.bufferProcessing = false;
  15056. }
  15057. Writable.prototype._write = function (chunk, encoding, cb) {
  15058. cb(new Error('_write() is not implemented'));
  15059. };
  15060. Writable.prototype._writev = null;
  15061. Writable.prototype.end = function (chunk, encoding, cb) {
  15062. var state = this._writableState;
  15063. if (typeof chunk === 'function') {
  15064. cb = chunk;
  15065. chunk = null;
  15066. encoding = null;
  15067. } else if (typeof encoding === 'function') {
  15068. cb = encoding;
  15069. encoding = null;
  15070. }
  15071. if (chunk !== null && chunk !== undefined) this.write(chunk, encoding);
  15072. // .end() fully uncorks
  15073. if (state.corked) {
  15074. state.corked = 1;
  15075. this.uncork();
  15076. }
  15077. // ignore unnecessary end() calls.
  15078. if (!state.ending && !state.finished) endWritable(this, state, cb);
  15079. };
  15080. function needFinish(state) {
  15081. return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
  15082. }
  15083. function prefinish(stream, state) {
  15084. if (!state.prefinished) {
  15085. state.prefinished = true;
  15086. stream.emit('prefinish');
  15087. }
  15088. }
  15089. function finishMaybe(stream, state) {
  15090. var need = needFinish(state);
  15091. if (need) {
  15092. if (state.pendingcb === 0) {
  15093. prefinish(stream, state);
  15094. state.finished = true;
  15095. stream.emit('finish');
  15096. } else {
  15097. prefinish(stream, state);
  15098. }
  15099. }
  15100. return need;
  15101. }
  15102. function endWritable(stream, state, cb) {
  15103. state.ending = true;
  15104. finishMaybe(stream, state);
  15105. if (cb) {
  15106. if (state.finished) processNextTick(cb);else stream.once('finish', cb);
  15107. }
  15108. state.ended = true;
  15109. stream.writable = false;
  15110. }
  15111. // It seems a linked list but it is not
  15112. // there will be only 2 of these for each stream
  15113. function CorkedRequest(state) {
  15114. var _this = this;
  15115. this.next = null;
  15116. this.entry = null;
  15117. this.finish = function (err) {
  15118. var entry = _this.entry;
  15119. _this.entry = null;
  15120. while (entry) {
  15121. var cb = entry.callback;
  15122. state.pendingcb--;
  15123. cb(err);
  15124. entry = entry.next;
  15125. }
  15126. if (state.corkedRequestsFree) {
  15127. state.corkedRequestsFree.next = _this;
  15128. } else {
  15129. state.corkedRequestsFree = _this;
  15130. }
  15131. };
  15132. }
  15133. }).call(this,require('_process'))
  15134. },{"./_stream_duplex":75,"_process":73,"buffer":65,"buffer-shims":64,"core-util-is":66,"events":67,"inherits":69,"process-nextick-args":72,"util-deprecate":87}],80:[function(require,module,exports){
  15135. 'use strict';
  15136. var Buffer = require('buffer').Buffer;
  15137. /*<replacement>*/
  15138. var bufferShim = require('buffer-shims');
  15139. /*</replacement>*/
  15140. module.exports = BufferList;
  15141. function BufferList() {
  15142. this.head = null;
  15143. this.tail = null;
  15144. this.length = 0;
  15145. }
  15146. BufferList.prototype.push = function (v) {
  15147. var entry = { data: v, next: null };
  15148. if (this.length > 0) this.tail.next = entry;else this.head = entry;
  15149. this.tail = entry;
  15150. ++this.length;
  15151. };
  15152. BufferList.prototype.unshift = function (v) {
  15153. var entry = { data: v, next: this.head };
  15154. if (this.length === 0) this.tail = entry;
  15155. this.head = entry;
  15156. ++this.length;
  15157. };
  15158. BufferList.prototype.shift = function () {
  15159. if (this.length === 0) return;
  15160. var ret = this.head.data;
  15161. if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
  15162. --this.length;
  15163. return ret;
  15164. };
  15165. BufferList.prototype.clear = function () {
  15166. this.head = this.tail = null;
  15167. this.length = 0;
  15168. };
  15169. BufferList.prototype.join = function (s) {
  15170. if (this.length === 0) return '';
  15171. var p = this.head;
  15172. var ret = '' + p.data;
  15173. while (p = p.next) {
  15174. ret += s + p.data;
  15175. }return ret;
  15176. };
  15177. BufferList.prototype.concat = function (n) {
  15178. if (this.length === 0) return bufferShim.alloc(0);
  15179. if (this.length === 1) return this.head.data;
  15180. var ret = bufferShim.allocUnsafe(n >>> 0);
  15181. var p = this.head;
  15182. var i = 0;
  15183. while (p) {
  15184. p.data.copy(ret, i);
  15185. i += p.data.length;
  15186. p = p.next;
  15187. }
  15188. return ret;
  15189. };
  15190. },{"buffer":65,"buffer-shims":64}],81:[function(require,module,exports){
  15191. module.exports = require("./lib/_stream_passthrough.js")
  15192. },{"./lib/_stream_passthrough.js":76}],82:[function(require,module,exports){
  15193. (function (process){
  15194. var Stream = (function (){
  15195. try {
  15196. return require('st' + 'ream'); // hack to fix a circular dependency issue when used with browserify
  15197. } catch(_){}
  15198. }());
  15199. exports = module.exports = require('./lib/_stream_readable.js');
  15200. exports.Stream = Stream || exports;
  15201. exports.Readable = exports;
  15202. exports.Writable = require('./lib/_stream_writable.js');
  15203. exports.Duplex = require('./lib/_stream_duplex.js');
  15204. exports.Transform = require('./lib/_stream_transform.js');
  15205. exports.PassThrough = require('./lib/_stream_passthrough.js');
  15206. if (!process.browser && process.env.READABLE_STREAM === 'disable' && Stream) {
  15207. module.exports = Stream;
  15208. }
  15209. }).call(this,require('_process'))
  15210. },{"./lib/_stream_duplex.js":75,"./lib/_stream_passthrough.js":76,"./lib/_stream_readable.js":77,"./lib/_stream_transform.js":78,"./lib/_stream_writable.js":79,"_process":73}],83:[function(require,module,exports){
  15211. module.exports = require("./lib/_stream_transform.js")
  15212. },{"./lib/_stream_transform.js":78}],84:[function(require,module,exports){
  15213. module.exports = require("./lib/_stream_writable.js")
  15214. },{"./lib/_stream_writable.js":79}],85:[function(require,module,exports){
  15215. // Copyright Joyent, Inc. and other Node contributors.
  15216. //
  15217. // Permission is hereby granted, free of charge, to any person obtaining a
  15218. // copy of this software and associated documentation files (the
  15219. // "Software"), to deal in the Software without restriction, including
  15220. // without limitation the rights to use, copy, modify, merge, publish,
  15221. // distribute, sublicense, and/or sell copies of the Software, and to permit
  15222. // persons to whom the Software is furnished to do so, subject to the
  15223. // following conditions:
  15224. //
  15225. // The above copyright notice and this permission notice shall be included
  15226. // in all copies or substantial portions of the Software.
  15227. //
  15228. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15229. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15230. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  15231. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15232. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15233. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  15234. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  15235. module.exports = Stream;
  15236. var EE = require('events').EventEmitter;
  15237. var inherits = require('inherits');
  15238. inherits(Stream, EE);
  15239. Stream.Readable = require('readable-stream/readable.js');
  15240. Stream.Writable = require('readable-stream/writable.js');
  15241. Stream.Duplex = require('readable-stream/duplex.js');
  15242. Stream.Transform = require('readable-stream/transform.js');
  15243. Stream.PassThrough = require('readable-stream/passthrough.js');
  15244. // Backwards-compat with node 0.4.x
  15245. Stream.Stream = Stream;
  15246. // old-style streams. Note that the pipe method (the only relevant
  15247. // part of this class) is overridden in the Readable class.
  15248. function Stream() {
  15249. EE.call(this);
  15250. }
  15251. Stream.prototype.pipe = function(dest, options) {
  15252. var source = this;
  15253. function ondata(chunk) {
  15254. if (dest.writable) {
  15255. if (false === dest.write(chunk) && source.pause) {
  15256. source.pause();
  15257. }
  15258. }
  15259. }
  15260. source.on('data', ondata);
  15261. function ondrain() {
  15262. if (source.readable && source.resume) {
  15263. source.resume();
  15264. }
  15265. }
  15266. dest.on('drain', ondrain);
  15267. // If the 'end' option is not supplied, dest.end() will be called when
  15268. // source gets the 'end' or 'close' events. Only dest.end() once.
  15269. if (!dest._isStdio && (!options || options.end !== false)) {
  15270. source.on('end', onend);
  15271. source.on('close', onclose);
  15272. }
  15273. var didOnEnd = false;
  15274. function onend() {
  15275. if (didOnEnd) return;
  15276. didOnEnd = true;
  15277. dest.end();
  15278. }
  15279. function onclose() {
  15280. if (didOnEnd) return;
  15281. didOnEnd = true;
  15282. if (typeof dest.destroy === 'function') dest.destroy();
  15283. }
  15284. // don't leave dangling pipes when there are errors.
  15285. function onerror(er) {
  15286. cleanup();
  15287. if (EE.listenerCount(this, 'error') === 0) {
  15288. throw er; // Unhandled stream error in pipe.
  15289. }
  15290. }
  15291. source.on('error', onerror);
  15292. dest.on('error', onerror);
  15293. // remove all the event listeners that were added.
  15294. function cleanup() {
  15295. source.removeListener('data', ondata);
  15296. dest.removeListener('drain', ondrain);
  15297. source.removeListener('end', onend);
  15298. source.removeListener('close', onclose);
  15299. source.removeListener('error', onerror);
  15300. dest.removeListener('error', onerror);
  15301. source.removeListener('end', cleanup);
  15302. source.removeListener('close', cleanup);
  15303. dest.removeListener('close', cleanup);
  15304. }
  15305. source.on('end', cleanup);
  15306. source.on('close', cleanup);
  15307. dest.on('close', cleanup);
  15308. dest.emit('pipe', source);
  15309. // Allow for unix-like usage: A.pipe(B).pipe(C)
  15310. return dest;
  15311. };
  15312. },{"events":67,"inherits":69,"readable-stream/duplex.js":74,"readable-stream/passthrough.js":81,"readable-stream/readable.js":82,"readable-stream/transform.js":83,"readable-stream/writable.js":84}],86:[function(require,module,exports){
  15313. // Copyright Joyent, Inc. and other Node contributors.
  15314. //
  15315. // Permission is hereby granted, free of charge, to any person obtaining a
  15316. // copy of this software and associated documentation files (the
  15317. // "Software"), to deal in the Software without restriction, including
  15318. // without limitation the rights to use, copy, modify, merge, publish,
  15319. // distribute, sublicense, and/or sell copies of the Software, and to permit
  15320. // persons to whom the Software is furnished to do so, subject to the
  15321. // following conditions:
  15322. //
  15323. // The above copyright notice and this permission notice shall be included
  15324. // in all copies or substantial portions of the Software.
  15325. //
  15326. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15327. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15328. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  15329. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15330. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15331. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  15332. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  15333. var Buffer = require('buffer').Buffer;
  15334. var isBufferEncoding = Buffer.isEncoding
  15335. || function(encoding) {
  15336. switch (encoding && encoding.toLowerCase()) {
  15337. case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': case 'raw': return true;
  15338. default: return false;
  15339. }
  15340. }
  15341. function assertEncoding(encoding) {
  15342. if (encoding && !isBufferEncoding(encoding)) {
  15343. throw new Error('Unknown encoding: ' + encoding);
  15344. }
  15345. }
  15346. // StringDecoder provides an interface for efficiently splitting a series of
  15347. // buffers into a series of JS strings without breaking apart multi-byte
  15348. // characters. CESU-8 is handled as part of the UTF-8 encoding.
  15349. //
  15350. // @TODO Handling all encodings inside a single object makes it very difficult
  15351. // to reason about this code, so it should be split up in the future.
  15352. // @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
  15353. // points as used by CESU-8.
  15354. var StringDecoder = exports.StringDecoder = function(encoding) {
  15355. this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
  15356. assertEncoding(encoding);
  15357. switch (this.encoding) {
  15358. case 'utf8':
  15359. // CESU-8 represents each of Surrogate Pair by 3-bytes
  15360. this.surrogateSize = 3;
  15361. break;
  15362. case 'ucs2':
  15363. case 'utf16le':
  15364. // UTF-16 represents each of Surrogate Pair by 2-bytes
  15365. this.surrogateSize = 2;
  15366. this.detectIncompleteChar = utf16DetectIncompleteChar;
  15367. break;
  15368. case 'base64':
  15369. // Base-64 stores 3 bytes in 4 chars, and pads the remainder.
  15370. this.surrogateSize = 3;
  15371. this.detectIncompleteChar = base64DetectIncompleteChar;
  15372. break;
  15373. default:
  15374. this.write = passThroughWrite;
  15375. return;
  15376. }
  15377. // Enough space to store all bytes of a single character. UTF-8 needs 4
  15378. // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
  15379. this.charBuffer = new Buffer(6);
  15380. // Number of bytes received for the current incomplete multi-byte character.
  15381. this.charReceived = 0;
  15382. // Number of bytes expected for the current incomplete multi-byte character.
  15383. this.charLength = 0;
  15384. };
  15385. // write decodes the given buffer and returns it as JS string that is
  15386. // guaranteed to not contain any partial multi-byte characters. Any partial
  15387. // character found at the end of the buffer is buffered up, and will be
  15388. // returned when calling write again with the remaining bytes.
  15389. //
  15390. // Note: Converting a Buffer containing an orphan surrogate to a String
  15391. // currently works, but converting a String to a Buffer (via `new Buffer`, or
  15392. // Buffer#write) will replace incomplete surrogates with the unicode
  15393. // replacement character. See https://codereview.chromium.org/121173009/ .
  15394. StringDecoder.prototype.write = function(buffer) {
  15395. var charStr = '';
  15396. // if our last write ended with an incomplete multibyte character
  15397. while (this.charLength) {
  15398. // determine how many remaining bytes this buffer has to offer for this char
  15399. var available = (buffer.length >= this.charLength - this.charReceived) ?
  15400. this.charLength - this.charReceived :
  15401. buffer.length;
  15402. // add the new bytes to the char buffer
  15403. buffer.copy(this.charBuffer, this.charReceived, 0, available);
  15404. this.charReceived += available;
  15405. if (this.charReceived < this.charLength) {
  15406. // still not enough chars in this buffer? wait for more ...
  15407. return '';
  15408. }
  15409. // remove bytes belonging to the current character from the buffer
  15410. buffer = buffer.slice(available, buffer.length);
  15411. // get the character that was split
  15412. charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding);
  15413. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  15414. var charCode = charStr.charCodeAt(charStr.length - 1);
  15415. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  15416. this.charLength += this.surrogateSize;
  15417. charStr = '';
  15418. continue;
  15419. }
  15420. this.charReceived = this.charLength = 0;
  15421. // if there are no more bytes in this buffer, just emit our char
  15422. if (buffer.length === 0) {
  15423. return charStr;
  15424. }
  15425. break;
  15426. }
  15427. // determine and set charLength / charReceived
  15428. this.detectIncompleteChar(buffer);
  15429. var end = buffer.length;
  15430. if (this.charLength) {
  15431. // buffer the incomplete character bytes we got
  15432. buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end);
  15433. end -= this.charReceived;
  15434. }
  15435. charStr += buffer.toString(this.encoding, 0, end);
  15436. var end = charStr.length - 1;
  15437. var charCode = charStr.charCodeAt(end);
  15438. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  15439. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  15440. var size = this.surrogateSize;
  15441. this.charLength += size;
  15442. this.charReceived += size;
  15443. this.charBuffer.copy(this.charBuffer, size, 0, size);
  15444. buffer.copy(this.charBuffer, 0, 0, size);
  15445. return charStr.substring(0, end);
  15446. }
  15447. // or just emit the charStr
  15448. return charStr;
  15449. };
  15450. // detectIncompleteChar determines if there is an incomplete UTF-8 character at
  15451. // the end of the given buffer. If so, it sets this.charLength to the byte
  15452. // length that character, and sets this.charReceived to the number of bytes
  15453. // that are available for this character.
  15454. StringDecoder.prototype.detectIncompleteChar = function(buffer) {
  15455. // determine how many bytes we have to check at the end of this buffer
  15456. var i = (buffer.length >= 3) ? 3 : buffer.length;
  15457. // Figure out if one of the last i bytes of our buffer announces an
  15458. // incomplete char.
  15459. for (; i > 0; i--) {
  15460. var c = buffer[buffer.length - i];
  15461. // See http://en.wikipedia.org/wiki/UTF-8#Description
  15462. // 110XXXXX
  15463. if (i == 1 && c >> 5 == 0x06) {
  15464. this.charLength = 2;
  15465. break;
  15466. }
  15467. // 1110XXXX
  15468. if (i <= 2 && c >> 4 == 0x0E) {
  15469. this.charLength = 3;
  15470. break;
  15471. }
  15472. // 11110XXX
  15473. if (i <= 3 && c >> 3 == 0x1E) {
  15474. this.charLength = 4;
  15475. break;
  15476. }
  15477. }
  15478. this.charReceived = i;
  15479. };
  15480. StringDecoder.prototype.end = function(buffer) {
  15481. var res = '';
  15482. if (buffer && buffer.length)
  15483. res = this.write(buffer);
  15484. if (this.charReceived) {
  15485. var cr = this.charReceived;
  15486. var buf = this.charBuffer;
  15487. var enc = this.encoding;
  15488. res += buf.slice(0, cr).toString(enc);
  15489. }
  15490. return res;
  15491. };
  15492. function passThroughWrite(buffer) {
  15493. return buffer.toString(this.encoding);
  15494. }
  15495. function utf16DetectIncompleteChar(buffer) {
  15496. this.charReceived = buffer.length % 2;
  15497. this.charLength = this.charReceived ? 2 : 0;
  15498. }
  15499. function base64DetectIncompleteChar(buffer) {
  15500. this.charReceived = buffer.length % 3;
  15501. this.charLength = this.charReceived ? 3 : 0;
  15502. }
  15503. },{"buffer":65}],87:[function(require,module,exports){
  15504. (function (global){
  15505. /**
  15506. * Module exports.
  15507. */
  15508. module.exports = deprecate;
  15509. /**
  15510. * Mark that a method should not be used.
  15511. * Returns a modified function which warns once by default.
  15512. *
  15513. * If `localStorage.noDeprecation = true` is set, then it is a no-op.
  15514. *
  15515. * If `localStorage.throwDeprecation = true` is set, then deprecated functions
  15516. * will throw an Error when invoked.
  15517. *
  15518. * If `localStorage.traceDeprecation = true` is set, then deprecated functions
  15519. * will invoke `console.trace()` instead of `console.error()`.
  15520. *
  15521. * @param {Function} fn - the function to deprecate
  15522. * @param {String} msg - the string to print to the console when `fn` is invoked
  15523. * @returns {Function} a new "deprecated" version of `fn`
  15524. * @api public
  15525. */
  15526. function deprecate (fn, msg) {
  15527. if (config('noDeprecation')) {
  15528. return fn;
  15529. }
  15530. var warned = false;
  15531. function deprecated() {
  15532. if (!warned) {
  15533. if (config('throwDeprecation')) {
  15534. throw new Error(msg);
  15535. } else if (config('traceDeprecation')) {
  15536. console.trace(msg);
  15537. } else {
  15538. console.warn(msg);
  15539. }
  15540. warned = true;
  15541. }
  15542. return fn.apply(this, arguments);
  15543. }
  15544. return deprecated;
  15545. }
  15546. /**
  15547. * Checks `localStorage` for boolean values for the given `name`.
  15548. *
  15549. * @param {String} name
  15550. * @returns {Boolean}
  15551. * @api private
  15552. */
  15553. function config (name) {
  15554. // accessing global.localStorage can trigger a DOMException in sandboxed iframes
  15555. try {
  15556. if (!global.localStorage) return false;
  15557. } catch (_) {
  15558. return false;
  15559. }
  15560. var val = global.localStorage[name];
  15561. if (null == val) return false;
  15562. return String(val).toLowerCase() === 'true';
  15563. }
  15564. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  15565. },{}],88:[function(require,module,exports){
  15566. arguments[4][34][0].apply(exports,arguments)
  15567. },{"dup":34}],89:[function(require,module,exports){
  15568. module.exports = function isBuffer(arg) {
  15569. return arg && typeof arg === 'object'
  15570. && typeof arg.copy === 'function'
  15571. && typeof arg.fill === 'function'
  15572. && typeof arg.readUInt8 === 'function';
  15573. }
  15574. },{}],90:[function(require,module,exports){
  15575. (function (process,global){
  15576. // Copyright Joyent, Inc. and other Node contributors.
  15577. //
  15578. // Permission is hereby granted, free of charge, to any person obtaining a
  15579. // copy of this software and associated documentation files (the
  15580. // "Software"), to deal in the Software without restriction, including
  15581. // without limitation the rights to use, copy, modify, merge, publish,
  15582. // distribute, sublicense, and/or sell copies of the Software, and to permit
  15583. // persons to whom the Software is furnished to do so, subject to the
  15584. // following conditions:
  15585. //
  15586. // The above copyright notice and this permission notice shall be included
  15587. // in all copies or substantial portions of the Software.
  15588. //
  15589. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15590. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15591. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  15592. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15593. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15594. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  15595. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  15596. var formatRegExp = /%[sdj%]/g;
  15597. exports.format = function(f) {
  15598. if (!isString(f)) {
  15599. var objects = [];
  15600. for (var i = 0; i < arguments.length; i++) {
  15601. objects.push(inspect(arguments[i]));
  15602. }
  15603. return objects.join(' ');
  15604. }
  15605. var i = 1;
  15606. var args = arguments;
  15607. var len = args.length;
  15608. var str = String(f).replace(formatRegExp, function(x) {
  15609. if (x === '%%') return '%';
  15610. if (i >= len) return x;
  15611. switch (x) {
  15612. case '%s': return String(args[i++]);
  15613. case '%d': return Number(args[i++]);
  15614. case '%j':
  15615. try {
  15616. return JSON.stringify(args[i++]);
  15617. } catch (_) {
  15618. return '[Circular]';
  15619. }
  15620. default:
  15621. return x;
  15622. }
  15623. });
  15624. for (var x = args[i]; i < len; x = args[++i]) {
  15625. if (isNull(x) || !isObject(x)) {
  15626. str += ' ' + x;
  15627. } else {
  15628. str += ' ' + inspect(x);
  15629. }
  15630. }
  15631. return str;
  15632. };
  15633. // Mark that a method should not be used.
  15634. // Returns a modified function which warns once by default.
  15635. // If --no-deprecation is set, then it is a no-op.
  15636. exports.deprecate = function(fn, msg) {
  15637. // Allow for deprecating things in the process of starting up.
  15638. if (isUndefined(global.process)) {
  15639. return function() {
  15640. return exports.deprecate(fn, msg).apply(this, arguments);
  15641. };
  15642. }
  15643. if (process.noDeprecation === true) {
  15644. return fn;
  15645. }
  15646. var warned = false;
  15647. function deprecated() {
  15648. if (!warned) {
  15649. if (process.throwDeprecation) {
  15650. throw new Error(msg);
  15651. } else if (process.traceDeprecation) {
  15652. console.trace(msg);
  15653. } else {
  15654. console.error(msg);
  15655. }
  15656. warned = true;
  15657. }
  15658. return fn.apply(this, arguments);
  15659. }
  15660. return deprecated;
  15661. };
  15662. var debugs = {};
  15663. var debugEnviron;
  15664. exports.debuglog = function(set) {
  15665. if (isUndefined(debugEnviron))
  15666. debugEnviron = process.env.NODE_DEBUG || '';
  15667. set = set.toUpperCase();
  15668. if (!debugs[set]) {
  15669. if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
  15670. var pid = process.pid;
  15671. debugs[set] = function() {
  15672. var msg = exports.format.apply(exports, arguments);
  15673. console.error('%s %d: %s', set, pid, msg);
  15674. };
  15675. } else {
  15676. debugs[set] = function() {};
  15677. }
  15678. }
  15679. return debugs[set];
  15680. };
  15681. /**
  15682. * Echos the value of a value. Trys to print the value out
  15683. * in the best way possible given the different types.
  15684. *
  15685. * @param {Object} obj The object to print out.
  15686. * @param {Object} opts Optional options object that alters the output.
  15687. */
  15688. /* legacy: obj, showHidden, depth, colors*/
  15689. function inspect(obj, opts) {
  15690. // default options
  15691. var ctx = {
  15692. seen: [],
  15693. stylize: stylizeNoColor
  15694. };
  15695. // legacy...
  15696. if (arguments.length >= 3) ctx.depth = arguments[2];
  15697. if (arguments.length >= 4) ctx.colors = arguments[3];
  15698. if (isBoolean(opts)) {
  15699. // legacy...
  15700. ctx.showHidden = opts;
  15701. } else if (opts) {
  15702. // got an "options" object
  15703. exports._extend(ctx, opts);
  15704. }
  15705. // set default options
  15706. if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
  15707. if (isUndefined(ctx.depth)) ctx.depth = 2;
  15708. if (isUndefined(ctx.colors)) ctx.colors = false;
  15709. if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
  15710. if (ctx.colors) ctx.stylize = stylizeWithColor;
  15711. return formatValue(ctx, obj, ctx.depth);
  15712. }
  15713. exports.inspect = inspect;
  15714. // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  15715. inspect.colors = {
  15716. 'bold' : [1, 22],
  15717. 'italic' : [3, 23],
  15718. 'underline' : [4, 24],
  15719. 'inverse' : [7, 27],
  15720. 'white' : [37, 39],
  15721. 'grey' : [90, 39],
  15722. 'black' : [30, 39],
  15723. 'blue' : [34, 39],
  15724. 'cyan' : [36, 39],
  15725. 'green' : [32, 39],
  15726. 'magenta' : [35, 39],
  15727. 'red' : [31, 39],
  15728. 'yellow' : [33, 39]
  15729. };
  15730. // Don't use 'blue' not visible on cmd.exe
  15731. inspect.styles = {
  15732. 'special': 'cyan',
  15733. 'number': 'yellow',
  15734. 'boolean': 'yellow',
  15735. 'undefined': 'grey',
  15736. 'null': 'bold',
  15737. 'string': 'green',
  15738. 'date': 'magenta',
  15739. // "name": intentionally not styling
  15740. 'regexp': 'red'
  15741. };
  15742. function stylizeWithColor(str, styleType) {
  15743. var style = inspect.styles[styleType];
  15744. if (style) {
  15745. return '\u001b[' + inspect.colors[style][0] + 'm' + str +
  15746. '\u001b[' + inspect.colors[style][1] + 'm';
  15747. } else {
  15748. return str;
  15749. }
  15750. }
  15751. function stylizeNoColor(str, styleType) {
  15752. return str;
  15753. }
  15754. function arrayToHash(array) {
  15755. var hash = {};
  15756. array.forEach(function(val, idx) {
  15757. hash[val] = true;
  15758. });
  15759. return hash;
  15760. }
  15761. function formatValue(ctx, value, recurseTimes) {
  15762. // Provide a hook for user-specified inspect functions.
  15763. // Check that value is an object with an inspect function on it
  15764. if (ctx.customInspect &&
  15765. value &&
  15766. isFunction(value.inspect) &&
  15767. // Filter out the util module, it's inspect function is special
  15768. value.inspect !== exports.inspect &&
  15769. // Also filter out any prototype objects using the circular check.
  15770. !(value.constructor && value.constructor.prototype === value)) {
  15771. var ret = value.inspect(recurseTimes, ctx);
  15772. if (!isString(ret)) {
  15773. ret = formatValue(ctx, ret, recurseTimes);
  15774. }
  15775. return ret;
  15776. }
  15777. // Primitive types cannot have properties
  15778. var primitive = formatPrimitive(ctx, value);
  15779. if (primitive) {
  15780. return primitive;
  15781. }
  15782. // Look up the keys of the object.
  15783. var keys = Object.keys(value);
  15784. var visibleKeys = arrayToHash(keys);
  15785. if (ctx.showHidden) {
  15786. keys = Object.getOwnPropertyNames(value);
  15787. }
  15788. // IE doesn't make error fields non-enumerable
  15789. // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
  15790. if (isError(value)
  15791. && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
  15792. return formatError(value);
  15793. }
  15794. // Some type of object without properties can be shortcutted.
  15795. if (keys.length === 0) {
  15796. if (isFunction(value)) {
  15797. var name = value.name ? ': ' + value.name : '';
  15798. return ctx.stylize('[Function' + name + ']', 'special');
  15799. }
  15800. if (isRegExp(value)) {
  15801. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  15802. }
  15803. if (isDate(value)) {
  15804. return ctx.stylize(Date.prototype.toString.call(value), 'date');
  15805. }
  15806. if (isError(value)) {
  15807. return formatError(value);
  15808. }
  15809. }
  15810. var base = '', array = false, braces = ['{', '}'];
  15811. // Make Array say that they are Array
  15812. if (isArray(value)) {
  15813. array = true;
  15814. braces = ['[', ']'];
  15815. }
  15816. // Make functions say that they are functions
  15817. if (isFunction(value)) {
  15818. var n = value.name ? ': ' + value.name : '';
  15819. base = ' [Function' + n + ']';
  15820. }
  15821. // Make RegExps say that they are RegExps
  15822. if (isRegExp(value)) {
  15823. base = ' ' + RegExp.prototype.toString.call(value);
  15824. }
  15825. // Make dates with properties first say the date
  15826. if (isDate(value)) {
  15827. base = ' ' + Date.prototype.toUTCString.call(value);
  15828. }
  15829. // Make error with message first say the error
  15830. if (isError(value)) {
  15831. base = ' ' + formatError(value);
  15832. }
  15833. if (keys.length === 0 && (!array || value.length == 0)) {
  15834. return braces[0] + base + braces[1];
  15835. }
  15836. if (recurseTimes < 0) {
  15837. if (isRegExp(value)) {
  15838. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  15839. } else {
  15840. return ctx.stylize('[Object]', 'special');
  15841. }
  15842. }
  15843. ctx.seen.push(value);
  15844. var output;
  15845. if (array) {
  15846. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  15847. } else {
  15848. output = keys.map(function(key) {
  15849. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  15850. });
  15851. }
  15852. ctx.seen.pop();
  15853. return reduceToSingleString(output, base, braces);
  15854. }
  15855. function formatPrimitive(ctx, value) {
  15856. if (isUndefined(value))
  15857. return ctx.stylize('undefined', 'undefined');
  15858. if (isString(value)) {
  15859. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  15860. .replace(/'/g, "\\'")
  15861. .replace(/\\"/g, '"') + '\'';
  15862. return ctx.stylize(simple, 'string');
  15863. }
  15864. if (isNumber(value))
  15865. return ctx.stylize('' + value, 'number');
  15866. if (isBoolean(value))
  15867. return ctx.stylize('' + value, 'boolean');
  15868. // For some reason typeof null is "object", so special case here.
  15869. if (isNull(value))
  15870. return ctx.stylize('null', 'null');
  15871. }
  15872. function formatError(value) {
  15873. return '[' + Error.prototype.toString.call(value) + ']';
  15874. }
  15875. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  15876. var output = [];
  15877. for (var i = 0, l = value.length; i < l; ++i) {
  15878. if (hasOwnProperty(value, String(i))) {
  15879. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  15880. String(i), true));
  15881. } else {
  15882. output.push('');
  15883. }
  15884. }
  15885. keys.forEach(function(key) {
  15886. if (!key.match(/^\d+$/)) {
  15887. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  15888. key, true));
  15889. }
  15890. });
  15891. return output;
  15892. }
  15893. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  15894. var name, str, desc;
  15895. desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
  15896. if (desc.get) {
  15897. if (desc.set) {
  15898. str = ctx.stylize('[Getter/Setter]', 'special');
  15899. } else {
  15900. str = ctx.stylize('[Getter]', 'special');
  15901. }
  15902. } else {
  15903. if (desc.set) {
  15904. str = ctx.stylize('[Setter]', 'special');
  15905. }
  15906. }
  15907. if (!hasOwnProperty(visibleKeys, key)) {
  15908. name = '[' + key + ']';
  15909. }
  15910. if (!str) {
  15911. if (ctx.seen.indexOf(desc.value) < 0) {
  15912. if (isNull(recurseTimes)) {
  15913. str = formatValue(ctx, desc.value, null);
  15914. } else {
  15915. str = formatValue(ctx, desc.value, recurseTimes - 1);
  15916. }
  15917. if (str.indexOf('\n') > -1) {
  15918. if (array) {
  15919. str = str.split('\n').map(function(line) {
  15920. return ' ' + line;
  15921. }).join('\n').substr(2);
  15922. } else {
  15923. str = '\n' + str.split('\n').map(function(line) {
  15924. return ' ' + line;
  15925. }).join('\n');
  15926. }
  15927. }
  15928. } else {
  15929. str = ctx.stylize('[Circular]', 'special');
  15930. }
  15931. }
  15932. if (isUndefined(name)) {
  15933. if (array && key.match(/^\d+$/)) {
  15934. return str;
  15935. }
  15936. name = JSON.stringify('' + key);
  15937. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  15938. name = name.substr(1, name.length - 2);
  15939. name = ctx.stylize(name, 'name');
  15940. } else {
  15941. name = name.replace(/'/g, "\\'")
  15942. .replace(/\\"/g, '"')
  15943. .replace(/(^"|"$)/g, "'");
  15944. name = ctx.stylize(name, 'string');
  15945. }
  15946. }
  15947. return name + ': ' + str;
  15948. }
  15949. function reduceToSingleString(output, base, braces) {
  15950. var numLinesEst = 0;
  15951. var length = output.reduce(function(prev, cur) {
  15952. numLinesEst++;
  15953. if (cur.indexOf('\n') >= 0) numLinesEst++;
  15954. return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
  15955. }, 0);
  15956. if (length > 60) {
  15957. return braces[0] +
  15958. (base === '' ? '' : base + '\n ') +
  15959. ' ' +
  15960. output.join(',\n ') +
  15961. ' ' +
  15962. braces[1];
  15963. }
  15964. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  15965. }
  15966. // NOTE: These type checking functions intentionally don't use `instanceof`
  15967. // because it is fragile and can be easily faked with `Object.create()`.
  15968. function isArray(ar) {
  15969. return Array.isArray(ar);
  15970. }
  15971. exports.isArray = isArray;
  15972. function isBoolean(arg) {
  15973. return typeof arg === 'boolean';
  15974. }
  15975. exports.isBoolean = isBoolean;
  15976. function isNull(arg) {
  15977. return arg === null;
  15978. }
  15979. exports.isNull = isNull;
  15980. function isNullOrUndefined(arg) {
  15981. return arg == null;
  15982. }
  15983. exports.isNullOrUndefined = isNullOrUndefined;
  15984. function isNumber(arg) {
  15985. return typeof arg === 'number';
  15986. }
  15987. exports.isNumber = isNumber;
  15988. function isString(arg) {
  15989. return typeof arg === 'string';
  15990. }
  15991. exports.isString = isString;
  15992. function isSymbol(arg) {
  15993. return typeof arg === 'symbol';
  15994. }
  15995. exports.isSymbol = isSymbol;
  15996. function isUndefined(arg) {
  15997. return arg === void 0;
  15998. }
  15999. exports.isUndefined = isUndefined;
  16000. function isRegExp(re) {
  16001. return isObject(re) && objectToString(re) === '[object RegExp]';
  16002. }
  16003. exports.isRegExp = isRegExp;
  16004. function isObject(arg) {
  16005. return typeof arg === 'object' && arg !== null;
  16006. }
  16007. exports.isObject = isObject;
  16008. function isDate(d) {
  16009. return isObject(d) && objectToString(d) === '[object Date]';
  16010. }
  16011. exports.isDate = isDate;
  16012. function isError(e) {
  16013. return isObject(e) &&
  16014. (objectToString(e) === '[object Error]' || e instanceof Error);
  16015. }
  16016. exports.isError = isError;
  16017. function isFunction(arg) {
  16018. return typeof arg === 'function';
  16019. }
  16020. exports.isFunction = isFunction;
  16021. function isPrimitive(arg) {
  16022. return arg === null ||
  16023. typeof arg === 'boolean' ||
  16024. typeof arg === 'number' ||
  16025. typeof arg === 'string' ||
  16026. typeof arg === 'symbol' || // ES6 symbol
  16027. typeof arg === 'undefined';
  16028. }
  16029. exports.isPrimitive = isPrimitive;
  16030. exports.isBuffer = require('./support/isBuffer');
  16031. function objectToString(o) {
  16032. return Object.prototype.toString.call(o);
  16033. }
  16034. function pad(n) {
  16035. return n < 10 ? '0' + n.toString(10) : n.toString(10);
  16036. }
  16037. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
  16038. 'Oct', 'Nov', 'Dec'];
  16039. // 26 Feb 16:19:34
  16040. function timestamp() {
  16041. var d = new Date();
  16042. var time = [pad(d.getHours()),
  16043. pad(d.getMinutes()),
  16044. pad(d.getSeconds())].join(':');
  16045. return [d.getDate(), months[d.getMonth()], time].join(' ');
  16046. }
  16047. // log is just a thin wrapper to console.log that prepends a timestamp
  16048. exports.log = function() {
  16049. console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
  16050. };
  16051. /**
  16052. * Inherit the prototype methods from one constructor into another.
  16053. *
  16054. * The Function.prototype.inherits from lang.js rewritten as a standalone
  16055. * function (not on Function.prototype). NOTE: If this file is to be loaded
  16056. * during bootstrapping this function needs to be rewritten using some native
  16057. * functions as prototype setup using normal JavaScript does not work as
  16058. * expected during bootstrapping (see mirror.js in r114903).
  16059. *
  16060. * @param {function} ctor Constructor function which needs to inherit the
  16061. * prototype.
  16062. * @param {function} superCtor Constructor function to inherit prototype from.
  16063. */
  16064. exports.inherits = require('inherits');
  16065. exports._extend = function(origin, add) {
  16066. // Don't do anything if add isn't an object
  16067. if (!add || !isObject(add)) return origin;
  16068. var keys = Object.keys(add);
  16069. var i = keys.length;
  16070. while (i--) {
  16071. origin[keys[i]] = add[keys[i]];
  16072. }
  16073. return origin;
  16074. };
  16075. function hasOwnProperty(obj, prop) {
  16076. return Object.prototype.hasOwnProperty.call(obj, prop);
  16077. }
  16078. }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  16079. },{"./support/isBuffer":89,"_process":73,"inherits":88}]},{},[1]);