mpasbn.c (160840) | mpasbn.c (189092) |
---|---|
1/* 2 * Copyright (c) 2001 Dima Dorfman. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 56 unchanged lines hidden (view full) --- 65 * and not in mp.h) have an underscore prepended to their name (this 66 * is more for aesthetical reasons than technical). All such 67 * routines take an extra argument, 'msg', that denotes what they 68 * should call themselves in an error message. This is so a user 69 * doesn't get an error message from a function they didn't call. 70 */ 71 72#include <sys/cdefs.h> | 1/* 2 * Copyright (c) 2001 Dima Dorfman. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 56 unchanged lines hidden (view full) --- 65 * and not in mp.h) have an underscore prepended to their name (this 66 * is more for aesthetical reasons than technical). All such 67 * routines take an extra argument, 'msg', that denotes what they 68 * should call themselves in an error message. This is so a user 69 * doesn't get an error message from a function they didn't call. 70 */ 71 72#include <sys/cdefs.h> |
73__FBSDID("$FreeBSD: head/lib/libmp/mpasbn.c 160840 2006-07-30 19:29:26Z simon $"); | 73__FBSDID("$FreeBSD: head/lib/libmp/mpasbn.c 189092 2009-02-26 21:43:15Z ed $"); |
74 75#include <ctype.h> 76#include <err.h> 77#include <errno.h> 78#include <stdio.h> 79#include <stdlib.h> 80#include <string.h> 81 --- 53 unchanged lines hidden (view full) --- 135 BN_ERRCHECK(msg, BN_dec2bn(&mp->bn, s)); 136 return (mp); 137} 138 139/* 140 * Compute the greatest common divisor of mp1 and mp2; result goes in rmp. 141 */ 142void | 74 75#include <ctype.h> 76#include <err.h> 77#include <errno.h> 78#include <stdio.h> 79#include <stdlib.h> 80#include <string.h> 81 --- 53 unchanged lines hidden (view full) --- 135 BN_ERRCHECK(msg, BN_dec2bn(&mp->bn, s)); 136 return (mp); 137} 138 139/* 140 * Compute the greatest common divisor of mp1 and mp2; result goes in rmp. 141 */ 142void |
143gcd(const MINT *mp1, const MINT *mp2, MINT *rmp) | 143mp_gcd(const MINT *mp1, const MINT *mp2, MINT *rmp) |
144{ 145 BIGNUM b; 146 BN_CTX *c; 147 148 c = BN_CTX_new(); 149 if (c == NULL) 150 _bnerr("gcd"); 151 BN_init(&b); --- 16 unchanged lines hidden (view full) --- 168 if (s == NULL) 169 MPERR(("%s", msg)); 170 mp = _xtom(msg, s); 171 free(s); 172 return (mp); 173} 174 175MINT * | 144{ 145 BIGNUM b; 146 BN_CTX *c; 147 148 c = BN_CTX_new(); 149 if (c == NULL) 150 _bnerr("gcd"); 151 BN_init(&b); --- 16 unchanged lines hidden (view full) --- 168 if (s == NULL) 169 MPERR(("%s", msg)); 170 mp = _xtom(msg, s); 171 free(s); 172 return (mp); 173} 174 175MINT * |
176itom(short n) | 176mp_itom(short n) |
177{ 178 179 return (_itom("itom", n)); 180} 181 182/* 183 * Compute rmp=mp1+mp2. 184 */ --- 4 unchanged lines hidden (view full) --- 189 190 BN_init(&b); 191 BN_ERRCHECK(msg, BN_add(&b, mp1->bn, mp2->bn)); 192 _moveb(msg, &b, rmp); 193 BN_free(&b); 194} 195 196void | 177{ 178 179 return (_itom("itom", n)); 180} 181 182/* 183 * Compute rmp=mp1+mp2. 184 */ --- 4 unchanged lines hidden (view full) --- 189 190 BN_init(&b); 191 BN_ERRCHECK(msg, BN_add(&b, mp1->bn, mp2->bn)); 192 _moveb(msg, &b, rmp); 193 BN_free(&b); 194} 195 196void |
197madd(const MINT *mp1, const MINT *mp2, MINT *rmp) | 197mp_madd(const MINT *mp1, const MINT *mp2, MINT *rmp) |
198{ 199 200 _madd("madd", mp1, mp2, rmp); 201} 202 203/* 204 * Return -1, 0, or 1 if mp1<mp2, mp1==mp2, or mp1>mp2, respectivley. 205 */ 206int | 198{ 199 200 _madd("madd", mp1, mp2, rmp); 201} 202 203/* 204 * Return -1, 0, or 1 if mp1<mp2, mp1==mp2, or mp1>mp2, respectivley. 205 */ 206int |
207mcmp(const MINT *mp1, const MINT *mp2) | 207mp_mcmp(const MINT *mp1, const MINT *mp2) |
208{ 209 210 return (BN_cmp(mp1->bn, mp2->bn)); 211} 212 213/* 214 * Same as mcmp but compares absolute values. 215 */ --- 18 unchanged lines hidden (view full) --- 234 BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c)); 235 _moveb(msg, &q, qmp); 236 _moveb(msg, &r, rmp); 237 BN_free(&q); 238 BN_free(&r); 239} 240 241void | 208{ 209 210 return (BN_cmp(mp1->bn, mp2->bn)); 211} 212 213/* 214 * Same as mcmp but compares absolute values. 215 */ --- 18 unchanged lines hidden (view full) --- 234 BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c)); 235 _moveb(msg, &q, qmp); 236 _moveb(msg, &r, rmp); 237 BN_free(&q); 238 BN_free(&r); 239} 240 241void |
242mdiv(const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp) | 242mp_mdiv(const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp) |
243{ 244 BN_CTX *c; 245 246 c = BN_CTX_new(); 247 if (c == NULL) 248 _bnerr("mdiv"); 249 _mdiv("mdiv", nmp, dmp, qmp, rmp, c); 250 BN_CTX_free(c); --- 7 unchanged lines hidden (view full) --- 258{ 259 260 BN_clear(mp->bn); 261 BN_free(mp->bn); 262 free(mp); 263} 264 265void | 243{ 244 BN_CTX *c; 245 246 c = BN_CTX_new(); 247 if (c == NULL) 248 _bnerr("mdiv"); 249 _mdiv("mdiv", nmp, dmp, qmp, rmp, c); 250 BN_CTX_free(c); --- 7 unchanged lines hidden (view full) --- 258{ 259 260 BN_clear(mp->bn); 261 BN_free(mp->bn); 262 free(mp); 263} 264 265void |
266mfree(MINT *mp) | 266mp_mfree(MINT *mp) |
267{ 268 269 _mfree("mfree", mp); 270} 271 272/* 273 * Read an integer from standard input and stick the result in mp. 274 * The input is treated to be in base 10. This must be the silliest 275 * API in existence; why can't the program read in a string and call 276 * xtom()? (Or if base 10 is desires, perhaps dtom() could be 277 * exported.) 278 */ 279void | 267{ 268 269 _mfree("mfree", mp); 270} 271 272/* 273 * Read an integer from standard input and stick the result in mp. 274 * The input is treated to be in base 10. This must be the silliest 275 * API in existence; why can't the program read in a string and call 276 * xtom()? (Or if base 10 is desires, perhaps dtom() could be 277 * exported.) 278 */ 279void |
280min(MINT *mp) | 280mp_min(MINT *mp) |
281{ 282 MINT *rmp; 283 char *line, *nline; 284 size_t linelen; 285 286 line = fgetln(stdin, &linelen); 287 if (line == NULL) 288 MPERR(("min")); --- 8 unchanged lines hidden (view full) --- 297 free(nline); 298} 299 300/* 301 * Print the value of mp to standard output in base 10. See blurb 302 * above min() for why this is so useless. 303 */ 304void | 281{ 282 MINT *rmp; 283 char *line, *nline; 284 size_t linelen; 285 286 line = fgetln(stdin, &linelen); 287 if (line == NULL) 288 MPERR(("min")); --- 8 unchanged lines hidden (view full) --- 297 free(nline); 298} 299 300/* 301 * Print the value of mp to standard output in base 10. See blurb 302 * above min() for why this is so useless. 303 */ 304void |
305mout(const MINT *mp) | 305mp_mout(const MINT *mp) |
306{ 307 char *s; 308 309 s = _mtod("mout", mp); 310 printf("%s", s); 311 free(s); 312} 313 314/* 315 * Set the value of tmp to the value of smp (i.e., tmp=smp). 316 */ 317void | 306{ 307 char *s; 308 309 s = _mtod("mout", mp); 310 printf("%s", s); 311 free(s); 312} 313 314/* 315 * Set the value of tmp to the value of smp (i.e., tmp=smp). 316 */ 317void |
318move(const MINT *smp, MINT *tmp) | 318mp_move(const MINT *smp, MINT *tmp) |
319{ 320 321 _movem("move", smp, tmp); 322} 323 324 325/* 326 * Internal routine to set the value of tmp to that of sbp. --- 25 unchanged lines hidden (view full) --- 352 * 353 * x = (x + (n / x)) / 2 354 * 355 * where x is the square root of the positive number n. In the 356 * beginning, x should be a reasonable guess, but the value 1, 357 * although suboptimal, works, too; this is that is used below. 358 */ 359void | 319{ 320 321 _movem("move", smp, tmp); 322} 323 324 325/* 326 * Internal routine to set the value of tmp to that of sbp. --- 25 unchanged lines hidden (view full) --- 352 * 353 * x = (x + (n / x)) / 2 354 * 355 * where x is the square root of the positive number n. In the 356 * beginning, x should be a reasonable guess, but the value 1, 357 * although suboptimal, works, too; this is that is used below. 358 */ 359void |
360msqrt(const MINT *nmp, MINT *xmp, MINT *rmp) | 360mp_msqrt(const MINT *nmp, MINT *xmp, MINT *rmp) |
361{ 362 BN_CTX *c; 363 MINT *tolerance; 364 MINT *ox, *x; 365 MINT *z1, *z2, *z3; 366 short i; 367 368 c = BN_CTX_new(); --- 35 unchanged lines hidden (view full) --- 404 405 BN_init(&b); 406 BN_ERRCHECK(msg, BN_sub(&b, mp1->bn, mp2->bn)); 407 _moveb(msg, &b, rmp); 408 BN_free(&b); 409} 410 411void | 361{ 362 BN_CTX *c; 363 MINT *tolerance; 364 MINT *ox, *x; 365 MINT *z1, *z2, *z3; 366 short i; 367 368 c = BN_CTX_new(); --- 35 unchanged lines hidden (view full) --- 404 405 BN_init(&b); 406 BN_ERRCHECK(msg, BN_sub(&b, mp1->bn, mp2->bn)); 407 _moveb(msg, &b, rmp); 408 BN_free(&b); 409} 410 411void |
412msub(const MINT *mp1, const MINT *mp2, MINT *rmp) | 412mp_msub(const MINT *mp1, const MINT *mp2, MINT *rmp) |
413{ 414 415 _msub("msub", mp1, mp2, rmp); 416} 417 418/* 419 * Return a decimal representation of mp. Return value must be 420 * free()'d. --- 41 unchanged lines hidden (view full) --- 462 len = strlen(s2); 463 for (p = s2; p < s2 + len; p++) 464 *p = tolower(*p); 465 466 return (s2); 467} 468 469char * | 413{ 414 415 _msub("msub", mp1, mp2, rmp); 416} 417 418/* 419 * Return a decimal representation of mp. Return value must be 420 * free()'d. --- 41 unchanged lines hidden (view full) --- 462 len = strlen(s2); 463 for (p = s2; p < s2 + len; p++) 464 *p = tolower(*p); 465 466 return (s2); 467} 468 469char * |
470mtox(const MINT *mp) | 470mp_mtox(const MINT *mp) |
471{ 472 473 return (_mtox("mtox", mp)); 474} 475 476/* 477 * Compute rmp=mp1*mp2. 478 */ --- 4 unchanged lines hidden (view full) --- 483 484 BN_init(&b); 485 BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, c)); 486 _moveb(msg, &b, rmp); 487 BN_free(&b); 488} 489 490void | 471{ 472 473 return (_mtox("mtox", mp)); 474} 475 476/* 477 * Compute rmp=mp1*mp2. 478 */ --- 4 unchanged lines hidden (view full) --- 483 484 BN_init(&b); 485 BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, c)); 486 _moveb(msg, &b, rmp); 487 BN_free(&b); 488} 489 490void |
491mult(const MINT *mp1, const MINT *mp2, MINT *rmp) | 491mp_mult(const MINT *mp1, const MINT *mp2, MINT *rmp) |
492{ 493 BN_CTX *c; 494 495 c = BN_CTX_new(); 496 if (c == NULL) 497 _bnerr("mult"); 498 _mult("mult", mp1, mp2, rmp, c); 499 BN_CTX_free(c); 500} 501 502/* 503 * Compute rmp=(bmp^emp)mod mmp. (Note that here and above rpow() '^' 504 * means 'raise to power', not 'bitwise XOR'.) 505 */ 506void | 492{ 493 BN_CTX *c; 494 495 c = BN_CTX_new(); 496 if (c == NULL) 497 _bnerr("mult"); 498 _mult("mult", mp1, mp2, rmp, c); 499 BN_CTX_free(c); 500} 501 502/* 503 * Compute rmp=(bmp^emp)mod mmp. (Note that here and above rpow() '^' 504 * means 'raise to power', not 'bitwise XOR'.) 505 */ 506void |
507pow(const MINT *bmp, const MINT *emp, const MINT *mmp, MINT *rmp) | 507mp_pow(const MINT *bmp, const MINT *emp, const MINT *mmp, MINT *rmp) |
508{ 509 BIGNUM b; 510 BN_CTX *c; 511 512 c = BN_CTX_new(); 513 if (c == NULL) 514 _bnerr("pow"); 515 BN_init(&b); 516 BN_ERRCHECK("pow", BN_mod_exp(&b, bmp->bn, emp->bn, mmp->bn, c)); 517 _moveb("pow", &b, rmp); 518 BN_free(&b); 519 BN_CTX_free(c); 520} 521 522/* 523 * Compute rmp=bmp^e. (See note above pow().) 524 */ 525void | 508{ 509 BIGNUM b; 510 BN_CTX *c; 511 512 c = BN_CTX_new(); 513 if (c == NULL) 514 _bnerr("pow"); 515 BN_init(&b); 516 BN_ERRCHECK("pow", BN_mod_exp(&b, bmp->bn, emp->bn, mmp->bn, c)); 517 _moveb("pow", &b, rmp); 518 BN_free(&b); 519 BN_CTX_free(c); 520} 521 522/* 523 * Compute rmp=bmp^e. (See note above pow().) 524 */ 525void |
526rpow(const MINT *bmp, short e, MINT *rmp) | 526mp_rpow(const MINT *bmp, short e, MINT *rmp) |
527{ 528 MINT *emp; 529 BIGNUM b; 530 BN_CTX *c; 531 532 c = BN_CTX_new(); 533 if (c == NULL) 534 _bnerr("rpow"); --- 32 unchanged lines hidden (view full) --- 567 free(s); 568 _mfree(msg, dmp); 569 _mfree(msg, rmp); 570 BN_free(&r); 571 BN_free(&q); 572} 573 574void | 527{ 528 MINT *emp; 529 BIGNUM b; 530 BN_CTX *c; 531 532 c = BN_CTX_new(); 533 if (c == NULL) 534 _bnerr("rpow"); --- 32 unchanged lines hidden (view full) --- 567 free(s); 568 _mfree(msg, dmp); 569 _mfree(msg, rmp); 570 BN_free(&r); 571 BN_free(&q); 572} 573 574void |
575sdiv(const MINT *nmp, short d, MINT *qmp, short *ro) | 575mp_sdiv(const MINT *nmp, short d, MINT *qmp, short *ro) |
576{ 577 BN_CTX *c; 578 579 c = BN_CTX_new(); 580 if (c == NULL) 581 _bnerr("sdiv"); 582 _sdiv("sdiv", nmp, d, qmp, ro, c); 583 BN_CTX_free(c); --- 13 unchanged lines hidden (view full) --- 597 mp->bn = BN_new(); 598 if (mp->bn == NULL) 599 _bnerr(msg); 600 BN_ERRCHECK(msg, BN_hex2bn(&mp->bn, s)); 601 return (mp); 602} 603 604MINT * | 576{ 577 BN_CTX *c; 578 579 c = BN_CTX_new(); 580 if (c == NULL) 581 _bnerr("sdiv"); 582 _sdiv("sdiv", nmp, d, qmp, ro, c); 583 BN_CTX_free(c); --- 13 unchanged lines hidden (view full) --- 597 mp->bn = BN_new(); 598 if (mp->bn == NULL) 599 _bnerr(msg); 600 BN_ERRCHECK(msg, BN_hex2bn(&mp->bn, s)); 601 return (mp); 602} 603 604MINT * |
605xtom(const char *s) | 605mp_xtom(const char *s) |
606{ 607 608 return (_xtom("xtom", s)); 609} | 606{ 607 608 return (_xtom("xtom", s)); 609} |