Skip to content

Commit

Permalink
Remove pessimizing lookup tables
Browse files Browse the repository at this point in the history
  • Loading branch information
kimci86 committed Aug 17, 2024
1 parent cc50681 commit 7b4ea52
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 24 deletions.
14 changes: 0 additions & 14 deletions include/MultTab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
class MultTab
{
public:
/// \return mult * x using a lookup table
static auto getMult(std::uint8_t x) -> std::uint32_t
{
return instance.multtab[x];
}

/// \return mult^-1 * x using a lookup table
static auto getMultinv(std::uint8_t x) -> std::uint32_t
{
return instance.multinvtab[x];
}

/// \return a vector of bytes x such that
/// msb(x*mult^-1) is equal to msbprod or msbprod-1
static auto getMsbProdFiber2(std::uint8_t msbprodinv) -> const std::vector<std::uint8_t>&
Expand All @@ -45,8 +33,6 @@ class MultTab
MultTab();

// lookup tables
std::array<std::uint32_t, 256> multtab;
std::array<std::uint32_t, 256> multinvtab;
std::array<std::vector<std::uint8_t>, 256> msbprodfiber2;
std::array<std::vector<std::uint8_t>, 256> msbprodfiber3;

Expand Down
6 changes: 3 additions & 3 deletions src/Attack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ void Attack::exploreZlists(int i)
else // the Z-list is complete so iterate over possible Y values
{
// guess Y7[8,24) and keep prod == (Y7[8,32) - 1) * mult^-1
for (auto y7_8_24 = std::uint32_t{}, prod = (MultTab::getMultinv(msb(ylist[7])) << 24) - MultTab::multInv;
for (auto y7_8_24 = std::uint32_t{}, prod = (MultTab::multInv * msb(ylist[7]) << 24) - MultTab::multInv;
y7_8_24 < 1 << 24; y7_8_24 += 1 << 8, prod += MultTab::multInv << 8)
// get possible Y7[0,8) values
for (const auto y7_0_8 : MultTab::getMsbProdFiber3(msb(ylist[6]) - msb(prod)))
// filter Y7[0,8) using Y6[24,32)
if (prod + MultTab::getMultinv(y7_0_8) - (ylist[6] & mask<24, 32>) <= maxdiff<24>)
if (prod + MultTab::multInv * y7_0_8 - (ylist[6] & mask<24, 32>) <= maxdiff<24>)
{
ylist[7] = y7_0_8 | y7_8_24 | (ylist[7] & mask<24, 32>);
exploreYlists(7);
Expand All @@ -80,7 +80,7 @@ void Attack::exploreYlists(int i)
const auto yim1 = fy - xi_0_8;

// filter values with Y{i-2}[24,32)
if (ffy - MultTab::getMultinv(xi_0_8) - (ylist[i - 2] & mask<24, 32>) <= maxdiff<24> &&
if (ffy - MultTab::multInv * xi_0_8 - (ylist[i - 2] & mask<24, 32>) <= maxdiff<24> &&
msb(yim1) == msb(ylist[i - 1]))
{
// add Y{i-1} to the Y-list
Expand Down
6 changes: 1 addition & 5 deletions src/MultTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ const MultTab MultTab::instance;

MultTab::MultTab()
{
auto prod = std::uint32_t{}; // x * mult
auto prodinv = std::uint32_t{}; // x * mult^-1
for (auto x = 0; x < 256; x++, prod += mult, prodinv += multInv)
for (auto x = 0; x < 256; x++, prodinv += multInv)
{
multtab[x] = prod;
multinvtab[x] = prodinv;

msbprodfiber2[msb(prodinv)].push_back(x);
msbprodfiber2[(msb(prodinv) + 1) % 256].push_back(x);

Expand Down
4 changes: 2 additions & 2 deletions src/password.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class SixCharactersRecovery
const auto yim1 = fy - xi_0_8;

// filter values with Y{i-2}[24,32)
if (ffy - MultTab::getMultinv(xi_0_8) - (y[i - 2] & mask<24, 32>) <= maxdiff<24> &&
if (ffy - MultTab::multInv * xi_0_8 - (y[i - 2] & mask<24, 32>) <= maxdiff<24> &&
msb(yim1) == msb(y[i - 1]))
{
// add Y{i-1} to the Y-list
Expand Down Expand Up @@ -255,7 +255,7 @@ class BruteforceRecovery : public SixCharactersRecovery<BruteforceRecovery>
{
// finish to update the cipher state
const auto x0 = x0_partial ^ Crc32Tab::crc32(0, pi);
const auto y0 = y0_partial + MultTab::getMult(lsb(x0));
const auto y0 = y0_partial + MultTab::mult * lsb(x0);
const auto z0 = z0_partial ^ Crc32Tab::crc32(0, msb(y0));

// SixCharactersRecovery::search is inlined below for performance
Expand Down

0 comments on commit 7b4ea52

Please sign in to comment.