/src/botan/src/lib/math/bigint/big_ops3.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  | * BigInt Binary Operators  | 
3  |  | * (C) 1999-2007,2018 Jack Lloyd  | 
4  |  | *     2016 Matthias Gierlings  | 
5  |  | *  | 
6  |  | * Botan is released under the Simplified BSD License (see license.txt)  | 
7  |  | */  | 
8  |  |  | 
9  |  | #include <botan/bigint.h>  | 
10  |  | #include <botan/internal/divide.h>  | 
11  |  | #include <botan/internal/mp_core.h>  | 
12  |  | #include <botan/internal/bit_ops.h>  | 
13  |  | #include <algorithm>  | 
14  |  |  | 
15  |  | namespace Botan { | 
16  |  |  | 
17  |  | //static  | 
18  |  | BigInt BigInt::add2(const BigInt& x, const word y[], size_t y_words, BigInt::Sign y_sign)  | 
19  | 194k  |    { | 
20  | 194k  |    const size_t x_sw = x.sig_words();  | 
21  |  |  | 
22  | 194k  |    BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);  | 
23  |  |  | 
24  | 194k  |    if(x.sign() == y_sign)  | 
25  | 168k  |       { | 
26  | 168k  |       bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);  | 
27  | 168k  |       z.set_sign(x.sign());  | 
28  | 168k  |       }  | 
29  | 26.5k  |    else  | 
30  | 26.5k  |       { | 
31  | 26.5k  |       const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x.data(), x_sw, y, y_words);  | 
32  |  |  | 
33  |  |       //z.sign_fixup(relative_size, y_sign);  | 
34  | 26.5k  |       if(relative_size < 0)  | 
35  | 25.6k  |          z.set_sign(y_sign);  | 
36  | 903  |       else if(relative_size == 0)  | 
37  | 155  |          z.set_sign(BigInt::Positive);  | 
38  | 748  |       else  | 
39  | 748  |          z.set_sign(x.sign());  | 
40  | 26.5k  |       }  | 
41  |  |  | 
42  | 194k  |    return z;  | 
43  | 194k  |    }  | 
44  |  |  | 
45  |  | /*  | 
46  |  | * Multiplication Operator  | 
47  |  | */  | 
48  |  | BigInt operator*(const BigInt& x, const BigInt& y)  | 
49  | 2.36k  |    { | 
50  | 2.36k  |    const size_t x_sw = x.sig_words();  | 
51  | 2.36k  |    const size_t y_sw = y.sig_words();  | 
52  |  |  | 
53  | 2.36k  |    BigInt z = BigInt::with_capacity(x.size() + y.size());  | 
54  |  |  | 
55  | 2.36k  |    if(x_sw == 1 && y_sw)  | 
56  | 439  |       bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));  | 
57  | 1.92k  |    else if(y_sw == 1 && x_sw)  | 
58  | 68  |       bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));  | 
59  | 1.85k  |    else if(x_sw && y_sw)  | 
60  | 1.81k  |       { | 
61  | 1.81k  |       secure_vector<word> workspace(z.size());  | 
62  |  |  | 
63  | 1.81k  |       bigint_mul(z.mutable_data(), z.size(),  | 
64  | 1.81k  |                  x.data(), x.size(), x_sw,  | 
65  | 1.81k  |                  y.data(), y.size(), y_sw,  | 
66  | 1.81k  |                  workspace.data(), workspace.size());  | 
67  | 1.81k  |       }  | 
68  |  |  | 
69  | 2.36k  |    z.cond_flip_sign(x_sw > 0 && y_sw > 0 && x.sign() != y.sign());  | 
70  |  |  | 
71  | 2.36k  |    return z;  | 
72  | 2.36k  |    }  | 
73  |  |  | 
74  |  | /*  | 
75  |  | * Multiplication Operator  | 
76  |  | */  | 
77  |  | BigInt operator*(const BigInt& x, word y)  | 
78  | 10.0k  |    { | 
79  | 10.0k  |    const size_t x_sw = x.sig_words();  | 
80  |  |  | 
81  | 10.0k  |    BigInt z = BigInt::with_capacity(x_sw + 1);  | 
82  |  |  | 
83  | 10.0k  |    if(x_sw && y)  | 
84  | 4.98k  |       { | 
85  | 4.98k  |       bigint_linmul3(z.mutable_data(), x.data(), x_sw, y);  | 
86  | 4.98k  |       z.set_sign(x.sign());  | 
87  | 4.98k  |       }  | 
88  |  |  | 
89  | 10.0k  |    return z;  | 
90  | 10.0k  |    }  | 
91  |  |  | 
92  |  | /*  | 
93  |  | * Division Operator  | 
94  |  | */  | 
95  |  | BigInt operator/(const BigInt& x, const BigInt& y)  | 
96  | 0  |    { | 
97  | 0  |    if(y.sig_words() == 1)  | 
98  | 0  |       { | 
99  | 0  |       return x / y.word_at(0);  | 
100  | 0  |       }  | 
101  |  |  | 
102  | 0  |    BigInt q, r;  | 
103  | 0  |    vartime_divide(x, y, q, r);  | 
104  | 0  |    return q;  | 
105  | 0  |    }  | 
106  |  |  | 
107  |  | /*  | 
108  |  | * Division Operator  | 
109  |  | */  | 
110  |  | BigInt operator/(const BigInt& x, word y)  | 
111  | 0  |    { | 
112  | 0  |    if(y == 0)  | 
113  | 0  |       throw Invalid_Argument("BigInt::operator/ divide by zero"); | 
114  |  |  | 
115  | 0  |    BigInt q;  | 
116  | 0  |    word r;  | 
117  | 0  |    ct_divide_word(x, y, q, r);  | 
118  | 0  |    return q;  | 
119  | 0  |    }  | 
120  |  |  | 
121  |  | /*  | 
122  |  | * Modulo Operator  | 
123  |  | */  | 
124  |  | BigInt operator%(const BigInt& n, const BigInt& mod)  | 
125  | 446  |    { | 
126  | 446  |    if(mod.is_zero())  | 
127  | 5  |       throw Invalid_Argument("BigInt::operator% divide by zero"); | 
128  | 441  |    if(mod.is_negative())  | 
129  | 0  |       throw Invalid_Argument("BigInt::operator% modulus must be > 0"); | 
130  | 441  |    if(n.is_positive() && mod.is_positive() && n < mod)  | 
131  | 40  |       return n;  | 
132  |  |  | 
133  | 401  |    if(mod.sig_words() == 1)  | 
134  | 118  |       { | 
135  | 118  |       return BigInt::from_word(n % mod.word_at(0));  | 
136  | 118  |       }  | 
137  |  |  | 
138  | 283  |    BigInt q, r;  | 
139  | 283  |    vartime_divide(n, mod, q, r);  | 
140  | 283  |    return r;  | 
141  | 401  |    }  | 
142  |  |  | 
143  |  | /*  | 
144  |  | * Modulo Operator  | 
145  |  | */  | 
146  |  | word operator%(const BigInt& n, word mod)  | 
147  | 128  |    { | 
148  | 128  |    if(mod == 0)  | 
149  | 0  |       throw Invalid_Argument("BigInt::operator% divide by zero"); | 
150  |  |  | 
151  | 128  |    if(mod == 1)  | 
152  | 1  |       return 0;  | 
153  |  |  | 
154  | 127  |    word remainder = 0;  | 
155  |  |  | 
156  | 127  |    if(is_power_of_2(mod))  | 
157  | 13  |       { | 
158  | 13  |       remainder = (n.word_at(0) & (mod - 1));  | 
159  | 13  |       }  | 
160  | 114  |    else  | 
161  | 114  |       { | 
162  | 114  |       const size_t sw = n.sig_words();  | 
163  | 1.12k  |       for(size_t i = sw; i > 0; --i)  | 
164  | 1.01k  |          { | 
165  | 1.01k  |          remainder = bigint_modop(remainder, n.word_at(i-1), mod);  | 
166  | 1.01k  |          }  | 
167  | 114  |       }  | 
168  |  |  | 
169  | 127  |    if(remainder && n.sign() == BigInt::Negative)  | 
170  | 41  |       return mod - remainder;  | 
171  | 86  |    return remainder;  | 
172  | 127  |    }  | 
173  |  |  | 
174  |  | /*  | 
175  |  | * Left Shift Operator  | 
176  |  | */  | 
177  |  | BigInt operator<<(const BigInt& x, size_t shift)  | 
178  | 375  |    { | 
179  | 375  |    const size_t shift_words = shift / BOTAN_MP_WORD_BITS,  | 
180  | 375  |                 shift_bits  = shift % BOTAN_MP_WORD_BITS;  | 
181  |  |  | 
182  | 375  |    const size_t x_sw = x.sig_words();  | 
183  |  |  | 
184  | 375  |    BigInt y = BigInt::with_capacity(x_sw + shift_words + (shift_bits ? 1 : 0));  | 
185  | 375  |    bigint_shl2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);  | 
186  | 375  |    y.set_sign(x.sign());  | 
187  | 375  |    return y;  | 
188  | 375  |    }  | 
189  |  |  | 
190  |  | /*  | 
191  |  | * Right Shift Operator  | 
192  |  | */  | 
193  |  | BigInt operator>>(const BigInt& x, size_t shift)  | 
194  | 766  |    { | 
195  | 766  |    const size_t shift_words = shift / BOTAN_MP_WORD_BITS;  | 
196  | 766  |    const size_t shift_bits  = shift % BOTAN_MP_WORD_BITS;  | 
197  | 766  |    const size_t x_sw = x.sig_words();  | 
198  |  |  | 
199  | 766  |    if(shift_words >= x_sw)  | 
200  | 45  |       return BigInt::zero();  | 
201  |  |  | 
202  | 721  |    BigInt y = BigInt::with_capacity(x_sw - shift_words);  | 
203  | 721  |    bigint_shr2(y.mutable_data(), x.data(), x_sw, shift_words, shift_bits);  | 
204  |  |  | 
205  | 721  |    if(x.is_negative() && y.is_zero())  | 
206  | 0  |       y.set_sign(BigInt::Positive);  | 
207  | 721  |    else  | 
208  | 721  |       y.set_sign(x.sign());  | 
209  |  |  | 
210  | 721  |    return y;  | 
211  | 766  |    }  | 
212  |  |  | 
213  |  | }  |