SM2P256V1FieldElement.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.GM
  7. {
  8. internal class SM2P256V1FieldElement
  9. : AbstractFpFieldElement
  10. {
  11. public static readonly BigInteger Q = SM2P256V1Curve.q;
  12. protected internal readonly uint[] x;
  13. public SM2P256V1FieldElement(BigInteger x)
  14. {
  15. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  16. throw new ArgumentException("value invalid for SM2P256V1FieldElement", "x");
  17. this.x = SM2P256V1Field.FromBigInteger(x);
  18. }
  19. public SM2P256V1FieldElement()
  20. {
  21. this.x = Nat256.Create();
  22. }
  23. protected internal SM2P256V1FieldElement(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 "SM2P256V1Field"; }
  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. SM2P256V1Field.Add(x, ((SM2P256V1FieldElement)b).x, z);
  55. return new SM2P256V1FieldElement(z);
  56. }
  57. public override ECFieldElement AddOne()
  58. {
  59. uint[] z = Nat256.Create();
  60. SM2P256V1Field.AddOne(x, z);
  61. return new SM2P256V1FieldElement(z);
  62. }
  63. public override ECFieldElement Subtract(ECFieldElement b)
  64. {
  65. uint[] z = Nat256.Create();
  66. SM2P256V1Field.Subtract(x, ((SM2P256V1FieldElement)b).x, z);
  67. return new SM2P256V1FieldElement(z);
  68. }
  69. public override ECFieldElement Multiply(ECFieldElement b)
  70. {
  71. uint[] z = Nat256.Create();
  72. SM2P256V1Field.Multiply(x, ((SM2P256V1FieldElement)b).x, z);
  73. return new SM2P256V1FieldElement(z);
  74. }
  75. public override ECFieldElement Divide(ECFieldElement b)
  76. {
  77. //return Multiply(b.Invert());
  78. uint[] z = Nat256.Create();
  79. Mod.Invert(SM2P256V1Field.P, ((SM2P256V1FieldElement)b).x, z);
  80. SM2P256V1Field.Multiply(z, x, z);
  81. return new SM2P256V1FieldElement(z);
  82. }
  83. public override ECFieldElement Negate()
  84. {
  85. uint[] z = Nat256.Create();
  86. SM2P256V1Field.Negate(x, z);
  87. return new SM2P256V1FieldElement(z);
  88. }
  89. public override ECFieldElement Square()
  90. {
  91. uint[] z = Nat256.Create();
  92. SM2P256V1Field.Square(x, z);
  93. return new SM2P256V1FieldElement(z);
  94. }
  95. public override ECFieldElement Invert()
  96. {
  97. //return new SM2P256V1FieldElement(ToBigInteger().ModInverse(Q));
  98. uint[] z = Nat256.Create();
  99. Mod.Invert(SM2P256V1Field.P, x, z);
  100. return new SM2P256V1FieldElement(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^222 - 2^94 + 2^62
  110. *
  111. * Breaking up the exponent's binary representation into "repunits", we get:
  112. * { 31 1s } { 1 0s } { 128 1s } { 31 0s } { 1 1s } { 62 0s}
  113. *
  114. * We use an addition chain for the beginning: [1], 2, 3, 6, 12, [24], 30, [31]
  115. */
  116. uint[] x1 = this.x;
  117. if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
  118. {
  119. return this;
  120. }
  121. uint[] x2 = Nat256.Create();
  122. SM2P256V1Field.Square(x1, x2);
  123. SM2P256V1Field.Multiply(x2, x1, x2);
  124. uint[] x4 = Nat256.Create();
  125. SM2P256V1Field.SquareN(x2, 2, x4);
  126. SM2P256V1Field.Multiply(x4, x2, x4);
  127. uint[] x6 = Nat256.Create();
  128. SM2P256V1Field.SquareN(x4, 2, x6);
  129. SM2P256V1Field.Multiply(x6, x2, x6);
  130. uint[] x12 = x2;
  131. SM2P256V1Field.SquareN(x6, 6, x12);
  132. SM2P256V1Field.Multiply(x12, x6, x12);
  133. uint[] x24 = Nat256.Create();
  134. SM2P256V1Field.SquareN(x12, 12, x24);
  135. SM2P256V1Field.Multiply(x24, x12, x24);
  136. uint[] x30 = x12;
  137. SM2P256V1Field.SquareN(x24, 6, x30);
  138. SM2P256V1Field.Multiply(x30, x6, x30);
  139. uint[] x31 = x6;
  140. SM2P256V1Field.Square(x30, x31);
  141. SM2P256V1Field.Multiply(x31, x1, x31);
  142. uint[] t1 = x24;
  143. SM2P256V1Field.SquareN(x31, 31, t1);
  144. uint[] x62 = x30;
  145. SM2P256V1Field.Multiply(t1, x31, x62);
  146. SM2P256V1Field.SquareN(t1, 32, t1);
  147. SM2P256V1Field.Multiply(t1, x62, t1);
  148. SM2P256V1Field.SquareN(t1, 62, t1);
  149. SM2P256V1Field.Multiply(t1, x62, t1);
  150. SM2P256V1Field.SquareN(t1, 4, t1);
  151. SM2P256V1Field.Multiply(t1, x4, t1);
  152. SM2P256V1Field.SquareN(t1, 32, t1);
  153. SM2P256V1Field.Multiply(t1, x1, t1);
  154. SM2P256V1Field.SquareN(t1, 62, t1);
  155. uint[] t2 = x4;
  156. SM2P256V1Field.Square(t1, t2);
  157. return Nat256.Eq(x1, t2) ? new SM2P256V1FieldElement(t1) : null;
  158. }
  159. public override bool Equals(object obj)
  160. {
  161. return Equals(obj as SM2P256V1FieldElement);
  162. }
  163. public override bool Equals(ECFieldElement other)
  164. {
  165. return Equals(other as SM2P256V1FieldElement);
  166. }
  167. public virtual bool Equals(SM2P256V1FieldElement other)
  168. {
  169. if (this == other)
  170. return true;
  171. if (null == other)
  172. return false;
  173. return Nat256.Eq(x, other.x);
  174. }
  175. public override int GetHashCode()
  176. {
  177. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
  178. }
  179. }
  180. }
  181. #pragma warning restore
  182. #endif