monotone

monotone Mtn Source Tree

Root/botan/big_base.cpp

1/*************************************************
2* BigInt Base Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/bigint.h>
7#include <botan/mp_core.h>
8#include <botan/loadstor.h>
9#include <botan/parsing.h>
10#include <botan/util.h>
11
12namespace Botan {
13
14/*************************************************
15* Construct a BigInt from a regular number *
16*************************************************/
17BigInt::BigInt(u64bit n)
18 {
19 set_sign(Positive);
20
21 if(n == 0)
22 return;
23
24 const u32bit limbs_needed = sizeof(u64bit) / sizeof(word);
25
26 reg.create(4*limbs_needed);
27 for(u32bit j = 0; j != limbs_needed; ++j)
28 reg[j] = ((n >> (j*MP_WORD_BITS)) & MP_WORD_MASK);
29 }
30
31/*************************************************
32* Construct a BigInt of the specified size *
33*************************************************/
34BigInt::BigInt(Sign s, u32bit size)
35 {
36 reg.create(round_up(size, 8));
37 signedness = s;
38 }
39
40/*************************************************
41* Construct a BigInt from a "raw" BigInt *
42*************************************************/
43BigInt::BigInt(const BigInt& b)
44 {
45 const u32bit b_words = b.sig_words();
46
47 if(b_words)
48 {
49 reg.create(round_up(b_words, 8));
50 reg.copy(b.data(), b_words);
51 set_sign(b.sign());
52 }
53 else
54 {
55 reg.create(2);
56 set_sign(Positive);
57 }
58 }
59
60/*************************************************
61* Construct a BigInt from a string *
62*************************************************/
63BigInt::BigInt(const std::string& str)
64 {
65 Base base = Decimal;
66 u32bit markers = 0;
67 bool negative = false;
68 if(str.length() > 0 && str[0] == '-') { markers += 1; negative = true; }
69
70 if(str.length() > markers + 2 && str[markers ] == '0' &&
71 str[markers + 1] == 'x')
72 { markers += 2; base = Hexadecimal; }
73 else if(str.length() > markers + 1 && str[markers] == '0')
74 { markers += 1; base = Octal; }
75
76 *this = decode(reinterpret_cast<const byte*>(str.data()) + markers,
77 str.length() - markers, base);
78
79 if(negative) set_sign(Negative);
80 else set_sign(Positive);
81 }
82
83/*************************************************
84* Construct a BigInt from an encoded BigInt *
85*************************************************/
86BigInt::BigInt(const byte input[], u32bit length, Base base)
87 {
88 set_sign(Positive);
89 *this = decode(input, length, base);
90 }
91
92/*************************************************
93* Swap this BigInt with another *
94*************************************************/
95void BigInt::swap(BigInt& other)
96 {
97 std::swap(reg, other.reg);
98 std::swap(signedness, other.signedness);
99 }
100
101/*************************************************
102* Grow the internal storage *
103*************************************************/
104void BigInt::grow_reg(u32bit n) const
105 {
106 reg.grow_to(round_up(size() + n, 8));
107 }
108
109/*************************************************
110* Grow the internal storage *
111*************************************************/
112void BigInt::grow_to(u32bit n) const
113 {
114 if(n > size())
115 reg.grow_to(round_up(n, 8));
116 }
117
118/*************************************************
119* Comparison Function *
120*************************************************/
121s32bit BigInt::cmp(const BigInt& n, bool check_signs) const
122 {
123 if(check_signs)
124 {
125 if(n.is_positive() && this->is_negative()) return -1;
126 if(n.is_negative() && this->is_positive()) return 1;
127 if(n.is_negative() && this->is_negative())
128 return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words()));
129 }
130 return bigint_cmp(data(), sig_words(), n.data(), n.sig_words());
131 }
132
133/*************************************************
134* Convert this number to a u32bit, if possible *
135*************************************************/
136u32bit BigInt::to_u32bit() const
137 {
138 if(is_negative())
139 throw Encoding_Error("BigInt::to_u32bit: Number is negative");
140 if(bits() >= 32)
141 throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
142
143 u32bit out = 0;
144 for(u32bit j = 0; j != 4; ++j)
145 out = (out << 8) | byte_at(3-j);
146 return out;
147 }
148
149/*************************************************
150* Return byte n of this number *
151*************************************************/
152byte BigInt::byte_at(u32bit n) const
153 {
154 const u32bit WORD_BYTES = sizeof(word);
155 u32bit word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES;
156 if(word_num >= size())
157 return 0;
158 else
159 return get_byte(WORD_BYTES - byte_num - 1, reg[word_num]);
160 }
161
162/*************************************************
163* Return bit n of this number *
164*************************************************/
165bool BigInt::get_bit(u32bit n) const
166 {
167 return ((word_at(n / MP_WORD_BITS) >> (n % MP_WORD_BITS)) & 1);
168 }
169
170/*************************************************
171* Return bits {offset...offset+length} *
172*************************************************/
173u32bit BigInt::get_substring(u32bit offset, u32bit length) const
174 {
175 if(length > 32)
176 throw Invalid_Argument("BigInt::get_substring: Substring size too big");
177
178 u64bit piece = 0;
179 for(u32bit j = 0; j != 8; ++j)
180 piece = (piece << 8) | byte_at((offset / 8) + (7-j));
181
182 u64bit mask = (1 << length) - 1;
183 u32bit shift = (offset % 8);
184
185 return static_cast<u32bit>((piece >> shift) & mask);
186 }
187
188/*************************************************
189* Set bit number n *
190*************************************************/
191void BigInt::set_bit(u32bit n)
192 {
193 const u32bit which = n / MP_WORD_BITS;
194 const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
195 if(which >= size()) grow_to(which + 1);
196 reg[which] |= mask;
197 }
198
199/*************************************************
200* Clear bit number n *
201*************************************************/
202void BigInt::clear_bit(u32bit n)
203 {
204 const u32bit which = n / MP_WORD_BITS;
205 const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
206 if(which < size())
207 reg[which] &= ~mask;
208 }
209
210/*************************************************
211* Clear all but the lowest n bits *
212*************************************************/
213void BigInt::mask_bits(u32bit n)
214 {
215 if(n == 0) { clear(); return; }
216 if(n >= bits()) return;
217
218 const u32bit top_word = n / MP_WORD_BITS;
219 const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1;
220
221 if(top_word < size())
222 for(u32bit j = top_word + 1; j != size(); ++j)
223 reg[j] = 0;
224
225 reg[top_word] &= mask;
226 }
227
228/*************************************************
229* Count the significant words *
230*************************************************/
231u32bit BigInt::sig_words() const
232 {
233 const word* x = data();
234 u32bit top_set = size();
235
236 while(top_set >= 4)
237 {
238 word sum = x[top_set-1] | x[top_set-2] | x[top_set-3] | x[top_set-4];
239 if(sum) break;
240 else top_set -= 4;
241 }
242 while(top_set && (x[top_set-1] == 0))
243 top_set--;
244 return top_set;
245 }
246
247/*************************************************
248* Count how many bytes are being used *
249*************************************************/
250u32bit BigInt::bytes() const
251 {
252 return (bits() + 7) / 8;
253 }
254
255/*************************************************
256* Count how many bits are being used *
257*************************************************/
258u32bit BigInt::bits() const
259 {
260 if(sig_words() == 0)
261 return 0;
262
263 u32bit full_words = sig_words() - 1, top_bits = MP_WORD_BITS;
264 word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT;
265
266 while(top_bits && ((top_word & mask) == 0))
267 { mask >>= 1; top_bits--; }
268
269 return (full_words * MP_WORD_BITS + top_bits);
270 }
271
272/*************************************************
273* Calcluate the size in a certain base *
274*************************************************/
275u32bit BigInt::encoded_size(Base base) const
276 {
277 static const double LOG_2_BASE_10 = 0.30102999566;
278
279 if(base == Binary)
280 return bytes();
281 else if(base == Hexadecimal)
282 return 2*bytes();
283 else if(base == Octal)
284 return ((bits() + 2) / 3);
285 else if(base == Decimal)
286 return static_cast<u32bit>((bits() * LOG_2_BASE_10) + 1);
287 else
288 throw Invalid_Argument("Unknown base for BigInt encoding");
289 }
290
291/*************************************************
292* Return true if this number is zero *
293*************************************************/
294bool BigInt::is_zero() const
295 {
296 for(u32bit j = 0; j != size(); ++j)
297 if(reg[j]) return false;
298 return true;
299 }
300
301/*************************************************
302* Set the sign *
303*************************************************/
304void BigInt::set_sign(Sign s)
305 {
306 if(is_zero())
307 signedness = Positive;
308 else
309 signedness = s;
310 }
311
312/*************************************************
313* Reverse the value of the sign flag *
314*************************************************/
315void BigInt::flip_sign()
316 {
317 set_sign(reverse_sign());
318 }
319
320/*************************************************
321* Return the opposite value of the current sign *
322*************************************************/
323BigInt::Sign BigInt::reverse_sign() const
324 {
325 if(sign() == Positive)
326 return Negative;
327 return Positive;
328 }
329
330/*************************************************
331* Return the negation of this number *
332*************************************************/
333BigInt BigInt::operator-() const
334 {
335 BigInt x = (*this);
336 x.flip_sign();
337 return x;
338 }
339
340/*************************************************
341* Return a reference to the indexed word *
342*************************************************/
343word& BigInt::operator[](u32bit index)
344 {
345 reg.grow_to(index+1);
346 return reg[index];
347 }
348
349/*************************************************
350* Return the value of the indexed word *
351*************************************************/
352word BigInt::operator[](u32bit index) const
353 {
354 return (index < size()) ? reg[index] : 0;
355 }
356
357/*************************************************
358* Return the absolute value of this number *
359*************************************************/
360BigInt BigInt::abs() const
361 {
362 BigInt x = (*this);
363 x.set_sign(Positive);
364 return x;
365 }
366
367/*************************************************
368* Encode this number into bytes *
369*************************************************/
370void BigInt::binary_encode(byte output[]) const
371 {
372 const u32bit sig_bytes = bytes();
373 for(u32bit j = 0; j != sig_bytes; ++j)
374 output[sig_bytes-j-1] = byte_at(j);
375 }
376
377/*************************************************
378* Set this number to the value in buf *
379*************************************************/
380void BigInt::binary_decode(const byte buf[], u32bit length)
381 {
382 const u32bit WORD_BYTES = sizeof(word);
383 reg.create(round_up((length / WORD_BYTES) + 1, 8));
384
385 for(u32bit j = 0; j != length / WORD_BYTES; ++j)
386 {
387 u32bit top = length - WORD_BYTES*j;
388 for(u32bit k = WORD_BYTES; k > 0; --k)
389 reg[j] = (reg[j] << 8) | buf[top - k];
390 }
391 for(u32bit j = 0; j != length % WORD_BYTES; ++j)
392 reg[length / WORD_BYTES] = (reg[length / WORD_BYTES] << 8) | buf[j];
393 }
394
395/*************************************************
396* Set this number to the value in buf *
397*************************************************/
398void BigInt::binary_decode(const MemoryRegion<byte>& buf)
399 {
400 binary_decode(buf, buf.size());
401 }
402
403}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status