Skip to content

Commit 97402c1

Browse files
authored
Fix memory leaks and ci pipeline (#53)
* fix memory leaks * Use Chocolatey for Windows Lazarus Installation * Install OpenSSL via Chocolatey * Use OpenSSL Light * Install OpenSSL Light v1.1.1.20181020
1 parent f187ab0 commit 97402c1

File tree

11 files changed

+192
-83
lines changed

11 files changed

+192
-83
lines changed

.github/workflows/make.yml

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
---
21
name: Make
32

43
on:
54
schedule:
6-
- cron: '0 0 1 * *'
5+
- cron: '0 0 1 * *'
76
push:
87
branches:
98
- "**"
@@ -27,37 +26,43 @@ jobs:
2726
- windows-latest
2827

2928
steps:
30-
- name: Checkout
31-
uses: actions/checkout@v4
32-
with:
33-
submodules: true
34-
35-
- name: Build on Linux
36-
if: runner.os == 'Linux'
37-
shell: bash
38-
run: |
39-
set -xeuo pipefail
40-
sudo bash -c 'apt-get update; apt-get install -y lazarus' >/dev/null
41-
instantfpc -Fu/usr/lib/lazarus/*/components/lazutils \
42-
-B '.github/workflows/make.pas'
43-
44-
- name: Build on Windows
45-
if: runner.os == 'Windows'
46-
shell: powershell
47-
run: |
48-
$ErrorActionPreference = 'stop'
49-
Set-PSDebug -Strict
50-
New-Variable -Option Constant -Name VAR -Value @{
51-
Uri =
52-
'https://icolo.dl.sourceforge.net/project/lazarus/Lazarus%20Windows%2064%20bits/Lazarus%204.0/lazarus-4.0-fpc-3.2.2-win64.exe?viasf=1'
53-
OutFile = (New-TemporaryFile).FullName + '.exe'
54-
}
55-
Invoke-WebRequest @VAR
56-
& $VAR.OutFile.Replace('Temp', 'Temp\.') /SP- /VERYSILENT /NORESTART `
57-
/SUPPRESSMSGBOXES | Out-Null
58-
$Env:PATH+=';C:\Lazarus'
59-
(Get-Command 'lazbuild').Source | Out-Host
60-
$Env:PATH+=';C:\Lazarus\fpc\3.2.2\bin\x86_64-win64'
61-
(Get-Command 'instantfpc').Source | Out-Host
62-
instantfpc -FuC:\Lazarus\components\lazutils `
63-
-B '.github/workflows/make.pas'
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
with:
32+
submodules: true
33+
34+
- name: Build on Linux
35+
if: runner.os == 'Linux'
36+
shell: bash
37+
run: |
38+
set -xeuo pipefail
39+
sudo bash -c 'apt-get update; apt-get install -y lazarus' >/dev/null
40+
instantfpc -Fu/usr/lib/lazarus/*/components/lazutils \
41+
-B '.github/workflows/make.pas'
42+
43+
- name: Build on Windows
44+
if: runner.os == 'Windows'
45+
shell: powershell
46+
run: |
47+
$ErrorActionPreference = 'stop'
48+
Set-PSDebug -Strict
49+
50+
Write-Host "Installing Lazarus and OpenSSL 1.1 via Chocolatey..."
51+
choco upgrade chocolatey -y
52+
choco install lazarus -y
53+
choco install openssl.light --version=1.1.1.20181020 -y
54+
55+
Write-Host "Verifying installed packages..."
56+
choco list
57+
58+
# Lazarus installs to C:\Lazarus by default
59+
# Add Lazarus and OpenSSL paths for instantfpc
60+
$env:Path += ';C:\Lazarus;C:\Lazarus\fpc\3.2.2\bin\x86_64-win64;C:\ProgramData\chocolatey\lib\openssl.light\tools'
61+
62+
Write-Host "Checking lazbuild and instantfpc availability..."
63+
Get-Command lazbuild
64+
Get-Command instantfpc
65+
66+
Write-Host "Building make.pas..."
67+
instantfpc -FuC:\Lazarus\components\lazutils `
68+
-B '.github/workflows/make.pas'

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;

0 commit comments

Comments
 (0)