bntest.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  1. /* crypto/bn/bntest.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. *
  61. * Portions of the attached software ("Contribution") are developed by
  62. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  63. *
  64. * The Contribution is licensed pursuant to the Eric Young open source
  65. * license provided above.
  66. *
  67. * The binary polynomial arithmetic software is originally written by
  68. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  69. *
  70. */
  71. /* Until the key-gen callbacks are modified to use newer prototypes, we allow
  72. * deprecated functions for openssl-internal code */
  73. #ifdef OPENSSL_NO_DEPRECATED
  74. #undef OPENSSL_NO_DEPRECATED
  75. #endif
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. #include "e_os.h"
  80. #include <openssl/bio.h>
  81. #include <openssl/bn.h>
  82. #include <openssl/rand.h>
  83. #include <openssl/x509.h>
  84. #include <openssl/err.h>
  85. const int num0 = 100; /* number of tests */
  86. const int num1 = 50; /* additional tests for some functions */
  87. const int num2 = 5; /* number of tests for slow functions */
  88. int test_add(BIO *bp);
  89. int test_sub(BIO *bp);
  90. int test_lshift1(BIO *bp);
  91. int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
  92. int test_rshift1(BIO *bp);
  93. int test_rshift(BIO *bp,BN_CTX *ctx);
  94. int test_div(BIO *bp,BN_CTX *ctx);
  95. int test_div_word(BIO *bp);
  96. int test_div_recp(BIO *bp,BN_CTX *ctx);
  97. int test_mul(BIO *bp);
  98. int test_sqr(BIO *bp,BN_CTX *ctx);
  99. int test_mont(BIO *bp,BN_CTX *ctx);
  100. int test_mod(BIO *bp,BN_CTX *ctx);
  101. int test_mod_mul(BIO *bp,BN_CTX *ctx);
  102. int test_mod_exp(BIO *bp,BN_CTX *ctx);
  103. int test_mod_exp_mont_consttime(BIO *bp,BN_CTX *ctx);
  104. int test_exp(BIO *bp,BN_CTX *ctx);
  105. int test_gf2m_add(BIO *bp);
  106. int test_gf2m_mod(BIO *bp);
  107. int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx);
  108. int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx);
  109. int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx);
  110. int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx);
  111. int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx);
  112. int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx);
  113. int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx);
  114. int test_kron(BIO *bp,BN_CTX *ctx);
  115. int test_sqrt(BIO *bp,BN_CTX *ctx);
  116. int rand_neg(void);
  117. static int results=0;
  118. static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
  119. "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
  120. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  121. static void message(BIO *out, char *m)
  122. {
  123. fprintf(stderr, "test %s\n", m);
  124. BIO_puts(out, "print \"test ");
  125. BIO_puts(out, m);
  126. BIO_puts(out, "\\n\"\n");
  127. }
  128. int main(int argc, char *argv[])
  129. {
  130. BN_CTX *ctx;
  131. BIO *out;
  132. char *outfile=NULL;
  133. results = 0;
  134. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  135. argc--;
  136. argv++;
  137. while (argc >= 1)
  138. {
  139. if (strcmp(*argv,"-results") == 0)
  140. results=1;
  141. else if (strcmp(*argv,"-out") == 0)
  142. {
  143. if (--argc < 1) break;
  144. outfile= *(++argv);
  145. }
  146. argc--;
  147. argv++;
  148. }
  149. ctx=BN_CTX_new();
  150. if (ctx == NULL) EXIT(1);
  151. out=BIO_new(BIO_s_file());
  152. if (out == NULL) EXIT(1);
  153. if (outfile == NULL)
  154. {
  155. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  156. }
  157. else
  158. {
  159. if (!BIO_write_filename(out,outfile))
  160. {
  161. perror(outfile);
  162. EXIT(1);
  163. }
  164. }
  165. if (!results)
  166. BIO_puts(out,"obase=16\nibase=16\n");
  167. message(out,"BN_add");
  168. if (!test_add(out)) goto err;
  169. (void)BIO_flush(out);
  170. message(out,"BN_sub");
  171. if (!test_sub(out)) goto err;
  172. (void)BIO_flush(out);
  173. message(out,"BN_lshift1");
  174. if (!test_lshift1(out)) goto err;
  175. (void)BIO_flush(out);
  176. message(out,"BN_lshift (fixed)");
  177. if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
  178. goto err;
  179. (void)BIO_flush(out);
  180. message(out,"BN_lshift");
  181. if (!test_lshift(out,ctx,NULL)) goto err;
  182. (void)BIO_flush(out);
  183. message(out,"BN_rshift1");
  184. if (!test_rshift1(out)) goto err;
  185. (void)BIO_flush(out);
  186. message(out,"BN_rshift");
  187. if (!test_rshift(out,ctx)) goto err;
  188. (void)BIO_flush(out);
  189. message(out,"BN_sqr");
  190. if (!test_sqr(out,ctx)) goto err;
  191. (void)BIO_flush(out);
  192. message(out,"BN_mul");
  193. if (!test_mul(out)) goto err;
  194. (void)BIO_flush(out);
  195. message(out,"BN_div");
  196. if (!test_div(out,ctx)) goto err;
  197. (void)BIO_flush(out);
  198. message(out,"BN_div_word");
  199. if (!test_div_word(out)) goto err;
  200. (void)BIO_flush(out);
  201. message(out,"BN_div_recp");
  202. if (!test_div_recp(out,ctx)) goto err;
  203. (void)BIO_flush(out);
  204. message(out,"BN_mod");
  205. if (!test_mod(out,ctx)) goto err;
  206. (void)BIO_flush(out);
  207. message(out,"BN_mod_mul");
  208. if (!test_mod_mul(out,ctx)) goto err;
  209. (void)BIO_flush(out);
  210. message(out,"BN_mont");
  211. if (!test_mont(out,ctx)) goto err;
  212. (void)BIO_flush(out);
  213. message(out,"BN_mod_exp");
  214. if (!test_mod_exp(out,ctx)) goto err;
  215. (void)BIO_flush(out);
  216. message(out,"BN_mod_exp_mont_consttime");
  217. if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
  218. (void)BIO_flush(out);
  219. message(out,"BN_exp");
  220. if (!test_exp(out,ctx)) goto err;
  221. (void)BIO_flush(out);
  222. message(out,"BN_kronecker");
  223. if (!test_kron(out,ctx)) goto err;
  224. (void)BIO_flush(out);
  225. message(out,"BN_mod_sqrt");
  226. if (!test_sqrt(out,ctx)) goto err;
  227. (void)BIO_flush(out);
  228. message(out,"BN_GF2m_add");
  229. if (!test_gf2m_add(out)) goto err;
  230. (void)BIO_flush(out);
  231. message(out,"BN_GF2m_mod");
  232. if (!test_gf2m_mod(out)) goto err;
  233. (void)BIO_flush(out);
  234. message(out,"BN_GF2m_mod_mul");
  235. if (!test_gf2m_mod_mul(out,ctx)) goto err;
  236. (void)BIO_flush(out);
  237. message(out,"BN_GF2m_mod_sqr");
  238. if (!test_gf2m_mod_sqr(out,ctx)) goto err;
  239. (void)BIO_flush(out);
  240. message(out,"BN_GF2m_mod_inv");
  241. if (!test_gf2m_mod_inv(out,ctx)) goto err;
  242. (void)BIO_flush(out);
  243. message(out,"BN_GF2m_mod_div");
  244. if (!test_gf2m_mod_div(out,ctx)) goto err;
  245. (void)BIO_flush(out);
  246. message(out,"BN_GF2m_mod_exp");
  247. if (!test_gf2m_mod_exp(out,ctx)) goto err;
  248. (void)BIO_flush(out);
  249. message(out,"BN_GF2m_mod_sqrt");
  250. if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
  251. (void)BIO_flush(out);
  252. message(out,"BN_GF2m_mod_solve_quad");
  253. if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
  254. (void)BIO_flush(out);
  255. BN_CTX_free(ctx);
  256. BIO_free(out);
  257. /**/
  258. EXIT(0);
  259. err:
  260. BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
  261. * the failure, see test_bn in test/Makefile.ssl*/
  262. (void)BIO_flush(out);
  263. ERR_load_crypto_strings();
  264. ERR_print_errors_fp(stderr);
  265. EXIT(1);
  266. return(1);
  267. }
  268. int test_add(BIO *bp)
  269. {
  270. BIGNUM a,b,c;
  271. int i;
  272. BN_init(&a);
  273. BN_init(&b);
  274. BN_init(&c);
  275. BN_bntest_rand(&a,512,0,0);
  276. for (i=0; i<num0; i++)
  277. {
  278. BN_bntest_rand(&b,450+i,0,0);
  279. a.neg=rand_neg();
  280. b.neg=rand_neg();
  281. BN_add(&c,&a,&b);
  282. if (bp != NULL)
  283. {
  284. if (!results)
  285. {
  286. BN_print(bp,&a);
  287. BIO_puts(bp," + ");
  288. BN_print(bp,&b);
  289. BIO_puts(bp," - ");
  290. }
  291. BN_print(bp,&c);
  292. BIO_puts(bp,"\n");
  293. }
  294. a.neg=!a.neg;
  295. b.neg=!b.neg;
  296. BN_add(&c,&c,&b);
  297. BN_add(&c,&c,&a);
  298. if(!BN_is_zero(&c))
  299. {
  300. fprintf(stderr,"Add test failed!\n");
  301. return 0;
  302. }
  303. }
  304. BN_free(&a);
  305. BN_free(&b);
  306. BN_free(&c);
  307. return(1);
  308. }
  309. int test_sub(BIO *bp)
  310. {
  311. BIGNUM a,b,c;
  312. int i;
  313. BN_init(&a);
  314. BN_init(&b);
  315. BN_init(&c);
  316. for (i=0; i<num0+num1; i++)
  317. {
  318. if (i < num1)
  319. {
  320. BN_bntest_rand(&a,512,0,0);
  321. BN_copy(&b,&a);
  322. if (BN_set_bit(&a,i)==0) return(0);
  323. BN_add_word(&b,i);
  324. }
  325. else
  326. {
  327. BN_bntest_rand(&b,400+i-num1,0,0);
  328. a.neg=rand_neg();
  329. b.neg=rand_neg();
  330. }
  331. BN_sub(&c,&a,&b);
  332. if (bp != NULL)
  333. {
  334. if (!results)
  335. {
  336. BN_print(bp,&a);
  337. BIO_puts(bp," - ");
  338. BN_print(bp,&b);
  339. BIO_puts(bp," - ");
  340. }
  341. BN_print(bp,&c);
  342. BIO_puts(bp,"\n");
  343. }
  344. BN_add(&c,&c,&b);
  345. BN_sub(&c,&c,&a);
  346. if(!BN_is_zero(&c))
  347. {
  348. fprintf(stderr,"Subtract test failed!\n");
  349. return 0;
  350. }
  351. }
  352. BN_free(&a);
  353. BN_free(&b);
  354. BN_free(&c);
  355. return(1);
  356. }
  357. int test_div(BIO *bp, BN_CTX *ctx)
  358. {
  359. BIGNUM a,b,c,d,e;
  360. int i;
  361. BN_init(&a);
  362. BN_init(&b);
  363. BN_init(&c);
  364. BN_init(&d);
  365. BN_init(&e);
  366. for (i=0; i<num0+num1; i++)
  367. {
  368. if (i < num1)
  369. {
  370. BN_bntest_rand(&a,400,0,0);
  371. BN_copy(&b,&a);
  372. BN_lshift(&a,&a,i);
  373. BN_add_word(&a,i);
  374. }
  375. else
  376. BN_bntest_rand(&b,50+3*(i-num1),0,0);
  377. a.neg=rand_neg();
  378. b.neg=rand_neg();
  379. BN_div(&d,&c,&a,&b,ctx);
  380. if (bp != NULL)
  381. {
  382. if (!results)
  383. {
  384. BN_print(bp,&a);
  385. BIO_puts(bp," / ");
  386. BN_print(bp,&b);
  387. BIO_puts(bp," - ");
  388. }
  389. BN_print(bp,&d);
  390. BIO_puts(bp,"\n");
  391. if (!results)
  392. {
  393. BN_print(bp,&a);
  394. BIO_puts(bp," % ");
  395. BN_print(bp,&b);
  396. BIO_puts(bp," - ");
  397. }
  398. BN_print(bp,&c);
  399. BIO_puts(bp,"\n");
  400. }
  401. BN_mul(&e,&d,&b,ctx);
  402. BN_add(&d,&e,&c);
  403. BN_sub(&d,&d,&a);
  404. if(!BN_is_zero(&d))
  405. {
  406. fprintf(stderr,"Division test failed!\n");
  407. return 0;
  408. }
  409. }
  410. BN_free(&a);
  411. BN_free(&b);
  412. BN_free(&c);
  413. BN_free(&d);
  414. BN_free(&e);
  415. return(1);
  416. }
  417. static void print_word(BIO *bp,BN_ULONG w)
  418. {
  419. #ifdef SIXTY_FOUR_BIT
  420. if (sizeof(w) > sizeof(unsigned long))
  421. {
  422. unsigned long h=(unsigned long)(w>>32),
  423. l=(unsigned long)(w);
  424. if (h) BIO_printf(bp,"%lX%08lX",h,l);
  425. else BIO_printf(bp,"%lX",l);
  426. return;
  427. }
  428. #endif
  429. BIO_printf(bp,"%lX",w);
  430. }
  431. int test_div_word(BIO *bp)
  432. {
  433. BIGNUM a,b;
  434. BN_ULONG r,s;
  435. int i;
  436. BN_init(&a);
  437. BN_init(&b);
  438. for (i=0; i<num0; i++)
  439. {
  440. do {
  441. BN_bntest_rand(&a,512,-1,0);
  442. BN_bntest_rand(&b,BN_BITS2,-1,0);
  443. s = b.d[0];
  444. } while (!s);
  445. BN_copy(&b, &a);
  446. r = BN_div_word(&b, s);
  447. if (bp != NULL)
  448. {
  449. if (!results)
  450. {
  451. BN_print(bp,&a);
  452. BIO_puts(bp," / ");
  453. print_word(bp,s);
  454. BIO_puts(bp," - ");
  455. }
  456. BN_print(bp,&b);
  457. BIO_puts(bp,"\n");
  458. if (!results)
  459. {
  460. BN_print(bp,&a);
  461. BIO_puts(bp," % ");
  462. print_word(bp,s);
  463. BIO_puts(bp," - ");
  464. }
  465. print_word(bp,r);
  466. BIO_puts(bp,"\n");
  467. }
  468. BN_mul_word(&b,s);
  469. BN_add_word(&b,r);
  470. BN_sub(&b,&a,&b);
  471. if(!BN_is_zero(&b))
  472. {
  473. fprintf(stderr,"Division (word) test failed!\n");
  474. return 0;
  475. }
  476. }
  477. BN_free(&a);
  478. BN_free(&b);
  479. return(1);
  480. }
  481. int test_div_recp(BIO *bp, BN_CTX *ctx)
  482. {
  483. BIGNUM a,b,c,d,e;
  484. BN_RECP_CTX recp;
  485. int i;
  486. BN_RECP_CTX_init(&recp);
  487. BN_init(&a);
  488. BN_init(&b);
  489. BN_init(&c);
  490. BN_init(&d);
  491. BN_init(&e);
  492. for (i=0; i<num0+num1; i++)
  493. {
  494. if (i < num1)
  495. {
  496. BN_bntest_rand(&a,400,0,0);
  497. BN_copy(&b,&a);
  498. BN_lshift(&a,&a,i);
  499. BN_add_word(&a,i);
  500. }
  501. else
  502. BN_bntest_rand(&b,50+3*(i-num1),0,0);
  503. a.neg=rand_neg();
  504. b.neg=rand_neg();
  505. BN_RECP_CTX_set(&recp,&b,ctx);
  506. BN_div_recp(&d,&c,&a,&recp,ctx);
  507. if (bp != NULL)
  508. {
  509. if (!results)
  510. {
  511. BN_print(bp,&a);
  512. BIO_puts(bp," / ");
  513. BN_print(bp,&b);
  514. BIO_puts(bp," - ");
  515. }
  516. BN_print(bp,&d);
  517. BIO_puts(bp,"\n");
  518. if (!results)
  519. {
  520. BN_print(bp,&a);
  521. BIO_puts(bp," % ");
  522. BN_print(bp,&b);
  523. BIO_puts(bp," - ");
  524. }
  525. BN_print(bp,&c);
  526. BIO_puts(bp,"\n");
  527. }
  528. BN_mul(&e,&d,&b,ctx);
  529. BN_add(&d,&e,&c);
  530. BN_sub(&d,&d,&a);
  531. if(!BN_is_zero(&d))
  532. {
  533. fprintf(stderr,"Reciprocal division test failed!\n");
  534. fprintf(stderr,"a=");
  535. BN_print_fp(stderr,&a);
  536. fprintf(stderr,"\nb=");
  537. BN_print_fp(stderr,&b);
  538. fprintf(stderr,"\n");
  539. return 0;
  540. }
  541. }
  542. BN_free(&a);
  543. BN_free(&b);
  544. BN_free(&c);
  545. BN_free(&d);
  546. BN_free(&e);
  547. BN_RECP_CTX_free(&recp);
  548. return(1);
  549. }
  550. int test_mul(BIO *bp)
  551. {
  552. BIGNUM a,b,c,d,e;
  553. int i;
  554. BN_CTX *ctx;
  555. ctx = BN_CTX_new();
  556. if (ctx == NULL) EXIT(1);
  557. BN_init(&a);
  558. BN_init(&b);
  559. BN_init(&c);
  560. BN_init(&d);
  561. BN_init(&e);
  562. for (i=0; i<num0+num1; i++)
  563. {
  564. if (i <= num1)
  565. {
  566. BN_bntest_rand(&a,100,0,0);
  567. BN_bntest_rand(&b,100,0,0);
  568. }
  569. else
  570. BN_bntest_rand(&b,i-num1,0,0);
  571. a.neg=rand_neg();
  572. b.neg=rand_neg();
  573. BN_mul(&c,&a,&b,ctx);
  574. if (bp != NULL)
  575. {
  576. if (!results)
  577. {
  578. BN_print(bp,&a);
  579. BIO_puts(bp," * ");
  580. BN_print(bp,&b);
  581. BIO_puts(bp," - ");
  582. }
  583. BN_print(bp,&c);
  584. BIO_puts(bp,"\n");
  585. }
  586. BN_div(&d,&e,&c,&a,ctx);
  587. BN_sub(&d,&d,&b);
  588. if(!BN_is_zero(&d) || !BN_is_zero(&e))
  589. {
  590. fprintf(stderr,"Multiplication test failed!\n");
  591. return 0;
  592. }
  593. }
  594. BN_free(&a);
  595. BN_free(&b);
  596. BN_free(&c);
  597. BN_free(&d);
  598. BN_free(&e);
  599. BN_CTX_free(ctx);
  600. return(1);
  601. }
  602. int test_sqr(BIO *bp, BN_CTX *ctx)
  603. {
  604. BIGNUM a,c,d,e;
  605. int i;
  606. BN_init(&a);
  607. BN_init(&c);
  608. BN_init(&d);
  609. BN_init(&e);
  610. for (i=0; i<num0; i++)
  611. {
  612. BN_bntest_rand(&a,40+i*10,0,0);
  613. a.neg=rand_neg();
  614. BN_sqr(&c,&a,ctx);
  615. if (bp != NULL)
  616. {
  617. if (!results)
  618. {
  619. BN_print(bp,&a);
  620. BIO_puts(bp," * ");
  621. BN_print(bp,&a);
  622. BIO_puts(bp," - ");
  623. }
  624. BN_print(bp,&c);
  625. BIO_puts(bp,"\n");
  626. }
  627. BN_div(&d,&e,&c,&a,ctx);
  628. BN_sub(&d,&d,&a);
  629. if(!BN_is_zero(&d) || !BN_is_zero(&e))
  630. {
  631. fprintf(stderr,"Square test failed!\n");
  632. return 0;
  633. }
  634. }
  635. BN_free(&a);
  636. BN_free(&c);
  637. BN_free(&d);
  638. BN_free(&e);
  639. return(1);
  640. }
  641. int test_mont(BIO *bp, BN_CTX *ctx)
  642. {
  643. BIGNUM a,b,c,d,A,B;
  644. BIGNUM n;
  645. int i;
  646. BN_MONT_CTX *mont;
  647. BN_init(&a);
  648. BN_init(&b);
  649. BN_init(&c);
  650. BN_init(&d);
  651. BN_init(&A);
  652. BN_init(&B);
  653. BN_init(&n);
  654. mont=BN_MONT_CTX_new();
  655. BN_bntest_rand(&a,100,0,0); /**/
  656. BN_bntest_rand(&b,100,0,0); /**/
  657. for (i=0; i<num2; i++)
  658. {
  659. int bits = (200*(i+1))/num2;
  660. if (bits == 0)
  661. continue;
  662. BN_bntest_rand(&n,bits,0,1);
  663. BN_MONT_CTX_set(mont,&n,ctx);
  664. BN_nnmod(&a,&a,&n,ctx);
  665. BN_nnmod(&b,&b,&n,ctx);
  666. BN_to_montgomery(&A,&a,mont,ctx);
  667. BN_to_montgomery(&B,&b,mont,ctx);
  668. BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
  669. BN_from_montgomery(&A,&c,mont,ctx);/**/
  670. if (bp != NULL)
  671. {
  672. if (!results)
  673. {
  674. #ifdef undef
  675. fprintf(stderr,"%d * %d %% %d\n",
  676. BN_num_bits(&a),
  677. BN_num_bits(&b),
  678. BN_num_bits(mont->N));
  679. #endif
  680. BN_print(bp,&a);
  681. BIO_puts(bp," * ");
  682. BN_print(bp,&b);
  683. BIO_puts(bp," % ");
  684. BN_print(bp,&(mont->N));
  685. BIO_puts(bp," - ");
  686. }
  687. BN_print(bp,&A);
  688. BIO_puts(bp,"\n");
  689. }
  690. BN_mod_mul(&d,&a,&b,&n,ctx);
  691. BN_sub(&d,&d,&A);
  692. if(!BN_is_zero(&d))
  693. {
  694. fprintf(stderr,"Montgomery multiplication test failed!\n");
  695. return 0;
  696. }
  697. }
  698. BN_MONT_CTX_free(mont);
  699. BN_free(&a);
  700. BN_free(&b);
  701. BN_free(&c);
  702. BN_free(&d);
  703. BN_free(&A);
  704. BN_free(&B);
  705. BN_free(&n);
  706. return(1);
  707. }
  708. int test_mod(BIO *bp, BN_CTX *ctx)
  709. {
  710. BIGNUM *a,*b,*c,*d,*e;
  711. int i;
  712. a=BN_new();
  713. b=BN_new();
  714. c=BN_new();
  715. d=BN_new();
  716. e=BN_new();
  717. BN_bntest_rand(a,1024,0,0); /**/
  718. for (i=0; i<num0; i++)
  719. {
  720. BN_bntest_rand(b,450+i*10,0,0); /**/
  721. a->neg=rand_neg();
  722. b->neg=rand_neg();
  723. BN_mod(c,a,b,ctx);/**/
  724. if (bp != NULL)
  725. {
  726. if (!results)
  727. {
  728. BN_print(bp,a);
  729. BIO_puts(bp," % ");
  730. BN_print(bp,b);
  731. BIO_puts(bp," - ");
  732. }
  733. BN_print(bp,c);
  734. BIO_puts(bp,"\n");
  735. }
  736. BN_div(d,e,a,b,ctx);
  737. BN_sub(e,e,c);
  738. if(!BN_is_zero(e))
  739. {
  740. fprintf(stderr,"Modulo test failed!\n");
  741. return 0;
  742. }
  743. }
  744. BN_free(a);
  745. BN_free(b);
  746. BN_free(c);
  747. BN_free(d);
  748. BN_free(e);
  749. return(1);
  750. }
  751. int test_mod_mul(BIO *bp, BN_CTX *ctx)
  752. {
  753. BIGNUM *a,*b,*c,*d,*e;
  754. int i,j;
  755. a=BN_new();
  756. b=BN_new();
  757. c=BN_new();
  758. d=BN_new();
  759. e=BN_new();
  760. for (j=0; j<3; j++) {
  761. BN_bntest_rand(c,1024,0,0); /**/
  762. for (i=0; i<num0; i++)
  763. {
  764. BN_bntest_rand(a,475+i*10,0,0); /**/
  765. BN_bntest_rand(b,425+i*11,0,0); /**/
  766. a->neg=rand_neg();
  767. b->neg=rand_neg();
  768. if (!BN_mod_mul(e,a,b,c,ctx))
  769. {
  770. unsigned long l;
  771. while ((l=ERR_get_error()))
  772. fprintf(stderr,"ERROR:%s\n",
  773. ERR_error_string(l,NULL));
  774. EXIT(1);
  775. }
  776. if (bp != NULL)
  777. {
  778. if (!results)
  779. {
  780. BN_print(bp,a);
  781. BIO_puts(bp," * ");
  782. BN_print(bp,b);
  783. BIO_puts(bp," % ");
  784. BN_print(bp,c);
  785. if ((a->neg ^ b->neg) && !BN_is_zero(e))
  786. {
  787. /* If (a*b) % c is negative, c must be added
  788. * in order to obtain the normalized remainder
  789. * (new with OpenSSL 0.9.7, previous versions of
  790. * BN_mod_mul could generate negative results)
  791. */
  792. BIO_puts(bp," + ");
  793. BN_print(bp,c);
  794. }
  795. BIO_puts(bp," - ");
  796. }
  797. BN_print(bp,e);
  798. BIO_puts(bp,"\n");
  799. }
  800. BN_mul(d,a,b,ctx);
  801. BN_sub(d,d,e);
  802. BN_div(a,b,d,c,ctx);
  803. if(!BN_is_zero(b))
  804. {
  805. fprintf(stderr,"Modulo multiply test failed!\n");
  806. ERR_print_errors_fp(stderr);
  807. return 0;
  808. }
  809. }
  810. }
  811. BN_free(a);
  812. BN_free(b);
  813. BN_free(c);
  814. BN_free(d);
  815. BN_free(e);
  816. return(1);
  817. }
  818. int test_mod_exp(BIO *bp, BN_CTX *ctx)
  819. {
  820. BIGNUM *a,*b,*c,*d,*e;
  821. int i;
  822. a=BN_new();
  823. b=BN_new();
  824. c=BN_new();
  825. d=BN_new();
  826. e=BN_new();
  827. BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
  828. for (i=0; i<num2; i++)
  829. {
  830. BN_bntest_rand(a,20+i*5,0,0); /**/
  831. BN_bntest_rand(b,2+i,0,0); /**/
  832. if (!BN_mod_exp(d,a,b,c,ctx))
  833. return(00);
  834. if (bp != NULL)
  835. {
  836. if (!results)
  837. {
  838. BN_print(bp,a);
  839. BIO_puts(bp," ^ ");
  840. BN_print(bp,b);
  841. BIO_puts(bp," % ");
  842. BN_print(bp,c);
  843. BIO_puts(bp," - ");
  844. }
  845. BN_print(bp,d);
  846. BIO_puts(bp,"\n");
  847. }
  848. BN_exp(e,a,b,ctx);
  849. BN_sub(e,e,d);
  850. BN_div(a,b,e,c,ctx);
  851. if(!BN_is_zero(b))
  852. {
  853. fprintf(stderr,"Modulo exponentiation test failed!\n");
  854. return 0;
  855. }
  856. }
  857. BN_free(a);
  858. BN_free(b);
  859. BN_free(c);
  860. BN_free(d);
  861. BN_free(e);
  862. return(1);
  863. }
  864. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
  865. {
  866. BIGNUM *a,*b,*c,*d,*e;
  867. int i;
  868. a=BN_new();
  869. b=BN_new();
  870. c=BN_new();
  871. d=BN_new();
  872. e=BN_new();
  873. BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
  874. for (i=0; i<num2; i++)
  875. {
  876. BN_bntest_rand(a,20+i*5,0,0); /**/
  877. BN_bntest_rand(b,2+i,0,0); /**/
  878. if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL))
  879. return(00);
  880. if (bp != NULL)
  881. {
  882. if (!results)
  883. {
  884. BN_print(bp,a);
  885. BIO_puts(bp," ^ ");
  886. BN_print(bp,b);
  887. BIO_puts(bp," % ");
  888. BN_print(bp,c);
  889. BIO_puts(bp," - ");
  890. }
  891. BN_print(bp,d);
  892. BIO_puts(bp,"\n");
  893. }
  894. BN_exp(e,a,b,ctx);
  895. BN_sub(e,e,d);
  896. BN_div(a,b,e,c,ctx);
  897. if(!BN_is_zero(b))
  898. {
  899. fprintf(stderr,"Modulo exponentiation test failed!\n");
  900. return 0;
  901. }
  902. }
  903. BN_free(a);
  904. BN_free(b);
  905. BN_free(c);
  906. BN_free(d);
  907. BN_free(e);
  908. return(1);
  909. }
  910. int test_exp(BIO *bp, BN_CTX *ctx)
  911. {
  912. BIGNUM *a,*b,*d,*e,*one;
  913. int i;
  914. a=BN_new();
  915. b=BN_new();
  916. d=BN_new();
  917. e=BN_new();
  918. one=BN_new();
  919. BN_one(one);
  920. for (i=0; i<num2; i++)
  921. {
  922. BN_bntest_rand(a,20+i*5,0,0); /**/
  923. BN_bntest_rand(b,2+i,0,0); /**/
  924. if (!BN_exp(d,a,b,ctx))
  925. return(00);
  926. if (bp != NULL)
  927. {
  928. if (!results)
  929. {
  930. BN_print(bp,a);
  931. BIO_puts(bp," ^ ");
  932. BN_print(bp,b);
  933. BIO_puts(bp," - ");
  934. }
  935. BN_print(bp,d);
  936. BIO_puts(bp,"\n");
  937. }
  938. BN_one(e);
  939. for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
  940. BN_mul(e,e,a,ctx);
  941. BN_sub(e,e,d);
  942. if(!BN_is_zero(e))
  943. {
  944. fprintf(stderr,"Exponentiation test failed!\n");
  945. return 0;
  946. }
  947. }
  948. BN_free(a);
  949. BN_free(b);
  950. BN_free(d);
  951. BN_free(e);
  952. BN_free(one);
  953. return(1);
  954. }
  955. int test_gf2m_add(BIO *bp)
  956. {
  957. BIGNUM a,b,c;
  958. int i, ret = 0;
  959. BN_init(&a);
  960. BN_init(&b);
  961. BN_init(&c);
  962. for (i=0; i<num0; i++)
  963. {
  964. BN_rand(&a,512,0,0);
  965. BN_copy(&b, BN_value_one());
  966. a.neg=rand_neg();
  967. b.neg=rand_neg();
  968. BN_GF2m_add(&c,&a,&b);
  969. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  970. if (bp != NULL)
  971. {
  972. if (!results)
  973. {
  974. BN_print(bp,&a);
  975. BIO_puts(bp," ^ ");
  976. BN_print(bp,&b);
  977. BIO_puts(bp," = ");
  978. }
  979. BN_print(bp,&c);
  980. BIO_puts(bp,"\n");
  981. }
  982. #endif
  983. /* Test that two added values have the correct parity. */
  984. if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c)))
  985. {
  986. fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
  987. goto err;
  988. }
  989. BN_GF2m_add(&c,&c,&c);
  990. /* Test that c + c = 0. */
  991. if(!BN_is_zero(&c))
  992. {
  993. fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
  994. goto err;
  995. }
  996. }
  997. ret = 1;
  998. err:
  999. BN_free(&a);
  1000. BN_free(&b);
  1001. BN_free(&c);
  1002. return ret;
  1003. }
  1004. int test_gf2m_mod(BIO *bp)
  1005. {
  1006. BIGNUM *a,*b[2],*c,*d,*e;
  1007. int i, j, ret = 0;
  1008. unsigned int p0[] = {163,7,6,3,0};
  1009. unsigned int p1[] = {193,15,0};
  1010. a=BN_new();
  1011. b[0]=BN_new();
  1012. b[1]=BN_new();
  1013. c=BN_new();
  1014. d=BN_new();
  1015. e=BN_new();
  1016. BN_GF2m_arr2poly(p0, b[0]);
  1017. BN_GF2m_arr2poly(p1, b[1]);
  1018. for (i=0; i<num0; i++)
  1019. {
  1020. BN_bntest_rand(a, 1024, 0, 0);
  1021. for (j=0; j < 2; j++)
  1022. {
  1023. BN_GF2m_mod(c, a, b[j]);
  1024. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1025. if (bp != NULL)
  1026. {
  1027. if (!results)
  1028. {
  1029. BN_print(bp,a);
  1030. BIO_puts(bp," % ");
  1031. BN_print(bp,b[j]);
  1032. BIO_puts(bp," - ");
  1033. BN_print(bp,c);
  1034. BIO_puts(bp,"\n");
  1035. }
  1036. }
  1037. #endif
  1038. BN_GF2m_add(d, a, c);
  1039. BN_GF2m_mod(e, d, b[j]);
  1040. /* Test that a + (a mod p) mod p == 0. */
  1041. if(!BN_is_zero(e))
  1042. {
  1043. fprintf(stderr,"GF(2^m) modulo test failed!\n");
  1044. goto err;
  1045. }
  1046. }
  1047. }
  1048. ret = 1;
  1049. err:
  1050. BN_free(a);
  1051. BN_free(b[0]);
  1052. BN_free(b[1]);
  1053. BN_free(c);
  1054. BN_free(d);
  1055. BN_free(e);
  1056. return ret;
  1057. }
  1058. int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
  1059. {
  1060. BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
  1061. int i, j, ret = 0;
  1062. unsigned int p0[] = {163,7,6,3,0};
  1063. unsigned int p1[] = {193,15,0};
  1064. a=BN_new();
  1065. b[0]=BN_new();
  1066. b[1]=BN_new();
  1067. c=BN_new();
  1068. d=BN_new();
  1069. e=BN_new();
  1070. f=BN_new();
  1071. g=BN_new();
  1072. h=BN_new();
  1073. BN_GF2m_arr2poly(p0, b[0]);
  1074. BN_GF2m_arr2poly(p1, b[1]);
  1075. for (i=0; i<num0; i++)
  1076. {
  1077. BN_bntest_rand(a, 1024, 0, 0);
  1078. BN_bntest_rand(c, 1024, 0, 0);
  1079. BN_bntest_rand(d, 1024, 0, 0);
  1080. for (j=0; j < 2; j++)
  1081. {
  1082. BN_GF2m_mod_mul(e, a, c, b[j], ctx);
  1083. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1084. if (bp != NULL)
  1085. {
  1086. if (!results)
  1087. {
  1088. BN_print(bp,a);
  1089. BIO_puts(bp," * ");
  1090. BN_print(bp,c);
  1091. BIO_puts(bp," % ");
  1092. BN_print(bp,b[j]);
  1093. BIO_puts(bp," - ");
  1094. BN_print(bp,e);
  1095. BIO_puts(bp,"\n");
  1096. }
  1097. }
  1098. #endif
  1099. BN_GF2m_add(f, a, d);
  1100. BN_GF2m_mod_mul(g, f, c, b[j], ctx);
  1101. BN_GF2m_mod_mul(h, d, c, b[j], ctx);
  1102. BN_GF2m_add(f, e, g);
  1103. BN_GF2m_add(f, f, h);
  1104. /* Test that (a+d)*c = a*c + d*c. */
  1105. if(!BN_is_zero(f))
  1106. {
  1107. fprintf(stderr,"GF(2^m) modular multiplication test failed!\n");
  1108. goto err;
  1109. }
  1110. }
  1111. }
  1112. ret = 1;
  1113. err:
  1114. BN_free(a);
  1115. BN_free(b[0]);
  1116. BN_free(b[1]);
  1117. BN_free(c);
  1118. BN_free(d);
  1119. BN_free(e);
  1120. BN_free(f);
  1121. BN_free(g);
  1122. BN_free(h);
  1123. return ret;
  1124. }
  1125. int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
  1126. {
  1127. BIGNUM *a,*b[2],*c,*d;
  1128. int i, j, ret = 0;
  1129. unsigned int p0[] = {163,7,6,3,0};
  1130. unsigned int p1[] = {193,15,0};
  1131. a=BN_new();
  1132. b[0]=BN_new();
  1133. b[1]=BN_new();
  1134. c=BN_new();
  1135. d=BN_new();
  1136. BN_GF2m_arr2poly(p0, b[0]);
  1137. BN_GF2m_arr2poly(p1, b[1]);
  1138. for (i=0; i<num0; i++)
  1139. {
  1140. BN_bntest_rand(a, 1024, 0, 0);
  1141. for (j=0; j < 2; j++)
  1142. {
  1143. BN_GF2m_mod_sqr(c, a, b[j], ctx);
  1144. BN_copy(d, a);
  1145. BN_GF2m_mod_mul(d, a, d, b[j], ctx);
  1146. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1147. if (bp != NULL)
  1148. {
  1149. if (!results)
  1150. {
  1151. BN_print(bp,a);
  1152. BIO_puts(bp," ^ 2 % ");
  1153. BN_print(bp,b[j]);
  1154. BIO_puts(bp, " = ");
  1155. BN_print(bp,c);
  1156. BIO_puts(bp,"; a * a = ");
  1157. BN_print(bp,d);
  1158. BIO_puts(bp,"\n");
  1159. }
  1160. }
  1161. #endif
  1162. BN_GF2m_add(d, c, d);
  1163. /* Test that a*a = a^2. */
  1164. if(!BN_is_zero(d))
  1165. {
  1166. fprintf(stderr,"GF(2^m) modular squaring test failed!\n");
  1167. goto err;
  1168. }
  1169. }
  1170. }
  1171. ret = 1;
  1172. err:
  1173. BN_free(a);
  1174. BN_free(b[0]);
  1175. BN_free(b[1]);
  1176. BN_free(c);
  1177. BN_free(d);
  1178. return ret;
  1179. }
  1180. int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
  1181. {
  1182. BIGNUM *a,*b[2],*c,*d;
  1183. int i, j, ret = 0;
  1184. unsigned int p0[] = {163,7,6,3,0};
  1185. unsigned int p1[] = {193,15,0};
  1186. a=BN_new();
  1187. b[0]=BN_new();
  1188. b[1]=BN_new();
  1189. c=BN_new();
  1190. d=BN_new();
  1191. BN_GF2m_arr2poly(p0, b[0]);
  1192. BN_GF2m_arr2poly(p1, b[1]);
  1193. for (i=0; i<num0; i++)
  1194. {
  1195. BN_bntest_rand(a, 512, 0, 0);
  1196. for (j=0; j < 2; j++)
  1197. {
  1198. BN_GF2m_mod_inv(c, a, b[j], ctx);
  1199. BN_GF2m_mod_mul(d, a, c, b[j], ctx);
  1200. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1201. if (bp != NULL)
  1202. {
  1203. if (!results)
  1204. {
  1205. BN_print(bp,a);
  1206. BIO_puts(bp, " * ");
  1207. BN_print(bp,c);
  1208. BIO_puts(bp," - 1 % ");
  1209. BN_print(bp,b[j]);
  1210. BIO_puts(bp,"\n");
  1211. }
  1212. }
  1213. #endif
  1214. /* Test that ((1/a)*a) = 1. */
  1215. if(!BN_is_one(d))
  1216. {
  1217. fprintf(stderr,"GF(2^m) modular inversion test failed!\n");
  1218. goto err;
  1219. }
  1220. }
  1221. }
  1222. ret = 1;
  1223. err:
  1224. BN_free(a);
  1225. BN_free(b[0]);
  1226. BN_free(b[1]);
  1227. BN_free(c);
  1228. BN_free(d);
  1229. return ret;
  1230. }
  1231. int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
  1232. {
  1233. BIGNUM *a,*b[2],*c,*d,*e,*f;
  1234. int i, j, ret = 0;
  1235. unsigned int p0[] = {163,7,6,3,0};
  1236. unsigned int p1[] = {193,15,0};
  1237. a=BN_new();
  1238. b[0]=BN_new();
  1239. b[1]=BN_new();
  1240. c=BN_new();
  1241. d=BN_new();
  1242. e=BN_new();
  1243. f=BN_new();
  1244. BN_GF2m_arr2poly(p0, b[0]);
  1245. BN_GF2m_arr2poly(p1, b[1]);
  1246. for (i=0; i<num0; i++)
  1247. {
  1248. BN_bntest_rand(a, 512, 0, 0);
  1249. BN_bntest_rand(c, 512, 0, 0);
  1250. for (j=0; j < 2; j++)
  1251. {
  1252. BN_GF2m_mod_div(d, a, c, b[j], ctx);
  1253. BN_GF2m_mod_mul(e, d, c, b[j], ctx);
  1254. BN_GF2m_mod_div(f, a, e, b[j], ctx);
  1255. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1256. if (bp != NULL)
  1257. {
  1258. if (!results)
  1259. {
  1260. BN_print(bp,a);
  1261. BIO_puts(bp, " = ");
  1262. BN_print(bp,c);
  1263. BIO_puts(bp," * ");
  1264. BN_print(bp,d);
  1265. BIO_puts(bp, " % ");
  1266. BN_print(bp,b[j]);
  1267. BIO_puts(bp,"\n");
  1268. }
  1269. }
  1270. #endif
  1271. /* Test that ((a/c)*c)/a = 1. */
  1272. if(!BN_is_one(f))
  1273. {
  1274. fprintf(stderr,"GF(2^m) modular division test failed!\n");
  1275. goto err;
  1276. }
  1277. }
  1278. }
  1279. ret = 1;
  1280. err:
  1281. BN_free(a);
  1282. BN_free(b[0]);
  1283. BN_free(b[1]);
  1284. BN_free(c);
  1285. BN_free(d);
  1286. BN_free(e);
  1287. BN_free(f);
  1288. return ret;
  1289. }
  1290. int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
  1291. {
  1292. BIGNUM *a,*b[2],*c,*d,*e,*f;
  1293. int i, j, ret = 0;
  1294. unsigned int p0[] = {163,7,6,3,0};
  1295. unsigned int p1[] = {193,15,0};
  1296. a=BN_new();
  1297. b[0]=BN_new();
  1298. b[1]=BN_new();
  1299. c=BN_new();
  1300. d=BN_new();
  1301. e=BN_new();
  1302. f=BN_new();
  1303. BN_GF2m_arr2poly(p0, b[0]);
  1304. BN_GF2m_arr2poly(p1, b[1]);
  1305. for (i=0; i<num0; i++)
  1306. {
  1307. BN_bntest_rand(a, 512, 0, 0);
  1308. BN_bntest_rand(c, 512, 0, 0);
  1309. BN_bntest_rand(d, 512, 0, 0);
  1310. for (j=0; j < 2; j++)
  1311. {
  1312. BN_GF2m_mod_exp(e, a, c, b[j], ctx);
  1313. BN_GF2m_mod_exp(f, a, d, b[j], ctx);
  1314. BN_GF2m_mod_mul(e, e, f, b[j], ctx);
  1315. BN_add(f, c, d);
  1316. BN_GF2m_mod_exp(f, a, f, b[j], ctx);
  1317. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1318. if (bp != NULL)
  1319. {
  1320. if (!results)
  1321. {
  1322. BN_print(bp,a);
  1323. BIO_puts(bp, " ^ (");
  1324. BN_print(bp,c);
  1325. BIO_puts(bp," + ");
  1326. BN_print(bp,d);
  1327. BIO_puts(bp, ") = ");
  1328. BN_print(bp,e);
  1329. BIO_puts(bp, "; - ");
  1330. BN_print(bp,f);
  1331. BIO_puts(bp, " % ");
  1332. BN_print(bp,b[j]);
  1333. BIO_puts(bp,"\n");
  1334. }
  1335. }
  1336. #endif
  1337. BN_GF2m_add(f, e, f);
  1338. /* Test that a^(c+d)=a^c*a^d. */
  1339. if(!BN_is_zero(f))
  1340. {
  1341. fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
  1342. goto err;
  1343. }
  1344. }
  1345. }
  1346. ret = 1;
  1347. err:
  1348. BN_free(a);
  1349. BN_free(b[0]);
  1350. BN_free(b[1]);
  1351. BN_free(c);
  1352. BN_free(d);
  1353. BN_free(e);
  1354. BN_free(f);
  1355. return ret;
  1356. }
  1357. int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
  1358. {
  1359. BIGNUM *a,*b[2],*c,*d,*e,*f;
  1360. int i, j, ret = 0;
  1361. unsigned int p0[] = {163,7,6,3,0};
  1362. unsigned int p1[] = {193,15,0};
  1363. a=BN_new();
  1364. b[0]=BN_new();
  1365. b[1]=BN_new();
  1366. c=BN_new();
  1367. d=BN_new();
  1368. e=BN_new();
  1369. f=BN_new();
  1370. BN_GF2m_arr2poly(p0, b[0]);
  1371. BN_GF2m_arr2poly(p1, b[1]);
  1372. for (i=0; i<num0; i++)
  1373. {
  1374. BN_bntest_rand(a, 512, 0, 0);
  1375. for (j=0; j < 2; j++)
  1376. {
  1377. BN_GF2m_mod(c, a, b[j]);
  1378. BN_GF2m_mod_sqrt(d, a, b[j], ctx);
  1379. BN_GF2m_mod_sqr(e, d, b[j], ctx);
  1380. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1381. if (bp != NULL)
  1382. {
  1383. if (!results)
  1384. {
  1385. BN_print(bp,d);
  1386. BIO_puts(bp, " ^ 2 - ");
  1387. BN_print(bp,a);
  1388. BIO_puts(bp,"\n");
  1389. }
  1390. }
  1391. #endif
  1392. BN_GF2m_add(f, c, e);
  1393. /* Test that d^2 = a, where d = sqrt(a). */
  1394. if(!BN_is_zero(f))
  1395. {
  1396. fprintf(stderr,"GF(2^m) modular square root test failed!\n");
  1397. goto err;
  1398. }
  1399. }
  1400. }
  1401. ret = 1;
  1402. err:
  1403. BN_free(a);
  1404. BN_free(b[0]);
  1405. BN_free(b[1]);
  1406. BN_free(c);
  1407. BN_free(d);
  1408. BN_free(e);
  1409. BN_free(f);
  1410. return ret;
  1411. }
  1412. int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
  1413. {
  1414. BIGNUM *a,*b[2],*c,*d,*e;
  1415. int i, j, s = 0, t, ret = 0;
  1416. unsigned int p0[] = {163,7,6,3,0};
  1417. unsigned int p1[] = {193,15,0};
  1418. a=BN_new();
  1419. b[0]=BN_new();
  1420. b[1]=BN_new();
  1421. c=BN_new();
  1422. d=BN_new();
  1423. e=BN_new();
  1424. BN_GF2m_arr2poly(p0, b[0]);
  1425. BN_GF2m_arr2poly(p1, b[1]);
  1426. for (i=0; i<num0; i++)
  1427. {
  1428. BN_bntest_rand(a, 512, 0, 0);
  1429. for (j=0; j < 2; j++)
  1430. {
  1431. t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
  1432. if (t)
  1433. {
  1434. s++;
  1435. BN_GF2m_mod_sqr(d, c, b[j], ctx);
  1436. BN_GF2m_add(d, c, d);
  1437. BN_GF2m_mod(e, a, b[j]);
  1438. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1439. if (bp != NULL)
  1440. {
  1441. if (!results)
  1442. {
  1443. BN_print(bp,c);
  1444. BIO_puts(bp, " is root of z^2 + z = ");
  1445. BN_print(bp,a);
  1446. BIO_puts(bp, " % ");
  1447. BN_print(bp,b[j]);
  1448. BIO_puts(bp, "\n");
  1449. }
  1450. }
  1451. #endif
  1452. BN_GF2m_add(e, e, d);
  1453. /* Test that solution of quadratic c satisfies c^2 + c = a. */
  1454. if(!BN_is_zero(e))
  1455. {
  1456. fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n");
  1457. goto err;
  1458. }
  1459. }
  1460. else
  1461. {
  1462. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1463. if (bp != NULL)
  1464. {
  1465. if (!results)
  1466. {
  1467. BIO_puts(bp, "There are no roots of z^2 + z = ");
  1468. BN_print(bp,a);
  1469. BIO_puts(bp, " % ");
  1470. BN_print(bp,b[j]);
  1471. BIO_puts(bp, "\n");
  1472. }
  1473. }
  1474. #endif
  1475. }
  1476. }
  1477. }
  1478. if (s == 0)
  1479. {
  1480. fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
  1481. fprintf(stderr,"this is very unlikely and probably indicates an error.\n");
  1482. goto err;
  1483. }
  1484. ret = 1;
  1485. err:
  1486. BN_free(a);
  1487. BN_free(b[0]);
  1488. BN_free(b[1]);
  1489. BN_free(c);
  1490. BN_free(d);
  1491. BN_free(e);
  1492. return ret;
  1493. }
  1494. static int genprime_cb(int p, int n, BN_GENCB *arg)
  1495. {
  1496. char c='*';
  1497. if (p == 0) c='.';
  1498. if (p == 1) c='+';
  1499. if (p == 2) c='*';
  1500. if (p == 3) c='\n';
  1501. putc(c, stderr);
  1502. fflush(stderr);
  1503. return 1;
  1504. }
  1505. int test_kron(BIO *bp, BN_CTX *ctx)
  1506. {
  1507. BN_GENCB cb;
  1508. BIGNUM *a,*b,*r,*t;
  1509. int i;
  1510. int legendre, kronecker;
  1511. int ret = 0;
  1512. a = BN_new();
  1513. b = BN_new();
  1514. r = BN_new();
  1515. t = BN_new();
  1516. if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
  1517. BN_GENCB_set(&cb, genprime_cb, NULL);
  1518. /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol).
  1519. * In this case we know that if b is prime, then BN_kronecker(a, b, ctx)
  1520. * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
  1521. * So we generate a random prime b and compare these values
  1522. * for a number of random a's. (That is, we run the Solovay-Strassen
  1523. * primality test to confirm that b is prime, except that we
  1524. * don't want to test whether b is prime but whether BN_kronecker
  1525. * works.) */
  1526. if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err;
  1527. b->neg = rand_neg();
  1528. putc('\n', stderr);
  1529. for (i = 0; i < num0; i++)
  1530. {
  1531. if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
  1532. a->neg = rand_neg();
  1533. /* t := (|b|-1)/2 (note that b is odd) */
  1534. if (!BN_copy(t, b)) goto err;
  1535. t->neg = 0;
  1536. if (!BN_sub_word(t, 1)) goto err;
  1537. if (!BN_rshift1(t, t)) goto err;
  1538. /* r := a^t mod b */
  1539. b->neg=0;
  1540. if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err;
  1541. b->neg=1;
  1542. if (BN_is_word(r, 1))
  1543. legendre = 1;
  1544. else if (BN_is_zero(r))
  1545. legendre = 0;
  1546. else
  1547. {
  1548. if (!BN_add_word(r, 1)) goto err;
  1549. if (0 != BN_ucmp(r, b))
  1550. {
  1551. fprintf(stderr, "Legendre symbol computation failed\n");
  1552. goto err;
  1553. }
  1554. legendre = -1;
  1555. }
  1556. kronecker = BN_kronecker(a, b, ctx);
  1557. if (kronecker < -1) goto err;
  1558. /* we actually need BN_kronecker(a, |b|) */
  1559. if (a->neg && b->neg)
  1560. kronecker = -kronecker;
  1561. if (legendre != kronecker)
  1562. {
  1563. fprintf(stderr, "legendre != kronecker; a = ");
  1564. BN_print_fp(stderr, a);
  1565. fprintf(stderr, ", b = ");
  1566. BN_print_fp(stderr, b);
  1567. fprintf(stderr, "\n");
  1568. goto err;
  1569. }
  1570. putc('.', stderr);
  1571. fflush(stderr);
  1572. }
  1573. putc('\n', stderr);
  1574. fflush(stderr);
  1575. ret = 1;
  1576. err:
  1577. if (a != NULL) BN_free(a);
  1578. if (b != NULL) BN_free(b);
  1579. if (r != NULL) BN_free(r);
  1580. if (t != NULL) BN_free(t);
  1581. return ret;
  1582. }
  1583. int test_sqrt(BIO *bp, BN_CTX *ctx)
  1584. {
  1585. BN_GENCB cb;
  1586. BIGNUM *a,*p,*r;
  1587. int i, j;
  1588. int ret = 0;
  1589. a = BN_new();
  1590. p = BN_new();
  1591. r = BN_new();
  1592. if (a == NULL || p == NULL || r == NULL) goto err;
  1593. BN_GENCB_set(&cb, genprime_cb, NULL);
  1594. for (i = 0; i < 16; i++)
  1595. {
  1596. if (i < 8)
  1597. {
  1598. unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
  1599. if (!BN_set_word(p, primes[i])) goto err;
  1600. }
  1601. else
  1602. {
  1603. if (!BN_set_word(a, 32)) goto err;
  1604. if (!BN_set_word(r, 2*i + 1)) goto err;
  1605. if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err;
  1606. putc('\n', stderr);
  1607. }
  1608. p->neg = rand_neg();
  1609. for (j = 0; j < num2; j++)
  1610. {
  1611. /* construct 'a' such that it is a square modulo p,
  1612. * but in general not a proper square and not reduced modulo p */
  1613. if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
  1614. if (!BN_nnmod(r, r, p, ctx)) goto err;
  1615. if (!BN_mod_sqr(r, r, p, ctx)) goto err;
  1616. if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
  1617. if (!BN_nnmod(a, a, p, ctx)) goto err;
  1618. if (!BN_mod_sqr(a, a, p, ctx)) goto err;
  1619. if (!BN_mul(a, a, r, ctx)) goto err;
  1620. if (rand_neg())
  1621. if (!BN_sub(a, a, p)) goto err;
  1622. if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
  1623. if (!BN_mod_sqr(r, r, p, ctx)) goto err;
  1624. if (!BN_nnmod(a, a, p, ctx)) goto err;
  1625. if (BN_cmp(a, r) != 0)
  1626. {
  1627. fprintf(stderr, "BN_mod_sqrt failed: a = ");
  1628. BN_print_fp(stderr, a);
  1629. fprintf(stderr, ", r = ");
  1630. BN_print_fp(stderr, r);
  1631. fprintf(stderr, ", p = ");
  1632. BN_print_fp(stderr, p);
  1633. fprintf(stderr, "\n");
  1634. goto err;
  1635. }
  1636. putc('.', stderr);
  1637. fflush(stderr);
  1638. }
  1639. putc('\n', stderr);
  1640. fflush(stderr);
  1641. }
  1642. ret = 1;
  1643. err:
  1644. if (a != NULL) BN_free(a);
  1645. if (p != NULL) BN_free(p);
  1646. if (r != NULL) BN_free(r);
  1647. return ret;
  1648. }
  1649. int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
  1650. {
  1651. BIGNUM *a,*b,*c,*d;
  1652. int i;
  1653. b=BN_new();
  1654. c=BN_new();
  1655. d=BN_new();
  1656. BN_one(c);
  1657. if(a_)
  1658. a=a_;
  1659. else
  1660. {
  1661. a=BN_new();
  1662. BN_bntest_rand(a,200,0,0); /**/
  1663. a->neg=rand_neg();
  1664. }
  1665. for (i=0; i<num0; i++)
  1666. {
  1667. BN_lshift(b,a,i+1);
  1668. BN_add(c,c,c);
  1669. if (bp != NULL)
  1670. {
  1671. if (!results)
  1672. {
  1673. BN_print(bp,a);
  1674. BIO_puts(bp," * ");
  1675. BN_print(bp,c);
  1676. BIO_puts(bp," - ");
  1677. }
  1678. BN_print(bp,b);
  1679. BIO_puts(bp,"\n");
  1680. }
  1681. BN_mul(d,a,c,ctx);
  1682. BN_sub(d,d,b);
  1683. if(!BN_is_zero(d))
  1684. {
  1685. fprintf(stderr,"Left shift test failed!\n");
  1686. fprintf(stderr,"a=");
  1687. BN_print_fp(stderr,a);
  1688. fprintf(stderr,"\nb=");
  1689. BN_print_fp(stderr,b);
  1690. fprintf(stderr,"\nc=");
  1691. BN_print_fp(stderr,c);
  1692. fprintf(stderr,"\nd=");
  1693. BN_print_fp(stderr,d);
  1694. fprintf(stderr,"\n");
  1695. return 0;
  1696. }
  1697. }
  1698. BN_free(a);
  1699. BN_free(b);
  1700. BN_free(c);
  1701. BN_free(d);
  1702. return(1);
  1703. }
  1704. int test_lshift1(BIO *bp)
  1705. {
  1706. BIGNUM *a,*b,*c;
  1707. int i;
  1708. a=BN_new();
  1709. b=BN_new();
  1710. c=BN_new();
  1711. BN_bntest_rand(a,200,0,0); /**/
  1712. a->neg=rand_neg();
  1713. for (i=0; i<num0; i++)
  1714. {
  1715. BN_lshift1(b,a);
  1716. if (bp != NULL)
  1717. {
  1718. if (!results)
  1719. {
  1720. BN_print(bp,a);
  1721. BIO_puts(bp," * 2");
  1722. BIO_puts(bp," - ");
  1723. }
  1724. BN_print(bp,b);
  1725. BIO_puts(bp,"\n");
  1726. }
  1727. BN_add(c,a,a);
  1728. BN_sub(a,b,c);
  1729. if(!BN_is_zero(a))
  1730. {
  1731. fprintf(stderr,"Left shift one test failed!\n");
  1732. return 0;
  1733. }
  1734. BN_copy(a,b);
  1735. }
  1736. BN_free(a);
  1737. BN_free(b);
  1738. BN_free(c);
  1739. return(1);
  1740. }
  1741. int test_rshift(BIO *bp,BN_CTX *ctx)
  1742. {
  1743. BIGNUM *a,*b,*c,*d,*e;
  1744. int i;
  1745. a=BN_new();
  1746. b=BN_new();
  1747. c=BN_new();
  1748. d=BN_new();
  1749. e=BN_new();
  1750. BN_one(c);
  1751. BN_bntest_rand(a,200,0,0); /**/
  1752. a->neg=rand_neg();
  1753. for (i=0; i<num0; i++)
  1754. {
  1755. BN_rshift(b,a,i+1);
  1756. BN_add(c,c,c);
  1757. if (bp != NULL)
  1758. {
  1759. if (!results)
  1760. {
  1761. BN_print(bp,a);
  1762. BIO_puts(bp," / ");
  1763. BN_print(bp,c);
  1764. BIO_puts(bp," - ");
  1765. }
  1766. BN_print(bp,b);
  1767. BIO_puts(bp,"\n");
  1768. }
  1769. BN_div(d,e,a,c,ctx);
  1770. BN_sub(d,d,b);
  1771. if(!BN_is_zero(d))
  1772. {
  1773. fprintf(stderr,"Right shift test failed!\n");
  1774. return 0;
  1775. }
  1776. }
  1777. BN_free(a);
  1778. BN_free(b);
  1779. BN_free(c);
  1780. BN_free(d);
  1781. BN_free(e);
  1782. return(1);
  1783. }
  1784. int test_rshift1(BIO *bp)
  1785. {
  1786. BIGNUM *a,*b,*c;
  1787. int i;
  1788. a=BN_new();
  1789. b=BN_new();
  1790. c=BN_new();
  1791. BN_bntest_rand(a,200,0,0); /**/
  1792. a->neg=rand_neg();
  1793. for (i=0; i<num0; i++)
  1794. {
  1795. BN_rshift1(b,a);
  1796. if (bp != NULL)
  1797. {
  1798. if (!results)
  1799. {
  1800. BN_print(bp,a);
  1801. BIO_puts(bp," / 2");
  1802. BIO_puts(bp," - ");
  1803. }
  1804. BN_print(bp,b);
  1805. BIO_puts(bp,"\n");
  1806. }
  1807. BN_sub(c,a,b);
  1808. BN_sub(c,c,b);
  1809. if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
  1810. {
  1811. fprintf(stderr,"Right shift one test failed!\n");
  1812. return 0;
  1813. }
  1814. BN_copy(a,b);
  1815. }
  1816. BN_free(a);
  1817. BN_free(b);
  1818. BN_free(c);
  1819. return(1);
  1820. }
  1821. int rand_neg(void)
  1822. {
  1823. static unsigned int neg=0;
  1824. static int sign[8]={0,0,0,1,1,0,1,1};
  1825. return(sign[(neg++)%8]);
  1826. }