comparison libtommath/bn_mp_exteuclid.c @ 1470:8bba51a55704

Update to libtommath v1.0.1
author Matt Johnston <matt@ucc.asn.au>
date Thu, 08 Feb 2018 23:11:40 +0800
parents 60fc6476e044
children f52919ffd3b1
comparison
equal deleted inserted replaced
1469:51043e868f55 1470:8bba51a55704
27 return err; 27 return err;
28 } 28 }
29 29
30 /* initialize, (u1,u2,u3) = (1,0,a) */ 30 /* initialize, (u1,u2,u3) = (1,0,a) */
31 mp_set(&u1, 1); 31 mp_set(&u1, 1);
32 if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto _ERR; } 32 if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto LBL_ERR; }
33 33
34 /* initialize, (v1,v2,v3) = (0,1,b) */ 34 /* initialize, (v1,v2,v3) = (0,1,b) */
35 mp_set(&v2, 1); 35 mp_set(&v2, 1);
36 if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto _ERR; } 36 if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto LBL_ERR; }
37 37
38 /* loop while v3 != 0 */ 38 /* loop while v3 != 0 */
39 while (mp_iszero(&v3) == MP_NO) { 39 while (mp_iszero(&v3) == MP_NO) {
40 /* q = u3/v3 */ 40 /* q = u3/v3 */
41 if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto _ERR; } 41 if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto LBL_ERR; }
42 42
43 /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ 43 /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
44 if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto _ERR; } 44 if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
45 if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto _ERR; } 45 if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto LBL_ERR; }
46 if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto _ERR; } 46 if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
47 if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto _ERR; } 47 if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto LBL_ERR; }
48 if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto _ERR; } 48 if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
49 if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto _ERR; } 49 if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto LBL_ERR; }
50 50
51 /* (u1,u2,u3) = (v1,v2,v3) */ 51 /* (u1,u2,u3) = (v1,v2,v3) */
52 if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto _ERR; } 52 if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto LBL_ERR; }
53 if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto _ERR; } 53 if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto LBL_ERR; }
54 if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto _ERR; } 54 if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto LBL_ERR; }
55 55
56 /* (v1,v2,v3) = (t1,t2,t3) */ 56 /* (v1,v2,v3) = (t1,t2,t3) */
57 if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto _ERR; } 57 if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto LBL_ERR; }
58 if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto _ERR; } 58 if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto LBL_ERR; }
59 if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto _ERR; } 59 if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto LBL_ERR; }
60 } 60 }
61 61
62 /* make sure U3 >= 0 */ 62 /* make sure U3 >= 0 */
63 if (u3.sign == MP_NEG) { 63 if (u3.sign == MP_NEG) {
64 if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto _ERR; } 64 if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto LBL_ERR; }
65 if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto _ERR; } 65 if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto LBL_ERR; }
66 if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto _ERR; } 66 if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto LBL_ERR; }
67 } 67 }
68 68
69 /* copy result out */ 69 /* copy result out */
70 if (U1 != NULL) { mp_exch(U1, &u1); } 70 if (U1 != NULL) { mp_exch(U1, &u1); }
71 if (U2 != NULL) { mp_exch(U2, &u2); } 71 if (U2 != NULL) { mp_exch(U2, &u2); }
72 if (U3 != NULL) { mp_exch(U3, &u3); } 72 if (U3 != NULL) { mp_exch(U3, &u3); }
73 73
74 err = MP_OKAY; 74 err = MP_OKAY;
75 _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); 75 LBL_ERR:
76 mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
76 return err; 77 return err;
77 } 78 }
78 #endif 79 #endif
79 80
80 /* $Source$ */ 81 /* ref: $Format:%D$ */
81 /* $Revision$ */ 82 /* git commit: $Format:%H$ */
82 /* $Date$ */ 83 /* commit time: $Format:%ai$ */