SecP192R1FieldElement.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 SecP192R1FieldElement
  8. : ECFieldElement
  9. {
  10. public static readonly BigInteger Q = SecP192R1Curve.q;
  11. protected internal readonly uint[] x;
  12. public SecP192R1FieldElement(BigInteger x)
  13. {
  14. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  15. throw new ArgumentException("value invalid for SecP192R1FieldElement", "x");
  16. this.x = SecP192R1Field.FromBigInteger(x);
  17. }
  18. public SecP192R1FieldElement()
  19. {
  20. this.x = Nat192.Create();
  21. }
  22. protected internal SecP192R1FieldElement(uint[] x)
  23. {
  24. this.x = x;
  25. }
  26. public override bool IsZero
  27. {
  28. get { return Nat192.IsZero(x); }
  29. }
  30. public override bool IsOne
  31. {
  32. get { return Nat192.IsOne(x); }
  33. }
  34. public override bool TestBitZero()
  35. {
  36. return Nat192.GetBit(x, 0) == 1;
  37. }
  38. public override BigInteger ToBigInteger()
  39. {
  40. return Nat192.ToBigInteger(x);
  41. }
  42. public override string FieldName
  43. {
  44. get { return "SecP192R1Field"; }
  45. }
  46. public override int FieldSize
  47. {
  48. get { return Q.BitLength; }
  49. }
  50. public override ECFieldElement Add(ECFieldElement b)
  51. {
  52. uint[] z = Nat192.Create();
  53. SecP192R1Field.Add(x, ((SecP192R1FieldElement)b).x, z);
  54. return new SecP192R1FieldElement(z);
  55. }
  56. public override ECFieldElement AddOne()
  57. {
  58. uint[] z = Nat192.Create();
  59. SecP192R1Field.AddOne(x, z);
  60. return new SecP192R1FieldElement(z);
  61. }
  62. public override ECFieldElement Subtract(ECFieldElement b)
  63. {
  64. uint[] z = Nat192.Create();
  65. SecP192R1Field.Subtract(x, ((SecP192R1FieldElement)b).x, z);
  66. return new SecP192R1FieldElement(z);
  67. }
  68. public override ECFieldElement Multiply(ECFieldElement b)
  69. {
  70. uint[] z = Nat192.Create();
  71. SecP192R1Field.Multiply(x, ((SecP192R1FieldElement)b).x, z);
  72. return new SecP192R1FieldElement(z);
  73. }
  74. public override ECFieldElement Divide(ECFieldElement b)
  75. {
  76. //return Multiply(b.Invert());
  77. uint[] z = Nat192.Create();
  78. Mod.Invert(SecP192R1Field.P, ((SecP192R1FieldElement)b).x, z);
  79. SecP192R1Field.Multiply(z, x, z);
  80. return new SecP192R1FieldElement(z);
  81. }
  82. public override ECFieldElement Negate()
  83. {
  84. uint[] z = Nat192.Create();
  85. SecP192R1Field.Negate(x, z);
  86. return new SecP192R1FieldElement(z);
  87. }
  88. public override ECFieldElement Square()
  89. {
  90. uint[] z = Nat192.Create();
  91. SecP192R1Field.Square(x, z);
  92. return new SecP192R1FieldElement(z);
  93. }
  94. public override ECFieldElement Invert()
  95. {
  96. //return new SecP192R1FieldElement(ToBigInteger().ModInverse(Q));
  97. uint[] z = Nat192.Create();
  98. Mod.Invert(SecP192R1Field.P, x, z);
  99. return new SecP192R1FieldElement(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^190 - 2^62
  108. uint[] x1 = this.x;
  109. if (Nat192.IsZero(x1) || Nat192.IsOne(x1))
  110. return this;
  111. uint[] t1 = Nat192.Create();
  112. uint[] t2 = Nat192.Create();
  113. SecP192R1Field.Square(x1, t1);
  114. SecP192R1Field.Multiply(t1, x1, t1);
  115. SecP192R1Field.SquareN(t1, 2, t2);
  116. SecP192R1Field.Multiply(t2, t1, t2);
  117. SecP192R1Field.SquareN(t2, 4, t1);
  118. SecP192R1Field.Multiply(t1, t2, t1);
  119. SecP192R1Field.SquareN(t1, 8, t2);
  120. SecP192R1Field.Multiply(t2, t1, t2);
  121. SecP192R1Field.SquareN(t2, 16, t1);
  122. SecP192R1Field.Multiply(t1, t2, t1);
  123. SecP192R1Field.SquareN(t1, 32, t2);
  124. SecP192R1Field.Multiply(t2, t1, t2);
  125. SecP192R1Field.SquareN(t2, 64, t1);
  126. SecP192R1Field.Multiply(t1, t2, t1);
  127. SecP192R1Field.SquareN(t1, 62, t1);
  128. SecP192R1Field.Square(t1, t2);
  129. return Nat192.Eq(x1, t2) ? new SecP192R1FieldElement(t1) : null;
  130. }
  131. public override bool Equals(object obj)
  132. {
  133. return Equals(obj as SecP192R1FieldElement);
  134. }
  135. public override bool Equals(ECFieldElement other)
  136. {
  137. return Equals(other as SecP192R1FieldElement);
  138. }
  139. public virtual bool Equals(SecP192R1FieldElement other)
  140. {
  141. if (this == other)
  142. return true;
  143. if (null == other)
  144. return false;
  145. return Nat192.Eq(x, other.x);
  146. }
  147. public override int GetHashCode()
  148. {
  149. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 6);
  150. }
  151. }
  152. }
  153. #endif