添加超大数的实现
parent
a314e0ca77
commit
453274c4d9
|
@ -12,10 +12,11 @@
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="0facce0d-c642-4d80-b2fb-daf5f3e68dff" name="Default Changelist" comment="">
|
<list default="true" id="0facce0d-c642-4d80-b2fb-daf5f3e68dff" name="Default Changelist" comment="">
|
||||||
<change afterPath="$PROJECT_DIR$/obj/inc/rsa.cpp" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/obj/inc/rsa.h" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/cmake-build-debug/General.cbp" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/General.cbp" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/obj/inc/rsa.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/obj/inc/rsa.cpp" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/obj/inc/rsa.h" beforeDir="false" afterPath="$PROJECT_DIR$/obj/inc/rsa.h" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
|
@ -84,7 +85,7 @@
|
||||||
<workItem from="1577933660908" duration="635000" />
|
<workItem from="1577933660908" duration="635000" />
|
||||||
<workItem from="1578153114620" duration="4070000" />
|
<workItem from="1578153114620" duration="4070000" />
|
||||||
<workItem from="1578225965728" duration="1215000" />
|
<workItem from="1578225965728" duration="1215000" />
|
||||||
<workItem from="1578280389789" duration="7997000" />
|
<workItem from="1578280389789" duration="12145000" />
|
||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
|
|
@ -6,7 +6,8 @@ INCLUDE_DIRECTORIES (inc)
|
||||||
aux_source_directory(src DIRSRCS)
|
aux_source_directory(src DIRSRCS)
|
||||||
aux_source_directory(src/pattern PaternSrc)
|
aux_source_directory(src/pattern PaternSrc)
|
||||||
|
|
||||||
add_library(General ${DIRSRCS} ${PaternSrc} src/pattern/signleton.h src/pattern/Observer.h src/pattern/stratergy.h src/pattern/adapter.h src/encrypt/base64.cpp src/encrypt/base64.h src/encrypt/aes.cpp src/encrypt/aes.h obj/inc/rsa.cpp obj/inc/rsa.h)
|
add_library(General ${DIRSRCS} ${PaternSrc} src/pattern/signleton.h src/pattern/Observer.h src/pattern/stratergy.h src/pattern/adapter.h src/encrypt/base64.cpp src/encrypt/base64.h src/encrypt/aes.cpp src/encrypt/aes.h obj/inc/rsa.cpp obj/inc/rsa.h
|
||||||
|
src/math/BigInt.hpp)
|
||||||
set(COPYITEM inc)
|
set(COPYITEM inc)
|
||||||
file(GLOB INCLUDES ${PROJECT_SOURCE_DIR}/inc/*)
|
file(GLOB INCLUDES ${PROJECT_SOURCE_DIR}/inc/*)
|
||||||
file(COPY ${INCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
|
file(COPY ${INCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
|
||||||
|
@ -19,4 +20,8 @@ file(COPY ${PatternINCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
|
||||||
|
|
||||||
file(GLOB EncryptINCLUDES ${PROJECT_SOURCE_DIR}/src/encrypt/*.h)
|
file(GLOB EncryptINCLUDES ${PROJECT_SOURCE_DIR}/src/encrypt/*.h)
|
||||||
file(COPY ${EncryptINCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
|
file(COPY ${EncryptINCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
|
||||||
|
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
||||||
|
|
||||||
|
file(GLOB MathINCLUDES ${PROJECT_SOURCE_DIR}/src/encrypt/*.hpp)
|
||||||
|
file(COPY ${MathINCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
|
||||||
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
|
|
@ -108,6 +108,9 @@
|
||||||
<Unit filename="D:/project/c++/generallib/src/loger.cpp">
|
<Unit filename="D:/project/c++/generallib/src/loger.cpp">
|
||||||
<Option target="General"/>
|
<Option target="General"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
|
<Unit filename="D:/project/c++/generallib/src/math/BigInt.hpp">
|
||||||
|
<Option target="General"/>
|
||||||
|
</Unit>
|
||||||
<Unit filename="D:/project/c++/generallib/src/pattern/Observer.h">
|
<Unit filename="D:/project/c++/generallib/src/pattern/Observer.h">
|
||||||
<Option target="General"/>
|
<Option target="General"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
|
|
|
@ -68,6 +68,18 @@ const static int PrimeTable[550]=
|
||||||
3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001
|
3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int Mod(uint8_t *dat,int size,int table){
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
目前的做法是基于费马素性检测
|
||||||
|
假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。
|
||||||
|
也就是说, 如果p为素数, 那么对于任何a<p, 有
|
||||||
|
a ^ p % p == a 成立
|
||||||
|
而它的逆命题则至少有1/2的概率成立
|
||||||
|
那么我们就可以通过多次素性检测, 来减少假素数出现的概率
|
||||||
|
而素数定理, 又指出了素数的密度与ln(x)成反比, 也就是说, 我们可以先随机生成一个n bit的整数, 如果不是素数, 则继续向后取, 那么, 大概取n个数, 就能碰到一个素数
|
||||||
|
*/
|
||||||
BigInt GetPrime(int bits)
|
BigInt GetPrime(int bits)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
@ -77,7 +89,7 @@ BigInt GetPrime(int bits)
|
||||||
begin:
|
begin:
|
||||||
for(i=0;i<m_nLength;i++)
|
for(i=0;i<m_nLength;i++)
|
||||||
m_ulValue[i] = rand()*0x10000 + rand();
|
m_ulValue[i] = rand()*0x10000 + rand();
|
||||||
m_ulValue[0] = m_ulValue[0]|1;
|
m_ulValue[0] = m_ulValue[0] | 1;
|
||||||
for(i = m_nLength - 1;i > 0;i--)
|
for(i = m_nLength - 1;i > 0;i--)
|
||||||
{
|
{
|
||||||
m_ulValue[i] = m_ulValue[i]<<1;
|
m_ulValue[i] = m_ulValue[i]<<1;
|
||||||
|
@ -88,8 +100,8 @@ BigInt GetPrime(int bits)
|
||||||
m_ulValue[0]++;
|
m_ulValue[0]++;
|
||||||
for(i = 0; i <550;i++)
|
for(i = 0; i <550;i++)
|
||||||
{
|
{
|
||||||
//if(Mod(PrimeTable[i]) == 0)
|
if(Mod(PrimeTable[i]) == 0)
|
||||||
// goto begin;
|
goto begin;
|
||||||
}
|
}
|
||||||
//CBigInt S,A,I,K;
|
//CBigInt S,A,I,K;
|
||||||
//K.Mov(*this);
|
//K.Mov(*this);
|
||||||
|
@ -181,15 +193,16 @@ bool rabinmiller(size_t n, size_t k)
|
||||||
}
|
}
|
||||||
|
|
||||||
BigInt::BigInt(int size) {
|
BigInt::BigInt(int size) {
|
||||||
|
memset(this->mDat,0,size);
|
||||||
}
|
}
|
||||||
|
|
||||||
BigInt::BigInt(int size, uint8_t *dat) {
|
BigInt::BigInt(int size, uint8_t *dat) {
|
||||||
|
memcpy(this->mDat,dat,size);
|
||||||
}
|
}
|
||||||
|
|
||||||
BigInt BigInt::operator=(const BigInt &) {
|
bool BigInt::operator=(BigInt &cmp) {
|
||||||
return BigInt(0);
|
return memcmp((const void *)cmp.Data(),
|
||||||
|
(const void *)this->Data(),this->mSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
BigInt BigInt::operator>(const BigInt &) {
|
BigInt BigInt::operator>(const BigInt &) {
|
||||||
|
@ -208,6 +221,6 @@ string BigInt::ToString() {
|
||||||
return std::__cxx11::string();
|
return std::__cxx11::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *BigInt::Data() {
|
const uint8_t *BigInt::Data() {
|
||||||
return mDat;
|
return mDat;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,12 +13,12 @@ class BigInt{
|
||||||
public:
|
public:
|
||||||
BigInt(int size);
|
BigInt(int size);
|
||||||
BigInt(int size,uint8_t *dat);
|
BigInt(int size,uint8_t *dat);
|
||||||
BigInt operator=(const BigInt&);
|
bool operator=(BigInt&);
|
||||||
BigInt operator>(const BigInt&);
|
BigInt operator>(const BigInt&);
|
||||||
BigInt operator<(const BigInt&);
|
BigInt operator<(const BigInt&);
|
||||||
BigInt operator^(const BigInt&);
|
BigInt operator^(const BigInt&);
|
||||||
string ToString();
|
string ToString();
|
||||||
uint8_t *Data();
|
const uint8_t *Data();
|
||||||
private:
|
private:
|
||||||
uint8_t mDat[2048];
|
uint8_t mDat[2048];
|
||||||
int mSize;
|
int mSize;
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue