From 82f0987d1188a03eebf56092c17d0bc8a1538595 Mon Sep 17 00:00:00 2001 From: malloc Date: Wed, 21 Mar 2018 23:22:41 -0500 Subject: [PATCH] moderate boob --- server/src/utils/bigint.cpp | 57 +++++++++++++++++++++++++++++++++++++ server/src/utils/bigint.hpp | 15 ++++++---- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/server/src/utils/bigint.cpp b/server/src/utils/bigint.cpp index 255822c..54cf412 100644 --- a/server/src/utils/bigint.cpp +++ b/server/src/utils/bigint.cpp @@ -50,8 +50,29 @@ void sosc::BigUInt::RandomPrime(int byte_count) { } 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 { // TODO rabin-miller + return false; } std::tuple sosc::BigUInt::DivideWithRemainder @@ -100,3 +121,39 @@ sosc::BigUInt sosc::BigUInt::ModPow 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; +} diff --git a/server/src/utils/bigint.hpp b/server/src/utils/bigint.hpp index 51334af..dd0a8c7 100644 --- a/server/src/utils/bigint.hpp +++ b/server/src/utils/bigint.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "csprng.hpp" namespace sosc { @@ -34,19 +35,19 @@ public: void SetBit(uint64_t bit, bool value); 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) const; + BigUInt operator - (const BigUInt& rhs) const; 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) const; + BigUInt operator / (const BigUInt& rhs) const; 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; @@ -62,6 +63,10 @@ public: return this->ToString(); } private: + inline void Copy(const BigUInt& from) { + + } + std::vector value; }; }