Skip to content

Commit 9dcf640

Browse files
committed
update RS codes to use Berlekamp-Welch
Modify marshalling and unmarshalling to match new RS code and update test
1 parent 890c996 commit 9dcf640

File tree

8 files changed

+269
-186
lines changed

8 files changed

+269
-186
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ go 1.23.0
55
toolchain go1.24.1
66

77
require (
8+
github.com/HACKERALERT/infectious v0.0.0-20240622035800-1b100aa35681
9+
github.com/rs/zerolog v1.34.0
810
github.com/stretchr/testify v1.9.0
911
go.dedis.ch/kyber/v4 v4.0.0-pre2.0.20250219110603-23debab3f61d
1012
go.dedis.ch/protobuf v1.0.11
@@ -18,7 +20,6 @@ require (
1820
github.com/mattn/go-colorable v0.1.13 // indirect
1921
github.com/mattn/go-isatty v0.0.19 // indirect
2022
github.com/pmezard/go-difflib v1.0.0 // indirect
21-
github.com/rs/zerolog v1.34.0 // indirect
2223
go.dedis.ch/fixbuf v1.0.3 // indirect
2324
golang.org/x/crypto v0.35.0 // indirect
2425
golang.org/x/sys v0.30.0 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/HACKERALERT/infectious v0.0.0-20240622035800-1b100aa35681 h1:LOILOeWljaUZyXLpJrfY3F2F+YiXKcv867b/35SB1JQ=
2+
github.com/HACKERALERT/infectious v0.0.0-20240622035800-1b100aa35681/go.mod h1:ZGuTcvIkN3f3CPe2PmI4hqVfKD/253FCvrQGDJ+Ca9E=
13
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
24
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -45,16 +47,12 @@ go.dedis.ch/protobuf v1.0.7/go.mod h1:pv5ysfkDX/EawiPqcW3ikOxsL5t+BqnV6xHSmE79KI
4547
go.dedis.ch/protobuf v1.0.11 h1:FTYVIEzY/bfl37lu3pR4lIj+F9Vp1jE8oh91VmxKgLo=
4648
go.dedis.ch/protobuf v1.0.11/go.mod h1:97QR256dnkimeNdfmURz0wAMNVbd1VmLXhG1CrTYrJ4=
4749
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
48-
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
49-
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
5050
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
5151
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
5252
golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5353
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5454
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5555
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
56-
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
57-
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5856
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
5957
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
6058
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

rbc/fourRounds/four_rounds_rbc.go

Lines changed: 33 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
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
4747
type 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

5252
type 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

7575
func 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

118118
func (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

134136
func (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

231226
func (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

Comments
 (0)