SecP192K1Field.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.Math.Raw;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  7. {
  8. internal class SecP192K1Field
  9. {
  10. // 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1
  11. internal static readonly uint[] P = new uint[]{ 0xFFFFEE37, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  12. internal static readonly uint[] PExt = new uint[]{ 0x013C4FD1, 0x00002392, 0x00000001, 0x00000000, 0x00000000,
  13. 0x00000000, 0xFFFFDC6E, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  14. private static readonly uint[] PExtInv = new uint[]{ 0xFEC3B02F, 0xFFFFDC6D, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF,
  15. 0xFFFFFFFF, 0x00002391, 0x00000002 };
  16. private const uint P5 = 0xFFFFFFFF;
  17. private const uint PExt11 = 0xFFFFFFFF;
  18. private const uint PInv33 = 0x11C9;
  19. public static void Add(uint[] x, uint[] y, uint[] z)
  20. {
  21. uint c = Nat192.Add(x, y, z);
  22. if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
  23. {
  24. Nat.Add33To(6, PInv33, z);
  25. }
  26. }
  27. public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
  28. {
  29. uint c = Nat.Add(12, xx, yy, zz);
  30. if (c != 0 || (zz[11] == PExt11 && Nat.Gte(12, zz, PExt)))
  31. {
  32. if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
  33. {
  34. Nat.IncAt(12, zz, PExtInv.Length);
  35. }
  36. }
  37. }
  38. public static void AddOne(uint[] x, uint[] z)
  39. {
  40. uint c = Nat.Inc(6, x, z);
  41. if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
  42. {
  43. Nat.Add33To(6, PInv33, z);
  44. }
  45. }
  46. public static uint[] FromBigInteger(BigInteger x)
  47. {
  48. uint[] z = Nat192.FromBigInteger(x);
  49. if (z[5] == P5 && Nat192.Gte(z, P))
  50. {
  51. Nat192.SubFrom(P, z);
  52. }
  53. return z;
  54. }
  55. public static void Half(uint[] x, uint[] z)
  56. {
  57. if ((x[0] & 1) == 0)
  58. {
  59. Nat.ShiftDownBit(6, x, 0, z);
  60. }
  61. else
  62. {
  63. uint c = Nat192.Add(x, P, z);
  64. Nat.ShiftDownBit(6, z, c);
  65. }
  66. }
  67. public static void Multiply(uint[] x, uint[] y, uint[] z)
  68. {
  69. uint[] tt = Nat192.CreateExt();
  70. Nat192.Mul(x, y, tt);
  71. Reduce(tt, z);
  72. }
  73. public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
  74. {
  75. uint c = Nat192.MulAddTo(x, y, zz);
  76. if (c != 0 || (zz[11] == PExt11 && Nat.Gte(12, zz, PExt)))
  77. {
  78. if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
  79. {
  80. Nat.IncAt(12, zz, PExtInv.Length);
  81. }
  82. }
  83. }
  84. public static void Negate(uint[] x, uint[] z)
  85. {
  86. if (Nat192.IsZero(x))
  87. {
  88. Nat192.Zero(z);
  89. }
  90. else
  91. {
  92. Nat192.Sub(P, x, z);
  93. }
  94. }
  95. public static void Reduce(uint[] xx, uint[] z)
  96. {
  97. ulong cc = Nat192.Mul33Add(PInv33, xx, 6, xx, 0, z, 0);
  98. uint c = Nat192.Mul33DWordAdd(PInv33, cc, z, 0);
  99. Debug.Assert(c == 0 || c == 1);
  100. if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
  101. {
  102. Nat.Add33To(6, PInv33, z);
  103. }
  104. }
  105. public static void Reduce32(uint x, uint[] z)
  106. {
  107. if ((x != 0 && Nat192.Mul33WordAdd(PInv33, x, z, 0) != 0)
  108. || (z[5] == P5 && Nat192.Gte(z, P)))
  109. {
  110. Nat.Add33To(6, PInv33, z);
  111. }
  112. }
  113. public static void Square(uint[] x, uint[] z)
  114. {
  115. uint[] tt = Nat192.CreateExt();
  116. Nat192.Square(x, tt);
  117. Reduce(tt, z);
  118. }
  119. public static void SquareN(uint[] x, int n, uint[] z)
  120. {
  121. Debug.Assert(n > 0);
  122. uint[] tt = Nat192.CreateExt();
  123. Nat192.Square(x, tt);
  124. Reduce(tt, z);
  125. while (--n > 0)
  126. {
  127. Nat192.Square(z, tt);
  128. Reduce(tt, z);
  129. }
  130. }
  131. public static void Subtract(uint[] x, uint[] y, uint[] z)
  132. {
  133. int c = Nat192.Sub(x, y, z);
  134. if (c != 0)
  135. {
  136. Nat.Sub33From(6, PInv33, z);
  137. }
  138. }
  139. public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
  140. {
  141. int c = Nat.Sub(12, xx, yy, zz);
  142. if (c != 0)
  143. {
  144. if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
  145. {
  146. Nat.DecAt(12, zz, PExtInv.Length);
  147. }
  148. }
  149. }
  150. public static void Twice(uint[] x, uint[] z)
  151. {
  152. uint c = Nat.ShiftUpBit(6, x, 0, z);
  153. if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
  154. {
  155. Nat.Add33To(6, PInv33, z);
  156. }
  157. }
  158. }
  159. }
  160. #pragma warning restore
  161. #endif