Skip to content

Commit 2e4e7be

Browse files
committed
fix memory leaks
1 parent f187ab0 commit 2e4e7be

File tree

10 files changed

+151
-47
lines changed

10 files changed

+151
-47
lines changed

CryptoLib/src/Interfaces/ClpIECC.pas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ interface
271271

272272
function ThreeTimes(): IECPoint;
273273

274+
function Clone: IECPoint;
275+
274276
function Equals(const other: IECPoint): Boolean;
275277
function GetHashCode(): {$IFDEF DELPHI}Int32; {$ELSE}PtrInt; {$ENDIF DELPHI}
276278
function ToString(): String;

CryptoLib/src/Math/EC/Abc/ClpTnaf.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ class function TTnaf.GetPreComp(const p: IAbstractF2mPoint; a: ShortInt)
519519

520520
System.SetLength(pu, UInt32(System.Length(alphaTnaf) + 1) shr 1);
521521

522-
pu[0] := p;
522+
pu[0] := p.Clone() as IAbstractF2mPoint;
523523

524524
precompLen := UInt32(System.Length(alphaTnaf));
525525

CryptoLib/src/Math/EC/ClpECAlgorithms.pas

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,6 @@ class function TECAlgorithms.ImplShamirsTrickWNaf(const endomorphism
394394
result := ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ,
395395
preCompNegQ, wnafQ);
396396

397-
infoP.PreComp := Nil; // Review
398-
infoP.PreCompNeg := Nil; // Review
399-
infoQ.PreComp := Nil; // Review
400-
infoQ.PreCompNeg := Nil; // Review
401-
402397
end;
403398

404399
class function TECAlgorithms.ImplShamirsTrickWNaf(const p: IECPoint;
@@ -435,10 +430,6 @@ class function TECAlgorithms.ImplShamirsTrickWNaf(const p: IECPoint;
435430
then
436431
begin
437432
result := ImplShamirsTrickFixedPoint(p, k, q, l);
438-
infoP.PreComp := Nil; // Review
439-
infoP.PreCompNeg := Nil; // Review
440-
infoQ.PreComp := Nil; // Review
441-
infoQ.PreCompNeg := Nil; // Review
442433
Exit;
443434
end;
444435

@@ -486,10 +477,6 @@ class function TECAlgorithms.ImplShamirsTrickWNaf(const p: IECPoint;
486477

487478
result := ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ,
488479
preCompNegQ, wnafQ);
489-
infoP.PreComp := Nil; // Review
490-
infoP.PreCompNeg := Nil; // Review
491-
infoQ.PreComp := Nil; // Review
492-
infoQ.PreCompNeg := Nil; // Review
493480
end;
494481

495482
class function TECAlgorithms.ImplShamirsTrickWNaf(const preCompP,
@@ -642,13 +629,6 @@ class function TECAlgorithms.ImplSumOfMultiplies(const endomorphism
642629
end;
643630

644631
result := ImplSumOfMultiplies(negs, infos, wnafs);
645-
646-
for i := System.Low(infos) to System.High(infos) do
647-
begin
648-
infos[i].PreComp := Nil; // Review
649-
infos[i].PreCompNeg := Nil; // Review
650-
end;
651-
652632
end;
653633

654634
class function TECAlgorithms.ImplSumOfMultiplies
@@ -684,13 +664,6 @@ class function TECAlgorithms.ImplSumOfMultiplies
684664
end;
685665

686666
result := ImplSumOfMultiplies(negs, infos, wnafs);
687-
688-
for i := System.Low(infos) to System.High(infos) do
689-
begin
690-
infos[i].PreComp := Nil; // Review
691-
infos[i].PreCompNeg := Nil; // Review
692-
end;
693-
694667
end;
695668

696669
class function TECAlgorithms.ImplSumOfMultiplies

CryptoLib/src/Math/EC/ClpECC.pas

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

29943006
function 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+
29963023
var
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);
37193746
end;
37203747

3748+
destructor TDefaultLookupTable.Destroy;
3749+
begin
3750+
Fm_outer := nil;
3751+
Fm_table := nil;
3752+
inherited;
3753+
end;
3754+
37213755
function TDefaultLookupTable.GetSize: Int32;
37223756
begin
37233757
result := Fm_size;
@@ -3804,6 +3838,14 @@ function TDefaultF2mLookupTable.CreatePoint(const x, y: TCryptoLibInt64Array)
38043838
result := Fm_outer.CreateRawPoint(XFieldElement, YFieldElement, false);
38053839
end;
38063840

3841+
3842+
destructor TDefaultF2mLookupTable.Destroy;
3843+
begin
3844+
Fm_outer := nil;
3845+
Fm_table := nil;
3846+
inherited;
3847+
end;
3848+
38073849
function TDefaultF2mLookupTable.GetSize: Int32;
38083850
begin
38093851
result := Fm_size;
@@ -4084,12 +4126,23 @@ function TECPoint.CreateScaledPoint(const sx, sy: IECFieldElement): IECPoint;
40844126
end;
40854127

40864128
destructor TECPoint.Destroy;
4129+
var
4130+
Key: string;
40874131
begin
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;
40914143
end;
40924144

4145+
40934146
class constructor TECPoint.ECPoint;
40944147
begin
40954148
System.SetLength(FEMPTY_ZS, 0);
@@ -4400,6 +4453,18 @@ function TECPoint.GetDetachedPoint: IECPoint;
44004453
result := Normalize().Detach();
44014454
end;
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

44054470
constructor TF2mPoint.Create(const curve: IECCurve;
@@ -6703,6 +6768,19 @@ constructor TSimpleLookupTable.Create(const points
67036768
FPoints := Copy(points, off, len);
67046769
end;
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+
67066784
function TSimpleLookupTable.GetSize: Int32;
67076785
begin
67086786
result := System.Length(FPoints);

CryptoLib/src/Math/EC/ClpECCompUtilities.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ function TWNafUtilities.TWNafCallback.Precompute(const existing: IPreCompInfo)
10411041
curPreCompLen := iniPreCompLen;
10421042
if (curPreCompLen = 0) then
10431043
begin
1044-
PreComp[0] := Fm_p;
1044+
PreComp[0] := Fm_p.Clone();
10451045
curPreCompLen := 1;
10461046
end;
10471047

CryptoLib/src/Math/EC/Endo/ClpEndoPreCompInfo.pas

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ TEndoPreCompInfo = class sealed(TInterfacedObject, IPreCompInfo,
4343

4444
public
4545

46+
destructor Destroy; override;
47+
4648
property Endomorphism: IECEndomorphism read GetEndomorphism
4749
write SetEndomorphism;
4850
property MappedPoint: IECPoint read GetMappedPoint write SetMappedPoint;
@@ -52,6 +54,13 @@ implementation
5254

5355
{ TEndoPreCompInfo }
5456

57+
destructor TEndoPreCompInfo.Destroy;
58+
begin
59+
FEndomorphism := nil;
60+
FMappedPoint := nil;
61+
inherited;
62+
end;
63+
5564
function TEndoPreCompInfo.GetEndomorphism: IECEndomorphism;
5665
begin
5766
result := FEndomorphism;

CryptoLib/src/Math/EC/Multiplier/ClpFixedPointPreCompInfo.pas

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ TFixedPointPreCompInfo = class(TInterfacedObject, IPreCompInfo,
6464

6565
public
6666
constructor Create();
67+
destructor Destroy; override;
6768
property Offset: IECPoint read GetOffset write SetOffset;
6869
property LookupTable: IECLookupTable read GetLookupTable
6970
write SetLookupTable;
@@ -81,6 +82,13 @@ constructor TFixedPointPreCompInfo.Create;
8182
Fm_width := -1;
8283
end;
8384

85+
destructor TFixedPointPreCompInfo.Destroy;
86+
begin
87+
Fm_offset := nil;
88+
Fm_lookupTable := nil;
89+
inherited;
90+
end;
91+
8492
function TFixedPointPreCompInfo.GetLookupTable: IECLookupTable;
8593
begin
8694
Result := Fm_lookupTable;

CryptoLib/src/Math/EC/Multiplier/ClpMultipliers.pas

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ TWTauNafCallback = class(TInterfacedObject, IPreCompCallback,
201201
implementation
202202

203203
uses
204-
ClpECAlgorithms; // included here to avoid circular dependency :)
204+
ClpECAlgorithms;
205205

206206
{ TAbstractECMultiplier }
207207

@@ -466,9 +466,6 @@ function TWNafL2RMultiplier.MultiplyPositive(const p: IECPoint;
466466

467467
result := R;
468468

469-
info.preComp := Nil; // Review
470-
info.preCompNeg := Nil; // Review
471-
472469
end;
473470

474471
{ TWTauNafMultiplier }
@@ -534,8 +531,6 @@ class function TWTauNafMultiplier.MultiplyFromWTnaf(const p: IAbstractF2mPoint;
534531
end;
535532
result := q;
536533

537-
pre.preComp := Nil; // Review
538-
539534
end;
540535

541536
function TWTauNafMultiplier.MultiplyWTnaf(const p: IAbstractF2mPoint;
@@ -606,12 +601,11 @@ function TWTauNafMultiplier.TWTauNafCallback.Precompute(const existing
606601
tempResult: IWTauNafPreCompInfo;
607602
begin
608603

609-
// Review uncomment
610-
// if (Supports(existing, IWTauNafPreCompInfo)) then
611-
// begin
612-
// result := existing;
613-
// Exit;
614-
// end;
604+
if (Supports(existing, IWTauNafPreCompInfo)) then
605+
begin
606+
result := existing;
607+
Exit;
608+
end;
615609

616610
tempResult := TWTauNafPreCompInfo.Create();
617611
tempResult.preComp := TTnaf.GetPreComp(Fm_p, Fm_a);

CryptoLib/src/Math/EC/Multiplier/ClpWNafPreCompInfo.pas

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ TWNafPreCompInfo = class sealed(TInterfacedObject, IPreCompInfo,
8787
public
8888

8989
constructor Create();
90+
destructor Destroy; override;
9091
property PreComp: TCryptoLibGenericArray<IECPoint> read GetPreComp
9192
write SetPreComp;
9293
property PreCompNeg: TCryptoLibGenericArray<IECPoint> read GetPreCompNeg
@@ -126,6 +127,30 @@ function TWNafPreCompInfo.DecrementPromotionCountdown: Int32;
126127
result := t;
127128
end;
128129

130+
destructor TWNafPreCompInfo.Destroy;
131+
var
132+
i: Integer;
133+
begin
134+
if Assigned(FPreComp) then
135+
begin
136+
for i := 0 to Length(FPreComp) - 1 do
137+
FPreComp[i] := nil;
138+
FPreComp := nil;
139+
end;
140+
141+
if Assigned(FPreCompNeg) then
142+
begin
143+
for i := 0 to Length(FPreCompNeg) - 1 do
144+
FPreCompNeg[i] := nil;
145+
FPreCompNeg := nil;
146+
end;
147+
148+
FTwice := nil;
149+
150+
inherited;
151+
end;
152+
153+
129154
function TWNafPreCompInfo.GetConfWidth: Int32;
130155
begin
131156
result := FConfWidth;

0 commit comments

Comments
 (0)