Skip to content

Instantly share code, notes, and snippets.

@fanzeyi
Created November 10, 2010 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanzeyi/670929 to your computer and use it in GitHub Desktop.
Save fanzeyi/670929 to your computer and use it in GitHub Desktop.
高精度加法 来自openssl包
​​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