SecP160R2FieldElement.cs 7.0 KB

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