From a28c7d178b7e98904f94a79c07d444bc605d3189 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Mon, 2 Mar 2020 19:05:15 +0100 Subject: [PATCH] Updated some code --- include/gp/array.hpp | 14 ++++++++++++++ include/rc6_generic.hpp | 24 ++++++++++-------------- tests/rc6_generic.cpp | 28 ++++++++++++++-------------- tests/shared_fd.cpp | 22 ++++++++++------------ 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/include/gp/array.hpp b/include/gp/array.hpp index 43c15f9..bf50b02 100644 --- a/include/gp/array.hpp +++ b/include/gp/array.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include namespace gp{ @@ -7,6 +8,14 @@ namespace gp{ T ary[sz]; public: using associated_iterator = typename buffer::associated_iterator; + using associated_const_iterator = typename buffer::associated_iterator; + + array() = default; + + template + 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 as_buffer() { return gp::buffer{(T*)ary, (T*)ary+sz}; diff --git a/include/rc6_generic.hpp b/include/rc6_generic.hpp index 3a804e2..fd4ab47 100644 --- a/include/rc6_generic.hpp +++ b/include/rc6_generic.hpp @@ -13,9 +13,9 @@ Sean Eron Anderson seander@cs.stanford.edu **/ template<> -size_t lg(uint32_t v) +constexpr size_t lg(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 v) +constexpr size_t lg(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 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 ), 5); - auto t = r_l( B * ( 2 * B + 1 ), 5); + auto u = r_l( D * ( 2 * D + 1 ), lg(word_size)); + auto t = r_l( B * ( 2 * B + 1 ), 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 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 ), 5); - auto t = r_l( B * ( 2 * B + 1 ), 5); + auto u = r_l( D * ( 2 * D + 1 ), lg(word_size)); + auto t = r_l( B * ( 2 * B + 1 ), lg(word_size)); C = r_r( (C - *(it++)) , t % word_size) ^ u ; A = r_r( (A - *(it++)) , u % word_size) ^ t ; } diff --git a/tests/rc6_generic.cpp b/tests/rc6_generic.cpp index 41c8809..f25b53e 100644 --- a/tests/rc6_generic.cpp +++ b/tests/rc6_generic.cpp @@ -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); - std::cout<<"\nkey__:"; + /*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); - std::cout<<"\nkey__:"; + /*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); - std::cout<<"\ncidec:"; + /*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; } }; diff --git a/tests/shared_fd.cpp b/tests/shared_fd.cpp index d4457ce..7552edd 100644 --- a/tests/shared_fd.cpp +++ b/tests/shared_fd.cpp @@ -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()<