pcpgfpxstuff.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*
  17. // Intel(R) Integrated Performance Primitives
  18. // Cryptographic Primitives (ippCP)
  19. // GF(p) extension internal
  20. //
  21. */
  22. #if !defined(_PCP_GFPEXT_H_)
  23. #define _PCP_GFPEXT_H_
  24. #include "pcpgfpstuff.h"
  25. #define _EXTENSION_2_BINOMIAL_SUPPORT_
  26. #define _EXTENSION_3_BINOMIAL_SUPPORT_
  27. #if defined(_EXTENSION_2_BINOMIAL_SUPPORT_) && defined(_EXTENSION_3_BINOMIAL_SUPPORT_)
  28. /* Intel(R) EPID specific:
  29. (Fq2) GF(q^2) generating polynomial is g(t) = t^2 + beta, beta = 1
  30. */
  31. #define _EPID20_GF_PARAM_SPECIFIC_
  32. #endif
  33. /* GF(p^d) pool */
  34. #define GFPX_PESIZE(pGF) GFP_FELEN((pGF))
  35. #define GFPX_POOL_SIZE (14) //(8) /* Number of temporary variables in pool */
  36. /* address of ground field element inside expanded field element */
  37. #define GFPX_IDX_ELEMENT(pxe, idx, eleSize) ((pxe)+(eleSize)*(idx))
  38. #if 0
  39. /* internal function prototypes */
  40. __INLINE BNU_CHUNK_T* cpGFpxGetPool(int n, IppsGFpState* pGFpx)
  41. {
  42. BNU_CHUNK_T* pPool = GFP_POOL(pGFpx);
  43. GFP_POOL(pGFpx) += n*GFPX_PESIZE(pGFpx);
  44. return pPool;
  45. }
  46. __INLINE void cpGFpxReleasePool(int n, IppsGFpState* pGFpx)
  47. {
  48. GFP_POOL(pGFpx) -= n * GFPX_PESIZE(pGFpx);
  49. }
  50. #endif
  51. __INLINE int degree(const BNU_CHUNK_T* pE, const IppsGFpState* pGFpx)
  52. {
  53. int groundElemLen = GFP_FELEN( (IppsGFpState*)GFP_GROUNDGF(pGFpx) );
  54. int deg;
  55. for(deg=GFP_DEGREE(pGFpx)-1; deg>=0; deg-- ) {
  56. if(!GFP_IS_ZERO(pE+groundElemLen*deg, groundElemLen)) break;
  57. }
  58. return deg;
  59. }
  60. __INLINE IppsGFpState* cpGFpBasic(const IppsGFpState* pGFp)
  61. {
  62. while( !GFP_IS_BASIC(pGFp) ) {
  63. pGFp = GFP_GROUNDGF(pGFp);
  64. }
  65. return (IppsGFpState*)pGFp;
  66. }
  67. __INLINE int cpGFpBasicDegreeExtension(const IppsGFpState* pGFp)
  68. {
  69. int degree = GFP_DEGREE(pGFp);
  70. while( !GFP_IS_BASIC(pGFp) ) {
  71. IppsGFpState* pGroundGF = GFP_GROUNDGF(pGFp);
  72. degree *= GFP_DEGREE(pGroundGF);
  73. pGFp = pGroundGF;
  74. }
  75. return degree;
  76. }
  77. /* convert external data (Ipp32u) => internal element (BNU_CHUNK_T) representation
  78. returns length of element (in BNU_CHUNK_T)
  79. */
  80. __INLINE int cpGFpxCopyToChunk(BNU_CHUNK_T* pElm, const Ipp32u* pA, int nsA, const IppsGFpState* pGFpx)
  81. {
  82. IppsGFpState* pBasicGF = cpGFpBasic(pGFpx);
  83. int basicExtension = cpGFpBasicDegreeExtension(pGFpx);
  84. int basicElmLen32 = GFP_FELEN32(pBasicGF);
  85. int basicElmLen = GFP_FELEN(pBasicGF);
  86. int deg;
  87. for(deg=0; deg<basicExtension && nsA>0; deg++, nsA -= basicElmLen32) {
  88. int srcLen = IPP_MIN(nsA, basicElmLen32);
  89. ZEXPAND_COPY_BNU((Ipp32u*)pElm, basicElmLen*(int)(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u)), pA,srcLen);
  90. pElm += basicElmLen;
  91. pA += basicElmLen32;
  92. }
  93. return basicElmLen*deg;
  94. }
  95. /* convert internal element (BNU_CHUNK_T) => external data (Ipp32u) representation
  96. returns length of data (in Ipp32u)
  97. */
  98. __INLINE int cpGFpxCopyFromChunk(Ipp32u* pA, const BNU_CHUNK_T* pElm, const IppsGFpState* pGFpx)
  99. {
  100. IppsGFpState* pBasicGF = cpGFpBasic(pGFpx);
  101. int basicExtension = cpGFpBasicDegreeExtension(pGFpx);
  102. int basicElmLen32 = GFP_FELEN32(pBasicGF);
  103. int basicElmLen = GFP_FELEN(pBasicGF);
  104. int deg;
  105. for(deg=0; deg<basicExtension; deg++) {
  106. COPY_BNU(pA, (Ipp32u*)pElm, basicElmLen32);
  107. pA += basicElmLen32;
  108. pElm += basicElmLen;
  109. }
  110. return basicElmLen32*deg;
  111. }
  112. /*
  113. // cpScramblePut/cpScrambleGet
  114. // stores to/retrieves from pScrambleEntry position
  115. // pre-computed data if fixed window method is used
  116. */
  117. __INLINE void cpScramblePut(Ipp8u* pScrambleEntry, int scale, const Ipp8u* pData, int dataSize)
  118. {
  119. int i;
  120. for(i=0; i<dataSize; i++)
  121. pScrambleEntry[i*scale] = pData[i];
  122. }
  123. __INLINE void cpScrambleGet(Ipp8u* pData, int dataSize, const Ipp8u* pScrambleEntry, int scale)
  124. {
  125. int i;
  126. for(i=0; i<dataSize; i++)
  127. pData[i] = pScrambleEntry[i*scale];
  128. }
  129. int cpGFpxCompare(const IppsGFpState* pGFpx1, const IppsGFpState* pGFpx2);
  130. BNU_CHUNK_T* cpGFpxRand(BNU_CHUNK_T* pR, IppsGFpState* pGFpx, IppBitSupplier rndFunc, void* pRndParam, int montSpace);
  131. BNU_CHUNK_T* cpGFpxSet (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pDataA, int nsA, IppsGFpState* pGFpx, int montSpace);
  132. BNU_CHUNK_T* cpGFpxGet (BNU_CHUNK_T* pDataA, int nsA, const BNU_CHUNK_T* pR, IppsGFpState* pGFpx, int montSpace);
  133. BNU_CHUNK_T* cpGFpxSetPolyTerm (BNU_CHUNK_T* pR, int deg, const BNU_CHUNK_T* pDataA, int nsA, IppsGFpState* pGFpx, int montSpace);
  134. BNU_CHUNK_T* cpGFpxGetPolyTerm (BNU_CHUNK_T* pDataA, int nsA, const BNU_CHUNK_T* pR, int deg, IppsGFpState* pGFpx, int montSpace);
  135. BNU_CHUNK_T* cpGFpxAdd (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, IppsGFpState* pGFpx);
  136. BNU_CHUNK_T* cpGFpxSub (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, IppsGFpState* pGFpx);
  137. BNU_CHUNK_T* cpGFpxMul (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, IppsGFpState* pGFpx);
  138. BNU_CHUNK_T* cpGFpxSqr (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, IppsGFpState* pGFpx);
  139. BNU_CHUNK_T* cpGFpxAdd_GFE (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pGroundB, IppsGFpState* pGFpx);
  140. BNU_CHUNK_T* cpGFpxSub_GFE (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pGroundB, IppsGFpState* pGFpx);
  141. BNU_CHUNK_T* cpGFpxMul_GFE (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pGroundB, IppsGFpState* pGFpx);
  142. int cpGFpGetOptimalWinSize(int bitsize);
  143. BNU_CHUNK_T* cpGFpxExp (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pE, int nsE, IppsGFpState* pGFpx, Ipp8u* pScratchBuffer);
  144. BNU_CHUNK_T* cpGFpxMultiExp(BNU_CHUNK_T* pR, const BNU_CHUNK_T* ppA[], const BNU_CHUNK_T* ppE[], int nsE[], int nItems,
  145. IppsGFpState* pGFpx, Ipp8u* pScratchBuffer);
  146. BNU_CHUNK_T* cpGFpxConj(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, IppsGFpState* pGFpx);
  147. BNU_CHUNK_T* cpGFpxNeg (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, IppsGFpState* pGFpx);
  148. BNU_CHUNK_T* cpGFpxInv (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, IppsGFpState* pGFpx);
  149. BNU_CHUNK_T* cpGFpxHalve (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, IppsGFpState* pGFpx);
  150. //BNU_CHUNK_T* gfpolyDiv(BNU_CHUNK_T* pQ, BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, IppsGFpState* pGFpx);
  151. #endif /* _PCP_GFPEXT_H_ */