BigTwistedEdwards.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Addition and doubling formulas using Extended Twisted Edwards coordinates from
  27. Hisil–Wong–Carter–Dawson paper "Twisted Edwards Curves Revisited" (Asiacrypt 2008)
  28. w-MOF scalar multiplication from http://www.sdl.hitachi.co.jp/crypto/mof/index-e.html
  29. Scalar multiplication precomputation with "conjugate addition" inspired by
  30. Longa-Gebotys paper "Novel Precomputation Schemes for Elliptic Curve Cryptosystems" (2008)
  31. */
  32. /*
  33. Twisted Edwards E(p) Curve: a * x^2 + y^2 = 1 + d * x^2 * y^2, a = -1, p = 2^256 - c, c small
  34. Edwards coordinates: (X : Y : Z)
  35. Extended Edwards coordinates: (X : Y : T : Z), T = XY
  36. Edwards to Extended Edwards: (X : Y : Z) -> (XZ : YZ : XY : ZZ)
  37. Extended Edwards to Edwards: (X : Y : T : Z) -> (X : Y : Z)
  38. -(X : Y : T : Z) = (-X : Y : -T : Z)
  39. Additive Identity element: X = 0
  40. When Z = 1, a multiplication can be omitted
  41. Mixing operations for more speed:
  42. doubling, doubling -> E = 2E
  43. doubling, add -> Ee = 2E, E = Ee + Ee
  44. */
  45. #ifndef CAT_BIG_TWISTED_EDWARDS_HPP
  46. #define CAT_BIG_TWISTED_EDWARDS_HPP
  47. #include <cat/math/BigPseudoMersenne.hpp>
  48. #include <cat/rand/IRandom.hpp>
  49. namespace cat {
  50. class BigTwistedEdwards : public BigPseudoMersenne
  51. {
  52. static const int POINT_REGS = 4;
  53. static const int XOFF = 0;
  54. int YOFF, TOFF, ZOFF, POINT_STRIDE;
  55. // Point multiplication default window
  56. static const int WINDOW_BITS = 6;
  57. static const int PRECOMP_POINTS = 1 << (WINDOW_BITS-1);
  58. static const int PRECOMP_NEG_OFFSET = PRECOMP_POINTS / 2;
  59. static const int TE_OVERHEAD = (1 + PRECOMP_POINTS) * POINT_REGS + 9 + POINT_REGS * 2;
  60. int te_regs;
  61. // Local workspace
  62. Leg *A, *B, *C, *D, *E, *F, *G, *H, *CurveQ, *Generator;
  63. Leg *TempPt;
  64. protected:
  65. Leg curve_d;
  66. // Simultaneous Add and Subtract for efficient precomputation (A +/- B) in 14M 1D 11a (versus 16M 2D 16a)
  67. void PtPrecompAddSub(const Leg *in_a, const Leg *in_b, Leg *sum, Leg *diff, int neg_offset);
  68. public:
  69. BigTwistedEdwards(int regs, int bits, int C, int D, const u8 *Q, const u8 *GenPt);
  70. int PtLegs() { return Legs() * POINT_REGS; }
  71. Leg GetCurveD() { return curve_d; }
  72. const Leg *GetCurveQ() { return CurveQ; }
  73. const Leg *GetGenerator() { return Generator; }
  74. public:
  75. // Unpack an Extended Projective point (X,Y,T,Z) from affine point (x,y)
  76. void PtUnpack(Leg *inout);
  77. // Set a point to the identity
  78. void PtIdentity(Leg *inout);
  79. // Check if the affine point (x,y) is the additive identity x=0
  80. bool IsAffineIdentity(const Leg *in);
  81. public:
  82. void PtCopy(const Leg *in, Leg *out);
  83. // Fill the X coordinate of the point with a random value
  84. void PtFillRandomX(IRandom *prng, Leg *out);
  85. // Generate a random point on the curve that is not part of a small subgroup
  86. void PtGenerate(IRandom *prng, Leg *out);
  87. public:
  88. // Solve for Y given the X point on a curve
  89. void PtSolveAffineY(Leg *inout);
  90. // Verify that the point (x,y) exists on the given curve
  91. bool PtValidAffine(const Leg *in);
  92. public:
  93. // out(x) = X/Z
  94. void SaveAffineX(const Leg *in, void *out_x);
  95. // out(x,y) = (X/Z,Y/Z)
  96. void SaveAffineXY(const Leg *in, void *out_x, void *out_y);
  97. // out(X,Y) = (X,Y) without attempting to convert to affine from projective
  98. void SaveProjectiveXY(const Leg *in, void *out_x, void *out_y);
  99. // out(X,Y,Z,T) = (in_x,in_y), returns false if the coordinates are invalid
  100. bool LoadVerifyAffineXY(const void *in_x, const void *in_y, Leg *out);
  101. // Compute affine coordinates for (X,Y), set Z=1, and compute T = xy
  102. void PtNormalize(const Leg *in, Leg *out);
  103. public:
  104. // Extended Twisted Edwards Negation Formula
  105. void PtNegate(const Leg *in, Leg *out);
  106. // Extended Twisted Edwards Unified Addition Formula (works when both inputs are the same) in 8M 1D 9a
  107. void PtEAdd(const Leg *in_a, const Leg *in_b, Leg *out);
  108. void PtAdd(const Leg *in_a, const Leg *in_b, Leg *out); // -1M, cannot be followed by PtAdd
  109. // Extended Twisted Edwards Unified Subtraction Formula (works when both inputs are the same) in 8M 1D 9a
  110. void PtESubtract(const Leg *in_a, const Leg *in_b, Leg *out);
  111. void PtSubtract(const Leg *in_a, const Leg *in_b, Leg *out); // -1M, cannot be followed by PtAdd
  112. // Extended Twisted Edwards Dedicated Doubling Formula in 4M 4S 5a
  113. void PtEDouble(const Leg *in, Leg *out);
  114. void PtDouble(const Leg *in, Leg *out); // -1M, cannot be followed by PtAdd
  115. // Extended Twisted Edwards Dedicated Doubling Formula Assuming Z=1, in 4M 3S 4a
  116. void PtEDoubleZ1(const Leg *in, Leg *out);
  117. void PtDoubleZ1(const Leg *in, Leg *out); // -1M, cannot be followed by PtAdd
  118. public:
  119. // Allocate a table for use with PtMultiplyPrecomp()
  120. // Free the table with Aligned::Delete()
  121. Leg *PtMultiplyPrecompAlloc(int w);
  122. // Precompute odd multiples of input point
  123. void PtMultiplyPrecomp(const Leg *in, int w, Leg *table);
  124. public:
  125. // Extended Twisted Edwards Scalar Multiplication k*p
  126. // Requires precomputation with PtMultiplyPrecomp()
  127. // CAN *NOT* BE followed by a Pt[E]Add()
  128. void PtMultiply(const Leg *in_precomp, int w, const Leg *in_k, u8 msb_k, Leg *out);
  129. // Extended Twisted Edwards Scalar Multiplication k*p
  130. // Uses default precomputation
  131. // CAN *NOT* BE followed by a Pt[E]Add()
  132. void PtMultiply(const Leg *in_p, const Leg *in_k, u8 msb_k, Leg *out);
  133. // A reference multiplier to verify that PtMultiply() is functionally the same
  134. void RefMul(const Leg *in_p, const Leg *in_k, u8 msb_k, Leg *out);
  135. public:
  136. // Extended Twisted Edwards Simultaneous Scalar Multiplication k*P + l*Q
  137. // Requires precomputation with PtMultiplyPrecomp()
  138. // CAN *NOT* BE followed by a Pt[E]Add()
  139. void PtSiMultiply(const Leg *precomp_p, const Leg *precomp_q, int w,
  140. const Leg *in_k, u8 msb_k, const Leg *in_l, u8 msb_l, Leg *out);
  141. };
  142. } // namespace cat
  143. #endif // CAT_BIG_TWISTED_EDWARDS_HPP
粤ICP备19079148号