pcpbn.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #if !defined(_CP_BN_H)
  32. #define _CP_BN_H
  33. #include "pcpbnuimpl.h"
  34. #include "pcpbnuarith.h"
  35. #include "pcpbnumisc.h"
  36. #include "pcpbnu32arith.h"
  37. #include "pcpbnu32misc.h"
  38. /*
  39. // Big Number context
  40. */
  41. struct _cpBigNum
  42. {
  43. IppCtxId idCtx; /* BigNum ctx id */
  44. IppsBigNumSGN sgn; /* sign */
  45. cpSize size; /* BigNum size (BNU_CHUNK_T) */
  46. cpSize room; /* BigNum max size (BNU_CHUNK_T) */
  47. BNU_CHUNK_T* number; /* BigNum value */
  48. BNU_CHUNK_T* buffer; /* temporary buffer */
  49. };
  50. /* BN accessory macros */
  51. #define BN_ID(pBN) ((pBN)->idCtx)
  52. #define BN_SIGN(pBN) ((pBN)->sgn)
  53. #define BN_POSITIVE(pBN) (BN_SIGN(pBN)==ippBigNumPOS)
  54. #define BN_NEGATIVE(pBN) (BN_SIGN(pBN)==ippBigNumNEG)
  55. #define BN_NUMBER(pBN) ((pBN)->number)
  56. #define BN_BUFFER(pBN) ((pBN)->buffer)
  57. #define BN_ROOM(pBN) ((pBN)->room)
  58. #define BN_SIZE(pBN) ((pBN)->size)
  59. #define BN_SIZE32(pBN) ((pBN)->size*(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u)))
  60. //#define BN_SIZE32(pBN) (BITS2WORD32_SIZE( BITSIZE_BNU(BN_NUMBER((pBN)),BN_SIZE((pBN)))))
  61. #define BN_VALID_ID(pBN) (BN_ID((pBN))==idCtxBigNum)
  62. #define INVERSE_SIGN(s) (((s)==ippBigNumPOS)? ippBigNumNEG : ippBigNumPOS)
  63. #define BN_ALIGNMENT ((int)sizeof(void*))
  64. /* pack-unpack context */
  65. void cpPackBigNumCtx(const IppsBigNumState* pBN, Ipp8u* pBuffer);
  66. void cpUnpackBigNumCtx(const Ipp8u* pBuffer, IppsBigNumState* pBN);
  67. /* copy BN */
  68. __INLINE IppsBigNumState* cpBN_copy(IppsBigNumState* pDst, const IppsBigNumState* pSrc)
  69. {
  70. BN_SIGN(pDst) = BN_SIGN(pSrc);
  71. BN_SIZE(pDst) = BN_SIZE(pSrc);
  72. ZEXPAND_COPY_BNU(BN_NUMBER(pDst), BN_ROOM(pDst), BN_NUMBER(pSrc), BN_SIZE(pSrc));
  73. return pDst;
  74. }
  75. /* set BN to zero */
  76. __INLINE IppsBigNumState* cpBN_zero(IppsBigNumState* pBN)
  77. {
  78. BN_SIGN(pBN) = ippBigNumPOS;
  79. BN_SIZE(pBN) = 1;
  80. ZEXPAND_BNU(BN_NUMBER(pBN),0, (int)BN_ROOM(pBN));
  81. return pBN;
  82. }
  83. /* fixup BN */
  84. __INLINE IppsBigNumState* cpBN_fix(IppsBigNumState* pBN)
  85. {
  86. cpSize len = BN_SIZE(pBN);
  87. FIX_BNU(BN_NUMBER(pBN), len);
  88. BN_SIZE(pBN) = len;
  89. return pBN;
  90. }
  91. /* set BN to chunk */
  92. __INLINE IppsBigNumState* cpBN_chunk(IppsBigNumState* pBN, BNU_CHUNK_T a)
  93. {
  94. BN_SIGN(pBN) = ippBigNumPOS;
  95. BN_SIZE(pBN) = 1;
  96. ZEXPAND_BNU(BN_NUMBER(pBN),0, (int)BN_ROOM(pBN));
  97. BN_NUMBER(pBN)[0] = a;
  98. return pBN;
  99. }
  100. /* set BN to 2^m */
  101. __INLINE IppsBigNumState* cpBN_power2(IppsBigNumState* pBN, int power)
  102. {
  103. cpSize size = BITS_BNU_CHUNK(power+1);
  104. if(BN_ROOM(pBN) >= size) {
  105. BN_SIGN(pBN) = ippBigNumPOS;
  106. BN_SIZE(pBN) = size;
  107. ZEXPAND_BNU(BN_NUMBER(pBN),0, BN_ROOM(pBN));
  108. SET_BIT(BN_NUMBER(pBN), power);
  109. return pBN;
  110. }
  111. else return NULL;
  112. }
  113. /* bitsize of BN */
  114. __INLINE int cpBN_bitsize(const IppsBigNumState* pA)
  115. {
  116. int bitsize = BITSIZE_BNU(BN_NUMBER(pA), BN_SIZE(pA));
  117. return bitsize;
  118. }
  119. /* returns -1/0/+1 depemding on A~B comparison */
  120. __INLINE int cpBN_cmp(const IppsBigNumState* pA, const IppsBigNumState* pB)
  121. {
  122. IppsBigNumSGN signA = BN_SIGN(pA);
  123. IppsBigNumSGN signB = BN_SIGN(pB);
  124. if(signA==signB) {
  125. int result = cpCmp_BNU(BN_NUMBER(pA), BN_SIZE(pA), BN_NUMBER(pB), BN_SIZE(pB));
  126. return (ippBigNumPOS==signA)? result : -result;
  127. }
  128. return (ippBigNumPOS==signA)? 1 : -1;
  129. }
  130. /* returns -1/0/+1 depemding on A comparison 0</==0/>0 */
  131. __INLINE int cpBN_tst(const IppsBigNumState* pA)
  132. {
  133. if(1==BN_SIZE(pA) && 0==BN_NUMBER(pA)[0])
  134. return 0;
  135. else
  136. return BN_POSITIVE(pA)? 1 : -1;
  137. }
  138. // some addtition functions
  139. __INLINE int IsZero_BN(const IppsBigNumState* pA)
  140. {
  141. return ( BN_SIZE(pA)==1 ) && ( BN_NUMBER(pA)[0]==0 );
  142. }
  143. __INLINE int IsOdd_BN(const IppsBigNumState* pA)
  144. {
  145. return BN_NUMBER(pA)[0] & 1;
  146. }
  147. __INLINE IppsBigNumState* BN_Word(IppsBigNumState* pBN, BNU_CHUNK_T w)
  148. {
  149. BN_SIGN(pBN) = ippBigNumPOS;
  150. BN_SIZE(pBN) = 1;
  151. ZEXPAND_BNU(BN_NUMBER(pBN),0, BN_ROOM(pBN));
  152. BN_NUMBER(pBN)[0] = w;
  153. return pBN;
  154. }
  155. __INLINE IppsBigNumState* BN_Set(const BNU_CHUNK_T* pData, cpSize len, IppsBigNumState* pBN)
  156. {
  157. BN_SIGN(pBN) = ippBigNumPOS;
  158. BN_SIZE(pBN) = len;
  159. ZEXPAND_COPY_BNU(BN_NUMBER(pBN), BN_ROOM(pBN), pData, len);
  160. return pBN;
  161. }
  162. __INLINE IppsBigNumState* BN_Make(BNU_CHUNK_T* pData, BNU_CHUNK_T* pBuffer, cpSize len, IppsBigNumState* pBN)
  163. {
  164. BN_ID(pBN) = idCtxBigNum;
  165. BN_SIGN(pBN) = ippBigNumPOS;
  166. BN_SIZE(pBN) = 1;
  167. BN_ROOM(pBN) = len;
  168. BN_NUMBER(pBN) = pData;
  169. BN_BUFFER(pBN) = pBuffer;
  170. return pBN;
  171. }
  172. /*
  173. // fixed single chunk BN
  174. */
  175. typedef struct _ippcpBigNumChunk {
  176. IppsBigNumState bn;
  177. BNU_CHUNK_T value;
  178. BNU_CHUNK_T temporary;
  179. } IppsBigNumStateChunk;
  180. /* reference to BN(1) and BN(2) */
  181. IppsBigNumState* cpBN_OneRef(void);
  182. IppsBigNumState* cpBN_TwoRef(void);
  183. IppsBigNumState* cpBN_ThreeRef(void);
  184. #define BN_ONE_REF() cpBN_OneRef()
  185. #define BN_TWO_REF() cpBN_TwoRef()
  186. #define BN_THREE_REF() cpBN_ThreeRef()
  187. #endif /* _CP_BN_H */