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