SecP224K1FieldElement.cs 7.9 KB

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