You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tests.c 211KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086
  1. /**********************************************************************
  2. * Copyright (c) 2013, 2014, 2015 Pieter Wuille, Gregory Maxwell *
  3. * Distributed under the MIT software license, see the accompanying *
  4. * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
  5. **********************************************************************/
  6. #if defined HAVE_CONFIG_H
  7. #include "libsecp256k1-config.h"
  8. #endif
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include "secp256k1.c"
  14. #include "include/secp256k1.h"
  15. #include "testrand_impl.h"
  16. #ifdef ENABLE_OPENSSL_TESTS
  17. #include "openssl/bn.h"
  18. #include "openssl/ec.h"
  19. #include "openssl/ecdsa.h"
  20. #include "openssl/obj_mac.h"
  21. # if OPENSSL_VERSION_NUMBER < 0x10100000L
  22. void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) {*pr = sig->r; *ps = sig->s;}
  23. # endif
  24. #endif
  25. #include "contrib/lax_der_parsing.c"
  26. #include "contrib/lax_der_privatekey_parsing.c"
  27. #if !defined(VG_CHECK)
  28. # if defined(VALGRIND)
  29. # include <valgrind/memcheck.h>
  30. # define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
  31. # define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
  32. # else
  33. # define VG_UNDEF(x,y)
  34. # define VG_CHECK(x,y)
  35. # endif
  36. #endif
  37. static int count = 64;
  38. static secp256k1_context *ctx = NULL;
  39. static void counting_illegal_callback_fn(const char* str, void* data) {
  40. /* Dummy callback function that just counts. */
  41. int32_t *p;
  42. (void)str;
  43. p = data;
  44. (*p)++;
  45. }
  46. static void uncounting_illegal_callback_fn(const char* str, void* data) {
  47. /* Dummy callback function that just counts (backwards). */
  48. int32_t *p;
  49. (void)str;
  50. p = data;
  51. (*p)--;
  52. }
  53. void random_field_element_test(secp256k1_fe *fe) {
  54. do {
  55. unsigned char b32[32];
  56. secp256k1_rand256_test(b32);
  57. if (secp256k1_fe_set_b32(fe, b32)) {
  58. break;
  59. }
  60. } while(1);
  61. }
  62. void random_field_element_magnitude(secp256k1_fe *fe) {
  63. secp256k1_fe zero;
  64. int n = secp256k1_rand_int(9);
  65. secp256k1_fe_normalize(fe);
  66. if (n == 0) {
  67. return;
  68. }
  69. secp256k1_fe_clear(&zero);
  70. secp256k1_fe_negate(&zero, &zero, 0);
  71. secp256k1_fe_mul_int(&zero, n - 1);
  72. secp256k1_fe_add(fe, &zero);
  73. VERIFY_CHECK(fe->magnitude == n);
  74. }
  75. void random_group_element_test(secp256k1_ge *ge) {
  76. secp256k1_fe fe;
  77. do {
  78. random_field_element_test(&fe);
  79. if (secp256k1_ge_set_xo_var(ge, &fe, secp256k1_rand_bits(1))) {
  80. secp256k1_fe_normalize(&ge->y);
  81. break;
  82. }
  83. } while(1);
  84. }
  85. void random_group_element_jacobian_test(secp256k1_gej *gej, const secp256k1_ge *ge) {
  86. secp256k1_fe z2, z3;
  87. do {
  88. random_field_element_test(&gej->z);
  89. if (!secp256k1_fe_is_zero(&gej->z)) {
  90. break;
  91. }
  92. } while(1);
  93. secp256k1_fe_sqr(&z2, &gej->z);
  94. secp256k1_fe_mul(&z3, &z2, &gej->z);
  95. secp256k1_fe_mul(&gej->x, &ge->x, &z2);
  96. secp256k1_fe_mul(&gej->y, &ge->y, &z3);
  97. gej->infinity = ge->infinity;
  98. }
  99. void random_scalar_order_test(secp256k1_scalar *num) {
  100. do {
  101. unsigned char b32[32];
  102. int overflow = 0;
  103. secp256k1_rand256_test(b32);
  104. secp256k1_scalar_set_b32(num, b32, &overflow);
  105. if (overflow || secp256k1_scalar_is_zero(num)) {
  106. continue;
  107. }
  108. break;
  109. } while(1);
  110. }
  111. void random_scalar_order(secp256k1_scalar *num) {
  112. do {
  113. unsigned char b32[32];
  114. int overflow = 0;
  115. secp256k1_rand256(b32);
  116. secp256k1_scalar_set_b32(num, b32, &overflow);
  117. if (overflow || secp256k1_scalar_is_zero(num)) {
  118. continue;
  119. }
  120. break;
  121. } while(1);
  122. }
  123. void run_context_tests(void) {
  124. secp256k1_pubkey pubkey;
  125. secp256k1_pubkey zero_pubkey;
  126. secp256k1_ecdsa_signature sig;
  127. unsigned char ctmp[32];
  128. int32_t ecount;
  129. int32_t ecount2;
  130. secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
  131. secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
  132. secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
  133. secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
  134. secp256k1_gej pubj;
  135. secp256k1_ge pub;
  136. secp256k1_scalar msg, key, nonce;
  137. secp256k1_scalar sigr, sigs;
  138. memset(&zero_pubkey, 0, sizeof(zero_pubkey));
  139. ecount = 0;
  140. ecount2 = 10;
  141. secp256k1_context_set_illegal_callback(vrfy, counting_illegal_callback_fn, &ecount);
  142. secp256k1_context_set_illegal_callback(sign, counting_illegal_callback_fn, &ecount2);
  143. secp256k1_context_set_error_callback(sign, counting_illegal_callback_fn, NULL);
  144. CHECK(vrfy->error_callback.fn != sign->error_callback.fn);
  145. /*** clone and destroy all of them to make sure cloning was complete ***/
  146. {
  147. secp256k1_context *ctx_tmp;
  148. ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_destroy(ctx_tmp);
  149. ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_destroy(ctx_tmp);
  150. ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_destroy(ctx_tmp);
  151. ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_destroy(ctx_tmp);
  152. }
  153. /* Verify that the error callback makes it across the clone. */
  154. CHECK(vrfy->error_callback.fn != sign->error_callback.fn);
  155. /* And that it resets back to default. */
  156. secp256k1_context_set_error_callback(sign, NULL, NULL);
  157. CHECK(vrfy->error_callback.fn == sign->error_callback.fn);
  158. /*** attempt to use them ***/
  159. random_scalar_order_test(&msg);
  160. random_scalar_order_test(&key);
  161. secp256k1_ecmult_gen(&both->ecmult_gen_ctx, &pubj, &key);
  162. secp256k1_ge_set_gej(&pub, &pubj);
  163. /* Verify context-type checking illegal-argument errors. */
  164. memset(ctmp, 1, 32);
  165. CHECK(secp256k1_ec_pubkey_create(vrfy, &pubkey, ctmp) == 0);
  166. CHECK(ecount == 1);
  167. VG_UNDEF(&pubkey, sizeof(pubkey));
  168. CHECK(secp256k1_ec_pubkey_create(sign, &pubkey, ctmp) == 1);
  169. VG_CHECK(&pubkey, sizeof(pubkey));
  170. CHECK(secp256k1_ecdsa_sign(vrfy, &sig, ctmp, ctmp, NULL, NULL) == 0);
  171. CHECK(ecount == 2);
  172. VG_UNDEF(&sig, sizeof(sig));
  173. CHECK(secp256k1_ecdsa_sign(sign, &sig, ctmp, ctmp, NULL, NULL) == 1);
  174. VG_CHECK(&sig, sizeof(sig));
  175. CHECK(ecount2 == 10);
  176. CHECK(secp256k1_ecdsa_verify(sign, &sig, ctmp, &pubkey) == 0);
  177. CHECK(ecount2 == 11);
  178. CHECK(secp256k1_ecdsa_verify(vrfy, &sig, ctmp, &pubkey) == 1);
  179. CHECK(ecount == 2);
  180. CHECK(secp256k1_ec_pubkey_tweak_add(sign, &pubkey, ctmp) == 0);
  181. CHECK(ecount2 == 12);
  182. CHECK(secp256k1_ec_pubkey_tweak_add(vrfy, &pubkey, ctmp) == 1);
  183. CHECK(ecount == 2);
  184. CHECK(secp256k1_ec_pubkey_tweak_mul(sign, &pubkey, ctmp) == 0);
  185. CHECK(ecount2 == 13);
  186. CHECK(secp256k1_ec_pubkey_negate(vrfy, &pubkey) == 1);
  187. CHECK(ecount == 2);
  188. CHECK(secp256k1_ec_pubkey_negate(sign, &pubkey) == 1);
  189. CHECK(ecount == 2);
  190. CHECK(secp256k1_ec_pubkey_negate(sign, NULL) == 0);
  191. CHECK(ecount2 == 14);
  192. CHECK(secp256k1_ec_pubkey_negate(vrfy, &zero_pubkey) == 0);
  193. CHECK(ecount == 3);
  194. CHECK(secp256k1_ec_pubkey_tweak_mul(vrfy, &pubkey, ctmp) == 1);
  195. CHECK(ecount == 3);
  196. CHECK(secp256k1_context_randomize(vrfy, ctmp) == 0);
  197. CHECK(ecount == 4);
  198. CHECK(secp256k1_context_randomize(sign, NULL) == 1);
  199. CHECK(ecount2 == 14);
  200. secp256k1_context_set_illegal_callback(vrfy, NULL, NULL);
  201. secp256k1_context_set_illegal_callback(sign, NULL, NULL);
  202. /* This shouldn't leak memory, due to already-set tests. */
  203. secp256k1_ecmult_gen_context_build(&sign->ecmult_gen_ctx, NULL);
  204. secp256k1_ecmult_context_build(&vrfy->ecmult_ctx, NULL);
  205. /* obtain a working nonce */
  206. do {
  207. random_scalar_order_test(&nonce);
  208. } while(!secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
  209. /* try signing */
  210. CHECK(secp256k1_ecdsa_sig_sign(&sign->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
  211. CHECK(secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
  212. /* try verifying */
  213. CHECK(secp256k1_ecdsa_sig_verify(&vrfy->ecmult_ctx, &sigr, &sigs, &pub, &msg));
  214. CHECK(secp256k1_ecdsa_sig_verify(&both->ecmult_ctx, &sigr, &sigs, &pub, &msg));
  215. /* cleanup */
  216. secp256k1_context_destroy(none);
  217. secp256k1_context_destroy(sign);
  218. secp256k1_context_destroy(vrfy);
  219. secp256k1_context_destroy(both);
  220. /* Defined as no-op. */
  221. secp256k1_context_destroy(NULL);
  222. }
  223. void run_scratch_tests(void) {
  224. int32_t ecount = 0;
  225. secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
  226. secp256k1_scratch_space *scratch;
  227. /* Test public API */
  228. secp256k1_context_set_illegal_callback(none, counting_illegal_callback_fn, &ecount);
  229. scratch = secp256k1_scratch_space_create(none, 1000);
  230. CHECK(scratch != NULL);
  231. CHECK(ecount == 0);
  232. /* Test internal API */
  233. CHECK(secp256k1_scratch_max_allocation(scratch, 0) == 1000);
  234. CHECK(secp256k1_scratch_max_allocation(scratch, 1) < 1000);
  235. /* Allocating 500 bytes with no frame fails */
  236. CHECK(secp256k1_scratch_alloc(scratch, 500) == NULL);
  237. CHECK(secp256k1_scratch_max_allocation(scratch, 0) == 1000);
  238. /* ...but pushing a new stack frame does affect the max allocation */
  239. CHECK(secp256k1_scratch_allocate_frame(scratch, 500, 1 == 1));
  240. CHECK(secp256k1_scratch_max_allocation(scratch, 1) < 500); /* 500 - ALIGNMENT */
  241. CHECK(secp256k1_scratch_alloc(scratch, 500) != NULL);
  242. CHECK(secp256k1_scratch_alloc(scratch, 500) == NULL);
  243. CHECK(secp256k1_scratch_allocate_frame(scratch, 500, 1) == 0);
  244. /* ...and this effect is undone by popping the frame */
  245. secp256k1_scratch_deallocate_frame(scratch);
  246. CHECK(secp256k1_scratch_max_allocation(scratch, 0) == 1000);
  247. CHECK(secp256k1_scratch_alloc(scratch, 500) == NULL);
  248. /* cleanup */
  249. secp256k1_scratch_space_destroy(scratch);
  250. secp256k1_context_destroy(none);
  251. }
  252. /***** HASH TESTS *****/
  253. void run_sha256_tests(void) {
  254. static const char *inputs[8] = {
  255. "", "abc", "message digest", "secure hash algorithm", "SHA256 is considered to be safe",
  256. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  257. "For this sample, this 63-byte string will be used as input data",
  258. "This is exactly 64 bytes long, not counting the terminating byte"
  259. };
  260. static const unsigned char outputs[8][32] = {
  261. {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
  262. {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad},
  263. {0xf7, 0x84, 0x6f, 0x55, 0xcf, 0x23, 0xe1, 0x4e, 0xeb, 0xea, 0xb5, 0xb4, 0xe1, 0x55, 0x0c, 0xad, 0x5b, 0x50, 0x9e, 0x33, 0x48, 0xfb, 0xc4, 0xef, 0xa3, 0xa1, 0x41, 0x3d, 0x39, 0x3c, 0xb6, 0x50},
  264. {0xf3, 0x0c, 0xeb, 0x2b, 0xb2, 0x82, 0x9e, 0x79, 0xe4, 0xca, 0x97, 0x53, 0xd3, 0x5a, 0x8e, 0xcc, 0x00, 0x26, 0x2d, 0x16, 0x4c, 0xc0, 0x77, 0x08, 0x02, 0x95, 0x38, 0x1c, 0xbd, 0x64, 0x3f, 0x0d},
  265. {0x68, 0x19, 0xd9, 0x15, 0xc7, 0x3f, 0x4d, 0x1e, 0x77, 0xe4, 0xe1, 0xb5, 0x2d, 0x1f, 0xa0, 0xf9, 0xcf, 0x9b, 0xea, 0xea, 0xd3, 0x93, 0x9f, 0x15, 0x87, 0x4b, 0xd9, 0x88, 0xe2, 0xa2, 0x36, 0x30},
  266. {0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1},
  267. {0xf0, 0x8a, 0x78, 0xcb, 0xba, 0xee, 0x08, 0x2b, 0x05, 0x2a, 0xe0, 0x70, 0x8f, 0x32, 0xfa, 0x1e, 0x50, 0xc5, 0xc4, 0x21, 0xaa, 0x77, 0x2b, 0xa5, 0xdb, 0xb4, 0x06, 0xa2, 0xea, 0x6b, 0xe3, 0x42},
  268. {0xab, 0x64, 0xef, 0xf7, 0xe8, 0x8e, 0x2e, 0x46, 0x16, 0x5e, 0x29, 0xf2, 0xbc, 0xe4, 0x18, 0x26, 0xbd, 0x4c, 0x7b, 0x35, 0x52, 0xf6, 0xb3, 0x82, 0xa9, 0xe7, 0xd3, 0xaf, 0x47, 0xc2, 0x45, 0xf8}
  269. };
  270. int i;
  271. for (i = 0; i < 8; i++) {
  272. unsigned char out[32];
  273. secp256k1_sha256 hasher;
  274. secp256k1_sha256_initialize(&hasher);
  275. secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), strlen(inputs[i]));
  276. secp256k1_sha256_finalize(&hasher, out);
  277. CHECK(memcmp(out, outputs[i], 32) == 0);
  278. if (strlen(inputs[i]) > 0) {
  279. int split = secp256k1_rand_int(strlen(inputs[i]));
  280. secp256k1_sha256_initialize(&hasher);
  281. secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), split);
  282. secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i] + split), strlen(inputs[i]) - split);
  283. secp256k1_sha256_finalize(&hasher, out);
  284. CHECK(memcmp(out, outputs[i], 32) == 0);
  285. }
  286. }
  287. }
  288. void run_hmac_sha256_tests(void) {
  289. static const char *keys[6] = {
  290. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  291. "\x4a\x65\x66\x65",
  292. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  293. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  294. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  295. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  296. };
  297. static const char *inputs[6] = {
  298. "\x48\x69\x20\x54\x68\x65\x72\x65",
  299. "\x77\x68\x61\x74\x20\x64\x6f\x20\x79\x61\x20\x77\x61\x6e\x74\x20\x66\x6f\x72\x20\x6e\x6f\x74\x68\x69\x6e\x67\x3f",
  300. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  301. "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
  302. "\x54\x65\x73\x74\x20\x55\x73\x69\x6e\x67\x20\x4c\x61\x72\x67\x65\x72\x20\x54\x68\x61\x6e\x20\x42\x6c\x6f\x63\x6b\x2d\x53\x69\x7a\x65\x20\x4b\x65\x79\x20\x2d\x20\x48\x61\x73\x68\x20\x4b\x65\x79\x20\x46\x69\x72\x73\x74",
  303. "\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x20\x75\x73\x69\x6e\x67\x20\x61\x20\x6c\x61\x72\x67\x65\x72\x20\x74\x68\x61\x6e\x20\x62\x6c\x6f\x63\x6b\x2d\x73\x69\x7a\x65\x20\x6b\x65\x79\x20\x61\x6e\x64\x20\x61\x20\x6c\x61\x72\x67\x65\x72\x20\x74\x68\x61\x6e\x20\x62\x6c\x6f\x63\x6b\x2d\x73\x69\x7a\x65\x20\x64\x61\x74\x61\x2e\x20\x54\x68\x65\x20\x6b\x65\x79\x20\x6e\x65\x65\x64\x73\x20\x74\x6f\x20\x62\x65\x20\x68\x61\x73\x68\x65\x64\x20\x62\x65\x66\x6f\x72\x65\x20\x62\x65\x69\x6e\x67\x20\x75\x73\x65\x64\x20\x62\x79\x20\x74\x68\x65\x20\x48\x4d\x41\x43\x20\x61\x6c\x67\x6f\x72\x69\x74\x68\x6d\x2e"
  304. };
  305. static const unsigned char outputs[6][32] = {
  306. {0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7},
  307. {0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43},
  308. {0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe},
  309. {0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b},
  310. {0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54},
  311. {0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2}
  312. };
  313. int i;
  314. for (i = 0; i < 6; i++) {
  315. secp256k1_hmac_sha256 hasher;
  316. unsigned char out[32];
  317. secp256k1_hmac_sha256_initialize(&hasher, (const unsigned char*)(keys[i]), strlen(keys[i]));
  318. secp256k1_hmac_sha256_write(&hasher, (const unsigned char*)(inputs[i]), strlen(inputs[i]));
  319. secp256k1_hmac_sha256_finalize(&hasher, out);
  320. CHECK(memcmp(out, outputs[i], 32) == 0);
  321. if (strlen(inputs[i]) > 0) {
  322. int split = secp256k1_rand_int(strlen(inputs[i]));
  323. secp256k1_hmac_sha256_initialize(&hasher, (const unsigned char*)(keys[i]), strlen(keys[i]));
  324. secp256k1_hmac_sha256_write(&hasher, (const unsigned char*)(inputs[i]), split);
  325. secp256k1_hmac_sha256_write(&hasher, (const unsigned char*)(inputs[i] + split), strlen(inputs[i]) - split);
  326. secp256k1_hmac_sha256_finalize(&hasher, out);
  327. CHECK(memcmp(out, outputs[i], 32) == 0);
  328. }
  329. }
  330. }
  331. void run_rfc6979_hmac_sha256_tests(void) {
  332. static const unsigned char key1[65] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00, 0x4b, 0xf5, 0x12, 0x2f, 0x34, 0x45, 0x54, 0xc5, 0x3b, 0xde, 0x2e, 0xbb, 0x8c, 0xd2, 0xb7, 0xe3, 0xd1, 0x60, 0x0a, 0xd6, 0x31, 0xc3, 0x85, 0xa5, 0xd7, 0xcc, 0xe2, 0x3c, 0x77, 0x85, 0x45, 0x9a, 0};
  333. static const unsigned char out1[3][32] = {
  334. {0x4f, 0xe2, 0x95, 0x25, 0xb2, 0x08, 0x68, 0x09, 0x15, 0x9a, 0xcd, 0xf0, 0x50, 0x6e, 0xfb, 0x86, 0xb0, 0xec, 0x93, 0x2c, 0x7b, 0xa4, 0x42, 0x56, 0xab, 0x32, 0x1e, 0x42, 0x1e, 0x67, 0xe9, 0xfb},
  335. {0x2b, 0xf0, 0xff, 0xf1, 0xd3, 0xc3, 0x78, 0xa2, 0x2d, 0xc5, 0xde, 0x1d, 0x85, 0x65, 0x22, 0x32, 0x5c, 0x65, 0xb5, 0x04, 0x49, 0x1a, 0x0c, 0xbd, 0x01, 0xcb, 0x8f, 0x3a, 0xa6, 0x7f, 0xfd, 0x4a},
  336. {0xf5, 0x28, 0xb4, 0x10, 0xcb, 0x54, 0x1f, 0x77, 0x00, 0x0d, 0x7a, 0xfb, 0x6c, 0x5b, 0x53, 0xc5, 0xc4, 0x71, 0xea, 0xb4, 0x3e, 0x46, 0x6d, 0x9a, 0xc5, 0x19, 0x0c, 0x39, 0xc8, 0x2f, 0xd8, 0x2e}
  337. };
  338. static const unsigned char key2[64] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
  339. static const unsigned char out2[3][32] = {
  340. {0x9c, 0x23, 0x6c, 0x16, 0x5b, 0x82, 0xae, 0x0c, 0xd5, 0x90, 0x65, 0x9e, 0x10, 0x0b, 0x6b, 0xab, 0x30, 0x36, 0xe7, 0xba, 0x8b, 0x06, 0x74, 0x9b, 0xaf, 0x69, 0x81, 0xe1, 0x6f, 0x1a, 0x2b, 0x95},
  341. {0xdf, 0x47, 0x10, 0x61, 0x62, 0x5b, 0xc0, 0xea, 0x14, 0xb6, 0x82, 0xfe, 0xee, 0x2c, 0x9c, 0x02, 0xf2, 0x35, 0xda, 0x04, 0x20, 0x4c, 0x1d, 0x62, 0xa1, 0x53, 0x6c, 0x6e, 0x17, 0xae, 0xd7, 0xa9},
  342. {0x75, 0x97, 0x88, 0x7c, 0xbd, 0x76, 0x32, 0x1f, 0x32, 0xe3, 0x04, 0x40, 0x67, 0x9a, 0x22, 0xcf, 0x7f, 0x8d, 0x9d, 0x2e, 0xac, 0x39, 0x0e, 0x58, 0x1f, 0xea, 0x09, 0x1c, 0xe2, 0x02, 0xba, 0x94}
  343. };
  344. secp256k1_rfc6979_hmac_sha256 rng;
  345. unsigned char out[32];
  346. int i;
  347. secp256k1_rfc6979_hmac_sha256_initialize(&rng, key1, 64);
  348. for (i = 0; i < 3; i++) {
  349. secp256k1_rfc6979_hmac_sha256_generate(&rng, out, 32);
  350. CHECK(memcmp(out, out1[i], 32) == 0);
  351. }
  352. secp256k1_rfc6979_hmac_sha256_finalize(&rng);
  353. secp256k1_rfc6979_hmac_sha256_initialize(&rng, key1, 65);
  354. for (i = 0; i < 3; i++) {
  355. secp256k1_rfc6979_hmac_sha256_generate(&rng, out, 32);
  356. CHECK(memcmp(out, out1[i], 32) != 0);
  357. }
  358. secp256k1_rfc6979_hmac_sha256_finalize(&rng);
  359. secp256k1_rfc6979_hmac_sha256_initialize(&rng, key2, 64);
  360. for (i = 0; i < 3; i++) {
  361. secp256k1_rfc6979_hmac_sha256_generate(&rng, out, 32);
  362. CHECK(memcmp(out, out2[i], 32) == 0);
  363. }
  364. secp256k1_rfc6979_hmac_sha256_finalize(&rng);
  365. }
  366. /***** RANDOM TESTS *****/
  367. void test_rand_bits(int rand32, int bits) {
  368. /* (1-1/2^B)^rounds[B] < 1/10^9, so rounds is the number of iterations to
  369. * get a false negative chance below once in a billion */
  370. static const unsigned int rounds[7] = {1, 30, 73, 156, 322, 653, 1316};
  371. /* We try multiplying the results with various odd numbers, which shouldn't
  372. * influence the uniform distribution modulo a power of 2. */
  373. static const uint32_t mults[6] = {1, 3, 21, 289, 0x9999, 0x80402011};
  374. /* We only select up to 6 bits from the output to analyse */
  375. unsigned int usebits = bits > 6 ? 6 : bits;
  376. unsigned int maxshift = bits - usebits;
  377. /* For each of the maxshift+1 usebits-bit sequences inside a bits-bit
  378. number, track all observed outcomes, one per bit in a uint64_t. */
  379. uint64_t x[6][27] = {{0}};
  380. unsigned int i, shift, m;
  381. /* Multiply the output of all rand calls with the odd number m, which
  382. should not change the uniformity of its distribution. */
  383. for (i = 0; i < rounds[usebits]; i++) {
  384. uint32_t r = (rand32 ? secp256k1_rand32() : secp256k1_rand_bits(bits));
  385. CHECK((((uint64_t)r) >> bits) == 0);
  386. for (m = 0; m < sizeof(mults) / sizeof(mults[0]); m++) {
  387. uint32_t rm = r * mults[m];
  388. for (shift = 0; shift <= maxshift; shift++) {
  389. x[m][shift] |= (((uint64_t)1) << ((rm >> shift) & ((1 << usebits) - 1)));
  390. }
  391. }
  392. }
  393. for (m = 0; m < sizeof(mults) / sizeof(mults[0]); m++) {
  394. for (shift = 0; shift <= maxshift; shift++) {
  395. /* Test that the lower usebits bits of x[shift] are 1 */
  396. CHECK(((~x[m][shift]) << (64 - (1 << usebits))) == 0);
  397. }
  398. }
  399. }
  400. /* Subrange must be a whole divisor of range, and at most 64 */
  401. void test_rand_int(uint32_t range, uint32_t subrange) {
  402. /* (1-1/subrange)^rounds < 1/10^9 */
  403. int rounds = (subrange * 2073) / 100;
  404. int i;
  405. uint64_t x = 0;
  406. CHECK((range % subrange) == 0);
  407. for (i = 0; i < rounds; i++) {
  408. uint32_t r = secp256k1_rand_int(range);
  409. CHECK(r < range);
  410. r = r % subrange;
  411. x |= (((uint64_t)1) << r);
  412. }
  413. /* Test that the lower subrange bits of x are 1. */
  414. CHECK(((~x) << (64 - subrange)) == 0);
  415. }
  416. void run_rand_bits(void) {
  417. size_t b;
  418. test_rand_bits(1, 32);
  419. for (b = 1; b <= 32; b++) {
  420. test_rand_bits(0, b);
  421. }
  422. }
  423. void run_rand_int(void) {
  424. static const uint32_t ms[] = {1, 3, 17, 1000, 13771, 999999, 33554432};
  425. static const uint32_t ss[] = {1, 3, 6, 9, 13, 31, 64};
  426. unsigned int m, s;
  427. for (m = 0; m < sizeof(ms) / sizeof(ms[0]); m++) {
  428. for (s = 0; s < sizeof(ss) / sizeof(ss[0]); s++) {
  429. test_rand_int(ms[m] * ss[s], ss[s]);
  430. }
  431. }
  432. }
  433. /***** NUM TESTS *****/
  434. #ifndef USE_NUM_NONE
  435. void random_num_negate(secp256k1_num *num) {
  436. if (secp256k1_rand_bits(1)) {
  437. secp256k1_num_negate(num);
  438. }
  439. }
  440. void random_num_order_test(secp256k1_num *num) {
  441. secp256k1_scalar sc;
  442. random_scalar_order_test(&sc);
  443. secp256k1_scalar_get_num(num, &sc);
  444. }
  445. void random_num_order(secp256k1_num *num) {
  446. secp256k1_scalar sc;
  447. random_scalar_order(&sc);
  448. secp256k1_scalar_get_num(num, &sc);
  449. }
  450. void test_num_negate(void) {
  451. secp256k1_num n1;
  452. secp256k1_num n2;
  453. random_num_order_test(&n1); /* n1 = R */
  454. random_num_negate(&n1);
  455. secp256k1_num_copy(&n2, &n1); /* n2 = R */
  456. secp256k1_num_sub(&n1, &n2, &n1); /* n1 = n2-n1 = 0 */
  457. CHECK(secp256k1_num_is_zero(&n1));
  458. secp256k1_num_copy(&n1, &n2); /* n1 = R */
  459. secp256k1_num_negate(&n1); /* n1 = -R */
  460. CHECK(!secp256k1_num_is_zero(&n1));
  461. secp256k1_num_add(&n1, &n2, &n1); /* n1 = n2+n1 = 0 */
  462. CHECK(secp256k1_num_is_zero(&n1));
  463. secp256k1_num_copy(&n1, &n2); /* n1 = R */
  464. secp256k1_num_negate(&n1); /* n1 = -R */
  465. CHECK(secp256k1_num_is_neg(&n1) != secp256k1_num_is_neg(&n2));
  466. secp256k1_num_negate(&n1); /* n1 = R */
  467. CHECK(secp256k1_num_eq(&n1, &n2));
  468. }
  469. void test_num_add_sub(void) {
  470. int i;
  471. secp256k1_scalar s;
  472. secp256k1_num n1;
  473. secp256k1_num n2;
  474. secp256k1_num n1p2, n2p1, n1m2, n2m1;
  475. random_num_order_test(&n1); /* n1 = R1 */
  476. if (secp256k1_rand_bits(1)) {
  477. random_num_negate(&n1);
  478. }
  479. random_num_order_test(&n2); /* n2 = R2 */
  480. if (secp256k1_rand_bits(1)) {
  481. random_num_negate(&n2);
  482. }
  483. secp256k1_num_add(&n1p2, &n1, &n2); /* n1p2 = R1 + R2 */
  484. secp256k1_num_add(&n2p1, &n2, &n1); /* n2p1 = R2 + R1 */
  485. secp256k1_num_sub(&n1m2, &n1, &n2); /* n1m2 = R1 - R2 */
  486. secp256k1_num_sub(&n2m1, &n2, &n1); /* n2m1 = R2 - R1 */
  487. CHECK(secp256k1_num_eq(&n1p2, &n2p1));
  488. CHECK(!secp256k1_num_eq(&n1p2, &n1m2));
  489. secp256k1_num_negate(&n2m1); /* n2m1 = -R2 + R1 */
  490. CHECK(secp256k1_num_eq(&n2m1, &n1m2));
  491. CHECK(!secp256k1_num_eq(&n2m1, &n1));
  492. secp256k1_num_add(&n2m1, &n2m1, &n2); /* n2m1 = -R2 + R1 + R2 = R1 */
  493. CHECK(secp256k1_num_eq(&n2m1, &n1));
  494. CHECK(!secp256k1_num_eq(&n2p1, &n1));
  495. secp256k1_num_sub(&n2p1, &n2p1, &n2); /* n2p1 = R2 + R1 - R2 = R1 */
  496. CHECK(secp256k1_num_eq(&n2p1, &n1));
  497. /* check is_one */
  498. secp256k1_scalar_set_int(&s, 1);
  499. secp256k1_scalar_get_num(&n1, &s);
  500. CHECK(secp256k1_num_is_one(&n1));
  501. /* check that 2^n + 1 is never 1 */
  502. secp256k1_scalar_get_num(&n2, &s);
  503. for (i = 0; i < 250; ++i) {
  504. secp256k1_num_add(&n1, &n1, &n1); /* n1 *= 2 */
  505. secp256k1_num_add(&n1p2, &n1, &n2); /* n1p2 = n1 + 1 */
  506. CHECK(!secp256k1_num_is_one(&n1p2));
  507. }
  508. }
  509. void test_num_mod(void) {
  510. int i;
  511. secp256k1_scalar s;
  512. secp256k1_num order, n;
  513. /* check that 0 mod anything is 0 */
  514. random_scalar_order_test(&s);
  515. secp256k1_scalar_get_num(&order, &s);
  516. secp256k1_scalar_set_int(&s, 0);
  517. secp256k1_scalar_get_num(&n, &s);
  518. secp256k1_num_mod(&n, &order);
  519. CHECK(secp256k1_num_is_zero(&n));
  520. /* check that anything mod 1 is 0 */
  521. secp256k1_scalar_set_int(&s, 1);
  522. secp256k1_scalar_get_num(&order, &s);
  523. secp256k1_scalar_get_num(&n, &s);
  524. secp256k1_num_mod(&n, &order);
  525. CHECK(secp256k1_num_is_zero(&n));
  526. /* check that increasing the number past 2^256 does not break this */
  527. random_scalar_order_test(&s);
  528. secp256k1_scalar_get_num(&n, &s);
  529. /* multiply by 2^8, which'll test this case with high probability */
  530. for (i = 0; i < 8; ++i) {
  531. secp256k1_num_add(&n, &n, &n);
  532. }
  533. secp256k1_num_mod(&n, &order);
  534. CHECK(secp256k1_num_is_zero(&n));
  535. }
  536. void test_num_jacobi(void) {
  537. secp256k1_scalar sqr;
  538. secp256k1_scalar small;
  539. secp256k1_scalar five; /* five is not a quadratic residue */
  540. secp256k1_num order, n;
  541. int i;
  542. /* squares mod 5 are 1, 4 */
  543. const int jacobi5[10] = { 0, 1, -1, -1, 1, 0, 1, -1, -1, 1 };
  544. /* check some small values with 5 as the order */
  545. secp256k1_scalar_set_int(&five, 5);
  546. secp256k1_scalar_get_num(&order, &five);
  547. for (i = 0; i < 10; ++i) {
  548. secp256k1_scalar_set_int(&small, i);
  549. secp256k1_scalar_get_num(&n, &small);
  550. CHECK(secp256k1_num_jacobi(&n, &order) == jacobi5[i]);
  551. }
  552. /** test large values with 5 as group order */
  553. secp256k1_scalar_get_num(&order, &five);
  554. /* we first need a scalar which is not a multiple of 5 */
  555. do {
  556. secp256k1_num fiven;
  557. random_scalar_order_test(&sqr);
  558. secp256k1_scalar_get_num(&fiven, &five);
  559. secp256k1_scalar_get_num(&n, &sqr);
  560. secp256k1_num_mod(&n, &fiven);
  561. } while (secp256k1_num_is_zero(&n));
  562. /* next force it to be a residue. 2 is a nonresidue mod 5 so we can
  563. * just multiply by two, i.e. add the number to itself */
  564. if (secp256k1_num_jacobi(&n, &order) == -1) {
  565. secp256k1_num_add(&n, &n, &n);
  566. }
  567. /* test residue */
  568. CHECK(secp256k1_num_jacobi(&n, &order) == 1);
  569. /* test nonresidue */
  570. secp256k1_num_add(&n, &n, &n);
  571. CHECK(secp256k1_num_jacobi(&n, &order) == -1);
  572. /** test with secp group order as order */
  573. secp256k1_scalar_order_get_num(&order);
  574. random_scalar_order_test(&sqr);
  575. secp256k1_scalar_sqr(&sqr, &sqr);
  576. /* test residue */
  577. secp256k1_scalar_get_num(&n, &sqr);
  578. CHECK(secp256k1_num_jacobi(&n, &order) == 1);
  579. /* test nonresidue */
  580. secp256k1_scalar_mul(&sqr, &sqr, &five);
  581. secp256k1_scalar_get_num(&n, &sqr);
  582. CHECK(secp256k1_num_jacobi(&n, &order) == -1);
  583. /* test multiple of the order*/
  584. CHECK(secp256k1_num_jacobi(&order, &order) == 0);
  585. /* check one less than the order */
  586. secp256k1_scalar_set_int(&small, 1);
  587. secp256k1_scalar_get_num(&n, &small);
  588. secp256k1_num_sub(&n, &order, &n);
  589. CHECK(secp256k1_num_jacobi(&n, &order) == 1); /* sage confirms this is 1 */
  590. }
  591. void run_num_smalltests(void) {
  592. int i;
  593. for (i = 0; i < 100*count; i++) {
  594. test_num_negate();
  595. test_num_add_sub();
  596. test_num_mod();
  597. test_num_jacobi();
  598. }
  599. }
  600. #endif
  601. /***** SCALAR TESTS *****/
  602. void scalar_test(void) {
  603. secp256k1_scalar s;
  604. secp256k1_scalar s1;
  605. secp256k1_scalar s2;
  606. #ifndef USE_NUM_NONE
  607. secp256k1_num snum, s1num, s2num;
  608. secp256k1_num order, half_order;
  609. #endif
  610. unsigned char c[32];
  611. /* Set 's' to a random scalar, with value 'snum'. */
  612. random_scalar_order_test(&s);
  613. /* Set 's1' to a random scalar, with value 's1num'. */
  614. random_scalar_order_test(&s1);
  615. /* Set 's2' to a random scalar, with value 'snum2', and byte array representation 'c'. */
  616. random_scalar_order_test(&s2);
  617. secp256k1_scalar_get_b32(c, &s2);
  618. #ifndef USE_NUM_NONE
  619. secp256k1_scalar_get_num(&snum, &s);
  620. secp256k1_scalar_get_num(&s1num, &s1);
  621. secp256k1_scalar_get_num(&s2num, &s2);
  622. secp256k1_scalar_order_get_num(&order);
  623. half_order = order;
  624. secp256k1_num_shift(&half_order, 1);
  625. #endif
  626. {
  627. int i;
  628. /* Test that fetching groups of 4 bits from a scalar and recursing n(i)=16*n(i-1)+p(i) reconstructs it. */
  629. secp256k1_scalar n;
  630. secp256k1_scalar_set_int(&n, 0);
  631. for (i = 0; i < 256; i += 4) {
  632. secp256k1_scalar t;
  633. int j;
  634. secp256k1_scalar_set_int(&t, secp256k1_scalar_get_bits(&s, 256 - 4 - i, 4));
  635. for (j = 0; j < 4; j++) {
  636. secp256k1_scalar_add(&n, &n, &n);
  637. }
  638. secp256k1_scalar_add(&n, &n, &t);
  639. }
  640. CHECK(secp256k1_scalar_eq(&n, &s));
  641. }
  642. {
  643. /* Test that fetching groups of randomly-sized bits from a scalar and recursing n(i)=b*n(i-1)+p(i) reconstructs it. */
  644. secp256k1_scalar n;
  645. int i = 0;
  646. secp256k1_scalar_set_int(&n, 0);
  647. while (i < 256) {
  648. secp256k1_scalar t;
  649. int j;
  650. int now = secp256k1_rand_int(15) + 1;
  651. if (now + i > 256) {
  652. now = 256 - i;
  653. }
  654. secp256k1_scalar_set_int(&t, secp256k1_scalar_get_bits_var(&s, 256 - now - i, now));
  655. for (j = 0; j < now; j++) {
  656. secp256k1_scalar_add(&n, &n, &n);
  657. }
  658. secp256k1_scalar_add(&n, &n, &t);
  659. i += now;
  660. }
  661. CHECK(secp256k1_scalar_eq(&n, &s));
  662. }
  663. #ifndef USE_NUM_NONE
  664. {
  665. /* Test that adding the scalars together is equal to adding their numbers together modulo the order. */
  666. secp256k1_num rnum;
  667. secp256k1_num r2num;
  668. secp256k1_scalar r;
  669. secp256k1_num_add(&rnum, &snum, &s2num);
  670. secp256k1_num_mod(&rnum, &order);
  671. secp256k1_scalar_add(&r, &s, &s2);
  672. secp256k1_scalar_get_num(&r2num, &r);
  673. CHECK(secp256k1_num_eq(&rnum, &r2num));
  674. }
  675. {
  676. /* Test that multiplying the scalars is equal to multiplying their numbers modulo the order. */
  677. secp256k1_scalar r;
  678. secp256k1_num r2num;
  679. secp256k1_num rnum;
  680. secp256k1_num_mul(&rnum, &snum, &s2num);
  681. secp256k1_num_mod(&rnum, &order);
  682. secp256k1_scalar_mul(&r, &s, &s2);
  683. secp256k1_scalar_get_num(&r2num, &r);
  684. CHECK(secp256k1_num_eq(&rnum, &r2num));
  685. /* The result can only be zero if at least one of the factors was zero. */
  686. CHECK(secp256k1_scalar_is_zero(&r) == (secp256k1_scalar_is_zero(&s) || secp256k1_scalar_is_zero(&s2)));
  687. /* The results can only be equal to one of the factors if that factor was zero, or the other factor was one. */
  688. CHECK(secp256k1_num_eq(&rnum, &snum) == (secp256k1_scalar_is_zero(&s) || secp256k1_scalar_is_one(&s2)));
  689. CHECK(secp256k1_num_eq(&rnum, &s2num) == (secp256k1_scalar_is_zero(&s2) || secp256k1_scalar_is_one(&s)));
  690. }
  691. {
  692. secp256k1_scalar neg;
  693. secp256k1_num negnum;
  694. secp256k1_num negnum2;
  695. /* Check that comparison with zero matches comparison with zero on the number. */
  696. CHECK(secp256k1_num_is_zero(&snum) == secp256k1_scalar_is_zero(&s));
  697. /* Check that comparison with the half order is equal to testing for high scalar. */
  698. CHECK(secp256k1_scalar_is_high(&s) == (secp256k1_num_cmp(&snum, &half_order) > 0));
  699. secp256k1_scalar_negate(&neg, &s);
  700. secp256k1_num_sub(&negnum, &order, &snum);
  701. secp256k1_num_mod(&negnum, &order);
  702. /* Check that comparison with the half order is equal to testing for high scalar after negation. */
  703. CHECK(secp256k1_scalar_is_high(&neg) == (secp256k1_num_cmp(&negnum, &half_order) > 0));
  704. /* Negating should change the high property, unless the value was already zero. */
  705. CHECK((secp256k1_scalar_is_high(&s) == secp256k1_scalar_is_high(&neg)) == secp256k1_scalar_is_zero(&s));
  706. secp256k1_scalar_get_num(&negnum2, &neg);
  707. /* Negating a scalar should be equal to (order - n) mod order on the number. */
  708. CHECK(secp256k1_num_eq(&negnum, &negnum2));
  709. secp256k1_scalar_add(&neg, &neg, &s);
  710. /* Adding a number to its negation should result in zero. */
  711. CHECK(secp256k1_scalar_is_zero(&neg));
  712. secp256k1_scalar_negate(&neg, &neg);
  713. /* Negating zero should still result in zero. */
  714. CHECK(secp256k1_scalar_is_zero(&neg));
  715. }
  716. {
  717. /* Test secp256k1_scalar_mul_shift_var. */
  718. secp256k1_scalar r;
  719. secp256k1_num one;
  720. secp256k1_num rnum;
  721. secp256k1_num rnum2;
  722. unsigned char cone[1] = {0x01};
  723. unsigned int shift = 256 + secp256k1_rand_int(257);
  724. secp256k1_scalar_mul_shift_var(&r, &s1, &s2, shift);
  725. secp256k1_num_mul(&rnum, &s1num, &s2num);
  726. secp256k1_num_shift(&rnum, shift - 1);
  727. secp256k1_num_set_bin(&one, cone, 1);
  728. secp256k1_num_add(&rnum, &rnum, &one);
  729. secp256k1_num_shift(&rnum, 1);
  730. secp256k1_scalar_get_num(&rnum2, &r);
  731. CHECK(secp256k1_num_eq(&rnum, &rnum2));
  732. }
  733. {
  734. /* test secp256k1_scalar_shr_int */
  735. secp256k1_scalar r;
  736. int i;
  737. random_scalar_order_test(&r);
  738. for (i = 0; i < 100; ++i) {
  739. int low;
  740. int shift = 1 + secp256k1_rand_int(15);
  741. int expected = r.d[0] % (1 << shift);
  742. low = secp256k1_scalar_shr_int(&r, shift);
  743. CHECK(expected == low);
  744. }
  745. }
  746. #endif
  747. {
  748. /* Test that scalar inverses are equal to the inverse of their number modulo the order. */
  749. if (!secp256k1_scalar_is_zero(&s)) {
  750. secp256k1_scalar inv;
  751. #ifndef USE_NUM_NONE
  752. secp256k1_num invnum;
  753. secp256k1_num invnum2;
  754. #endif
  755. secp256k1_scalar_inverse(&inv, &s);
  756. #ifndef USE_NUM_NONE
  757. secp256k1_num_mod_inverse(&invnum, &snum, &order);
  758. secp256k1_scalar_get_num(&invnum2, &inv);
  759. CHECK(secp256k1_num_eq(&invnum, &invnum2));
  760. #endif
  761. secp256k1_scalar_mul(&inv, &inv, &s);
  762. /* Multiplying a scalar with its inverse must result in one. */
  763. CHECK(secp256k1_scalar_is_one(&inv));
  764. secp256k1_scalar_inverse(&inv, &inv);
  765. /* Inverting one must result in one. */
  766. CHECK(secp256k1_scalar_is_one(&inv));
  767. #ifndef USE_NUM_NONE
  768. secp256k1_scalar_get_num(&invnum, &inv);
  769. CHECK(secp256k1_num_is_one(&invnum));
  770. #endif
  771. }
  772. }
  773. {
  774. /* Test commutativity of add. */
  775. secp256k1_scalar r1, r2;
  776. secp256k1_scalar_add(&r1, &s1, &s2);
  777. secp256k1_scalar_add(&r2, &s2, &s1);
  778. CHECK(secp256k1_scalar_eq(&r1, &r2));
  779. }
  780. {
  781. secp256k1_scalar r1, r2;
  782. secp256k1_scalar b;
  783. int i;
  784. /* Test add_bit. */
  785. int bit = secp256k1_rand_bits(8);
  786. secp256k1_scalar_set_int(&b, 1);
  787. CHECK(secp256k1_scalar_is_one(&b));
  788. for (i = 0; i < bit; i++) {
  789. secp256k1_scalar_add(&b, &b, &b);
  790. }
  791. r1 = s1;
  792. r2 = s1;
  793. if (!secp256k1_scalar_add(&r1, &r1, &b)) {
  794. /* No overflow happened. */
  795. secp256k1_scalar_cadd_bit(&r2, bit, 1);
  796. CHECK(secp256k1_scalar_eq(&r1, &r2));
  797. /* cadd is a noop when flag is zero */
  798. secp256k1_scalar_cadd_bit(&r2, bit, 0);
  799. CHECK(secp256k1_scalar_eq(&r1, &r2));
  800. }
  801. }
  802. {
  803. /* Test commutativity of mul. */
  804. secp256k1_scalar r1, r2;
  805. secp256k1_scalar_mul(&r1, &s1, &s2);
  806. secp256k1_scalar_mul(&r2, &s2, &s1);
  807. CHECK(secp256k1_scalar_eq(&r1, &r2));
  808. }
  809. {
  810. /* Test associativity of add. */
  811. secp256k1_scalar r1, r2;
  812. secp256k1_scalar_add(&r1, &s1, &s2);
  813. secp256k1_scalar_add(&r1, &r1, &s);
  814. secp256k1_scalar_add(&r2, &s2, &s);
  815. secp256k1_scalar_add(&r2, &s1, &r2);
  816. CHECK(secp256k1_scalar_eq(&r1, &r2));
  817. }
  818. {
  819. /* Test associativity of mul. */
  820. secp256k1_scalar r1, r2;
  821. secp256k1_scalar_mul(&r1, &s1, &s2);
  822. secp256k1_scalar_mul(&r1, &r1, &s);
  823. secp256k1_scalar_mul(&r2, &s2, &s);
  824. secp256k1_scalar_mul(&r2, &s1, &r2);
  825. CHECK(secp256k1_scalar_eq(&r1, &r2));
  826. }
  827. {
  828. /* Test distributitivity of mul over add. */
  829. secp256k1_scalar r1, r2, t;
  830. secp256k1_scalar_add(&r1, &s1, &s2);
  831. secp256k1_scalar_mul(&r1, &r1, &s);
  832. secp256k1_scalar_mul(&r2, &s1, &s);
  833. secp256k1_scalar_mul(&t, &s2, &s);
  834. secp256k1_scalar_add(&r2, &r2, &t);
  835. CHECK(secp256k1_scalar_eq(&r1, &r2));
  836. }
  837. {
  838. /* Test square. */
  839. secp256k1_scalar r1, r2;
  840. secp256k1_scalar_sqr(&r1, &s1);
  841. secp256k1_scalar_mul(&r2, &s1, &s1);
  842. CHECK(secp256k1_scalar_eq(&r1, &r2));
  843. }
  844. {
  845. /* Test multiplicative identity. */
  846. secp256k1_scalar r1, v1;
  847. secp256k1_scalar_set_int(&v1,1);
  848. secp256k1_scalar_mul(&r1, &s1, &v1);
  849. CHECK(secp256k1_scalar_eq(&r1, &s1));
  850. }
  851. {
  852. /* Test additive identity. */
  853. secp256k1_scalar r1, v0;
  854. secp256k1_scalar_set_int(&v0,0);
  855. secp256k1_scalar_add(&r1, &s1, &v0);
  856. CHECK(secp256k1_scalar_eq(&r1, &s1));
  857. }
  858. {
  859. /* Test zero product property. */
  860. secp256k1_scalar r1, v0;
  861. secp256k1_scalar_set_int(&v0,0);
  862. secp256k1_scalar_mul(&r1, &s1, &v0);
  863. CHECK(secp256k1_scalar_eq(&r1, &v0));
  864. }
  865. }
  866. void run_scalar_tests(void) {
  867. int i;
  868. for (i = 0; i < 128 * count; i++) {
  869. scalar_test();
  870. }
  871. {
  872. /* (-1)+1 should be zero. */
  873. secp256k1_scalar s, o;
  874. secp256k1_scalar_set_int(&s, 1);
  875. CHECK(secp256k1_scalar_is_one(&s));
  876. secp256k1_scalar_negate(&o, &s);
  877. secp256k1_scalar_add(&o, &o, &s);
  878. CHECK(secp256k1_scalar_is_zero(&o));
  879. secp256k1_scalar_negate(&o, &o);
  880. CHECK(secp256k1_scalar_is_zero(&o));
  881. }
  882. #ifndef USE_NUM_NONE
  883. {
  884. /* A scalar with value of the curve order should be 0. */
  885. secp256k1_num order;
  886. secp256k1_scalar zero;
  887. unsigned char bin[32];
  888. int overflow = 0;
  889. secp256k1_scalar_order_get_num(&order);
  890. secp256k1_num_get_bin(bin, 32, &order);
  891. secp256k1_scalar_set_b32(&zero, bin, &overflow);
  892. CHECK(overflow == 1);
  893. CHECK(secp256k1_scalar_is_zero(&zero));
  894. }
  895. #endif
  896. {
  897. /* Does check_overflow check catch all ones? */
  898. static const secp256k1_scalar overflowed = SECP256K1_SCALAR_CONST(
  899. 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
  900. 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
  901. );
  902. CHECK(secp256k1_scalar_check_overflow(&overflowed));
  903. }
  904. {
  905. /* Static test vectors.
  906. * These were reduced from ~10^12 random vectors based on comparison-decision
  907. * and edge-case coverage on 32-bit and 64-bit implementations.
  908. * The responses were generated with Sage 5.9.
  909. */
  910. secp256k1_scalar x;
  911. secp256k1_scalar y;
  912. secp256k1_scalar z;
  913. secp256k1_scalar zz;
  914. secp256k1_scalar one;
  915. secp256k1_scalar r1;
  916. secp256k1_scalar r2;
  917. #if defined(USE_SCALAR_INV_NUM)
  918. secp256k1_scalar zzv;
  919. #endif
  920. int overflow;
  921. unsigned char chal[33][2][32] = {
  922. {{0xff, 0xff, 0x03, 0x07, 0x00, 0x00, 0x00, 0x00,
  923. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
  924. 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff,
  925. 0xff, 0xff, 0x03, 0x00, 0xc0, 0xff, 0xff, 0xff},
  926. {0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00,
  927. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8,
  928. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  929. 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff}},
  930. {{0xef, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00,
  931. 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00,
  932. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  933. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  934. {0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  935. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
  936. 0xff, 0xff, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff,
  937. 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x80, 0xff}},
  938. {{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
  940. 0x80, 0x00, 0x00, 0x80, 0xff, 0x3f, 0x00, 0x00,
  941. 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0x00},
  942. {0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0x80,
  943. 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0xe0,
  944. 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00,
  945. 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff}},
  946. {{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  947. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80,
  948. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  949. 0x00, 0x1e, 0xf8, 0xff, 0xff, 0xff, 0xfd, 0xff},
  950. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
  951. 0x00, 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, 0xe0,
  952. 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff,
  953. 0xf3, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}},
  954. {{0x80, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0x00,
  955. 0x00, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
  956. 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0x00,
  957. 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff},
  958. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00,
  959. 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  960. 0xff, 0x1f, 0x00, 0x00, 0x80, 0xff, 0xff, 0x3f,
  961. 0x00, 0xfe, 0xff, 0xff, 0xff, 0xdf, 0xff, 0xff}},
  962. {{0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xfc, 0x9f,
  963. 0xff, 0xff, 0xff, 0x00, 0x80, 0x00, 0x00, 0x80,
  964. 0xff, 0x0f, 0xfc, 0xff, 0x7f, 0x00, 0x00, 0x00,
  965. 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00},
  966. {0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  967. 0x00, 0x00, 0xf8, 0xff, 0x0f, 0xc0, 0xff, 0xff,
  968. 0xff, 0x1f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff,
  969. 0xff, 0xff, 0xff, 0x07, 0x80, 0xff, 0xff, 0xff}},
  970. {{0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x00,
  971. 0x80, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff,
  972. 0xf7, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0x00,
  973. 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xf0},
  974. {0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
  975. 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00,
  976. 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff,
  977. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  978. {{0x00, 0xf8, 0xff, 0x03, 0xff, 0xff, 0xff, 0x00,
  979. 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  980. 0x80, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff,
  981. 0xff, 0xff, 0x03, 0xc0, 0xff, 0x0f, 0xfc, 0xff},
  982. {0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff,
  983. 0xff, 0x01, 0x00, 0x00, 0x00, 0x3f, 0x00, 0xc0,
  984. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  985. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  986. {{0x8f, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  987. 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff,
  988. 0xff, 0x7f, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80,
  989. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00},
  990. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  991. 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  992. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  993. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
  994. {{0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff,
  995. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  996. 0xff, 0xff, 0x03, 0x00, 0x80, 0x00, 0x00, 0x80,
  997. 0xff, 0xff, 0xff, 0x00, 0x00, 0x80, 0xff, 0x7f},
  998. {0xff, 0xcf, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00,
  999. 0x00, 0xc0, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff,
  1000. 0xbf, 0xff, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
  1001. 0x80, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00}},
  1002. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff,
  1003. 0xff, 0xff, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff,
  1004. 0xff, 0xff, 0xff, 0x00, 0x80, 0x00, 0x00, 0x80,
  1005. 0xff, 0x01, 0xfc, 0xff, 0x01, 0x00, 0xfe, 0xff},
  1006. {0xff, 0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00,
  1007. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1008. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
  1009. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00}},
  1010. {{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  1011. 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1012. 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1013. 0x7f, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80},
  1014. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1015. 0x00, 0xf8, 0xff, 0x01, 0x00, 0xf0, 0xff, 0xff,
  1016. 0xe0, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
  1017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
  1018. {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1019. 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1020. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1021. 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x00},
  1022. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
  1023. 0xfc, 0xff, 0xff, 0x3f, 0xf0, 0xff, 0xff, 0x3f,
  1024. 0x00, 0x00, 0xf8, 0x07, 0x00, 0x00, 0x00, 0xff,
  1025. 0xff, 0xff, 0xff, 0xff, 0x0f, 0x7e, 0x00, 0x00}},
  1026. {{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1027. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80,
  1028. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1029. 0xff, 0xff, 0x1f, 0x00, 0x00, 0xfe, 0x07, 0x00},
  1030. {0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff,
  1031. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1032. 0xff, 0xfb, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00,
  1033. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60}},
  1034. {{0xff, 0x01, 0x00, 0xff, 0xff, 0xff, 0x0f, 0x00,
  1035. 0x80, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x03,
  1036. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1037. 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  1038. {0xff, 0xff, 0x1f, 0x00, 0xf0, 0xff, 0xff, 0xff,
  1039. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1040. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1041. 0xff, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00}},
  1042. {{0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  1043. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1044. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1045. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  1046. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1047. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff,
  1048. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
  1049. 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff}},
  1050. {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1051. 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1052. 0xc0, 0xff, 0xff, 0xcf, 0xff, 0x1f, 0x00, 0x00,
  1053. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
  1054. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1055. 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff,
  1056. 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x7e,
  1057. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
  1058. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1059. 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff,
  1060. 0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  1061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00},
  1062. {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  1063. 0xff, 0xff, 0x7f, 0x00, 0x80, 0x00, 0x00, 0x00,
  1064. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1065. 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff}},
  1066. {{0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x80,
  1067. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1068. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  1069. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00},
  1070. {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1071. 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x80,
  1072. 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  1073. 0xff, 0x7f, 0xf8, 0xff, 0xff, 0x1f, 0x00, 0xfe}},
  1074. {{0xff, 0xff, 0xff, 0x3f, 0xf8, 0xff, 0xff, 0xff,
  1075. 0xff, 0x03, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x00,
  1076. 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1077. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07},
  1078. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1079. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  1080. 0xff, 0xff, 0xff, 0xff, 0x01, 0x80, 0xff, 0xff,
  1081. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}},
  1082. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1083. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1084. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1085. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  1086. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1087. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  1088. 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
  1089. 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x40}},
  1090. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1091. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1092. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1093. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
  1094. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1095. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1096. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1097. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
  1098. {{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1099. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1100. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1101. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  1102. {0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1103. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1104. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1105. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  1106. {{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xc0,
  1107. 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1108. 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
  1109. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
  1110. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  1111. 0xf0, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0x00,
  1112. 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff,
  1113. 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff}},
  1114. {{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1115. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1116. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1117. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  1118. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}},
  1122. {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1123. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  1124. 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
  1125. 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x40},
  1126. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}},
  1130. {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1131. 0x7e, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x07, 0x00,
  1132. 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  1133. 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  1134. {0xff, 0x01, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff,
  1135. 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x80,
  1136. 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0x00,
  1137. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  1138. {{0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x00,
  1139. 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1140. 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
  1141. 0x80, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff},
  1142. {0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff,
  1143. 0xff, 0xff, 0x3f, 0x00, 0xf8, 0xff, 0xff, 0xff,
  1144. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1145. 0xff, 0x3f, 0x00, 0x00, 0xc0, 0xf1, 0x7f, 0x00}},
  1146. {{0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  1147. 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff,
  1148. 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  1149. 0x80, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0x00},
  1150. {0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
  1151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff,
  1152. 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f,
  1153. 0x00, 0x00, 0xfc, 0xff, 0xff, 0x01, 0xff, 0xff}},
  1154. {{0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1155. 0x80, 0x00, 0x00, 0x80, 0xff, 0x03, 0xe0, 0x01,
  1156. 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xfc, 0xff,
  1157. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00},
  1158. {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  1159. 0xfe, 0xff, 0xff, 0xf0, 0x07, 0x00, 0x3c, 0x80,
  1160. 0xff, 0xff, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff,
  1161. 0xff, 0xff, 0x07, 0xe0, 0xff, 0x00, 0x00, 0x00}},
  1162. {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  1163. 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1164. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0xf8,
  1165. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80},
  1166. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1167. 0xff, 0xff, 0xff, 0xff, 0xff, 0x0c, 0x80, 0x00,
  1168. 0x00, 0x00, 0x00, 0xc0, 0x7f, 0xfe, 0xff, 0x1f,
  1169. 0x00, 0xfe, 0xff, 0x03, 0x00, 0x00, 0xfe, 0xff}},
  1170. {{0xff, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0x00,
  1171. 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x83,
  1172. 0xff, 0xff, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80,
  1173. 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xf0},
  1174. {0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff,
  1175. 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00,
  1176. 0xf8, 0x07, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff,
  1177. 0xff, 0xc7, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff}},
  1178. {{0x82, 0xc9, 0xfa, 0xb0, 0x68, 0x04, 0xa0, 0x00,
  1179. 0x82, 0xc9, 0xfa, 0xb0, 0x68, 0x04, 0xa0, 0x00,
  1180. 0xff, 0xff, 0xff, 0xff, 0xff, 0x6f, 0x03, 0xfb,
  1181. 0xfa, 0x8a, 0x7d, 0xdf, 0x13, 0x86, 0xe2, 0x03},
  1182. {0x82, 0xc9, 0xfa, 0xb0, 0x68, 0x04, 0xa0, 0x00,
  1183. 0x82, 0xc9, 0xfa, 0xb0, 0x68, 0x04, 0xa0, 0x00,
  1184. 0xff, 0xff, 0xff, 0xff, 0xff, 0x6f, 0x03, 0xfb,
  1185. 0xfa, 0x8a, 0x7d, 0xdf, 0x13, 0x86, 0xe2, 0x03}}
  1186. };
  1187. unsigned char res[33][2][32] = {
  1188. {{0x0c, 0x3b, 0x0a, 0xca, 0x8d, 0x1a, 0x2f, 0xb9,
  1189. 0x8a, 0x7b, 0x53, 0x5a, 0x1f, 0xc5, 0x22, 0xa1,
  1190. 0x07, 0x2a, 0x48, 0xea, 0x02, 0xeb, 0xb3, 0xd6,
  1191. 0x20, 0x1e, 0x86, 0xd0, 0x95, 0xf6, 0x92, 0x35},
  1192. {0xdc, 0x90, 0x7a, 0x07, 0x2e, 0x1e, 0x44, 0x6d,
  1193. 0xf8, 0x15, 0x24, 0x5b, 0x5a, 0x96, 0x37, 0x9c,
  1194. 0x37, 0x7b, 0x0d, 0xac, 0x1b, 0x65, 0x58, 0x49,
  1195. 0x43, 0xb7, 0x31, 0xbb, 0xa7, 0xf4, 0x97, 0x15}},
  1196. {{0xf1, 0xf7, 0x3a, 0x50, 0xe6, 0x10, 0xba, 0x22,
  1197. 0x43, 0x4d, 0x1f, 0x1f, 0x7c, 0x27, 0xca, 0x9c,
  1198. 0xb8, 0xb6, 0xa0, 0xfc, 0xd8, 0xc0, 0x05, 0x2f,
  1199. 0xf7, 0x08, 0xe1, 0x76, 0xdd, 0xd0, 0x80, 0xc8},
  1200. {0xe3, 0x80, 0x80, 0xb8, 0xdb, 0xe3, 0xa9, 0x77,
  1201. 0x00, 0xb0, 0xf5, 0x2e, 0x27, 0xe2, 0x68, 0xc4,
  1202. 0x88, 0xe8, 0x04, 0xc1, 0x12, 0xbf, 0x78, 0x59,
  1203. 0xe6, 0xa9, 0x7c, 0xe1, 0x81, 0xdd, 0xb9, 0xd5}},
  1204. {{0x96, 0xe2, 0xee, 0x01, 0xa6, 0x80, 0x31, 0xef,
  1205. 0x5c, 0xd0, 0x19, 0xb4, 0x7d, 0x5f, 0x79, 0xab,
  1206. 0xa1, 0x97, 0xd3, 0x7e, 0x33, 0xbb, 0x86, 0x55,
  1207. 0x60, 0x20, 0x10, 0x0d, 0x94, 0x2d, 0x11, 0x7c},
  1208. {0xcc, 0xab, 0xe0, 0xe8, 0x98, 0x65, 0x12, 0x96,
  1209. 0x38, 0x5a, 0x1a, 0xf2, 0x85, 0x23, 0x59, 0x5f,
  1210. 0xf9, 0xf3, 0xc2, 0x81, 0x70, 0x92, 0x65, 0x12,
  1211. 0x9c, 0x65, 0x1e, 0x96, 0x00, 0xef, 0xe7, 0x63}},
  1212. {{0xac, 0x1e, 0x62, 0xc2, 0x59, 0xfc, 0x4e, 0x5c,
  1213. 0x83, 0xb0, 0xd0, 0x6f, 0xce, 0x19, 0xf6, 0xbf,
  1214. 0xa4, 0xb0, 0xe0, 0x53, 0x66, 0x1f, 0xbf, 0xc9,
  1215. 0x33, 0x47, 0x37, 0xa9, 0x3d, 0x5d, 0xb0, 0x48},
  1216. {0x86, 0xb9, 0x2a, 0x7f, 0x8e, 0xa8, 0x60, 0x42,
  1217. 0x26, 0x6d, 0x6e, 0x1c, 0xa2, 0xec, 0xe0, 0xe5,
  1218. 0x3e, 0x0a, 0x33, 0xbb, 0x61, 0x4c, 0x9f, 0x3c,
  1219. 0xd1, 0xdf, 0x49, 0x33, 0xcd, 0x72, 0x78, 0x18}},
  1220. {{0xf7, 0xd3, 0xcd, 0x49, 0x5c, 0x13, 0x22, 0xfb,
  1221. 0x2e, 0xb2, 0x2f, 0x27, 0xf5, 0x8a, 0x5d, 0x74,
  1222. 0xc1, 0x58, 0xc5, 0xc2, 0x2d, 0x9f, 0x52, 0xc6,
  1223. 0x63, 0x9f, 0xba, 0x05, 0x76, 0x45, 0x7a, 0x63},
  1224. {0x8a, 0xfa, 0x55, 0x4d, 0xdd, 0xa3, 0xb2, 0xc3,
  1225. 0x44, 0xfd, 0xec, 0x72, 0xde, 0xef, 0xc0, 0x99,
  1226. 0xf5, 0x9f, 0xe2, 0x52, 0xb4, 0x05, 0x32, 0x58,
  1227. 0x57, 0xc1, 0x8f, 0xea, 0xc3, 0x24, 0x5b, 0x94}},
  1228. {{0x05, 0x83, 0xee, 0xdd, 0x64, 0xf0, 0x14, 0x3b,
  1229. 0xa0, 0x14, 0x4a, 0x3a, 0x41, 0x82, 0x7c, 0xa7,
  1230. 0x2c, 0xaa, 0xb1, 0x76, 0xbb, 0x59, 0x64, 0x5f,
  1231. 0x52, 0xad, 0x25, 0x29, 0x9d, 0x8f, 0x0b, 0xb0},
  1232. {0x7e, 0xe3, 0x7c, 0xca, 0xcd, 0x4f, 0xb0, 0x6d,
  1233. 0x7a, 0xb2, 0x3e, 0xa0, 0x08, 0xb9, 0xa8, 0x2d,
  1234. 0xc2, 0xf4, 0x99, 0x66, 0xcc, 0xac, 0xd8, 0xb9,
  1235. 0x72, 0x2a, 0x4a, 0x3e, 0x0f, 0x7b, 0xbf, 0xf4}},
  1236. {{0x8c, 0x9c, 0x78, 0x2b, 0x39, 0x61, 0x7e, 0xf7,
  1237. 0x65, 0x37, 0x66, 0x09, 0x38, 0xb9, 0x6f, 0x70,
  1238. 0x78, 0x87, 0xff, 0xcf, 0x93, 0xca, 0x85, 0x06,
  1239. 0x44, 0x84, 0xa7, 0xfe, 0xd3, 0xa4, 0xe3, 0x7e},
  1240. {0xa2, 0x56, 0x49, 0x23, 0x54, 0xa5, 0x50, 0xe9,
  1241. 0x5f, 0xf0, 0x4d, 0xe7, 0xdc, 0x38, 0x32, 0x79,
  1242. 0x4f, 0x1c, 0xb7, 0xe4, 0xbb, 0xf8, 0xbb, 0x2e,
  1243. 0x40, 0x41, 0x4b, 0xcc, 0xe3, 0x1e, 0x16, 0x36}},
  1244. {{0x0c, 0x1e, 0xd7, 0x09, 0x25, 0x40, 0x97, 0xcb,
  1245. 0x5c, 0x46, 0xa8, 0xda, 0xef, 0x25, 0xd5, 0xe5,
  1246. 0x92, 0x4d, 0xcf, 0xa3, 0xc4, 0x5d, 0x35, 0x4a,
  1247. 0xe4, 0x61, 0x92, 0xf3, 0xbf, 0x0e, 0xcd, 0xbe},
  1248. {0xe4, 0xaf, 0x0a, 0xb3, 0x30, 0x8b, 0x9b, 0x48,
  1249. 0x49, 0x43, 0xc7, 0x64, 0x60, 0x4a, 0x2b, 0x9e,
  1250. 0x95, 0x5f, 0x56, 0xe8, 0x35, 0xdc, 0xeb, 0xdc,
  1251. 0xc7, 0xc4, 0xfe, 0x30, 0x40, 0xc7, 0xbf, 0xa4}},
  1252. {{0xd4, 0xa0, 0xf5, 0x81, 0x49, 0x6b, 0xb6, 0x8b,
  1253. 0x0a, 0x69, 0xf9, 0xfe, 0xa8, 0x32, 0xe5, 0xe0,
  1254. 0xa5, 0xcd, 0x02, 0x53, 0xf9, 0x2c, 0xe3, 0x53,
  1255. 0x83, 0x36, 0xc6, 0x02, 0xb5, 0xeb, 0x64, 0xb8},
  1256. {0x1d, 0x42, 0xb9, 0xf9, 0xe9, 0xe3, 0x93, 0x2c,
  1257. 0x4c, 0xee, 0x6c, 0x5a, 0x47, 0x9e, 0x62, 0x01,
  1258. 0x6b, 0x04, 0xfe, 0xa4, 0x30, 0x2b, 0x0d, 0x4f,
  1259. 0x71, 0x10, 0xd3, 0x55, 0xca, 0xf3, 0x5e, 0x80}},
  1260. {{0x77, 0x05, 0xf6, 0x0c, 0x15, 0x9b, 0x45, 0xe7,
  1261. 0xb9, 0x11, 0xb8, 0xf5, 0xd6, 0xda, 0x73, 0x0c,
  1262. 0xda, 0x92, 0xea, 0xd0, 0x9d, 0xd0, 0x18, 0x92,
  1263. 0xce, 0x9a, 0xaa, 0xee, 0x0f, 0xef, 0xde, 0x30},
  1264. {0xf1, 0xf1, 0xd6, 0x9b, 0x51, 0xd7, 0x77, 0x62,
  1265. 0x52, 0x10, 0xb8, 0x7a, 0x84, 0x9d, 0x15, 0x4e,
  1266. 0x07, 0xdc, 0x1e, 0x75, 0x0d, 0x0c, 0x3b, 0xdb,
  1267. 0x74, 0x58, 0x62, 0x02, 0x90, 0x54, 0x8b, 0x43}},
  1268. {{0xa6, 0xfe, 0x0b, 0x87, 0x80, 0x43, 0x67, 0x25,
  1269. 0x57, 0x5d, 0xec, 0x40, 0x50, 0x08, 0xd5, 0x5d,
  1270. 0x43, 0xd7, 0xe0, 0xaa, 0xe0, 0x13, 0xb6, 0xb0,
  1271. 0xc0, 0xd4, 0xe5, 0x0d, 0x45, 0x83, 0xd6, 0x13},
  1272. {0x40, 0x45, 0x0a, 0x92, 0x31, 0xea, 0x8c, 0x60,
  1273. 0x8c, 0x1f, 0xd8, 0x76, 0x45, 0xb9, 0x29, 0x00,
  1274. 0x26, 0x32, 0xd8, 0xa6, 0x96, 0x88, 0xe2, 0xc4,
  1275. 0x8b, 0xdb, 0x7f, 0x17, 0x87, 0xcc, 0xc8, 0xf2}},
  1276. {{0xc2, 0x56, 0xe2, 0xb6, 0x1a, 0x81, 0xe7, 0x31,
  1277. 0x63, 0x2e, 0xbb, 0x0d, 0x2f, 0x81, 0x67, 0xd4,
  1278. 0x22, 0xe2, 0x38, 0x02, 0x25, 0x97, 0xc7, 0x88,
  1279. 0x6e, 0xdf, 0xbe, 0x2a, 0xa5, 0x73, 0x63, 0xaa},
  1280. {0x50, 0x45, 0xe2, 0xc3, 0xbd, 0x89, 0xfc, 0x57,
  1281. 0xbd, 0x3c, 0xa3, 0x98, 0x7e, 0x7f, 0x36, 0x38,
  1282. 0x92, 0x39, 0x1f, 0x0f, 0x81, 0x1a, 0x06, 0x51,
  1283. 0x1f, 0x8d, 0x6a, 0xff, 0x47, 0x16, 0x06, 0x9c}},
  1284. {{0x33, 0x95, 0xa2, 0x6f, 0x27, 0x5f, 0x9c, 0x9c,
  1285. 0x64, 0x45, 0xcb, 0xd1, 0x3c, 0xee, 0x5e, 0x5f,
  1286. 0x48, 0xa6, 0xaf, 0xe3, 0x79, 0xcf, 0xb1, 0xe2,
  1287. 0xbf, 0x55, 0x0e, 0xa2, 0x3b, 0x62, 0xf0, 0xe4},
  1288. {0x14, 0xe8, 0x06, 0xe3, 0xbe, 0x7e, 0x67, 0x01,
  1289. 0xc5, 0x21, 0x67, 0xd8, 0x54, 0xb5, 0x7f, 0xa4,
  1290. 0xf9, 0x75, 0x70, 0x1c, 0xfd, 0x79, 0xdb, 0x86,
  1291. 0xad, 0x37, 0x85, 0x83, 0x56, 0x4e, 0xf0, 0xbf}},
  1292. {{0xbc, 0xa6, 0xe0, 0x56, 0x4e, 0xef, 0xfa, 0xf5,
  1293. 0x1d, 0x5d, 0x3f, 0x2a, 0x5b, 0x19, 0xab, 0x51,
  1294. 0xc5, 0x8b, 0xdd, 0x98, 0x28, 0x35, 0x2f, 0xc3,
  1295. 0x81, 0x4f, 0x5c, 0xe5, 0x70, 0xb9, 0xeb, 0x62},
  1296. {0xc4, 0x6d, 0x26, 0xb0, 0x17, 0x6b, 0xfe, 0x6c,
  1297. 0x12, 0xf8, 0xe7, 0xc1, 0xf5, 0x2f, 0xfa, 0x91,
  1298. 0x13, 0x27, 0xbd, 0x73, 0xcc, 0x33, 0x31, 0x1c,
  1299. 0x39, 0xe3, 0x27, 0x6a, 0x95, 0xcf, 0xc5, 0xfb}},
  1300. {{0x30, 0xb2, 0x99, 0x84, 0xf0, 0x18, 0x2a, 0x6e,
  1301. 0x1e, 0x27, 0xed, 0xa2, 0x29, 0x99, 0x41, 0x56,
  1302. 0xe8, 0xd4, 0x0d, 0xef, 0x99, 0x9c, 0xf3, 0x58,
  1303. 0x29, 0x55, 0x1a, 0xc0, 0x68, 0xd6, 0x74, 0xa4},
  1304. {0x07, 0x9c, 0xe7, 0xec, 0xf5, 0x36, 0x73, 0x41,
  1305. 0xa3, 0x1c, 0xe5, 0x93, 0x97, 0x6a, 0xfd, 0xf7,
  1306. 0x53, 0x18, 0xab, 0xaf, 0xeb, 0x85, 0xbd, 0x92,
  1307. 0x90, 0xab, 0x3c, 0xbf, 0x30, 0x82, 0xad, 0xf6}},
  1308. {{0xc6, 0x87, 0x8a, 0x2a, 0xea, 0xc0, 0xa9, 0xec,
  1309. 0x6d, 0xd3, 0xdc, 0x32, 0x23, 0xce, 0x62, 0x19,
  1310. 0xa4, 0x7e, 0xa8, 0xdd, 0x1c, 0x33, 0xae, 0xd3,
  1311. 0x4f, 0x62, 0x9f, 0x52, 0xe7, 0x65, 0x46, 0xf4},
  1312. {0x97, 0x51, 0x27, 0x67, 0x2d, 0xa2, 0x82, 0x87,
  1313. 0x98, 0xd3, 0xb6, 0x14, 0x7f, 0x51, 0xd3, 0x9a,
  1314. 0x0b, 0xd0, 0x76, 0x81, 0xb2, 0x4f, 0x58, 0x92,
  1315. 0xa4, 0x86, 0xa1, 0xa7, 0x09, 0x1d, 0xef, 0x9b}},
  1316. {{0xb3, 0x0f, 0x2b, 0x69, 0x0d, 0x06, 0x90, 0x64,
  1317. 0xbd, 0x43, 0x4c, 0x10, 0xe8, 0x98, 0x1c, 0xa3,
  1318. 0xe1, 0x68, 0xe9, 0x79, 0x6c, 0x29, 0x51, 0x3f,
  1319. 0x41, 0xdc, 0xdf, 0x1f, 0xf3, 0x60, 0xbe, 0x33},
  1320. {0xa1, 0x5f, 0xf7, 0x1d, 0xb4, 0x3e, 0x9b, 0x3c,
  1321. 0xe7, 0xbd, 0xb6, 0x06, 0xd5, 0x60, 0x06, 0x6d,
  1322. 0x50, 0xd2, 0xf4, 0x1a, 0x31, 0x08, 0xf2, 0xea,
  1323. 0x8e, 0xef, 0x5f, 0x7d, 0xb6, 0xd0, 0xc0, 0x27}},
  1324. {{0x62, 0x9a, 0xd9, 0xbb, 0x38, 0x36, 0xce, 0xf7,
  1325. 0x5d, 0x2f, 0x13, 0xec, 0xc8, 0x2d, 0x02, 0x8a,
  1326. 0x2e, 0x72, 0xf0, 0xe5, 0x15, 0x9d, 0x72, 0xae,
  1327. 0xfc, 0xb3, 0x4f, 0x02, 0xea, 0xe1, 0x09, 0xfe},
  1328. {0x00, 0x00, 0x00, 0x00, 0xfa, 0x0a, 0x3d, 0xbc,
  1329. 0xad, 0x16, 0x0c, 0xb6, 0xe7, 0x7c, 0x8b, 0x39,
  1330. 0x9a, 0x43, 0xbb, 0xe3, 0xc2, 0x55, 0x15, 0x14,
  1331. 0x75, 0xac, 0x90, 0x9b, 0x7f, 0x9a, 0x92, 0x00}},
  1332. {{0x8b, 0xac, 0x70, 0x86, 0x29, 0x8f, 0x00, 0x23,
  1333. 0x7b, 0x45, 0x30, 0xaa, 0xb8, 0x4c, 0xc7, 0x8d,
  1334. 0x4e, 0x47, 0x85, 0xc6, 0x19, 0xe3, 0x96, 0xc2,
  1335. 0x9a, 0xa0, 0x12, 0xed, 0x6f, 0xd7, 0x76, 0x16},
  1336. {0x45, 0xaf, 0x7e, 0x33, 0xc7, 0x7f, 0x10, 0x6c,
  1337. 0x7c, 0x9f, 0x29, 0xc1, 0xa8, 0x7e, 0x15, 0x84,
  1338. 0xe7, 0x7d, 0xc0, 0x6d, 0xab, 0x71, 0x5d, 0xd0,
  1339. 0x6b, 0x9f, 0x97, 0xab, 0xcb, 0x51, 0x0c, 0x9f}},
  1340. {{0x9e, 0xc3, 0x92, 0xb4, 0x04, 0x9f, 0xc8, 0xbb,
  1341. 0xdd, 0x9e, 0xc6, 0x05, 0xfd, 0x65, 0xec, 0x94,
  1342. 0x7f, 0x2c, 0x16, 0xc4, 0x40, 0xac, 0x63, 0x7b,
  1343. 0x7d, 0xb8, 0x0c, 0xe4, 0x5b, 0xe3, 0xa7, 0x0e},
  1344. {0x43, 0xf4, 0x44, 0xe8, 0xcc, 0xc8, 0xd4, 0x54,
  1345. 0x33, 0x37, 0x50, 0xf2, 0x87, 0x42, 0x2e, 0x00,
  1346. 0x49, 0x60, 0x62, 0x02, 0xfd, 0x1a, 0x7c, 0xdb,
  1347. 0x29, 0x6c, 0x6d, 0x54, 0x53, 0x08, 0xd1, 0xc8}},
  1348. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1349. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1350. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1351. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  1352. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1353. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1354. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1355. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
  1356. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1357. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1358. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1359. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  1360. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1361. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1362. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}},
  1364. {{0x27, 0x59, 0xc7, 0x35, 0x60, 0x71, 0xa6, 0xf1,
  1365. 0x79, 0xa5, 0xfd, 0x79, 0x16, 0xf3, 0x41, 0xf0,
  1366. 0x57, 0xb4, 0x02, 0x97, 0x32, 0xe7, 0xde, 0x59,
  1367. 0xe2, 0x2d, 0x9b, 0x11, 0xea, 0x2c, 0x35, 0x92},
  1368. {0x27, 0x59, 0xc7, 0x35, 0x60, 0x71, 0xa6, 0xf1,
  1369. 0x79, 0xa5, 0xfd, 0x79, 0x16, 0xf3, 0x41, 0xf0,
  1370. 0x57, 0xb4, 0x02, 0x97, 0x32, 0xe7, 0xde, 0x59,
  1371. 0xe2, 0x2d, 0x9b, 0x11, 0xea, 0x2c, 0x35, 0x92}},
  1372. {{0x28, 0x56, 0xac, 0x0e, 0x4f, 0x98, 0x09, 0xf0,
  1373. 0x49, 0xfa, 0x7f, 0x84, 0xac, 0x7e, 0x50, 0x5b,
  1374. 0x17, 0x43, 0x14, 0x89, 0x9c, 0x53, 0xa8, 0x94,
  1375. 0x30, 0xf2, 0x11, 0x4d, 0x92, 0x14, 0x27, 0xe8},
  1376. {0x39, 0x7a, 0x84, 0x56, 0x79, 0x9d, 0xec, 0x26,
  1377. 0x2c, 0x53, 0xc1, 0x94, 0xc9, 0x8d, 0x9e, 0x9d,
  1378. 0x32, 0x1f, 0xdd, 0x84, 0x04, 0xe8, 0xe2, 0x0a,
  1379. 0x6b, 0xbe, 0xbb, 0x42, 0x40, 0x67, 0x30, 0x6c}},
  1380. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1381. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  1382. 0x45, 0x51, 0x23, 0x19, 0x50, 0xb7, 0x5f, 0xc4,
  1383. 0x40, 0x2d, 0xa1, 0x73, 0x2f, 0xc9, 0xbe, 0xbd},
  1384. {0x27, 0x59, 0xc7, 0x35, 0x60, 0x71, 0xa6, 0xf1,
  1385. 0x79, 0xa5, 0xfd, 0x79, 0x16, 0xf3, 0x41, 0xf0,
  1386. 0x57, 0xb4, 0x02, 0x97, 0x32, 0xe7, 0xde, 0x59,
  1387. 0xe2, 0x2d, 0x9b, 0x11, 0xea, 0x2c, 0x35, 0x92}},
  1388. {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1389. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  1390. 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
  1391. 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x40},
  1392. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1393. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1394. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1395. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}},
  1396. {{0x1c, 0xc4, 0xf7, 0xda, 0x0f, 0x65, 0xca, 0x39,
  1397. 0x70, 0x52, 0x92, 0x8e, 0xc3, 0xc8, 0x15, 0xea,
  1398. 0x7f, 0x10, 0x9e, 0x77, 0x4b, 0x6e, 0x2d, 0xdf,
  1399. 0xe8, 0x30, 0x9d, 0xda, 0xe8, 0x9a, 0x65, 0xae},
  1400. {0x02, 0xb0, 0x16, 0xb1, 0x1d, 0xc8, 0x57, 0x7b,
  1401. 0xa2, 0x3a, 0xa2, 0xa3, 0x38, 0x5c, 0x8f, 0xeb,
  1402. 0x66, 0x37, 0x91, 0xa8, 0x5f, 0xef, 0x04, 0xf6,
  1403. 0x59, 0x75, 0xe1, 0xee, 0x92, 0xf6, 0x0e, 0x30}},
  1404. {{0x8d, 0x76, 0x14, 0xa4, 0x14, 0x06, 0x9f, 0x9a,
  1405. 0xdf, 0x4a, 0x85, 0xa7, 0x6b, 0xbf, 0x29, 0x6f,
  1406. 0xbc, 0x34, 0x87, 0x5d, 0xeb, 0xbb, 0x2e, 0xa9,
  1407. 0xc9, 0x1f, 0x58, 0xd6, 0x9a, 0x82, 0xa0, 0x56},
  1408. {0xd4, 0xb9, 0xdb, 0x88, 0x1d, 0x04, 0xe9, 0x93,
  1409. 0x8d, 0x3f, 0x20, 0xd5, 0x86, 0xa8, 0x83, 0x07,
  1410. 0xdb, 0x09, 0xd8, 0x22, 0x1f, 0x7f, 0xf1, 0x71,
  1411. 0xc8, 0xe7, 0x5d, 0x47, 0xaf, 0x8b, 0x72, 0xe9}},
  1412. {{0x83, 0xb9, 0x39, 0xb2, 0xa4, 0xdf, 0x46, 0x87,
  1413. 0xc2, 0xb8, 0xf1, 0xe6, 0x4c, 0xd1, 0xe2, 0xa9,
  1414. 0xe4, 0x70, 0x30, 0x34, 0xbc, 0x52, 0x7c, 0x55,
  1415. 0xa6, 0xec, 0x80, 0xa4, 0xe5, 0xd2, 0xdc, 0x73},
  1416. {0x08, 0xf1, 0x03, 0xcf, 0x16, 0x73, 0xe8, 0x7d,
  1417. 0xb6, 0x7e, 0x9b, 0xc0, 0xb4, 0xc2, 0xa5, 0x86,
  1418. 0x02, 0x77, 0xd5, 0x27, 0x86, 0xa5, 0x15, 0xfb,
  1419. 0xae, 0x9b, 0x8c, 0xa9, 0xf9, 0xf8, 0xa8, 0x4a}},
  1420. {{0x8b, 0x00, 0x49, 0xdb, 0xfa, 0xf0, 0x1b, 0xa2,
  1421. 0xed, 0x8a, 0x9a, 0x7a, 0x36, 0x78, 0x4a, 0xc7,
  1422. 0xf7, 0xad, 0x39, 0xd0, 0x6c, 0x65, 0x7a, 0x41,
  1423. 0xce, 0xd6, 0xd6, 0x4c, 0x20, 0x21, 0x6b, 0xc7},
  1424. {0xc6, 0xca, 0x78, 0x1d, 0x32, 0x6c, 0x6c, 0x06,
  1425. 0x91, 0xf2, 0x1a, 0xe8, 0x43, 0x16, 0xea, 0x04,
  1426. 0x3c, 0x1f, 0x07, 0x85, 0xf7, 0x09, 0x22, 0x08,
  1427. 0xba, 0x13, 0xfd, 0x78, 0x1e, 0x3f, 0x6f, 0x62}},
  1428. {{0x25, 0x9b, 0x7c, 0xb0, 0xac, 0x72, 0x6f, 0xb2,
  1429. 0xe3, 0x53, 0x84, 0x7a, 0x1a, 0x9a, 0x98, 0x9b,
  1430. 0x44, 0xd3, 0x59, 0xd0, 0x8e, 0x57, 0x41, 0x40,
  1431. 0x78, 0xa7, 0x30, 0x2f, 0x4c, 0x9c, 0xb9, 0x68},
  1432. {0xb7, 0x75, 0x03, 0x63, 0x61, 0xc2, 0x48, 0x6e,
  1433. 0x12, 0x3d, 0xbf, 0x4b, 0x27, 0xdf, 0xb1, 0x7a,
  1434. 0xff, 0x4e, 0x31, 0x07, 0x83, 0xf4, 0x62, 0x5b,
  1435. 0x19, 0xa5, 0xac, 0xa0, 0x32, 0x58, 0x0d, 0xa7}},
  1436. {{0x43, 0x4f, 0x10, 0xa4, 0xca, 0xdb, 0x38, 0x67,
  1437. 0xfa, 0xae, 0x96, 0xb5, 0x6d, 0x97, 0xff, 0x1f,
  1438. 0xb6, 0x83, 0x43, 0xd3, 0xa0, 0x2d, 0x70, 0x7a,
  1439. 0x64, 0x05, 0x4c, 0xa7, 0xc1, 0xa5, 0x21, 0x51},
  1440. {0xe4, 0xf1, 0x23, 0x84, 0xe1, 0xb5, 0x9d, 0xf2,
  1441. 0xb8, 0x73, 0x8b, 0x45, 0x2b, 0x35, 0x46, 0x38,
  1442. 0x10, 0x2b, 0x50, 0xf8, 0x8b, 0x35, 0xcd, 0x34,
  1443. 0xc8, 0x0e, 0xf6, 0xdb, 0x09, 0x35, 0xf0, 0xda}},
  1444. {{0xdb, 0x21, 0x5c, 0x8d, 0x83, 0x1d, 0xb3, 0x34,
  1445. 0xc7, 0x0e, 0x43, 0xa1, 0x58, 0x79, 0x67, 0x13,
  1446. 0x1e, 0x86, 0x5d, 0x89, 0x63, 0xe6, 0x0a, 0x46,
  1447. 0x5c, 0x02, 0x97, 0x1b, 0x62, 0x43, 0x86, 0xf5},
  1448. {0xdb, 0x21, 0x5c, 0x8d, 0x83, 0x1d, 0xb3, 0x34,
  1449. 0xc7, 0x0e, 0x43, 0xa1, 0x58, 0x79, 0x67, 0x13,
  1450. 0x1e, 0x86, 0x5d, 0x89, 0x63, 0xe6, 0x0a, 0x46,
  1451. 0x5c, 0x02, 0x97, 0x1b, 0x62, 0x43, 0x86, 0xf5}}
  1452. };
  1453. secp256k1_scalar_set_int(&one, 1);
  1454. for (i = 0; i < 33; i++) {
  1455. secp256k1_scalar_set_b32(&x, chal[i][0], &overflow);
  1456. CHECK(!overflow);
  1457. secp256k1_scalar_set_b32(&y, chal[i][1], &overflow);
  1458. CHECK(!overflow);
  1459. secp256k1_scalar_set_b32(&r1, res[i][0], &overflow);
  1460. CHECK(!overflow);
  1461. secp256k1_scalar_set_b32(&r2, res[i][1], &overflow);
  1462. CHECK(!overflow);
  1463. secp256k1_scalar_mul(&z, &x, &y);
  1464. CHECK(!secp256k1_scalar_check_overflow(&z));
  1465. CHECK(secp256k1_scalar_eq(&r1, &z));
  1466. if (!secp256k1_scalar_is_zero(&y)) {
  1467. secp256k1_scalar_inverse(&zz, &y);
  1468. CHECK(!secp256k1_scalar_check_overflow(&zz));
  1469. #if defined(USE_SCALAR_INV_NUM)
  1470. secp256k1_scalar_inverse_var(&zzv, &y);
  1471. CHECK(secp256k1_scalar_eq(&zzv, &zz));
  1472. #endif
  1473. secp256k1_scalar_mul(&z, &z, &zz);
  1474. CHECK(!secp256k1_scalar_check_overflow(&z));
  1475. CHECK(secp256k1_scalar_eq(&x, &z));
  1476. secp256k1_scalar_mul(&zz, &zz, &y);
  1477. CHECK(!secp256k1_scalar_check_overflow(&zz));
  1478. CHECK(secp256k1_scalar_eq(&one, &zz));
  1479. }
  1480. secp256k1_scalar_mul(&z, &x, &x);
  1481. CHECK(!secp256k1_scalar_check_overflow(&z));
  1482. secp256k1_scalar_sqr(&zz, &x);
  1483. CHECK(!secp256k1_scalar_check_overflow(&zz));
  1484. CHECK(secp256k1_scalar_eq(&zz, &z));
  1485. CHECK(secp256k1_scalar_eq(&r2, &zz));
  1486. }
  1487. }
  1488. }
  1489. /***** FIELD TESTS *****/
  1490. void random_fe(secp256k1_fe *x) {
  1491. unsigned char bin[32];
  1492. do {
  1493. secp256k1_rand256(bin);
  1494. if (secp256k1_fe_set_b32(x, bin)) {
  1495. return;
  1496. }
  1497. } while(1);
  1498. }
  1499. void random_fe_test(secp256k1_fe *x) {
  1500. unsigned char bin[32];
  1501. do {
  1502. secp256k1_rand256_test(bin);
  1503. if (secp256k1_fe_set_b32(x, bin)) {
  1504. return;
  1505. }
  1506. } while(1);
  1507. }
  1508. void random_fe_non_zero(secp256k1_fe *nz) {
  1509. int tries = 10;
  1510. while (--tries >= 0) {
  1511. random_fe(nz);
  1512. secp256k1_fe_normalize(nz);
  1513. if (!secp256k1_fe_is_zero(nz)) {
  1514. break;
  1515. }
  1516. }
  1517. /* Infinitesimal probability of spurious failure here */
  1518. CHECK(tries >= 0);
  1519. }
  1520. void random_fe_non_square(secp256k1_fe *ns) {
  1521. secp256k1_fe r;
  1522. random_fe_non_zero(ns);
  1523. if (secp256k1_fe_sqrt(&r, ns)) {
  1524. secp256k1_fe_negate(ns, ns, 1);
  1525. }
  1526. }
  1527. int check_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b) {
  1528. secp256k1_fe an = *a;
  1529. secp256k1_fe bn = *b;
  1530. secp256k1_fe_normalize_weak(&an);
  1531. secp256k1_fe_normalize_var(&bn);
  1532. return secp256k1_fe_equal_var(&an, &bn);
  1533. }
  1534. int check_fe_inverse(const secp256k1_fe *a, const secp256k1_fe *ai) {
  1535. secp256k1_fe x;
  1536. secp256k1_fe one = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
  1537. secp256k1_fe_mul(&x, a, ai);
  1538. return check_fe_equal(&x, &one);
  1539. }
  1540. void run_field_convert(void) {
  1541. static const unsigned char b32[32] = {
  1542. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1543. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  1544. 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
  1545. 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40
  1546. };
  1547. static const secp256k1_fe_storage fes = SECP256K1_FE_STORAGE_CONST(
  1548. 0x00010203UL, 0x04050607UL, 0x11121314UL, 0x15161718UL,
  1549. 0x22232425UL, 0x26272829UL, 0x33343536UL, 0x37383940UL
  1550. );
  1551. static const secp256k1_fe fe = SECP256K1_FE_CONST(
  1552. 0x00010203UL, 0x04050607UL, 0x11121314UL, 0x15161718UL,
  1553. 0x22232425UL, 0x26272829UL, 0x33343536UL, 0x37383940UL
  1554. );
  1555. secp256k1_fe fe2;
  1556. unsigned char b322[32];
  1557. secp256k1_fe_storage fes2;
  1558. /* Check conversions to fe. */
  1559. CHECK(secp256k1_fe_set_b32(&fe2, b32));
  1560. CHECK(secp256k1_fe_equal_var(&fe, &fe2));
  1561. secp256k1_fe_from_storage(&fe2, &fes);
  1562. CHECK(secp256k1_fe_equal_var(&fe, &fe2));
  1563. /* Check conversion from fe. */
  1564. secp256k1_fe_get_b32(b322, &fe);
  1565. CHECK(memcmp(b322, b32, 32) == 0);
  1566. secp256k1_fe_to_storage(&fes2, &fe);
  1567. CHECK(memcmp(&fes2, &fes, sizeof(fes)) == 0);
  1568. }
  1569. int fe_memcmp(const secp256k1_fe *a, const secp256k1_fe *b) {
  1570. secp256k1_fe t = *b;
  1571. #ifdef VERIFY
  1572. t.magnitude = a->magnitude;
  1573. t.normalized = a->normalized;
  1574. #endif
  1575. return memcmp(a, &t, sizeof(secp256k1_fe));
  1576. }
  1577. void run_field_misc(void) {
  1578. secp256k1_fe x;
  1579. secp256k1_fe y;
  1580. secp256k1_fe z;
  1581. secp256k1_fe q;
  1582. secp256k1_fe fe5 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 5);
  1583. int i, j;
  1584. for (i = 0; i < 5*count; i++) {
  1585. secp256k1_fe_storage xs, ys, zs;
  1586. random_fe(&x);
  1587. random_fe_non_zero(&y);
  1588. /* Test the fe equality and comparison operations. */
  1589. CHECK(secp256k1_fe_cmp_var(&x, &x) == 0);
  1590. CHECK(secp256k1_fe_equal_var(&x, &x));
  1591. z = x;
  1592. secp256k1_fe_add(&z,&y);
  1593. /* Test fe conditional move; z is not normalized here. */
  1594. q = x;
  1595. secp256k1_fe_cmov(&x, &z, 0);
  1596. VERIFY_CHECK(!x.normalized && x.magnitude == z.magnitude);
  1597. secp256k1_fe_cmov(&x, &x, 1);
  1598. CHECK(fe_memcmp(&x, &z) != 0);
  1599. CHECK(fe_memcmp(&x, &q) == 0);
  1600. secp256k1_fe_cmov(&q, &z, 1);
  1601. VERIFY_CHECK(!q.normalized && q.magnitude == z.magnitude);
  1602. CHECK(fe_memcmp(&q, &z) == 0);
  1603. secp256k1_fe_normalize_var(&x);
  1604. secp256k1_fe_normalize_var(&z);
  1605. CHECK(!secp256k1_fe_equal_var(&x, &z));
  1606. secp256k1_fe_normalize_var(&q);
  1607. secp256k1_fe_cmov(&q, &z, (i&1));
  1608. VERIFY_CHECK(q.normalized && q.magnitude == 1);
  1609. for (j = 0; j < 6; j++) {
  1610. secp256k1_fe_negate(&z, &z, j+1);
  1611. secp256k1_fe_normalize_var(&q);
  1612. secp256k1_fe_cmov(&q, &z, (j&1));
  1613. VERIFY_CHECK(!q.normalized && q.magnitude == (j+2));
  1614. }
  1615. secp256k1_fe_normalize_var(&z);
  1616. /* Test storage conversion and conditional moves. */
  1617. secp256k1_fe_to_storage(&xs, &x);
  1618. secp256k1_fe_to_storage(&ys, &y);
  1619. secp256k1_fe_to_storage(&zs, &z);
  1620. secp256k1_fe_storage_cmov(&zs, &xs, 0);
  1621. secp256k1_fe_storage_cmov(&zs, &zs, 1);
  1622. CHECK(memcmp(&xs, &zs, sizeof(xs)) != 0);
  1623. secp256k1_fe_storage_cmov(&ys, &xs, 1);
  1624. CHECK(memcmp(&xs, &ys, sizeof(xs)) == 0);
  1625. secp256k1_fe_from_storage(&x, &xs);
  1626. secp256k1_fe_from_storage(&y, &ys);
  1627. secp256k1_fe_from_storage(&z, &zs);
  1628. /* Test that mul_int, mul, and add agree. */
  1629. secp256k1_fe_add(&y, &x);
  1630. secp256k1_fe_add(&y, &x);
  1631. z = x;
  1632. secp256k1_fe_mul_int(&z, 3);
  1633. CHECK(check_fe_equal(&y, &z));
  1634. secp256k1_fe_add(&y, &x);
  1635. secp256k1_fe_add(&z, &x);
  1636. CHECK(check_fe_equal(&z, &y));
  1637. z = x;
  1638. secp256k1_fe_mul_int(&z, 5);
  1639. secp256k1_fe_mul(&q, &x, &fe5);
  1640. CHECK(check_fe_equal(&z, &q));
  1641. secp256k1_fe_negate(&x, &x, 1);
  1642. secp256k1_fe_add(&z, &x);
  1643. secp256k1_fe_add(&q, &x);
  1644. CHECK(check_fe_equal(&y, &z));
  1645. CHECK(check_fe_equal(&q, &y));
  1646. }
  1647. }
  1648. void run_field_inv(void) {
  1649. secp256k1_fe x, xi, xii;
  1650. int i;
  1651. for (i = 0; i < 10*count; i++) {
  1652. random_fe_non_zero(&x);
  1653. secp256k1_fe_inv(&xi, &x);
  1654. CHECK(check_fe_inverse(&x, &xi));
  1655. secp256k1_fe_inv(&xii, &xi);
  1656. CHECK(check_fe_equal(&x, &xii));
  1657. }
  1658. }
  1659. void run_field_inv_var(void) {
  1660. secp256k1_fe x, xi, xii;
  1661. int i;
  1662. for (i = 0; i < 10*count; i++) {
  1663. random_fe_non_zero(&x);
  1664. secp256k1_fe_inv_var(&xi, &x);
  1665. CHECK(check_fe_inverse(&x, &xi));
  1666. secp256k1_fe_inv_var(&xii, &xi);
  1667. CHECK(check_fe_equal(&x, &xii));
  1668. }
  1669. }
  1670. void run_field_inv_all_var(void) {
  1671. secp256k1_fe x[16], xi[16], xii[16];
  1672. int i;
  1673. /* Check it's safe to call for 0 elements */
  1674. secp256k1_fe_inv_all_var(xi, x, 0);
  1675. for (i = 0; i < count; i++) {
  1676. size_t j;
  1677. size_t len = secp256k1_rand_int(15) + 1;
  1678. for (j = 0; j < len; j++) {
  1679. random_fe_non_zero(&x[j]);
  1680. }
  1681. secp256k1_fe_inv_all_var(xi, x, len);
  1682. for (j = 0; j < len; j++) {
  1683. CHECK(check_fe_inverse(&x[j], &xi[j]));
  1684. }
  1685. secp256k1_fe_inv_all_var(xii, xi, len);
  1686. for (j = 0; j < len; j++) {
  1687. CHECK(check_fe_equal(&x[j], &xii[j]));
  1688. }
  1689. }
  1690. }
  1691. void run_sqr(void) {
  1692. secp256k1_fe x, s;
  1693. {
  1694. int i;
  1695. secp256k1_fe_set_int(&x, 1);
  1696. secp256k1_fe_negate(&x, &x, 1);
  1697. for (i = 1; i <= 512; ++i) {
  1698. secp256k1_fe_mul_int(&x, 2);
  1699. secp256k1_fe_normalize(&x);
  1700. secp256k1_fe_sqr(&s, &x);
  1701. }
  1702. }
  1703. }
  1704. void test_sqrt(const secp256k1_fe *a, const secp256k1_fe *k) {
  1705. secp256k1_fe r1, r2;
  1706. int v = secp256k1_fe_sqrt(&r1, a);
  1707. CHECK((v == 0) == (k == NULL));
  1708. if (k != NULL) {
  1709. /* Check that the returned root is +/- the given known answer */
  1710. secp256k1_fe_negate(&r2, &r1, 1);
  1711. secp256k1_fe_add(&r1, k); secp256k1_fe_add(&r2, k);
  1712. secp256k1_fe_normalize(&r1); secp256k1_fe_normalize(&r2);
  1713. CHECK(secp256k1_fe_is_zero(&r1) || secp256k1_fe_is_zero(&r2));
  1714. }
  1715. }
  1716. void run_sqrt(void) {
  1717. secp256k1_fe ns, x, s, t;
  1718. int i;
  1719. /* Check sqrt(0) is 0 */
  1720. secp256k1_fe_set_int(&x, 0);
  1721. secp256k1_fe_sqr(&s, &x);
  1722. test_sqrt(&s, &x);
  1723. /* Check sqrt of small squares (and their negatives) */
  1724. for (i = 1; i <= 100; i++) {
  1725. secp256k1_fe_set_int(&x, i);
  1726. secp256k1_fe_sqr(&s, &x);
  1727. test_sqrt(&s, &x);
  1728. secp256k1_fe_negate(&t, &s, 1);
  1729. test_sqrt(&t, NULL);
  1730. }
  1731. /* Consistency checks for large random values */
  1732. for (i = 0; i < 10; i++) {
  1733. int j;
  1734. random_fe_non_square(&ns);
  1735. for (j = 0; j < count; j++) {
  1736. random_fe(&x);
  1737. secp256k1_fe_sqr(&s, &x);
  1738. test_sqrt(&s, &x);
  1739. secp256k1_fe_negate(&t, &s, 1);
  1740. test_sqrt(&t, NULL);
  1741. secp256k1_fe_mul(&t, &s, &ns);
  1742. test_sqrt(&t, NULL);
  1743. }
  1744. }
  1745. }
  1746. /***** GROUP TESTS *****/
  1747. void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
  1748. CHECK(a->infinity == b->infinity);
  1749. if (a->infinity) {
  1750. return;
  1751. }
  1752. CHECK(secp256k1_fe_equal_var(&a->x, &b->x));
  1753. CHECK(secp256k1_fe_equal_var(&a->y, &b->y));
  1754. }
  1755. /* This compares jacobian points including their Z, not just their geometric meaning. */
  1756. int gej_xyz_equals_gej(const secp256k1_gej *a, const secp256k1_gej *b) {
  1757. secp256k1_gej a2;
  1758. secp256k1_gej b2;
  1759. int ret = 1;
  1760. ret &= a->infinity == b->infinity;
  1761. if (ret && !a->infinity) {
  1762. a2 = *a;
  1763. b2 = *b;
  1764. secp256k1_fe_normalize(&a2.x);
  1765. secp256k1_fe_normalize(&a2.y);
  1766. secp256k1_fe_normalize(&a2.z);
  1767. secp256k1_fe_normalize(&b2.x);
  1768. secp256k1_fe_normalize(&b2.y);
  1769. secp256k1_fe_normalize(&b2.z);
  1770. ret &= secp256k1_fe_cmp_var(&a2.x, &b2.x) == 0;
  1771. ret &= secp256k1_fe_cmp_var(&a2.y, &b2.y) == 0;
  1772. ret &= secp256k1_fe_cmp_var(&a2.z, &b2.z) == 0;
  1773. }
  1774. return ret;
  1775. }
  1776. void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
  1777. secp256k1_fe z2s;
  1778. secp256k1_fe u1, u2, s1, s2;
  1779. CHECK(a->infinity == b->infinity);
  1780. if (a->infinity) {
  1781. return;
  1782. }
  1783. /* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
  1784. secp256k1_fe_sqr(&z2s, &b->z);
  1785. secp256k1_fe_mul(&u1, &a->x, &z2s);
  1786. u2 = b->x; secp256k1_fe_normalize_weak(&u2);
  1787. secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
  1788. s2 = b->y; secp256k1_fe_normalize_weak(&s2);
  1789. CHECK(secp256k1_fe_equal_var(&u1, &u2));
  1790. CHECK(secp256k1_fe_equal_var(&s1, &s2));
  1791. }
  1792. void test_ge(void) {
  1793. int i, i1;
  1794. #ifdef USE_ENDOMORPHISM
  1795. int runs = 6;
  1796. #else
  1797. int runs = 4;
  1798. #endif
  1799. /* Points: (infinity, p1, p1, -p1, -p1, p2, p2, -p2, -p2, p3, p3, -p3, -p3, p4, p4, -p4, -p4).
  1800. * The second in each pair of identical points uses a random Z coordinate in the Jacobian form.
  1801. * All magnitudes are randomized.
  1802. * All 17*17 combinations of points are added to each other, using all applicable methods.
  1803. *
  1804. * When the endomorphism code is compiled in, p5 = lambda*p1 and p6 = lambda^2*p1 are added as well.
  1805. */
  1806. secp256k1_ge *ge = (secp256k1_ge *)checked_malloc(&ctx->error_callback, sizeof(secp256k1_ge) * (1 + 4 * runs));
  1807. secp256k1_gej *gej = (secp256k1_gej *)checked_malloc(&ctx->error_callback, sizeof(secp256k1_gej) * (1 + 4 * runs));
  1808. secp256k1_fe *zinv = (secp256k1_fe *)checked_malloc(&ctx->error_callback, sizeof(secp256k1_fe) * (1 + 4 * runs));
  1809. secp256k1_fe zf;
  1810. secp256k1_fe zfi2, zfi3;
  1811. secp256k1_gej_set_infinity(&gej[0]);
  1812. secp256k1_ge_clear(&ge[0]);
  1813. secp256k1_ge_set_gej_var(&ge[0], &gej[0]);
  1814. for (i = 0; i < runs; i++) {
  1815. int j;
  1816. secp256k1_ge g;
  1817. random_group_element_test(&g);
  1818. #ifdef USE_ENDOMORPHISM
  1819. if (i >= runs - 2) {
  1820. secp256k1_ge_mul_lambda(&g, &ge[1]);
  1821. }
  1822. if (i >= runs - 1) {
  1823. secp256k1_ge_mul_lambda(&g, &g);
  1824. }
  1825. #endif
  1826. ge[1 + 4 * i] = g;
  1827. ge[2 + 4 * i] = g;
  1828. secp256k1_ge_neg(&ge[3 + 4 * i], &g);
  1829. secp256k1_ge_neg(&ge[4 + 4 * i], &g);
  1830. secp256k1_gej_set_ge(&gej[1 + 4 * i], &ge[1 + 4 * i]);
  1831. random_group_element_jacobian_test(&gej[2 + 4 * i], &ge[2 + 4 * i]);
  1832. secp256k1_gej_set_ge(&gej[3 + 4 * i], &ge[3 + 4 * i]);
  1833. random_group_element_jacobian_test(&gej[4 + 4 * i], &ge[4 + 4 * i]);
  1834. for (j = 0; j < 4; j++) {
  1835. random_field_element_magnitude(&ge[1 + j + 4 * i].x);
  1836. random_field_element_magnitude(&ge[1 + j + 4 * i].y);
  1837. random_field_element_magnitude(&gej[1 + j + 4 * i].x);
  1838. random_field_element_magnitude(&gej[1 + j + 4 * i].y);
  1839. random_field_element_magnitude(&gej[1 + j + 4 * i].z);
  1840. }
  1841. }
  1842. /* Compute z inverses. */
  1843. {
  1844. secp256k1_fe *zs = checked_malloc(&ctx->error_callback, sizeof(secp256k1_fe) * (1 + 4 * runs));
  1845. for (i = 0; i < 4 * runs + 1; i++) {
  1846. if (i == 0) {
  1847. /* The point at infinity does not have a meaningful z inverse. Any should do. */
  1848. do {
  1849. random_field_element_test(&zs[i]);
  1850. } while(secp256k1_fe_is_zero(&zs[i]));
  1851. } else {
  1852. zs[i] = gej[i].z;
  1853. }
  1854. }
  1855. secp256k1_fe_inv_all_var(zinv, zs, 4 * runs + 1);
  1856. free(zs);
  1857. }
  1858. /* Generate random zf, and zfi2 = 1/zf^2, zfi3 = 1/zf^3 */
  1859. do {
  1860. random_field_element_test(&zf);
  1861. } while(secp256k1_fe_is_zero(&zf));
  1862. random_field_element_magnitude(&zf);
  1863. secp256k1_fe_inv_var(&zfi3, &zf);
  1864. secp256k1_fe_sqr(&zfi2, &zfi3);
  1865. secp256k1_fe_mul(&zfi3, &zfi3, &zfi2);
  1866. for (i1 = 0; i1 < 1 + 4 * runs; i1++) {
  1867. int i2;
  1868. for (i2 = 0; i2 < 1 + 4 * runs; i2++) {
  1869. /* Compute reference result using gej + gej (var). */
  1870. secp256k1_gej refj, resj;
  1871. secp256k1_ge ref;
  1872. secp256k1_fe zr;
  1873. secp256k1_gej_add_var(&refj, &gej[i1], &gej[i2], secp256k1_gej_is_infinity(&gej[i1]) ? NULL : &zr);
  1874. /* Check Z ratio. */
  1875. if (!secp256k1_gej_is_infinity(&gej[i1]) && !secp256k1_gej_is_infinity(&refj)) {
  1876. secp256k1_fe zrz; secp256k1_fe_mul(&zrz, &zr, &gej[i1].z);
  1877. CHECK(secp256k1_fe_equal_var(&zrz, &refj.z));
  1878. }
  1879. secp256k1_ge_set_gej_var(&ref, &refj);
  1880. /* Test gej + ge with Z ratio result (var). */
  1881. secp256k1_gej_add_ge_var(&resj, &gej[i1], &ge[i2], secp256k1_gej_is_infinity(&gej[i1]) ? NULL : &zr);
  1882. ge_equals_gej(&ref, &resj);
  1883. if (!secp256k1_gej_is_infinity(&gej[i1]) && !secp256k1_gej_is_infinity(&resj)) {
  1884. secp256k1_fe zrz; secp256k1_fe_mul(&zrz, &zr, &gej[i1].z);
  1885. CHECK(secp256k1_fe_equal_var(&zrz, &resj.z));
  1886. }
  1887. /* Test gej + ge (var, with additional Z factor). */
  1888. {
  1889. secp256k1_ge ge2_zfi = ge[i2]; /* the second term with x and y rescaled for z = 1/zf */
  1890. secp256k1_fe_mul(&ge2_zfi.x, &ge2_zfi.x, &zfi2);
  1891. secp256k1_fe_mul(&ge2_zfi.y, &ge2_zfi.y, &zfi3);
  1892. random_field_element_magnitude(&ge2_zfi.x);
  1893. random_field_element_magnitude(&ge2_zfi.y);
  1894. secp256k1_gej_add_zinv_var(&resj, &gej[i1], &ge2_zfi, &zf);
  1895. ge_equals_gej(&ref, &resj);
  1896. }
  1897. /* Test gej + ge (const). */
  1898. if (i2 != 0) {
  1899. /* secp256k1_gej_add_ge does not support its second argument being infinity. */
  1900. secp256k1_gej_add_ge(&resj, &gej[i1], &ge[i2]);
  1901. ge_equals_gej(&ref, &resj);
  1902. }
  1903. /* Test doubling (var). */
  1904. if ((i1 == 0 && i2 == 0) || ((i1 + 3)/4 == (i2 + 3)/4 && ((i1 + 3)%4)/2 == ((i2 + 3)%4)/2)) {
  1905. secp256k1_fe zr2;
  1906. /* Normal doubling with Z ratio result. */
  1907. secp256k1_gej_double_var(&resj, &gej[i1], &zr2);
  1908. ge_equals_gej(&ref, &resj);
  1909. /* Check Z ratio. */
  1910. secp256k1_fe_mul(&zr2, &zr2, &gej[i1].z);
  1911. CHECK(secp256k1_fe_equal_var(&zr2, &resj.z));
  1912. /* Normal doubling. */
  1913. secp256k1_gej_double_var(&resj, &gej[i2], NULL);
  1914. ge_equals_gej(&ref, &resj);
  1915. }
  1916. /* Test adding opposites. */
  1917. if ((i1 == 0 && i2 == 0) || ((i1 + 3)/4 == (i2 + 3)/4 && ((i1 + 3)%4)/2 != ((i2 + 3)%4)/2)) {
  1918. CHECK(secp256k1_ge_is_infinity(&ref));
  1919. }
  1920. /* Test adding infinity. */
  1921. if (i1 == 0) {
  1922. CHECK(secp256k1_ge_is_infinity(&ge[i1]));
  1923. CHECK(secp256k1_gej_is_infinity(&gej[i1]));
  1924. ge_equals_gej(&ref, &gej[i2]);
  1925. }
  1926. if (i2 == 0) {
  1927. CHECK(secp256k1_ge_is_infinity(&ge[i2]));
  1928. CHECK(secp256k1_gej_is_infinity(&gej[i2]));
  1929. ge_equals_gej(&ref, &gej[i1]);
  1930. }
  1931. }
  1932. }
  1933. /* Test adding all points together in random order equals infinity. */
  1934. {
  1935. secp256k1_gej sum = SECP256K1_GEJ_CONST_INFINITY;
  1936. secp256k1_gej *gej_shuffled = (secp256k1_gej *)checked_malloc(&ctx->error_callback, (4 * runs + 1) * sizeof(secp256k1_gej));
  1937. for (i = 0; i < 4 * runs + 1; i++) {
  1938. gej_shuffled[i] = gej[i];
  1939. }
  1940. for (i = 0; i < 4 * runs + 1; i++) {
  1941. int swap = i + secp256k1_rand_int(4 * runs + 1 - i);
  1942. if (swap != i) {
  1943. secp256k1_gej t = gej_shuffled[i];
  1944. gej_shuffled[i] = gej_shuffled[swap];
  1945. gej_shuffled[swap] = t;
  1946. }
  1947. }
  1948. for (i = 0; i < 4 * runs + 1; i++) {
  1949. secp256k1_gej_add_var(&sum, &sum, &gej_shuffled[i], NULL);
  1950. }
  1951. CHECK(secp256k1_gej_is_infinity(&sum));
  1952. free(gej_shuffled);
  1953. }
  1954. /* Test batch gej -> ge conversion with and without known z ratios. */
  1955. {
  1956. secp256k1_fe *zr = (secp256k1_fe *)checked_malloc(&ctx->error_callback, (4 * runs + 1) * sizeof(secp256k1_fe));
  1957. secp256k1_ge *ge_set_table = (secp256k1_ge *)checked_malloc(&ctx->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
  1958. secp256k1_ge *ge_set_all = (secp256k1_ge *)checked_malloc(&ctx->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
  1959. for (i = 0; i < 4 * runs + 1; i++) {
  1960. /* Compute gej[i + 1].z / gez[i].z (with gej[n].z taken to be 1). */
  1961. if (i < 4 * runs) {
  1962. secp256k1_fe_mul(&zr[i + 1], &zinv[i], &gej[i + 1].z);
  1963. }
  1964. }
  1965. secp256k1_ge_set_table_gej_var(ge_set_table, gej, zr, 4 * runs + 1);
  1966. secp256k1_ge_set_all_gej_var(ge_set_all, gej, 4 * runs + 1, &ctx->error_callback);
  1967. for (i = 0; i < 4 * runs + 1; i++) {
  1968. secp256k1_fe s;
  1969. random_fe_non_zero(&s);
  1970. secp256k1_gej_rescale(&gej[i], &s);
  1971. ge_equals_gej(&ge_set_table[i], &gej[i]);
  1972. ge_equals_gej(&ge_set_all[i], &gej[i]);
  1973. }
  1974. free(ge_set_table);
  1975. free(ge_set_all);
  1976. free(zr);
  1977. }
  1978. free(ge);
  1979. free(gej);
  1980. free(zinv);
  1981. }
  1982. void test_add_neg_y_diff_x(void) {
  1983. /* The point of this test is to check that we can add two points
  1984. * whose y-coordinates are negatives of each other but whose x
  1985. * coordinates differ. If the x-coordinates were the same, these
  1986. * points would be negatives of each other and their sum is
  1987. * infinity. This is cool because it "covers up" any degeneracy
  1988. * in the addition algorithm that would cause the xy coordinates
  1989. * of the sum to be wrong (since infinity has no xy coordinates).
  1990. * HOWEVER, if the x-coordinates are different, infinity is the
  1991. * wrong answer, and such degeneracies are exposed. This is the
  1992. * root of https://github.com/bitcoin-core/secp256k1/issues/257
  1993. * which this test is a regression test for.
  1994. *
  1995. * These points were generated in sage as
  1996. * # secp256k1 params
  1997. * F = FiniteField (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F)
  1998. * C = EllipticCurve ([F (0), F (7)])
  1999. * G = C.lift_x(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798)
  2000. * N = FiniteField(G.order())
  2001. *
  2002. * # endomorphism values (lambda is 1^{1/3} in N, beta is 1^{1/3} in F)
  2003. * x = polygen(N)
  2004. * lam = (1 - x^3).roots()[1][0]
  2005. *
  2006. * # random "bad pair"
  2007. * P = C.random_element()
  2008. * Q = -int(lam) * P
  2009. * print " P: %x %x" % P.xy()
  2010. * print " Q: %x %x" % Q.xy()
  2011. * print "P + Q: %x %x" % (P + Q).xy()
  2012. */
  2013. secp256k1_gej aj = SECP256K1_GEJ_CONST(
  2014. 0x8d24cd95, 0x0a355af1, 0x3c543505, 0x44238d30,
  2015. 0x0643d79f, 0x05a59614, 0x2f8ec030, 0xd58977cb,
  2016. 0x001e337a, 0x38093dcd, 0x6c0f386d, 0x0b1293a8,
  2017. 0x4d72c879, 0xd7681924, 0x44e6d2f3, 0x9190117d
  2018. );
  2019. secp256k1_gej bj = SECP256K1_GEJ_CONST(
  2020. 0xc7b74206, 0x1f788cd9, 0xabd0937d, 0x164a0d86,
  2021. 0x95f6ff75, 0xf19a4ce9, 0xd013bd7b, 0xbf92d2a7,
  2022. 0xffe1cc85, 0xc7f6c232, 0x93f0c792, 0xf4ed6c57,
  2023. 0xb28d3786, 0x2897e6db, 0xbb192d0b, 0x6e6feab2
  2024. );
  2025. secp256k1_gej sumj = SECP256K1_GEJ_CONST(
  2026. 0x671a63c0, 0x3efdad4c, 0x389a7798, 0x24356027,
  2027. 0xb3d69010, 0x278625c3, 0x5c86d390, 0x184a8f7a,
  2028. 0x5f6409c2, 0x2ce01f2b, 0x511fd375, 0x25071d08,
  2029. 0xda651801, 0x70e95caf, 0x8f0d893c, 0xbed8fbbe
  2030. );
  2031. secp256k1_ge b;
  2032. secp256k1_gej resj;
  2033. secp256k1_ge res;
  2034. secp256k1_ge_set_gej(&b, &bj);
  2035. secp256k1_gej_add_var(&resj, &aj, &bj, NULL);
  2036. secp256k1_ge_set_gej(&res, &resj);
  2037. ge_equals_gej(&res, &sumj);
  2038. secp256k1_gej_add_ge(&resj, &aj, &b);
  2039. secp256k1_ge_set_gej(&res, &resj);
  2040. ge_equals_gej(&res, &sumj);
  2041. secp256k1_gej_add_ge_var(&resj, &aj, &b, NULL);
  2042. secp256k1_ge_set_gej(&res, &resj);
  2043. ge_equals_gej(&res, &sumj);
  2044. }
  2045. void run_ge(void) {
  2046. int i;
  2047. for (i = 0; i < count * 32; i++) {
  2048. test_ge();
  2049. }
  2050. test_add_neg_y_diff_x();
  2051. }
  2052. void test_ec_combine(void) {
  2053. secp256k1_scalar sum = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
  2054. secp256k1_pubkey data[6];
  2055. const secp256k1_pubkey* d[6];
  2056. secp256k1_pubkey sd;
  2057. secp256k1_pubkey sd2;
  2058. secp256k1_gej Qj;
  2059. secp256k1_ge Q;
  2060. int i;
  2061. for (i = 1; i <= 6; i++) {
  2062. secp256k1_scalar s;
  2063. random_scalar_order_test(&s);
  2064. secp256k1_scalar_add(&sum, &sum, &s);
  2065. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &Qj, &s);
  2066. secp256k1_ge_set_gej(&Q, &Qj);
  2067. secp256k1_pubkey_save(&data[i - 1], &Q);
  2068. d[i - 1] = &data[i - 1];
  2069. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &Qj, &sum);
  2070. secp256k1_ge_set_gej(&Q, &Qj);
  2071. secp256k1_pubkey_save(&sd, &Q);
  2072. CHECK(secp256k1_ec_pubkey_combine(ctx, &sd2, d, i) == 1);
  2073. CHECK(memcmp(&sd, &sd2, sizeof(sd)) == 0);
  2074. }
  2075. }
  2076. void run_ec_combine(void) {
  2077. int i;
  2078. for (i = 0; i < count * 8; i++) {
  2079. test_ec_combine();
  2080. }
  2081. }
  2082. void test_group_decompress(const secp256k1_fe* x) {
  2083. /* The input itself, normalized. */
  2084. secp256k1_fe fex = *x;
  2085. secp256k1_fe fez;
  2086. /* Results of set_xquad_var, set_xo_var(..., 0), set_xo_var(..., 1). */
  2087. secp256k1_ge ge_quad, ge_even, ge_odd;
  2088. secp256k1_gej gej_quad;
  2089. /* Return values of the above calls. */
  2090. int res_quad, res_even, res_odd;
  2091. secp256k1_fe_normalize_var(&fex);
  2092. res_quad = secp256k1_ge_set_xquad(&ge_quad, &fex);
  2093. res_even = secp256k1_ge_set_xo_var(&ge_even, &fex, 0);
  2094. res_odd = secp256k1_ge_set_xo_var(&ge_odd, &fex, 1);
  2095. CHECK(res_quad == res_even);
  2096. CHECK(res_quad == res_odd);
  2097. if (res_quad) {
  2098. secp256k1_fe_normalize_var(&ge_quad.x);
  2099. secp256k1_fe_normalize_var(&ge_odd.x);
  2100. secp256k1_fe_normalize_var(&ge_even.x);
  2101. secp256k1_fe_normalize_var(&ge_quad.y);
  2102. secp256k1_fe_normalize_var(&ge_odd.y);
  2103. secp256k1_fe_normalize_var(&ge_even.y);
  2104. /* No infinity allowed. */
  2105. CHECK(!ge_quad.infinity);
  2106. CHECK(!ge_even.infinity);
  2107. CHECK(!ge_odd.infinity);
  2108. /* Check that the x coordinates check out. */
  2109. CHECK(secp256k1_fe_equal_var(&ge_quad.x, x));
  2110. CHECK(secp256k1_fe_equal_var(&ge_even.x, x));
  2111. CHECK(secp256k1_fe_equal_var(&ge_odd.x, x));
  2112. /* Check that the Y coordinate result in ge_quad is a square. */
  2113. CHECK(secp256k1_fe_is_quad_var(&ge_quad.y));
  2114. /* Check odd/even Y in ge_odd, ge_even. */
  2115. CHECK(secp256k1_fe_is_odd(&ge_odd.y));
  2116. CHECK(!secp256k1_fe_is_odd(&ge_even.y));
  2117. /* Check secp256k1_gej_has_quad_y_var. */
  2118. secp256k1_gej_set_ge(&gej_quad, &ge_quad);
  2119. CHECK(secp256k1_gej_has_quad_y_var(&gej_quad));
  2120. do {
  2121. random_fe_test(&fez);
  2122. } while (secp256k1_fe_is_zero(&fez));
  2123. secp256k1_gej_rescale(&gej_quad, &fez);
  2124. CHECK(secp256k1_gej_has_quad_y_var(&gej_quad));
  2125. secp256k1_gej_neg(&gej_quad, &gej_quad);
  2126. CHECK(!secp256k1_gej_has_quad_y_var(&gej_quad));
  2127. do {
  2128. random_fe_test(&fez);
  2129. } while (secp256k1_fe_is_zero(&fez));
  2130. secp256k1_gej_rescale(&gej_quad, &fez);
  2131. CHECK(!secp256k1_gej_has_quad_y_var(&gej_quad));
  2132. secp256k1_gej_neg(&gej_quad, &gej_quad);
  2133. CHECK(secp256k1_gej_has_quad_y_var(&gej_quad));
  2134. }
  2135. }
  2136. void run_group_decompress(void) {
  2137. int i;
  2138. for (i = 0; i < count * 4; i++) {
  2139. secp256k1_fe fe;
  2140. random_fe_test(&fe);
  2141. test_group_decompress(&fe);
  2142. }
  2143. }
  2144. /***** ECMULT TESTS *****/
  2145. void run_ecmult_chain(void) {
  2146. /* random starting point A (on the curve) */
  2147. secp256k1_gej a = SECP256K1_GEJ_CONST(
  2148. 0x8b30bbe9, 0xae2a9906, 0x96b22f67, 0x0709dff3,
  2149. 0x727fd8bc, 0x04d3362c, 0x6c7bf458, 0xe2846004,
  2150. 0xa357ae91, 0x5c4a6528, 0x1309edf2, 0x0504740f,
  2151. 0x0eb33439, 0x90216b4f, 0x81063cb6, 0x5f2f7e0f
  2152. );
  2153. /* two random initial factors xn and gn */
  2154. secp256k1_scalar xn = SECP256K1_SCALAR_CONST(
  2155. 0x84cc5452, 0xf7fde1ed, 0xb4d38a8c, 0xe9b1b84c,
  2156. 0xcef31f14, 0x6e569be9, 0x705d357a, 0x42985407
  2157. );
  2158. secp256k1_scalar gn = SECP256K1_SCALAR_CONST(
  2159. 0xa1e58d22, 0x553dcd42, 0xb2398062, 0x5d4c57a9,
  2160. 0x6e9323d4, 0x2b3152e5, 0xca2c3990, 0xedc7c9de
  2161. );
  2162. /* two small multipliers to be applied to xn and gn in every iteration: */
  2163. static const secp256k1_scalar xf = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0x1337);
  2164. static const secp256k1_scalar gf = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0x7113);
  2165. /* accumulators with the resulting coefficients to A and G */
  2166. secp256k1_scalar ae = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
  2167. secp256k1_scalar ge = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
  2168. /* actual points */
  2169. secp256k1_gej x;
  2170. secp256k1_gej x2;
  2171. int i;
  2172. /* the point being computed */
  2173. x = a;
  2174. for (i = 0; i < 200*count; i++) {
  2175. /* in each iteration, compute X = xn*X + gn*G; */
  2176. secp256k1_ecmult(&ctx->ecmult_ctx, &x, &x, &xn, &gn);
  2177. /* also compute ae and ge: the actual accumulated factors for A and G */
  2178. /* if X was (ae*A+ge*G), xn*X + gn*G results in (xn*ae*A + (xn*ge+gn)*G) */
  2179. secp256k1_scalar_mul(&ae, &ae, &xn);
  2180. secp256k1_scalar_mul(&ge, &ge, &xn);
  2181. secp256k1_scalar_add(&ge, &ge, &gn);
  2182. /* modify xn and gn */
  2183. secp256k1_scalar_mul(&xn, &xn, &xf);
  2184. secp256k1_scalar_mul(&gn, &gn, &gf);
  2185. /* verify */
  2186. if (i == 19999) {
  2187. /* expected result after 19999 iterations */
  2188. secp256k1_gej rp = SECP256K1_GEJ_CONST(
  2189. 0xD6E96687, 0xF9B10D09, 0x2A6F3543, 0x9D86CEBE,
  2190. 0xA4535D0D, 0x409F5358, 0x6440BD74, 0xB933E830,
  2191. 0xB95CBCA2, 0xC77DA786, 0x539BE8FD, 0x53354D2D,
  2192. 0x3B4F566A, 0xE6580454, 0x07ED6015, 0xEE1B2A88
  2193. );
  2194. secp256k1_gej_neg(&rp, &rp);
  2195. secp256k1_gej_add_var(&rp, &rp, &x, NULL);
  2196. CHECK(secp256k1_gej_is_infinity(&rp));
  2197. }
  2198. }
  2199. /* redo the computation, but directly with the resulting ae and ge coefficients: */
  2200. secp256k1_ecmult(&ctx->ecmult_ctx, &x2, &a, &ae, &ge);
  2201. secp256k1_gej_neg(&x2, &x2);
  2202. secp256k1_gej_add_var(&x2, &x2, &x, NULL);
  2203. CHECK(secp256k1_gej_is_infinity(&x2));
  2204. }
  2205. void test_point_times_order(const secp256k1_gej *point) {
  2206. /* X * (point + G) + (order-X) * (pointer + G) = 0 */
  2207. secp256k1_scalar x;
  2208. secp256k1_scalar nx;
  2209. secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
  2210. secp256k1_scalar one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
  2211. secp256k1_gej res1, res2;
  2212. secp256k1_ge res3;
  2213. unsigned char pub[65];
  2214. size_t psize = 65;
  2215. random_scalar_order_test(&x);
  2216. secp256k1_scalar_negate(&nx, &x);
  2217. secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &x, &x); /* calc res1 = x * point + x * G; */
  2218. secp256k1_ecmult(&ctx->ecmult_ctx, &res2, point, &nx, &nx); /* calc res2 = (order - x) * point + (order - x) * G; */
  2219. secp256k1_gej_add_var(&res1, &res1, &res2, NULL);
  2220. CHECK(secp256k1_gej_is_infinity(&res1));
  2221. CHECK(secp256k1_gej_is_valid_var(&res1) == 0);
  2222. secp256k1_ge_set_gej(&res3, &res1);
  2223. CHECK(secp256k1_ge_is_infinity(&res3));
  2224. CHECK(secp256k1_ge_is_valid_var(&res3) == 0);
  2225. CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 0) == 0);
  2226. psize = 65;
  2227. CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 1) == 0);
  2228. /* check zero/one edge cases */
  2229. secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &zero, &zero);
  2230. secp256k1_ge_set_gej(&res3, &res1);
  2231. CHECK(secp256k1_ge_is_infinity(&res3));
  2232. secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &one, &zero);
  2233. secp256k1_ge_set_gej(&res3, &res1);
  2234. ge_equals_gej(&res3, point);
  2235. secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &zero, &one);
  2236. secp256k1_ge_set_gej(&res3, &res1);
  2237. ge_equals_ge(&res3, &secp256k1_ge_const_g);
  2238. }
  2239. void run_point_times_order(void) {
  2240. int i;
  2241. secp256k1_fe x = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 2);
  2242. static const secp256k1_fe xr = SECP256K1_FE_CONST(
  2243. 0x7603CB59, 0xB0EF6C63, 0xFE608479, 0x2A0C378C,
  2244. 0xDB3233A8, 0x0F8A9A09, 0xA877DEAD, 0x31B38C45
  2245. );
  2246. for (i = 0; i < 500; i++) {
  2247. secp256k1_ge p;
  2248. if (secp256k1_ge_set_xo_var(&p, &x, 1)) {
  2249. secp256k1_gej j;
  2250. CHECK(secp256k1_ge_is_valid_var(&p));
  2251. secp256k1_gej_set_ge(&j, &p);
  2252. CHECK(secp256k1_gej_is_valid_var(&j));
  2253. test_point_times_order(&j);
  2254. }
  2255. secp256k1_fe_sqr(&x, &x);
  2256. }
  2257. secp256k1_fe_normalize_var(&x);
  2258. CHECK(secp256k1_fe_equal_var(&x, &xr));
  2259. }
  2260. void ecmult_const_random_mult(void) {
  2261. /* random starting point A (on the curve) */
  2262. secp256k1_ge a = SECP256K1_GE_CONST(
  2263. 0x6d986544, 0x57ff52b8, 0xcf1b8126, 0x5b802a5b,
  2264. 0xa97f9263, 0xb1e88044, 0x93351325, 0x91bc450a,
  2265. 0x535c59f7, 0x325e5d2b, 0xc391fbe8, 0x3c12787c,
  2266. 0x337e4a98, 0xe82a9011, 0x0123ba37, 0xdd769c7d
  2267. );
  2268. /* random initial factor xn */
  2269. secp256k1_scalar xn = SECP256K1_SCALAR_CONST(
  2270. 0x649d4f77, 0xc4242df7, 0x7f2079c9, 0x14530327,
  2271. 0xa31b876a, 0xd2d8ce2a, 0x2236d5c6, 0xd7b2029b
  2272. );
  2273. /* expected xn * A (from sage) */
  2274. secp256k1_ge expected_b = SECP256K1_GE_CONST(
  2275. 0x23773684, 0x4d209dc7, 0x098a786f, 0x20d06fcd,
  2276. 0x070a38bf, 0xc11ac651, 0x03004319, 0x1e2a8786,
  2277. 0xed8c3b8e, 0xc06dd57b, 0xd06ea66e, 0x45492b0f,
  2278. 0xb84e4e1b, 0xfb77e21f, 0x96baae2a, 0x63dec956
  2279. );
  2280. secp256k1_gej b;
  2281. secp256k1_ecmult_const(&b, &a, &xn, 256);
  2282. CHECK(secp256k1_ge_is_valid_var(&a));
  2283. ge_equals_gej(&expected_b, &b);
  2284. }
  2285. void ecmult_const_commutativity(void) {
  2286. secp256k1_scalar a;
  2287. secp256k1_scalar b;
  2288. secp256k1_gej res1;
  2289. secp256k1_gej res2;
  2290. secp256k1_ge mid1;
  2291. secp256k1_ge mid2;
  2292. random_scalar_order_test(&a);
  2293. random_scalar_order_test(&b);
  2294. secp256k1_ecmult_const(&res1, &secp256k1_ge_const_g, &a, 256);
  2295. secp256k1_ecmult_const(&res2, &secp256k1_ge_const_g, &b, 256);
  2296. secp256k1_ge_set_gej(&mid1, &res1);
  2297. secp256k1_ge_set_gej(&mid2, &res2);
  2298. secp256k1_ecmult_const(&res1, &mid1, &b, 256);
  2299. secp256k1_ecmult_const(&res2, &mid2, &a, 256);
  2300. secp256k1_ge_set_gej(&mid1, &res1);
  2301. secp256k1_ge_set_gej(&mid2, &res2);
  2302. ge_equals_ge(&mid1, &mid2);
  2303. }
  2304. void ecmult_const_mult_zero_one(void) {
  2305. secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
  2306. secp256k1_scalar one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
  2307. secp256k1_scalar negone;
  2308. secp256k1_gej res1;
  2309. secp256k1_ge res2;
  2310. secp256k1_ge point;
  2311. secp256k1_scalar_negate(&negone, &one);
  2312. random_group_element_test(&point);
  2313. secp256k1_ecmult_const(&res1, &point, &zero, 3);
  2314. secp256k1_ge_set_gej(&res2, &res1);
  2315. CHECK(secp256k1_ge_is_infinity(&res2));
  2316. secp256k1_ecmult_const(&res1, &point, &one, 2);
  2317. secp256k1_ge_set_gej(&res2, &res1);
  2318. ge_equals_ge(&res2, &point);
  2319. secp256k1_ecmult_const(&res1, &point, &negone, 256);
  2320. secp256k1_gej_neg(&res1, &res1);
  2321. secp256k1_ge_set_gej(&res2, &res1);
  2322. ge_equals_ge(&res2, &point);
  2323. }
  2324. void ecmult_const_chain_multiply(void) {
  2325. /* Check known result (randomly generated test problem from sage) */
  2326. const secp256k1_scalar scalar = SECP256K1_SCALAR_CONST(
  2327. 0x4968d524, 0x2abf9b7a, 0x466abbcf, 0x34b11b6d,
  2328. 0xcd83d307, 0x827bed62, 0x05fad0ce, 0x18fae63b
  2329. );
  2330. const secp256k1_gej expected_point = SECP256K1_GEJ_CONST(
  2331. 0x5494c15d, 0x32099706, 0xc2395f94, 0x348745fd,
  2332. 0x757ce30e, 0x4e8c90fb, 0xa2bad184, 0xf883c69f,
  2333. 0x5d195d20, 0xe191bf7f, 0x1be3e55f, 0x56a80196,
  2334. 0x6071ad01, 0xf1462f66, 0xc997fa94, 0xdb858435
  2335. );
  2336. secp256k1_gej point;
  2337. secp256k1_ge res;
  2338. int i;
  2339. secp256k1_gej_set_ge(&point, &secp256k1_ge_const_g);
  2340. for (i = 0; i < 100; ++i) {
  2341. secp256k1_ge tmp;
  2342. secp256k1_ge_set_gej(&tmp, &point);
  2343. secp256k1_ecmult_const(&point, &tmp, &scalar, 256);
  2344. }
  2345. secp256k1_ge_set_gej(&res, &point);
  2346. ge_equals_gej(&res, &expected_point);
  2347. }
  2348. void run_ecmult_const_tests(void) {
  2349. ecmult_const_mult_zero_one();
  2350. ecmult_const_random_mult();
  2351. ecmult_const_commutativity();
  2352. ecmult_const_chain_multiply();
  2353. }
  2354. typedef struct {
  2355. secp256k1_scalar *sc;
  2356. secp256k1_ge *pt;
  2357. } ecmult_multi_data;
  2358. static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata) {
  2359. ecmult_multi_data *data = (ecmult_multi_data*) cbdata;
  2360. *sc = data->sc[idx];
  2361. *pt = data->pt[idx];
  2362. return 1;
  2363. }
  2364. static int ecmult_multi_false_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata) {
  2365. (void)sc;
  2366. (void)pt;
  2367. (void)idx;
  2368. (void)cbdata;
  2369. return 0;
  2370. }
  2371. void test_ecmult_multi(secp256k1_scratch *scratch, secp256k1_ecmult_multi_func ecmult_multi) {
  2372. int ncount;
  2373. secp256k1_scalar szero;
  2374. secp256k1_scalar sc[32];
  2375. secp256k1_ge pt[32];
  2376. secp256k1_gej r;
  2377. secp256k1_gej r2;
  2378. ecmult_multi_data data;
  2379. secp256k1_scratch *scratch_empty;
  2380. data.sc = sc;
  2381. data.pt = pt;
  2382. secp256k1_scalar_set_int(&szero, 0);
  2383. /* No points to multiply */
  2384. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, NULL, ecmult_multi_callback, &data, 0));
  2385. /* Check 1- and 2-point multiplies against ecmult */
  2386. for (ncount = 0; ncount < count; ncount++) {
  2387. secp256k1_ge ptg;
  2388. secp256k1_gej ptgj;
  2389. random_scalar_order(&sc[0]);
  2390. random_scalar_order(&sc[1]);
  2391. random_group_element_test(&ptg);
  2392. secp256k1_gej_set_ge(&ptgj, &ptg);
  2393. pt[0] = ptg;
  2394. pt[1] = secp256k1_ge_const_g;
  2395. /* only G scalar */
  2396. secp256k1_ecmult(&ctx->ecmult_ctx, &r2, &ptgj, &szero, &sc[0]);
  2397. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &sc[0], ecmult_multi_callback, &data, 0));
  2398. secp256k1_gej_neg(&r2, &r2);
  2399. secp256k1_gej_add_var(&r, &r, &r2, NULL);
  2400. CHECK(secp256k1_gej_is_infinity(&r));
  2401. /* 1-point */
  2402. secp256k1_ecmult(&ctx->ecmult_ctx, &r2, &ptgj, &sc[0], &szero);
  2403. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 1));
  2404. secp256k1_gej_neg(&r2, &r2);
  2405. secp256k1_gej_add_var(&r, &r, &r2, NULL);
  2406. CHECK(secp256k1_gej_is_infinity(&r));
  2407. /* Try to multiply 1 point, but scratch space is empty */
  2408. scratch_empty = secp256k1_scratch_create(&ctx->error_callback, 0);
  2409. CHECK(!ecmult_multi(&ctx->ecmult_ctx, scratch_empty, &r, &szero, ecmult_multi_callback, &data, 1));
  2410. secp256k1_scratch_destroy(scratch_empty);
  2411. /* Try to multiply 1 point, but callback returns false */
  2412. CHECK(!ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_false_callback, &data, 1));
  2413. /* 2-point */
  2414. secp256k1_ecmult(&ctx->ecmult_ctx, &r2, &ptgj, &sc[0], &sc[1]);
  2415. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 2));
  2416. secp256k1_gej_neg(&r2, &r2);
  2417. secp256k1_gej_add_var(&r, &r, &r2, NULL);
  2418. CHECK(secp256k1_gej_is_infinity(&r));
  2419. /* 2-point with G scalar */
  2420. secp256k1_ecmult(&ctx->ecmult_ctx, &r2, &ptgj, &sc[0], &sc[1]);
  2421. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &sc[1], ecmult_multi_callback, &data, 1));
  2422. secp256k1_gej_neg(&r2, &r2);
  2423. secp256k1_gej_add_var(&r, &r, &r2, NULL);
  2424. CHECK(secp256k1_gej_is_infinity(&r));
  2425. }
  2426. /* Check infinite outputs of various forms */
  2427. for (ncount = 0; ncount < count; ncount++) {
  2428. secp256k1_ge ptg;
  2429. size_t i, j;
  2430. size_t sizes[] = { 2, 10, 32 };
  2431. for (j = 0; j < 3; j++) {
  2432. for (i = 0; i < 32; i++) {
  2433. random_scalar_order(&sc[i]);
  2434. secp256k1_ge_set_infinity(&pt[i]);
  2435. }
  2436. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, sizes[j]));
  2437. CHECK(secp256k1_gej_is_infinity(&r));
  2438. }
  2439. for (j = 0; j < 3; j++) {
  2440. for (i = 0; i < 32; i++) {
  2441. random_group_element_test(&ptg);
  2442. pt[i] = ptg;
  2443. secp256k1_scalar_set_int(&sc[i], 0);
  2444. }
  2445. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, sizes[j]));
  2446. CHECK(secp256k1_gej_is_infinity(&r));
  2447. }
  2448. for (j = 0; j < 3; j++) {
  2449. random_group_element_test(&ptg);
  2450. for (i = 0; i < 16; i++) {
  2451. random_scalar_order(&sc[2*i]);
  2452. secp256k1_scalar_negate(&sc[2*i + 1], &sc[2*i]);
  2453. pt[2 * i] = ptg;
  2454. pt[2 * i + 1] = ptg;
  2455. }
  2456. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, sizes[j]));
  2457. CHECK(secp256k1_gej_is_infinity(&r));
  2458. random_scalar_order(&sc[0]);
  2459. for (i = 0; i < 16; i++) {
  2460. random_group_element_test(&ptg);
  2461. sc[2*i] = sc[0];
  2462. sc[2*i+1] = sc[0];
  2463. pt[2 * i] = ptg;
  2464. secp256k1_ge_neg(&pt[2*i+1], &pt[2*i]);
  2465. }
  2466. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, sizes[j]));
  2467. CHECK(secp256k1_gej_is_infinity(&r));
  2468. }
  2469. random_group_element_test(&ptg);
  2470. secp256k1_scalar_set_int(&sc[0], 0);
  2471. pt[0] = ptg;
  2472. for (i = 1; i < 32; i++) {
  2473. pt[i] = ptg;
  2474. random_scalar_order(&sc[i]);
  2475. secp256k1_scalar_add(&sc[0], &sc[0], &sc[i]);
  2476. secp256k1_scalar_negate(&sc[i], &sc[i]);
  2477. }
  2478. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 32));
  2479. CHECK(secp256k1_gej_is_infinity(&r));
  2480. }
  2481. /* Check random points, constant scalar */
  2482. for (ncount = 0; ncount < count; ncount++) {
  2483. size_t i;
  2484. secp256k1_gej_set_infinity(&r);
  2485. random_scalar_order(&sc[0]);
  2486. for (i = 0; i < 20; i++) {
  2487. secp256k1_ge ptg;
  2488. sc[i] = sc[0];
  2489. random_group_element_test(&ptg);
  2490. pt[i] = ptg;
  2491. secp256k1_gej_add_ge_var(&r, &r, &pt[i], NULL);
  2492. }
  2493. secp256k1_ecmult(&ctx->ecmult_ctx, &r2, &r, &sc[0], &szero);
  2494. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 20));
  2495. secp256k1_gej_neg(&r2, &r2);
  2496. secp256k1_gej_add_var(&r, &r, &r2, NULL);
  2497. CHECK(secp256k1_gej_is_infinity(&r));
  2498. }
  2499. /* Check random scalars, constant point */
  2500. for (ncount = 0; ncount < count; ncount++) {
  2501. size_t i;
  2502. secp256k1_ge ptg;
  2503. secp256k1_gej p0j;
  2504. secp256k1_scalar rs;
  2505. secp256k1_scalar_set_int(&rs, 0);
  2506. random_group_element_test(&ptg);
  2507. for (i = 0; i < 20; i++) {
  2508. random_scalar_order(&sc[i]);
  2509. pt[i] = ptg;
  2510. secp256k1_scalar_add(&rs, &rs, &sc[i]);
  2511. }
  2512. secp256k1_gej_set_ge(&p0j, &pt[0]);
  2513. secp256k1_ecmult(&ctx->ecmult_ctx, &r2, &p0j, &rs, &szero);
  2514. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 20));
  2515. secp256k1_gej_neg(&r2, &r2);
  2516. secp256k1_gej_add_var(&r, &r, &r2, NULL);
  2517. CHECK(secp256k1_gej_is_infinity(&r));
  2518. }
  2519. /* Sanity check that zero scalars don't cause problems */
  2520. for (ncount = 0; ncount < 20; ncount++) {
  2521. random_scalar_order(&sc[ncount]);
  2522. random_group_element_test(&pt[ncount]);
  2523. }
  2524. secp256k1_scalar_clear(&sc[0]);
  2525. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 20));
  2526. secp256k1_scalar_clear(&sc[1]);
  2527. secp256k1_scalar_clear(&sc[2]);
  2528. secp256k1_scalar_clear(&sc[3]);
  2529. secp256k1_scalar_clear(&sc[4]);
  2530. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 6));
  2531. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &r, &szero, ecmult_multi_callback, &data, 5));
  2532. CHECK(secp256k1_gej_is_infinity(&r));
  2533. /* Run through s0*(t0*P) + s1*(t1*P) exhaustively for many small values of s0, s1, t0, t1 */
  2534. {
  2535. const size_t TOP = 8;
  2536. size_t s0i, s1i;
  2537. size_t t0i, t1i;
  2538. secp256k1_ge ptg;
  2539. secp256k1_gej ptgj;
  2540. random_group_element_test(&ptg);
  2541. secp256k1_gej_set_ge(&ptgj, &ptg);
  2542. for(t0i = 0; t0i < TOP; t0i++) {
  2543. for(t1i = 0; t1i < TOP; t1i++) {
  2544. secp256k1_gej t0p, t1p;
  2545. secp256k1_scalar t0, t1;
  2546. secp256k1_scalar_set_int(&t0, (t0i + 1) / 2);
  2547. secp256k1_scalar_cond_negate(&t0, t0i & 1);
  2548. secp256k1_scalar_set_int(&t1, (t1i + 1) / 2);
  2549. secp256k1_scalar_cond_negate(&t1, t1i & 1);
  2550. secp256k1_ecmult(&ctx->ecmult_ctx, &t0p, &ptgj, &t0, &szero);
  2551. secp256k1_ecmult(&ctx->ecmult_ctx, &t1p, &ptgj, &t1, &szero);
  2552. for(s0i = 0; s0i < TOP; s0i++) {
  2553. for(s1i = 0; s1i < TOP; s1i++) {
  2554. secp256k1_scalar tmp1, tmp2;
  2555. secp256k1_gej expected, actual;
  2556. secp256k1_ge_set_gej(&pt[0], &t0p);
  2557. secp256k1_ge_set_gej(&pt[1], &t1p);
  2558. secp256k1_scalar_set_int(&sc[0], (s0i + 1) / 2);
  2559. secp256k1_scalar_cond_negate(&sc[0], s0i & 1);
  2560. secp256k1_scalar_set_int(&sc[1], (s1i + 1) / 2);
  2561. secp256k1_scalar_cond_negate(&sc[1], s1i & 1);
  2562. secp256k1_scalar_mul(&tmp1, &t0, &sc[0]);
  2563. secp256k1_scalar_mul(&tmp2, &t1, &sc[1]);
  2564. secp256k1_scalar_add(&tmp1, &tmp1, &tmp2);
  2565. secp256k1_ecmult(&ctx->ecmult_ctx, &expected, &ptgj, &tmp1, &szero);
  2566. CHECK(ecmult_multi(&ctx->ecmult_ctx, scratch, &actual, &szero, ecmult_multi_callback, &data, 2));
  2567. secp256k1_gej_neg(&expected, &expected);
  2568. secp256k1_gej_add_var(&actual, &actual, &expected, NULL);
  2569. CHECK(secp256k1_gej_is_infinity(&actual));
  2570. }
  2571. }
  2572. }
  2573. }
  2574. }
  2575. }
  2576. void test_secp256k1_pippenger_bucket_window_inv(void) {
  2577. int i;
  2578. CHECK(secp256k1_pippenger_bucket_window_inv(0) == 0);
  2579. for(i = 1; i <= PIPPENGER_MAX_BUCKET_WINDOW; i++) {
  2580. #ifdef USE_ENDOMORPHISM
  2581. /* Bucket_window of 8 is not used with endo */
  2582. if (i == 8) {
  2583. continue;
  2584. }
  2585. #endif
  2586. CHECK(secp256k1_pippenger_bucket_window(secp256k1_pippenger_bucket_window_inv(i)) == i);
  2587. if (i != PIPPENGER_MAX_BUCKET_WINDOW) {
  2588. CHECK(secp256k1_pippenger_bucket_window(secp256k1_pippenger_bucket_window_inv(i)+1) > i);
  2589. }
  2590. }
  2591. }
  2592. /**
  2593. * Probabilistically test the function returning the maximum number of possible points
  2594. * for a given scratch space.
  2595. */
  2596. void test_ecmult_multi_pippenger_max_points(void) {
  2597. size_t scratch_size = secp256k1_rand_int(256);
  2598. size_t max_size = secp256k1_pippenger_scratch_size(secp256k1_pippenger_bucket_window_inv(PIPPENGER_MAX_BUCKET_WINDOW-1)+512, 12);
  2599. secp256k1_scratch *scratch;
  2600. size_t n_points_supported;
  2601. int bucket_window = 0;
  2602. for(; scratch_size < max_size; scratch_size+=256) {
  2603. scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size);
  2604. CHECK(scratch != NULL);
  2605. n_points_supported = secp256k1_pippenger_max_points(scratch);
  2606. if (n_points_supported == 0) {
  2607. secp256k1_scratch_destroy(scratch);
  2608. continue;
  2609. }
  2610. bucket_window = secp256k1_pippenger_bucket_window(n_points_supported);
  2611. CHECK(secp256k1_scratch_allocate_frame(scratch, secp256k1_pippenger_scratch_size(n_points_supported, bucket_window), PIPPENGER_SCRATCH_OBJECTS));
  2612. secp256k1_scratch_deallocate_frame(scratch);
  2613. secp256k1_scratch_destroy(scratch);
  2614. }
  2615. CHECK(bucket_window == PIPPENGER_MAX_BUCKET_WINDOW);
  2616. }
  2617. /**
  2618. * Run secp256k1_ecmult_multi_var with num points and a scratch space restricted to
  2619. * 1 <= i <= num points.
  2620. */
  2621. void test_ecmult_multi_batching(void) {
  2622. static const int n_points = 2*ECMULT_PIPPENGER_THRESHOLD;
  2623. secp256k1_scalar scG;
  2624. secp256k1_scalar szero;
  2625. secp256k1_scalar *sc = (secp256k1_scalar *)checked_malloc(&ctx->error_callback, sizeof(secp256k1_scalar) * n_points);
  2626. secp256k1_ge *pt = (secp256k1_ge *)checked_malloc(&ctx->error_callback, sizeof(secp256k1_ge) * n_points);
  2627. secp256k1_gej r;
  2628. secp256k1_gej r2;
  2629. ecmult_multi_data data;
  2630. int i;
  2631. secp256k1_scratch *scratch;
  2632. secp256k1_gej_set_infinity(&r2);
  2633. secp256k1_scalar_set_int(&szero, 0);
  2634. /* Get random scalars and group elements and compute result */
  2635. random_scalar_order(&scG);
  2636. secp256k1_ecmult(&ctx->ecmult_ctx, &r2, &r2, &szero, &scG);
  2637. for(i = 0; i < n_points; i++) {
  2638. secp256k1_ge ptg;
  2639. secp256k1_gej ptgj;
  2640. random_group_element_test(&ptg);
  2641. secp256k1_gej_set_ge(&ptgj, &ptg);
  2642. pt[i] = ptg;
  2643. random_scalar_order(&sc[i]);
  2644. secp256k1_ecmult(&ctx->ecmult_ctx, &ptgj, &ptgj, &sc[i], NULL);
  2645. secp256k1_gej_add_var(&r2, &r2, &ptgj, NULL);
  2646. }
  2647. data.sc = sc;
  2648. data.pt = pt;
  2649. /* Test with empty scratch space */
  2650. scratch = secp256k1_scratch_create(&ctx->error_callback, 0);
  2651. CHECK(!secp256k1_ecmult_multi_var(&ctx->ecmult_ctx, scratch, &r, &scG, ecmult_multi_callback, &data, 1));
  2652. secp256k1_scratch_destroy(scratch);
  2653. /* Test with space for 1 point in pippenger. That's not enough because
  2654. * ecmult_multi selects strauss which requires more memory. */
  2655. scratch = secp256k1_scratch_create(&ctx->error_callback, secp256k1_pippenger_scratch_size(1, 1) + PIPPENGER_SCRATCH_OBJECTS*ALIGNMENT);
  2656. CHECK(!secp256k1_ecmult_multi_var(&ctx->ecmult_ctx, scratch, &r, &scG, ecmult_multi_callback, &data, 1));
  2657. secp256k1_scratch_destroy(scratch);
  2658. secp256k1_gej_neg(&r2, &r2);
  2659. for(i = 1; i <= n_points; i++) {
  2660. if (i > ECMULT_PIPPENGER_THRESHOLD) {
  2661. int bucket_window = secp256k1_pippenger_bucket_window(i);
  2662. size_t scratch_size = secp256k1_pippenger_scratch_size(i, bucket_window);
  2663. scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size + PIPPENGER_SCRATCH_OBJECTS*ALIGNMENT);
  2664. } else {
  2665. size_t scratch_size = secp256k1_strauss_scratch_size(i);
  2666. scratch = secp256k1_scratch_create(&ctx->error_callback, scratch_size + STRAUSS_SCRATCH_OBJECTS*ALIGNMENT);
  2667. }
  2668. CHECK(secp256k1_ecmult_multi_var(&ctx->ecmult_ctx, scratch, &r, &scG, ecmult_multi_callback, &data, n_points));
  2669. secp256k1_gej_add_var(&r, &r, &r2, NULL);
  2670. CHECK(secp256k1_gej_is_infinity(&r));
  2671. secp256k1_scratch_destroy(scratch);
  2672. }
  2673. free(sc);
  2674. free(pt);
  2675. }
  2676. void run_ecmult_multi_tests(void) {
  2677. secp256k1_scratch *scratch;
  2678. test_secp256k1_pippenger_bucket_window_inv();
  2679. test_ecmult_multi_pippenger_max_points();
  2680. scratch = secp256k1_scratch_create(&ctx->error_callback, 819200);
  2681. test_ecmult_multi(scratch, secp256k1_ecmult_multi_var);
  2682. test_ecmult_multi(scratch, secp256k1_ecmult_pippenger_batch_single);
  2683. test_ecmult_multi(scratch, secp256k1_ecmult_strauss_batch_single);
  2684. secp256k1_scratch_destroy(scratch);
  2685. /* Run test_ecmult_multi with space for exactly one point */
  2686. scratch = secp256k1_scratch_create(&ctx->error_callback, secp256k1_strauss_scratch_size(1) + STRAUSS_SCRATCH_OBJECTS*ALIGNMENT);
  2687. test_ecmult_multi(scratch, secp256k1_ecmult_multi_var);
  2688. secp256k1_scratch_destroy(scratch);
  2689. test_ecmult_multi_batching();
  2690. }
  2691. void test_wnaf(const secp256k1_scalar *number, int w) {
  2692. secp256k1_scalar x, two, t;
  2693. int wnaf[256];
  2694. int zeroes = -1;
  2695. int i;
  2696. int bits;
  2697. secp256k1_scalar_set_int(&x, 0);
  2698. secp256k1_scalar_set_int(&two, 2);
  2699. bits = secp256k1_ecmult_wnaf(wnaf, 256, number, w);
  2700. CHECK(bits <= 256);
  2701. for (i = bits-1; i >= 0; i--) {
  2702. int v = wnaf[i];
  2703. secp256k1_scalar_mul(&x, &x, &two);
  2704. if (v) {
  2705. CHECK(zeroes == -1 || zeroes >= w-1); /* check that distance between non-zero elements is at least w-1 */
  2706. zeroes=0;
  2707. CHECK((v & 1) == 1); /* check non-zero elements are odd */
  2708. CHECK(v <= (1 << (w-1)) - 1); /* check range below */
  2709. CHECK(v >= -(1 << (w-1)) - 1); /* check range above */
  2710. } else {
  2711. CHECK(zeroes != -1); /* check that no unnecessary zero padding exists */
  2712. zeroes++;
  2713. }
  2714. if (v >= 0) {
  2715. secp256k1_scalar_set_int(&t, v);
  2716. } else {
  2717. secp256k1_scalar_set_int(&t, -v);
  2718. secp256k1_scalar_negate(&t, &t);
  2719. }
  2720. secp256k1_scalar_add(&x, &x, &t);
  2721. }
  2722. CHECK(secp256k1_scalar_eq(&x, number)); /* check that wnaf represents number */
  2723. }
  2724. void test_constant_wnaf_negate(const secp256k1_scalar *number) {
  2725. secp256k1_scalar neg1 = *number;
  2726. secp256k1_scalar neg2 = *number;
  2727. int sign1 = 1;
  2728. int sign2 = 1;
  2729. if (!secp256k1_scalar_get_bits(&neg1, 0, 1)) {
  2730. secp256k1_scalar_negate(&neg1, &neg1);
  2731. sign1 = -1;
  2732. }
  2733. sign2 = secp256k1_scalar_cond_negate(&neg2, secp256k1_scalar_is_even(&neg2));
  2734. CHECK(sign1 == sign2);
  2735. CHECK(secp256k1_scalar_eq(&neg1, &neg2));
  2736. }
  2737. void test_constant_wnaf(const secp256k1_scalar *number, int w) {
  2738. secp256k1_scalar x, shift;
  2739. int wnaf[256] = {0};
  2740. int i;
  2741. int skew;
  2742. int bits = 256;
  2743. secp256k1_scalar num = *number;
  2744. secp256k1_scalar_set_int(&x, 0);
  2745. secp256k1_scalar_set_int(&shift, 1 << w);
  2746. /* With USE_ENDOMORPHISM on we only consider 128-bit numbers */
  2747. #ifdef USE_ENDOMORPHISM
  2748. for (i = 0; i < 16; ++i) {
  2749. secp256k1_scalar_shr_int(&num, 8);
  2750. }
  2751. bits = 128;
  2752. #endif
  2753. skew = secp256k1_wnaf_const(wnaf, num, w, bits);
  2754. for (i = WNAF_SIZE_BITS(bits, w); i >= 0; --i) {
  2755. secp256k1_scalar t;
  2756. int v = wnaf[i];
  2757. CHECK(v != 0); /* check nonzero */
  2758. CHECK(v & 1); /* check parity */
  2759. CHECK(v > -(1 << w)); /* check range above */
  2760. CHECK(v < (1 << w)); /* check range below */
  2761. secp256k1_scalar_mul(&x, &x, &shift);
  2762. if (v >= 0) {
  2763. secp256k1_scalar_set_int(&t, v);
  2764. } else {
  2765. secp256k1_scalar_set_int(&t, -v);
  2766. secp256k1_scalar_negate(&t, &t);
  2767. }
  2768. secp256k1_scalar_add(&x, &x, &t);
  2769. }
  2770. /* Skew num because when encoding numbers as odd we use an offset */
  2771. secp256k1_scalar_cadd_bit(&num, skew == 2, 1);
  2772. CHECK(secp256k1_scalar_eq(&x, &num));
  2773. }
  2774. void test_fixed_wnaf(const secp256k1_scalar *number, int w) {
  2775. secp256k1_scalar x, shift;
  2776. int wnaf[256] = {0};
  2777. int i;
  2778. int skew;
  2779. secp256k1_scalar num = *number;
  2780. secp256k1_scalar_set_int(&x, 0);
  2781. secp256k1_scalar_set_int(&shift, 1 << w);
  2782. /* With USE_ENDOMORPHISM on we only consider 128-bit numbers */
  2783. #ifdef USE_ENDOMORPHISM
  2784. for (i = 0; i < 16; ++i) {
  2785. secp256k1_scalar_shr_int(&num, 8);
  2786. }
  2787. #endif
  2788. skew = secp256k1_wnaf_fixed(wnaf, &num, w);
  2789. for (i = WNAF_SIZE(w)-1; i >= 0; --i) {
  2790. secp256k1_scalar t;
  2791. int v = wnaf[i];
  2792. CHECK(v == 0 || v & 1); /* check parity */
  2793. CHECK(v > -(1 << w)); /* check range above */
  2794. CHECK(v < (1 << w)); /* check range below */
  2795. secp256k1_scalar_mul(&x, &x, &shift);
  2796. if (v >= 0) {
  2797. secp256k1_scalar_set_int(&t, v);
  2798. } else {
  2799. secp256k1_scalar_set_int(&t, -v);
  2800. secp256k1_scalar_negate(&t, &t);
  2801. }
  2802. secp256k1_scalar_add(&x, &x, &t);
  2803. }
  2804. /* If skew is 1 then add 1 to num */
  2805. secp256k1_scalar_cadd_bit(&num, 0, skew == 1);
  2806. CHECK(secp256k1_scalar_eq(&x, &num));
  2807. }
  2808. /* Checks that the first 8 elements of wnaf are equal to wnaf_expected and the
  2809. * rest is 0.*/
  2810. void test_fixed_wnaf_small_helper(int *wnaf, int *wnaf_expected, int w) {
  2811. int i;
  2812. for (i = WNAF_SIZE(w)-1; i >= 8; --i) {
  2813. CHECK(wnaf[i] == 0);
  2814. }
  2815. for (i = 7; i >= 0; --i) {
  2816. CHECK(wnaf[i] == wnaf_expected[i]);
  2817. }
  2818. }
  2819. void test_fixed_wnaf_small(void) {
  2820. int w = 4;
  2821. int wnaf[256] = {0};
  2822. int i;
  2823. int skew;
  2824. secp256k1_scalar num;
  2825. secp256k1_scalar_set_int(&num, 0);
  2826. skew = secp256k1_wnaf_fixed(wnaf, &num, w);
  2827. for (i = WNAF_SIZE(w)-1; i >= 0; --i) {
  2828. int v = wnaf[i];
  2829. CHECK(v == 0);
  2830. }
  2831. CHECK(skew == 0);
  2832. secp256k1_scalar_set_int(&num, 1);
  2833. skew = secp256k1_wnaf_fixed(wnaf, &num, w);
  2834. for (i = WNAF_SIZE(w)-1; i >= 1; --i) {
  2835. int v = wnaf[i];
  2836. CHECK(v == 0);
  2837. }
  2838. CHECK(wnaf[0] == 1);
  2839. CHECK(skew == 0);
  2840. {
  2841. int wnaf_expected[8] = { 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf };
  2842. secp256k1_scalar_set_int(&num, 0xffffffff);
  2843. skew = secp256k1_wnaf_fixed(wnaf, &num, w);
  2844. test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
  2845. CHECK(skew == 0);
  2846. }
  2847. {
  2848. int wnaf_expected[8] = { -1, -1, -1, -1, -1, -1, -1, 0xf };
  2849. secp256k1_scalar_set_int(&num, 0xeeeeeeee);
  2850. skew = secp256k1_wnaf_fixed(wnaf, &num, w);
  2851. test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
  2852. CHECK(skew == 1);
  2853. }
  2854. {
  2855. int wnaf_expected[8] = { 1, 0, 1, 0, 1, 0, 1, 0 };
  2856. secp256k1_scalar_set_int(&num, 0x01010101);
  2857. skew = secp256k1_wnaf_fixed(wnaf, &num, w);
  2858. test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
  2859. CHECK(skew == 0);
  2860. }
  2861. {
  2862. int wnaf_expected[8] = { -0xf, 0, 0xf, -0xf, 0, 0xf, 1, 0 };
  2863. secp256k1_scalar_set_int(&num, 0x01ef1ef1);
  2864. skew = secp256k1_wnaf_fixed(wnaf, &num, w);
  2865. test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
  2866. CHECK(skew == 0);
  2867. }
  2868. }
  2869. void run_wnaf(void) {
  2870. int i;
  2871. secp256k1_scalar n = {{0}};
  2872. /* Sanity check: 1 and 2 are the smallest odd and even numbers and should
  2873. * have easier-to-diagnose failure modes */
  2874. n.d[0] = 1;
  2875. test_constant_wnaf(&n, 4);
  2876. n.d[0] = 2;
  2877. test_constant_wnaf(&n, 4);
  2878. /* Test 0 */
  2879. test_fixed_wnaf_small();
  2880. /* Random tests */
  2881. for (i = 0; i < count; i++) {
  2882. random_scalar_order(&n);
  2883. test_wnaf(&n, 4+(i%10));
  2884. test_constant_wnaf_negate(&n);
  2885. test_constant_wnaf(&n, 4 + (i % 10));
  2886. test_fixed_wnaf(&n, 4 + (i % 10));
  2887. }
  2888. secp256k1_scalar_set_int(&n, 0);
  2889. CHECK(secp256k1_scalar_cond_negate(&n, 1) == -1);
  2890. CHECK(secp256k1_scalar_is_zero(&n));
  2891. CHECK(secp256k1_scalar_cond_negate(&n, 0) == 1);
  2892. CHECK(secp256k1_scalar_is_zero(&n));
  2893. }
  2894. void test_ecmult_constants(void) {
  2895. /* Test ecmult_gen() for [0..36) and [order-36..0). */
  2896. secp256k1_scalar x;
  2897. secp256k1_gej r;
  2898. secp256k1_ge ng;
  2899. int i;
  2900. int j;
  2901. secp256k1_ge_neg(&ng, &secp256k1_ge_const_g);
  2902. for (i = 0; i < 36; i++ ) {
  2903. secp256k1_scalar_set_int(&x, i);
  2904. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &r, &x);
  2905. for (j = 0; j < i; j++) {
  2906. if (j == i - 1) {
  2907. ge_equals_gej(&secp256k1_ge_const_g, &r);
  2908. }
  2909. secp256k1_gej_add_ge(&r, &r, &ng);
  2910. }
  2911. CHECK(secp256k1_gej_is_infinity(&r));
  2912. }
  2913. for (i = 1; i <= 36; i++ ) {
  2914. secp256k1_scalar_set_int(&x, i);
  2915. secp256k1_scalar_negate(&x, &x);
  2916. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &r, &x);
  2917. for (j = 0; j < i; j++) {
  2918. if (j == i - 1) {
  2919. ge_equals_gej(&ng, &r);
  2920. }
  2921. secp256k1_gej_add_ge(&r, &r, &secp256k1_ge_const_g);
  2922. }
  2923. CHECK(secp256k1_gej_is_infinity(&r));
  2924. }
  2925. }
  2926. void run_ecmult_constants(void) {
  2927. test_ecmult_constants();
  2928. }
  2929. void test_ecmult_gen_blind(void) {
  2930. /* Test ecmult_gen() blinding and confirm that the blinding changes, the affine points match, and the z's don't match. */
  2931. secp256k1_scalar key;
  2932. secp256k1_scalar b;
  2933. unsigned char seed32[32];
  2934. secp256k1_gej pgej;
  2935. secp256k1_gej pgej2;
  2936. secp256k1_gej i;
  2937. secp256k1_ge pge;
  2938. random_scalar_order_test(&key);
  2939. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pgej, &key);
  2940. secp256k1_rand256(seed32);
  2941. b = ctx->ecmult_gen_ctx.blind;
  2942. i = ctx->ecmult_gen_ctx.initial;
  2943. secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
  2944. CHECK(!secp256k1_scalar_eq(&b, &ctx->ecmult_gen_ctx.blind));
  2945. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pgej2, &key);
  2946. CHECK(!gej_xyz_equals_gej(&pgej, &pgej2));
  2947. CHECK(!gej_xyz_equals_gej(&i, &ctx->ecmult_gen_ctx.initial));
  2948. secp256k1_ge_set_gej(&pge, &pgej);
  2949. ge_equals_gej(&pge, &pgej2);
  2950. }
  2951. void test_ecmult_gen_blind_reset(void) {
  2952. /* Test ecmult_gen() blinding reset and confirm that the blinding is consistent. */
  2953. secp256k1_scalar b;
  2954. secp256k1_gej initial;
  2955. secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, 0);
  2956. b = ctx->ecmult_gen_ctx.blind;
  2957. initial = ctx->ecmult_gen_ctx.initial;
  2958. secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, 0);
  2959. CHECK(secp256k1_scalar_eq(&b, &ctx->ecmult_gen_ctx.blind));
  2960. CHECK(gej_xyz_equals_gej(&initial, &ctx->ecmult_gen_ctx.initial));
  2961. }
  2962. void run_ecmult_gen_blind(void) {
  2963. int i;
  2964. test_ecmult_gen_blind_reset();
  2965. for (i = 0; i < 10; i++) {
  2966. test_ecmult_gen_blind();
  2967. }
  2968. }
  2969. #ifdef USE_ENDOMORPHISM
  2970. /***** ENDOMORPHISH TESTS *****/
  2971. void test_scalar_split(void) {
  2972. secp256k1_scalar full;
  2973. secp256k1_scalar s1, slam;
  2974. const unsigned char zero[32] = {0};
  2975. unsigned char tmp[32];
  2976. random_scalar_order_test(&full);
  2977. secp256k1_scalar_split_lambda(&s1, &slam, &full);
  2978. /* check that both are <= 128 bits in size */
  2979. if (secp256k1_scalar_is_high(&s1)) {
  2980. secp256k1_scalar_negate(&s1, &s1);
  2981. }
  2982. if (secp256k1_scalar_is_high(&slam)) {
  2983. secp256k1_scalar_negate(&slam, &slam);
  2984. }
  2985. secp256k1_scalar_get_b32(tmp, &s1);
  2986. CHECK(memcmp(zero, tmp, 16) == 0);
  2987. secp256k1_scalar_get_b32(tmp, &slam);
  2988. CHECK(memcmp(zero, tmp, 16) == 0);
  2989. }
  2990. void run_endomorphism_tests(void) {
  2991. test_scalar_split();
  2992. }
  2993. #endif
  2994. void ec_pubkey_parse_pointtest(const unsigned char *input, int xvalid, int yvalid) {
  2995. unsigned char pubkeyc[65];
  2996. secp256k1_pubkey pubkey;
  2997. secp256k1_ge ge;
  2998. size_t pubkeyclen;
  2999. int32_t ecount;
  3000. ecount = 0;
  3001. secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
  3002. for (pubkeyclen = 3; pubkeyclen <= 65; pubkeyclen++) {
  3003. /* Smaller sizes are tested exhaustively elsewhere. */
  3004. int32_t i;
  3005. memcpy(&pubkeyc[1], input, 64);
  3006. VG_UNDEF(&pubkeyc[pubkeyclen], 65 - pubkeyclen);
  3007. for (i = 0; i < 256; i++) {
  3008. /* Try all type bytes. */
  3009. int xpass;
  3010. int ypass;
  3011. int ysign;
  3012. pubkeyc[0] = i;
  3013. /* What sign does this point have? */
  3014. ysign = (input[63] & 1) + 2;
  3015. /* For the current type (i) do we expect parsing to work? Handled all of compressed/uncompressed/hybrid. */
  3016. xpass = xvalid && (pubkeyclen == 33) && ((i & 254) == 2);
  3017. /* Do we expect a parse and re-serialize as uncompressed to give a matching y? */
  3018. ypass = xvalid && yvalid && ((i & 4) == ((pubkeyclen == 65) << 2)) &&
  3019. ((i == 4) || ((i & 251) == ysign)) && ((pubkeyclen == 33) || (pubkeyclen == 65));
  3020. if (xpass || ypass) {
  3021. /* These cases must parse. */
  3022. unsigned char pubkeyo[65];
  3023. size_t outl;
  3024. memset(&pubkey, 0, sizeof(pubkey));
  3025. VG_UNDEF(&pubkey, sizeof(pubkey));
  3026. ecount = 0;
  3027. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, pubkeyclen) == 1);
  3028. VG_CHECK(&pubkey, sizeof(pubkey));
  3029. outl = 65;
  3030. VG_UNDEF(pubkeyo, 65);
  3031. CHECK(secp256k1_ec_pubkey_serialize(ctx, pubkeyo, &outl, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
  3032. VG_CHECK(pubkeyo, outl);
  3033. CHECK(outl == 33);
  3034. CHECK(memcmp(&pubkeyo[1], &pubkeyc[1], 32) == 0);
  3035. CHECK((pubkeyclen != 33) || (pubkeyo[0] == pubkeyc[0]));
  3036. if (ypass) {
  3037. /* This test isn't always done because we decode with alternative signs, so the y won't match. */
  3038. CHECK(pubkeyo[0] == ysign);
  3039. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 1);
  3040. memset(&pubkey, 0, sizeof(pubkey));
  3041. VG_UNDEF(&pubkey, sizeof(pubkey));
  3042. secp256k1_pubkey_save(&pubkey, &ge);
  3043. VG_CHECK(&pubkey, sizeof(pubkey));
  3044. outl = 65;
  3045. VG_UNDEF(pubkeyo, 65);
  3046. CHECK(secp256k1_ec_pubkey_serialize(ctx, pubkeyo, &outl, &pubkey, SECP256K1_EC_UNCOMPRESSED) == 1);
  3047. VG_CHECK(pubkeyo, outl);
  3048. CHECK(outl == 65);
  3049. CHECK(pubkeyo[0] == 4);
  3050. CHECK(memcmp(&pubkeyo[1], input, 64) == 0);
  3051. }
  3052. CHECK(ecount == 0);
  3053. } else {
  3054. /* These cases must fail to parse. */
  3055. memset(&pubkey, 0xfe, sizeof(pubkey));
  3056. ecount = 0;
  3057. VG_UNDEF(&pubkey, sizeof(pubkey));
  3058. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, pubkeyclen) == 0);
  3059. VG_CHECK(&pubkey, sizeof(pubkey));
  3060. CHECK(ecount == 0);
  3061. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3062. CHECK(ecount == 1);
  3063. }
  3064. }
  3065. }
  3066. secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
  3067. }
  3068. void run_ec_pubkey_parse_test(void) {
  3069. #define SECP256K1_EC_PARSE_TEST_NVALID (12)
  3070. const unsigned char valid[SECP256K1_EC_PARSE_TEST_NVALID][64] = {
  3071. {
  3072. /* Point with leading and trailing zeros in x and y serialization. */
  3073. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x52,
  3074. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3075. 0x00, 0x00, 0x64, 0xef, 0xa1, 0x7b, 0x77, 0x61, 0xe1, 0xe4, 0x27, 0x06, 0x98, 0x9f, 0xb4, 0x83,
  3076. 0xb8, 0xd2, 0xd4, 0x9b, 0xf7, 0x8f, 0xae, 0x98, 0x03, 0xf0, 0x99, 0xb8, 0x34, 0xed, 0xeb, 0x00
  3077. },
  3078. {
  3079. /* Point with x equal to a 3rd root of unity.*/
  3080. 0x7a, 0xe9, 0x6a, 0x2b, 0x65, 0x7c, 0x07, 0x10, 0x6e, 0x64, 0x47, 0x9e, 0xac, 0x34, 0x34, 0xe9,
  3081. 0x9c, 0xf0, 0x49, 0x75, 0x12, 0xf5, 0x89, 0x95, 0xc1, 0x39, 0x6c, 0x28, 0x71, 0x95, 0x01, 0xee,
  3082. 0x42, 0x18, 0xf2, 0x0a, 0xe6, 0xc6, 0x46, 0xb3, 0x63, 0xdb, 0x68, 0x60, 0x58, 0x22, 0xfb, 0x14,
  3083. 0x26, 0x4c, 0xa8, 0xd2, 0x58, 0x7f, 0xdd, 0x6f, 0xbc, 0x75, 0x0d, 0x58, 0x7e, 0x76, 0xa7, 0xee,
  3084. },
  3085. {
  3086. /* Point with largest x. (1/2) */
  3087. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3088. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2c,
  3089. 0x0e, 0x99, 0x4b, 0x14, 0xea, 0x72, 0xf8, 0xc3, 0xeb, 0x95, 0xc7, 0x1e, 0xf6, 0x92, 0x57, 0x5e,
  3090. 0x77, 0x50, 0x58, 0x33, 0x2d, 0x7e, 0x52, 0xd0, 0x99, 0x5c, 0xf8, 0x03, 0x88, 0x71, 0xb6, 0x7d,
  3091. },
  3092. {
  3093. /* Point with largest x. (2/2) */
  3094. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3095. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2c,
  3096. 0xf1, 0x66, 0xb4, 0xeb, 0x15, 0x8d, 0x07, 0x3c, 0x14, 0x6a, 0x38, 0xe1, 0x09, 0x6d, 0xa8, 0xa1,
  3097. 0x88, 0xaf, 0xa7, 0xcc, 0xd2, 0x81, 0xad, 0x2f, 0x66, 0xa3, 0x07, 0xfb, 0x77, 0x8e, 0x45, 0xb2,
  3098. },
  3099. {
  3100. /* Point with smallest x. (1/2) */
  3101. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  3103. 0x42, 0x18, 0xf2, 0x0a, 0xe6, 0xc6, 0x46, 0xb3, 0x63, 0xdb, 0x68, 0x60, 0x58, 0x22, 0xfb, 0x14,
  3104. 0x26, 0x4c, 0xa8, 0xd2, 0x58, 0x7f, 0xdd, 0x6f, 0xbc, 0x75, 0x0d, 0x58, 0x7e, 0x76, 0xa7, 0xee,
  3105. },
  3106. {
  3107. /* Point with smallest x. (2/2) */
  3108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  3110. 0xbd, 0xe7, 0x0d, 0xf5, 0x19, 0x39, 0xb9, 0x4c, 0x9c, 0x24, 0x97, 0x9f, 0xa7, 0xdd, 0x04, 0xeb,
  3111. 0xd9, 0xb3, 0x57, 0x2d, 0xa7, 0x80, 0x22, 0x90, 0x43, 0x8a, 0xf2, 0xa6, 0x81, 0x89, 0x54, 0x41,
  3112. },
  3113. {
  3114. /* Point with largest y. (1/3) */
  3115. 0x1f, 0xe1, 0xe5, 0xef, 0x3f, 0xce, 0xb5, 0xc1, 0x35, 0xab, 0x77, 0x41, 0x33, 0x3c, 0xe5, 0xa6,
  3116. 0xe8, 0x0d, 0x68, 0x16, 0x76, 0x53, 0xf6, 0xb2, 0xb2, 0x4b, 0xcb, 0xcf, 0xaa, 0xaf, 0xf5, 0x07,
  3117. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3118. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2e,
  3119. },
  3120. {
  3121. /* Point with largest y. (2/3) */
  3122. 0xcb, 0xb0, 0xde, 0xab, 0x12, 0x57, 0x54, 0xf1, 0xfd, 0xb2, 0x03, 0x8b, 0x04, 0x34, 0xed, 0x9c,
  3123. 0xb3, 0xfb, 0x53, 0xab, 0x73, 0x53, 0x91, 0x12, 0x99, 0x94, 0xa5, 0x35, 0xd9, 0x25, 0xf6, 0x73,
  3124. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3125. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2e,
  3126. },
  3127. {
  3128. /* Point with largest y. (3/3) */
  3129. 0x14, 0x6d, 0x3b, 0x65, 0xad, 0xd9, 0xf5, 0x4c, 0xcc, 0xa2, 0x85, 0x33, 0xc8, 0x8e, 0x2c, 0xbc,
  3130. 0x63, 0xf7, 0x44, 0x3e, 0x16, 0x58, 0x78, 0x3a, 0xb4, 0x1f, 0x8e, 0xf9, 0x7c, 0x2a, 0x10, 0xb5,
  3131. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3132. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2e,
  3133. },
  3134. {
  3135. /* Point with smallest y. (1/3) */
  3136. 0x1f, 0xe1, 0xe5, 0xef, 0x3f, 0xce, 0xb5, 0xc1, 0x35, 0xab, 0x77, 0x41, 0x33, 0x3c, 0xe5, 0xa6,
  3137. 0xe8, 0x0d, 0x68, 0x16, 0x76, 0x53, 0xf6, 0xb2, 0xb2, 0x4b, 0xcb, 0xcf, 0xaa, 0xaf, 0xf5, 0x07,
  3138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  3140. },
  3141. {
  3142. /* Point with smallest y. (2/3) */
  3143. 0xcb, 0xb0, 0xde, 0xab, 0x12, 0x57, 0x54, 0xf1, 0xfd, 0xb2, 0x03, 0x8b, 0x04, 0x34, 0xed, 0x9c,
  3144. 0xb3, 0xfb, 0x53, 0xab, 0x73, 0x53, 0x91, 0x12, 0x99, 0x94, 0xa5, 0x35, 0xd9, 0x25, 0xf6, 0x73,
  3145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  3147. },
  3148. {
  3149. /* Point with smallest y. (3/3) */
  3150. 0x14, 0x6d, 0x3b, 0x65, 0xad, 0xd9, 0xf5, 0x4c, 0xcc, 0xa2, 0x85, 0x33, 0xc8, 0x8e, 0x2c, 0xbc,
  3151. 0x63, 0xf7, 0x44, 0x3e, 0x16, 0x58, 0x78, 0x3a, 0xb4, 0x1f, 0x8e, 0xf9, 0x7c, 0x2a, 0x10, 0xb5,
  3152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  3154. }
  3155. };
  3156. #define SECP256K1_EC_PARSE_TEST_NXVALID (4)
  3157. const unsigned char onlyxvalid[SECP256K1_EC_PARSE_TEST_NXVALID][64] = {
  3158. {
  3159. /* Valid if y overflow ignored (y = 1 mod p). (1/3) */
  3160. 0x1f, 0xe1, 0xe5, 0xef, 0x3f, 0xce, 0xb5, 0xc1, 0x35, 0xab, 0x77, 0x41, 0x33, 0x3c, 0xe5, 0xa6,
  3161. 0xe8, 0x0d, 0x68, 0x16, 0x76, 0x53, 0xf6, 0xb2, 0xb2, 0x4b, 0xcb, 0xcf, 0xaa, 0xaf, 0xf5, 0x07,
  3162. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3163. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x30,
  3164. },
  3165. {
  3166. /* Valid if y overflow ignored (y = 1 mod p). (2/3) */
  3167. 0xcb, 0xb0, 0xde, 0xab, 0x12, 0x57, 0x54, 0xf1, 0xfd, 0xb2, 0x03, 0x8b, 0x04, 0x34, 0xed, 0x9c,
  3168. 0xb3, 0xfb, 0x53, 0xab, 0x73, 0x53, 0x91, 0x12, 0x99, 0x94, 0xa5, 0x35, 0xd9, 0x25, 0xf6, 0x73,
  3169. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3170. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x30,
  3171. },
  3172. {
  3173. /* Valid if y overflow ignored (y = 1 mod p). (3/3)*/
  3174. 0x14, 0x6d, 0x3b, 0x65, 0xad, 0xd9, 0xf5, 0x4c, 0xcc, 0xa2, 0x85, 0x33, 0xc8, 0x8e, 0x2c, 0xbc,
  3175. 0x63, 0xf7, 0x44, 0x3e, 0x16, 0x58, 0x78, 0x3a, 0xb4, 0x1f, 0x8e, 0xf9, 0x7c, 0x2a, 0x10, 0xb5,
  3176. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3177. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x30,
  3178. },
  3179. {
  3180. /* x on curve, y is from y^2 = x^3 + 8. */
  3181. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3182. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  3183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03
  3185. }
  3186. };
  3187. #define SECP256K1_EC_PARSE_TEST_NINVALID (7)
  3188. const unsigned char invalid[SECP256K1_EC_PARSE_TEST_NINVALID][64] = {
  3189. {
  3190. /* x is third root of -8, y is -1 * (x^3+7); also on the curve for y^2 = x^3 + 9. */
  3191. 0x0a, 0x2d, 0x2b, 0xa9, 0x35, 0x07, 0xf1, 0xdf, 0x23, 0x37, 0x70, 0xc2, 0xa7, 0x97, 0x96, 0x2c,
  3192. 0xc6, 0x1f, 0x6d, 0x15, 0xda, 0x14, 0xec, 0xd4, 0x7d, 0x8d, 0x27, 0xae, 0x1c, 0xd5, 0xf8, 0x53,
  3193. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3194. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  3195. },
  3196. {
  3197. /* Valid if x overflow ignored (x = 1 mod p). */
  3198. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3199. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x30,
  3200. 0x42, 0x18, 0xf2, 0x0a, 0xe6, 0xc6, 0x46, 0xb3, 0x63, 0xdb, 0x68, 0x60, 0x58, 0x22, 0xfb, 0x14,
  3201. 0x26, 0x4c, 0xa8, 0xd2, 0x58, 0x7f, 0xdd, 0x6f, 0xbc, 0x75, 0x0d, 0x58, 0x7e, 0x76, 0xa7, 0xee,
  3202. },
  3203. {
  3204. /* Valid if x overflow ignored (x = 1 mod p). */
  3205. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3206. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x30,
  3207. 0xbd, 0xe7, 0x0d, 0xf5, 0x19, 0x39, 0xb9, 0x4c, 0x9c, 0x24, 0x97, 0x9f, 0xa7, 0xdd, 0x04, 0xeb,
  3208. 0xd9, 0xb3, 0x57, 0x2d, 0xa7, 0x80, 0x22, 0x90, 0x43, 0x8a, 0xf2, 0xa6, 0x81, 0x89, 0x54, 0x41,
  3209. },
  3210. {
  3211. /* x is -1, y is the result of the sqrt ladder; also on the curve for y^2 = x^3 - 5. */
  3212. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3213. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2e,
  3214. 0xf4, 0x84, 0x14, 0x5c, 0xb0, 0x14, 0x9b, 0x82, 0x5d, 0xff, 0x41, 0x2f, 0xa0, 0x52, 0xa8, 0x3f,
  3215. 0xcb, 0x72, 0xdb, 0x61, 0xd5, 0x6f, 0x37, 0x70, 0xce, 0x06, 0x6b, 0x73, 0x49, 0xa2, 0xaa, 0x28,
  3216. },
  3217. {
  3218. /* x is -1, y is the result of the sqrt ladder; also on the curve for y^2 = x^3 - 5. */
  3219. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3220. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2e,
  3221. 0x0b, 0x7b, 0xeb, 0xa3, 0x4f, 0xeb, 0x64, 0x7d, 0xa2, 0x00, 0xbe, 0xd0, 0x5f, 0xad, 0x57, 0xc0,
  3222. 0x34, 0x8d, 0x24, 0x9e, 0x2a, 0x90, 0xc8, 0x8f, 0x31, 0xf9, 0x94, 0x8b, 0xb6, 0x5d, 0x52, 0x07,
  3223. },
  3224. {
  3225. /* x is zero, y is the result of the sqrt ladder; also on the curve for y^2 = x^3 - 7. */
  3226. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3227. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3228. 0x8f, 0x53, 0x7e, 0xef, 0xdf, 0xc1, 0x60, 0x6a, 0x07, 0x27, 0xcd, 0x69, 0xb4, 0xa7, 0x33, 0x3d,
  3229. 0x38, 0xed, 0x44, 0xe3, 0x93, 0x2a, 0x71, 0x79, 0xee, 0xcb, 0x4b, 0x6f, 0xba, 0x93, 0x60, 0xdc,
  3230. },
  3231. {
  3232. /* x is zero, y is the result of the sqrt ladder; also on the curve for y^2 = x^3 - 7. */
  3233. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3234. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3235. 0x70, 0xac, 0x81, 0x10, 0x20, 0x3e, 0x9f, 0x95, 0xf8, 0xd8, 0x32, 0x96, 0x4b, 0x58, 0xcc, 0xc2,
  3236. 0xc7, 0x12, 0xbb, 0x1c, 0x6c, 0xd5, 0x8e, 0x86, 0x11, 0x34, 0xb4, 0x8f, 0x45, 0x6c, 0x9b, 0x53
  3237. }
  3238. };
  3239. const unsigned char pubkeyc[66] = {
  3240. /* Serialization of G. */
  3241. 0x04, 0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC, 0x55, 0xA0, 0x62, 0x95, 0xCE, 0x87, 0x0B,
  3242. 0x07, 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE, 0x28, 0xD9, 0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17,
  3243. 0x98, 0x48, 0x3A, 0xDA, 0x77, 0x26, 0xA3, 0xC4, 0x65, 0x5D, 0xA4, 0xFB, 0xFC, 0x0E, 0x11, 0x08,
  3244. 0xA8, 0xFD, 0x17, 0xB4, 0x48, 0xA6, 0x85, 0x54, 0x19, 0x9C, 0x47, 0xD0, 0x8F, 0xFB, 0x10, 0xD4,
  3245. 0xB8, 0x00
  3246. };
  3247. unsigned char sout[65];
  3248. unsigned char shortkey[2];
  3249. secp256k1_ge ge;
  3250. secp256k1_pubkey pubkey;
  3251. size_t len;
  3252. int32_t i;
  3253. int32_t ecount;
  3254. int32_t ecount2;
  3255. ecount = 0;
  3256. /* Nothing should be reading this far into pubkeyc. */
  3257. VG_UNDEF(&pubkeyc[65], 1);
  3258. secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
  3259. /* Zero length claimed, fail, zeroize, no illegal arg error. */
  3260. memset(&pubkey, 0xfe, sizeof(pubkey));
  3261. ecount = 0;
  3262. VG_UNDEF(shortkey, 2);
  3263. VG_UNDEF(&pubkey, sizeof(pubkey));
  3264. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, shortkey, 0) == 0);
  3265. VG_CHECK(&pubkey, sizeof(pubkey));
  3266. CHECK(ecount == 0);
  3267. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3268. CHECK(ecount == 1);
  3269. /* Length one claimed, fail, zeroize, no illegal arg error. */
  3270. for (i = 0; i < 256 ; i++) {
  3271. memset(&pubkey, 0xfe, sizeof(pubkey));
  3272. ecount = 0;
  3273. shortkey[0] = i;
  3274. VG_UNDEF(&shortkey[1], 1);
  3275. VG_UNDEF(&pubkey, sizeof(pubkey));
  3276. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, shortkey, 1) == 0);
  3277. VG_CHECK(&pubkey, sizeof(pubkey));
  3278. CHECK(ecount == 0);
  3279. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3280. CHECK(ecount == 1);
  3281. }
  3282. /* Length two claimed, fail, zeroize, no illegal arg error. */
  3283. for (i = 0; i < 65536 ; i++) {
  3284. memset(&pubkey, 0xfe, sizeof(pubkey));
  3285. ecount = 0;
  3286. shortkey[0] = i & 255;
  3287. shortkey[1] = i >> 8;
  3288. VG_UNDEF(&pubkey, sizeof(pubkey));
  3289. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, shortkey, 2) == 0);
  3290. VG_CHECK(&pubkey, sizeof(pubkey));
  3291. CHECK(ecount == 0);
  3292. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3293. CHECK(ecount == 1);
  3294. }
  3295. memset(&pubkey, 0xfe, sizeof(pubkey));
  3296. ecount = 0;
  3297. VG_UNDEF(&pubkey, sizeof(pubkey));
  3298. /* 33 bytes claimed on otherwise valid input starting with 0x04, fail, zeroize output, no illegal arg error. */
  3299. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, 33) == 0);
  3300. VG_CHECK(&pubkey, sizeof(pubkey));
  3301. CHECK(ecount == 0);
  3302. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3303. CHECK(ecount == 1);
  3304. /* NULL pubkey, illegal arg error. Pubkey isn't rewritten before this step, since it's NULL into the parser. */
  3305. CHECK(secp256k1_ec_pubkey_parse(ctx, NULL, pubkeyc, 65) == 0);
  3306. CHECK(ecount == 2);
  3307. /* NULL input string. Illegal arg and zeroize output. */
  3308. memset(&pubkey, 0xfe, sizeof(pubkey));
  3309. ecount = 0;
  3310. VG_UNDEF(&pubkey, sizeof(pubkey));
  3311. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, NULL, 65) == 0);
  3312. VG_CHECK(&pubkey, sizeof(pubkey));
  3313. CHECK(ecount == 1);
  3314. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3315. CHECK(ecount == 2);
  3316. /* 64 bytes claimed on input starting with 0x04, fail, zeroize output, no illegal arg error. */
  3317. memset(&pubkey, 0xfe, sizeof(pubkey));
  3318. ecount = 0;
  3319. VG_UNDEF(&pubkey, sizeof(pubkey));
  3320. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, 64) == 0);
  3321. VG_CHECK(&pubkey, sizeof(pubkey));
  3322. CHECK(ecount == 0);
  3323. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3324. CHECK(ecount == 1);
  3325. /* 66 bytes claimed, fail, zeroize output, no illegal arg error. */
  3326. memset(&pubkey, 0xfe, sizeof(pubkey));
  3327. ecount = 0;
  3328. VG_UNDEF(&pubkey, sizeof(pubkey));
  3329. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, 66) == 0);
  3330. VG_CHECK(&pubkey, sizeof(pubkey));
  3331. CHECK(ecount == 0);
  3332. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 0);
  3333. CHECK(ecount == 1);
  3334. /* Valid parse. */
  3335. memset(&pubkey, 0, sizeof(pubkey));
  3336. ecount = 0;
  3337. VG_UNDEF(&pubkey, sizeof(pubkey));
  3338. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, 65) == 1);
  3339. VG_CHECK(&pubkey, sizeof(pubkey));
  3340. CHECK(ecount == 0);
  3341. VG_UNDEF(&ge, sizeof(ge));
  3342. CHECK(secp256k1_pubkey_load(ctx, &ge, &pubkey) == 1);
  3343. VG_CHECK(&ge.x, sizeof(ge.x));
  3344. VG_CHECK(&ge.y, sizeof(ge.y));
  3345. VG_CHECK(&ge.infinity, sizeof(ge.infinity));
  3346. ge_equals_ge(&secp256k1_ge_const_g, &ge);
  3347. CHECK(ecount == 0);
  3348. /* secp256k1_ec_pubkey_serialize illegal args. */
  3349. ecount = 0;
  3350. len = 65;
  3351. CHECK(secp256k1_ec_pubkey_serialize(ctx, NULL, &len, &pubkey, SECP256K1_EC_UNCOMPRESSED) == 0);
  3352. CHECK(ecount == 1);
  3353. CHECK(len == 0);
  3354. CHECK(secp256k1_ec_pubkey_serialize(ctx, sout, NULL, &pubkey, SECP256K1_EC_UNCOMPRESSED) == 0);
  3355. CHECK(ecount == 2);
  3356. len = 65;
  3357. VG_UNDEF(sout, 65);
  3358. CHECK(secp256k1_ec_pubkey_serialize(ctx, sout, &len, NULL, SECP256K1_EC_UNCOMPRESSED) == 0);
  3359. VG_CHECK(sout, 65);
  3360. CHECK(ecount == 3);
  3361. CHECK(len == 0);
  3362. len = 65;
  3363. CHECK(secp256k1_ec_pubkey_serialize(ctx, sout, &len, &pubkey, ~0) == 0);
  3364. CHECK(ecount == 4);
  3365. CHECK(len == 0);
  3366. len = 65;
  3367. VG_UNDEF(sout, 65);
  3368. CHECK(secp256k1_ec_pubkey_serialize(ctx, sout, &len, &pubkey, SECP256K1_EC_UNCOMPRESSED) == 1);
  3369. VG_CHECK(sout, 65);
  3370. CHECK(ecount == 4);
  3371. CHECK(len == 65);
  3372. /* Multiple illegal args. Should still set arg error only once. */
  3373. ecount = 0;
  3374. ecount2 = 11;
  3375. CHECK(secp256k1_ec_pubkey_parse(ctx, NULL, NULL, 65) == 0);
  3376. CHECK(ecount == 1);
  3377. /* Does the illegal arg callback actually change the behavior? */
  3378. secp256k1_context_set_illegal_callback(ctx, uncounting_illegal_callback_fn, &ecount2);
  3379. CHECK(secp256k1_ec_pubkey_parse(ctx, NULL, NULL, 65) == 0);
  3380. CHECK(ecount == 1);
  3381. CHECK(ecount2 == 10);
  3382. secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
  3383. /* Try a bunch of prefabbed points with all possible encodings. */
  3384. for (i = 0; i < SECP256K1_EC_PARSE_TEST_NVALID; i++) {
  3385. ec_pubkey_parse_pointtest(valid[i], 1, 1);
  3386. }
  3387. for (i = 0; i < SECP256K1_EC_PARSE_TEST_NXVALID; i++) {
  3388. ec_pubkey_parse_pointtest(onlyxvalid[i], 1, 0);
  3389. }
  3390. for (i = 0; i < SECP256K1_EC_PARSE_TEST_NINVALID; i++) {
  3391. ec_pubkey_parse_pointtest(invalid[i], 0, 0);
  3392. }
  3393. }
  3394. void run_eckey_edge_case_test(void) {
  3395. const unsigned char orderc[32] = {
  3396. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3397. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  3398. 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
  3399. 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
  3400. };
  3401. const unsigned char zeros[sizeof(secp256k1_pubkey)] = {0x00};
  3402. unsigned char ctmp[33];
  3403. unsigned char ctmp2[33];
  3404. secp256k1_pubkey pubkey;
  3405. secp256k1_pubkey pubkey2;
  3406. secp256k1_pubkey pubkey_one;
  3407. secp256k1_pubkey pubkey_negone;
  3408. const secp256k1_pubkey *pubkeys[3];
  3409. size_t len;
  3410. int32_t ecount;
  3411. /* Group order is too large, reject. */
  3412. CHECK(secp256k1_ec_seckey_verify(ctx, orderc) == 0);
  3413. VG_UNDEF(&pubkey, sizeof(pubkey));
  3414. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, orderc) == 0);
  3415. VG_CHECK(&pubkey, sizeof(pubkey));
  3416. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3417. /* Maximum value is too large, reject. */
  3418. memset(ctmp, 255, 32);
  3419. CHECK(secp256k1_ec_seckey_verify(ctx, ctmp) == 0);
  3420. memset(&pubkey, 1, sizeof(pubkey));
  3421. VG_UNDEF(&pubkey, sizeof(pubkey));
  3422. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 0);
  3423. VG_CHECK(&pubkey, sizeof(pubkey));
  3424. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3425. /* Zero is too small, reject. */
  3426. memset(ctmp, 0, 32);
  3427. CHECK(secp256k1_ec_seckey_verify(ctx, ctmp) == 0);
  3428. memset(&pubkey, 1, sizeof(pubkey));
  3429. VG_UNDEF(&pubkey, sizeof(pubkey));
  3430. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 0);
  3431. VG_CHECK(&pubkey, sizeof(pubkey));
  3432. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3433. /* One must be accepted. */
  3434. ctmp[31] = 0x01;
  3435. CHECK(secp256k1_ec_seckey_verify(ctx, ctmp) == 1);
  3436. memset(&pubkey, 0, sizeof(pubkey));
  3437. VG_UNDEF(&pubkey, sizeof(pubkey));
  3438. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 1);
  3439. VG_CHECK(&pubkey, sizeof(pubkey));
  3440. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
  3441. pubkey_one = pubkey;
  3442. /* Group order + 1 is too large, reject. */
  3443. memcpy(ctmp, orderc, 32);
  3444. ctmp[31] = 0x42;
  3445. CHECK(secp256k1_ec_seckey_verify(ctx, ctmp) == 0);
  3446. memset(&pubkey, 1, sizeof(pubkey));
  3447. VG_UNDEF(&pubkey, sizeof(pubkey));
  3448. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 0);
  3449. VG_CHECK(&pubkey, sizeof(pubkey));
  3450. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3451. /* -1 must be accepted. */
  3452. ctmp[31] = 0x40;
  3453. CHECK(secp256k1_ec_seckey_verify(ctx, ctmp) == 1);
  3454. memset(&pubkey, 0, sizeof(pubkey));
  3455. VG_UNDEF(&pubkey, sizeof(pubkey));
  3456. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 1);
  3457. VG_CHECK(&pubkey, sizeof(pubkey));
  3458. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
  3459. pubkey_negone = pubkey;
  3460. /* Tweak of zero leaves the value unchanged. */
  3461. memset(ctmp2, 0, 32);
  3462. CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, ctmp2) == 1);
  3463. CHECK(memcmp(orderc, ctmp, 31) == 0 && ctmp[31] == 0x40);
  3464. memcpy(&pubkey2, &pubkey, sizeof(pubkey));
  3465. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
  3466. CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
  3467. /* Multiply tweak of zero zeroizes the output. */
  3468. CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, ctmp2) == 0);
  3469. CHECK(memcmp(zeros, ctmp, 32) == 0);
  3470. CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp2) == 0);
  3471. CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
  3472. memcpy(&pubkey, &pubkey2, sizeof(pubkey));
  3473. /* Overflowing key tweak zeroizes. */
  3474. memcpy(ctmp, orderc, 32);
  3475. ctmp[31] = 0x40;
  3476. CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, orderc) == 0);
  3477. CHECK(memcmp(zeros, ctmp, 32) == 0);
  3478. memcpy(ctmp, orderc, 32);
  3479. ctmp[31] = 0x40;
  3480. CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, orderc) == 0);
  3481. CHECK(memcmp(zeros, ctmp, 32) == 0);
  3482. memcpy(ctmp, orderc, 32);
  3483. ctmp[31] = 0x40;
  3484. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, orderc) == 0);
  3485. CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
  3486. memcpy(&pubkey, &pubkey2, sizeof(pubkey));
  3487. CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, orderc) == 0);
  3488. CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
  3489. memcpy(&pubkey, &pubkey2, sizeof(pubkey));
  3490. /* Private key tweaks results in a key of zero. */
  3491. ctmp2[31] = 1;
  3492. CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 0);
  3493. CHECK(memcmp(zeros, ctmp2, 32) == 0);
  3494. ctmp2[31] = 1;
  3495. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 0);
  3496. CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
  3497. memcpy(&pubkey, &pubkey2, sizeof(pubkey));
  3498. /* Tweak computation wraps and results in a key of 1. */
  3499. ctmp2[31] = 2;
  3500. CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 1);
  3501. CHECK(memcmp(ctmp2, zeros, 31) == 0 && ctmp2[31] == 1);
  3502. ctmp2[31] = 2;
  3503. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
  3504. ctmp2[31] = 1;
  3505. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, ctmp2) == 1);
  3506. CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
  3507. /* Tweak mul * 2 = 1+1. */
  3508. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
  3509. ctmp2[31] = 2;
  3510. CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey2, ctmp2) == 1);
  3511. CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
  3512. /* Test argument errors. */
  3513. ecount = 0;
  3514. secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
  3515. CHECK(ecount == 0);
  3516. /* Zeroize pubkey on parse error. */
  3517. memset(&pubkey, 0, 32);
  3518. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 0);
  3519. CHECK(ecount == 1);
  3520. CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
  3521. memcpy(&pubkey, &pubkey2, sizeof(pubkey));
  3522. memset(&pubkey2, 0, 32);
  3523. CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey2, ctmp2) == 0);
  3524. CHECK(ecount == 2);
  3525. CHECK(memcmp(&pubkey2, zeros, sizeof(pubkey2)) == 0);
  3526. /* Plain argument errors. */
  3527. ecount = 0;
  3528. CHECK(secp256k1_ec_seckey_verify(ctx, ctmp) == 1);
  3529. CHECK(ecount == 0);
  3530. CHECK(secp256k1_ec_seckey_verify(ctx, NULL) == 0);
  3531. CHECK(ecount == 1);
  3532. ecount = 0;
  3533. memset(ctmp2, 0, 32);
  3534. ctmp2[31] = 4;
  3535. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, NULL, ctmp2) == 0);
  3536. CHECK(ecount == 1);
  3537. CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, NULL) == 0);
  3538. CHECK(ecount == 2);
  3539. ecount = 0;
  3540. memset(ctmp2, 0, 32);
  3541. ctmp2[31] = 4;
  3542. CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, NULL, ctmp2) == 0);
  3543. CHECK(ecount == 1);
  3544. CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, NULL) == 0);
  3545. CHECK(ecount == 2);
  3546. ecount = 0;
  3547. memset(ctmp2, 0, 32);
  3548. CHECK(secp256k1_ec_privkey_tweak_add(ctx, NULL, ctmp2) == 0);
  3549. CHECK(ecount == 1);
  3550. CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, NULL) == 0);
  3551. CHECK(ecount == 2);
  3552. ecount = 0;
  3553. memset(ctmp2, 0, 32);
  3554. ctmp2[31] = 1;
  3555. CHECK(secp256k1_ec_privkey_tweak_mul(ctx, NULL, ctmp2) == 0);
  3556. CHECK(ecount == 1);
  3557. CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, NULL) == 0);
  3558. CHECK(ecount == 2);
  3559. ecount = 0;
  3560. CHECK(secp256k1_ec_pubkey_create(ctx, NULL, ctmp) == 0);
  3561. CHECK(ecount == 1);
  3562. memset(&pubkey, 1, sizeof(pubkey));
  3563. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, NULL) == 0);
  3564. CHECK(ecount == 2);
  3565. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3566. /* secp256k1_ec_pubkey_combine tests. */
  3567. ecount = 0;
  3568. pubkeys[0] = &pubkey_one;
  3569. VG_UNDEF(&pubkeys[0], sizeof(secp256k1_pubkey *));
  3570. VG_UNDEF(&pubkeys[1], sizeof(secp256k1_pubkey *));
  3571. VG_UNDEF(&pubkeys[2], sizeof(secp256k1_pubkey *));
  3572. memset(&pubkey, 255, sizeof(secp256k1_pubkey));
  3573. VG_UNDEF(&pubkey, sizeof(secp256k1_pubkey));
  3574. CHECK(secp256k1_ec_pubkey_combine(ctx, &pubkey, pubkeys, 0) == 0);
  3575. VG_CHECK(&pubkey, sizeof(secp256k1_pubkey));
  3576. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3577. CHECK(ecount == 1);
  3578. CHECK(secp256k1_ec_pubkey_combine(ctx, NULL, pubkeys, 1) == 0);
  3579. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3580. CHECK(ecount == 2);
  3581. memset(&pubkey, 255, sizeof(secp256k1_pubkey));
  3582. VG_UNDEF(&pubkey, sizeof(secp256k1_pubkey));
  3583. CHECK(secp256k1_ec_pubkey_combine(ctx, &pubkey, NULL, 1) == 0);
  3584. VG_CHECK(&pubkey, sizeof(secp256k1_pubkey));
  3585. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3586. CHECK(ecount == 3);
  3587. pubkeys[0] = &pubkey_negone;
  3588. memset(&pubkey, 255, sizeof(secp256k1_pubkey));
  3589. VG_UNDEF(&pubkey, sizeof(secp256k1_pubkey));
  3590. CHECK(secp256k1_ec_pubkey_combine(ctx, &pubkey, pubkeys, 1) == 1);
  3591. VG_CHECK(&pubkey, sizeof(secp256k1_pubkey));
  3592. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
  3593. CHECK(ecount == 3);
  3594. len = 33;
  3595. CHECK(secp256k1_ec_pubkey_serialize(ctx, ctmp, &len, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
  3596. CHECK(secp256k1_ec_pubkey_serialize(ctx, ctmp2, &len, &pubkey_negone, SECP256K1_EC_COMPRESSED) == 1);
  3597. CHECK(memcmp(ctmp, ctmp2, 33) == 0);
  3598. /* Result is infinity. */
  3599. pubkeys[0] = &pubkey_one;
  3600. pubkeys[1] = &pubkey_negone;
  3601. memset(&pubkey, 255, sizeof(secp256k1_pubkey));
  3602. VG_UNDEF(&pubkey, sizeof(secp256k1_pubkey));
  3603. CHECK(secp256k1_ec_pubkey_combine(ctx, &pubkey, pubkeys, 2) == 0);
  3604. VG_CHECK(&pubkey, sizeof(secp256k1_pubkey));
  3605. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) == 0);
  3606. CHECK(ecount == 3);
  3607. /* Passes through infinity but comes out one. */
  3608. pubkeys[2] = &pubkey_one;
  3609. memset(&pubkey, 255, sizeof(secp256k1_pubkey));
  3610. VG_UNDEF(&pubkey, sizeof(secp256k1_pubkey));
  3611. CHECK(secp256k1_ec_pubkey_combine(ctx, &pubkey, pubkeys, 3) == 1);
  3612. VG_CHECK(&pubkey, sizeof(secp256k1_pubkey));
  3613. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
  3614. CHECK(ecount == 3);
  3615. len = 33;
  3616. CHECK(secp256k1_ec_pubkey_serialize(ctx, ctmp, &len, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
  3617. CHECK(secp256k1_ec_pubkey_serialize(ctx, ctmp2, &len, &pubkey_one, SECP256K1_EC_COMPRESSED) == 1);
  3618. CHECK(memcmp(ctmp, ctmp2, 33) == 0);
  3619. /* Adds to two. */
  3620. pubkeys[1] = &pubkey_one;
  3621. memset(&pubkey, 255, sizeof(secp256k1_pubkey));
  3622. VG_UNDEF(&pubkey, sizeof(secp256k1_pubkey));
  3623. CHECK(secp256k1_ec_pubkey_combine(ctx, &pubkey, pubkeys, 2) == 1);
  3624. VG_CHECK(&pubkey, sizeof(secp256k1_pubkey));
  3625. CHECK(memcmp(&pubkey, zeros, sizeof(secp256k1_pubkey)) > 0);
  3626. CHECK(ecount == 3);
  3627. secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
  3628. }
  3629. void random_sign(secp256k1_scalar *sigr, secp256k1_scalar *sigs, const secp256k1_scalar *key, const secp256k1_scalar *msg, int *recid) {
  3630. secp256k1_scalar nonce;
  3631. do {
  3632. random_scalar_order_test(&nonce);
  3633. } while(!secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, sigr, sigs, key, msg, &nonce, recid));
  3634. }
  3635. void test_ecdsa_sign_verify(void) {
  3636. secp256k1_gej pubj;
  3637. secp256k1_ge pub;
  3638. secp256k1_scalar one;
  3639. secp256k1_scalar msg, key;
  3640. secp256k1_scalar sigr, sigs;
  3641. int recid;
  3642. int getrec;
  3643. random_scalar_order_test(&msg);
  3644. random_scalar_order_test(&key);
  3645. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubj, &key);
  3646. secp256k1_ge_set_gej(&pub, &pubj);
  3647. getrec = secp256k1_rand_bits(1);
  3648. random_sign(&sigr, &sigs, &key, &msg, getrec?&recid:NULL);
  3649. if (getrec) {
  3650. CHECK(recid >= 0 && recid < 4);
  3651. }
  3652. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sigr, &sigs, &pub, &msg));
  3653. secp256k1_scalar_set_int(&one, 1);
  3654. secp256k1_scalar_add(&msg, &msg, &one);
  3655. CHECK(!secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sigr, &sigs, &pub, &msg));
  3656. }
  3657. void run_ecdsa_sign_verify(void) {
  3658. int i;
  3659. for (i = 0; i < 10*count; i++) {
  3660. test_ecdsa_sign_verify();
  3661. }
  3662. }
  3663. /** Dummy nonce generation function that just uses a precomputed nonce, and fails if it is not accepted. Use only for testing. */
  3664. static int precomputed_nonce_function(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
  3665. (void)msg32;
  3666. (void)key32;
  3667. (void)algo16;
  3668. memcpy(nonce32, data, 32);
  3669. return (counter == 0);
  3670. }
  3671. static int nonce_function_test_fail(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
  3672. /* Dummy nonce generator that has a fatal error on the first counter value. */
  3673. if (counter == 0) {
  3674. return 0;
  3675. }
  3676. return nonce_function_rfc6979(nonce32, msg32, key32, algo16, data, counter - 1);
  3677. }
  3678. static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
  3679. /* Dummy nonce generator that produces unacceptable nonces for the first several counter values. */
  3680. if (counter < 3) {
  3681. memset(nonce32, counter==0 ? 0 : 255, 32);
  3682. if (counter == 2) {
  3683. nonce32[31]--;
  3684. }
  3685. return 1;
  3686. }
  3687. if (counter < 5) {
  3688. static const unsigned char order[] = {
  3689. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  3690. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
  3691. 0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
  3692. 0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
  3693. };
  3694. memcpy(nonce32, order, 32);
  3695. if (counter == 4) {
  3696. nonce32[31]++;
  3697. }
  3698. return 1;
  3699. }
  3700. /* Retry rate of 6979 is negligible esp. as we only call this in deterministic tests. */
  3701. /* If someone does fine a case where it retries for secp256k1, we'd like to know. */
  3702. if (counter > 5) {
  3703. return 0;
  3704. }
  3705. return nonce_function_rfc6979(nonce32, msg32, key32, algo16, data, counter - 5);
  3706. }
  3707. int is_empty_signature(const secp256k1_ecdsa_signature *sig) {
  3708. static const unsigned char res[sizeof(secp256k1_ecdsa_signature)] = {0};
  3709. return memcmp(sig, res, sizeof(secp256k1_ecdsa_signature)) == 0;
  3710. }
  3711. void test_ecdsa_end_to_end(void) {
  3712. unsigned char extra[32] = {0x00};
  3713. unsigned char privkey[32];
  3714. unsigned char message[32];
  3715. unsigned char privkey2[32];
  3716. secp256k1_ecdsa_signature signature[6];
  3717. secp256k1_scalar r, s;
  3718. unsigned char sig[74];
  3719. size_t siglen = 74;
  3720. unsigned char pubkeyc[65];
  3721. size_t pubkeyclen = 65;
  3722. secp256k1_pubkey pubkey;
  3723. secp256k1_pubkey pubkey_tmp;
  3724. unsigned char seckey[300];
  3725. size_t seckeylen = 300;
  3726. /* Generate a random key and message. */
  3727. {
  3728. secp256k1_scalar msg, key;
  3729. random_scalar_order_test(&msg);
  3730. random_scalar_order_test(&key);
  3731. secp256k1_scalar_get_b32(privkey, &key);
  3732. secp256k1_scalar_get_b32(message, &msg);
  3733. }
  3734. /* Construct and verify corresponding public key. */
  3735. CHECK(secp256k1_ec_seckey_verify(ctx, privkey) == 1);
  3736. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, privkey) == 1);
  3737. /* Verify exporting and importing public key. */
  3738. CHECK(secp256k1_ec_pubkey_serialize(ctx, pubkeyc, &pubkeyclen, &pubkey, secp256k1_rand_bits(1) == 1 ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED));
  3739. memset(&pubkey, 0, sizeof(pubkey));
  3740. CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, pubkeyclen) == 1);
  3741. /* Verify negation changes the key and changes it back */
  3742. memcpy(&pubkey_tmp, &pubkey, sizeof(pubkey));
  3743. CHECK(secp256k1_ec_pubkey_negate(ctx, &pubkey_tmp) == 1);
  3744. CHECK(memcmp(&pubkey_tmp, &pubkey, sizeof(pubkey)) != 0);
  3745. CHECK(secp256k1_ec_pubkey_negate(ctx, &pubkey_tmp) == 1);
  3746. CHECK(memcmp(&pubkey_tmp, &pubkey, sizeof(pubkey)) == 0);
  3747. /* Verify private key import and export. */
  3748. CHECK(ec_privkey_export_der(ctx, seckey, &seckeylen, privkey, secp256k1_rand_bits(1) == 1));
  3749. CHECK(ec_privkey_import_der(ctx, privkey2, seckey, seckeylen) == 1);
  3750. CHECK(memcmp(privkey, privkey2, 32) == 0);
  3751. /* Optionally tweak the keys using addition. */
  3752. if (secp256k1_rand_int(3) == 0) {
  3753. int ret1;
  3754. int ret2;
  3755. unsigned char rnd[32];
  3756. secp256k1_pubkey pubkey2;
  3757. secp256k1_rand256_test(rnd);
  3758. ret1 = secp256k1_ec_privkey_tweak_add(ctx, privkey, rnd);
  3759. ret2 = secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, rnd);
  3760. CHECK(ret1 == ret2);
  3761. if (ret1 == 0) {
  3762. return;
  3763. }
  3764. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
  3765. CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
  3766. }
  3767. /* Optionally tweak the keys using multiplication. */
  3768. if (secp256k1_rand_int(3) == 0) {
  3769. int ret1;
  3770. int ret2;
  3771. unsigned char rnd[32];
  3772. secp256k1_pubkey pubkey2;
  3773. secp256k1_rand256_test(rnd);
  3774. ret1 = secp256k1_ec_privkey_tweak_mul(ctx, privkey, rnd);
  3775. ret2 = secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, rnd);
  3776. CHECK(ret1 == ret2);
  3777. if (ret1 == 0) {
  3778. return;
  3779. }
  3780. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
  3781. CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
  3782. }
  3783. /* Sign. */
  3784. CHECK(secp256k1_ecdsa_sign(ctx, &signature[0], message, privkey, NULL, NULL) == 1);
  3785. CHECK(secp256k1_ecdsa_sign(ctx, &signature[4], message, privkey, NULL, NULL) == 1);
  3786. CHECK(secp256k1_ecdsa_sign(ctx, &signature[1], message, privkey, NULL, extra) == 1);
  3787. extra[31] = 1;
  3788. CHECK(secp256k1_ecdsa_sign(ctx, &signature[2], message, privkey, NULL, extra) == 1);
  3789. extra[31] = 0;
  3790. extra[0] = 1;
  3791. CHECK(secp256k1_ecdsa_sign(ctx, &signature[3], message, privkey, NULL, extra) == 1);
  3792. CHECK(memcmp(&signature[0], &signature[4], sizeof(signature[0])) == 0);
  3793. CHECK(memcmp(&signature[0], &signature[1], sizeof(signature[0])) != 0);
  3794. CHECK(memcmp(&signature[0], &signature[2], sizeof(signature[0])) != 0);
  3795. CHECK(memcmp(&signature[0], &signature[3], sizeof(signature[0])) != 0);
  3796. CHECK(memcmp(&signature[1], &signature[2], sizeof(signature[0])) != 0);
  3797. CHECK(memcmp(&signature[1], &signature[3], sizeof(signature[0])) != 0);
  3798. CHECK(memcmp(&signature[2], &signature[3], sizeof(signature[0])) != 0);
  3799. /* Verify. */
  3800. CHECK(secp256k1_ecdsa_verify(ctx, &signature[0], message, &pubkey) == 1);
  3801. CHECK(secp256k1_ecdsa_verify(ctx, &signature[1], message, &pubkey) == 1);
  3802. CHECK(secp256k1_ecdsa_verify(ctx, &signature[2], message, &pubkey) == 1);
  3803. CHECK(secp256k1_ecdsa_verify(ctx, &signature[3], message, &pubkey) == 1);
  3804. /* Test lower-S form, malleate, verify and fail, test again, malleate again */
  3805. CHECK(!secp256k1_ecdsa_signature_normalize(ctx, NULL, &signature[0]));
  3806. secp256k1_ecdsa_signature_load(ctx, &r, &s, &signature[0]);
  3807. secp256k1_scalar_negate(&s, &s);
  3808. secp256k1_ecdsa_signature_save(&signature[5], &r, &s);
  3809. CHECK(secp256k1_ecdsa_verify(ctx, &signature[5], message, &pubkey) == 0);
  3810. CHECK(secp256k1_ecdsa_signature_normalize(ctx, NULL, &signature[5]));
  3811. CHECK(secp256k1_ecdsa_signature_normalize(ctx, &signature[5], &signature[5]));
  3812. CHECK(!secp256k1_ecdsa_signature_normalize(ctx, NULL, &signature[5]));
  3813. CHECK(!secp256k1_ecdsa_signature_normalize(ctx, &signature[5], &signature[5]));
  3814. CHECK(secp256k1_ecdsa_verify(ctx, &signature[5], message, &pubkey) == 1);
  3815. secp256k1_scalar_negate(&s, &s);
  3816. secp256k1_ecdsa_signature_save(&signature[5], &r, &s);
  3817. CHECK(!secp256k1_ecdsa_signature_normalize(ctx, NULL, &signature[5]));
  3818. CHECK(secp256k1_ecdsa_verify(ctx, &signature[5], message, &pubkey) == 1);
  3819. CHECK(memcmp(&signature[5], &signature[0], 64) == 0);
  3820. /* Serialize/parse DER and verify again */
  3821. CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, sig, &siglen, &signature[0]) == 1);
  3822. memset(&signature[0], 0, sizeof(signature[0]));
  3823. CHECK(secp256k1_ecdsa_signature_parse_der(ctx, &signature[0], sig, siglen) == 1);
  3824. CHECK(secp256k1_ecdsa_verify(ctx, &signature[0], message, &pubkey) == 1);
  3825. /* Serialize/destroy/parse DER and verify again. */
  3826. siglen = 74;
  3827. CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, sig, &siglen, &signature[0]) == 1);
  3828. sig[secp256k1_rand_int(siglen)] += 1 + secp256k1_rand_int(255);
  3829. CHECK(secp256k1_ecdsa_signature_parse_der(ctx, &signature[0], sig, siglen) == 0 ||
  3830. secp256k1_ecdsa_verify(ctx, &signature[0], message, &pubkey) == 0);
  3831. }
  3832. void test_random_pubkeys(void) {
  3833. secp256k1_ge elem;
  3834. secp256k1_ge elem2;
  3835. unsigned char in[65];
  3836. /* Generate some randomly sized pubkeys. */
  3837. size_t len = secp256k1_rand_bits(2) == 0 ? 65 : 33;
  3838. if (secp256k1_rand_bits(2) == 0) {
  3839. len = secp256k1_rand_bits(6);
  3840. }
  3841. if (len == 65) {
  3842. in[0] = secp256k1_rand_bits(1) ? 4 : (secp256k1_rand_bits(1) ? 6 : 7);
  3843. } else {
  3844. in[0] = secp256k1_rand_bits(1) ? 2 : 3;
  3845. }
  3846. if (secp256k1_rand_bits(3) == 0) {
  3847. in[0] = secp256k1_rand_bits(8);
  3848. }
  3849. if (len > 1) {
  3850. secp256k1_rand256(&in[1]);
  3851. }
  3852. if (len > 33) {
  3853. secp256k1_rand256(&in[33]);
  3854. }
  3855. if (secp256k1_eckey_pubkey_parse(&elem, in, len)) {
  3856. unsigned char out[65];
  3857. unsigned char firstb;
  3858. int res;
  3859. size_t size = len;
  3860. firstb = in[0];
  3861. /* If the pubkey can be parsed, it should round-trip... */
  3862. CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, len == 33));
  3863. CHECK(size == len);
  3864. CHECK(memcmp(&in[1], &out[1], len-1) == 0);
  3865. /* ... except for the type of hybrid inputs. */
  3866. if ((in[0] != 6) && (in[0] != 7)) {
  3867. CHECK(in[0] == out[0]);
  3868. }
  3869. size = 65;
  3870. CHECK(secp256k1_eckey_pubkey_serialize(&elem, in, &size, 0));
  3871. CHECK(size == 65);
  3872. CHECK(secp256k1_eckey_pubkey_parse(&elem2, in, size));
  3873. ge_equals_ge(&elem,&elem2);
  3874. /* Check that the X9.62 hybrid type is checked. */
  3875. in[0] = secp256k1_rand_bits(1) ? 6 : 7;
  3876. res = secp256k1_eckey_pubkey_parse(&elem2, in, size);
  3877. if (firstb == 2 || firstb == 3) {
  3878. if (in[0] == firstb + 4) {
  3879. CHECK(res);
  3880. } else {
  3881. CHECK(!res);
  3882. }
  3883. }
  3884. if (res) {
  3885. ge_equals_ge(&elem,&elem2);
  3886. CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, 0));
  3887. CHECK(memcmp(&in[1], &out[1], 64) == 0);
  3888. }
  3889. }
  3890. }
  3891. void run_random_pubkeys(void) {
  3892. int i;
  3893. for (i = 0; i < 10*count; i++) {
  3894. test_random_pubkeys();
  3895. }
  3896. }
  3897. void run_ecdsa_end_to_end(void) {
  3898. int i;
  3899. for (i = 0; i < 64*count; i++) {
  3900. test_ecdsa_end_to_end();
  3901. }
  3902. }
  3903. int test_ecdsa_der_parse(const unsigned char *sig, size_t siglen, int certainly_der, int certainly_not_der) {
  3904. static const unsigned char zeroes[32] = {0};
  3905. #ifdef ENABLE_OPENSSL_TESTS
  3906. static const unsigned char max_scalar[32] = {
  3907. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3908. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  3909. 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
  3910. 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x40
  3911. };
  3912. #endif
  3913. int ret = 0;
  3914. secp256k1_ecdsa_signature sig_der;
  3915. unsigned char roundtrip_der[2048];
  3916. unsigned char compact_der[64];
  3917. size_t len_der = 2048;
  3918. int parsed_der = 0, valid_der = 0, roundtrips_der = 0;
  3919. secp256k1_ecdsa_signature sig_der_lax;
  3920. unsigned char roundtrip_der_lax[2048];
  3921. unsigned char compact_der_lax[64];
  3922. size_t len_der_lax = 2048;
  3923. int parsed_der_lax = 0, valid_der_lax = 0, roundtrips_der_lax = 0;
  3924. #ifdef ENABLE_OPENSSL_TESTS
  3925. ECDSA_SIG *sig_openssl;
  3926. const BIGNUM *r = NULL, *s = NULL;
  3927. const unsigned char *sigptr;
  3928. unsigned char roundtrip_openssl[2048];
  3929. int len_openssl = 2048;
  3930. int parsed_openssl, valid_openssl = 0, roundtrips_openssl = 0;
  3931. #endif
  3932. parsed_der = secp256k1_ecdsa_signature_parse_der(ctx, &sig_der, sig, siglen);
  3933. if (parsed_der) {
  3934. ret |= (!secp256k1_ecdsa_signature_serialize_compact(ctx, compact_der, &sig_der)) << 0;
  3935. valid_der = (memcmp(compact_der, zeroes, 32) != 0) && (memcmp(compact_der + 32, zeroes, 32) != 0);
  3936. }
  3937. if (valid_der) {
  3938. ret |= (!secp256k1_ecdsa_signature_serialize_der(ctx, roundtrip_der, &len_der, &sig_der)) << 1;
  3939. roundtrips_der = (len_der == siglen) && memcmp(roundtrip_der, sig, siglen) == 0;
  3940. }
  3941. parsed_der_lax = ecdsa_signature_parse_der_lax(ctx, &sig_der_lax, sig, siglen);
  3942. if (parsed_der_lax) {
  3943. ret |= (!secp256k1_ecdsa_signature_serialize_compact(ctx, compact_der_lax, &sig_der_lax)) << 10;
  3944. valid_der_lax = (memcmp(compact_der_lax, zeroes, 32) != 0) && (memcmp(compact_der_lax + 32, zeroes, 32) != 0);
  3945. }
  3946. if (valid_der_lax) {
  3947. ret |= (!secp256k1_ecdsa_signature_serialize_der(ctx, roundtrip_der_lax, &len_der_lax, &sig_der_lax)) << 11;
  3948. roundtrips_der_lax = (len_der_lax == siglen) && memcmp(roundtrip_der_lax, sig, siglen) == 0;
  3949. }
  3950. if (certainly_der) {
  3951. ret |= (!parsed_der) << 2;
  3952. }
  3953. if (certainly_not_der) {
  3954. ret |= (parsed_der) << 17;
  3955. }
  3956. if (valid_der) {
  3957. ret |= (!roundtrips_der) << 3;
  3958. }
  3959. if (valid_der) {
  3960. ret |= (!roundtrips_der_lax) << 12;
  3961. ret |= (len_der != len_der_lax) << 13;
  3962. ret |= (memcmp(roundtrip_der_lax, roundtrip_der, len_der) != 0) << 14;
  3963. }
  3964. ret |= (roundtrips_der != roundtrips_der_lax) << 15;
  3965. if (parsed_der) {
  3966. ret |= (!parsed_der_lax) << 16;
  3967. }
  3968. #ifdef ENABLE_OPENSSL_TESTS
  3969. sig_openssl = ECDSA_SIG_new();
  3970. sigptr = sig;
  3971. parsed_openssl = (d2i_ECDSA_SIG(&sig_openssl, &sigptr, siglen) != NULL);
  3972. if (parsed_openssl) {
  3973. ECDSA_SIG_get0(sig_openssl, &r, &s);
  3974. valid_openssl = !BN_is_negative(r) && !BN_is_negative(s) && BN_num_bits(r) > 0 && BN_num_bits(r) <= 256 && BN_num_bits(s) > 0 && BN_num_bits(s) <= 256;
  3975. if (valid_openssl) {
  3976. unsigned char tmp[32] = {0};
  3977. BN_bn2bin(r, tmp + 32 - BN_num_bytes(r));
  3978. valid_openssl = memcmp(tmp, max_scalar, 32) < 0;
  3979. }
  3980. if (valid_openssl) {
  3981. unsigned char tmp[32] = {0};
  3982. BN_bn2bin(s, tmp + 32 - BN_num_bytes(s));
  3983. valid_openssl = memcmp(tmp, max_scalar, 32) < 0;
  3984. }
  3985. }
  3986. len_openssl = i2d_ECDSA_SIG(sig_openssl, NULL);
  3987. if (len_openssl <= 2048) {
  3988. unsigned char *ptr = roundtrip_openssl;
  3989. CHECK(i2d_ECDSA_SIG(sig_openssl, &ptr) == len_openssl);
  3990. roundtrips_openssl = valid_openssl && ((size_t)len_openssl == siglen) && (memcmp(roundtrip_openssl, sig, siglen) == 0);
  3991. } else {
  3992. len_openssl = 0;
  3993. }
  3994. ECDSA_SIG_free(sig_openssl);
  3995. ret |= (parsed_der && !parsed_openssl) << 4;
  3996. ret |= (valid_der && !valid_openssl) << 5;
  3997. ret |= (roundtrips_openssl && !parsed_der) << 6;
  3998. ret |= (roundtrips_der != roundtrips_openssl) << 7;
  3999. if (roundtrips_openssl) {
  4000. ret |= (len_der != (size_t)len_openssl) << 8;
  4001. ret |= (memcmp(roundtrip_der, roundtrip_openssl, len_der) != 0) << 9;
  4002. }
  4003. #endif
  4004. return ret;
  4005. }
  4006. static void assign_big_endian(unsigned char *ptr, size_t ptrlen, uint32_t val) {
  4007. size_t i;
  4008. for (i = 0; i < ptrlen; i++) {
  4009. int shift = ptrlen - 1 - i;
  4010. if (shift >= 4) {
  4011. ptr[i] = 0;
  4012. } else {
  4013. ptr[i] = (val >> shift) & 0xFF;
  4014. }
  4015. }
  4016. }
  4017. static void damage_array(unsigned char *sig, size_t *len) {
  4018. int pos;
  4019. int action = secp256k1_rand_bits(3);
  4020. if (action < 1 && *len > 3) {
  4021. /* Delete a byte. */
  4022. pos = secp256k1_rand_int(*len);
  4023. memmove(sig + pos, sig + pos + 1, *len - pos - 1);
  4024. (*len)--;
  4025. return;
  4026. } else if (action < 2 && *len < 2048) {
  4027. /* Insert a byte. */
  4028. pos = secp256k1_rand_int(1 + *len);
  4029. memmove(sig + pos + 1, sig + pos, *len - pos);
  4030. sig[pos] = secp256k1_rand_bits(8);
  4031. (*len)++;
  4032. return;
  4033. } else if (action < 4) {
  4034. /* Modify a byte. */
  4035. sig[secp256k1_rand_int(*len)] += 1 + secp256k1_rand_int(255);
  4036. return;
  4037. } else { /* action < 8 */
  4038. /* Modify a bit. */
  4039. sig[secp256k1_rand_int(*len)] ^= 1 << secp256k1_rand_bits(3);
  4040. return;
  4041. }
  4042. }
  4043. static void random_ber_signature(unsigned char *sig, size_t *len, int* certainly_der, int* certainly_not_der) {
  4044. int der;
  4045. int nlow[2], nlen[2], nlenlen[2], nhbit[2], nhbyte[2], nzlen[2];
  4046. size_t tlen, elen, glen;
  4047. int indet;
  4048. int n;
  4049. *len = 0;
  4050. der = secp256k1_rand_bits(2) == 0;
  4051. *certainly_der = der;
  4052. *certainly_not_der = 0;
  4053. indet = der ? 0 : secp256k1_rand_int(10) == 0;
  4054. for (n = 0; n < 2; n++) {
  4055. /* We generate two classes of numbers: nlow==1 "low" ones (up to 32 bytes), nlow==0 "high" ones (32 bytes with 129 top bits set, or larger than 32 bytes) */
  4056. nlow[n] = der ? 1 : (secp256k1_rand_bits(3) != 0);
  4057. /* The length of the number in bytes (the first byte of which will always be nonzero) */
  4058. nlen[n] = nlow[n] ? secp256k1_rand_int(33) : 32 + secp256k1_rand_int(200) * secp256k1_rand_int(8) / 8;
  4059. CHECK(nlen[n] <= 232);
  4060. /* The top bit of the number. */
  4061. nhbit[n] = (nlow[n] == 0 && nlen[n] == 32) ? 1 : (nlen[n] == 0 ? 0 : secp256k1_rand_bits(1));
  4062. /* The top byte of the number (after the potential hardcoded 16 0xFF characters for "high" 32 bytes numbers) */
  4063. nhbyte[n] = nlen[n] == 0 ? 0 : (nhbit[n] ? 128 + secp256k1_rand_bits(7) : 1 + secp256k1_rand_int(127));
  4064. /* The number of zero bytes in front of the number (which is 0 or 1 in case of DER, otherwise we extend up to 300 bytes) */
  4065. nzlen[n] = der ? ((nlen[n] == 0 || nhbit[n]) ? 1 : 0) : (nlow[n] ? secp256k1_rand_int(3) : secp256k1_rand_int(300 - nlen[n]) * secp256k1_rand_int(8) / 8);
  4066. if (nzlen[n] > ((nlen[n] == 0 || nhbit[n]) ? 1 : 0)) {
  4067. *certainly_not_der = 1;
  4068. }
  4069. CHECK(nlen[n] + nzlen[n] <= 300);
  4070. /* The length of the length descriptor for the number. 0 means short encoding, anything else is long encoding. */
  4071. nlenlen[n] = nlen[n] + nzlen[n] < 128 ? 0 : (nlen[n] + nzlen[n] < 256 ? 1 : 2);
  4072. if (!der) {
  4073. /* nlenlen[n] max 127 bytes */
  4074. int add = secp256k1_rand_int(127 - nlenlen[n]) * secp256k1_rand_int(16) * secp256k1_rand_int(16) / 256;
  4075. nlenlen[n] += add;
  4076. if (add != 0) {
  4077. *certainly_not_der = 1;
  4078. }
  4079. }
  4080. CHECK(nlen[n] + nzlen[n] + nlenlen[n] <= 427);
  4081. }
  4082. /* The total length of the data to go, so far */
  4083. tlen = 2 + nlenlen[0] + nlen[0] + nzlen[0] + 2 + nlenlen[1] + nlen[1] + nzlen[1];
  4084. CHECK(tlen <= 856);
  4085. /* The length of the garbage inside the tuple. */
  4086. elen = (der || indet) ? 0 : secp256k1_rand_int(980 - tlen) * secp256k1_rand_int(8) / 8;
  4087. if (elen != 0) {
  4088. *certainly_not_der = 1;
  4089. }
  4090. tlen += elen;
  4091. CHECK(tlen <= 980);
  4092. /* The length of the garbage after the end of the tuple. */
  4093. glen = der ? 0 : secp256k1_rand_int(990 - tlen) * secp256k1_rand_int(8) / 8;
  4094. if (glen != 0) {
  4095. *certainly_not_der = 1;
  4096. }
  4097. CHECK(tlen + glen <= 990);
  4098. /* Write the tuple header. */
  4099. sig[(*len)++] = 0x30;
  4100. if (indet) {
  4101. /* Indeterminate length */
  4102. sig[(*len)++] = 0x80;
  4103. *certainly_not_der = 1;
  4104. } else {
  4105. int tlenlen = tlen < 128 ? 0 : (tlen < 256 ? 1 : 2);
  4106. if (!der) {
  4107. int add = secp256k1_rand_int(127 - tlenlen) * secp256k1_rand_int(16) * secp256k1_rand_int(16) / 256;
  4108. tlenlen += add;
  4109. if (add != 0) {
  4110. *certainly_not_der = 1;
  4111. }
  4112. }
  4113. if (tlenlen == 0) {
  4114. /* Short length notation */
  4115. sig[(*len)++] = tlen;
  4116. } else {
  4117. /* Long length notation */
  4118. sig[(*len)++] = 128 + tlenlen;
  4119. assign_big_endian(sig + *len, tlenlen, tlen);
  4120. *len += tlenlen;
  4121. }
  4122. tlen += tlenlen;
  4123. }
  4124. tlen += 2;
  4125. CHECK(tlen + glen <= 1119);
  4126. for (n = 0; n < 2; n++) {
  4127. /* Write the integer header. */
  4128. sig[(*len)++] = 0x02;
  4129. if (nlenlen[n] == 0) {
  4130. /* Short length notation */
  4131. sig[(*len)++] = nlen[n] + nzlen[n];
  4132. } else {
  4133. /* Long length notation. */
  4134. sig[(*len)++] = 128 + nlenlen[n];
  4135. assign_big_endian(sig + *len, nlenlen[n], nlen[n] + nzlen[n]);
  4136. *len += nlenlen[n];
  4137. }
  4138. /* Write zero padding */
  4139. while (nzlen[n] > 0) {
  4140. sig[(*len)++] = 0x00;
  4141. nzlen[n]--;
  4142. }
  4143. if (nlen[n] == 32 && !nlow[n]) {
  4144. /* Special extra 16 0xFF bytes in "high" 32-byte numbers */
  4145. int i;
  4146. for (i = 0; i < 16; i++) {
  4147. sig[(*len)++] = 0xFF;
  4148. }
  4149. nlen[n] -= 16;
  4150. }
  4151. /* Write first byte of number */
  4152. if (nlen[n] > 0) {
  4153. sig[(*len)++] = nhbyte[n];
  4154. nlen[n]--;
  4155. }
  4156. /* Generate remaining random bytes of number */
  4157. secp256k1_rand_bytes_test(sig + *len, nlen[n]);
  4158. *len += nlen[n];
  4159. nlen[n] = 0;
  4160. }
  4161. /* Generate random garbage inside tuple. */
  4162. secp256k1_rand_bytes_test(sig + *len, elen);
  4163. *len += elen;
  4164. /* Generate end-of-contents bytes. */
  4165. if (indet) {
  4166. sig[(*len)++] = 0;
  4167. sig[(*len)++] = 0;
  4168. tlen += 2;
  4169. }
  4170. CHECK(tlen + glen <= 1121);
  4171. /* Generate random garbage outside tuple. */
  4172. secp256k1_rand_bytes_test(sig + *len, glen);
  4173. *len += glen;
  4174. tlen += glen;
  4175. CHECK(tlen <= 1121);
  4176. CHECK(tlen == *len);
  4177. }
  4178. void run_ecdsa_der_parse(void) {
  4179. int i,j;
  4180. for (i = 0; i < 200 * count; i++) {
  4181. unsigned char buffer[2048];
  4182. size_t buflen = 0;
  4183. int certainly_der = 0;
  4184. int certainly_not_der = 0;
  4185. random_ber_signature(buffer, &buflen, &certainly_der, &certainly_not_der);
  4186. CHECK(buflen <= 2048);
  4187. for (j = 0; j < 16; j++) {
  4188. int ret = 0;
  4189. if (j > 0) {
  4190. damage_array(buffer, &buflen);
  4191. /* We don't know anything anymore about the DERness of the result */
  4192. certainly_der = 0;
  4193. certainly_not_der = 0;
  4194. }
  4195. ret = test_ecdsa_der_parse(buffer, buflen, certainly_der, certainly_not_der);
  4196. if (ret != 0) {
  4197. size_t k;
  4198. fprintf(stderr, "Failure %x on ", ret);
  4199. for (k = 0; k < buflen; k++) {
  4200. fprintf(stderr, "%02x ", buffer[k]);
  4201. }
  4202. fprintf(stderr, "\n");
  4203. }
  4204. CHECK(ret == 0);
  4205. }
  4206. }
  4207. }
  4208. /* Tests several edge cases. */
  4209. void test_ecdsa_edge_cases(void) {
  4210. int t;
  4211. secp256k1_ecdsa_signature sig;
  4212. /* Test the case where ECDSA recomputes a point that is infinity. */
  4213. {
  4214. secp256k1_gej keyj;
  4215. secp256k1_ge key;
  4216. secp256k1_scalar msg;
  4217. secp256k1_scalar sr, ss;
  4218. secp256k1_scalar_set_int(&ss, 1);
  4219. secp256k1_scalar_negate(&ss, &ss);
  4220. secp256k1_scalar_inverse(&ss, &ss);
  4221. secp256k1_scalar_set_int(&sr, 1);
  4222. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &keyj, &sr);
  4223. secp256k1_ge_set_gej(&key, &keyj);
  4224. msg = ss;
  4225. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 0);
  4226. }
  4227. /* Verify signature with r of zero fails. */
  4228. {
  4229. const unsigned char pubkey_mods_zero[33] = {
  4230. 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  4231. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  4232. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  4233. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  4234. 0x41
  4235. };
  4236. secp256k1_ge key;
  4237. secp256k1_scalar msg;
  4238. secp256k1_scalar sr, ss;
  4239. secp256k1_scalar_set_int(&ss, 1);
  4240. secp256k1_scalar_set_int(&msg, 0);
  4241. secp256k1_scalar_set_int(&sr, 0);
  4242. CHECK(secp256k1_eckey_pubkey_parse(&key, pubkey_mods_zero, 33));
  4243. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 0);
  4244. }
  4245. /* Verify signature with s of zero fails. */
  4246. {
  4247. const unsigned char pubkey[33] = {
  4248. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4249. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4250. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4251. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4252. 0x01
  4253. };
  4254. secp256k1_ge key;
  4255. secp256k1_scalar msg;
  4256. secp256k1_scalar sr, ss;
  4257. secp256k1_scalar_set_int(&ss, 0);
  4258. secp256k1_scalar_set_int(&msg, 0);
  4259. secp256k1_scalar_set_int(&sr, 1);
  4260. CHECK(secp256k1_eckey_pubkey_parse(&key, pubkey, 33));
  4261. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 0);
  4262. }
  4263. /* Verify signature with message 0 passes. */
  4264. {
  4265. const unsigned char pubkey[33] = {
  4266. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4267. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4268. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4269. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4270. 0x02
  4271. };
  4272. const unsigned char pubkey2[33] = {
  4273. 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  4274. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  4275. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  4276. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  4277. 0x43
  4278. };
  4279. secp256k1_ge key;
  4280. secp256k1_ge key2;
  4281. secp256k1_scalar msg;
  4282. secp256k1_scalar sr, ss;
  4283. secp256k1_scalar_set_int(&ss, 2);
  4284. secp256k1_scalar_set_int(&msg, 0);
  4285. secp256k1_scalar_set_int(&sr, 2);
  4286. CHECK(secp256k1_eckey_pubkey_parse(&key, pubkey, 33));
  4287. CHECK(secp256k1_eckey_pubkey_parse(&key2, pubkey2, 33));
  4288. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 1);
  4289. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key2, &msg) == 1);
  4290. secp256k1_scalar_negate(&ss, &ss);
  4291. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 1);
  4292. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key2, &msg) == 1);
  4293. secp256k1_scalar_set_int(&ss, 1);
  4294. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 0);
  4295. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key2, &msg) == 0);
  4296. }
  4297. /* Verify signature with message 1 passes. */
  4298. {
  4299. const unsigned char pubkey[33] = {
  4300. 0x02, 0x14, 0x4e, 0x5a, 0x58, 0xef, 0x5b, 0x22,
  4301. 0x6f, 0xd2, 0xe2, 0x07, 0x6a, 0x77, 0xcf, 0x05,
  4302. 0xb4, 0x1d, 0xe7, 0x4a, 0x30, 0x98, 0x27, 0x8c,
  4303. 0x93, 0xe6, 0xe6, 0x3c, 0x0b, 0xc4, 0x73, 0x76,
  4304. 0x25
  4305. };
  4306. const unsigned char pubkey2[33] = {
  4307. 0x02, 0x8a, 0xd5, 0x37, 0xed, 0x73, 0xd9, 0x40,
  4308. 0x1d, 0xa0, 0x33, 0xd2, 0xdc, 0xf0, 0xaf, 0xae,
  4309. 0x34, 0xcf, 0x5f, 0x96, 0x4c, 0x73, 0x28, 0x0f,
  4310. 0x92, 0xc0, 0xf6, 0x9d, 0xd9, 0xb2, 0x09, 0x10,
  4311. 0x62
  4312. };
  4313. const unsigned char csr[32] = {
  4314. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4315. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  4316. 0x45, 0x51, 0x23, 0x19, 0x50, 0xb7, 0x5f, 0xc4,
  4317. 0x40, 0x2d, 0xa1, 0x72, 0x2f, 0xc9, 0xba, 0xeb
  4318. };
  4319. secp256k1_ge key;
  4320. secp256k1_ge key2;
  4321. secp256k1_scalar msg;
  4322. secp256k1_scalar sr, ss;
  4323. secp256k1_scalar_set_int(&ss, 1);
  4324. secp256k1_scalar_set_int(&msg, 1);
  4325. secp256k1_scalar_set_b32(&sr, csr, NULL);
  4326. CHECK(secp256k1_eckey_pubkey_parse(&key, pubkey, 33));
  4327. CHECK(secp256k1_eckey_pubkey_parse(&key2, pubkey2, 33));
  4328. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 1);
  4329. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key2, &msg) == 1);
  4330. secp256k1_scalar_negate(&ss, &ss);
  4331. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 1);
  4332. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key2, &msg) == 1);
  4333. secp256k1_scalar_set_int(&ss, 2);
  4334. secp256k1_scalar_inverse_var(&ss, &ss);
  4335. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 0);
  4336. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key2, &msg) == 0);
  4337. }
  4338. /* Verify signature with message -1 passes. */
  4339. {
  4340. const unsigned char pubkey[33] = {
  4341. 0x03, 0xaf, 0x97, 0xff, 0x7d, 0x3a, 0xf6, 0xa0,
  4342. 0x02, 0x94, 0xbd, 0x9f, 0x4b, 0x2e, 0xd7, 0x52,
  4343. 0x28, 0xdb, 0x49, 0x2a, 0x65, 0xcb, 0x1e, 0x27,
  4344. 0x57, 0x9c, 0xba, 0x74, 0x20, 0xd5, 0x1d, 0x20,
  4345. 0xf1
  4346. };
  4347. const unsigned char csr[32] = {
  4348. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4349. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  4350. 0x45, 0x51, 0x23, 0x19, 0x50, 0xb7, 0x5f, 0xc4,
  4351. 0x40, 0x2d, 0xa1, 0x72, 0x2f, 0xc9, 0xba, 0xee
  4352. };
  4353. secp256k1_ge key;
  4354. secp256k1_scalar msg;
  4355. secp256k1_scalar sr, ss;
  4356. secp256k1_scalar_set_int(&ss, 1);
  4357. secp256k1_scalar_set_int(&msg, 1);
  4358. secp256k1_scalar_negate(&msg, &msg);
  4359. secp256k1_scalar_set_b32(&sr, csr, NULL);
  4360. CHECK(secp256k1_eckey_pubkey_parse(&key, pubkey, 33));
  4361. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 1);
  4362. secp256k1_scalar_negate(&ss, &ss);
  4363. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 1);
  4364. secp256k1_scalar_set_int(&ss, 3);
  4365. secp256k1_scalar_inverse_var(&ss, &ss);
  4366. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sr, &ss, &key, &msg) == 0);
  4367. }
  4368. /* Signature where s would be zero. */
  4369. {
  4370. secp256k1_pubkey pubkey;
  4371. size_t siglen;
  4372. int32_t ecount;
  4373. unsigned char signature[72];
  4374. static const unsigned char nonce[32] = {
  4375. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4376. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4377. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4378. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  4379. };
  4380. static const unsigned char nonce2[32] = {
  4381. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  4382. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
  4383. 0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
  4384. 0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x40
  4385. };
  4386. const unsigned char key[32] = {
  4387. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4388. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4389. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4390. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  4391. };
  4392. unsigned char msg[32] = {
  4393. 0x86, 0x41, 0x99, 0x81, 0x06, 0x23, 0x44, 0x53,
  4394. 0xaa, 0x5f, 0x9d, 0x6a, 0x31, 0x78, 0xf4, 0xf7,
  4395. 0xb8, 0x12, 0xe0, 0x0b, 0x81, 0x7a, 0x77, 0x62,
  4396. 0x65, 0xdf, 0xdd, 0x31, 0xb9, 0x3e, 0x29, 0xa9,
  4397. };
  4398. ecount = 0;
  4399. secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
  4400. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, precomputed_nonce_function, nonce) == 0);
  4401. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, precomputed_nonce_function, nonce2) == 0);
  4402. msg[31] = 0xaa;
  4403. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, precomputed_nonce_function, nonce) == 1);
  4404. CHECK(ecount == 0);
  4405. CHECK(secp256k1_ecdsa_sign(ctx, NULL, msg, key, precomputed_nonce_function, nonce2) == 0);
  4406. CHECK(ecount == 1);
  4407. CHECK(secp256k1_ecdsa_sign(ctx, &sig, NULL, key, precomputed_nonce_function, nonce2) == 0);
  4408. CHECK(ecount == 2);
  4409. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, NULL, precomputed_nonce_function, nonce2) == 0);
  4410. CHECK(ecount == 3);
  4411. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, precomputed_nonce_function, nonce2) == 1);
  4412. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, key) == 1);
  4413. CHECK(secp256k1_ecdsa_verify(ctx, NULL, msg, &pubkey) == 0);
  4414. CHECK(ecount == 4);
  4415. CHECK(secp256k1_ecdsa_verify(ctx, &sig, NULL, &pubkey) == 0);
  4416. CHECK(ecount == 5);
  4417. CHECK(secp256k1_ecdsa_verify(ctx, &sig, msg, NULL) == 0);
  4418. CHECK(ecount == 6);
  4419. CHECK(secp256k1_ecdsa_verify(ctx, &sig, msg, &pubkey) == 1);
  4420. CHECK(ecount == 6);
  4421. CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, NULL) == 0);
  4422. CHECK(ecount == 7);
  4423. /* That pubkeyload fails via an ARGCHECK is a little odd but makes sense because pubkeys are an opaque data type. */
  4424. CHECK(secp256k1_ecdsa_verify(ctx, &sig, msg, &pubkey) == 0);
  4425. CHECK(ecount == 8);
  4426. siglen = 72;
  4427. CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, NULL, &siglen, &sig) == 0);
  4428. CHECK(ecount == 9);
  4429. CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, signature, NULL, &sig) == 0);
  4430. CHECK(ecount == 10);
  4431. CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, signature, &siglen, NULL) == 0);
  4432. CHECK(ecount == 11);
  4433. CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, signature, &siglen, &sig) == 1);
  4434. CHECK(ecount == 11);
  4435. CHECK(secp256k1_ecdsa_signature_parse_der(ctx, NULL, signature, siglen) == 0);
  4436. CHECK(ecount == 12);
  4437. CHECK(secp256k1_ecdsa_signature_parse_der(ctx, &sig, NULL, siglen) == 0);
  4438. CHECK(ecount == 13);
  4439. CHECK(secp256k1_ecdsa_signature_parse_der(ctx, &sig, signature, siglen) == 1);
  4440. CHECK(ecount == 13);
  4441. siglen = 10;
  4442. /* Too little room for a signature does not fail via ARGCHECK. */
  4443. CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, signature, &siglen, &sig) == 0);
  4444. CHECK(ecount == 13);
  4445. ecount = 0;
  4446. CHECK(secp256k1_ecdsa_signature_normalize(ctx, NULL, NULL) == 0);
  4447. CHECK(ecount == 1);
  4448. CHECK(secp256k1_ecdsa_signature_serialize_compact(ctx, NULL, &sig) == 0);
  4449. CHECK(ecount == 2);
  4450. CHECK(secp256k1_ecdsa_signature_serialize_compact(ctx, signature, NULL) == 0);
  4451. CHECK(ecount == 3);
  4452. CHECK(secp256k1_ecdsa_signature_serialize_compact(ctx, signature, &sig) == 1);
  4453. CHECK(ecount == 3);
  4454. CHECK(secp256k1_ecdsa_signature_parse_compact(ctx, NULL, signature) == 0);
  4455. CHECK(ecount == 4);
  4456. CHECK(secp256k1_ecdsa_signature_parse_compact(ctx, &sig, NULL) == 0);
  4457. CHECK(ecount == 5);
  4458. CHECK(secp256k1_ecdsa_signature_parse_compact(ctx, &sig, signature) == 1);
  4459. CHECK(ecount == 5);
  4460. memset(signature, 255, 64);
  4461. CHECK(secp256k1_ecdsa_signature_parse_compact(ctx, &sig, signature) == 0);
  4462. CHECK(ecount == 5);
  4463. secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
  4464. }
  4465. /* Nonce function corner cases. */
  4466. for (t = 0; t < 2; t++) {
  4467. static const unsigned char zero[32] = {0x00};
  4468. int i;
  4469. unsigned char key[32];
  4470. unsigned char msg[32];
  4471. secp256k1_ecdsa_signature sig2;
  4472. secp256k1_scalar sr[512], ss;
  4473. const unsigned char *extra;
  4474. extra = t == 0 ? NULL : zero;
  4475. memset(msg, 0, 32);
  4476. msg[31] = 1;
  4477. /* High key results in signature failure. */
  4478. memset(key, 0xFF, 32);
  4479. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, NULL, extra) == 0);
  4480. CHECK(is_empty_signature(&sig));
  4481. /* Zero key results in signature failure. */
  4482. memset(key, 0, 32);
  4483. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, NULL, extra) == 0);
  4484. CHECK(is_empty_signature(&sig));
  4485. /* Nonce function failure results in signature failure. */
  4486. key[31] = 1;
  4487. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, nonce_function_test_fail, extra) == 0);
  4488. CHECK(is_empty_signature(&sig));
  4489. /* The retry loop successfully makes its way to the first good value. */
  4490. CHECK(secp256k1_ecdsa_sign(ctx, &sig, msg, key, nonce_function_test_retry, extra) == 1);
  4491. CHECK(!is_empty_signature(&sig));
  4492. CHECK(secp256k1_ecdsa_sign(ctx, &sig2, msg, key, nonce_function_rfc6979, extra) == 1);
  4493. CHECK(!is_empty_signature(&sig2));
  4494. CHECK(memcmp(&sig, &sig2, sizeof(sig)) == 0);
  4495. /* The default nonce function is deterministic. */
  4496. CHECK(secp256k1_ecdsa_sign(ctx, &sig2, msg, key, NULL, extra) == 1);
  4497. CHECK(!is_empty_signature(&sig2));
  4498. CHECK(memcmp(&sig, &sig2, sizeof(sig)) == 0);
  4499. /* The default nonce function changes output with different messages. */
  4500. for(i = 0; i < 256; i++) {
  4501. int j;
  4502. msg[0] = i;
  4503. CHECK(secp256k1_ecdsa_sign(ctx, &sig2, msg, key, NULL, extra) == 1);
  4504. CHECK(!is_empty_signature(&sig2));
  4505. secp256k1_ecdsa_signature_load(ctx, &sr[i], &ss, &sig2);
  4506. for (j = 0; j < i; j++) {
  4507. CHECK(!secp256k1_scalar_eq(&sr[i], &sr[j]));
  4508. }
  4509. }
  4510. msg[0] = 0;
  4511. msg[31] = 2;
  4512. /* The default nonce function changes output with different keys. */
  4513. for(i = 256; i < 512; i++) {
  4514. int j;
  4515. key[0] = i - 256;
  4516. CHECK(secp256k1_ecdsa_sign(ctx, &sig2, msg, key, NULL, extra) == 1);
  4517. CHECK(!is_empty_signature(&sig2));
  4518. secp256k1_ecdsa_signature_load(ctx, &sr[i], &ss, &sig2);
  4519. for (j = 0; j < i; j++) {
  4520. CHECK(!secp256k1_scalar_eq(&sr[i], &sr[j]));
  4521. }
  4522. }
  4523. key[0] = 0;
  4524. }
  4525. {
  4526. /* Check that optional nonce arguments do not have equivalent effect. */
  4527. const unsigned char zeros[32] = {0};
  4528. unsigned char nonce[32];
  4529. unsigned char nonce2[32];
  4530. unsigned char nonce3[32];
  4531. unsigned char nonce4[32];
  4532. VG_UNDEF(nonce,32);
  4533. VG_UNDEF(nonce2,32);
  4534. VG_UNDEF(nonce3,32);
  4535. VG_UNDEF(nonce4,32);
  4536. CHECK(nonce_function_rfc6979(nonce, zeros, zeros, NULL, NULL, 0) == 1);
  4537. VG_CHECK(nonce,32);
  4538. CHECK(nonce_function_rfc6979(nonce2, zeros, zeros, zeros, NULL, 0) == 1);
  4539. VG_CHECK(nonce2,32);
  4540. CHECK(nonce_function_rfc6979(nonce3, zeros, zeros, NULL, (void *)zeros, 0) == 1);
  4541. VG_CHECK(nonce3,32);
  4542. CHECK(nonce_function_rfc6979(nonce4, zeros, zeros, zeros, (void *)zeros, 0) == 1);
  4543. VG_CHECK(nonce4,32);
  4544. CHECK(memcmp(nonce, nonce2, 32) != 0);
  4545. CHECK(memcmp(nonce, nonce3, 32) != 0);
  4546. CHECK(memcmp(nonce, nonce4, 32) != 0);
  4547. CHECK(memcmp(nonce2, nonce3, 32) != 0);
  4548. CHECK(memcmp(nonce2, nonce4, 32) != 0);
  4549. CHECK(memcmp(nonce3, nonce4, 32) != 0);
  4550. }
  4551. /* Privkey export where pubkey is the point at infinity. */
  4552. {
  4553. unsigned char privkey[300];
  4554. unsigned char seckey[32] = {
  4555. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  4556. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
  4557. 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
  4558. 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41,
  4559. };
  4560. size_t outlen = 300;
  4561. CHECK(!ec_privkey_export_der(ctx, privkey, &outlen, seckey, 0));
  4562. outlen = 300;
  4563. CHECK(!ec_privkey_export_der(ctx, privkey, &outlen, seckey, 1));
  4564. }
  4565. }
  4566. void run_ecdsa_edge_cases(void) {
  4567. test_ecdsa_edge_cases();
  4568. }
  4569. #ifdef ENABLE_OPENSSL_TESTS
  4570. EC_KEY *get_openssl_key(const unsigned char *key32) {
  4571. unsigned char privkey[300];
  4572. size_t privkeylen;
  4573. const unsigned char* pbegin = privkey;
  4574. int compr = secp256k1_rand_bits(1);
  4575. EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_secp256k1);
  4576. CHECK(ec_privkey_export_der(ctx, privkey, &privkeylen, key32, compr));
  4577. CHECK(d2i_ECPrivateKey(&ec_key, &pbegin, privkeylen));
  4578. CHECK(EC_KEY_check_key(ec_key));
  4579. return ec_key;
  4580. }
  4581. void test_ecdsa_openssl(void) {
  4582. secp256k1_gej qj;
  4583. secp256k1_ge q;
  4584. secp256k1_scalar sigr, sigs;
  4585. secp256k1_scalar one;
  4586. secp256k1_scalar msg2;
  4587. secp256k1_scalar key, msg;
  4588. EC_KEY *ec_key;
  4589. unsigned int sigsize = 80;
  4590. size_t secp_sigsize = 80;
  4591. unsigned char message[32];
  4592. unsigned char signature[80];
  4593. unsigned char key32[32];
  4594. secp256k1_rand256_test(message);
  4595. secp256k1_scalar_set_b32(&msg, message, NULL);
  4596. random_scalar_order_test(&key);
  4597. secp256k1_scalar_get_b32(key32, &key);
  4598. secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &qj, &key);
  4599. secp256k1_ge_set_gej(&q, &qj);
  4600. ec_key = get_openssl_key(key32);
  4601. CHECK(ec_key != NULL);
  4602. CHECK(ECDSA_sign(0, message, sizeof(message), signature, &sigsize, ec_key));
  4603. CHECK(secp256k1_ecdsa_sig_parse(&sigr, &sigs, signature, sigsize));
  4604. CHECK(secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sigr, &sigs, &q, &msg));
  4605. secp256k1_scalar_set_int(&one, 1);
  4606. secp256k1_scalar_add(&msg2, &msg, &one);
  4607. CHECK(!secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &sigr, &sigs, &q, &msg2));
  4608. random_sign(&sigr, &sigs, &key, &msg, NULL);
  4609. CHECK(secp256k1_ecdsa_sig_serialize(signature, &secp_sigsize, &sigr, &sigs));
  4610. CHECK(ECDSA_verify(0, message, sizeof(message), signature, secp_sigsize, ec_key) == 1);
  4611. EC_KEY_free(ec_key);
  4612. }
  4613. void run_ecdsa_openssl(void) {
  4614. int i;
  4615. for (i = 0; i < 10*count; i++) {
  4616. test_ecdsa_openssl();
  4617. }
  4618. }
  4619. #endif
  4620. #ifdef ENABLE_MODULE_ECDH
  4621. # include "modules/ecdh/tests_impl.h"
  4622. #endif
  4623. #ifdef ENABLE_MODULE_RECOVERY
  4624. # include "modules/recovery/tests_impl.h"
  4625. #endif
  4626. int main(int argc, char **argv) {
  4627. unsigned char seed16[16] = {0};
  4628. unsigned char run32[32] = {0};
  4629. /* find iteration count */
  4630. if (argc > 1) {
  4631. count = strtol(argv[1], NULL, 0);
  4632. }
  4633. /* find random seed */
  4634. if (argc > 2) {
  4635. int pos = 0;
  4636. const char* ch = argv[2];
  4637. while (pos < 16 && ch[0] != 0 && ch[1] != 0) {
  4638. unsigned short sh;
  4639. if (sscanf(ch, "%2hx", &sh)) {
  4640. seed16[pos] = sh;
  4641. } else {
  4642. break;
  4643. }
  4644. ch += 2;
  4645. pos++;
  4646. }
  4647. } else {
  4648. FILE *frand = fopen("/dev/urandom", "r");
  4649. if ((frand == NULL) || fread(&seed16, sizeof(seed16), 1, frand) != sizeof(seed16)) {
  4650. uint64_t t = time(NULL) * (uint64_t)1337;
  4651. seed16[0] ^= t;
  4652. seed16[1] ^= t >> 8;
  4653. seed16[2] ^= t >> 16;
  4654. seed16[3] ^= t >> 24;
  4655. seed16[4] ^= t >> 32;
  4656. seed16[5] ^= t >> 40;
  4657. seed16[6] ^= t >> 48;
  4658. seed16[7] ^= t >> 56;
  4659. }
  4660. if (frand) {
  4661. fclose(frand);
  4662. }
  4663. }
  4664. secp256k1_rand_seed(seed16);
  4665. printf("test count = %i\n", count);
  4666. printf("random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", seed16[0], seed16[1], seed16[2], seed16[3], seed16[4], seed16[5], seed16[6], seed16[7], seed16[8], seed16[9], seed16[10], seed16[11], seed16[12], seed16[13], seed16[14], seed16[15]);
  4667. /* initialize */
  4668. run_context_tests();
  4669. run_scratch_tests();
  4670. ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
  4671. if (secp256k1_rand_bits(1)) {
  4672. secp256k1_rand256(run32);
  4673. CHECK(secp256k1_context_randomize(ctx, secp256k1_rand_bits(1) ? run32 : NULL));
  4674. }
  4675. run_rand_bits();
  4676. run_rand_int();
  4677. run_sha256_tests();
  4678. run_hmac_sha256_tests();
  4679. run_rfc6979_hmac_sha256_tests();
  4680. #ifndef USE_NUM_NONE
  4681. /* num tests */
  4682. run_num_smalltests();
  4683. #endif
  4684. /* scalar tests */
  4685. run_scalar_tests();
  4686. /* field tests */
  4687. run_field_inv();
  4688. run_field_inv_var();
  4689. run_field_inv_all_var();
  4690. run_field_misc();
  4691. run_field_convert();
  4692. run_sqr();
  4693. run_sqrt();
  4694. /* group tests */
  4695. run_ge();
  4696. run_group_decompress();
  4697. /* ecmult tests */
  4698. run_wnaf();
  4699. run_point_times_order();
  4700. run_ecmult_chain();
  4701. run_ecmult_constants();
  4702. run_ecmult_gen_blind();
  4703. run_ecmult_const_tests();
  4704. run_ecmult_multi_tests();
  4705. run_ec_combine();
  4706. /* endomorphism tests */
  4707. #ifdef USE_ENDOMORPHISM
  4708. run_endomorphism_tests();
  4709. #endif
  4710. /* EC point parser test */
  4711. run_ec_pubkey_parse_test();
  4712. /* EC key edge cases */
  4713. run_eckey_edge_case_test();
  4714. #ifdef ENABLE_MODULE_ECDH
  4715. /* ecdh tests */
  4716. run_ecdh_tests();
  4717. #endif
  4718. /* ecdsa tests */
  4719. run_random_pubkeys();
  4720. run_ecdsa_der_parse();
  4721. run_ecdsa_sign_verify();
  4722. run_ecdsa_end_to_end();
  4723. run_ecdsa_edge_cases();
  4724. #ifdef ENABLE_OPENSSL_TESTS
  4725. run_ecdsa_openssl();
  4726. #endif
  4727. #ifdef ENABLE_MODULE_RECOVERY
  4728. /* ECDSA pubkey recovery tests */
  4729. run_recovery_tests();
  4730. #endif
  4731. secp256k1_rand256(run32);
  4732. printf("random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]);
  4733. /* shutdown */
  4734. secp256k1_context_destroy(ctx);
  4735. printf("no problems found\n");
  4736. return 0;
  4737. }