ECPoint.cs 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using Org.BouncyCastle.Math.EC.Multiplier;
  7. namespace Org.BouncyCastle.Math.EC
  8. {
  9. /**
  10. * base class for points on elliptic curves.
  11. */
  12. public abstract class ECPoint
  13. {
  14. protected static ECFieldElement[] EMPTY_ZS = new ECFieldElement[0];
  15. protected static ECFieldElement[] GetInitialZCoords(ECCurve curve)
  16. {
  17. // Cope with null curve, most commonly used by implicitlyCa
  18. int coord = null == curve ? ECCurve.COORD_AFFINE : curve.CoordinateSystem;
  19. switch (coord)
  20. {
  21. case ECCurve.COORD_AFFINE:
  22. case ECCurve.COORD_LAMBDA_AFFINE:
  23. return EMPTY_ZS;
  24. default:
  25. break;
  26. }
  27. ECFieldElement one = curve.FromBigInteger(BigInteger.One);
  28. switch (coord)
  29. {
  30. case ECCurve.COORD_HOMOGENEOUS:
  31. case ECCurve.COORD_JACOBIAN:
  32. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  33. return new ECFieldElement[] { one };
  34. case ECCurve.COORD_JACOBIAN_CHUDNOVSKY:
  35. return new ECFieldElement[] { one, one, one };
  36. case ECCurve.COORD_JACOBIAN_MODIFIED:
  37. return new ECFieldElement[] { one, curve.A };
  38. default:
  39. throw new ArgumentException("unknown coordinate system");
  40. }
  41. }
  42. protected internal readonly ECCurve m_curve;
  43. protected internal readonly ECFieldElement m_x, m_y;
  44. protected internal readonly ECFieldElement[] m_zs;
  45. protected internal readonly bool m_withCompression;
  46. // Dictionary is (string -> PreCompInfo)
  47. protected internal IDictionary m_preCompTable = null;
  48. protected ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  49. : this(curve, x, y, GetInitialZCoords(curve), withCompression)
  50. {
  51. }
  52. internal ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  53. {
  54. this.m_curve = curve;
  55. this.m_x = x;
  56. this.m_y = y;
  57. this.m_zs = zs;
  58. this.m_withCompression = withCompression;
  59. }
  60. protected internal bool SatisfiesCofactor()
  61. {
  62. BigInteger h = Curve.Cofactor;
  63. return h == null || h.Equals(BigInteger.One) || !ECAlgorithms.ReferenceMultiply(this, h).IsInfinity;
  64. }
  65. protected abstract bool SatisfiesCurveEquation();
  66. public ECPoint GetDetachedPoint()
  67. {
  68. return Normalize().Detach();
  69. }
  70. public virtual ECCurve Curve
  71. {
  72. get { return m_curve; }
  73. }
  74. protected abstract ECPoint Detach();
  75. protected virtual int CurveCoordinateSystem
  76. {
  77. get
  78. {
  79. // Cope with null curve, most commonly used by implicitlyCa
  80. return null == m_curve ? ECCurve.COORD_AFFINE : m_curve.CoordinateSystem;
  81. }
  82. }
  83. /**
  84. * Normalizes this point, and then returns the affine x-coordinate.
  85. *
  86. * Note: normalization can be expensive, this method is deprecated in favour
  87. * of caller-controlled normalization.
  88. */
  89. [Obsolete("Use AffineXCoord, or Normalize() and XCoord, instead")]
  90. public virtual ECFieldElement X
  91. {
  92. get { return Normalize().XCoord; }
  93. }
  94. /**
  95. * Normalizes this point, and then returns the affine y-coordinate.
  96. *
  97. * Note: normalization can be expensive, this method is deprecated in favour
  98. * of caller-controlled normalization.
  99. */
  100. [Obsolete("Use AffineYCoord, or Normalize() and YCoord, instead")]
  101. public virtual ECFieldElement Y
  102. {
  103. get { return Normalize().YCoord; }
  104. }
  105. /**
  106. * Returns the affine x-coordinate after checking that this point is normalized.
  107. *
  108. * @return The affine x-coordinate of this point
  109. * @throws IllegalStateException if the point is not normalized
  110. */
  111. public virtual ECFieldElement AffineXCoord
  112. {
  113. get
  114. {
  115. CheckNormalized();
  116. return XCoord;
  117. }
  118. }
  119. /**
  120. * Returns the affine y-coordinate after checking that this point is normalized
  121. *
  122. * @return The affine y-coordinate of this point
  123. * @throws IllegalStateException if the point is not normalized
  124. */
  125. public virtual ECFieldElement AffineYCoord
  126. {
  127. get
  128. {
  129. CheckNormalized();
  130. return YCoord;
  131. }
  132. }
  133. /**
  134. * Returns the x-coordinate.
  135. *
  136. * Caution: depending on the curve's coordinate system, this may not be the same value as in an
  137. * affine coordinate system; use Normalize() to get a point where the coordinates have their
  138. * affine values, or use AffineXCoord if you expect the point to already have been normalized.
  139. *
  140. * @return the x-coordinate of this point
  141. */
  142. public virtual ECFieldElement XCoord
  143. {
  144. get { return m_x; }
  145. }
  146. /**
  147. * Returns the y-coordinate.
  148. *
  149. * Caution: depending on the curve's coordinate system, this may not be the same value as in an
  150. * affine coordinate system; use Normalize() to get a point where the coordinates have their
  151. * affine values, or use AffineYCoord if you expect the point to already have been normalized.
  152. *
  153. * @return the y-coordinate of this point
  154. */
  155. public virtual ECFieldElement YCoord
  156. {
  157. get { return m_y; }
  158. }
  159. public virtual ECFieldElement GetZCoord(int index)
  160. {
  161. return (index < 0 || index >= m_zs.Length) ? null : m_zs[index];
  162. }
  163. public virtual ECFieldElement[] GetZCoords()
  164. {
  165. int zsLen = m_zs.Length;
  166. if (zsLen == 0)
  167. {
  168. return m_zs;
  169. }
  170. ECFieldElement[] copy = new ECFieldElement[zsLen];
  171. Array.Copy(m_zs, 0, copy, 0, zsLen);
  172. return copy;
  173. }
  174. protected internal ECFieldElement RawXCoord
  175. {
  176. get { return m_x; }
  177. }
  178. protected internal ECFieldElement RawYCoord
  179. {
  180. get { return m_y; }
  181. }
  182. protected internal ECFieldElement[] RawZCoords
  183. {
  184. get { return m_zs; }
  185. }
  186. protected virtual void CheckNormalized()
  187. {
  188. if (!IsNormalized())
  189. throw new InvalidOperationException("point not in normal form");
  190. }
  191. public virtual bool IsNormalized()
  192. {
  193. int coord = this.CurveCoordinateSystem;
  194. return coord == ECCurve.COORD_AFFINE
  195. || coord == ECCurve.COORD_LAMBDA_AFFINE
  196. || IsInfinity
  197. || RawZCoords[0].IsOne;
  198. }
  199. /**
  200. * Normalization ensures that any projective coordinate is 1, and therefore that the x, y
  201. * coordinates reflect those of the equivalent point in an affine coordinate system.
  202. *
  203. * @return a new ECPoint instance representing the same point, but with normalized coordinates
  204. */
  205. public virtual ECPoint Normalize()
  206. {
  207. if (this.IsInfinity)
  208. {
  209. return this;
  210. }
  211. switch (this.CurveCoordinateSystem)
  212. {
  213. case ECCurve.COORD_AFFINE:
  214. case ECCurve.COORD_LAMBDA_AFFINE:
  215. {
  216. return this;
  217. }
  218. default:
  219. {
  220. ECFieldElement Z1 = RawZCoords[0];
  221. if (Z1.IsOne)
  222. {
  223. return this;
  224. }
  225. return Normalize(Z1.Invert());
  226. }
  227. }
  228. }
  229. internal virtual ECPoint Normalize(ECFieldElement zInv)
  230. {
  231. switch (this.CurveCoordinateSystem)
  232. {
  233. case ECCurve.COORD_HOMOGENEOUS:
  234. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  235. {
  236. return CreateScaledPoint(zInv, zInv);
  237. }
  238. case ECCurve.COORD_JACOBIAN:
  239. case ECCurve.COORD_JACOBIAN_CHUDNOVSKY:
  240. case ECCurve.COORD_JACOBIAN_MODIFIED:
  241. {
  242. ECFieldElement zInv2 = zInv.Square(), zInv3 = zInv2.Multiply(zInv);
  243. return CreateScaledPoint(zInv2, zInv3);
  244. }
  245. default:
  246. {
  247. throw new InvalidOperationException("not a projective coordinate system");
  248. }
  249. }
  250. }
  251. protected virtual ECPoint CreateScaledPoint(ECFieldElement sx, ECFieldElement sy)
  252. {
  253. return Curve.CreateRawPoint(RawXCoord.Multiply(sx), RawYCoord.Multiply(sy), IsCompressed);
  254. }
  255. public bool IsInfinity
  256. {
  257. get { return m_x == null && m_y == null; }
  258. }
  259. public bool IsCompressed
  260. {
  261. get { return m_withCompression; }
  262. }
  263. public bool IsValid()
  264. {
  265. if (IsInfinity)
  266. return true;
  267. // TODO Sanity-check the field elements
  268. ECCurve curve = Curve;
  269. if (curve != null)
  270. {
  271. if (!SatisfiesCurveEquation())
  272. return false;
  273. if (!SatisfiesCofactor())
  274. return false;
  275. }
  276. return true;
  277. }
  278. public virtual ECPoint ScaleX(ECFieldElement scale)
  279. {
  280. return IsInfinity
  281. ? this
  282. : Curve.CreateRawPoint(RawXCoord.Multiply(scale), RawYCoord, RawZCoords, IsCompressed);
  283. }
  284. public virtual ECPoint ScaleY(ECFieldElement scale)
  285. {
  286. return IsInfinity
  287. ? this
  288. : Curve.CreateRawPoint(RawXCoord, RawYCoord.Multiply(scale), RawZCoords, IsCompressed);
  289. }
  290. public override bool Equals(object obj)
  291. {
  292. return Equals(obj as ECPoint);
  293. }
  294. public virtual bool Equals(ECPoint other)
  295. {
  296. if (this == other)
  297. return true;
  298. if (null == other)
  299. return false;
  300. ECCurve c1 = this.Curve, c2 = other.Curve;
  301. bool n1 = (null == c1), n2 = (null == c2);
  302. bool i1 = IsInfinity, i2 = other.IsInfinity;
  303. if (i1 || i2)
  304. {
  305. return (i1 && i2) && (n1 || n2 || c1.Equals(c2));
  306. }
  307. ECPoint p1 = this, p2 = other;
  308. if (n1 && n2)
  309. {
  310. // Points with null curve are in affine form, so already normalized
  311. }
  312. else if (n1)
  313. {
  314. p2 = p2.Normalize();
  315. }
  316. else if (n2)
  317. {
  318. p1 = p1.Normalize();
  319. }
  320. else if (!c1.Equals(c2))
  321. {
  322. return false;
  323. }
  324. else
  325. {
  326. // TODO Consider just requiring already normalized, to avoid silent performance degradation
  327. ECPoint[] points = new ECPoint[] { this, c1.ImportPoint(p2) };
  328. // TODO This is a little strong, really only requires coZNormalizeAll to get Zs equal
  329. c1.NormalizeAll(points);
  330. p1 = points[0];
  331. p2 = points[1];
  332. }
  333. return p1.XCoord.Equals(p2.XCoord) && p1.YCoord.Equals(p2.YCoord);
  334. }
  335. public override int GetHashCode()
  336. {
  337. ECCurve c = this.Curve;
  338. int hc = (null == c) ? 0 : ~c.GetHashCode();
  339. if (!this.IsInfinity)
  340. {
  341. // TODO Consider just requiring already normalized, to avoid silent performance degradation
  342. ECPoint p = Normalize();
  343. hc ^= p.XCoord.GetHashCode() * 17;
  344. hc ^= p.YCoord.GetHashCode() * 257;
  345. }
  346. return hc;
  347. }
  348. public override string ToString()
  349. {
  350. if (this.IsInfinity)
  351. {
  352. return "INF";
  353. }
  354. StringBuilder sb = new StringBuilder();
  355. sb.Append('(');
  356. sb.Append(RawXCoord);
  357. sb.Append(',');
  358. sb.Append(RawYCoord);
  359. for (int i = 0; i < m_zs.Length; ++i)
  360. {
  361. sb.Append(',');
  362. sb.Append(m_zs[i]);
  363. }
  364. sb.Append(')');
  365. return sb.ToString();
  366. }
  367. public virtual byte[] GetEncoded()
  368. {
  369. return GetEncoded(m_withCompression);
  370. }
  371. public abstract byte[] GetEncoded(bool compressed);
  372. protected internal abstract bool CompressionYTilde { get; }
  373. public abstract ECPoint Add(ECPoint b);
  374. public abstract ECPoint Subtract(ECPoint b);
  375. public abstract ECPoint Negate();
  376. public virtual ECPoint TimesPow2(int e)
  377. {
  378. if (e < 0)
  379. throw new ArgumentException("cannot be negative", "e");
  380. ECPoint p = this;
  381. while (--e >= 0)
  382. {
  383. p = p.Twice();
  384. }
  385. return p;
  386. }
  387. public abstract ECPoint Twice();
  388. public abstract ECPoint Multiply(BigInteger b);
  389. public virtual ECPoint TwicePlus(ECPoint b)
  390. {
  391. return Twice().Add(b);
  392. }
  393. public virtual ECPoint ThreeTimes()
  394. {
  395. return TwicePlus(this);
  396. }
  397. }
  398. public abstract class ECPointBase
  399. : ECPoint
  400. {
  401. protected internal ECPointBase(
  402. ECCurve curve,
  403. ECFieldElement x,
  404. ECFieldElement y,
  405. bool withCompression)
  406. : base(curve, x, y, withCompression)
  407. {
  408. }
  409. protected internal ECPointBase(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  410. : base(curve, x, y, zs, withCompression)
  411. {
  412. }
  413. /**
  414. * return the field element encoded with point compression. (S 4.3.6)
  415. */
  416. public override byte[] GetEncoded(bool compressed)
  417. {
  418. if (this.IsInfinity)
  419. {
  420. return new byte[1];
  421. }
  422. ECPoint normed = Normalize();
  423. byte[] X = normed.XCoord.GetEncoded();
  424. if (compressed)
  425. {
  426. byte[] PO = new byte[X.Length + 1];
  427. PO[0] = (byte)(normed.CompressionYTilde ? 0x03 : 0x02);
  428. Array.Copy(X, 0, PO, 1, X.Length);
  429. return PO;
  430. }
  431. byte[] Y = normed.YCoord.GetEncoded();
  432. {
  433. byte[] PO = new byte[X.Length + Y.Length + 1];
  434. PO[0] = 0x04;
  435. Array.Copy(X, 0, PO, 1, X.Length);
  436. Array.Copy(Y, 0, PO, X.Length + 1, Y.Length);
  437. return PO;
  438. }
  439. }
  440. /**
  441. * Multiplies this <code>ECPoint</code> by the given number.
  442. * @param k The multiplicator.
  443. * @return <code>k * this</code>.
  444. */
  445. public override ECPoint Multiply(BigInteger k)
  446. {
  447. return this.Curve.GetMultiplier().Multiply(this, k);
  448. }
  449. }
  450. public abstract class AbstractFpPoint
  451. : ECPointBase
  452. {
  453. protected AbstractFpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  454. : base(curve, x, y, withCompression)
  455. {
  456. }
  457. protected AbstractFpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  458. : base(curve, x, y, zs, withCompression)
  459. {
  460. }
  461. protected internal override bool CompressionYTilde
  462. {
  463. get { return this.AffineYCoord.TestBitZero(); }
  464. }
  465. protected override bool SatisfiesCurveEquation()
  466. {
  467. ECFieldElement X = this.RawXCoord, Y = this.RawYCoord, A = Curve.A, B = Curve.B;
  468. ECFieldElement lhs = Y.Square();
  469. switch (CurveCoordinateSystem)
  470. {
  471. case ECCurve.COORD_AFFINE:
  472. break;
  473. case ECCurve.COORD_HOMOGENEOUS:
  474. {
  475. ECFieldElement Z = this.RawZCoords[0];
  476. if (!Z.IsOne)
  477. {
  478. ECFieldElement Z2 = Z.Square(), Z3 = Z.Multiply(Z2);
  479. lhs = lhs.Multiply(Z);
  480. A = A.Multiply(Z2);
  481. B = B.Multiply(Z3);
  482. }
  483. break;
  484. }
  485. case ECCurve.COORD_JACOBIAN:
  486. case ECCurve.COORD_JACOBIAN_CHUDNOVSKY:
  487. case ECCurve.COORD_JACOBIAN_MODIFIED:
  488. {
  489. ECFieldElement Z = this.RawZCoords[0];
  490. if (!Z.IsOne)
  491. {
  492. ECFieldElement Z2 = Z.Square(), Z4 = Z2.Square(), Z6 = Z2.Multiply(Z4);
  493. A = A.Multiply(Z4);
  494. B = B.Multiply(Z6);
  495. }
  496. break;
  497. }
  498. default:
  499. throw new InvalidOperationException("unsupported coordinate system");
  500. }
  501. ECFieldElement rhs = X.Square().Add(A).Multiply(X).Add(B);
  502. return lhs.Equals(rhs);
  503. }
  504. public override ECPoint Subtract(ECPoint b)
  505. {
  506. if (b.IsInfinity)
  507. return this;
  508. // Add -b
  509. return Add(b.Negate());
  510. }
  511. }
  512. /**
  513. * Elliptic curve points over Fp
  514. */
  515. public class FpPoint
  516. : AbstractFpPoint
  517. {
  518. /**
  519. * Create a point which encodes without point compression.
  520. *
  521. * @param curve the curve to use
  522. * @param x affine x co-ordinate
  523. * @param y affine y co-ordinate
  524. */
  525. public FpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y)
  526. : this(curve, x, y, false)
  527. {
  528. }
  529. /**
  530. * Create a point that encodes with or without point compression.
  531. *
  532. * @param curve the curve to use
  533. * @param x affine x co-ordinate
  534. * @param y affine y co-ordinate
  535. * @param withCompression if true encode with point compression
  536. */
  537. public FpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  538. : base(curve, x, y, withCompression)
  539. {
  540. if ((x == null) != (y == null))
  541. throw new ArgumentException("Exactly one of the field elements is null");
  542. }
  543. internal FpPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  544. : base(curve, x, y, zs, withCompression)
  545. {
  546. }
  547. protected override ECPoint Detach()
  548. {
  549. return new FpPoint(null, AffineXCoord, AffineYCoord);
  550. }
  551. public override ECFieldElement GetZCoord(int index)
  552. {
  553. if (index == 1 && ECCurve.COORD_JACOBIAN_MODIFIED == this.CurveCoordinateSystem)
  554. {
  555. return GetJacobianModifiedW();
  556. }
  557. return base.GetZCoord(index);
  558. }
  559. // B.3 pg 62
  560. public override ECPoint Add(ECPoint b)
  561. {
  562. if (this.IsInfinity)
  563. return b;
  564. if (b.IsInfinity)
  565. return this;
  566. if (this == b)
  567. return Twice();
  568. ECCurve curve = this.Curve;
  569. int coord = curve.CoordinateSystem;
  570. ECFieldElement X1 = this.RawXCoord, Y1 = this.RawYCoord;
  571. ECFieldElement X2 = b.RawXCoord, Y2 = b.RawYCoord;
  572. switch (coord)
  573. {
  574. case ECCurve.COORD_AFFINE:
  575. {
  576. ECFieldElement dx = X2.Subtract(X1), dy = Y2.Subtract(Y1);
  577. if (dx.IsZero)
  578. {
  579. if (dy.IsZero)
  580. {
  581. // this == b, i.e. this must be doubled
  582. return Twice();
  583. }
  584. // this == -b, i.e. the result is the point at infinity
  585. return Curve.Infinity;
  586. }
  587. ECFieldElement gamma = dy.Divide(dx);
  588. ECFieldElement X3 = gamma.Square().Subtract(X1).Subtract(X2);
  589. ECFieldElement Y3 = gamma.Multiply(X1.Subtract(X3)).Subtract(Y1);
  590. return new FpPoint(Curve, X3, Y3, IsCompressed);
  591. }
  592. case ECCurve.COORD_HOMOGENEOUS:
  593. {
  594. ECFieldElement Z1 = this.RawZCoords[0];
  595. ECFieldElement Z2 = b.RawZCoords[0];
  596. bool Z1IsOne = Z1.IsOne;
  597. bool Z2IsOne = Z2.IsOne;
  598. ECFieldElement u1 = Z1IsOne ? Y2 : Y2.Multiply(Z1);
  599. ECFieldElement u2 = Z2IsOne ? Y1 : Y1.Multiply(Z2);
  600. ECFieldElement u = u1.Subtract(u2);
  601. ECFieldElement v1 = Z1IsOne ? X2 : X2.Multiply(Z1);
  602. ECFieldElement v2 = Z2IsOne ? X1 : X1.Multiply(Z2);
  603. ECFieldElement v = v1.Subtract(v2);
  604. // Check if b == this or b == -this
  605. if (v.IsZero)
  606. {
  607. if (u.IsZero)
  608. {
  609. // this == b, i.e. this must be doubled
  610. return this.Twice();
  611. }
  612. // this == -b, i.e. the result is the point at infinity
  613. return curve.Infinity;
  614. }
  615. // TODO Optimize for when w == 1
  616. ECFieldElement w = Z1IsOne ? Z2 : Z2IsOne ? Z1 : Z1.Multiply(Z2);
  617. ECFieldElement vSquared = v.Square();
  618. ECFieldElement vCubed = vSquared.Multiply(v);
  619. ECFieldElement vSquaredV2 = vSquared.Multiply(v2);
  620. ECFieldElement A = u.Square().Multiply(w).Subtract(vCubed).Subtract(Two(vSquaredV2));
  621. ECFieldElement X3 = v.Multiply(A);
  622. ECFieldElement Y3 = vSquaredV2.Subtract(A).MultiplyMinusProduct(u, u2, vCubed);
  623. ECFieldElement Z3 = vCubed.Multiply(w);
  624. return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  625. }
  626. case ECCurve.COORD_JACOBIAN:
  627. case ECCurve.COORD_JACOBIAN_MODIFIED:
  628. {
  629. ECFieldElement Z1 = this.RawZCoords[0];
  630. ECFieldElement Z2 = b.RawZCoords[0];
  631. bool Z1IsOne = Z1.IsOne;
  632. ECFieldElement X3, Y3, Z3, Z3Squared = null;
  633. if (!Z1IsOne && Z1.Equals(Z2))
  634. {
  635. // TODO Make this available as public method coZAdd?
  636. ECFieldElement dx = X1.Subtract(X2), dy = Y1.Subtract(Y2);
  637. if (dx.IsZero)
  638. {
  639. if (dy.IsZero)
  640. {
  641. return Twice();
  642. }
  643. return curve.Infinity;
  644. }
  645. ECFieldElement C = dx.Square();
  646. ECFieldElement W1 = X1.Multiply(C), W2 = X2.Multiply(C);
  647. ECFieldElement A1 = W1.Subtract(W2).Multiply(Y1);
  648. X3 = dy.Square().Subtract(W1).Subtract(W2);
  649. Y3 = W1.Subtract(X3).Multiply(dy).Subtract(A1);
  650. Z3 = dx;
  651. if (Z1IsOne)
  652. {
  653. Z3Squared = C;
  654. }
  655. else
  656. {
  657. Z3 = Z3.Multiply(Z1);
  658. }
  659. }
  660. else
  661. {
  662. ECFieldElement Z1Squared, U2, S2;
  663. if (Z1IsOne)
  664. {
  665. Z1Squared = Z1; U2 = X2; S2 = Y2;
  666. }
  667. else
  668. {
  669. Z1Squared = Z1.Square();
  670. U2 = Z1Squared.Multiply(X2);
  671. ECFieldElement Z1Cubed = Z1Squared.Multiply(Z1);
  672. S2 = Z1Cubed.Multiply(Y2);
  673. }
  674. bool Z2IsOne = Z2.IsOne;
  675. ECFieldElement Z2Squared, U1, S1;
  676. if (Z2IsOne)
  677. {
  678. Z2Squared = Z2; U1 = X1; S1 = Y1;
  679. }
  680. else
  681. {
  682. Z2Squared = Z2.Square();
  683. U1 = Z2Squared.Multiply(X1);
  684. ECFieldElement Z2Cubed = Z2Squared.Multiply(Z2);
  685. S1 = Z2Cubed.Multiply(Y1);
  686. }
  687. ECFieldElement H = U1.Subtract(U2);
  688. ECFieldElement R = S1.Subtract(S2);
  689. // Check if b == this or b == -this
  690. if (H.IsZero)
  691. {
  692. if (R.IsZero)
  693. {
  694. // this == b, i.e. this must be doubled
  695. return this.Twice();
  696. }
  697. // this == -b, i.e. the result is the point at infinity
  698. return curve.Infinity;
  699. }
  700. ECFieldElement HSquared = H.Square();
  701. ECFieldElement G = HSquared.Multiply(H);
  702. ECFieldElement V = HSquared.Multiply(U1);
  703. X3 = R.Square().Add(G).Subtract(Two(V));
  704. Y3 = V.Subtract(X3).MultiplyMinusProduct(R, G, S1);
  705. Z3 = H;
  706. if (!Z1IsOne)
  707. {
  708. Z3 = Z3.Multiply(Z1);
  709. }
  710. if (!Z2IsOne)
  711. {
  712. Z3 = Z3.Multiply(Z2);
  713. }
  714. // Alternative calculation of Z3 using fast square
  715. //X3 = four(X3);
  716. //Y3 = eight(Y3);
  717. //Z3 = doubleProductFromSquares(Z1, Z2, Z1Squared, Z2Squared).Multiply(H);
  718. if (Z3 == H)
  719. {
  720. Z3Squared = HSquared;
  721. }
  722. }
  723. ECFieldElement[] zs;
  724. if (coord == ECCurve.COORD_JACOBIAN_MODIFIED)
  725. {
  726. // TODO If the result will only be used in a subsequent addition, we don't need W3
  727. ECFieldElement W3 = CalculateJacobianModifiedW(Z3, Z3Squared);
  728. zs = new ECFieldElement[] { Z3, W3 };
  729. }
  730. else
  731. {
  732. zs = new ECFieldElement[] { Z3 };
  733. }
  734. return new FpPoint(curve, X3, Y3, zs, IsCompressed);
  735. }
  736. default:
  737. {
  738. throw new InvalidOperationException("unsupported coordinate system");
  739. }
  740. }
  741. }
  742. // B.3 pg 62
  743. public override ECPoint Twice()
  744. {
  745. if (this.IsInfinity)
  746. return this;
  747. ECCurve curve = this.Curve;
  748. ECFieldElement Y1 = this.RawYCoord;
  749. if (Y1.IsZero)
  750. return curve.Infinity;
  751. int coord = curve.CoordinateSystem;
  752. ECFieldElement X1 = this.RawXCoord;
  753. switch (coord)
  754. {
  755. case ECCurve.COORD_AFFINE:
  756. {
  757. ECFieldElement X1Squared = X1.Square();
  758. ECFieldElement gamma = Three(X1Squared).Add(this.Curve.A).Divide(Two(Y1));
  759. ECFieldElement X3 = gamma.Square().Subtract(Two(X1));
  760. ECFieldElement Y3 = gamma.Multiply(X1.Subtract(X3)).Subtract(Y1);
  761. return new FpPoint(Curve, X3, Y3, IsCompressed);
  762. }
  763. case ECCurve.COORD_HOMOGENEOUS:
  764. {
  765. ECFieldElement Z1 = this.RawZCoords[0];
  766. bool Z1IsOne = Z1.IsOne;
  767. // TODO Optimize for small negative a4 and -3
  768. ECFieldElement w = curve.A;
  769. if (!w.IsZero && !Z1IsOne)
  770. {
  771. w = w.Multiply(Z1.Square());
  772. }
  773. w = w.Add(Three(X1.Square()));
  774. ECFieldElement s = Z1IsOne ? Y1 : Y1.Multiply(Z1);
  775. ECFieldElement t = Z1IsOne ? Y1.Square() : s.Multiply(Y1);
  776. ECFieldElement B = X1.Multiply(t);
  777. ECFieldElement _4B = Four(B);
  778. ECFieldElement h = w.Square().Subtract(Two(_4B));
  779. ECFieldElement _2s = Two(s);
  780. ECFieldElement X3 = h.Multiply(_2s);
  781. ECFieldElement _2t = Two(t);
  782. ECFieldElement Y3 = _4B.Subtract(h).Multiply(w).Subtract(Two(_2t.Square()));
  783. ECFieldElement _4sSquared = Z1IsOne ? Two(_2t) : _2s.Square();
  784. ECFieldElement Z3 = Two(_4sSquared).Multiply(s);
  785. return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  786. }
  787. case ECCurve.COORD_JACOBIAN:
  788. {
  789. ECFieldElement Z1 = this.RawZCoords[0];
  790. bool Z1IsOne = Z1.IsOne;
  791. ECFieldElement Y1Squared = Y1.Square();
  792. ECFieldElement T = Y1Squared.Square();
  793. ECFieldElement a4 = curve.A;
  794. ECFieldElement a4Neg = a4.Negate();
  795. ECFieldElement M, S;
  796. if (a4Neg.ToBigInteger().Equals(BigInteger.ValueOf(3)))
  797. {
  798. ECFieldElement Z1Squared = Z1IsOne ? Z1 : Z1.Square();
  799. M = Three(X1.Add(Z1Squared).Multiply(X1.Subtract(Z1Squared)));
  800. S = Four(Y1Squared.Multiply(X1));
  801. }
  802. else
  803. {
  804. ECFieldElement X1Squared = X1.Square();
  805. M = Three(X1Squared);
  806. if (Z1IsOne)
  807. {
  808. M = M.Add(a4);
  809. }
  810. else if (!a4.IsZero)
  811. {
  812. ECFieldElement Z1Squared = Z1IsOne ? Z1 : Z1.Square();
  813. ECFieldElement Z1Pow4 = Z1Squared.Square();
  814. if (a4Neg.BitLength < a4.BitLength)
  815. {
  816. M = M.Subtract(Z1Pow4.Multiply(a4Neg));
  817. }
  818. else
  819. {
  820. M = M.Add(Z1Pow4.Multiply(a4));
  821. }
  822. }
  823. //S = two(doubleProductFromSquares(X1, Y1Squared, X1Squared, T));
  824. S = Four(X1.Multiply(Y1Squared));
  825. }
  826. ECFieldElement X3 = M.Square().Subtract(Two(S));
  827. ECFieldElement Y3 = S.Subtract(X3).Multiply(M).Subtract(Eight(T));
  828. ECFieldElement Z3 = Two(Y1);
  829. if (!Z1IsOne)
  830. {
  831. Z3 = Z3.Multiply(Z1);
  832. }
  833. // Alternative calculation of Z3 using fast square
  834. //ECFieldElement Z3 = doubleProductFromSquares(Y1, Z1, Y1Squared, Z1Squared);
  835. return new FpPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  836. }
  837. case ECCurve.COORD_JACOBIAN_MODIFIED:
  838. {
  839. return TwiceJacobianModified(true);
  840. }
  841. default:
  842. {
  843. throw new InvalidOperationException("unsupported coordinate system");
  844. }
  845. }
  846. }
  847. public override ECPoint TwicePlus(ECPoint b)
  848. {
  849. if (this == b)
  850. return ThreeTimes();
  851. if (this.IsInfinity)
  852. return b;
  853. if (b.IsInfinity)
  854. return Twice();
  855. ECFieldElement Y1 = this.RawYCoord;
  856. if (Y1.IsZero)
  857. return b;
  858. ECCurve curve = this.Curve;
  859. int coord = curve.CoordinateSystem;
  860. switch (coord)
  861. {
  862. case ECCurve.COORD_AFFINE:
  863. {
  864. ECFieldElement X1 = this.RawXCoord;
  865. ECFieldElement X2 = b.RawXCoord, Y2 = b.RawYCoord;
  866. ECFieldElement dx = X2.Subtract(X1), dy = Y2.Subtract(Y1);
  867. if (dx.IsZero)
  868. {
  869. if (dy.IsZero)
  870. {
  871. // this == b i.e. the result is 3P
  872. return ThreeTimes();
  873. }
  874. // this == -b, i.e. the result is P
  875. return this;
  876. }
  877. /*
  878. * Optimized calculation of 2P + Q, as described in "Trading Inversions for
  879. * Multiplications in Elliptic Curve Cryptography", by Ciet, Joye, Lauter, Montgomery.
  880. */
  881. ECFieldElement X = dx.Square(), Y = dy.Square();
  882. ECFieldElement d = X.Multiply(Two(X1).Add(X2)).Subtract(Y);
  883. if (d.IsZero)
  884. {
  885. return Curve.Infinity;
  886. }
  887. ECFieldElement D = d.Multiply(dx);
  888. ECFieldElement I = D.Invert();
  889. ECFieldElement L1 = d.Multiply(I).Multiply(dy);
  890. ECFieldElement L2 = Two(Y1).Multiply(X).Multiply(dx).Multiply(I).Subtract(L1);
  891. ECFieldElement X4 = (L2.Subtract(L1)).Multiply(L1.Add(L2)).Add(X2);
  892. ECFieldElement Y4 = (X1.Subtract(X4)).Multiply(L2).Subtract(Y1);
  893. return new FpPoint(Curve, X4, Y4, IsCompressed);
  894. }
  895. case ECCurve.COORD_JACOBIAN_MODIFIED:
  896. {
  897. return TwiceJacobianModified(false).Add(b);
  898. }
  899. default:
  900. {
  901. return Twice().Add(b);
  902. }
  903. }
  904. }
  905. public override ECPoint ThreeTimes()
  906. {
  907. if (this.IsInfinity)
  908. return this;
  909. ECFieldElement Y1 = this.RawYCoord;
  910. if (Y1.IsZero)
  911. return this;
  912. ECCurve curve = this.Curve;
  913. int coord = curve.CoordinateSystem;
  914. switch (coord)
  915. {
  916. case ECCurve.COORD_AFFINE:
  917. {
  918. ECFieldElement X1 = this.RawXCoord;
  919. ECFieldElement _2Y1 = Two(Y1);
  920. ECFieldElement X = _2Y1.Square();
  921. ECFieldElement Z = Three(X1.Square()).Add(Curve.A);
  922. ECFieldElement Y = Z.Square();
  923. ECFieldElement d = Three(X1).Multiply(X).Subtract(Y);
  924. if (d.IsZero)
  925. {
  926. return Curve.Infinity;
  927. }
  928. ECFieldElement D = d.Multiply(_2Y1);
  929. ECFieldElement I = D.Invert();
  930. ECFieldElement L1 = d.Multiply(I).Multiply(Z);
  931. ECFieldElement L2 = X.Square().Multiply(I).Subtract(L1);
  932. ECFieldElement X4 = (L2.Subtract(L1)).Multiply(L1.Add(L2)).Add(X1);
  933. ECFieldElement Y4 = (X1.Subtract(X4)).Multiply(L2).Subtract(Y1);
  934. return new FpPoint(Curve, X4, Y4, IsCompressed);
  935. }
  936. case ECCurve.COORD_JACOBIAN_MODIFIED:
  937. {
  938. return TwiceJacobianModified(false).Add(this);
  939. }
  940. default:
  941. {
  942. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  943. return Twice().Add(this);
  944. }
  945. }
  946. }
  947. public override ECPoint TimesPow2(int e)
  948. {
  949. if (e < 0)
  950. throw new ArgumentException("cannot be negative", "e");
  951. if (e == 0 || this.IsInfinity)
  952. return this;
  953. if (e == 1)
  954. return Twice();
  955. ECCurve curve = this.Curve;
  956. ECFieldElement Y1 = this.RawYCoord;
  957. if (Y1.IsZero)
  958. return curve.Infinity;
  959. int coord = curve.CoordinateSystem;
  960. ECFieldElement W1 = curve.A;
  961. ECFieldElement X1 = this.RawXCoord;
  962. ECFieldElement Z1 = this.RawZCoords.Length < 1 ? curve.FromBigInteger(BigInteger.One) : this.RawZCoords[0];
  963. if (!Z1.IsOne)
  964. {
  965. switch (coord)
  966. {
  967. case ECCurve.COORD_HOMOGENEOUS:
  968. ECFieldElement Z1Sq = Z1.Square();
  969. X1 = X1.Multiply(Z1);
  970. Y1 = Y1.Multiply(Z1Sq);
  971. W1 = CalculateJacobianModifiedW(Z1, Z1Sq);
  972. break;
  973. case ECCurve.COORD_JACOBIAN:
  974. W1 = CalculateJacobianModifiedW(Z1, null);
  975. break;
  976. case ECCurve.COORD_JACOBIAN_MODIFIED:
  977. W1 = GetJacobianModifiedW();
  978. break;
  979. }
  980. }
  981. for (int i = 0; i < e; ++i)
  982. {
  983. if (Y1.IsZero)
  984. return curve.Infinity;
  985. ECFieldElement X1Squared = X1.Square();
  986. ECFieldElement M = Three(X1Squared);
  987. ECFieldElement _2Y1 = Two(Y1);
  988. ECFieldElement _2Y1Squared = _2Y1.Multiply(Y1);
  989. ECFieldElement S = Two(X1.Multiply(_2Y1Squared));
  990. ECFieldElement _4T = _2Y1Squared.Square();
  991. ECFieldElement _8T = Two(_4T);
  992. if (!W1.IsZero)
  993. {
  994. M = M.Add(W1);
  995. W1 = Two(_8T.Multiply(W1));
  996. }
  997. X1 = M.Square().Subtract(Two(S));
  998. Y1 = M.Multiply(S.Subtract(X1)).Subtract(_8T);
  999. Z1 = Z1.IsOne ? _2Y1 : _2Y1.Multiply(Z1);
  1000. }
  1001. switch (coord)
  1002. {
  1003. case ECCurve.COORD_AFFINE:
  1004. ECFieldElement zInv = Z1.Invert(), zInv2 = zInv.Square(), zInv3 = zInv2.Multiply(zInv);
  1005. return new FpPoint(curve, X1.Multiply(zInv2), Y1.Multiply(zInv3), IsCompressed);
  1006. case ECCurve.COORD_HOMOGENEOUS:
  1007. X1 = X1.Multiply(Z1);
  1008. Z1 = Z1.Multiply(Z1.Square());
  1009. return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1 }, IsCompressed);
  1010. case ECCurve.COORD_JACOBIAN:
  1011. return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1 }, IsCompressed);
  1012. case ECCurve.COORD_JACOBIAN_MODIFIED:
  1013. return new FpPoint(curve, X1, Y1, new ECFieldElement[] { Z1, W1 }, IsCompressed);
  1014. default:
  1015. throw new InvalidOperationException("unsupported coordinate system");
  1016. }
  1017. }
  1018. protected virtual ECFieldElement Two(ECFieldElement x)
  1019. {
  1020. return x.Add(x);
  1021. }
  1022. protected virtual ECFieldElement Three(ECFieldElement x)
  1023. {
  1024. return Two(x).Add(x);
  1025. }
  1026. protected virtual ECFieldElement Four(ECFieldElement x)
  1027. {
  1028. return Two(Two(x));
  1029. }
  1030. protected virtual ECFieldElement Eight(ECFieldElement x)
  1031. {
  1032. return Four(Two(x));
  1033. }
  1034. protected virtual ECFieldElement DoubleProductFromSquares(ECFieldElement a, ECFieldElement b,
  1035. ECFieldElement aSquared, ECFieldElement bSquared)
  1036. {
  1037. /*
  1038. * NOTE: If squaring in the field is faster than multiplication, then this is a quicker
  1039. * way to calculate 2.A.B, if A^2 and B^2 are already known.
  1040. */
  1041. return a.Add(b).Square().Subtract(aSquared).Subtract(bSquared);
  1042. }
  1043. public override ECPoint Negate()
  1044. {
  1045. if (IsInfinity)
  1046. return this;
  1047. ECCurve curve = Curve;
  1048. int coord = curve.CoordinateSystem;
  1049. if (ECCurve.COORD_AFFINE != coord)
  1050. {
  1051. return new FpPoint(curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed);
  1052. }
  1053. return new FpPoint(curve, RawXCoord, RawYCoord.Negate(), IsCompressed);
  1054. }
  1055. protected virtual ECFieldElement CalculateJacobianModifiedW(ECFieldElement Z, ECFieldElement ZSquared)
  1056. {
  1057. ECFieldElement a4 = this.Curve.A;
  1058. if (a4.IsZero || Z.IsOne)
  1059. return a4;
  1060. if (ZSquared == null)
  1061. {
  1062. ZSquared = Z.Square();
  1063. }
  1064. ECFieldElement W = ZSquared.Square();
  1065. ECFieldElement a4Neg = a4.Negate();
  1066. if (a4Neg.BitLength < a4.BitLength)
  1067. {
  1068. W = W.Multiply(a4Neg).Negate();
  1069. }
  1070. else
  1071. {
  1072. W = W.Multiply(a4);
  1073. }
  1074. return W;
  1075. }
  1076. protected virtual ECFieldElement GetJacobianModifiedW()
  1077. {
  1078. ECFieldElement[] ZZ = this.RawZCoords;
  1079. ECFieldElement W = ZZ[1];
  1080. if (W == null)
  1081. {
  1082. // NOTE: Rarely, TwicePlus will result in the need for a lazy W1 calculation here
  1083. ZZ[1] = W = CalculateJacobianModifiedW(ZZ[0], null);
  1084. }
  1085. return W;
  1086. }
  1087. protected virtual FpPoint TwiceJacobianModified(bool calculateW)
  1088. {
  1089. ECFieldElement X1 = this.RawXCoord, Y1 = this.RawYCoord, Z1 = this.RawZCoords[0], W1 = GetJacobianModifiedW();
  1090. ECFieldElement X1Squared = X1.Square();
  1091. ECFieldElement M = Three(X1Squared).Add(W1);
  1092. ECFieldElement _2Y1 = Two(Y1);
  1093. ECFieldElement _2Y1Squared = _2Y1.Multiply(Y1);
  1094. ECFieldElement S = Two(X1.Multiply(_2Y1Squared));
  1095. ECFieldElement X3 = M.Square().Subtract(Two(S));
  1096. ECFieldElement _4T = _2Y1Squared.Square();
  1097. ECFieldElement _8T = Two(_4T);
  1098. ECFieldElement Y3 = M.Multiply(S.Subtract(X3)).Subtract(_8T);
  1099. ECFieldElement W3 = calculateW ? Two(_8T.Multiply(W1)) : null;
  1100. ECFieldElement Z3 = Z1.IsOne ? _2Y1 : _2Y1.Multiply(Z1);
  1101. return new FpPoint(this.Curve, X3, Y3, new ECFieldElement[] { Z3, W3 }, IsCompressed);
  1102. }
  1103. }
  1104. public abstract class AbstractF2mPoint
  1105. : ECPointBase
  1106. {
  1107. protected AbstractF2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  1108. : base(curve, x, y, withCompression)
  1109. {
  1110. }
  1111. protected AbstractF2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  1112. : base(curve, x, y, zs, withCompression)
  1113. {
  1114. }
  1115. protected override bool SatisfiesCurveEquation()
  1116. {
  1117. ECCurve curve = Curve;
  1118. ECFieldElement X = this.RawXCoord, Y = this.RawYCoord, A = curve.A, B = curve.B;
  1119. ECFieldElement lhs, rhs;
  1120. int coord = curve.CoordinateSystem;
  1121. if (coord == ECCurve.COORD_LAMBDA_PROJECTIVE)
  1122. {
  1123. ECFieldElement Z = this.RawZCoords[0];
  1124. bool ZIsOne = Z.IsOne;
  1125. if (X.IsZero)
  1126. {
  1127. // NOTE: For x == 0, we expect the affine-y instead of the lambda-y
  1128. lhs = Y.Square();
  1129. rhs = B;
  1130. if (!ZIsOne)
  1131. {
  1132. ECFieldElement Z2 = Z.Square();
  1133. rhs = rhs.Multiply(Z2);
  1134. }
  1135. }
  1136. else
  1137. {
  1138. ECFieldElement L = Y, X2 = X.Square();
  1139. if (ZIsOne)
  1140. {
  1141. lhs = L.Square().Add(L).Add(A);
  1142. rhs = X2.Square().Add(B);
  1143. }
  1144. else
  1145. {
  1146. ECFieldElement Z2 = Z.Square(), Z4 = Z2.Square();
  1147. lhs = L.Add(Z).MultiplyPlusProduct(L, A, Z2);
  1148. // TODO If sqrt(b) is precomputed this can be simplified to a single square
  1149. rhs = X2.SquarePlusProduct(B, Z4);
  1150. }
  1151. lhs = lhs.Multiply(X2);
  1152. }
  1153. }
  1154. else
  1155. {
  1156. lhs = Y.Add(X).Multiply(Y);
  1157. switch (coord)
  1158. {
  1159. case ECCurve.COORD_AFFINE:
  1160. break;
  1161. case ECCurve.COORD_HOMOGENEOUS:
  1162. {
  1163. ECFieldElement Z = this.RawZCoords[0];
  1164. if (!Z.IsOne)
  1165. {
  1166. ECFieldElement Z2 = Z.Square(), Z3 = Z.Multiply(Z2);
  1167. lhs = lhs.Multiply(Z);
  1168. A = A.Multiply(Z);
  1169. B = B.Multiply(Z3);
  1170. }
  1171. break;
  1172. }
  1173. default:
  1174. throw new InvalidOperationException("unsupported coordinate system");
  1175. }
  1176. rhs = X.Add(A).Multiply(X.Square()).Add(B);
  1177. }
  1178. return lhs.Equals(rhs);
  1179. }
  1180. public override ECPoint ScaleX(ECFieldElement scale)
  1181. {
  1182. if (this.IsInfinity)
  1183. return this;
  1184. switch (CurveCoordinateSystem)
  1185. {
  1186. case ECCurve.COORD_LAMBDA_AFFINE:
  1187. {
  1188. // Y is actually Lambda (X + Y/X) here
  1189. ECFieldElement X = RawXCoord, L = RawYCoord;
  1190. ECFieldElement X2 = X.Multiply(scale);
  1191. ECFieldElement L2 = L.Add(X).Divide(scale).Add(X2);
  1192. return Curve.CreateRawPoint(X, L2, RawZCoords, IsCompressed);
  1193. }
  1194. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1195. {
  1196. // Y is actually Lambda (X + Y/X) here
  1197. ECFieldElement X = RawXCoord, L = RawYCoord, Z = RawZCoords[0];
  1198. // We scale the Z coordinate also, to avoid an inversion
  1199. ECFieldElement X2 = X.Multiply(scale.Square());
  1200. ECFieldElement L2 = L.Add(X).Add(X2);
  1201. ECFieldElement Z2 = Z.Multiply(scale);
  1202. return Curve.CreateRawPoint(X, L2, new ECFieldElement[] { Z2 }, IsCompressed);
  1203. }
  1204. default:
  1205. {
  1206. return base.ScaleX(scale);
  1207. }
  1208. }
  1209. }
  1210. public override ECPoint ScaleY(ECFieldElement scale)
  1211. {
  1212. if (this.IsInfinity)
  1213. return this;
  1214. switch (CurveCoordinateSystem)
  1215. {
  1216. case ECCurve.COORD_LAMBDA_AFFINE:
  1217. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1218. {
  1219. ECFieldElement X = RawXCoord, L = RawYCoord;
  1220. // Y is actually Lambda (X + Y/X) here
  1221. ECFieldElement L2 = L.Add(X).Multiply(scale).Add(X);
  1222. return Curve.CreateRawPoint(X, L2, RawZCoords, IsCompressed);
  1223. }
  1224. default:
  1225. {
  1226. return base.ScaleY(scale);
  1227. }
  1228. }
  1229. }
  1230. public override ECPoint Subtract(ECPoint b)
  1231. {
  1232. if (b.IsInfinity)
  1233. return this;
  1234. // Add -b
  1235. return Add(b.Negate());
  1236. }
  1237. public virtual AbstractF2mPoint Tau()
  1238. {
  1239. if (this.IsInfinity)
  1240. return this;
  1241. ECCurve curve = this.Curve;
  1242. int coord = curve.CoordinateSystem;
  1243. ECFieldElement X1 = this.RawXCoord;
  1244. switch (coord)
  1245. {
  1246. case ECCurve.COORD_AFFINE:
  1247. case ECCurve.COORD_LAMBDA_AFFINE:
  1248. {
  1249. ECFieldElement Y1 = this.RawYCoord;
  1250. return (AbstractF2mPoint)curve.CreateRawPoint(X1.Square(), Y1.Square(), IsCompressed);
  1251. }
  1252. case ECCurve.COORD_HOMOGENEOUS:
  1253. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1254. {
  1255. ECFieldElement Y1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  1256. return (AbstractF2mPoint)curve.CreateRawPoint(X1.Square(), Y1.Square(),
  1257. new ECFieldElement[] { Z1.Square() }, IsCompressed);
  1258. }
  1259. default:
  1260. {
  1261. throw new InvalidOperationException("unsupported coordinate system");
  1262. }
  1263. }
  1264. }
  1265. public virtual AbstractF2mPoint TauPow(int pow)
  1266. {
  1267. if (this.IsInfinity)
  1268. return this;
  1269. ECCurve curve = this.Curve;
  1270. int coord = curve.CoordinateSystem;
  1271. ECFieldElement X1 = this.RawXCoord;
  1272. switch (coord)
  1273. {
  1274. case ECCurve.COORD_AFFINE:
  1275. case ECCurve.COORD_LAMBDA_AFFINE:
  1276. {
  1277. ECFieldElement Y1 = this.RawYCoord;
  1278. return (AbstractF2mPoint)curve.CreateRawPoint(X1.SquarePow(pow), Y1.SquarePow(pow), IsCompressed);
  1279. }
  1280. case ECCurve.COORD_HOMOGENEOUS:
  1281. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1282. {
  1283. ECFieldElement Y1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  1284. return (AbstractF2mPoint)curve.CreateRawPoint(X1.SquarePow(pow), Y1.SquarePow(pow),
  1285. new ECFieldElement[] { Z1.SquarePow(pow) }, IsCompressed);
  1286. }
  1287. default:
  1288. {
  1289. throw new InvalidOperationException("unsupported coordinate system");
  1290. }
  1291. }
  1292. }
  1293. }
  1294. /**
  1295. * Elliptic curve points over F2m
  1296. */
  1297. public class F2mPoint
  1298. : AbstractF2mPoint
  1299. {
  1300. /**
  1301. * @param curve base curve
  1302. * @param x x point
  1303. * @param y y point
  1304. */
  1305. public F2mPoint(
  1306. ECCurve curve,
  1307. ECFieldElement x,
  1308. ECFieldElement y)
  1309. : this(curve, x, y, false)
  1310. {
  1311. }
  1312. /**
  1313. * @param curve base curve
  1314. * @param x x point
  1315. * @param y y point
  1316. * @param withCompression true if encode with point compression.
  1317. */
  1318. public F2mPoint(
  1319. ECCurve curve,
  1320. ECFieldElement x,
  1321. ECFieldElement y,
  1322. bool withCompression)
  1323. : base(curve, x, y, withCompression)
  1324. {
  1325. if ((x == null) != (y == null))
  1326. {
  1327. throw new ArgumentException("Exactly one of the field elements is null");
  1328. }
  1329. if (x != null)
  1330. {
  1331. // Check if x and y are elements of the same field
  1332. F2mFieldElement.CheckFieldElements(x, y);
  1333. // Check if x and a are elements of the same field
  1334. if (curve != null)
  1335. {
  1336. F2mFieldElement.CheckFieldElements(x, curve.A);
  1337. }
  1338. }
  1339. }
  1340. internal F2mPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  1341. : base(curve, x, y, zs, withCompression)
  1342. {
  1343. }
  1344. /**
  1345. * Constructor for point at infinity
  1346. */
  1347. [Obsolete("Use ECCurve.Infinity property")]
  1348. public F2mPoint(
  1349. ECCurve curve)
  1350. : this(curve, null, null)
  1351. {
  1352. }
  1353. protected override ECPoint Detach()
  1354. {
  1355. return new F2mPoint(null, AffineXCoord, AffineYCoord);
  1356. }
  1357. public override ECFieldElement YCoord
  1358. {
  1359. get
  1360. {
  1361. int coord = this.CurveCoordinateSystem;
  1362. switch (coord)
  1363. {
  1364. case ECCurve.COORD_LAMBDA_AFFINE:
  1365. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1366. {
  1367. ECFieldElement X = RawXCoord, L = RawYCoord;
  1368. if (this.IsInfinity || X.IsZero)
  1369. return L;
  1370. // Y is actually Lambda (X + Y/X) here; convert to affine value on the fly
  1371. ECFieldElement Y = L.Add(X).Multiply(X);
  1372. if (ECCurve.COORD_LAMBDA_PROJECTIVE == coord)
  1373. {
  1374. ECFieldElement Z = RawZCoords[0];
  1375. if (!Z.IsOne)
  1376. {
  1377. Y = Y.Divide(Z);
  1378. }
  1379. }
  1380. return Y;
  1381. }
  1382. default:
  1383. {
  1384. return RawYCoord;
  1385. }
  1386. }
  1387. }
  1388. }
  1389. protected internal override bool CompressionYTilde
  1390. {
  1391. get
  1392. {
  1393. ECFieldElement X = this.RawXCoord;
  1394. if (X.IsZero)
  1395. {
  1396. return false;
  1397. }
  1398. ECFieldElement Y = this.RawYCoord;
  1399. switch (this.CurveCoordinateSystem)
  1400. {
  1401. case ECCurve.COORD_LAMBDA_AFFINE:
  1402. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1403. {
  1404. // Y is actually Lambda (X + Y/X) here
  1405. return Y.TestBitZero() != X.TestBitZero();
  1406. }
  1407. default:
  1408. {
  1409. return Y.Divide(X).TestBitZero();
  1410. }
  1411. }
  1412. }
  1413. }
  1414. public override ECPoint Add(ECPoint b)
  1415. {
  1416. if (this.IsInfinity)
  1417. return b;
  1418. if (b.IsInfinity)
  1419. return this;
  1420. ECCurve curve = this.Curve;
  1421. int coord = curve.CoordinateSystem;
  1422. ECFieldElement X1 = this.RawXCoord;
  1423. ECFieldElement X2 = b.RawXCoord;
  1424. switch (coord)
  1425. {
  1426. case ECCurve.COORD_AFFINE:
  1427. {
  1428. ECFieldElement Y1 = this.RawYCoord;
  1429. ECFieldElement Y2 = b.RawYCoord;
  1430. ECFieldElement dx = X1.Add(X2), dy = Y1.Add(Y2);
  1431. if (dx.IsZero)
  1432. {
  1433. if (dy.IsZero)
  1434. {
  1435. return Twice();
  1436. }
  1437. return curve.Infinity;
  1438. }
  1439. ECFieldElement L = dy.Divide(dx);
  1440. ECFieldElement X3 = L.Square().Add(L).Add(dx).Add(curve.A);
  1441. ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1);
  1442. return new F2mPoint(curve, X3, Y3, IsCompressed);
  1443. }
  1444. case ECCurve.COORD_HOMOGENEOUS:
  1445. {
  1446. ECFieldElement Y1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  1447. ECFieldElement Y2 = b.RawYCoord, Z2 = b.RawZCoords[0];
  1448. bool Z1IsOne = Z1.IsOne;
  1449. ECFieldElement U1 = Y2, V1 = X2;
  1450. if (!Z1IsOne)
  1451. {
  1452. U1 = U1.Multiply(Z1);
  1453. V1 = V1.Multiply(Z1);
  1454. }
  1455. bool Z2IsOne = Z2.IsOne;
  1456. ECFieldElement U2 = Y1, V2 = X1;
  1457. if (!Z2IsOne)
  1458. {
  1459. U2 = U2.Multiply(Z2);
  1460. V2 = V2.Multiply(Z2);
  1461. }
  1462. ECFieldElement U = U1.Add(U2);
  1463. ECFieldElement V = V1.Add(V2);
  1464. if (V.IsZero)
  1465. {
  1466. if (U.IsZero)
  1467. {
  1468. return Twice();
  1469. }
  1470. return curve.Infinity;
  1471. }
  1472. ECFieldElement VSq = V.Square();
  1473. ECFieldElement VCu = VSq.Multiply(V);
  1474. ECFieldElement W = Z1IsOne ? Z2 : Z2IsOne ? Z1 : Z1.Multiply(Z2);
  1475. ECFieldElement uv = U.Add(V);
  1476. ECFieldElement A = uv.MultiplyPlusProduct(U, VSq, curve.A).Multiply(W).Add(VCu);
  1477. ECFieldElement X3 = V.Multiply(A);
  1478. ECFieldElement VSqZ2 = Z2IsOne ? VSq : VSq.Multiply(Z2);
  1479. ECFieldElement Y3 = U.MultiplyPlusProduct(X1, V, Y1).MultiplyPlusProduct(VSqZ2, uv, A);
  1480. ECFieldElement Z3 = VCu.Multiply(W);
  1481. return new F2mPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  1482. }
  1483. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1484. {
  1485. if (X1.IsZero)
  1486. {
  1487. if (X2.IsZero)
  1488. return curve.Infinity;
  1489. return b.Add(this);
  1490. }
  1491. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  1492. ECFieldElement L2 = b.RawYCoord, Z2 = b.RawZCoords[0];
  1493. bool Z1IsOne = Z1.IsOne;
  1494. ECFieldElement U2 = X2, S2 = L2;
  1495. if (!Z1IsOne)
  1496. {
  1497. U2 = U2.Multiply(Z1);
  1498. S2 = S2.Multiply(Z1);
  1499. }
  1500. bool Z2IsOne = Z2.IsOne;
  1501. ECFieldElement U1 = X1, S1 = L1;
  1502. if (!Z2IsOne)
  1503. {
  1504. U1 = U1.Multiply(Z2);
  1505. S1 = S1.Multiply(Z2);
  1506. }
  1507. ECFieldElement A = S1.Add(S2);
  1508. ECFieldElement B = U1.Add(U2);
  1509. if (B.IsZero)
  1510. {
  1511. if (A.IsZero)
  1512. {
  1513. return Twice();
  1514. }
  1515. return curve.Infinity;
  1516. }
  1517. ECFieldElement X3, L3, Z3;
  1518. if (X2.IsZero)
  1519. {
  1520. // TODO This can probably be optimized quite a bit
  1521. ECPoint p = this.Normalize();
  1522. X1 = p.RawXCoord;
  1523. ECFieldElement Y1 = p.YCoord;
  1524. ECFieldElement Y2 = L2;
  1525. ECFieldElement L = Y1.Add(Y2).Divide(X1);
  1526. X3 = L.Square().Add(L).Add(X1).Add(curve.A);
  1527. if (X3.IsZero)
  1528. {
  1529. return new F2mPoint(curve, X3, curve.B.Sqrt(), IsCompressed);
  1530. }
  1531. ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1);
  1532. L3 = Y3.Divide(X3).Add(X3);
  1533. Z3 = curve.FromBigInteger(BigInteger.One);
  1534. }
  1535. else
  1536. {
  1537. B = B.Square();
  1538. ECFieldElement AU1 = A.Multiply(U1);
  1539. ECFieldElement AU2 = A.Multiply(U2);
  1540. X3 = AU1.Multiply(AU2);
  1541. if (X3.IsZero)
  1542. {
  1543. return new F2mPoint(curve, X3, curve.B.Sqrt(), IsCompressed);
  1544. }
  1545. ECFieldElement ABZ2 = A.Multiply(B);
  1546. if (!Z2IsOne)
  1547. {
  1548. ABZ2 = ABZ2.Multiply(Z2);
  1549. }
  1550. L3 = AU2.Add(B).SquarePlusProduct(ABZ2, L1.Add(Z1));
  1551. Z3 = ABZ2;
  1552. if (!Z1IsOne)
  1553. {
  1554. Z3 = Z3.Multiply(Z1);
  1555. }
  1556. }
  1557. return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed);
  1558. }
  1559. default:
  1560. {
  1561. throw new InvalidOperationException("unsupported coordinate system");
  1562. }
  1563. }
  1564. }
  1565. /* (non-Javadoc)
  1566. * @see Org.BouncyCastle.Math.EC.ECPoint#twice()
  1567. */
  1568. public override ECPoint Twice()
  1569. {
  1570. if (this.IsInfinity)
  1571. return this;
  1572. ECCurve curve = this.Curve;
  1573. ECFieldElement X1 = this.RawXCoord;
  1574. if (X1.IsZero)
  1575. {
  1576. // A point with X == 0 is it's own additive inverse
  1577. return curve.Infinity;
  1578. }
  1579. int coord = curve.CoordinateSystem;
  1580. switch (coord)
  1581. {
  1582. case ECCurve.COORD_AFFINE:
  1583. {
  1584. ECFieldElement Y1 = this.RawYCoord;
  1585. ECFieldElement L1 = Y1.Divide(X1).Add(X1);
  1586. ECFieldElement X3 = L1.Square().Add(L1).Add(curve.A);
  1587. ECFieldElement Y3 = X1.SquarePlusProduct(X3, L1.AddOne());
  1588. return new F2mPoint(curve, X3, Y3, IsCompressed);
  1589. }
  1590. case ECCurve.COORD_HOMOGENEOUS:
  1591. {
  1592. ECFieldElement Y1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  1593. bool Z1IsOne = Z1.IsOne;
  1594. ECFieldElement X1Z1 = Z1IsOne ? X1 : X1.Multiply(Z1);
  1595. ECFieldElement Y1Z1 = Z1IsOne ? Y1 : Y1.Multiply(Z1);
  1596. ECFieldElement X1Sq = X1.Square();
  1597. ECFieldElement S = X1Sq.Add(Y1Z1);
  1598. ECFieldElement V = X1Z1;
  1599. ECFieldElement vSquared = V.Square();
  1600. ECFieldElement sv = S.Add(V);
  1601. ECFieldElement h = sv.MultiplyPlusProduct(S, vSquared, curve.A);
  1602. ECFieldElement X3 = V.Multiply(h);
  1603. ECFieldElement Y3 = X1Sq.Square().MultiplyPlusProduct(V, h, sv);
  1604. ECFieldElement Z3 = V.Multiply(vSquared);
  1605. return new F2mPoint(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  1606. }
  1607. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1608. {
  1609. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  1610. bool Z1IsOne = Z1.IsOne;
  1611. ECFieldElement L1Z1 = Z1IsOne ? L1 : L1.Multiply(Z1);
  1612. ECFieldElement Z1Sq = Z1IsOne ? Z1 : Z1.Square();
  1613. ECFieldElement a = curve.A;
  1614. ECFieldElement aZ1Sq = Z1IsOne ? a : a.Multiply(Z1Sq);
  1615. ECFieldElement T = L1.Square().Add(L1Z1).Add(aZ1Sq);
  1616. if (T.IsZero)
  1617. {
  1618. return new F2mPoint(curve, T, curve.B.Sqrt(), IsCompressed);
  1619. }
  1620. ECFieldElement X3 = T.Square();
  1621. ECFieldElement Z3 = Z1IsOne ? T : T.Multiply(Z1Sq);
  1622. ECFieldElement b = curve.B;
  1623. ECFieldElement L3;
  1624. if (b.BitLength < (curve.FieldSize >> 1))
  1625. {
  1626. ECFieldElement t1 = L1.Add(X1).Square();
  1627. ECFieldElement t2;
  1628. if (b.IsOne)
  1629. {
  1630. t2 = aZ1Sq.Add(Z1Sq).Square();
  1631. }
  1632. else
  1633. {
  1634. // TODO Can be calculated with one square if we pre-compute sqrt(b)
  1635. t2 = aZ1Sq.SquarePlusProduct(b, Z1Sq.Square());
  1636. }
  1637. L3 = t1.Add(T).Add(Z1Sq).Multiply(t1).Add(t2).Add(X3);
  1638. if (a.IsZero)
  1639. {
  1640. L3 = L3.Add(Z3);
  1641. }
  1642. else if (!a.IsOne)
  1643. {
  1644. L3 = L3.Add(a.AddOne().Multiply(Z3));
  1645. }
  1646. }
  1647. else
  1648. {
  1649. ECFieldElement X1Z1 = Z1IsOne ? X1 : X1.Multiply(Z1);
  1650. L3 = X1Z1.SquarePlusProduct(T, L1Z1).Add(X3).Add(Z3);
  1651. }
  1652. return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed);
  1653. }
  1654. default:
  1655. {
  1656. throw new InvalidOperationException("unsupported coordinate system");
  1657. }
  1658. }
  1659. }
  1660. public override ECPoint TwicePlus(ECPoint b)
  1661. {
  1662. if (this.IsInfinity)
  1663. return b;
  1664. if (b.IsInfinity)
  1665. return Twice();
  1666. ECCurve curve = this.Curve;
  1667. ECFieldElement X1 = this.RawXCoord;
  1668. if (X1.IsZero)
  1669. {
  1670. // A point with X == 0 is it's own additive inverse
  1671. return b;
  1672. }
  1673. int coord = curve.CoordinateSystem;
  1674. switch (coord)
  1675. {
  1676. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1677. {
  1678. // NOTE: twicePlus() only optimized for lambda-affine argument
  1679. ECFieldElement X2 = b.RawXCoord, Z2 = b.RawZCoords[0];
  1680. if (X2.IsZero || !Z2.IsOne)
  1681. {
  1682. return Twice().Add(b);
  1683. }
  1684. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  1685. ECFieldElement L2 = b.RawYCoord;
  1686. ECFieldElement X1Sq = X1.Square();
  1687. ECFieldElement L1Sq = L1.Square();
  1688. ECFieldElement Z1Sq = Z1.Square();
  1689. ECFieldElement L1Z1 = L1.Multiply(Z1);
  1690. ECFieldElement T = curve.A.Multiply(Z1Sq).Add(L1Sq).Add(L1Z1);
  1691. ECFieldElement L2plus1 = L2.AddOne();
  1692. ECFieldElement A = curve.A.Add(L2plus1).Multiply(Z1Sq).Add(L1Sq).MultiplyPlusProduct(T, X1Sq, Z1Sq);
  1693. ECFieldElement X2Z1Sq = X2.Multiply(Z1Sq);
  1694. ECFieldElement B = X2Z1Sq.Add(T).Square();
  1695. if (B.IsZero)
  1696. {
  1697. if (A.IsZero)
  1698. {
  1699. return b.Twice();
  1700. }
  1701. return curve.Infinity;
  1702. }
  1703. if (A.IsZero)
  1704. {
  1705. return new F2mPoint(curve, A, curve.B.Sqrt(), IsCompressed);
  1706. }
  1707. ECFieldElement X3 = A.Square().Multiply(X2Z1Sq);
  1708. ECFieldElement Z3 = A.Multiply(B).Multiply(Z1Sq);
  1709. ECFieldElement L3 = A.Add(B).Square().MultiplyPlusProduct(T, L2plus1, Z3);
  1710. return new F2mPoint(curve, X3, L3, new ECFieldElement[] { Z3 }, IsCompressed);
  1711. }
  1712. default:
  1713. {
  1714. return Twice().Add(b);
  1715. }
  1716. }
  1717. }
  1718. public override ECPoint Negate()
  1719. {
  1720. if (this.IsInfinity)
  1721. return this;
  1722. ECFieldElement X = this.RawXCoord;
  1723. if (X.IsZero)
  1724. return this;
  1725. ECCurve curve = this.Curve;
  1726. int coord = curve.CoordinateSystem;
  1727. switch (coord)
  1728. {
  1729. case ECCurve.COORD_AFFINE:
  1730. {
  1731. ECFieldElement Y = this.RawYCoord;
  1732. return new F2mPoint(curve, X, Y.Add(X), IsCompressed);
  1733. }
  1734. case ECCurve.COORD_HOMOGENEOUS:
  1735. {
  1736. ECFieldElement Y = this.RawYCoord, Z = this.RawZCoords[0];
  1737. return new F2mPoint(curve, X, Y.Add(X), new ECFieldElement[] { Z }, IsCompressed);
  1738. }
  1739. case ECCurve.COORD_LAMBDA_AFFINE:
  1740. {
  1741. ECFieldElement L = this.RawYCoord;
  1742. return new F2mPoint(curve, X, L.AddOne(), IsCompressed);
  1743. }
  1744. case ECCurve.COORD_LAMBDA_PROJECTIVE:
  1745. {
  1746. // L is actually Lambda (X + Y/X) here
  1747. ECFieldElement L = this.RawYCoord, Z = this.RawZCoords[0];
  1748. return new F2mPoint(curve, X, L.Add(Z), new ECFieldElement[] { Z }, IsCompressed);
  1749. }
  1750. default:
  1751. {
  1752. throw new InvalidOperationException("unsupported coordinate system");
  1753. }
  1754. }
  1755. }
  1756. }
  1757. }
  1758. #endif