@@ -38,6 +38,7 @@ import (
3838 "reflect"
3939
4040 "go.dedis.ch/kyber/v4"
41+ "go.dedis.ch/kyber/v4/internal"
4142 "go.dedis.ch/kyber/v4/internal/protobuf"
4243 "go.dedis.ch/kyber/v4/share"
4344 "go.dedis.ch/kyber/v4/sign/schnorr"
@@ -86,6 +87,65 @@ type Deal struct {
8687 Commitments []kyber.Point
8788}
8889
90+ // rabinCompatibleDeal is a struct for Deal used when marshaling
91+ // to ensure compatibility with Kyber V3.
92+ type rabinCompatibleDeal struct {
93+ SessionID []byte
94+ SecShare []byte
95+ RndShare []byte
96+ T uint32
97+ Commitments []kyber.Point
98+ }
99+
100+ // Marshal marshals a Deal into bytes or return an error if encoding failed.
101+ // This encoding should always be preferred as it is compatible with Kyber V3.
102+ func (deal * Deal ) Marshal () ([]byte , error ) {
103+ secShareBytes , err := internal .MarshalPriShare (deal .SecShare )
104+ if err != nil {
105+ return nil , err
106+ }
107+ rndShareBytes , err := internal .MarshalPriShare (deal .RndShare )
108+ if err != nil {
109+ return nil , err
110+ }
111+ compatibleDeal := & rabinCompatibleDeal {
112+ SessionID : deal .SessionID ,
113+ SecShare : secShareBytes ,
114+ RndShare : rndShareBytes ,
115+ T : deal .T ,
116+ Commitments : deal .Commitments ,
117+ }
118+ return protobuf .Encode (compatibleDeal )
119+ }
120+
121+ // Unmarshal unmarshals a Deal from bytes or return an error if decoding failed.
122+ // This decoding should always be preferred as it is compatible with Kyber V3.
123+ func (deal * Deal ) Unmarshal (data []byte , suite Suite ) error {
124+ compatibleDeal := & rabinCompatibleDeal {}
125+ constructors := make (protobuf.Constructors )
126+ constructors [reflect .TypeFor [kyber.Point ]()] = func () interface {} { return suite .Point () }
127+ err := protobuf .DecodeWithConstructors (data , compatibleDeal , constructors )
128+ if err != nil {
129+ return err
130+ }
131+
132+ secShare , err := internal .UnmarshalPriShare (compatibleDeal .SecShare , suite )
133+ if err != nil {
134+ return err
135+ }
136+
137+ rndShare , err := internal .UnmarshalPriShare (compatibleDeal .RndShare , suite )
138+ if err != nil {
139+ return err
140+ }
141+ deal .SessionID = compatibleDeal .SessionID
142+ deal .SecShare = secShare
143+ deal .RndShare = rndShare
144+ deal .T = compatibleDeal .T
145+ deal .Commitments = compatibleDeal .Commitments
146+ return nil
147+ }
148+
89149// EncryptedDeal contains the deal in a encrypted form only decipherable by the
90150// correct recipient. The encryption is performed in a similar manner as what is
91151// done in TLS. The dealer generates a temporary key pair, signs it with its
@@ -95,7 +155,7 @@ type EncryptedDeal struct {
95155 DHKey kyber.Point
96156 // Signature of the DH key by the longterm key of the dealer
97157 Signature []byte
98- // AEAD encryption of the deal marshalled by protobuf
158+ // AEAD encryption of the deal marshalled
99159 Cipher []byte
100160}
101161
@@ -222,7 +282,7 @@ func (d *Dealer) EncryptedDeal(i int) (*EncryptedDeal, error) {
222282 }
223283
224284 nonce := make ([]byte , gcm .NonceSize ())
225- dealBuff , err := protobuf . Encode ( d .deals [i ])
285+ dealBuff , err := d .deals [i ]. Marshal ( )
226286 if err != nil {
227287 return nil , err
228288 }
@@ -438,7 +498,7 @@ func (v *Verifier) decryptDeal(e *EncryptedDeal) (*Deal, error) {
438498 return nil , err
439499 }
440500 deal := & Deal {}
441- err = deal .decode ( v .suite , decrypted )
501+ err = deal .Unmarshal ( decrypted , v .suite )
442502 return deal , err
443503}
444504
@@ -763,22 +823,13 @@ func (r *Response) Hash(s Suite) []byte {
763823 return h .Sum (nil )
764824}
765825
766- func (d * Deal ) decode (s Suite , buff []byte ) error {
767- constructors := make (protobuf.Constructors )
768- var point kyber.Point
769- var secret kyber.Scalar
770- constructors [reflect .TypeOf (& point ).Elem ()] = func () interface {} { return s .Point () }
771- constructors [reflect .TypeOf (& secret ).Elem ()] = func () interface {} { return s .Scalar () }
772- return protobuf .DecodeWithConstructors (buff , d , constructors )
773- }
774-
775826// Hash returns the hash of a Justification.
776827func (j * Justification ) Hash (s Suite ) []byte {
777828 h := s .Hash ()
778829 _ , _ = h .Write ([]byte ("justification" ))
779830 _ , _ = h .Write (j .SessionID )
780831 _ = binary .Write (h , binary .LittleEndian , j .Index )
781- buff , _ := protobuf . Encode ( j .Deal )
832+ buff , _ := j .Deal . Marshal ( )
782833 _ , _ = h .Write (buff )
783834 return h .Sum (nil )
784835}
0 commit comments