pcpmontexpbinca.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "pcpmontgomery.h"
  35. /*
  36. // Binary method of Exponentiation
  37. */
  38. cpSize cpMontExpBin_BNU(BNU_CHUNK_T* dataY,
  39. const BNU_CHUNK_T* dataX, cpSize nsX,
  40. const BNU_CHUNK_T* dataE, cpSize nsE,
  41. IppsMontState* pMont)
  42. {
  43. cpSize nsM = MNT_SIZE(pMont);
  44. /*
  45. // test for special cases:
  46. // x^0 = 1
  47. // 0^e = 0
  48. */
  49. if( cpEqu_BNU_CHUNK(dataE, nsE, 0) ) {
  50. COPY_BNU(dataY, MNT_1(pMont), nsM);
  51. }
  52. else if( cpEqu_BNU_CHUNK(dataX, nsX, 0) ) {
  53. ZEXPAND_BNU(dataY, 0, nsM);
  54. }
  55. /* general case */
  56. else {
  57. BNU_CHUNK_T* dataM = MNT_MODULUS(pMont);
  58. BNU_CHUNK_T m0 = MNT_HELPER(pMont);
  59. /* Montgomery engine buffers */
  60. BNU_CHUNK_T* pKBuffer = MNT_KBUFFER(pMont);
  61. BNU_CHUNK_T* pProduct = MNT_PRODUCT(pMont);
  62. BNU_CHUNK_T* dataT = MNT_TBUFFER(pMont);
  63. /* execute most significant part pE */
  64. BNU_CHUNK_T eValue = dataE[nsE-1];
  65. int n = cpNLZ_BNU(eValue)+1;
  66. /* expand base and init result */
  67. ZEXPAND_COPY_BNU(dataT, nsM, dataX, nsX);
  68. COPY_BNU(dataY, dataT, nsM);
  69. eValue <<= n;
  70. for(; n<BNU_CHUNK_BITS; n++, eValue<<=1) {
  71. /* squaring R = R*R mod Modulus */
  72. cpMontSqr_BNU(dataY,
  73. dataY, nsM,
  74. dataM, nsM, m0,
  75. pProduct, pKBuffer);
  76. /* and multiply R = R*X mod Modulus */
  77. if(eValue & ((BNU_CHUNK_T)1<<(BNU_CHUNK_BITS-1)))
  78. cpMontMul_BNU(dataY,
  79. dataY, nsM,
  80. dataT, nsM,
  81. dataM, nsM, m0,
  82. pProduct, pKBuffer);
  83. }
  84. /* execute rest bits of E */
  85. for(--nsE; nsE>0; nsE--) {
  86. eValue = dataE[nsE-1];
  87. for(n=0; n<BNU_CHUNK_BITS; n++, eValue<<=1) {
  88. /* squaring: R = R*R mod Modulus */
  89. cpMontSqr_BNU(dataY,
  90. dataY, nsM,
  91. dataM, nsM, m0,
  92. pProduct, pKBuffer);
  93. if(eValue & ((BNU_CHUNK_T)1<<(BNU_CHUNK_BITS-1)))
  94. cpMontMul_BNU(dataY,
  95. dataY, nsM,
  96. dataT, nsM,
  97. dataM, nsM, m0,
  98. pProduct, pKBuffer);
  99. }
  100. }
  101. }
  102. return nsM;
  103. }