ClpWNafPreCompInfo.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 ClpWNafPreCompInfo;
  14. {$I ..\..\..\Include\CryptoLib.inc}
  15. interface
  16. uses
  17. ClpCryptoLibTypes,
  18. ClpIECC,
  19. ClpIWNafPreCompInfo,
  20. ClpIPreCompInfo;
  21. type
  22. /// <summary>
  23. /// Class holding precomputation data for the WNAF (Window Non-Adjacent
  24. /// Form) algorithm.
  25. /// </summary>
  26. TWNafPreCompInfo = class sealed(TInterfacedObject, IPreCompInfo,
  27. IWNafPreCompInfo)
  28. strict private
  29. var
  30. /// <summary>
  31. /// Array holding the precomputed <c>ECPoint</c>s used for a Window NAF
  32. /// multiplication.
  33. /// </summary>
  34. FPreComp: TCryptoLibGenericArray<IECPoint>;
  35. /// <summary>
  36. /// Array holding the negations of the precomputed <c>ECPoint</c>s used
  37. /// for a Window NAF multiplication.
  38. /// </summary>
  39. FPreCompNeg: TCryptoLibGenericArray<IECPoint>;
  40. /// <summary>
  41. /// Holds an <c>ECPoint</c> representing Twice(this). Used for the Window
  42. /// NAF multiplication to create or extend the precomputed values.
  43. /// </summary>
  44. FTwice: IECPoint;
  45. FConfWidth, FWidth: Int32;
  46. {$IFNDEF FPC}
  47. {$IFDEF HAS_VOLATILE}[volatile]
  48. {$ENDIF}
  49. {$ENDIF}
  50. FPromotionCountdown: Int32;
  51. function GetPreComp: TCryptoLibGenericArray<IECPoint>; inline;
  52. procedure SetPreComp(const Value: TCryptoLibGenericArray<IECPoint>); inline;
  53. function GetPreCompNeg: TCryptoLibGenericArray<IECPoint>; inline;
  54. procedure SetPreCompNeg(const Value
  55. : TCryptoLibGenericArray<IECPoint>); inline;
  56. function GetTwice: IECPoint; inline;
  57. procedure SetTwice(const Value: IECPoint); inline;
  58. function GetConfWidth: Int32; inline;
  59. procedure SetConfWidth(Value: Int32); inline;
  60. function GetWidth: Int32; inline;
  61. procedure SetWidth(Value: Int32); inline;
  62. function GetPromotionCountdown: Int32; inline;
  63. procedure SetPromotionCountdown(Value: Int32); inline;
  64. function DecrementPromotionCountdown: Int32; inline;
  65. function IsPromoted: Boolean; inline;
  66. public
  67. constructor Create();
  68. destructor Destroy; override;
  69. property PreComp: TCryptoLibGenericArray<IECPoint> read GetPreComp
  70. write SetPreComp;
  71. property PreCompNeg: TCryptoLibGenericArray<IECPoint> read GetPreCompNeg
  72. write SetPreCompNeg;
  73. property Twice: IECPoint read GetTwice write SetTwice;
  74. property ConfWidth: Int32 read GetConfWidth write SetConfWidth;
  75. property Width: Int32 read GetWidth write SetWidth;
  76. property PromotionCountdown: Int32 read GetPromotionCountdown
  77. write SetPromotionCountdown;
  78. end;
  79. implementation
  80. { TWNafPreCompInfo }
  81. constructor TWNafPreCompInfo.Create;
  82. begin
  83. inherited Create();
  84. FConfWidth := -1;
  85. FWidth := -1;
  86. FPromotionCountdown := 4;
  87. end;
  88. function TWNafPreCompInfo.DecrementPromotionCountdown: Int32;
  89. var
  90. t: Int32;
  91. begin
  92. t := PromotionCountdown;
  93. if (t > 0) then
  94. begin
  95. System.Dec(t);
  96. PromotionCountdown := t;
  97. end;
  98. result := t;
  99. end;
  100. destructor TWNafPreCompInfo.Destroy;
  101. var
  102. i: Integer;
  103. begin
  104. if Assigned(FPreComp) then
  105. begin
  106. for i := 0 to Length(FPreComp) - 1 do
  107. FPreComp[i] := nil;
  108. FPreComp := nil;
  109. end;
  110. if Assigned(FPreCompNeg) then
  111. begin
  112. for i := 0 to Length(FPreCompNeg) - 1 do
  113. FPreCompNeg[i] := nil;
  114. FPreCompNeg := nil;
  115. end;
  116. FTwice := nil;
  117. inherited;
  118. end;
  119. function TWNafPreCompInfo.GetConfWidth: Int32;
  120. begin
  121. result := FConfWidth;
  122. end;
  123. function TWNafPreCompInfo.GetPreComp: TCryptoLibGenericArray<IECPoint>;
  124. begin
  125. result := FPreComp;
  126. end;
  127. function TWNafPreCompInfo.GetPreCompNeg: TCryptoLibGenericArray<IECPoint>;
  128. begin
  129. result := FPreCompNeg;
  130. end;
  131. function TWNafPreCompInfo.GetPromotionCountdown: Int32;
  132. begin
  133. {$IFDEF FPC}
  134. result := {$IFDEF HAS_VOLATILE}volatile{$ENDIF}(FPromotionCountdown);
  135. {$ELSE}
  136. result := FPromotionCountdown;
  137. {$ENDIF}
  138. end;
  139. function TWNafPreCompInfo.GetTwice: IECPoint;
  140. begin
  141. result := FTwice;
  142. end;
  143. function TWNafPreCompInfo.GetWidth: Int32;
  144. begin
  145. result := FWidth;
  146. end;
  147. function TWNafPreCompInfo.IsPromoted: Boolean;
  148. begin
  149. result := PromotionCountdown <= 0;
  150. end;
  151. procedure TWNafPreCompInfo.SetConfWidth(Value: Int32);
  152. begin
  153. FConfWidth := Value;
  154. end;
  155. procedure TWNafPreCompInfo.SetPreComp(const Value
  156. : TCryptoLibGenericArray<IECPoint>);
  157. begin
  158. FPreComp := Value;
  159. end;
  160. procedure TWNafPreCompInfo.SetPreCompNeg(const Value
  161. : TCryptoLibGenericArray<IECPoint>);
  162. begin
  163. FPreCompNeg := Value;
  164. end;
  165. procedure TWNafPreCompInfo.SetPromotionCountdown(Value: Int32);
  166. begin
  167. {$IFDEF FPC}
  168. FPromotionCountdown := {$IFDEF HAS_VOLATILE}volatile{$ENDIF}(Value);
  169. {$ELSE}
  170. FPromotionCountdown := Value;
  171. {$ENDIF}
  172. end;
  173. procedure TWNafPreCompInfo.SetTwice(const Value: IECPoint);
  174. begin
  175. FTwice := Value;
  176. end;
  177. procedure TWNafPreCompInfo.SetWidth(Value: Int32);
  178. begin
  179. FWidth := Value;
  180. end;
  181. end.