From 2969016625c22d9b1e73534f82aed5a4f26b602f Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sat, 1 Oct 2016 10:35:58 +0200 Subject: Created a container Message CMixMessage for all network communication. --- libcmix-protobuf/cmix.proto | 10 +++- node/node.cpp | 124 ++++++++++++++++++++++++++------------------ node/node.hpp | 3 ++ 3 files changed, 86 insertions(+), 51 deletions(-) diff --git a/libcmix-protobuf/cmix.proto b/libcmix-protobuf/cmix.proto index f0c1dc3..3068291 100644 --- a/libcmix-protobuf/cmix.proto +++ b/libcmix-protobuf/cmix.proto @@ -1,3 +1,11 @@ -message initialization { +package cmix_proto; + +message Initialization { required bytes public_share = 1; } + +message CMixMessage { + oneof contents { + Initialization initialization = 1; + } +} diff --git a/node/node.cpp b/node/node.cpp index 8327269..761419b 100644 --- a/node/node.cpp +++ b/node/node.cpp @@ -50,60 +50,84 @@ void Node::accept_handler(boost::asio::ip::tcp::socket&& socket) } void Node::start_initialisation() { - initialization init; + cmix_proto::Initialization init; init.set_public_share(keypair.pub, keypair.pub_len); - - std::string message; - init.SerializeToString(&message); - - next_node.send(message); - auto f = [this](std::vector bytes) { - if(network_settings.is_first) { - init.ParseFromArray(bytes.data(), bytes.size()); - std::string share = init.public_share(); - - network_pub_key = std::vector(share.begin(), share.end()); - - 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 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); - } - }; + cmix_proto::CMixMessage message; + *message.mutable_initialization() = init; + + std::string message_str; + message.SerializeToString(&message_str); + + next_node.send(message_str); for(auto&& client : clients) { - client.receive(f); + client.receive(std::bind(&Node::handle_message, this, std::placeholders::_1)); + } +} + +void Node::handle_message(const std::vector& message_buffer) +{ + cmix_proto::CMixMessage message; + if(!message.ParseFromArray(message_buffer.data(), message_buffer.size())) { + BOOST_LOG_TRIVIAL(error) << "Received something which was not a CMixMessage"; + } + + switch(message.contents_case()) { + case cmix_proto::CMixMessage::ContentsCase::kInitialization: { + handle_initialization(message.initialization()); + break; + } + default: { + BOOST_LOG_TRIVIAL(error) << "CMixMessage contains unknown contents."; + } + } +} + +void Node::handle_initialization(const cmix_proto::Initialization& init) +{ + if(network_settings.is_first) { + std::string share = init.public_share(); + + network_pub_key = std::vector(share.begin(), share.end()); + + start_precomputation(); + } else { + mpz_t shared; + mpz_init(shared); + mpz_import(shared, init.public_share().size(), -1, 1, 0, 0, init.public_share().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 new_shared(keypair.pub_len, '\0'); + size_t size; + mpz_export(new_shared.data(), &size, -1, 1, 0, 0, shared); + + cmix_proto::Initialization init; + init.set_public_share(new_shared.data(), new_shared.size()); + + cmix_proto::CMixMessage message; + *message.mutable_initialization() = init; + + std::string message_str; + message.SerializeToString(&message_str); + next_node.send(message_str); + + mpz_clear(shared); + mpz_clear(my_share); + mpz_clear(mod); } } diff --git a/node/node.hpp b/node/node.hpp index ef9e43c..a481c70 100644 --- a/node/node.hpp +++ b/node/node.hpp @@ -47,6 +47,9 @@ class Node void start_precomputation(); void start_initialisation(); + + void handle_message(std::vector const& message_buffer); + void handle_initialization(cmix_proto::Initialization const& init); public: /*! -- cgit v1.2.3-70-g09d2