|
@ -13,9 +13,9 @@ Sean Eron Anderson |
|
|
seander@cs.stanford.edu |
|
|
seander@cs.stanford.edu |
|
|
**/ |
|
|
**/ |
|
|
template<> |
|
|
template<> |
|
|
size_t lg<uint32_t>(uint32_t v) |
|
|
|
|
|
|
|
|
constexpr size_t lg<uint32_t>(uint32_t v) |
|
|
{ |
|
|
{ |
|
|
static const int MultiplyDeBruijnBitPosition[32] = |
|
|
|
|
|
|
|
|
constexpr int MultiplyDeBruijnBitPosition[32] = |
|
|
{ |
|
|
{ |
|
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, |
|
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, |
|
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 |
|
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 |
|
@ -30,9 +30,9 @@ size_t lg(uint32_t v) |
|
|
return MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27]; |
|
|
return MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27]; |
|
|
} |
|
|
} |
|
|
template<> |
|
|
template<> |
|
|
size_t lg<uint64_t>(uint64_t v) |
|
|
|
|
|
|
|
|
constexpr size_t lg<uint64_t>(uint64_t v) |
|
|
{ |
|
|
{ |
|
|
static const int MultiplyDeBruijnBitPosition[64] = |
|
|
|
|
|
|
|
|
constexpr int MultiplyDeBruijnBitPosition[64] = |
|
|
{ |
|
|
{ |
|
|
0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, |
|
|
0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, |
|
|
51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, |
|
|
51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, |
|
@ -99,10 +99,6 @@ class RC6 { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const word_t& operator[](const size_t pos) { |
|
|
|
|
|
return S[pos]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const auto cbegin() |
|
|
const auto cbegin() |
|
|
{ |
|
|
{ |
|
|
return S.cbegin(); |
|
|
return S.cbegin(); |
|
@ -135,7 +131,7 @@ public: |
|
|
: S(key) |
|
|
: S(key) |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
constexpr k">const block_type encrypt(block_type plaintext) { |
|
|
|
|
|
|
|
|
constexpr block_type encrypt(block_type plaintext) { |
|
|
auto& A = plaintext[0]; |
|
|
auto& A = plaintext[0]; |
|
|
auto& B = plaintext[1]; |
|
|
auto& B = plaintext[1]; |
|
|
auto& C = plaintext[2]; |
|
|
auto& C = plaintext[2]; |
|
@ -148,8 +144,8 @@ public: |
|
|
|
|
|
|
|
|
for(size_t i = 0; i < r; ++i) |
|
|
for(size_t i = 0; i < r; ++i) |
|
|
{ |
|
|
{ |
|
|
auto u = r_l( D * ( 2 * D + 1 ), mi">5); |
|
|
|
|
|
auto t = r_l( B * ( 2 * B + 1 ), mi">5); |
|
|
|
|
|
|
|
|
auto u = r_l( D * ( 2 * D + 1 ), n">lg(word_size)); |
|
|
|
|
|
auto t = r_l( B * ( 2 * B + 1 ), n">lg(word_size)); |
|
|
A = r_l((A ^ t), u % word_size) + *(it++); |
|
|
A = r_l((A ^ t), u % word_size) + *(it++); |
|
|
C = r_l((C ^ u), t % word_size) + *(it++); |
|
|
C = r_l((C ^ u), t % word_size) + *(it++); |
|
|
std::rotate(plaintext.begin(), plaintext.begin()+1, plaintext.end()); |
|
|
std::rotate(plaintext.begin(), plaintext.begin()+1, plaintext.end()); |
|
@ -161,7 +157,7 @@ public: |
|
|
return plaintext; |
|
|
return plaintext; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr k">const block_type decrypt(block_type plaintext) { |
|
|
|
|
|
|
|
|
constexpr block_type decrypt(block_type plaintext) { |
|
|
auto& A = plaintext[0]; |
|
|
auto& A = plaintext[0]; |
|
|
auto& B = plaintext[1]; |
|
|
auto& B = plaintext[1]; |
|
|
auto& C = plaintext[2]; |
|
|
auto& C = plaintext[2]; |
|
@ -174,8 +170,8 @@ public: |
|
|
for(size_t i = 0; i < r; ++i) |
|
|
for(size_t i = 0; i < r; ++i) |
|
|
{ |
|
|
{ |
|
|
std::rotate(plaintext.begin(), plaintext.end()-1, plaintext.end()); |
|
|
std::rotate(plaintext.begin(), plaintext.end()-1, plaintext.end()); |
|
|
auto u = r_l( D * ( 2 * D + 1 ), mi">5); |
|
|
|
|
|
auto t = r_l( B * ( 2 * B + 1 ), mi">5); |
|
|
|
|
|
|
|
|
auto u = r_l( D * ( 2 * D + 1 ), n">lg(word_size)); |
|
|
|
|
|
auto t = r_l( B * ( 2 * B + 1 ), n">lg(word_size)); |
|
|
C = r_r( (C - *(it++)) , t % word_size) ^ u ; |
|
|
C = r_r( (C - *(it++)) , t % word_size) ^ u ; |
|
|
A = r_r( (A - *(it++)) , u % word_size) ^ t ; |
|
|
A = r_r( (A - *(it++)) , u % word_size) ^ t ; |
|
|
} |
|
|
} |
|
|