Skip to content

Commit 78c552f

Browse files
authored
Merge pull request #609 from dedis/remove-protobuf-dependencies
Remove protobuf dependencies
2 parents d00dc2c + 0794135 commit 78c552f

11 files changed

Lines changed: 181 additions & 48 deletions

File tree

encoding_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
9-
"go.dedis.ch/protobuf"
9+
"go.dedis.ch/kyber/v4/internal/protobuf"
1010
)
1111

1212
type intWrapper struct {

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/kilic/bls12-381 v0.1.0
1010
github.com/stretchr/testify v1.11.1
1111
go.dedis.ch/fixbuf v1.0.3
12-
go.dedis.ch/protobuf v1.0.11
1312
golang.org/x/crypto v0.44.0
1413
golang.org/x/sys v0.38.0
1514
gopkg.in/yaml.v3 v3.0.1

go.sum

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZ
55
github.com/consensys/gnark-crypto v0.19.2 h1:qrEAIXq3T4egxqiliFFoNrepkIWVEeIYwt3UL0fvS80=
66
github.com/consensys/gnark-crypto v0.19.2/go.mod h1:rT23F0XSZqE0mUA0+pRtnL56IbPxs6gp4CeRsBk4XS0=
77
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
8-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
98
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
109
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1110
github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbdFz6I=
@@ -22,23 +21,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
2221
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2322
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
2423
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
25-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
26-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2724
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
2825
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
2926
go.dedis.ch/fixbuf v1.0.3 h1:hGcV9Cd/znUxlusJ64eAlExS+5cJDIyTyEG+otu5wQs=
3027
go.dedis.ch/fixbuf v1.0.3/go.mod h1:yzJMt34Wa5xD37V5RTdmp38cz3QhMagdGoem9anUalw=
31-
go.dedis.ch/kyber/v3 v3.0.4/go.mod h1:OzvaEnPvKlyrWyp3kGXlFdp7ap1VC6RkZDTaPikqhsQ=
32-
go.dedis.ch/kyber/v3 v3.0.9 h1:i0ZbOQocHUjfFasBiUql5zVeC7u/vahFd96DFA8UOWk=
33-
go.dedis.ch/kyber/v3 v3.0.9/go.mod h1:rhNjUUg6ahf8HEg5HUvVBYoWY4boAafX8tYxX+PS+qg=
34-
go.dedis.ch/protobuf v1.0.5/go.mod h1:eIV4wicvi6JK0q/QnfIEGeSFNG0ZeB24kzut5+HaRLo=
35-
go.dedis.ch/protobuf v1.0.7/go.mod h1:pv5ysfkDX/EawiPqcW3ikOxsL5t+BqnV6xHSmE79KI4=
36-
go.dedis.ch/protobuf v1.0.11 h1:FTYVIEzY/bfl37lu3pR4lIj+F9Vp1jE8oh91VmxKgLo=
37-
go.dedis.ch/protobuf v1.0.11/go.mod h1:97QR256dnkimeNdfmURz0wAMNVbd1VmLXhG1CrTYrJ4=
38-
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
3928
golang.org/x/crypto v0.44.0 h1:A97SsFvM3AIwEEmTBiaxPPTYpDC47w720rdiiUvgoAU=
4029
golang.org/x/crypto v0.44.0/go.mod h1:013i+Nw79BMiQiMsOPcVCB5ZIJbYkerPrGnOa00tvmc=
41-
golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4230
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4331
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
4432
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=

internal/v3marshaling.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"reflect"
7+
8+
"go.dedis.ch/kyber/v4"
9+
"go.dedis.ch/kyber/v4/internal/protobuf"
10+
"go.dedis.ch/kyber/v4/share"
11+
)
12+
13+
// Suite defines the capabilities required by the v3marshalling package.
14+
type Suite interface {
15+
// Group is needed for Group.Scalar
16+
kyber.Group
17+
}
18+
19+
// compatiblePriShare is a struct for PriShare used when marshaling to
20+
// ensure compatibility with V3
21+
type compatiblePriShare struct {
22+
I int64
23+
V kyber.Scalar
24+
}
25+
26+
// MarshalPriShare marshals a share.PriShare into bytes or returns an error
27+
// if the encoding did not work. Encoding is compatible with Kyber V3
28+
func MarshalPriShare(priShare *share.PriShare) ([]byte, error) {
29+
toEncode := &compatiblePriShare{
30+
I: int64(priShare.I),
31+
V: priShare.V,
32+
}
33+
return protobuf.Encode(toEncode)
34+
}
35+
36+
// UnmarshalPriShare unmarshals a share.PriShare from bytes or returns an error
37+
// if the decoding did not work. Decoding is compatible with Kyber V3
38+
func UnmarshalPriShare(data []byte, suite Suite) (*share.PriShare, error) {
39+
compatiblePriShare := &compatiblePriShare{}
40+
constructors := make(protobuf.Constructors)
41+
constructors[reflect.TypeFor[kyber.Scalar]()] = func() interface{} { return suite.Scalar() }
42+
err := protobuf.DecodeWithConstructors(data, compatiblePriShare, constructors)
43+
44+
// Check for overflow on I
45+
if compatiblePriShare.I < 0 || compatiblePriShare.I > math.MaxUint32 {
46+
return nil, fmt.Errorf("cannot cast I as uint32 due to overflow")
47+
}
48+
49+
if err != nil {
50+
return nil, err
51+
}
52+
priShare := &share.PriShare{
53+
I: uint32(compatiblePriShare.I),
54+
V: compatiblePriShare.V,
55+
}
56+
return priShare, nil
57+
}

pairing/bn254/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/stretchr/testify/require"
1212
"go.dedis.ch/kyber/v4"
1313
"go.dedis.ch/kyber/v4/group/mod"
14+
"go.dedis.ch/kyber/v4/internal/protobuf"
1415
"go.dedis.ch/kyber/v4/util/random"
15-
"go.dedis.ch/protobuf"
1616
)
1717

1818
func TestScalarMarshal(t *testing.T) {

pairing/bn256/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/stretchr/testify/require"
1111
"go.dedis.ch/kyber/v4"
1212
"go.dedis.ch/kyber/v4/group/mod"
13+
"go.dedis.ch/kyber/v4/internal/protobuf"
1314
"go.dedis.ch/kyber/v4/util/random"
14-
"go.dedis.ch/protobuf"
1515
//nolint:staticcheck // bn256 is deprecated. We need this package for our implementation.
1616
"golang.org/x/crypto/bn256"
1717
)

share/dkg/rabin/dkg.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import (
4545
"go.dedis.ch/kyber/v4/share"
4646
vss "go.dedis.ch/kyber/v4/share/vss/rabin"
4747
"go.dedis.ch/kyber/v4/sign/schnorr"
48-
"go.dedis.ch/protobuf"
4948
)
5049

5150
// Suite wraps the functionalities needed by the dkg package
@@ -681,7 +680,7 @@ func (cc *ComplaintCommits) Hash(s Suite) []byte {
681680
_, _ = h.Write([]byte("commitcomplaint"))
682681
_ = binary.Write(h, binary.LittleEndian, cc.Index)
683682
_ = binary.Write(h, binary.LittleEndian, cc.DealerIndex)
684-
buff, _ := protobuf.Encode(cc.Deal)
683+
buff, _ := cc.Deal.Marshal()
685684
_, _ = h.Write(buff)
686685
return h.Sum(nil)
687686
}

share/vss/pedersen/vss.go

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
"reflect"
1313

1414
"go.dedis.ch/kyber/v4"
15+
"go.dedis.ch/kyber/v4/internal"
16+
"go.dedis.ch/kyber/v4/internal/protobuf"
1517
"go.dedis.ch/kyber/v4/share"
1618
"go.dedis.ch/kyber/v4/sign/schnorr"
17-
"go.dedis.ch/protobuf"
1819
)
1920

2021
// Suite defines the capabilities required by the vss package.
@@ -58,6 +59,53 @@ type Deal struct {
5859
Commitments []kyber.Point
5960
}
6061

62+
// pedersenCompatibleDeal is a struct for Deal used when marshaling
63+
// to ensure compatibility with Kyber V3.
64+
type pedersenCompatibleDeal struct {
65+
SessionID []byte
66+
SecShare []byte
67+
T uint32
68+
Commitments []kyber.Point
69+
}
70+
71+
// Marshal marshals a Deal into bytes or return an error if encoding failed.
72+
// This encoding should always be preferred as it is compatible with Kyber V3.
73+
func (d *Deal) Marshal() ([]byte, error) {
74+
secShareBytes, err := internal.MarshalPriShare(d.SecShare)
75+
if err != nil {
76+
return nil, err
77+
}
78+
compatibleDeal := &pedersenCompatibleDeal{
79+
SessionID: d.SessionID,
80+
SecShare: secShareBytes,
81+
T: d.T,
82+
Commitments: d.Commitments,
83+
}
84+
return protobuf.Encode(compatibleDeal)
85+
}
86+
87+
// Unmarshal unmarshals a Deal from bytes or return an error if decoding failed.
88+
// This decoding should always be preferred as it is compatible with Kyber V3.
89+
func (d *Deal) Unmarshal(data []byte, suite Suite) error {
90+
compatibleDeal := &pedersenCompatibleDeal{}
91+
constructors := make(protobuf.Constructors)
92+
constructors[reflect.TypeFor[kyber.Point]()] = func() interface{} { return suite.Point() }
93+
err := protobuf.DecodeWithConstructors(data, compatibleDeal, constructors)
94+
if err != nil {
95+
return err
96+
}
97+
secShare, err := internal.UnmarshalPriShare(compatibleDeal.SecShare, suite)
98+
if err != nil {
99+
return err
100+
}
101+
102+
d.SessionID = compatibleDeal.SessionID
103+
d.T = compatibleDeal.T
104+
d.SecShare = secShare
105+
d.Commitments = compatibleDeal.Commitments
106+
return nil
107+
}
108+
61109
// EncryptedDeal contains the deal in a encrypted form only decipherable by the
62110
// correct recipient. The encryption is performed in a similar manner as what is
63111
// done in TLS. The dealer generates a temporary key pair, signs it with its
@@ -67,7 +115,7 @@ type EncryptedDeal struct {
67115
DHKey []byte
68116
// Signature of the DH key by the longterm key of the dealer
69117
Signature []byte
70-
// AEAD encryption of the deal marshalled by protobuf
118+
// AEAD encryption of the deal marshalled
71119
Cipher []byte
72120
}
73121

@@ -193,7 +241,7 @@ func (d *Dealer) EncryptedDeal(i int) (*EncryptedDeal, error) {
193241
}
194242

195243
nonce := make([]byte, gcm.NonceSize())
196-
dealBuff, err := protobuf.Encode(d.deals[i])
244+
dealBuff, err := d.deals[i].Marshal()
197245
if err != nil {
198246
return nil, err
199247
}
@@ -408,7 +456,7 @@ func (v *Verifier) decryptDeal(e *EncryptedDeal) (*Deal, error) {
408456
return nil, err
409457
}
410458
deal := &Deal{}
411-
err = deal.decode(v.suite, decrypted)
459+
err = deal.Unmarshal(decrypted, v.suite)
412460
return deal, err
413461
}
414462

@@ -772,22 +820,13 @@ func (r *Response) Hash(s Suite) []byte {
772820
return h.Sum(nil)
773821
}
774822

775-
func (d *Deal) decode(s Suite, buff []byte) error {
776-
constructors := make(protobuf.Constructors)
777-
var point kyber.Point
778-
var secret kyber.Scalar
779-
constructors[reflect.TypeOf(&point).Elem()] = func() interface{} { return s.Point() }
780-
constructors[reflect.TypeOf(&secret).Elem()] = func() interface{} { return s.Scalar() }
781-
return protobuf.DecodeWithConstructors(buff, d, constructors)
782-
}
783-
784823
// Hash returns the hash of a Justification.
785824
func (j *Justification) Hash(s Suite) []byte {
786825
h := s.Hash()
787826
_, _ = h.Write([]byte("justification"))
788827
_, _ = h.Write(j.SessionID)
789828
_ = binary.Write(h, binary.LittleEndian, j.Index)
790-
buff, _ := protobuf.Encode(j.Deal)
829+
buff, _ := j.Deal.Marshal()
791830
_, _ = h.Write(buff)
792831
return h.Sum(nil)
793832
}

share/vss/pedersen/vss_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"github.com/stretchr/testify/require"
1111
"go.dedis.ch/kyber/v4"
1212
"go.dedis.ch/kyber/v4/group/edwards25519"
13+
"go.dedis.ch/kyber/v4/internal/protobuf"
1314
"go.dedis.ch/kyber/v4/sign/schnorr"
1415
"go.dedis.ch/kyber/v4/xof/blake2xb"
15-
"go.dedis.ch/protobuf"
1616
)
1717

1818
var rng = blake2xb.New(nil)

0 commit comments

Comments
 (0)