SecP192K1FieldElement.cs 6.9 KB

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