SecP160R2FieldElement.cs 6.9 KB

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