1 | /** @file
|
---|
2 | * IPRT - Big Integer Numbers.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2014 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | #ifndef ___iprt_bignum_h
|
---|
28 | #define ___iprt_bignum_h
|
---|
29 |
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 | RT_C_DECLS_BEGIN
|
---|
33 |
|
---|
34 | /** @defgroup grp_rtbignum RTBigNum - Big Integer Numbers
|
---|
35 | * @ingroup grp_rt
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 | /** The big integer number element type. */
|
---|
40 | typedef uint32_t RTBIGNUMELEMENT;
|
---|
41 | /** The size (in bytes) of one array element. */
|
---|
42 | #define RTBIGNUM_ELEMENT_SIZE 4
|
---|
43 | /** The number of bits in one array element. */
|
---|
44 | #define RTBIGNUM_ELEMENT_BITS (RTBIGNUM_ELEMENT_SIZE * 8)
|
---|
45 | /** Returns the bitmask corrsponding to given bit number. */
|
---|
46 | #define RTBIGNUM_ELEMENT_BIT(iBit) RT_BIT_32(iBit)
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * IPRT big integer number.
|
---|
50 | */
|
---|
51 | typedef struct RTBIGNUM
|
---|
52 | {
|
---|
53 | /** Elements array where the magnitue of the value is stored. */
|
---|
54 | RTBIGNUMELEMENT *pauElements;
|
---|
55 | /** The current number of elements we're using in the pauElements array. */
|
---|
56 | uint32_t cUsed;
|
---|
57 | /** The current allocation size of pauElements. */
|
---|
58 | uint32_t cAllocated;
|
---|
59 | /** Reserved for future use. */
|
---|
60 | uint32_t uReserved;
|
---|
61 |
|
---|
62 | /** Set if it's a negative number, clear if positive or zero. */
|
---|
63 | uint32_t fNegative : 1;
|
---|
64 |
|
---|
65 | /** Whether to use a the data is sensitive (RTBIGNUMINIT_F_SENSITIVE). */
|
---|
66 | uint32_t fSensitive : 1;
|
---|
67 | /** The number is currently scrambled */
|
---|
68 | uint32_t fCurScrambled : 1;
|
---|
69 |
|
---|
70 | /** Bits reserved for future use. */
|
---|
71 | uint32_t fReserved : 30;
|
---|
72 | } RTBIGNUM;
|
---|
73 |
|
---|
74 |
|
---|
75 | RTDECL(int) RTBigNumInit(PRTBIGNUM pBigNum, uint32_t fFlags, void const *pvRaw, size_t cbRaw);
|
---|
76 | RTDECL(int) RTBigNumInitZero(PRTBIGNUM pBigNum, uint32_t fFlags);
|
---|
77 |
|
---|
78 | /** @name RTBIGNUMINIT_F_XXX - RTBigNumInit flags.
|
---|
79 | * @{ */
|
---|
80 | /** The number is sensitive so use a safer allocator, scramble it when not
|
---|
81 | * in use, and apply RTMemWipeThoroughly before freeing. The RTMemSafer API
|
---|
82 | * takes care of these things.
|
---|
83 | * @note When using this flag, concurrent access is not possible! */
|
---|
84 | #define RTBIGNUMINIT_F_SENSITIVE RT_BIT(0)
|
---|
85 | /** Big endian number. */
|
---|
86 | #define RTBIGNUMINIT_F_ENDIAN_BIG RT_BIT(1)
|
---|
87 | /** Little endian number. */
|
---|
88 | #define RTBIGNUMINIT_F_ENDIAN_LITTLE RT_BIT(2)
|
---|
89 | /** The raw number is unsigned. */
|
---|
90 | #define RTBIGNUMINIT_F_UNSIGNED RT_BIT(3)
|
---|
91 | /** The raw number is signed. */
|
---|
92 | #define RTBIGNUMINIT_F_SIGNED RT_BIT(4)
|
---|
93 | /** @} */
|
---|
94 |
|
---|
95 | RTDECL(int) RTBigNumClone(PRTBIGNUM pBigNum, PCRTBIGNUM pSrc);
|
---|
96 |
|
---|
97 | RTDECL(int) RTBigNumDestroy(PRTBIGNUM pBigNum);
|
---|
98 |
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * The minimum number of bits require store the two's complement representation
|
---|
102 | * of the number.
|
---|
103 | *
|
---|
104 | * @returns Width in number of bits.
|
---|
105 | * @param pBigNum The big number.
|
---|
106 | */
|
---|
107 | RTDECL(uint32_t) RTBigNumBitWidth(PCRTBIGNUM pBigNum);
|
---|
108 | RTDECL(uint32_t) RTBigNumByteWidth(PCRTBIGNUM pBigNum);
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Converts the big number to a sign-extended big endian byte sequence.
|
---|
113 | *
|
---|
114 | * @returns IPRT status code
|
---|
115 | * @retval VERR_BUFFER_OVERFLOW if the specified buffer is too small.
|
---|
116 | * @param pBigNum The big number.
|
---|
117 | * @param pvBuf The output buffer (size is at least cbWanted).
|
---|
118 | * @param cbWanted The number of bytes wanted.
|
---|
119 | */
|
---|
120 | RTDECL(int) RTBigNumToBytesBigEndian(PCRTBIGNUM pBigNum, void *pvBuf, size_t cbWanted);
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Compares two numbers.
|
---|
124 | *
|
---|
125 | * @retval -1 if pLeft < pRight.
|
---|
126 | * @retval 0 if pLeft == pRight.
|
---|
127 | * @retval 1 if pLeft > pRight.
|
---|
128 | *
|
---|
129 | * @param pLeft The left side number.
|
---|
130 | * @param pRight The right side number.
|
---|
131 | */
|
---|
132 | RTDECL(int) RTBigNumCompare(PRTBIGNUM pLeft, PRTBIGNUM pRight);
|
---|
133 | RTDECL(int) RTBigNumCompareWithU64(PRTBIGNUM pLeft, uint64_t uRight);
|
---|
134 | RTDECL(int) RTBigNumCompareWithS64(PRTBIGNUM pLeft, int64_t iRight);
|
---|
135 |
|
---|
136 | RTDECL(int) RTBigNumAssign(PRTBIGNUM pDst, PCRTBIGNUM pSrc);
|
---|
137 | RTDECL(int) RTBigNumNegate(PRTBIGNUM pResult, PCRTBIGNUM pBigNum);
|
---|
138 | RTDECL(int) RTBigNumNegateThis(PRTBIGNUM pThis);
|
---|
139 |
|
---|
140 | RTDECL(int) RTBigNumAdd(PRTBIGNUM pResult, PCRTBIGNUM pAugend, PCRTBIGNUM pAddend);
|
---|
141 | RTDECL(int) RTBigNumSubtract(PRTBIGNUM pResult, PCRTBIGNUM pMinuend, PCRTBIGNUM pSubtrahend);
|
---|
142 | RTDECL(int) RTBigNumMultiply(PRTBIGNUM pResult, PCRTBIGNUM pMultiplicand, PCRTBIGNUM pMultiplier);
|
---|
143 | RTDECL(int) RTBigNumDivide(PRTBIGNUM pQuotient, PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
|
---|
144 | RTDECL(int) RTBigNumModulo(PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
|
---|
145 | RTDECL(int) RTBigNumExponentiate(PRTBIGNUM pResult, PCRTBIGNUM pBase, PCRTBIGNUM pExponent);
|
---|
146 |
|
---|
147 | RTDECL(int) RTBigNumModExp(PRTBIGNUM pResult, PRTBIGNUM pBase, PRTBIGNUM pExponent, PRTBIGNUM pModulus);
|
---|
148 |
|
---|
149 |
|
---|
150 | /** @} */
|
---|
151 |
|
---|
152 | RT_C_DECLS_END
|
---|
153 |
|
---|
154 | #endif
|
---|
155 |
|
---|