SecP224K1Field.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  9. {
  10. internal class SecP224K1Field
  11. {
  12. // 2^224 - 2^32 - 2^12 - 2^11 - 2^9 - 2^7 - 2^4 - 2 - 1
  13. internal static readonly uint[] P = new uint[]{ 0xFFFFE56D, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  14. 0xFFFFFFFF, 0xFFFFFFFF };
  15. private static readonly uint[] PExt = new uint[]{ 0x02C23069, 0x00003526, 0x00000001, 0x00000000, 0x00000000,
  16. 0x00000000, 0x00000000, 0xFFFFCADA, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
  17. };
  18. private static readonly uint[] PExtInv = new uint[]{ 0xFD3DCF97, 0xFFFFCAD9, 0xFFFFFFFE, 0xFFFFFFFF,
  19. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00003525, 0x00000002 };
  20. private const uint P6 = 0xFFFFFFFF;
  21. private const uint PExt13 = 0xFFFFFFFF;
  22. private const uint PInv33 = 0x1A93;
  23. public static void Add(uint[] x, uint[] y, uint[] z)
  24. {
  25. uint c = Nat224.Add(x, y, z);
  26. if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
  27. {
  28. Nat.Add33To(7, PInv33, z);
  29. }
  30. }
  31. public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
  32. {
  33. uint c = Nat.Add(14, xx, yy, zz);
  34. if (c != 0 || (zz[13] == PExt13 && Nat.Gte(14, zz, PExt)))
  35. {
  36. if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
  37. {
  38. Nat.IncAt(14, zz, PExtInv.Length);
  39. }
  40. }
  41. }
  42. public static void AddOne(uint[] x, uint[] z)
  43. {
  44. uint c = Nat.Inc(7, x, z);
  45. if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
  46. {
  47. Nat.Add33To(7, PInv33, z);
  48. }
  49. }
  50. public static uint[] FromBigInteger(BigInteger x)
  51. {
  52. uint[] z = Nat.FromBigInteger(224, x);
  53. if (z[6] == P6 && Nat224.Gte(z, P))
  54. {
  55. Nat224.SubFrom(P, z);
  56. }
  57. return z;
  58. }
  59. public static void Half(uint[] x, uint[] z)
  60. {
  61. if ((x[0] & 1) == 0)
  62. {
  63. Nat.ShiftDownBit(7, x, 0, z);
  64. }
  65. else
  66. {
  67. uint c = Nat224.Add(x, P, z);
  68. Nat.ShiftDownBit(7, z, c);
  69. }
  70. }
  71. public static void Inv(uint[] x, uint[] z)
  72. {
  73. Mod.CheckedModOddInverse(P, x, z);
  74. }
  75. public static int IsZero(uint[] x)
  76. {
  77. uint d = 0;
  78. for (int i = 0; i < 7; ++i)
  79. {
  80. d |= x[i];
  81. }
  82. d = (d >> 1) | (d & 1);
  83. return ((int)d - 1) >> 31;
  84. }
  85. public static void Multiply(uint[] x, uint[] y, uint[] z)
  86. {
  87. uint[] tt = Nat224.CreateExt();
  88. Nat224.Mul(x, y, tt);
  89. Reduce(tt, z);
  90. }
  91. public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
  92. {
  93. uint c = Nat224.MulAddTo(x, y, zz);
  94. if (c != 0 || (zz[13] == PExt13 && Nat.Gte(14, zz, PExt)))
  95. {
  96. if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
  97. {
  98. Nat.IncAt(14, zz, PExtInv.Length);
  99. }
  100. }
  101. }
  102. public static void Negate(uint[] x, uint[] z)
  103. {
  104. if (0 != IsZero(x))
  105. {
  106. Nat224.Sub(P, P, z);
  107. }
  108. else
  109. {
  110. Nat224.Sub(P, x, z);
  111. }
  112. }
  113. public static void Random(SecureRandom r, uint[] z)
  114. {
  115. byte[] bb = new byte[7 * 4];
  116. do
  117. {
  118. r.NextBytes(bb);
  119. Pack.LE_To_UInt32(bb, 0, z, 0, 7);
  120. }
  121. while (0 == Nat.LessThan(7, z, P));
  122. }
  123. public static void RandomMult(SecureRandom r, uint[] z)
  124. {
  125. do
  126. {
  127. Random(r, z);
  128. }
  129. while (0 != IsZero(z));
  130. }
  131. public static void Reduce(uint[] xx, uint[] z)
  132. {
  133. ulong cc = Nat224.Mul33Add(PInv33, xx, 7, xx, 0, z, 0);
  134. uint c = Nat224.Mul33DWordAdd(PInv33, cc, z, 0);
  135. Debug.Assert(c == 0 || c == 1);
  136. if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
  137. {
  138. Nat.Add33To(7, PInv33, z);
  139. }
  140. }
  141. public static void Reduce32(uint x, uint[] z)
  142. {
  143. if ((x != 0 && Nat224.Mul33WordAdd(PInv33, x, z, 0) != 0)
  144. || (z[6] == P6 && Nat224.Gte(z, P)))
  145. {
  146. Nat.Add33To(7, PInv33, z);
  147. }
  148. }
  149. public static void Square(uint[] x, uint[] z)
  150. {
  151. uint[] tt = Nat224.CreateExt();
  152. Nat224.Square(x, tt);
  153. Reduce(tt, z);
  154. }
  155. public static void SquareN(uint[] x, int n, uint[] z)
  156. {
  157. Debug.Assert(n > 0);
  158. uint[] tt = Nat224.CreateExt();
  159. Nat224.Square(x, tt);
  160. Reduce(tt, z);
  161. while (--n > 0)
  162. {
  163. Nat224.Square(z, tt);
  164. Reduce(tt, z);
  165. }
  166. }
  167. public static void Subtract(uint[] x, uint[] y, uint[] z)
  168. {
  169. int c = Nat224.Sub(x, y, z);
  170. if (c != 0)
  171. {
  172. Nat.Sub33From(7, PInv33, z);
  173. }
  174. }
  175. public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
  176. {
  177. int c = Nat.Sub(14, xx, yy, zz);
  178. if (c != 0)
  179. {
  180. if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
  181. {
  182. Nat.DecAt(14, zz, PExtInv.Length);
  183. }
  184. }
  185. }
  186. public static void Twice(uint[] x, uint[] z)
  187. {
  188. uint c = Nat.ShiftUpBit(7, x, 0, z);
  189. if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
  190. {
  191. Nat.Add33To(7, PInv33, z);
  192. }
  193. }
  194. }
  195. }
  196. #pragma warning restore
  197. #endif