SecP256K1Field.cs 5.3 KB

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