Browse Source

Updated some code

devel
Ludovic 'Archivist' Lagouardette 4 years ago
parent
commit
a28c7d178b
4 changed files with 48 additions and 40 deletions
  1. +14
    -0
      include/gp/array.hpp
  2. +10
    -14
      include/rc6_generic.hpp
  3. +14
    -14
      tests/rc6_generic.cpp
  4. +10
    -12
      tests/shared_fd.cpp

+ 14
- 0
include/gp/array.hpp View File

@ -1,4 +1,5 @@
#pragma once
#include <initializer_list>
#include <gp/buffer.hpp>
namespace gp{
@ -7,6 +8,14 @@ namespace gp{
T ary[sz];
public:
using associated_iterator = typename buffer<T>::associated_iterator;
using associated_const_iterator = typename buffer<const T>::associated_iterator;
array() = default;
template<typename ...U>
array(U&& ...v)
: ary{gp::forward(v...)}
{}
constexpr T& operator[] (size_t off)
{
@ -40,6 +49,11 @@ namespace gp{
return true;
}
constexpr bool operator!=(const array& oth) const
{
return !(*this == oth);
}
gp::buffer<T> as_buffer()
{
return gp::buffer<T>{(T*)ary, (T*)ary+sz};

+ 10
- 14
include/rc6_generic.hpp View File

@ -13,9 +13,9 @@ Sean Eron Anderson
seander@cs.stanford.edu
**/
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,
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];
}
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,
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()
{
return S.cbegin();
@ -135,7 +131,7 @@ public:
: S(key)
{}
constexpr k">const block_type encrypt(block_type plaintext) {
constexpr block_type encrypt(block_type plaintext) {
auto& A = plaintext[0];
auto& B = plaintext[1];
auto& C = plaintext[2];
@ -148,8 +144,8 @@ public:
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++);
C = r_l((C ^ u), t % word_size) + *(it++);
std::rotate(plaintext.begin(), plaintext.begin()+1, plaintext.end());
@ -161,7 +157,7 @@ public:
return plaintext;
}
constexpr k">const block_type decrypt(block_type plaintext) {
constexpr block_type decrypt(block_type plaintext) {
auto& A = plaintext[0];
auto& B = plaintext[1];
auto& C = plaintext[2];
@ -174,8 +170,8 @@ public:
for(size_t i = 0; i < r; ++i)
{
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 ;
A = r_r( (A - *(it++)) , u % word_size) ^ t ;
}

+ 14
- 14
tests/rc6_generic.cpp View File

@ -13,17 +13,17 @@ struct RC6test : public test_scaffold {
rc::key_type key = {0,0,0,0};
rc::block_type plaintext = {0,0,0,0};
rc::block_type expected{0x8fc3a536,0x56b1f778,0xc129df4e,0x9848a41e};
rc::block_type expected = {0x8fc3a536,0x56b1f778,0xc129df4e,0x9848a41e};
/*
std::cout<<"plain:";
for(auto a : plaintext)
std::cout << std::hex << a;
auto cipher = rc(key);
*/
auto cipher = rc{key};
plaintext = cipher.encrypt(plaintext);
n">std::cout<<"\nkey__:";
cm">/*std::cout<<"\nkey__:";
for(auto a : key)
std::cout << std::hex << a;
@ -35,7 +35,7 @@ struct RC6test : public test_scaffold {
for(auto a : expected)
std::cout << std::hex << a;
std::cout << std::endl;
*/
return plaintext != expected;
}
};
@ -52,27 +52,27 @@ struct RC6test2 : public test_scaffold {
rc::key_type key = {0,0,0,0};
rc::block_type plaintext = {0,0,0,0};
rc::block_type expected{0,0,0,0};
rc::block_type expected = {0,0,0,0};
/*
std::cout<<"plain:";
for(auto a : plaintext)
std::cout << std::hex << a;
auto cipher = rc(key);
*/
auto cipher = rc{key};
plaintext = cipher.encrypt(plaintext);
n">std::cout<<"\nkey__:";
cm">/*std::cout<<"\nkey__:";
for(auto a : key)
std::cout << std::hex << a;
std::cout<<"\nciphe:";
for(auto a : plaintext)
std::cout << std::hex << a;
*/
plaintext = cipher.decrypt(plaintext);
n">std::cout<<"\ncidec:";
cm">/*std::cout<<"\ncidec:";
for(auto a : plaintext)
std::cout << std::hex << a;
@ -80,7 +80,7 @@ struct RC6test2 : public test_scaffold {
for(auto a : expected)
std::cout << std::hex << a;
std::cout << std::endl;
*/
return plaintext != expected;
}
};

+ 10
- 12
tests/shared_fd.cpp View File

@ -211,29 +211,27 @@ struct sockets_co_test : public test_scaffold {
virtual int run() {
std::atomic_int error = 0;
{
auto v1 = gp::shared_fd::tcp_socket();
error += !(v1.is_valid());
auto v2 = gp::shared_fd::tcp_socket();
error += !(v2.is_valid());
v1.bind(gp::make_ipv4("254.254.254.254",1234));
error += !(v1.has_failed());
std::thread sside([&](){
auto v1 = gp::shared_fd::tcp_socket();
v1.bind(gp::make_ipv4("0.0.0.0",1235));
error += v1.has_failed();
v1.bind(gp::make_ipv4("0.0.0.0",1235));
error += v1.has_failed();
v1.listen();
std::cout << "listens?:" << v1.is_valid()<<std::endl;
v1.listen();
std::cout << "listens?:" << v1.is_valid()<<std::endl;
std::thread sside([&](){
error += !(v1.is_valid());
gp::shared_fd listener = v1;
gp::shared_fd v3;
do
{
v3 = listener.accept();
std::this_thread::sleep_for(500ms);
std::cout << "accept_attempt:" << v3.is_valid()<<std::endl;
std::cout << "accept_attempt:" << v3.error()<<std::endl;
}while(listener.must_retry());
puts("accepted");
error += !(listener.has_failed());
@ -246,7 +244,7 @@ struct sockets_co_test : public test_scaffold {
error += !(v3.has_failed());
return;
});
std::this_thread::sleep_for(1ms);
std::this_thread::sleep_for(6ms);
v2.connect(gp::make_ipv4("255.255.255.255",1235));
error += v2.has_failed();
@ -284,4 +282,4 @@ struct sockets_co_test : public test_scaffold {
}
};
//append_test dummy_polmdf43(new sockets_co_test{});
// append_test dummy_polmdf43(new sockets_co_test{});

Loading…
Cancel
Save