comparison libtommath/bn_mp_exteuclid.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_EXTEUCLID_C 2 #ifdef BN_MP_EXTEUCLID_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* Extended euclidean algorithm of (a, b) produces 6 /* Extended euclidean algorithm of (a, b) produces
19 a*u1 + b*u2 = u3 7 a*u1 + b*u2 = u3
20 */ 8 */
21 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) 9 mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
22 { 10 {
23 mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp; 11 mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp;
24 int err; 12 mp_err err;
25 13
26 if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) { 14 if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) {
27 return err; 15 return err;
28 } 16 }
29 17
30 /* initialize, (u1,u2,u3) = (1,0,a) */ 18 /* initialize, (u1,u2,u3) = (1,0,a) */
31 mp_set(&u1, 1); 19 mp_set(&u1, 1uL);
32 if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto LBL_ERR; } 20 if ((err = mp_copy(a, &u3)) != MP_OKAY) goto LBL_ERR;
33 21
34 /* initialize, (v1,v2,v3) = (0,1,b) */ 22 /* initialize, (v1,v2,v3) = (0,1,b) */
35 mp_set(&v2, 1); 23 mp_set(&v2, 1uL);
36 if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto LBL_ERR; } 24 if ((err = mp_copy(b, &v3)) != MP_OKAY) goto LBL_ERR;
37 25
38 /* loop while v3 != 0 */ 26 /* loop while v3 != 0 */
39 while (mp_iszero(&v3) == MP_NO) { 27 while (!MP_IS_ZERO(&v3)) {
40 /* q = u3/v3 */ 28 /* q = u3/v3 */
41 if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto LBL_ERR; } 29 if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) goto LBL_ERR;
42 30
43 /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ 31 /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
44 if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; } 32 if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) goto LBL_ERR;
45 if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto LBL_ERR; } 33 if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) goto LBL_ERR;
46 if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; } 34 if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) goto LBL_ERR;
47 if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto LBL_ERR; } 35 if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) goto LBL_ERR;
48 if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; } 36 if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) goto LBL_ERR;
49 if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto LBL_ERR; } 37 if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) goto LBL_ERR;
50 38
51 /* (u1,u2,u3) = (v1,v2,v3) */ 39 /* (u1,u2,u3) = (v1,v2,v3) */
52 if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto LBL_ERR; } 40 if ((err = mp_copy(&v1, &u1)) != MP_OKAY) goto LBL_ERR;
53 if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto LBL_ERR; } 41 if ((err = mp_copy(&v2, &u2)) != MP_OKAY) goto LBL_ERR;
54 if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto LBL_ERR; } 42 if ((err = mp_copy(&v3, &u3)) != MP_OKAY) goto LBL_ERR;
55 43
56 /* (v1,v2,v3) = (t1,t2,t3) */ 44 /* (v1,v2,v3) = (t1,t2,t3) */
57 if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto LBL_ERR; } 45 if ((err = mp_copy(&t1, &v1)) != MP_OKAY) goto LBL_ERR;
58 if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto LBL_ERR; } 46 if ((err = mp_copy(&t2, &v2)) != MP_OKAY) goto LBL_ERR;
59 if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto LBL_ERR; } 47 if ((err = mp_copy(&t3, &v3)) != MP_OKAY) goto LBL_ERR;
60 } 48 }
61 49
62 /* make sure U3 >= 0 */ 50 /* make sure U3 >= 0 */
63 if (u3.sign == MP_NEG) { 51 if (u3.sign == MP_NEG) {
64 if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto LBL_ERR; } 52 if ((err = mp_neg(&u1, &u1)) != MP_OKAY) goto LBL_ERR;
65 if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto LBL_ERR; } 53 if ((err = mp_neg(&u2, &u2)) != MP_OKAY) goto LBL_ERR;
66 if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto LBL_ERR; } 54 if ((err = mp_neg(&u3, &u3)) != MP_OKAY) goto LBL_ERR;
67 } 55 }
68 56
69 /* copy result out */ 57 /* copy result out */
70 if (U1 != NULL) { mp_exch(U1, &u1); } 58 if (U1 != NULL) {
71 if (U2 != NULL) { mp_exch(U2, &u2); } 59 mp_exch(U1, &u1);
72 if (U3 != NULL) { mp_exch(U3, &u3); } 60 }
61 if (U2 != NULL) {
62 mp_exch(U2, &u2);
63 }
64 if (U3 != NULL) {
65 mp_exch(U3, &u3);
66 }
73 67
74 err = MP_OKAY; 68 err = MP_OKAY;
75 LBL_ERR: 69 LBL_ERR:
76 mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); 70 mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
77 return err; 71 return err;
78 } 72 }
79 #endif 73 #endif
80
81 /* ref: $Format:%D$ */
82 /* git commit: $Format:%H$ */
83 /* commit time: $Format:%ai$ */