SecP192K1FieldElement.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  8. {
  9. internal class SecP192K1FieldElement
  10. : AbstractFpFieldElement
  11. {
  12. public static readonly BigInteger Q = SecP192K1Curve.q;
  13. protected internal readonly uint[] x;
  14. public SecP192K1FieldElement(BigInteger x)
  15. {
  16. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  17. throw new ArgumentException("value invalid for SecP192K1FieldElement", "x");
  18. this.x = SecP192K1Field.FromBigInteger(x);
  19. }
  20. public SecP192K1FieldElement()
  21. {
  22. this.x = Nat192.Create();
  23. }
  24. protected internal SecP192K1FieldElement(uint[] x)
  25. {
  26. this.x = x;
  27. }
  28. public override bool IsZero
  29. {
  30. get { return Nat192.IsZero(x); }
  31. }
  32. public override bool IsOne
  33. {
  34. get { return Nat192.IsOne(x); }
  35. }
  36. public override bool TestBitZero()
  37. {
  38. return Nat192.GetBit(x, 0) == 1;
  39. }
  40. public override BigInteger ToBigInteger()
  41. {
  42. return Nat192.ToBigInteger(x);
  43. }
  44. public override string FieldName
  45. {
  46. get { return "SecP192K1Field"; }
  47. }
  48. public override int FieldSize
  49. {
  50. get { return Q.BitLength; }
  51. }
  52. public override ECFieldElement Add(ECFieldElement b)
  53. {
  54. uint[] z = Nat192.Create();
  55. SecP192K1Field.Add(x, ((SecP192K1FieldElement)b).x, z);
  56. return new SecP192K1FieldElement(z);
  57. }
  58. public override ECFieldElement AddOne()
  59. {
  60. uint[] z = Nat192.Create();
  61. SecP192K1Field.AddOne(x, z);
  62. return new SecP192K1FieldElement(z);
  63. }
  64. public override ECFieldElement Subtract(ECFieldElement b)
  65. {
  66. uint[] z = Nat192.Create();
  67. SecP192K1Field.Subtract(x, ((SecP192K1FieldElement)b).x, z);
  68. return new SecP192K1FieldElement(z);
  69. }
  70. public override ECFieldElement Multiply(ECFieldElement b)
  71. {
  72. uint[] z = Nat192.Create();
  73. SecP192K1Field.Multiply(x, ((SecP192K1FieldElement)b).x, z);
  74. return new SecP192K1FieldElement(z);
  75. }
  76. public override ECFieldElement Divide(ECFieldElement b)
  77. {
  78. //return Multiply(b.Invert());
  79. uint[] z = Nat192.Create();
  80. Mod.Invert(SecP192K1Field.P, ((SecP192K1FieldElement)b).x, z);
  81. SecP192K1Field.Multiply(z, x, z);
  82. return new SecP192K1FieldElement(z);
  83. }
  84. public override ECFieldElement Negate()
  85. {
  86. uint[] z = Nat192.Create();
  87. SecP192K1Field.Negate(x, z);
  88. return new SecP192K1FieldElement(z);
  89. }
  90. public override ECFieldElement Square()
  91. {
  92. uint[] z = Nat192.Create();
  93. SecP192K1Field.Square(x, z);
  94. return new SecP192K1FieldElement(z);
  95. }
  96. public override ECFieldElement Invert()
  97. {
  98. //return new SecP192K1FieldElement(ToBigInteger().ModInverse(Q));
  99. uint[] z = Nat192.Create();
  100. Mod.Invert(SecP192K1Field.P, x, z);
  101. return new SecP192K1FieldElement(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. /*
  110. * Raise this element to the exponent 2^190 - 2^30 - 2^10 - 2^6 - 2^5 - 2^4 - 2^1
  111. *
  112. * Breaking up the exponent's binary representation into "repunits", we get:
  113. * { 159 1s } { 1 0s } { 19 1s } { 1 0s } { 3 1s } { 3 0s} { 3 1s } { 1 0s }
  114. *
  115. * Therefore we need an addition chain containing 3, 19, 159 (the lengths of the repunits)
  116. * We use: 1, 2, [3], 6, 8, 16, [19], 35, 70, 140, [159]
  117. */
  118. uint[] x1 = this.x;
  119. if (Nat192.IsZero(x1) || Nat192.IsOne(x1))
  120. return this;
  121. uint[] x2 = Nat192.Create();
  122. SecP192K1Field.Square(x1, x2);
  123. SecP192K1Field.Multiply(x2, x1, x2);
  124. uint[] x3 = Nat192.Create();
  125. SecP192K1Field.Square(x2, x3);
  126. SecP192K1Field.Multiply(x3, x1, x3);
  127. uint[] x6 = Nat192.Create();
  128. SecP192K1Field.SquareN(x3, 3, x6);
  129. SecP192K1Field.Multiply(x6, x3, x6);
  130. uint[] x8 = x6;
  131. SecP192K1Field.SquareN(x6, 2, x8);
  132. SecP192K1Field.Multiply(x8, x2, x8);
  133. uint[] x16 = x2;
  134. SecP192K1Field.SquareN(x8, 8, x16);
  135. SecP192K1Field.Multiply(x16, x8, x16);
  136. uint[] x19 = x8;
  137. SecP192K1Field.SquareN(x16, 3, x19);
  138. SecP192K1Field.Multiply(x19, x3, x19);
  139. uint[] x35 = Nat192.Create();
  140. SecP192K1Field.SquareN(x19, 16, x35);
  141. SecP192K1Field.Multiply(x35, x16, x35);
  142. uint[] x70 = x16;
  143. SecP192K1Field.SquareN(x35, 35, x70);
  144. SecP192K1Field.Multiply(x70, x35, x70);
  145. uint[] x140 = x35;
  146. SecP192K1Field.SquareN(x70, 70, x140);
  147. SecP192K1Field.Multiply(x140, x70, x140);
  148. uint[] x159 = x70;
  149. SecP192K1Field.SquareN(x140, 19, x159);
  150. SecP192K1Field.Multiply(x159, x19, x159);
  151. uint[] t1 = x159;
  152. SecP192K1Field.SquareN(t1, 20, t1);
  153. SecP192K1Field.Multiply(t1, x19, t1);
  154. SecP192K1Field.SquareN(t1, 4, t1);
  155. SecP192K1Field.Multiply(t1, x3, t1);
  156. SecP192K1Field.SquareN(t1, 6, t1);
  157. SecP192K1Field.Multiply(t1, x3, t1);
  158. SecP192K1Field.Square(t1, t1);
  159. uint[] t2 = x3;
  160. SecP192K1Field.Square(t1, t2);
  161. return Nat192.Eq(x1, t2) ? new SecP192K1FieldElement(t1) : null;
  162. }
  163. public override bool Equals(object obj)
  164. {
  165. return Equals(obj as SecP192K1FieldElement);
  166. }
  167. public override bool Equals(ECFieldElement other)
  168. {
  169. return Equals(other as SecP192K1FieldElement);
  170. }
  171. public virtual bool Equals(SecP192K1FieldElement other)
  172. {
  173. if (this == other)
  174. return true;
  175. if (null == other)
  176. return false;
  177. return Nat192.Eq(x, other.x);
  178. }
  179. public override int GetHashCode()
  180. {
  181. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 6);
  182. }
  183. }
  184. }
  185. #pragma warning restore
  186. #endif