SecP160R1FieldElement.cs 6.3 KB

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