monotone

monotone Mtn Source Tree

Root/botan/bigint.h

1/*************************************************
2* BigInt Header File *
3* (C) 1999-2007 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);
82 word operator[](u32bit) const;
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 void binary_decode(const MemoryRegion<byte>&);
90 u32bit encoded_size(Base = Binary) const;
91
92 static SecureVector<byte> encode(const BigInt&, Base = Binary);
93 static void encode(byte[], const BigInt&, Base = Binary);
94 static BigInt decode(const byte[], u32bit, Base = Binary);
95 static BigInt decode(const MemoryRegion<byte>&, Base = Binary);
96 static SecureVector<byte> encode_1363(const BigInt&, u32bit);
97
98 void swap(BigInt&);
99
100 BigInt() { signedness = Positive; }
101 BigInt(u64bit);
102 BigInt(const BigInt&);
103 BigInt(const std::string&);
104 BigInt(const byte[], u32bit, Base = Binary);
105 BigInt(Sign, u32bit);
106 BigInt(NumberType, u32bit);
107 private:
108 void grow_to(u32bit) const;
109 SecureVector<word> reg;
110 Sign signedness;
111 };
112
113/*************************************************
114* Arithmetic Operators *
115*************************************************/
116BigInt operator+(const BigInt&, const BigInt&);
117BigInt operator-(const BigInt&, const BigInt&);
118BigInt operator*(const BigInt&, const BigInt&);
119BigInt operator/(const BigInt&, const BigInt&);
120BigInt operator%(const BigInt&, const BigInt&);
121word operator%(const BigInt&, word);
122BigInt operator<<(const BigInt&, u32bit);
123BigInt operator>>(const BigInt&, u32bit);
124
125/*************************************************
126* Comparison Operators *
127*************************************************/
128inline bool operator==(const BigInt& a, const BigInt& b)
129 { return (a.cmp(b) == 0); }
130inline bool operator!=(const BigInt& a, const BigInt& b)
131 { return (a.cmp(b) != 0); }
132inline bool operator<=(const BigInt& a, const BigInt& b)
133 { return (a.cmp(b) <= 0); }
134inline bool operator>=(const BigInt& a, const BigInt& b)
135 { return (a.cmp(b) >= 0); }
136inline bool operator<(const BigInt& a, const BigInt& b)
137 { return (a.cmp(b) < 0); }
138inline bool operator>(const BigInt& a, const BigInt& b)
139 { return (a.cmp(b) > 0); }
140
141/*************************************************
142* I/O Operators *
143*************************************************/
144std::ostream& operator<<(std::ostream&, const BigInt&);
145std::istream& operator>>(std::istream&, BigInt&);
146
147}
148
149namespace std {
150
151inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); }
152
153}
154
155#endif

Archive Download this file

Branches

Tags

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