Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching look up table (LUT) in work-group local memory #4

Open
wants to merge 3 commits into
base: parallel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions include/harpocrates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,57 @@ namespace harpocrates {
//
// Output:
// - enc: 16 encrypted output bytes
#if defined HARPOCRATES_PARALLEL
static inline void
encrypt(const sycl::local_ptr<uint8_t> lut, // look up table
const uint8_t* const __restrict txt, // input plain text
uint8_t* const __restrict enc // output encrypted bytes
)
{
uint16_t state[8] = { 0u };

constexpr size_t itr_cnt = harpocrates_common::N_ROWS >> 1;

#if defined __clang__
#pragma unroll 4
#elif defined __GNUG__
#pragma GCC ivdep
#pragma GCC unroll 4
#endif
for (size_t i = 0; i < itr_cnt; i++) {
const size_t b_off = i << 2;
const size_t r_off = i << 1;

state[r_off ^ 0] = (static_cast<uint16_t>(txt[b_off ^ 0]) << 8) |
static_cast<uint16_t>(txt[b_off ^ 1]);
state[r_off ^ 1] = (static_cast<uint16_t>(txt[b_off ^ 2]) << 8) |
static_cast<uint16_t>(txt[b_off ^ 3]);
}

for (size_t i = 0; i < harpocrates_common::N_ROUNDS; i++) {
harpocrates_utils::left_to_right_convoluted_substitution(state, lut);
harpocrates_utils::add_rc(state, i);
harpocrates_utils::column_substitution(state, lut);
harpocrates_utils::right_to_left_convoluted_substitution(state, lut);
}

#if defined __clang__
#pragma unroll 4
#elif defined __GNUG__
#pragma GCC ivdep
#pragma GCC unroll 4
#endif
for (size_t i = 0; i < itr_cnt; i++) {
const size_t b_off = i << 2;
const size_t r_off = i << 1;

enc[b_off ^ 0] = static_cast<uint8_t>(state[r_off ^ 0] >> 8);
enc[b_off ^ 1] = static_cast<uint8_t>(state[r_off ^ 0]);
enc[b_off ^ 2] = static_cast<uint8_t>(state[r_off ^ 1] >> 8);
enc[b_off ^ 3] = static_cast<uint8_t>(state[r_off ^ 1]);
}
}
#else
static inline void
encrypt(const uint8_t* const __restrict lut, // look up table
const uint8_t* const __restrict txt, // input plain text
Expand Down Expand Up @@ -64,6 +115,7 @@ encrypt(const uint8_t* const __restrict lut, // look up table
enc[b_off ^ 3] = static_cast<uint8_t>(state[r_off ^ 1]);
}
}
#endif

// Given 16 -bytes of encrypted input message block & inverse look up table
// ( read `inv_lut` ) of size 256 ( because each LUT element is of size 8 -bit
Expand All @@ -82,6 +134,59 @@ encrypt(const uint8_t* const __restrict lut, // look up table
// inv_lut = harpocrates_utils::generate_inv_lut(lut)
//
// where `lut` is the same look up table used during encryption.
#if defined HARPOCRATES_PARALLEL
static inline void
decrypt(const sycl::local_ptr<uint8_t> inv_lut, // inverse look up table
const uint8_t* const __restrict enc, // input encrypted bytes
uint8_t* const __restrict dec // output decrypted bytes
)
{
uint16_t state[8] = { 0u };

constexpr size_t itr_cnt = harpocrates_common::N_ROWS >> 1;

#if defined __clang__
#pragma unroll 4
#elif defined __GNUG__
#pragma GCC ivdep
#pragma GCC unroll 4
#endif
for (size_t i = 0; i < itr_cnt; i++) {
const size_t b_off = i << 2;
const size_t r_off = i << 1;

state[r_off ^ 0] = (static_cast<uint16_t>(enc[b_off ^ 0]) << 8) |
static_cast<uint16_t>(enc[b_off ^ 1]);
state[r_off ^ 1] = (static_cast<uint16_t>(enc[b_off ^ 2]) << 8) |
static_cast<uint16_t>(enc[b_off ^ 3]);
}

for (size_t i = 0; i < harpocrates_common::N_ROUNDS; i++) {
using namespace harpocrates_utils;

left_to_right_convoluted_substitution(state, inv_lut);
column_substitution(state, inv_lut);
add_rc(state, harpocrates_common::N_ROUNDS - (i + 1));
right_to_left_convoluted_substitution(state, inv_lut);
}

#if defined __clang__
#pragma unroll 4
#elif defined __GNUG__
#pragma GCC ivdep
#pragma GCC unroll 4
#endif
for (size_t i = 0; i < itr_cnt; i++) {
const size_t b_off = i << 2;
const size_t r_off = i << 1;

dec[b_off ^ 0] = static_cast<uint8_t>(state[r_off ^ 0] >> 8);
dec[b_off ^ 1] = static_cast<uint8_t>(state[r_off ^ 0]);
dec[b_off ^ 2] = static_cast<uint8_t>(state[r_off ^ 1] >> 8);
dec[b_off ^ 3] = static_cast<uint8_t>(state[r_off ^ 1]);
}
}
#else
static inline void
decrypt(const uint8_t* const __restrict inv_lut, // inverse look up table
const uint8_t* const __restrict enc, // input encrypted bytes
Expand Down Expand Up @@ -133,5 +238,6 @@ decrypt(const uint8_t* const __restrict inv_lut, // inverse look up table
dec[b_off ^ 3] = static_cast<uint8_t>(state[r_off ^ 1]);
}
}
#endif

}
41 changes: 39 additions & 2 deletions include/harpocrates_parallel.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#pragma once

#if !defined HARPOCRATES_PARALLEL
#define HARPOCRATES_PARALLEL
#endif

#include "harpocrates.hpp"
#include <CL/sycl.hpp>
#include <cassert>
Expand Down Expand Up @@ -70,12 +75,28 @@ encrypt(sycl::queue& q, // SYCL queue
// create dependency graph
h.depends_on(evts);

sycl::accessor<uint8_t,
1,
sycl::access_mode::read_write,
sycl::access::target::local>
loc_lut{ sycl::range<1>{ 256ul }, h };

const auto rng = sycl::nd_range<1>{ wi_cnt, wg_size };
h.parallel_for<kernelHarpocratesEncrypt>(rng, [=](sycl::nd_item<1> it) {
const auto grp = it.get_group();

if (it.get_local_linear_id() == 0ul) {
for (size_t i = 0; i < 256ul; i++) {
loc_lut[i] = lut[i];
}
}

sycl::group_barrier(grp, sycl::memory_scope_work_group);

const size_t idx = it.get_global_linear_id();
const size_t b_off = idx << 4;

harpocrates::encrypt(lut, txt + b_off, enc + b_off);
harpocrates::encrypt(loc_lut.get_pointer(), txt + b_off, enc + b_off);
});
});
}
Expand Down Expand Up @@ -120,12 +141,28 @@ decrypt(
// create dependency graph
h.depends_on(evts);

sycl::accessor<uint8_t,
1,
sycl::access_mode::read_write,
sycl::access::target::local>
loc_inv_lut{ sycl::range<1>{ 256ul }, h };

const auto rng = sycl::nd_range<1>{ wi_cnt, wg_size };
h.parallel_for<kernelHarpocratesDecrypt>(rng, [=](sycl::nd_item<1> it) {
const auto grp = it.get_group();

if (it.get_local_linear_id() == 0ul) {
for (size_t i = 0; i < 256ul; i++) {
loc_inv_lut[i] = inv_lut[i];
}
}

sycl::group_barrier(grp, sycl::memory_scope_work_group);

const size_t idx = it.get_global_linear_id();
const size_t b_off = idx << 4;

harpocrates::decrypt(inv_lut, enc + b_off, dec + b_off);
harpocrates::decrypt(loc_inv_lut.get_pointer(), enc + b_off, dec + b_off);
});
});
}
Expand Down
Loading