VirtualBox

source: vbox/trunk/include/iprt/bignum.h@ 52225

Last change on this file since 52225 was 51770, checked in by vboxsync, 10 years ago

Merged in iprt++ dev branch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
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
32RT_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. */
40typedef 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 */
51typedef 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
75RTDECL(int) RTBigNumInit(PRTBIGNUM pBigNum, uint32_t fFlags, void const *pvRaw, size_t cbRaw);
76RTDECL(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
95RTDECL(int) RTBigNumClone(PRTBIGNUM pBigNum, PCRTBIGNUM pSrc);
96
97RTDECL(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 */
107RTDECL(uint32_t) RTBigNumBitWidth(PCRTBIGNUM pBigNum);
108RTDECL(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 */
120RTDECL(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 */
132RTDECL(int) RTBigNumCompare(PRTBIGNUM pLeft, PRTBIGNUM pRight);
133RTDECL(int) RTBigNumCompareWithU64(PRTBIGNUM pLeft, uint64_t uRight);
134RTDECL(int) RTBigNumCompareWithS64(PRTBIGNUM pLeft, int64_t iRight);
135
136RTDECL(int) RTBigNumAssign(PRTBIGNUM pDst, PCRTBIGNUM pSrc);
137RTDECL(int) RTBigNumNegate(PRTBIGNUM pResult, PCRTBIGNUM pBigNum);
138RTDECL(int) RTBigNumNegateThis(PRTBIGNUM pThis);
139
140RTDECL(int) RTBigNumAdd(PRTBIGNUM pResult, PCRTBIGNUM pAugend, PCRTBIGNUM pAddend);
141RTDECL(int) RTBigNumSubtract(PRTBIGNUM pResult, PCRTBIGNUM pMinuend, PCRTBIGNUM pSubtrahend);
142RTDECL(int) RTBigNumMultiply(PRTBIGNUM pResult, PCRTBIGNUM pMultiplicand, PCRTBIGNUM pMultiplier);
143RTDECL(int) RTBigNumDivide(PRTBIGNUM pQuotient, PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
144RTDECL(int) RTBigNumModulo(PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
145RTDECL(int) RTBigNumExponentiate(PRTBIGNUM pResult, PCRTBIGNUM pBase, PCRTBIGNUM pExponent);
146
147RTDECL(int) RTBigNumModExp(PRTBIGNUM pResult, PRTBIGNUM pBase, PRTBIGNUM pExponent, PRTBIGNUM pModulus);
148
149
150/** @} */
151
152RT_C_DECLS_END
153
154#endif
155
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette