SM2P256V1FieldElement.cs 6.7 KB

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