Tnaf.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Abc
  5. {
  6. /**
  7. * Class holding methods for point multiplication based on the window
  8. * τ-adic nonadjacent form (WTNAF). The algorithms are based on the
  9. * paper "Improved Algorithms for Arithmetic on Anomalous Binary Curves"
  10. * by Jerome A. Solinas. The paper first appeared in the Proceedings of
  11. * Crypto 1997.
  12. */
  13. internal class Tnaf
  14. {
  15. private static readonly BigInteger MinusOne = BigInteger.One.Negate();
  16. private static readonly BigInteger MinusTwo = BigInteger.Two.Negate();
  17. private static readonly BigInteger MinusThree = BigInteger.Three.Negate();
  18. private static readonly BigInteger Four = BigInteger.ValueOf(4);
  19. /**
  20. * The window width of WTNAF. The standard value of 4 is slightly less
  21. * than optimal for running time, but keeps space requirements for
  22. * precomputation low. For typical curves, a value of 5 or 6 results in
  23. * a better running time. When changing this value, the
  24. * <code>&#945;<sub>u</sub></code>'s must be computed differently, see
  25. * e.g. "Guide to Elliptic Curve Cryptography", Darrel Hankerson,
  26. * Alfred Menezes, Scott Vanstone, Springer-Verlag New York Inc., 2004,
  27. * p. 121-122
  28. */
  29. public const sbyte Width = 4;
  30. /**
  31. * 2<sup>4</sup>
  32. */
  33. public const sbyte Pow2Width = 16;
  34. /**
  35. * The <code>&#945;<sub>u</sub></code>'s for <code>a=0</code> as an array
  36. * of <code>ZTauElement</code>s.
  37. */
  38. public static readonly ZTauElement[] Alpha0 =
  39. {
  40. null,
  41. new ZTauElement(BigInteger.One, BigInteger.Zero), null,
  42. new ZTauElement(MinusThree, MinusOne), null,
  43. new ZTauElement(MinusOne, MinusOne), null,
  44. new ZTauElement(BigInteger.One, MinusOne), null
  45. };
  46. /**
  47. * The <code>&#945;<sub>u</sub></code>'s for <code>a=0</code> as an array
  48. * of TNAFs.
  49. */
  50. public static readonly sbyte[][] Alpha0Tnaf =
  51. {
  52. null, new sbyte[]{1}, null, new sbyte[]{-1, 0, 1}, null, new sbyte[]{1, 0, 1}, null, new sbyte[]{-1, 0, 0, 1}
  53. };
  54. /**
  55. * The <code>&#945;<sub>u</sub></code>'s for <code>a=1</code> as an array
  56. * of <code>ZTauElement</code>s.
  57. */
  58. public static readonly ZTauElement[] Alpha1 =
  59. {
  60. null,
  61. new ZTauElement(BigInteger.One, BigInteger.Zero), null,
  62. new ZTauElement(MinusThree, BigInteger.One), null,
  63. new ZTauElement(MinusOne, BigInteger.One), null,
  64. new ZTauElement(BigInteger.One, BigInteger.One), null
  65. };
  66. /**
  67. * The <code>&#945;<sub>u</sub></code>'s for <code>a=1</code> as an array
  68. * of TNAFs.
  69. */
  70. public static readonly sbyte[][] Alpha1Tnaf =
  71. {
  72. null, new sbyte[]{1}, null, new sbyte[]{-1, 0, 1}, null, new sbyte[]{1, 0, 1}, null, new sbyte[]{-1, 0, 0, -1}
  73. };
  74. /**
  75. * Computes the norm of an element <code>&#955;</code> of
  76. * <code><b>Z</b>[&#964;]</code>.
  77. * @param mu The parameter <code>&#956;</code> of the elliptic curve.
  78. * @param lambda The element <code>&#955;</code> of
  79. * <code><b>Z</b>[&#964;]</code>.
  80. * @return The norm of <code>&#955;</code>.
  81. */
  82. public static BigInteger Norm(sbyte mu, ZTauElement lambda)
  83. {
  84. BigInteger norm;
  85. // s1 = u^2
  86. BigInteger s1 = lambda.u.Multiply(lambda.u);
  87. // s2 = u * v
  88. BigInteger s2 = lambda.u.Multiply(lambda.v);
  89. // s3 = 2 * v^2
  90. BigInteger s3 = lambda.v.Multiply(lambda.v).ShiftLeft(1);
  91. if (mu == 1)
  92. {
  93. norm = s1.Add(s2).Add(s3);
  94. }
  95. else if (mu == -1)
  96. {
  97. norm = s1.Subtract(s2).Add(s3);
  98. }
  99. else
  100. {
  101. throw new ArgumentException("mu must be 1 or -1");
  102. }
  103. return norm;
  104. }
  105. /**
  106. * Computes the norm of an element <code>&#955;</code> of
  107. * <code><b>R</b>[&#964;]</code>, where <code>&#955; = u + v&#964;</code>
  108. * and <code>u</code> and <code>u</code> are real numbers (elements of
  109. * <code><b>R</b></code>).
  110. * @param mu The parameter <code>&#956;</code> of the elliptic curve.
  111. * @param u The real part of the element <code>&#955;</code> of
  112. * <code><b>R</b>[&#964;]</code>.
  113. * @param v The <code>&#964;</code>-adic part of the element
  114. * <code>&#955;</code> of <code><b>R</b>[&#964;]</code>.
  115. * @return The norm of <code>&#955;</code>.
  116. */
  117. public static SimpleBigDecimal Norm(sbyte mu, SimpleBigDecimal u, SimpleBigDecimal v)
  118. {
  119. SimpleBigDecimal norm;
  120. // s1 = u^2
  121. SimpleBigDecimal s1 = u.Multiply(u);
  122. // s2 = u * v
  123. SimpleBigDecimal s2 = u.Multiply(v);
  124. // s3 = 2 * v^2
  125. SimpleBigDecimal s3 = v.Multiply(v).ShiftLeft(1);
  126. if (mu == 1)
  127. {
  128. norm = s1.Add(s2).Add(s3);
  129. }
  130. else if (mu == -1)
  131. {
  132. norm = s1.Subtract(s2).Add(s3);
  133. }
  134. else
  135. {
  136. throw new ArgumentException("mu must be 1 or -1");
  137. }
  138. return norm;
  139. }
  140. /**
  141. * Rounds an element <code>&#955;</code> of <code><b>R</b>[&#964;]</code>
  142. * to an element of <code><b>Z</b>[&#964;]</code>, such that their difference
  143. * has minimal norm. <code>&#955;</code> is given as
  144. * <code>&#955; = &#955;<sub>0</sub> + &#955;<sub>1</sub>&#964;</code>.
  145. * @param lambda0 The component <code>&#955;<sub>0</sub></code>.
  146. * @param lambda1 The component <code>&#955;<sub>1</sub></code>.
  147. * @param mu The parameter <code>&#956;</code> of the elliptic curve. Must
  148. * equal 1 or -1.
  149. * @return The rounded element of <code><b>Z</b>[&#964;]</code>.
  150. * @throws ArgumentException if <code>lambda0</code> and
  151. * <code>lambda1</code> do not have same scale.
  152. */
  153. public static ZTauElement Round(SimpleBigDecimal lambda0,
  154. SimpleBigDecimal lambda1, sbyte mu)
  155. {
  156. int scale = lambda0.Scale;
  157. if (lambda1.Scale != scale)
  158. throw new ArgumentException("lambda0 and lambda1 do not have same scale");
  159. if (!((mu == 1) || (mu == -1)))
  160. throw new ArgumentException("mu must be 1 or -1");
  161. BigInteger f0 = lambda0.Round();
  162. BigInteger f1 = lambda1.Round();
  163. SimpleBigDecimal eta0 = lambda0.Subtract(f0);
  164. SimpleBigDecimal eta1 = lambda1.Subtract(f1);
  165. // eta = 2*eta0 + mu*eta1
  166. SimpleBigDecimal eta = eta0.Add(eta0);
  167. if (mu == 1)
  168. {
  169. eta = eta.Add(eta1);
  170. }
  171. else
  172. {
  173. // mu == -1
  174. eta = eta.Subtract(eta1);
  175. }
  176. // check1 = eta0 - 3*mu*eta1
  177. // check2 = eta0 + 4*mu*eta1
  178. SimpleBigDecimal threeEta1 = eta1.Add(eta1).Add(eta1);
  179. SimpleBigDecimal fourEta1 = threeEta1.Add(eta1);
  180. SimpleBigDecimal check1;
  181. SimpleBigDecimal check2;
  182. if (mu == 1)
  183. {
  184. check1 = eta0.Subtract(threeEta1);
  185. check2 = eta0.Add(fourEta1);
  186. }
  187. else
  188. {
  189. // mu == -1
  190. check1 = eta0.Add(threeEta1);
  191. check2 = eta0.Subtract(fourEta1);
  192. }
  193. sbyte h0 = 0;
  194. sbyte h1 = 0;
  195. // if eta >= 1
  196. if (eta.CompareTo(BigInteger.One) >= 0)
  197. {
  198. if (check1.CompareTo(MinusOne) < 0)
  199. {
  200. h1 = mu;
  201. }
  202. else
  203. {
  204. h0 = 1;
  205. }
  206. }
  207. else
  208. {
  209. // eta < 1
  210. if (check2.CompareTo(BigInteger.Two) >= 0)
  211. {
  212. h1 = mu;
  213. }
  214. }
  215. // if eta < -1
  216. if (eta.CompareTo(MinusOne) < 0)
  217. {
  218. if (check1.CompareTo(BigInteger.One) >= 0)
  219. {
  220. h1 = (sbyte)-mu;
  221. }
  222. else
  223. {
  224. h0 = -1;
  225. }
  226. }
  227. else
  228. {
  229. // eta >= -1
  230. if (check2.CompareTo(MinusTwo) < 0)
  231. {
  232. h1 = (sbyte)-mu;
  233. }
  234. }
  235. BigInteger q0 = f0.Add(BigInteger.ValueOf(h0));
  236. BigInteger q1 = f1.Add(BigInteger.ValueOf(h1));
  237. return new ZTauElement(q0, q1);
  238. }
  239. /**
  240. * Approximate division by <code>n</code>. For an integer
  241. * <code>k</code>, the value <code>&#955; = s k / n</code> is
  242. * computed to <code>c</code> bits of accuracy.
  243. * @param k The parameter <code>k</code>.
  244. * @param s The curve parameter <code>s<sub>0</sub></code> or
  245. * <code>s<sub>1</sub></code>.
  246. * @param vm The Lucas Sequence element <code>V<sub>m</sub></code>.
  247. * @param a The parameter <code>a</code> of the elliptic curve.
  248. * @param m The bit length of the finite field
  249. * <code><b>F</b><sub>m</sub></code>.
  250. * @param c The number of bits of accuracy, i.e. the scale of the returned
  251. * <code>SimpleBigDecimal</code>.
  252. * @return The value <code>&#955; = s k / n</code> computed to
  253. * <code>c</code> bits of accuracy.
  254. */
  255. public static SimpleBigDecimal ApproximateDivisionByN(BigInteger k,
  256. BigInteger s, BigInteger vm, sbyte a, int m, int c)
  257. {
  258. int _k = (m + 5)/2 + c;
  259. BigInteger ns = k.ShiftRight(m - _k - 2 + a);
  260. BigInteger gs = s.Multiply(ns);
  261. BigInteger hs = gs.ShiftRight(m);
  262. BigInteger js = vm.Multiply(hs);
  263. BigInteger gsPlusJs = gs.Add(js);
  264. BigInteger ls = gsPlusJs.ShiftRight(_k-c);
  265. if (gsPlusJs.TestBit(_k-c-1))
  266. {
  267. // round up
  268. ls = ls.Add(BigInteger.One);
  269. }
  270. return new SimpleBigDecimal(ls, c);
  271. }
  272. /**
  273. * Computes the <code>&#964;</code>-adic NAF (non-adjacent form) of an
  274. * element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>.
  275. * @param mu The parameter <code>&#956;</code> of the elliptic curve.
  276. * @param lambda The element <code>&#955;</code> of
  277. * <code><b>Z</b>[&#964;]</code>.
  278. * @return The <code>&#964;</code>-adic NAF of <code>&#955;</code>.
  279. */
  280. public static sbyte[] TauAdicNaf(sbyte mu, ZTauElement lambda)
  281. {
  282. if (!((mu == 1) || (mu == -1)))
  283. throw new ArgumentException("mu must be 1 or -1");
  284. BigInteger norm = Norm(mu, lambda);
  285. // Ceiling of log2 of the norm
  286. int log2Norm = norm.BitLength;
  287. // If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52
  288. int maxLength = log2Norm > 30 ? log2Norm + 4 : 34;
  289. // The array holding the TNAF
  290. sbyte[] u = new sbyte[maxLength];
  291. int i = 0;
  292. // The actual length of the TNAF
  293. int length = 0;
  294. BigInteger r0 = lambda.u;
  295. BigInteger r1 = lambda.v;
  296. while(!((r0.Equals(BigInteger.Zero)) && (r1.Equals(BigInteger.Zero))))
  297. {
  298. // If r0 is odd
  299. if (r0.TestBit(0))
  300. {
  301. u[i] = (sbyte) BigInteger.Two.Subtract((r0.Subtract(r1.ShiftLeft(1))).Mod(Four)).IntValue;
  302. // r0 = r0 - u[i]
  303. if (u[i] == 1)
  304. {
  305. r0 = r0.ClearBit(0);
  306. }
  307. else
  308. {
  309. // u[i] == -1
  310. r0 = r0.Add(BigInteger.One);
  311. }
  312. length = i;
  313. }
  314. else
  315. {
  316. u[i] = 0;
  317. }
  318. BigInteger t = r0;
  319. BigInteger s = r0.ShiftRight(1);
  320. if (mu == 1)
  321. {
  322. r0 = r1.Add(s);
  323. }
  324. else
  325. {
  326. // mu == -1
  327. r0 = r1.Subtract(s);
  328. }
  329. r1 = t.ShiftRight(1).Negate();
  330. i++;
  331. }
  332. length++;
  333. // Reduce the TNAF array to its actual length
  334. sbyte[] tnaf = new sbyte[length];
  335. Array.Copy(u, 0, tnaf, 0, length);
  336. return tnaf;
  337. }
  338. /**
  339. * Applies the operation <code>&#964;()</code> to an
  340. * <code>AbstractF2mPoint</code>.
  341. * @param p The AbstractF2mPoint to which <code>&#964;()</code> is applied.
  342. * @return <code>&#964;(p)</code>
  343. */
  344. public static AbstractF2mPoint Tau(AbstractF2mPoint p)
  345. {
  346. return p.Tau();
  347. }
  348. /**
  349. * Returns the parameter <code>&#956;</code> of the elliptic curve.
  350. * @param curve The elliptic curve from which to obtain <code>&#956;</code>.
  351. * The curve must be a Koblitz curve, i.e. <code>a</code> Equals
  352. * <code>0</code> or <code>1</code> and <code>b</code> Equals
  353. * <code>1</code>.
  354. * @return <code>&#956;</code> of the elliptic curve.
  355. * @throws ArgumentException if the given ECCurve is not a Koblitz
  356. * curve.
  357. */
  358. public static sbyte GetMu(AbstractF2mCurve curve)
  359. {
  360. BigInteger a = curve.A.ToBigInteger();
  361. sbyte mu;
  362. if (a.SignValue == 0)
  363. {
  364. mu = -1;
  365. }
  366. else if (a.Equals(BigInteger.One))
  367. {
  368. mu = 1;
  369. }
  370. else
  371. {
  372. throw new ArgumentException("No Koblitz curve (ABC), TNAF multiplication not possible");
  373. }
  374. return mu;
  375. }
  376. public static sbyte GetMu(ECFieldElement curveA)
  377. {
  378. return (sbyte)(curveA.IsZero ? -1 : 1);
  379. }
  380. public static sbyte GetMu(int curveA)
  381. {
  382. return (sbyte)(curveA == 0 ? -1 : 1);
  383. }
  384. /**
  385. * Calculates the Lucas Sequence elements <code>U<sub>k-1</sub></code> and
  386. * <code>U<sub>k</sub></code> or <code>V<sub>k-1</sub></code> and
  387. * <code>V<sub>k</sub></code>.
  388. * @param mu The parameter <code>&#956;</code> of the elliptic curve.
  389. * @param k The index of the second element of the Lucas Sequence to be
  390. * returned.
  391. * @param doV If set to true, computes <code>V<sub>k-1</sub></code> and
  392. * <code>V<sub>k</sub></code>, otherwise <code>U<sub>k-1</sub></code> and
  393. * <code>U<sub>k</sub></code>.
  394. * @return An array with 2 elements, containing <code>U<sub>k-1</sub></code>
  395. * and <code>U<sub>k</sub></code> or <code>V<sub>k-1</sub></code>
  396. * and <code>V<sub>k</sub></code>.
  397. */
  398. public static BigInteger[] GetLucas(sbyte mu, int k, bool doV)
  399. {
  400. if (!(mu == 1 || mu == -1))
  401. throw new ArgumentException("mu must be 1 or -1");
  402. BigInteger u0;
  403. BigInteger u1;
  404. BigInteger u2;
  405. if (doV)
  406. {
  407. u0 = BigInteger.Two;
  408. u1 = BigInteger.ValueOf(mu);
  409. }
  410. else
  411. {
  412. u0 = BigInteger.Zero;
  413. u1 = BigInteger.One;
  414. }
  415. for (int i = 1; i < k; i++)
  416. {
  417. // u2 = mu*u1 - 2*u0;
  418. BigInteger s = null;
  419. if (mu == 1)
  420. {
  421. s = u1;
  422. }
  423. else
  424. {
  425. // mu == -1
  426. s = u1.Negate();
  427. }
  428. u2 = s.Subtract(u0.ShiftLeft(1));
  429. u0 = u1;
  430. u1 = u2;
  431. // System.out.println(i + ": " + u2);
  432. // System.out.println();
  433. }
  434. BigInteger[] retVal = {u0, u1};
  435. return retVal;
  436. }
  437. /**
  438. * Computes the auxiliary value <code>t<sub>w</sub></code>. If the width is
  439. * 4, then for <code>mu = 1</code>, <code>t<sub>w</sub> = 6</code> and for
  440. * <code>mu = -1</code>, <code>t<sub>w</sub> = 10</code>
  441. * @param mu The parameter <code>&#956;</code> of the elliptic curve.
  442. * @param w The window width of the WTNAF.
  443. * @return the auxiliary value <code>t<sub>w</sub></code>
  444. */
  445. public static BigInteger GetTw(sbyte mu, int w)
  446. {
  447. if (w == 4)
  448. {
  449. if (mu == 1)
  450. {
  451. return BigInteger.ValueOf(6);
  452. }
  453. else
  454. {
  455. // mu == -1
  456. return BigInteger.ValueOf(10);
  457. }
  458. }
  459. else
  460. {
  461. // For w <> 4, the values must be computed
  462. BigInteger[] us = GetLucas(mu, w, false);
  463. BigInteger twoToW = BigInteger.Zero.SetBit(w);
  464. BigInteger u1invert = us[1].ModInverse(twoToW);
  465. BigInteger tw;
  466. tw = BigInteger.Two.Multiply(us[0]).Multiply(u1invert).Mod(twoToW);
  467. //System.out.println("mu = " + mu);
  468. //System.out.println("tw = " + tw);
  469. return tw;
  470. }
  471. }
  472. /**
  473. * Computes the auxiliary values <code>s<sub>0</sub></code> and
  474. * <code>s<sub>1</sub></code> used for partial modular reduction.
  475. * @param curve The elliptic curve for which to compute
  476. * <code>s<sub>0</sub></code> and <code>s<sub>1</sub></code>.
  477. * @throws ArgumentException if <code>curve</code> is not a
  478. * Koblitz curve (Anomalous Binary Curve, ABC).
  479. */
  480. public static BigInteger[] GetSi(AbstractF2mCurve curve)
  481. {
  482. if (!curve.IsKoblitz)
  483. throw new ArgumentException("si is defined for Koblitz curves only");
  484. int m = curve.FieldSize;
  485. int a = curve.A.ToBigInteger().IntValue;
  486. sbyte mu = GetMu(a);
  487. int shifts = GetShiftsForCofactor(curve.Cofactor);
  488. int index = m + 3 - a;
  489. BigInteger[] ui = GetLucas(mu, index, false);
  490. if (mu == 1)
  491. {
  492. ui[0] = ui[0].Negate();
  493. ui[1] = ui[1].Negate();
  494. }
  495. BigInteger dividend0 = BigInteger.One.Add(ui[1]).ShiftRight(shifts);
  496. BigInteger dividend1 = BigInteger.One.Add(ui[0]).ShiftRight(shifts).Negate();
  497. return new BigInteger[] { dividend0, dividend1 };
  498. }
  499. public static BigInteger[] GetSi(int fieldSize, int curveA, BigInteger cofactor)
  500. {
  501. sbyte mu = GetMu(curveA);
  502. int shifts = GetShiftsForCofactor(cofactor);
  503. int index = fieldSize + 3 - curveA;
  504. BigInteger[] ui = GetLucas(mu, index, false);
  505. if (mu == 1)
  506. {
  507. ui[0] = ui[0].Negate();
  508. ui[1] = ui[1].Negate();
  509. }
  510. BigInteger dividend0 = BigInteger.One.Add(ui[1]).ShiftRight(shifts);
  511. BigInteger dividend1 = BigInteger.One.Add(ui[0]).ShiftRight(shifts).Negate();
  512. return new BigInteger[] { dividend0, dividend1 };
  513. }
  514. protected static int GetShiftsForCofactor(BigInteger h)
  515. {
  516. if (h != null && h.BitLength < 4)
  517. {
  518. int hi = h.IntValue;
  519. if (hi == 2)
  520. return 1;
  521. if (hi == 4)
  522. return 2;
  523. }
  524. throw new ArgumentException("h (Cofactor) must be 2 or 4");
  525. }
  526. /**
  527. * Partial modular reduction modulo
  528. * <code>(&#964;<sup>m</sup> - 1)/(&#964; - 1)</code>.
  529. * @param k The integer to be reduced.
  530. * @param m The bitlength of the underlying finite field.
  531. * @param a The parameter <code>a</code> of the elliptic curve.
  532. * @param s The auxiliary values <code>s<sub>0</sub></code> and
  533. * <code>s<sub>1</sub></code>.
  534. * @param mu The parameter &#956; of the elliptic curve.
  535. * @param c The precision (number of bits of accuracy) of the partial
  536. * modular reduction.
  537. * @return <code>&#961; := k partmod (&#964;<sup>m</sup> - 1)/(&#964; - 1)</code>
  538. */
  539. public static ZTauElement PartModReduction(BigInteger k, int m, sbyte a,
  540. BigInteger[] s, sbyte mu, sbyte c)
  541. {
  542. // d0 = s[0] + mu*s[1]; mu is either 1 or -1
  543. BigInteger d0;
  544. if (mu == 1)
  545. {
  546. d0 = s[0].Add(s[1]);
  547. }
  548. else
  549. {
  550. d0 = s[0].Subtract(s[1]);
  551. }
  552. BigInteger[] v = GetLucas(mu, m, true);
  553. BigInteger vm = v[1];
  554. SimpleBigDecimal lambda0 = ApproximateDivisionByN(
  555. k, s[0], vm, a, m, c);
  556. SimpleBigDecimal lambda1 = ApproximateDivisionByN(
  557. k, s[1], vm, a, m, c);
  558. ZTauElement q = Round(lambda0, lambda1, mu);
  559. // r0 = n - d0*q0 - 2*s1*q1
  560. BigInteger r0 = k.Subtract(d0.Multiply(q.u)).Subtract(
  561. BigInteger.ValueOf(2).Multiply(s[1]).Multiply(q.v));
  562. // r1 = s1*q0 - s0*q1
  563. BigInteger r1 = s[1].Multiply(q.u).Subtract(s[0].Multiply(q.v));
  564. return new ZTauElement(r0, r1);
  565. }
  566. /**
  567. * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
  568. * by a <code>BigInteger</code> using the reduced <code>&#964;</code>-adic
  569. * NAF (RTNAF) method.
  570. * @param p The AbstractF2mPoint to Multiply.
  571. * @param k The <code>BigInteger</code> by which to Multiply <code>p</code>.
  572. * @return <code>k * p</code>
  573. */
  574. public static AbstractF2mPoint MultiplyRTnaf(AbstractF2mPoint p, BigInteger k)
  575. {
  576. AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
  577. int m = curve.FieldSize;
  578. int a = curve.A.ToBigInteger().IntValue;
  579. sbyte mu = GetMu(a);
  580. BigInteger[] s = curve.GetSi();
  581. ZTauElement rho = PartModReduction(k, m, (sbyte)a, s, mu, (sbyte)10);
  582. return MultiplyTnaf(p, rho);
  583. }
  584. /**
  585. * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
  586. * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
  587. * using the <code>&#964;</code>-adic NAF (TNAF) method.
  588. * @param p The AbstractF2mPoint to Multiply.
  589. * @param lambda The element <code>&#955;</code> of
  590. * <code><b>Z</b>[&#964;]</code>.
  591. * @return <code>&#955; * p</code>
  592. */
  593. public static AbstractF2mPoint MultiplyTnaf(AbstractF2mPoint p, ZTauElement lambda)
  594. {
  595. AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
  596. sbyte mu = GetMu(curve.A);
  597. sbyte[] u = TauAdicNaf(mu, lambda);
  598. AbstractF2mPoint q = MultiplyFromTnaf(p, u);
  599. return q;
  600. }
  601. /**
  602. * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
  603. * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
  604. * using the <code>&#964;</code>-adic NAF (TNAF) method, given the TNAF
  605. * of <code>&#955;</code>.
  606. * @param p The AbstractF2mPoint to Multiply.
  607. * @param u The the TNAF of <code>&#955;</code>..
  608. * @return <code>&#955; * p</code>
  609. */
  610. public static AbstractF2mPoint MultiplyFromTnaf(AbstractF2mPoint p, sbyte[] u)
  611. {
  612. ECCurve curve = p.Curve;
  613. AbstractF2mPoint q = (AbstractF2mPoint)curve.Infinity;
  614. AbstractF2mPoint pNeg = (AbstractF2mPoint)p.Negate();
  615. int tauCount = 0;
  616. for (int i = u.Length - 1; i >= 0; i--)
  617. {
  618. ++tauCount;
  619. sbyte ui = u[i];
  620. if (ui != 0)
  621. {
  622. q = q.TauPow(tauCount);
  623. tauCount = 0;
  624. ECPoint x = ui > 0 ? p : pNeg;
  625. q = (AbstractF2mPoint)q.Add(x);
  626. }
  627. }
  628. if (tauCount > 0)
  629. {
  630. q = q.TauPow(tauCount);
  631. }
  632. return q;
  633. }
  634. /**
  635. * Computes the <code>[&#964;]</code>-adic window NAF of an element
  636. * <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>.
  637. * @param mu The parameter &#956; of the elliptic curve.
  638. * @param lambda The element <code>&#955;</code> of
  639. * <code><b>Z</b>[&#964;]</code> of which to compute the
  640. * <code>[&#964;]</code>-adic NAF.
  641. * @param width The window width of the resulting WNAF.
  642. * @param pow2w 2<sup>width</sup>.
  643. * @param tw The auxiliary value <code>t<sub>w</sub></code>.
  644. * @param alpha The <code>&#945;<sub>u</sub></code>'s for the window width.
  645. * @return The <code>[&#964;]</code>-adic window NAF of
  646. * <code>&#955;</code>.
  647. */
  648. public static sbyte[] TauAdicWNaf(sbyte mu, ZTauElement lambda,
  649. sbyte width, BigInteger pow2w, BigInteger tw, ZTauElement[] alpha)
  650. {
  651. if (!((mu == 1) || (mu == -1)))
  652. throw new ArgumentException("mu must be 1 or -1");
  653. BigInteger norm = Norm(mu, lambda);
  654. // Ceiling of log2 of the norm
  655. int log2Norm = norm.BitLength;
  656. // If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52
  657. int maxLength = log2Norm > 30 ? log2Norm + 4 + width : 34 + width;
  658. // The array holding the TNAF
  659. sbyte[] u = new sbyte[maxLength];
  660. // 2^(width - 1)
  661. BigInteger pow2wMin1 = pow2w.ShiftRight(1);
  662. // Split lambda into two BigIntegers to simplify calculations
  663. BigInteger r0 = lambda.u;
  664. BigInteger r1 = lambda.v;
  665. int i = 0;
  666. // while lambda <> (0, 0)
  667. while (!((r0.Equals(BigInteger.Zero))&&(r1.Equals(BigInteger.Zero))))
  668. {
  669. // if r0 is odd
  670. if (r0.TestBit(0))
  671. {
  672. // uUnMod = r0 + r1*tw Mod 2^width
  673. BigInteger uUnMod
  674. = r0.Add(r1.Multiply(tw)).Mod(pow2w);
  675. sbyte uLocal;
  676. // if uUnMod >= 2^(width - 1)
  677. if (uUnMod.CompareTo(pow2wMin1) >= 0)
  678. {
  679. uLocal = (sbyte) uUnMod.Subtract(pow2w).IntValue;
  680. }
  681. else
  682. {
  683. uLocal = (sbyte) uUnMod.IntValue;
  684. }
  685. // uLocal is now in [-2^(width-1), 2^(width-1)-1]
  686. u[i] = uLocal;
  687. bool s = true;
  688. if (uLocal < 0)
  689. {
  690. s = false;
  691. uLocal = (sbyte)-uLocal;
  692. }
  693. // uLocal is now >= 0
  694. if (s)
  695. {
  696. r0 = r0.Subtract(alpha[uLocal].u);
  697. r1 = r1.Subtract(alpha[uLocal].v);
  698. }
  699. else
  700. {
  701. r0 = r0.Add(alpha[uLocal].u);
  702. r1 = r1.Add(alpha[uLocal].v);
  703. }
  704. }
  705. else
  706. {
  707. u[i] = 0;
  708. }
  709. BigInteger t = r0;
  710. if (mu == 1)
  711. {
  712. r0 = r1.Add(r0.ShiftRight(1));
  713. }
  714. else
  715. {
  716. // mu == -1
  717. r0 = r1.Subtract(r0.ShiftRight(1));
  718. }
  719. r1 = t.ShiftRight(1).Negate();
  720. i++;
  721. }
  722. return u;
  723. }
  724. /**
  725. * Does the precomputation for WTNAF multiplication.
  726. * @param p The <code>ECPoint</code> for which to do the precomputation.
  727. * @param a The parameter <code>a</code> of the elliptic curve.
  728. * @return The precomputation array for <code>p</code>.
  729. */
  730. public static AbstractF2mPoint[] GetPreComp(AbstractF2mPoint p, sbyte a)
  731. {
  732. sbyte[][] alphaTnaf = (a == 0) ? Tnaf.Alpha0Tnaf : Tnaf.Alpha1Tnaf;
  733. AbstractF2mPoint[] pu = new AbstractF2mPoint[(uint)(alphaTnaf.Length + 1) >> 1];
  734. pu[0] = p;
  735. uint precompLen = (uint)alphaTnaf.Length;
  736. for (uint i = 3; i < precompLen; i += 2)
  737. {
  738. pu[i >> 1] = Tnaf.MultiplyFromTnaf(p, alphaTnaf[i]);
  739. }
  740. p.Curve.NormalizeAll(pu);
  741. return pu;
  742. }
  743. }
  744. }
  745. #pragma warning restore
  746. #endif