Nat576.cs 2.4 KB

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