Skip to content

Commit

Permalink
Benchmark and optimize AddRoundKey
Browse files Browse the repository at this point in the history
  • Loading branch information
tuokri committed Aug 13, 2024
1 parent add0b41 commit 0d54839
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Classes/FCryptoAES.uc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class FCryptoAES extends Object
notplaceable
abstract;

`include(FCrypto\Classes\FCryptoMacros.uci);
`include(FCrypto\Classes\FCryptoAESMacros.uci);

var const array<byte> RCon;
Expand Down Expand Up @@ -551,8 +552,39 @@ static final function AddRoundKey(
// Q[I] = Q[I] ^ SK[I];
// }

// TODO: need to benchmark whether a temp var here is better.
local int Offset_1;
local int Offset_2;
local int Offset_3;
local int Offset_4;
local int Offset_5;
local int Offset_6;
local int Offset_7;

Offset_1 = Offset + 1;
Offset_2 = Offset + 2;
Offset_3 = Offset + 3;
Offset_4 = Offset + 4;
Offset_5 = Offset + 5;
Offset_6 = Offset + 6;
Offset_7 = Offset + 7;

Q[Offset ] = Q[Offset ] ^ SK[Offset ];
Q[Offset_1] = Q[Offset_1] ^ SK[Offset_1];
Q[Offset_2] = Q[Offset_2] ^ SK[Offset_2];
Q[Offset_3] = Q[Offset_3] ^ SK[Offset_3];
Q[Offset_4] = Q[Offset_4] ^ SK[Offset_4];
Q[Offset_5] = Q[Offset_5] ^ SK[Offset_5];
Q[Offset_6] = Q[Offset_6] ^ SK[Offset_6];
Q[Offset_7] = Q[Offset_7] ^ SK[Offset_7];
}

`if(`isdefined(FCBENCHMARK))
static final function AddRoundKey_NoTempVars(
out array<int> Q,
const out array<int> SK,
optional int Offset = 0
)
{
Q[Offset ] = Q[Offset ] ^ SK[Offset ];
Q[Offset + 1] = Q[Offset + 1] ^ SK[Offset + 1];
Q[Offset + 2] = Q[Offset + 2] ^ SK[Offset + 2];
Expand All @@ -562,6 +594,7 @@ static final function AddRoundKey(
Q[Offset + 6] = Q[Offset + 6] ^ SK[Offset + 6];
Q[Offset + 7] = Q[Offset + 7] ^ SK[Offset + 7];
}
`endif

// TODO: can be made a macro for performance?
static final function InvShiftRows(out array<int> Q)
Expand Down
28 changes: 28 additions & 0 deletions Classes/FCryptoTestMutator.uc
Original file line number Diff line number Diff line change
Expand Up @@ -1401,9 +1401,11 @@ private final simulated function int TestSpeed()
local FCQWORD QW9;
local bool bQWCarry;
local float QWClock;
local float Q;
local int QWIdx;
local int BenchmarkRound;
local array<int> X;
local array<int> Y;

// TODO: Design for FCQWORD arithmetic.
Dummy = 0xFFFFFFFF;
Expand Down Expand Up @@ -1568,6 +1570,32 @@ private final simulated function int TestSpeed()
UnClock(QWClock);
`fclog("QWClock (decode2)=" $ QWClock);

Q = 0;
X.Length = 0;
X.Length = 1024;
Y.Length = 0;
Y.Length = 1024;
Clock(Q);
for (BenchmarkRound = 0; BenchmarkRound < 512; ++BenchmarkRound)
{
class'FCryptoAES'.static.AddRoundKey(X, Y);
}
UnClock(Q);
`fclog("Qclock (AddRoundKey (TempVars) )=" $ Q);

Q = 0;
X.Length = 0;
X.Length = 1024;
Y.Length = 0;
Y.Length = 1024;
Clock(Q);
for (BenchmarkRound = 0; BenchmarkRound < 512; ++BenchmarkRound)
{
class'FCryptoAES'.static.AddRoundKey_NoTempVars(X, Y);
}
UnClock(Q);
`fclog("Qclock (AddRoundKey (no temp vars))=" $ Q);

return 0;
}

Expand Down

0 comments on commit 0d54839

Please sign in to comment.