Browse Source

Cleaning up code to be more legible, and checking it with valgrind

tristangurtler 3 years ago
parent
commit
33e41b4eb5

+ 6 - 5
prsona/Makefile

@@ -38,9 +38,9 @@ CC = gcc
 CFLAGS = -std=c99 -O3 -fomit-frame-pointer -I$(666_INC_PATH) -I$(MG_INC_PATH)
 C_LDFLAGS = -lm
 
-all : main server client orchestrator
+all : localTest server client orchestrator
 
-main: $(PRSONA_BIN_PATH) $(PRSONA_OBJ_PATH) $(BGN_OBJ_PATH) $(666_OBJ_PATH) $(PRSONA_BIN_PATH)/main
+localTest: $(PRSONA_BIN_PATH) $(PRSONA_OBJ_PATH) $(BGN_OBJ_PATH) $(666_OBJ_PATH) $(PRSONA_BIN_PATH)/localTest
 
 server: $(PRSONA_BIN_PATH) $(PRSONA_OBJ_PATH) $(BGN_OBJ_PATH) $(666_OBJ_PATH) $(PRSONA_TMP_PATH) $(PRSONA_BIN_PATH)/server
 client: $(PRSONA_BIN_PATH) $(PRSONA_OBJ_PATH) $(BGN_OBJ_PATH) $(666_OBJ_PATH) $(PRSONA_TMP_PATH) $(PRSONA_BIN_PATH)/client
@@ -66,10 +66,11 @@ $(MG_OBJ_PATH):
 
 PRSONA_FULL_SRC = $(wildcard $(PRSONA_SRC_PATH)/*.cpp)
 PRSONA_FULL_NETWORK_SRC = $(wildcard $(PRSONA_SRC_PATH)/network*.cpp)
-PRSONA_SRC = $(filter-out $(PRSONA_FULL_NETWORK_SRC) $(wildcard $(PRSONA_SRC_PATH)/*ain.cpp), $(PRSONA_FULL_SRC)) 
+PRSONA_SRC = $(filter-out $(PRSONA_FULL_NETWORK_SRC) $(wildcard $(PRSONA_SRC_PATH)/*Main.cpp), $(PRSONA_FULL_SRC)) 
 PRSONA_SHARED_NETWORK_OBJ = $(PRSONA_OBJ_PATH)/networking.o
 PRSONA_SERVER_NETWORK_OBJ = $(PRSONA_OBJ_PATH)/networkServer.o
 PRSONA_CLIENT_NETWORK_OBJ = $(PRSONA_OBJ_PATH)/networkClient.o
+PRSONA_ORCHESTRATOR_NETWORK_OBJ = $(PRSONA_OBJ_PATH)/networkOrchestrator.o
 PRSONA_NETWORK_OBJ = $(patsubst $(PRSONA_SRC_PATH)/%.cpp, $(PRSONA_OBJ_PATH)/%.o, $(PRSONA_NETWORK_SRC))
 PRSONA_OBJ = $(patsubst $(PRSONA_SRC_PATH)/%.cpp, $(PRSONA_OBJ_PATH)/%.o, $(PRSONA_SRC))
 
@@ -106,7 +107,7 @@ MG_OBJ += $(MG_OBJ_PATH)/CivetServer.o
 $(MG_OBJ_PATH)/%.o: 
 	make -C ../civetweb WITH_WEBSOCKET=1 WITH_CPP=1 lib
 
-$(PRSONA_BIN_PATH)/main: $(PRSONA_OBJ_PATH)/main.o $(PRSONA_OBJ) $(BGN_OBJ_PATH)/bgn.a
+$(PRSONA_BIN_PATH)/localTest: $(PRSONA_OBJ_PATH)/localMain.o $(PRSONA_OBJ) $(BGN_OBJ_PATH)/bgn.a
 	$(CPP) $(CPPFLAGS) -no-pie -o $@ $^ $(LDFLAGS)
 
 $(PRSONA_BIN_PATH)/server: $(PRSONA_OBJ_PATH)/serverMain.o $(PRSONA_OBJ) $(PRSONA_SHARED_NETWORK_OBJ) $(PRSONA_SERVER_NETWORK_OBJ) $(MG_OBJ) $(BGN_OBJ_PATH)/bgn.a
@@ -115,7 +116,7 @@ $(PRSONA_BIN_PATH)/server: $(PRSONA_OBJ_PATH)/serverMain.o $(PRSONA_OBJ) $(PRSON
 $(PRSONA_BIN_PATH)/client: $(PRSONA_OBJ_PATH)/clientMain.o $(PRSONA_OBJ) $(PRSONA_SHARED_NETWORK_OBJ) $(PRSONA_CLIENT_NETWORK_OBJ) $(MG_OBJ) $(BGN_OBJ_PATH)/bgn.a
 	$(CPP) $(CPPFLAGS) -no-pie -o $@ $^ $(LDFLAGS) $(NETWORK_LDFLAGS)
 
-$(PRSONA_BIN_PATH)/orchestrator: $(PRSONA_OBJ_PATH)/orchestratorMain.o $(PRSONA_OBJ) $(PRSONA_SHARED_NETWORK_OBJ) $(MG_OBJ) $(BGN_OBJ_PATH)/bgn.a
+$(PRSONA_BIN_PATH)/orchestrator: $(PRSONA_OBJ_PATH)/orchestratorMain.o $(PRSONA_OBJ) $(PRSONA_SHARED_NETWORK_OBJ) $(PRSONA_ORCHESTRATOR_NETWORK_OBJ) $(MG_OBJ) $(BGN_OBJ_PATH)/bgn.a
 	$(CPP) $(CPPFLAGS) -no-pie -o $@ $^ $(LDFLAGS) $(NETWORK_LDFLAGS)
 
 .PHONY: clean

+ 12 - 4
prsona/inc/EGCiphertext.hpp

@@ -6,13 +6,21 @@
 class EGCiphertext {
     public:
         EGCiphertext();
-        EGCiphertext(const Twistpoint& mask, const Twistpoint& encryptedMessage);
+        EGCiphertext(
+            const Twistpoint& mask,
+            const Twistpoint& encryptedMessage);
         Twistpoint mask;
         Twistpoint encryptedMessage;
 
-        bool operator==(const EGCiphertext& other) const;
-        friend std::ostream& operator<<(std::ostream& os, const EGCiphertext& output);
-        friend std::istream& operator>>(std::istream& is, EGCiphertext& input);
+        bool operator==(
+            const EGCiphertext& other
+        ) const;
+        friend std::ostream& operator<<(
+            std::ostream& os,
+            const EGCiphertext& output);
+        friend std::istream& operator>>(
+            std::istream& is,
+            EGCiphertext& input);
 };
 
 #endif

+ 16 - 7
prsona/inc/base.hpp

@@ -23,11 +23,15 @@ class PrsonaBase {
         // CONST GETTERS
         static size_t get_max_allowed_vote();
         Twistpoint get_blinding_generator() const;
-        Twistpoint get_blinding_generator(std::vector<Proof>& pi) const;
+        Twistpoint get_blinding_generator(
+            std::vector<Proof>& pi
+        ) const;
 
         // BINARY SEARCH
         size_t binary_search(
-            const std::vector<Twistpoint> list, const Twistpoint& index) const;
+            const std::vector<Twistpoint> list,
+            const Twistpoint& index
+        ) const;
 
     protected:
         // Essentially constants, true for both servers and clients
@@ -230,15 +234,20 @@ class PrsonaBase {
 
         // SERVER AGREEMENT PROOFS
         Proof generate_valid_vote_row_proof(
-            const std::vector<TwistBipoint>& commitment) const;
+            const std::vector<TwistBipoint>& commitment
+        ) const;
         Proof generate_valid_vote_matrix_proof(
-            const std::vector<std::vector<TwistBipoint>>& commitment) const;
+            const std::vector<std::vector<TwistBipoint>>& commitment
+        ) const;
         Proof generate_valid_user_tally_proof(
-            const EGCiphertext& commitment) const;
+            const EGCiphertext& commitment
+        ) const;
         Proof generate_valid_server_tally_proof(
-            const CurveBipoint& commitment) const;
+            const CurveBipoint& commitment
+        ) const;
         Proof generate_valid_pseudonyms_proof(
-            const std::vector<Twistpoint>& commitment) const;
+            const std::vector<Twistpoint>& commitment
+        ) const;
         
         bool verify_valid_vote_row_proof(
             const std::vector<Proof>& pi,

+ 5 - 2
prsona/inc/client.hpp

@@ -23,7 +23,9 @@ class PrsonaClient : public PrsonaBase {
 
         // BASIC PUBLIC SYSTEM INFO GETTERS
         Twistpoint get_short_term_public_key() const;
-        Twistpoint get_short_term_public_key(Proof &pi) const;
+        Twistpoint get_short_term_public_key(
+            Proof &pi
+        ) const;
 
         // SERVER INTERACTIONS
         std::vector<TwistBipoint> make_votes(
@@ -88,7 +90,8 @@ class PrsonaClient : public PrsonaBase {
         Scalar max_checked;
 
         // SCORE DECRYPTION
-        Scalar decrypt_score(const EGCiphertext& score);
+        Scalar decrypt_score(
+            const EGCiphertext& score);
 
         // OWNERSHIP OF STPK PROOFS
         Proof generate_ownership_proof() const;

+ 147 - 62
prsona/inc/networkClient.hpp

@@ -8,19 +8,153 @@
 #include "client.hpp"
 #include "networking.hpp"
 
-// #define BGN_TMP_FILE (TMP_DIR "bgn")
-// #define GEN_TMP_FILE (TMP_DIR "generator")
-// #define EPOCH_GEN_TMP_FILE (TMP_DIR "epoch")
+enum EventType {
+    CLIENT_MAKE_VOTE = 1,
+    CLIENT_MAKE_REP_PROOF
+};
+
+/* "PUBLIC" FUNCTIONS */
+
+// CREATOR FOR A NEW CLIENT
+PrsonaClient *create_client(
+    std::default_random_engine& rng,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    size_t numServers);
+
+// FUNCTIONS TO PERFORM OPERATIONS FOR EXPERIMENT
+void make_vote(
+    std::default_random_engine& rng,
+    PrsonaClient* prsonaClient,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& target,
+    int targetPort,
+    size_t numClients);
+
+bool make_reputation_proof(
+    std::default_random_engine& rng,
+    PrsonaClient* prsonaClient,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& target,
+    int targetPort,
+    size_t numClients);
+
+/* "PRIVATE" FUNCTIONS */
+
+// HELPERS TO ADD THIS CLIENT TO SERVERS
+void register_new_client(
+    std::default_random_engine& rng,
+    PrsonaClient *newUser,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const Proof& proofOfValidSTPK,
+    const Twistpoint& shortTermPublicKey);
+
+void verify_valid_addition(
+    std::default_random_engine& rng,
+    PrsonaClient *newUser,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::vector<Proof>& proofOfValidAddition,
+    const Twistpoint& shortTermPublicKey);
+
+// GETTERS FOR VARIOUS SERVER VALUES
+Twistpoint get_generator(
+    std::default_random_engine& rng,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    bool fresh,
+    std::vector<Proof>& pi);
+
+BGNPublicKey get_bgn_public_key(
+    std::default_random_engine& rng,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts);
+
+template <typename T>
+T get_server_committed_val(
+    std::default_random_engine& rng,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const char *firstUri,
+    const char *commitUri,
+    std::vector<Proof>& pi,
+    const Twistpoint& shortTermPublicKey);
+
+// HELPERS FOR GENERALIZED GETTER FUNCTION
+template <typename T>
+T get_first_committed_val(
+    std::default_random_engine& rng,
+    const std::string& server,
+    int serverPort,
+    const char *firstUri,
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey);
+
+void get_additional_commitment(
+    std::default_random_engine& rng,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& skipIP,
+    int skipPort,
+    const char *commitUri,
+    std::vector<Proof>& pi,
+    const Twistpoint& shortTermPublicKey);
+
+// FILE I/O HELPERS FOR ALL GETTERS
+std::vector<Proof> get_valid_addition_proof_from_file(
+    const char *filename);
+
+Twistpoint get_generator_from_file(
+    const char *filename,
+    std::vector<Proof>& pi);
+
+BGNPublicKey get_bgn_public_key_from_file(
+    const char *filename);
+
+template <typename T>
+T get_committed_val_from_file(
+    const char *filename,
+    Proof& pi);
+
+Proof get_commitment_from_file(
+    struct synchronization_tool *sync,
+    const char *filename);
+
+// GENERALIZED SENDER FOR ORCHESTRATOR-SIGNALED OPERATIONS
+char *send_item(
+    std::default_random_engine& rng,
+    const std::string& target,
+    int targetPort,
+    const char* whichUri,
+    const std::string& data,
+    bool responseExpected);
+
+// DATA SERIALIZERS
+std::string make_vote_string(
+    const std::vector<Proof>& pi,
+    const std::vector<TwistBipoint>& newVotes,
+    const Twistpoint& shortTermPublicKey);
+
+std::string make_rep_proof_string(
+    const std::vector<Proof>& pi,
+    const Twistpoint& shortTermPublicKey,
+    const Scalar& threshold);
+
+/* CLIENT-SPECIFIC HANDLER */
 
 class PrsonaClientWebSocketHandler : public CivetWebSocketHandler {
     public:
-        // CONSTRUCTORS
+        // CONSTRUCTOR
         PrsonaClientWebSocketHandler(
-            PrsonaClient *prsonaClient, 
+            std::default_random_engine& rng,
+            PrsonaClient *prsonaClient,
             const std::vector<std::string>& serverIPs,
-            const std::vector<int>& serverPorts,
-            std::default_random_engine *generator);
+            const std::vector<int>& serverPorts);
 
+        // REQUIRED BY INHERITED CLASS
         virtual bool handleConnection(
             CivetServer *server,
             const struct mg_connection *conn);
@@ -41,69 +175,20 @@ class PrsonaClientWebSocketHandler : public CivetWebSocketHandler {
             const struct mg_connection *conn);
 
     private:
+        std::default_random_engine &rng;
         PrsonaClient *prsonaClient;
         const std::vector<std::string> serverIPs;
         const std::vector<int> serverPorts;
-        std::default_random_engine *generator;
 
+        // RESPONSE ROUTER FUNCTION
         void generate_response(
             struct mg_connection *conn,
-            const char *filename) const;
+            const char *filename);
 
+        // REPUTATION PROOF RESPONSE
         void verify_reputation_proof(
             struct mg_connection *conn,
-            const char *filename) const;
+            const char *filename);
 };
 
-template <typename T>
-T get_committed_val_from_file(
-    struct synchronization_tool *sync,
-    const char *filename,
-    Proof& pi);
-
-template <typename T>
-T get_first_committed_val(
-    std::default_random_engine *rng,
-    const std::string& server,
-    int serverPort,
-    Proof& pi,
-    const Twistpoint& shortTermPublicKey,
-    const char *firstUri);
-
-Proof get_commitment_from_file(
-    struct synchronization_tool *sync,
-    char *filename);
-
-void get_additional_commitment(
-    std::default_random_engine *rng,
-    const std::vector<std::string>& serverIPs,
-    const std::vector<int>& serverPorts,
-    const std::string& skipIP,
-    int skipPort,
-    std::vector<Proof>& pi,
-    const Twistpoint& shortTermPublicKey,
-    char *which);
-
-template <typename T>
-T get_server_committed_val(
-    std::default_random_engine *generator,
-    const std::vector<std::string>& serverIPs,
-    const std::vector<int>& serverPorts,
-    std::vector<Proof>& pi,
-    const Twistpoint& shortTermPublicKey,
-    const char *firstUri,
-    const char *commitUri);
-
-Twistpoint get_generator_from_file(
-    struct synchronization_tool *sync,
-    const char *filename,
-    std::vector<Proof>& pi);
-
-Twistpoint get_generator(
-    std::default_random_engine *randomGenerator,
-    const std::vector<std::string>& serverIPs,
-    const std::vector<int>& serverPorts,
-    std::vector<Proof>& pi,
-    bool fresh);
-
-#endif
+#endif

+ 59 - 0
prsona/inc/networkOrchestrator.hpp

@@ -0,0 +1,59 @@
+#ifndef __PRSONA_NETWORK_ORCHESTRATOR_HPP
+#define __PRSONA_NETWORK_ORCHESTRATOR_HPP
+
+#include <string>
+#include <vector>
+#include <chrono>
+
+#include "networking.hpp"
+
+const std::chrono::seconds ONE_SECOND(1);
+
+/* "PUBLIC" FUNCTIONS */
+
+// START UP AND SHUT DOWN INSTANCES
+int start_remote_actor(
+    const std::string& target,
+    bool server,
+    const std::string& id,
+    const std::string& output,
+    bool maliciousServers);
+
+void shut_down_remote_actors(
+    const std::vector<std::string>& relevantIPs,
+    const std::vector<int>& relevantPorts);
+
+// SYNCHRONIZATION
+void wait_for_servers_ready(
+    std::string dealer,
+    int dealerPort);
+
+void wait_for_clients_ready(
+    std::string dealer,
+    int dealerPort,
+    size_t numClients);
+
+// RUN EXPERIMENT
+void execute_experiment(
+    std::string dealer,
+    int dealerPort,
+    const char *filename);
+
+/* "PRIVATE" FUNCTIONS */
+
+// TRIGGER EXPERIMENT EVENTS
+void trigger_epoch_change(
+    std::string dealer,
+    int dealerPort);
+
+void trigger_vote(
+    std::string target,
+    int targetPort);
+
+void trigger_reputation_proof(
+    std::string target,
+    int targetPort,
+    std::string verifier,
+    int verifierPort);
+
+#endif

+ 312 - 65
prsona/inc/networkServer.hpp

@@ -9,26 +9,163 @@
 #include "server.hpp"
 #include "networking.hpp"
 
-void obtain_update_locks(
-    std::unique_lock<std::mutex> &lck,
+/* "PUBLIC" FUNCTIONS */
+
+// CREATOR FOR A NEW SERVER
+PrsonaServer *create_server(
+    std::default_random_engine& rng,
+    std::string dealerIP,
+    int dealerPort,
+    bool bgnDealer,
+    size_t numServers);
+
+// CHECK IN FUNCTION USED FOR SYNCHRONIZATION IN SETUP
+void check_in_with_dealer(
+    std::string dealerIP,
+    int dealerPort);
+
+// INITIATER FOR SHARED GLOBAL VALUES
+void initiate_generators(
+    std::default_random_engine& rng,
+    PrsonaServer* prsonaServer,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort);
+
+// FUNCTION TO PERFORM OPERATIONS FOR EXPERIMENT
+void make_epoch(
+    std::default_random_engine& rng,
+    PrsonaServer *prsonaServer,
     const std::vector<std::string>& serverIPs,
     const std::vector<int>& serverPorts,
     const std::string& selfIP,
     int selfPort,
-    struct synchronization_tool *synch);
+    std::mutex& updateMtx,
+    std::atomic<size_t>& epochNum);
+
+/* "PRIVATE" FUNCTIONS */
+
+// SHARED GLOBAL UPDATE LOCK GETTERS AND RELEASERS
+void obtain_update_locks(
+    std::unique_lock<std::mutex> &updateLock,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort);
 
 void release_update_locks(
-    std::unique_lock<std::mutex> &lck,
+    std::unique_lock<std::mutex> &updateLock,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort);
+
+// GETTER FOR DEALER VALUE
+BGN get_bgn_private_key(
+    std::default_random_engine& rng,
+    std::string dealerIP,
+    int dealerPort);
+
+// HELPERS TO INITIATE SHARED GLOBAL VALUES
+Twistpoint make_generator(
+    std::default_random_engine& rng,
+    PrsonaServer *prsonaServer,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort,
+    bool fresh,
+    std::vector<Proof>& pi);
+
+void distribute_generator(
+    PrsonaServer *prsonaServer,
     const std::vector<std::string>& serverIPs,
     const std::vector<int>& serverPorts,
     const std::string& selfIP,
     int selfPort,
-    struct synchronization_tool *synch);
+    bool fresh,
+    const std::vector<Proof>& pi,
+    const Twistpoint& generator);
 
+// HELPERS FOR EPOCH CALCULATIONS
+std::vector<Proof> epoch_build_up(
+    std::default_random_engine& rng,
+    PrsonaServer *prsonaServer,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort,
+    Twistpoint& nextGenerator);
+
+void epoch_break_down(
+    std::default_random_engine& rng,
+    PrsonaServer *prsonaServer,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort,
+    const std::vector<Proof>& generatorProof,
+    const Twistpoint& nextGenerator);
+
+// HELPERS FOR EPOCH HELPERS
+Twistpoint initiate_epoch_updates(
+    std::default_random_engine& rng,
+    const std::string& recipient,
+    int recipientPort,
+    const std::string& data,
+    bool isBreakdown,
+    std::vector<std::vector<Proof>>& generatorProofHolder);
+
+struct mg_connection *distribute_epoch_updates(
+    const std::string& recipient,
+    int recipientPort,
+    const std::string& data,
+    struct synchronization_tool* sync);
+
+// SCORE TALLYING AND DISTRIBUTION HELPERS
+void tally_scores(
+    PrsonaServer *prsonaServer,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort,
+    const Twistpoint& nextGenerator,
+    std::vector<EGCiphertext>& userTallyScores,
+    std::vector<CurveBipoint>& serverTallyScores);
+
+void distribute_tallied_scores(
+    PrsonaServer *prsonaServer,
+    const std::vector<std::string>& serverIPs,
+    const std::vector<int>& serverPorts,
+    const std::string& selfIP,
+    int selfPort,
+    const Twistpoint& nextGenerator,
+    const std::vector<EGCiphertext>& userTallyScores,
+    const std::vector<CurveBipoint>& serverTallyScores);
+
+// FILE I/O HELPERS
+BGN get_bgn_private_key_from_file(
+    const char *filename);
+
+Twistpoint get_generator_from_file(
+    const char *filename,
+    Proof& pi);
+
+Twistpoint get_generator_from_file(
+    const char *filename,
+    std::vector<Proof>& pi);
+
+// EPOCH DATA SERIALIZERS/UN-SERIALIZERS
 std::string make_epoch_initiator_string(
     const std::vector<Proof>& generatorProof,
     const Twistpoint& nextGenerator);
 
+void read_epoch_initiator_string(
+    const char *filename,
+    std::vector<Proof>& generatorProof,
+    Twistpoint& nextGenerator);
+
 std::string make_epoch_update_string(
     const std::vector<std::vector<Proof>>& pi,
     const std::vector<std::vector<Twistpoint>>& permutationCommits,
@@ -43,122 +180,254 @@ std::string make_epoch_update_string(
     const Twistpoint& nextGenerator,
     bool doUserTallies);
 
-struct mg_connection *distribute_epoch_updates(
-    const std::string& recipient,
-    int recipientPort,
-    const std::string& data,
-    const struct synchronization_tool* sync);
+bool read_epoch_update_string(
+    const char *filename,
+    std::vector<std::vector<Proof>>& pi,
+    std::vector<std::vector<Twistpoint>>& permutationCommits,
+    std::vector<std::vector<Twistpoint>>& freshPseudonymCommits,
+    std::vector<std::vector<Twistpoint>>& freshPseudonymSeedCommits,
+    std::vector<std::vector<CurveBipoint>>& serverTallyCommits,
+    std::vector<std::vector<std::vector<TwistBipoint>>>& partwayVoteMatrixCommits,
+    std::vector<std::vector<std::vector<TwistBipoint>>>& finalVoteMatrixCommits,
+    std::vector<std::vector<Twistpoint>>& userTallyMaskCommits,
+    std::vector<std::vector<Twistpoint>>& userTallyMessageCommits,
+    std::vector<std::vector<Twistpoint>>& userTallySeedCommits,
+    Twistpoint& nextGenerator);
+
+/* OTHER SERVER-RELEVANT HANDLERS */
+
+// Used to tell orchestrator when the system is ready to do an epoch change
+class EpochReadyHandler : public CivetHandler
+{
+    public:
+        EpochReadyHandler(
+            struct synchronization_tool *exitSync,
+            struct synchronization_tool *readySync,
+            std::mutex& updateMtx,
+            size_t numServers);
+
+        bool handleGet(
+            CivetServer *server,
+            struct mg_connection *conn);
+
+    private:
+        struct synchronization_tool *exitSync, *readySync;
+        std::mutex& updateMtx;
+        const size_t numServers;
+
+};
+
+// Used to indicate which epoch the system is currently in
+class EpochNumHandler : public CivetHandler
+{
+    public:
+        EpochNumHandler(
+            std::atomic<size_t>& epochNum);
+        
+        bool handleGet(
+            CivetServer *server,
+            struct mg_connection *conn);
+
+    private:
+        std::atomic<size_t>& epochNum;
+};
+
+// Used to take and release update locks
+class UpdateLockWebSocketHandler : public CivetWebSocketHandler
+{
+    public:
+        UpdateLockWebSocketHandler(
+            std::mutex& updateMtx,
+            std::unique_lock<std::mutex> **lockHolder,
+            bool isLocking);
+
+        ~UpdateLockWebSocketHandler();
+
+        bool handleConnection(
+            CivetServer *server,
+            const struct mg_connection *conn);
+
+        void handleReadyState(
+            CivetServer *server,
+            struct mg_connection *conn);
+
+        bool handleData(
+            CivetServer *server,
+            struct mg_connection *conn,
+            int bits,
+            char *data,
+            size_t data_len);
+
+        void handleClose(
+            CivetServer *server,
+            const struct mg_connection *conn);
+
+    private:
+        std::mutex& updateMtx;
+        std::unique_lock<std::mutex> **lockHolder;
+        const bool isLocking;
+};
+
+/* SERVER-SPECIFIC HANDLER */
 
 class PrsonaServerWebSocketHandler : public CivetWebSocketHandler  {
     public:
         // CONSTRUCTORS
         PrsonaServerWebSocketHandler(
+            std::default_random_engine& rng,
             PrsonaServer *prsonaServer,
-            std::default_random_engine *rng, 
-            std::mutex *updateMtx,
-            std::atomic<size_t> *epochNum,
             const std::vector<std::string>& serverIPs,
             const std::vector<int>& serverPorts,
             const std::string& selfIP,
-            int selfPort);
+            int selfPort,
+            std::mutex& updateMtx,
+            std::atomic<size_t>& epochNum);
 
-        virtual bool handleConnection(
+        // REQUIRED BY INHERITED CLASS
+        bool handleConnection(
             CivetServer *server,
             const struct mg_connection *conn);
 
-        virtual void handleReadyState(
+        void handleReadyState(
             CivetServer *server,
             struct mg_connection *conn);
 
-        virtual bool handleData(
+        bool handleData(
             CivetServer *server,
             struct mg_connection *conn,
             int bits,
             char *data,
             size_t data_len);
 
-        virtual void handleClose(
+        void handleClose(
             CivetServer *server,
             const struct mg_connection *conn);
 
     private:
+        std::default_random_engine& rng;
         PrsonaServer *prsonaServer;
-        std::default_random_engine *rng;
-        std::mutex *updateMtx;
-        std::atomic<size_t> *epochNum;
         const std::vector<std::string> serverIPs;
         const std::vector<int> serverPorts;
         const std::string selfIP;
         const int selfPort;
+        std::mutex& updateMtx;
+        std::atomic<size_t>& epochNum;
 
         struct synchronization_tool updateSynch, distributeSynch;
 
+        // RESPONSE ROUTER FUNCTION
         void generate_response(
             struct mg_connection *conn,
             const char *filename);
 
         // BASIC PUBLIC SYSTEM INFO GETTERS
-        void get_bgn_public_key(struct mg_connection *c) const;
-        void get_num_clients(struct mg_connection *c) const;
-        void get_num_servers(struct mg_connection *c) const;
+        void get_bgn_public_key(
+            struct mg_connection *conn
+        ) const;
+
+        void get_num_clients(
+            struct mg_connection *conn
+        ) const;
+
+        void get_num_servers(
+            struct mg_connection *conn
+        ) const;
+
+        void get_generator(
+            struct mg_connection *conn,
+            bool fresh
+        );
 
         // ENCRYPTED DATA GETTERS
         void get_current_votes_by(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         ) const;
+
         void get_all_current_votes(
-            struct mg_connection *c
+            struct mg_connection *conn
         ) const;
+
         void get_current_user_encrypted_tally(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         ) const;
+
         void get_current_server_encrypted_tally(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         ) const;
+
         void get_current_pseudonyms(
-            struct mg_connection *c
+            struct mg_connection *conn
         ) const;
 
         // PROOF COMMITMENT GETTERS
         void get_vote_row_commitment(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         ) const;
+
         void get_vote_matrix_commitment(
-            struct mg_connection *c
+            struct mg_connection *conn
         ) const;
+
         void get_user_tally_commitment(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         ) const;
+
         void get_server_tally_commitment(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         ) const;
+
         void get_pseudonyms_commitment(
-            struct mg_connection *c
+            struct mg_connection *conn
         ) const;
 
         // CLIENT INTERACTIONS
         void add_new_client(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         );
+
         void receive_vote(
-            struct mg_connection *c,
+            struct mg_connection *conn,
+            const char *filename
+        );
+
+        // DISTRIBUTION HELPERS FOR CLIENT INTERACTIONS
+        void distribute_new_user_updates(
+            const std::vector<Proof>& proofOfValidAddition,
+            const std::vector<CurveBipoint>& previousVoteTallies,
+            const std::vector<Twistpoint>& currentPseudonyms,
+            const std::vector<EGCiphertext>& currentUserEncryptedTallies,
+            const std::vector<std::vector<TwistBipoint>>& voteMatrix
+        ) const;
+
+        void distribute_new_vote(
+            std::vector<Proof> pi,
+            std::vector<TwistBipoint> newVotes,
+            Twistpoint shortTermPublicKey
+        ) const;
+
+        void import_new_user_update(
+            struct mg_connection *conn,
             const char *filename
         );
 
         // CONSTRUCTOR HELPERS
-        void get_bgn_details(struct mg_connection *c) const;
+        void get_bgn_details(
+            struct mg_connection *conn
+        ) const;
+
         void add_seed_to_generator(
             struct mg_connection *conn,
             const char *filename,
             bool fresh
         ) const;
+
         void set_generator(
             const char *filename,
             bool fresh
@@ -166,38 +435,21 @@ class PrsonaServerWebSocketHandler : public CivetWebSocketHandler  {
 
         // EPOCH ROUNDS
         void build_up_midway_pseudonyms(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         );
+
         void break_down_midway_pseudonyms(
-            struct mg_connection *c,
-            const char *filename
-        );
-        void accept_epoch_updates(
-            struct mg_connection *c,
+            struct mg_connection *conn,
             const char *filename
         );
 
-        // DATA MAINTENANCE
-        void import_new_user_update(
+        void accept_epoch_updates(
             struct mg_connection *conn,
             const char *filename
         );
 
-        void distribute_new_user_updates(
-            const std::vector<Proof>& proofOfValidAddition,
-            const std::vector<CurveBipoint>& previousVoteTallies,
-            const std::vector<Twistpoint>& currentPseudonyms,
-            const std::vector<EGCiphertext>& currentUserEncryptedTallies,
-            const std::vector<std::vector<TwistBipoint>>& voteMatrix
-        ) const;
-
-        void distribute_new_vote(
-            std::vector<Proof> pi,
-            std::vector<TwistBipoint> newVotes,
-            Twistpoint shortTermPublicKey
-        ) const;
-
+        // DISTRIBUTED BGN
         void get_partial_decryption(
             struct mg_connection *conn
         ) const;
@@ -206,11 +458,6 @@ class PrsonaServerWebSocketHandler : public CivetWebSocketHandler  {
             struct mg_connection *conn,
             const char *filename
         );
-
-        void get_generator(
-            struct mg_connection *conn,
-            bool fresh
-        );
 }; 
 
-#endif
+#endif

+ 224 - 106
prsona/inc/networking.hpp

@@ -3,143 +3,193 @@
 
 #include <mutex>
 #include <condition_variable>
+#include <random>
 #include <string>
 
 #include "CivetServer.h"
+#include "Scalar.hpp"
+#include "base.hpp"
 
 #define MG_WEBSOCKET_OPCODE_DATACOMPLETE 0xb
-#define PRSONA_PORT_STR "8080"
+#define DEFAULT_PRSONA_PORT_STR "8080"
 #define USE_SSL 0
 
-#define EXIT_URI "/exit"
-#define SERVER_READY_URI "/init"
-#define EPOCH_READY_URI "/ready"
-#define TRIGGER_EPOCH_URI "/epoch"
-#define TRIGGER_VOTE_URI "/vote"
-#define TRIGGER_REP_URI "/rep"
-#define WHICH_EPOCH_URI "/which"
-#define UPDATE_LOCK_URI "/lock"
-#define UPDATE_UNLOCK_URI "/unlock"
-
-#define ADD_USER_URI "/ws?a"
-#define GIVE_NEW_VOTE_URI "/ws?b"
-#define PUBLIC_BGN_URI "/ws?c"
-#define NUM_CLIENTS_URI "/ws?d"
-#define VOTES_BY_URI "/ws?f"
-#define VOTE_MATRIX_URI "/ws?g"
-#define USER_TALLY_URI "/ws?h"
-#define SERVER_TALLY_URI "/ws?i"
-#define PSEUDONYMS_URI "/ws?j"
-#define VOTES_BY_COMMIT_URI "/ws?k"
-#define VOTE_MATRIX_COMMIT_URI "/ws?l"
-#define USER_TALLY_COMMIT_URI "/ws?m"
-#define SERVER_TALLY_COMMIT_URI "/ws?n"
-#define PSEUDONYMS_COMMIT_URI "/ws?o"
-#define PRIVATE_BGN_URI "/ws?p"
-#define GET_FRESH_GEN_URI "/ws?q"
-#define GIVE_FRESH_GEN_URI "/ws?r"
-#define GET_BLIND_GEN_URI "/ws?s"
-#define GIVE_BLIND_GEN_URI "/ws?t"
-#define EPOCH_BUILD_UP_URI "/ws?u"
-#define EPOCH_BREAK_DOWN_URI "/ws?v"
-#define ACCEPT_EPOCH_UPDATES_URI "/ws?w"
-#define GIVE_NEW_USER_URI "/ws?x"
-#define GET_DECRYPTION_URI "/ws?y"
-#define GIVE_DECRYPTION_URI "/ws?z"
-#define FRESH_GEN_URI "/ws?-"
-#define BLIND_GEN_URI "/ws?_"
-#define REP_PROOF_URI "/ws?d"
-
 #define TMP_FILE_SIZE 12
 #define TMP_DIR "./tmp/"
 #define TMP_DIR_SIZE 6
 
 enum RequestType {
-    PRSONA_ADD_CLIENT = 'a',
+    /* SERVER PUBLIC FUNCTIONS */
+
+    // BASIC PUBLIC SYSTEM INFO GETTERS
+    PRSONA_GIVE_BGN_PUBKEY = 'a',
+    PRSONA_GIVE_NUM_CLIENTS,
+    PRSONA_GIVE_NUM_SERVERS,
+    PRSONA_GIVE_EG_BLIND_GENERATOR,
+    PRSONA_GIVE_FRESH_GENERATOR,
+
+    // ENCRYPTED DATA GETTERS
+    PRSONA_GIVE_VOTE_ROW,
+    PRSONA_GIVE_VOTE_MATRIX,
+    PRSONA_GIVE_CLIENT_TALLY,
+    PRSONA_GIVE_SERVER_TALLY,
+    PRSONA_GIVE_PSEUDONYMS,
+
+    // PROOF COMMITMENT GETTERS
+    PRSONA_GIVE_VOTE_ROW_COMMITMENT,
+    PRSONA_GIVE_VOTE_MATRIX_COMMITMENT,
+    PRSONA_GIVE_CLIENT_TALLY_COMMITMENT,
+    PRSONA_GIVE_SERVER_TALLY_COMMITMENT,
+    PRSONA_GIVE_PSEUDONYMS_COMMITMENT,
+
+    // CLIENT INTERACTIONS
+    PRSONA_RECEIVE_NEW_CLIENT,
     PRSONA_RECEIVE_VOTE,
-    PRSONA_GET_BGN_PUBKEY,
-    PRSONA_GET_NUM_CLIENTS,
-    PRSONA_GET_NUM_SERVERS,
-    PRSONA_GET_VOTES_BY,
-    PRSONA_GET_ALL_VOTES,
-    PRSONA_GET_USER_TALLY,
-    PRSONA_GET_SERVER_TALLY,
-    PRSONA_GET_PSEUDONYMS,
-    PRSONA_GET_VOTE_ROW_COMMITMENT,
-    PRSONA_GET_VOTE_MATRIX_COMMITMENT,
-    PRSONA_GET_USER_TALLY_COMMITMENT,
-    PRSONA_GET_SERVER_TALLY_COMMITMENT,
-    PRSONA_GET_PSEUDONYMS_COMMITMENT,
-    PRSONA_GET_BGN_DETAILS,
+
+    /* SERVER PRIVATE FUNCTIONS */
+
+    // CLIENT INTERACTION HELPER
+    PRSONA_RECEIVE_UPDATE_WITH_NEW_USER,
+
+    // CONSTRUCTOR HELPERS
+    PRSONA_GIVE_BGN_PRIVKEY,
     PRSONA_ADD_CURR_SEED_TO_GENERATOR,
-    PRSONA_SET_FRESH_GENERATOR,
+    PRSONA_RECEIVE_FRESH_GENERATOR,
     PRSONA_ADD_RAND_SEED_TO_GENERATOR,
-    PRSONA_SET_EG_BLIND_GENERATOR,
-    PRSONA_EPOCH_BUILD_UP,
-    PRSONA_EPOCH_BREAK_DOWN,
-    PRSONA_EPOCH_UPDATE,
-    PRSONA_NEW_USER_UPDATE,
-    PRSONA_GET_PARTIAL_DECRYPTION,
-    PRSONA_RECEIVE_PARTIAL_DECRYPTION,
-    PRSONA_GET_FRESH_GENERATOR = '-',
-    PRSONA_GET_EG_BLIND_GENERATOR ='_',
-    PRSONA_RECEIVE_FRESH_GENERATOR = 'a',
-    PRSONA_RECEIVE_VOTE_TALLY,
-    PRSONA_RECEIVE_NEW_USER_DATA,
-    PRSONA_VERIFY_REPUTATION_PROOF
+    PRSONA_RECEIVE_EG_BLIND_GENERATOR,
+
+    // EPOCH ROUNDS
+    PRSONA_PERFORM_EPOCH_BUILD_UP,
+    PRSONA_PERFORM_EPOCH_BREAK_DOWN,
+    PRSONA_RECEIVE_EPOCH_UPDATE,
+
+    // DISTRIBUTED BGN
+    PRSONA_GIVE_PARTIAL_DECRYPTION = '-',
+    PRSONA_RECEIVE_PARTIAL_DECRYPTION = '_',
+    
+    /* CLIENT PUBLIC FUNCTIONS */
+
+    // REPUTATION PROOF VERIFIER
+    PRSONA_VERIFY_REPUTATION_PROOF = 'a'
 };
 
+/* SERVER PUBLIC FUNCTIONS */
+
+// BASIC PUBLIC SYSTEM INFO GETTERS
+#define REQUEST_BGN_PUBKEY_URI "/ws?a"
+#define REQUEST_NUM_CLIENTS_URI "/ws?b"
+#define REQUEST_NUM_SERVERS_URI "/ws?c"
+#define REQUEST_EG_BLIND_GENERATOR_URI "/ws?d"
+#define REQUEST_FRESH_GENERATOR_URI "/ws?e"
+
+// ENCRYPTED DATA GETTERS
+#define REQUEST_VOTE_ROW_URI "/ws?f"
+#define REQUEST_VOTE_MATRIX_URI "/ws?g"
+#define REQUEST_CLIENT_TALLY_URI "/ws?h"
+#define REQUEST_SERVER_TALLY_URI "/ws?i"
+#define REQUEST_PSEUDONYMS_URI "/ws?j"
+
+// PROOF COMMITMENT GETTERS
+#define REQUEST_VOTE_ROW_COMMITMENT_URI "/ws?k"
+#define REQUEST_VOTE_MATRIX_COMMITMENT_URI "/ws?l"
+#define REQUEST_CLIENT_TALLY_COMMITMENT_URI "/ws?m"
+#define REQUEST_SERVER_TALLY_COMMITMENT_URI "/ws?n"
+#define REQUEST_PSEUDONYMS_COMMITMENT_URI "/ws?o"
+
+// CLIENT INTERACTIONS
+#define SUBMIT_NEW_CLIENT_URI "/ws?p"
+#define SUBMIT_VOTE_URI "/ws?q"
+
+/* SERVER PRIVATE FUNCTIONS */
+
+// CLIENT INTERACTION HELPER
+#define SUBMIT_UPDATE_WITH_NEW_USER_URI "/ws?r"
+
+// CONSTRUCTOR HELPERS
+#define REQUEST_BGN_PRIVKEY_URI "/ws?s"
+#define REQUEST_ADD_CURR_SEED_FOR_FRESH_GENERATOR_URI "/ws?t"
+#define SUBMIT_FRESH_GENERATOR_URI "/ws?u"
+#define REQUEST_ADD_RAND_SEED_FOR_EG_BLIND_GENERATOR_URI "/ws?v"
+#define SUBMIT_EG_BLIND_GENERATOR_URI "/ws?w"
+
+// EPOCH ROUNDS
+#define REQUEST_EPOCH_BUILD_UP_URI "/ws?x"
+#define REQUEST_EPOCH_BREAK_DOWN_URI "/ws?y"
+#define SUBMIT_EPOCH_UPDATES_URI "/ws?z"
+
+// DISTRIBUTED BGN
+#define REQUEST_PARTIAL_DECRYPTION_URI "/ws?-"
+#define SUBMIT_PARTIAL_DECRYPTION_URI "/ws?_"
+
+/* CLIENT PUBLIC FUNCTIONS */
+
+// REPUTATION PROOF VERIFIER
+#define VERIFY_REPUTATION_PROOF_URI "/ws?a"
+
+/* SYNCHRONIZATION HANDLERS */
+
+// ORCHESTRATOR-SERVER/CLIENT SYNCHRONIZATION
+#define EXIT_URI "/exit"
+
+// INTER-SERVER SYNCHRONIZATION
+#define SERVER_CHECK_IN_URI "/init"
+#define UPDATE_LOCK_URI "/lock"
+#define UPDATE_UNLOCK_URI "/unlock"
+
+// SERVER-ORCHESTRATOR SYNCHRONIZATION
+#define EPOCH_READY_URI "/ready"
+
+// SERVER EXPERIMENT TRIGGER
+#define TRIGGER_EPOCH_URI "/epoch"
+
+// CLIENT EXPERIMENT TRIGGERS
+#define TRIGGER_VOTE_URI "/vote"
+#define TRIGGER_REP_URI "/rep"
+
+// SERVER-CLIENT SYNCHRONIZATION
+#define WHICH_EPOCH_URI "/which"
+
+/* Helper types */
+
+// Struct to help synchronization efforts across multi-threaded websocket usage
 struct synchronization_tool {
     std::mutex mtx;
     std::condition_variable cv;
     size_t val, val2;
 };
 
-std::string random_string(std::default_random_engine& generator, size_t length);
-char *set_temp_filename(std::default_random_engine& generator, struct mg_connection *conn);
+/* "PUBLIC" GENERIC HELPER FUNCTIONS */
 
-class RemoteControlHandler : public CivetHandler
-{
-    public:
-        RemoteControlHandler(struct synchronization_tool *sync)
-            : sync(sync)
-        { /* */ }
+// This function must be run once before any PRSONA objects are created or used
+void initialize_prsona_classes();
 
-        RemoteControlHandler(struct synchronization_tool *sync,
-            const std::string& message)
-            : sync(sync), message(message)
-        { /* */ }
+// Makes a random temp filename to be used in receiving serialized data
+char *set_temp_filename(
+    std::default_random_engine& rng,
+    struct mg_connection *conn);
 
-        bool handleGet(CivetServer *server, struct mg_connection *conn);
+// Loads config information
+void load_multiple_instances_config(
+    std::vector<std::string>& relevantIPs,
+    std::vector<int>& relevantPorts,
+    const char *filename);
 
-    private:
-        struct synchronization_tool *sync;
-        const std::string message;
-};
+void load_single_instance_config(
+    std::string& relevantIP,
+    std::string& relevantPortStr,
+    int& relevantPort,
+    const char *filename);
 
-class AltRemoteControlHandler : public CivetHandler
-{
-    public:
-        AltRemoteControlHandler(size_t value, struct synchronization_tool *sync)
-            : value(value), sync(sync)
-        { /* */ }
+/* "PRIVATE" FUNCTIONS TO HELP THE GENERIC HELPERS */
 
-        AltRemoteControlHandler(size_t value,
-            struct synchronization_tool *sync,
-            const std::string& message)
-            : value(value), sync(sync), message(message)
-        { /* */ }
+// Helper for set_temp_filename()
+std::string random_string(
+    std::default_random_engine& rng,
+    size_t length);
 
-        bool handleGet(CivetServer *server, struct mg_connection *conn);
-        std::string getQuery() const;
-
-    private:
-        const size_t value;
-        struct synchronization_tool *sync;
-        const std::string message;
-        std::string query;
-};
+/* WEBSOCKET HANDLER FUNCTIONS */
 
+// NULL
 int empty_websocket_data_handler(
     struct mg_connection *conn,
     int bits,
@@ -151,6 +201,7 @@ void empty_websocket_close_handler(
     const struct mg_connection *conn,
     void *user_data);
 
+// SYNCHRONIZATION ONLY
 int synchro_websocket_data_handler(
     struct mg_connection *conn,
     int bits,
@@ -162,6 +213,7 @@ void synchro_websocket_close_handler(
     const struct mg_connection *conn,
     void *user_data);
 
+// RECEIVE SERIALIZED DATA ONLY
 int file_websocket_data_handler(
     struct mg_connection *conn,
     int bits,
@@ -173,4 +225,70 @@ void file_websocket_close_handler(
     const struct mg_connection *conn,
     void *user_data);
 
+// SYNCHRONIZATION AND RECEIVE SERIALIZED DATA
+int epoch_websocket_data_handler(
+    struct mg_connection *conn,
+    int bits,
+    char *data,
+    size_t data_len,
+    void *user_data);
+
+void epoch_websocket_close_handler(
+    const struct mg_connection *conn,
+    void *user_data);
+
+// SPECIAL FOR HANDLING UNUSUAL DATA
+
+int clients_websocket_data_handler(
+    struct mg_connection *conn,
+    int bits,
+    char *data,
+    size_t data_len,
+    void *user_data);
+
+/* GENERIC HANDLERS */
+
+// Used for signaling exits
+class RemoteControlHandler : public CivetHandler
+{
+    public:
+        RemoteControlHandler(
+            struct synchronization_tool *sync);
+
+        RemoteControlHandler(
+            struct synchronization_tool *sync,
+            const std::string& message);
+
+        bool handleGet(
+            CivetServer *server,
+            struct mg_connection *conn);
+
+    private:
+        struct synchronization_tool *sync;
+        const std::string message;
+};
+
+// Used for signaling events (making votes, epoch changes, etc.)
+class AltRemoteControlHandler : public CivetHandler
+{
+    public:
+        AltRemoteControlHandler(
+            size_t value,
+            struct synchronization_tool *sync);
+
+        AltRemoteControlHandler(size_t value,
+            struct synchronization_tool *sync,
+            const std::string& message);
+
+        bool handleGet(CivetServer *server, struct mg_connection *conn);
+
+        std::string getQuery() const;
+
+    private:
+        const size_t value;
+        struct synchronization_tool *sync;
+        const std::string message;
+        std::string query;
+};
+
 #endif

+ 13 - 6
prsona/inc/proof.hpp

@@ -17,8 +17,9 @@ class Proof {
         Proof()
         { /* */ }
 
-        Proof(std::string hbc)
-            : hbc(hbc)
+        Proof(
+            std::string hbc)
+        : hbc(hbc)
         { /* */ }
 
         void clear();
@@ -32,13 +33,19 @@ class Proof {
         std::vector<Scalar> challengeParts;
         std::vector<Scalar> responseParts;
 
-        bool operator==(const Proof& b);
+        bool operator==(
+            const Proof& b);
 
-        friend std::ostream& operator<<(std::ostream& os, const Proof& output);
-        friend std::istream& operator>>(std::istream& is, Proof& input);
+        friend std::ostream& operator<<(
+            std::ostream& os,
+            const Proof& output);
+        friend std::istream& operator>>(
+            std::istream& is,
+            Proof& input);
 };
 
-Scalar oracle(const std::string& input);
+Scalar oracle(
+    const std::string& input);
 
 #endif
 

+ 42 - 16
prsona/inc/server.hpp

@@ -14,44 +14,67 @@
 class PrsonaServer : public PrsonaBase {
     public:
         // CONSTRUCTORS
-        PrsonaServer(size_t numServers);
-        PrsonaServer(size_t numServers, const BGN& other_bgn);
+        PrsonaServer(
+            size_t numServers);
+        PrsonaServer(
+            size_t numServers,
+            const BGN& other_bgn);
 
         // BASIC PUBLIC SYSTEM INFO GETTERS
         BGNPublicKey get_bgn_public_key() const;
         size_t get_num_clients() const;
         size_t get_num_servers() const;
 
-        Twistpoint get_fresh_generator(std::vector<Proof>& pi) const;
+        Twistpoint get_fresh_generator(
+            std::vector<Proof>& pi
+        ) const;
         
         // FRESH GENERATOR CALCULATION
         Twistpoint add_curr_seed_to_generator(
             std::vector<Proof>& pi,
-            const Twistpoint& currGenerator) const;
+            const Twistpoint& currGenerator
+        ) const;
         Twistpoint add_next_seed_to_generator(
             std::vector<Proof>& pi,
-            const Twistpoint& currGenerator) const;
+            const Twistpoint& currGenerator
+        ) const;
         Twistpoint add_rand_seed_to_generator(
             std::vector<Proof>& pi,
-            const Twistpoint& currGenerator) const;
+            const Twistpoint& currGenerator
+        ) const;
 
         // ENCRYPTED DATA GETTERS
         std::vector<TwistBipoint> get_current_votes_by(
-            Proof& pi, const Twistpoint& shortTermPublicKey) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey
+        ) const;
         std::vector<std::vector<TwistBipoint>> get_all_current_votes(
-            Proof& pi) const;
+            Proof& pi
+        ) const;
         EGCiphertext get_current_user_encrypted_tally(
-            Proof& pi, const Twistpoint& shortTermPublicKey) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey
+        ) const;
         CurveBipoint get_current_server_encrypted_tally(
-            Proof& pi, const Twistpoint& shortTermPublicKey) const;
-        std::vector<Twistpoint> get_current_pseudonyms(Proof& pi) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey
+        ) const;
+        std::vector<Twistpoint> get_current_pseudonyms(
+            Proof& pi
+        ) const;
         std::vector<Twistpoint> get_current_pseudonyms() const;
 
         // PROOF COMMITMENT GETTERS
-        Proof get_vote_row_commitment(const Twistpoint& request) const;
+        Proof get_vote_row_commitment(
+            const Twistpoint& request
+        ) const;
         Proof get_vote_matrix_commitment() const;
-        Proof get_user_tally_commitment(const Twistpoint& request) const;
-        Proof get_server_tally_commitment(const Twistpoint& request) const;
+        Proof get_user_tally_commitment(
+            const Twistpoint& request
+        ) const;
+        Proof get_server_tally_commitment(
+            const Twistpoint& request
+        ) const;
         Proof get_pseudonyms_commitment() const;
         void print_current_commitments() const;
 
@@ -65,7 +88,8 @@ class PrsonaServer : public PrsonaBase {
             const std::vector<TwistBipoint>& newVotes,
             const Twistpoint& shortTermPublicKey);
 
-        void print_scores(const std::vector<CurveBipoint>& scores);
+        void print_scores(
+            const std::vector<CurveBipoint>& scores);
 
         // CONSTRUCTOR HELPERS
         const BGN& get_bgn_details() const;
@@ -310,7 +334,9 @@ class PrsonaServer : public PrsonaBase {
         std::vector<size_t> order_data();
         
         // BINARY SEARCH
-        size_t binary_search(const Twistpoint& index) const;
+        size_t binary_search(
+            const Twistpoint& index
+        ) const;
 
         // VALID VOTE PROOFS
         bool verify_vote_proof(

+ 104 - 35
prsona/inc/serverEntity.hpp

@@ -14,78 +14,132 @@ class PrsonaServerEntity {
 
         // BASIC PUBLIC SYSTEM INFO GETTERS
         BGNPublicKey get_bgn_public_key() const;
-        BGNPublicKey get_bgn_public_key(size_t which) const;
+        BGNPublicKey get_bgn_public_key(
+            size_t which
+        ) const;
         Twistpoint get_blinding_generator() const;
-        Twistpoint get_blinding_generator(size_t which) const;
-        Twistpoint get_blinding_generator(std::vector<Proof>& pi) const;
         Twistpoint get_blinding_generator(
-            std::vector<Proof>& pi, size_t which) const;
+            size_t which
+        ) const;
+        Twistpoint get_blinding_generator(
+            std::vector<Proof>& pi
+        ) const;
+        Twistpoint get_blinding_generator(
+            std::vector<Proof>& pi,
+            size_t which
+        ) const;
         Twistpoint get_fresh_generator() const;
-        Twistpoint get_fresh_generator(size_t which) const;
-        Twistpoint get_fresh_generator(std::vector<Proof>& pi) const;
         Twistpoint get_fresh_generator(
-            std::vector<Proof>& pi, size_t which) const;
+            size_t which
+        ) const;
+        Twistpoint get_fresh_generator(
+            std::vector<Proof>& pi
+        ) const;
+        Twistpoint get_fresh_generator(
+            std::vector<Proof>& pi,
+            size_t which
+        ) const;
         size_t get_num_clients() const;
-        size_t get_num_clients(size_t which) const;
+        size_t get_num_clients(
+            size_t which
+        ) const;
         size_t get_num_servers() const;
-        size_t get_num_servers(size_t which) const;
+        size_t get_num_servers(
+            size_t which
+        ) const;
 
         // ENCRYPTED DATA GETTERS
         std::vector<TwistBipoint> get_current_votes_by(
-            Proof& pi, const Twistpoint& shortTermPublicKey) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey
+        ) const;
         std::vector<TwistBipoint> get_current_votes_by(
-            Proof& pi, const Twistpoint& shortTermPublicKey, size_t which) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey,
+            size_t which
+        ) const;
         std::vector<std::vector<TwistBipoint>> get_all_current_votes(
-            Proof& pi) const;
+            Proof& pi
+        ) const;
         std::vector<std::vector<TwistBipoint>> get_all_current_votes(
-            Proof& pi, size_t which) const;
+            Proof& pi,
+            size_t which
+        ) const;
         EGCiphertext get_current_user_encrypted_tally(
-            Proof& pi, const Twistpoint& shortTermPublicKey) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey
+        ) const;
         EGCiphertext get_current_user_encrypted_tally(
-            Proof& pi, const Twistpoint& shortTermPublicKey, size_t which) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey,
+            size_t which
+        ) const;
         CurveBipoint get_current_server_encrypted_tally(
-            Proof& pi, const Twistpoint& shortTermPublicKey) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey
+        ) const;
         CurveBipoint get_current_server_encrypted_tally(
-            Proof& pi, const Twistpoint& shortTermPublicKey, size_t which) const;
+            Proof& pi,
+            const Twistpoint& shortTermPublicKey,
+            size_t which
+        ) const;
         std::vector<Twistpoint> get_current_pseudonyms(
-            Proof& pi) const;
+            Proof& pi
+        ) const;
         std::vector<Twistpoint> get_current_pseudonyms(
-            Proof& pi, size_t which) const;
+            Proof& pi,
+            size_t which
+        ) const;
 
         // PROOF COMMITMENT GETTERS
         void get_other_vote_row_commitments(
-            std::vector<Proof>& pi, const Twistpoint& request) const;
+            std::vector<Proof>& pi,
+            const Twistpoint& request
+        ) const;
         void get_other_vote_row_commitments(
             std::vector<Proof>& pi,
             const Twistpoint& request,
             size_t whichNot
         ) const;
         void get_other_vote_matrix_commitments(
-            std::vector<Proof>& pi) const;
+            std::vector<Proof>& pi
+        ) const;
         void get_other_vote_matrix_commitments(
-            std::vector<Proof>& pi, size_t whichNot) const;
+            std::vector<Proof>& pi,
+            size_t whichNot
+        ) const;
         void get_other_user_tally_commitments(
-            std::vector<Proof>& pi, const Twistpoint& request) const;
+            std::vector<Proof>& pi,
+            const Twistpoint& request
+        ) const;
         void get_other_user_tally_commitments(
             std::vector<Proof>& pi,
             const Twistpoint& request,
             size_t whichNot
         ) const;
         void get_other_server_tally_commitments(
-            std::vector<Proof>& pi, const Twistpoint& request) const;
+            std::vector<Proof>& pi,
+            const Twistpoint& request
+        ) const;
         void get_other_server_tally_commitments(
             std::vector<Proof>& pi,
             const Twistpoint& request,
             size_t whichNot)
         const;
         void get_other_pseudonyms_commitments(
-            std::vector<Proof>& pi) const;
+            std::vector<Proof>& pi
+        ) const;
         void get_other_pseudonyms_commitments(
-            std::vector<Proof>& pi, size_t whichNot) const;
+            std::vector<Proof>& pi,
+            size_t whichNot
+        ) const;
 
         // CLIENT INTERACTIONS
-        void add_new_client(PrsonaClient& newUser);
-        void add_new_client(PrsonaClient& newUser, size_t which);
+        void add_new_client(
+            PrsonaClient& newUser);
+        void add_new_client(
+            PrsonaClient& newUser,
+            size_t which);
         bool receive_vote(
             const std::vector<Proof>& pi,
             const std::vector<TwistBipoint>& newVotes,
@@ -96,19 +150,32 @@ class PrsonaServerEntity {
             const Twistpoint& shortTermPublicKey,
             size_t which);
         void transmit_new_user_data(
-            const std::vector<Proof>& pi, PrsonaClient& newUser) const;
+            const std::vector<Proof>& pi,
+            PrsonaClient& newUser
+        ) const;
         void transmit_new_user_data(
-            const std::vector<Proof>& pi, PrsonaClient& newUser, size_t which) const;
-        void transmit_updates(PrsonaClient& currUser) const;
-        void transmit_updates(PrsonaClient& currUser, size_t which) const;
+            const std::vector<Proof>& pi,
+            PrsonaClient& newUser,
+            size_t which
+        ) const;
+        void transmit_updates(
+            PrsonaClient& currUser
+        ) const;
+        void transmit_updates(
+            PrsonaClient& currUser,
+            size_t which
+        ) const;
 
         // EPOCH
         void epoch();
-        void epoch(size_t which);
+        void epoch(
+            size_t which);
 
         void print_scores() const;
         void print_votes() const;
-        void print_current_votes_by(const Twistpoint& index) const;
+        void print_current_votes_by(
+            const Twistpoint& index
+        ) const;
 
     private:
         std::vector<PrsonaServer> servers;
@@ -125,7 +192,9 @@ class PrsonaServerEntity {
         
         // BINARY SEARCH
         size_t binary_search(
-            const Twistpoint& shortTermPublicKey, size_t which) const;
+            const Twistpoint& shortTermPublicKey,
+            size_t which
+        ) const;
 }; 
 
 #endif

+ 14 - 9
prsona/src/EGCiphertext.cpp

@@ -1,27 +1,32 @@
 #include "EGCiphertext.hpp"
 
 EGCiphertext::EGCiphertext()
-{ /* Do nothing */ }
+{ /* */ }
 
-EGCiphertext::EGCiphertext(const Twistpoint& mask, const Twistpoint& encryptedMessage)
-{
-    this->mask = mask;
-    this->encryptedMessage = encryptedMessage;
-}
+EGCiphertext::EGCiphertext(
+    const Twistpoint& mask,
+    const Twistpoint& encryptedMessage)
+: mask(mask), encryptedMessage(encryptedMessage)
+{ /* */ }
 
-bool EGCiphertext::operator==(const EGCiphertext& other) const
+bool EGCiphertext::operator==(
+    const EGCiphertext& other) const
 {
     return mask == other.mask && encryptedMessage == other.encryptedMessage;
 }
 
-std::ostream& operator<<(std::ostream& os, const EGCiphertext& output)
+std::ostream& operator<<(
+    std::ostream& os,
+    const EGCiphertext& output)
 {
     os << output.mask << output.encryptedMessage;
 
     return os;
 }
 
-std::istream& operator>>(std::istream& is, EGCiphertext& input)
+std::istream& operator>>(
+    std::istream& is,
+    EGCiphertext& input)
 {
     is >> input.mask >> input.encryptedMessage;
 

+ 26 - 45
prsona/src/base.cpp

@@ -80,7 +80,8 @@ Twistpoint PrsonaBase::get_blinding_generator() const
     return elGamalBlindGenerator;
 }
 
-Twistpoint PrsonaBase::get_blinding_generator(std::vector<Proof>& pi) const
+Twistpoint PrsonaBase::get_blinding_generator(
+    std::vector<Proof>& pi) const
 {
     pi = elGamalBlindGeneratorProof;
 
@@ -119,7 +120,8 @@ bool PrsonaBase::set_EG_blind_generator(
  * There might be a standard function for this in <algorithms>?
  * But it returns an iterator, not a size_t, so less useful. */
 size_t PrsonaBase::binary_search(
-    const std::vector<Twistpoint> list, const Twistpoint& index) const
+    const std::vector<Twistpoint> list,
+    const Twistpoint& index) const
 {
     size_t lo, hi;
     lo = 0;
@@ -263,14 +265,11 @@ bool PrsonaBase::verify_generator_proof(
     for (size_t i = 0; i < pi.size(); i++)
     {
         Twistpoint generator = pi[i].curvepointUniversals[0];
-        Twistpoint commitment = (i == pi.size() - 1 ?
-                                    currGenerator :
-                                    pi[i + 1].curvepointUniversals[0]);
+        Twistpoint commitment = (i == pi.size() - 1 ? currGenerator : pi[i + 1].curvepointUniversals[0]);
         Scalar c = pi[i].challengeParts[0];
         Scalar z = pi[i].responseParts[0];
 
-        retval = retval && 
-            schnorr_verification(generator, commitment, c, z);
+        retval = retval && schnorr_verification(generator, commitment, c, z);
         if (!retval)
             std::cerr << "Error in index " << i+1 << " of " << pi.size() << std::endl;
     }
@@ -312,8 +311,7 @@ std::vector<Proof> PrsonaBase::generate_reputation_proof(
     // The value we're actually using in our proof
     mpz_class proofVal = (currentScore - threshold).toInt();
     // Top of the range in our proof determined by what scores are even possible
-    mpz_class proofBits =
-        log2(numClients * MAX_ALLOWED_VOTE - threshold.toInt());
+    mpz_class proofBits = log2(numClients * MAX_ALLOWED_VOTE - threshold.toInt());
     
     // Don't risk a situation that would divulge our private key
     if (proofBits <= 1)
@@ -328,8 +326,7 @@ std::vector<Proof> PrsonaBase::generate_reputation_proof(
         currMask.set_random();
 
         masksPerBit.push_back(currMask);
-        masksPerBit[0] =
-            masksPerBit[0] - (currMask * Scalar(1 << i));
+        masksPerBit[0] = masksPerBit[0] - (currMask * Scalar(1 << i));
     }
 
     // Taken from Fig. 1 in https://eprint.iacr.org/2014/764.pdf
@@ -445,9 +442,7 @@ bool PrsonaBase::verify_reputation_proof(
         }
     }
 
-    Twistpoint scoreCommitment =
-        commitment.encryptedMessage +
-        elGamalBlindGenerator * -threshold;
+    Twistpoint scoreCommitment = commitment.encryptedMessage + elGamalBlindGenerator * -threshold;
     
     return X == scoreCommitment;
 }
@@ -508,8 +503,8 @@ std::vector<Proof> PrsonaBase::generate_vote_proof(
             t_2.set_random();
 
             TwistBipoint U = h * z_r +
-                                oldEncryptedVotes[i] * x_r -
-                                newEncryptedVotes[i] * x_r;
+                oldEncryptedVotes[i] * x_r -
+                newEncryptedVotes[i] * x_r;
 
             TwistBipoint C_a = g * a + h * s;
 
@@ -517,8 +512,7 @@ std::vector<Proof> PrsonaBase::generate_vote_proof(
             TwistBipoint C_b = g * power + h * t_1;
             currProof.curveBipointUniversals.push_back(C_b);
 
-            TwistBipoint C_c = g * (a * a * m) +
-                                h * t_2;
+            TwistBipoint C_c = g * (a * a * m) + h * t_2;
 
             oracleInput << U << C_a << C_b << C_c;
 
@@ -529,8 +523,7 @@ std::vector<Proof> PrsonaBase::generate_vote_proof(
 
             Scalar f = m * x_n + a;
             Scalar z_na = r * x_n + s;
-            Scalar z_nb = 
-                r * (f - x_n) * (x_n + x_n - f) + t_1 * x_n + t_2;
+            Scalar z_nb = r * (f - x_n) * (x_n + x_n - f) + t_1 * x_n + t_2;
 
             currProof.responseParts.push_back(z_r);
             currProof.responseParts.push_back(f);
@@ -543,8 +536,7 @@ std::vector<Proof> PrsonaBase::generate_vote_proof(
         {
             Proof currProof;
 
-            Scalar u, commitmentLambda_1, commitmentLambda_2,
-                x_n, z_na, z_nb, f;
+            Scalar u, commitmentLambda_1, commitmentLambda_2, x_n, z_na, z_nb, f;
             u.set_random();
             commitmentLambda_1.set_random();
             commitmentLambda_2.set_random();
@@ -562,8 +554,7 @@ std::vector<Proof> PrsonaBase::generate_vote_proof(
             TwistBipoint C_b = g * commitmentLambda_1 + h * commitmentLambda_2;
             currProof.curveBipointUniversals.push_back(C_b);
 
-            TwistBipoint C_c =
-                h * z_nb -
+            TwistBipoint C_c = h * z_nb -
                 newEncryptedVotes[i] * ((f - x_n) * (x_n + x_n - f)) -
                 C_b * x_n;
 
@@ -647,9 +638,7 @@ bool PrsonaBase::verify_vote_proof(
             C_b * x_n;
 
         std::stringstream oracleInput;
-        oracleInput << g << h
-            << oldEncryptedVotes[voteIndex] << newEncryptedVotes[voteIndex]
-            << U << C_a << C_b << C_c;
+        oracleInput << g << h << oldEncryptedVotes[voteIndex] << newEncryptedVotes[voteIndex] << U << C_a << C_b << C_c;
 
         if (oracle(oracleInput.str()) != x_r + x_n)
             return false;
@@ -759,8 +748,7 @@ bool PrsonaBase::verify_proof_of_added_user(
             if (currVote !=
                 curveG * DEFAULT_VOTE + curveH * currSeed)
             {
-                std::cerr << "Issue in vote by verifier for user " << i + 1
-                    << " of " << pi[2].responseParts.size() << "." << std::endl;
+                std::cerr << "Issue in vote by verifier for user " << i + 1 << " of " << pi[2].responseParts.size() << "." << std::endl;
                 return false;
             }
         }
@@ -776,8 +764,7 @@ bool PrsonaBase::verify_proof_of_added_user(
             if (currVote !=
                 curveG * DEFAULT_VOTE + curveH * currSeed)
             {
-                std::cerr << "Issue in vote for verifier by user " << i + 1
-                    << " of " << pi[3].responseParts.size() << "." << std::endl;
+                std::cerr << "Issue in vote for verifier by user " << i + 1 << " of " << pi[3].responseParts.size() << "." << std::endl;
                 return false;
             }
         }
@@ -1238,22 +1225,14 @@ std::vector<Proof> PrsonaBase::generate_user_tally_proofs(
             currt2.set_random();
 
             U1 = g * currb2 + h * currt1;
-            U2 = userTallyMasks[j] *
-                (b1 * permutations[j][i] + currb2 * power) +
-                currPseudonyms[j] *
-                (permutations[j][i] * b1 * userTallySeeds[i][j] +
-                    power * currb2 * userTallySeeds[i][j] +
-                    permutations[j][i] * power * currt2) +
+            U2 = userTallyMasks[j] * (b1 * permutations[j][i] + currb2 * power) +
+                currPseudonyms[j] * (permutations[j][i] * b1 * userTallySeeds[i][j] + power * currb2 * userTallySeeds[i][j] + permutations[j][i] * power * currt2) +
                 h * currt2;
             U3 = userTallyMasks[j] * (b1 * currb2) +
-                currPseudonyms[j] *
-                (b1 * currb2 * userTallySeeds[i][j] +
-                    permutations[j][i] * b1 * currt2 +
-                    power * currb2 * currt2);
+                currPseudonyms[j] * (b1 * currb2 * userTallySeeds[i][j] + permutations[j][i] * b1 * currt2 + power * currb2 * currt2);
             U4 = currPseudonyms[j] * (b1 * currb2 * currt2);
             U5 = userTallyMessages[j] * currb2 +
-                nextGenerator * (permutations[j][i] * currt2 +
-                    userTallySeeds[i][j] * currb2) +
+                nextGenerator * (permutations[j][i] * currt2 + userTallySeeds[i][j] * currb2) +
                 h * currt2;
             U6 = nextGenerator * (currb2 * currt2);
             U7 = g * currt2;
@@ -1565,10 +1544,12 @@ bool PrsonaBase::verify_proof_of_reordering(
             Scalar z1 = pi[piIndex].responseParts[1];
             Scalar z2 = pi[piIndex].responseParts[2];
 
-            U1 = g * f + h * z1 -
+            U1 = g * f +
+                h * z1 -
                 permutationCommits[j][i] * x;
             
-            U2 = oldValues[j] * f + otherH * z2 -
+            U2 = oldValues[j] * f +
+                otherH * z2 -
                 productCommits[i][j] * x;
 
             oracleInput << U1 << U2;

+ 14 - 47
prsona/src/client.cpp

@@ -15,12 +15,11 @@ PrsonaClient::PrsonaClient(
     const Twistpoint& elGamalBlindGenerator,
     const BGNPublicKey& serverPublicKey,
     size_t numServers)
-    : serverPublicKey(serverPublicKey),
+: serverPublicKey(serverPublicKey),
         numServers(numServers),
         max_checked(0)
 {
-    set_EG_blind_generator(
-        generatorProof, elGamalBlindGenerator, numServers);
+    set_EG_blind_generator(generatorProof, elGamalBlindGenerator, numServers);
 
     longTermPrivateKey.set_random();
     inversePrivateKey = longTermPrivateKey.curveMultInverse();
@@ -37,7 +36,8 @@ Twistpoint PrsonaClient::get_short_term_public_key() const
     return currentFreshGenerator * longTermPrivateKey;
 }
 
-Twistpoint PrsonaClient::get_short_term_public_key(Proof &pi) const
+Twistpoint PrsonaClient::get_short_term_public_key(
+    Proof &pi) const
 {
     pi = generate_ownership_proof();
     return currentFreshGenerator * longTermPrivateKey;
@@ -71,19 +71,12 @@ std::vector<TwistBipoint> PrsonaClient::make_votes(
     for (size_t i = 0; i < votes.size(); i++)
     {
         if (replaces[i])
-        {
-            newEncryptedVotes[i] =
-                serverPublicKey.twistEncrypt(seeds[i], votes[i]);
-        }
+            newEncryptedVotes[i] = serverPublicKey.twistEncrypt(seeds[i], votes[i]);
         else
-        {
-            newEncryptedVotes[i] =
-                serverPublicKey.rerandomize(seeds[i], oldEncryptedVotes[i]);
-        }
+            newEncryptedVotes[i] = serverPublicKey.rerandomize(seeds[i], oldEncryptedVotes[i]);
     }
 
-    validVoteProof = generate_vote_proof(
-        replaces, oldEncryptedVotes, newEncryptedVotes, seeds, votes);
+    validVoteProof = generate_vote_proof(replaces, oldEncryptedVotes, newEncryptedVotes, seeds, votes);
     return newEncryptedVotes;
 }
 
@@ -162,18 +155,7 @@ bool PrsonaClient::receive_new_user_data(
         return false;
     }
 
-    bool flag = verify_proof_of_added_user(
-        mainProof,
-        currentFreshGenerator,
-        shortTermPublicKey,
-        serverPublicKey.get_bipoint_twistgen(),
-        serverPublicKey.get_bipoint_twist_subgroup_gen(),
-        serverPublicKey.get_bipoint_curvegen(),
-        serverPublicKey.get_bipoint_curve_subgroup_gen(),
-        selfIndex,
-        userEncryptedScore,
-        serverEncryptedScore,
-        encryptedVoteMatrix);
+    bool flag = verify_proof_of_added_user(mainProof, currentFreshGenerator, shortTermPublicKey, serverPublicKey.get_bipoint_twistgen(), serverPublicKey.get_bipoint_twist_subgroup_gen(), serverPublicKey.get_bipoint_curvegen(), serverPublicKey.get_bipoint_curve_subgroup_gen(), selfIndex, userEncryptedScore, serverEncryptedScore, encryptedVoteMatrix);
 
     if (!flag)
     {
@@ -198,13 +180,7 @@ std::vector<Proof> PrsonaClient::generate_reputation_proof(
 {
     Proof ownershipProof = generate_ownership_proof();
     
-    return PrsonaBase::generate_reputation_proof(
-        ownershipProof,
-        currentEncryptedScore,
-        currentScore,
-        threshold,
-        inversePrivateKey,
-        numClients);
+    return PrsonaBase::generate_reputation_proof(ownershipProof, currentEncryptedScore, currentScore, threshold, inversePrivateKey, numClients);
 }
 
 bool PrsonaClient::verify_reputation_proof(
@@ -220,8 +196,7 @@ bool PrsonaClient::verify_reputation_proof(
         return false;
     }
 
-    return PrsonaBase::verify_reputation_proof(
-        pi, currentFreshGenerator, shortTermPublicKey, encryptedScore, threshold);
+    return PrsonaBase::verify_reputation_proof(pi, currentFreshGenerator, shortTermPublicKey, encryptedScore, threshold);
 }
 
 Scalar PrsonaClient::get_score() const
@@ -238,7 +213,8 @@ Scalar PrsonaClient::get_score() const
  */
 
 // Basic memoized score decryption
-Scalar PrsonaClient::decrypt_score(const EGCiphertext& score)
+Scalar PrsonaClient::decrypt_score(
+    const EGCiphertext& score)
 {
     Twistpoint s, hashedDecrypted;
 
@@ -276,8 +252,7 @@ Proof PrsonaClient::generate_ownership_proof() const
 {
     Twistpoint shortTermPublicKey = currentFreshGenerator * longTermPrivateKey;
 
-    return PrsonaBase::generate_ownership_proof(
-        currentFreshGenerator, shortTermPublicKey, longTermPrivateKey);
+    return PrsonaBase::generate_ownership_proof(currentFreshGenerator, shortTermPublicKey, longTermPrivateKey);
 }
 
 /*
@@ -293,13 +268,5 @@ std::vector<Proof> PrsonaClient::generate_vote_proof(
 {
     Proof pi = generate_ownership_proof();
 
-    return PrsonaBase::generate_vote_proof(
-        pi,
-        serverPublicKey.get_bipoint_twistgen(),
-        serverPublicKey.get_bipoint_twist_subgroup_gen(),
-        replaces,
-        oldEncryptedVotes,
-        newEncryptedVotes,
-        seeds,
-        votes);
+    return PrsonaBase::generate_vote_proof(pi, serverPublicKey.get_bipoint_twistgen(), serverPublicKey.get_bipoint_twist_subgroup_gen(), replaces, oldEncryptedVotes, newEncryptedVotes, seeds, votes);
 }

+ 80 - 678
prsona/src/clientMain.cpp

@@ -1,592 +1,65 @@
-#include <mutex>
-#include <condition_variable>
-#include <chrono>
+/**
+ * clientMain.cpp
+ *      - compiles to bin/client
+ *      - runs a client program for experiments with PRSONA
+ *
+ * Stan Gurtler
+ */
+
 #include <iostream>
 #include <fstream>
-#include <cstring>
-#include <cstdlib>
 
 #include "networkClient.hpp"
 
 using namespace std;
 
-enum EventType {
-    CLIENT_MAKE_VOTE = 1,
-    CLIENT_MAKE_REP_PROOF
-};
-
-// Initialize the classes we use
-void initialize_prsona_classes()
-{
-    Scalar::init();
-    PrsonaBase::init();
-    PrsonaBase::set_client_malicious();
-}
-
-BGNPublicKey get_bgn_public_key_from_file(
-    struct synchronization_tool *sync,
-    const char *filename)
-{
-    unique_lock<mutex> lck(sync->mtx);
-    ifstream bgnFile(filename);
-
-    BGNPublicKey publicKey;
-    bgnFile >> publicKey;
-
-    return publicKey;
-}
-
-vector<Proof> get_valid_addition_proof_from_file(
-    struct synchronization_tool *sync,
-    const char *filename)
-{
-    unique_lock<mutex> lck(sync->mtx);
-    ifstream additionFile(filename);
-
-    vector<Proof> retval;
-
-    BinarySizeT sizeOfVector;
-    additionFile >> sizeOfVector;
-    for (size_t i = 0; i < sizeOfVector.val(); i++)
-    {
-        Proof currProof;
-        additionFile >> currProof;
-        retval.push_back(currProof);
-    }
-
-    return retval;
-}
-
-BGNPublicKey get_bgn_public_key(
-    default_random_engine *randomGenerator,
-    const vector<string>& serverIPs,
-    const vector<int>& serverPorts)
-{
-    struct synchronization_tool sync;
-    char *filename = NULL;
-
-    uniform_int_distribution<size_t> distribution(0, serverIPs.size() - 1);
-    size_t whichServer = distribution(*randomGenerator);
-
-    bool flag = false;
-    while (!flag)
-    {
-        struct mg_connection *conn = mg_connect_websocket_client(
-            serverIPs[whichServer].c_str(),
-            serverPorts[whichServer],
-            USE_SSL,
-            NULL,
-            0,
-            PUBLIC_BGN_URI,
-            "null",
-            file_websocket_data_handler,
-            file_websocket_close_handler,
-            (void *) &sync);
-
-        if (!conn)
-        {
-            cerr << "Couldn't obtain BGN details" << endl;
-            continue;
-        }
-
-        unique_lock<mutex> lck(sync.mtx);
-        filename = set_temp_filename(*randomGenerator, conn);
-        sync.val = 0;
-
-        mg_websocket_client_write(
-            conn,
-            MG_WEBSOCKET_OPCODE_DATACOMPLETE,
-            "",
-            0);
-
-        while (!sync.val)
-            sync.cv.wait(lck);
-
-        mg_close_connection(conn);
-
-        flag = true;
-    }
-
-    BGNPublicKey retval =
-        get_bgn_public_key_from_file(&sync, filename);
-
-    remove(filename);
-    delete [] filename;
-
-    return retval;
-}
-
-void verify_valid_addition(
-    PrsonaClient *newUser,
-    default_random_engine *rng,
-    const vector<string>& serverIPs,
-    const vector<int>& serverPorts,
-    const vector<Proof>& proofOfValidAddition,
-    const Twistpoint& shortTermPublicKey)
-{
-    vector<Proof> serverEncryptedScoreProof;
-    CurveBipoint serverEncryptedScore =
-        get_server_committed_val<CurveBipoint>(
-            rng,
-            serverIPs,
-            serverPorts,
-            serverEncryptedScoreProof,
-            shortTermPublicKey,
-            SERVER_TALLY_URI,
-            SERVER_TALLY_COMMIT_URI);
-
-    vector<Proof> userEncryptedScoreProof;
-    EGCiphertext userEncryptedScore =
-        get_server_committed_val<EGCiphertext>(
-            rng,
-            serverIPs,
-            serverPorts,
-            userEncryptedScoreProof,
-            shortTermPublicKey,
-            USER_TALLY_URI,
-            USER_TALLY_COMMIT_URI);
-
-    vector<Proof> voteMatrixProof;
-    vector<vector<TwistBipoint>> voteMatrix =
-        get_server_committed_val<vector<vector<TwistBipoint>>>(
-            rng,
-            serverIPs,
-            serverPorts,
-            voteMatrixProof,
-            shortTermPublicKey,
-            VOTE_MATRIX_URI,
-            VOTE_MATRIX_COMMIT_URI);
-
-    vector<Proof> pseudonymsProof;
-    vector<Twistpoint> currentPseudonyms =
-        get_server_committed_val<vector<Twistpoint>>(
-            rng,
-            serverIPs,
-            serverPorts,
-            pseudonymsProof,
-            shortTermPublicKey,
-            PSEUDONYMS_URI,
-            PSEUDONYMS_COMMIT_URI);
-
-    newUser->receive_new_user_data(
-        proofOfValidAddition,
-        serverEncryptedScoreProof,
-        serverEncryptedScore,
-        userEncryptedScoreProof,
-        userEncryptedScore,
-        voteMatrixProof,
-        voteMatrix,
-        pseudonymsProof,
-        currentPseudonyms);
-}
-
-void register_new_client(
-    default_random_engine *generator,
-    const vector<string>& serverIPs,
-    const vector<int>& serverPorts,
-    PrsonaClient *newUser,
-    const Proof& proofOfValidSTPK,
-    const Twistpoint& shortTermPublicKey)
-{
-    struct synchronization_tool sync;
-    char *filename = NULL;
-    stringstream buffer;
-    string data;
-
-    buffer << proofOfValidSTPK;
-    buffer << shortTermPublicKey;
-
-    data = buffer.str();
-
-    uniform_int_distribution<size_t> distribution(0, serverIPs.size() - 1);
-    size_t whichServer = distribution(*generator);
-
-    bool flag = false;
-    while (!flag)
-    {
-        struct mg_connection *conn = mg_connect_websocket_client(
-            serverIPs[whichServer].c_str(),
-            serverPorts[whichServer],
-            USE_SSL,
-            NULL,
-            0,
-            ADD_USER_URI,
-            "null",
-            file_websocket_data_handler,
-            file_websocket_close_handler,
-            (void *) &sync);
-
-        if (!conn)
-        {
-            cerr << "Couldn't register new client" << endl;
-            continue;
-        }
-
-        unique_lock<mutex> lck(sync.mtx);
-        filename = set_temp_filename(*generator, conn);
-        sync.val = 0;
-        mg_websocket_client_write(
-            conn,
-            MG_WEBSOCKET_OPCODE_BINARY,
-            data.c_str(),
-            data.length());
-
-        mg_websocket_client_write(
-            conn,
-            MG_WEBSOCKET_OPCODE_DATACOMPLETE,
-            "",
-            0);
-
-        while (!sync.val)
-            sync.cv.wait(lck);
-
-        mg_close_connection(conn);
-
-        flag = true;
-    }
-
-    vector<Proof> proofOfValidAddition = 
-        get_valid_addition_proof_from_file(&sync, filename);
-
-    remove(filename);
-    delete [] filename;
-
-    verify_valid_addition(newUser, generator, serverIPs, serverPorts, proofOfValidAddition, shortTermPublicKey);
-}
-
-PrsonaClient *create_client(
-    default_random_engine *generator,
-    const vector<string>& serverIPs,
-    const vector<int>& serverPorts,
-    size_t numServers)
-{
-    BGNPublicKey publicKey =
-        get_bgn_public_key(
-            generator,
-            serverIPs,
-            serverPorts);
-
-    vector<Proof> generatorProof;
-    Twistpoint blindGenerator =
-        get_generator(
-            generator,
-            serverIPs,
-            serverPorts,
-            generatorProof,
-            false);
-
-    PrsonaClient *retval =
-        new PrsonaClient(generatorProof, blindGenerator, publicKey, numServers);
-
-    generatorProof.clear();
-    Twistpoint freshGenerator =
-        get_generator(
-            generator,
-            serverIPs,
-            serverPorts,
-            generatorProof,
-            true);
-
-    retval->receive_fresh_generator(
-        generatorProof, freshGenerator);
-
-    Proof proofOfValidSTPK;
-    Twistpoint shortTermPublicKey = retval->get_short_term_public_key(
-                                        proofOfValidSTPK);
-
-    register_new_client(
-        generator,
-        serverIPs,
-        serverPorts,
-        retval,
-        proofOfValidSTPK,
-        shortTermPublicKey);
-
-    return retval;
-}
-
-string make_vote_string(
-    const vector<Proof>& pi,
-    const vector<TwistBipoint>& newVotes,
-    const Twistpoint& shortTermPublicKey)
-{
-    stringstream buffer;
-    BinarySizeT sizeOfVector;
-
-    sizeOfVector.set(pi.size());
-    buffer << sizeOfVector;
-    for (size_t i = 0; i < sizeOfVector.val(); i++)
-        buffer << pi[i];
-
-    sizeOfVector.set(newVotes.size());
-    buffer << sizeOfVector;
-    for (size_t i = 0; i < sizeOfVector.val(); i++)
-        buffer << newVotes[i];
-
-    buffer << shortTermPublicKey;
-
-    BinaryBool shouldDeal(true);
-    buffer << shouldDeal;
-
-    return buffer.str();
-}
-
-char *send_item(
-    std::default_random_engine& rng,
-    const string& target,
-    int targetPort,
-    const string& data,
-    const char* whichUri,
-    bool responseExpected)
-{
-    struct synchronization_tool sync;
-    char *retval = NULL;
-
-    unique_lock<mutex> lck(sync.mtx);
-    sync.val = 0;
-    sync.val2 = 0;
-
-    while (!sync.val)
-    {
-        struct mg_connection *conn = NULL;
-        while (!conn)
-        {
-            if (responseExpected)
-            {
-                conn = mg_connect_websocket_client(
-                    target.c_str(),
-                    targetPort,
-                    USE_SSL,
-                    NULL,
-                    0,
-                    whichUri,
-                    "null",
-                    file_websocket_data_handler,
-                    file_websocket_close_handler,
-                    (void *) &sync);
-            }
-            else
-            {
-                conn = mg_connect_websocket_client(
-                    target.c_str(),
-                    targetPort,
-                    USE_SSL,
-                    NULL,
-                    0,
-                    whichUri,
-                    "null",
-                    synchro_websocket_data_handler,
-                    synchro_websocket_close_handler,
-                    (void *) &sync);    
-            }
-
-            if (!conn)
-                cerr << "Couldn't connect to server for purposes of sending item." << endl;
-        }
-
-        if (responseExpected)
-            retval = set_temp_filename(rng, conn);
-
-        mg_websocket_client_write(
-            conn,
-            MG_WEBSOCKET_OPCODE_BINARY,
-            data.c_str(),
-            data.length());
-
-        mg_websocket_client_write(
-            conn,
-            MG_WEBSOCKET_OPCODE_DATACOMPLETE,
-            "",
-            0);
-
-        while (!sync.val2)
-            sync.cv.wait(lck);
-
-        mg_close_connection(conn);
-    }
-
-    return retval;
-}
-
-void make_vote(
-    default_random_engine& generator,
-    const string& target,
-    int targetPort,
-    PrsonaClient* prsonaClient,
-    const vector<string>& serverIPs,
-    const vector<int>& serverPorts,
-    size_t numClients)
-{
-    uniform_int_distribution<int> voteDistribution(
-        0, PrsonaBase::get_max_allowed_vote());
-    uniform_int_distribution<int> numVoteDistribution(
-        0, numClients);
-    size_t numVotes = numVoteDistribution(generator);
-
-    // Make the correct number of new votes, but shuffle where they go
-    vector<Scalar> votes;
-    vector<bool> replaces;
-    for (size_t j = 0; j < numClients; j++)
-    {
-        votes.push_back(Scalar(voteDistribution(generator)));
-        replaces.push_back(j < numVotes);
-    }
-    shuffle(replaces.begin(), replaces.end(), generator);
-
-    vector<Proof> generatorProof;
-    Twistpoint freshGenerator =
-        get_generator(
-            &generator,
-            serverIPs,
-            serverPorts,
-            generatorProof,
-            true);
-
-    prsonaClient->receive_fresh_generator(
-        generatorProof, freshGenerator);
-    
-    Twistpoint shortTermPublicKey =
-        prsonaClient->get_short_term_public_key();
-
-    vector<Proof> fullProof;
-    vector<TwistBipoint> encryptedVotes =
-        get_server_committed_val<vector<TwistBipoint>>(
-            &generator,
-            serverIPs,
-            serverPorts,
-            fullProof,
-            shortTermPublicKey,
-            VOTES_BY_URI,
-            VOTES_BY_COMMIT_URI);
-
-    vector<Proof> voteProof;
-    encryptedVotes =
-        prsonaClient->make_votes(
-            voteProof,
-            fullProof,
-            encryptedVotes,
-            votes,
-            replaces);
-
-    string data =
-        make_vote_string(voteProof, encryptedVotes, shortTermPublicKey);
-
-    send_item(generator, target, targetPort, data, GIVE_NEW_VOTE_URI, false);
-}
-
-string make_vote_string(
-    const vector<Proof>& pi,
-    const Twistpoint& shortTermPublicKey,
-    const Scalar& threshold)
-{
-    stringstream buffer;
-    BinarySizeT sizeOfVector;
-
-    sizeOfVector.set(pi.size());
-    buffer << sizeOfVector;
-    for (size_t i = 0; i < sizeOfVector.val(); i++)
-        buffer << pi[i];
-
-    buffer << shortTermPublicKey;
-    buffer << threshold;
-
-    return buffer.str();
-}
-
-string make_rep_proof_string(
-    const vector<Proof>& pi,
-    const Twistpoint& shortTermPublicKey,
-    const Scalar& threshold)
-{
-    stringstream buffer;
-    BinarySizeT sizeOfVector;
-
-    sizeOfVector.set(pi.size());
-    
-    buffer << sizeOfVector;
-    for (size_t i = 0; i < sizeOfVector.val(); i++)
-        buffer << pi[i];
-
-    buffer << shortTermPublicKey;
-    buffer << threshold;
-
-    return buffer.str();
-}
-
-bool make_reputation_proof(
-    default_random_engine& generator,
-    const string& target,
-    int targetPort,
-    PrsonaClient* prsonaClient,
-    const vector<string>& serverIPs,
-    const vector<int>& serverPorts,
-    size_t numClients)
-{
-    vector<Proof> generatorProof;
-    Twistpoint freshGenerator =
-        get_generator(
-            &generator,
-            serverIPs,
-            serverPorts,
-            generatorProof,
-            true);
-
-    prsonaClient->receive_fresh_generator(
-        generatorProof, freshGenerator);
-
-    Twistpoint shortTermPublicKey =
-        prsonaClient->get_short_term_public_key();
-
-    vector<Proof> encryptedScoreProof;
-    EGCiphertext encryptedScore =
-        get_server_committed_val<EGCiphertext>(
-            &generator,
-            serverIPs,
-            serverPorts,
-            encryptedScoreProof,
-            shortTermPublicKey,
-            USER_TALLY_URI,
-            USER_TALLY_COMMIT_URI);
-
-    prsonaClient->receive_vote_tally(
-        encryptedScoreProof,
-        encryptedScore);
-
-    Scalar threshold(0);
-
-    vector<Proof> repProof =
-        prsonaClient->generate_reputation_proof(
-            threshold, numClients);
-
-    string data =
-        make_rep_proof_string(repProof, shortTermPublicKey, threshold);
-
-    char *responseFile = send_item(generator, target, targetPort, data, REP_PROOF_URI, true);
-
-    ifstream response(responseFile);
-
-    char passed = response.get();
-
-    remove(responseFile);
-    delete responseFile;
-
-    return passed == '\x01';
-}
-
+/**
+ * This program (bin/client) expects to be called as follows:
+ * `bin/client <id> <output> <servers_are_malicious>`
+ *
+ * <id> - a name for this instance, which comes with a config file identifying
+ *      its IP address and port
+ * <output> - a string that will name the file in which outputs for this run of
+ *      the program will be written (that is, timings and traffic data)
+ * <servers_are_malicious> - a bool (given as T/t or F/f)
+ *      which is true when servers are in malicious security
+ *      and false when they are in HBC security
+ */
 int main(int argc, char *argv[])
 {
-    initialize_prsona_classes();
+    /*
+     * PRELIMINARY SETUP CODE
+     */
 
+    initialize_prsona_classes();
 #if USE_SSL
-    mg_init_library(0);
-#else
     mg_init_library(MG_FEATURES_SSL);
+#else
+    mg_init_library(0);
 #endif
 
+    // The id effectively tells the client what ip/port it is
     string id = "";
     if (argc > 1)
         id = argv[1];
 
+    string output = "default";
+    if (argc > 2)
+        output = argv[2];
+
+    // Default to malicious security if not specified
+    bool maliciousServers = true;
+    if (argc > 3)
+    {
+        bool setting = argv[3][0] == 't' || argv[3][0] == 'T';
+        maliciousServers = setting;
+    }
+
+    // Set malicious flags where necessary
+    if (maliciousServers)
+        PrsonaBase::set_server_malicious();
+
+    // This seed is used to generate temp filenames and decide which server to make requests to at any step.
     string seedStr;
     if (id.empty())
         seedStr = "default-client";
@@ -595,57 +68,21 @@ int main(int argc, char *argv[])
         seedStr = id;
         seedStr += "-client";
     }
+    seed_seq seed(seedStr.begin(), seedStr.end());
+    default_random_engine rng(seed);
     
     vector<string> serverIPs, clientIPs;
     vector<int> serverPorts, clientPorts;
     string selfIP, selfPortStr;
     int selfPort = 0;
 
-    char buffer[46], *helper;
-    ifstream serverConfig("cfg/serverIPs.cfg");
-    while (!serverConfig.eof())
-    {
-        serverConfig.getline(buffer, 46);
-        if (strlen(buffer) > 0)
-        {
-            helper = buffer;
-            if (strchr(helper, ':'))
-            {
-                helper = strtok(helper, ":");
-                serverIPs.push_back(string(helper));
-                helper = strtok(NULL, ":");
-                serverPorts.push_back(atoi(helper));
-            }
-            else
-            {
-                serverIPs.push_back(string(helper));
-                serverPorts.push_back(atoi(PRSONA_PORT_STR));
-            }
-        }
-    }
+    // Read in from config files the server locations
+    load_multiple_instances_config(serverIPs, serverPorts, "cfg/serverIPs.cfg");
 
-    ifstream clientConfig("cfg/clientIPs.cfg");
-    while (!clientConfig.eof())
-    {
-        clientConfig.getline(buffer, 46);
-        if (strlen(buffer) > 0)
-        {
-            helper = buffer;
-            if (strchr(helper, ':'))
-            {
-                helper = strtok(helper, ":");
-                clientIPs.push_back(string(helper));
-                helper = strtok(NULL, ":");
-                clientPorts.push_back(atoi(helper));
-            }
-            else
-            {
-                clientIPs.push_back(string(helper));
-                clientPorts.push_back(atoi(PRSONA_PORT_STR));
-            }
-        }
-    }
+    // And now the client locations
+    load_multiple_instances_config(clientIPs, clientPorts, "cfg/clientIPs.cfg");
 
+    // Finally, read in the ip/port corresponding to the id that this instance was given
     string selfConfigFilename = "cfg/selfIP";
     if (!id.empty())
     {
@@ -653,47 +90,17 @@ int main(int argc, char *argv[])
         selfConfigFilename += id;
     }
     selfConfigFilename += ".cfg";
+    load_single_instance_config(selfIP, selfPortStr, selfPort, selfConfigFilename.c_str());
 
-    ifstream selfConfig(selfConfigFilename);
-    while (!selfConfig.eof())
-    {
-        selfConfig.getline(buffer, 46);
-        if (strlen(buffer) > 0)
-        {
-            helper = buffer;
-            if (strchr(helper, ':'))
-            {
-                helper = strtok(helper, ":");
-                selfIP = helper;
-                helper = strtok(NULL, ":");
-                selfPortStr = helper;
-                selfPort = atoi(helper);
-            }
-            else
-            {
-                selfIP = helper;
-                selfPortStr = PRSONA_PORT_STR;
-                selfPort = atoi(PRSONA_PORT_STR);
-            }
-        }
-    }
-
-    // Defaults
     size_t numServers = serverIPs.size();
     size_t numClients = clientIPs.size();
-    bool maliciousServers = true;
     uniform_int_distribution<size_t> distribution(0, numServers - 1);
-
     const char *options[] = {"listening_ports", selfPortStr.c_str(), 0};
+    CivetServer server(options);
 
-    if (argc > 2)
-    {
-        bool setting = argv[2][0] == 't' || argv[2][0] == 'T';
-        maliciousServers = setting;
-    }
-
-    seed_seq seed(seedStr.begin(), seedStr.end());
-    default_random_engine generator(seed);
+    /*
+     * CLIENT SETUP CODE
+     */
 
     cout << "[" << seedStr << "] Establishing PRSONA client with the following parameters: " << endl;
     cout << "[" << seedStr << "] " << numServers << " PRSONA servers" << endl;
@@ -702,59 +109,57 @@ int main(int argc, char *argv[])
     cout << "[" << seedStr << "] This client is at IP address: " << selfIP << ":" << selfPort << endl;
     cout << endl;
 
-    // Set malicious flags where necessary
-    if (maliciousServers)
-        PrsonaBase::set_server_malicious();
+    cout << "[" << seedStr << "] Creating PRSONA client object." << endl;
 
-    // Entities we operate with
-    cout << "[" << seedStr << "] Creating PRSONA client." << endl;
-    PrsonaClient *prsonaClient =
-        create_client(&generator, serverIPs, serverPorts, numServers);
-    CivetServer server(options);
+    PrsonaClient *prsonaClient = create_client(rng, serverIPs, serverPorts, numServers);
 
     cout << "[" << seedStr << "] Setting up handlers for client." << endl;
 
-    PrsonaClientWebSocketHandler wsHandler(prsonaClient, serverIPs, serverPorts, &generator);
+    // Main handler (in clients, only used for verifying reputation proofs)
+    PrsonaClientWebSocketHandler wsHandler(rng, prsonaClient, serverIPs, serverPorts);
     server.addWebSocketHandler("/ws", wsHandler);
 
+    // Exit handler (allows client to be brought down by making correct GET request)
     struct synchronization_tool exitSync;
-
     unique_lock<mutex> exitLock(exitSync.mtx);
     exitSync.val = 0;
     exitSync.val2 = 0;
     RemoteControlHandler exitHandler(&exitSync, "Client coming down!");
     server.addHandler(EXIT_URI, exitHandler);
 
+    // Make-vote handler (allows orchestrator to cause this client to make a new, random vote)
     AltRemoteControlHandler triggerVoteHandler(CLIENT_MAKE_VOTE, &exitSync, "Client will make new votes!");
     server.addHandler(TRIGGER_VOTE_URI, triggerVoteHandler);
 
+    // Make-reputation handler (allows orchestrator to cause this client to make a new reputation proof, and specifies which other client to give it to)
     AltRemoteControlHandler triggerRepHandler(CLIENT_MAKE_REP_PROOF, &exitSync, "Client will make reputation proof!");
     server.addHandler(TRIGGER_REP_URI, triggerRepHandler);
 
+    /*
+     * MAIN CLIENT LOOP CODE
+     */
+
     cout << "[" << seedStr << "] Entering main ready loop." << endl;
 
     while (!exitSync.val)
     {
+        // exitSync.val controls when an exit is signaled.
+        // exitSync.val2 controls when making a vote or making a reputation proof is signaled
         while (!exitSync.val && !exitSync.val2)
             exitSync.cv.wait(exitLock);
 
-        size_t whichServer = distribution(generator);
+        size_t whichServer;
         string fullQuery, target;
         size_t colonLocation;
         int targetPort;
 
+        // exitSync.val2 is set by the make-vote and make-reputation handlers, but will be 0 in the event of signaling an exit
         switch (exitSync.val2)
         {
             case CLIENT_MAKE_VOTE:
                 cout << "[" << seedStr << "] Making new vote row." << endl;
-                make_vote(
-                    generator,
-                    serverIPs[whichServer],
-                    serverPorts[whichServer],
-                    prsonaClient,
-                    serverIPs,
-                    serverPorts,
-                    numClients);
+                whichServer = distribution(rng);
+                make_vote(rng, prsonaClient, serverIPs, serverPorts, serverIPs[whichServer], serverPorts[whichServer], numClients);
                 cout << "[" << seedStr << "] New vote row complete." << endl;
                 break;
 
@@ -766,28 +171,25 @@ int main(int argc, char *argv[])
                 targetPort = stoi(fullQuery.substr(colonLocation + 1));
 
                 cout << "[" << seedStr << "] Making new reputation proof." << endl;
-                make_reputation_proof(
-                    generator,
-                    target,
-                    targetPort,
-                    prsonaClient,
-                    serverIPs,
-                    serverPorts,
-                    numClients);
+                make_reputation_proof(rng, prsonaClient, serverIPs, serverPorts, target, targetPort, numClients);
                 cout << "[" << seedStr << "] New reputation proof complete." << endl;
                 break;
 
-            default:
+            default:    // Occurs when an exit is signaled; do nothing
                 break;
         }
 
+        // Make sure to reset this, so that we wait until something new is signaled to do
         exitSync.val2 = 0;
     }
 
+    /*
+     * SHUTDOWN CODE
+     */
+
     cout << "[" << seedStr << "] Shutting down." << endl;
 
     mg_exit_library();
-
     delete prsonaClient;
 
     return 0;

+ 70 - 172
prsona/src/main.cpp → prsona/src/localMain.cpp

@@ -18,23 +18,23 @@ void initialize_prsona_classes()
 }
 
 // Quick and dirty mean calculation (used for averaging timings)
-double mean(vector<double> xx)
+double mean(
+    vector<double> xx)
 {
     return accumulate(xx.begin(), xx.end(), 0.0) / xx.size(); 
 }
 
-void print_user_scores(const vector<PrsonaClient>& users)
+void print_user_scores(
+    const vector<PrsonaClient>& users)
 {
     std::cout << "<";
     for (size_t i = 0; i < users.size(); i++)
-    {
-            std::cout << users[i].get_score()
-                << (i == users.size() - 1 ? ">" : " ");
-    }
+        std::cout << users[i].get_score() << (i == users.size() - 1 ? ">" : " ");
     std::cout << std::endl;
 }
 
-bool test_proof_output(const vector<Proof>& pi)
+bool test_proof_output(
+    const vector<Proof>& pi)
 {
     vector<Proof> copy;
     stringstream buffer;
@@ -69,8 +69,7 @@ vector<double> make_votes(
     size_t numVotes)
 {
     vector<double> retval;
-    uniform_int_distribution<int> voteDistribution(
-        0, PrsonaBase::get_max_allowed_vote());
+    uniform_int_distribution<int> voteDistribution(0, PrsonaBase::get_max_allowed_vote());
     size_t numUsers = users.size();
     newEncryptedVotes.clear();
 
@@ -88,30 +87,20 @@ vector<double> make_votes(
         
         Proof baseProof;
         vector<Proof> fullProof;
-        Twistpoint shortTermPublicKey =
-            users[i].get_short_term_public_key();
-        vector<TwistBipoint> currEncryptedVotes =
-            servers.get_current_votes_by(baseProof, shortTermPublicKey);
+        Twistpoint shortTermPublicKey = users[i].get_short_term_public_key();
+        vector<TwistBipoint> currEncryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
         fullProof.push_back(baseProof);
         servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
         
         vector<Proof> currVoteProof;
-        chrono::high_resolution_clock::time_point t0 =
-            chrono::high_resolution_clock::now();
-        currEncryptedVotes = users[i].make_votes(
-                                currVoteProof,
-                                fullProof,
-                                currEncryptedVotes,
-                                votes,
-                                replaces);
-        chrono::high_resolution_clock::time_point t1 =
-            chrono::high_resolution_clock::now();
+        chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
+        currEncryptedVotes = users[i].make_votes(currVoteProof, fullProof, currEncryptedVotes, votes, replaces);
+        chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
 
         newEncryptedVotes.push_back(currEncryptedVotes);
         validVoteProofs.push_back(currVoteProof);
 
-        chrono::duration<double> time_span =
-            chrono::duration_cast<chrono::duration<double>>(t1 - t0);
+        chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
         retval.push_back(time_span.count());
     }
 
@@ -132,23 +121,15 @@ vector<double> transmit_votes_to_servers(
     for (size_t i = 0; i < numUsers; i++)
     {
         Proof ownerProof;
-        Twistpoint shortTermPublicKey =
-            users[i].get_short_term_public_key(ownerProof);
+        Twistpoint shortTermPublicKey = users[i].get_short_term_public_key(ownerProof);
 
         for (size_t j = 0; j < numServers; j++)
         {
-            chrono::high_resolution_clock::time_point t0 =
-                chrono::high_resolution_clock::now();
-            servers.receive_vote(
-                validVoteProofs[i],
-                newEncryptedVotes[i],
-                shortTermPublicKey,
-                j);
-            chrono::high_resolution_clock::time_point t1 =
-                chrono::high_resolution_clock::now();
-
-            chrono::duration<double> time_span =
-                chrono::duration_cast<chrono::duration<double>>(t1 - t0);
+            chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
+            servers.receive_vote(validVoteProofs[i], newEncryptedVotes[i], shortTermPublicKey, j);
+            chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
+
+            chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
             retval.push_back(time_span.count());
         }
             
@@ -158,38 +139,34 @@ vector<double> transmit_votes_to_servers(
 }
 
 // Time how long it takes to do the operations associated with an epoch
-double epoch(PrsonaServerEntity& servers)
+double epoch(
+    PrsonaServerEntity& servers)
 {
     // Do the epoch server calculations
-    chrono::high_resolution_clock::time_point t0 =
-        chrono::high_resolution_clock::now();
+    chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
     servers.epoch();
-    chrono::high_resolution_clock::time_point t1 =
-        chrono::high_resolution_clock::now();
+    chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
 
     // Return the timing of the epoch server calculations
-    chrono::duration<double> time_span =
-        chrono::duration_cast<chrono::duration<double>>(t1 - t0);
+    chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
     return time_span.count();
 }
 
 // Time how long it takes each user to decrypt their new scores
 vector<double> transmit_epoch_updates(
-    vector<PrsonaClient>& users, const PrsonaServerEntity& servers)
+    vector<PrsonaClient>& users,
+    const PrsonaServerEntity& servers)
 {
     vector<double> retval;
     size_t numUsers = users.size();
 
     for (size_t i = 0; i < numUsers; i++)
     {
-        chrono::high_resolution_clock::time_point t0 =
-            chrono::high_resolution_clock::now();
+        chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
         servers.transmit_updates(users[i]);
-        chrono::high_resolution_clock::time_point t1 =
-            chrono::high_resolution_clock::now();
+        chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
 
-        chrono::duration<double> time_span =
-            chrono::duration_cast<chrono::duration<double>>(t1 - t0);
+        chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t1 - t0);
         retval.push_back(time_span.count());
     }
 
@@ -214,45 +191,27 @@ void test_reputation_proof(
     Scalar badThreshold(aScore + 1);
 
     Twistpoint shortTermPublicKey = a.get_short_term_public_key();
-    vector<Proof> goodRepProof =
-        a.generate_reputation_proof(goodThreshold, servers.get_num_clients());
+    vector<Proof> goodRepProof = a.generate_reputation_proof(goodThreshold, servers.get_num_clients());
 
     Proof baseProof;
     vector<Proof> fullProof;
-    EGCiphertext currEncryptedScore =
-        servers.get_current_user_encrypted_tally(baseProof, shortTermPublicKey);
+    EGCiphertext currEncryptedScore = servers.get_current_user_encrypted_tally(baseProof, shortTermPublicKey);
     fullProof.push_back(baseProof);
     servers.get_other_user_tally_commitments(fullProof, shortTermPublicKey);
 
-    flag = b.verify_reputation_proof(
-                goodRepProof,
-                shortTermPublicKey,
-                goodThreshold,
-                fullProof,
-                currEncryptedScore);
-    cout << "TEST VALID REPUTATION PROOF:     "
-        << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" )
-        << endl;
+    flag = b.verify_reputation_proof(goodRepProof, shortTermPublicKey, goodThreshold, fullProof, currEncryptedScore);
+    cout << "TEST VALID REPUTATION PROOF:     " << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" ) << endl;
     
-    vector<Proof> badRepProof =
-        a.generate_reputation_proof(badThreshold, servers.get_num_clients());
+    vector<Proof> badRepProof = a.generate_reputation_proof(badThreshold, servers.get_num_clients());
 
     baseProof.clear();
     fullProof.clear();
-    currEncryptedScore =
-        servers.get_current_user_encrypted_tally(baseProof, shortTermPublicKey);
+    currEncryptedScore = servers.get_current_user_encrypted_tally(baseProof, shortTermPublicKey);
     fullProof.push_back(baseProof);
     servers.get_other_user_tally_commitments(fullProof, shortTermPublicKey);
 
-    flag = b.verify_reputation_proof(
-                badRepProof,
-                shortTermPublicKey,
-                goodThreshold,
-                fullProof,
-                currEncryptedScore);
-    cout << "TEST INVALID REPUTATION PROOF:   "
-        << (flag ? "FAILED (Proof verified)" : "PASSED (Proof not verified)" )
-        << endl << endl;
+    flag = b.verify_reputation_proof(badRepProof, shortTermPublicKey, goodThreshold, fullProof, currEncryptedScore);
+    cout << "TEST INVALID REPUTATION PROOF:   " << (flag ? "FAILED (Proof verified)" : "PASSED (Proof not verified)" ) << endl << endl;
 }
 
 // Test if the proof of valid votes is working as expected
@@ -276,47 +235,28 @@ void test_vote_proof(
 
     Proof baseProof;
     vector<Proof> fullProof;
-    Twistpoint shortTermPublicKey =
-        user.get_short_term_public_key();
-    vector<TwistBipoint> encryptedVotes =
-        servers.get_current_votes_by(baseProof, shortTermPublicKey);
+    Twistpoint shortTermPublicKey = user.get_short_term_public_key();
+    vector<TwistBipoint> encryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
     fullProof.push_back(baseProof);
     servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
 
-    encryptedVotes = user.make_votes(
-        validVoteProof,
-        fullProof,
-        encryptedVotes,
-        votes,
-        replaces);
+    encryptedVotes = user.make_votes(validVoteProof, fullProof, encryptedVotes, votes, replaces);
 
-    flag = servers.receive_vote(
-                validVoteProof, encryptedVotes, shortTermPublicKey);
-    cout << "TEST REPLACE VOTE PROOF:         "
-        << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" )
-        << endl;
+    flag = servers.receive_vote(validVoteProof, encryptedVotes, shortTermPublicKey);
+    cout << "TEST REPLACE VOTE PROOF:         " << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" ) << endl;
 
     for (size_t i = 0; i < numUsers; i++)
         replaces[i] = false;
 
     baseProof.clear();
     fullProof.clear();
-    encryptedVotes =
-        servers.get_current_votes_by(baseProof, shortTermPublicKey);
+    encryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
     fullProof.push_back(baseProof);
     servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
-    encryptedVotes = user.make_votes(
-        validVoteProof,
-        fullProof,
-        encryptedVotes,
-        votes,
-        replaces);
-
-    flag = servers.receive_vote(
-                validVoteProof, encryptedVotes, shortTermPublicKey);
-    cout << "TEST RERANDOMIZE VOTE PROOF:     "
-        << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" )
-        << endl;
+    encryptedVotes = user.make_votes(validVoteProof, fullProof, encryptedVotes, votes, replaces);
+
+    flag = servers.receive_vote(validVoteProof, encryptedVotes, shortTermPublicKey);
+    cout << "TEST RERANDOMIZE VOTE PROOF:     " << (flag ? "PASSED (Proof verified)" : "FAILED (Proof not verified)" ) << endl;
 
     for (size_t i = 0; i < numUsers; i++)
     {
@@ -326,22 +266,13 @@ void test_vote_proof(
 
     baseProof.clear();
     fullProof.clear();
-    encryptedVotes =
-        servers.get_current_votes_by(baseProof, shortTermPublicKey);
+    encryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
     fullProof.push_back(baseProof);
     servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
-    encryptedVotes = user.make_votes(
-        validVoteProof,
-        fullProof,
-        encryptedVotes,
-        votes,
-        replaces);
-
-    flag = servers.receive_vote(
-                validVoteProof, encryptedVotes, shortTermPublicKey);
-    cout << "TEST INVALID REPLACE VOTE PROOF: "
-        << (flag ? "FAILED (Proof verified)" : "PASSED (Proof not verified)" )
-        << endl << endl;
+    encryptedVotes = user.make_votes(validVoteProof, fullProof, encryptedVotes, votes, replaces);
+
+    flag = servers.receive_vote(validVoteProof, encryptedVotes, shortTermPublicKey);
+    cout << "TEST INVALID REPLACE VOTE PROOF: " << (flag ? "FAILED (Proof verified)" : "PASSED (Proof not verified)" ) << endl << endl;
 }
 
 void check_vote_matrix_updates()
@@ -357,34 +288,26 @@ void check_vote_matrix_updates()
     PrsonaServerEntity servers(numServers);
     vector<Proof> elGamalBlindGeneratorProof;
     BGNPublicKey bgnPublicKey = servers.get_bgn_public_key();
-    Twistpoint elGamalBlindGenerator =
-        servers.get_blinding_generator(elGamalBlindGeneratorProof);
+    Twistpoint elGamalBlindGenerator = servers.get_blinding_generator(elGamalBlindGeneratorProof);
 
     vector<PrsonaClient> users;
     for (size_t i = 0; i < numUsers; i++)
     {
-        PrsonaClient currUser(
-            elGamalBlindGeneratorProof,
-            elGamalBlindGenerator,
-            bgnPublicKey,
-            numServers);
+        PrsonaClient currUser(elGamalBlindGeneratorProof, elGamalBlindGenerator, bgnPublicKey, numServers);
         users.push_back(currUser);
         servers.add_new_client(users[i]);
     }
 
     Proof pseudonymsProof;
-    vector<Twistpoint> currentPseudonyms =
-        servers.get_current_pseudonyms(pseudonymsProof);
+    vector<Twistpoint> currentPseudonyms = servers.get_current_pseudonyms(pseudonymsProof);
 
     cout << "Making votes." << endl;
 
     for (size_t i = 0; i < numUsers; i++)
     {   
-        Twistpoint shortTermPublicKey =
-            users[i].get_short_term_public_key();
+        Twistpoint shortTermPublicKey = users[i].get_short_term_public_key();
 
-        size_t myIndex =
-            users[i].binary_search(currentPseudonyms, shortTermPublicKey);
+        size_t myIndex = users[i].binary_search(currentPseudonyms, shortTermPublicKey);
 
         cout << "User " << i+1 << " has initial index " << myIndex << endl;
 
@@ -403,23 +326,14 @@ void check_vote_matrix_updates()
 
         Proof baseProof;
         vector<Proof> fullProof;
-        vector<TwistBipoint> currEncryptedVotes =
-            servers.get_current_votes_by(baseProof, shortTermPublicKey);
+        vector<TwistBipoint> currEncryptedVotes = servers.get_current_votes_by(baseProof, shortTermPublicKey);
         fullProof.push_back(baseProof);
         servers.get_other_vote_row_commitments(fullProof, shortTermPublicKey);
 
         vector<Proof> currVoteProof;
-        currEncryptedVotes = users[i].make_votes(
-                                currVoteProof,
-                                fullProof,
-                                currEncryptedVotes,
-                                votes,
-                                replaces);
-
-        servers.receive_vote(
-                currVoteProof,
-                currEncryptedVotes,
-                shortTermPublicKey);
+        currEncryptedVotes = users[i].make_votes(currVoteProof, fullProof, currEncryptedVotes, votes, replaces);
+
+        servers.receive_vote(currVoteProof, currEncryptedVotes, shortTermPublicKey);
         cout << "User " << i+1 << " now has the following votes:" << endl;
         servers.print_current_votes_by(shortTermPublicKey);
     }
@@ -439,8 +353,7 @@ void check_vote_matrix_updates()
     for (size_t i = 0; i < numUsers; i++)
     {   
         Proof ownerProof;
-        Twistpoint shortTermPublicKey =
-            users[i].get_short_term_public_key(ownerProof);
+        Twistpoint shortTermPublicKey = users[i].get_short_term_public_key(ownerProof);
 
         cout << "User " << i+1 << " now has the following votes:" << endl;
         servers.print_current_votes_by(shortTermPublicKey);
@@ -502,8 +415,7 @@ int main(int argc, char *argv[])
     PrsonaServerEntity servers(numServers);
     vector<Proof> elGamalBlindGeneratorProof;
     BGNPublicKey bgnPublicKey = servers.get_bgn_public_key();
-    Twistpoint elGamalBlindGenerator =
-        servers.get_blinding_generator(elGamalBlindGeneratorProof);
+    Twistpoint elGamalBlindGenerator = servers.get_blinding_generator(elGamalBlindGeneratorProof);
 
     test_proof_output(elGamalBlindGeneratorProof);
 
@@ -511,11 +423,7 @@ int main(int argc, char *argv[])
     vector<PrsonaClient> users;
     for (size_t i = 0; i < numUsers; i++)
     {
-        PrsonaClient currUser(
-            elGamalBlindGeneratorProof,
-            elGamalBlindGenerator,
-            bgnPublicKey,
-            numServers);
+        PrsonaClient currUser(elGamalBlindGeneratorProof, elGamalBlindGenerator, bgnPublicKey, numServers);
         users.push_back(currUser);
         servers.add_new_client(users[i]);
     }
@@ -533,23 +441,14 @@ int main(int argc, char *argv[])
         
         vector<vector<TwistBipoint>> newEncryptedVotes;
         vector<vector<Proof>> validVoteProofs;
-        timings = make_votes(
-            generator,
-            newEncryptedVotes,
-            validVoteProofs,
-            users,
-            servers,
-            numVotesPerRound);
+        timings = make_votes(generator, newEncryptedVotes, validVoteProofs, users, servers, numVotesPerRound);
         
-        cout << "Vote generation (with proofs): " << mean(timings)
-            << " seconds per user" << endl;
+        cout << "Vote generation (with proofs): " << mean(timings) << " seconds per user" << endl;
         timings.clear();
 
-        timings = transmit_votes_to_servers(
-            newEncryptedVotes, validVoteProofs, users, servers);
+        timings = transmit_votes_to_servers(newEncryptedVotes, validVoteProofs, users, servers);
 
-        cout << "Vote validation: " << mean(timings)
-            << " seconds per vote vector/server" << endl;
+        cout << "Vote validation: " << mean(timings) << " seconds per vote vector/server" << endl;
         timings.clear();
 
         timings.push_back(epoch(servers));
@@ -559,8 +458,7 @@ int main(int argc, char *argv[])
 
         timings = transmit_epoch_updates(users, servers);
 
-        cout << "Transmit epoch updates: " << mean(timings)
-            << " seconds per user" << endl << endl;
+        cout << "Transmit epoch updates: " << mean(timings) << " seconds per user" << endl << endl;
     }
 
     // Pick random users for our tests

File diff suppressed because it is too large
+ 647 - 306
prsona/src/networkClient.cpp


+ 332 - 0
prsona/src/networkOrchestrator.cpp

@@ -0,0 +1,332 @@
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <cstdlib>
+#include <thread>
+
+#include "networkOrchestrator.hpp"
+
+/***************************************************
+ *********                                 *********
+ *********  orchestrator public functions  *********
+ *********                                 *********
+ ***************************************************/
+
+/*
+ * START UP AND SHUT DOWN INSTANCES
+ */
+
+int start_remote_actor(
+    const std::string& target,
+    bool server,
+    const std::string& id,
+    const std::string& output,
+    bool maliciousServers)
+{
+    std::stringstream buffer;
+    std::string command;
+
+    // buffer << "ssh tmgurtle@" << target << " \"screen \'~/prsona/prsona/scripts/startup.sh "
+    //     << (server ? "server " : "client ") << id << " " << output << (maliciousServers ? " T\'\"" : " F\'\"");
+
+    buffer << "bin/" << (server ? "server " : "client ") << id << " " << output << (maliciousServers ? " T &" : " F &");
+    command = buffer.str();
+
+    return system(command.c_str());
+}
+
+void shut_down_remote_actors(
+    const std::vector<std::string>& relevantIPs,
+    const std::vector<int>& relevantPorts)
+{
+    for (size_t i = 0; i < relevantIPs.size(); i++)
+    {
+        // Shut downs are triggered by a GET request to the correct location
+        std::stringstream sysString;
+        std::string data;
+        sysString << "GET " << EXIT_URI << " HTTP/1.1\r\n";
+        sysString << "Host: " << relevantIPs[i] << ":" << relevantPorts[i] << "\r\n\r\n";
+        data = sysString.str();
+
+        struct mg_connection *conn = NULL;
+
+        // Connect to the instance
+        while (!conn)
+        {
+            conn = mg_connect_client(relevantIPs[i].c_str(), relevantPorts[i], USE_SSL, NULL, 0);
+
+            if (!conn)
+                std::cerr << "Couldn't connect to instance at " << relevantIPs[i] << ":" << relevantPorts[i] << " for shut down." << std::endl;
+        }
+
+        // Make correct GET request
+        mg_write(conn, data.c_str(), data.length());
+
+        // Close connection
+        mg_close_connection(conn);
+    }
+}
+
+/*
+ * SYNCHRONIZATION
+ */
+
+void wait_for_servers_ready(
+    std::string dealer,
+    int dealerPort)
+{
+    // Requesting information about servers being ready is done via a GET request
+    std::stringstream sysString;
+    std::string data;
+    sysString << "GET " << EPOCH_READY_URI << " HTTP/1.1\r\n";
+    sysString << "Host: " << dealer << ":" << dealerPort << "\r\n\r\n";
+    data = sysString.str();
+
+    bool ready = false;
+    while (!ready)
+    {
+        struct mg_connection *conn = NULL;
+
+        // Connect to the dealer
+        while (!conn)
+        {
+            std::this_thread::sleep_for(ONE_SECOND);
+
+            conn = mg_connect_client(dealer.c_str(), dealerPort, USE_SSL, NULL, 0);
+
+            if (!conn)
+                std::cerr << "Couldn't make connection while waiting for servers to be ready." << std::endl;
+        }
+
+        // Make the correct GET request
+        mg_write(conn, data.c_str(), data.length());
+
+        // Wait for a response
+        mg_get_response(conn, NULL, 0, 250);
+        const struct mg_response_info *info = mg_get_response_info(conn);
+
+        // Close connection
+        mg_close_connection(conn);
+
+        // If the dealer says it's ready, then we can move on
+        if (info->status_code == 200)
+            ready = true;
+    }
+}
+
+void wait_for_clients_ready(
+    std::string dealer,
+    int dealerPort,
+    size_t numClients)
+{
+    bool ready = false;
+    while (!ready)
+    {
+        struct synchronization_tool sync;
+        struct mg_connection *conn = NULL;
+
+        // Connect to the dealer
+        std::unique_lock<std::mutex> lck(sync.mtx);
+        sync.val = 0;
+        sync.val2 = 0;
+        while (!conn)
+        {
+            std::this_thread::sleep_for(ONE_SECOND);
+
+            conn = mg_connect_websocket_client(dealer.c_str(), dealerPort, USE_SSL, NULL, 0, REQUEST_NUM_CLIENTS_URI, "null", clients_websocket_data_handler, synchro_websocket_close_handler, &sync);
+
+            if (!conn)
+                std::cerr << "Couldn't make connection while waiting for clients to be ready." << std::endl;
+        }
+
+        // Tell the dealer we're ready for its response
+        mg_websocket_client_write(conn, MG_WEBSOCKET_OPCODE_DATACOMPLETE, "", 0);
+
+        // Wait for that response
+        while (!sync.val2)
+            sync.cv.wait(lck);
+
+        // Close connection
+        mg_close_connection(conn);
+
+        // If the dealer says it's ready, then we can move on
+        if (sync.val == numClients)
+            ready = true;
+    }
+}
+
+/*
+ * RUN EXPERIMENT
+ */
+
+void execute_experiment(
+    std::string dealer,
+    int dealerPort,
+    const char *filename)
+{
+    size_t line = 1;
+
+    // Iterate across each line in the command file, which contains one command per line
+    char buffer[128];
+    std::ifstream commands(filename);
+    while (!commands.eof())
+    {
+        commands.getline(buffer, 128);
+        if (strlen(buffer) == 0)
+        {
+            line++;
+            continue;
+        }
+
+        std::cout << "Command " << line << ": " << std::string(buffer) << std::endl;
+
+        // The first character of each command tells us which it is
+        switch(buffer[0])
+        {
+            // Vote triggers come in form `V <voterIP>:<voterPort>`
+            case 'V':
+                char *voter, *voterPort;
+                voter = strtok(buffer + 1, " :");
+                voterPort = strtok(NULL, " :");
+
+                trigger_vote(std::string(voter), atoi(voterPort));
+                break;
+
+            // Reputation proof triggers come in form `R <proverIP>:<proverPort> <verifierIP>:<verifierPort>`
+            case 'R':
+                char *target, *targetPortStr, *verifier, *verifierPortStr;
+                target = strtok(buffer + 1, " :");
+                targetPortStr = strtok(NULL, " :");
+                verifier = strtok(NULL, " :");
+                verifierPortStr = strtok(NULL, " :");
+
+                trigger_reputation_proof(
+                    std::string(target),
+                    atoi(targetPortStr),
+                    std::string(verifier),
+                    atoi(verifierPortStr));
+                break;
+
+            // Epoch change triggers come in form `E`
+            case 'E':
+                trigger_epoch_change(dealer, dealerPort);
+                break;
+
+            default:
+                break;
+        }
+
+        line++;
+    }
+
+    // Don't let ourselves shut down servers and clients until we're sure they're not in the middle of anything else
+    wait_for_servers_ready(dealer, dealerPort);
+}
+
+/****************************************************
+ *********                                  *********
+ *********  orchestrator private functions  *********
+ *********                                  *********
+ ****************************************************/
+
+/*
+ * TRIGGER EXPERIMENT EVENTS
+ */
+
+void trigger_epoch_change(
+    std::string dealer,
+    int dealerPort)
+{
+    // Give other updates a chance to resolve
+    wait_for_servers_ready(dealer, dealerPort);
+
+    // Epoch changes are triggered via GET request to the correct location
+    std::stringstream sysString;
+    std::string data;
+    sysString << "GET " << TRIGGER_EPOCH_URI << " HTTP/1.1\r\n";
+    sysString << "Host: " << dealer << ":" << dealerPort << "\r\n\r\n";
+    data = sysString.str();
+
+    struct mg_connection *conn = NULL;
+
+    // Connect to the dealer
+    while (!conn)
+    {
+        conn = mg_connect_client(dealer.c_str(), dealerPort, USE_SSL, NULL, 0);
+
+        if (!conn)
+            std::cerr << "Couldn't connect to dealer to trigger epoch change." << std::endl;
+    }
+
+    // Make the relevant GET request
+    mg_write(conn, data.c_str(), data.length());
+
+    // Close connection
+    mg_close_connection(conn);
+
+    // Don't bother giving new commands until this one has resolved
+    wait_for_servers_ready(dealer, dealerPort);
+}
+
+void trigger_vote(
+    std::string target,
+    int targetPort)
+{
+    // New votes are triggered via GET request to the correct location
+    std::stringstream sysString;
+    std::string data;
+    sysString << "GET " << TRIGGER_VOTE_URI << " HTTP/1.1\r\n";
+    sysString << "Host: " << target << ":" << targetPort << "\r\n\r\n";
+    data = sysString.str();
+
+    struct mg_connection *conn = NULL;
+
+    // Connect to the client
+    while (!conn)
+    {
+        conn = mg_connect_client(target.c_str(), targetPort, USE_SSL, NULL, 0);
+
+        if (!conn)
+            std::cerr << "Couldn't connect to client at " << target << ":" << targetPort << " to trigger new vote." << std::endl;
+    }
+
+    // Make the relevant GET request
+    mg_write(conn, data.c_str(), data.length());
+
+    // Close connection
+    mg_close_connection(conn);
+}
+
+void trigger_reputation_proof(
+    std::string target,
+    int targetPort,
+    std::string verifier,
+    int verifierPort)
+{
+    // Reputation proofs are triggered via GET request to the correct location (with a parameter for the intended verifier)
+    std::stringstream sysString;
+    std::string data;
+    sysString << "GET " << TRIGGER_REP_URI << "?" << verifier << ":" << verifierPort << " HTTP/1.1\r\n";
+    sysString << "Host: " << target << "\r\n\r\n";
+    data = sysString.str();
+
+    struct mg_connection *conn = NULL;
+
+    // Connect to the client
+    while (!conn)
+    {
+        conn = mg_connect_client(target.c_str(), targetPort, USE_SSL, NULL, 0);
+
+        if (!conn)
+            std::cerr << "Couldn't connect to client at " << target << ":" << targetPort << " to trigger reputation proof." << std::endl;
+    }
+
+    // Make the relevant GET request
+    mg_write(conn, data.c_str(), data.length());
+
+    // Close connection
+    mg_close_connection(conn);
+
+    // Give this command a small amount of time to resolve
+    std::this_thread::sleep_for(ONE_SECOND);
+}

File diff suppressed because it is too large
+ 995 - 105
prsona/src/networkServer.cpp


+ 340 - 69
prsona/src/networking.cpp

@@ -1,32 +1,30 @@
-#include <random>
 #include <algorithm>
 #include <iostream>
+#include <fstream>
 #include <cstring>
+#include <cstdlib>
+#include <cstdio>
 
 #include "networking.hpp"
 
-std::string random_string(std::default_random_engine& generator, size_t length)
+/*********************************************
+ ****                                     ****
+ ****  "public" generic helper functions  ****
+ ****                                     ****
+ *********************************************/
+
+void initialize_prsona_classes()
 {
-    auto randchar = [&]() -> char
-    {
-        const char charset[] =
-        "0123456789_-"
-        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-        "abcdefghijklmnopqrstuvwxyz";
-        const size_t max_index = (sizeof(charset) - 1);
-        std::uniform_int_distribution<int> dist(0, max_index - 1);
-        return charset[ dist(generator) ];
-    };
-    std::string str(length,0);
-    std::generate_n(str.begin(), length, randchar);
-    return str;
+    Scalar::init();
+    PrsonaBase::init();
+    PrsonaBase::set_client_malicious();
 }
 
 char *set_temp_filename(
-    std::default_random_engine& generator,
+    std::default_random_engine& rng,
     struct mg_connection *conn)
 {
-    std::string filename = random_string(generator, TMP_FILE_SIZE);
+    std::string filename = random_string(rng, TMP_FILE_SIZE);
     
     char *c_filename = new char[TMP_FILE_SIZE+TMP_DIR_SIZE+1];
     strncpy(c_filename, TMP_DIR, TMP_DIR_SIZE);
@@ -40,64 +38,135 @@ char *set_temp_filename(
     return c_filename;
 }
 
-bool RemoteControlHandler::handleGet(CivetServer *server, struct mg_connection *conn)
+void load_multiple_instances_config(
+    std::vector<std::string>& relevantIPs,
+    std::vector<int>& relevantPorts,
+    const char *filename)
 {
-    std::unique_lock<std::mutex> lck(sync->mtx);
+    relevantIPs.clear();
+    relevantPorts.clear();
 
-    mg_printf(conn,
-              "HTTP/1.1 200 OK\r\nContent-Type: "
-              "text/plain\r\nConnection: close\r\n\r\n");
-    if (message.empty())
-        mg_printf(conn, "Event triggered.\n");
-    else
-        mg_printf(conn, "%s\n", message.c_str());
+    char buffer[46], *helper;
+    std::ifstream configFile(filename);
+    while (!configFile.eof())
+    {
+        configFile.getline(buffer, 46);
+        if (strlen(buffer) > 0)
+        {
+            helper = buffer;
 
-    sync->val++;
-    sync->cv.notify_all();
+            if (strchr(helper, ':'))    // File specifies a port
+            {
+                helper = strtok(helper, ":");
+                relevantIPs.push_back(std::string(helper));
 
-    return true;
+                helper = strtok(NULL, ":");
+                relevantPorts.push_back(atoi(helper));
+            }
+            else                        // We use a default port
+            {
+                relevantIPs.push_back(std::string(helper));
+
+                relevantPorts.push_back(atoi(DEFAULT_PRSONA_PORT_STR));
+            }
+        }
+    }
 }
 
-bool AltRemoteControlHandler::handleGet(CivetServer *server, struct mg_connection *conn)
+void load_single_instance_config(
+    std::string& relevantIP,
+    std::string& relevantPortStr,
+    int& relevantPort,
+    const char *filename)
 {
-    std::unique_lock<std::mutex> lck(sync->mtx);
-
-    const struct mg_request_info *info = mg_get_request_info(conn);
-    if (info->query_string)
-        query = info->query_string;
+    char buffer[46], *helper;
+    std::ifstream configFile(filename);
+    while (!configFile.eof())
+    {
+        configFile.getline(buffer, 46);
+        if (strlen(buffer) > 0)
+        {
+            helper = buffer;
 
-    mg_printf(conn,
-              "HTTP/1.1 200 OK\r\nContent-Type: "
-              "text/plain\r\nConnection: close\r\n\r\n");
-    if (message.empty())
-        mg_printf(conn, "Event triggered.\n");
-    else
-        mg_printf(conn, "%s\n", message.c_str());
+            if (strchr(helper, ':'))    // File specifies a port
+            {
+                helper = strtok(helper, ":");
+                relevantIP = helper;
 
-    sync->val2 = value;
-    sync->cv.notify_all();
+                helper = strtok(NULL, ":");
+                relevantPortStr = helper;
+                relevantPort = stoi(relevantPortStr);
+            }
+            else                        // We use default port
+            {
+                relevantIP = helper;
 
-    return true;
+                relevantPortStr = DEFAULT_PRSONA_PORT_STR;
+                relevantPort = stoi(relevantPortStr);
+            }
+        }
+    }
 }
 
-std::string AltRemoteControlHandler::getQuery() const
+/***********************************************************
+ ****                                                   ****
+ ****  "private" functions to help the generic helpers  ****
+ ****                                                   ****
+ ***********************************************************/
+
+std::string random_string(
+    std::default_random_engine& rng,
+    size_t length)
 {
-    return query;
+    const char charset[] =
+        "0123456789_-"
+        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+        "abcdefghijklmnopqrstuvwxyz";
+
+    const size_t max_index = (sizeof(charset) - 1);
+    std::uniform_int_distribution<int> dist(0, max_index - 1);
+
+    auto randchar = [&]() -> char
+    {
+        return charset[ dist(rng) ];
+    };
+
+    std::string retval(length, 0);
+
+    std::generate_n(retval.begin(), length, randchar);
+
+    return retval;
 }
 
+/***************************************
+ ****                               ****
+ ****  websocket handler functions  ****
+ ****                               ****
+ ***************************************/
+
+/*
+ * NULL
+ */
+
 int empty_websocket_data_handler(
     struct mg_connection *conn,
     int bits,
     char *data,
     size_t data_len,
     void *user_data)
-{ return false; }
+{
+    return false;
+}
 
 void empty_websocket_close_handler(
     const struct mg_connection *conn,
     void *user_data)
 { /* */ }
 
+/*
+ * SYNCHRONIZATION
+ */
+
 int synchro_websocket_data_handler(
     struct mg_connection *conn,
     int bits,
@@ -105,21 +174,27 @@ int synchro_websocket_data_handler(
     size_t data_len,
     void *user_data)
 {
-    if ((bits & 0xf) == MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE)
-        return false;
+    struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
+    std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
 
-    if ((bits & 0xf) == MG_WEBSOCKET_OPCODE_DATACOMPLETE)
+    switch (bits & 0xf)
     {
-        struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
+        case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
+            break;
 
-        std::unique_lock<std::mutex> lck(sync->mtx);
-        sync->val++;
+        // Responder has indicated receipt of submitted data
+        case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
+            lck.lock();
+            sync->val++;
+            break;
 
-        return false;
+        // Something strange has happened
+        default:
+            std::cerr << "Unknown packet type received. Failing." << std::endl;
+            break;
     }
 
-    std::cerr << "Unknown response when trying to get update lock." << std::endl;
-    return false;    
+    return false;
 }
 
 void synchro_websocket_close_handler(
@@ -133,6 +208,10 @@ void synchro_websocket_close_handler(
     synch->cv.notify_all();
 }
 
+/*
+ * RECEIVE SERIALIZED DATA
+ */
+
 int file_websocket_data_handler(
     struct mg_connection *conn,
     int bits,
@@ -140,34 +219,226 @@ int file_websocket_data_handler(
     size_t data_len,
     void *user_data)
 {
-    if ((bits & 0xf) == MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE || (bits & 0xf) == MG_WEBSOCKET_OPCODE_DATACOMPLETE)
-        return false;
+    struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
+    char *filename = (char *) mg_get_user_connection_data(conn);
+    FILE *currFile = NULL;
+    std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
 
-    if ((bits & 0xf) != MG_WEBSOCKET_OPCODE_BINARY && (bits & 0xf) != MG_WEBSOCKET_OPCODE_CONTINUATION)
+    switch (bits & 0xf)
     {
-        std::cerr << "Unknown opcode: failing." << std::endl;
-        return false;
+        // Responder has indicated they have sent all relevant data
+        case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
+        case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
+            break;
+
+        // Responder has sent more data (which may theoretically be broken up into multiple packets)
+        case MG_WEBSOCKET_OPCODE_BINARY:
+        case MG_WEBSOCKET_OPCODE_CONTINUATION:
+            lck.lock();
+            currFile = fopen(filename, "ab");
+            fwrite(data, sizeof(char), data_len, currFile);
+            fclose(currFile);
+            return true;
+
+        // Something strange has happened
+        default:
+            std::cerr << "Unknown packet type received. Failing." << std::endl;
+            break;
     }
 
+    return false;
+}
+
+void file_websocket_close_handler(
+    const struct mg_connection *conn,
+    void *user_data)
+{
     struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
-    char *filename = (char *) mg_get_user_connection_data(conn);
 
     std::unique_lock<std::mutex> lck(sync->mtx);
-    FILE *currFile = fopen(filename, "ab");
-    fwrite(data, sizeof(char), data_len, currFile);
-    fclose(currFile);
+    sync->val = 1;
+    sync->val2 = 1;
+    sync->cv.notify_all();
+}
 
-    return true;
+/*
+ * SYNCHRONIZATION AND RECEIVE SERIALIZED DATA
+ */
+
+int epoch_websocket_data_handler(
+    struct mg_connection *conn,
+    int bits,
+    char *data,
+    size_t data_len,
+    void *user_data)
+{
+    struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
+    char *filename = (char *) mg_get_user_connection_data(conn);
+    FILE *currFile = NULL;
+    std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
+
+    switch (bits & 0xf)
+    {
+        case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
+            break;
+
+        // Responder has indicated they have sent all relevant data
+        case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
+            lck.lock();
+            sync->val++;
+            break;
+
+        // Responder has sent more data (which may theoretically be broken up into multiple packets)
+        case MG_WEBSOCKET_OPCODE_BINARY:
+        case MG_WEBSOCKET_OPCODE_CONTINUATION:
+            lck.lock();
+            currFile = fopen(filename, "ab");
+            fwrite(data, sizeof(char), data_len, currFile);
+            fclose(currFile);
+            return true;
+
+        // Something strange has happened
+        default:
+            std::cerr << "Unknown packet type received. Failing." << std::endl;
+            break;
+    }
+
+    return false;
 }
 
-void file_websocket_close_handler(
+void epoch_websocket_close_handler(
     const struct mg_connection *conn,
     void *user_data)
 {
     struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
 
     std::unique_lock<std::mutex> lck(sync->mtx);
-    sync->val = 1;
     sync->val2 = 1;
     sync->cv.notify_all();
 }
+
+/*
+ * SPECIAL FOR HANDLING UNUSUAL DATA
+ */
+
+int clients_websocket_data_handler(
+    struct mg_connection *conn,
+    int bits,
+    char *data,
+    size_t data_len,
+    void *user_data)
+{
+    struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
+    std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
+
+    switch (bits & 0xf)
+    {
+        // Responder has indicated they have sent all relevant data
+        case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
+        case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
+            break;
+
+        // Responder has sent data
+        case MG_WEBSOCKET_OPCODE_BINARY:
+            lck.lock();
+            if (data_len == sizeof(sync->val))
+                sync->val = *((size_t *) data);
+            break;
+
+        // Something strange has happened
+        default:
+            std::cerr << "Unknown packet type received. Failing." << std::endl;
+            break;
+    }
+
+    return false;
+}
+
+/********************************************
+ ****                                    ****
+ ****  Generic handler member functions  ****
+ ****                                    ****
+ ********************************************/
+
+/*
+ * EXIT SYNCHRONIZATION HANDLER
+ */
+
+RemoteControlHandler::RemoteControlHandler(
+    struct synchronization_tool *sync)
+: sync(sync)
+{ /* */ }
+
+RemoteControlHandler::RemoteControlHandler(
+    struct synchronization_tool *sync,
+    const std::string& message)
+: sync(sync), message(message)
+{ /* */ }
+
+bool RemoteControlHandler::handleGet(
+    CivetServer *server,
+    struct mg_connection *conn)
+{
+    std::unique_lock<std::mutex> lck(sync->mtx);
+
+    mg_printf(conn, "HTTP/1.1 200 OK\r\n"
+                    "Content-Type: text/plain\r\n"
+                    "Connection: close\r\n\r\n");
+
+    if (message.empty())
+        mg_printf(conn, "Event triggered.\n");
+    else
+        mg_printf(conn, "%s\n", message.c_str());
+
+    sync->val++;
+    sync->cv.notify_all();
+
+    return true;
+}
+
+/*
+ * EXPERIMENT EVENT SYNCHRONIZATION HANDLER
+ */
+
+AltRemoteControlHandler::AltRemoteControlHandler(
+    size_t value,
+    struct synchronization_tool *sync)
+: value(value), sync(sync)
+{ /* */ }
+
+AltRemoteControlHandler::AltRemoteControlHandler(
+    size_t value,
+    struct synchronization_tool *sync,
+    const std::string& message)
+: value(value), sync(sync), message(message)
+{ /* */ }
+
+bool AltRemoteControlHandler::handleGet(
+    CivetServer *server,
+    struct mg_connection *conn)
+{
+    std::unique_lock<std::mutex> lck(sync->mtx);
+
+    const struct mg_request_info *info = mg_get_request_info(conn);
+    if (info->query_string)
+        query = info->query_string;
+
+    mg_printf(conn, "HTTP/1.1 200 OK\r\n"
+                    "Content-Type: text/plain\r\n"
+                    "Connection: close\r\n\r\n");
+
+    if (message.empty())
+        mg_printf(conn, "Event triggered.\n");
+    else
+        mg_printf(conn, "%s\n", message.c_str());
+
+    sync->val2 = value;
+    sync->cv.notify_all();
+
+    return true;
+}
+
+std::string AltRemoteControlHandler::getQuery() const
+{
+    return query;
+}

+ 65 - 413
prsona/src/orchestratorMain.cpp

@@ -1,400 +1,82 @@
+/**
+ * orchestratorMain.cpp
+ *      - compiles to bin/orchestrator
+ *      - initiates a set of servers and clients, then commands them, for PRSONA experiments
+ *
+ * Stan Gurtler
+ */
+
 #include <iostream>
-#include <random>
 #include <fstream>
-#include <sstream>
-#include <string>
-#include <cstring>
-#include <cstdlib>
-#include <vector>
-#include <chrono>
 #include <thread>
 
-#include "print_helpers.hpp"
-#include "networking.hpp"
+#include "networkOrchestrator.hpp"
 
 using namespace std;
 
-const chrono::seconds ONE_SECOND(1); 
-const chrono::seconds FIVE_SECONDS(5); 
-
-int clients_websocket_data_handler(
-    struct mg_connection *conn,
-    int bits,
-    char *data,
-    size_t data_len,
-    void *user_data)
-{
-    if ((bits & 0xf) == MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE || (bits & 0xf) == MG_WEBSOCKET_OPCODE_DATACOMPLETE)
-        return false;
-
-    if ((bits & 0xf) == MG_WEBSOCKET_OPCODE_BINARY)
-    {
-        struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
-
-        unique_lock<mutex> lck(sync->mtx);
-        
-        if (data_len == sizeof(sync->val))
-            sync->val = *((size_t *) data);
-
-        return false;
-    }
-
-    std::cerr << "Unknown response when trying to get update lock." << std::endl;
-    return false;    
-}
-
-int start_remote_actor(string target, bool server, string id, string output)
-{
-    stringstream buffer;
-    string command;
-
-    // buffer << "ssh tmgurtle@" << target << " \"screen \'~/prsona/prsona/scripts/startup.sh "
-    //     << (server ? "server " : "client ") << id << " t " << output << "\'\"" ;
-
-    buffer << "bin/" << (server ? "server " : "client ") << id << " T " << output << " &";
-    command = buffer.str();
-
-    return system(command.c_str());
-}
-
-void wait_for_servers_ready(string dealer, int dealerPort)
-{
-    bool flag = false;
-    while (!flag)
-    {
-        this_thread::sleep_for(ONE_SECOND);
-
-        stringstream sysString;
-        string data;
-
-        struct mg_connection *conn =
-            mg_connect_client(
-                dealer.c_str(), 
-                dealerPort,
-                USE_SSL, 
-                NULL,
-                0);
-
-        if (!conn)
-        {
-            cerr << "couldn't make connection while waiting for servers ready" << endl;
-            continue;
-        }
-
-        sysString << "GET " << EPOCH_READY_URI << " HTTP/1.1\r\n";
-        sysString << "Host: " << dealer << ":" << dealerPort << "\r\n\r\n";
-        data = sysString.str();
-
-        mg_write(conn, data.c_str(), data.length());
-        mg_get_response(conn, NULL, 0, 250);
-        const struct mg_response_info *info = mg_get_response_info(conn);
-
-        if (info->status_code == 200)
-            flag = true;
-
-        mg_close_connection(conn);
-    }
-}
-
-void wait_for_clients_ready(string dealer, int dealerPort, size_t numClients)
-{
-    struct synchronization_tool numClientsSync;
-
-    bool flag = false;
-    while (!flag)
-    {
-        this_thread::sleep_for(ONE_SECOND);
-
-        stringstream sysString;
-        string data;
-
-        struct mg_connection *conn =
-            mg_connect_websocket_client(
-                dealer.c_str(),
-                dealerPort,
-                USE_SSL,
-                NULL,
-                0,
-                NUM_CLIENTS_URI,
-                "null",
-                clients_websocket_data_handler,
-                synchro_websocket_close_handler,
-                &numClientsSync);
-
-        if (!conn)
-        {
-            cerr << "couldn't make connection while waiting for clients ready" << endl;
-            continue;
-        }
-
-        unique_lock<mutex> lck(numClientsSync.mtx);
-        numClientsSync.val = 0;
-        numClientsSync.val2 = 0;
-
-        mg_websocket_client_write(
-            conn,
-            MG_WEBSOCKET_OPCODE_DATACOMPLETE,
-            "",
-            0);
-
-        while (!numClientsSync.val2)
-            numClientsSync.cv.wait(lck);
-
-        mg_close_connection(conn);
-
-        if (numClientsSync.val == numClients)
-            flag = true;
-    }
-}
-
-void trigger_epoch(string dealer, int dealerPort)
-{
-    wait_for_servers_ready(dealer, dealerPort);
-
-    bool flag = false;
-    while (!flag)
-    {
-        stringstream sysString;
-        string data;
-
-        struct mg_connection *conn =
-            mg_connect_client(
-                dealer.c_str(), 
-                dealerPort,
-                USE_SSL, 
-                NULL,
-                0);
-
-        if (!conn)
-            continue;
-
-        sysString << "GET " << TRIGGER_EPOCH_URI << " HTTP/1.1\r\n";
-        sysString << "Host: " << dealer << "\r\n\r\n";
-        data = sysString.str();
-
-        mg_write(conn, data.c_str(), data.length());
-        mg_close_connection(conn);
-
-        flag = true;
-    }
-
-    wait_for_servers_ready(dealer, dealerPort);
-}
-
-void trigger_vote(string target, int port)
-{
-    bool flag = false;
-    while (!flag)
-    {
-        stringstream sysString;
-        string data;
-
-        struct mg_connection *conn =
-            mg_connect_client(
-                target.c_str(), 
-                port,
-                USE_SSL, 
-                NULL,
-                0);
-
-        if (!conn)
-            continue;
-
-        sysString << "GET " << TRIGGER_VOTE_URI << " HTTP/1.1\r\n";
-        sysString << "Host: " << target << "\r\n\r\n";
-        data = sysString.str();
-
-        mg_write(conn, data.c_str(), data.length());
-        mg_close_connection(conn);
-
-        flag = true;
-    }
-}
-
-void trigger_reputation_proof(string target, int targetPort, string verifier, int verifierPort)
-{
-    bool flag = false;
-    while (!flag)
-    {
-        stringstream sysString;
-        string data;
-
-        struct mg_connection *conn =
-            mg_connect_client(
-                target.c_str(), 
-                targetPort,
-                USE_SSL, 
-                NULL,
-                0);
-
-        if (!conn)
-            continue;
-
-        sysString << "GET " << TRIGGER_REP_URI << "?" << verifier << ":" << verifierPort << " HTTP/1.1\r\n";
-        sysString << "Host: " << target << "\r\n\r\n";
-        data = sysString.str();
-
-        mg_write(conn, data.c_str(), data.length());
-        mg_close_connection(conn);
-
-        flag = true;
-    }
-
-    this_thread::sleep_for(ONE_SECOND);
-}
-
-void execute_experiment(string dealer, int dealerPort)
-{
-    size_t line = 1;
-
-    char buffer[128];
-    ifstream commands("cfg/commands.cfg");
-
-    while (!commands.eof())
-    {
-        commands.getline(buffer, 128);
-        if (strlen(buffer) == 0)
-        {
-            line++;
-            continue;
-        }
-
-        cout << "Command " << line << ": " << string(buffer) << endl;
-
-        switch(buffer[0])
-        {
-            case 'V':
-                char *voter, *voterPort;
-                voter = strtok(buffer + 1, " :");
-                voterPort = strtok(NULL, " :");
-
-                trigger_vote(string(voter), atoi(voterPort));
-                break;
-
-            case 'R':
-                char *target, *targetPortStr, *verifier, *verifierPortStr;
-                target = strtok(buffer + 1, " :");
-                targetPortStr = strtok(NULL, " :");
-                verifier = strtok(NULL, " :");
-                verifierPortStr = strtok(NULL, " :");
-
-                trigger_reputation_proof(
-                    string(target),
-                    atoi(targetPortStr),
-                    string(verifier),
-                    atoi(verifierPortStr));
-                break;
-
-            case 'E':
-                trigger_epoch(dealer, dealerPort);
-                break;
-
-            default:
-                break;
-        }
-
-        line++;
-    }
-
-    wait_for_servers_ready(dealer, dealerPort);
-}
-
+/**
+ * This program (bin/orchestrator) expects to be called as follows:
+ * `bin/orchestrator <output> <servers_are_malicious>`
+ *
+ * <output> - a string that will name the files in which outputs for this run of
+ *      the experiment will be written (that is, timings and traffic data)
+ * <servers_are_malicious> - a bool (given as T/t or F/f)
+ *      which is true when servers are in malicious security
+ *      and false when they are in HBC security
+ */
 int main(int argc, char* argv[])
 {
-    string seedStr = "seed";
-
-    seed_seq seed(seedStr.begin(), seedStr.end());
-    default_random_engine rng(seed);
-
-    string experimentOutput = random_string(rng, 8);
+    /*
+     * PRELIMINARY SETUP CODE
+     */
 
 #if USE_SSL
-    mg_init_library(0);
-#else
     mg_init_library(MG_FEATURES_SSL);
+#else
+    mg_init_library(0);
 #endif
 
+    string output = "default";
+    if (argc > 1)
+        output = argv[1];
+
+    // Default to malicious security if not specified
+    bool maliciousServers = true;
+    if (argc > 2)
+    {
+        bool setting = argv[2][0] == 't' || argv[2][0] == 'T';
+        maliciousServers = setting;
+    }
+
     vector<string> serverIPs, clientIPs;
     vector<int> serverPorts, clientPorts;
-    string dealerIP;
+    string dealerIP, dealerPortStr;
     int dealerPort = 0;
 
-    char buffer[46], *helper;
-    ifstream serverConfig("cfg/serverIPs.cfg");
-    while (!serverConfig.eof())
-    {
-        serverConfig.getline(buffer, 46);
-        if (strlen(buffer) > 0)
-        {
-            helper = buffer;
-            if (strchr(helper, ':'))
-            {
-                helper = strtok(helper, ":");
-                serverIPs.push_back(string(helper));
-                helper = strtok(NULL, ":");
-                serverPorts.push_back(atoi(helper));
-            }
-            else
-            {
-                serverIPs.push_back(string(helper));
-                serverPorts.push_back(atoi(PRSONA_PORT_STR));
-            }
-        }
-    }
+    // Read in from config files the server locations
+    load_multiple_instances_config(serverIPs, serverPorts, "cfg/serverIPs.cfg");
 
-    ifstream clientConfig("cfg/clientIPs.cfg");
-    while (!clientConfig.eof())
-    {
-        clientConfig.getline(buffer, 46);
-        if (strlen(buffer) > 0)
-        {
-            helper = buffer;
-            if (strchr(helper, ':'))
-            {
-                helper = strtok(helper, ":");
-                clientIPs.push_back(string(helper));
-                helper = strtok(NULL, ":");
-                clientPorts.push_back(atoi(helper));
-            }
-            else
-            {
-                clientIPs.push_back(string(helper));
-                clientPorts.push_back(atoi(PRSONA_PORT_STR));
-            }
-        }
-    }
+    // And now the client locations
+    load_multiple_instances_config(clientIPs, clientPorts, "cfg/clientIPs.cfg");
 
-    ifstream dealerConfig("cfg/dealerIP.cfg");
-    while (!dealerConfig.eof())
-    {
-        dealerConfig.getline(buffer, 46);
-        if (strlen(buffer) > 0)
-        {
-            helper = buffer;
-            if (strchr(helper, ':'))
-            {
-                helper = strtok(helper, ":");
-                dealerIP = helper;
-                helper = strtok(NULL, ":");
-                dealerPort = atoi(helper);
-            }
-            else
-            {
-                dealerIP = helper;
-                dealerPort = atoi(PRSONA_PORT_STR);
-            }
-        }
-    }
+    // And finally the dealer location
+    load_single_instance_config(dealerIP, dealerPortStr, dealerPort, "cfg/dealerIP.cfg");
 
     size_t numServers = serverIPs.size();
     size_t numClients = clientIPs.size();
 
-    cout << "[ORC] This experiment is running with output code: " << experimentOutput << endl;
+    /*
+     * ORCHESTRATOR SETUP CODE
+     */
+
+    cout << "[ORC] This experiment is running with output code: " << output << endl;
+    cout << "[ORC] This experiment is running with " << (maliciousServers ? "MALICIOUS" : "HBC") << " servers." << endl;
+    cout << endl;
+    
     cout << "[ORC] Starting BGN dealer server." << endl;
 
     vector<thread> serverStartup, clientStartup;
-    serverStartup.push_back(thread(start_remote_actor, dealerIP, true, "d", experimentOutput));
-
+    serverStartup.push_back(thread(start_remote_actor, dealerIP, true, "d", output, maliciousServers));
     this_thread::sleep_for(ONE_SECOND);
 
     cout << "[ORC] Starting other servers." << endl;
@@ -404,7 +86,7 @@ int main(int argc, char* argv[])
         if (serverIPs[i] == dealerIP && serverPorts[i] == dealerPort)
             continue;
 
-        serverStartup.push_back(thread(start_remote_actor, serverIPs[i], true, "s" + to_string(i), experimentOutput));
+        serverStartup.push_back(thread(start_remote_actor, serverIPs[i], true, "s" + to_string(i), output, maliciousServers));
     }
 
     cout << "[ORC] Waiting for confirmation that servers are ready to continue." << endl;
@@ -418,7 +100,7 @@ int main(int argc, char* argv[])
 
     for (size_t i = 0; i < numClients; i++)
     {
-        clientStartup.push_back(thread(start_remote_actor, clientIPs[i], false, "c" + to_string(i), experimentOutput));
+        clientStartup.push_back(thread(start_remote_actor, clientIPs[i], false, "c" + to_string(i), output, maliciousServers));
         this_thread::sleep_for(ONE_SECOND);
     }
 
@@ -429,56 +111,26 @@ int main(int argc, char* argv[])
 
     wait_for_clients_ready(dealerIP, dealerPort, numClients);
 
+    /*
+     * MAIN ORCHESTRATOR LOOP CODE
+     */
+
     cout << "[ORC] Beginning experiment." << endl;
 
-    execute_experiment(dealerIP, dealerPort);
+    execute_experiment(dealerIP, dealerPort, "cfg/commands.cfg");
+
+    /*
+     * SHUTDOWN CODE
+     */
 
     cout << "[ORC] Finishing experiment." << endl;
     cout << "[ORC] Sending shutdown commands to clients." << endl;
 
-    for (size_t i = 0; i < clientIPs.size(); i++)
-    {
-        stringstream sysString;
-        string data;
-
-        struct mg_connection *conn =
-            mg_connect_client(
-                clientIPs[i].c_str(), 
-                clientPorts[i],
-                USE_SSL, 
-                NULL,
-                0);
-
-        sysString << "GET " << EXIT_URI << " HTTP/1.1\r\n";
-        sysString << "Host: " << clientIPs[i] << "\r\n\r\n";
-        data = sysString.str();
-
-        mg_write(conn, data.c_str(), data.length());
-        mg_close_connection(conn);
-    }
+    shut_down_remote_actors(clientIPs, clientPorts);
 
     cout << "[ORC] Sending shutdown commands to servers." << endl;
 
-    for (size_t i = 0; i < serverIPs.size(); i++)
-    {
-        stringstream sysString;
-        string data;
-
-        struct mg_connection *conn =
-            mg_connect_client(
-                serverIPs[i].c_str(), 
-                serverPorts[i],
-                USE_SSL, 
-                NULL,
-                0);
-
-        sysString << "GET " << EXIT_URI << " HTTP/1.1\r\n";
-        sysString << "Host: " << serverIPs[i] << ":" << serverPorts[i] << "\r\n\r\n";
-        data = sysString.str();
-
-        mg_write(conn, data.c_str(), data.length());
-        mg_close_connection(conn);
-    }
+    shut_down_remote_actors(serverIPs, serverPorts);
 
     return 0;
 }

+ 14 - 11
prsona/src/proof.cpp

@@ -5,16 +5,13 @@
  */
 
 // Convert the bytes to a single integer, then make that a Scalar
-Scalar bytes_to_scalar(const std::vector<uint8_t>& bytes)
+Scalar bytes_to_scalar(
+    const std::vector<uint8_t>& bytes)
 {
     std::stringstream stream;
     for (uint8_t b : bytes)
-    {
-        stream << std::setw(2)
-            << std::setfill('0')
-            << std::hex
-            << static_cast<int>(b);
-    }
+        stream << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(b);
+
     mpz_class value;
     value.set_str(stream.str(), 16);
 
@@ -22,7 +19,8 @@ Scalar bytes_to_scalar(const std::vector<uint8_t>& bytes)
 }
 
 // Random Oracle (i.e. SHA3_256)
-Scalar oracle(const std::string& input)
+Scalar oracle(
+    const std::string& input)
 {
     uint32_t digest_length = SHA256_DIGEST_LENGTH;
     const EVP_MD* algorithm = EVP_sha3_256();
@@ -50,7 +48,8 @@ void Proof::clear()
     responseParts.clear();
 }
 
-bool Proof::operator==(const Proof& b)
+bool Proof::operator==(
+    const Proof& b)
 {
     bool retval = this->hbc == b.hbc;
     
@@ -74,7 +73,9 @@ bool Proof::operator==(const Proof& b)
     return retval;
 }
 
-std::ostream& operator<<(std::ostream& os, const Proof& output)
+std::ostream& operator<<(
+    std::ostream& os,
+    const Proof& output)
 {
     BinaryBool hbc(!output.hbc.empty());
     os << hbc;
@@ -114,7 +115,9 @@ std::ostream& operator<<(std::ostream& os, const Proof& output)
     return os;
 }
 
-std::istream& operator>>(std::istream& is, Proof& input)
+std::istream& operator>>(
+    std::istream& is,
+    Proof& input)
 {
     BinaryBool hbc;
     is >> hbc;

+ 90 - 320
prsona/src/server.cpp

@@ -11,14 +11,17 @@
  */
 
 // Used to generate the first server; instantiates BGN for the first time
-PrsonaServer::PrsonaServer(size_t numServers)
+PrsonaServer::PrsonaServer(
+    size_t numServers)
 : numServers(numServers)
 {
     currentSeed.set_random();
 }
 
 // Used for all other servers, so they have the same BGN parameters
-PrsonaServer::PrsonaServer(size_t numServers, const BGN& otherBgn)
+PrsonaServer::PrsonaServer(
+    size_t numServers,
+    const BGN& otherBgn)
 : numServers(numServers), bgnSystem(otherBgn)
 {
     currentSeed.set_random();
@@ -43,7 +46,8 @@ size_t PrsonaServer::get_num_servers() const
     return numServers;
 }
 
-Twistpoint PrsonaServer::get_fresh_generator(std::vector<Proof>& pi) const
+Twistpoint PrsonaServer::get_fresh_generator(
+    std::vector<Proof>& pi) const
 {
     pi = currentGeneratorProof;
     return currentFreshGenerator;
@@ -84,7 +88,8 @@ Twistpoint PrsonaServer::add_next_seed_to_generator(
  * In practice, this is intended for clients,
  * who need to know their current votes in order to rerandomize them. */
 std::vector<TwistBipoint> PrsonaServer::get_current_votes_by(
-    Proof& pi, const Twistpoint& shortTermPublicKey) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey) const
 {
     std::vector<TwistBipoint> retval;
     size_t voteSubmitter = binary_search(shortTermPublicKey);
@@ -106,7 +111,8 @@ std::vector<std::vector<TwistBipoint>> PrsonaServer::get_all_current_votes(
  * In practice, this is intended for clients, so that the servers vouch
  * for their ciphertexts being valid as part of their reputation proofs. */
 EGCiphertext PrsonaServer::get_current_user_encrypted_tally(
-    Proof& pi, const Twistpoint& shortTermPublicKey) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey) const
 {
     EGCiphertext retval;
     size_t tallyOwner = binary_search(shortTermPublicKey);
@@ -117,7 +123,8 @@ EGCiphertext PrsonaServer::get_current_user_encrypted_tally(
 }
 
 CurveBipoint PrsonaServer::get_current_server_encrypted_tally(
-    Proof& pi, const Twistpoint& shortTermPublicKey) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey) const
 {
     CurveBipoint retval;
     size_t tallyOwner = binary_search(shortTermPublicKey);
@@ -127,7 +134,8 @@ CurveBipoint PrsonaServer::get_current_server_encrypted_tally(
     return retval;
 }
 
-std::vector<Twistpoint> PrsonaServer::get_current_pseudonyms(Proof& pi) const
+std::vector<Twistpoint> PrsonaServer::get_current_pseudonyms(
+    Proof& pi) const
 {    
     pi = generate_valid_pseudonyms_proof(currentPseudonyms);
     return currentPseudonyms;
@@ -142,7 +150,8 @@ std::vector<Twistpoint> PrsonaServer::get_current_pseudonyms() const
  * PROOF COMMITMENT GETTERS
  */
 
-Proof PrsonaServer::get_vote_row_commitment(const Twistpoint& request) const
+Proof PrsonaServer::get_vote_row_commitment(
+    const Twistpoint& request) const
 {
     size_t requestID = binary_search(request);
     return generate_valid_vote_row_proof(voteMatrix[requestID]);
@@ -153,13 +162,15 @@ Proof PrsonaServer::get_vote_matrix_commitment() const
     return generate_valid_vote_matrix_proof(voteMatrix);
 }
 
-Proof PrsonaServer::get_user_tally_commitment(const Twistpoint& request) const
+Proof PrsonaServer::get_user_tally_commitment(
+    const Twistpoint& request) const
 {
     size_t requestID = binary_search(request);
     return generate_valid_user_tally_proof(currentUserEncryptedTallies[requestID]);
 }
 
-Proof PrsonaServer::get_server_tally_commitment(const Twistpoint& request) const
+Proof PrsonaServer::get_server_tally_commitment(
+    const Twistpoint& request) const
 {
     size_t requestID = binary_search(request);
     return generate_valid_server_tally_proof(previousVoteTallies[requestID]);
@@ -203,8 +214,7 @@ void PrsonaServer::add_new_client(
     const Proof& proofOfValidKey,
     const Twistpoint& shortTermPublicKey)
 {
-    if (!verify_ownership_proof(
-            proofOfValidKey, currentFreshGenerator, shortTermPublicKey))
+    if (!verify_ownership_proof(proofOfValidKey, currentFreshGenerator, shortTermPublicKey))
     {
         std::cerr << "Could not verify proof of valid key." << std::endl;
         return;
@@ -215,26 +225,21 @@ void PrsonaServer::add_new_client(
     // The first epoch's score for a new user will be low,
     // but will typically converge on an average score quickly
     Scalar tallySeed;
-    CurveBipoint encryptedDefaultTally =
-        bgnSystem.get_public_key().curveEncrypt(tallySeed, DEFAULT_TALLY);
+    CurveBipoint encryptedDefaultTally = bgnSystem.get_public_key().curveEncrypt(tallySeed, DEFAULT_TALLY);
     previousVoteTallies.push_back(encryptedDefaultTally);
 
     Scalar seedForUserTally;
     seedForUserTally.set_random();
     EGCiphertext newUserEncryptedTally;
     newUserEncryptedTally.mask = shortTermPublicKey * seedForUserTally;
-    newUserEncryptedTally.encryptedMessage =
-        currentFreshGenerator * seedForUserTally +
-        elGamalBlindGenerator * DEFAULT_TALLY;
+    newUserEncryptedTally.encryptedMessage = currentFreshGenerator * seedForUserTally + elGamalBlindGenerator * DEFAULT_TALLY;
     currentUserEncryptedTallies.push_back(newUserEncryptedTally);
 
     // Users are defaulted to casting a neutral vote for others.
     TwistBipoint encryptedDefaultVote, encryptedSelfVote;
     Scalar currDefaultSeed, currSelfSeed;
-    encryptedDefaultVote = 
-        bgnSystem.get_public_key().twistEncrypt(currDefaultSeed, DEFAULT_VOTE);
-    encryptedSelfVote =
-        bgnSystem.get_public_key().twistEncrypt(currSelfSeed, Scalar(MAX_ALLOWED_VOTE));
+    encryptedDefaultVote = bgnSystem.get_public_key().twistEncrypt(currDefaultSeed, DEFAULT_VOTE);
+    encryptedSelfVote = bgnSystem.get_public_key().twistEncrypt(currSelfSeed, Scalar(MAX_ALLOWED_VOTE));
 
     std::vector<TwistBipoint> newRow;
     std::vector<Scalar> userVoteSeeds;
@@ -280,11 +285,7 @@ void PrsonaServer::add_new_client(
         newOtherVoteSeeds.push_back(otherVoteSeeds[sortOrder[i]]);
     }
 
-    proofOfValidAddition = generate_proof_of_added_user(
-        tallySeed,
-        seedForUserTally,
-        newUserVoteSeeds,
-        newOtherVoteSeeds);
+    proofOfValidAddition = generate_proof_of_added_user(tallySeed, seedForUserTally, newUserVoteSeeds, newOtherVoteSeeds);
 }
 
 // Receive a new vote row from a user (identified by short term public key).
@@ -372,9 +373,7 @@ std::vector<Scalar> PrsonaServer::tally_scores()
         // ZIP
         for (size_t j = 0; j < previousVoteTallies.size(); j++)
         {
-            Quadripoint curr =
-                bgnSystem.homomorphic_multiplication_no_rerandomize(
-                    previousVoteTallies[j], voteMatrix[j][i]);
+            Quadripoint curr = bgnSystem.homomorphic_multiplication_no_rerandomize(previousVoteTallies[j], voteMatrix[j][i]);
 
             weightedVotes.push_back(curr);
         }
@@ -382,11 +381,7 @@ std::vector<Scalar> PrsonaServer::tally_scores()
         // FOLDL
         Quadripoint currEncryptedTally = weightedVotes[0];
         for (size_t j = 1; j < weightedVotes.size(); j++)
-        {
-            currEncryptedTally =
-                bgnSystem.homomorphic_addition_no_rerandomize(
-                    currEncryptedTally, weightedVotes[j]);
-        }
+            currEncryptedTally = bgnSystem.homomorphic_addition_no_rerandomize(currEncryptedTally, weightedVotes[j]);
 
         // DECRYPT
         decryptedTallies.push_back(bgnSystem.decrypt(currEncryptedTally));
@@ -405,11 +400,7 @@ Scalar PrsonaServer::get_max_possible_score()
     // FOLDL
     CurveBipoint currEncryptedVal = previousVoteTallies[0];
     for (size_t i = 1; i < previousVoteTallies.size(); i++)
-    {
-        currEncryptedVal =
-            bgnSystem.homomorphic_addition_no_rerandomize(
-                currEncryptedVal, previousVoteTallies[i]);
-    }
+        currEncryptedVal = bgnSystem.homomorphic_addition_no_rerandomize(currEncryptedVal, previousVoteTallies[i]);
 
     // DECRYPT
     Scalar retval = bgnSystem.decrypt(currEncryptedVal);
@@ -458,19 +449,7 @@ void PrsonaServer::build_up_midway_pseudonyms(
     std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
     std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
     std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
-    pi.push_back(epoch_calculations(
-        currPermutationCommits,
-        currFreshPseudonymCommits,
-        currFreshPseudonymSeedCommits,
-        currServerTallyCommits,
-        currPartwayVoteMatrixCommits,
-        currFinalVoteMatrixCommits,
-        currUserTallyMaskCommits,
-        currUserTallyMessageCommits,
-        currUserTallySeedCommits,
-        nextSeed,
-        nextGenerator,
-        false));
+    pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, nextSeed, nextGenerator, false));
 
     permutationCommits.push_back(currPermutationCommits);
     freshPseudonymCommits.push_back(currFreshPseudonymCommits);
@@ -479,8 +458,7 @@ void PrsonaServer::build_up_midway_pseudonyms(
     partwayVoteMatrixCommits.push_back(currPartwayVoteMatrixCommits);
     finalVoteMatrixCommits.push_back(currFinalVoteMatrixCommits);
 
-    pi[0][0].push_back(
-        add_to_generator_proof(nextGenerator, nextSeed));
+    pi[0][0].push_back(add_to_generator_proof(nextGenerator, nextSeed));
     nextGenerator = nextGenerator * nextSeed;
 }
 
@@ -519,19 +497,7 @@ void PrsonaServer::break_down_midway_pseudonyms(
     std::vector<std::vector<Twistpoint>> currUserTallyMaskCommits;
     std::vector<std::vector<Twistpoint>> currUserTallyMessageCommits;
     std::vector<std::vector<Twistpoint>> currUserTallySeedCommits;
-    pi.push_back(epoch_calculations(
-        currPermutationCommits,
-        currFreshPseudonymCommits,
-        currFreshPseudonymSeedCommits,
-        currServerTallyCommits,
-        currPartwayVoteMatrixCommits,
-        currFinalVoteMatrixCommits,
-        currUserTallyMaskCommits,
-        currUserTallyMessageCommits,
-        currUserTallySeedCommits,
-        inverseSeed,
-        nextGenerator,
-        true));
+    pi.push_back(epoch_calculations(currPermutationCommits, currFreshPseudonymCommits, currFreshPseudonymSeedCommits, currServerTallyCommits, currPartwayVoteMatrixCommits, currFinalVoteMatrixCommits, currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, inverseSeed, nextGenerator, true));
 
     permutationCommits.push_back(currPermutationCommits);
     freshPseudonymCommits.push_back(currFreshPseudonymCommits);
@@ -566,94 +532,40 @@ std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
 {
     std::vector<std::vector<Proof>> retval;
 
-    std::vector<std::vector<Scalar>> permutations =
-        generate_permutation_matrix(power);
+    std::vector<std::vector<Scalar>> permutations = generate_permutation_matrix(power);
 
     std::vector<std::vector<Scalar>> permutationSeeds;
     permutationCommits.clear();
-    permutationCommits = 
-        generate_commitment_matrix(permutations, permutationSeeds);
+    permutationCommits = generate_commitment_matrix(permutations, permutationSeeds);
 
-    retval.push_back(generate_valid_permutation_proof(
-        permutations, permutationSeeds, permutationCommits));
+    retval.push_back(generate_valid_permutation_proof(permutations, permutationSeeds, permutationCommits));
 
     std::vector<std::vector<Scalar>> freshPseudonymSeeds;
     freshPseudonymSeedCommits.clear();
     freshPseudonymCommits.clear();
-    freshPseudonymCommits = 
-        generate_pseudonym_matrix(
-            permutations,
-            power,
-            freshPseudonymSeeds,
-            freshPseudonymSeedCommits);
-
-    retval.push_back(
-        generate_proof_of_reordering_plus_power(
-            permutations,
-            power,
-            permutationSeeds,
-            freshPseudonymSeeds,
-            currentPseudonyms,
-            permutationCommits,
-            freshPseudonymCommits,
-            freshPseudonymSeedCommits));
+    freshPseudonymCommits = generate_pseudonym_matrix(permutations, power, freshPseudonymSeeds, freshPseudonymSeedCommits);
+
+    retval.push_back(generate_proof_of_reordering_plus_power(permutations, power, permutationSeeds, freshPseudonymSeeds, currentPseudonyms, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits));
 
     std::vector<std::vector<Scalar>> serverTallySeeds;
     serverTallyCommits.clear();
-    serverTallyCommits = 
-        generate_server_tally_matrix(
-            permutations,
-            serverTallySeeds);
-
-    retval.push_back(
-        generate_proof_of_reordering<CurveBipoint>(
-            permutations,
-            permutationSeeds,
-            serverTallySeeds,
-            previousVoteTallies,
-            permutationCommits,
-            serverTallyCommits,
-            bgnSystem.get_public_key().get_bipoint_curvegen(),
-            bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen()));
+    serverTallyCommits = generate_server_tally_matrix(permutations, serverTallySeeds);
+
+    retval.push_back(generate_proof_of_reordering<CurveBipoint>(permutations, permutationSeeds, serverTallySeeds, previousVoteTallies, permutationCommits, serverTallyCommits, bgnSystem.get_public_key().get_bipoint_curvegen(), bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen()));
 
     std::vector<std::vector<std::vector<Scalar>>> partwayVoteMatrixSeeds;
     std::vector<std::vector<std::vector<Scalar>>> finalVoteMatrixSeeds;
     partwayVoteMatrixCommits.clear();
-    partwayVoteMatrixCommits = generate_vote_tensor(
-        permutations,
-        voteMatrix,
-        partwayVoteMatrixSeeds,
-        false);
+    partwayVoteMatrixCommits = generate_vote_tensor(permutations, voteMatrix, partwayVoteMatrixSeeds, false);
 
-    std::vector<std::vector<TwistBipoint>> partialVoteMatrix = 
-        calculate_next_vote_matrix(partwayVoteMatrixCommits);
+    std::vector<std::vector<TwistBipoint>> partialVoteMatrix = calculate_next_vote_matrix(partwayVoteMatrixCommits);
 
     finalVoteMatrixCommits.clear();
-    finalVoteMatrixCommits = generate_vote_tensor(
-        permutations,
-        partialVoteMatrix,
-        finalVoteMatrixSeeds,
-        true);
-
-    generate_vote_tensor_proofs(
-        retval,
-        permutations,
-        permutationSeeds,
-        partwayVoteMatrixSeeds,
-        voteMatrix,
-        permutationCommits,
-        partwayVoteMatrixCommits,
-        false);
-
-    generate_vote_tensor_proofs(
-        retval,
-        permutations,
-        permutationSeeds,
-        finalVoteMatrixSeeds,
-        partialVoteMatrix,
-        permutationCommits,
-        finalVoteMatrixCommits,
-        true);
+    finalVoteMatrixCommits = generate_vote_tensor(permutations, partialVoteMatrix, finalVoteMatrixSeeds, true);
+
+    generate_vote_tensor_proofs(retval, permutations, permutationSeeds, partwayVoteMatrixSeeds, voteMatrix, permutationCommits, partwayVoteMatrixCommits, false);
+
+    generate_vote_tensor_proofs(retval, permutations, permutationSeeds, finalVoteMatrixSeeds, partialVoteMatrix, permutationCommits, finalVoteMatrixCommits, true);
 
     if (doUserTallies)
     {
@@ -663,41 +575,13 @@ std::vector<std::vector<Proof>> PrsonaServer::epoch_calculations(
         userTallyMaskCommits.clear();
         userTallyMessageCommits.clear();
         userTallySeedCommits.clear();
-        generate_user_tally_matrix(
-            permutations,
-            power,
-            nextGenerator,
-            currentPseudonyms,
-            userTallyMasks,
-            userTallyMaskCommits,
-            userTallyMessages,
-            userTallyMessageCommits,
-            userTallySeeds,
-            userTallySeedCommits);
-
-        retval.push_back(
-            generate_user_tally_proofs(
-                permutations,
-                power,
-                nextGenerator,
-                permutationSeeds,
-                userTallySeeds,
-                currentPseudonyms,
-                userTallyMasks,
-                userTallyMessages,
-                permutationCommits,
-                userTallyMaskCommits,
-                userTallyMessageCommits,
-                userTallySeedCommits));
+        generate_user_tally_matrix(permutations, power, nextGenerator, currentPseudonyms, userTallyMasks, userTallyMaskCommits, userTallyMessages, userTallyMessageCommits, userTallySeeds, userTallySeedCommits);
+
+        retval.push_back(generate_user_tally_proofs(permutations, power, nextGenerator, permutationSeeds, userTallySeeds, currentPseudonyms, userTallyMasks, userTallyMessages, permutationCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits));
     }
 
     // Replace internal values
-    update_data(
-        freshPseudonymCommits,
-        serverTallyCommits,
-        finalVoteMatrixCommits,
-        userTallyMaskCommits,
-        userTallyMessageCommits);
+    update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
 
     return retval;
 }
@@ -729,8 +613,7 @@ bool PrsonaServer::accept_epoch_updates(
 
     size_t currOffset = 0;
 
-    verification =
-        verify_valid_permutation_proof(pi[currOffset], permutationCommits);
+    verification = verify_valid_permutation_proof(pi[currOffset], permutationCommits);
     currOffset++;
     if (!verification)
     {
@@ -738,13 +621,7 @@ bool PrsonaServer::accept_epoch_updates(
         return false;
     }
 
-    verification = 
-        verify_proof_of_reordering_plus_power(
-            pi[currOffset],
-            currentPseudonyms,
-            permutationCommits,
-            freshPseudonymCommits,
-            freshPseudonymSeedCommits);
+    verification = verify_proof_of_reordering_plus_power(pi[currOffset], currentPseudonyms, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits);
     currOffset++;
     if (!verification)
     {
@@ -752,14 +629,7 @@ bool PrsonaServer::accept_epoch_updates(
         return false;
     }
 
-    verification = 
-        verify_proof_of_reordering<CurveBipoint>(
-            pi[currOffset],
-            previousVoteTallies,
-            permutationCommits,
-            serverTallyCommits,
-            bgnSystem.get_public_key().get_bipoint_curvegen(),
-            bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen());
+    verification = verify_proof_of_reordering<CurveBipoint>( pi[currOffset], previousVoteTallies, permutationCommits, serverTallyCommits, bgnSystem.get_public_key().get_bipoint_curvegen(), bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen());
     currOffset++;
     if (!verification)
     {
@@ -767,13 +637,7 @@ bool PrsonaServer::accept_epoch_updates(
         return false;
     }
 
-    verification = verify_vote_tensor_proofs(
-        pi,
-        currOffset,
-        voteMatrix,
-        permutationCommits,
-        partwayVoteMatrixCommits,
-        false);
+    verification = verify_vote_tensor_proofs(pi, currOffset, voteMatrix, permutationCommits, partwayVoteMatrixCommits, false);
     currOffset += voteMatrix.size();
     if (!verification)
     {
@@ -781,15 +645,8 @@ bool PrsonaServer::accept_epoch_updates(
         return false;
     }
 
-    std::vector<std::vector<TwistBipoint>> partialVoteMatrix =
-        calculate_next_vote_matrix(partwayVoteMatrixCommits);
-    verification = verify_vote_tensor_proofs(
-        pi,
-        currOffset,
-        partialVoteMatrix,
-        permutationCommits,
-        finalVoteMatrixCommits,
-        true);
+    std::vector<std::vector<TwistBipoint>> partialVoteMatrix = calculate_next_vote_matrix(partwayVoteMatrixCommits);
+    verification = verify_vote_tensor_proofs(pi, currOffset, partialVoteMatrix, permutationCommits, finalVoteMatrixCommits, true);
     currOffset += voteMatrix.size();
     if (!verification)
     {
@@ -807,16 +664,7 @@ bool PrsonaServer::accept_epoch_updates(
             userTallyMessages.push_back(currentUserEncryptedTallies[i].encryptedMessage);
         }
 
-        verification = verify_user_tally_proofs(
-            pi[currOffset],
-            nextGenerator,
-            currentPseudonyms,
-            userTallyMasks,
-            userTallyMessages,
-            permutationCommits,
-            userTallyMaskCommits,
-            userTallyMessageCommits,
-            userTallySeedCommits);
+        verification = verify_user_tally_proofs(pi[currOffset], nextGenerator, currentPseudonyms, userTallyMasks, userTallyMessages, permutationCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits);
         currOffset++;
         if (!verification)
         {
@@ -825,12 +673,7 @@ bool PrsonaServer::accept_epoch_updates(
         }
     }
 
-    verification = update_data(
-        freshPseudonymCommits,
-        serverTallyCommits,
-        finalVoteMatrixCommits,
-        userTallyMaskCommits,
-        userTallyMessageCommits);
+    verification = update_data(freshPseudonymCommits, serverTallyCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits);
 
     return verification;
 }
@@ -910,25 +753,14 @@ std::vector<std::vector<Twistpoint>> PrsonaServer::generate_pseudonym_matrix(
     std::vector<std::vector<Scalar>>& seeds,
     std::vector<std::vector<Twistpoint>>& seedCommits) const
 {
-    return generate_reordered_plus_power_matrix<Twistpoint>(
-        permutations,
-        power,
-        currentPseudonyms,
-        seeds,
-        seedCommits,
-        elGamalBlindGenerator);
+    return generate_reordered_plus_power_matrix<Twistpoint>(permutations, power, currentPseudonyms, seeds, seedCommits, elGamalBlindGenerator);
 }
 
 std::vector<std::vector<CurveBipoint>> PrsonaServer::generate_server_tally_matrix(
     const std::vector<std::vector<Scalar>>& permutations,
     std::vector<std::vector<Scalar>>& seeds) const
 {
-    return generate_reordered_matrix<CurveBipoint>(
-        permutations,
-        previousVoteTallies,
-        seeds,
-        bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
-        false);
+    return generate_reordered_matrix<CurveBipoint>(permutations, previousVoteTallies, seeds, bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), false);
 }
 
 std::vector<std::vector<std::vector<TwistBipoint>>> PrsonaServer::generate_vote_tensor(
@@ -954,12 +786,7 @@ std::vector<std::vector<std::vector<TwistBipoint>>> PrsonaServer::generate_vote_
             inputRow = currVoteMatrix[i];
         }
         
-        retval.push_back(generate_reordered_matrix<TwistBipoint>(
-            permutations,
-            inputRow,
-            currSeeds,
-            bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
-            false));
+        retval.push_back(generate_reordered_matrix<TwistBipoint>(permutations, inputRow, currSeeds, bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), false));
 
         seeds.push_back(currSeeds);
     }
@@ -1013,15 +840,7 @@ void PrsonaServer::generate_vote_tensor_proofs(
             inputRow = currMatrix[i];
         }
         
-        pi.push_back(generate_proof_of_reordering<TwistBipoint>(
-            permutations,
-            permutationSeeds,
-            matrixSeeds[i],
-            inputRow,
-            permutationCommits,
-            matrixCommits[i],
-            bgnSystem.get_public_key().get_bipoint_twistgen(),
-            bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen()));
+        pi.push_back(generate_proof_of_reordering<TwistBipoint>(permutations, permutationSeeds, matrixSeeds[i], inputRow, permutationCommits, matrixCommits[i], bgnSystem.get_public_key().get_bipoint_twistgen(), bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen()));
     }
 }
 
@@ -1049,13 +868,7 @@ bool PrsonaServer::verify_vote_tensor_proofs(
         }
 
         size_t whichProof = i + start_offset;
-        retval = retval && verify_proof_of_reordering<TwistBipoint>(
-            pi[whichProof],
-            inputRow,
-            permutationCommits,
-            matrixCommits[i],
-            bgnSystem.get_public_key().get_bipoint_twistgen(),
-            bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen());
+        retval = retval && verify_proof_of_reordering<TwistBipoint>(pi[whichProof], inputRow, permutationCommits, matrixCommits[i], bgnSystem.get_public_key().get_bipoint_twistgen(), bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen());
     }
 
     return retval;
@@ -1109,23 +922,18 @@ void PrsonaServer::generate_user_tally_matrix(
             if (j != last)
             {
                 userTallySeeds[i][j].set_random();
-                userTallySeeds[i][last] =
-                    userTallySeeds[i][last] -
-                    userTallySeeds[i][j];
+                userTallySeeds[i][last] = userTallySeeds[i][last] - userTallySeeds[i][j];
             }
 
-            maskCommits[i][j] =
-                masks[j] * permutations[j][i] * power +
+            maskCommits[i][j] = masks[j] * permutations[j][i] * power +
                 currPseudonyms[j] * power * permutations[j][i] * userTallySeeds[i][j] +
                 elGamalBlindGenerator * userTallySeeds[i][j];
 
-            messageCommits[i][j] =
-                messages[j] * permutations[j][i] +
+            messageCommits[i][j] = messages[j] * permutations[j][i] +
                 nextGenerator * permutations[j][i] * userTallySeeds[i][j] +
                 elGamalBlindGenerator * userTallySeeds[i][j];
 
-            userTallySeedCommits[i][j] =
-                EL_GAMAL_GENERATOR * userTallySeeds[i][j];
+            userTallySeedCommits[i][j] = EL_GAMAL_GENERATOR * userTallySeeds[i][j];
         }
     }
 }
@@ -1155,13 +963,7 @@ std::vector<std::vector<T>> PrsonaServer::generate_reordered_plus_power_matrix(
         seedCommits.push_back(currSeedCommits);
     }
 
-    std::vector<std::vector<T>> retval = 
-        generate_reordered_matrix<T>(
-            permutation_plus_power,
-            oldValues,
-            seeds,
-            h,
-            true);
+    std::vector<std::vector<T>> retval = generate_reordered_matrix<T>(permutation_plus_power, oldValues, seeds, h, true);
 
     for (size_t i = 0; i < permutations.size(); i++)
         for (size_t j = 0; j < permutations[i].size(); j++)
@@ -1216,7 +1018,8 @@ std::vector<std::vector<T>> PrsonaServer::generate_reordered_matrix(
     return retval;
 }
 
-std::vector<size_t> PrsonaServer::sort_data(const std::vector<Twistpoint>& inputs) const
+std::vector<size_t> PrsonaServer::sort_data(
+    const std::vector<Twistpoint>& inputs) const
 {
     std::vector<size_t> retval;
 
@@ -1269,10 +1072,8 @@ bool PrsonaServer::update_data(
 
             if (!userTallyMaskCommits.empty())
             {
-                userTallyMask = userTallyMask +
-                    userTallyMaskCommits[i][j];
-                userTallyMessage = userTallyMessage +
-                    userTallyMessageCommits[i][j];
+                userTallyMask = userTallyMask + userTallyMaskCommits[i][j];
+                userTallyMessage = userTallyMessage + userTallyMessageCommits[i][j];
             }
         }
 
@@ -1280,10 +1081,7 @@ bool PrsonaServer::update_data(
         newVoteTallies.push_back(voteTallySum);
 
         if (!userTallyMaskCommits.empty())
-        {
-            newUserTallies.push_back(
-                EGCiphertext(userTallyMask, userTallyMessage));
-        }
+            newUserTallies.push_back(EGCiphertext(userTallyMask, userTallyMessage));
     }
 
     if (!pseudonyms_sorted(newPseudonyms))
@@ -1341,18 +1139,7 @@ bool PrsonaServer::import_new_user_update(
 
     Twistpoint shortTermPublicKey = otherCurrentPseudonyms[newIndex];
 
-    bool flag = verify_proof_of_added_user(
-        pi,
-        currentFreshGenerator,
-        shortTermPublicKey,
-        bgnSystem.get_public_key().get_bipoint_twistgen(),
-        bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(),
-        bgnSystem.get_public_key().get_bipoint_curvegen(),
-        bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(),
-        newIndex,
-        otherCurrentUserEncryptedTallies[newIndex],
-        otherPreviousVoteTallies[newIndex],
-        otherVoteMatrix);
+    bool flag = verify_proof_of_added_user(pi, currentFreshGenerator, shortTermPublicKey, bgnSystem.get_public_key().get_bipoint_twistgen(), bgnSystem.get_public_key().get_bipoint_twist_subgroup_gen(), bgnSystem.get_public_key().get_bipoint_curvegen(), bgnSystem.get_public_key().get_bipoint_curve_subgroup_gen(), newIndex, otherCurrentUserEncryptedTallies[newIndex], otherPreviousVoteTallies[newIndex], otherVoteMatrix);
 
     if (!flag)
     {
@@ -1367,12 +1154,9 @@ bool PrsonaServer::import_new_user_update(
 
         size_t otherI = (i > newIndex ? i - 1 : i);
 
-        flag = flag && otherCurrentPseudonyms[i] ==
-                currentPseudonyms[otherI];
-        flag = flag && otherCurrentUserEncryptedTallies[i] ==
-                currentUserEncryptedTallies[otherI];
-        flag = flag && otherPreviousVoteTallies[i] ==
-                previousVoteTallies[otherI];
+        flag = flag && otherCurrentPseudonyms[i] == currentPseudonyms[otherI];
+        flag = flag && otherCurrentUserEncryptedTallies[i] == currentUserEncryptedTallies[otherI];
+        flag = flag && otherPreviousVoteTallies[i] == previousVoteTallies[otherI];
 
         for (size_t j = 0; j < otherCurrentPseudonyms.size(); j++)
         {
@@ -1380,8 +1164,7 @@ bool PrsonaServer::import_new_user_update(
                 continue;
 
             size_t otherJ = (j > newIndex ? j - 1 : j);
-            flag = flag && otherVoteMatrix[i][j] ==
-                    voteMatrix[otherI][otherJ];
+            flag = flag && otherVoteMatrix[i][j] == voteMatrix[otherI][otherJ];
         }
     }
 
@@ -1423,17 +1206,11 @@ std::vector<size_t> PrsonaServer::order_data()
         newVoteTallies.push_back(previousVoteTallies[retval[i]]);
         
         if (!currentUserEncryptedTallies.empty())
-        {
-            newUserEncryptedTallies.push_back(
-                currentUserEncryptedTallies[retval[i]]);
-        }
+            newUserEncryptedTallies.push_back(currentUserEncryptedTallies[retval[i]]);
 
         std::vector<TwistBipoint> currNewRow;
         for (size_t j = 0; j < currentPseudonyms.size(); j++)
-        {
-            currNewRow.push_back(
-                voteMatrix[retval[i]][retval[j]]);
-        }
+            currNewRow.push_back(voteMatrix[retval[i]][retval[j]]);
         newVoteMatrix.push_back(currNewRow);
     }
 
@@ -1450,7 +1227,8 @@ std::vector<size_t> PrsonaServer::order_data()
  */
 
 // Completely normal binary search
-size_t PrsonaServer::binary_search(const Twistpoint& index) const
+size_t PrsonaServer::binary_search(
+    const Twistpoint& index) const
 {
     return PrsonaBase::binary_search(currentPseudonyms, index);
 }
@@ -1466,23 +1244,15 @@ bool PrsonaServer::verify_vote_proof(
     const Twistpoint& shortTermPublicKey) const
 {
     const BGNPublicKey& pubKey = bgnSystem.get_public_key();
-    return PrsonaBase::verify_vote_proof(
-        pubKey.get_bipoint_twistgen(),
-        pubKey.get_bipoint_twist_subgroup_gen(),
-        pi,
-        oldVotes,
-        newVotes,
-        currentFreshGenerator,
-        shortTermPublicKey);
+
+    return PrsonaBase::verify_vote_proof(pubKey.get_bipoint_twistgen(), pubKey.get_bipoint_twist_subgroup_gen(), pi, oldVotes, newVotes, currentFreshGenerator, shortTermPublicKey);
 }
 
-void PrsonaServer::print_scores(const std::vector<CurveBipoint>& scores)
+void PrsonaServer::print_scores(
+    const std::vector<CurveBipoint>& scores)
 {
     std::cout << "[";
     for (size_t i = 0; i < scores.size(); i++)
-    {
-            std::cout << bgnSystem.decrypt(scores[i])
-                << (i == scores.size() - 1 ? "]" : " ");
-    }
+        std::cout << bgnSystem.decrypt(scores[i]) << (i == scores.size() - 1 ? "]" : " ");
     std::cout << std::endl;
 }

+ 108 - 174
prsona/src/serverEntity.cpp

@@ -10,13 +10,11 @@
  * CONSTRUCTORS
  */
 
-PrsonaServerEntity::PrsonaServerEntity(size_t numServers)
+PrsonaServerEntity::PrsonaServerEntity(
+    size_t numServers)
 {
     if (numServers < 1)
-    {
-        std::cerr << "You have to have at least 1 server. "
-            << "I'm making it anyways." << std::endl;
-    }
+        std::cerr << "You have to have at least 1 server. I'm making it anyways." << std::endl;
 
     // Make the first server, which makes the BGN parameters
     PrsonaServer firstServer(numServers);
@@ -40,10 +38,8 @@ PrsonaServerEntity::PrsonaServerEntity(size_t numServers)
     // so have each server collaborate to make h.
     Twistpoint blindGenerator = PrsonaServer::EL_GAMAL_GENERATOR;
     for (size_t i = 0; i < numServers; i++)
-    {
-        blindGenerator =
-            servers[i].add_rand_seed_to_generator(pi, blindGenerator);
-    }
+        blindGenerator = servers[i].add_rand_seed_to_generator(pi, blindGenerator);
+
     for (size_t i = 0; i < numServers; i++)
         servers[i].set_EG_blind_generator(pi, blindGenerator);
 }
@@ -57,7 +53,8 @@ BGNPublicKey PrsonaServerEntity::get_bgn_public_key() const
     return get_bgn_public_key(0);
 }
 
-BGNPublicKey PrsonaServerEntity::get_bgn_public_key(size_t which) const
+BGNPublicKey PrsonaServerEntity::get_bgn_public_key(
+    size_t which) const
 {
     return servers[which].get_bgn_public_key();
 }
@@ -67,13 +64,13 @@ Twistpoint PrsonaServerEntity::get_blinding_generator() const
     return get_blinding_generator(0);
 }
 
-Twistpoint PrsonaServerEntity::get_blinding_generator(size_t which) const
+Twistpoint PrsonaServerEntity::get_blinding_generator(
+    size_t which) const
 {
     std::vector<Proof> pi;
     Twistpoint retval = get_blinding_generator(pi, which);
 
-    if (!servers[which].verify_generator_proof(
-            pi, retval, servers[which].get_num_servers()))
+    if (!servers[which].verify_generator_proof(pi, retval, servers[which].get_num_servers()))
     {
         std::cerr << "Error making the generator, aborting." << std::endl;
         return Twistpoint();
@@ -89,7 +86,8 @@ Twistpoint PrsonaServerEntity::get_blinding_generator(
 }
 
 Twistpoint PrsonaServerEntity::get_blinding_generator(
-    std::vector<Proof>& pi, size_t which) const
+    std::vector<Proof>& pi,
+    size_t which) const
 {
     return servers[which].get_blinding_generator(pi);
 }
@@ -99,13 +97,13 @@ Twistpoint PrsonaServerEntity::get_fresh_generator() const
     return get_fresh_generator(0);
 }
 
-Twistpoint PrsonaServerEntity::get_fresh_generator(size_t which) const
+Twistpoint PrsonaServerEntity::get_fresh_generator(
+    size_t which) const
 {
     std::vector<Proof> pi;
     Twistpoint retval = get_fresh_generator(pi, which);
 
-    if (!servers[which].verify_generator_proof(
-            pi, retval, servers[which].get_num_servers()))
+    if (!servers[which].verify_generator_proof(pi, retval, servers[which].get_num_servers()))
     {
         std::cerr << "Error making the generator, aborting." << std::endl;
         return Twistpoint();
@@ -121,7 +119,8 @@ Twistpoint PrsonaServerEntity::get_fresh_generator(
 }
 
 Twistpoint PrsonaServerEntity::get_fresh_generator(
-    std::vector<Proof>& pi, size_t which) const
+    std::vector<Proof>& pi,
+    size_t which) const
 {
     Twistpoint retval = PrsonaServer::EL_GAMAL_GENERATOR;
 
@@ -140,7 +139,8 @@ size_t PrsonaServerEntity::get_num_clients() const
     return get_num_clients(0);
 }
 
-size_t PrsonaServerEntity::get_num_clients(size_t which) const
+size_t PrsonaServerEntity::get_num_clients(
+    size_t which) const
 {
     return servers[which].get_num_clients();
 }
@@ -150,7 +150,8 @@ size_t PrsonaServerEntity::get_num_servers() const
     return get_num_servers(0);
 }
 
-size_t PrsonaServerEntity::get_num_servers(size_t which) const
+size_t PrsonaServerEntity::get_num_servers(
+    size_t which) const
 {
     return servers[which].get_num_servers();
 }
@@ -160,7 +161,8 @@ size_t PrsonaServerEntity::get_num_servers(size_t which) const
  */
 
 std::vector<TwistBipoint> PrsonaServerEntity::get_current_votes_by(
-    Proof& pi, const Twistpoint& shortTermPublicKey) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey) const
 {
     return get_current_votes_by(pi, shortTermPublicKey, 0);
 }
@@ -170,7 +172,9 @@ std::vector<TwistBipoint> PrsonaServerEntity::get_current_votes_by(
  * In practice, this is intended for clients,
  * who need to know their current votes in order to rerandomize them. */
 std::vector<TwistBipoint> PrsonaServerEntity::get_current_votes_by(
-    Proof& pi, const Twistpoint& shortTermPublicKey, size_t which) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey,
+    size_t which) const
 {
     return servers[which].get_current_votes_by(pi, shortTermPublicKey);
 }
@@ -182,35 +186,40 @@ std::vector<std::vector<TwistBipoint>> PrsonaServerEntity::get_all_current_votes
 }
 
 std::vector<std::vector<TwistBipoint>> PrsonaServerEntity::get_all_current_votes(
-    Proof& pi, size_t which) const
+    Proof& pi,
+    size_t which) const
 {
     return servers[which].get_all_current_votes(pi);
 }
 
 EGCiphertext PrsonaServerEntity::get_current_user_encrypted_tally(
-    Proof& pi, const Twistpoint& shortTermPublicKey) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey) const
 {
     return get_current_user_encrypted_tally(pi, shortTermPublicKey, 0);
 }
 
 EGCiphertext PrsonaServerEntity::get_current_user_encrypted_tally(
-    Proof& pi, const Twistpoint& shortTermPublicKey, size_t which) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey,
+    size_t which) const
 {
-    return servers[which].get_current_user_encrypted_tally(
-        pi, shortTermPublicKey);
+    return servers[which].get_current_user_encrypted_tally(pi, shortTermPublicKey);
 }
 
 CurveBipoint PrsonaServerEntity::get_current_server_encrypted_tally(
-    Proof& pi, const Twistpoint& shortTermPublicKey) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey) const
 {
     return get_current_server_encrypted_tally(pi, shortTermPublicKey, 0);
 }
 
 CurveBipoint PrsonaServerEntity::get_current_server_encrypted_tally(
-    Proof& pi, const Twistpoint& shortTermPublicKey, size_t which) const
+    Proof& pi,
+    const Twistpoint& shortTermPublicKey,
+    size_t which) const
 {
-    return servers[which].get_current_server_encrypted_tally(
-        pi, shortTermPublicKey);
+    return servers[which].get_current_server_encrypted_tally(pi, shortTermPublicKey);
 }
 
 std::vector<Twistpoint> PrsonaServerEntity::get_current_pseudonyms(
@@ -220,7 +229,8 @@ std::vector<Twistpoint> PrsonaServerEntity::get_current_pseudonyms(
 }
 
 std::vector<Twistpoint> PrsonaServerEntity::get_current_pseudonyms(
-    Proof& pi, size_t which) const
+    Proof& pi,
+    size_t which) const
 {
     return servers[which].get_current_pseudonyms(pi);
 }
@@ -230,13 +240,16 @@ std::vector<Twistpoint> PrsonaServerEntity::get_current_pseudonyms(
  */
 
 void PrsonaServerEntity::get_other_vote_row_commitments(
-    std::vector<Proof>& pi, const Twistpoint& request) const
+    std::vector<Proof>& pi,
+    const Twistpoint& request) const
 {
     get_other_vote_row_commitments(pi, request, 0);
 }
 
 void PrsonaServerEntity::get_other_vote_row_commitments(
-    std::vector<Proof>& pi, const Twistpoint& request, size_t whichNot) const
+    std::vector<Proof>& pi,
+    const Twistpoint& request,
+    size_t whichNot) const
 {
     for (size_t i = 0; i < servers.size(); i++)
     {
@@ -254,7 +267,8 @@ void PrsonaServerEntity::get_other_vote_matrix_commitments(
 }
 
 void PrsonaServerEntity::get_other_vote_matrix_commitments(
-    std::vector<Proof>& pi, size_t whichNot) const
+    std::vector<Proof>& pi,
+    size_t whichNot) const
 {
     for (size_t i = 0; i < servers.size(); i++)
     {
@@ -266,13 +280,16 @@ void PrsonaServerEntity::get_other_vote_matrix_commitments(
 }
 
 void PrsonaServerEntity::get_other_user_tally_commitments(
-    std::vector<Proof>& pi, const Twistpoint& request) const
+    std::vector<Proof>& pi,
+    const Twistpoint& request) const
 {
     get_other_user_tally_commitments(pi, request, 0);
 }
 
 void PrsonaServerEntity::get_other_user_tally_commitments(
-    std::vector<Proof>& pi, const Twistpoint& request, size_t whichNot) const
+    std::vector<Proof>& pi,
+    const Twistpoint& request,
+    size_t whichNot) const
 {
     for (size_t i = 0; i < servers.size(); i++)
     {
@@ -284,13 +301,16 @@ void PrsonaServerEntity::get_other_user_tally_commitments(
 }
 
 void PrsonaServerEntity::get_other_server_tally_commitments(
-    std::vector<Proof>& pi, const Twistpoint& request) const
+    std::vector<Proof>& pi,
+    const Twistpoint& request) const
 {
     get_other_server_tally_commitments(pi, request, 0);
 }
 
 void PrsonaServerEntity::get_other_server_tally_commitments(
-    std::vector<Proof>& pi, const Twistpoint& request, size_t whichNot) const
+    std::vector<Proof>& pi,
+    const Twistpoint& request,
+    size_t whichNot) const
 {
     for (size_t i = 0; i < servers.size(); i++)
     {
@@ -308,7 +328,8 @@ void PrsonaServerEntity::get_other_pseudonyms_commitments(
 }
 
 void PrsonaServerEntity::get_other_pseudonyms_commitments(
-    std::vector<Proof>& pi, size_t whichNot) const
+    std::vector<Proof>& pi,
+    size_t whichNot) const
 {
     for (size_t i = 0; i < servers.size(); i++)
     {
@@ -323,7 +344,8 @@ void PrsonaServerEntity::get_other_pseudonyms_commitments(
  * CLIENT INTERACTIONS
  */
 
-void PrsonaServerEntity::add_new_client(PrsonaClient& newUser)
+void PrsonaServerEntity::add_new_client(
+    PrsonaClient& newUser)
 {
     add_new_client(newUser, 0);
 }
@@ -331,22 +353,21 @@ void PrsonaServerEntity::add_new_client(PrsonaClient& newUser)
 /* Add a new client (who is identified only by their short term public key)
  * One server does the main work, then other servers import their (proven)
  * exported data. */
-void PrsonaServerEntity::add_new_client(PrsonaClient& newUser, size_t which)
+void PrsonaServerEntity::add_new_client(
+    PrsonaClient& newUser,
+    size_t which)
 {
     Proof proofOfValidSTPK, proofOfValidVotes;
     std::vector<Proof> proofOfValidGenerator, proofOfCorrectAddition;
-    Twistpoint freshGenerator =
-        get_fresh_generator(proofOfValidGenerator, which);
+    Twistpoint freshGenerator = get_fresh_generator(proofOfValidGenerator, which);
 
     // Users can't actually announce a short term public key
     // if they don't know the fresh generator.
     newUser.receive_fresh_generator(proofOfValidGenerator, freshGenerator);
-    Twistpoint shortTermPublicKey = newUser.get_short_term_public_key(
-                                        proofOfValidSTPK);
+    Twistpoint shortTermPublicKey = newUser.get_short_term_public_key(proofOfValidSTPK);
 
     // Do the actual work of adding the client to the first server
-    servers[which].add_new_client(
-        proofOfCorrectAddition, proofOfValidSTPK, shortTermPublicKey);
+    servers[which].add_new_client(proofOfCorrectAddition, proofOfValidSTPK, shortTermPublicKey);
 
     // Then, export the data to the rest of the servers
     std::vector<CurveBipoint> previousVoteTallies;
@@ -362,12 +383,7 @@ void PrsonaServerEntity::add_new_client(PrsonaClient& newUser, size_t which)
     for (size_t j = 1; j < servers[which].get_num_servers(); j++)
     {
         size_t index = (which + j) % servers[which].get_num_servers();
-        servers[index].import_new_user_update(
-            proofOfCorrectAddition,
-            previousVoteTallies,
-            currentPseudonyms,
-            currentUserEncryptedTallies,
-            voteMatrix);
+        servers[index].import_new_user_update(proofOfCorrectAddition, previousVoteTallies, currentPseudonyms, currentUserEncryptedTallies, voteMatrix);
     }
 
     // Finally, give the user the information it needs
@@ -396,66 +412,55 @@ bool PrsonaServerEntity::receive_vote(
     {
         size_t index = (i + which) % servers[which].get_num_servers();
 
-        retval = retval &&
-            servers[index].receive_vote(pi, newVotes, shortTermPublicKey);
+        retval = retval && servers[index].receive_vote(pi, newVotes, shortTermPublicKey);
     }
 
     return retval;
 }
 
 void PrsonaServerEntity::transmit_new_user_data(
-    const std::vector<Proof>& pi, PrsonaClient& newUser) const
+    const std::vector<Proof>& pi,
+    PrsonaClient& newUser) const
 {
     transmit_new_user_data(pi, newUser, 0);
 }
 
 void PrsonaServerEntity::transmit_new_user_data(
-    const std::vector<Proof>& pi, PrsonaClient& newUser, size_t which) const
+    const std::vector<Proof>& pi,
+    PrsonaClient& newUser,
+    size_t which) const
 {
-    Twistpoint shortTermPublicKey =
-        newUser.get_short_term_public_key();
+    Twistpoint shortTermPublicKey = newUser.get_short_term_public_key();
 
     Proof baseProof;
     std::vector<Proof> serverEncryptedScoreProof;
-    CurveBipoint serverEncryptedScore =
-        get_current_server_encrypted_tally(baseProof, shortTermPublicKey, which);
+    CurveBipoint serverEncryptedScore = get_current_server_encrypted_tally(baseProof, shortTermPublicKey, which);
     serverEncryptedScoreProof.push_back(baseProof);
     get_other_server_tally_commitments(serverEncryptedScoreProof, shortTermPublicKey, which);
 
     baseProof.clear();
     std::vector<Proof> userEncryptedScoreProof;
-    EGCiphertext userEncryptedScore =
-        get_current_user_encrypted_tally(baseProof, shortTermPublicKey, which);
+    EGCiphertext userEncryptedScore = get_current_user_encrypted_tally(baseProof, shortTermPublicKey, which);
     userEncryptedScoreProof.push_back(baseProof);
     get_other_user_tally_commitments(userEncryptedScoreProof, shortTermPublicKey, which);
 
     baseProof.clear();
     std::vector<Proof> voteMatrixProof;
-    std::vector<std::vector<TwistBipoint>> voteMatrix =
-        get_all_current_votes(baseProof, which);
+    std::vector<std::vector<TwistBipoint>> voteMatrix = get_all_current_votes(baseProof, which);
     voteMatrixProof.push_back(baseProof);
     get_other_vote_matrix_commitments(voteMatrixProof, which);
 
     baseProof.clear();
     std::vector<Proof> pseudonymsProof;
-    std::vector<Twistpoint> currentPseudonyms =
-        get_current_pseudonyms(baseProof, which);
+    std::vector<Twistpoint> currentPseudonyms = get_current_pseudonyms(baseProof, which);
     pseudonymsProof.push_back(baseProof);
     get_other_pseudonyms_commitments(pseudonymsProof, which);
 
-    newUser.receive_new_user_data(
-        pi,
-        serverEncryptedScoreProof,
-        serverEncryptedScore,
-        userEncryptedScoreProof,
-        userEncryptedScore,
-        voteMatrixProof,
-        voteMatrix,
-        pseudonymsProof,
-        currentPseudonyms);
+    newUser.receive_new_user_data(pi, serverEncryptedScoreProof, serverEncryptedScore, userEncryptedScoreProof, userEncryptedScore, voteMatrixProof, voteMatrix, pseudonymsProof, currentPseudonyms);
 }
 
-void PrsonaServerEntity::transmit_updates(PrsonaClient& currUser) const
+void PrsonaServerEntity::transmit_updates(
+    PrsonaClient& currUser) const
 {
     transmit_updates(currUser, 0);
 }
@@ -463,12 +468,12 @@ void PrsonaServerEntity::transmit_updates(PrsonaClient& currUser) const
 // After tallying scores and new vote matrix,
 // give those to a user for the new epoch
 void PrsonaServerEntity::transmit_updates(
-    PrsonaClient& currUser, size_t which) const
+    PrsonaClient& currUser,
+    size_t which) const
 {
     Proof proofOfValidSTPK, proofOfScore, proofOfCorrectVotes;
     std::vector<Proof> proofOfValidGenerator;
-    Twistpoint freshGenerator =
-        get_fresh_generator(proofOfValidGenerator, which);
+    Twistpoint freshGenerator = get_fresh_generator(proofOfValidGenerator, which);
 
     // Get users the next fresh generator so they can correctly
     // ask for their new scores and vote row
@@ -476,10 +481,8 @@ void PrsonaServerEntity::transmit_updates(
 
     Proof baseProof;
     std::vector<Proof> fullProof;
-    Twistpoint shortTermPublicKey =
-        currUser.get_short_term_public_key();
-    EGCiphertext encryptedScore =
-        get_current_user_encrypted_tally(baseProof, shortTermPublicKey, which);
+    Twistpoint shortTermPublicKey = currUser.get_short_term_public_key();
+    EGCiphertext encryptedScore = get_current_user_encrypted_tally(baseProof, shortTermPublicKey, which);
     fullProof.push_back(baseProof);
     get_other_user_tally_commitments(fullProof, shortTermPublicKey, which);
 
@@ -496,7 +499,8 @@ void PrsonaServerEntity::epoch()
 }
 
 // Do the epoch process
-void PrsonaServerEntity::epoch(size_t which)
+void PrsonaServerEntity::epoch(
+    size_t which)
 {
     Twistpoint nextGenerator = PrsonaServer::EL_GAMAL_GENERATOR;
     
@@ -519,15 +523,7 @@ void PrsonaServerEntity::epoch(size_t which)
     {   
         size_t realI = (which + i) % servers[which].get_num_servers();
 
-        servers[realI].build_up_midway_pseudonyms(
-            pi,
-            permutationCommits,
-            freshPseudonymCommits,
-            freshPseudonymSeedCommits,
-            serverTallyCommits,
-            partwayVoteMatrixCommits,
-            finalVoteMatrixCommits,
-            nextGenerator);
+        servers[realI].build_up_midway_pseudonyms(pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, nextGenerator);
 
         for (size_t j = 1; j < servers[which].get_num_servers(); j++)
         {
@@ -537,19 +533,7 @@ void PrsonaServerEntity::epoch(size_t which)
 
             size_t realJ = (realI + j) % servers[which].get_num_servers();
 
-            servers[realJ].accept_epoch_updates(
-                pi[i + 1],
-                permutationCommits[i],
-                freshPseudonymCommits[i],
-                freshPseudonymSeedCommits[i],
-                serverTallyCommits[i],
-                partwayVoteMatrixCommits[i],
-                finalVoteMatrixCommits[i],
-                currUserTallyMaskCommits,
-                currUserTallyMessageCommits,
-                currUserTallySeedCommits,
-                nextGenerator,
-                false);
+            servers[realJ].accept_epoch_updates(pi[i + 1], permutationCommits[i], freshPseudonymCommits[i], freshPseudonymSeedCommits[i], serverTallyCommits[i], partwayVoteMatrixCommits[i], finalVoteMatrixCommits[i], currUserTallyMaskCommits, currUserTallyMessageCommits, currUserTallySeedCommits, nextGenerator, false);
         }
     }
 
@@ -568,51 +552,21 @@ void PrsonaServerEntity::epoch(size_t which)
      * to the committed values. */
     std::vector<EGCiphertext> currentUserEncryptedTallies;
     std::vector<CurveBipoint> currentServerEncryptedTallies;
-    tally_scores(
-        nextGenerator,
-        currentUserEncryptedTallies,
-        currentServerEncryptedTallies,
-        which);
-    distribute_tallied_scores(
-        currentUserEncryptedTallies,
-        currentServerEncryptedTallies);
+    tally_scores(nextGenerator, currentUserEncryptedTallies, currentServerEncryptedTallies, which);
+    distribute_tallied_scores(currentUserEncryptedTallies, currentServerEncryptedTallies);
     
     // go from A_0.5 to A_1
     for (size_t i = 0; i < servers[which].get_num_servers(); i++)
     {
         size_t realI = (which + i) % servers[which].get_num_servers();
         
-        servers[realI].break_down_midway_pseudonyms(
-            generator_proof,
-            pi,
-            permutationCommits,
-            freshPseudonymCommits,
-            freshPseudonymSeedCommits,
-            serverTallyCommits,
-            partwayVoteMatrixCommits,
-            finalVoteMatrixCommits,
-            userTallyMaskCommits,
-            userTallyMessageCommits,
-            userTallySeedCommits,
-            nextGenerator);
+        servers[realI].break_down_midway_pseudonyms(generator_proof, pi, permutationCommits, freshPseudonymCommits, freshPseudonymSeedCommits, serverTallyCommits, partwayVoteMatrixCommits, finalVoteMatrixCommits, userTallyMaskCommits, userTallyMessageCommits, userTallySeedCommits, nextGenerator);
         
         for (size_t j = 1; j < servers[which].get_num_servers(); j++)
         {
             size_t realJ = (realI + j) % servers[which].get_num_servers();
 
-            servers[realJ].accept_epoch_updates(
-                pi[i],
-                permutationCommits[i],
-                freshPseudonymCommits[i],
-                freshPseudonymSeedCommits[i],
-                serverTallyCommits[i],
-                partwayVoteMatrixCommits[i],
-                finalVoteMatrixCommits[i],
-                userTallyMaskCommits[i],
-                userTallyMessageCommits[i],
-                userTallySeedCommits[i],
-                nextGenerator,
-                true);
+            servers[realJ].accept_epoch_updates(pi[i], permutationCommits[i], freshPseudonymCommits[i], freshPseudonymSeedCommits[i], serverTallyCommits[i], partwayVoteMatrixCommits[i], finalVoteMatrixCommits[i], userTallyMaskCommits[i], userTallyMessageCommits[i], userTallySeedCommits[i], nextGenerator, true);
         }
     }
 }
@@ -624,10 +578,7 @@ void PrsonaServerEntity::print_scores() const
 
     std::cout << "[";
     for (size_t i = 0; i < scores.size(); i++)
-    {
-            std::cout << bgnSystem.decrypt(scores[i])
-                << (i == scores.size() - 1 ? "]" : " ");
-    }
+        std::cout << bgnSystem.decrypt(scores[i]) << (i == scores.size() - 1 ? "]" : " ");
     std::cout << std::endl;
 }
 
@@ -640,11 +591,7 @@ void PrsonaServerEntity::print_votes() const
     {
         std::cout << (i == 0 ? "[[" : " [");
         for (size_t j = 0; j < voteMatrix[i].size(); j++)
-        {
-
-            std::cout << bgnSystem.decrypt(voteMatrix[i][j])
-                << (j == voteMatrix[i].size() - 1 ? "]" : " ");
-        }
+            std::cout << bgnSystem.decrypt(voteMatrix[i][j]) << (j == voteMatrix[i].size() - 1 ? "]" : " ");
         std::cout << (i == voteMatrix.size() - 1 ? "]" : " ") << std::endl;
     }
 }
@@ -653,16 +600,12 @@ void PrsonaServerEntity::print_current_votes_by(
     const Twistpoint& index) const
 {
     BGN bgnSystem = servers[0].bgnSystem;
-    size_t realIndex =
-        servers[0].binary_search(index);
+    size_t realIndex = servers[0].binary_search(index);
     std::vector<TwistBipoint> scores = servers[0].voteMatrix[realIndex];
 
     std::cout << "[";
     for (size_t i = 0; i < scores.size(); i++)
-    {
-            std::cout << bgnSystem.decrypt(scores[i])
-                << (i == scores.size() - 1 ? "]" : " ");
-    }
+        std::cout << bgnSystem.decrypt(scores[i]) << (i == scores.size() - 1 ? "]" : " ");
     std::cout << std::endl;
 }
 
@@ -687,21 +630,14 @@ void PrsonaServerEntity::tally_scores(
 {
     std::vector<EGCiphertext> retval;
     std::vector<Scalar> decryptedTalliedScores = servers[which].tally_scores();
-    mpz_class maxScorePossibleThisRound =
-        servers[which].get_max_possible_score().toInt() *
-        PrsonaBase::get_max_allowed_vote();
-    mpz_class topOfScoreRange =
-        decryptedTalliedScores.size() * PrsonaBase::get_max_allowed_vote();
+    mpz_class maxScorePossibleThisRound = servers[which].get_max_possible_score().toInt() * PrsonaBase::get_max_allowed_vote();
+    mpz_class topOfScoreRange = decryptedTalliedScores.size() * PrsonaBase::get_max_allowed_vote();
 
     userTallyScores.clear();
     serverTallyScores.clear();
     for (size_t i = 0; i < decryptedTalliedScores.size(); i++)
     {
-        decryptedTalliedScores[i] =
-            Scalar(
-                (decryptedTalliedScores[i].toInt() * topOfScoreRange) /
-                maxScorePossibleThisRound
-            );
+        decryptedTalliedScores[i] = Scalar((decryptedTalliedScores[i].toInt() * topOfScoreRange) / maxScorePossibleThisRound);
 
         EGCiphertext currCiphertext;
         userTallyScores.push_back(currCiphertext);
@@ -712,13 +648,10 @@ void PrsonaServerEntity::tally_scores(
 
         // Give the server the new weights,
         // to get passed around to the other servers
-        servers[which].bgnSystem.encrypt(
-            serverTallyScores[i], decryptedTalliedScores[i]);
+        servers[which].bgnSystem.encrypt(serverTallyScores[i], decryptedTalliedScores[i]);
 
         userTallyScores[i].mask = servers[which].currentPseudonyms[i] * currMask;
-        userTallyScores[i].encryptedMessage =
-            (nextGenerator * currMask) +
-            (servers[which].get_blinding_generator() * decryptedTalliedScores[i]);
+        userTallyScores[i].encryptedMessage = (nextGenerator * currMask) + (servers[which].get_blinding_generator() * decryptedTalliedScores[i]);
     }
 }
 
@@ -736,7 +669,8 @@ void PrsonaServerEntity::distribute_tallied_scores(
 
 // Completely normal binary search
 size_t PrsonaServerEntity::binary_search(
-    const Twistpoint& shortTermPublicKey, size_t which) const
+    const Twistpoint& shortTermPublicKey,
+    size_t which) const
 {
     return servers[which].binary_search(shortTermPublicKey);
 }

File diff suppressed because it is too large
+ 44 - 955
prsona/src/serverMain.cpp


Some files were not shown because too many files changed in this diff