SecP256K1FieldElement.cs 7.0 KB

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