SecP192K1Field.cs 5.2 KB

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