SecP256K1FieldElement.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 SecP256K1FieldElement
  10. : AbstractFpFieldElement
  11. {
  12. public static readonly BigInteger Q = SecP256K1Curve.q;
  13. protected internal readonly uint[] x;
  14. public SecP256K1FieldElement(BigInteger x)
  15. {
  16. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  17. throw new ArgumentException("value invalid for SecP256K1FieldElement", "x");
  18. this.x = SecP256K1Field.FromBigInteger(x);
  19. }
  20. public SecP256K1FieldElement()
  21. {
  22. this.x = Nat256.Create();
  23. }
  24. protected internal SecP256K1FieldElement(uint[] x)
  25. {
  26. this.x = x;
  27. }
  28. public override bool IsZero
  29. {
  30. get { return Nat256.IsZero(x); }
  31. }
  32. public override bool IsOne
  33. {
  34. get { return Nat256.IsOne(x); }
  35. }
  36. public override bool TestBitZero()
  37. {
  38. return Nat256.GetBit(x, 0) == 1;
  39. }
  40. public override BigInteger ToBigInteger()
  41. {
  42. return Nat256.ToBigInteger(x);
  43. }
  44. public override string FieldName
  45. {
  46. get { return "SecP256K1Field"; }
  47. }
  48. public override int FieldSize
  49. {
  50. get { return Q.BitLength; }
  51. }
  52. public override ECFieldElement Add(ECFieldElement b)
  53. {
  54. uint[] z = Nat256.Create();
  55. SecP256K1Field.Add(x, ((SecP256K1FieldElement)b).x, z);
  56. return new SecP256K1FieldElement(z);
  57. }
  58. public override ECFieldElement AddOne()
  59. {
  60. uint[] z = Nat256.Create();
  61. SecP256K1Field.AddOne(x, z);
  62. return new SecP256K1FieldElement(z);
  63. }
  64. public override ECFieldElement Subtract(ECFieldElement b)
  65. {
  66. uint[] z = Nat256.Create();
  67. SecP256K1Field.Subtract(x, ((SecP256K1FieldElement)b).x, z);
  68. return new SecP256K1FieldElement(z);
  69. }
  70. public override ECFieldElement Multiply(ECFieldElement b)
  71. {
  72. uint[] z = Nat256.Create();
  73. SecP256K1Field.Multiply(x, ((SecP256K1FieldElement)b).x, z);
  74. return new SecP256K1FieldElement(z);
  75. }
  76. public override ECFieldElement Divide(ECFieldElement b)
  77. {
  78. //return Multiply(b.Invert());
  79. uint[] z = Nat256.Create();
  80. Mod.Invert(SecP256K1Field.P, ((SecP256K1FieldElement)b).x, z);
  81. SecP256K1Field.Multiply(z, x, z);
  82. return new SecP256K1FieldElement(z);
  83. }
  84. public override ECFieldElement Negate()
  85. {
  86. uint[] z = Nat256.Create();
  87. SecP256K1Field.Negate(x, z);
  88. return new SecP256K1FieldElement(z);
  89. }
  90. public override ECFieldElement Square()
  91. {
  92. uint[] z = Nat256.Create();
  93. SecP256K1Field.Square(x, z);
  94. return new SecP256K1FieldElement(z);
  95. }
  96. public override ECFieldElement Invert()
  97. {
  98. //return new SecP256K1FieldElement(ToBigInteger().ModInverse(Q));
  99. uint[] z = Nat256.Create();
  100. Mod.Invert(SecP256K1Field.P, x, z);
  101. return new SecP256K1FieldElement(z);
  102. }
  103. /**
  104. * return a sqrt root - the routine verifies that the calculation returns the right value - if
  105. * none exists it returns null.
  106. */
  107. public override ECFieldElement Sqrt()
  108. {
  109. /*
  110. * Raise this element to the exponent 2^254 - 2^30 - 2^7 - 2^6 - 2^5 - 2^4 - 2^2
  111. *
  112. * Breaking up the exponent's binary representation into "repunits", we get:
  113. * { 223 1s } { 1 0s } { 22 1s } { 4 0s } { 2 1s } { 2 0s}
  114. *
  115. * Therefore we need an addition chain containing 2, 22, 223 (the lengths of the repunits)
  116. * We use: 1, [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
  117. */
  118. uint[] x1 = this.x;
  119. if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
  120. return this;
  121. uint[] x2 = Nat256.Create();
  122. SecP256K1Field.Square(x1, x2);
  123. SecP256K1Field.Multiply(x2, x1, x2);
  124. uint[] x3 = Nat256.Create();
  125. SecP256K1Field.Square(x2, x3);
  126. SecP256K1Field.Multiply(x3, x1, x3);
  127. uint[] x6 = Nat256.Create();
  128. SecP256K1Field.SquareN(x3, 3, x6);
  129. SecP256K1Field.Multiply(x6, x3, x6);
  130. uint[] x9 = x6;
  131. SecP256K1Field.SquareN(x6, 3, x9);
  132. SecP256K1Field.Multiply(x9, x3, x9);
  133. uint[] x11 = x9;
  134. SecP256K1Field.SquareN(x9, 2, x11);
  135. SecP256K1Field.Multiply(x11, x2, x11);
  136. uint[] x22 = Nat256.Create();
  137. SecP256K1Field.SquareN(x11, 11, x22);
  138. SecP256K1Field.Multiply(x22, x11, x22);
  139. uint[] x44 = x11;
  140. SecP256K1Field.SquareN(x22, 22, x44);
  141. SecP256K1Field.Multiply(x44, x22, x44);
  142. uint[] x88 = Nat256.Create();
  143. SecP256K1Field.SquareN(x44, 44, x88);
  144. SecP256K1Field.Multiply(x88, x44, x88);
  145. uint[] x176 = Nat256.Create();
  146. SecP256K1Field.SquareN(x88, 88, x176);
  147. SecP256K1Field.Multiply(x176, x88, x176);
  148. uint[] x220 = x88;
  149. SecP256K1Field.SquareN(x176, 44, x220);
  150. SecP256K1Field.Multiply(x220, x44, x220);
  151. uint[] x223 = x44;
  152. SecP256K1Field.SquareN(x220, 3, x223);
  153. SecP256K1Field.Multiply(x223, x3, x223);
  154. uint[] t1 = x223;
  155. SecP256K1Field.SquareN(t1, 23, t1);
  156. SecP256K1Field.Multiply(t1, x22, t1);
  157. SecP256K1Field.SquareN(t1, 6, t1);
  158. SecP256K1Field.Multiply(t1, x2, t1);
  159. SecP256K1Field.SquareN(t1, 2, t1);
  160. uint[] t2 = x2;
  161. SecP256K1Field.Square(t1, t2);
  162. return Nat256.Eq(x1, t2) ? new SecP256K1FieldElement(t1) : null;
  163. }
  164. public override bool Equals(object obj)
  165. {
  166. return Equals(obj as SecP256K1FieldElement);
  167. }
  168. public override bool Equals(ECFieldElement other)
  169. {
  170. return Equals(other as SecP256K1FieldElement);
  171. }
  172. public virtual bool Equals(SecP256K1FieldElement other)
  173. {
  174. if (this == other)
  175. return true;
  176. if (null == other)
  177. return false;
  178. return Nat256.Eq(x, other.x);
  179. }
  180. public override int GetHashCode()
  181. {
  182. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
  183. }
  184. }
  185. }
  186. #pragma warning restore
  187. #endif