Nat448.cs 2.3 KB

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