Mercurial > dropbear
comparison libtommath/bn_mp_div_2.c @ 284:eed26cff980b
propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
to branch 'au.asn.ucc.matt.dropbear' (head fff0894a0399405a9410ea1c6d118f342cf2aa64)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 08 Mar 2006 13:23:49 +0000 |
parents | |
children | 5ff8218bcee9 |
comparison
equal
deleted
inserted
replaced
283:bd240aa12ba7 | 284:eed26cff980b |
---|---|
1 #include <tommath.h> | |
2 #ifdef BN_MP_DIV_2_C | |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | |
4 * | |
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://math.libtomcrypt.org | |
16 */ | |
17 | |
18 /* b = a/2 */ | |
19 int mp_div_2(mp_int * a, mp_int * b) | |
20 { | |
21 int x, res, oldused; | |
22 | |
23 /* copy */ | |
24 if (b->alloc < a->used) { | |
25 if ((res = mp_grow (b, a->used)) != MP_OKAY) { | |
26 return res; | |
27 } | |
28 } | |
29 | |
30 oldused = b->used; | |
31 b->used = a->used; | |
32 { | |
33 register mp_digit r, rr, *tmpa, *tmpb; | |
34 | |
35 /* source alias */ | |
36 tmpa = a->dp + b->used - 1; | |
37 | |
38 /* dest alias */ | |
39 tmpb = b->dp + b->used - 1; | |
40 | |
41 /* carry */ | |
42 r = 0; | |
43 for (x = b->used - 1; x >= 0; x--) { | |
44 /* get the carry for the next iteration */ | |
45 rr = *tmpa & 1; | |
46 | |
47 /* shift the current digit, add in carry and store */ | |
48 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); | |
49 | |
50 /* forward carry to next iteration */ | |
51 r = rr; | |
52 } | |
53 | |
54 /* zero excess digits */ | |
55 tmpb = b->dp + b->used; | |
56 for (x = b->used; x < oldused; x++) { | |
57 *tmpb++ = 0; | |
58 } | |
59 } | |
60 b->sign = a->sign; | |
61 mp_clamp (b); | |
62 return MP_OKAY; | |
63 } | |
64 #endif |