BigMontgomery.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. Several algorithms based on ideas from the "Handbook of Applied Cryptography"
  27. http://www.cacr.math.uwaterloo.ca/hac/
  28. */
  29. #ifndef CAT_BIG_MONTGOMERY_HPP
  30. #define CAT_BIG_MONTGOMERY_HPP
  31. #include <cat/math/BigRTL.hpp>
  32. #include <cat/rand/IRandom.hpp>
  33. namespace cat {
  34. // Performs fast modular arithmetic in the Montgomery Residue Number System
  35. class BigMontgomery : public BigRTL
  36. {
  37. static const int MON_OVERHEAD = 3 + 4;
  38. int mon_regs;
  39. protected:
  40. Leg *TempProduct;
  41. Leg *TempProductHi;
  42. Leg *CachedModulus;
  43. Leg mod_inv;
  44. public:
  45. BigMontgomery(int regs, int bits);
  46. // Must call SetModulus() before using this object
  47. void SetModulus(const Leg *mod);
  48. public:
  49. const Leg *GetModulus() { return CachedModulus; }
  50. void CopyModulus(Leg *out);
  51. public:
  52. // Convert value in register into RNS, stored in out
  53. void MonInput(const Leg *in, Leg *out);
  54. // Convert value in register from RNS, stored in out
  55. void MonOutput(const Leg *in, Leg *out);
  56. // Note: This will clobber the input product!
  57. // Reduce a double-register product to a single register in the RNS
  58. void MonReduceProduct(Leg *inout_product, Leg *out);
  59. public:
  60. // Inputs and outputs must be in the Montgomery RNS
  61. void MonAdd(const Leg *in_a, const Leg *in_b, Leg *out);
  62. void MonSubtract(const Leg *in_a, const Leg *in_b, Leg *out);
  63. void MonNegate(const Leg *in, Leg *out);
  64. void MonDouble(const Leg *in, Leg *out);
  65. public:
  66. // Inputs and outputs must be in the Montgomery RNS
  67. void MonMultiply(const Leg *in_a, const Leg *in_b, Leg *out);
  68. void MonSquare(const Leg *in, Leg *out);
  69. public:
  70. // Base must be in the Montgomery RNS. in_base != out
  71. void MonExpMod(const Leg *in_base, const Leg *in_exp, Leg *out);
  72. public:
  73. // Input is NOT in the RNS (don't call MonInput)
  74. // Probably a prime, certainty = 4^-trials. 20: %99.9999999999 certainty
  75. bool IsRabinMillerPrime(IRandom *prng, const Leg *n, int trials = 20);
  76. };
  77. } // namespace cat
  78. #endif // CAT_BIG_MONTGOMERY_HPP
粤ICP备19079148号