Nat320.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Nat320
  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. }
  18. public static void Copy64(ulong[] x, int xOff, ulong[] z, int zOff)
  19. {
  20. z[zOff + 0] = x[xOff + 0];
  21. z[zOff + 1] = x[xOff + 1];
  22. z[zOff + 2] = x[xOff + 2];
  23. z[zOff + 3] = x[xOff + 3];
  24. z[zOff + 4] = x[xOff + 4];
  25. }
  26. public static ulong[] Create64()
  27. {
  28. return new ulong[5];
  29. }
  30. public static ulong[] CreateExt64()
  31. {
  32. return new ulong[10];
  33. }
  34. public static bool Eq64(ulong[] x, ulong[] y)
  35. {
  36. for (int i = 4; i >= 0; --i)
  37. {
  38. if (x[i] != y[i])
  39. {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. public static ulong[] FromBigInteger64(BigInteger x)
  46. {
  47. if (x.SignValue < 0 || x.BitLength > 320)
  48. throw new ArgumentException();
  49. ulong[] z = Create64();
  50. int i = 0;
  51. while (x.SignValue != 0)
  52. {
  53. z[i++] = (ulong)x.LongValue;
  54. x = x.ShiftRight(64);
  55. }
  56. return z;
  57. }
  58. public static bool IsOne64(ulong[] x)
  59. {
  60. if (x[0] != 1UL)
  61. {
  62. return false;
  63. }
  64. for (int i = 1; i < 5; ++i)
  65. {
  66. if (x[i] != 0UL)
  67. {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. public static bool IsZero64(ulong[] x)
  74. {
  75. for (int i = 0; i < 5; ++i)
  76. {
  77. if (x[i] != 0UL)
  78. {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. public static BigInteger ToBigInteger64(ulong[] x)
  85. {
  86. byte[] bs = new byte[40];
  87. for (int i = 0; i < 5; ++i)
  88. {
  89. ulong x_i = x[i];
  90. if (x_i != 0L)
  91. {
  92. Pack.UInt64_To_BE(x_i, bs, (4 - i) << 3);
  93. }
  94. }
  95. return new BigInteger(1, bs);
  96. }
  97. }
  98. }
  99. #pragma warning restore
  100. #endif