monotone

monotone Mtn Source Tree

Root/botan/bigint.h

1/*************************************************
2* BigInt Header File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_BIGINT_H__
7#define BOTAN_BIGINT_H__
8
9#include <botan/base.h>
10#include <botan/mp_types.h>
11#include <iosfwd>
12
13namespace Botan {
14
15/*************************************************
16* BigInt *
17*************************************************/
18class BigInt
19 {
20 public:
21 enum Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 };
22 enum Sign { Negative = 0, Positive = 1 };
23 enum NumberType { Random, Power2 };
24
25 struct DivideByZero : public Exception
26 { DivideByZero() : Exception("BigInt divide by zero") {} };
27
28 BigInt& operator+=(const BigInt&);
29 BigInt& operator-=(const BigInt&);
30
31 BigInt& operator*=(const BigInt&);
32 BigInt& operator/=(const BigInt&);
33 BigInt& operator%=(const BigInt&);
34 word operator%=(word);
35 BigInt& operator<<=(u32bit);
36 BigInt& operator>>=(u32bit);
37
38 BigInt& operator++() { return (*this += 1); }
39 BigInt& operator--() { return (*this -= 1); }
40 BigInt operator++(int) { BigInt x = (*this); ++(*this); return x; }
41 BigInt operator--(int) { BigInt x = (*this); --(*this); return x; }
42
43 BigInt operator-() const;
44 bool operator !() const { return (!is_nonzero()); }
45
46 s32bit cmp(const BigInt&, bool = true) const;
47 bool is_even() const { return (get_bit(0) == 0); }
48 bool is_odd() const { return (get_bit(0) == 1); }
49 bool is_nonzero() const { return (!is_zero()); }
50 bool is_zero() const;
51
52 void set_bit(u32bit);
53 void clear_bit(u32bit);
54 void mask_bits(u32bit);
55
56 bool get_bit(u32bit) const;
57 u32bit get_substring(u32bit, u32bit) const;
58 byte byte_at(u32bit) const;
59 word word_at(u32bit n) const
60 { return ((n < size()) ? reg[n] : 0); }
61
62 u32bit to_u32bit() const;
63
64 bool is_negative() const { return (sign() == Negative); }
65 bool is_positive() const { return (sign() == Positive); }
66 Sign sign() const { return (signedness); }
67 Sign reverse_sign() const;
68 void flip_sign();
69 void set_sign(Sign);
70 BigInt abs() const;
71
72 u32bit size() const { return reg.size(); }
73 u32bit sig_words() const;
74 u32bit bytes() const;
75 u32bit bits() const;
76
77 const word* data() const { return reg.begin(); }
78 SecureVector<word>& get_reg() { return reg; }
79 void grow_reg(u32bit) const;
80
81 word& operator[](u32bit index) { return reg[index]; }
82 word operator[](u32bit index) const { return reg[index]; }
83 void clear() { reg.clear(); }
84
85 void randomize(u32bit = 0);
86
87 void binary_encode(byte[]) const;
88 void binary_decode(const byte[], u32bit);
89 u32bit encoded_size(Base = Binary) const;
90
91 static SecureVector<byte> encode(const BigInt&, Base = Binary);
92 static void encode(byte[], const BigInt&, Base = Binary);
93 static BigInt decode(const byte[], u32bit, Base = Binary);
94 static BigInt decode(const MemoryRegion<byte>&, Base = Binary);
95 static SecureVector<byte> encode_1363(const BigInt&, u32bit);
96
97 void swap(BigInt&);
98
99 BigInt() { signedness = Positive; }
100 BigInt(u64bit);
101 BigInt(const BigInt&);
102 BigInt(const std::string&);
103 BigInt(const byte[], u32bit, Base = Binary);
104 BigInt(Sign, u32bit);
105 BigInt(NumberType, u32bit);
106 private:
107 void grow_to(u32bit) const;
108 SecureVector<word> reg;
109 Sign signedness;
110 };
111
112/*************************************************
113* Arithmetic Operators *
114*************************************************/
115BigInt operator+(const BigInt&, const BigInt&);
116BigInt operator-(const BigInt&, const BigInt&);
117BigInt operator*(const BigInt&, const BigInt&);
118BigInt operator/(const BigInt&, const BigInt&);
119BigInt operator%(const BigInt&, const BigInt&);
120word operator%(const BigInt&, word);
121BigInt operator<<(const BigInt&, u32bit);
122BigInt operator>>(const BigInt&, u32bit);
123
124/*************************************************
125* Comparison Operators *
126*************************************************/
127inline bool operator==(const BigInt& a, const BigInt& b)
128 { return (a.cmp(b) == 0); }
129inline bool operator!=(const BigInt& a, const BigInt& b)
130 { return (a.cmp(b) != 0); }
131inline bool operator<=(const BigInt& a, const BigInt& b)
132 { return (a.cmp(b) <= 0); }
133inline bool operator>=(const BigInt& a, const BigInt& b)
134 { return (a.cmp(b) >= 0); }
135inline bool operator<(const BigInt& a, const BigInt& b)
136 { return (a.cmp(b) < 0); }
137inline bool operator>(const BigInt& a, const BigInt& b)
138 { return (a.cmp(b) > 0); }
139
140/*************************************************
141* I/O Operators *
142*************************************************/
143std::ostream& operator<<(std::ostream&, const BigInt&);
144std::istream& operator>>(std::istream&, BigInt&);
145
146}
147
148namespace std {
149
150inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); }
151
152}
153
154#endif

Archive Download this file

Branches

Tags

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