ECAlgorithms.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Endo;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Multiplier;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Field;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC
  9. {
  10. public class ECAlgorithms
  11. {
  12. public static bool IsF2mCurve(ECCurve c)
  13. {
  14. return IsF2mField(c.Field);
  15. }
  16. public static bool IsF2mField(IFiniteField field)
  17. {
  18. return field.Dimension > 1 && field.Characteristic.Equals(BigInteger.Two)
  19. && field is IPolynomialExtensionField;
  20. }
  21. public static bool IsFpCurve(ECCurve c)
  22. {
  23. return IsFpField(c.Field);
  24. }
  25. public static bool IsFpField(IFiniteField field)
  26. {
  27. return field.Dimension == 1;
  28. }
  29. public static ECPoint SumOfMultiplies(ECPoint[] ps, BigInteger[] ks)
  30. {
  31. if (ps == null || ks == null || ps.Length != ks.Length || ps.Length < 1)
  32. throw new ArgumentException("point and scalar arrays should be non-null, and of equal, non-zero, length");
  33. int count = ps.Length;
  34. switch (count)
  35. {
  36. case 1:
  37. return ps[0].Multiply(ks[0]);
  38. case 2:
  39. return SumOfTwoMultiplies(ps[0], ks[0], ps[1], ks[1]);
  40. default:
  41. break;
  42. }
  43. ECPoint p = ps[0];
  44. ECCurve c = p.Curve;
  45. ECPoint[] imported = new ECPoint[count];
  46. imported[0] = p;
  47. for (int i = 1; i < count; ++i)
  48. {
  49. imported[i] = ImportPoint(c, ps[i]);
  50. }
  51. GlvEndomorphism glvEndomorphism = c.GetEndomorphism() as GlvEndomorphism;
  52. if (glvEndomorphism != null)
  53. {
  54. return ImplCheckResult(ImplSumOfMultipliesGlv(imported, ks, glvEndomorphism));
  55. }
  56. return ImplCheckResult(ImplSumOfMultiplies(imported, ks));
  57. }
  58. public static ECPoint SumOfTwoMultiplies(ECPoint P, BigInteger a, ECPoint Q, BigInteger b)
  59. {
  60. ECCurve cp = P.Curve;
  61. Q = ImportPoint(cp, Q);
  62. // Point multiplication for Koblitz curves (using WTNAF) beats Shamir's trick
  63. {
  64. AbstractF2mCurve f2mCurve = cp as AbstractF2mCurve;
  65. if (f2mCurve != null && f2mCurve.IsKoblitz)
  66. {
  67. return ImplCheckResult(P.Multiply(a).Add(Q.Multiply(b)));
  68. }
  69. }
  70. GlvEndomorphism glvEndomorphism = cp.GetEndomorphism() as GlvEndomorphism;
  71. if (glvEndomorphism != null)
  72. {
  73. return ImplCheckResult(
  74. ImplSumOfMultipliesGlv(new ECPoint[] { P, Q }, new BigInteger[] { a, b }, glvEndomorphism));
  75. }
  76. return ImplCheckResult(ImplShamirsTrickWNaf(P, a, Q, b));
  77. }
  78. /*
  79. * "Shamir's Trick", originally due to E. G. Straus
  80. * (Addition chains of vectors. American Mathematical Monthly,
  81. * 71(7):806-808, Aug./Sept. 1964)
  82. *
  83. * Input: The points P, Q, scalar k = (km?, ... , k1, k0)
  84. * and scalar l = (lm?, ... , l1, l0).
  85. * Output: R = k * P + l * Q.
  86. * 1: Z <- P + Q
  87. * 2: R <- O
  88. * 3: for i from m-1 down to 0 do
  89. * 4: R <- R + R {point doubling}
  90. * 5: if (ki = 1) and (li = 0) then R <- R + P end if
  91. * 6: if (ki = 0) and (li = 1) then R <- R + Q end if
  92. * 7: if (ki = 1) and (li = 1) then R <- R + Z end if
  93. * 8: end for
  94. * 9: return R
  95. */
  96. public static ECPoint ShamirsTrick(ECPoint P, BigInteger k, ECPoint Q, BigInteger l)
  97. {
  98. ECCurve cp = P.Curve;
  99. Q = ImportPoint(cp, Q);
  100. return ImplCheckResult(ImplShamirsTrickJsf(P, k, Q, l));
  101. }
  102. public static ECPoint ImportPoint(ECCurve c, ECPoint p)
  103. {
  104. ECCurve cp = p.Curve;
  105. if (!c.Equals(cp))
  106. throw new ArgumentException("Point must be on the same curve");
  107. return c.ImportPoint(p);
  108. }
  109. public static void MontgomeryTrick(ECFieldElement[] zs, int off, int len)
  110. {
  111. MontgomeryTrick(zs, off, len, null);
  112. }
  113. public static void MontgomeryTrick(ECFieldElement[] zs, int off, int len, ECFieldElement scale)
  114. {
  115. /*
  116. * Uses the "Montgomery Trick" to invert many field elements, with only a single actual
  117. * field inversion. See e.g. the paper:
  118. * "Fast Multi-scalar Multiplication Methods on Elliptic Curves with Precomputation Strategy Using Montgomery Trick"
  119. * by Katsuyuki Okeya, Kouichi Sakurai.
  120. */
  121. ECFieldElement[] c = new ECFieldElement[len];
  122. c[0] = zs[off];
  123. int i = 0;
  124. while (++i < len)
  125. {
  126. c[i] = c[i - 1].Multiply(zs[off + i]);
  127. }
  128. --i;
  129. if (scale != null)
  130. {
  131. c[i] = c[i].Multiply(scale);
  132. }
  133. ECFieldElement u = c[i].Invert();
  134. while (i > 0)
  135. {
  136. int j = off + i--;
  137. ECFieldElement tmp = zs[j];
  138. zs[j] = c[i].Multiply(u);
  139. u = u.Multiply(tmp);
  140. }
  141. zs[off] = u;
  142. }
  143. /**
  144. * Simple shift-and-add multiplication. Serves as reference implementation to verify (possibly
  145. * faster) implementations, and for very small scalars. CAUTION: This implementation is NOT
  146. * constant-time in any way. It is only intended to be used for diagnostics.
  147. *
  148. * @param p
  149. * The point to multiply.
  150. * @param k
  151. * The multiplier.
  152. * @return The result of the point multiplication <code>kP</code>.
  153. */
  154. public static ECPoint ReferenceMultiply(ECPoint p, BigInteger k)
  155. {
  156. BigInteger x = k.Abs();
  157. ECPoint q = p.Curve.Infinity;
  158. int t = x.BitLength;
  159. if (t > 0)
  160. {
  161. if (x.TestBit(0))
  162. {
  163. q = p;
  164. }
  165. for (int i = 1; i < t; i++)
  166. {
  167. p = p.Twice();
  168. if (x.TestBit(i))
  169. {
  170. q = q.Add(p);
  171. }
  172. }
  173. }
  174. return k.SignValue < 0 ? q.Negate() : q;
  175. }
  176. public static ECPoint ValidatePoint(ECPoint p)
  177. {
  178. if (!p.IsValid())
  179. throw new InvalidOperationException("Invalid point");
  180. return p;
  181. }
  182. public static ECPoint CleanPoint(ECCurve c, ECPoint p)
  183. {
  184. ECCurve cp = p.Curve;
  185. if (!c.Equals(cp))
  186. throw new ArgumentException("Point must be on the same curve", nameof(p));
  187. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  188. int encodedLength = p.GetEncodedLength(false);
  189. Span<byte> encoding = encodedLength <= 512
  190. ? stackalloc byte[encodedLength]
  191. : new byte[encodedLength];
  192. p.EncodeTo(false, encoding);
  193. return c.DecodePoint(encoding);
  194. #else
  195. return c.DecodePoint(p.GetEncoded(false));
  196. #endif
  197. }
  198. internal static ECPoint ImplCheckResult(ECPoint p)
  199. {
  200. if (!p.IsValidPartial())
  201. throw new InvalidOperationException("Invalid result");
  202. return p;
  203. }
  204. internal static ECPoint ImplShamirsTrickJsf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l)
  205. {
  206. ECCurve curve = P.Curve;
  207. ECPoint infinity = curve.Infinity;
  208. // TODO conjugate co-Z addition (ZADDC) can return both of these
  209. ECPoint PaddQ = P.Add(Q);
  210. ECPoint PsubQ = P.Subtract(Q);
  211. ECPoint[] points = new ECPoint[] { Q, PsubQ, P, PaddQ };
  212. curve.NormalizeAll(points);
  213. ECPoint[] table = new ECPoint[] {
  214. points[3].Negate(), points[2].Negate(), points[1].Negate(),
  215. points[0].Negate(), infinity, points[0],
  216. points[1], points[2], points[3] };
  217. byte[] jsf = WNafUtilities.GenerateJsf(k, l);
  218. ECPoint R = infinity;
  219. int i = jsf.Length;
  220. while (--i >= 0)
  221. {
  222. int jsfi = jsf[i];
  223. // NOTE: The shifting ensures the sign is extended correctly
  224. int kDigit = ((jsfi << 24) >> 28), lDigit = ((jsfi << 28) >> 28);
  225. int index = 4 + (kDigit * 3) + lDigit;
  226. R = R.TwicePlus(table[index]);
  227. }
  228. return R;
  229. }
  230. internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k,
  231. ECPoint Q, BigInteger l)
  232. {
  233. bool negK = k.SignValue < 0, negL = l.SignValue < 0;
  234. BigInteger kAbs = k.Abs(), lAbs = l.Abs();
  235. int minWidthP = WNafUtilities.GetWindowSize(kAbs.BitLength, 8);
  236. int minWidthQ = WNafUtilities.GetWindowSize(lAbs.BitLength, 8);
  237. WNafPreCompInfo infoP = WNafUtilities.Precompute(P, minWidthP, true);
  238. WNafPreCompInfo infoQ = WNafUtilities.Precompute(Q, minWidthQ, true);
  239. // When P, Q are 'promoted' (i.e. reused several times), switch to fixed-point algorithm
  240. {
  241. ECCurve c = P.Curve;
  242. int combSize = FixedPointUtilities.GetCombSize(c);
  243. if (!negK && !negL
  244. && k.BitLength <= combSize && l.BitLength <= combSize
  245. && infoP.IsPromoted && infoQ.IsPromoted)
  246. {
  247. return ImplShamirsTrickFixedPoint(P, k, Q, l);
  248. }
  249. }
  250. int widthP = System.Math.Min(8, infoP.Width);
  251. int widthQ = System.Math.Min(8, infoQ.Width);
  252. ECPoint[] preCompP = negK ? infoP.PreCompNeg : infoP.PreComp;
  253. ECPoint[] preCompQ = negL ? infoQ.PreCompNeg : infoQ.PreComp;
  254. ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg;
  255. ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg;
  256. byte[] wnafP = WNafUtilities.GenerateWindowNaf(widthP, kAbs);
  257. byte[] wnafQ = WNafUtilities.GenerateWindowNaf(widthQ, lAbs);
  258. return ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ);
  259. }
  260. internal static ECPoint ImplShamirsTrickWNaf(ECEndomorphism endomorphism, ECPoint P, BigInteger k, BigInteger l)
  261. {
  262. bool negK = k.SignValue < 0, negL = l.SignValue < 0;
  263. k = k.Abs();
  264. l = l.Abs();
  265. int minWidth = WNafUtilities.GetWindowSize(System.Math.Max(k.BitLength, l.BitLength), 8);
  266. WNafPreCompInfo infoP = WNafUtilities.Precompute(P, minWidth, true);
  267. ECPoint Q = EndoUtilities.MapPoint(endomorphism, P);
  268. WNafPreCompInfo infoQ = WNafUtilities.PrecomputeWithPointMap(Q, endomorphism.PointMap, infoP, true);
  269. int widthP = System.Math.Min(8, infoP.Width);
  270. int widthQ = System.Math.Min(8, infoQ.Width);
  271. ECPoint[] preCompP = negK ? infoP.PreCompNeg : infoP.PreComp;
  272. ECPoint[] preCompQ = negL ? infoQ.PreCompNeg : infoQ.PreComp;
  273. ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg;
  274. ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg;
  275. byte[] wnafP = WNafUtilities.GenerateWindowNaf(widthP, k);
  276. byte[] wnafQ = WNafUtilities.GenerateWindowNaf(widthQ, l);
  277. return ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ);
  278. }
  279. private static ECPoint ImplShamirsTrickWNaf(ECPoint[] preCompP, ECPoint[] preCompNegP, byte[] wnafP,
  280. ECPoint[] preCompQ, ECPoint[] preCompNegQ, byte[] wnafQ)
  281. {
  282. int len = System.Math.Max(wnafP.Length, wnafQ.Length);
  283. ECCurve curve = preCompP[0].Curve;
  284. ECPoint infinity = curve.Infinity;
  285. ECPoint R = infinity;
  286. int zeroes = 0;
  287. for (int i = len - 1; i >= 0; --i)
  288. {
  289. int wiP = i < wnafP.Length ? (int)(sbyte)wnafP[i] : 0;
  290. int wiQ = i < wnafQ.Length ? (int)(sbyte)wnafQ[i] : 0;
  291. if ((wiP | wiQ) == 0)
  292. {
  293. ++zeroes;
  294. continue;
  295. }
  296. ECPoint r = infinity;
  297. if (wiP != 0)
  298. {
  299. int nP = System.Math.Abs(wiP);
  300. ECPoint[] tableP = wiP < 0 ? preCompNegP : preCompP;
  301. r = r.Add(tableP[nP >> 1]);
  302. }
  303. if (wiQ != 0)
  304. {
  305. int nQ = System.Math.Abs(wiQ);
  306. ECPoint[] tableQ = wiQ < 0 ? preCompNegQ : preCompQ;
  307. r = r.Add(tableQ[nQ >> 1]);
  308. }
  309. if (zeroes > 0)
  310. {
  311. R = R.TimesPow2(zeroes);
  312. zeroes = 0;
  313. }
  314. R = R.TwicePlus(r);
  315. }
  316. if (zeroes > 0)
  317. {
  318. R = R.TimesPow2(zeroes);
  319. }
  320. return R;
  321. }
  322. internal static ECPoint ImplSumOfMultiplies(ECPoint[] ps, BigInteger[] ks)
  323. {
  324. int count = ps.Length;
  325. bool[] negs = new bool[count];
  326. WNafPreCompInfo[] infos = new WNafPreCompInfo[count];
  327. byte[][] wnafs = new byte[count][];
  328. for (int i = 0; i < count; ++i)
  329. {
  330. BigInteger ki = ks[i]; negs[i] = ki.SignValue < 0; ki = ki.Abs();
  331. int minWidth = WNafUtilities.GetWindowSize(ki.BitLength, 8);
  332. WNafPreCompInfo info = WNafUtilities.Precompute(ps[i], minWidth, true);
  333. int width = System.Math.Min(8, info.Width);
  334. infos[i] = info;
  335. wnafs[i] = WNafUtilities.GenerateWindowNaf(width, ki);
  336. }
  337. return ImplSumOfMultiplies(negs, infos, wnafs);
  338. }
  339. internal static ECPoint ImplSumOfMultipliesGlv(ECPoint[] ps, BigInteger[] ks, GlvEndomorphism glvEndomorphism)
  340. {
  341. BigInteger n = ps[0].Curve.Order;
  342. int len = ps.Length;
  343. BigInteger[] abs = new BigInteger[len << 1];
  344. for (int i = 0, j = 0; i < len; ++i)
  345. {
  346. BigInteger[] ab = glvEndomorphism.DecomposeScalar(ks[i].Mod(n));
  347. abs[j++] = ab[0];
  348. abs[j++] = ab[1];
  349. }
  350. if (glvEndomorphism.HasEfficientPointMap)
  351. {
  352. return ImplSumOfMultiplies(glvEndomorphism, ps, abs);
  353. }
  354. ECPoint[] pqs = new ECPoint[len << 1];
  355. for (int i = 0, j = 0; i < len; ++i)
  356. {
  357. ECPoint p = ps[i];
  358. ECPoint q = EndoUtilities.MapPoint(glvEndomorphism, p);
  359. pqs[j++] = p;
  360. pqs[j++] = q;
  361. }
  362. return ImplSumOfMultiplies(pqs, abs);
  363. }
  364. internal static ECPoint ImplSumOfMultiplies(ECEndomorphism endomorphism, ECPoint[] ps, BigInteger[] ks)
  365. {
  366. int halfCount = ps.Length, fullCount = halfCount << 1;
  367. bool[] negs = new bool[fullCount];
  368. WNafPreCompInfo[] infos = new WNafPreCompInfo[fullCount];
  369. byte[][] wnafs = new byte[fullCount][];
  370. ECPointMap pointMap = endomorphism.PointMap;
  371. for (int i = 0; i < halfCount; ++i)
  372. {
  373. int j0 = i << 1, j1 = j0 + 1;
  374. BigInteger kj0 = ks[j0]; negs[j0] = kj0.SignValue < 0; kj0 = kj0.Abs();
  375. BigInteger kj1 = ks[j1]; negs[j1] = kj1.SignValue < 0; kj1 = kj1.Abs();
  376. int minWidth = WNafUtilities.GetWindowSize(System.Math.Max(kj0.BitLength, kj1.BitLength), 8);
  377. ECPoint P = ps[i];
  378. WNafPreCompInfo infoP = WNafUtilities.Precompute(P, minWidth, true);
  379. ECPoint Q = EndoUtilities.MapPoint(endomorphism, P);
  380. WNafPreCompInfo infoQ = WNafUtilities.PrecomputeWithPointMap(Q, pointMap, infoP, true);
  381. int widthP = System.Math.Min(8, infoP.Width);
  382. int widthQ = System.Math.Min(8, infoQ.Width);
  383. infos[j0] = infoP;
  384. infos[j1] = infoQ;
  385. wnafs[j0] = WNafUtilities.GenerateWindowNaf(widthP, kj0);
  386. wnafs[j1] = WNafUtilities.GenerateWindowNaf(widthQ, kj1);
  387. }
  388. return ImplSumOfMultiplies(negs, infos, wnafs);
  389. }
  390. private static ECPoint ImplSumOfMultiplies(bool[] negs, WNafPreCompInfo[] infos, byte[][] wnafs)
  391. {
  392. int len = 0, count = wnafs.Length;
  393. for (int i = 0; i < count; ++i)
  394. {
  395. len = System.Math.Max(len, wnafs[i].Length);
  396. }
  397. ECCurve curve = infos[0].PreComp[0].Curve;
  398. ECPoint infinity = curve.Infinity;
  399. ECPoint R = infinity;
  400. int zeroes = 0;
  401. for (int i = len - 1; i >= 0; --i)
  402. {
  403. ECPoint r = infinity;
  404. for (int j = 0; j < count; ++j)
  405. {
  406. byte[] wnaf = wnafs[j];
  407. int wi = i < wnaf.Length ? (int)(sbyte)wnaf[i] : 0;
  408. if (wi != 0)
  409. {
  410. int n = System.Math.Abs(wi);
  411. WNafPreCompInfo info = infos[j];
  412. ECPoint[] table = (wi < 0 == negs[j]) ? info.PreComp : info.PreCompNeg;
  413. r = r.Add(table[n >> 1]);
  414. }
  415. }
  416. if (r == infinity)
  417. {
  418. ++zeroes;
  419. continue;
  420. }
  421. if (zeroes > 0)
  422. {
  423. R = R.TimesPow2(zeroes);
  424. zeroes = 0;
  425. }
  426. R = R.TwicePlus(r);
  427. }
  428. if (zeroes > 0)
  429. {
  430. R = R.TimesPow2(zeroes);
  431. }
  432. return R;
  433. }
  434. private static ECPoint ImplShamirsTrickFixedPoint(ECPoint p, BigInteger k, ECPoint q, BigInteger l)
  435. {
  436. ECCurve c = p.Curve;
  437. int combSize = FixedPointUtilities.GetCombSize(c);
  438. if (k.BitLength > combSize || l.BitLength > combSize)
  439. {
  440. /*
  441. * TODO The comb works best when the scalars are less than the (possibly unknown) order.
  442. * Still, if we want to handle larger scalars, we could allow customization of the comb
  443. * size, or alternatively we could deal with the 'extra' bits either by running the comb
  444. * multiple times as necessary, or by using an alternative multiplier as prelude.
  445. */
  446. throw new InvalidOperationException("fixed-point comb doesn't support scalars larger than the curve order");
  447. }
  448. FixedPointPreCompInfo infoP = FixedPointUtilities.Precompute(p);
  449. FixedPointPreCompInfo infoQ = FixedPointUtilities.Precompute(q);
  450. ECLookupTable lookupTableP = infoP.LookupTable;
  451. ECLookupTable lookupTableQ = infoQ.LookupTable;
  452. int widthP = infoP.Width;
  453. int widthQ = infoQ.Width;
  454. // TODO This shouldn't normally happen, but a better "solution" is desirable anyway
  455. if (widthP != widthQ)
  456. {
  457. FixedPointCombMultiplier m = new FixedPointCombMultiplier();
  458. ECPoint r1 = m.Multiply(p, k);
  459. ECPoint r2 = m.Multiply(q, l);
  460. return r1.Add(r2);
  461. }
  462. int width = widthP;
  463. int d = (combSize + width - 1) / width;
  464. ECPoint R = c.Infinity;
  465. int fullComb = d * width;
  466. uint[] K = Nat.FromBigInteger(fullComb, k);
  467. uint[] L = Nat.FromBigInteger(fullComb, l);
  468. int top = fullComb - 1;
  469. for (int i = 0; i < d; ++i)
  470. {
  471. uint secretIndexK = 0, secretIndexL = 0;
  472. for (int j = top - i; j >= 0; j -= d)
  473. {
  474. uint secretBitK = K[j >> 5] >> (j & 0x1F);
  475. secretIndexK ^= secretBitK >> 1;
  476. secretIndexK <<= 1;
  477. secretIndexK ^= secretBitK;
  478. uint secretBitL = L[j >> 5] >> (j & 0x1F);
  479. secretIndexL ^= secretBitL >> 1;
  480. secretIndexL <<= 1;
  481. secretIndexL ^= secretBitL;
  482. }
  483. ECPoint addP = lookupTableP.LookupVar((int)secretIndexK);
  484. ECPoint addQ = lookupTableQ.LookupVar((int)secretIndexL);
  485. ECPoint T = addP.Add(addQ);
  486. R = R.TwicePlus(T);
  487. }
  488. return R.Add(infoP.Offset).Add(infoQ.Offset);
  489. }
  490. }
  491. }
  492. #pragma warning restore
  493. #endif