1 | /* |
2 | Copyright (C) 1999-2007 The Botan Project. All rights reserved. |
3 | |
4 | Redistribution and use in source and binary forms, for any use, with or without |
5 | modification, is permitted provided that the following conditions are met: |
6 | |
7 | 1. Redistributions of source code must retain the above copyright notice, this |
8 | list of conditions, and the following disclaimer. |
9 | |
10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | this list of conditions, and the following disclaimer in the documentation |
12 | and/or other materials provided with the distribution. |
13 | |
14 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED |
15 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
16 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. |
17 | |
18 | IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, |
19 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
20 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
22 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
23 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
24 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | // LICENSEHEADER_END |
27 | namespace QCA { // WRAPNS_LINE |
28 | /************************************************* |
29 | * Division Algorithm Source File * |
30 | * (C) 1999-2007 The Botan Project * |
31 | *************************************************/ |
32 | |
33 | } // WRAPNS_LINE |
34 | #include <botan/numthry.h> |
35 | namespace QCA { // WRAPNS_LINE |
36 | } // WRAPNS_LINE |
37 | #include <botan/mp_core.h> |
38 | namespace QCA { // WRAPNS_LINE |
39 | |
40 | namespace Botan { |
41 | |
42 | namespace { |
43 | |
44 | /************************************************* |
45 | * Handle signed operands, if necessary * |
46 | *************************************************/ |
47 | void sign_fixup(const BigInt &x, const BigInt &y, BigInt &q, BigInt &r) |
48 | { |
49 | if (x.sign() == BigInt::Negative) { |
50 | q.flip_sign(); |
51 | if (r.is_nonzero()) { |
52 | --q; |
53 | r = y.abs() - r; |
54 | } |
55 | } |
56 | if (y.sign() == BigInt::Negative) |
57 | q.flip_sign(); |
58 | } |
59 | |
60 | } |
61 | |
62 | /************************************************* |
63 | * Solve x = q * y + r * |
64 | *************************************************/ |
65 | void divide(const BigInt &x, const BigInt &y_arg, BigInt &q, BigInt &r) |
66 | { |
67 | if (y_arg.is_zero()) |
68 | throw BigInt::DivideByZero(); |
69 | |
70 | BigInt y = y_arg; |
71 | const u32bit y_words = y.sig_words(); |
72 | r = x; |
73 | |
74 | r.set_sign(BigInt::Positive); |
75 | y.set_sign(BigInt::Positive); |
76 | |
77 | s32bit compare = r.cmp(y); |
78 | |
79 | if (compare < 0) |
80 | q = 0; |
81 | else if (compare == 0) { |
82 | q = 1; |
83 | r = 0; |
84 | } else { |
85 | u32bit shifts = 0; |
86 | word y_top = y[y.sig_words() - 1]; |
87 | while (y_top < MP_WORD_TOP_BIT) { |
88 | y_top <<= 1; |
89 | ++shifts; |
90 | } |
91 | y <<= shifts; |
92 | r <<= shifts; |
93 | |
94 | const u32bit n = r.sig_words() - 1, t = y_words - 1; |
95 | |
96 | q.get_reg().create(n: n - t + 1); |
97 | if (n <= t) { |
98 | while (r > y) { |
99 | r -= y; |
100 | q++; |
101 | } |
102 | r >>= shifts; |
103 | sign_fixup(x, y: y_arg, q, r); |
104 | return; |
105 | } |
106 | |
107 | BigInt temp = y << (MP_WORD_BITS * (n - t)); |
108 | |
109 | while (r >= temp) { |
110 | r -= temp; |
111 | ++q[n - t]; |
112 | } |
113 | |
114 | for (u32bit j = n; j != t; --j) { |
115 | const word x_j0 = r.word_at(n: j); |
116 | const word x_j1 = r.word_at(n: j - 1); |
117 | const word y_t = y.word_at(n: t); |
118 | |
119 | if (x_j0 == y_t) |
120 | q[j - t - 1] = MP_WORD_MAX; |
121 | else |
122 | q[j - t - 1] = bigint_divop(x_j0, x_j1, y_t); |
123 | |
124 | while (bigint_divcore(q[j - t - 1], y_t, y.word_at(n: t - 1), x_j0, x_j1, r.word_at(n: j - 2))) |
125 | --q[j - t - 1]; |
126 | |
127 | r -= (q[j - t - 1] * y) << (MP_WORD_BITS * (j - t - 1)); |
128 | if (r.is_negative()) { |
129 | r += y << (MP_WORD_BITS * (j - t - 1)); |
130 | --q[j - t - 1]; |
131 | } |
132 | } |
133 | r >>= shifts; |
134 | } |
135 | |
136 | sign_fixup(x, y: y_arg, q, r); |
137 | } |
138 | |
139 | } |
140 | } // WRAPNS_LINE |
141 | |