SecP521R1FieldElement.cs 4.7 KB

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