SecT233FieldElement.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Math.Raw;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Math.EC.Custom.Sec
  6. {
  7. internal class SecT233FieldElement
  8. : ECFieldElement
  9. {
  10. protected readonly ulong[] x;
  11. public SecT233FieldElement(BigInteger x)
  12. {
  13. if (x == null || x.SignValue < 0 || x.BitLength > 233)
  14. throw new ArgumentException("value invalid for SecT233FieldElement", "x");
  15. this.x = SecT233Field.FromBigInteger(x);
  16. }
  17. public SecT233FieldElement()
  18. {
  19. this.x = Nat256.Create64();
  20. }
  21. protected internal SecT233FieldElement(ulong[] x)
  22. {
  23. this.x = x;
  24. }
  25. public override bool IsOne
  26. {
  27. get { return Nat256.IsOne64(x); }
  28. }
  29. public override bool IsZero
  30. {
  31. get { return Nat256.IsZero64(x); }
  32. }
  33. public override bool TestBitZero()
  34. {
  35. return (x[0] & 1UL) != 0UL;
  36. }
  37. public override BigInteger ToBigInteger()
  38. {
  39. return Nat256.ToBigInteger64(x);
  40. }
  41. public override string FieldName
  42. {
  43. get { return "SecT233Field"; }
  44. }
  45. public override int FieldSize
  46. {
  47. get { return 233; }
  48. }
  49. public override ECFieldElement Add(ECFieldElement b)
  50. {
  51. ulong[] z = Nat256.Create64();
  52. SecT233Field.Add(x, ((SecT233FieldElement)b).x, z);
  53. return new SecT233FieldElement(z);
  54. }
  55. public override ECFieldElement AddOne()
  56. {
  57. ulong[] z = Nat256.Create64();
  58. SecT233Field.AddOne(x, z);
  59. return new SecT233FieldElement(z);
  60. }
  61. public override ECFieldElement Subtract(ECFieldElement b)
  62. {
  63. // Addition and Subtraction are the same in F2m
  64. return Add(b);
  65. }
  66. public override ECFieldElement Multiply(ECFieldElement b)
  67. {
  68. ulong[] z = Nat256.Create64();
  69. SecT233Field.Multiply(x, ((SecT233FieldElement)b).x, z);
  70. return new SecT233FieldElement(z);
  71. }
  72. public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  73. {
  74. return MultiplyPlusProduct(b, x, y);
  75. }
  76. public override ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  77. {
  78. ulong[] ax = this.x, bx = ((SecT233FieldElement)b).x;
  79. ulong[] xx = ((SecT233FieldElement)x).x, yx = ((SecT233FieldElement)y).x;
  80. ulong[] tt = Nat256.CreateExt64();
  81. SecT233Field.MultiplyAddToExt(ax, bx, tt);
  82. SecT233Field.MultiplyAddToExt(xx, yx, tt);
  83. ulong[] z = Nat256.Create64();
  84. SecT233Field.Reduce(tt, z);
  85. return new SecT233FieldElement(z);
  86. }
  87. public override ECFieldElement Divide(ECFieldElement b)
  88. {
  89. return Multiply(b.Invert());
  90. }
  91. public override ECFieldElement Negate()
  92. {
  93. return this;
  94. }
  95. public override ECFieldElement Square()
  96. {
  97. ulong[] z = Nat256.Create64();
  98. SecT233Field.Square(x, z);
  99. return new SecT233FieldElement(z);
  100. }
  101. public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
  102. {
  103. return SquarePlusProduct(x, y);
  104. }
  105. public override ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
  106. {
  107. ulong[] ax = this.x;
  108. ulong[] xx = ((SecT233FieldElement)x).x, yx = ((SecT233FieldElement)y).x;
  109. ulong[] tt = Nat256.CreateExt64();
  110. SecT233Field.SquareAddToExt(ax, tt);
  111. SecT233Field.MultiplyAddToExt(xx, yx, tt);
  112. ulong[] z = Nat256.Create64();
  113. SecT233Field.Reduce(tt, z);
  114. return new SecT233FieldElement(z);
  115. }
  116. public override ECFieldElement SquarePow(int pow)
  117. {
  118. if (pow < 1)
  119. return this;
  120. ulong[] z = Nat256.Create64();
  121. SecT233Field.SquareN(x, pow, z);
  122. return new SecT233FieldElement(z);
  123. }
  124. public override ECFieldElement Invert()
  125. {
  126. ulong[] z = Nat256.Create64();
  127. SecT233Field.Invert(x, z);
  128. return new SecT233FieldElement(z);
  129. }
  130. public override ECFieldElement Sqrt()
  131. {
  132. ulong[] z = Nat256.Create64();
  133. SecT233Field.Sqrt(x, z);
  134. return new SecT233FieldElement(z);
  135. }
  136. public virtual int Representation
  137. {
  138. get { return F2mFieldElement.Tpb; }
  139. }
  140. public virtual int M
  141. {
  142. get { return 233; }
  143. }
  144. public virtual int K1
  145. {
  146. get { return 74; }
  147. }
  148. public virtual int K2
  149. {
  150. get { return 0; }
  151. }
  152. public virtual int K3
  153. {
  154. get { return 0; }
  155. }
  156. public override bool Equals(object obj)
  157. {
  158. return Equals(obj as SecT233FieldElement);
  159. }
  160. public override bool Equals(ECFieldElement other)
  161. {
  162. return Equals(other as SecT233FieldElement);
  163. }
  164. public virtual bool Equals(SecT233FieldElement other)
  165. {
  166. if (this == other)
  167. return true;
  168. if (null == other)
  169. return false;
  170. return Nat256.Eq64(x, other.x);
  171. }
  172. public override int GetHashCode()
  173. {
  174. return 2330074 ^ Arrays.GetHashCode(x, 0, 4);
  175. }
  176. }
  177. }
  178. #endif