SecP192K1Field.cs 6.2 KB

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