📄 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 + -