SecP192R1FieldElement.cs 5.5 KB

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