monotone

monotone Mtn Source Tree

Root/botan/bigint.h

1/*************************************************
2* BigInt Header File *
3* (C) 1999-2005 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++();
39 BigInt& operator--();
40 BigInt operator++(int) { BigInt tmp = (*this); ++(*this); return tmp; }
41 BigInt operator--(int) { BigInt tmp = (*this); --(*this); return tmp; }
42
43 BigInt operator-() const;
44 bool operator !() const { return (!is_nonzero()); }
45
46 void add(word);
47 void sub(word);
48
49 s32bit cmp(const BigInt&, bool = true) const;
50 bool is_even() const { return (get_bit(0) == 0); }
51 bool is_odd() const { return (get_bit(0) == 1); }
52 bool is_nonzero() const { return (!is_zero()); }
53 bool is_zero() const;
54
55 void set_bit(u32bit);
56 void clear_bit(u32bit);
57 void mask_bits(u32bit);
58
59 bool get_bit(u32bit) const;
60 u32bit get_nibble(u32bit, u32bit) const;
61 byte byte_at(u32bit) const;
62 word word_at(u32bit) const;
63
64 u32bit to_u32bit() const;
65
66 bool is_negative() const { return (sign() == Negative); }
67 bool is_positive() const { return (sign() == Positive); }
68 Sign sign() const { return (signedness); }
69 Sign reverse_sign() const;
70 void flip_sign();
71 void set_sign(Sign);
72 BigInt abs() const;
73
74 u32bit size() const { return reg.size(); }
75 u32bit sig_words() const;
76 u32bit bytes() const;
77 u32bit bits() const;
78
79 const word* data() const { return reg.begin(); }
80 SecureVector<word>& get_reg() { return reg; }
81 void grow_reg(u32bit n) const { reg.grow_by(n); }
82
83 word& operator[](u32bit index) { return reg[index]; }
84 word operator[](u32bit index) const { return reg[index]; }
85 void clear() { reg.clear(); }
86
87 void randomize(u32bit = 0, RNG_Quality = SessionKey);
88
89 void binary_encode(byte[]) const;
90 void binary_decode(const byte[], u32bit);
91 u32bit encoded_size(Base = Binary) const;
92
93 static SecureVector<byte> encode(const BigInt&, Base = Binary);
94 static void encode(byte[], const BigInt&, Base = Binary);
95 static BigInt decode(const byte[], u32bit, Base = Binary);
96 static BigInt decode(const MemoryRegion<byte>&, Base = Binary);
97 static SecureVector<byte> encode_1363(const BigInt&, u32bit);
98
99 void swap(BigInt&);
100
101 BigInt(u64bit = 0);
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 friend void modifying_divide(BigInt&, BigInt&, BigInt&);
109 void grow_to(u32bit n) const { reg.grow_to(n); }
110 Sign signedness;
111 SecureVector<word> reg;
112 };
113
114/*************************************************
115* Arithmetic Operators *
116*************************************************/
117BigInt operator+(const BigInt&, const BigInt&);
118BigInt operator-(const BigInt&, const BigInt&);
119BigInt operator*(const BigInt&, const BigInt&);
120BigInt operator/(const BigInt&, const BigInt&);
121BigInt operator%(const BigInt&, const BigInt&);
122word operator%(const BigInt&, word);
123BigInt operator<<(const BigInt&, u32bit);
124BigInt operator>>(const BigInt&, u32bit);
125
126/*************************************************
127* Comparison Operators *
128*************************************************/
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); }
139inline bool operator>(const BigInt& a, const BigInt& b)
140 { return (a.cmp(b) > 0); }
141
142/*************************************************
143* I/O Operators *
144*************************************************/
145std::ostream& operator<<(std::ostream&, const BigInt&);
146std::istream& operator>>(std::istream&, BigInt&);
147
148}
149
150namespace std {
151
152inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); }
153
154}
155
156#endif

Archive Download this file

Branches

Tags

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