Mercurial > dropbear
diff libtommath/bn_mp_exteuclid.c @ 1692:1051e4eea25a
Update LibTomMath to 1.2.0 (#84)
* update C files
* update other files
* update headers
* update makefiles
* remove mp_set/get_double()
* use ltm 1.2.0 API
* update ltm_desc
* use bundled tommath if system-tommath is too old
* XMALLOC etc. were changed to MP_MALLOC etc.
author | Steffen Jaeckel <s@jaeckel.eu> |
---|---|
date | Tue, 26 May 2020 17:36:47 +0200 |
parents | f52919ffd3b1 |
children |
line wrap: on
line diff
--- a/libtommath/bn_mp_exteuclid.c Tue May 26 23:27:26 2020 +0800 +++ b/libtommath/bn_mp_exteuclid.c Tue May 26 17:36:47 2020 +0200 @@ -1,24 +1,15 @@ #include "tommath_private.h" #ifdef BN_MP_EXTEUCLID_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis - * - * LibTomMath is a library that provides multiple-precision - * integer arithmetic as well as number theoretic functionality. - * - * The library was designed directly after the MPI library by - * Michael Fromberger but has been written from scratch with - * additional optimizations in place. - * - * SPDX-License-Identifier: Unlicense - */ +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ /* Extended euclidean algorithm of (a, b) produces a*u1 + b*u2 = u3 */ -int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) +mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) { mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp; - int err; + mp_err err; if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) { return err; @@ -26,77 +17,41 @@ /* initialize, (u1,u2,u3) = (1,0,a) */ mp_set(&u1, 1uL); - if ((err = mp_copy(a, &u3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(a, &u3)) != MP_OKAY) goto LBL_ERR; /* initialize, (v1,v2,v3) = (0,1,b) */ mp_set(&v2, 1uL); - if ((err = mp_copy(b, &v3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(b, &v3)) != MP_OKAY) goto LBL_ERR; /* loop while v3 != 0 */ - while (mp_iszero(&v3) == MP_NO) { + while (!MP_IS_ZERO(&v3)) { /* q = u3/v3 */ - if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) goto LBL_ERR; /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ - if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) goto LBL_ERR; /* (u1,u2,u3) = (v1,v2,v3) */ - if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(&v1, &u1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&v2, &u2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&v3, &u3)) != MP_OKAY) goto LBL_ERR; /* (v1,v2,v3) = (t1,t2,t3) */ - if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(&t1, &v1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&t2, &v2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&t3, &v3)) != MP_OKAY) goto LBL_ERR; } /* make sure U3 >= 0 */ if (u3.sign == MP_NEG) { - if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_neg(&u1, &u1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_neg(&u2, &u2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_neg(&u3, &u3)) != MP_OKAY) goto LBL_ERR; } /* copy result out */ @@ -116,7 +71,3 @@ return err; } #endif - -/* ref: HEAD -> master, tag: v1.1.0 */ -/* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ -/* commit time: 2019-01-28 20:32:32 +0100 */