VirtualBox

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

Last change on this file since 52290 was 52290, checked in by vboxsync, 11 years ago

RTBigNum: Two assembly optimizations related to RTBigNumModExp. Use 64-bit element type on 64-bit hosts (instead of 32-bit everywhere). Fixed some bugs in the bit operations, which apparently didn't affect x86 or AMD64.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 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. */
40#if ARCH_BITS == 64
41typedef uint64_t RTBIGNUMELEMENT;
42#else
43typedef uint32_t RTBIGNUMELEMENT;
44#endif
45/** The size (in bytes) of one array element. */
46#if ARCH_BITS == 64
47# define RTBIGNUM_ELEMENT_SIZE 8
48#else
49# define RTBIGNUM_ELEMENT_SIZE 4
50#endif
51/** The number of bits in one array element. */
52#define RTBIGNUM_ELEMENT_BITS (RTBIGNUM_ELEMENT_SIZE * 8)
53/** Returns the bitmask corrsponding to given bit number. */
54#if ARCH_BITS == 64
55# define RTBIGNUM_ELEMENT_BIT(iBit) RT_BIT_64(iBit)
56#else
57# define RTBIGNUM_ELEMENT_BIT(iBit) RT_BIT_32(iBit)
58#endif
59
60/**
61 * IPRT big integer number.
62 */
63typedef struct RTBIGNUM
64{
65 /** Elements array where the magnitue of the value is stored. */
66 RTBIGNUMELEMENT *pauElements;
67 /** The current number of elements we're using in the pauElements array. */
68 uint32_t cUsed;
69 /** The current allocation size of pauElements. */
70 uint32_t cAllocated;
71 /** Reserved for future use. */
72 uint32_t uReserved;
73
74 /** Set if it's a negative number, clear if positive or zero. */
75 uint32_t fNegative : 1;
76
77 /** Whether to use a the data is sensitive (RTBIGNUMINIT_F_SENSITIVE). */
78 uint32_t fSensitive : 1;
79 /** The number is currently scrambled */
80 uint32_t fCurScrambled : 1;
81
82 /** Bits reserved for future use. */
83 uint32_t fReserved : 30;
84} RTBIGNUM;
85
86
87RTDECL(int) RTBigNumInit(PRTBIGNUM pBigNum, uint32_t fFlags, void const *pvRaw, size_t cbRaw);
88RTDECL(int) RTBigNumInitZero(PRTBIGNUM pBigNum, uint32_t fFlags);
89
90/** @name RTBIGNUMINIT_F_XXX - RTBigNumInit flags.
91 * @{ */
92/** The number is sensitive so use a safer allocator, scramble it when not
93 * in use, and apply RTMemWipeThoroughly before freeing. The RTMemSafer API
94 * takes care of these things.
95 * @note When using this flag, concurrent access is not possible! */
96#define RTBIGNUMINIT_F_SENSITIVE RT_BIT(0)
97/** Big endian number. */
98#define RTBIGNUMINIT_F_ENDIAN_BIG RT_BIT(1)
99/** Little endian number. */
100#define RTBIGNUMINIT_F_ENDIAN_LITTLE RT_BIT(2)
101/** The raw number is unsigned. */
102#define RTBIGNUMINIT_F_UNSIGNED RT_BIT(3)
103/** The raw number is signed. */
104#define RTBIGNUMINIT_F_SIGNED RT_BIT(4)
105/** @} */
106
107RTDECL(int) RTBigNumClone(PRTBIGNUM pBigNum, PCRTBIGNUM pSrc);
108
109RTDECL(int) RTBigNumDestroy(PRTBIGNUM pBigNum);
110
111
112/**
113 * The minimum number of bits require store the two's complement representation
114 * of the number.
115 *
116 * @returns Width in number of bits.
117 * @param pBigNum The big number.
118 */
119RTDECL(uint32_t) RTBigNumBitWidth(PCRTBIGNUM pBigNum);
120RTDECL(uint32_t) RTBigNumByteWidth(PCRTBIGNUM pBigNum);
121
122
123/**
124 * Converts the big number to a sign-extended big endian byte sequence.
125 *
126 * @returns IPRT status code
127 * @retval VERR_BUFFER_OVERFLOW if the specified buffer is too small.
128 * @param pBigNum The big number.
129 * @param pvBuf The output buffer (size is at least cbWanted).
130 * @param cbWanted The number of bytes wanted.
131 */
132RTDECL(int) RTBigNumToBytesBigEndian(PCRTBIGNUM pBigNum, void *pvBuf, size_t cbWanted);
133
134/**
135 * Compares two numbers.
136 *
137 * @retval -1 if pLeft < pRight.
138 * @retval 0 if pLeft == pRight.
139 * @retval 1 if pLeft > pRight.
140 *
141 * @param pLeft The left side number.
142 * @param pRight The right side number.
143 */
144RTDECL(int) RTBigNumCompare(PRTBIGNUM pLeft, PRTBIGNUM pRight);
145RTDECL(int) RTBigNumCompareWithU64(PRTBIGNUM pLeft, uint64_t uRight);
146RTDECL(int) RTBigNumCompareWithS64(PRTBIGNUM pLeft, int64_t iRight);
147
148RTDECL(int) RTBigNumAssign(PRTBIGNUM pDst, PCRTBIGNUM pSrc);
149RTDECL(int) RTBigNumNegate(PRTBIGNUM pResult, PCRTBIGNUM pBigNum);
150RTDECL(int) RTBigNumNegateThis(PRTBIGNUM pThis);
151
152RTDECL(int) RTBigNumAdd(PRTBIGNUM pResult, PCRTBIGNUM pAugend, PCRTBIGNUM pAddend);
153RTDECL(int) RTBigNumSubtract(PRTBIGNUM pResult, PCRTBIGNUM pMinuend, PCRTBIGNUM pSubtrahend);
154RTDECL(int) RTBigNumMultiply(PRTBIGNUM pResult, PCRTBIGNUM pMultiplicand, PCRTBIGNUM pMultiplier);
155RTDECL(int) RTBigNumDivide(PRTBIGNUM pQuotient, PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
156RTDECL(int) RTBigNumModulo(PRTBIGNUM pRemainder, PCRTBIGNUM pDividend, PCRTBIGNUM pDivisor);
157RTDECL(int) RTBigNumExponentiate(PRTBIGNUM pResult, PCRTBIGNUM pBase, PCRTBIGNUM pExponent);
158
159RTDECL(int) RTBigNumModExp(PRTBIGNUM pResult, PRTBIGNUM pBase, PRTBIGNUM pExponent, PRTBIGNUM pModulus);
160
161
162/** @} */
163
164RT_C_DECLS_END
165
166#endif
167
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