Nat320.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Diagnostics;
  4. using Org.BouncyCastle.Crypto.Utilities;
  5. namespace Org.BouncyCastle.Math.Raw
  6. {
  7. internal abstract class Nat320
  8. {
  9. public static void Copy64(ulong[] x, ulong[] z)
  10. {
  11. z[0] = x[0];
  12. z[1] = x[1];
  13. z[2] = x[2];
  14. z[3] = x[3];
  15. z[4] = x[4];
  16. }
  17. public static ulong[] Create64()
  18. {
  19. return new ulong[5];
  20. }
  21. public static ulong[] CreateExt64()
  22. {
  23. return new ulong[10];
  24. }
  25. public static bool Eq64(ulong[] x, ulong[] y)
  26. {
  27. for (int i = 4; i >= 0; --i)
  28. {
  29. if (x[i] != y[i])
  30. {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. public static ulong[] FromBigInteger64(BigInteger x)
  37. {
  38. if (x.SignValue < 0 || x.BitLength > 320)
  39. throw new ArgumentException();
  40. ulong[] z = Create64();
  41. int i = 0;
  42. while (x.SignValue != 0)
  43. {
  44. z[i++] = (ulong)x.LongValue;
  45. x = x.ShiftRight(64);
  46. }
  47. return z;
  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 < 5; ++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 < 5; ++i)
  67. {
  68. if (x[i] != 0UL)
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. public static BigInteger ToBigInteger64(ulong[] x)
  76. {
  77. byte[] bs = new byte[40];
  78. for (int i = 0; i < 5; ++i)
  79. {
  80. ulong x_i = x[i];
  81. if (x_i != 0L)
  82. {
  83. Pack.UInt64_To_BE(x_i, bs, (4 - i) << 3);
  84. }
  85. }
  86. return new BigInteger(1, bs);
  87. }
  88. }
  89. }
  90. #endif