⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmbigintegersclass.h

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 H
📖 第 1 页 / 共 2 页
字号:
//
// Returns true if w is a probable prime  t tests using FIPS-186-2/Rabin-Miller
//
static int mpIsPrime( VM_DIGIT_T w[], unsigned int ndigits, unsigned int t );


///////////////////////////////////////////////////////////////////////////////
//
// mpShort = mp x single digit calculations

///////////////////////////////////////////////////////////////////////////////
//
// Computes w = u + d, returns carry
//
static VM_DIGIT_T mpShortAdd( VM_DIGIT_T w[], VM_DIGIT_T u[], VM_DIGIT_T d, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Computes w = u - v, returns borrow
//
static VM_DIGIT_T mpShortSub( VM_DIGIT_T w[], VM_DIGIT_T u[], VM_DIGIT_T v, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Computes product p = x * y
//
static VM_DIGIT_T mpShortMult( VM_DIGIT_T p[], VM_DIGIT_T x[], VM_DIGIT_T y, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Computes q = u / v, returns remainder
//
static VM_DIGIT_T mpShortDiv( VM_DIGIT_T q[], VM_DIGIT_T u[], VM_DIGIT_T v, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Computes r = a mod d
//
static VM_DIGIT_T mpShortMod( VM_DIGIT_T a[], VM_DIGIT_T d, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Returns sign of (a - b) where b is a single digit
//
static int mpShortCmp( VM_DIGIT_T a[], VM_DIGIT_T b, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Short division using only half-digit divisor

///////////////////////////////////////////////////////////////////////////////
//
// Computes q = a mod d, returns remainder
//
static VM_DIGIT_T mpHalfDiv( VM_DIGIT_T q[], VM_DIGIT_T a[], VM_HALF_DIGIT_T d, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Computes r = a mod d
//
static VM_DIGIT_T mpHalfMod( VM_DIGIT_T a[], VM_HALF_DIGIT_T d, unsigned int ndigits );


///////////////////////////////////////////////////////////////////////////////
//
// Single precision calculations (double where necessary)

///////////////////////////////////////////////////////////////////////////////
//
// Computes p = x * y
//
static int spMultiply( VM_DIGIT_T p[2], VM_DIGIT_T x, VM_DIGIT_T y );


///////////////////////////////////////////////////////////////////////////////
//
// Computes quotient q = u / v, remainder r = u mod v
//
static VM_DIGIT_T spDivide( VM_DIGIT_T *q, VM_DIGIT_T *r, VM_DIGIT_T u[2], VM_DIGIT_T v );


///////////////////////////////////////////////////////////////////////////////
//
// Computes exp = x^n mod d
//
static int spModExp( VM_DIGIT_T *exp, VM_DIGIT_T x, VM_DIGIT_T n, VM_DIGIT_T d );


///////////////////////////////////////////////////////////////////////////////
//
// Computes a = (x * y) mod m
//
static int spModMult( VM_DIGIT_T *a, VM_DIGIT_T x, VM_DIGIT_T y, VM_DIGIT_T m );


///////////////////////////////////////////////////////////////////////////////
//
// Computes inv = u^-1 mod v
//
static int spModInv( VM_DIGIT_T *inv, VM_DIGIT_T u, VM_DIGIT_T v );


///////////////////////////////////////////////////////////////////////////////
//
// Returns gcd(x, y)
//
static VM_DIGIT_T spGcd( VM_DIGIT_T x, VM_DIGIT_T y );


///////////////////////////////////////////////////////////////////////////////
//
// Returns true if w is prime, else false; t tests
//
static int spIsPrime( VM_DIGIT_T w, unsigned int t );


///////////////////////////////////////////////////////////////////////////////
//
// Returns single pseudo-random digit between lower and upper
// WARN: This prng is not crypto secure
//
static VM_DIGIT_T spPseudoRand( VM_DIGIT_T lower, VM_DIGIT_T upper );



///////////////////////////////////////////////////////////////////////////////
//
// Print utilities

///////////////////////////////////////////////////////////////////////////////
//
// Print all digits incl leading zero digits
//
static void mpPrint( VM_DIGIT_T *p, unsigned int len );


///////////////////////////////////////////////////////////////////////////////
//
// Print all digits with newlines
//
static void mpPrintNL( VM_DIGIT_T *p, unsigned int len );


///////////////////////////////////////////////////////////////////////////////
//
// Print but trim leading zero digits
//
static void mpPrintTrim( VM_DIGIT_T *p, unsigned int len );


///////////////////////////////////////////////////////////////////////////////
//
// Print, trim leading zeroes, add newlines
//
static void mpPrintTrimNL( VM_DIGIT_T *p, unsigned int len );


///////////////////////////////////////////////////////////////////////////////
//
// Return ptr to initialised BIGD structure with precision digits
//
static BIGD_T *bpInit( BIGD_T *b, unsigned int precision );


///////////////////////////////////////////////////////////////////////////////
//
// Zeroise and free memory
//
static void bpFinal( BIGD_T *b );


///////////////////////////////////////////////////////////////////////////////
//
// Increase allocated memory to newsize digits
//
static BIGD_T *bpResize( BIGD_T *b, unsigned int newsize );


///////////////////////////////////////////////////////////////////////////////
//
// Compute w = u + v
//
static int bpAdd( BIGD_T *w, BIGD_T *u, BIGD_T *v );


///////////////////////////////////////////////////////////////////////////////
//
// Compute w = u - v, return borrow
//
static int bpSubtract( BIGD_T *w, BIGD_T *u, BIGD_T *v );


///////////////////////////////////////////////////////////////////////////////
//
// Returns true if a == b, else false
//
static int bpEqual( BIGD_T *a, BIGD_T *b );


///////////////////////////////////////////////////////////////////////////////
//
// Print all significant digits in BIGD_T
//
static void bpPrint(BIGD_T *p);


private:

static int     spModExpK( VM_DIGIT_T *exp, VM_DIGIT_T x, VM_DIGIT_T n, VM_DIGIT_T d );
static int     spModExpB( VM_DIGIT_T *exp, VM_DIGIT_T x, VM_DIGIT_T n, VM_DIGIT_T d );
static void    spMultSub( VM_DIGIT_T uu[2], VM_DIGIT_T qhat, VM_DIGIT_T v1, VM_DIGIT_T v0 );
static VM_DIGIT_T mpHalfDivK( VM_DIGIT_T q[], VM_DIGIT_T u[], VM_HALF_DIGIT_T v, unsigned int ndigits );
static VM_DIGIT_T mpHalfDivZ( VM_DIGIT_T q[], VM_DIGIT_T a[], VM_HALF_DIGIT_T d, unsigned int ndigits );
static VM_DIGIT_T mpMultSub( VM_DIGIT_T wn, VM_DIGIT_T w[], VM_DIGIT_T v[], VM_DIGIT_T q, unsigned int n );
static int     QhatTooBig( VM_DIGIT_T qhat, VM_DIGIT_T rhat, VM_DIGIT_T vn2, VM_DIGIT_T ujn2 );

};


///////////////////////////////////////////////////////////////////////////////
//
// The diffie hellman key pair exchange packages up a few key functions in the
// big integer library. So, it simply extends that class and is set up for the
// convenience of the user of the class (allows instances to be kept and has
// non-static methods)
//
///////////////////////////////////////////////////////////////////////////////

class VMDiffieHellman : private VMBigIntegers
{
public:
  static VMDiffieHellman* GetInstance( void );
  ~VMDiffieHellman( void ){;};

  void GetUniqueSessionKey( char*         pchKeyOutput, 
                            int           iKeyLength, 
                            unsigned long lLocalIPAddress, 
                            unsigned long lRemoteIPAddress );


private:
  VMDiffieHellman( void );

  // prevent defaults from being created by compiler 
  // to enforce singleton pattern
  //
  VMDiffieHellman& operator = ( VMDiffieHellman& roOther ){;};
  VMDiffieHellman( VMDiffieHellman& roOther ){;};

  void mpTrimToBuffer( VM_DIGIT_T* p, int iDigitLength, char* pchKeyOutput, int iKeyLength );
};



#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -