@@ -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.
785824func (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}
0 commit comments