Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cBigUnsigned Class Reference

#include <cBigUnsigned.h>

Inheritance diagram for GrayLib::cBigUnsigned:
GrayLib::cBitArray GrayLib::cBitArrayStatic Gray::cBits GrayLib::cBigInteger

Public Member Functions

 cBigUnsigned ()
 
 cBigUnsigned (const cBigUnsigned &x)
 
 cBigUnsigned (BIT_ENUM_t nBits, BLOCK_t uValMask)
 
 cBigUnsigned (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
 cBigUnsigned (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks, bool bStatic)
 
 cBigUnsigned (const char *pszStr, RADIX_t nBaseRadix=10)
 
 ~cBigUnsigned ()
 
 cBigUnsigned (UINTMAX_t x)
 
 cBigUnsigned (INTMAX_t x)
 
 cBigUnsigned (UINT32 x)
 
 cBigUnsigned (INT32 x)
 
void SetBlockSizeInit (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
bool isZero () const
 
bool isOdd () const
 
template<typename TYPE >
TYPE get_ValU () const
 
template<typename TYPE >
TYPE get_ValUC () const
 
template<typename TYPE >
TYPE get_ValS () const
 
template<typename TYPE >
TYPE get_Val () const
 
COMPARE_t CompareU (BLOCK_t x) const
 Compare against primitive type for speed. More...
 
bool operator== (BLOCK_t x) const
 
bool operator!= (BLOCK_t x) const
 
bool operator< (BLOCK_t x) const
 
bool operator<= (BLOCK_t x) const
 
bool operator>= (BLOCK_t x) const
 
bool operator> (BLOCK_t x) const
 
COMPARE_t Compare (const cBigUnsigned &x) const
 Compares this to x like Perl's <=> More...
 
bool operator== (const cBigUnsigned &x) const
 
bool operator!= (const cBigUnsigned &x) const
 
bool operator< (const cBigUnsigned &x) const
 
bool operator<= (const cBigUnsigned &x) const
 
bool operator>= (const cBigUnsigned &x) const
 
bool operator> (const cBigUnsigned &x) const
 
StrLen_t GetStr (char *pszOut, StrLen_t iOutMax, RADIX_t nBaseRadix=10) const
 
cString GetStr (RADIX_t nBaseRadix=10) const
 
bool SetStr (const char *pszVal, RADIX_t nBaseRadix=10, const char **ppszEnd=(const char **) nullptr)
 
void put_ValU (UINTMAX_t n)
 
HRESULT put_ValS (INTMAX_t n)
 
template<typename TYPE >
void put_Val (TYPE n)
 
void InitBitAnd (const cBigUnsigned &a, const cBigUnsigned &b)
 
void InitBitOr (const cBigUnsigned &a, const cBigUnsigned &b)
 
void InitBitXor (const cBigUnsigned &a, const cBigUnsigned &b)
 
HRESULT InitBitShiftLeft (const cBigUnsigned &a, int b)
 
HRESULT InitBitShiftRight (const cBigUnsigned &a, int b)
 
void OpBitShiftLeft1 (BLOCK_t nBitMask=0)
 
HRESULT OpBitShiftLeft (BIT_ENUM_t b)
 
HRESULT OpBitShiftRight (BIT_ENUM_t b)
 
void OpAdd1 (BLOCK_t nVal)
 
HRESULT OpSubtract1 (BLOCK_t nVal)
 
void InitSubtract1 (const THIS_t &a, BLOCK_t n)
 
void InitAdd (const cBigUnsigned &a, const cBigUnsigned &b)
 
HRESULT InitSubtract (const cBigUnsigned &a, const cBigUnsigned &b)
 
HRESULT OpSubtract (const THIS_t &b)
 
void InitMultiply1 (const cBigUnsigned &a, BLOCK_t b)
 
void InitMultiplyH (const cBigUnsigned &a, BLOCKH_t b)
 
void InitMultiply (const cBigUnsigned &a, const cBigUnsigned &b)
 
BLOCK_t GetModulusH (BLOCKH_t b) const
 
BLOCKH_t InitDivideH (const cBigUnsigned &a, BLOCKH_t b)
 
HRESULT InitDivide (const cBigUnsigned &a, const cBigUnsigned &b, OUT cBigUnsigned &remainder)
 
HRESULT InitDivide (const THIS_t &a, const THIS_t &b)
 
HRESULT InitModulus (const THIS_t &a, const THIS_t &b)
 
HRESULT InitModInv (const cBigUnsigned &A, const cBigUnsigned &N)
 
HRESULT SetRandomBits2 (BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr)
 
HRESULT SetRandomBits (BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr)
 
BLOCK_t get_MontgomeryInit () const
 
void SetMontMul (THIS_t &rDst, const THIS_t &B, const THIS_t &N, BLOCK_t mm)
 
void SetMontRedux (THIS_t &rDst, const THIS_t &N, BLOCK_t mm)
 
void SetPower (const THIS_t &base, const THIS_t &exponent)
 
HRESULT SetPowerMod (const THIS_t &base, const THIS_t &exponent, const THIS_t &modulus, OUT THIS_t *pRR=nullptr)
 
BITOP_TYPE TestPrimeSmall () const
 
HRESULT TestPrimeMiller (IRandomNoise *pRandom=nullptr) const
 
HRESULT TestPrimeFermat (IRandomNoise *pRandom=nullptr) const
 
HRESULT TestPrime (IRandomNoise *pRandom=nullptr) const
 
HRESULT SetPrimePrev (IRandomNoise *pRandom=nullptr, cThreadState *pCancel=nullptr)
 
HRESULT SetPrimeBits (BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr, cThreadState *pCancel=nullptr)
 
HRESULT SetPrimeBitsDH (BIT_ENUM_t nbits, IRandomNoise *pRandom=nullptr, cThreadState *pCancel=nullptr)
 
cBigUnsigned operator+ (const THIS_t &x) const
 Overloaded return-by-value operators. More...
 
cBigUnsigned operator+ (BLOCK_t x) const
 
cBigUnsigned operator- (const THIS_t &x) const
 
cBigUnsigned operator- (BLOCK_t x) const
 
cBigUnsigned operator* (const THIS_t &x) const
 
cBigUnsigned operator* (BLOCKH_t x) const
 
cBigUnsigned operator/ (const THIS_t &x) const
 
cBigUnsigned operator/ (BLOCKH_t n) const
 
cBigUnsigned operator% (const THIS_t &x) const
 
BLOCKH_t operator% (BLOCKH_t n) const
 
cBigUnsigned operator& (const THIS_t &x) const
 
cBigUnsigned operator| (const THIS_t &x) const
 
cBigUnsigned operator^ (const THIS_t &x) const
 
cBigUnsigned operator<< (int b) const
 
cBigUnsigned operator>> (int b) const
 
void operator+= (const THIS_t &x)
 
void operator+= (BLOCK_t x)
 
void operator-= (const THIS_t &x)
 
void operator-= (BLOCK_t x)
 
void operator*= (const THIS_t &x)
 
void operator*= (BLOCKH_t x)
 
void operator/= (const THIS_t &x)
 
void operator/= (BLOCKH_t n)
 
void operator%= (const THIS_t &x)
 
void operator%= (BLOCKH_t n)
 
void operator&= (const THIS_t &x)
 
void operator|= (const THIS_t &x)
 
void operator^= (const THIS_t &x)
 
void operator<<= (int b)
 
void operator>>= (int b)
 
void operator++ ()
 
void operator++ (int)
 
void operator-- ()
 
void operator-- (int)
 
 UNITTEST_FRIEND (cBigUnsigned)
 
template<>
long get_Val () const
 overloads to get signed value correctly. More...
 
template<>
void put_Val (long n)
 
template<>
void put_Val (int n)
 
template<>
void put_Val (short n)
 
- Public Member Functions inherited from GrayLib::cBitArray
bool isHighBlockUseExact () const
 
bool isHighBlockUseValid () const
 
BLOCK_ENUM_t get_BlocksUse () const
 
void UpdateBlocksUse ()
 
void UpdateBlocksUseMax (BLOCK_ENUM_t nBlocksUseMax)
 
void UpdateBlocksUseCap ()
 
BLOCK_tRefBlock (BLOCK_ENUM_t nBlock)
 
 cBitArray ()
 
 cBitArray (BIT_ENUM_t nBits)
 
 cBitArray (BIT_ENUM_t nBits, BLOCK_t uValueMask)
 
 cBitArray (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
 cBitArray (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks, bool bStatic)
 
 ~cBitArray ()
 
void SetBlocksStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
void SetBlocksStatic (const cBitArrayStatic &b)
 
HRESULT SetBlockSizeGrow (BLOCK_ENUM_t nBlocksNew)
 
HRESULT SetBlockSizeShrink (BLOCK_ENUM_t nBlocksNew)
 
void SetBlockSizeUse (BLOCK_ENUM_t nBlocksNew)
 
bool IsEqual (const THIS_t &x) const
 
bool IsEqualU (BLOCK_t x) const
 
bool operator== (const THIS_t &x) const
 
bool operator!= (const THIS_t &x) const
 
bool isEmptyBits () const
 
bool IsSet (BIT_ENUM_t nBit) const
 
BIT_ENUM_t get_Lowest1Bit () const
 
BIT_ENUM_t get_Highest1Bit () const
 
BIT_ENUM_t get_Count1Bits () const
 
void SetBit (BIT_ENUM_t nBit)
 
void ClrBit (BIT_ENUM_t nBit)
 
void ModBit (BIT_ENUM_t nBit, bool bVal)
 
void SetTruncateBits (BIT_ENUM_t nBits)
 
HRESULT SetRandomBits (BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr)
 
HRESULT SetRandomBitsLarge (BIT_ENUM_t nBits, IRandomNoise *pRandom=nullptr)
 
void SetCopyBits (const THIS_t &ref)
 
HRESULT SetCopySecure (const THIS_t &val, bool assign)
 
void SetZeroAll ()
 
void SetNullVal ()
 
size_t get_BinarySize () const
 
HRESULT GetBinaryBE (BYTE *pOut, size_t nSizeOut) const
 
HRESULT GetBinaryBE (cHeapBlock &b) const
 
HRESULT SetBinaryBE (const BYTE *pData, size_t nSize)
 
HRESULT SetBinaryLE (const BYTE *pData, size_t nSize)
 
 UNITTEST_FRIEND (cBitArray)
 
- Public Member Functions inherited from GrayLib::cBitArrayStatic
 cBitArrayStatic () noexcept
 
 cBitArrayStatic (BIT_ENUM_t nBits)
 
 cBitArrayStatic (BIT_ENUM_t nBits, BLOCK_t uValueMask)
 
 cBitArrayStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pData)
 
 cBitArrayStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocksInit, bool bStatic)
 
 cBitArrayStatic (const THIS_t &ref)
 
 cBitArrayStatic (THIS_t &&ref) noexcept
 
 ~cBitArrayStatic ()
 
bool isNullVal () const noexcept
 
bool isZeroVal () const noexcept
 
BLOCK_ENUM_t get_BlocksCap () const noexcept
 
size_t get_BytesCap () const noexcept
 
BLOCK_t GetBlockInt (BLOCK_ENUM_t i) const
 
const BLOCK_tget_BlockPtrC () const noexcept
 
BLOCK_tget_BlockPtr ()
 
BLOCK_tRefBlockInt (BLOCK_ENUM_t i)
 
UINT32 GetU32Int (BLOCK_ENUM_t j) const
 
void SetU32Int (BLOCK_ENUM_t j, UINT32 val32)
 
const THIS_toperator= (const THIS_t &ref)
 
THIS_toperator= (THIS_t &&ref)
 
bool isStatic () const noexcept
 
void SetBlocksStatic (BLOCK_ENUM_t nBlocks, const BLOCK_t *pBlocks)
 
bool isReadOnly () const noexcept
 
BIT_ENUM_t get_BitsCap () const noexcept
 
bool IsValidBit (BIT_ENUM_t nBit) const noexcept
 
bool IsSet (BIT_ENUM_t nBit) const
 
bool isOdd () const
 
void ClrBit (BIT_ENUM_t nBit)
 
void SetBit (BIT_ENUM_t nBit)
 
bool SetBitRet (BIT_ENUM_t nBit)
 
void ModBit (BIT_ENUM_t nBit, bool bVal)
 
void SetNullVal () noexcept
 
void SetZeroAll ()
 
void SetAll1 ()
 
void SetBitsSizeZ (BIT_ENUM_t nBits)
 
void SetInvertBytes (size_t nSize)
 
void SetCopyBits (const THIS_t &ref)
 

Static Public Member Functions

static void MultiplyHelper (BLOCK_t *pDst, BLOCK_ENUM_t nBlocks, const cBigUnsigned &rSrc, BLOCK_t nMult)
 
static void GRAYCALL EuclideanAlgorithm (const cBigUnsigned &x, const cBigUnsigned &y, OUT cBigUnsigned &a, OUT cBigUnsigned &b, OUT cBigUnsigned &g)
 
static cBigUnsigned GRAYCALL GetGreatestCommonDivisor (const cBigUnsigned &x, const cBigUnsigned &y)
 
- Static Public Member Functions inherited from GrayLib::cBitArray
static HRESULT GRAYCALL ReturnError (HRESULT hRes, const char *pszMsg)
 
static HRESULT GRAYCALL SwapSecure (THIS_t &X, THIS_t &Y, bool swap)
 
- Static Public Member Functions inherited from GrayLib::cBitArrayStatic
static BLOCK_ENUM_t GetBlocksFromBits (BIT_ENUM_t nBits) noexcept
 
static BLOCK_ENUM_t GetBlocksFromBytes (size_t nBytes) noexcept
 
static bool IsReasonableBlockQty (BLOCK_ENUM_t nBlocks) noexcept
 
- Static Public Member Functions inherited from Gray::cBits
static constexpr size_t GetSizeBytes (BIT_ENUM_t nBits) noexcept
 
template<typename TYPE >
static constexpr TYPE Mask1 (BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr bool IsMask1 (TYPE nVal) noexcept
 
template<typename TYPE >
static constexpr bool IsSet (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr bool IsClear (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr TYPE SetBit (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr TYPE ClearBit (TYPE nVal, BIT_ENUM_t nBit) noexcept
 
template<typename TYPE >
static constexpr bool HasMask (TYPE nVal, TYPE nMask) noexcept
 
template<typename TYPE >
static BIT_ENUM_t Highest1Bit (TYPE nMask) noexcept
 
template<typename TYPE >
static BIT_ENUM_t Count1Bits (TYPE nMask) noexcept
 
template<typename TYPE >
static TYPE Rotl (TYPE nVal, BIT_ENUM_t nBits) noexcept
 
template<typename TYPE >
static TYPE Rotr (TYPE nVal, BIT_ENUM_t nBits) noexcept
 
template<typename TYPE >
static TYPE Reverse (TYPE nVal) noexcept
 
template<>
BIT_ENUM_t Count1Bits (UINT32 nVal) noexcept
 
template<>
BYTE Reverse (BYTE nVal) noexcept
 
template<>
UINT32 Reverse (UINT32 nVal) noexcept
 
template<>
ULONG Reverse (ULONG nVal) noexcept
 

Additional Inherited Members

- Public Types inherited from GrayLib::cBitArrayStatic
typedef UINT32 BLOCK_t
 The biggest unsigned type I can do atomic math on for this architecture. More...
 
typedef INT32 BLOCKI_t
 The biggest signed type I can do atomic math on. More...
 
typedef WORD BLOCKH_t
 half sized BLOCK_t for multiplication overflows. More...
 
typedef unsigned int BLOCK_ENUM_t
 Type for the index of a BLOCK_t in the array. NOT bits BIT_ENUM_t. More...
 
- Static Public Attributes inherited from GrayLib::cBitArrayStatic
static const BLOCK_ENUM_t k_BLOCK_MAX = 8096
 Arbitrary Max reasonable size. More...
 
static const size_t k_SIZE_MAX = k_BLOCK_MAX * sizeof(BLOCK_t)
 
static const BIT_ENUM_t k_BIT_MAX = k_SIZE_MAX * 8
 
static const cBitArrayStatic k_Zero
 
static const BLOCK_t k_OneBlock = 1
 
static const BIT_ENUM_t k_BLOCK_BITS = (BIT_ENUM_t)(sizeof(BLOCK_t) * 8)
 The number of bits in a BLOCK_t, BIT_ENUM_t. More...
 
static const BIT_ENUM_t k_BLOCKH_BITS = (BIT_ENUM_t)(sizeof(BLOCKH_t) * 8)
 The number of bits in a BLOCKH_t, BIT_ENUM_t. More...
 
- Static Public Attributes inherited from Gray::cBits
static const BYTE k_8 = 8
 represent the 8 bits in a byte. BIT_ENUM_t More...
 
- Protected Member Functions inherited from GrayLib::cBitArray
void put_BlocksUse (BLOCK_ENUM_t nBlocksUse)
 
BLOCK_t GetBlock (BLOCK_ENUM_t nBlock) const
 
void AddBlockGrow (BLOCK_t nVal)
 
void SetBlock (BLOCK_ENUM_t i, BLOCK_t nVal)
 
- Protected Member Functions inherited from GrayLib::cBitArrayStatic
bool IsValidBlockIndex (BLOCK_ENUM_t i) const noexcept
 
void SetBlockSizeReAlloc (BLOCK_ENUM_t nBlocksNew)
 
void SetBlockSizeUninit (BLOCK_ENUM_t nBlocksNew)
 
void SetBlockSizeGrow (BLOCK_ENUM_t nBlocksNew)
 
BLOCK_ENUM_t FindUsedBlocksQty (BLOCK_ENUM_t nBlocks) const
 
BLOCK_ENUM_t FindUsedBlocksQty () const
 
- Protected Attributes inherited from GrayLib::cBitArray
BLOCK_ENUM_t m_nBlocksUse
 Qty of blocks that is non zero. ASSUME anything above this is 0 blocks. More...
 

Detailed Description

An arbitrarily large unsigned number. limited only by memory. Stored as little endian order blocks. Can be used to hold a RSA key or other large number. Often used for a prime number.

Note
DON'T ASSUME BLOCK_t is USE_LITTLE_ENDIAN native format. Beware of type punning to BLOCKH_t. Can throw: Overflow (also if signed), Divide By Zero, etc. Check out sample: http://cpp-bigint.sourceforge.net/ or http://dclib.sourceforge.net/

Constructor & Destructor Documentation

◆ cBigUnsigned() [1/10]

GrayLib::cBigUnsigned::cBigUnsigned ( )
inline

Constructs zero.

◆ cBigUnsigned() [2/10]

GrayLib::cBigUnsigned::cBigUnsigned ( const cBigUnsigned x)
inline

Copy constructor. used for default operator =. not use explicit.

◆ cBigUnsigned() [3/10]

GrayLib::cBigUnsigned::cBigUnsigned ( BIT_ENUM_t  nBits,
BLOCK_t  uValMask 
)
inline

◆ cBigUnsigned() [4/10]

GrayLib::cBigUnsigned::cBigUnsigned ( BLOCK_ENUM_t  nBlocks,
const BLOCK_t pBlocks 
)
inline

Create a NON static init object.

◆ cBigUnsigned() [5/10]

GrayLib::cBigUnsigned::cBigUnsigned ( BLOCK_ENUM_t  nBlocks,
const BLOCK_t pBlocks,
bool  bStatic 
)
inline

Create a static init object.

◆ cBigUnsigned() [6/10]

GrayLib::cBigUnsigned::cBigUnsigned ( const char *  pszStr,
RADIX_t  nBaseRadix = 10 
)
inlineexplicit

construct from const string.

◆ ~cBigUnsigned()

GrayLib::cBigUnsigned::~cBigUnsigned ( )
inline

assume cBitArray does the delete for us.

◆ cBigUnsigned() [7/10]

GrayLib::cBigUnsigned::cBigUnsigned ( UINTMAX_t  x)
inline

◆ cBigUnsigned() [8/10]

GrayLib::cBigUnsigned::cBigUnsigned ( INTMAX_t  x)
inline

◆ cBigUnsigned() [9/10]

GrayLib::cBigUnsigned::cBigUnsigned ( UINT32  x)
inline

◆ cBigUnsigned() [10/10]

GrayLib::cBigUnsigned::cBigUnsigned ( INT32  x)
inline

Member Function Documentation

◆ Compare()

COMPARE_t GrayLib::cBigUnsigned::Compare ( const cBigUnsigned x) const

Compares this to x like Perl's <=>

Comparison of 2 values. similar to memcmp() A bigger used length implies a bigger number.

◆ CompareU()

COMPARE_t GrayLib::cBigUnsigned::CompareU ( BLOCK_t  x) const

Compare against primitive type for speed.

Compare a single block value to this. A bigger length implies a bigger number.

Returns
COMPARE_Greater = tis is greater than nVal2

◆ EuclideanAlgorithm()

void GrayLib::cBigUnsigned::EuclideanAlgorithm ( const cBigUnsigned x,
const cBigUnsigned y,
OUT cBigUnsigned a,
OUT cBigUnsigned b,
OUT cBigUnsigned g 
)
static

This function uses the Euclidean algorithm to find the greatest common divisor (GCD) g of the positive integers x and y and also two integers a and b such that ax - by = g, 1 <= a <= y and 0 <= b < x.

Here cBigUnsigned is an unsigned integer type. All calculations use unsigned arithmetic, and none produces any result larger than the maximum of x and y.

This function will fail in undefined ways if either x or y is zero.

◆ get_MontgomeryInit()

cBigUnsigned::BLOCK_t GrayLib::cBigUnsigned::get_MontgomeryInit ( ) const

Get Fast Montgomery initialization

◆ get_Val() [1/2]

template<typename TYPE >
TYPE GrayLib::cBigUnsigned::get_Val ( ) const
inline

just default to assume TYPE is unsigned.

◆ get_Val() [2/2]

template<>
short GrayLib::cBigUnsigned::get_Val ( ) const
inline

overloads to get signed value correctly.

◆ get_ValS()

template<typename TYPE >
TYPE GrayLib::cBigUnsigned::get_ValS ( ) const
inline

get a signed type.

◆ get_ValU()

template<typename TYPE >
TYPE GrayLib::cBigUnsigned::get_ValU ( ) const
inline

Get a mask/value of native/primitive unsigned TYPE.

Returns
the least significant X bits. Allow/ignore overflow.

◆ get_ValUC()

template<typename TYPE >
TYPE GrayLib::cBigUnsigned::get_ValUC ( ) const
inline

Get unsigned value but throw if not big enough to hold the value.

◆ GetGreatestCommonDivisor()

cBigUnsigned GRAYCALL GrayLib::cBigUnsigned::GetGreatestCommonDivisor ( const cBigUnsigned x,
const cBigUnsigned y 
)
static

Greatest common divisor: G = gcd(A, B) (HAC 14.54)

Returns
the greatest common divisor of x and y. (GCD) This function uses the Euclidean algorithm to find the greatest common divisor of the positive integers x and y. This function will fail in undefined ways if either x or y is zero. http://en.wikipedia.org/wiki/Greatest_common_divisor

◆ GetModulusH()

cBigUnsigned::BLOCK_t GrayLib::cBigUnsigned::GetModulusH ( BLOCKH_t  b) const
  • b = Integer to divide *this by
    Returns
    Modulo/remainder: *this mod b

◆ GetStr() [1/2]

StrLen_t GrayLib::cBigUnsigned::GetStr ( char *  pszOut,
StrLen_t  iOutMax,
RADIX_t  nBaseRadix = 10 
) const

Get the value as a string. high to low.

Note
DSig wants this in Base64 format. so used UUEncode instead of this.
Returns
length in characters.

◆ GetStr() [2/2]

cString GrayLib::cBigUnsigned::GetStr ( RADIX_t  nBaseRadix = 10) const
Note
We can estimate the string size via get_Highest1Bit()

◆ InitAdd()

void GrayLib::cBigUnsigned::InitAdd ( const cBigUnsigned a,
const cBigUnsigned b 
)

Set *this to the result of a+b. don't care if &a or &b = this Unsigned addition: X = |A| + |B| (HAC 14.7)

◆ InitBitAnd()

void GrayLib::cBigUnsigned::InitBitAnd ( const cBigUnsigned a,
const cBigUnsigned b 
)

don't care if &a or &b is this

◆ InitBitOr()

void GrayLib::cBigUnsigned::InitBitOr ( const cBigUnsigned a,
const cBigUnsigned b 
)

don't care if &a or &b is this

◆ InitBitShiftLeft()

HRESULT GrayLib::cBigUnsigned::InitBitShiftLeft ( const cBigUnsigned a,
int  b 
)

Shift up/left. grow. don't care if &a = this.

  • b = is int NOT BIT_ENUM_t because we want the sign.

◆ InitBitShiftRight()

HRESULT GrayLib::cBigUnsigned::InitBitShiftRight ( const cBigUnsigned a,
int  b 
)

Shift Down/Right. Shrink. don't care if a === *this

  • b = is NOT BIT_ENUM_t because we want the sign.

◆ InitBitXor()

void GrayLib::cBigUnsigned::InitBitXor ( const cBigUnsigned a,
const cBigUnsigned b 
)

don't care if a or b is *this

◆ InitDivide() [1/2]

HRESULT GrayLib::cBigUnsigned::InitDivide ( const cBigUnsigned a,
const cBigUnsigned b,
OUT cBigUnsigned remainder 
)

Division with remainder. get quotient,remainder for A/B. A = Q * B + R (HAC 14.20) remainder = modulus.

◆ InitDivide() [2/2]

HRESULT GrayLib::cBigUnsigned::InitDivide ( const THIS_t a,
const THIS_t b 
)
inline

this = a / b;

◆ InitDivideH()

cBigUnsigned::BLOCKH_t GrayLib::cBigUnsigned::InitDivideH ( const cBigUnsigned a,
BLOCKH_t  b 
)

this = a / b; It's OK if a == *this. Divide x by intrinsic type n. Faster small number math.

Returns
the remainder/modulus.

◆ InitModInv()

HRESULT GrayLib::cBigUnsigned::InitModInv ( const cBigUnsigned A,
const cBigUnsigned N 
)

Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64) returns X in range 1..N-1 such that X*A = 1 mod N A must be in range 1..N-1 Returns the multiplicative inverse of x modulo n, or throws an exception if they have a common factor.

◆ InitModulus()

HRESULT GrayLib::cBigUnsigned::InitModulus ( const THIS_t a,
const THIS_t b 
)
inline

this = a % b;

◆ InitMultiply()

void GrayLib::cBigUnsigned::InitMultiply ( const cBigUnsigned a,
const cBigUnsigned b 
)

Set *this to the result of a*b. Half precision multiplies

◆ InitMultiply1()

void GrayLib::cBigUnsigned::InitMultiply1 ( const cBigUnsigned a,
BLOCK_t  b 
)

small block multiplication: *this = a * b

◆ InitMultiplyH()

void GrayLib::cBigUnsigned::InitMultiplyH ( const cBigUnsigned a,
BLOCKH_t  b 
)

small half block multiplication: *this = a * b just do half precision multiplies. so we can keep overflow on each multiply. It's OK if a == *this. Faster small number math. Faster than InitMultiply1.

◆ InitSubtract()

HRESULT GrayLib::cBigUnsigned::InitSubtract ( const cBigUnsigned a,
const cBigUnsigned b 
)

Set *this to the result of a-b. don't care if &a or &b == this Unsigned subtraction : X = |A| - |B|

Returns
DISP_E_OVERFLOW if it would go negative.

◆ InitSubtract1()

void GrayLib::cBigUnsigned::InitSubtract1 ( const THIS_t a,
BLOCK_t  n 
)
inline

◆ isOdd()

bool GrayLib::cBigUnsigned::isOdd ( ) const
inline

Is the LSB odd ?

◆ isZero()

bool GrayLib::cBigUnsigned::isZero ( ) const
inline

ASSUME: used length is zero = the number is zero. and visa versa. like isEmptyBits()

◆ MultiplyHelper()

void GrayLib::cBigUnsigned::MultiplyHelper ( BLOCK_t pDst,
BLOCK_ENUM_t  nBlocks,
const cBigUnsigned rSrc,
BLOCK_t  nMult 
)
static

Helper for cBigUnsigned multiplication Multiply source vector [pSrc] with nMult, add result to destination vector [pDst] and set carry nCarry.

Currently supports: . IA-32 (386+) . AMD64 / EM64T . IA-32 (SSE2) . Motorola 68000 . PowerPC, 32-bit . MicroBlaze . PowerPC, 64-bit . TriCore . SPARC v8 . ARM v3+ . Alpha . MIPS32 . C, longlong . C, generic

◆ OpAdd1()

void GrayLib::cBigUnsigned::OpAdd1 ( BLOCK_t  nVal)

Faster small number math. n is unsigned. cant be negative.

Note
No such thing as overflow.

◆ OpBitShiftLeft()

HRESULT GrayLib::cBigUnsigned::OpBitShiftLeft ( BIT_ENUM_t  b)
inline

Signed bits. NOT BIT_ENUM_t

◆ OpBitShiftLeft1()

void GrayLib::cBigUnsigned::OpBitShiftLeft1 ( BLOCK_t  nBitMask = 0)

Used to fast shift left/grow 1 bit and add a bit 0/1. Used in Divide() Faster than OpBitShiftLeft() nBitMask = a 1 bit value. 0 or 1.

◆ OpBitShiftRight()

HRESULT GrayLib::cBigUnsigned::OpBitShiftRight ( BIT_ENUM_t  b)
inline

Signed bits. NOT BIT_ENUM_t

◆ operator!=() [1/2]

bool GrayLib::cBigUnsigned::operator!= ( BLOCK_t  x) const
inline

◆ operator!=() [2/2]

bool GrayLib::cBigUnsigned::operator!= ( const cBigUnsigned x) const
inline

◆ operator%() [1/2]

BLOCKH_t GrayLib::cBigUnsigned::operator% ( BLOCKH_t  n) const
inline

Modulus small.

◆ operator%() [2/2]

cBigUnsigned GrayLib::cBigUnsigned::operator% ( const THIS_t x) const
inline

Modulus.

◆ operator%=() [1/2]

void GrayLib::cBigUnsigned::operator%= ( BLOCKH_t  n)
inline

◆ operator%=() [2/2]

void GrayLib::cBigUnsigned::operator%= ( const THIS_t x)
inline

Modulus *this by x. Don't care about quotient left in q.

◆ operator&()

cBigUnsigned GrayLib::cBigUnsigned::operator& ( const THIS_t x) const
inline

◆ operator&=()

void GrayLib::cBigUnsigned::operator&= ( const THIS_t x)
inline

◆ operator*() [1/2]

cBigUnsigned GrayLib::cBigUnsigned::operator* ( BLOCKH_t  x) const
inline

◆ operator*() [2/2]

cBigUnsigned GrayLib::cBigUnsigned::operator* ( const THIS_t x) const
inline

◆ operator*=() [1/2]

void GrayLib::cBigUnsigned::operator*= ( BLOCKH_t  x)
inline

◆ operator*=() [2/2]

void GrayLib::cBigUnsigned::operator*= ( const THIS_t x)
inline

◆ operator+() [1/2]

cBigUnsigned GrayLib::cBigUnsigned::operator+ ( BLOCK_t  x) const
inline

◆ operator+() [2/2]

cBigUnsigned GrayLib::cBigUnsigned::operator+ ( const THIS_t x) const
inline

Overloaded return-by-value operators.

◆ operator++() [1/2]

void GrayLib::cBigUnsigned::operator++ ( )
inline

◆ operator++() [2/2]

void GrayLib::cBigUnsigned::operator++ ( int  )
inline

Post fix increment: same as prefix

◆ operator+=() [1/2]

void GrayLib::cBigUnsigned::operator+= ( BLOCK_t  x)
inline

◆ operator+=() [2/2]

void GrayLib::cBigUnsigned::operator+= ( const THIS_t x)
inline

◆ operator-() [1/2]

cBigUnsigned GrayLib::cBigUnsigned::operator- ( BLOCK_t  x) const
inline

◆ operator-() [2/2]

cBigUnsigned GrayLib::cBigUnsigned::operator- ( const THIS_t x) const
inline

◆ operator--() [1/2]

void GrayLib::cBigUnsigned::operator-- ( )
inline

◆ operator--() [2/2]

void GrayLib::cBigUnsigned::operator-- ( int  )
inline

Postfix decrement: same as prefix

◆ operator-=() [1/2]

void GrayLib::cBigUnsigned::operator-= ( BLOCK_t  x)
inline

◆ operator-=() [2/2]

void GrayLib::cBigUnsigned::operator-= ( const THIS_t x)
inline

◆ operator/() [1/2]

cBigUnsigned GrayLib::cBigUnsigned::operator/ ( BLOCKH_t  n) const
inline

◆ operator/() [2/2]

cBigUnsigned GrayLib::cBigUnsigned::operator/ ( const THIS_t x) const
inline

◆ operator/=() [1/2]

void GrayLib::cBigUnsigned::operator/= ( BLOCKH_t  n)
inline

◆ operator/=() [2/2]

void GrayLib::cBigUnsigned::operator/= ( const THIS_t x)
inline

Divide *this by x. don't care about remainder.

◆ operator<() [1/2]

bool GrayLib::cBigUnsigned::operator< ( BLOCK_t  x) const
inline

◆ operator<() [2/2]

bool GrayLib::cBigUnsigned::operator< ( const cBigUnsigned x) const
inline

◆ operator<<()

cBigUnsigned GrayLib::cBigUnsigned::operator<< ( int  b) const
inline

◆ operator<<=()

void GrayLib::cBigUnsigned::operator<<= ( int  b)
inline

◆ operator<=() [1/2]

bool GrayLib::cBigUnsigned::operator<= ( BLOCK_t  x) const
inline

◆ operator<=() [2/2]

bool GrayLib::cBigUnsigned::operator<= ( const cBigUnsigned x) const
inline

◆ operator==() [1/2]

bool GrayLib::cBigUnsigned::operator== ( BLOCK_t  x) const
inline

◆ operator==() [2/2]

bool GrayLib::cBigUnsigned::operator== ( const cBigUnsigned x) const
inline

◆ operator>() [1/2]

bool GrayLib::cBigUnsigned::operator> ( BLOCK_t  x) const
inline

◆ operator>() [2/2]

bool GrayLib::cBigUnsigned::operator> ( const cBigUnsigned x) const
inline

◆ operator>=() [1/2]

bool GrayLib::cBigUnsigned::operator>= ( BLOCK_t  x) const
inline

◆ operator>=() [2/2]

bool GrayLib::cBigUnsigned::operator>= ( const cBigUnsigned x) const
inline

◆ operator>>()

cBigUnsigned GrayLib::cBigUnsigned::operator>> ( int  b) const
inline

◆ operator>>=()

void GrayLib::cBigUnsigned::operator>>= ( int  b)
inline

◆ operator^()

cBigUnsigned GrayLib::cBigUnsigned::operator^ ( const THIS_t x) const
inline

◆ operator^=()

void GrayLib::cBigUnsigned::operator^= ( const THIS_t x)
inline

◆ operator|()

cBigUnsigned GrayLib::cBigUnsigned::operator| ( const THIS_t x) const
inline

◆ operator|=()

void GrayLib::cBigUnsigned::operator|= ( const THIS_t x)
inline

◆ OpSubtract()

HRESULT GrayLib::cBigUnsigned::OpSubtract ( const THIS_t b)
inline

◆ OpSubtract1()

HRESULT GrayLib::cBigUnsigned::OpSubtract1 ( BLOCK_t  nVal)

Faster small number math. n is unsigned. cant be negative.

Returns
true = underflow. false = done.

◆ put_Val() [1/4]

template<>
void GrayLib::cBigUnsigned::put_Val ( int  n)
inline

◆ put_Val() [2/4]

template<>
void GrayLib::cBigUnsigned::put_Val ( long  n)
inline

◆ put_Val() [3/4]

template<>
void GrayLib::cBigUnsigned::put_Val ( short  n)
inline

◆ put_Val() [4/4]

template<typename TYPE >
void GrayLib::cBigUnsigned::put_Val ( TYPE  n)
inline

assume unsigned TYPE. signed types are handled special.

◆ put_ValS()

HRESULT GrayLib::cBigUnsigned::put_ValS ( INTMAX_t  n)

◆ put_ValU()

void GrayLib::cBigUnsigned::put_ValU ( UINTMAX_t  n)

Sets this from a native/primitive unsigned TYPE.

◆ SetBlockSizeInit()

void GrayLib::cBigUnsigned::SetBlockSizeInit ( BLOCK_ENUM_t  nBlocks,
const BLOCK_t pBlocks 
)

Set the size and value from an array of blocks.

◆ SetMontMul()

void GrayLib::cBigUnsigned::SetMontMul ( THIS_t rDst,
const THIS_t B,
const THIS_t N,
BLOCK_t  mm 
)

Montgomery multiplication: this = this * B * R^-1 mod N (HAC 14.36)

◆ SetMontRedux()

void GrayLib::cBigUnsigned::SetMontRedux ( THIS_t rDst,
const THIS_t N,
BLOCK_t  mm 
)

Montgomery reduction: this = this * R^-1 mod N

◆ SetPower()

void GrayLib::cBigUnsigned::SetPower ( const THIS_t base,
const THIS_t exponent 
)

Set *this to 'base' to the power of 'exponent'. Automatically sizes this to fit the result.

◆ SetPowerMod()

HRESULT GrayLib::cBigUnsigned::SetPowerMod ( const THIS_t base,
const THIS_t exponent,
const THIS_t modulus,
OUT THIS_t pRR = nullptr 
)

*this = ((base^exponent)modulus) Set *this to 'base' to the power of 'exponent' then modulus. Sliding-window exponentiation: X = base^exponent mod modulus Automatically sizes this to fit the result. AKA expmod() or exp_mod()

  • pRN = return cached RR if desired.
    Note
    pRN is used to avoid re-computing R*R mod N across multiple calls, which speeds up things a bit. It can be set to nullptr if the extra performance is unneeded.
    < Maximum windows size used. bits.

◆ SetPrimeBits()

HRESULT GrayLib::cBigUnsigned::SetPrimeBits ( BIT_ENUM_t  nBits,
IRandomNoise pRandom = nullptr,
cThreadState pCancel = nullptr 
)

Set the prime on one single thread. Might be slow.

◆ SetPrimeBitsDH()

HRESULT GrayLib::cBigUnsigned::SetPrimeBitsDH ( BIT_ENUM_t  nbits,
IRandomNoise pRandom = nullptr,
cThreadState pCancel = nullptr 
)

Prime number generation on one single thread. Might be slow. add condition for Y and this = 2Y + 1 to be prime is this = 2 mod 3 (which is equivalent to Y = 2 mod 3). Make sure it is satisfied, while keeping this = 3 mod 4

◆ SetPrimePrev()

HRESULT GrayLib::cBigUnsigned::SetPrimePrev ( IRandomNoise pRandom = nullptr,
cThreadState pCancel = nullptr 
)

This function generates/finds/guesses a random (potentially large) prime. Assume SetRandomBitsLarge was called on this. Look for previous to ensure we never overflow the number of bits.

Note
May take/block a LONG time to search for one. 1024 bits can take >= 5 minutes.
break this into n threads to utilize more CPU power. this is just one thread.
Returns
Number of tries to get a prime.

◆ SetRandomBits()

HRESULT GrayLib::cBigUnsigned::SetRandomBits ( BIT_ENUM_t  nBits,
IRandomNoise pRandom = nullptr 
)

Fill this with random.

◆ SetRandomBits2()

HRESULT GrayLib::cBigUnsigned::SetRandomBits2 ( BIT_ENUM_t  nBits,
IRandomNoise pRandom = nullptr 
)

Fill this with random number of a desired size. Use SetBinary() to make sure the result is the same regardless of the platform endian-ness (ONLY useful if pRandom is actually deterministic, e.g. for tests).

◆ SetStr()

bool GrayLib::cBigUnsigned::SetStr ( const char *  pszVal,
RADIX_t  nBaseRadix = 10,
const char **  ppszEnd = (const char**) nullptr 
)

Set the value from a string.

Returns
true = set valid.

◆ TestPrime()

HRESULT GrayLib::cBigUnsigned::TestPrime ( IRandomNoise pRandom = nullptr) const

Pseudo-primality test: small factors, then Miller-Rabin

Returns
S_FALSE = NOT prime.
0 = S_OK = prime.

◆ TestPrimeFermat()

HRESULT GrayLib::cBigUnsigned::TestPrimeFermat ( IRandomNoise pRandom = nullptr) const

This function uses Fermat's (little) Theorem 100 times to test the primeness of a (large) positive integer. if p is prime then for any integer x, ( x^p - x ) will be evenly divisible by p. Small Primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 http://en.wikipedia.org/wiki/Primality_test

Note
this can be VERY slow for big numbers.

◆ TestPrimeMiller()

HRESULT GrayLib::cBigUnsigned::TestPrimeMiller ( IRandomNoise pRandom = nullptr) const

Miller-Rabin pseudo-primality test (HAC 4.24)

Returns
S_FALSE = NOT prime.
0 = S_OK = prime.

◆ TestPrimeSmall()

BITOP_TYPE GrayLib::cBigUnsigned::TestPrimeSmall ( ) const

Faster to test small divisors first (ASSUME unsigned)

Returns
TriState 0 = BITOP_CLR certain non-prime 1 = certain prime BITOP_TOGGLE = -1 = maybe prime. no small factor (possible prime?, more tests needed)

◆ UNITTEST_FRIEND()

GrayLib::cBigUnsigned::UNITTEST_FRIEND ( cBigUnsigned  )

The documentation for this class was generated from the following files: