moderate boob

This commit is contained in:
malloc 2018-03-21 23:22:41 -05:00
parent 336c40d809
commit 82f0987d11
2 changed files with 67 additions and 5 deletions

View file

@ -50,8 +50,29 @@ void sosc::BigUInt::RandomPrime(int byte_count) {
} while(!this->IsProbablePrime()); } while(!this->IsProbablePrime());
} }
bool sosc::BigUInt::IsZero() const {
return
std::all_of(this->value.begin(), this->value.end(),
[](uint8_t x) { return x == 0; });
}
bool sosc::BigUInt::IsOne() const {
if(this->value[0] != 1)
return false;
return this->value.size() == 1
? true
: std::all_of(this->value.begin() + 1, this->value.end(),
[](uint8_t x) { return x == 0; });
}
bool sosc::BigUInt::IsEven() const {
return !this->GetBit(0);
}
bool sosc::BigUInt::IsProbablePrime() const { bool sosc::BigUInt::IsProbablePrime() const {
// TODO rabin-miller // TODO rabin-miller
return false;
} }
std::tuple<sosc::BigUInt, sosc::BigUInt> sosc::BigUInt::DivideWithRemainder std::tuple<sosc::BigUInt, sosc::BigUInt> sosc::BigUInt::DivideWithRemainder
@ -100,3 +121,39 @@ sosc::BigUInt sosc::BigUInt::ModPow
return accum; return accum;
} }
void sosc::BigUInt::SetBit(uint64_t bit, bool value) {
uint64_t byte = bit / 8;
if(byte >= this->ByteCount())
this->value.resize(byte + 1);
if(value)
this->value[byte] |= (1 << (bit % 8));
else
this->value[byte] &= ~(1 << (bit % 8));
}
bool sosc::BigUInt::GetBit(uint64_t bit) const {
uint64_t byte = bit / 8;
if(byte >= this->ByteCount())
return false;
return (this->value[byte] & (1 << (bit % 8))) != 0;
}
sosc::BigUInt sosc::BigUInt::operator + (const BigUInt& rhs) const {
uint64_t sum_range = std::min(this->ByteCount(), rhs.ByteCount());
BigUInt sum;
uint8_t carry = 0;
for(uint64_t i = 0; i < sum_range; ++i) {
}
return sum;
}
sosc::BigUInt& sosc::BigUInt::operator += (const BigUInt& rhs) {
this->Copy(*this + rhs);
return *this;
}

View file

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
#include <algorithm>
#include "csprng.hpp" #include "csprng.hpp"
namespace sosc { namespace sosc {
@ -34,19 +35,19 @@ public:
void SetBit(uint64_t bit, bool value); void SetBit(uint64_t bit, bool value);
bool GetBit(uint64_t bit) const; bool GetBit(uint64_t bit) const;
BigUInt& operator + (const BigUInt& rhs) const; BigUInt operator + (const BigUInt& rhs) const;
BigUInt& operator += (const BigUInt& rhs); BigUInt& operator += (const BigUInt& rhs);
BigUInt& operator - (const BigUInt& rhs) const; BigUInt operator - (const BigUInt& rhs) const;
BigUInt& operator -= (const BigUInt& rhs); BigUInt& operator -= (const BigUInt& rhs);
BigUInt& operator * (const BigUInt& rhs) const; BigUInt operator * (const BigUInt& rhs) const;
BigUInt& operator *= (const BigUInt& rhs); BigUInt& operator *= (const BigUInt& rhs);
BigUInt& operator / (const BigUInt& rhs) const; BigUInt operator / (const BigUInt& rhs) const;
BigUInt& operator /= (const BigUInt& rhs); BigUInt& operator /= (const BigUInt& rhs);
BigUInt& operator % (const BigUInt& rhs) const; BigUInt operator % (const BigUInt& rhs) const;
bool operator == (const BigUInt& rhs) const; bool operator == (const BigUInt& rhs) const;
bool operator > (const BigUInt& rhs) const; bool operator > (const BigUInt& rhs) const;
@ -62,6 +63,10 @@ public:
return this->ToString(); return this->ToString();
} }
private: private:
inline void Copy(const BigUInt& from) {
}
std::vector<uint8_t> value; std::vector<uint8_t> value;
}; };
} }