SecP521R1FieldElement.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  7. {
  8. internal class SecP521R1FieldElement
  9. : AbstractFpFieldElement
  10. {
  11. public static readonly BigInteger Q = SecP521R1Curve.q;
  12. protected internal readonly uint[] x;
  13. public SecP521R1FieldElement(BigInteger x)
  14. {
  15. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  16. throw new ArgumentException("value invalid for SecP521R1FieldElement", "x");
  17. this.x = SecP521R1Field.FromBigInteger(x);
  18. }
  19. public SecP521R1FieldElement()
  20. {
  21. this.x = Nat.Create(17);
  22. }
  23. protected internal SecP521R1FieldElement(uint[] x)
  24. {
  25. this.x = x;
  26. }
  27. public override bool IsZero
  28. {
  29. get { return Nat.IsZero(17, x); }
  30. }
  31. public override bool IsOne
  32. {
  33. get { return Nat.IsOne(17, x); }
  34. }
  35. public override bool TestBitZero()
  36. {
  37. return Nat.GetBit(x, 0) == 1;
  38. }
  39. public override BigInteger ToBigInteger()
  40. {
  41. return Nat.ToBigInteger(17, x);
  42. }
  43. public override string FieldName
  44. {
  45. get { return "SecP521R1Field"; }
  46. }
  47. public override int FieldSize
  48. {
  49. get { return Q.BitLength; }
  50. }
  51. public override ECFieldElement Add(ECFieldElement b)
  52. {
  53. uint[] z = Nat.Create(17);
  54. SecP521R1Field.Add(x, ((SecP521R1FieldElement)b).x, z);
  55. return new SecP521R1FieldElement(z);
  56. }
  57. public override ECFieldElement AddOne()
  58. {
  59. uint[] z = Nat.Create(17);
  60. SecP521R1Field.AddOne(x, z);
  61. return new SecP521R1FieldElement(z);
  62. }
  63. public override ECFieldElement Subtract(ECFieldElement b)
  64. {
  65. uint[] z = Nat.Create(17);
  66. SecP521R1Field.Subtract(x, ((SecP521R1FieldElement)b).x, z);
  67. return new SecP521R1FieldElement(z);
  68. }
  69. public override ECFieldElement Multiply(ECFieldElement b)
  70. {
  71. uint[] z = Nat.Create(17);
  72. SecP521R1Field.Multiply(x, ((SecP521R1FieldElement)b).x, z);
  73. return new SecP521R1FieldElement(z);
  74. }
  75. public override ECFieldElement Divide(ECFieldElement b)
  76. {
  77. //return Multiply(b.Invert());
  78. uint[] z = Nat.Create(17);
  79. Mod.Invert(SecP521R1Field.P, ((SecP521R1FieldElement)b).x, z);
  80. SecP521R1Field.Multiply(z, x, z);
  81. return new SecP521R1FieldElement(z);
  82. }
  83. public override ECFieldElement Negate()
  84. {
  85. uint[] z = Nat.Create(17);
  86. SecP521R1Field.Negate(x, z);
  87. return new SecP521R1FieldElement(z);
  88. }
  89. public override ECFieldElement Square()
  90. {
  91. uint[] z = Nat.Create(17);
  92. SecP521R1Field.Square(x, z);
  93. return new SecP521R1FieldElement(z);
  94. }
  95. public override ECFieldElement Invert()
  96. {
  97. //return new SecP521R1FieldElement(ToBigInteger().ModInverse(Q));
  98. uint[] z = Nat.Create(17);
  99. Mod.Invert(SecP521R1Field.P, x, z);
  100. return new SecP521R1FieldElement(z);
  101. }
  102. /**
  103. * return a sqrt root - the routine verifies that the calculation returns the right value - if
  104. * none exists it returns null.
  105. */
  106. public override ECFieldElement Sqrt()
  107. {
  108. // Raise this element to the exponent 2^519
  109. uint[] x1 = this.x;
  110. if (Nat.IsZero(17, x1) || Nat.IsOne(17, x1))
  111. return this;
  112. uint[] t1 = Nat.Create(17);
  113. uint[] t2 = Nat.Create(17);
  114. SecP521R1Field.SquareN(x1, 519, t1);
  115. SecP521R1Field.Square(t1, t2);
  116. return Nat.Eq(17, x1, t2) ? new SecP521R1FieldElement(t1) : null;
  117. }
  118. public override bool Equals(object obj)
  119. {
  120. return Equals(obj as SecP521R1FieldElement);
  121. }
  122. public override bool Equals(ECFieldElement other)
  123. {
  124. return Equals(other as SecP521R1FieldElement);
  125. }
  126. public virtual bool Equals(SecP521R1FieldElement other)
  127. {
  128. if (this == other)
  129. return true;
  130. if (null == other)
  131. return false;
  132. return Nat.Eq(17, x, other.x);
  133. }
  134. public override int GetHashCode()
  135. {
  136. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 17);
  137. }
  138. }
  139. }
  140. #pragma warning restore
  141. #endif