SecP224K1FieldElement.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  8. {
  9. internal class SecP224K1FieldElement
  10. : AbstractFpFieldElement
  11. {
  12. public static readonly BigInteger Q = SecP224K1Curve.q;
  13. // Calculated as BigInteger.Two.ModPow(Q.ShiftRight(2), Q)
  14. private static readonly uint[] PRECOMP_POW2 = new uint[]{ 0x33bfd202, 0xdcfad133, 0x2287624a, 0xc3811ba8,
  15. 0xa85558fc, 0x1eaef5d7, 0x8edf154c };
  16. protected internal readonly uint[] x;
  17. public SecP224K1FieldElement(BigInteger x)
  18. {
  19. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  20. throw new ArgumentException("value invalid for SecP224K1FieldElement", "x");
  21. this.x = SecP224K1Field.FromBigInteger(x);
  22. }
  23. public SecP224K1FieldElement()
  24. {
  25. this.x = Nat224.Create();
  26. }
  27. protected internal SecP224K1FieldElement(uint[] x)
  28. {
  29. this.x = x;
  30. }
  31. public override bool IsZero
  32. {
  33. get { return Nat224.IsZero(x); }
  34. }
  35. public override bool IsOne
  36. {
  37. get { return Nat224.IsOne(x); }
  38. }
  39. public override bool TestBitZero()
  40. {
  41. return Nat224.GetBit(x, 0) == 1;
  42. }
  43. public override BigInteger ToBigInteger()
  44. {
  45. return Nat224.ToBigInteger(x);
  46. }
  47. public override string FieldName
  48. {
  49. get { return "SecP224K1Field"; }
  50. }
  51. public override int FieldSize
  52. {
  53. get { return Q.BitLength; }
  54. }
  55. public override ECFieldElement Add(ECFieldElement b)
  56. {
  57. uint[] z = Nat224.Create();
  58. SecP224K1Field.Add(x, ((SecP224K1FieldElement)b).x, z);
  59. return new SecP224K1FieldElement(z);
  60. }
  61. public override ECFieldElement AddOne()
  62. {
  63. uint[] z = Nat224.Create();
  64. SecP224K1Field.AddOne(x, z);
  65. return new SecP224K1FieldElement(z);
  66. }
  67. public override ECFieldElement Subtract(ECFieldElement b)
  68. {
  69. uint[] z = Nat224.Create();
  70. SecP224K1Field.Subtract(x, ((SecP224K1FieldElement)b).x, z);
  71. return new SecP224K1FieldElement(z);
  72. }
  73. public override ECFieldElement Multiply(ECFieldElement b)
  74. {
  75. uint[] z = Nat224.Create();
  76. SecP224K1Field.Multiply(x, ((SecP224K1FieldElement)b).x, z);
  77. return new SecP224K1FieldElement(z);
  78. }
  79. public override ECFieldElement Divide(ECFieldElement b)
  80. {
  81. //return Multiply(b.Invert());
  82. uint[] z = Nat224.Create();
  83. Mod.Invert(SecP224K1Field.P, ((SecP224K1FieldElement)b).x, z);
  84. SecP224K1Field.Multiply(z, x, z);
  85. return new SecP224K1FieldElement(z);
  86. }
  87. public override ECFieldElement Negate()
  88. {
  89. uint[] z = Nat224.Create();
  90. SecP224K1Field.Negate(x, z);
  91. return new SecP224K1FieldElement(z);
  92. }
  93. public override ECFieldElement Square()
  94. {
  95. uint[] z = Nat224.Create();
  96. SecP224K1Field.Square(x, z);
  97. return new SecP224K1FieldElement(z);
  98. }
  99. public override ECFieldElement Invert()
  100. {
  101. //return new SecP224K1FieldElement(ToBigInteger().ModInverse(Q));
  102. uint[] z = Nat224.Create();
  103. Mod.Invert(SecP224K1Field.P, x, z);
  104. return new SecP224K1FieldElement(z);
  105. }
  106. /**
  107. * return a sqrt root - the routine verifies that the calculation returns the right value - if
  108. * none exists it returns null.
  109. */
  110. public override ECFieldElement Sqrt()
  111. {
  112. /*
  113. * Q == 8m + 5, so we use Pocklington's method for this case.
  114. *
  115. * First, raise this element to the exponent 2^221 - 2^29 - 2^9 - 2^8 - 2^6 - 2^4 - 2^1 (i.e. m + 1)
  116. *
  117. * Breaking up the exponent's binary representation into "repunits", we get:
  118. * { 191 1s } { 1 0s } { 19 1s } { 2 0s } { 1 1s } { 1 0s} { 1 1s } { 1 0s} { 3 1s } { 1 0s}
  119. *
  120. * Therefore we need an addition chain containing 1, 3, 19, 191 (the lengths of the repunits)
  121. * We use: [1], 2, [3], 4, 8, 11, [19], 23, 42, 84, 107, [191]
  122. */
  123. uint[] x1 = this.x;
  124. if (Nat224.IsZero(x1) || Nat224.IsOne(x1))
  125. return this;
  126. uint[] x2 = Nat224.Create();
  127. SecP224K1Field.Square(x1, x2);
  128. SecP224K1Field.Multiply(x2, x1, x2);
  129. uint[] x3 = x2;
  130. SecP224K1Field.Square(x2, x3);
  131. SecP224K1Field.Multiply(x3, x1, x3);
  132. uint[] x4 = Nat224.Create();
  133. SecP224K1Field.Square(x3, x4);
  134. SecP224K1Field.Multiply(x4, x1, x4);
  135. uint[] x8 = Nat224.Create();
  136. SecP224K1Field.SquareN(x4, 4, x8);
  137. SecP224K1Field.Multiply(x8, x4, x8);
  138. uint[] x11 = Nat224.Create();
  139. SecP224K1Field.SquareN(x8, 3, x11);
  140. SecP224K1Field.Multiply(x11, x3, x11);
  141. uint[] x19 = x11;
  142. SecP224K1Field.SquareN(x11, 8, x19);
  143. SecP224K1Field.Multiply(x19, x8, x19);
  144. uint[] x23 = x8;
  145. SecP224K1Field.SquareN(x19, 4, x23);
  146. SecP224K1Field.Multiply(x23, x4, x23);
  147. uint[] x42 = x4;
  148. SecP224K1Field.SquareN(x23, 19, x42);
  149. SecP224K1Field.Multiply(x42, x19, x42);
  150. uint[] x84 = Nat224.Create();
  151. SecP224K1Field.SquareN(x42, 42, x84);
  152. SecP224K1Field.Multiply(x84, x42, x84);
  153. uint[] x107 = x42;
  154. SecP224K1Field.SquareN(x84, 23, x107);
  155. SecP224K1Field.Multiply(x107, x23, x107);
  156. uint[] x191 = x23;
  157. SecP224K1Field.SquareN(x107, 84, x191);
  158. SecP224K1Field.Multiply(x191, x84, x191);
  159. uint[] t1 = x191;
  160. SecP224K1Field.SquareN(t1, 20, t1);
  161. SecP224K1Field.Multiply(t1, x19, t1);
  162. SecP224K1Field.SquareN(t1, 3, t1);
  163. SecP224K1Field.Multiply(t1, x1, t1);
  164. SecP224K1Field.SquareN(t1, 2, t1);
  165. SecP224K1Field.Multiply(t1, x1, t1);
  166. SecP224K1Field.SquareN(t1, 4, t1);
  167. SecP224K1Field.Multiply(t1, x3, t1);
  168. SecP224K1Field.Square(t1, t1);
  169. uint[] t2 = x84;
  170. SecP224K1Field.Square(t1, t2);
  171. if (Nat224.Eq(x1, t2))
  172. {
  173. return new SecP224K1FieldElement(t1);
  174. }
  175. /*
  176. * If the first guess is incorrect, we multiply by a precomputed power of 2 to get the second guess,
  177. * which is ((4x)^(m + 1))/2 mod Q
  178. */
  179. SecP224K1Field.Multiply(t1, PRECOMP_POW2, t1);
  180. SecP224K1Field.Square(t1, t2);
  181. if (Nat224.Eq(x1, t2))
  182. {
  183. return new SecP224K1FieldElement(t1);
  184. }
  185. return null;
  186. }
  187. public override bool Equals(object obj)
  188. {
  189. return Equals(obj as SecP224K1FieldElement);
  190. }
  191. public override bool Equals(ECFieldElement other)
  192. {
  193. return Equals(other as SecP224K1FieldElement);
  194. }
  195. public virtual bool Equals(SecP224K1FieldElement other)
  196. {
  197. if (this == other)
  198. return true;
  199. if (null == other)
  200. return false;
  201. return Nat224.Eq(x, other.x);
  202. }
  203. public override int GetHashCode()
  204. {
  205. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 7);
  206. }
  207. }
  208. }
  209. #pragma warning restore
  210. #endif