FixedPointUtilities.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. namespace Org.BouncyCastle.Math.EC.Multiplier
  4. {
  5. public class FixedPointUtilities
  6. {
  7. public static readonly string PRECOMP_NAME = "bc_fixed_point";
  8. public static int GetCombSize(ECCurve c)
  9. {
  10. BigInteger order = c.Order;
  11. return order == null ? c.FieldSize + 1 : order.BitLength;
  12. }
  13. public static FixedPointPreCompInfo GetFixedPointPreCompInfo(PreCompInfo preCompInfo)
  14. {
  15. if ((preCompInfo != null) && (preCompInfo is FixedPointPreCompInfo))
  16. {
  17. return (FixedPointPreCompInfo)preCompInfo;
  18. }
  19. return new FixedPointPreCompInfo();
  20. }
  21. public static FixedPointPreCompInfo Precompute(ECPoint p, int minWidth)
  22. {
  23. ECCurve c = p.Curve;
  24. int n = 1 << minWidth;
  25. FixedPointPreCompInfo info = GetFixedPointPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME));
  26. ECPoint[] lookupTable = info.PreComp;
  27. if (lookupTable == null || lookupTable.Length < n)
  28. {
  29. int bits = GetCombSize(c);
  30. int d = (bits + minWidth - 1) / minWidth;
  31. ECPoint[] pow2Table = new ECPoint[minWidth];
  32. pow2Table[0] = p;
  33. for (int i = 1; i < minWidth; ++i)
  34. {
  35. pow2Table[i] = pow2Table[i - 1].TimesPow2(d);
  36. }
  37. c.NormalizeAll(pow2Table);
  38. lookupTable = new ECPoint[n];
  39. lookupTable[0] = c.Infinity;
  40. for (int bit = minWidth - 1; bit >= 0; --bit)
  41. {
  42. ECPoint pow2 = pow2Table[bit];
  43. int step = 1 << bit;
  44. for (int i = step; i < n; i += (step << 1))
  45. {
  46. lookupTable[i] = lookupTable[i - step].Add(pow2);
  47. }
  48. }
  49. c.NormalizeAll(lookupTable);
  50. info.PreComp = lookupTable;
  51. info.Width = minWidth;
  52. c.SetPreCompInfo(p, PRECOMP_NAME, info);
  53. }
  54. return info;
  55. }
  56. }
  57. }
  58. #endif