SecP160R1FieldElement.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 SecP160R1FieldElement
  9. : AbstractFpFieldElement
  10. {
  11. public static readonly BigInteger Q = SecP160R1Curve.q;
  12. protected internal readonly uint[] x;
  13. public SecP160R1FieldElement(BigInteger x)
  14. {
  15. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  16. throw new ArgumentException("value invalid for SecP160R1FieldElement", "x");
  17. this.x = SecP160R1Field.FromBigInteger(x);
  18. }
  19. public SecP160R1FieldElement()
  20. {
  21. this.x = Nat160.Create();
  22. }
  23. protected internal SecP160R1FieldElement(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 "SecP160R1Field"; }
  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. SecP160R1Field.Add(x, ((SecP160R1FieldElement)b).x, z);
  55. return new SecP160R1FieldElement(z);
  56. }
  57. public override ECFieldElement AddOne()
  58. {
  59. uint[] z = Nat160.Create();
  60. SecP160R1Field.AddOne(x, z);
  61. return new SecP160R1FieldElement(z);
  62. }
  63. public override ECFieldElement Subtract(ECFieldElement b)
  64. {
  65. uint[] z = Nat160.Create();
  66. SecP160R1Field.Subtract(x, ((SecP160R1FieldElement)b).x, z);
  67. return new SecP160R1FieldElement(z);
  68. }
  69. public override ECFieldElement Multiply(ECFieldElement b)
  70. {
  71. uint[] z = Nat160.Create();
  72. SecP160R1Field.Multiply(x, ((SecP160R1FieldElement)b).x, z);
  73. return new SecP160R1FieldElement(z);
  74. }
  75. public override ECFieldElement Divide(ECFieldElement b)
  76. {
  77. // return multiply(b.invert());
  78. uint[] z = Nat160.Create();
  79. Mod.Invert(SecP160R1Field.P, ((SecP160R1FieldElement)b).x, z);
  80. SecP160R1Field.Multiply(z, x, z);
  81. return new SecP160R1FieldElement(z);
  82. }
  83. public override ECFieldElement Negate()
  84. {
  85. uint[] z = Nat160.Create();
  86. SecP160R1Field.Negate(x, z);
  87. return new SecP160R1FieldElement(z);
  88. }
  89. public override ECFieldElement Square()
  90. {
  91. uint[] z = Nat160.Create();
  92. SecP160R1Field.Square(x, z);
  93. return new SecP160R1FieldElement(z);
  94. }
  95. public override ECFieldElement Invert()
  96. {
  97. // return new SecP160R1FieldElement(ToBigInteger().modInverse(Q));
  98. uint[] z = Nat160.Create();
  99. Mod.Invert(SecP160R1Field.P, x, z);
  100. return new SecP160R1FieldElement(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^29
  111. *
  112. * Breaking up the exponent's binary representation into "repunits", we get:
  113. * { 129 1s } { 29 0s }
  114. *
  115. * Therefore we need an addition chain containing 129 (the length of the repunit) We use:
  116. * 1, 2, 4, 8, 16, 32, 64, 128, [129]
  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. SecP160R1Field.Square(x1, x2);
  125. SecP160R1Field.Multiply(x2, x1, x2);
  126. uint[] x4 = Nat160.Create();
  127. SecP160R1Field.SquareN(x2, 2, x4);
  128. SecP160R1Field.Multiply(x4, x2, x4);
  129. uint[] x8 = x2;
  130. SecP160R1Field.SquareN(x4, 4, x8);
  131. SecP160R1Field.Multiply(x8, x4, x8);
  132. uint[] x16 = x4;
  133. SecP160R1Field.SquareN(x8, 8, x16);
  134. SecP160R1Field.Multiply(x16, x8, x16);
  135. uint[] x32 = x8;
  136. SecP160R1Field.SquareN(x16, 16, x32);
  137. SecP160R1Field.Multiply(x32, x16, x32);
  138. uint[] x64 = x16;
  139. SecP160R1Field.SquareN(x32, 32, x64);
  140. SecP160R1Field.Multiply(x64, x32, x64);
  141. uint[] x128 = x32;
  142. SecP160R1Field.SquareN(x64, 64, x128);
  143. SecP160R1Field.Multiply(x128, x64, x128);
  144. uint[] x129 = x64;
  145. SecP160R1Field.Square(x128, x129);
  146. SecP160R1Field.Multiply(x129, x1, x129);
  147. uint[] t1 = x129;
  148. SecP160R1Field.SquareN(t1, 29, t1);
  149. uint[] t2 = x128;
  150. SecP160R1Field.Square(t1, t2);
  151. return Nat160.Eq(x1, t2) ? new SecP160R1FieldElement(t1) : null;
  152. }
  153. public override bool Equals(object obj)
  154. {
  155. return Equals(obj as SecP160R1FieldElement);
  156. }
  157. public override bool Equals(ECFieldElement other)
  158. {
  159. return Equals(other as SecP160R1FieldElement);
  160. }
  161. public virtual bool Equals(SecP160R1FieldElement other)
  162. {
  163. if (this == other)
  164. return true;
  165. if (null == other)
  166. return false;
  167. return Nat160.Eq(x, other.x);
  168. }
  169. public override int GetHashCode()
  170. {
  171. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 5);
  172. }
  173. }
  174. }
  175. #pragma warning restore
  176. #endif