bignum.h revision 13493ab7596e827b8d0caab2c89e635dd65f78f9
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT - Big Integer Numbers.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2006-2014 Oracle Corporation
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * available from http://www.virtualbox.org. This file is free software;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * General Public License (GPL) as published by the Free Software
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The contents of this file may alternatively be used under the terms
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * of the Common Development and Distribution License Version 1.0
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution, in which case the provisions of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * CDDL are applicable instead of those of the GPL.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * You may elect to license modified versions of this file under the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * terms and conditions of either the GPL or the CDDL or both.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @defgroup grp_rtbignum RTBigNum - Big Integer Numbers
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @ingroup grp_rt
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The big integer number element type. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The size (in bytes) of one array element. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The number of bits in one array element. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define RTBIGNUM_ELEMENT_BITS (RTBIGNUM_ELEMENT_SIZE * 8)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Returns the bitmask corrsponding to given bit number. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT big integer number.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsynctypedef struct RTBIGNUM
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Elements array where the magnitue of the value is stored. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The current number of elements we're using in the pauElements array. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The current allocation size of pauElements. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Reserved for future use. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Set if it's a negative number, clear if positive or zero. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Whether to use a the data is sensitive (RTBIGNUMINIT_F_SENSITIVE). */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The number is currently scrambled */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Bits reserved for future use. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumInit(PRTBIGNUM pBigNum, uint32_t fFlags, void const *pvRaw, size_t cbRaw);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumInitZero(PRTBIGNUM pBigNum, uint32_t fFlags);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @name RTBIGNUMINIT_F_XXX - RTBigNumInit flags.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The number is sensitive so use a safer allocator, scramble it when not
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * in use, and apply RTMemWipeThoroughly before freeing. The RTMemSafer API
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * takes care of these things.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @note When using this flag, concurrent access is not possible! */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Big endian number. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Little endian number. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The raw number is unsigned. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The raw number is signed. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumClone(PRTBIGNUM pBigNum, PCRTBIGNUM pSrc);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The minimum number of bits require store the two's complement representation
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * of the number.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns Width in number of bits.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pBigNum The big number.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(uint32_t) RTBigNumBitWidth(PCRTBIGNUM pBigNum);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(uint32_t) RTBigNumByteWidth(PCRTBIGNUM pBigNum);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Converts the big number to a sign-extended big endian byte sequence.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @returns IPRT status code
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @retval VERR_BUFFER_OVERFLOW if the specified buffer is too small.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pBigNum The big number.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pvBuf The output buffer (size is at least cbWanted).
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param cbWanted The number of bytes wanted.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumToBytesBigEndian(PCRTBIGNUM pBigNum, void *pvBuf, size_t cbWanted);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Compares two numbers.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @retval -1 if pLeft < pRight.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @retval 0 if pLeft == pRight.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @retval 1 if pLeft > pRight.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pLeft The left side number.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * @param pRight The right side number.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumCompare(PRTBIGNUM pLeft, PRTBIGNUM pRight);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumCompareWithU64(PRTBIGNUM pLeft, uint64_t uRight);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumCompareWithS64(PRTBIGNUM pLeft, int64_t iRight);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumAssign(PRTBIGNUM pDst, PCRTBIGNUM pSrc);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumNegate(PRTBIGNUM pResult, PCRTBIGNUM pBigNum);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumAdd(PRTBIGNUM pResult, PCRTBIGNUM pAugend, PCRTBIGNUM pAddend);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumSubtract(PRTBIGNUM pResult, PCRTBIGNUM pMinuend, PCRTBIGNUM pSubtrahend);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumMultiply(PRTBIGNUM pResult, PCRTBIGNUM pMultiplicand, PCRTBIGNUM pMultiplier);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumDivide(PRTBIGNUM pQuotient, PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumModulo(PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTBigNumExponentiate(PRTBIGNUM pResult, PCRTBIGNUM pBase, PCRTBIGNUM pExponent);