55 "context"
66 "errors"
77 "fmt"
8- "github.com/HACKERALERT/infectious"
98 "github.com/rs/zerolog"
10- "go.dedis.ch/kyber/v4"
119 "golang.org/x/xerrors"
1210 "google.golang.org/protobuf/proto"
1311 "hash"
4240 }
4341)
4442
45- // Marshaller represents an interface for an object that can marshal
46- // and unmarshal some value type
47- type Marshaller [M any ] interface {
48- Marshal ([]M ) ([]byte , error )
49- Unmarshal ([]byte ) ([]M , error )
50- }
51-
52- type FourRoundRBC [M any ] struct {
53- iface rbc.AuthenticatedMessageStream
54- marshaller Marshaller [M ]
55- pred func ([]M ) bool
43+ type FourRoundRBC struct {
44+ iface rbc.AuthenticatedMessageStream
45+ pred func ([]byte ) bool
5646 hash.Hash
5747 rs reedsolomon.RSCodes
5848 stopChan chan struct {}
@@ -62,19 +52,17 @@ type FourRoundRBC[M any] struct {
6252 echoCount map [string ]int
6353 readyCounts map [string ]int
6454 readyMis map [string ]map [string ]struct {}
65- th []infectious.Share
66- kyber.Group
67- r int
68- nbNodes int
69- finalValue []M
70- finished bool
71- log zerolog.Logger
72- id uint32
55+ th []reedsolomon.Encoding
56+ r int
57+ finalValue []byte
58+ finished bool
59+ log zerolog.Logger
60+ id uint32
7361}
7462
75- func NewFourRoundRBC [ M any ] (pred func ([]M ) bool , h hash.Hash , threshold int ,
63+ func NewFourRoundRBC (pred func ([]byte ) bool , h hash.Hash , threshold int ,
7664 iface rbc.AuthenticatedMessageStream ,
77- marshaller Marshaller [ M ], group kyber. Group , r , nbNodes , mLen int , id uint32 ) * FourRoundRBC [ M ] {
65+ rs reedsolomon. RSCodes , r int , id uint32 ) * FourRoundRBC {
7866
7967 // Disable logging based on the GLOG environment variable
8068 var logLevel zerolog.Level
@@ -91,56 +79,46 @@ func NewFourRoundRBC[M any](pred func([]M) bool, h hash.Hash, threshold int,
9179 Str ("id" , strconv .Itoa (int (id ))).
9280 Logger ()
9381
94- return & FourRoundRBC [ M ] {
82+ return & FourRoundRBC {
9583 iface : iface ,
96- marshaller : marshaller ,
9784 pred : pred ,
9885 Hash : h ,
99- rs : reedsolomon . NewBWCodes ( mLen , nbNodes ) ,
86+ rs : rs ,
10087 stopChan : make (chan struct {}),
10188 threshold : threshold ,
10289 sentReady : false ,
10390 RWMutex : sync.RWMutex {},
10491 echoCount : make (map [string ]int ),
10592 readyCounts : make (map [string ]int ),
10693 readyMis : make (map [string ]map [string ]struct {}),
107- th : make ([]infectious.Share , 0 ),
108- Group : group ,
94+ th : make ([]reedsolomon.Encoding , 0 ),
10995 r : r ,
110- nbNodes : nbNodes ,
11196 finalValue : nil ,
11297 finished : false ,
11398 log : logger ,
11499 id : id ,
115100 }
116101}
117102
118- func (f * FourRoundRBC [ M ] ) FreshHash (ms []M ) ([]byte , error ) {
103+ func (f * FourRoundRBC ) FreshHash (bs []byte ) ([]byte , error ) {
119104 f .Hash .Reset ()
120105
121- // Marshall the list of value
122- bs , err := f .marshaller .Marshal (ms )
123- if err != nil {
124- return nil , err
125- }
126-
127106 // Write the bytes
128- _ , err = f .Hash .Write (bs )
107+ _ , err : = f .Hash .Write (bs )
129108 if err != nil {
130109 return nil , err
131110 }
132111 h := f .Hash .Sum (nil )
133112 return h , nil
134113}
135114
136- func (f * FourRoundRBC [M ]) broadcast (ms []M ) error {
137- msBytes , err := f .marshaller .Marshal (ms )
138- inst := createProposeMessage (msBytes )
139- err = f .broadcastInstruction (inst )
115+ func (f * FourRoundRBC ) broadcast (bs []byte ) error {
116+ inst := createProposeMessage (bs )
117+ err := f .broadcastInstruction (inst )
140118 return err
141119}
142120
143- func (f * FourRoundRBC [ M ] ) start (cancelFunc context.CancelFunc ) {
121+ func (f * FourRoundRBC ) start (cancelFunc context.CancelFunc ) {
144122 go func () {
145123 for {
146124 bs , err := f .iface .Receive (f .stopChan )
@@ -175,7 +153,7 @@ func (f *FourRoundRBC[M]) start(cancelFunc context.CancelFunc) {
175153 }()
176154}
177155
178- func (f * FourRoundRBC [ M ] ) RBroadcast (m []M ) error {
156+ func (f * FourRoundRBC ) RBroadcast (m []byte ) error {
179157 ctx , cancel := context .WithCancel (context .Background ())
180158 f .start (cancel )
181159
@@ -190,20 +168,20 @@ func (f *FourRoundRBC[M]) RBroadcast(m []M) error {
190168 return nil
191169}
192170
193- func (f * FourRoundRBC [ M ] ) Listen () error {
171+ func (f * FourRoundRBC ) Listen () error {
194172 ctx , cancel := context .WithCancel (context .Background ())
195173 f .start (cancel )
196174
197175 <- ctx .Done ()
198176 return nil
199177}
200178
201- func (f * FourRoundRBC [ M ] ) Stop () error {
179+ func (f * FourRoundRBC ) Stop () error {
202180 f .stopChan <- struct {}{}
203181 return nil
204182}
205183
206- func (f * FourRoundRBC [ M ] ) handleMessage (instruction * typedefs.Instruction ) (error , bool ) {
184+ func (f * FourRoundRBC ) handleMessage (instruction * typedefs.Instruction ) (error , bool ) {
207185 var err error = nil
208186 finished := false
209187 switch op := instruction .GetOperation ().GetOp ().(type ) {
@@ -215,22 +193,21 @@ func (f *FourRoundRBC[M]) handleMessage(instruction *typedefs.Instruction) (erro
215193 err = f .receiveEcho (op .EchoInst )
216194 case * typedefs.Message_ReadyInst :
217195 f .log .Info ().Msg ("Received ready message" )
218- finished = f .receiveReady (op .ReadyInst )
196+ finished , err = f .receiveReady (op .ReadyInst )
219197 default :
220- return xerrors .New ("Invalid operation" ), false
198+ err = xerrors .New ("Invalid operation" )
221199 }
222200
223201 return err , finished
224202}
225203
226- func (f * FourRoundRBC [M ]) receivePropose (m * typedefs.Message_Propose ) error {
227- vals , err := f .marshaller .Unmarshal (m .Content )
228- if ! f .pred (vals ) {
204+ func (f * FourRoundRBC ) receivePropose (m * typedefs.Message_Propose ) error {
205+ if ! f .pred (m .Content ) {
229206 return xerrors .New ("Given value did not pass the predicate" )
230207 }
231208
232209 // Hash the value
233- h , err := f .FreshHash (vals )
210+ h , err := f .FreshHash (m . Content )
234211 if err != nil {
235212 return err
236213 }
@@ -240,7 +217,7 @@ func (f *FourRoundRBC[M]) receivePropose(m *typedefs.Message_Propose) error {
240217
241218 // Broadcast an echo message for each encoding
242219 for _ , Mi := range encodings {
243- echoInst := createEchoMessage (Mi .Data , h , uint32 (Mi .Number ))
220+ echoInst := createEchoMessage (Mi .Val , h , uint32 (Mi .Idx ))
244221 err = f .broadcastInstruction (echoInst )
245222 if err != nil {
246223 return err
@@ -250,7 +227,7 @@ func (f *FourRoundRBC[M]) receivePropose(m *typedefs.Message_Propose) error {
250227 return nil
251228}
252229
253- func (f * FourRoundRBC [ M ] ) receiveEcho (msg * typedefs.Message_Echo ) error {
230+ func (f * FourRoundRBC ) receiveEcho (msg * typedefs.Message_Echo ) error {
254231 f .Lock ()
255232 defer f .Unlock ()
256233
@@ -295,11 +272,11 @@ func (f *FourRoundRBC[M]) receiveEcho(msg *typedefs.Message_Echo) error {
295272 return nil
296273}
297274
298- func (f * FourRoundRBC [ M ] ) receiveReady (msg * typedefs.Message_Ready ) bool {
275+ func (f * FourRoundRBC ) receiveReady (msg * typedefs.Message_Ready ) ( bool , error ) {
299276 f .Lock ()
300277 defer f .Unlock ()
301278
302- // Update the count of ready message received for that hash and check if a READY message needs to be sent
279+ // Update the count of ready messages received for that hash and check if a READY message needs to be sent
303280 count , ok := f .readyCounts [string (msg .H )]
304281 if ! ok {
305282 count = 0
@@ -319,13 +296,13 @@ func (f *FourRoundRBC[M]) receiveReady(msg *typedefs.Message_Ready) bool {
319296 err := f .broadcastInstruction (inst )
320297 if err != nil {
321298 f .log .Err (err ).Msg ("Failed to broadcast ready message" )
322- return false
299+ return false , err
323300 }
324301 f .sentReady = true
325302 f .log .Printf ("Sent Ready message for %x" , msg .Mi )
326303 }
327304
328- var value []M = nil
305+ var value []byte = nil
329306 hashMis , ok := f .readyMis [string (msg .H )]
330307
331308 if ! ok {
@@ -340,17 +317,13 @@ func (f *FourRoundRBC[M]) receiveReady(msg *typedefs.Message_Ready) bool {
340317 f .readyMis [string (msg .H )] = hashMis
341318
342319 finished := false
320+ var err error
343321 // If this value is seen for the first time, add it to T_h and try to reconstruct
344322 if firstTime {
345- MiScalar := f .Scalar ()
346- err := MiScalar .UnmarshalBinary (msg .Mi )
347- if err != nil {
348- return false
349- }
350323 f .log .Printf ("Received first ready message for %x" , msg .Mi )
351- f .th = append (f .th , infectious. Share {
352- Data : msg .Mi ,
353- Number : int (msg .I ),
324+ f .th = append (f .th , reedsolomon. Encoding {
325+ Val : msg .Mi ,
326+ Idx : int (msg .I ),
354327 })
355328 f .log .Printf ("Got %d messges in th" , len (f .th ))
356329 // Try to reconstruct
@@ -364,13 +337,13 @@ func (f *FourRoundRBC[M]) receiveReady(msg *typedefs.Message_Ready) bool {
364337 if finished {
365338 f .finalValue = value
366339 f .finished = true
367- return true
340+ return true , nil
368341 }
369342
370- return false
343+ return false , nil
371344}
372345
373- func (f * FourRoundRBC [ M ] ) reconstruct (expHash []byte ) ([]M , bool , error ) {
346+ func (f * FourRoundRBC ) reconstruct (expHash []byte ) ([]byte , bool , error ) {
374347 for ri := 0 ; ri < f .r ; ri ++ {
375348 if len (f .th ) < 2 * f .threshold + ri + 1 {
376349 // If it is not the case now, it won't be in the next iteration since r increases
@@ -382,21 +355,19 @@ func (f *FourRoundRBC[M]) reconstruct(expHash []byte) ([]M, bool, error) {
382355 return nil , false , err
383356 }
384357
385- coefficientsMarshalled , err := f .marshaller .Unmarshal (coefficients )
386- h , err := f .FreshHash (coefficientsMarshalled )
358+ h , err := f .FreshHash (coefficients )
387359 if err != nil {
388360 return nil , false , err
389361 }
390362
391363 if bytes .Equal (h , expHash ) {
392- return coefficientsMarshalled , true , nil
393-
364+ return coefficients , true , nil
394365 }
395366 }
396367 return nil , false , nil
397368}
398369
399- func (f * FourRoundRBC [ M ] ) broadcastInstruction (instruction * typedefs.Instruction ) error {
370+ func (f * FourRoundRBC ) broadcastInstruction (instruction * typedefs.Instruction ) error {
400371 out , err := proto .Marshal (instruction )
401372 if err != nil {
402373 return err
@@ -436,13 +407,13 @@ func createProposeMessage(ms []byte) *typedefs.Instruction {
436407 return inst
437408}
438409
439- func (f * FourRoundRBC [ M ] ) checkEchoThreshold (count int , hashReady bool ) bool {
410+ func (f * FourRoundRBC ) checkEchoThreshold (count int , hashReady bool ) bool {
440411 if hashReady {
441412 return count >= (f .threshold + 1 )
442413 }
443414 return count >= (2 * f .threshold + 1 )
444415}
445416
446- func (f * FourRoundRBC [ M ] ) checkReadyThreshold (count int ) bool {
417+ func (f * FourRoundRBC ) checkReadyThreshold (count int ) bool {
447418 return count >= (f .threshold + 1 )
448419}
0 commit comments