ECFieldElement.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC
  8. {
  9. public abstract class ECFieldElement
  10. {
  11. public abstract BigInteger ToBigInteger();
  12. public abstract string FieldName { get; }
  13. public abstract int FieldSize { get; }
  14. public abstract ECFieldElement Add(ECFieldElement b);
  15. public abstract ECFieldElement AddOne();
  16. public abstract ECFieldElement Subtract(ECFieldElement b);
  17. public abstract ECFieldElement Multiply(ECFieldElement b);
  18. public abstract ECFieldElement Divide(ECFieldElement b);
  19. public abstract ECFieldElement Negate();
  20. public abstract ECFieldElement Square();
  21. public abstract ECFieldElement Invert();
  22. public abstract ECFieldElement Sqrt();
  23. public virtual int BitLength
  24. {
  25. get { return ToBigInteger().BitLength; }
  26. }
  27. public virtual bool IsOne
  28. {
  29. get { return BitLength == 1; }
  30. }
  31. public virtual bool IsZero
  32. {
  33. get { return 0 == ToBigInteger().SignValue; }
  34. }
  35. public virtual ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  36. {
  37. return Multiply(b).Subtract(x.Multiply(y));
  38. }
  39. public virtual ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  40. {
  41. return Multiply(b).Add(x.Multiply(y));
  42. }
  43. public virtual ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
  44. {
  45. return Square().Subtract(x.Multiply(y));
  46. }
  47. public virtual ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
  48. {
  49. return Square().Add(x.Multiply(y));
  50. }
  51. public virtual ECFieldElement SquarePow(int pow)
  52. {
  53. ECFieldElement r = this;
  54. for (int i = 0; i < pow; ++i)
  55. {
  56. r = r.Square();
  57. }
  58. return r;
  59. }
  60. public virtual bool TestBitZero()
  61. {
  62. return ToBigInteger().TestBit(0);
  63. }
  64. public override bool Equals(object obj)
  65. {
  66. return Equals(obj as ECFieldElement);
  67. }
  68. public virtual bool Equals(ECFieldElement other)
  69. {
  70. if (this == other)
  71. return true;
  72. if (null == other)
  73. return false;
  74. return ToBigInteger().Equals(other.ToBigInteger());
  75. }
  76. public override int GetHashCode()
  77. {
  78. return ToBigInteger().GetHashCode();
  79. }
  80. public override string ToString()
  81. {
  82. return this.ToBigInteger().ToString(16);
  83. }
  84. public virtual byte[] GetEncoded()
  85. {
  86. return BigIntegers.AsUnsignedByteArray((FieldSize + 7) / 8, ToBigInteger());
  87. }
  88. }
  89. public abstract class AbstractFpFieldElement
  90. : ECFieldElement
  91. {
  92. }
  93. public class FpFieldElement
  94. : AbstractFpFieldElement
  95. {
  96. private readonly BigInteger q, r, x;
  97. internal static BigInteger CalculateResidue(BigInteger p)
  98. {
  99. int bitLength = p.BitLength;
  100. if (bitLength >= 96)
  101. {
  102. BigInteger firstWord = p.ShiftRight(bitLength - 64);
  103. if (firstWord.LongValue == -1L)
  104. {
  105. return BigInteger.One.ShiftLeft(bitLength).Subtract(p);
  106. }
  107. if ((bitLength & 7) == 0)
  108. {
  109. return BigInteger.One.ShiftLeft(bitLength << 1).Divide(p).Negate();
  110. }
  111. }
  112. return null;
  113. }
  114. [Obsolete("Use ECCurve.FromBigInteger to construct field elements")]
  115. public FpFieldElement(BigInteger q, BigInteger x)
  116. : this(q, CalculateResidue(q), x)
  117. {
  118. }
  119. internal FpFieldElement(BigInteger q, BigInteger r, BigInteger x)
  120. {
  121. if (x == null || x.SignValue < 0 || x.CompareTo(q) >= 0)
  122. throw new ArgumentException("value invalid in Fp field element", "x");
  123. this.q = q;
  124. this.r = r;
  125. this.x = x;
  126. }
  127. public override BigInteger ToBigInteger()
  128. {
  129. return x;
  130. }
  131. /**
  132. * return the field name for this field.
  133. *
  134. * @return the string "Fp".
  135. */
  136. public override string FieldName
  137. {
  138. get { return "Fp"; }
  139. }
  140. public override int FieldSize
  141. {
  142. get { return q.BitLength; }
  143. }
  144. public BigInteger Q
  145. {
  146. get { return q; }
  147. }
  148. public override ECFieldElement Add(
  149. ECFieldElement b)
  150. {
  151. return new FpFieldElement(q, r, ModAdd(x, b.ToBigInteger()));
  152. }
  153. public override ECFieldElement AddOne()
  154. {
  155. BigInteger x2 = x.Add(BigInteger.One);
  156. if (x2.CompareTo(q) == 0)
  157. {
  158. x2 = BigInteger.Zero;
  159. }
  160. return new FpFieldElement(q, r, x2);
  161. }
  162. public override ECFieldElement Subtract(
  163. ECFieldElement b)
  164. {
  165. return new FpFieldElement(q, r, ModSubtract(x, b.ToBigInteger()));
  166. }
  167. public override ECFieldElement Multiply(
  168. ECFieldElement b)
  169. {
  170. return new FpFieldElement(q, r, ModMult(x, b.ToBigInteger()));
  171. }
  172. public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  173. {
  174. BigInteger ax = this.x, bx = b.ToBigInteger(), xx = x.ToBigInteger(), yx = y.ToBigInteger();
  175. BigInteger ab = ax.Multiply(bx);
  176. BigInteger xy = xx.Multiply(yx);
  177. return new FpFieldElement(q, r, ModReduce(ab.Subtract(xy)));
  178. }
  179. public override ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  180. {
  181. BigInteger ax = this.x, bx = b.ToBigInteger(), xx = x.ToBigInteger(), yx = y.ToBigInteger();
  182. BigInteger ab = ax.Multiply(bx);
  183. BigInteger xy = xx.Multiply(yx);
  184. BigInteger sum = ab.Add(xy);
  185. if (r != null && r.SignValue < 0 && sum.BitLength > (q.BitLength << 1))
  186. {
  187. sum = sum.Subtract(q.ShiftLeft(q.BitLength));
  188. }
  189. return new FpFieldElement(q, r, ModReduce(sum));
  190. }
  191. public override ECFieldElement Divide(
  192. ECFieldElement b)
  193. {
  194. return new FpFieldElement(q, r, ModMult(x, ModInverse(b.ToBigInteger())));
  195. }
  196. public override ECFieldElement Negate()
  197. {
  198. return x.SignValue == 0 ? this : new FpFieldElement(q, r, q.Subtract(x));
  199. }
  200. public override ECFieldElement Square()
  201. {
  202. return new FpFieldElement(q, r, ModMult(x, x));
  203. }
  204. public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
  205. {
  206. BigInteger ax = this.x, xx = x.ToBigInteger(), yx = y.ToBigInteger();
  207. BigInteger aa = ax.Multiply(ax);
  208. BigInteger xy = xx.Multiply(yx);
  209. return new FpFieldElement(q, r, ModReduce(aa.Subtract(xy)));
  210. }
  211. public override ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
  212. {
  213. BigInteger ax = this.x, xx = x.ToBigInteger(), yx = y.ToBigInteger();
  214. BigInteger aa = ax.Multiply(ax);
  215. BigInteger xy = xx.Multiply(yx);
  216. BigInteger sum = aa.Add(xy);
  217. if (r != null && r.SignValue < 0 && sum.BitLength > (q.BitLength << 1))
  218. {
  219. sum = sum.Subtract(q.ShiftLeft(q.BitLength));
  220. }
  221. return new FpFieldElement(q, r, ModReduce(sum));
  222. }
  223. public override ECFieldElement Invert()
  224. {
  225. // TODO Modular inversion can be faster for a (Generalized) Mersenne Prime.
  226. return new FpFieldElement(q, r, ModInverse(x));
  227. }
  228. /**
  229. * return a sqrt root - the routine verifies that the calculation
  230. * returns the right value - if none exists it returns null.
  231. */
  232. public override ECFieldElement Sqrt()
  233. {
  234. if (IsZero || IsOne)
  235. return this;
  236. if (!q.TestBit(0))
  237. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("even value of q");
  238. if (q.TestBit(1)) // q == 4m + 3
  239. {
  240. BigInteger e = q.ShiftRight(2).Add(BigInteger.One);
  241. return CheckSqrt(new FpFieldElement(q, r, x.ModPow(e, q)));
  242. }
  243. if (q.TestBit(2)) // q == 8m + 5
  244. {
  245. BigInteger t1 = x.ModPow(q.ShiftRight(3), q);
  246. BigInteger t2 = ModMult(t1, x);
  247. BigInteger t3 = ModMult(t2, t1);
  248. if (t3.Equals(BigInteger.One))
  249. {
  250. return CheckSqrt(new FpFieldElement(q, r, t2));
  251. }
  252. // TODO This is constant and could be precomputed
  253. BigInteger t4 = BigInteger.Two.ModPow(q.ShiftRight(2), q);
  254. BigInteger y = ModMult(t2, t4);
  255. return CheckSqrt(new FpFieldElement(q, r, y));
  256. }
  257. // q == 8m + 1
  258. BigInteger legendreExponent = q.ShiftRight(1);
  259. if (!(x.ModPow(legendreExponent, q).Equals(BigInteger.One)))
  260. return null;
  261. BigInteger X = this.x;
  262. BigInteger fourX = ModDouble(ModDouble(X)); ;
  263. BigInteger k = legendreExponent.Add(BigInteger.One), qMinusOne = q.Subtract(BigInteger.One);
  264. BigInteger U, V;
  265. do
  266. {
  267. BigInteger P;
  268. do
  269. {
  270. P = BigInteger.Arbitrary(q.BitLength);
  271. }
  272. while (P.CompareTo(q) >= 0
  273. || !ModReduce(P.Multiply(P).Subtract(fourX)).ModPow(legendreExponent, q).Equals(qMinusOne));
  274. BigInteger[] result = LucasSequence(P, X, k);
  275. U = result[0];
  276. V = result[1];
  277. if (ModMult(V, V).Equals(fourX))
  278. {
  279. return new FpFieldElement(q, r, ModHalfAbs(V));
  280. }
  281. }
  282. while (U.Equals(BigInteger.One) || U.Equals(qMinusOne));
  283. return null;
  284. }
  285. private ECFieldElement CheckSqrt(ECFieldElement z)
  286. {
  287. return z.Square().Equals(this) ? z : null;
  288. }
  289. private BigInteger[] LucasSequence(
  290. BigInteger P,
  291. BigInteger Q,
  292. BigInteger k)
  293. {
  294. // TODO Research and apply "common-multiplicand multiplication here"
  295. int n = k.BitLength;
  296. int s = k.GetLowestSetBit();
  297. Debug.Assert(k.TestBit(s));
  298. BigInteger Uh = BigInteger.One;
  299. BigInteger Vl = BigInteger.Two;
  300. BigInteger Vh = P;
  301. BigInteger Ql = BigInteger.One;
  302. BigInteger Qh = BigInteger.One;
  303. for (int j = n - 1; j >= s + 1; --j)
  304. {
  305. Ql = ModMult(Ql, Qh);
  306. if (k.TestBit(j))
  307. {
  308. Qh = ModMult(Ql, Q);
  309. Uh = ModMult(Uh, Vh);
  310. Vl = ModReduce(Vh.Multiply(Vl).Subtract(P.Multiply(Ql)));
  311. Vh = ModReduce(Vh.Multiply(Vh).Subtract(Qh.ShiftLeft(1)));
  312. }
  313. else
  314. {
  315. Qh = Ql;
  316. Uh = ModReduce(Uh.Multiply(Vl).Subtract(Ql));
  317. Vh = ModReduce(Vh.Multiply(Vl).Subtract(P.Multiply(Ql)));
  318. Vl = ModReduce(Vl.Multiply(Vl).Subtract(Ql.ShiftLeft(1)));
  319. }
  320. }
  321. Ql = ModMult(Ql, Qh);
  322. Qh = ModMult(Ql, Q);
  323. Uh = ModReduce(Uh.Multiply(Vl).Subtract(Ql));
  324. Vl = ModReduce(Vh.Multiply(Vl).Subtract(P.Multiply(Ql)));
  325. Ql = ModMult(Ql, Qh);
  326. for (int j = 1; j <= s; ++j)
  327. {
  328. Uh = ModMult(Uh, Vl);
  329. Vl = ModReduce(Vl.Multiply(Vl).Subtract(Ql.ShiftLeft(1)));
  330. Ql = ModMult(Ql, Ql);
  331. }
  332. return new BigInteger[] { Uh, Vl };
  333. }
  334. protected virtual BigInteger ModAdd(BigInteger x1, BigInteger x2)
  335. {
  336. BigInteger x3 = x1.Add(x2);
  337. if (x3.CompareTo(q) >= 0)
  338. {
  339. x3 = x3.Subtract(q);
  340. }
  341. return x3;
  342. }
  343. protected virtual BigInteger ModDouble(BigInteger x)
  344. {
  345. BigInteger _2x = x.ShiftLeft(1);
  346. if (_2x.CompareTo(q) >= 0)
  347. {
  348. _2x = _2x.Subtract(q);
  349. }
  350. return _2x;
  351. }
  352. protected virtual BigInteger ModHalf(BigInteger x)
  353. {
  354. if (x.TestBit(0))
  355. {
  356. x = q.Add(x);
  357. }
  358. return x.ShiftRight(1);
  359. }
  360. protected virtual BigInteger ModHalfAbs(BigInteger x)
  361. {
  362. if (x.TestBit(0))
  363. {
  364. x = q.Subtract(x);
  365. }
  366. return x.ShiftRight(1);
  367. }
  368. protected virtual BigInteger ModInverse(BigInteger x)
  369. {
  370. int bits = FieldSize;
  371. int len = (bits + 31) >> 5;
  372. uint[] p = Nat.FromBigInteger(bits, q);
  373. uint[] n = Nat.FromBigInteger(bits, x);
  374. uint[] z = Nat.Create(len);
  375. Mod.Invert(p, n, z);
  376. return Nat.ToBigInteger(len, z);
  377. }
  378. protected virtual BigInteger ModMult(BigInteger x1, BigInteger x2)
  379. {
  380. return ModReduce(x1.Multiply(x2));
  381. }
  382. protected virtual BigInteger ModReduce(BigInteger x)
  383. {
  384. if (r == null)
  385. {
  386. x = x.Mod(q);
  387. }
  388. else
  389. {
  390. bool negative = x.SignValue < 0;
  391. if (negative)
  392. {
  393. x = x.Abs();
  394. }
  395. int qLen = q.BitLength;
  396. if (r.SignValue > 0)
  397. {
  398. BigInteger qMod = BigInteger.One.ShiftLeft(qLen);
  399. bool rIsOne = r.Equals(BigInteger.One);
  400. while (x.BitLength > (qLen + 1))
  401. {
  402. BigInteger u = x.ShiftRight(qLen);
  403. BigInteger v = x.Remainder(qMod);
  404. if (!rIsOne)
  405. {
  406. u = u.Multiply(r);
  407. }
  408. x = u.Add(v);
  409. }
  410. }
  411. else
  412. {
  413. int d = ((qLen - 1) & 31) + 1;
  414. BigInteger mu = r.Negate();
  415. BigInteger u = mu.Multiply(x.ShiftRight(qLen - d));
  416. BigInteger quot = u.ShiftRight(qLen + d);
  417. BigInteger v = quot.Multiply(q);
  418. BigInteger bk1 = BigInteger.One.ShiftLeft(qLen + d);
  419. v = v.Remainder(bk1);
  420. x = x.Remainder(bk1);
  421. x = x.Subtract(v);
  422. if (x.SignValue < 0)
  423. {
  424. x = x.Add(bk1);
  425. }
  426. }
  427. while (x.CompareTo(q) >= 0)
  428. {
  429. x = x.Subtract(q);
  430. }
  431. if (negative && x.SignValue != 0)
  432. {
  433. x = q.Subtract(x);
  434. }
  435. }
  436. return x;
  437. }
  438. protected virtual BigInteger ModSubtract(BigInteger x1, BigInteger x2)
  439. {
  440. BigInteger x3 = x1.Subtract(x2);
  441. if (x3.SignValue < 0)
  442. {
  443. x3 = x3.Add(q);
  444. }
  445. return x3;
  446. }
  447. public override bool Equals(
  448. object obj)
  449. {
  450. if (obj == this)
  451. return true;
  452. FpFieldElement other = obj as FpFieldElement;
  453. if (other == null)
  454. return false;
  455. return Equals(other);
  456. }
  457. public virtual bool Equals(
  458. FpFieldElement other)
  459. {
  460. return q.Equals(other.q) && base.Equals(other);
  461. }
  462. public override int GetHashCode()
  463. {
  464. return q.GetHashCode() ^ base.GetHashCode();
  465. }
  466. }
  467. public abstract class AbstractF2mFieldElement
  468. : ECFieldElement
  469. {
  470. public virtual ECFieldElement HalfTrace()
  471. {
  472. int m = FieldSize;
  473. if ((m & 1) == 0)
  474. throw new InvalidOperationException("Half-trace only defined for odd m");
  475. ECFieldElement fe = this;
  476. ECFieldElement ht = fe;
  477. for (int i = 2; i < m; i += 2)
  478. {
  479. fe = fe.SquarePow(2);
  480. ht = ht.Add(fe);
  481. }
  482. return ht;
  483. }
  484. public virtual int Trace()
  485. {
  486. int m = FieldSize;
  487. ECFieldElement fe = this;
  488. ECFieldElement tr = fe;
  489. for (int i = 1; i < m; ++i)
  490. {
  491. fe = fe.Square();
  492. tr = tr.Add(fe);
  493. }
  494. if (tr.IsZero)
  495. return 0;
  496. if (tr.IsOne)
  497. return 1;
  498. throw new InvalidOperationException("Internal error in trace calculation");
  499. }
  500. }
  501. /**
  502. * Class representing the Elements of the finite field
  503. * <code>F<sub>2<sup>m</sup></sub></code> in polynomial basis (PB)
  504. * representation. Both trinomial (Tpb) and pentanomial (Ppb) polynomial
  505. * basis representations are supported. Gaussian normal basis (GNB)
  506. * representation is not supported.
  507. */
  508. public class F2mFieldElement
  509. : AbstractF2mFieldElement
  510. {
  511. /**
  512. * Indicates gaussian normal basis representation (GNB). Number chosen
  513. * according to X9.62. GNB is not implemented at present.
  514. */
  515. public const int Gnb = 1;
  516. /**
  517. * Indicates trinomial basis representation (Tpb). Number chosen
  518. * according to X9.62.
  519. */
  520. public const int Tpb = 2;
  521. /**
  522. * Indicates pentanomial basis representation (Ppb). Number chosen
  523. * according to X9.62.
  524. */
  525. public const int Ppb = 3;
  526. /**
  527. * Tpb or Ppb.
  528. */
  529. private int representation;
  530. /**
  531. * The exponent <code>m</code> of <code>F<sub>2<sup>m</sup></sub></code>.
  532. */
  533. private int m;
  534. private int[] ks;
  535. /**
  536. * The <code>LongArray</code> holding the bits.
  537. */
  538. internal LongArray x;
  539. /**
  540. * Constructor for Ppb.
  541. * @param m The exponent <code>m</code> of
  542. * <code>F<sub>2<sup>m</sup></sub></code>.
  543. * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> +
  544. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  545. * represents the reduction polynomial <code>f(z)</code>.
  546. * @param k2 The integer <code>k2</code> where <code>x<sup>m</sup> +
  547. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  548. * represents the reduction polynomial <code>f(z)</code>.
  549. * @param k3 The integer <code>k3</code> where <code>x<sup>m</sup> +
  550. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  551. * represents the reduction polynomial <code>f(z)</code>.
  552. * @param x The BigInteger representing the value of the field element.
  553. */
  554. [Obsolete("Use ECCurve.FromBigInteger to construct field elements")]
  555. public F2mFieldElement(
  556. int m,
  557. int k1,
  558. int k2,
  559. int k3,
  560. BigInteger x)
  561. {
  562. if (x == null || x.SignValue < 0 || x.BitLength > m)
  563. throw new ArgumentException("value invalid in F2m field element", "x");
  564. if ((k2 == 0) && (k3 == 0))
  565. {
  566. this.representation = Tpb;
  567. this.ks = new int[] { k1 };
  568. }
  569. else
  570. {
  571. if (k2 >= k3)
  572. throw new ArgumentException("k2 must be smaller than k3");
  573. if (k2 <= 0)
  574. throw new ArgumentException("k2 must be larger than 0");
  575. this.representation = Ppb;
  576. this.ks = new int[] { k1, k2, k3 };
  577. }
  578. this.m = m;
  579. this.x = new LongArray(x);
  580. }
  581. /**
  582. * Constructor for Tpb.
  583. * @param m The exponent <code>m</code> of
  584. * <code>F<sub>2<sup>m</sup></sub></code>.
  585. * @param k The integer <code>k</code> where <code>x<sup>m</sup> +
  586. * x<sup>k</sup> + 1</code> represents the reduction
  587. * polynomial <code>f(z)</code>.
  588. * @param x The BigInteger representing the value of the field element.
  589. */
  590. [Obsolete("Use ECCurve.FromBigInteger to construct field elements")]
  591. public F2mFieldElement(
  592. int m,
  593. int k,
  594. BigInteger x)
  595. : this(m, k, 0, 0, x)
  596. {
  597. // Set k1 to k, and set k2 and k3 to 0
  598. }
  599. internal F2mFieldElement(int m, int[] ks, LongArray x)
  600. {
  601. this.m = m;
  602. this.representation = (ks.Length == 1) ? Tpb : Ppb;
  603. this.ks = ks;
  604. this.x = x;
  605. }
  606. public override int BitLength
  607. {
  608. get { return x.Degree(); }
  609. }
  610. public override bool IsOne
  611. {
  612. get { return x.IsOne(); }
  613. }
  614. public override bool IsZero
  615. {
  616. get { return x.IsZero(); }
  617. }
  618. public override bool TestBitZero()
  619. {
  620. return x.TestBitZero();
  621. }
  622. public override BigInteger ToBigInteger()
  623. {
  624. return x.ToBigInteger();
  625. }
  626. public override string FieldName
  627. {
  628. get { return "F2m"; }
  629. }
  630. public override int FieldSize
  631. {
  632. get { return m; }
  633. }
  634. /**
  635. * Checks, if the ECFieldElements <code>a</code> and <code>b</code>
  636. * are elements of the same field <code>F<sub>2<sup>m</sup></sub></code>
  637. * (having the same representation).
  638. * @param a field element.
  639. * @param b field element to be compared.
  640. * @throws ArgumentException if <code>a</code> and <code>b</code>
  641. * are not elements of the same field
  642. * <code>F<sub>2<sup>m</sup></sub></code> (having the same
  643. * representation).
  644. */
  645. public static void CheckFieldElements(
  646. ECFieldElement a,
  647. ECFieldElement b)
  648. {
  649. if (!(a is F2mFieldElement) || !(b is F2mFieldElement))
  650. {
  651. throw new ArgumentException("Field elements are not "
  652. + "both instances of F2mFieldElement");
  653. }
  654. F2mFieldElement aF2m = (F2mFieldElement)a;
  655. F2mFieldElement bF2m = (F2mFieldElement)b;
  656. if (aF2m.representation != bF2m.representation)
  657. {
  658. // Should never occur
  659. throw new ArgumentException("One of the F2m field elements has incorrect representation");
  660. }
  661. if ((aF2m.m != bF2m.m) || !Arrays.AreEqual(aF2m.ks, bF2m.ks))
  662. {
  663. throw new ArgumentException("Field elements are not elements of the same field F2m");
  664. }
  665. }
  666. public override ECFieldElement Add(
  667. ECFieldElement b)
  668. {
  669. // No check performed here for performance reasons. Instead the
  670. // elements involved are checked in ECPoint.F2m
  671. // checkFieldElements(this, b);
  672. LongArray iarrClone = this.x.Copy();
  673. F2mFieldElement bF2m = (F2mFieldElement)b;
  674. iarrClone.AddShiftedByWords(bF2m.x, 0);
  675. return new F2mFieldElement(m, ks, iarrClone);
  676. }
  677. public override ECFieldElement AddOne()
  678. {
  679. return new F2mFieldElement(m, ks, x.AddOne());
  680. }
  681. public override ECFieldElement Subtract(
  682. ECFieldElement b)
  683. {
  684. // Addition and subtraction are the same in F2m
  685. return Add(b);
  686. }
  687. public override ECFieldElement Multiply(
  688. ECFieldElement b)
  689. {
  690. // Right-to-left comb multiplication in the LongArray
  691. // Input: Binary polynomials a(z) and b(z) of degree at most m-1
  692. // Output: c(z) = a(z) * b(z) mod f(z)
  693. // No check performed here for performance reasons. Instead the
  694. // elements involved are checked in ECPoint.F2m
  695. // checkFieldElements(this, b);
  696. return new F2mFieldElement(m, ks, x.ModMultiply(((F2mFieldElement)b).x, m, ks));
  697. }
  698. public override ECFieldElement MultiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  699. {
  700. return MultiplyPlusProduct(b, x, y);
  701. }
  702. public override ECFieldElement MultiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
  703. {
  704. LongArray ax = this.x, bx = ((F2mFieldElement)b).x, xx = ((F2mFieldElement)x).x, yx = ((F2mFieldElement)y).x;
  705. LongArray ab = ax.Multiply(bx, m, ks);
  706. LongArray xy = xx.Multiply(yx, m, ks);
  707. if (ab == ax || ab == bx)
  708. {
  709. ab = (LongArray)ab.Copy();
  710. }
  711. ab.AddShiftedByWords(xy, 0);
  712. ab.Reduce(m, ks);
  713. return new F2mFieldElement(m, ks, ab);
  714. }
  715. public override ECFieldElement Divide(
  716. ECFieldElement b)
  717. {
  718. // There may be more efficient implementations
  719. ECFieldElement bInv = b.Invert();
  720. return Multiply(bInv);
  721. }
  722. public override ECFieldElement Negate()
  723. {
  724. // -x == x holds for all x in F2m
  725. return this;
  726. }
  727. public override ECFieldElement Square()
  728. {
  729. return new F2mFieldElement(m, ks, x.ModSquare(m, ks));
  730. }
  731. public override ECFieldElement SquareMinusProduct(ECFieldElement x, ECFieldElement y)
  732. {
  733. return SquarePlusProduct(x, y);
  734. }
  735. public override ECFieldElement SquarePlusProduct(ECFieldElement x, ECFieldElement y)
  736. {
  737. LongArray ax = this.x, xx = ((F2mFieldElement)x).x, yx = ((F2mFieldElement)y).x;
  738. LongArray aa = ax.Square(m, ks);
  739. LongArray xy = xx.Multiply(yx, m, ks);
  740. if (aa == ax)
  741. {
  742. aa = (LongArray)aa.Copy();
  743. }
  744. aa.AddShiftedByWords(xy, 0);
  745. aa.Reduce(m, ks);
  746. return new F2mFieldElement(m, ks, aa);
  747. }
  748. public override ECFieldElement SquarePow(int pow)
  749. {
  750. return pow < 1 ? this : new F2mFieldElement(m, ks, x.ModSquareN(pow, m, ks));
  751. }
  752. public override ECFieldElement Invert()
  753. {
  754. return new F2mFieldElement(this.m, this.ks, this.x.ModInverse(m, ks));
  755. }
  756. public override ECFieldElement Sqrt()
  757. {
  758. return (x.IsZero() || x.IsOne()) ? this : SquarePow(m - 1);
  759. }
  760. /**
  761. * @return the representation of the field
  762. * <code>F<sub>2<sup>m</sup></sub></code>, either of
  763. * {@link F2mFieldElement.Tpb} (trinomial
  764. * basis representation) or
  765. * {@link F2mFieldElement.Ppb} (pentanomial
  766. * basis representation).
  767. */
  768. public int Representation
  769. {
  770. get { return this.representation; }
  771. }
  772. /**
  773. * @return the degree <code>m</code> of the reduction polynomial
  774. * <code>f(z)</code>.
  775. */
  776. public int M
  777. {
  778. get { return this.m; }
  779. }
  780. /**
  781. * @return Tpb: The integer <code>k</code> where <code>x<sup>m</sup> +
  782. * x<sup>k</sup> + 1</code> represents the reduction polynomial
  783. * <code>f(z)</code>.<br/>
  784. * Ppb: The integer <code>k1</code> where <code>x<sup>m</sup> +
  785. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  786. * represents the reduction polynomial <code>f(z)</code>.<br/>
  787. */
  788. public int K1
  789. {
  790. get { return this.ks[0]; }
  791. }
  792. /**
  793. * @return Tpb: Always returns <code>0</code><br/>
  794. * Ppb: The integer <code>k2</code> where <code>x<sup>m</sup> +
  795. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  796. * represents the reduction polynomial <code>f(z)</code>.<br/>
  797. */
  798. public int K2
  799. {
  800. get { return this.ks.Length >= 2 ? this.ks[1] : 0; }
  801. }
  802. /**
  803. * @return Tpb: Always set to <code>0</code><br/>
  804. * Ppb: The integer <code>k3</code> where <code>x<sup>m</sup> +
  805. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  806. * represents the reduction polynomial <code>f(z)</code>.<br/>
  807. */
  808. public int K3
  809. {
  810. get { return this.ks.Length >= 3 ? this.ks[2] : 0; }
  811. }
  812. public override bool Equals(
  813. object obj)
  814. {
  815. if (obj == this)
  816. return true;
  817. F2mFieldElement other = obj as F2mFieldElement;
  818. if (other == null)
  819. return false;
  820. return Equals(other);
  821. }
  822. public virtual bool Equals(
  823. F2mFieldElement other)
  824. {
  825. return ((this.m == other.m)
  826. && (this.representation == other.representation)
  827. && Arrays.AreEqual(this.ks, other.ks)
  828. && (this.x.Equals(other.x)));
  829. }
  830. public override int GetHashCode()
  831. {
  832. return x.GetHashCode() ^ m ^ Arrays.GetHashCode(ks);
  833. }
  834. }
  835. }
  836. #pragma warning restore
  837. #endif