@@ -44,6 +44,10 @@ interface
4444 ClpTnaf,
4545 ClpValidityPreCompInfo,
4646 ClpIValidityPreCompInfo,
47+ ClpIWNafPreCompInfo,
48+ ClpIEndoPreCompInfo,
49+ ClpIWTauNafPreCompInfo,
50+ ClpIFixedPointPreCompInfo,
4751 ClpIECC,
4852 ClpIFiniteField,
4953 ClpIPreCompInfo;
@@ -722,6 +726,8 @@ TSimpleLookupTable = class abstract(TAbstractECLookupTable,
722726 constructor Create(const points: TCryptoLibGenericArray<IECPoint>;
723727 off, len: Int32);
724728
729+ destructor Destroy; override;
730+
725731 function Lookup (index: Int32): IECPoint; override;
726732 function LookupVar (index: Int32): IECPoint; override;
727733
@@ -744,6 +750,8 @@ TDefaultLookupTable = class(TAbstractECLookupTable, IDefaultLookupTable)
744750 constructor Create(const outer: IECCurve; const table: TCryptoLibByteArray;
745751 Size: Int32);
746752
753+ destructor Destroy; override;
754+
747755 function Lookup (index: Int32): IECPoint; override;
748756 function LookupVar (index: Int32): IECPoint; override;
749757
@@ -781,6 +789,8 @@ TDefaultF2mLookupTable = class(TAbstractECLookupTable, IDefaultF2mLookupTable)
781789 constructor Create(const outer: IF2mCurve;
782790 const table: TCryptoLibInt64Array; Size: Int32);
783791
792+ destructor Destroy; override;
793+
784794 function Lookup (index: Int32): IECPoint; override;
785795 function LookupVar (index: Int32): IECPoint; override;
786796
@@ -1222,6 +1232,8 @@ TValidityCallback = class(TInterfacedObject, IPreCompCallback,
12221232
12231233 function ThreeTimes (): IECPoint; virtual ;
12241234
1235+ function Clone (): IECPoint; virtual ;
1236+
12251237 function Equals (const other: IECPoint): Boolean; reintroduce;
12261238 function GetHashCode (): { $IFDEF DELPHI} Int32; { $ELSE} PtrInt;
12271239{ $ENDIF DELPHI} override;
@@ -2993,6 +3005,21 @@ procedure TECCurve.SetMultiplier(const Value: IECMultiplier);
29933005
29943006function TECCurve.Precompute (const point: IECPoint; const name : String;
29953007 const callback: IPreCompCallback): IPreCompInfo;
3008+
3009+ function IsHeavy (const info: IPreCompInfo): Boolean;
3010+ var
3011+ wnaf: IWNafPreCompInfo;
3012+ wtnaf: IWTauNafPreCompInfo;
3013+ endo: IEndoPreCompInfo;
3014+ fixed: IFixedPointPreCompInfo;
3015+ begin
3016+ Result :=
3017+ Supports(info, IWNafPreCompInfo, wnaf) or
3018+ Supports(info, IWTauNafPreCompInfo, wtnaf) or
3019+ Supports(info, IEndoPreCompInfo, endo) or
3020+ Supports(info, IFixedPointPreCompInfo, fixed);
3021+ end ;
3022+
29963023var
29973024 table: TDictionary<String, IPreCompInfo>;
29983025 existing: IPreCompInfo;
@@ -3012,10 +3039,10 @@ function TECCurve.Precompute(const point: IECPoint; const name: String;
30123039
30133040 result := callback.Precompute(existing);
30143041
3015- if (result <> existing) then
3016- begin
3017- table.AddOrSetValue(name , result);
3018- end ;
3042+ if (result <> existing) and ((existing <> Nil ) or ( not IsHeavy(result)) ) then
3043+ begin
3044+ table.AddOrSetValue(name , result);
3045+ end ;
30193046
30203047 finally
30213048 FLock.Release;
@@ -3718,6 +3745,13 @@ function TDefaultLookupTable.CreatePoint(const x, y: TCryptoLibByteArray)
37183745 result := Fm_outer.CreateRawPoint(XFieldElement, YFieldElement, false);
37193746end ;
37203747
3748+ destructor TDefaultLookupTable.Destroy;
3749+ begin
3750+ Fm_outer := nil ;
3751+ Fm_table := nil ;
3752+ inherited ;
3753+ end ;
3754+
37213755function TDefaultLookupTable.GetSize : Int32;
37223756begin
37233757 result := Fm_size;
@@ -3804,6 +3838,14 @@ function TDefaultF2mLookupTable.CreatePoint(const x, y: TCryptoLibInt64Array)
38043838 result := Fm_outer.CreateRawPoint(XFieldElement, YFieldElement, false);
38053839end ;
38063840
3841+
3842+ destructor TDefaultF2mLookupTable.Destroy;
3843+ begin
3844+ Fm_outer := nil ;
3845+ Fm_table := nil ;
3846+ inherited ;
3847+ end ;
3848+
38073849function TDefaultF2mLookupTable.GetSize : Int32;
38083850begin
38093851 result := Fm_size;
@@ -4084,12 +4126,23 @@ function TECPoint.CreateScaledPoint(const sx, sy: IECFieldElement): IECPoint;
40844126end ;
40854127
40864128destructor TECPoint.Destroy;
4129+ var
4130+ Key: string;
40874131begin
4088- TSetWeakRef.SetWeakReference(@Fm_curve, Nil );
4089- Fm_preCompTable.Free;
4132+ TSetWeakRef.SetWeakReference(@Fm_curve, nil );
4133+
4134+ if Assigned(Fm_preCompTable) then
4135+ begin
4136+ for Key in Fm_preCompTable.Keys do
4137+ Fm_preCompTable[Key] := nil ;
4138+
4139+ Fm_preCompTable.Free;
4140+ end ;
4141+
40904142 inherited Destroy;
40914143end ;
40924144
4145+
40934146class constructor TECPoint.ECPoint;
40944147begin
40954148 System.SetLength(FEMPTY_ZS, 0 );
@@ -4400,6 +4453,18 @@ function TECPoint.GetDetachedPoint: IECPoint;
44004453 result := Normalize().Detach();
44014454end ;
44024455
4456+ function TECPoint.Clone : IECPoint;
4457+ var
4458+ baseNorm: IECPoint;
4459+ begin
4460+ baseNorm := Self.Normalize();
4461+ Result := Fm_curve.CreatePoint(
4462+ baseNorm.XCoord.ToBigInteger,
4463+ baseNorm.YCoord.ToBigInteger,
4464+ baseNorm.IsCompressed
4465+ );
4466+ end ;
4467+
44034468{ TF2mPoint }
44044469
44054470constructor TF2mPoint.Create(const curve: IECCurve;
@@ -6703,6 +6768,19 @@ constructor TSimpleLookupTable.Create(const points
67036768 FPoints := Copy(points, off, len);
67046769end ;
67056770
6771+ destructor TSimpleLookupTable.Destroy;
6772+ var
6773+ i: Integer;
6774+ begin
6775+ if Assigned(FPoints) then
6776+ begin
6777+ for i := 0 to Length(FPoints) - 1 do
6778+ FPoints[i] := nil ;
6779+ FPoints := nil ;
6780+ end ;
6781+ inherited ;
6782+ end ;
6783+
67066784function TSimpleLookupTable.GetSize : Int32;
67076785begin
67086786 result := System.Length(FPoints);
0 commit comments