?? bigint.hpp
字號:
/*****************************************************************
大數運算庫頭文件:BigInt.h
作者:afanty@vip.sina.com
版本:1.2 (2003.5.13)
說明:適用于MFC,1024位RSA運算
*****************************************************************/
/********************************************************************
created: 2004/08/25
created: 25:8:2004 17:31
filename: d:\sun\srcwork\floattool\bigint.hpp
file path: d:\sun\srcwork\floattool
file base: bigint
file ext: hpp
author: afanty@vip.sina.com
modifier: 孫寶建(sunbaojian)
version: 1.0.0.1
purpose: 增加功能,改為標準C++版
*********************************************************************/
#ifndef _INCLUCDE_BIGINT_HPP_INCLUDE
#define _INCLUCDE_BIGINT_HPP_INCLUDE
#ifndef _INCLUDE_SLIB_DEF_HPP_INCLUDE
#include "slib_def.hpp"
#endif
#ifndef _INCLUDE_SLIB_TCHAR_HPP_INCLUDE
#include "slib_tchar.hpp"
#endif
#ifndef _INCLUDE_SLIB_ERROR_HPP_INCLUDE
#include "slib_error.hpp"
#endif
using namespace SLib;
//允許生成1120位(二進制)的中間結果
#define BI_MAXLEN 35
//#define DEC 10
//#define HEX 16
class CBigInt
{
public:
enum { BIN =2, DEC = 10, HEX = 16, FLOAT, DOUBLE, LONG_DOUBLE};
//大數在0x100000000進制下的長度
unsigned m_nLength;
//用數組記錄大數在0x100000000進制下每一位的值
unsigned long m_ulValue[BI_MAXLEN];
// CBigInt(INT64 A);
CBigInt(UINT64 A);
CBigInt(const CBigInt & A);
CBigInt();
~CBigInt();
/*****************************************************************
基本操作與運算
Mov,賦值運算,可賦值為大數或普通整數,可重載為運算符“=”
Cmp,比較運算,可重載為運算符“==”、“!=”、“>=”、“<=”等
Add,加,求大數與大數或大數與普通整數的和,可重載為運算符“+”
Sub,減,求大數與大數或大數與普通整數的差,可重載為運算符“-”
Mul,乘,求大數與大數或大數與普通整數的積,可重載為運算符“*”
Div,除,求大數與大數或大數與普通整數的商,可重載為運算符“/”
Mod,模,求大數與大數或大數與普通整數的模,可重載為運算符“%”
*****************************************************************/
void Mov(UINT64 A);
void Mov(const CBigInt& A);
CBigInt Add(CBigInt& A);
CBigInt Sub(CBigInt& A);
CBigInt Mul(CBigInt& A);
CBigInt Div(CBigInt& A);
CBigInt Mod(CBigInt& A);
CBigInt Add(unsigned long A);
CBigInt Sub(unsigned long A);
CBigInt Mul(unsigned long A);
CBigInt Div(unsigned long A);
unsigned long Mod(unsigned long A);
int Cmp(CBigInt& A);
CBigInt& operator =(UINT64 A){Mov(A); return *this;};
CBigInt& operator =(const CBigInt& A){Mov(A); return *this;};
CBigInt operator +(CBigInt& A){return Add(A);};
CBigInt operator -(CBigInt& A){return Sub(A);};
CBigInt operator *(CBigInt& A){return Mul(A);};
CBigInt operator /(CBigInt& A){return Div(A);};
CBigInt operator %(CBigInt& A){return Mod(A);};
CBigInt operator +(unsigned long A){return Add(A);};
CBigInt operator -(unsigned long A){return Sub(A);};
CBigInt operator *(unsigned long A){return Mul(A);};
CBigInt operator /(unsigned long A){return Div(A);};
unsigned long operator %(unsigned long A){return Mod(A);};
/****************************************************************************************
大數比較
調用方式:N.Cmp(A)
返回值:若N<A返回-1;若N=A返回0;若N>A返回1
****************************************************************************************/
BOOL operator ==(CBigInt& A){return Cmp(A) == 0 ? 1 : 0;};
BOOL operator !=(CBigInt& A){return Cmp(A) == 0 ? 0 : 1;};
BOOL operator >=(CBigInt& A)
{
int ret =Cmp(A);
return (ret == 0 || ret == 1) ? 1 : 0;
};
BOOL operator <=(CBigInt& A)
{
int ret =Cmp(A);
return (ret == 0 || ret == -1) ? 1 : 0;
};
BOOL operator >(CBigInt& A){return Cmp(A) == 1 ? 1 : 0;};
BOOL operator <(CBigInt& A){return Cmp(A) == -1 ? 1 : 0;};
/*****************************************************************
輸入輸出
Get,從字符串按10進制或16進制格式輸入到大數
Put,將大數按10進制或16進制格式輸出到字符串
*****************************************************************/
void Get(tstring& str, unsigned int system = CBigInt::HEX);
void Put(tstring& str, unsigned int system = CBigInt::HEX);
tstring ItoA(unsigned int system, int size);
DWORD AtoI(const tstring& str, unsigned int system, int size);
/*****************************************************************
RSA相關運算
Rab,拉賓米勒算法進行素數測試
Euc,歐幾里德算法求解同余方程
RsaTrans,反復平方算法進行冪模運算
GetPrime,產生指定長度的隨機大素數
*****************************************************************/
// int Rab();
CBigInt Euc(CBigInt& A);
CBigInt RsaTrans(CBigInt& A, CBigInt& B);
// void GetPrime(int bits);
int GetSize(void);
};
#endif //_INCLUCDE_BIGINT_HPP_INCLUDE
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -