pcpbnuarith.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_BNU_ARITH_H)
  32. #define _CP_BNU_ARITH_H
  33. #include "pcpbnuimpl.h"
  34. #include "pcpbnu32arith.h"
  35. BNU_CHUNK_T cpAdd_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, cpSize ns);
  36. BNU_CHUNK_T cpSub_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, cpSize ns);
  37. BNU_CHUNK_T cpInc_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, cpSize ns, BNU_CHUNK_T val);
  38. BNU_CHUNK_T cpDec_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, cpSize ns, BNU_CHUNK_T val);
  39. BNU_CHUNK_T cpAddMulDgt_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, cpSize ns, BNU_CHUNK_T val);
  40. BNU_CHUNK_T cpMulAdc_BNU_school(BNU_CHUNK_T* pR,
  41. const BNU_CHUNK_T* pA, cpSize nsA,
  42. const BNU_CHUNK_T* pB, cpSize nsB);
  43. __INLINE BNU_CHUNK_T cpMul_BNU_school(BNU_CHUNK_T* pR,
  44. const BNU_CHUNK_T* pA, cpSize nsA,
  45. const BNU_CHUNK_T* pB, cpSize nsB)
  46. {
  47. return cpMulAdc_BNU_school(pR, pA,nsA, pB,nsB);
  48. }
  49. BNU_CHUNK_T cpSqrAdc_BNU_school(BNU_CHUNK_T * pR, const BNU_CHUNK_T * pA, cpSize nsA);
  50. __INLINE BNU_CHUNK_T cpSqr_BNU_school(BNU_CHUNK_T * pR, const BNU_CHUNK_T * pA, cpSize nsA)
  51. {
  52. return cpSqrAdc_BNU_school(pR, pA,nsA);
  53. }
  54. BNU_CHUNK_T cpGcd_BNU(BNU_CHUNK_T a, BNU_CHUNK_T b);
  55. int cpModInv_BNU(BNU_CHUNK_T* pInv,
  56. const BNU_CHUNK_T* pA, cpSize nsA,
  57. const BNU_CHUNK_T* pM, cpSize nsM,
  58. BNU_CHUNK_T* bufInv, BNU_CHUNK_T* bufA, BNU_CHUNK_T* bufM);
  59. /*
  60. // multiplication/squaring wrappers
  61. */
  62. __INLINE BNU_CHUNK_T cpMul_BNU(BNU_CHUNK_T* pR,
  63. const BNU_CHUNK_T* pA, cpSize nsA,
  64. const BNU_CHUNK_T* pB, cpSize nsB,
  65. BNU_CHUNK_T* pBuffer)
  66. {
  67. UNREFERENCED_PARAMETER(pBuffer);
  68. return cpMul_BNU_school(pR, pA,nsA, pB,nsB);
  69. }
  70. __INLINE BNU_CHUNK_T cpSqr_BNU(BNU_CHUNK_T * pR,
  71. const BNU_CHUNK_T * pA, cpSize nsA,
  72. BNU_CHUNK_T* pBuffer)
  73. {
  74. UNREFERENCED_PARAMETER(pBuffer);
  75. return cpSqr_BNU_school(pR, pA,nsA);
  76. }
  77. /*
  78. // division/reduction wrappers
  79. */
  80. __INLINE cpSize cpDiv_BNU(BNU_CHUNK_T* pQ, cpSize* pnsQ, BNU_CHUNK_T* pA, cpSize nsA, BNU_CHUNK_T* pB, cpSize nsB)
  81. {
  82. int nsR = cpDiv_BNU32((Ipp32u*)pQ, pnsQ,
  83. (Ipp32u*)pA, nsA*(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u)),
  84. (Ipp32u*)pB, nsB*(sizeof(BNU_CHUNK_T)/sizeof(Ipp32u)));
  85. #if (BNU_CHUNK_BITS == BNU_CHUNK_64BIT)
  86. if(nsR&1) ((Ipp32u*)pA)[nsR] = 0;
  87. nsR = INTERNAL_BNU_LENGTH(nsR);
  88. if(pQ) {
  89. if(*pnsQ&1) ((Ipp32u*)pQ)[*pnsQ] = 0;
  90. *pnsQ = INTERNAL_BNU_LENGTH(*pnsQ);
  91. }
  92. #endif
  93. return nsR;
  94. }
  95. __INLINE cpSize cpMod_BNU(BNU_CHUNK_T* pX, cpSize nsX, BNU_CHUNK_T* pModulus, cpSize nsM)
  96. {
  97. return cpDiv_BNU(NULL,NULL, pX,nsX, pModulus, nsM);
  98. }
  99. #endif /* _CP_BNU_ARITH_H */