Sorted everything into namespaces

Added headers for some common mathematical fields
  - Started adding class structures
  - Added declarations for useful functions
Added operator-assignment operator overloads to BigInteger
This commit is contained in:
Gabriel Tofvesson 2018-02-26 02:28:58 +01:00
parent 1448af586d
commit 179cbc234b
9 changed files with 719 additions and 573 deletions

View File

@ -1,5 +1,17 @@
#pragma once #pragma once
struct PublicKey { namespace CryptoCPP {
namespace RSA {
struct PublicKey
{
// Big integer modulus
// Big integer exponent
}; };
class RSA
{
public:
//RSA(PublicKey* key, Math::BigInteger* privateKey);
};
}
}

View File

@ -69,7 +69,7 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>

View File

@ -2,6 +2,8 @@
#include "BigInteger.h" #include "BigInteger.h"
namespace CryptoCPP {
namespace Math {
BIGINT_API BigInteger::BigInteger(int64_t initialValue) BIGINT_API BigInteger::BigInteger(int64_t initialValue)
{ {
data = new std::vector<BYTE>(); data = new std::vector<BYTE>();
@ -105,46 +107,70 @@ BIGINT_API BigInteger * BigInteger::operator>>(uint64_t shiftcount) const
return create; return create;
} }
BIGINT_API BigInteger * BigInteger::operator+(const BigInteger * val) const
BIGINT_API BigInteger* BigInteger::operator+=(const BigInteger& val)
{ {
return operator+(*val); iadd(val, false);
return this;
} }
BIGINT_API BigInteger * BigInteger::operator-(const BigInteger * val) const BIGINT_API BigInteger* BigInteger::operator-=(const BigInteger& val)
{ {
return operator-(*val); isub(val, false);
return this;
} }
BIGINT_API BigInteger * BigInteger::operator*(const BigInteger * val) const BIGINT_API BigInteger* BigInteger::operator*=(const BigInteger& val)
{ {
return operator*(*val); imul(val, false);
return this;
} }
BIGINT_API BigInteger * BigInteger::operator/(const BigInteger * val) const BIGINT_API BigInteger* BigInteger::operator/=(const BigInteger& val)
{ {
return operator/(*val); idiv(val, false);
return this;
} }
BIGINT_API BigInteger * BigInteger::operator%(const BigInteger * val) const BIGINT_API BigInteger* BigInteger::operator%=(const BigInteger& val)
{ {
return operator%(*val); imod(val, false);
return this;
} }
BIGINT_API BigInteger * BigInteger::operator^(const BigInteger * val) const BIGINT_API BigInteger* BigInteger::operator^=(const BigInteger& val)
{ {
return operator^(*val); ixor(val, false);
return this;
} }
BIGINT_API BigInteger * BigInteger::operator&(const BigInteger * val) const BIGINT_API BigInteger* BigInteger::operator&=(const BigInteger& val)
{ {
return operator&(*val); iand(val, false);
return this;
} }
BIGINT_API BigInteger * BigInteger::operator|(const BigInteger * val) const BIGINT_API BigInteger* BigInteger::operator|=(const BigInteger& val)
{ {
return operator|(*val); ior(val, false);
return this;
} }
BIGINT_API BigInteger* BigInteger::operator<<=(uint64_t shiftcount)
{
ishl(shiftcount);
return this;
}
BIGINT_API BigInteger* BigInteger::operator>>=(uint64_t shiftcount)
{
ishr(shiftcount);
return this;
}
BIGINT_API bool BigInteger::operator<(const BigInteger& val) const BIGINT_API bool BigInteger::operator<(const BigInteger& val) const
{ {
return cmp(val, false) == 1; return cmp(val, false) == 1;
@ -192,7 +218,7 @@ BIGINT_API char* BigInteger::toString()
} }
void BigInteger::iadd(const BigInteger & other, bool swaptarget) BIGINT_API void BigInteger::iadd(const BigInteger & other, bool swaptarget)
{ {
if ((other.sign != sign) ^ swaptarget) if ((other.sign != sign) ^ swaptarget)
{ {
@ -248,7 +274,7 @@ void BigInteger::iadd(const BigInteger & other, bool swaptarget)
if (!data->size()) data->push_back(0); if (!data->size()) data->push_back(0);
} }
void BigInteger::isub(const BigInteger & other, bool swaptarget) BIGINT_API void BigInteger::isub(const BigInteger & other, bool swaptarget)
{ {
if (other.sign ^ sign ^ swaptarget) // this - (-other) = this + other if (other.sign ^ sign ^ swaptarget) // this - (-other) = this + other
{ {
@ -288,7 +314,7 @@ void BigInteger::isub(const BigInteger & other, bool swaptarget)
if (!data->size()) data->push_back(0); if (!data->size()) data->push_back(0);
} }
void BigInteger::imul(const BigInteger & val, bool swaptarget) BIGINT_API void BigInteger::imul(const BigInteger & val, bool swaptarget)
{ {
bool hb, passed = false; bool hb, passed = false;
size_t track = -1; size_t track = -1;
@ -310,7 +336,7 @@ Loop:
goto Loop; goto Loop;
} }
BigInteger* BigInteger::idiv(const BigInteger & val, bool swaptarget) BIGINT_API BigInteger* BigInteger::idiv(const BigInteger & val, bool swaptarget)
{ {
if (val.is_zero()) throw new std::exception("Divide by zero!"); if (val.is_zero()) throw new std::exception("Divide by zero!");
BigInteger* rem = new BigInteger(0); BigInteger* rem = new BigInteger(0);
@ -335,14 +361,14 @@ BigInteger* BigInteger::idiv(const BigInteger & val, bool swaptarget)
return rem; return rem;
} }
void BigInteger::imod(const BigInteger & val, bool swaptarget) BIGINT_API void BigInteger::imod(const BigInteger & val, bool swaptarget)
{ {
BigInteger* modres = idiv(val, swaptarget); BigInteger* modres = idiv(val, swaptarget);
cpy(*modres, true); cpy(*modres, true);
delete modres; delete modres;
} }
void BigInteger::ixor(const BigInteger & val, bool swaptarget) BIGINT_API void BigInteger::ixor(const BigInteger & val, bool swaptarget)
{ {
size_t s1 = val.data->size(), s2 = data->size(); size_t s1 = val.data->size(), s2 = data->size();
for (size_t t = 0; t < s1; ++t) for (size_t t = 0; t < s1; ++t)
@ -352,7 +378,7 @@ void BigInteger::ixor(const BigInteger & val, bool swaptarget)
data->push_back(val.data->at(t)); data->push_back(val.data->at(t));
} }
void BigInteger::iand(const BigInteger & val, bool swaptarget) BIGINT_API void BigInteger::iand(const BigInteger & val, bool swaptarget)
{ {
size_t s1 = data->size(), s2 = val.data->size(); size_t s1 = data->size(), s2 = val.data->size();
for (size_t t = 0; t < s1; ++t) for (size_t t = 0; t < s1; ++t)
@ -362,7 +388,7 @@ void BigInteger::iand(const BigInteger & val, bool swaptarget)
break; break;
} }
void BigInteger::ior(const BigInteger & val, bool swaptarget) BIGINT_API void BigInteger::ior(const BigInteger & val, bool swaptarget)
{ {
size_t s1 = val.data->size(), s2 = data->size(); size_t s1 = val.data->size(), s2 = data->size();
for (size_t t = 0; t < s1; ++t) for (size_t t = 0; t < s1; ++t)
@ -372,12 +398,12 @@ void BigInteger::ior(const BigInteger & val, bool swaptarget)
data->push_back(val.data->at(t)); data->push_back(val.data->at(t));
} }
void BigInteger::inot() BIGINT_API void BigInteger::inot()
{ {
for (size_t t = 0; t < data->size(); ++t) (*data)[t] = ~(*data)[t]; for (size_t t = 0; t < data->size(); ++t) (*data)[t] = ~(*data)[t];
} }
void BigInteger::ishl(uint64_t shift) BIGINT_API void BigInteger::ishl(uint64_t shift)
{ {
size_t set = shift / 8; size_t set = shift / 8;
char sub = shift % 8; char sub = shift % 8;
@ -404,7 +430,7 @@ void BigInteger::ishl(uint64_t shift)
clip_zeroes(); clip_zeroes();
} }
void BigInteger::ishr(uint64_t shift) BIGINT_API void BigInteger::ishr(uint64_t shift)
{ {
size_t offset = shift / 8; size_t offset = shift / 8;
char sub = shift % 8; char sub = shift % 8;
@ -429,27 +455,27 @@ void BigInteger::ishr(uint64_t shift)
if (!data->size()) data->push_back(0); if (!data->size()) data->push_back(0);
} }
void BigInteger::twos_complement() BIGINT_API void BigInteger::twos_complement()
{ {
for (size_t i = 0; i < data->size(); ++i) (*data)[i] = ~(*data)[i]; for (size_t i = 0; i < data->size(); ++i) (*data)[i] = ~(*data)[i];
iadd(BigInteger(1), false); iadd(BigInteger(1), false);
} }
void BigInteger::set_bit(size_t index, bool value) BIGINT_API void BigInteger::set_bit(size_t index, bool value)
{ {
BYTE fill = sign ? 0xFF : 0x00; BYTE fill = sign ? 0xFF : 0x00;
while (index >= data->size() * 8) data->push_back(fill); while (index >= data->size() * 8) data->push_back(fill);
(*data)[index / 8] = ((*data)[index / 8] & ~(1 << (index % 8))) | ((value ? 1 : 0) << (index % 8)); (*data)[index / 8] = ((*data)[index / 8] & ~(1 << (index % 8))) | ((value ? 1 : 0) << (index % 8));
} }
void BigInteger::cpy(const BigInteger& val, bool withsign) BIGINT_API void BigInteger::cpy(const BigInteger& val, bool withsign)
{ {
if (withsign) sign = val.sign; if (withsign) sign = val.sign;
data->clear(); data->clear();
for (BYTE b : *val.data) data->push_back(b); for (BYTE b : *val.data) data->push_back(b);
} }
char BigInteger::cmp(const BigInteger & other, bool grt) const BIGINT_API char BigInteger::cmp(const BigInteger & other, bool grt) const
{ {
// If the other number is less than zero and this is a positive number, this number is larger and vice versa // If the other number is less than zero and this is a positive number, this number is larger and vice versa
if (other.sign && !sign && other.data->size() != 0) return grt ? 1 : 0; if (other.sign && !sign && other.data->size() != 0) return grt ? 1 : 0;
@ -465,19 +491,19 @@ char BigInteger::cmp(const BigInteger & other, bool grt) const
return ((l1 > l2 && (!sign == grt)) || ((sign == grt) && l1 < l2)) ? 1 : 0; return ((l1 > l2 && (!sign == grt)) || ((sign == grt) && l1 < l2)) ? 1 : 0;
} }
char BigInteger::shift_mask(int64_t shift, bool left) BIGINT_API char BigInteger::shift_mask(int64_t shift, bool left)
{ {
BYTE res = 0; BYTE res = 0;
for (uint64_t i = shift; i > 0; --i) res = left ? (res >> 1) | 128 : (res << 1) | 1; for (uint64_t i = shift; i > 0; --i) res = left ? (res >> 1) | 128 : (res << 1) | 1;
return res; return res;
} }
void BigInteger::clip_zeroes() BIGINT_API void BigInteger::clip_zeroes()
{ {
while (data->size() > 1 && !data->at(data->size() - 1)) data->pop_back(); while (data->size() > 1 && !data->at(data->size() - 1)) data->pop_back();
} }
size_t BigInteger::highest_set_bit(bool * hasbits) const BIGINT_API size_t BigInteger::highest_set_bit(bool * hasbits) const
{ {
*hasbits = true; *hasbits = true;
for (size_t t = data->size(); t > 0; --t) for (size_t t = data->size(); t > 0; --t)
@ -489,7 +515,7 @@ size_t BigInteger::highest_set_bit(bool * hasbits) const
return 0; return 0;
} }
size_t BigInteger::lowest_set_bit(bool * hasbits) const BIGINT_API size_t BigInteger::lowest_set_bit(bool * hasbits) const
{ {
*hasbits = true; *hasbits = true;
for (size_t t = 0; t < data->size(); ++t) for (size_t t = 0; t < data->size(); ++t)
@ -501,7 +527,7 @@ size_t BigInteger::lowest_set_bit(bool * hasbits) const
return 0; return 0;
} }
size_t BigInteger::nth_set_bit(size_t index, bool minfirst, bool * hasbits) const BIGINT_API size_t BigInteger::nth_set_bit(size_t index, bool minfirst, bool * hasbits) const
{ {
*hasbits = true; *hasbits = true;
size_t target = index + 1; size_t target = index + 1;
@ -512,10 +538,13 @@ size_t BigInteger::nth_set_bit(size_t index, bool minfirst, bool * hasbits) cons
return 0; return 0;
} }
bool BigInteger::is_zero() const BIGINT_API bool BigInteger::is_zero() const
{ {
for (size_t t = 0; t < data->size(); ++t) for (size_t t = 0; t < data->size(); ++t)
if (data->at(t)) if (data->at(t))
return false; return false;
return true; return true;
} }
}
}

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <queue>
#ifdef BIGINT_API #ifdef BIGINT_API
#define BIGINT_API __declspec(dllexport) #define BIGINT_API __declspec(dllexport)
@ -12,7 +11,10 @@
#define BYTE unsigned char #define BYTE unsigned char
class BigInteger { namespace CryptoCPP {
namespace Math {
class BigInteger
{
public: public:
BIGINT_API BigInteger(int64_t initialValue); BIGINT_API BigInteger(int64_t initialValue);
BIGINT_API BigInteger(const BigInteger& initialvalue); BIGINT_API BigInteger(const BigInteger& initialvalue);
@ -30,16 +32,16 @@ public:
BIGINT_API BigInteger* operator<<(uint64_t shiftcount) const; BIGINT_API BigInteger* operator<<(uint64_t shiftcount) const;
BIGINT_API BigInteger* operator>>(uint64_t shiftcount) const; BIGINT_API BigInteger* operator>>(uint64_t shiftcount) const;
BIGINT_API BigInteger* operator+=(const BigInteger& val);
BIGINT_API BigInteger* operator+(const BigInteger* val) const; BIGINT_API BigInteger* operator-=(const BigInteger& val);
BIGINT_API BigInteger* operator-(const BigInteger* val) const; BIGINT_API BigInteger* operator*=(const BigInteger& val);
BIGINT_API BigInteger* operator*(const BigInteger* val) const; BIGINT_API BigInteger* operator/=(const BigInteger& val);
BIGINT_API BigInteger* operator/(const BigInteger* val) const; BIGINT_API BigInteger* operator%=(const BigInteger& val);
BIGINT_API BigInteger* operator%(const BigInteger* val) const; BIGINT_API BigInteger* operator^=(const BigInteger& val);
BIGINT_API BigInteger* operator^(const BigInteger* val) const; BIGINT_API BigInteger* operator&=(const BigInteger& val);
BIGINT_API BigInteger* operator&(const BigInteger* val) const; BIGINT_API BigInteger* operator|=(const BigInteger& val);
BIGINT_API BigInteger* operator|(const BigInteger* val) const; BIGINT_API BigInteger* operator<<=(uint64_t shiftcount);
BIGINT_API BigInteger* operator>>=(uint64_t shiftcount);
BIGINT_API bool operator<(const BigInteger& val) const; BIGINT_API bool operator<(const BigInteger& val) const;
BIGINT_API bool operator>(const BigInteger& val) const; BIGINT_API bool operator>(const BigInteger& val) const;
@ -55,30 +57,32 @@ protected:
bool sign; bool sign;
// Internal functions: manipulate the object they are called on // Internal functions: manipulate the object they are called on
void iadd(const BigInteger& val, bool swaptarget); BIGINT_API void iadd(const BigInteger& val, bool swaptarget);
void isub(const BigInteger& val, bool swaptarget); BIGINT_API void isub(const BigInteger& val, bool swaptarget);
void imul(const BigInteger& val, bool swaptarget); BIGINT_API void imul(const BigInteger& val, bool swaptarget);
BigInteger* idiv(const BigInteger& val, bool swaptarget); BIGINT_API BigInteger* idiv(const BigInteger& val, bool swaptarget);
void imod(const BigInteger& val, bool swaptarget); BIGINT_API void imod(const BigInteger& val, bool swaptarget);
void ixor(const BigInteger& val, bool swaptarget); BIGINT_API void ixor(const BigInteger& val, bool swaptarget);
void iand(const BigInteger& val, bool swaptarget); BIGINT_API void iand(const BigInteger& val, bool swaptarget);
void ior(const BigInteger& val, bool swaptarget); BIGINT_API void ior(const BigInteger& val, bool swaptarget);
void inot(); BIGINT_API void inot();
void ishl(uint64_t shift); BIGINT_API void ishl(uint64_t shift);
void ishr(uint64_t shift); BIGINT_API void ishr(uint64_t shift);
void twos_complement(); BIGINT_API void twos_complement();
void set_bit(size_t index, bool value); BIGINT_API void set_bit(size_t index, bool value);
void cpy(const BigInteger& val, bool withsign); BIGINT_API void cpy(const BigInteger& val, bool withsign);
char cmp(const BigInteger& other, bool grt) const; BIGINT_API char cmp(const BigInteger& other, bool grt) const;
// Math helper functions // Math helper functions
char shift_mask(int64_t shift, bool left); BIGINT_API char shift_mask(int64_t shift, bool left);
// For sorting and whatnot // For sorting and whatnot
void clip_zeroes(); BIGINT_API void clip_zeroes();
size_t highest_set_bit(bool* hasbits) const; BIGINT_API size_t highest_set_bit(bool* hasbits) const;
size_t lowest_set_bit(bool* hasbits) const; BIGINT_API size_t lowest_set_bit(bool* hasbits) const;
size_t nth_set_bit(size_t index, bool minfirst, bool* hasbits) const; BIGINT_API size_t nth_set_bit(size_t index, bool minfirst, bool* hasbits) const;
bool is_zero() const; BIGINT_API bool is_zero() const;
}; };
}
}

21
XMath/Galois.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
namespace CryptoCPP {
namespace Math {
class Galois
{
public:
Galois(size_t characteristic, size_t exponent, size_t irreducible);
Galois * add(const Galois * value) const; // Add
Galois * sub(const Galois * value) const; // Subtract
Galois * mul(const Galois * value) const; // Multiply
Galois * inv(const Galois * value) const; // Inverse multiply
protected:
size_t characteristic, exponent, irreducible;
// Reduce the value of this galois to one that fits the field parameters
void reduce();
};
}
}

56
XMath/Matrix.h Normal file
View File

@ -0,0 +1,56 @@
#pragma once
#include <vector>
#ifdef MATRIX_API
#define MATRIX_API __declspec(dllexport)
#else
#define MATRIX_API __declspec(dllimport)
#endif
namespace CryptoCPP {
namespace Math {
class Matrix
{
public:
MATRIX_API typedef void(*ChainFunction) (Vector & valueSet);
MATRIX_API Matrix(size_t height, size_t width);
MATRIX_API Matrix(Matrix & copy);
MATRIX_API ChainFunction set_row(const Vector & row);
MATRIX_API ChainFunction set_col(const Vector & col);
MATRIX_API Vector* at_row(size_t index) const;
MATRIX_API Vector* at_col(size_t index) const;
MATRIX_API long long at(size_t row, size_t col) const;
MATRIX_API Matrix* mul(const Matrix & factor) const;
MATRIX_API Matrix* mul(long long scalar) const;
MATRIX_API Matrix* minor(size_t row, size_t col) const;
MATRIX_API long long det() const;
protected:
Vector * columns;
size_t height;
size_t width;
};
class Vector
{
public:
MATRIX_API Vector(size_t count);
MATRIX_API Vector(size_t count, long long * values);
MATRIX_API long long at(size_t index) const;
MATRIX_API long long at(size_t index, long long newval);
protected:
long double * valueSet;
size_t count;
};
}
}

12
XMath/Primes.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "BigInteger.h"
namespace CryptoCPP {
namespace Primes {
bool fermat_prime_test(const BigInteger & value, size_t certainty);
bool miller_rabin_prime_test(const BigInteger & value, size_t certainty);
BigInteger * generate_prime(size_t byteCount, size_t certainty);
}
}

View File

@ -116,6 +116,9 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="BigInteger.h" /> <ClInclude Include="BigInteger.h" />
<ClInclude Include="Galois.h" />
<ClInclude Include="Matrix.h" />
<ClInclude Include="Primes.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="BigInteger.cpp" /> <ClCompile Include="BigInteger.cpp" />

View File

@ -23,5 +23,14 @@
<ClInclude Include="BigInteger.h"> <ClInclude Include="BigInteger.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Matrix.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Galois.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Primes.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>