Created
November 10, 2010 14:43
-
-
Save fanzeyi/670929 to your computer and use it in GitHub Desktop.
高精度加法 来自openssl包
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) | |
{ | |
const BIGNUM *tmp; | |
int a_neg = a->neg, ret; | |
bn_check_top(a); | |
bn_check_top(b); | |
/* a + b a+b | |
* a + -b a-b | |
* -a + b b-a | |
* -a + -b -(a+b) | |
*/ | |
if (a_neg ^ b->neg) | |
{ | |
/* only one is negative */ | |
if (a_neg) | |
{ tmp=a; a=b; b=tmp; } | |
/* we are now a - b */ | |
if (BN_ucmp(a,b) < 0) | |
{ | |
if (!BN_usub(r,b,a)) return(0); | |
r->neg=1; | |
} | |
else | |
{ | |
if (!BN_usub(r,a,b)) return(0); | |
r->neg=0; | |
} | |
return(1); | |
} | |
ret = BN_uadd(r,a,b); | |
r->neg = a_neg; | |
bn_check_top(r); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment