bntest.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  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. BIO_flush(out);
  170. message(out,"BN_sub");
  171. if (!test_sub(out)) goto err;
  172. BIO_flush(out);
  173. message(out,"BN_lshift1");
  174. if (!test_lshift1(out)) goto err;
  175. 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. BIO_flush(out);
  180. message(out,"BN_lshift");
  181. if (!test_lshift(out,ctx,NULL)) goto err;
  182. BIO_flush(out);
  183. message(out,"BN_rshift1");
  184. if (!test_rshift1(out)) goto err;
  185. BIO_flush(out);
  186. message(out,"BN_rshift");
  187. if (!test_rshift(out,ctx)) goto err;
  188. BIO_flush(out);
  189. message(out,"BN_sqr");
  190. if (!test_sqr(out,ctx)) goto err;
  191. BIO_flush(out);
  192. message(out,"BN_mul");
  193. if (!test_mul(out)) goto err;
  194. BIO_flush(out);
  195. message(out,"BN_div");
  196. if (!test_div(out,ctx)) goto err;
  197. BIO_flush(out);
  198. message(out,"BN_div_word");
  199. if (!test_div_word(out)) goto err;
  200. BIO_flush(out);
  201. message(out,"BN_div_recp");
  202. if (!test_div_recp(out,ctx)) goto err;
  203. BIO_flush(out);
  204. message(out,"BN_mod");
  205. if (!test_mod(out,ctx)) goto err;
  206. BIO_flush(out);
  207. message(out,"BN_mod_mul");
  208. if (!test_mod_mul(out,ctx)) goto err;
  209. BIO_flush(out);
  210. message(out,"BN_mont");
  211. if (!test_mont(out,ctx)) goto err;
  212. BIO_flush(out);
  213. message(out,"BN_mod_exp");
  214. if (!test_mod_exp(out,ctx)) goto err;
  215. BIO_flush(out);
  216. message(out,"BN_mod_exp_mont_consttime");
  217. if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
  218. BIO_flush(out);
  219. message(out,"BN_exp");
  220. if (!test_exp(out,ctx)) goto err;
  221. BIO_flush(out);
  222. message(out,"BN_kronecker");
  223. if (!test_kron(out,ctx)) goto err;
  224. BIO_flush(out);
  225. message(out,"BN_mod_sqrt");
  226. if (!test_sqrt(out,ctx)) goto err;
  227. BIO_flush(out);
  228. message(out,"BN_GF2m_add");
  229. if (!test_gf2m_add(out)) goto err;
  230. BIO_flush(out);
  231. message(out,"BN_GF2m_mod");
  232. if (!test_gf2m_mod(out)) goto err;
  233. BIO_flush(out);
  234. message(out,"BN_GF2m_mod_mul");
  235. if (!test_gf2m_mod_mul(out,ctx)) goto err;
  236. BIO_flush(out);
  237. message(out,"BN_GF2m_mod_sqr");
  238. if (!test_gf2m_mod_sqr(out,ctx)) goto err;
  239. BIO_flush(out);
  240. message(out,"BN_GF2m_mod_inv");
  241. if (!test_gf2m_mod_inv(out,ctx)) goto err;
  242. BIO_flush(out);
  243. message(out,"BN_GF2m_mod_div");
  244. if (!test_gf2m_mod_div(out,ctx)) goto err;
  245. BIO_flush(out);
  246. message(out,"BN_GF2m_mod_exp");
  247. if (!test_gf2m_mod_exp(out,ctx)) goto err;
  248. BIO_flush(out);
  249. message(out,"BN_GF2m_mod_sqrt");
  250. if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
  251. BIO_flush(out);
  252. message(out,"BN_GF2m_mod_solve_quad");
  253. if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
  254. 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. 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,BN_HEX_FMT1,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. if (mont == NULL)
  656. return 0;
  657. BN_bntest_rand(&a,100,0,0); /**/
  658. BN_bntest_rand(&b,100,0,0); /**/
  659. for (i=0; i<num2; i++)
  660. {
  661. int bits = (200*(i+1))/num2;
  662. if (bits == 0)
  663. continue;
  664. BN_bntest_rand(&n,bits,0,1);
  665. BN_MONT_CTX_set(mont,&n,ctx);
  666. BN_nnmod(&a,&a,&n,ctx);
  667. BN_nnmod(&b,&b,&n,ctx);
  668. BN_to_montgomery(&A,&a,mont,ctx);
  669. BN_to_montgomery(&B,&b,mont,ctx);
  670. BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
  671. BN_from_montgomery(&A,&c,mont,ctx);/**/
  672. if (bp != NULL)
  673. {
  674. if (!results)
  675. {
  676. #ifdef undef
  677. fprintf(stderr,"%d * %d %% %d\n",
  678. BN_num_bits(&a),
  679. BN_num_bits(&b),
  680. BN_num_bits(mont->N));
  681. #endif
  682. BN_print(bp,&a);
  683. BIO_puts(bp," * ");
  684. BN_print(bp,&b);
  685. BIO_puts(bp," % ");
  686. BN_print(bp,&(mont->N));
  687. BIO_puts(bp," - ");
  688. }
  689. BN_print(bp,&A);
  690. BIO_puts(bp,"\n");
  691. }
  692. BN_mod_mul(&d,&a,&b,&n,ctx);
  693. BN_sub(&d,&d,&A);
  694. if(!BN_is_zero(&d))
  695. {
  696. fprintf(stderr,"Montgomery multiplication test failed!\n");
  697. return 0;
  698. }
  699. }
  700. BN_MONT_CTX_free(mont);
  701. BN_free(&a);
  702. BN_free(&b);
  703. BN_free(&c);
  704. BN_free(&d);
  705. BN_free(&A);
  706. BN_free(&B);
  707. BN_free(&n);
  708. return(1);
  709. }
  710. int test_mod(BIO *bp, BN_CTX *ctx)
  711. {
  712. BIGNUM *a,*b,*c,*d,*e;
  713. int i;
  714. a=BN_new();
  715. b=BN_new();
  716. c=BN_new();
  717. d=BN_new();
  718. e=BN_new();
  719. BN_bntest_rand(a,1024,0,0); /**/
  720. for (i=0; i<num0; i++)
  721. {
  722. BN_bntest_rand(b,450+i*10,0,0); /**/
  723. a->neg=rand_neg();
  724. b->neg=rand_neg();
  725. BN_mod(c,a,b,ctx);/**/
  726. if (bp != NULL)
  727. {
  728. if (!results)
  729. {
  730. BN_print(bp,a);
  731. BIO_puts(bp," % ");
  732. BN_print(bp,b);
  733. BIO_puts(bp," - ");
  734. }
  735. BN_print(bp,c);
  736. BIO_puts(bp,"\n");
  737. }
  738. BN_div(d,e,a,b,ctx);
  739. BN_sub(e,e,c);
  740. if(!BN_is_zero(e))
  741. {
  742. fprintf(stderr,"Modulo test failed!\n");
  743. return 0;
  744. }
  745. }
  746. BN_free(a);
  747. BN_free(b);
  748. BN_free(c);
  749. BN_free(d);
  750. BN_free(e);
  751. return(1);
  752. }
  753. int test_mod_mul(BIO *bp, BN_CTX *ctx)
  754. {
  755. BIGNUM *a,*b,*c,*d,*e;
  756. int i,j;
  757. a=BN_new();
  758. b=BN_new();
  759. c=BN_new();
  760. d=BN_new();
  761. e=BN_new();
  762. for (j=0; j<3; j++) {
  763. BN_bntest_rand(c,1024,0,0); /**/
  764. for (i=0; i<num0; i++)
  765. {
  766. BN_bntest_rand(a,475+i*10,0,0); /**/
  767. BN_bntest_rand(b,425+i*11,0,0); /**/
  768. a->neg=rand_neg();
  769. b->neg=rand_neg();
  770. if (!BN_mod_mul(e,a,b,c,ctx))
  771. {
  772. unsigned long l;
  773. while ((l=ERR_get_error()))
  774. fprintf(stderr,"ERROR:%s\n",
  775. ERR_error_string(l,NULL));
  776. EXIT(1);
  777. }
  778. if (bp != NULL)
  779. {
  780. if (!results)
  781. {
  782. BN_print(bp,a);
  783. BIO_puts(bp," * ");
  784. BN_print(bp,b);
  785. BIO_puts(bp," % ");
  786. BN_print(bp,c);
  787. if ((a->neg ^ b->neg) && !BN_is_zero(e))
  788. {
  789. /* If (a*b) % c is negative, c must be added
  790. * in order to obtain the normalized remainder
  791. * (new with OpenSSL 0.9.7, previous versions of
  792. * BN_mod_mul could generate negative results)
  793. */
  794. BIO_puts(bp," + ");
  795. BN_print(bp,c);
  796. }
  797. BIO_puts(bp," - ");
  798. }
  799. BN_print(bp,e);
  800. BIO_puts(bp,"\n");
  801. }
  802. BN_mul(d,a,b,ctx);
  803. BN_sub(d,d,e);
  804. BN_div(a,b,d,c,ctx);
  805. if(!BN_is_zero(b))
  806. {
  807. fprintf(stderr,"Modulo multiply test failed!\n");
  808. ERR_print_errors_fp(stderr);
  809. return 0;
  810. }
  811. }
  812. }
  813. BN_free(a);
  814. BN_free(b);
  815. BN_free(c);
  816. BN_free(d);
  817. BN_free(e);
  818. return(1);
  819. }
  820. int test_mod_exp(BIO *bp, BN_CTX *ctx)
  821. {
  822. BIGNUM *a,*b,*c,*d,*e;
  823. int i;
  824. a=BN_new();
  825. b=BN_new();
  826. c=BN_new();
  827. d=BN_new();
  828. e=BN_new();
  829. BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
  830. for (i=0; i<num2; i++)
  831. {
  832. BN_bntest_rand(a,20+i*5,0,0); /**/
  833. BN_bntest_rand(b,2+i,0,0); /**/
  834. if (!BN_mod_exp(d,a,b,c,ctx))
  835. return(00);
  836. if (bp != NULL)
  837. {
  838. if (!results)
  839. {
  840. BN_print(bp,a);
  841. BIO_puts(bp," ^ ");
  842. BN_print(bp,b);
  843. BIO_puts(bp," % ");
  844. BN_print(bp,c);
  845. BIO_puts(bp," - ");
  846. }
  847. BN_print(bp,d);
  848. BIO_puts(bp,"\n");
  849. }
  850. BN_exp(e,a,b,ctx);
  851. BN_sub(e,e,d);
  852. BN_div(a,b,e,c,ctx);
  853. if(!BN_is_zero(b))
  854. {
  855. fprintf(stderr,"Modulo exponentiation test failed!\n");
  856. return 0;
  857. }
  858. }
  859. BN_free(a);
  860. BN_free(b);
  861. BN_free(c);
  862. BN_free(d);
  863. BN_free(e);
  864. return(1);
  865. }
  866. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
  867. {
  868. BIGNUM *a,*b,*c,*d,*e;
  869. int i;
  870. a=BN_new();
  871. b=BN_new();
  872. c=BN_new();
  873. d=BN_new();
  874. e=BN_new();
  875. BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
  876. for (i=0; i<num2; i++)
  877. {
  878. BN_bntest_rand(a,20+i*5,0,0); /**/
  879. BN_bntest_rand(b,2+i,0,0); /**/
  880. if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL))
  881. return(00);
  882. if (bp != NULL)
  883. {
  884. if (!results)
  885. {
  886. BN_print(bp,a);
  887. BIO_puts(bp," ^ ");
  888. BN_print(bp,b);
  889. BIO_puts(bp," % ");
  890. BN_print(bp,c);
  891. BIO_puts(bp," - ");
  892. }
  893. BN_print(bp,d);
  894. BIO_puts(bp,"\n");
  895. }
  896. BN_exp(e,a,b,ctx);
  897. BN_sub(e,e,d);
  898. BN_div(a,b,e,c,ctx);
  899. if(!BN_is_zero(b))
  900. {
  901. fprintf(stderr,"Modulo exponentiation test failed!\n");
  902. return 0;
  903. }
  904. }
  905. BN_free(a);
  906. BN_free(b);
  907. BN_free(c);
  908. BN_free(d);
  909. BN_free(e);
  910. return(1);
  911. }
  912. int test_exp(BIO *bp, BN_CTX *ctx)
  913. {
  914. BIGNUM *a,*b,*d,*e,*one;
  915. int i;
  916. a=BN_new();
  917. b=BN_new();
  918. d=BN_new();
  919. e=BN_new();
  920. one=BN_new();
  921. BN_one(one);
  922. for (i=0; i<num2; i++)
  923. {
  924. BN_bntest_rand(a,20+i*5,0,0); /**/
  925. BN_bntest_rand(b,2+i,0,0); /**/
  926. if (!BN_exp(d,a,b,ctx))
  927. return(00);
  928. if (bp != NULL)
  929. {
  930. if (!results)
  931. {
  932. BN_print(bp,a);
  933. BIO_puts(bp," ^ ");
  934. BN_print(bp,b);
  935. BIO_puts(bp," - ");
  936. }
  937. BN_print(bp,d);
  938. BIO_puts(bp,"\n");
  939. }
  940. BN_one(e);
  941. for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
  942. BN_mul(e,e,a,ctx);
  943. BN_sub(e,e,d);
  944. if(!BN_is_zero(e))
  945. {
  946. fprintf(stderr,"Exponentiation test failed!\n");
  947. return 0;
  948. }
  949. }
  950. BN_free(a);
  951. BN_free(b);
  952. BN_free(d);
  953. BN_free(e);
  954. BN_free(one);
  955. return(1);
  956. }
  957. int test_gf2m_add(BIO *bp)
  958. {
  959. BIGNUM a,b,c;
  960. int i, ret = 0;
  961. BN_init(&a);
  962. BN_init(&b);
  963. BN_init(&c);
  964. for (i=0; i<num0; i++)
  965. {
  966. BN_rand(&a,512,0,0);
  967. BN_copy(&b, BN_value_one());
  968. a.neg=rand_neg();
  969. b.neg=rand_neg();
  970. BN_GF2m_add(&c,&a,&b);
  971. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  972. if (bp != NULL)
  973. {
  974. if (!results)
  975. {
  976. BN_print(bp,&a);
  977. BIO_puts(bp," ^ ");
  978. BN_print(bp,&b);
  979. BIO_puts(bp," = ");
  980. }
  981. BN_print(bp,&c);
  982. BIO_puts(bp,"\n");
  983. }
  984. #endif
  985. /* Test that two added values have the correct parity. */
  986. if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c)))
  987. {
  988. fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
  989. goto err;
  990. }
  991. BN_GF2m_add(&c,&c,&c);
  992. /* Test that c + c = 0. */
  993. if(!BN_is_zero(&c))
  994. {
  995. fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
  996. goto err;
  997. }
  998. }
  999. ret = 1;
  1000. err:
  1001. BN_free(&a);
  1002. BN_free(&b);
  1003. BN_free(&c);
  1004. return ret;
  1005. }
  1006. int test_gf2m_mod(BIO *bp)
  1007. {
  1008. BIGNUM *a,*b[2],*c,*d,*e;
  1009. int i, j, ret = 0;
  1010. unsigned int p0[] = {163,7,6,3,0};
  1011. unsigned int p1[] = {193,15,0};
  1012. a=BN_new();
  1013. b[0]=BN_new();
  1014. b[1]=BN_new();
  1015. c=BN_new();
  1016. d=BN_new();
  1017. e=BN_new();
  1018. BN_GF2m_arr2poly(p0, b[0]);
  1019. BN_GF2m_arr2poly(p1, b[1]);
  1020. for (i=0; i<num0; i++)
  1021. {
  1022. BN_bntest_rand(a, 1024, 0, 0);
  1023. for (j=0; j < 2; j++)
  1024. {
  1025. BN_GF2m_mod(c, a, b[j]);
  1026. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1027. if (bp != NULL)
  1028. {
  1029. if (!results)
  1030. {
  1031. BN_print(bp,a);
  1032. BIO_puts(bp," % ");
  1033. BN_print(bp,b[j]);
  1034. BIO_puts(bp," - ");
  1035. BN_print(bp,c);
  1036. BIO_puts(bp,"\n");
  1037. }
  1038. }
  1039. #endif
  1040. BN_GF2m_add(d, a, c);
  1041. BN_GF2m_mod(e, d, b[j]);
  1042. /* Test that a + (a mod p) mod p == 0. */
  1043. if(!BN_is_zero(e))
  1044. {
  1045. fprintf(stderr,"GF(2^m) modulo test failed!\n");
  1046. goto err;
  1047. }
  1048. }
  1049. }
  1050. ret = 1;
  1051. err:
  1052. BN_free(a);
  1053. BN_free(b[0]);
  1054. BN_free(b[1]);
  1055. BN_free(c);
  1056. BN_free(d);
  1057. BN_free(e);
  1058. return ret;
  1059. }
  1060. int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
  1061. {
  1062. BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
  1063. int i, j, ret = 0;
  1064. unsigned int p0[] = {163,7,6,3,0};
  1065. unsigned int p1[] = {193,15,0};
  1066. a=BN_new();
  1067. b[0]=BN_new();
  1068. b[1]=BN_new();
  1069. c=BN_new();
  1070. d=BN_new();
  1071. e=BN_new();
  1072. f=BN_new();
  1073. g=BN_new();
  1074. h=BN_new();
  1075. BN_GF2m_arr2poly(p0, b[0]);
  1076. BN_GF2m_arr2poly(p1, b[1]);
  1077. for (i=0; i<num0; i++)
  1078. {
  1079. BN_bntest_rand(a, 1024, 0, 0);
  1080. BN_bntest_rand(c, 1024, 0, 0);
  1081. BN_bntest_rand(d, 1024, 0, 0);
  1082. for (j=0; j < 2; j++)
  1083. {
  1084. BN_GF2m_mod_mul(e, a, c, b[j], ctx);
  1085. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1086. if (bp != NULL)
  1087. {
  1088. if (!results)
  1089. {
  1090. BN_print(bp,a);
  1091. BIO_puts(bp," * ");
  1092. BN_print(bp,c);
  1093. BIO_puts(bp," % ");
  1094. BN_print(bp,b[j]);
  1095. BIO_puts(bp," - ");
  1096. BN_print(bp,e);
  1097. BIO_puts(bp,"\n");
  1098. }
  1099. }
  1100. #endif
  1101. BN_GF2m_add(f, a, d);
  1102. BN_GF2m_mod_mul(g, f, c, b[j], ctx);
  1103. BN_GF2m_mod_mul(h, d, c, b[j], ctx);
  1104. BN_GF2m_add(f, e, g);
  1105. BN_GF2m_add(f, f, h);
  1106. /* Test that (a+d)*c = a*c + d*c. */
  1107. if(!BN_is_zero(f))
  1108. {
  1109. fprintf(stderr,"GF(2^m) modular multiplication test failed!\n");
  1110. goto err;
  1111. }
  1112. }
  1113. }
  1114. ret = 1;
  1115. err:
  1116. BN_free(a);
  1117. BN_free(b[0]);
  1118. BN_free(b[1]);
  1119. BN_free(c);
  1120. BN_free(d);
  1121. BN_free(e);
  1122. BN_free(f);
  1123. BN_free(g);
  1124. BN_free(h);
  1125. return ret;
  1126. }
  1127. int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
  1128. {
  1129. BIGNUM *a,*b[2],*c,*d;
  1130. int i, j, ret = 0;
  1131. unsigned int p0[] = {163,7,6,3,0};
  1132. unsigned int p1[] = {193,15,0};
  1133. a=BN_new();
  1134. b[0]=BN_new();
  1135. b[1]=BN_new();
  1136. c=BN_new();
  1137. d=BN_new();
  1138. BN_GF2m_arr2poly(p0, b[0]);
  1139. BN_GF2m_arr2poly(p1, b[1]);
  1140. for (i=0; i<num0; i++)
  1141. {
  1142. BN_bntest_rand(a, 1024, 0, 0);
  1143. for (j=0; j < 2; j++)
  1144. {
  1145. BN_GF2m_mod_sqr(c, a, b[j], ctx);
  1146. BN_copy(d, a);
  1147. BN_GF2m_mod_mul(d, a, d, b[j], ctx);
  1148. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1149. if (bp != NULL)
  1150. {
  1151. if (!results)
  1152. {
  1153. BN_print(bp,a);
  1154. BIO_puts(bp," ^ 2 % ");
  1155. BN_print(bp,b[j]);
  1156. BIO_puts(bp, " = ");
  1157. BN_print(bp,c);
  1158. BIO_puts(bp,"; a * a = ");
  1159. BN_print(bp,d);
  1160. BIO_puts(bp,"\n");
  1161. }
  1162. }
  1163. #endif
  1164. BN_GF2m_add(d, c, d);
  1165. /* Test that a*a = a^2. */
  1166. if(!BN_is_zero(d))
  1167. {
  1168. fprintf(stderr,"GF(2^m) modular squaring test failed!\n");
  1169. goto err;
  1170. }
  1171. }
  1172. }
  1173. ret = 1;
  1174. err:
  1175. BN_free(a);
  1176. BN_free(b[0]);
  1177. BN_free(b[1]);
  1178. BN_free(c);
  1179. BN_free(d);
  1180. return ret;
  1181. }
  1182. int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
  1183. {
  1184. BIGNUM *a,*b[2],*c,*d;
  1185. int i, j, ret = 0;
  1186. unsigned int p0[] = {163,7,6,3,0};
  1187. unsigned int p1[] = {193,15,0};
  1188. a=BN_new();
  1189. b[0]=BN_new();
  1190. b[1]=BN_new();
  1191. c=BN_new();
  1192. d=BN_new();
  1193. BN_GF2m_arr2poly(p0, b[0]);
  1194. BN_GF2m_arr2poly(p1, b[1]);
  1195. for (i=0; i<num0; i++)
  1196. {
  1197. BN_bntest_rand(a, 512, 0, 0);
  1198. for (j=0; j < 2; j++)
  1199. {
  1200. BN_GF2m_mod_inv(c, a, b[j], ctx);
  1201. BN_GF2m_mod_mul(d, a, c, b[j], ctx);
  1202. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1203. if (bp != NULL)
  1204. {
  1205. if (!results)
  1206. {
  1207. BN_print(bp,a);
  1208. BIO_puts(bp, " * ");
  1209. BN_print(bp,c);
  1210. BIO_puts(bp," - 1 % ");
  1211. BN_print(bp,b[j]);
  1212. BIO_puts(bp,"\n");
  1213. }
  1214. }
  1215. #endif
  1216. /* Test that ((1/a)*a) = 1. */
  1217. if(!BN_is_one(d))
  1218. {
  1219. fprintf(stderr,"GF(2^m) modular inversion test failed!\n");
  1220. goto err;
  1221. }
  1222. }
  1223. }
  1224. ret = 1;
  1225. err:
  1226. BN_free(a);
  1227. BN_free(b[0]);
  1228. BN_free(b[1]);
  1229. BN_free(c);
  1230. BN_free(d);
  1231. return ret;
  1232. }
  1233. int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
  1234. {
  1235. BIGNUM *a,*b[2],*c,*d,*e,*f;
  1236. int i, j, ret = 0;
  1237. unsigned int p0[] = {163,7,6,3,0};
  1238. unsigned int p1[] = {193,15,0};
  1239. a=BN_new();
  1240. b[0]=BN_new();
  1241. b[1]=BN_new();
  1242. c=BN_new();
  1243. d=BN_new();
  1244. e=BN_new();
  1245. f=BN_new();
  1246. BN_GF2m_arr2poly(p0, b[0]);
  1247. BN_GF2m_arr2poly(p1, b[1]);
  1248. for (i=0; i<num0; i++)
  1249. {
  1250. BN_bntest_rand(a, 512, 0, 0);
  1251. BN_bntest_rand(c, 512, 0, 0);
  1252. for (j=0; j < 2; j++)
  1253. {
  1254. BN_GF2m_mod_div(d, a, c, b[j], ctx);
  1255. BN_GF2m_mod_mul(e, d, c, b[j], ctx);
  1256. BN_GF2m_mod_div(f, a, e, b[j], ctx);
  1257. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1258. if (bp != NULL)
  1259. {
  1260. if (!results)
  1261. {
  1262. BN_print(bp,a);
  1263. BIO_puts(bp, " = ");
  1264. BN_print(bp,c);
  1265. BIO_puts(bp," * ");
  1266. BN_print(bp,d);
  1267. BIO_puts(bp, " % ");
  1268. BN_print(bp,b[j]);
  1269. BIO_puts(bp,"\n");
  1270. }
  1271. }
  1272. #endif
  1273. /* Test that ((a/c)*c)/a = 1. */
  1274. if(!BN_is_one(f))
  1275. {
  1276. fprintf(stderr,"GF(2^m) modular division test failed!\n");
  1277. goto err;
  1278. }
  1279. }
  1280. }
  1281. ret = 1;
  1282. err:
  1283. BN_free(a);
  1284. BN_free(b[0]);
  1285. BN_free(b[1]);
  1286. BN_free(c);
  1287. BN_free(d);
  1288. BN_free(e);
  1289. BN_free(f);
  1290. return ret;
  1291. }
  1292. int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
  1293. {
  1294. BIGNUM *a,*b[2],*c,*d,*e,*f;
  1295. int i, j, ret = 0;
  1296. unsigned int p0[] = {163,7,6,3,0};
  1297. unsigned int p1[] = {193,15,0};
  1298. a=BN_new();
  1299. b[0]=BN_new();
  1300. b[1]=BN_new();
  1301. c=BN_new();
  1302. d=BN_new();
  1303. e=BN_new();
  1304. f=BN_new();
  1305. BN_GF2m_arr2poly(p0, b[0]);
  1306. BN_GF2m_arr2poly(p1, b[1]);
  1307. for (i=0; i<num0; i++)
  1308. {
  1309. BN_bntest_rand(a, 512, 0, 0);
  1310. BN_bntest_rand(c, 512, 0, 0);
  1311. BN_bntest_rand(d, 512, 0, 0);
  1312. for (j=0; j < 2; j++)
  1313. {
  1314. BN_GF2m_mod_exp(e, a, c, b[j], ctx);
  1315. BN_GF2m_mod_exp(f, a, d, b[j], ctx);
  1316. BN_GF2m_mod_mul(e, e, f, b[j], ctx);
  1317. BN_add(f, c, d);
  1318. BN_GF2m_mod_exp(f, a, f, b[j], ctx);
  1319. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1320. if (bp != NULL)
  1321. {
  1322. if (!results)
  1323. {
  1324. BN_print(bp,a);
  1325. BIO_puts(bp, " ^ (");
  1326. BN_print(bp,c);
  1327. BIO_puts(bp," + ");
  1328. BN_print(bp,d);
  1329. BIO_puts(bp, ") = ");
  1330. BN_print(bp,e);
  1331. BIO_puts(bp, "; - ");
  1332. BN_print(bp,f);
  1333. BIO_puts(bp, " % ");
  1334. BN_print(bp,b[j]);
  1335. BIO_puts(bp,"\n");
  1336. }
  1337. }
  1338. #endif
  1339. BN_GF2m_add(f, e, f);
  1340. /* Test that a^(c+d)=a^c*a^d. */
  1341. if(!BN_is_zero(f))
  1342. {
  1343. fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
  1344. goto err;
  1345. }
  1346. }
  1347. }
  1348. ret = 1;
  1349. err:
  1350. BN_free(a);
  1351. BN_free(b[0]);
  1352. BN_free(b[1]);
  1353. BN_free(c);
  1354. BN_free(d);
  1355. BN_free(e);
  1356. BN_free(f);
  1357. return ret;
  1358. }
  1359. int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
  1360. {
  1361. BIGNUM *a,*b[2],*c,*d,*e,*f;
  1362. int i, j, ret = 0;
  1363. unsigned int p0[] = {163,7,6,3,0};
  1364. unsigned int p1[] = {193,15,0};
  1365. a=BN_new();
  1366. b[0]=BN_new();
  1367. b[1]=BN_new();
  1368. c=BN_new();
  1369. d=BN_new();
  1370. e=BN_new();
  1371. f=BN_new();
  1372. BN_GF2m_arr2poly(p0, b[0]);
  1373. BN_GF2m_arr2poly(p1, b[1]);
  1374. for (i=0; i<num0; i++)
  1375. {
  1376. BN_bntest_rand(a, 512, 0, 0);
  1377. for (j=0; j < 2; j++)
  1378. {
  1379. BN_GF2m_mod(c, a, b[j]);
  1380. BN_GF2m_mod_sqrt(d, a, b[j], ctx);
  1381. BN_GF2m_mod_sqr(e, d, b[j], ctx);
  1382. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1383. if (bp != NULL)
  1384. {
  1385. if (!results)
  1386. {
  1387. BN_print(bp,d);
  1388. BIO_puts(bp, " ^ 2 - ");
  1389. BN_print(bp,a);
  1390. BIO_puts(bp,"\n");
  1391. }
  1392. }
  1393. #endif
  1394. BN_GF2m_add(f, c, e);
  1395. /* Test that d^2 = a, where d = sqrt(a). */
  1396. if(!BN_is_zero(f))
  1397. {
  1398. fprintf(stderr,"GF(2^m) modular square root test failed!\n");
  1399. goto err;
  1400. }
  1401. }
  1402. }
  1403. ret = 1;
  1404. err:
  1405. BN_free(a);
  1406. BN_free(b[0]);
  1407. BN_free(b[1]);
  1408. BN_free(c);
  1409. BN_free(d);
  1410. BN_free(e);
  1411. BN_free(f);
  1412. return ret;
  1413. }
  1414. int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
  1415. {
  1416. BIGNUM *a,*b[2],*c,*d,*e;
  1417. int i, j, s = 0, t, ret = 0;
  1418. unsigned int p0[] = {163,7,6,3,0};
  1419. unsigned int p1[] = {193,15,0};
  1420. a=BN_new();
  1421. b[0]=BN_new();
  1422. b[1]=BN_new();
  1423. c=BN_new();
  1424. d=BN_new();
  1425. e=BN_new();
  1426. BN_GF2m_arr2poly(p0, b[0]);
  1427. BN_GF2m_arr2poly(p1, b[1]);
  1428. for (i=0; i<num0; i++)
  1429. {
  1430. BN_bntest_rand(a, 512, 0, 0);
  1431. for (j=0; j < 2; j++)
  1432. {
  1433. t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
  1434. if (t)
  1435. {
  1436. s++;
  1437. BN_GF2m_mod_sqr(d, c, b[j], ctx);
  1438. BN_GF2m_add(d, c, d);
  1439. BN_GF2m_mod(e, a, b[j]);
  1440. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1441. if (bp != NULL)
  1442. {
  1443. if (!results)
  1444. {
  1445. BN_print(bp,c);
  1446. BIO_puts(bp, " is root of z^2 + z = ");
  1447. BN_print(bp,a);
  1448. BIO_puts(bp, " % ");
  1449. BN_print(bp,b[j]);
  1450. BIO_puts(bp, "\n");
  1451. }
  1452. }
  1453. #endif
  1454. BN_GF2m_add(e, e, d);
  1455. /* Test that solution of quadratic c satisfies c^2 + c = a. */
  1456. if(!BN_is_zero(e))
  1457. {
  1458. fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n");
  1459. goto err;
  1460. }
  1461. }
  1462. else
  1463. {
  1464. #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
  1465. if (bp != NULL)
  1466. {
  1467. if (!results)
  1468. {
  1469. BIO_puts(bp, "There are no roots of z^2 + z = ");
  1470. BN_print(bp,a);
  1471. BIO_puts(bp, " % ");
  1472. BN_print(bp,b[j]);
  1473. BIO_puts(bp, "\n");
  1474. }
  1475. }
  1476. #endif
  1477. }
  1478. }
  1479. }
  1480. if (s == 0)
  1481. {
  1482. fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
  1483. fprintf(stderr,"this is very unlikely and probably indicates an error.\n");
  1484. goto err;
  1485. }
  1486. ret = 1;
  1487. err:
  1488. BN_free(a);
  1489. BN_free(b[0]);
  1490. BN_free(b[1]);
  1491. BN_free(c);
  1492. BN_free(d);
  1493. BN_free(e);
  1494. return ret;
  1495. }
  1496. static int genprime_cb(int p, int n, BN_GENCB *arg)
  1497. {
  1498. char c='*';
  1499. if (p == 0) c='.';
  1500. if (p == 1) c='+';
  1501. if (p == 2) c='*';
  1502. if (p == 3) c='\n';
  1503. putc(c, stderr);
  1504. fflush(stderr);
  1505. return 1;
  1506. }
  1507. int test_kron(BIO *bp, BN_CTX *ctx)
  1508. {
  1509. BN_GENCB cb;
  1510. BIGNUM *a,*b,*r,*t;
  1511. int i;
  1512. int legendre, kronecker;
  1513. int ret = 0;
  1514. a = BN_new();
  1515. b = BN_new();
  1516. r = BN_new();
  1517. t = BN_new();
  1518. if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
  1519. BN_GENCB_set(&cb, genprime_cb, NULL);
  1520. /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol).
  1521. * In this case we know that if b is prime, then BN_kronecker(a, b, ctx)
  1522. * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
  1523. * So we generate a random prime b and compare these values
  1524. * for a number of random a's. (That is, we run the Solovay-Strassen
  1525. * primality test to confirm that b is prime, except that we
  1526. * don't want to test whether b is prime but whether BN_kronecker
  1527. * works.) */
  1528. if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err;
  1529. b->neg = rand_neg();
  1530. putc('\n', stderr);
  1531. for (i = 0; i < num0; i++)
  1532. {
  1533. if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
  1534. a->neg = rand_neg();
  1535. /* t := (|b|-1)/2 (note that b is odd) */
  1536. if (!BN_copy(t, b)) goto err;
  1537. t->neg = 0;
  1538. if (!BN_sub_word(t, 1)) goto err;
  1539. if (!BN_rshift1(t, t)) goto err;
  1540. /* r := a^t mod b */
  1541. b->neg=0;
  1542. if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err;
  1543. b->neg=1;
  1544. if (BN_is_word(r, 1))
  1545. legendre = 1;
  1546. else if (BN_is_zero(r))
  1547. legendre = 0;
  1548. else
  1549. {
  1550. if (!BN_add_word(r, 1)) goto err;
  1551. if (0 != BN_ucmp(r, b))
  1552. {
  1553. fprintf(stderr, "Legendre symbol computation failed\n");
  1554. goto err;
  1555. }
  1556. legendre = -1;
  1557. }
  1558. kronecker = BN_kronecker(a, b, ctx);
  1559. if (kronecker < -1) goto err;
  1560. /* we actually need BN_kronecker(a, |b|) */
  1561. if (a->neg && b->neg)
  1562. kronecker = -kronecker;
  1563. if (legendre != kronecker)
  1564. {
  1565. fprintf(stderr, "legendre != kronecker; a = ");
  1566. BN_print_fp(stderr, a);
  1567. fprintf(stderr, ", b = ");
  1568. BN_print_fp(stderr, b);
  1569. fprintf(stderr, "\n");
  1570. goto err;
  1571. }
  1572. putc('.', stderr);
  1573. fflush(stderr);
  1574. }
  1575. putc('\n', stderr);
  1576. fflush(stderr);
  1577. ret = 1;
  1578. err:
  1579. if (a != NULL) BN_free(a);
  1580. if (b != NULL) BN_free(b);
  1581. if (r != NULL) BN_free(r);
  1582. if (t != NULL) BN_free(t);
  1583. return ret;
  1584. }
  1585. int test_sqrt(BIO *bp, BN_CTX *ctx)
  1586. {
  1587. BN_GENCB cb;
  1588. BIGNUM *a,*p,*r;
  1589. int i, j;
  1590. int ret = 0;
  1591. a = BN_new();
  1592. p = BN_new();
  1593. r = BN_new();
  1594. if (a == NULL || p == NULL || r == NULL) goto err;
  1595. BN_GENCB_set(&cb, genprime_cb, NULL);
  1596. for (i = 0; i < 16; i++)
  1597. {
  1598. if (i < 8)
  1599. {
  1600. unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
  1601. if (!BN_set_word(p, primes[i])) goto err;
  1602. }
  1603. else
  1604. {
  1605. if (!BN_set_word(a, 32)) goto err;
  1606. if (!BN_set_word(r, 2*i + 1)) goto err;
  1607. if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err;
  1608. putc('\n', stderr);
  1609. }
  1610. p->neg = rand_neg();
  1611. for (j = 0; j < num2; j++)
  1612. {
  1613. /* construct 'a' such that it is a square modulo p,
  1614. * but in general not a proper square and not reduced modulo p */
  1615. if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
  1616. if (!BN_nnmod(r, r, p, ctx)) goto err;
  1617. if (!BN_mod_sqr(r, r, p, ctx)) goto err;
  1618. if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
  1619. if (!BN_nnmod(a, a, p, ctx)) goto err;
  1620. if (!BN_mod_sqr(a, a, p, ctx)) goto err;
  1621. if (!BN_mul(a, a, r, ctx)) goto err;
  1622. if (rand_neg())
  1623. if (!BN_sub(a, a, p)) goto err;
  1624. if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
  1625. if (!BN_mod_sqr(r, r, p, ctx)) goto err;
  1626. if (!BN_nnmod(a, a, p, ctx)) goto err;
  1627. if (BN_cmp(a, r) != 0)
  1628. {
  1629. fprintf(stderr, "BN_mod_sqrt failed: a = ");
  1630. BN_print_fp(stderr, a);
  1631. fprintf(stderr, ", r = ");
  1632. BN_print_fp(stderr, r);
  1633. fprintf(stderr, ", p = ");
  1634. BN_print_fp(stderr, p);
  1635. fprintf(stderr, "\n");
  1636. goto err;
  1637. }
  1638. putc('.', stderr);
  1639. fflush(stderr);
  1640. }
  1641. putc('\n', stderr);
  1642. fflush(stderr);
  1643. }
  1644. ret = 1;
  1645. err:
  1646. if (a != NULL) BN_free(a);
  1647. if (p != NULL) BN_free(p);
  1648. if (r != NULL) BN_free(r);
  1649. return ret;
  1650. }
  1651. int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
  1652. {
  1653. BIGNUM *a,*b,*c,*d;
  1654. int i;
  1655. b=BN_new();
  1656. c=BN_new();
  1657. d=BN_new();
  1658. BN_one(c);
  1659. if(a_)
  1660. a=a_;
  1661. else
  1662. {
  1663. a=BN_new();
  1664. BN_bntest_rand(a,200,0,0); /**/
  1665. a->neg=rand_neg();
  1666. }
  1667. for (i=0; i<num0; i++)
  1668. {
  1669. BN_lshift(b,a,i+1);
  1670. BN_add(c,c,c);
  1671. if (bp != NULL)
  1672. {
  1673. if (!results)
  1674. {
  1675. BN_print(bp,a);
  1676. BIO_puts(bp," * ");
  1677. BN_print(bp,c);
  1678. BIO_puts(bp," - ");
  1679. }
  1680. BN_print(bp,b);
  1681. BIO_puts(bp,"\n");
  1682. }
  1683. BN_mul(d,a,c,ctx);
  1684. BN_sub(d,d,b);
  1685. if(!BN_is_zero(d))
  1686. {
  1687. fprintf(stderr,"Left shift test failed!\n");
  1688. fprintf(stderr,"a=");
  1689. BN_print_fp(stderr,a);
  1690. fprintf(stderr,"\nb=");
  1691. BN_print_fp(stderr,b);
  1692. fprintf(stderr,"\nc=");
  1693. BN_print_fp(stderr,c);
  1694. fprintf(stderr,"\nd=");
  1695. BN_print_fp(stderr,d);
  1696. fprintf(stderr,"\n");
  1697. return 0;
  1698. }
  1699. }
  1700. BN_free(a);
  1701. BN_free(b);
  1702. BN_free(c);
  1703. BN_free(d);
  1704. return(1);
  1705. }
  1706. int test_lshift1(BIO *bp)
  1707. {
  1708. BIGNUM *a,*b,*c;
  1709. int i;
  1710. a=BN_new();
  1711. b=BN_new();
  1712. c=BN_new();
  1713. BN_bntest_rand(a,200,0,0); /**/
  1714. a->neg=rand_neg();
  1715. for (i=0; i<num0; i++)
  1716. {
  1717. BN_lshift1(b,a);
  1718. if (bp != NULL)
  1719. {
  1720. if (!results)
  1721. {
  1722. BN_print(bp,a);
  1723. BIO_puts(bp," * 2");
  1724. BIO_puts(bp," - ");
  1725. }
  1726. BN_print(bp,b);
  1727. BIO_puts(bp,"\n");
  1728. }
  1729. BN_add(c,a,a);
  1730. BN_sub(a,b,c);
  1731. if(!BN_is_zero(a))
  1732. {
  1733. fprintf(stderr,"Left shift one test failed!\n");
  1734. return 0;
  1735. }
  1736. BN_copy(a,b);
  1737. }
  1738. BN_free(a);
  1739. BN_free(b);
  1740. BN_free(c);
  1741. return(1);
  1742. }
  1743. int test_rshift(BIO *bp,BN_CTX *ctx)
  1744. {
  1745. BIGNUM *a,*b,*c,*d,*e;
  1746. int i;
  1747. a=BN_new();
  1748. b=BN_new();
  1749. c=BN_new();
  1750. d=BN_new();
  1751. e=BN_new();
  1752. BN_one(c);
  1753. BN_bntest_rand(a,200,0,0); /**/
  1754. a->neg=rand_neg();
  1755. for (i=0; i<num0; i++)
  1756. {
  1757. BN_rshift(b,a,i+1);
  1758. BN_add(c,c,c);
  1759. if (bp != NULL)
  1760. {
  1761. if (!results)
  1762. {
  1763. BN_print(bp,a);
  1764. BIO_puts(bp," / ");
  1765. BN_print(bp,c);
  1766. BIO_puts(bp," - ");
  1767. }
  1768. BN_print(bp,b);
  1769. BIO_puts(bp,"\n");
  1770. }
  1771. BN_div(d,e,a,c,ctx);
  1772. BN_sub(d,d,b);
  1773. if(!BN_is_zero(d))
  1774. {
  1775. fprintf(stderr,"Right shift test failed!\n");
  1776. return 0;
  1777. }
  1778. }
  1779. BN_free(a);
  1780. BN_free(b);
  1781. BN_free(c);
  1782. BN_free(d);
  1783. BN_free(e);
  1784. return(1);
  1785. }
  1786. int test_rshift1(BIO *bp)
  1787. {
  1788. BIGNUM *a,*b,*c;
  1789. int i;
  1790. a=BN_new();
  1791. b=BN_new();
  1792. c=BN_new();
  1793. BN_bntest_rand(a,200,0,0); /**/
  1794. a->neg=rand_neg();
  1795. for (i=0; i<num0; i++)
  1796. {
  1797. BN_rshift1(b,a);
  1798. if (bp != NULL)
  1799. {
  1800. if (!results)
  1801. {
  1802. BN_print(bp,a);
  1803. BIO_puts(bp," / 2");
  1804. BIO_puts(bp," - ");
  1805. }
  1806. BN_print(bp,b);
  1807. BIO_puts(bp,"\n");
  1808. }
  1809. BN_sub(c,a,b);
  1810. BN_sub(c,c,b);
  1811. if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
  1812. {
  1813. fprintf(stderr,"Right shift one test failed!\n");
  1814. return 0;
  1815. }
  1816. BN_copy(a,b);
  1817. }
  1818. BN_free(a);
  1819. BN_free(b);
  1820. BN_free(c);
  1821. return(1);
  1822. }
  1823. int rand_neg(void)
  1824. {
  1825. static unsigned int neg=0;
  1826. static int sign[8]={0,0,0,1,1,0,1,1};
  1827. return(sign[(neg++)%8]);
  1828. }