aboutsummaryrefslogtreecommitdiffstats
path: root/include/bigint.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/bigint.h')
-rw-r--r--include/bigint.h91
1 files changed, 24 insertions, 67 deletions
diff --git a/include/bigint.h b/include/bigint.h
index 2231ba99c..b1286551e 100644
--- a/include/bigint.h
+++ b/include/bigint.h
@@ -50,10 +50,14 @@ class BOTAN_DLL BigInt
bool is_zero() const
{
- for(u32bit i = 0; i != rep.sig_words(); ++i)
- if(rep[i]) return false;
+ const u32bit sw = sig_words();
+
+ for(u32bit i = 0; i != sw; ++i)
+ if(reg[i])
+ return false;
return true;
}
+
bool is_nonzero() const { return (!is_zero()); }
void set_bit(u32bit);
@@ -79,18 +83,29 @@ class BOTAN_DLL BigInt
BigInt abs() const;
u32bit size() const { return get_reg().size(); }
- u32bit sig_words() const { return rep.sig_words(); }
+
+ u32bit sig_words() const
+ {
+ const word* x = reg.begin();
+ u32bit sig = reg.size();
+
+ while(sig && (x[sig-1] == 0))
+ sig--;
+ return sig;
+ }
+
u32bit bytes() const;
u32bit bits() const;
- const word* data() const { return get_reg().begin(); }
- SecureVector<word>& get_reg() { return rep.get_reg(); }
- const SecureVector<word>& get_reg() const { return rep.get_reg(); }
+ const word* data() const { return reg.begin(); }
+ SecureVector<word>& get_reg() { return reg; }
+ const SecureVector<word>& get_reg() const { return reg; }
+
void grow_reg(u32bit);
void grow_to(u32bit);
- word& operator[](u32bit i) { return rep[i]; }
- word operator[](u32bit i) const { return rep[i]; }
+ word& operator[](u32bit i) { return reg[i]; }
+ word operator[](u32bit i) const { return reg[i]; }
void clear() { get_reg().clear(); }
void randomize(RandomNumberGenerator& rng, u32bit n);
@@ -117,65 +132,7 @@ class BOTAN_DLL BigInt
BigInt(Sign, u32bit);
BigInt(NumberType, u32bit);
private:
- class Rep
- {
- private:
- static const u32bit INVALID_SIG_WORD = 0xFFFFFFFF;
- mutable u32bit sig;
- SecureVector<word> reg;
-
- public:
- Rep() { sig = INVALID_SIG_WORD; }
-
- void swap(Rep& other)
- {
- std::swap(reg, other.reg);
- std::swap(sig, other.sig);
- }
-
- SecureVector<word>& get_reg()
- { sig = INVALID_SIG_WORD; return reg; }
-
- word& operator[](u32bit n)
- {
- sig = INVALID_SIG_WORD;
-
- if(n > reg.size())
- reg.grow_to(n+1);
- return reg[n];
- }
-
- word operator[](u32bit n) const
- {
- if(n > reg.size())
- return 0;
- return reg[n];
- }
-
- const SecureVector<word>& get_reg() const { return reg; }
-
- /*************************************************
- * Count the significant words, if cached value is
- * not valid
- *************************************************/
- u32bit sig_words() const
- {
- if(sig == INVALID_SIG_WORD)
- {
- const word* x = reg.begin();
- u32bit top_set = reg.size();
-
- while(top_set && (x[top_set-1] == 0))
- top_set--;
-
- sig = top_set;
- }
-
- return sig;
- }
- };
-
- Rep rep;
+ SecureVector<word> reg;
Sign signedness;
};