SecP160R1FieldElement.cs 6.1 KB

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