format with astyle (step 3)
This commit is contained in:
parent
9eed07f09b
commit
4439fae168
30
bn_mp_init.c
30
bn_mp_init.c
@ -18,26 +18,26 @@
|
|||||||
/* init a new mp_int */
|
/* init a new mp_int */
|
||||||
int mp_init(mp_int *a)
|
int mp_init(mp_int *a)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* allocate memory required and clear it */
|
/* allocate memory required and clear it */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof (mp_digit) * MP_PREC);
|
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * MP_PREC);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set the digits to zero */
|
/* set the digits to zero */
|
||||||
for (i = 0; i < MP_PREC; i++) {
|
for (i = 0; i < MP_PREC; i++) {
|
||||||
a->dp[i] = 0;
|
a->dp[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set the used to zero, allocated digits to the default precision
|
/* set the used to zero, allocated digits to the default precision
|
||||||
* and sign to positive */
|
* and sign to positive */
|
||||||
a->used = 0;
|
a->used = 0;
|
||||||
a->alloc = MP_PREC;
|
a->alloc = MP_PREC;
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,17 +18,17 @@
|
|||||||
/* creates "a" then copies b into it */
|
/* creates "a" then copies b into it */
|
||||||
int mp_init_copy(mp_int *a, mp_int *b)
|
int mp_init_copy(mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
if ((res = mp_init_size(a, b->used)) != MP_OKAY) {
|
if ((res = mp_init_size(a, b->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_copy(b, a)) != MP_OKAY) {
|
if ((res = mp_copy(b, a)) != MP_OKAY) {
|
||||||
mp_clear(a);
|
mp_clear(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,35 +18,35 @@
|
|||||||
|
|
||||||
int mp_init_multi(mp_int *mp, ...)
|
int mp_init_multi(mp_int *mp, ...)
|
||||||
{
|
{
|
||||||
mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
|
mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
|
||||||
int n = 0; /* Number of ok inits */
|
int n = 0; /* Number of ok inits */
|
||||||
mp_int* cur_arg = mp;
|
mp_int *cur_arg = mp;
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
va_start(args, mp); /* init args to next argument from caller */
|
va_start(args, mp); /* init args to next argument from caller */
|
||||||
while (cur_arg != NULL) {
|
while (cur_arg != NULL) {
|
||||||
if (mp_init(cur_arg) != MP_OKAY) {
|
if (mp_init(cur_arg) != MP_OKAY) {
|
||||||
/* Oops - error! Back-track and mp_clear what we already
|
/* Oops - error! Back-track and mp_clear what we already
|
||||||
succeeded in init-ing, then return error.
|
succeeded in init-ing, then return error.
|
||||||
*/
|
*/
|
||||||
va_list clean_args;
|
va_list clean_args;
|
||||||
|
|
||||||
/* now start cleaning up */
|
/* now start cleaning up */
|
||||||
cur_arg = mp;
|
cur_arg = mp;
|
||||||
va_start(clean_args, mp);
|
va_start(clean_args, mp);
|
||||||
while (n-- != 0) {
|
while (n-- != 0) {
|
||||||
mp_clear(cur_arg);
|
mp_clear(cur_arg);
|
||||||
cur_arg = va_arg(clean_args, mp_int*);
|
cur_arg = va_arg(clean_args, mp_int *);
|
||||||
}
|
}
|
||||||
va_end(clean_args);
|
va_end(clean_args);
|
||||||
res = MP_MEM;
|
res = MP_MEM;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
n++;
|
n++;
|
||||||
cur_arg = va_arg(args, mp_int*);
|
cur_arg = va_arg(args, mp_int *);
|
||||||
}
|
}
|
||||||
va_end(args);
|
va_end(args);
|
||||||
return res; /* Assumed ok, if error flagged above. */
|
return res; /* Assumed ok, if error flagged above. */
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -18,12 +18,12 @@
|
|||||||
/* initialize and set a digit */
|
/* initialize and set a digit */
|
||||||
int mp_init_set(mp_int *a, mp_digit b)
|
int mp_init_set(mp_int *a, mp_digit b)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
if ((err = mp_init(a)) != MP_OKAY) {
|
if ((err = mp_init(a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
mp_set(a, b);
|
mp_set(a, b);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,11 +18,11 @@
|
|||||||
/* initialize and set a digit */
|
/* initialize and set a digit */
|
||||||
int mp_init_set_int(mp_int *a, unsigned long b)
|
int mp_init_set_int(mp_int *a, unsigned long b)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
if ((err = mp_init(a)) != MP_OKAY) {
|
if ((err = mp_init(a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
return mp_set_int(a, b);
|
return mp_set_int(a, b);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,28 +18,28 @@
|
|||||||
/* init an mp_init for a given size */
|
/* init an mp_init for a given size */
|
||||||
int mp_init_size(mp_int *a, int size)
|
int mp_init_size(mp_int *a, int size)
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
/* pad size so there are always extra digits */
|
/* pad size so there are always extra digits */
|
||||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||||
|
|
||||||
/* alloc mem */
|
/* alloc mem */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof (mp_digit) * size);
|
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * size);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set the members */
|
/* set the members */
|
||||||
a->used = 0;
|
a->used = 0;
|
||||||
a->alloc = size;
|
a->alloc = size;
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
|
|
||||||
/* zero the digits */
|
/* zero the digits */
|
||||||
for (x = 0; x < size; x++) {
|
for (x = 0; x < size; x++) {
|
||||||
a->dp[x] = 0;
|
a->dp[x] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,22 +18,22 @@
|
|||||||
/* hac 14.61, pp608 */
|
/* hac 14.61, pp608 */
|
||||||
int mp_invmod(mp_int *a, mp_int *b, mp_int *c)
|
int mp_invmod(mp_int *a, mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
/* b cannot be negative */
|
/* b cannot be negative */
|
||||||
if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
|
if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BN_FAST_MP_INVMOD_C
|
#ifdef BN_FAST_MP_INVMOD_C
|
||||||
/* if the modulus is odd we can use a faster routine instead */
|
/* if the modulus is odd we can use a faster routine instead */
|
||||||
if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) {
|
if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) {
|
||||||
return fast_mp_invmod(a, b, c);
|
return fast_mp_invmod(a, b, c);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef BN_MP_INVMOD_SLOW_C
|
#ifdef BN_MP_INVMOD_SLOW_C
|
||||||
return mp_invmod_slow(a, b, c);
|
return mp_invmod_slow(a, b, c);
|
||||||
#else
|
#else
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -17,91 +17,91 @@
|
|||||||
|
|
||||||
/* Check if remainders are possible squares - fast exclude non-squares */
|
/* Check if remainders are possible squares - fast exclude non-squares */
|
||||||
static const char rem_128[128] = {
|
static const char rem_128[128] = {
|
||||||
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
||||||
0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
||||||
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
||||||
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
||||||
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
||||||
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
||||||
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
|
||||||
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1
|
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char rem_105[105] = {
|
static const char rem_105[105] = {
|
||||||
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
|
0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
|
||||||
0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
|
0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
|
||||||
0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
|
0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
|
||||||
1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
|
1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
|
0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
|
||||||
1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1,
|
1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1,
|
||||||
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1
|
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Store non-zero to ret if arg is square, and zero if not */
|
/* Store non-zero to ret if arg is square, and zero if not */
|
||||||
int mp_is_square(mp_int *arg, int *ret)
|
int mp_is_square(mp_int *arg, int *ret)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_digit c;
|
mp_digit c;
|
||||||
mp_int t;
|
mp_int t;
|
||||||
unsigned long r;
|
unsigned long r;
|
||||||
|
|
||||||
/* Default to Non-square :) */
|
/* Default to Non-square :) */
|
||||||
*ret = MP_NO;
|
*ret = MP_NO;
|
||||||
|
|
||||||
if (arg->sign == MP_NEG) {
|
if (arg->sign == MP_NEG) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* digits used? (TSD) */
|
/* digits used? (TSD) */
|
||||||
if (arg->used == 0) {
|
if (arg->used == 0) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
|
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
|
||||||
if (rem_128[127 & DIGIT(arg, 0)] == 1) {
|
if (rem_128[127 & DIGIT(arg, 0)] == 1) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Next check mod 105 (3*5*7) */
|
/* Next check mod 105 (3*5*7) */
|
||||||
if ((res = mp_mod_d(arg, 105, &c)) != MP_OKAY) {
|
if ((res = mp_mod_d(arg, 105, &c)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
if (rem_105[c] == 1) {
|
if (rem_105[c] == 1) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ((res = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
|
if ((res = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
|
if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
r = mp_get_int(&t);
|
r = mp_get_int(&t);
|
||||||
/* Check for other prime modules, note it's not an ERROR but we must
|
/* Check for other prime modules, note it's not an ERROR but we must
|
||||||
* free "t" so the easiest way is to goto ERR. We know that res
|
* free "t" so the easiest way is to goto ERR. We know that res
|
||||||
* is already equal to MP_OKAY from the mp_mod call
|
* is already equal to MP_OKAY from the mp_mod call
|
||||||
*/
|
*/
|
||||||
if (((1L<<(r%11)) & 0x5C4L) != 0L) goto ERR;
|
if (((1L<<(r%11)) & 0x5C4L) != 0L) goto ERR;
|
||||||
if (((1L<<(r%13)) & 0x9E4L) != 0L) goto ERR;
|
if (((1L<<(r%13)) & 0x9E4L) != 0L) goto ERR;
|
||||||
if (((1L<<(r%17)) & 0x5CE8L) != 0L) goto ERR;
|
if (((1L<<(r%17)) & 0x5CE8L) != 0L) goto ERR;
|
||||||
if (((1L<<(r%19)) & 0x4F50CL) != 0L) goto ERR;
|
if (((1L<<(r%19)) & 0x4F50CL) != 0L) goto ERR;
|
||||||
if (((1L<<(r%23)) & 0x7ACCA0L) != 0L) goto ERR;
|
if (((1L<<(r%23)) & 0x7ACCA0L) != 0L) goto ERR;
|
||||||
if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L) goto ERR;
|
if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L) goto ERR;
|
||||||
if (((1L<<(r%31)) & 0x6DE2B848L) != 0L) goto ERR;
|
if (((1L<<(r%31)) & 0x6DE2B848L) != 0L) goto ERR;
|
||||||
|
|
||||||
/* Final check - is sqr(sqrt(arg)) == arg ? */
|
/* Final check - is sqr(sqrt(arg)) == arg ? */
|
||||||
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
|
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
|
if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
|
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
|
||||||
ERR:
|
ERR:
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -46,126 +46,126 @@
|
|||||||
*/
|
*/
|
||||||
int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c)
|
int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
|
mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
|
||||||
int B, err;
|
int B, err;
|
||||||
|
|
||||||
/* default the return code to an error */
|
/* default the return code to an error */
|
||||||
err = MP_MEM;
|
err = MP_MEM;
|
||||||
|
|
||||||
/* min # of digits */
|
/* min # of digits */
|
||||||
B = MIN(a->used, b->used);
|
B = MIN(a->used, b->used);
|
||||||
|
|
||||||
/* now divide in two */
|
/* now divide in two */
|
||||||
B = B >> 1;
|
B = B >> 1;
|
||||||
|
|
||||||
/* init copy all the temps */
|
/* init copy all the temps */
|
||||||
if (mp_init_size(&x0, B) != MP_OKAY)
|
if (mp_init_size(&x0, B) != MP_OKAY)
|
||||||
goto ERR;
|
goto ERR;
|
||||||
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
||||||
goto X0;
|
goto X0;
|
||||||
if (mp_init_size(&y0, B) != MP_OKAY)
|
if (mp_init_size(&y0, B) != MP_OKAY)
|
||||||
goto X1;
|
goto X1;
|
||||||
if (mp_init_size(&y1, b->used - B) != MP_OKAY)
|
if (mp_init_size(&y1, b->used - B) != MP_OKAY)
|
||||||
goto Y0;
|
goto Y0;
|
||||||
|
|
||||||
/* init temps */
|
/* init temps */
|
||||||
if (mp_init_size(&t1, B * 2) != MP_OKAY)
|
if (mp_init_size(&t1, B * 2) != MP_OKAY)
|
||||||
goto Y1;
|
goto Y1;
|
||||||
if (mp_init_size(&x0y0, B * 2) != MP_OKAY)
|
if (mp_init_size(&x0y0, B * 2) != MP_OKAY)
|
||||||
goto T1;
|
goto T1;
|
||||||
if (mp_init_size(&x1y1, B * 2) != MP_OKAY)
|
if (mp_init_size(&x1y1, B * 2) != MP_OKAY)
|
||||||
goto X0Y0;
|
goto X0Y0;
|
||||||
|
|
||||||
/* now shift the digits */
|
/* now shift the digits */
|
||||||
x0.used = y0.used = B;
|
x0.used = y0.used = B;
|
||||||
x1.used = a->used - B;
|
x1.used = a->used - B;
|
||||||
y1.used = b->used - B;
|
y1.used = b->used - B;
|
||||||
|
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
|
mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
|
||||||
|
|
||||||
/* we copy the digits directly instead of using higher level functions
|
/* we copy the digits directly instead of using higher level functions
|
||||||
* since we also need to shift the digits
|
* since we also need to shift the digits
|
||||||
*/
|
*/
|
||||||
tmpa = a->dp;
|
tmpa = a->dp;
|
||||||
tmpb = b->dp;
|
tmpb = b->dp;
|
||||||
|
|
||||||
tmpx = x0.dp;
|
tmpx = x0.dp;
|
||||||
tmpy = y0.dp;
|
tmpy = y0.dp;
|
||||||
for (x = 0; x < B; x++) {
|
for (x = 0; x < B; x++) {
|
||||||
*tmpx++ = *tmpa++;
|
*tmpx++ = *tmpa++;
|
||||||
*tmpy++ = *tmpb++;
|
*tmpy++ = *tmpb++;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpx = x1.dp;
|
tmpx = x1.dp;
|
||||||
for (x = B; x < a->used; x++) {
|
for (x = B; x < a->used; x++) {
|
||||||
*tmpx++ = *tmpa++;
|
*tmpx++ = *tmpa++;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpy = y1.dp;
|
tmpy = y1.dp;
|
||||||
for (x = B; x < b->used; x++) {
|
for (x = B; x < b->used; x++) {
|
||||||
*tmpy++ = *tmpb++;
|
*tmpy++ = *tmpb++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* only need to clamp the lower words since by definition the
|
/* only need to clamp the lower words since by definition the
|
||||||
* upper words x1/y1 must have a known number of digits
|
* upper words x1/y1 must have a known number of digits
|
||||||
*/
|
*/
|
||||||
mp_clamp(&x0);
|
mp_clamp(&x0);
|
||||||
mp_clamp(&y0);
|
mp_clamp(&y0);
|
||||||
|
|
||||||
/* now calc the products x0y0 and x1y1 */
|
/* now calc the products x0y0 and x1y1 */
|
||||||
/* after this x0 is no longer required, free temp [x0==t2]! */
|
/* after this x0 is no longer required, free temp [x0==t2]! */
|
||||||
if (mp_mul(&x0, &y0, &x0y0) != MP_OKAY)
|
if (mp_mul(&x0, &y0, &x0y0) != MP_OKAY)
|
||||||
goto X1Y1; /* x0y0 = x0*y0 */
|
goto X1Y1; /* x0y0 = x0*y0 */
|
||||||
if (mp_mul(&x1, &y1, &x1y1) != MP_OKAY)
|
if (mp_mul(&x1, &y1, &x1y1) != MP_OKAY)
|
||||||
goto X1Y1; /* x1y1 = x1*y1 */
|
goto X1Y1; /* x1y1 = x1*y1 */
|
||||||
|
|
||||||
/* now calc x1+x0 and y1+y0 */
|
/* now calc x1+x0 and y1+y0 */
|
||||||
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
|
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
|
||||||
goto X1Y1; /* t1 = x1 - x0 */
|
goto X1Y1; /* t1 = x1 - x0 */
|
||||||
if (s_mp_add(&y1, &y0, &x0) != MP_OKAY)
|
if (s_mp_add(&y1, &y0, &x0) != MP_OKAY)
|
||||||
goto X1Y1; /* t2 = y1 - y0 */
|
goto X1Y1; /* t2 = y1 - y0 */
|
||||||
if (mp_mul(&t1, &x0, &t1) != MP_OKAY)
|
if (mp_mul(&t1, &x0, &t1) != MP_OKAY)
|
||||||
goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
|
goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
|
||||||
|
|
||||||
/* add x0y0 */
|
/* add x0y0 */
|
||||||
if (mp_add(&x0y0, &x1y1, &x0) != MP_OKAY)
|
if (mp_add(&x0y0, &x1y1, &x0) != MP_OKAY)
|
||||||
goto X1Y1; /* t2 = x0y0 + x1y1 */
|
goto X1Y1; /* t2 = x0y0 + x1y1 */
|
||||||
if (s_mp_sub(&t1, &x0, &t1) != MP_OKAY)
|
if (s_mp_sub(&t1, &x0, &t1) != MP_OKAY)
|
||||||
goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
|
goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
|
||||||
|
|
||||||
/* shift by B */
|
/* shift by B */
|
||||||
if (mp_lshd(&t1, B) != MP_OKAY)
|
if (mp_lshd(&t1, B) != MP_OKAY)
|
||||||
goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
|
goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
|
||||||
if (mp_lshd(&x1y1, B * 2) != MP_OKAY)
|
if (mp_lshd(&x1y1, B * 2) != MP_OKAY)
|
||||||
goto X1Y1; /* x1y1 = x1y1 << 2*B */
|
goto X1Y1; /* x1y1 = x1y1 << 2*B */
|
||||||
|
|
||||||
if (mp_add(&x0y0, &t1, &t1) != MP_OKAY)
|
if (mp_add(&x0y0, &t1, &t1) != MP_OKAY)
|
||||||
goto X1Y1; /* t1 = x0y0 + t1 */
|
goto X1Y1; /* t1 = x0y0 + t1 */
|
||||||
if (mp_add(&t1, &x1y1, c) != MP_OKAY)
|
if (mp_add(&t1, &x1y1, c) != MP_OKAY)
|
||||||
goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
|
goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
|
||||||
|
|
||||||
/* Algorithm succeeded set the return code to MP_OKAY */
|
/* Algorithm succeeded set the return code to MP_OKAY */
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
|
|
||||||
X1Y1:
|
X1Y1:
|
||||||
mp_clear(&x1y1);
|
mp_clear(&x1y1);
|
||||||
X0Y0:
|
X0Y0:
|
||||||
mp_clear(&x0y0);
|
mp_clear(&x0y0);
|
||||||
T1:
|
T1:
|
||||||
mp_clear(&t1);
|
mp_clear(&t1);
|
||||||
Y1:
|
Y1:
|
||||||
mp_clear(&y1);
|
mp_clear(&y1);
|
||||||
Y0:
|
Y0:
|
||||||
mp_clear(&y0);
|
mp_clear(&y0);
|
||||||
X1:
|
X1:
|
||||||
mp_clear(&x1);
|
mp_clear(&x1);
|
||||||
X0:
|
X0:
|
||||||
mp_clear(&x0);
|
mp_clear(&x0);
|
||||||
ERR:
|
ERR:
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -24,101 +24,101 @@
|
|||||||
*/
|
*/
|
||||||
int mp_karatsuba_sqr(mp_int *a, mp_int *b)
|
int mp_karatsuba_sqr(mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
mp_int x0, x1, t1, t2, x0x0, x1x1;
|
mp_int x0, x1, t1, t2, x0x0, x1x1;
|
||||||
int B, err;
|
int B, err;
|
||||||
|
|
||||||
err = MP_MEM;
|
err = MP_MEM;
|
||||||
|
|
||||||
/* min # of digits */
|
/* min # of digits */
|
||||||
B = a->used;
|
B = a->used;
|
||||||
|
|
||||||
/* now divide in two */
|
/* now divide in two */
|
||||||
B = B >> 1;
|
B = B >> 1;
|
||||||
|
|
||||||
/* init copy all the temps */
|
/* init copy all the temps */
|
||||||
if (mp_init_size(&x0, B) != MP_OKAY)
|
if (mp_init_size(&x0, B) != MP_OKAY)
|
||||||
goto ERR;
|
goto ERR;
|
||||||
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
||||||
goto X0;
|
goto X0;
|
||||||
|
|
||||||
/* init temps */
|
/* init temps */
|
||||||
if (mp_init_size(&t1, a->used * 2) != MP_OKAY)
|
if (mp_init_size(&t1, a->used * 2) != MP_OKAY)
|
||||||
goto X1;
|
goto X1;
|
||||||
if (mp_init_size(&t2, a->used * 2) != MP_OKAY)
|
if (mp_init_size(&t2, a->used * 2) != MP_OKAY)
|
||||||
goto T1;
|
goto T1;
|
||||||
if (mp_init_size(&x0x0, B * 2) != MP_OKAY)
|
if (mp_init_size(&x0x0, B * 2) != MP_OKAY)
|
||||||
goto T2;
|
goto T2;
|
||||||
if (mp_init_size(&x1x1, (a->used - B) * 2) != MP_OKAY)
|
if (mp_init_size(&x1x1, (a->used - B) * 2) != MP_OKAY)
|
||||||
goto X0X0;
|
goto X0X0;
|
||||||
|
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
mp_digit *dst, *src;
|
mp_digit *dst, *src;
|
||||||
|
|
||||||
src = a->dp;
|
src = a->dp;
|
||||||
|
|
||||||
/* now shift the digits */
|
/* now shift the digits */
|
||||||
dst = x0.dp;
|
dst = x0.dp;
|
||||||
for (x = 0; x < B; x++) {
|
for (x = 0; x < B; x++) {
|
||||||
*dst++ = *src++;
|
*dst++ = *src++;
|
||||||
}
|
}
|
||||||
|
|
||||||
dst = x1.dp;
|
dst = x1.dp;
|
||||||
for (x = B; x < a->used; x++) {
|
for (x = B; x < a->used; x++) {
|
||||||
*dst++ = *src++;
|
*dst++ = *src++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
x0.used = B;
|
x0.used = B;
|
||||||
x1.used = a->used - B;
|
x1.used = a->used - B;
|
||||||
|
|
||||||
mp_clamp(&x0);
|
mp_clamp(&x0);
|
||||||
|
|
||||||
/* now calc the products x0*x0 and x1*x1 */
|
/* now calc the products x0*x0 and x1*x1 */
|
||||||
if (mp_sqr(&x0, &x0x0) != MP_OKAY)
|
if (mp_sqr(&x0, &x0x0) != MP_OKAY)
|
||||||
goto X1X1; /* x0x0 = x0*x0 */
|
goto X1X1; /* x0x0 = x0*x0 */
|
||||||
if (mp_sqr(&x1, &x1x1) != MP_OKAY)
|
if (mp_sqr(&x1, &x1x1) != MP_OKAY)
|
||||||
goto X1X1; /* x1x1 = x1*x1 */
|
goto X1X1; /* x1x1 = x1*x1 */
|
||||||
|
|
||||||
/* now calc (x1+x0)**2 */
|
/* now calc (x1+x0)**2 */
|
||||||
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
|
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
|
||||||
goto X1X1; /* t1 = x1 - x0 */
|
goto X1X1; /* t1 = x1 - x0 */
|
||||||
if (mp_sqr(&t1, &t1) != MP_OKAY)
|
if (mp_sqr(&t1, &t1) != MP_OKAY)
|
||||||
goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
|
goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
|
||||||
|
|
||||||
/* add x0y0 */
|
/* add x0y0 */
|
||||||
if (s_mp_add(&x0x0, &x1x1, &t2) != MP_OKAY)
|
if (s_mp_add(&x0x0, &x1x1, &t2) != MP_OKAY)
|
||||||
goto X1X1; /* t2 = x0x0 + x1x1 */
|
goto X1X1; /* t2 = x0x0 + x1x1 */
|
||||||
if (s_mp_sub(&t1, &t2, &t1) != MP_OKAY)
|
if (s_mp_sub(&t1, &t2, &t1) != MP_OKAY)
|
||||||
goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
|
goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
|
||||||
|
|
||||||
/* shift by B */
|
/* shift by B */
|
||||||
if (mp_lshd(&t1, B) != MP_OKAY)
|
if (mp_lshd(&t1, B) != MP_OKAY)
|
||||||
goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
|
goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
|
||||||
if (mp_lshd(&x1x1, B * 2) != MP_OKAY)
|
if (mp_lshd(&x1x1, B * 2) != MP_OKAY)
|
||||||
goto X1X1; /* x1x1 = x1x1 << 2*B */
|
goto X1X1; /* x1x1 = x1x1 << 2*B */
|
||||||
|
|
||||||
if (mp_add(&x0x0, &t1, &t1) != MP_OKAY)
|
if (mp_add(&x0x0, &t1, &t1) != MP_OKAY)
|
||||||
goto X1X1; /* t1 = x0x0 + t1 */
|
goto X1X1; /* t1 = x0x0 + t1 */
|
||||||
if (mp_add(&t1, &x1x1, b) != MP_OKAY)
|
if (mp_add(&t1, &x1x1, b) != MP_OKAY)
|
||||||
goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
|
goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
|
||||||
|
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
|
|
||||||
X1X1:
|
X1X1:
|
||||||
mp_clear(&x1x1);
|
mp_clear(&x1x1);
|
||||||
X0X0:
|
X0X0:
|
||||||
mp_clear(&x0x0);
|
mp_clear(&x0x0);
|
||||||
T2:
|
T2:
|
||||||
mp_clear(&t2);
|
mp_clear(&t2);
|
||||||
T1:
|
T1:
|
||||||
mp_clear(&t1);
|
mp_clear(&t1);
|
||||||
X1:
|
X1:
|
||||||
mp_clear(&x1);
|
mp_clear(&x1);
|
||||||
X0:
|
X0:
|
||||||
mp_clear(&x0);
|
mp_clear(&x0);
|
||||||
ERR:
|
ERR:
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
54
bn_mp_lcm.c
54
bn_mp_lcm.c
@ -18,40 +18,40 @@
|
|||||||
/* computes least common multiple as |a*b|/(a, b) */
|
/* computes least common multiple as |a*b|/(a, b) */
|
||||||
int mp_lcm(mp_int *a, mp_int *b, mp_int *c)
|
int mp_lcm(mp_int *a, mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_int t1, t2;
|
mp_int t1, t2;
|
||||||
|
|
||||||
|
|
||||||
if ((res = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) {
|
if ((res = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* t1 = get the GCD of the two inputs */
|
/* t1 = get the GCD of the two inputs */
|
||||||
if ((res = mp_gcd(a, b, &t1)) != MP_OKAY) {
|
if ((res = mp_gcd(a, b, &t1)) != MP_OKAY) {
|
||||||
goto LBL_T;
|
goto LBL_T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* divide the smallest by the GCD */
|
/* divide the smallest by the GCD */
|
||||||
if (mp_cmp_mag(a, b) == MP_LT) {
|
if (mp_cmp_mag(a, b) == MP_LT) {
|
||||||
/* store quotient in t2 such that t2 * b is the LCM */
|
/* store quotient in t2 such that t2 * b is the LCM */
|
||||||
if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) {
|
if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) {
|
||||||
goto LBL_T;
|
goto LBL_T;
|
||||||
}
|
}
|
||||||
res = mp_mul(b, &t2, c);
|
res = mp_mul(b, &t2, c);
|
||||||
} else {
|
} else {
|
||||||
/* store quotient in t2 such that t2 * a is the LCM */
|
/* store quotient in t2 such that t2 * a is the LCM */
|
||||||
if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) {
|
if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) {
|
||||||
goto LBL_T;
|
goto LBL_T;
|
||||||
}
|
}
|
||||||
res = mp_mul(a, &t2, c);
|
res = mp_mul(a, &t2, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fix the sign to positive */
|
/* fix the sign to positive */
|
||||||
c->sign = MP_ZPOS;
|
c->sign = MP_ZPOS;
|
||||||
|
|
||||||
LBL_T:
|
LBL_T:
|
||||||
mp_clear_multi(&t1, &t2, NULL);
|
mp_clear_multi(&t1, &t2, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
66
bn_mp_lshd.c
66
bn_mp_lshd.c
@ -18,47 +18,47 @@
|
|||||||
/* shift left a certain amount of digits */
|
/* shift left a certain amount of digits */
|
||||||
int mp_lshd(mp_int *a, int b)
|
int mp_lshd(mp_int *a, int b)
|
||||||
{
|
{
|
||||||
int x, res;
|
int x, res;
|
||||||
|
|
||||||
/* if its less than zero return */
|
/* if its less than zero return */
|
||||||
if (b <= 0) {
|
if (b <= 0) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* grow to fit the new digits */
|
/* grow to fit the new digits */
|
||||||
if (a->alloc < (a->used + b)) {
|
if (a->alloc < (a->used + b)) {
|
||||||
if ((res = mp_grow(a, a->used + b)) != MP_OKAY) {
|
if ((res = mp_grow(a, a->used + b)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
mp_digit *top, *bottom;
|
mp_digit *top, *bottom;
|
||||||
|
|
||||||
/* increment the used by the shift amount then copy upwards */
|
/* increment the used by the shift amount then copy upwards */
|
||||||
a->used += b;
|
a->used += b;
|
||||||
|
|
||||||
/* top */
|
/* top */
|
||||||
top = a->dp + a->used - 1;
|
top = a->dp + a->used - 1;
|
||||||
|
|
||||||
/* base */
|
/* base */
|
||||||
bottom = (a->dp + a->used - 1) - b;
|
bottom = (a->dp + a->used - 1) - b;
|
||||||
|
|
||||||
/* much like mp_rshd this is implemented using a sliding window
|
/* much like mp_rshd this is implemented using a sliding window
|
||||||
* except the window goes the otherway around. Copying from
|
* except the window goes the otherway around. Copying from
|
||||||
* the bottom to the top. see bn_mp_rshd.c for more info.
|
* the bottom to the top. see bn_mp_rshd.c for more info.
|
||||||
*/
|
*/
|
||||||
for (x = a->used - 1; x >= b; x--) {
|
for (x = a->used - 1; x >= b; x--) {
|
||||||
*top-- = *bottom--;
|
*top-- = *bottom--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* zero the lower digits */
|
/* zero the lower digits */
|
||||||
top = a->dp;
|
top = a->dp;
|
||||||
for (x = 0; x < b; x++) {
|
for (x = 0; x < b; x++) {
|
||||||
*top++ = 0;
|
*top++ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
34
bn_mp_mod.c
34
bn_mp_mod.c
@ -18,27 +18,27 @@
|
|||||||
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
|
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
|
||||||
int mp_mod(mp_int *a, mp_int *b, mp_int *c)
|
int mp_mod(mp_int *a, mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_int t;
|
mp_int t;
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
if ((res = mp_init_size(&t, b->used)) != MP_OKAY) {
|
if ((res = mp_init_size(&t, b->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_div(a, b, NULL, &t)) != MP_OKAY) {
|
if ((res = mp_div(a, b, NULL, &t)) != MP_OKAY) {
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
|
if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
mp_exch(&t, c);
|
mp_exch(&t, c);
|
||||||
} else {
|
} else {
|
||||||
res = mp_add(b, &t, c);
|
res = mp_add(b, &t, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,34 +18,34 @@
|
|||||||
/* calc a value mod 2**b */
|
/* calc a value mod 2**b */
|
||||||
int mp_mod_2d(mp_int *a, int b, mp_int *c)
|
int mp_mod_2d(mp_int *a, int b, mp_int *c)
|
||||||
{
|
{
|
||||||
int x, res;
|
int x, res;
|
||||||
|
|
||||||
/* if b is <= 0 then zero the int */
|
/* if b is <= 0 then zero the int */
|
||||||
if (b <= 0) {
|
if (b <= 0) {
|
||||||
mp_zero(c);
|
mp_zero(c);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if the modulus is larger than the value than return */
|
/* if the modulus is larger than the value than return */
|
||||||
if (b >= (int) (a->used * DIGIT_BIT)) {
|
if (b >= (int)(a->used * DIGIT_BIT)) {
|
||||||
res = mp_copy(a, c);
|
res = mp_copy(a, c);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy */
|
/* copy */
|
||||||
if ((res = mp_copy(a, c)) != MP_OKAY) {
|
if ((res = mp_copy(a, c)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* zero digits above the last digit of the modulus */
|
/* zero digits above the last digit of the modulus */
|
||||||
for (x = (b / DIGIT_BIT) + (((b % DIGIT_BIT) == 0) ? 0 : 1); x < c->used; x++) {
|
for (x = (b / DIGIT_BIT) + (((b % DIGIT_BIT) == 0) ? 0 : 1); x < c->used; x++) {
|
||||||
c->dp[x] = 0;
|
c->dp[x] = 0;
|
||||||
}
|
}
|
||||||
/* clear the digit that is not completely outside/inside the modulus */
|
/* clear the digit that is not completely outside/inside the modulus */
|
||||||
c->dp[b / DIGIT_BIT] &=
|
c->dp[b / DIGIT_BIT] &=
|
||||||
(mp_digit)((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
|
(mp_digit)((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
|
||||||
mp_clamp(c);
|
mp_clamp(c);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c)
|
int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c)
|
||||||
{
|
{
|
||||||
return mp_div_d(a, b, NULL, c);
|
return mp_div_d(a, b, NULL, c);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,38 +18,38 @@
|
|||||||
/* setups the montgomery reduction stuff */
|
/* setups the montgomery reduction stuff */
|
||||||
int mp_montgomery_setup(mp_int *n, mp_digit *rho)
|
int mp_montgomery_setup(mp_int *n, mp_digit *rho)
|
||||||
{
|
{
|
||||||
mp_digit x, b;
|
mp_digit x, b;
|
||||||
|
|
||||||
/* fast inversion mod 2**k
|
/* fast inversion mod 2**k
|
||||||
*
|
*
|
||||||
* Based on the fact that
|
* Based on the fact that
|
||||||
*
|
*
|
||||||
* XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
|
* XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
|
||||||
* => 2*X*A - X*X*A*A = 1
|
* => 2*X*A - X*X*A*A = 1
|
||||||
* => 2*(1) - (1) = 1
|
* => 2*(1) - (1) = 1
|
||||||
*/
|
*/
|
||||||
b = n->dp[0];
|
b = n->dp[0];
|
||||||
|
|
||||||
if ((b & 1) == 0) {
|
if ((b & 1) == 0) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
|
x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**8 */
|
x *= 2 - (b * x); /* here x*a==1 mod 2**8 */
|
||||||
#if !defined(MP_8BIT)
|
#if !defined(MP_8BIT)
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**16 */
|
x *= 2 - (b * x); /* here x*a==1 mod 2**16 */
|
||||||
#endif
|
#endif
|
||||||
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
|
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**32 */
|
x *= 2 - (b * x); /* here x*a==1 mod 2**32 */
|
||||||
#endif
|
#endif
|
||||||
#ifdef MP_64BIT
|
#ifdef MP_64BIT
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**64 */
|
x *= 2 - (b * x); /* here x*a==1 mod 2**64 */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* rho = -1/m mod b */
|
/* rho = -1/m mod b */
|
||||||
*rho = (mp_digit)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
|
*rho = (mp_digit)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
60
bn_mp_mul.c
60
bn_mp_mul.c
@ -18,47 +18,47 @@
|
|||||||
/* high level multiplication (handles sign) */
|
/* high level multiplication (handles sign) */
|
||||||
int mp_mul(mp_int *a, mp_int *b, mp_int *c)
|
int mp_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
int res, neg;
|
int res, neg;
|
||||||
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
||||||
|
|
||||||
/* use Toom-Cook? */
|
/* use Toom-Cook? */
|
||||||
#ifdef BN_MP_TOOM_MUL_C
|
#ifdef BN_MP_TOOM_MUL_C
|
||||||
if (MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
if (MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
||||||
res = mp_toom_mul(a, b, c);
|
res = mp_toom_mul(a, b, c);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
#ifdef BN_MP_KARATSUBA_MUL_C
|
#ifdef BN_MP_KARATSUBA_MUL_C
|
||||||
/* use Karatsuba? */
|
/* use Karatsuba? */
|
||||||
if (MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
if (MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
||||||
res = mp_karatsuba_mul(a, b, c);
|
res = mp_karatsuba_mul(a, b, c);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
/* can we use the fast multiplier?
|
/* can we use the fast multiplier?
|
||||||
*
|
*
|
||||||
* The fast multiplier can be used if the output will
|
* The fast multiplier can be used if the output will
|
||||||
* have less than MP_WARRAY digits and the number of
|
* have less than MP_WARRAY digits and the number of
|
||||||
* digits won't affect carry propagation
|
* digits won't affect carry propagation
|
||||||
*/
|
*/
|
||||||
int digs = a->used + b->used + 1;
|
int digs = a->used + b->used + 1;
|
||||||
|
|
||||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||||
if ((digs < MP_WARRAY) &&
|
if ((digs < MP_WARRAY) &&
|
||||||
(MIN(a->used, b->used) <=
|
(MIN(a->used, b->used) <=
|
||||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
||||||
res = fast_s_mp_mul_digs(a, b, c, digs);
|
res = fast_s_mp_mul_digs(a, b, c, digs);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
#ifdef BN_S_MP_MUL_DIGS_C
|
#ifdef BN_S_MP_MUL_DIGS_C
|
||||||
res = s_mp_mul(a, b, c); /* uses s_mp_mul_digs */
|
res = s_mp_mul(a, b, c); /* uses s_mp_mul_digs */
|
||||||
#else
|
#else
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c->sign = (c->used > 0) ? neg : MP_ZPOS;
|
c->sign = (c->used > 0) ? neg : MP_ZPOS;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,62 +18,62 @@
|
|||||||
/* b = a*2 */
|
/* b = a*2 */
|
||||||
int mp_mul_2(mp_int *a, mp_int *b)
|
int mp_mul_2(mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
int x, res, oldused;
|
int x, res, oldused;
|
||||||
|
|
||||||
/* grow to accomodate result */
|
/* grow to accomodate result */
|
||||||
if (b->alloc < (a->used + 1)) {
|
if (b->alloc < (a->used + 1)) {
|
||||||
if ((res = mp_grow(b, a->used + 1)) != MP_OKAY) {
|
if ((res = mp_grow(b, a->used + 1)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
oldused = b->used;
|
oldused = b->used;
|
||||||
b->used = a->used;
|
b->used = a->used;
|
||||||
|
|
||||||
{
|
{
|
||||||
mp_digit r, rr, *tmpa, *tmpb;
|
mp_digit r, rr, *tmpa, *tmpb;
|
||||||
|
|
||||||
/* alias for source */
|
/* alias for source */
|
||||||
tmpa = a->dp;
|
tmpa = a->dp;
|
||||||
|
|
||||||
/* alias for dest */
|
/* alias for dest */
|
||||||
tmpb = b->dp;
|
tmpb = b->dp;
|
||||||
|
|
||||||
/* carry */
|
/* carry */
|
||||||
r = 0;
|
r = 0;
|
||||||
for (x = 0; x < a->used; x++) {
|
for (x = 0; x < a->used; x++) {
|
||||||
|
|
||||||
/* get what will be the *next* carry bit from the
|
/* get what will be the *next* carry bit from the
|
||||||
* MSB of the current digit
|
* MSB of the current digit
|
||||||
|
*/
|
||||||
|
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
||||||
|
|
||||||
|
/* now shift up this digit, add in the carry [from the previous] */
|
||||||
|
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
||||||
|
|
||||||
|
/* copy the carry that would be from the source
|
||||||
|
* digit into the next iteration
|
||||||
|
*/
|
||||||
|
r = rr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* new leading digit? */
|
||||||
|
if (r != 0) {
|
||||||
|
/* add a MSB which is always 1 at this point */
|
||||||
|
*tmpb = 1;
|
||||||
|
++(b->used);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* now zero any excess digits on the destination
|
||||||
|
* that we didn't write to
|
||||||
*/
|
*/
|
||||||
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
tmpb = b->dp + b->used;
|
||||||
|
for (x = b->used; x < oldused; x++) {
|
||||||
/* now shift up this digit, add in the carry [from the previous] */
|
*tmpb++ = 0;
|
||||||
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
}
|
||||||
|
}
|
||||||
/* copy the carry that would be from the source
|
b->sign = a->sign;
|
||||||
* digit into the next iteration
|
return MP_OKAY;
|
||||||
*/
|
|
||||||
r = rr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* new leading digit? */
|
|
||||||
if (r != 0) {
|
|
||||||
/* add a MSB which is always 1 at this point */
|
|
||||||
*tmpb = 1;
|
|
||||||
++(b->used);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* now zero any excess digits on the destination
|
|
||||||
* that we didn't write to
|
|
||||||
*/
|
|
||||||
tmpb = b->dp + b->used;
|
|
||||||
for (x = b->used; x < oldused; x++) {
|
|
||||||
*tmpb++ = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b->sign = a->sign;
|
|
||||||
return MP_OKAY;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,65 +18,65 @@
|
|||||||
/* shift left by a certain bit count */
|
/* shift left by a certain bit count */
|
||||||
int mp_mul_2d(mp_int *a, int b, mp_int *c)
|
int mp_mul_2d(mp_int *a, int b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_digit d;
|
mp_digit d;
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
/* copy */
|
/* copy */
|
||||||
if (a != c) {
|
if (a != c) {
|
||||||
if ((res = mp_copy(a, c)) != MP_OKAY) {
|
if ((res = mp_copy(a, c)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) {
|
if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) {
|
||||||
if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
|
if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift by as many digits in the bit count */
|
/* shift by as many digits in the bit count */
|
||||||
if (b >= (int)DIGIT_BIT) {
|
if (b >= (int)DIGIT_BIT) {
|
||||||
if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
|
if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift any bit count < DIGIT_BIT */
|
/* shift any bit count < DIGIT_BIT */
|
||||||
d = (mp_digit)(b % DIGIT_BIT);
|
d = (mp_digit)(b % DIGIT_BIT);
|
||||||
if (d != 0) {
|
if (d != 0) {
|
||||||
mp_digit *tmpc, shift, mask, r, rr;
|
mp_digit *tmpc, shift, mask, r, rr;
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
/* bitmask for carries */
|
/* bitmask for carries */
|
||||||
mask = (((mp_digit)1) << d) - 1;
|
mask = (((mp_digit)1) << d) - 1;
|
||||||
|
|
||||||
/* shift for msbs */
|
/* shift for msbs */
|
||||||
shift = DIGIT_BIT - d;
|
shift = DIGIT_BIT - d;
|
||||||
|
|
||||||
/* alias */
|
/* alias */
|
||||||
tmpc = c->dp;
|
tmpc = c->dp;
|
||||||
|
|
||||||
/* carry */
|
/* carry */
|
||||||
r = 0;
|
r = 0;
|
||||||
for (x = 0; x < c->used; x++) {
|
for (x = 0; x < c->used; x++) {
|
||||||
/* get the higher bits of the current word */
|
/* get the higher bits of the current word */
|
||||||
rr = (*tmpc >> shift) & mask;
|
rr = (*tmpc >> shift) & mask;
|
||||||
|
|
||||||
/* shift the current word and OR in the carry */
|
/* shift the current word and OR in the carry */
|
||||||
*tmpc = ((*tmpc << d) | r) & MP_MASK;
|
*tmpc = ((*tmpc << d) | r) & MP_MASK;
|
||||||
++tmpc;
|
++tmpc;
|
||||||
|
|
||||||
/* set the carry to the carry bits of the current word */
|
/* set the carry to the carry bits of the current word */
|
||||||
r = rr;
|
r = rr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set final carry */
|
/* set final carry */
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
c->dp[(c->used)++] = r;
|
c->dp[(c->used)++] = r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mp_clamp(c);
|
mp_clamp(c);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,58 +18,58 @@
|
|||||||
/* multiply by a digit */
|
/* multiply by a digit */
|
||||||
int mp_mul_d(mp_int *a, mp_digit b, mp_int *c)
|
int mp_mul_d(mp_int *a, mp_digit b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_digit u, *tmpa, *tmpc;
|
mp_digit u, *tmpa, *tmpc;
|
||||||
mp_word r;
|
mp_word r;
|
||||||
int ix, res, olduse;
|
int ix, res, olduse;
|
||||||
|
|
||||||
/* make sure c is big enough to hold a*b */
|
/* make sure c is big enough to hold a*b */
|
||||||
if (c->alloc < (a->used + 1)) {
|
if (c->alloc < (a->used + 1)) {
|
||||||
if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
|
if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get the original destinations used count */
|
/* get the original destinations used count */
|
||||||
olduse = c->used;
|
olduse = c->used;
|
||||||
|
|
||||||
/* set the sign */
|
/* set the sign */
|
||||||
c->sign = a->sign;
|
c->sign = a->sign;
|
||||||
|
|
||||||
/* alias for a->dp [source] */
|
/* alias for a->dp [source] */
|
||||||
tmpa = a->dp;
|
tmpa = a->dp;
|
||||||
|
|
||||||
/* alias for c->dp [dest] */
|
/* alias for c->dp [dest] */
|
||||||
tmpc = c->dp;
|
tmpc = c->dp;
|
||||||
|
|
||||||
/* zero carry */
|
/* zero carry */
|
||||||
u = 0;
|
u = 0;
|
||||||
|
|
||||||
/* compute columns */
|
/* compute columns */
|
||||||
for (ix = 0; ix < a->used; ix++) {
|
for (ix = 0; ix < a->used; ix++) {
|
||||||
/* compute product and carry sum for this term */
|
/* compute product and carry sum for this term */
|
||||||
r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
|
r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
|
||||||
|
|
||||||
/* mask off higher bits to get a single digit */
|
/* mask off higher bits to get a single digit */
|
||||||
*tmpc++ = (mp_digit)(r & ((mp_word)MP_MASK));
|
*tmpc++ = (mp_digit)(r & ((mp_word)MP_MASK));
|
||||||
|
|
||||||
/* send carry into next iteration */
|
/* send carry into next iteration */
|
||||||
u = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
u = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store final carry [if any] and increment ix offset */
|
/* store final carry [if any] and increment ix offset */
|
||||||
*tmpc++ = u;
|
*tmpc++ = u;
|
||||||
++ix;
|
++ix;
|
||||||
|
|
||||||
/* now zero digits above the top */
|
/* now zero digits above the top */
|
||||||
while (ix++ < olduse) {
|
while (ix++ < olduse) {
|
||||||
*tmpc++ = 0;
|
*tmpc++ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set used count */
|
/* set used count */
|
||||||
c->used = a->used + 1;
|
c->used = a->used + 1;
|
||||||
mp_clamp(c);
|
mp_clamp(c);
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -18,20 +18,20 @@
|
|||||||
/* d = a * b (mod c) */
|
/* d = a * b (mod c) */
|
||||||
int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
|
int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_int t;
|
mp_int t;
|
||||||
|
|
||||||
if ((res = mp_init_size(&t, c->used)) != MP_OKAY) {
|
if ((res = mp_init_size(&t, c->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_mul(a, b, &t)) != MP_OKAY) {
|
if ((res = mp_mul(a, b, &t)) != MP_OKAY) {
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
res = mp_mod(&t, c, d);
|
res = mp_mod(&t, c, d);
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
int mp_n_root(mp_int *a, mp_digit b, mp_int *c)
|
int mp_n_root(mp_int *a, mp_digit b, mp_int *c)
|
||||||
{
|
{
|
||||||
return mp_n_root_ex(a, b, c, 0);
|
return mp_n_root_ex(a, b, c, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
24
bn_mp_neg.c
24
bn_mp_neg.c
@ -18,20 +18,20 @@
|
|||||||
/* b = -a */
|
/* b = -a */
|
||||||
int mp_neg(mp_int *a, mp_int *b)
|
int mp_neg(mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
if (a != b) {
|
if (a != b) {
|
||||||
if ((res = mp_copy(a, b)) != MP_OKAY) {
|
if ((res = mp_copy(a, b)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_iszero(b) != MP_YES) {
|
if (mp_iszero(b) != MP_YES) {
|
||||||
b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
|
b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
|
||||||
} else {
|
} else {
|
||||||
b->sign = MP_ZPOS;
|
b->sign = MP_ZPOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
44
bn_mp_or.c
44
bn_mp_or.c
@ -18,30 +18,30 @@
|
|||||||
/* OR two ints together */
|
/* OR two ints together */
|
||||||
int mp_or(mp_int *a, mp_int *b, mp_int *c)
|
int mp_or(mp_int *a, mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
int res, ix, px;
|
int res, ix, px;
|
||||||
mp_int t, *x;
|
mp_int t, *x;
|
||||||
|
|
||||||
if (a->used > b->used) {
|
if (a->used > b->used) {
|
||||||
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
px = b->used;
|
px = b->used;
|
||||||
x = b;
|
x = b;
|
||||||
} else {
|
} else {
|
||||||
if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
|
if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
px = a->used;
|
px = a->used;
|
||||||
x = a;
|
x = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ix = 0; ix < px; ix++) {
|
for (ix = 0; ix < px; ix++) {
|
||||||
t.dp[ix] |= x->dp[ix];
|
t.dp[ix] |= x->dp[ix];
|
||||||
}
|
}
|
||||||
mp_clamp(&t);
|
mp_clamp(&t);
|
||||||
mp_exch(c, &t);
|
mp_exch(c, &t);
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user