pcpeccpsscm.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include "owndefs.h"
  32. #include "owncp.h"
  33. #include "pcpbn.h"
  34. #include "pcpeccppoint.h"
  35. #define LOG2_CACHE_LINE_SIZE (LOG_CACHE_LINE_SIZE)
  36. static int div_upper(int a, int d)
  37. { return (a+d-1)/d; }
  38. static int getNumOperations(int bitsize, int w)
  39. {
  40. int n_overhead = (1<<w) -1;
  41. int n_ops = div_upper(bitsize, w) + n_overhead;
  42. return n_ops;
  43. }
  44. int cpECCP_OptimalWinSize(int bitsize)
  45. {
  46. #define LIMIT (LOG2_CACHE_LINE_SIZE)
  47. int w_opt = 1;
  48. int n_opt = getNumOperations(bitsize, w_opt);
  49. int w_trial;
  50. for(w_trial=w_opt+1; w_trial<=LIMIT; w_trial++) {
  51. int n_trial = getNumOperations(bitsize, w_trial);
  52. if(n_trial>=n_opt) break;
  53. w_opt = w_trial;
  54. n_opt = n_trial;
  55. }
  56. return w_opt;
  57. #undef LIMIT
  58. }
  59. int cpECCP_ConvertRepresentation(BNU_CHUNK_T* pInput, int inpBits, int w)
  60. {
  61. Ipp32u* pR = (Ipp32u*)pInput;
  62. Ipp16u* pR16 = (Ipp16u*)pInput;
  63. int outBits = 0;
  64. Ipp32u base = (BNU_CHUNK_T)1<<w;
  65. Ipp32u digitMask = base-1;
  66. int i;
  67. cpSize nsR = BITS2WORD32_SIZE(inpBits);
  68. pR[nsR] = 0; // expand 32-bit representation of input
  69. for(i=0; i<inpBits; i+=w) {
  70. cpSize chunkIdx = i/BITSIZE(Ipp16u);
  71. Ipp32u chunk = ((Ipp32u*)(pR16+chunkIdx))[0];
  72. int digitShift = i % BITSIZE(Ipp16u);
  73. Ipp32u digit = (chunk>>digitShift) &digitMask;
  74. Ipp32u delta = (base-digit) & ~digitMask;
  75. delta <<= digitShift;
  76. cpDec_BNU32((Ipp32u*)(pR16+chunkIdx), (Ipp32u*)(pR16+chunkIdx), (2*nsR-chunkIdx+1)/2, delta);
  77. inpBits = BITSIZE_BNU32(pR, nsR);
  78. outBits += w;
  79. }
  80. return outBits;
  81. }
  82. /*
  83. // cpsScramblePut/cpsScrambleGet
  84. // stores to/retrieves from pScrambleEntry position
  85. // pre-computed data if fixed window method is used
  86. */
  87. void cpECCP_ScramblePut(Ipp8u* pScrambleEntry, int proposity,
  88. const IppsECCPPointState* pPoint, cpSize coordLen)
  89. {
  90. int i;
  91. Ipp8u* pCoord;
  92. BNU_CHUNK_T* pX = BN_NUMBER(ECP_POINT_X(pPoint));
  93. BNU_CHUNK_T* pY = BN_NUMBER(ECP_POINT_Y(pPoint));
  94. BNU_CHUNK_T* pZ = BN_NUMBER(ECP_POINT_Z(pPoint));
  95. int coordSize = coordLen*sizeof(BNU_CHUNK_T);
  96. ZEXPAND_BNU(pX, BN_SIZE(ECP_POINT_X(pPoint)), coordLen);
  97. ZEXPAND_BNU(pY, BN_SIZE(ECP_POINT_Y(pPoint)), coordLen);
  98. ZEXPAND_BNU(pZ, BN_SIZE(ECP_POINT_Z(pPoint)), coordLen);
  99. pCoord = (Ipp8u*)pX;
  100. for(i=0; i<coordSize; i++, pScrambleEntry+=proposity)
  101. *pScrambleEntry = pCoord[i];
  102. pCoord = (Ipp8u*)pY;
  103. for(i=0; i<coordSize; i++, pScrambleEntry+=proposity)
  104. *pScrambleEntry = pCoord[i];
  105. pCoord = (Ipp8u*)pZ;
  106. for(i=0; i<coordSize; i++, pScrambleEntry+=proposity)
  107. *pScrambleEntry = pCoord[i];
  108. }
  109. void cpECCP_ScrambleGet(IppsECCPPointState* pPoint, cpSize coordLen,
  110. const Ipp8u* pScrambleEntry, int proposity)
  111. {
  112. BNU_CHUNK_T* pX = BN_NUMBER(ECP_POINT_X(pPoint));
  113. BNU_CHUNK_T* pY = BN_NUMBER(ECP_POINT_Y(pPoint));
  114. BNU_CHUNK_T* pZ = BN_NUMBER(ECP_POINT_Z(pPoint));
  115. int coordSize = coordLen*sizeof(BNU_CHUNK_T);
  116. int i;
  117. Ipp8u* pCoord = (Ipp8u*)pX;
  118. for(i=0; i<coordSize; i++, pScrambleEntry+=proposity)
  119. pCoord[i] = *pScrambleEntry;
  120. pCoord = (Ipp8u*)pY;
  121. for(i=0; i<coordSize; i++, pScrambleEntry+=proposity)
  122. pCoord[i] = *pScrambleEntry;
  123. pCoord = (Ipp8u*)pZ;
  124. for(i=0; i<coordSize; i++, pScrambleEntry+=proposity)
  125. pCoord[i] = *pScrambleEntry;
  126. i = coordLen;
  127. FIX_BNU(pX, i);
  128. BN_SIZE(ECP_POINT_X(pPoint)) = i;
  129. i = coordLen;
  130. FIX_BNU(pY, i);
  131. BN_SIZE(ECP_POINT_Y(pPoint)) = i;
  132. i = coordLen;
  133. FIX_BNU(pZ, i);
  134. BN_SIZE(ECP_POINT_Z(pPoint)) = i;
  135. }