SecP256K1FieldElement.cs 6.7 KB

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