ClpFixedPointUtilities.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. { *********************************************************************************** }
  2. { * CryptoLib Library * }
  3. { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
  4. { * Github Repository <https://github.com/Xor-el> * }
  5. { * Distributed under the MIT software license, see the accompanying file LICENSE * }
  6. { * or visit http://www.opensource.org/licenses/mit-license.php. * }
  7. { * Acknowledgements: * }
  8. { * * }
  9. { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
  10. { * development of this library * }
  11. { * ******************************************************************************* * }
  12. (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
  13. unit ClpFixedPointUtilities;
  14. {$I ..\..\..\Include\CryptoLib.inc}
  15. interface
  16. uses
  17. SysUtils,
  18. ClpBigInteger,
  19. ClpCryptoLibTypes,
  20. ClpIPreCompInfo,
  21. ClpFixedPointPreCompInfo,
  22. ClpIFixedPointPreCompInfo,
  23. ClpIECInterface;
  24. type
  25. TFixedPointUtilities = class sealed(TObject)
  26. strict private
  27. const
  28. PRECOMP_NAME: String = 'bc_fixed_point';
  29. public
  30. class function GetCombSize(const c: IECCurve): Int32; static; inline;
  31. class function GetFixedPointPreCompInfo(const preCompInfo: IPreCompInfo)
  32. : IFixedPointPreCompInfo; static; inline;
  33. class function Precompute(const p: IECPoint): IFixedPointPreCompInfo;
  34. overload; static;
  35. class function Precompute(const p: IECPoint; minWidth: Int32)
  36. : IFixedPointPreCompInfo; overload; static;
  37. deprecated
  38. 'Use "Precompute(ECPoint)" instead, as minWidth parameter is now ignored';
  39. end;
  40. implementation
  41. { TFixedPointUtilities }
  42. class function TFixedPointUtilities.GetCombSize(const c: IECCurve): Int32;
  43. var
  44. order: TBigInteger;
  45. begin
  46. order := c.order;
  47. if (not(order.IsInitialized)) then
  48. begin
  49. Result := c.FieldSize + 1;
  50. end
  51. else
  52. begin
  53. Result := order.BitLength;
  54. end;
  55. end;
  56. class function TFixedPointUtilities.GetFixedPointPreCompInfo(const preCompInfo
  57. : IPreCompInfo): IFixedPointPreCompInfo;
  58. begin
  59. if (Supports(preCompInfo, IFixedPointPreCompInfo, Result)) then
  60. begin
  61. Exit;
  62. end;
  63. Result := TFixedPointPreCompInfo.Create();
  64. end;
  65. class function TFixedPointUtilities.Precompute(const p: IECPoint)
  66. : IFixedPointPreCompInfo;
  67. var
  68. c: IECCurve;
  69. n, bit, bits, d, i, step, minWidth: Int32;
  70. info: IFixedPointPreCompInfo;
  71. pow2: IECPoint;
  72. lookupTable, pow2Table: TCryptoLibGenericArray<IECPoint>;
  73. begin
  74. c := p.Curve;
  75. if GetCombSize(c) > 257 then
  76. begin
  77. minWidth := 6
  78. end
  79. else
  80. begin
  81. minWidth := 5
  82. end;
  83. n := 1 shl minWidth;
  84. info := GetFixedPointPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME));
  85. lookupTable := info.PreComp;
  86. if ((lookupTable = Nil) or (System.Length(lookupTable) < n)) then
  87. begin
  88. bits := GetCombSize(c);
  89. d := (bits + minWidth - 1) div minWidth;
  90. System.SetLength(pow2Table, minWidth + 1);
  91. pow2Table[0] := p;
  92. for i := 1 to System.Pred(minWidth) do
  93. begin
  94. pow2Table[i] := pow2Table[i - 1].TimesPow2(d);
  95. end;
  96. // This will be the 'offset' value
  97. pow2Table[minWidth] := pow2Table[0].Subtract(pow2Table[1]);
  98. c.NormalizeAll(pow2Table);
  99. System.SetLength(lookupTable, n);
  100. lookupTable[0] := pow2Table[0];
  101. bit := minWidth - 1;
  102. while bit >= 0 do
  103. begin
  104. pow2 := pow2Table[bit];
  105. step := 1 shl bit;
  106. i := step;
  107. while i < n do
  108. begin
  109. lookupTable[i] := lookupTable[i - step].Add(pow2);
  110. System.Inc(i, step shl 1);
  111. end;
  112. System.Dec(bit);
  113. end;
  114. c.NormalizeAll(lookupTable);
  115. info.lookupTable := c.CreateCacheSafeLookupTable(lookupTable, 0,
  116. System.Length(lookupTable));
  117. info.Offset := pow2Table[minWidth];
  118. info.PreComp := lookupTable;
  119. info.Width := minWidth;
  120. c.SetPreCompInfo(p, PRECOMP_NAME, info);
  121. end;
  122. Result := info;
  123. end;
  124. class function TFixedPointUtilities.Precompute(const p: IECPoint;
  125. minWidth: Int32): IFixedPointPreCompInfo;
  126. begin
  127. Result := Precompute(p);
  128. end;
  129. end.