55 "context"
66 "errors"
77 "fmt"
8+ "github.com/HACKERALERT/infectious"
89 "github.com/rs/zerolog"
910 "go.dedis.ch/kyber/v4"
10- "go.dedis.ch/kyber/v4/share"
1111 "golang.org/x/xerrors"
1212 "google.golang.org/protobuf/proto"
1313 "hash"
@@ -45,24 +45,24 @@ var (
4545// Marshaller represents an interface for an object that can marshal
4646// and unmarshal some value type
4747type Marshaller [M any ] interface {
48- Marshal (M ) ([]byte , error )
49- Unmarshal ([]byte ) (M , error )
48+ Marshal ([] M ) ([]byte , error )
49+ Unmarshal ([]byte ) ([] M , error )
5050}
5151
5252type FourRoundRBC [M any ] struct {
5353 iface rbc.AuthenticatedMessageStream
5454 marshaller Marshaller [M ]
5555 pred func ([]M ) bool
5656 hash.Hash
57- rs * reedsolomon.RSCodes
57+ rs reedsolomon.RSCodes
5858 stopChan chan struct {}
5959 threshold int
6060 sentReady bool
6161 sync.RWMutex
6262 echoCount map [string ]int
6363 readyCounts map [string ]int
6464 readyMis map [string ]map [string ]struct {}
65- th []* share. PriShare
65+ th []infectious. Share
6666 kyber.Group
6767 r int
6868 nbNodes int
@@ -74,7 +74,7 @@ type FourRoundRBC[M any] struct {
7474
7575func NewFourRoundRBC [M any ](pred func ([]M ) bool , h hash.Hash , threshold int ,
7676 iface rbc.AuthenticatedMessageStream ,
77- marshaller Marshaller [M ], group kyber.Group , r , nbNodes int , id uint32 ) * FourRoundRBC [M ] {
77+ marshaller Marshaller [M ], group kyber.Group , r , nbNodes , mLen int , id uint32 ) * FourRoundRBC [M ] {
7878
7979 // Disable logging based on the GLOG environment variable
8080 var logLevel zerolog.Level
@@ -96,15 +96,15 @@ func NewFourRoundRBC[M any](pred func([]M) bool, h hash.Hash, threshold int,
9696 marshaller : marshaller ,
9797 pred : pred ,
9898 Hash : h ,
99- rs : reedsolomon .NewRSCodes ( group ),
99+ rs : reedsolomon .NewBWCodes ( mLen , nbNodes ),
100100 stopChan : make (chan struct {}),
101101 threshold : threshold ,
102102 sentReady : false ,
103103 RWMutex : sync.RWMutex {},
104104 echoCount : make (map [string ]int ),
105105 readyCounts : make (map [string ]int ),
106106 readyMis : make (map [string ]map [string ]struct {}),
107- th : make ([]* share. PriShare , 0 ),
107+ th : make ([]infectious. Share , 0 ),
108108 Group : group ,
109109 r : r ,
110110 nbNodes : nbNodes ,
@@ -117,31 +117,26 @@ func NewFourRoundRBC[M any](pred func([]M) bool, h hash.Hash, threshold int,
117117
118118func (f * FourRoundRBC [M ]) FreshHash (ms []M ) ([]byte , error ) {
119119 f .Hash .Reset ()
120- for _ , m := range ms {
121- b , err := f .marshaller .Marshal (m )
122- if err != nil {
123- return nil , err
124- }
125- _ , err = f .Hash .Write (b )
126- if err != nil {
127- return nil , err
128- }
120+
121+ // Marshall the list of value
122+ bs , err := f .marshaller .Marshal (ms )
123+ if err != nil {
124+ return nil , err
125+ }
126+
127+ // Write the bytes
128+ _ , err = f .Hash .Write (bs )
129+ if err != nil {
130+ return nil , err
129131 }
130132 h := f .Hash .Sum (nil )
131133 return h , nil
132134}
133135
134136func (f * FourRoundRBC [M ]) broadcast (ms []M ) error {
135- msBytes := make ([][]byte , len (ms ))
136- for i , m := range ms {
137- b , err := f .marshaller .Marshal (m )
138- if err != nil {
139- return err
140- }
141- msBytes [i ] = b
142- }
137+ msBytes , err := f .marshaller .Marshal (ms )
143138 inst := createProposeMessage (msBytes )
144- err : = f .broadcastInstruction (inst )
139+ err = f .broadcastInstruction (inst )
145140 return err
146141}
147142
@@ -150,7 +145,7 @@ func (f *FourRoundRBC[M]) start(cancelFunc context.CancelFunc) {
150145 for {
151146 bs , err := f .iface .Receive (f .stopChan )
152147 if err != nil {
153- // Check if the error is that the receive was stop via the channel or not
148+ // Check if the error is that the " receive" was stop via the channel or not
154149 if errors .Is (err , rbc.NodeStoppedError {}) {
155150 // The channel was stopped so we just return
156151 cancelFunc ()
@@ -229,16 +224,7 @@ func (f *FourRoundRBC[M]) handleMessage(instruction *typedefs.Instruction) (erro
229224}
230225
231226func (f * FourRoundRBC [M ]) receivePropose (m * typedefs.Message_Propose ) error {
232-
233- valBytes := m .Content
234- vals := make ([]M , len (valBytes ))
235- for i , b := range valBytes {
236- value , err := f .marshaller .Unmarshal (b )
237- if err != nil {
238- return err
239- }
240- vals [i ] = value
241- }
227+ vals , err := f .marshaller .Unmarshal (m .Content )
242228 if ! f .pred (vals ) {
243229 return xerrors .New ("Given value did not pass the predicate" )
244230 }
@@ -249,29 +235,12 @@ func (f *FourRoundRBC[M]) receivePropose(m *typedefs.Message_Propose) error {
249235 return err
250236 }
251237
252- scalars := make ([]kyber.Scalar , len (m .Content ))
253- for i , b := range m .Content {
254- scalar := f .Group .Scalar ().One ()
255- err := scalar .UnmarshalBinary (b )
256- if err != nil {
257- return err
258- }
259- scalars [i ] = scalar
260- }
261-
262238 // Encode to have a share to send for each node
263- encodings , err := f .rs .Encode (scalars , f . nbNodes )
239+ encodings , err := f .rs .Encode (m . Content )
264240
265241 // Broadcast an echo message for each encoding
266242 for _ , Mi := range encodings {
267- if Mi == nil {
268- return err
269- }
270- MiBytes , err := Mi .V .MarshalBinary ()
271- if err != nil {
272- return err
273- }
274- echoInst := createEchoMessage (MiBytes , h , Mi .I )
243+ echoInst := createEchoMessage (Mi .Data , h , uint32 (Mi .Number ))
275244 err = f .broadcastInstruction (echoInst )
276245 if err != nil {
277246 return err
@@ -379,9 +348,9 @@ func (f *FourRoundRBC[M]) receiveReady(msg *typedefs.Message_Ready) bool {
379348 return false
380349 }
381350 f .log .Printf ("Received first ready message for %x" , msg .Mi )
382- f .th = append (f .th , & share. PriShare {
383- I : msg .I ,
384- V : MiScalar ,
351+ f .th = append (f .th , infectious. Share {
352+ Data : msg .Mi ,
353+ Number : int ( msg . I ) ,
385354 })
386355 f .log .Printf ("Got %d messges in th" , len (f .th ))
387356 // Try to reconstruct
@@ -407,22 +376,13 @@ func (f *FourRoundRBC[M]) reconstruct(expHash []byte) ([]M, bool, error) {
407376 // If it is not the case now, it won't be in the next iteration since r increases
408377 return nil , false , nil
409378 }
410- coefficients , err := f .rs .Decode (f .th , f .threshold + 1 )
379+
380+ coefficients , err := f .rs .Decode (f .th )
411381 if err != nil {
412382 return nil , false , err
413383 }
414- coefficientsMarshalled := make ([]M , len (coefficients ))
415- for i , c := range coefficients {
416- b , err := c .MarshalBinary ()
417- if err != nil {
418- return nil , false , err
419- }
420- m , err := f .marshaller .Unmarshal (b )
421- if err != nil {
422- return nil , false , err
423- }
424- coefficientsMarshalled [i ] = m
425- }
384+
385+ coefficientsMarshalled , err := f .marshaller .Unmarshal (coefficients )
426386 h , err := f .FreshHash (coefficientsMarshalled )
427387 if err != nil {
428388 return nil , false , err
@@ -467,7 +427,7 @@ func createEchoMessage(mi, h []byte, i uint32) *typedefs.Instruction {
467427 return inst
468428}
469429
470- func createProposeMessage (ms [][] byte ) * typedefs.Instruction {
430+ func createProposeMessage (ms []byte ) * typedefs.Instruction {
471431 proposeMsg := & typedefs.Message_Propose {
472432 Content : ms ,
473433 }
0 commit comments