added libtommath-0.33
This commit is contained in:
parent
e549ccfec5
commit
4b7111d96e
16
TODO
Normal file
16
TODO
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
things for book in order of importance...
|
||||||
|
|
||||||
|
- Fix up pseudo-code [only] for combas that are not consistent with source
|
||||||
|
- Start in chapter 3 [basics] and work up...
|
||||||
|
- re-write to prose [less abrupt]
|
||||||
|
- clean up pseudo code [spacing]
|
||||||
|
- more examples where appropriate and figures
|
||||||
|
|
||||||
|
Goal:
|
||||||
|
- Get sync done by mid January [roughly 8-12 hours work]
|
||||||
|
- Finish ch3-6 by end of January [roughly 12-16 hours of work]
|
||||||
|
- Finish ch7-end by mid Feb [roughly 20-24 hours of work].
|
||||||
|
|
||||||
|
Goal isn't "first edition" but merely cleaner to read.
|
||||||
|
|
||||||
|
|
2
bn.tex
2
bn.tex
@ -49,7 +49,7 @@
|
|||||||
\begin{document}
|
\begin{document}
|
||||||
\frontmatter
|
\frontmatter
|
||||||
\pagestyle{empty}
|
\pagestyle{empty}
|
||||||
\title{LibTomMath User Manual \\ v0.32}
|
\title{LibTomMath User Manual \\ v0.33}
|
||||||
\author{Tom St Denis \\ tomstdenis@iahu.ca}
|
\author{Tom St Denis \\ tomstdenis@iahu.ca}
|
||||||
\maketitle
|
\maketitle
|
||||||
This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
|
This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
|
||||||
|
@ -39,20 +39,20 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
|
|
||||||
/* x == modulus, y == value to invert */
|
/* x == modulus, y == value to invert */
|
||||||
if ((res = mp_copy (b, &x)) != MP_OKAY) {
|
if ((res = mp_copy (b, &x)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* we need y = |a| */
|
/* we need y = |a| */
|
||||||
if ((res = mp_abs (a, &y)) != MP_OKAY) {
|
if ((res = mp_abs (a, &y)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
||||||
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
|
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
mp_set (&D, 1);
|
mp_set (&D, 1);
|
||||||
|
|
||||||
@ -61,17 +61,17 @@ top:
|
|||||||
while (mp_iseven (&u) == 1) {
|
while (mp_iseven (&u) == 1) {
|
||||||
/* 4.1 u = u/2 */
|
/* 4.1 u = u/2 */
|
||||||
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
|
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
/* 4.2 if B is odd then */
|
/* 4.2 if B is odd then */
|
||||||
if (mp_isodd (&B) == 1) {
|
if (mp_isodd (&B) == 1) {
|
||||||
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
|
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* B = B/2 */
|
/* B = B/2 */
|
||||||
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
|
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,18 +79,18 @@ top:
|
|||||||
while (mp_iseven (&v) == 1) {
|
while (mp_iseven (&v) == 1) {
|
||||||
/* 5.1 v = v/2 */
|
/* 5.1 v = v/2 */
|
||||||
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
|
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
/* 5.2 if D is odd then */
|
/* 5.2 if D is odd then */
|
||||||
if (mp_isodd (&D) == 1) {
|
if (mp_isodd (&D) == 1) {
|
||||||
/* D = (D-x)/2 */
|
/* D = (D-x)/2 */
|
||||||
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
|
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* D = D/2 */
|
/* D = D/2 */
|
||||||
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
|
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,20 +98,20 @@ top:
|
|||||||
if (mp_cmp (&u, &v) != MP_LT) {
|
if (mp_cmp (&u, &v) != MP_LT) {
|
||||||
/* u = u - v, B = B - D */
|
/* u = u - v, B = B - D */
|
||||||
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
|
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
|
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* v - v - u, D = D - B */
|
/* v - v - u, D = D - B */
|
||||||
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
|
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
|
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,21 +125,21 @@ top:
|
|||||||
/* if v != 1 then there is no inverse */
|
/* if v != 1 then there is no inverse */
|
||||||
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* b is now the inverse */
|
/* b is now the inverse */
|
||||||
neg = a->sign;
|
neg = a->sign;
|
||||||
while (D.sign == MP_NEG) {
|
while (D.sign == MP_NEG) {
|
||||||
if ((res = mp_add (&D, b, &D)) != MP_OKAY) {
|
if ((res = mp_add (&D, b, &D)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mp_exch (&D, c);
|
mp_exch (&D, c);
|
||||||
c->sign = neg;
|
c->sign = neg;
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
|
|
||||||
__ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
|
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -50,7 +50,7 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
|||||||
|
|
||||||
/* clear the carry */
|
/* clear the carry */
|
||||||
_W = 0;
|
_W = 0;
|
||||||
for (ix = 0; ix <= pa; ix++) {
|
for (ix = 0; ix < pa; ix++) {
|
||||||
int tx, ty;
|
int tx, ty;
|
||||||
int iy;
|
int iy;
|
||||||
mp_digit *tmpx, *tmpy;
|
mp_digit *tmpx, *tmpy;
|
||||||
@ -80,6 +80,9 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
|||||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
_W = _W >> ((mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* store final carry */
|
||||||
|
W[ix] = _W;
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
olduse = c->used;
|
olduse = c->used;
|
||||||
c->used = digs;
|
c->used = digs;
|
||||||
|
@ -42,7 +42,7 @@ fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
|||||||
/* number of output digits to produce */
|
/* number of output digits to produce */
|
||||||
pa = a->used + b->used;
|
pa = a->used + b->used;
|
||||||
_W = 0;
|
_W = 0;
|
||||||
for (ix = digs; ix <= pa; ix++) {
|
for (ix = digs; ix < pa; ix++) {
|
||||||
int tx, ty, iy;
|
int tx, ty, iy;
|
||||||
mp_digit *tmpx, *tmpy;
|
mp_digit *tmpx, *tmpy;
|
||||||
|
|
||||||
@ -70,6 +70,9 @@ fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
|||||||
/* make next carry */
|
/* make next carry */
|
||||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
_W = _W >> ((mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* store final carry */
|
||||||
|
W[ix] = _W;
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
olduse = c->used;
|
olduse = c->used;
|
||||||
|
@ -60,7 +60,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
|||||||
|
|
||||||
/* number of output digits to produce */
|
/* number of output digits to produce */
|
||||||
W1 = 0;
|
W1 = 0;
|
||||||
for (ix = 0; ix <= pa; ix++) {
|
for (ix = 0; ix < pa; ix++) {
|
||||||
int tx, ty, iy;
|
int tx, ty, iy;
|
||||||
mp_word _W;
|
mp_word _W;
|
||||||
mp_digit *tmpy;
|
mp_digit *tmpy;
|
||||||
|
56
bn_mp_div.c
56
bn_mp_div.c
@ -49,23 +49,23 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
|
|
||||||
mp_set(&tq, 1);
|
mp_set(&tq, 1);
|
||||||
n = mp_count_bits(a) - mp_count_bits(b);
|
n = mp_count_bits(a) - mp_count_bits(b);
|
||||||
if (((res = mp_copy(a, &ta)) != MP_OKAY) ||
|
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
||||||
((res = mp_copy(b, &tb)) != MP_OKAY) ||
|
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||||
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
|
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
|
||||||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
|
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (n-- >= 0) {
|
while (n-- >= 0) {
|
||||||
if (mp_cmp(&tb, &ta) != MP_GT) {
|
if (mp_cmp(&tb, &ta) != MP_GT) {
|
||||||
if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
|
if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
|
||||||
((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
|
((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
|
if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
|
||||||
((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
|
((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,13 +74,13 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
|
n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
|
||||||
if (c != NULL) {
|
if (c != NULL) {
|
||||||
mp_exch(c, &q);
|
mp_exch(c, &q);
|
||||||
c->sign = n2;
|
c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
|
||||||
}
|
}
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
mp_exch(d, &ta);
|
mp_exch(d, &ta);
|
||||||
d->sign = n;
|
d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
|
||||||
}
|
}
|
||||||
__ERR:
|
LBL_ERR:
|
||||||
mp_clear_multi(&ta, &tb, &tq, &q, NULL);
|
mp_clear_multi(&ta, &tb, &tq, &q, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -129,19 +129,19 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
q.used = a->used + 2;
|
q.used = a->used + 2;
|
||||||
|
|
||||||
if ((res = mp_init (&t1)) != MP_OKAY) {
|
if ((res = mp_init (&t1)) != MP_OKAY) {
|
||||||
goto __Q;
|
goto LBL_Q;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&t2)) != MP_OKAY) {
|
if ((res = mp_init (&t2)) != MP_OKAY) {
|
||||||
goto __T1;
|
goto LBL_T1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init_copy (&x, a)) != MP_OKAY) {
|
if ((res = mp_init_copy (&x, a)) != MP_OKAY) {
|
||||||
goto __T2;
|
goto LBL_T2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init_copy (&y, b)) != MP_OKAY) {
|
if ((res = mp_init_copy (&y, b)) != MP_OKAY) {
|
||||||
goto __X;
|
goto LBL_X;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fix the sign */
|
/* fix the sign */
|
||||||
@ -153,10 +153,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
if (norm < (int)(DIGIT_BIT-1)) {
|
if (norm < (int)(DIGIT_BIT-1)) {
|
||||||
norm = (DIGIT_BIT-1) - norm;
|
norm = (DIGIT_BIT-1) - norm;
|
||||||
if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
|
if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
|
if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
norm = 0;
|
norm = 0;
|
||||||
@ -168,13 +168,13 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
|
|
||||||
/* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
|
/* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
|
||||||
if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
|
if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (mp_cmp (&x, &y) != MP_LT) {
|
while (mp_cmp (&x, &y) != MP_LT) {
|
||||||
++(q.dp[n - t]);
|
++(q.dp[n - t]);
|
||||||
if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) {
|
if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,7 +216,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
t1.dp[1] = y.dp[t];
|
t1.dp[1] = y.dp[t];
|
||||||
t1.used = 2;
|
t1.used = 2;
|
||||||
if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) {
|
if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* find right hand */
|
/* find right hand */
|
||||||
@ -228,27 +228,27 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
|
|
||||||
/* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
|
/* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
|
||||||
if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
|
if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
|
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) {
|
if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
|
/* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
|
||||||
if (x.sign == MP_NEG) {
|
if (x.sign == MP_NEG) {
|
||||||
if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
|
if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
|
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
|
if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
|
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
|
||||||
@ -275,11 +275,11 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
|
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
|
|
||||||
__Y:mp_clear (&y);
|
LBL_Y:mp_clear (&y);
|
||||||
__X:mp_clear (&x);
|
LBL_X:mp_clear (&x);
|
||||||
__T2:mp_clear (&t2);
|
LBL_T2:mp_clear (&t2);
|
||||||
__T1:mp_clear (&t1);
|
LBL_T1:mp_clear (&t1);
|
||||||
__Q:mp_clear (&q);
|
LBL_Q:mp_clear (&q);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
* Based on algorithm from the paper
|
* Based on algorithm from the paper
|
||||||
*
|
*
|
||||||
* "Generating Efficient Primes for Discrete Log Cryptosystems"
|
* "Generating Efficient Primes for Discrete Log Cryptosystems"
|
||||||
* Chae Hoon Lim, Pil Loong Lee,
|
* Chae Hoon Lim, Pil Joong Lee,
|
||||||
* POSTECH Information Research Laboratories
|
* POSTECH Information Research Laboratories
|
||||||
*
|
*
|
||||||
* The modulus must be of a special format [see manual]
|
* The modulus must be of a special format [see manual]
|
||||||
|
@ -61,7 +61,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
return err;
|
return err;
|
||||||
#else
|
#else
|
||||||
/* no invmod */
|
/* no invmod */
|
||||||
return MP_VAL
|
return MP_VAL;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,11 +88,11 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||||
/* now setup montgomery */
|
/* now setup montgomery */
|
||||||
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
err = MP_VAL;
|
err = MP_VAL;
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
|
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
|
||||||
@ -108,7 +108,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
redux = mp_montgomery_reduce;
|
redux = mp_montgomery_reduce;
|
||||||
#else
|
#else
|
||||||
err = MP_VAL;
|
err = MP_VAL;
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
} else if (redmode == 1) {
|
} else if (redmode == 1) {
|
||||||
@ -118,24 +118,24 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
redux = mp_dr_reduce;
|
redux = mp_dr_reduce;
|
||||||
#else
|
#else
|
||||||
err = MP_VAL;
|
err = MP_VAL;
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
#if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
|
#if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
|
||||||
/* setup DR reduction for moduli of the form 2**k - b */
|
/* setup DR reduction for moduli of the form 2**k - b */
|
||||||
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
|
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
}
|
}
|
||||||
redux = mp_reduce_2k;
|
redux = mp_reduce_2k;
|
||||||
#else
|
#else
|
||||||
err = MP_VAL;
|
err = MP_VAL;
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup result */
|
/* setup result */
|
||||||
if ((err = mp_init (&res)) != MP_OKAY) {
|
if ((err = mp_init (&res)) != MP_OKAY) {
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create M table
|
/* create M table
|
||||||
@ -149,45 +149,45 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
|
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
|
||||||
/* now we need R mod m */
|
/* now we need R mod m */
|
||||||
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
err = MP_VAL;
|
err = MP_VAL;
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* now set M[1] to G * R mod m */
|
/* now set M[1] to G * R mod m */
|
||||||
if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
|
if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mp_set(&res, 1);
|
mp_set(&res, 1);
|
||||||
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
|
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
|
/* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
|
||||||
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < (winsize - 1); x++) {
|
for (x = 0; x < (winsize - 1); x++) {
|
||||||
if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
|
if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create upper table */
|
/* create upper table */
|
||||||
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
|
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
|
||||||
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
|
if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,10 +227,10 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
/* if the bit is zero and mode == 1 then we square */
|
/* if the bit is zero and mode == 1 then we square */
|
||||||
if (mode == 1 && y == 0) {
|
if (mode == 1 && y == 0) {
|
||||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -244,19 +244,19 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
/* square first */
|
/* square first */
|
||||||
for (x = 0; x < winsize; x++) {
|
for (x = 0; x < winsize; x++) {
|
||||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* then multiply */
|
/* then multiply */
|
||||||
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
|
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* empty window and reset */
|
/* empty window and reset */
|
||||||
@ -271,10 +271,10 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
/* square then multiply if the bit is set */
|
/* square then multiply if the bit is set */
|
||||||
for (x = 0; x < bitcpy; x++) {
|
for (x = 0; x < bitcpy; x++) {
|
||||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get next bit of the window */
|
/* get next bit of the window */
|
||||||
@ -282,10 +282,10 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
if ((bitbuf & (1 << winsize)) != 0) {
|
if ((bitbuf & (1 << winsize)) != 0) {
|
||||||
/* then multiply */
|
/* then multiply */
|
||||||
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
|
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -299,15 +299,15 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
|||||||
* of R.
|
* of R.
|
||||||
*/
|
*/
|
||||||
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* swap res with Y */
|
/* swap res with Y */
|
||||||
mp_exch (&res, Y);
|
mp_exch (&res, Y);
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
__RES:mp_clear (&res);
|
LBL_RES:mp_clear (&res);
|
||||||
__M:
|
LBL_M:
|
||||||
mp_clear(&M[1]);
|
mp_clear(&M[1]);
|
||||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||||
mp_clear (&M[x]);
|
mp_clear (&M[x]);
|
||||||
|
20
bn_mp_gcd.c
20
bn_mp_gcd.c
@ -43,7 +43,7 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init_copy (&v, b)) != MP_OKAY) {
|
if ((res = mp_init_copy (&v, b)) != MP_OKAY) {
|
||||||
goto __U;
|
goto LBL_U;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* must be positive for the remainder of the algorithm */
|
/* must be positive for the remainder of the algorithm */
|
||||||
@ -57,24 +57,24 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
if (k > 0) {
|
if (k > 0) {
|
||||||
/* divide the power of two out */
|
/* divide the power of two out */
|
||||||
if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* divide any remaining factors of two out */
|
/* divide any remaining factors of two out */
|
||||||
if (u_lsb != k) {
|
if (u_lsb != k) {
|
||||||
if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (v_lsb != k) {
|
if (v_lsb != k) {
|
||||||
if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,23 +87,23 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
|
|
||||||
/* subtract smallest from largest */
|
/* subtract smallest from largest */
|
||||||
if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
|
if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Divide out all factors of two */
|
/* Divide out all factors of two */
|
||||||
if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* multiply by 2**k which we divided out at the beginning */
|
/* multiply by 2**k which we divided out at the beginning */
|
||||||
if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
|
if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
c->sign = MP_ZPOS;
|
c->sign = MP_ZPOS;
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
__V:mp_clear (&u);
|
LBL_V:mp_clear (&u);
|
||||||
__U:mp_clear (&v);
|
LBL_U:mp_clear (&v);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -34,24 +34,24 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
|
|
||||||
/* x = a, y = b */
|
/* x = a, y = b */
|
||||||
if ((res = mp_copy (a, &x)) != MP_OKAY) {
|
if ((res = mp_copy (a, &x)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_copy (b, &y)) != MP_OKAY) {
|
if ((res = mp_copy (b, &y)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 2. [modified] if x,y are both even then return an error! */
|
/* 2. [modified] if x,y are both even then return an error! */
|
||||||
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
|
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
||||||
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
|
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
mp_set (&A, 1);
|
mp_set (&A, 1);
|
||||||
mp_set (&D, 1);
|
mp_set (&D, 1);
|
||||||
@ -61,24 +61,24 @@ top:
|
|||||||
while (mp_iseven (&u) == 1) {
|
while (mp_iseven (&u) == 1) {
|
||||||
/* 4.1 u = u/2 */
|
/* 4.1 u = u/2 */
|
||||||
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
|
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
/* 4.2 if A or B is odd then */
|
/* 4.2 if A or B is odd then */
|
||||||
if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
|
if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
|
||||||
/* A = (A+y)/2, B = (B-x)/2 */
|
/* A = (A+y)/2, B = (B-x)/2 */
|
||||||
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
|
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
|
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* A = A/2, B = B/2 */
|
/* A = A/2, B = B/2 */
|
||||||
if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
|
if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
|
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,24 +86,24 @@ top:
|
|||||||
while (mp_iseven (&v) == 1) {
|
while (mp_iseven (&v) == 1) {
|
||||||
/* 5.1 v = v/2 */
|
/* 5.1 v = v/2 */
|
||||||
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
|
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
/* 5.2 if C or D is odd then */
|
/* 5.2 if C or D is odd then */
|
||||||
if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
|
if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
|
||||||
/* C = (C+y)/2, D = (D-x)/2 */
|
/* C = (C+y)/2, D = (D-x)/2 */
|
||||||
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
|
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
|
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* C = C/2, D = D/2 */
|
/* C = C/2, D = D/2 */
|
||||||
if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
|
if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
|
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,28 +111,28 @@ top:
|
|||||||
if (mp_cmp (&u, &v) != MP_LT) {
|
if (mp_cmp (&u, &v) != MP_LT) {
|
||||||
/* u = u - v, A = A - C, B = B - D */
|
/* u = u - v, A = A - C, B = B - D */
|
||||||
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
|
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
|
if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
|
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* v - v - u, C = C - A, D = D - B */
|
/* v - v - u, C = C - A, D = D - B */
|
||||||
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
|
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
|
if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
|
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,27 +145,27 @@ top:
|
|||||||
/* if v != 1 then there is no inverse */
|
/* if v != 1 then there is no inverse */
|
||||||
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if its too low */
|
/* if its too low */
|
||||||
while (mp_cmp_d(&C, 0) == MP_LT) {
|
while (mp_cmp_d(&C, 0) == MP_LT) {
|
||||||
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
|
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* too big */
|
/* too big */
|
||||||
while (mp_cmp_mag(&C, b) != MP_LT) {
|
while (mp_cmp_mag(&C, b) != MP_LT) {
|
||||||
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
|
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* C is now the inverse */
|
/* C is now the inverse */
|
||||||
mp_exch (&C, c);
|
mp_exch (&C, c);
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
__ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
|
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -50,13 +50,13 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&p1)) != MP_OKAY) {
|
if ((res = mp_init (&p1)) != MP_OKAY) {
|
||||||
goto __A1;
|
goto LBL_A1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* divide out larger power of two */
|
/* divide out larger power of two */
|
||||||
k = mp_cnt_lsb(&a1);
|
k = mp_cnt_lsb(&a1);
|
||||||
if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) {
|
||||||
goto __P1;
|
goto LBL_P1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* step 4. if e is even set s=1 */
|
/* step 4. if e is even set s=1 */
|
||||||
@ -84,18 +84,18 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c)
|
|||||||
} else {
|
} else {
|
||||||
/* n1 = n mod a1 */
|
/* n1 = n mod a1 */
|
||||||
if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) {
|
if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) {
|
||||||
goto __P1;
|
goto LBL_P1;
|
||||||
}
|
}
|
||||||
if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) {
|
if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) {
|
||||||
goto __P1;
|
goto LBL_P1;
|
||||||
}
|
}
|
||||||
*c = s * r;
|
*c = s * r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* done */
|
/* done */
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
__P1:mp_clear (&p1);
|
LBL_P1:mp_clear (&p1);
|
||||||
__A1:mp_clear (&a1);
|
LBL_A1:mp_clear (&a1);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -28,20 +28,20 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
|
|
||||||
/* 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 __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 __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 __T;
|
goto LBL_T;
|
||||||
}
|
}
|
||||||
res = mp_mul(a, &t2, c);
|
res = mp_mul(a, &t2, c);
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
/* fix the sign to positive */
|
/* fix the sign to positive */
|
||||||
c->sign = MP_ZPOS;
|
c->sign = MP_ZPOS;
|
||||||
|
|
||||||
__T:
|
LBL_T:
|
||||||
mp_clear_multi (&t1, &t2, NULL);
|
mp_clear_multi (&t1, &t2, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 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;
|
||||||
}
|
}
|
||||||
|
@ -40,11 +40,11 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&t2)) != MP_OKAY) {
|
if ((res = mp_init (&t2)) != MP_OKAY) {
|
||||||
goto __T1;
|
goto LBL_T1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&t3)) != MP_OKAY) {
|
if ((res = mp_init (&t3)) != MP_OKAY) {
|
||||||
goto __T2;
|
goto LBL_T2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if a is negative fudge the sign but keep track */
|
/* if a is negative fudge the sign but keep track */
|
||||||
@ -57,52 +57,52 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
|||||||
do {
|
do {
|
||||||
/* t1 = t2 */
|
/* t1 = t2 */
|
||||||
if ((res = mp_copy (&t2, &t1)) != MP_OKAY) {
|
if ((res = mp_copy (&t2, &t1)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
||||||
|
|
||||||
/* t3 = t1**(b-1) */
|
/* t3 = t1**(b-1) */
|
||||||
if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) {
|
if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* numerator */
|
/* numerator */
|
||||||
/* t2 = t1**b */
|
/* t2 = t1**b */
|
||||||
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) {
|
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* t2 = t1**b - a */
|
/* t2 = t1**b - a */
|
||||||
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
|
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* denominator */
|
/* denominator */
|
||||||
/* t3 = t1**(b-1) * b */
|
/* t3 = t1**(b-1) * b */
|
||||||
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) {
|
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
||||||
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) {
|
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) {
|
if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
} while (mp_cmp (&t1, &t2) != MP_EQ);
|
} while (mp_cmp (&t1, &t2) != MP_EQ);
|
||||||
|
|
||||||
/* result can be off by a few so check */
|
/* result can be off by a few so check */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) {
|
if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp (&t2, a) == MP_GT) {
|
if (mp_cmp (&t2, a) == MP_GT) {
|
||||||
if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) {
|
if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) {
|
||||||
goto __T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -120,9 +120,9 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
|||||||
|
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
|
|
||||||
__T3:mp_clear (&t3);
|
LBL_T3:mp_clear (&t3);
|
||||||
__T2:mp_clear (&t2);
|
LBL_T2:mp_clear (&t2);
|
||||||
__T1:mp_clear (&t1);
|
LBL_T1:mp_clear (&t1);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -43,7 +43,7 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
|||||||
|
|
||||||
/* compute t = b**a mod a */
|
/* compute t = b**a mod a */
|
||||||
if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) {
|
if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) {
|
||||||
goto __T;
|
goto LBL_T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* is it equal to b? */
|
/* is it equal to b? */
|
||||||
@ -52,7 +52,7 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
__T:mp_clear (&t);
|
LBL_T:mp_clear (&t);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -29,8 +29,8 @@ int mp_prime_is_divisible (mp_int * a, int *result)
|
|||||||
*result = MP_NO;
|
*result = MP_NO;
|
||||||
|
|
||||||
for (ix = 0; ix < PRIME_SIZE; ix++) {
|
for (ix = 0; ix < PRIME_SIZE; ix++) {
|
||||||
/* what is a mod __prime_tab[ix] */
|
/* what is a mod LBL_prime_tab[ix] */
|
||||||
if ((err = mp_mod_d (a, __prime_tab[ix], &res)) != MP_OKAY) {
|
if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
|
|||||||
|
|
||||||
/* is the input equal to one of the primes in the table? */
|
/* is the input equal to one of the primes in the table? */
|
||||||
for (ix = 0; ix < PRIME_SIZE; ix++) {
|
for (ix = 0; ix < PRIME_SIZE; ix++) {
|
||||||
if (mp_cmp_d(a, __prime_tab[ix]) == MP_EQ) {
|
if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
|
||||||
*result = 1;
|
*result = 1;
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
@ -60,20 +60,20 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
|
|||||||
|
|
||||||
for (ix = 0; ix < t; ix++) {
|
for (ix = 0; ix < t; ix++) {
|
||||||
/* set the prime */
|
/* set the prime */
|
||||||
mp_set (&b, __prime_tab[ix]);
|
mp_set (&b, ltm_prime_tab[ix]);
|
||||||
|
|
||||||
if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
|
if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
|
||||||
goto __B;
|
goto LBL_B;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res == MP_NO) {
|
if (res == MP_NO) {
|
||||||
goto __B;
|
goto LBL_B;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* passed the test */
|
/* passed the test */
|
||||||
*result = MP_YES;
|
*result = MP_YES;
|
||||||
__B:mp_clear (&b);
|
LBL_B:mp_clear (&b);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -40,12 +40,12 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) {
|
if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) {
|
||||||
goto __N1;
|
goto LBL_N1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set 2**s * r = n1 */
|
/* set 2**s * r = n1 */
|
||||||
if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) {
|
if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) {
|
||||||
goto __N1;
|
goto LBL_N1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* count the number of least significant bits
|
/* count the number of least significant bits
|
||||||
@ -55,15 +55,15 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
|
|
||||||
/* now divide n - 1 by 2**s */
|
/* now divide n - 1 by 2**s */
|
||||||
if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) {
|
if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) {
|
||||||
goto __R;
|
goto LBL_R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compute y = b**r mod a */
|
/* compute y = b**r mod a */
|
||||||
if ((err = mp_init (&y)) != MP_OKAY) {
|
if ((err = mp_init (&y)) != MP_OKAY) {
|
||||||
goto __R;
|
goto LBL_R;
|
||||||
}
|
}
|
||||||
if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) {
|
if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if y != 1 and y != n1 do */
|
/* if y != 1 and y != n1 do */
|
||||||
@ -72,12 +72,12 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
/* while j <= s-1 and y != n1 */
|
/* while j <= s-1 and y != n1 */
|
||||||
while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) {
|
while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) {
|
||||||
if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) {
|
if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if y == 1 then composite */
|
/* if y == 1 then composite */
|
||||||
if (mp_cmp_d (&y, 1) == MP_EQ) {
|
if (mp_cmp_d (&y, 1) == MP_EQ) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
++j;
|
++j;
|
||||||
@ -85,15 +85,15 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
|
|
||||||
/* if y != n1 then composite */
|
/* if y != n1 then composite */
|
||||||
if (mp_cmp (&y, &n1) != MP_EQ) {
|
if (mp_cmp (&y, &n1) != MP_EQ) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* probably prime now */
|
/* probably prime now */
|
||||||
*result = MP_YES;
|
*result = MP_YES;
|
||||||
__Y:mp_clear (&y);
|
LBL_Y:mp_clear (&y);
|
||||||
__R:mp_clear (&r);
|
LBL_R:mp_clear (&r);
|
||||||
__N1:mp_clear (&n1);
|
LBL_N1:mp_clear (&n1);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -35,10 +35,10 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
|
|
||||||
/* simple algo if a is less than the largest prime in the table */
|
/* simple algo if a is less than the largest prime in the table */
|
||||||
if (mp_cmp_d(a, __prime_tab[PRIME_SIZE-1]) == MP_LT) {
|
if (mp_cmp_d(a, ltm_prime_tab[PRIME_SIZE-1]) == MP_LT) {
|
||||||
/* find which prime it is bigger than */
|
/* find which prime it is bigger than */
|
||||||
for (x = PRIME_SIZE - 2; x >= 0; x--) {
|
for (x = PRIME_SIZE - 2; x >= 0; x--) {
|
||||||
if (mp_cmp_d(a, __prime_tab[x]) != MP_LT) {
|
if (mp_cmp_d(a, ltm_prime_tab[x]) != MP_LT) {
|
||||||
if (bbs_style == 1) {
|
if (bbs_style == 1) {
|
||||||
/* ok we found a prime smaller or
|
/* ok we found a prime smaller or
|
||||||
* equal [so the next is larger]
|
* equal [so the next is larger]
|
||||||
@ -46,17 +46,17 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
* however, the prime must be
|
* however, the prime must be
|
||||||
* congruent to 3 mod 4
|
* congruent to 3 mod 4
|
||||||
*/
|
*/
|
||||||
if ((__prime_tab[x + 1] & 3) != 3) {
|
if ((ltm_prime_tab[x + 1] & 3) != 3) {
|
||||||
/* scan upwards for a prime congruent to 3 mod 4 */
|
/* scan upwards for a prime congruent to 3 mod 4 */
|
||||||
for (y = x + 1; y < PRIME_SIZE; y++) {
|
for (y = x + 1; y < PRIME_SIZE; y++) {
|
||||||
if ((__prime_tab[y] & 3) == 3) {
|
if ((ltm_prime_tab[y] & 3) == 3) {
|
||||||
mp_set(a, __prime_tab[y]);
|
mp_set(a, ltm_prime_tab[y]);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mp_set(a, __prime_tab[x + 1]);
|
mp_set(a, ltm_prime_tab[x + 1]);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
|
|
||||||
/* generate the restable */
|
/* generate the restable */
|
||||||
for (x = 1; x < PRIME_SIZE; x++) {
|
for (x = 1; x < PRIME_SIZE; x++) {
|
||||||
if ((err = mp_mod_d(a, __prime_tab[x], res_tab + x)) != MP_OKAY) {
|
if ((err = mp_mod_d(a, ltm_prime_tab[x], res_tab + x)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,8 +120,8 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
res_tab[x] += kstep;
|
res_tab[x] += kstep;
|
||||||
|
|
||||||
/* subtract the modulus [instead of using division] */
|
/* subtract the modulus [instead of using division] */
|
||||||
if (res_tab[x] >= __prime_tab[x]) {
|
if (res_tab[x] >= ltm_prime_tab[x]) {
|
||||||
res_tab[x] -= __prime_tab[x];
|
res_tab[x] -= ltm_prime_tab[x];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set flag if zero */
|
/* set flag if zero */
|
||||||
@ -133,7 +133,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
|
|
||||||
/* add the step */
|
/* add the step */
|
||||||
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
|
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if didn't pass sieve and step == MAX then skip test */
|
/* if didn't pass sieve and step == MAX then skip test */
|
||||||
@ -143,9 +143,9 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
|
|
||||||
/* is this prime? */
|
/* is this prime? */
|
||||||
for (x = 0; x < t; x++) {
|
for (x = 0; x < t; x++) {
|
||||||
mp_set(&b, __prime_tab[t]);
|
mp_set(&b, ltm_prime_tab[t]);
|
||||||
if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
|
if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if (res == MP_NO) {
|
if (res == MP_NO) {
|
||||||
break;
|
break;
|
||||||
@ -158,7 +158,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
__ERR:
|
LBL_ERR:
|
||||||
mp_clear(&b);
|
mp_clear(&b);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* calc the byte size */
|
/* calc the byte size */
|
||||||
bsize = (size>>3)+(size&7?1:0);
|
bsize = (size>>3) + ((size&7)?1:0);
|
||||||
|
|
||||||
/* we need a buffer of bsize bytes */
|
/* we need a buffer of bsize bytes */
|
||||||
tmp = OPT_CAST(unsigned char) XMALLOC(bsize);
|
tmp = OPT_CAST(unsigned char) XMALLOC(bsize);
|
||||||
@ -56,7 +56,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* calc the maskAND value for the MSbyte*/
|
/* calc the maskAND value for the MSbyte*/
|
||||||
maskAND = 0xFF >> (8 - (size & 7));
|
maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));
|
||||||
|
|
||||||
/* calc the maskOR_msb */
|
/* calc the maskOR_msb */
|
||||||
maskOR_msb = 0;
|
maskOR_msb = 0;
|
||||||
@ -65,7 +65,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
maskOR_msb |= 1 << ((size - 2) & 7);
|
maskOR_msb |= 1 << ((size - 2) & 7);
|
||||||
} else if (flags & LTM_PRIME_2MSB_OFF) {
|
} else if (flags & LTM_PRIME_2MSB_OFF) {
|
||||||
maskAND &= ~(1 << ((size - 2) & 7));
|
maskAND &= ~(1 << ((size - 2) & 7));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get the maskOR_lsb */
|
/* get the maskOR_lsb */
|
||||||
maskOR_lsb = 0;
|
maskOR_lsb = 0;
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
const mp_digit __prime_tab[] = {
|
const mp_digit ltm_prime_tab[] = {
|
||||||
0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
|
0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
|
||||||
0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
|
0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
|
||||||
0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
|
0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
|
||||||
|
@ -70,10 +70,10 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
|
|
||||||
/* create mu, used for Barrett reduction */
|
/* create mu, used for Barrett reduction */
|
||||||
if ((err = mp_init (&mu)) != MP_OKAY) {
|
if ((err = mp_init (&mu)) != MP_OKAY) {
|
||||||
goto __M;
|
goto LBL_M;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create M table
|
/* create M table
|
||||||
@ -85,23 +85,23 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
* computed though accept for M[0] and M[1]
|
* computed though accept for M[0] and M[1]
|
||||||
*/
|
*/
|
||||||
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compute the value at M[1<<(winsize-1)] by squaring
|
/* compute the value at M[1<<(winsize-1)] by squaring
|
||||||
* M[1] (winsize-1) times
|
* M[1] (winsize-1) times
|
||||||
*/
|
*/
|
||||||
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < (winsize - 1); x++) {
|
for (x = 0; x < (winsize - 1); x++) {
|
||||||
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
||||||
&M[1 << (winsize - 1)])) != MP_OKAY) {
|
&M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
|
if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,16 +110,16 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
*/
|
*/
|
||||||
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
|
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
|
||||||
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) {
|
if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup result */
|
/* setup result */
|
||||||
if ((err = mp_init (&res)) != MP_OKAY) {
|
if ((err = mp_init (&res)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
mp_set (&res, 1);
|
mp_set (&res, 1);
|
||||||
|
|
||||||
@ -159,10 +159,10 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
/* if the bit is zero and mode == 1 then we square */
|
/* if the bit is zero and mode == 1 then we square */
|
||||||
if (mode == 1 && y == 0) {
|
if (mode == 1 && y == 0) {
|
||||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -176,19 +176,19 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
/* square first */
|
/* square first */
|
||||||
for (x = 0; x < winsize; x++) {
|
for (x = 0; x < winsize; x++) {
|
||||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* then multiply */
|
/* then multiply */
|
||||||
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
|
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* empty window and reset */
|
/* empty window and reset */
|
||||||
@ -203,20 +203,20 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
/* square then multiply if the bit is set */
|
/* square then multiply if the bit is set */
|
||||||
for (x = 0; x < bitcpy; x++) {
|
for (x = 0; x < bitcpy; x++) {
|
||||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
bitbuf <<= 1;
|
bitbuf <<= 1;
|
||||||
if ((bitbuf & (1 << winsize)) != 0) {
|
if ((bitbuf & (1 << winsize)) != 0) {
|
||||||
/* then multiply */
|
/* then multiply */
|
||||||
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
|
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,9 +224,9 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
|||||||
|
|
||||||
mp_exch (&res, Y);
|
mp_exch (&res, Y);
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
__RES:mp_clear (&res);
|
LBL_RES:mp_clear (&res);
|
||||||
__MU:mp_clear (&mu);
|
LBL_MU:mp_clear (&mu);
|
||||||
__M:
|
LBL_M:
|
||||||
mp_clear(&M[1]);
|
mp_clear(&M[1]);
|
||||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||||
mp_clear (&M[x]);
|
mp_clear (&M[x]);
|
||||||
|
@ -245,6 +245,7 @@ BN_MP_SQRT_C
|
|||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
| | | +--->BN_MP_CLEAR_C
|
| | | +--->BN_MP_CLEAR_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -298,6 +299,7 @@ BN_MP_SQRT_C
|
|||||||
| | +--->BN_MP_CLEAR_C
|
| | +--->BN_MP_CLEAR_C
|
||||||
| +--->BN_MP_SET_C
|
| +--->BN_MP_SET_C
|
||||||
| +--->BN_MP_COUNT_BITS_C
|
| +--->BN_MP_COUNT_BITS_C
|
||||||
|
| +--->BN_MP_ABS_C
|
||||||
| +--->BN_MP_MUL_2D_C
|
| +--->BN_MP_MUL_2D_C
|
||||||
| | +--->BN_MP_GROW_C
|
| | +--->BN_MP_GROW_C
|
||||||
| | +--->BN_MP_LSHD_C
|
| | +--->BN_MP_LSHD_C
|
||||||
@ -404,6 +406,7 @@ BN_MP_IS_SQUARE_C
|
|||||||
| | | +--->BN_MP_CLEAR_C
|
| | | +--->BN_MP_CLEAR_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -700,6 +703,7 @@ BN_MP_IS_SQUARE_C
|
|||||||
| | | +--->BN_MP_INIT_MULTI_C
|
| | | +--->BN_MP_INIT_MULTI_C
|
||||||
| | | | +--->BN_MP_CLEAR_C
|
| | | | +--->BN_MP_CLEAR_C
|
||||||
| | | +--->BN_MP_COUNT_BITS_C
|
| | | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | | +--->BN_MP_ABS_C
|
||||||
| | | +--->BN_MP_MUL_2D_C
|
| | | +--->BN_MP_MUL_2D_C
|
||||||
| | | | +--->BN_MP_GROW_C
|
| | | | +--->BN_MP_GROW_C
|
||||||
| | | | +--->BN_MP_LSHD_C
|
| | | | +--->BN_MP_LSHD_C
|
||||||
@ -753,6 +757,7 @@ BN_MP_IS_SQUARE_C
|
|||||||
| | | +--->BN_MP_CLEAR_C
|
| | | +--->BN_MP_CLEAR_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -2618,6 +2623,7 @@ BN_MP_SUBMOD_C
|
|||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -2838,6 +2844,7 @@ BN_MP_SQRMOD_C
|
|||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -3313,6 +3320,7 @@ BN_MP_N_ROOT_C
|
|||||||
| +--->BN_MP_INIT_MULTI_C
|
| +--->BN_MP_INIT_MULTI_C
|
||||||
| | +--->BN_MP_CLEAR_C
|
| | +--->BN_MP_CLEAR_C
|
||||||
| +--->BN_MP_COUNT_BITS_C
|
| +--->BN_MP_COUNT_BITS_C
|
||||||
|
| +--->BN_MP_ABS_C
|
||||||
| +--->BN_MP_MUL_2D_C
|
| +--->BN_MP_MUL_2D_C
|
||||||
| | +--->BN_MP_GROW_C
|
| | +--->BN_MP_GROW_C
|
||||||
| | +--->BN_MP_LSHD_C
|
| | +--->BN_MP_LSHD_C
|
||||||
@ -4322,6 +4330,7 @@ BN_MP_PRIME_RANDOM_EX_C
|
|||||||
| | | | | +--->BN_MP_ZERO_C
|
| | | | | +--->BN_MP_ZERO_C
|
||||||
| | | | | +--->BN_MP_INIT_MULTI_C
|
| | | | | +--->BN_MP_INIT_MULTI_C
|
||||||
| | | | | +--->BN_MP_COUNT_BITS_C
|
| | | | | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | | | | +--->BN_MP_ABS_C
|
||||||
| | | | | +--->BN_MP_MUL_2D_C
|
| | | | | +--->BN_MP_MUL_2D_C
|
||||||
| | | | | | +--->BN_MP_GROW_C
|
| | | | | | +--->BN_MP_GROW_C
|
||||||
| | | | | | +--->BN_MP_LSHD_C
|
| | | | | | +--->BN_MP_LSHD_C
|
||||||
@ -4548,6 +4557,7 @@ BN_MP_MOD_C
|
|||||||
| | +--->BN_MP_CLEAR_C
|
| | +--->BN_MP_CLEAR_C
|
||||||
| +--->BN_MP_SET_C
|
| +--->BN_MP_SET_C
|
||||||
| +--->BN_MP_COUNT_BITS_C
|
| +--->BN_MP_COUNT_BITS_C
|
||||||
|
| +--->BN_MP_ABS_C
|
||||||
| +--->BN_MP_MUL_2D_C
|
| +--->BN_MP_MUL_2D_C
|
||||||
| | +--->BN_MP_GROW_C
|
| | +--->BN_MP_GROW_C
|
||||||
| | +--->BN_MP_LSHD_C
|
| | +--->BN_MP_LSHD_C
|
||||||
@ -5600,6 +5610,7 @@ BN_MP_PRIME_IS_PRIME_C
|
|||||||
| | | | +--->BN_MP_ZERO_C
|
| | | | +--->BN_MP_ZERO_C
|
||||||
| | | | +--->BN_MP_INIT_MULTI_C
|
| | | | +--->BN_MP_INIT_MULTI_C
|
||||||
| | | | +--->BN_MP_COUNT_BITS_C
|
| | | | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | | | +--->BN_MP_ABS_C
|
||||||
| | | | +--->BN_MP_MUL_2D_C
|
| | | | +--->BN_MP_MUL_2D_C
|
||||||
| | | | | +--->BN_MP_GROW_C
|
| | | | | +--->BN_MP_GROW_C
|
||||||
| | | | | +--->BN_MP_LSHD_C
|
| | | | | +--->BN_MP_LSHD_C
|
||||||
@ -5809,6 +5820,7 @@ BN_MP_EXPTMOD_FAST_C
|
|||||||
| | | +--->BN_MP_ZERO_C
|
| | | +--->BN_MP_ZERO_C
|
||||||
| | | +--->BN_MP_INIT_MULTI_C
|
| | | +--->BN_MP_INIT_MULTI_C
|
||||||
| | | +--->BN_MP_SET_C
|
| | | +--->BN_MP_SET_C
|
||||||
|
| | | +--->BN_MP_ABS_C
|
||||||
| | | +--->BN_MP_MUL_2D_C
|
| | | +--->BN_MP_MUL_2D_C
|
||||||
| | | | +--->BN_MP_GROW_C
|
| | | | +--->BN_MP_GROW_C
|
||||||
| | | | +--->BN_MP_LSHD_C
|
| | | | +--->BN_MP_LSHD_C
|
||||||
@ -5865,6 +5877,7 @@ BN_MP_EXPTMOD_FAST_C
|
|||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | +--->BN_MP_ZERO_C
|
| | +--->BN_MP_ZERO_C
|
||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -6284,6 +6297,7 @@ BN_MP_MULMOD_C
|
|||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -7339,6 +7353,7 @@ BN_MP_PRIME_NEXT_PRIME_C
|
|||||||
| | | | +--->BN_MP_ZERO_C
|
| | | | +--->BN_MP_ZERO_C
|
||||||
| | | | +--->BN_MP_INIT_MULTI_C
|
| | | | +--->BN_MP_INIT_MULTI_C
|
||||||
| | | | +--->BN_MP_COUNT_BITS_C
|
| | | | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | | | +--->BN_MP_ABS_C
|
||||||
| | | | +--->BN_MP_MUL_2D_C
|
| | | | +--->BN_MP_MUL_2D_C
|
||||||
| | | | | +--->BN_MP_GROW_C
|
| | | | | +--->BN_MP_GROW_C
|
||||||
| | | | | +--->BN_MP_LSHD_C
|
| | | | | +--->BN_MP_LSHD_C
|
||||||
@ -7465,6 +7480,7 @@ BN_MP_LCM_C
|
|||||||
| +--->BN_MP_ZERO_C
|
| +--->BN_MP_ZERO_C
|
||||||
| +--->BN_MP_SET_C
|
| +--->BN_MP_SET_C
|
||||||
| +--->BN_MP_COUNT_BITS_C
|
| +--->BN_MP_COUNT_BITS_C
|
||||||
|
| +--->BN_MP_ABS_C
|
||||||
| +--->BN_MP_MUL_2D_C
|
| +--->BN_MP_MUL_2D_C
|
||||||
| | +--->BN_MP_GROW_C
|
| | +--->BN_MP_GROW_C
|
||||||
| | +--->BN_MP_LSHD_C
|
| | +--->BN_MP_LSHD_C
|
||||||
@ -7928,6 +7944,7 @@ BN_S_MP_EXPTMOD_C
|
|||||||
| | +--->BN_MP_ZERO_C
|
| | +--->BN_MP_ZERO_C
|
||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -7974,6 +7991,7 @@ BN_S_MP_EXPTMOD_C
|
|||||||
| | +--->BN_MP_ZERO_C
|
| | +--->BN_MP_ZERO_C
|
||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -8372,6 +8390,7 @@ BN_MP_DIV_C
|
|||||||
| +--->BN_MP_CLEAR_C
|
| +--->BN_MP_CLEAR_C
|
||||||
+--->BN_MP_SET_C
|
+--->BN_MP_SET_C
|
||||||
+--->BN_MP_COUNT_BITS_C
|
+--->BN_MP_COUNT_BITS_C
|
||||||
|
+--->BN_MP_ABS_C
|
||||||
+--->BN_MP_MUL_2D_C
|
+--->BN_MP_MUL_2D_C
|
||||||
| +--->BN_MP_GROW_C
|
| +--->BN_MP_GROW_C
|
||||||
| +--->BN_MP_LSHD_C
|
| +--->BN_MP_LSHD_C
|
||||||
@ -8465,6 +8484,7 @@ BN_MP_ADDMOD_C
|
|||||||
| | +--->BN_MP_INIT_MULTI_C
|
| | +--->BN_MP_INIT_MULTI_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -8551,6 +8571,7 @@ BN_MP_REDUCE_C
|
|||||||
| | | +--->BN_MP_CLEAR_C
|
| | | +--->BN_MP_CLEAR_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -8766,6 +8787,7 @@ BN_MP_JACOBI_C
|
|||||||
| | | +--->BN_MP_CLEAR_C
|
| | | +--->BN_MP_CLEAR_C
|
||||||
| | +--->BN_MP_SET_C
|
| | +--->BN_MP_SET_C
|
||||||
| | +--->BN_MP_COUNT_BITS_C
|
| | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | +--->BN_MP_ABS_C
|
||||||
| | +--->BN_MP_MUL_2D_C
|
| | +--->BN_MP_MUL_2D_C
|
||||||
| | | +--->BN_MP_GROW_C
|
| | | +--->BN_MP_GROW_C
|
||||||
| | | +--->BN_MP_LSHD_C
|
| | | +--->BN_MP_LSHD_C
|
||||||
@ -8912,6 +8934,7 @@ BN_MP_EXTEUCLID_C
|
|||||||
| +--->BN_MP_CMP_MAG_C
|
| +--->BN_MP_CMP_MAG_C
|
||||||
| +--->BN_MP_ZERO_C
|
| +--->BN_MP_ZERO_C
|
||||||
| +--->BN_MP_COUNT_BITS_C
|
| +--->BN_MP_COUNT_BITS_C
|
||||||
|
| +--->BN_MP_ABS_C
|
||||||
| +--->BN_MP_MUL_2D_C
|
| +--->BN_MP_MUL_2D_C
|
||||||
| | +--->BN_MP_GROW_C
|
| | +--->BN_MP_GROW_C
|
||||||
| | +--->BN_MP_LSHD_C
|
| | +--->BN_MP_LSHD_C
|
||||||
@ -9078,6 +9101,7 @@ BN_MP_REDUCE_SETUP_C
|
|||||||
| | +--->BN_MP_CLEAR_C
|
| | +--->BN_MP_CLEAR_C
|
||||||
| +--->BN_MP_SET_C
|
| +--->BN_MP_SET_C
|
||||||
| +--->BN_MP_COUNT_BITS_C
|
| +--->BN_MP_COUNT_BITS_C
|
||||||
|
| +--->BN_MP_ABS_C
|
||||||
| +--->BN_MP_MUL_2D_C
|
| +--->BN_MP_MUL_2D_C
|
||||||
| | +--->BN_MP_GROW_C
|
| | +--->BN_MP_GROW_C
|
||||||
| | +--->BN_MP_LSHD_C
|
| | +--->BN_MP_LSHD_C
|
||||||
@ -10118,6 +10142,7 @@ BN_MP_PRIME_MILLER_RABIN_C
|
|||||||
| | | +--->BN_MP_INIT_MULTI_C
|
| | | +--->BN_MP_INIT_MULTI_C
|
||||||
| | | +--->BN_MP_SET_C
|
| | | +--->BN_MP_SET_C
|
||||||
| | | +--->BN_MP_COUNT_BITS_C
|
| | | +--->BN_MP_COUNT_BITS_C
|
||||||
|
| | | +--->BN_MP_ABS_C
|
||||||
| | | +--->BN_MP_MUL_2D_C
|
| | | +--->BN_MP_MUL_2D_C
|
||||||
| | | | +--->BN_MP_GROW_C
|
| | | | +--->BN_MP_GROW_C
|
||||||
| | | | +--->BN_MP_LSHD_C
|
| | | | +--->BN_MP_LSHD_C
|
||||||
|
@ -1,3 +1,12 @@
|
|||||||
|
December 23rd, 2004
|
||||||
|
v0.33 -- Fixed "small" variant for mp_div() which would munge with negative dividends...
|
||||||
|
-- Fixed bug in mp_prime_random_ex() which would set the most significant byte to zero when
|
||||||
|
no special flags were set
|
||||||
|
-- Fixed overflow [minor] bug in fast_s_mp_sqr()
|
||||||
|
-- Made the makefiles easier to configure the group/user that ltm will install as
|
||||||
|
-- Fixed "final carry" bug in comba multipliers. (Volkan Ceylan)
|
||||||
|
-- Matt Johnston pointed out a missing semi-colon in mp_exptmod
|
||||||
|
|
||||||
October 29th, 2004
|
October 29th, 2004
|
||||||
v0.32 -- Added "makefile.shared" for shared object support
|
v0.32 -- Added "makefile.shared" for shared object support
|
||||||
-- Added more to the build options/configs in the manual
|
-- Added more to the build options/configs in the manual
|
||||||
|
@ -11,9 +11,9 @@
|
|||||||
|
|
||||||
void ndraw(mp_int *a, char *name)
|
void ndraw(mp_int *a, char *name)
|
||||||
{
|
{
|
||||||
char buf[4096];
|
char buf[16000];
|
||||||
printf("%s: ", name);
|
printf("%s: ", name);
|
||||||
mp_toradix(a, buf, 64);
|
mp_toradix(a, buf, 10);
|
||||||
printf("%s\n", buf);
|
printf("%s\n", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,7 +395,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
|
|||||||
|
|
||||||
mp_div(&a, &b, &e, &f);
|
mp_div(&a, &b, &e, &f);
|
||||||
if (mp_cmp(&c, &e) != MP_EQ || mp_cmp(&d, &f) != MP_EQ) {
|
if (mp_cmp(&c, &e) != MP_EQ || mp_cmp(&d, &f) != MP_EQ) {
|
||||||
printf("div %lu failure!\n", div_n);
|
printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e), mp_cmp(&d, &f));
|
||||||
draw(&a);draw(&b);draw(&c);draw(&d); draw(&e); draw(&f);
|
draw(&a);draw(&b);draw(&c);draw(&d); draw(&e); draw(&f);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -38,14 +38,13 @@ int lbit(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__i386__) || defined(_M_IX86) || defined(_M_AMD64)
|
|
||||||
/* RDTSC from Scott Duplichan */
|
/* RDTSC from Scott Duplichan */
|
||||||
static ulong64 TIMFUNC (void)
|
static ulong64 TIMFUNC (void)
|
||||||
{
|
{
|
||||||
#if defined __GNUC__
|
#if defined __GNUC__
|
||||||
#ifdef __i386__
|
#if defined(__i386__) || defined(__x86_64__)
|
||||||
ulong64 a;
|
unsigned long long a;
|
||||||
__asm__ __volatile__ ("rdtsc ":"=A" (a));
|
__asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx");
|
||||||
return a;
|
return a;
|
||||||
#else /* gcc-IA64 version */
|
#else /* gcc-IA64 version */
|
||||||
unsigned long result;
|
unsigned long result;
|
||||||
@ -69,9 +68,6 @@ static ulong64 TIMFUNC (void)
|
|||||||
#error need rdtsc function for this build
|
#error need rdtsc function for this build
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
#define TIMFUNC clock
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define DO(x) x; x;
|
#define DO(x) x; x;
|
||||||
//#define DO4(x) DO2(x); DO2(x);
|
//#define DO4(x) DO2(x); DO2(x);
|
||||||
|
@ -18,15 +18,15 @@ is_mersenne (long s, int *pp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&u)) != MP_OKAY) {
|
if ((res = mp_init (&u)) != MP_OKAY) {
|
||||||
goto __N;
|
goto LBL_N;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* n = 2^s - 1 */
|
/* n = 2^s - 1 */
|
||||||
if ((res = mp_2expt(&n, s)) != MP_OKAY) {
|
if ((res = mp_2expt(&n, s)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub_d (&n, 1, &n)) != MP_OKAY) {
|
if ((res = mp_sub_d (&n, 1, &n)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set u=4 */
|
/* set u=4 */
|
||||||
@ -36,22 +36,22 @@ is_mersenne (long s, int *pp)
|
|||||||
for (k = 1; k <= s - 2; k++) {
|
for (k = 1; k <= s - 2; k++) {
|
||||||
/* u = u^2 - 2 mod n */
|
/* u = u^2 - 2 mod n */
|
||||||
if ((res = mp_sqr (&u, &u)) != MP_OKAY) {
|
if ((res = mp_sqr (&u, &u)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub_d (&u, 2, &u)) != MP_OKAY) {
|
if ((res = mp_sub_d (&u, 2, &u)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* make sure u is positive */
|
/* make sure u is positive */
|
||||||
while (u.sign == MP_NEG) {
|
while (u.sign == MP_NEG) {
|
||||||
if ((res = mp_add (&u, &n, &u)) != MP_OKAY) {
|
if ((res = mp_add (&u, &n, &u)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* reduce */
|
/* reduce */
|
||||||
if ((res = mp_reduce_2k (&u, &n, 1)) != MP_OKAY) {
|
if ((res = mp_reduce_2k (&u, &n, 1)) != MP_OKAY) {
|
||||||
goto __MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,8 +62,8 @@ is_mersenne (long s, int *pp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
__MU:mp_clear (&u);
|
LBL_MU:mp_clear (&u);
|
||||||
__N:mp_clear (&n);
|
LBL_N:mp_clear (&n);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
54
etc/pprime.c
54
etc/pprime.c
@ -189,7 +189,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&v)) != MP_OKAY) {
|
if ((res = mp_init (&v)) != MP_OKAY) {
|
||||||
goto __C;
|
goto LBL_C;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* product of first 50 primes */
|
/* product of first 50 primes */
|
||||||
@ -197,34 +197,34 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
mp_read_radix (&v,
|
mp_read_radix (&v,
|
||||||
"19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
|
"19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
|
||||||
10)) != MP_OKAY) {
|
10)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&a)) != MP_OKAY) {
|
if ((res = mp_init (&a)) != MP_OKAY) {
|
||||||
goto __V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set the prime */
|
/* set the prime */
|
||||||
mp_set (&a, prime_digit ());
|
mp_set (&a, prime_digit ());
|
||||||
|
|
||||||
if ((res = mp_init (&b)) != MP_OKAY) {
|
if ((res = mp_init (&b)) != MP_OKAY) {
|
||||||
goto __A;
|
goto LBL_A;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&n)) != MP_OKAY) {
|
if ((res = mp_init (&n)) != MP_OKAY) {
|
||||||
goto __B;
|
goto LBL_B;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&x)) != MP_OKAY) {
|
if ((res = mp_init (&x)) != MP_OKAY) {
|
||||||
goto __N;
|
goto LBL_N;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&y)) != MP_OKAY) {
|
if ((res = mp_init (&y)) != MP_OKAY) {
|
||||||
goto __X;
|
goto LBL_X;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init (&z)) != MP_OKAY) {
|
if ((res = mp_init (&z)) != MP_OKAY) {
|
||||||
goto __Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now loop making the single digit */
|
/* now loop making the single digit */
|
||||||
@ -236,25 +236,25 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
|
|
||||||
/* now compute z = a * b * 2 */
|
/* now compute z = a * b * 2 */
|
||||||
if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */
|
if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */
|
if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
|
if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* n = z + 1 */
|
/* n = z + 1 */
|
||||||
if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
|
if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check (n, v) == 1 */
|
/* check (n, v) == 1 */
|
||||||
if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
|
if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp_d (&y, 1) != MP_EQ)
|
if (mp_cmp_d (&y, 1) != MP_EQ)
|
||||||
@ -266,7 +266,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
|
|
||||||
/* compute x^a mod n */
|
/* compute x^a mod n */
|
||||||
if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
|
if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if y == 1 loop */
|
/* if y == 1 loop */
|
||||||
@ -275,7 +275,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
|
|
||||||
/* now x^2a mod n */
|
/* now x^2a mod n */
|
||||||
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
|
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp_d (&y, 1) == MP_EQ)
|
if (mp_cmp_d (&y, 1) == MP_EQ)
|
||||||
@ -283,7 +283,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
|
|
||||||
/* compute x^b mod n */
|
/* compute x^b mod n */
|
||||||
if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
|
if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if y == 1 loop */
|
/* if y == 1 loop */
|
||||||
@ -292,7 +292,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
|
|
||||||
/* now x^2b mod n */
|
/* now x^2b mod n */
|
||||||
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
|
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp_d (&y, 1) == MP_EQ)
|
if (mp_cmp_d (&y, 1) == MP_EQ)
|
||||||
@ -300,7 +300,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
|
|
||||||
/* compute x^c mod n == x^ab mod n */
|
/* compute x^c mod n == x^ab mod n */
|
||||||
if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
|
if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if y == 1 loop */
|
/* if y == 1 loop */
|
||||||
@ -309,7 +309,7 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
|
|
||||||
/* now compute (x^c mod n)^2 */
|
/* now compute (x^c mod n)^2 */
|
||||||
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
|
if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
|
||||||
goto __Z;
|
goto LBL_Z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* y should be 1 */
|
/* y should be 1 */
|
||||||
@ -346,14 +346,14 @@ pprime (int k, int li, mp_int * p, mp_int * q)
|
|||||||
mp_exch (&n, p);
|
mp_exch (&n, p);
|
||||||
|
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
__Z:mp_clear (&z);
|
LBL_Z:mp_clear (&z);
|
||||||
__Y:mp_clear (&y);
|
LBL_Y:mp_clear (&y);
|
||||||
__X:mp_clear (&x);
|
LBL_X:mp_clear (&x);
|
||||||
__N:mp_clear (&n);
|
LBL_N:mp_clear (&n);
|
||||||
__B:mp_clear (&b);
|
LBL_B:mp_clear (&b);
|
||||||
__A:mp_clear (&a);
|
LBL_A:mp_clear (&a);
|
||||||
__V:mp_clear (&v);
|
LBL_V:mp_clear (&v);
|
||||||
__C:mp_clear (&c);
|
LBL_C:mp_clear (&c);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,9 +14,9 @@
|
|||||||
#ifndef X86_TIMER
|
#ifndef X86_TIMER
|
||||||
|
|
||||||
/* generic ISO C timer */
|
/* generic ISO C timer */
|
||||||
ulong64 __T;
|
ulong64 LBL_T;
|
||||||
void t_start(void) { __T = clock(); }
|
void t_start(void) { LBL_T = clock(); }
|
||||||
ulong64 t_read(void) { return clock() - __T; }
|
ulong64 t_read(void) { return clock() - LBL_T; }
|
||||||
|
|
||||||
#else
|
#else
|
||||||
extern void t_start(void);
|
extern void t_start(void);
|
||||||
|
32
logs/add.log
32
logs/add.log
@ -1,16 +1,16 @@
|
|||||||
224 222
|
480 88
|
||||||
448 330
|
960 113
|
||||||
672 436
|
1440 138
|
||||||
896 520
|
1920 163
|
||||||
1120 612
|
2400 202
|
||||||
1344 696
|
2880 226
|
||||||
1568 810
|
3360 251
|
||||||
1792 912
|
3840 272
|
||||||
2016 1006
|
4320 296
|
||||||
2240 1116
|
4800 320
|
||||||
2464 1152
|
5280 344
|
||||||
2688 1284
|
5760 368
|
||||||
2912 1348
|
6240 392
|
||||||
3136 1486
|
6720 416
|
||||||
3360 1580
|
7200 440
|
||||||
3584 1636
|
7680 464
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
513 1499509
|
||||||
|
769 3682671
|
||||||
|
1025 8098887
|
||||||
|
2049 49332743
|
||||||
|
2561 89647783
|
||||||
|
3073 149440713
|
||||||
|
4097 326135364
|
@ -0,0 +1,6 @@
|
|||||||
|
521 1423346
|
||||||
|
607 1841305
|
||||||
|
1279 8375656
|
||||||
|
2203 34104708
|
||||||
|
3217 83830729
|
||||||
|
4253 167916804
|
@ -0,0 +1,7 @@
|
|||||||
|
532 1803110
|
||||||
|
784 3607375
|
||||||
|
1036 6089790
|
||||||
|
1540 14739797
|
||||||
|
2072 33251589
|
||||||
|
3080 82794331
|
||||||
|
4116 165212734
|
286
logs/mult.log
286
logs/mult.log
@ -1,143 +1,143 @@
|
|||||||
140 1272
|
271 580
|
||||||
195 1428
|
390 861
|
||||||
252 1996
|
511 1177
|
||||||
307 2586
|
630 1598
|
||||||
364 3464
|
749 2115
|
||||||
420 4420
|
871 2670
|
||||||
476 5260
|
991 3276
|
||||||
532 6430
|
1111 3987
|
||||||
588 7692
|
1231 4722
|
||||||
644 8704
|
1351 5474
|
||||||
699 10226
|
1471 6281
|
||||||
755 11670
|
1589 7126
|
||||||
812 13190
|
1710 8114
|
||||||
865 14834
|
1831 8988
|
||||||
924 16738
|
1946 10038
|
||||||
979 18362
|
2071 10995
|
||||||
1036 20660
|
2188 12286
|
||||||
1092 22776
|
2310 13152
|
||||||
1148 24848
|
2430 14480
|
||||||
1204 27168
|
2549 15521
|
||||||
1260 29930
|
2671 17171
|
||||||
1316 32258
|
2790 18081
|
||||||
1370 35172
|
2911 19754
|
||||||
1422 37534
|
3031 20809
|
||||||
1482 40390
|
3150 22849
|
||||||
1537 43990
|
3269 23757
|
||||||
1589 46946
|
3391 25772
|
||||||
1652 50438
|
3508 26832
|
||||||
1703 52902
|
3631 29304
|
||||||
1764 56646
|
3750 30149
|
||||||
1820 59892
|
3865 32581
|
||||||
1876 63248
|
3988 33644
|
||||||
1932 66872
|
4111 36565
|
||||||
1988 72596
|
4231 37309
|
||||||
2042 74662
|
4351 40152
|
||||||
2100 78512
|
4471 41188
|
||||||
2156 82944
|
4590 44658
|
||||||
2211 87444
|
4710 45256
|
||||||
2268 92170
|
4827 48538
|
||||||
2324 95534
|
4951 49490
|
||||||
2380 100484
|
5070 53472
|
||||||
2435 105024
|
5190 53902
|
||||||
2491 109460
|
5308 57619
|
||||||
2546 114154
|
5431 58509
|
||||||
2603 118946
|
5550 63044
|
||||||
2660 124110
|
5664 63333
|
||||||
2716 129300
|
5791 67542
|
||||||
2771 134274
|
5911 68279
|
||||||
2828 139594
|
6028 73477
|
||||||
2883 145234
|
6150 73475
|
||||||
2939 150332
|
6271 78189
|
||||||
2996 155750
|
6390 78842
|
||||||
3048 161718
|
6510 84691
|
||||||
3108 167492
|
6631 84444
|
||||||
3162 173882
|
6751 89721
|
||||||
3219 179766
|
6871 90186
|
||||||
3276 185560
|
6991 96665
|
||||||
3330 191826
|
7111 96119
|
||||||
3388 197822
|
7231 101937
|
||||||
3442 204176
|
7350 102212
|
||||||
3500 210682
|
7471 109439
|
||||||
3556 217236
|
7591 108491
|
||||||
3612 223484
|
7709 114965
|
||||||
3666 230714
|
7829 115025
|
||||||
3724 237744
|
7951 123002
|
||||||
3779 244080
|
8071 121630
|
||||||
3835 250970
|
8190 128725
|
||||||
3890 257914
|
8311 128536
|
||||||
3947 265162
|
8430 137298
|
||||||
4001 272128
|
8550 135568
|
||||||
4060 279108
|
8671 143265
|
||||||
4116 287606
|
8791 142793
|
||||||
4171 294716
|
8911 152432
|
||||||
4227 302806
|
9030 150202
|
||||||
4284 310260
|
9151 158616
|
||||||
4340 318564
|
9271 157848
|
||||||
4395 326164
|
9391 168374
|
||||||
4443 334034
|
9511 165651
|
||||||
4508 342108
|
9627 174775
|
||||||
4561 351810
|
9750 173375
|
||||||
4618 358828
|
9871 185067
|
||||||
4675 367332
|
9985 181845
|
||||||
4732 376140
|
10111 191708
|
||||||
4787 384172
|
10229 190239
|
||||||
4841 393308
|
10351 202585
|
||||||
4899 402036
|
10467 198704
|
||||||
4955 411286
|
10591 209193
|
||||||
5010 420290
|
10711 207322
|
||||||
5067 429688
|
10831 220842
|
||||||
5124 438810
|
10950 215882
|
||||||
5180 448130
|
11071 227761
|
||||||
5235 457264
|
11191 225501
|
||||||
5290 467390
|
11311 239669
|
||||||
5348 476586
|
11430 234809
|
||||||
5404 486120
|
11550 243511
|
||||||
5459 496512
|
11671 255947
|
||||||
5516 506624
|
11791 255243
|
||||||
5569 516346
|
11906 267828
|
||||||
5628 526604
|
12029 263437
|
||||||
5684 536544
|
12149 276571
|
||||||
5740 546936
|
12270 275579
|
||||||
5796 557284
|
12390 288963
|
||||||
5852 568106
|
12510 284001
|
||||||
5907 578824
|
12631 298196
|
||||||
5963 589204
|
12751 297018
|
||||||
6019 600176
|
12869 310848
|
||||||
6076 610564
|
12990 305369
|
||||||
6127 621972
|
13111 319086
|
||||||
6188 633564
|
13230 318940
|
||||||
6244 644730
|
13349 333685
|
||||||
6300 655288
|
13471 327495
|
||||||
6354 667402
|
13588 343678
|
||||||
6412 678824
|
13711 341817
|
||||||
6467 690594
|
13831 357181
|
||||||
6522 702718
|
13948 350440
|
||||||
6580 714148
|
14071 367526
|
||||||
6636 725608
|
14189 365330
|
||||||
6690 737834
|
14311 381551
|
||||||
6747 750100
|
14429 374149
|
||||||
6804 762202
|
14549 392203
|
||||||
6860 774184
|
14670 389764
|
||||||
6916 787298
|
14791 406761
|
||||||
6971 798734
|
14910 398652
|
||||||
7028 811162
|
15026 417718
|
||||||
7083 824570
|
15150 414733
|
||||||
7139 837738
|
15269 432759
|
||||||
7196 2579488
|
15390 1037071
|
||||||
7245 2626714
|
15511 1053454
|
||||||
7308 2643582
|
15631 1069198
|
||||||
7364 2698746
|
15748 1086164
|
||||||
7416 2734106
|
15871 1112820
|
||||||
7476 2773372
|
15991 1129676
|
||||||
7530 2816738
|
16111 1145924
|
||||||
7588 2859204
|
16230 1163016
|
||||||
7643 2938596
|
16345 1179911
|
||||||
7698 2919716
|
16471 1197048
|
||||||
7754 2988542
|
16586 1214352
|
||||||
7812 3026520
|
16711 1232095
|
||||||
7867 3058304
|
16829 1249338
|
||||||
7924 3115790
|
16947 1266987
|
||||||
7977 3161450
|
17071 1284181
|
||||||
8035 3203138
|
17188 1302521
|
||||||
8092 3244056
|
17311 1320539
|
||||||
|
286
logs/sqr.log
286
logs/sqr.log
@ -1,143 +1,143 @@
|
|||||||
139 806
|
271 552
|
||||||
195 1212
|
389 883
|
||||||
252 1604
|
510 1191
|
||||||
307 2260
|
629 1572
|
||||||
364 2892
|
750 1996
|
||||||
420 3308
|
863 2428
|
||||||
476 4152
|
991 2891
|
||||||
532 4814
|
1108 3539
|
||||||
588 5754
|
1231 4182
|
||||||
644 6684
|
1351 4980
|
||||||
700 7226
|
1471 5771
|
||||||
756 8324
|
1590 6551
|
||||||
808 9092
|
1711 7313
|
||||||
866 10068
|
1830 8240
|
||||||
924 11204
|
1951 9184
|
||||||
976 12918
|
2070 10087
|
||||||
1036 13656
|
2191 11140
|
||||||
1092 15248
|
2311 12111
|
||||||
1148 15956
|
2431 13219
|
||||||
1204 17270
|
2550 14247
|
||||||
1260 19894
|
2669 15353
|
||||||
1316 20516
|
2791 16446
|
||||||
1370 21864
|
2911 17692
|
||||||
1428 25554
|
3029 18848
|
||||||
1483 26138
|
3151 20028
|
||||||
1540 27086
|
3268 21282
|
||||||
1596 29246
|
3391 22696
|
||||||
1652 32210
|
3511 23971
|
||||||
1707 32704
|
3631 25303
|
||||||
1764 35142
|
3751 26675
|
||||||
1820 39050
|
3871 28245
|
||||||
1876 39256
|
3990 29736
|
||||||
1931 41574
|
4111 31124
|
||||||
1985 45070
|
4229 32714
|
||||||
2044 46352
|
4347 34397
|
||||||
2099 48114
|
4471 35877
|
||||||
2155 51332
|
4587 37269
|
||||||
2212 53268
|
4710 39011
|
||||||
2267 55890
|
4831 40884
|
||||||
2324 59054
|
4950 42501
|
||||||
2380 60206
|
5070 44005
|
||||||
2434 63540
|
5191 46026
|
||||||
2491 66084
|
5310 48168
|
||||||
2547 68590
|
5431 49801
|
||||||
2604 74332
|
5551 51385
|
||||||
2660 74784
|
5671 53604
|
||||||
2715 77974
|
5787 55942
|
||||||
2772 79924
|
5910 57757
|
||||||
2826 82914
|
6031 59391
|
||||||
2884 87210
|
6151 61754
|
||||||
2929 89076
|
6271 64234
|
||||||
2996 92480
|
6390 66110
|
||||||
3052 96814
|
6511 67845
|
||||||
3108 99990
|
6627 70474
|
||||||
3162 102550
|
6751 73113
|
||||||
3219 105396
|
6871 75064
|
||||||
3276 109284
|
6990 76940
|
||||||
3332 113752
|
7111 79681
|
||||||
3387 116628
|
7230 82548
|
||||||
3444 120782
|
7351 84597
|
||||||
3500 122938
|
7471 86507
|
||||||
3556 127940
|
7591 89497
|
||||||
3612 303656
|
7711 225216
|
||||||
3667 312212
|
7831 232192
|
||||||
3724 324376
|
7951 239583
|
||||||
3779 329204
|
8071 247302
|
||||||
3833 340910
|
8191 255497
|
||||||
3892 353850
|
8308 261587
|
||||||
3943 362348
|
8431 271490
|
||||||
4003 367780
|
8550 279492
|
||||||
4056 380448
|
8671 286927
|
||||||
4114 393616
|
8790 294680
|
||||||
4172 404104
|
8910 302974
|
||||||
4227 415148
|
9030 311300
|
||||||
4284 409770
|
9150 318635
|
||||||
4339 436648
|
9271 326740
|
||||||
4394 442970
|
9390 335304
|
||||||
4451 463096
|
9511 344297
|
||||||
4507 472056
|
9630 352056
|
||||||
4564 485780
|
9748 358652
|
||||||
4616 496286
|
9870 369723
|
||||||
4675 507612
|
9991 379119
|
||||||
4732 519524
|
10111 386982
|
||||||
4788 536768
|
10231 396075
|
||||||
4843 542754
|
10349 404396
|
||||||
4899 553090
|
10470 415375
|
||||||
4956 571986
|
10590 424146
|
||||||
5012 586340
|
10711 433390
|
||||||
5068 599606
|
10829 442662
|
||||||
5124 613670
|
10950 453238
|
||||||
5179 624256
|
11071 462178
|
||||||
5235 636266
|
11186 469811
|
||||||
5292 655518
|
11311 482529
|
||||||
5348 668142
|
11431 493214
|
||||||
5403 677266
|
11550 503210
|
||||||
5460 696040
|
11671 513486
|
||||||
5516 712772
|
11791 524244
|
||||||
5570 723942
|
11911 535277
|
||||||
5628 739052
|
12031 544872
|
||||||
5684 755350
|
12151 555695
|
||||||
5739 769962
|
12271 566893
|
||||||
5790 775258
|
12391 578385
|
||||||
5851 790128
|
12510 588658
|
||||||
5908 814536
|
12628 596914
|
||||||
5962 827278
|
12751 611324
|
||||||
6018 844510
|
12871 623437
|
||||||
6076 851606
|
12991 633907
|
||||||
6130 865748
|
13110 645605
|
||||||
6188 894752
|
13231 657684
|
||||||
6244 900474
|
13351 670037
|
||||||
6300 928174
|
13471 680939
|
||||||
6356 928440
|
13591 693047
|
||||||
6410 957758
|
13710 705363
|
||||||
6468 981134
|
13829 718178
|
||||||
6524 994088
|
13949 727930
|
||||||
6580 1011124
|
14069 739641
|
||||||
6636 1027178
|
14190 754817
|
||||||
6692 1045466
|
14310 768192
|
||||||
6747 1056910
|
14431 779875
|
||||||
6804 1083784
|
14551 792655
|
||||||
6860 1104706
|
14667 802847
|
||||||
6915 1116450
|
14791 819806
|
||||||
6972 1137894
|
14911 831684
|
||||||
7028 1154670
|
15031 844936
|
||||||
7084 1158064
|
15151 858813
|
||||||
7138 1188734
|
15270 873037
|
||||||
7196 1214218
|
15387 882123
|
||||||
7249 1226822
|
15510 899117
|
||||||
7307 1247528
|
15631 913465
|
||||||
7363 1255338
|
15750 927989
|
||||||
7420 1291104
|
15870 940790
|
||||||
7475 1297940
|
15991 954948
|
||||||
7532 1324994
|
16110 969483
|
||||||
7587 1340274
|
16231 984544
|
||||||
7644 1342596
|
16350 997837
|
||||||
7698 1381418
|
16470 1012445
|
||||||
7756 1382904
|
16590 1027834
|
||||||
7812 1432588
|
16710 1043032
|
||||||
7867 1443632
|
16831 1056394
|
||||||
7922 1465092
|
16951 1071408
|
||||||
7979 1496804
|
17069 1097263
|
||||||
8036 1520142
|
17191 1113364
|
||||||
8092 1539566
|
17306 1123650
|
||||||
|
32
logs/sub.log
32
logs/sub.log
@ -1,16 +1,16 @@
|
|||||||
224 216
|
480 87
|
||||||
448 324
|
960 114
|
||||||
672 428
|
1440 139
|
||||||
896 532
|
1920 159
|
||||||
1120 648
|
2400 204
|
||||||
1344 766
|
2880 228
|
||||||
1568 862
|
3360 250
|
||||||
1792 928
|
3840 273
|
||||||
2016 1070
|
4320 300
|
||||||
2240 1128
|
4800 321
|
||||||
2464 1250
|
5280 348
|
||||||
2688 1344
|
5760 370
|
||||||
2912 1436
|
6240 393
|
||||||
3136 1542
|
6720 420
|
||||||
3360 1628
|
7200 444
|
||||||
3584 1696
|
7680 466
|
||||||
|
27
makefile
27
makefile
@ -1,10 +1,14 @@
|
|||||||
#Makefile for GCC
|
#Makefile for GCC
|
||||||
#
|
#
|
||||||
#Tom St Denis
|
#Tom St Denis
|
||||||
|
|
||||||
|
#version of library
|
||||||
|
VERSION=0.33
|
||||||
|
|
||||||
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
|
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
|
||||||
|
|
||||||
#for speed
|
#for speed
|
||||||
CFLAGS += -O3 -funroll-loops
|
CFLAGS += -O3 -funroll-all-loops
|
||||||
|
|
||||||
#for size
|
#for size
|
||||||
#CFLAGS += -Os
|
#CFLAGS += -Os
|
||||||
@ -15,13 +19,15 @@ CFLAGS += -fomit-frame-pointer
|
|||||||
#debug
|
#debug
|
||||||
#CFLAGS += -g3
|
#CFLAGS += -g3
|
||||||
|
|
||||||
VERSION=0.32
|
#install as this user
|
||||||
|
USER=root
|
||||||
|
GROUP=root
|
||||||
|
|
||||||
default: libtommath.a
|
default: libtommath.a
|
||||||
|
|
||||||
#default files to install
|
#default files to install
|
||||||
LIBNAME=libtommath.a
|
LIBNAME=libtommath.a
|
||||||
HEADERS=tommath.h
|
HEADERS=tommath.h tommath_class.h tommath_superclass.h
|
||||||
|
|
||||||
#LIBPATH-The directory for libtommath to be installed to.
|
#LIBPATH-The directory for libtommath to be installed to.
|
||||||
#INCPATH-The directory to install the header files for libtommath.
|
#INCPATH-The directory to install the header files for libtommath.
|
||||||
@ -61,7 +67,6 @@ libtommath.a: $(OBJECTS)
|
|||||||
$(AR) $(ARFLAGS) libtommath.a $(OBJECTS)
|
$(AR) $(ARFLAGS) libtommath.a $(OBJECTS)
|
||||||
ranlib libtommath.a
|
ranlib libtommath.a
|
||||||
|
|
||||||
|
|
||||||
#make a profiled library (takes a while!!!)
|
#make a profiled library (takes a while!!!)
|
||||||
#
|
#
|
||||||
# This will build the library with profile generation
|
# This will build the library with profile generation
|
||||||
@ -86,19 +91,19 @@ profiled_single:
|
|||||||
ranlib libtommath.a
|
ranlib libtommath.a
|
||||||
|
|
||||||
install: libtommath.a
|
install: libtommath.a
|
||||||
install -d -g root -o root $(DESTDIR)$(LIBPATH)
|
install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH)
|
||||||
install -d -g root -o root $(DESTDIR)$(INCPATH)
|
install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
|
||||||
install -g root -o root $(LIBNAME) $(DESTDIR)$(LIBPATH)
|
install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH)
|
||||||
install -g root -o root $(HEADERS) $(DESTDIR)$(INCPATH)
|
install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)
|
||||||
|
|
||||||
test: libtommath.a demo/demo.o
|
test: libtommath.a demo/demo.o
|
||||||
$(CC) demo/demo.o libtommath.a -o test
|
$(CC) $(CFLAGS) demo/demo.o libtommath.a -o test
|
||||||
|
|
||||||
mtest: test
|
mtest: test
|
||||||
cd mtest ; $(CC) $(CFLAGS) mtest.c -o mtest -s
|
cd mtest ; $(CC) $(CFLAGS) mtest.c -o mtest
|
||||||
|
|
||||||
timing: libtommath.a
|
timing: libtommath.a
|
||||||
$(CC) $(CFLAGS) -DTIMER demo/timing.c libtommath.a -o ltmtest -s
|
$(CC) $(CFLAGS) -DTIMER demo/timing.c libtommath.a -o ltmtest
|
||||||
|
|
||||||
# makes the LTM book DVI file, requires tetex, perl and makeindex [part of tetex I think]
|
# makes the LTM book DVI file, requires tetex, perl and makeindex [part of tetex I think]
|
||||||
docdvi: tommath.src
|
docdvi: tommath.src
|
||||||
|
12
makefile.icc
12
makefile.icc
@ -21,6 +21,10 @@ CFLAGS += -I./
|
|||||||
# Default to just generic max opts
|
# Default to just generic max opts
|
||||||
CFLAGS += -O3 -xN
|
CFLAGS += -O3 -xN
|
||||||
|
|
||||||
|
#install as this user
|
||||||
|
USER=root
|
||||||
|
GROUP=root
|
||||||
|
|
||||||
default: libtommath.a
|
default: libtommath.a
|
||||||
|
|
||||||
#default files to install
|
#default files to install
|
||||||
@ -89,10 +93,10 @@ profiled_single:
|
|||||||
ranlib libtommath.a
|
ranlib libtommath.a
|
||||||
|
|
||||||
install: libtommath.a
|
install: libtommath.a
|
||||||
install -d -g root -o root $(DESTDIR)$(LIBPATH)
|
install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH)
|
||||||
install -d -g root -o root $(DESTDIR)$(INCPATH)
|
install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
|
||||||
install -g root -o root $(LIBNAME) $(DESTDIR)$(LIBPATH)
|
install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH)
|
||||||
install -g root -o root $(HEADERS) $(DESTDIR)$(INCPATH)
|
install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)
|
||||||
|
|
||||||
test: libtommath.a demo/demo.o
|
test: libtommath.a demo/demo.o
|
||||||
$(CC) demo/demo.o libtommath.a -o test
|
$(CC) demo/demo.o libtommath.a -o test
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
#Makefile for GCC
|
#Makefile for GCC
|
||||||
#
|
#
|
||||||
#Tom St Denis
|
#Tom St Denis
|
||||||
VERSION=0:32
|
VERSION=0:33
|
||||||
|
|
||||||
CC = libtool --mode=compile gcc
|
CC = libtool --mode=compile gcc
|
||||||
|
|
||||||
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
|
CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare
|
||||||
|
|
||||||
#for speed
|
#for speed
|
||||||
@ -16,11 +15,15 @@ CFLAGS += -O3 -funroll-loops
|
|||||||
#x86 optimizations [should be valid for any GCC install though]
|
#x86 optimizations [should be valid for any GCC install though]
|
||||||
CFLAGS += -fomit-frame-pointer
|
CFLAGS += -fomit-frame-pointer
|
||||||
|
|
||||||
|
#install as this user
|
||||||
|
USER=root
|
||||||
|
GROUP=root
|
||||||
|
|
||||||
default: libtommath.la
|
default: libtommath.la
|
||||||
|
|
||||||
#default files to install
|
#default files to install
|
||||||
LIBNAME=libtommath.la
|
LIBNAME=libtommath.la
|
||||||
HEADERS=tommath.h
|
HEADERS=tommath.h tommath_class.h tommath_superclass.h
|
||||||
|
|
||||||
#LIBPATH-The directory for libtommath to be installed to.
|
#LIBPATH-The directory for libtommath to be installed to.
|
||||||
#INCPATH-The directory to install the header files for libtommath.
|
#INCPATH-The directory to install the header files for libtommath.
|
||||||
@ -60,8 +63,8 @@ libtommath.la: $(OBJECTS)
|
|||||||
libtool --mode=link gcc *.lo -o libtommath.la -rpath $(LIBPATH) -version-info $(VERSION)
|
libtool --mode=link gcc *.lo -o libtommath.la -rpath $(LIBPATH) -version-info $(VERSION)
|
||||||
libtool --mode=link gcc *.o -o libtommath.a
|
libtool --mode=link gcc *.o -o libtommath.a
|
||||||
libtool --mode=install install -c libtommath.la $(LIBPATH)/libtommath.la
|
libtool --mode=install install -c libtommath.la $(LIBPATH)/libtommath.la
|
||||||
install -d -g root -o root $(DESTDIR)$(INCPATH)
|
install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH)
|
||||||
install -g root -o root $(HEADERS) $(DESTDIR)$(INCPATH)
|
install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH)
|
||||||
|
|
||||||
test: libtommath.a demo/demo.o
|
test: libtommath.a demo/demo.o
|
||||||
gcc $(CFLAGS) -c demo/demo.c -o demo/demo.o
|
gcc $(CFLAGS) -c demo/demo.c -o demo/demo.o
|
||||||
|
@ -46,7 +46,7 @@ void rand_num(mp_int *a)
|
|||||||
int n, size;
|
int n, size;
|
||||||
unsigned char buf[2048];
|
unsigned char buf[2048];
|
||||||
|
|
||||||
size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 1031;
|
size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
|
||||||
buf[0] = (fgetc(rng)&1)?1:0;
|
buf[0] = (fgetc(rng)&1)?1:0;
|
||||||
fread(buf+1, 1, size, rng);
|
fread(buf+1, 1, size, rng);
|
||||||
while (buf[1] == 0) buf[1] = fgetc(rng);
|
while (buf[1] == 0) buf[1] = fgetc(rng);
|
||||||
@ -58,7 +58,7 @@ void rand_num2(mp_int *a)
|
|||||||
int n, size;
|
int n, size;
|
||||||
unsigned char buf[2048];
|
unsigned char buf[2048];
|
||||||
|
|
||||||
size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 97;
|
size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
|
||||||
buf[0] = (fgetc(rng)&1)?1:0;
|
buf[0] = (fgetc(rng)&1)?1:0;
|
||||||
fread(buf+1, 1, size, rng);
|
fread(buf+1, 1, size, rng);
|
||||||
while (buf[1] == 0) buf[1] = fgetc(rng);
|
while (buf[1] == 0) buf[1] = fgetc(rng);
|
||||||
|
BIN
poster.pdf
BIN
poster.pdf
Binary file not shown.
402
pre_gen/mpi.c
402
pre_gen/mpi.c
File diff suppressed because it is too large
Load Diff
@ -442,7 +442,7 @@ int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* table of first PRIME_SIZE primes */
|
/* table of first PRIME_SIZE primes */
|
||||||
extern const mp_digit __prime_tab[];
|
extern const mp_digit ltm_prime_tab[];
|
||||||
|
|
||||||
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
|
/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
|
||||||
int mp_prime_is_divisible(mp_int *a, int *result);
|
int mp_prime_is_divisible(mp_int *a, int *result);
|
||||||
|
BIN
tommath.pdf
BIN
tommath.pdf
Binary file not shown.
257
tommath.tex
257
tommath.tex
@ -3420,7 +3420,7 @@ is copied to $b$, leading digits are removed and the remaining leading digit is
|
|||||||
027 \}
|
027 \}
|
||||||
028
|
028
|
||||||
029 /* if the modulus is larger than the value than return */
|
029 /* if the modulus is larger than the value than return */
|
||||||
030 if (b > (int) (a->used * DIGIT_BIT)) \{
|
030 if (b >= (int) (a->used * DIGIT_BIT)) \{
|
||||||
031 res = mp_copy (a, c);
|
031 res = mp_copy (a, c);
|
||||||
032 return res;
|
032 return res;
|
||||||
033 \}
|
033 \}
|
||||||
@ -3896,7 +3896,7 @@ and addition operations in the nested loop in parallel.
|
|||||||
049
|
049
|
||||||
050 /* clear the carry */
|
050 /* clear the carry */
|
||||||
051 _W = 0;
|
051 _W = 0;
|
||||||
052 for (ix = 0; ix <= pa; ix++) \{
|
052 for (ix = 0; ix < pa; ix++) \{
|
||||||
053 int tx, ty;
|
053 int tx, ty;
|
||||||
054 int iy;
|
054 int iy;
|
||||||
055 mp_digit *tmpx, *tmpy;
|
055 mp_digit *tmpx, *tmpy;
|
||||||
@ -3927,27 +3927,30 @@ and addition operations in the nested loop in parallel.
|
|||||||
079 _W = _W >> ((mp_word)DIGIT_BIT);
|
079 _W = _W >> ((mp_word)DIGIT_BIT);
|
||||||
080 \}
|
080 \}
|
||||||
081
|
081
|
||||||
082 /* setup dest */
|
082 /* store final carry */
|
||||||
083 olduse = c->used;
|
083 W[ix] = _W;
|
||||||
084 c->used = digs;
|
084
|
||||||
085
|
085 /* setup dest */
|
||||||
086 \{
|
086 olduse = c->used;
|
||||||
087 register mp_digit *tmpc;
|
087 c->used = digs;
|
||||||
088 tmpc = c->dp;
|
088
|
||||||
089 for (ix = 0; ix < digs; ix++) \{
|
089 \{
|
||||||
090 /* now extract the previous digit [below the carry] */
|
090 register mp_digit *tmpc;
|
||||||
091 *tmpc++ = W[ix];
|
091 tmpc = c->dp;
|
||||||
092 \}
|
092 for (ix = 0; ix < digs; ix++) \{
|
||||||
093
|
093 /* now extract the previous digit [below the carry] */
|
||||||
094 /* clear unused digits [that existed in the old copy of c] */
|
094 *tmpc++ = W[ix];
|
||||||
095 for (; ix < olduse; ix++) \{
|
095 \}
|
||||||
096 *tmpc++ = 0;
|
096
|
||||||
097 \}
|
097 /* clear unused digits [that existed in the old copy of c] */
|
||||||
098 \}
|
098 for (; ix < olduse; ix++) \{
|
||||||
099 mp_clamp (c);
|
099 *tmpc++ = 0;
|
||||||
100 return MP_OKAY;
|
100 \}
|
||||||
101 \}
|
101 \}
|
||||||
102 #endif
|
102 mp_clamp (c);
|
||||||
|
103 return MP_OKAY;
|
||||||
|
104 \}
|
||||||
|
105 #endif
|
||||||
\end{alltt}
|
\end{alltt}
|
||||||
\end{small}
|
\end{small}
|
||||||
|
|
||||||
@ -3955,7 +3958,7 @@ The memset on line @47,memset@ clears the initial $\hat W$ array to zero in a si
|
|||||||
implementation a series of aliases (\textit{lines 62, 63 and 76}) are used to simplify the inner $O(n^2)$ loop.
|
implementation a series of aliases (\textit{lines 62, 63 and 76}) are used to simplify the inner $O(n^2)$ loop.
|
||||||
In this case a new alias $\_\hat W$ has been added which refers to the double precision columns offset by $ix$ in each pass.
|
In this case a new alias $\_\hat W$ has been added which refers to the double precision columns offset by $ix$ in each pass.
|
||||||
|
|
||||||
The inner loop on lines 89, 79 and 80 is where the algorithm will spend the majority of the time, which is why it has been
|
The inner loop on lines 92, 79 and 80 is where the algorithm will spend the majority of the time, which is why it has been
|
||||||
stripped to the bones of any extra baggage\footnote{Hence the pointer aliases.}. On x86 processors the multiplication and additions amount to at the
|
stripped to the bones of any extra baggage\footnote{Hence the pointer aliases.}. On x86 processors the multiplication and additions amount to at the
|
||||||
very least five instructions (\textit{two loads, two additions, one multiply}) while on the ARMv4 processors they amount to only three
|
very least five instructions (\textit{two loads, two additions, one multiply}) while on the ARMv4 processors they amount to only three
|
||||||
(\textit{one load, one store, one multiply-add}). For both of the x86 and ARMv4 processors the GCC compiler performs a good job at unrolling the loop
|
(\textit{one load, one store, one multiply-add}). For both of the x86 and ARMv4 processors the GCC compiler performs a good job at unrolling the loop
|
||||||
@ -5100,7 +5103,7 @@ squares in place.
|
|||||||
059
|
059
|
||||||
060 /* number of output digits to produce */
|
060 /* number of output digits to produce */
|
||||||
061 W1 = 0;
|
061 W1 = 0;
|
||||||
062 for (ix = 0; ix <= pa; ix++) \{
|
062 for (ix = 0; ix < pa; ix++) \{
|
||||||
063 int tx, ty, iy;
|
063 int tx, ty, iy;
|
||||||
064 mp_word _W;
|
064 mp_word _W;
|
||||||
065 mp_digit *tmpy;
|
065 mp_digit *tmpy;
|
||||||
@ -6739,7 +6742,7 @@ at step 3.
|
|||||||
019 * Based on algorithm from the paper
|
019 * Based on algorithm from the paper
|
||||||
020 *
|
020 *
|
||||||
021 * "Generating Efficient Primes for Discrete Log Cryptosystems"
|
021 * "Generating Efficient Primes for Discrete Log Cryptosystems"
|
||||||
022 * Chae Hoon Lim, Pil Loong Lee,
|
022 * Chae Hoon Lim, Pil Joong Lee,
|
||||||
023 * POSTECH Information Research Laboratories
|
023 * POSTECH Information Research Laboratories
|
||||||
024 *
|
024 *
|
||||||
025 * The modulus must be of a special format [see manual]
|
025 * The modulus must be of a special format [see manual]
|
||||||
@ -7594,7 +7597,7 @@ algorithm since their arguments are essentially the same (\textit{two mp\_ints a
|
|||||||
060 return err;
|
060 return err;
|
||||||
061 #else
|
061 #else
|
||||||
062 /* no invmod */
|
062 /* no invmod */
|
||||||
063 return MP_VAL
|
063 return MP_VAL;
|
||||||
064 #endif
|
064 #endif
|
||||||
065 \}
|
065 \}
|
||||||
066
|
066
|
||||||
@ -7866,10 +7869,10 @@ a Left-to-Right algorithm is used to process the remaining few bits.
|
|||||||
069
|
069
|
||||||
070 /* create mu, used for Barrett reduction */
|
070 /* create mu, used for Barrett reduction */
|
||||||
071 if ((err = mp_init (&mu)) != MP_OKAY) \{
|
071 if ((err = mp_init (&mu)) != MP_OKAY) \{
|
||||||
072 goto __M;
|
072 goto LBL_M;
|
||||||
073 \}
|
073 \}
|
||||||
074 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) \{
|
074 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) \{
|
||||||
075 goto __MU;
|
075 goto LBL_MU;
|
||||||
076 \}
|
076 \}
|
||||||
077
|
077
|
||||||
078 /* create M table
|
078 /* create M table
|
||||||
@ -7881,23 +7884,23 @@ a Left-to-Right algorithm is used to process the remaining few bits.
|
|||||||
084 * computed though accept for M[0] and M[1]
|
084 * computed though accept for M[0] and M[1]
|
||||||
085 */
|
085 */
|
||||||
086 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) \{
|
086 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) \{
|
||||||
087 goto __MU;
|
087 goto LBL_MU;
|
||||||
088 \}
|
088 \}
|
||||||
089
|
089
|
||||||
090 /* compute the value at M[1<<(winsize-1)] by squaring
|
090 /* compute the value at M[1<<(winsize-1)] by squaring
|
||||||
091 * M[1] (winsize-1) times
|
091 * M[1] (winsize-1) times
|
||||||
092 */
|
092 */
|
||||||
093 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) \{
|
093 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) \{
|
||||||
094 goto __MU;
|
094 goto LBL_MU;
|
||||||
095 \}
|
095 \}
|
||||||
096
|
096
|
||||||
097 for (x = 0; x < (winsize - 1); x++) \{
|
097 for (x = 0; x < (winsize - 1); x++) \{
|
||||||
098 if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
098 if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
||||||
099 &M[1 << (winsize - 1)])) != MP_OKAY) \{
|
099 &M[1 << (winsize - 1)])) != MP_OKAY) \{
|
||||||
100 goto __MU;
|
100 goto LBL_MU;
|
||||||
101 \}
|
101 \}
|
||||||
102 if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) \{
|
102 if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) \{
|
||||||
103 goto __MU;
|
103 goto LBL_MU;
|
||||||
104 \}
|
104 \}
|
||||||
105 \}
|
105 \}
|
||||||
106
|
106
|
||||||
@ -7906,16 +7909,16 @@ a Left-to-Right algorithm is used to process the remaining few bits.
|
|||||||
109 */
|
109 */
|
||||||
110 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) \{
|
110 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) \{
|
||||||
111 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) \{
|
111 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) \{
|
||||||
112 goto __MU;
|
112 goto LBL_MU;
|
||||||
113 \}
|
113 \}
|
||||||
114 if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) \{
|
114 if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) \{
|
||||||
115 goto __MU;
|
115 goto LBL_MU;
|
||||||
116 \}
|
116 \}
|
||||||
117 \}
|
117 \}
|
||||||
118
|
118
|
||||||
119 /* setup result */
|
119 /* setup result */
|
||||||
120 if ((err = mp_init (&res)) != MP_OKAY) \{
|
120 if ((err = mp_init (&res)) != MP_OKAY) \{
|
||||||
121 goto __MU;
|
121 goto LBL_MU;
|
||||||
122 \}
|
122 \}
|
||||||
123 mp_set (&res, 1);
|
123 mp_set (&res, 1);
|
||||||
124
|
124
|
||||||
@ -7955,10 +7958,10 @@ a Left-to-Right algorithm is used to process the remaining few bits.
|
|||||||
158 /* if the bit is zero and mode == 1 then we square */
|
158 /* if the bit is zero and mode == 1 then we square */
|
||||||
159 if (mode == 1 && y == 0) \{
|
159 if (mode == 1 && y == 0) \{
|
||||||
160 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
|
160 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
|
||||||
161 goto __RES;
|
161 goto LBL_RES;
|
||||||
162 \}
|
162 \}
|
||||||
163 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
163 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
||||||
164 goto __RES;
|
164 goto LBL_RES;
|
||||||
165 \}
|
165 \}
|
||||||
166 continue;
|
166 continue;
|
||||||
167 \}
|
167 \}
|
||||||
@ -7972,19 +7975,19 @@ a Left-to-Right algorithm is used to process the remaining few bits.
|
|||||||
175 /* square first */
|
175 /* square first */
|
||||||
176 for (x = 0; x < winsize; x++) \{
|
176 for (x = 0; x < winsize; x++) \{
|
||||||
177 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
|
177 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
|
||||||
178 goto __RES;
|
178 goto LBL_RES;
|
||||||
179 \}
|
179 \}
|
||||||
180 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
180 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
||||||
181 goto __RES;
|
181 goto LBL_RES;
|
||||||
182 \}
|
182 \}
|
||||||
183 \}
|
183 \}
|
||||||
184
|
184
|
||||||
185 /* then multiply */
|
185 /* then multiply */
|
||||||
186 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) \{
|
186 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) \{
|
||||||
187 goto __RES;
|
187 goto LBL_RES;
|
||||||
188 \}
|
188 \}
|
||||||
189 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
189 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
||||||
190 goto __RES;
|
190 goto LBL_RES;
|
||||||
191 \}
|
191 \}
|
||||||
192
|
192
|
||||||
193 /* empty window and reset */
|
193 /* empty window and reset */
|
||||||
@ -7999,20 +8002,20 @@ a Left-to-Right algorithm is used to process the remaining few bits.
|
|||||||
202 /* square then multiply if the bit is set */
|
202 /* square then multiply if the bit is set */
|
||||||
203 for (x = 0; x < bitcpy; x++) \{
|
203 for (x = 0; x < bitcpy; x++) \{
|
||||||
204 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
|
204 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{
|
||||||
205 goto __RES;
|
205 goto LBL_RES;
|
||||||
206 \}
|
206 \}
|
||||||
207 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
207 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
||||||
208 goto __RES;
|
208 goto LBL_RES;
|
||||||
209 \}
|
209 \}
|
||||||
210
|
210
|
||||||
211 bitbuf <<= 1;
|
211 bitbuf <<= 1;
|
||||||
212 if ((bitbuf & (1 << winsize)) != 0) \{
|
212 if ((bitbuf & (1 << winsize)) != 0) \{
|
||||||
213 /* then multiply */
|
213 /* then multiply */
|
||||||
214 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) \{
|
214 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) \{
|
||||||
215 goto __RES;
|
215 goto LBL_RES;
|
||||||
216 \}
|
216 \}
|
||||||
217 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
217 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{
|
||||||
218 goto __RES;
|
218 goto LBL_RES;
|
||||||
219 \}
|
219 \}
|
||||||
220 \}
|
220 \}
|
||||||
221 \}
|
221 \}
|
||||||
@ -8020,9 +8023,9 @@ a Left-to-Right algorithm is used to process the remaining few bits.
|
|||||||
223
|
223
|
||||||
224 mp_exch (&res, Y);
|
224 mp_exch (&res, Y);
|
||||||
225 err = MP_OKAY;
|
225 err = MP_OKAY;
|
||||||
226 __RES:mp_clear (&res);
|
226 LBL_RES:mp_clear (&res);
|
||||||
227 __MU:mp_clear (&mu);
|
227 LBL_MU:mp_clear (&mu);
|
||||||
228 __M:
|
228 LBL_M:
|
||||||
229 mp_clear(&M[1]);
|
229 mp_clear(&M[1]);
|
||||||
230 for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
|
230 for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{
|
||||||
231 mp_clear (&M[x]);
|
231 mp_clear (&M[x]);
|
||||||
@ -8386,23 +8389,23 @@ respectively be replaced with a zero.
|
|||||||
048
|
048
|
||||||
049 mp_set(&tq, 1);
|
049 mp_set(&tq, 1);
|
||||||
050 n = mp_count_bits(a) - mp_count_bits(b);
|
050 n = mp_count_bits(a) - mp_count_bits(b);
|
||||||
051 if (((res = mp_copy(a, &ta)) != MP_OKAY) ||
|
051 if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
||||||
052 ((res = mp_copy(b, &tb)) != MP_OKAY) ||
|
052 ((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||||
053 ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
|
053 ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
|
||||||
054 ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) \{
|
054 ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) \{
|
||||||
055 goto __ERR;
|
055 goto LBL_ERR;
|
||||||
056 \}
|
056 \}
|
||||||
057
|
057
|
||||||
058 while (n-- >= 0) \{
|
058 while (n-- >= 0) \{
|
||||||
059 if (mp_cmp(&tb, &ta) != MP_GT) \{
|
059 if (mp_cmp(&tb, &ta) != MP_GT) \{
|
||||||
060 if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
|
060 if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
|
||||||
061 ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) \{
|
061 ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) \{
|
||||||
062 goto __ERR;
|
062 goto LBL_ERR;
|
||||||
063 \}
|
063 \}
|
||||||
064 \}
|
064 \}
|
||||||
065 if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
|
065 if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
|
||||||
066 ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) \{
|
066 ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) \{
|
||||||
067 goto __ERR;
|
067 goto LBL_ERR;
|
||||||
068 \}
|
068 \}
|
||||||
069 \}
|
069 \}
|
||||||
070
|
070
|
||||||
@ -8411,13 +8414,13 @@ respectively be replaced with a zero.
|
|||||||
073 n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
|
073 n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
|
||||||
074 if (c != NULL) \{
|
074 if (c != NULL) \{
|
||||||
075 mp_exch(c, &q);
|
075 mp_exch(c, &q);
|
||||||
076 c->sign = n2;
|
076 c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
|
||||||
077 \}
|
077 \}
|
||||||
078 if (d != NULL) \{
|
078 if (d != NULL) \{
|
||||||
079 mp_exch(d, &ta);
|
079 mp_exch(d, &ta);
|
||||||
080 d->sign = n;
|
080 d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
|
||||||
081 \}
|
081 \}
|
||||||
082 __ERR:
|
082 LBL_ERR:
|
||||||
083 mp_clear_multi(&ta, &tb, &tq, &q, NULL);
|
083 mp_clear_multi(&ta, &tb, &tq, &q, NULL);
|
||||||
084 return res;
|
084 return res;
|
||||||
085 \}
|
085 \}
|
||||||
@ -8466,19 +8469,19 @@ respectively be replaced with a zero.
|
|||||||
128 q.used = a->used + 2;
|
128 q.used = a->used + 2;
|
||||||
129
|
129
|
||||||
130 if ((res = mp_init (&t1)) != MP_OKAY) \{
|
130 if ((res = mp_init (&t1)) != MP_OKAY) \{
|
||||||
131 goto __Q;
|
131 goto LBL_Q;
|
||||||
132 \}
|
132 \}
|
||||||
133
|
133
|
||||||
134 if ((res = mp_init (&t2)) != MP_OKAY) \{
|
134 if ((res = mp_init (&t2)) != MP_OKAY) \{
|
||||||
135 goto __T1;
|
135 goto LBL_T1;
|
||||||
136 \}
|
136 \}
|
||||||
137
|
137
|
||||||
138 if ((res = mp_init_copy (&x, a)) != MP_OKAY) \{
|
138 if ((res = mp_init_copy (&x, a)) != MP_OKAY) \{
|
||||||
139 goto __T2;
|
139 goto LBL_T2;
|
||||||
140 \}
|
140 \}
|
||||||
141
|
141
|
||||||
142 if ((res = mp_init_copy (&y, b)) != MP_OKAY) \{
|
142 if ((res = mp_init_copy (&y, b)) != MP_OKAY) \{
|
||||||
143 goto __X;
|
143 goto LBL_X;
|
||||||
144 \}
|
144 \}
|
||||||
145
|
145
|
||||||
146 /* fix the sign */
|
146 /* fix the sign */
|
||||||
@ -8490,10 +8493,10 @@ respectively be replaced with a zero.
|
|||||||
152 if (norm < (int)(DIGIT_BIT-1)) \{
|
152 if (norm < (int)(DIGIT_BIT-1)) \{
|
||||||
153 norm = (DIGIT_BIT-1) - norm;
|
153 norm = (DIGIT_BIT-1) - norm;
|
||||||
154 if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) \{
|
154 if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) \{
|
||||||
155 goto __Y;
|
155 goto LBL_Y;
|
||||||
156 \}
|
156 \}
|
||||||
157 if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) \{
|
157 if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) \{
|
||||||
158 goto __Y;
|
158 goto LBL_Y;
|
||||||
159 \}
|
159 \}
|
||||||
160 \} else \{
|
160 \} else \{
|
||||||
161 norm = 0;
|
161 norm = 0;
|
||||||
@ -8505,13 +8508,13 @@ respectively be replaced with a zero.
|
|||||||
167
|
167
|
||||||
168 /* while (x >= y*b**n-t) do \{ q[n-t] += 1; x -= y*b**\{n-t\} \} */
|
168 /* while (x >= y*b**n-t) do \{ q[n-t] += 1; x -= y*b**\{n-t\} \} */
|
||||||
169 if ((res = mp_lshd (&y, n - t)) != MP_OKAY) \{ /* y = y*b**\{n-t\} */
|
169 if ((res = mp_lshd (&y, n - t)) != MP_OKAY) \{ /* y = y*b**\{n-t\} */
|
||||||
170 goto __Y;
|
170 goto LBL_Y;
|
||||||
171 \}
|
171 \}
|
||||||
172
|
172
|
||||||
173 while (mp_cmp (&x, &y) != MP_LT) \{
|
173 while (mp_cmp (&x, &y) != MP_LT) \{
|
||||||
174 ++(q.dp[n - t]);
|
174 ++(q.dp[n - t]);
|
||||||
175 if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) \{
|
175 if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) \{
|
||||||
176 goto __Y;
|
176 goto LBL_Y;
|
||||||
177 \}
|
177 \}
|
||||||
178 \}
|
178 \}
|
||||||
179
|
179
|
||||||
@ -8553,7 +8556,7 @@ respectively be replaced with a zero.
|
|||||||
215 t1.dp[1] = y.dp[t];
|
215 t1.dp[1] = y.dp[t];
|
||||||
216 t1.used = 2;
|
216 t1.used = 2;
|
||||||
217 if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) \{
|
217 if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) \{
|
||||||
218 goto __Y;
|
218 goto LBL_Y;
|
||||||
219 \}
|
219 \}
|
||||||
220
|
220
|
||||||
221 /* find right hand */
|
221 /* find right hand */
|
||||||
@ -8565,27 +8568,27 @@ respectively be replaced with a zero.
|
|||||||
227
|
227
|
||||||
228 /* step 3.3 x = x - q\{i-t-1\} * y * b**\{i-t-1\} */
|
228 /* step 3.3 x = x - q\{i-t-1\} * y * b**\{i-t-1\} */
|
||||||
229 if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) \{
|
229 if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) \{
|
||||||
230 goto __Y;
|
230 goto LBL_Y;
|
||||||
231 \}
|
231 \}
|
||||||
232
|
232
|
||||||
233 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{
|
233 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{
|
||||||
234 goto __Y;
|
234 goto LBL_Y;
|
||||||
235 \}
|
235 \}
|
||||||
236
|
236
|
||||||
237 if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) \{
|
237 if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) \{
|
||||||
238 goto __Y;
|
238 goto LBL_Y;
|
||||||
239 \}
|
239 \}
|
||||||
240
|
240
|
||||||
241 /* if x < 0 then \{ x = x + y*b**\{i-t-1\}; q\{i-t-1\} -= 1; \} */
|
241 /* if x < 0 then \{ x = x + y*b**\{i-t-1\}; q\{i-t-1\} -= 1; \} */
|
||||||
242 if (x.sign == MP_NEG) \{
|
242 if (x.sign == MP_NEG) \{
|
||||||
243 if ((res = mp_copy (&y, &t1)) != MP_OKAY) \{
|
243 if ((res = mp_copy (&y, &t1)) != MP_OKAY) \{
|
||||||
244 goto __Y;
|
244 goto LBL_Y;
|
||||||
245 \}
|
245 \}
|
||||||
246 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{
|
246 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{
|
||||||
247 goto __Y;
|
247 goto LBL_Y;
|
||||||
248 \}
|
248 \}
|
||||||
249 if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) \{
|
249 if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) \{
|
||||||
250 goto __Y;
|
250 goto LBL_Y;
|
||||||
251 \}
|
251 \}
|
||||||
252
|
252
|
||||||
253 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
|
253 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
|
||||||
@ -8612,11 +8615,11 @@ respectively be replaced with a zero.
|
|||||||
274
|
274
|
||||||
275 res = MP_OKAY;
|
275 res = MP_OKAY;
|
||||||
276
|
276
|
||||||
277 __Y:mp_clear (&y);
|
277 LBL_Y:mp_clear (&y);
|
||||||
278 __X:mp_clear (&x);
|
278 LBL_X:mp_clear (&x);
|
||||||
279 __T2:mp_clear (&t2);
|
279 LBL_T2:mp_clear (&t2);
|
||||||
280 __T1:mp_clear (&t1);
|
280 LBL_T1:mp_clear (&t1);
|
||||||
281 __Q:mp_clear (&q);
|
281 LBL_Q:mp_clear (&q);
|
||||||
282 return res;
|
282 return res;
|
||||||
283 \}
|
283 \}
|
||||||
284
|
284
|
||||||
@ -9130,11 +9133,11 @@ root. Ideally this algorithm is meant to find the $n$'th root of an input where
|
|||||||
039 \}
|
039 \}
|
||||||
040
|
040
|
||||||
041 if ((res = mp_init (&t2)) != MP_OKAY) \{
|
041 if ((res = mp_init (&t2)) != MP_OKAY) \{
|
||||||
042 goto __T1;
|
042 goto LBL_T1;
|
||||||
043 \}
|
043 \}
|
||||||
044
|
044
|
||||||
045 if ((res = mp_init (&t3)) != MP_OKAY) \{
|
045 if ((res = mp_init (&t3)) != MP_OKAY) \{
|
||||||
046 goto __T2;
|
046 goto LBL_T2;
|
||||||
047 \}
|
047 \}
|
||||||
048
|
048
|
||||||
049 /* if a is negative fudge the sign but keep track */
|
049 /* if a is negative fudge the sign but keep track */
|
||||||
@ -9147,52 +9150,52 @@ root. Ideally this algorithm is meant to find the $n$'th root of an input where
|
|||||||
056 do \{
|
056 do \{
|
||||||
057 /* t1 = t2 */
|
057 /* t1 = t2 */
|
||||||
058 if ((res = mp_copy (&t2, &t1)) != MP_OKAY) \{
|
058 if ((res = mp_copy (&t2, &t1)) != MP_OKAY) \{
|
||||||
059 goto __T3;
|
059 goto LBL_T3;
|
||||||
060 \}
|
060 \}
|
||||||
061
|
061
|
||||||
062 /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
062 /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
||||||
063
|
063
|
||||||
064 /* t3 = t1**(b-1) */
|
064 /* t3 = t1**(b-1) */
|
||||||
065 if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) \{
|
065 if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) \{
|
||||||
066 goto __T3;
|
066 goto LBL_T3;
|
||||||
067 \}
|
067 \}
|
||||||
068
|
068
|
||||||
069 /* numerator */
|
069 /* numerator */
|
||||||
070 /* t2 = t1**b */
|
070 /* t2 = t1**b */
|
||||||
071 if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) \{
|
071 if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) \{
|
||||||
072 goto __T3;
|
072 goto LBL_T3;
|
||||||
073 \}
|
073 \}
|
||||||
074
|
074
|
||||||
075 /* t2 = t1**b - a */
|
075 /* t2 = t1**b - a */
|
||||||
076 if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) \{
|
076 if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) \{
|
||||||
077 goto __T3;
|
077 goto LBL_T3;
|
||||||
078 \}
|
078 \}
|
||||||
079
|
079
|
||||||
080 /* denominator */
|
080 /* denominator */
|
||||||
081 /* t3 = t1**(b-1) * b */
|
081 /* t3 = t1**(b-1) * b */
|
||||||
082 if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) \{
|
082 if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) \{
|
||||||
083 goto __T3;
|
083 goto LBL_T3;
|
||||||
084 \}
|
084 \}
|
||||||
085
|
085
|
||||||
086 /* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
086 /* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
||||||
087 if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) \{
|
087 if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) \{
|
||||||
088 goto __T3;
|
088 goto LBL_T3;
|
||||||
089 \}
|
089 \}
|
||||||
090
|
090
|
||||||
091 if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) \{
|
091 if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) \{
|
||||||
092 goto __T3;
|
092 goto LBL_T3;
|
||||||
093 \}
|
093 \}
|
||||||
094 \} while (mp_cmp (&t1, &t2) != MP_EQ);
|
094 \} while (mp_cmp (&t1, &t2) != MP_EQ);
|
||||||
095
|
095
|
||||||
096 /* result can be off by a few so check */
|
096 /* result can be off by a few so check */
|
||||||
097 for (;;) \{
|
097 for (;;) \{
|
||||||
098 if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) \{
|
098 if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) \{
|
||||||
099 goto __T3;
|
099 goto LBL_T3;
|
||||||
100 \}
|
100 \}
|
||||||
101
|
101
|
||||||
102 if (mp_cmp (&t2, a) == MP_GT) \{
|
102 if (mp_cmp (&t2, a) == MP_GT) \{
|
||||||
103 if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) \{
|
103 if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) \{
|
||||||
104 goto __T3;
|
104 goto LBL_T3;
|
||||||
105 \}
|
105 \}
|
||||||
106 \} else \{
|
106 \} else \{
|
||||||
107 break;
|
107 break;
|
||||||
@ -9210,9 +9213,9 @@ root. Ideally this algorithm is meant to find the $n$'th root of an input where
|
|||||||
119
|
119
|
||||||
120 res = MP_OKAY;
|
120 res = MP_OKAY;
|
||||||
121
|
121
|
||||||
122 __T3:mp_clear (&t3);
|
122 LBL_T3:mp_clear (&t3);
|
||||||
123 __T2:mp_clear (&t2);
|
123 LBL_T2:mp_clear (&t2);
|
||||||
124 __T1:mp_clear (&t1);
|
124 LBL_T1:mp_clear (&t1);
|
||||||
125 return res;
|
125 return res;
|
||||||
126 \}
|
126 \}
|
||||||
127 #endif
|
127 #endif
|
||||||
@ -9771,7 +9774,7 @@ must be adjusted by multiplying by the common factors of two ($2^k$) removed ear
|
|||||||
042 \}
|
042 \}
|
||||||
043
|
043
|
||||||
044 if ((res = mp_init_copy (&v, b)) != MP_OKAY) \{
|
044 if ((res = mp_init_copy (&v, b)) != MP_OKAY) \{
|
||||||
045 goto __U;
|
045 goto LBL_U;
|
||||||
046 \}
|
046 \}
|
||||||
047
|
047
|
||||||
048 /* must be positive for the remainder of the algorithm */
|
048 /* must be positive for the remainder of the algorithm */
|
||||||
@ -9785,24 +9788,24 @@ must be adjusted by multiplying by the common factors of two ($2^k$) removed ear
|
|||||||
056 if (k > 0) \{
|
056 if (k > 0) \{
|
||||||
057 /* divide the power of two out */
|
057 /* divide the power of two out */
|
||||||
058 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) \{
|
058 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) \{
|
||||||
059 goto __V;
|
059 goto LBL_V;
|
||||||
060 \}
|
060 \}
|
||||||
061
|
061
|
||||||
062 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) \{
|
062 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) \{
|
||||||
063 goto __V;
|
063 goto LBL_V;
|
||||||
064 \}
|
064 \}
|
||||||
065 \}
|
065 \}
|
||||||
066
|
066
|
||||||
067 /* divide any remaining factors of two out */
|
067 /* divide any remaining factors of two out */
|
||||||
068 if (u_lsb != k) \{
|
068 if (u_lsb != k) \{
|
||||||
069 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) \{
|
069 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) \{
|
||||||
070 goto __V;
|
070 goto LBL_V;
|
||||||
071 \}
|
071 \}
|
||||||
072 \}
|
072 \}
|
||||||
073
|
073
|
||||||
074 if (v_lsb != k) \{
|
074 if (v_lsb != k) \{
|
||||||
075 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) \{
|
075 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) \{
|
||||||
076 goto __V;
|
076 goto LBL_V;
|
||||||
077 \}
|
077 \}
|
||||||
078 \}
|
078 \}
|
||||||
079
|
079
|
||||||
@ -9815,23 +9818,23 @@ must be adjusted by multiplying by the common factors of two ($2^k$) removed ear
|
|||||||
086
|
086
|
||||||
087 /* subtract smallest from largest */
|
087 /* subtract smallest from largest */
|
||||||
088 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) \{
|
088 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) \{
|
||||||
089 goto __V;
|
089 goto LBL_V;
|
||||||
090 \}
|
090 \}
|
||||||
091
|
091
|
||||||
092 /* Divide out all factors of two */
|
092 /* Divide out all factors of two */
|
||||||
093 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) \{
|
093 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) \{
|
||||||
094 goto __V;
|
094 goto LBL_V;
|
||||||
095 \}
|
095 \}
|
||||||
096 \}
|
096 \}
|
||||||
097
|
097
|
||||||
098 /* multiply by 2**k which we divided out at the beginning */
|
098 /* multiply by 2**k which we divided out at the beginning */
|
||||||
099 if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) \{
|
099 if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) \{
|
||||||
100 goto __V;
|
100 goto LBL_V;
|
||||||
101 \}
|
101 \}
|
||||||
102 c->sign = MP_ZPOS;
|
102 c->sign = MP_ZPOS;
|
||||||
103 res = MP_OKAY;
|
103 res = MP_OKAY;
|
||||||
104 __V:mp_clear (&u);
|
104 LBL_V:mp_clear (&u);
|
||||||
105 __U:mp_clear (&v);
|
105 LBL_U:mp_clear (&v);
|
||||||
106 return res;
|
106 return res;
|
||||||
107 \}
|
107 \}
|
||||||
108 #endif
|
108 #endif
|
||||||
@ -9904,20 +9907,20 @@ dividing the product of the two inputs by their greatest common divisor.
|
|||||||
027
|
027
|
||||||
028 /* t1 = get the GCD of the two inputs */
|
028 /* t1 = get the GCD of the two inputs */
|
||||||
029 if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) \{
|
029 if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) \{
|
||||||
030 goto __T;
|
030 goto LBL_T;
|
||||||
031 \}
|
031 \}
|
||||||
032
|
032
|
||||||
033 /* divide the smallest by the GCD */
|
033 /* divide the smallest by the GCD */
|
||||||
034 if (mp_cmp_mag(a, b) == MP_LT) \{
|
034 if (mp_cmp_mag(a, b) == MP_LT) \{
|
||||||
035 /* store quotient in t2 such that t2 * b is the LCM */
|
035 /* store quotient in t2 such that t2 * b is the LCM */
|
||||||
036 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) \{
|
036 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) \{
|
||||||
037 goto __T;
|
037 goto LBL_T;
|
||||||
038 \}
|
038 \}
|
||||||
039 res = mp_mul(b, &t2, c);
|
039 res = mp_mul(b, &t2, c);
|
||||||
040 \} else \{
|
040 \} else \{
|
||||||
041 /* store quotient in t2 such that t2 * a is the LCM */
|
041 /* store quotient in t2 such that t2 * a is the LCM */
|
||||||
042 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) \{
|
042 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) \{
|
||||||
043 goto __T;
|
043 goto LBL_T;
|
||||||
044 \}
|
044 \}
|
||||||
045 res = mp_mul(a, &t2, c);
|
045 res = mp_mul(a, &t2, c);
|
||||||
046 \}
|
046 \}
|
||||||
@ -9925,7 +9928,7 @@ dividing the product of the two inputs by their greatest common divisor.
|
|||||||
048 /* fix the sign to positive */
|
048 /* fix the sign to positive */
|
||||||
049 c->sign = MP_ZPOS;
|
049 c->sign = MP_ZPOS;
|
||||||
050
|
050
|
||||||
051 __T:
|
051 LBL_T:
|
||||||
052 mp_clear_multi (&t1, &t2, NULL);
|
052 mp_clear_multi (&t1, &t2, NULL);
|
||||||
053 return res;
|
053 return res;
|
||||||
054 \}
|
054 \}
|
||||||
@ -10123,13 +10126,13 @@ $\left ( {p' \over a'} \right )$ which is multiplied against the current Jacobi
|
|||||||
049 \}
|
049 \}
|
||||||
050
|
050
|
||||||
051 if ((res = mp_init (&p1)) != MP_OKAY) \{
|
051 if ((res = mp_init (&p1)) != MP_OKAY) \{
|
||||||
052 goto __A1;
|
052 goto LBL_A1;
|
||||||
053 \}
|
053 \}
|
||||||
054
|
054
|
||||||
055 /* divide out larger power of two */
|
055 /* divide out larger power of two */
|
||||||
056 k = mp_cnt_lsb(&a1);
|
056 k = mp_cnt_lsb(&a1);
|
||||||
057 if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) \{
|
057 if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) \{
|
||||||
058 goto __P1;
|
058 goto LBL_P1;
|
||||||
059 \}
|
059 \}
|
||||||
060
|
060
|
||||||
061 /* step 4. if e is even set s=1 */
|
061 /* step 4. if e is even set s=1 */
|
||||||
@ -10157,18 +10160,18 @@ $\left ( {p' \over a'} \right )$ which is multiplied against the current Jacobi
|
|||||||
083 \} else \{
|
083 \} else \{
|
||||||
084 /* n1 = n mod a1 */
|
084 /* n1 = n mod a1 */
|
||||||
085 if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) \{
|
085 if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) \{
|
||||||
086 goto __P1;
|
086 goto LBL_P1;
|
||||||
087 \}
|
087 \}
|
||||||
088 if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) \{
|
088 if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) \{
|
||||||
089 goto __P1;
|
089 goto LBL_P1;
|
||||||
090 \}
|
090 \}
|
||||||
091 *c = s * r;
|
091 *c = s * r;
|
||||||
092 \}
|
092 \}
|
||||||
093
|
093
|
||||||
094 /* done */
|
094 /* done */
|
||||||
095 res = MP_OKAY;
|
095 res = MP_OKAY;
|
||||||
096 __P1:mp_clear (&p1);
|
096 LBL_P1:mp_clear (&p1);
|
||||||
097 __A1:mp_clear (&a1);
|
097 LBL_A1:mp_clear (&a1);
|
||||||
098 return res;
|
098 return res;
|
||||||
099 \}
|
099 \}
|
||||||
100 #endif
|
100 #endif
|
||||||
@ -10406,8 +10409,8 @@ This algorithm attempts to determine if a candidate integer $n$ is composite by
|
|||||||
028 *result = MP_NO;
|
028 *result = MP_NO;
|
||||||
029
|
029
|
||||||
030 for (ix = 0; ix < PRIME_SIZE; ix++) \{
|
030 for (ix = 0; ix < PRIME_SIZE; ix++) \{
|
||||||
031 /* what is a mod __prime_tab[ix] */
|
031 /* what is a mod LBL_prime_tab[ix] */
|
||||||
032 if ((err = mp_mod_d (a, __prime_tab[ix], &res)) != MP_OKAY) \{
|
032 if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) \{
|
||||||
033 return err;
|
033 return err;
|
||||||
034 \}
|
034 \}
|
||||||
035
|
035
|
||||||
@ -10431,7 +10434,7 @@ mp\_digit. The table \_\_prime\_tab is defined in the following file.
|
|||||||
\hspace{-5.1mm}{\bf File}: bn\_prime\_tab.c
|
\hspace{-5.1mm}{\bf File}: bn\_prime\_tab.c
|
||||||
\vspace{-3mm}
|
\vspace{-3mm}
|
||||||
\begin{alltt}
|
\begin{alltt}
|
||||||
016 const mp_digit __prime_tab[] = \{
|
016 const mp_digit ltm_prime_tab[] = \{
|
||||||
017 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
|
017 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
|
||||||
018 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
|
018 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
|
||||||
019 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
|
019 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
|
||||||
@ -10547,7 +10550,7 @@ determine the result.
|
|||||||
042
|
042
|
||||||
043 /* compute t = b**a mod a */
|
043 /* compute t = b**a mod a */
|
||||||
044 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) \{
|
044 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) \{
|
||||||
045 goto __T;
|
045 goto LBL_T;
|
||||||
046 \}
|
046 \}
|
||||||
047
|
047
|
||||||
048 /* is it equal to b? */
|
048 /* is it equal to b? */
|
||||||
@ -10556,7 +10559,7 @@ determine the result.
|
|||||||
051 \}
|
051 \}
|
||||||
052
|
052
|
||||||
053 err = MP_OKAY;
|
053 err = MP_OKAY;
|
||||||
054 __T:mp_clear (&t);
|
054 LBL_T:mp_clear (&t);
|
||||||
055 return err;
|
055 return err;
|
||||||
056 \}
|
056 \}
|
||||||
057 #endif
|
057 #endif
|
||||||
@ -10638,12 +10641,12 @@ composite then it is \textit{probably} prime.
|
|||||||
039 return err;
|
039 return err;
|
||||||
040 \}
|
040 \}
|
||||||
041 if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) \{
|
041 if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) \{
|
||||||
042 goto __N1;
|
042 goto LBL_N1;
|
||||||
043 \}
|
043 \}
|
||||||
044
|
044
|
||||||
045 /* set 2**s * r = n1 */
|
045 /* set 2**s * r = n1 */
|
||||||
046 if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) \{
|
046 if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) \{
|
||||||
047 goto __N1;
|
047 goto LBL_N1;
|
||||||
048 \}
|
048 \}
|
||||||
049
|
049
|
||||||
050 /* count the number of least significant bits
|
050 /* count the number of least significant bits
|
||||||
@ -10653,15 +10656,15 @@ composite then it is \textit{probably} prime.
|
|||||||
054
|
054
|
||||||
055 /* now divide n - 1 by 2**s */
|
055 /* now divide n - 1 by 2**s */
|
||||||
056 if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) \{
|
056 if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) \{
|
||||||
057 goto __R;
|
057 goto LBL_R;
|
||||||
058 \}
|
058 \}
|
||||||
059
|
059
|
||||||
060 /* compute y = b**r mod a */
|
060 /* compute y = b**r mod a */
|
||||||
061 if ((err = mp_init (&y)) != MP_OKAY) \{
|
061 if ((err = mp_init (&y)) != MP_OKAY) \{
|
||||||
062 goto __R;
|
062 goto LBL_R;
|
||||||
063 \}
|
063 \}
|
||||||
064 if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) \{
|
064 if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) \{
|
||||||
065 goto __Y;
|
065 goto LBL_Y;
|
||||||
066 \}
|
066 \}
|
||||||
067
|
067
|
||||||
068 /* if y != 1 and y != n1 do */
|
068 /* if y != 1 and y != n1 do */
|
||||||
@ -10670,12 +10673,12 @@ composite then it is \textit{probably} prime.
|
|||||||
071 /* while j <= s-1 and y != n1 */
|
071 /* while j <= s-1 and y != n1 */
|
||||||
072 while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) \{
|
072 while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) \{
|
||||||
073 if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) \{
|
073 if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) \{
|
||||||
074 goto __Y;
|
074 goto LBL_Y;
|
||||||
075 \}
|
075 \}
|
||||||
076
|
076
|
||||||
077 /* if y == 1 then composite */
|
077 /* if y == 1 then composite */
|
||||||
078 if (mp_cmp_d (&y, 1) == MP_EQ) \{
|
078 if (mp_cmp_d (&y, 1) == MP_EQ) \{
|
||||||
079 goto __Y;
|
079 goto LBL_Y;
|
||||||
080 \}
|
080 \}
|
||||||
081
|
081
|
||||||
082 ++j;
|
082 ++j;
|
||||||
@ -10683,15 +10686,15 @@ composite then it is \textit{probably} prime.
|
|||||||
084
|
084
|
||||||
085 /* if y != n1 then composite */
|
085 /* if y != n1 then composite */
|
||||||
086 if (mp_cmp (&y, &n1) != MP_EQ) \{
|
086 if (mp_cmp (&y, &n1) != MP_EQ) \{
|
||||||
087 goto __Y;
|
087 goto LBL_Y;
|
||||||
088 \}
|
088 \}
|
||||||
089 \}
|
089 \}
|
||||||
090
|
090
|
||||||
091 /* probably prime now */
|
091 /* probably prime now */
|
||||||
092 *result = MP_YES;
|
092 *result = MP_YES;
|
||||||
093 __Y:mp_clear (&y);
|
093 LBL_Y:mp_clear (&y);
|
||||||
094 __R:mp_clear (&r);
|
094 LBL_R:mp_clear (&r);
|
||||||
095 __N1:mp_clear (&n1);
|
095 LBL_N1:mp_clear (&n1);
|
||||||
096 return err;
|
096 return err;
|
||||||
097 \}
|
097 \}
|
||||||
098 #endif
|
098 #endif
|
||||||
|
@ -242,6 +242,7 @@
|
|||||||
#define BN_MP_INIT_MULTI_C
|
#define BN_MP_INIT_MULTI_C
|
||||||
#define BN_MP_SET_C
|
#define BN_MP_SET_C
|
||||||
#define BN_MP_COUNT_BITS_C
|
#define BN_MP_COUNT_BITS_C
|
||||||
|
#define BN_MP_ABS_C
|
||||||
#define BN_MP_MUL_2D_C
|
#define BN_MP_MUL_2D_C
|
||||||
#define BN_MP_CMP_C
|
#define BN_MP_CMP_C
|
||||||
#define BN_MP_SUB_C
|
#define BN_MP_SUB_C
|
||||||
|
Loading…
x
Reference in New Issue
Block a user