diff options
Diffstat (limited to 'node')
| -rw-r--r-- | node/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | node/nextnode.cpp | 20 | ||||
| -rw-r--r-- | node/nextnode.hpp | 7 | ||||
| -rw-r--r-- | node/node.cpp | 56 |
4 files changed, 64 insertions, 23 deletions
diff --git a/node/CMakeLists.txt b/node/CMakeLists.txt index c3c8e46..adbedb1 100644 --- a/node/CMakeLists.txt +++ b/node/CMakeLists.txt @@ -1,5 +1,7 @@ find_package(Boost COMPONENTS system program_options REQUIRED) +find_package(gmp REQUIRED) + add_executable(node main.cpp node.hpp node.cpp @@ -18,4 +20,6 @@ target_link_libraries(node PRIVATE cmix PRIVATE cmix-network PRIVATE cmix-protobuf + PRIVATE gmpxx + PRIVATE gmp ) diff --git a/node/nextnode.cpp b/node/nextnode.cpp index d38200d..f6c4454 100644 --- a/node/nextnode.cpp +++ b/node/nextnode.cpp @@ -1,34 +1,24 @@ #include "nextnode.hpp" -#include "connect.hpp" - #include "logging.hpp" using namespace boost::asio::ip; NextNode::NextNode(tcp::socket&& socket) -: Client(std::move(socket)) +: client(std::move(socket)) {} void NextNode::send(std::string message) { - auto handler = [](boost::system::error_code const& ec, std::size_t bytes_transferred) { - BOOST_LOG_TRIVIAL(trace) << "sent message"; - if(ec) { - BOOST_LOG_TRIVIAL(fatal) << ec; - throw std::runtime_error("unable to send message"); - } - }; - - socket.async_send(boost::asio::buffer(message), 0, handler); + client.send(message); } -void NextNode::connect(std::string next_host, std::string next_port, std::function<void ()> on_connect) +void NextNode::async_connect(std::string next_host, std::string next_port, std::function<void ()> on_connect) { - async_connect(socket, next_host, next_port, on_connect); + client.async_connect(next_host, next_port, on_connect); } void NextNode::close() { - socket.close(); + client.close(); } diff --git a/node/nextnode.hpp b/node/nextnode.hpp index 24b93c4..6550445 100644 --- a/node/nextnode.hpp +++ b/node/nextnode.hpp @@ -11,8 +11,9 @@ /*! * \brief The NextNode class represents the next node in the network, will only be sent to. */ -class NextNode : public Client +class NextNode { + Client client; public: /*! * \brief NextNode @@ -27,12 +28,12 @@ public: void send(std::string message); /*! - * \brief connect + * \brief async_connect * \param next_host The host of the next node. * \param next_port The port of the next node. * \param on_connect The callback to call when the connect was succesfull. */ - void connect(std::string next_host, std::string next_port, std::function<void()> on_connect); + void async_connect(std::string next_host, std::string next_port, std::function<void()> on_connect); /*! * \brief close This function closes the underlying socket connection. diff --git a/node/node.cpp b/node/node.cpp index 045eb13..92c51ec 100644 --- a/node/node.cpp +++ b/node/node.cpp @@ -2,6 +2,10 @@ #include "logging.hpp" +#include "gmpxx.h" + +#include <iostream> + using namespace boost::asio::ip; Node::Node(ListenSettings const& listen_settings, NodeNetworkSettings network_settings) @@ -21,7 +25,7 @@ Node::Node(ListenSettings const& listen_settings, NodeNetworkSettings network_se } }; - next_node.connect(network_settings.next_host, network_settings.next_port, on_connect); + next_node.async_connect(network_settings.next_host, network_settings.next_port, on_connect); } Node::~Node() { @@ -52,18 +56,60 @@ void Node::start_initialisation() { std::string message; init.SerializeToString(&message); - BOOST_LOG_TRIVIAL(trace) << init.DebugString(); + BOOST_LOG_TRIVIAL(trace) << "init: " << init.DebugString(); + BOOST_LOG_TRIVIAL(trace) << "size: " << message.size(); + BOOST_LOG_TRIVIAL(trace) << "raw: " << message; next_node.send(message); auto f = [this](std::vector<uint8_t> bytes) { - network_pub_key = bytes; - if(network_settings.is_first) { + BOOST_LOG_TRIVIAL(trace) << "bytes.size(): " << bytes.size(); + BOOST_LOG_TRIVIAL(trace) << "raw2: " << std::string(bytes.begin(), bytes.end()); + initialization init; + init.ParseFromArray(bytes.data(), bytes.size()); + std::string share = init.public_share(); + + network_pub_key = std::vector<uint8_t>(share.begin(), share.end()); + + BOOST_LOG_TRIVIAL(trace) << "The network pub_key: " << init.DebugString(); + start_precomputation(); + } else { + mpz_t shared; + mpz_init(shared); + mpz_import(shared, bytes.size(), -1, 1, 0, 0, bytes.data()); + + mpz_t my_share; + mpz_init(my_share); + mpz_import(my_share, keypair.pub_len, -1, 1, 0, 0, keypair.pub); + + mpz_mul(shared, shared, my_share); + + mpz_t mod; + mpz_init(mod); + mpz_set_ui(mod, 2); + mpz_pow_ui(mod, mod, 255); + mpz_sub_ui(mod, mod, 19); + + mpz_mod(shared, shared, mod); + + std::vector<uint8_t> new_shared(keypair.pub_len, '\0'); + size_t size; + mpz_export(new_shared.data(), &size, -1, 1, 0, 0, shared); + + initialization init; + init.set_public_share(new_shared.data(), new_shared.size()); + + std::string message; + init.SerializeToString(&message); + next_node.send(message); + + mpz_clear(shared); + mpz_clear(my_share); + mpz_clear(mod); } }; - BOOST_LOG_TRIVIAL(trace) << "number of clients: " << clients.size(); for(auto&& client : clients) { client.receive(f); } |
