1 /*******************************************************************************
2   CachinKursawePetzoldShoupSEABP.hh,
3               |S|ecure and |E|fficient |A|synchronous |B|roadcast |P|rotocols
4 
5      [CKPS01] Christian Cachin, Klaus Kursawe, Frank Petzold, and Victor Shoup:
6        'Secure and Efficient Asynchronous Broadcast Protocols',
7      Proceedings of CRYPTO 2001, LNCS 2139, pp. 524--541, Springer 2001.
8      Full length version of extended abstract: http://shoup.net/papers/ckps.pdf
9 
10    This file is part of LibTMCG.
11 
12  Copyright (C) 2016, 2017, 2018  Heiko Stamer <HeikoStamer@gmx.net>
13 
14    LibTMCG is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 2 of the License, or
17    (at your option) any later version.
18 
19    LibTMCG is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22    GNU General Public License for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with LibTMCG; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 *******************************************************************************/
28 
29 #ifndef INCLUDED_CachinKursawePetzoldShoupSEABP_HH
30 	#define INCLUDED_CachinKursawePetzoldShoupSEABP_HH
31 
32 // C and STL header
33 #include <cstdlib>
34 #include <ctime>
35 #include <string>
36 #include <vector>
37 #include <map>
38 #include <list>
39 
40 // GNU multiple precision library
41 #include <gmp.h>
42 
43 // asynchronous unicast transmission of mpz_t
44 #include "aiounicast.hh"
45 
46 // define some internal types for convenience
47 typedef std::map<std::string, bool>			RBC_TagCheck;
48 typedef std::map<std::string, size_t>		RBC_TagCount;
49 typedef std::map<std::string, mpz_ptr>		RBC_TagMpz;
50 typedef std::list<mpz_ptr>					RBC_BufferList;
51 typedef std::vector<mpz_ptr>				RBC_Message;
52 typedef std::vector<mpz_srcptr>				RBC_ConstMessage;
53 typedef std::list< RBC_Message >			RBC_VectorList;
54 
55 /* The following class implements an optimized version of Bracha's protocol
56    described in [CKPS01]. Additionally, a FIFO-order deliver mechanism based on
57    sequence numbers has been implemented. Original paper cited: G. Bracha: 'An
58    asynchronous [(n-1)/3]-resilient consensus protocol', Proc. 3rd ACM Symposium
59    on Principles of Distributed Computing (PODC), pp. 154–162, 1984.
60 
61    Note that Bracha's consensus algorithm is not implemented yet.*/
62 class CachinKursawePetzoldShoupRBC
63 {
64 	private:
65 		size_t									aio_default_scheduler;
66 		time_t									aio_default_timeout;
67 		const time_t							aio_timeout_vs;
68 		mpz_t									ID, whoami, s;
69 		RBC_BufferList							last_IDs, last_s;
70 		RBC_VectorList							last_deliver_s;
71 		mpz_t									r_send, r_echo, r_ready;
72 		mpz_t									r_request, r_answer;
73 		std::vector<RBC_TagCheck>				send, echo, ready;
74 		std::vector<RBC_TagCheck>				request, answer;
75 		RBC_TagMpz								mbar, dbar;
76 		std::map<std::string, RBC_TagCount>		e_d, r_d;
77 		std::vector<RBC_BufferList>				buf_mpz, buf_id, buf_msg;
78 		std::vector<bool>						deliver_error;
79 		std::list<RBC_Message>					deliver_buf;
80 		std::vector<mpz_ptr>					deliver_s;
81 		aiounicast*								aiou;
82 		static const size_t						sync_slices = 10;
83 
84 		void InitializeMessage
85 			(RBC_Message &message);
86 		void InitializeMessage
87 			(RBC_Message &message,
88 			 const RBC_ConstMessage &source);
89 		void InitializeMessage
90 			(RBC_Message &message,
91 			 const RBC_Message &source);
92 		void AssignMessage
93 			(RBC_ConstMessage &message,
94 			 const RBC_Message &source);
95 		void TagMessage
96 			(std::string &tag,
97 			 const RBC_Message &message);
98 		void ReleaseMessage
99 			(RBC_Message &message);
100 
101 	public:
102 		size_t									n, t, j;
103 
104 		CachinKursawePetzoldShoupRBC
105 			(const size_t n_in,
106 			 const size_t t_in,
107 			 const size_t j_in,
108 			 aiounicast* aiou_in,
109 			 const size_t scheduler_in = aiounicast::aio_scheduler_roundrobin,
110 			 const time_t timeout_in = aiounicast::aio_timeout_extremely_long);
111 		void setID
112 			(const std::string &ID_in);
113 		void unsetID
114 			();
115 		void Broadcast
116 			(mpz_srcptr m,
117 			 const bool simulate_faulty_behaviour = false);
118 		bool Deliver
119 			(mpz_ptr m,
120 			 size_t &i_out,
121 			 size_t scheduler = aiounicast::aio_scheduler_default,
122 			 time_t timeout = aiounicast::aio_timeout_default);
123 		bool DeliverFrom
124 			(mpz_ptr m,
125 			 const size_t i_in,
126 			 size_t scheduler = aiounicast::aio_scheduler_default,
127 			 time_t timeout = aiounicast::aio_timeout_default);
128 		bool Sync
129 			(time_t timeout = aiounicast::aio_timeout_default,
130 			 const std::string tag = "");
131 		~CachinKursawePetzoldShoupRBC
132 			();
133 };
134 
135 #endif
136 
137