SecP224K1FieldElement.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  8. namespace BestHTTP.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. //return new SecP224K1FieldElement(ToBigInteger().ModInverse(Q));
  104. uint[] z = Nat224.Create();
  105. SecP224K1Field.Inv(x, z);
  106. return new SecP224K1FieldElement(z);
  107. }
  108. /**
  109. * return a sqrt root - the routine verifies that the calculation returns the right value - if
  110. * none exists it returns null.
  111. */
  112. public override ECFieldElement Sqrt()
  113. {
  114. /*
  115. * Q == 8m + 5, so we use Pocklington's method for this case.
  116. *
  117. * 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)
  118. *
  119. * Breaking up the exponent's binary representation into "repunits", we get:
  120. * { 191 1s } { 1 0s } { 19 1s } { 2 0s } { 1 1s } { 1 0s } { 1 1s } { 1 0s } { 3 1s } { 1 0s }
  121. *
  122. * Therefore we need an addition chain containing 1, 3, 19, 191 (the lengths of the repunits)
  123. * We use: [1], 2, [3], 4, 8, 11, [19], 23, 42, 84, 107, [191]
  124. */
  125. uint[] x1 = this.x;
  126. if (Nat224.IsZero(x1) || Nat224.IsOne(x1))
  127. return this;
  128. uint[] x2 = Nat224.Create();
  129. SecP224K1Field.Square(x1, x2);
  130. SecP224K1Field.Multiply(x2, x1, x2);
  131. uint[] x3 = x2;
  132. SecP224K1Field.Square(x2, x3);
  133. SecP224K1Field.Multiply(x3, x1, x3);
  134. uint[] x4 = Nat224.Create();
  135. SecP224K1Field.Square(x3, x4);
  136. SecP224K1Field.Multiply(x4, x1, x4);
  137. uint[] x8 = Nat224.Create();
  138. SecP224K1Field.SquareN(x4, 4, x8);
  139. SecP224K1Field.Multiply(x8, x4, x8);
  140. uint[] x11 = Nat224.Create();
  141. SecP224K1Field.SquareN(x8, 3, x11);
  142. SecP224K1Field.Multiply(x11, x3, x11);
  143. uint[] x19 = x11;
  144. SecP224K1Field.SquareN(x11, 8, x19);
  145. SecP224K1Field.Multiply(x19, x8, x19);
  146. uint[] x23 = x8;
  147. SecP224K1Field.SquareN(x19, 4, x23);
  148. SecP224K1Field.Multiply(x23, x4, x23);
  149. uint[] x42 = x4;
  150. SecP224K1Field.SquareN(x23, 19, x42);
  151. SecP224K1Field.Multiply(x42, x19, x42);
  152. uint[] x84 = Nat224.Create();
  153. SecP224K1Field.SquareN(x42, 42, x84);
  154. SecP224K1Field.Multiply(x84, x42, x84);
  155. uint[] x107 = x42;
  156. SecP224K1Field.SquareN(x84, 23, x107);
  157. SecP224K1Field.Multiply(x107, x23, x107);
  158. uint[] x191 = x23;
  159. SecP224K1Field.SquareN(x107, 84, x191);
  160. SecP224K1Field.Multiply(x191, x84, x191);
  161. uint[] t1 = x191;
  162. SecP224K1Field.SquareN(t1, 20, t1);
  163. SecP224K1Field.Multiply(t1, x19, t1);
  164. SecP224K1Field.SquareN(t1, 3, t1);
  165. SecP224K1Field.Multiply(t1, x1, t1);
  166. SecP224K1Field.SquareN(t1, 2, t1);
  167. SecP224K1Field.Multiply(t1, x1, t1);
  168. SecP224K1Field.SquareN(t1, 4, t1);
  169. SecP224K1Field.Multiply(t1, x3, t1);
  170. SecP224K1Field.Square(t1, t1);
  171. uint[] t2 = x84;
  172. SecP224K1Field.Square(t1, t2);
  173. if (Nat224.Eq(x1, t2))
  174. {
  175. return new SecP224K1FieldElement(t1);
  176. }
  177. /*
  178. * If the first guess is incorrect, we multiply by a precomputed power of 2 to get the second guess,
  179. * which is ((4x)^(m + 1))/2 mod Q
  180. */
  181. SecP224K1Field.Multiply(t1, PRECOMP_POW2, t1);
  182. SecP224K1Field.Square(t1, t2);
  183. if (Nat224.Eq(x1, t2))
  184. {
  185. return new SecP224K1FieldElement(t1);
  186. }
  187. return null;
  188. }
  189. public override bool Equals(object obj)
  190. {
  191. return Equals(obj as SecP224K1FieldElement);
  192. }
  193. public override bool Equals(ECFieldElement other)
  194. {
  195. return Equals(other as SecP224K1FieldElement);
  196. }
  197. public virtual bool Equals(SecP224K1FieldElement other)
  198. {
  199. if (this == other)
  200. return true;
  201. if (null == other)
  202. return false;
  203. return Nat224.Eq(x, other.x);
  204. }
  205. public override int GetHashCode()
  206. {
  207. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 7);
  208. }
  209. }
  210. }
  211. #pragma warning restore
  212. #endif