Nat448.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw
  7. {
  8. internal abstract class Nat448
  9. {
  10. public static void Copy64(ulong[] x, ulong[] z)
  11. {
  12. z[0] = x[0];
  13. z[1] = x[1];
  14. z[2] = x[2];
  15. z[3] = x[3];
  16. z[4] = x[4];
  17. z[5] = x[5];
  18. z[6] = x[6];
  19. }
  20. public static void Copy64(ulong[] x, int xOff, ulong[] z, int zOff)
  21. {
  22. z[zOff + 0] = x[xOff + 0];
  23. z[zOff + 1] = x[xOff + 1];
  24. z[zOff + 2] = x[xOff + 2];
  25. z[zOff + 3] = x[xOff + 3];
  26. z[zOff + 4] = x[xOff + 4];
  27. z[zOff + 5] = x[xOff + 5];
  28. z[zOff + 6] = x[xOff + 6];
  29. }
  30. public static ulong[] Create64()
  31. {
  32. return new ulong[7];
  33. }
  34. public static ulong[] CreateExt64()
  35. {
  36. return new ulong[14];
  37. }
  38. public static bool Eq64(ulong[] x, ulong[] y)
  39. {
  40. for (int i = 6; i >= 0; --i)
  41. {
  42. if (x[i] != y[i])
  43. {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. public static bool IsOne64(ulong[] x)
  50. {
  51. if (x[0] != 1UL)
  52. {
  53. return false;
  54. }
  55. for (int i = 1; i < 7; ++i)
  56. {
  57. if (x[i] != 0UL)
  58. {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. public static bool IsZero64(ulong[] x)
  65. {
  66. for (int i = 0; i < 7; ++i)
  67. {
  68. if (x[i] != 0UL)
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. public static void Mul(uint[] x, uint[] y, uint[] zz)
  76. {
  77. Nat224.Mul(x, y, zz);
  78. Nat224.Mul(x, 7, y, 7, zz, 14);
  79. uint c21 = Nat224.AddToEachOther(zz, 7, zz, 14);
  80. uint c14 = c21 + Nat224.AddTo(zz, 0, zz, 7, 0);
  81. c21 += Nat224.AddTo(zz, 21, zz, 14, c14);
  82. uint[] dx = Nat224.Create(), dy = Nat224.Create();
  83. bool neg = Nat224.Diff(x, 7, x, 0, dx, 0) != Nat224.Diff(y, 7, y, 0, dy, 0);
  84. uint[] tt = Nat224.CreateExt();
  85. Nat224.Mul(dx, dy, tt);
  86. c21 += neg ? Nat.AddTo(14, tt, 0, zz, 7) : (uint)Nat.SubFrom(14, tt, 0, zz, 7);
  87. Nat.AddWordAt(28, c21, zz, 21);
  88. }
  89. public static void Square(uint[] x, uint[] zz)
  90. {
  91. Nat224.Square(x, zz);
  92. Nat224.Square(x, 7, zz, 14);
  93. uint c21 = Nat224.AddToEachOther(zz, 7, zz, 14);
  94. uint c14 = c21 + Nat224.AddTo(zz, 0, zz, 7, 0);
  95. c21 += Nat224.AddTo(zz, 21, zz, 14, c14);
  96. uint[] dx = Nat224.Create();
  97. Nat224.Diff(x, 7, x, 0, dx, 0);
  98. uint[] tt = Nat224.CreateExt();
  99. Nat224.Square(dx, tt);
  100. c21 += (uint)Nat.SubFrom(14, tt, 0, zz, 7);
  101. Nat.AddWordAt(28, c21, zz, 21);
  102. }
  103. public static BigInteger ToBigInteger64(ulong[] x)
  104. {
  105. byte[] bs = new byte[56];
  106. for (int i = 0; i < 7; ++i)
  107. {
  108. ulong x_i = x[i];
  109. if (x_i != 0L)
  110. {
  111. Pack.UInt64_To_BE(x_i, bs, (6 - i) << 3);
  112. }
  113. }
  114. return new BigInteger(1, bs);
  115. }
  116. }
  117. }
  118. #pragma warning restore
  119. #endif