SecP224K1FieldElement.cs 7.7 KB

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