Skip to content

Commit

Permalink
pederson test to reencrypt some data - part1
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed May 17, 2023
1 parent 92fd7c8 commit 7375dab
Show file tree
Hide file tree
Showing 7 changed files with 470 additions and 455 deletions.
8 changes: 6 additions & 2 deletions dkg/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"go.dedis.ch/dela/crypto"
"go.dedis.ch/dela/dkg/pedersen/types"
"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/share"
)

// DKG defines the primitive to start a DKG protocol
Expand All @@ -26,11 +27,14 @@ type Actor interface {

Encrypt(message []byte) (K, C kyber.Point, remainder []byte, err error)
Decrypt(K, C kyber.Point) ([]byte, error)
Reencrypt(k, c, pk kyber.Point) (K, C kyber.Point, remainder []byte, err error)

Reshare(co crypto.CollectiveAuthority, newThreshold int) error

VerifiableEncrypt(message []byte, GBar kyber.Point) (ciphertext types.Ciphertext, remainder []byte, err error)
VerifiableDecrypt(ciphertexts []types.Ciphertext) ([][]byte, error)
VerifiableReencrypt(ciphertexts []types.Ciphertext, pubk kyber.Point) (ciphertext []types.Ciphertext, remainder []byte, err error)

EncryptSecret(message []byte) (U kyber.Point, Cs []kyber.Point)
ReencryptSecret(U kyber.Point, Pk kyber.Point) (Uis []*share.PubShare, err error)

DecryptSecret(Cs []kyber.Point, XhatEnc kyber.Point, Sk kyber.Scalar) (message []byte, err error)
}
29 changes: 5 additions & 24 deletions dkg/pedersen/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,6 @@ func (s *instance) handleMessage(ctx context.Context, msg serde.Message, from mi

return s.handleReencryptRequest(out, msg, from)

case types.ReencryptReply:
err := s.startRes.checkState(certified)
if err != nil {
return xerrors.Errorf(badState, err)
}

return s.handleReencryptReply(out, msg, from)

default:
return xerrors.Errorf("expected Start message, decrypt request or "+
"Deal as first message, got: %T", msg)
Expand Down Expand Up @@ -814,11 +806,11 @@ func (s *instance) handleReencryptRequest(out mino.Sender, msg types.ReencryptRe
return xerrors.Errorf("you must first initialize DKG. Did you call setup() first?")
}

ui := s.getUI(msg.U, msg.Pk)
ui := s.getUI(msg.U, msg.PubK)

// Calculating proofs
si := suite.Scalar().Pick(suite.RandomStream())
uiHat := suite.Point().Mul(si, suite.Point().Add(msg.U, msg.Pk))
uiHat := suite.Point().Mul(si, suite.Point().Add(msg.U, msg.PubK))
hiHat := suite.Point().Mul(si, nil)
hash := sha256.New()
ui.V.MarshalTo(hash)
Expand All @@ -832,32 +824,21 @@ func (s *instance) handleReencryptRequest(out mino.Sender, msg types.ReencryptRe
errs := out.Send(response, from)
err := <-errs
if err != nil {
return xerrors.Errorf("got an error while sending the decrypt reply: %v", err)
return xerrors.Errorf("got an error while sending the reencrypt reply: %v", err)
}

return nil
}

func (s *instance) getUI(U, Xc kyber.Point) *share.PubShare {
func (s *instance) getUI(U, pubk kyber.Point) *share.PubShare {
v := suite.Point().Mul(s.privShare.V, U)
v.Add(v, suite.Point().Mul(s.privShare.V, Xc))
v.Add(v, suite.Point().Mul(s.privShare.V, pubk))
return &share.PubShare{
I: s.privShare.I,
V: v,
}
}

func (s *instance) handleReencryptReply(out mino.Sender, msg types.ReencryptReply,
from mino.Address) error {

if !s.startRes.Done() {
return xerrors.Errorf("you must first initialize DKG. Did you call setup() first?")
}

//TODO: to be continued...
return nil
}

func (s *instance) handleVerifiableDecrypt(out mino.Sender,
msg types.VerifiableDecryptRequest, from mino.Address) error {

Expand Down
147 changes: 147 additions & 0 deletions dkg/pedersen/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go.dedis.ch/dela/mino"
"go.dedis.ch/dela/serde"
"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/share"
"go.dedis.ch/kyber/v3/suites"
"golang.org/x/xerrors"
)
Expand Down Expand Up @@ -103,6 +104,19 @@ type VerifiableDecryptReply struct {
Sp []ShareAndProof
}

type ReencryptRequest struct {
U []byte
PubK PublicKey
VerificationData *[]byte
}

type ReencryptReply struct {
PubK PublicKey
Ui *share.PubShare
Ei []byte
Fi []byte
}

type Message struct {
Start *Start `json:",omitempty"`
StartResharing *StartResharing `json:",omitempty"`
Expand All @@ -114,6 +128,8 @@ type Message struct {
DecryptReply *DecryptReply `json:",omitempty"`
VerifiableDecryptReply *VerifiableDecryptReply `json:",omitempty"`
VerifiableDecryptRequest *VerifiableDecryptRequest `json:",omitempty"`
ReencryptRequest *ReencryptRequest `json:",omitempty"`
ReencryptReply *ReencryptReply `json:",omitempty"`
}

// MsgFormat is the engine to encode and decode dkg messages in JSON format.
Expand Down Expand Up @@ -177,6 +193,10 @@ func (f msgFormat) Encode(ctx serde.Context, msg serde.Message) ([]byte, error)
m, err = encodeDecryptReply(in)
case types.VerifiableDecryptReply:
m, err = encodeVerifiableDecryptReply(in)
case types.ReencryptRequest:
m, err = encodeReencryptRequest(in)
case types.ReencryptReply:
m, err = encodeReencryptReply(in)
default:
return nil, xerrors.Errorf("unsupported message of type '%T'", msg)
}
Expand Down Expand Up @@ -253,6 +273,12 @@ func (f msgFormat) Decode(ctx serde.Context, data []byte) (serde.Message, error)

case m.VerifiableDecryptReply != nil:
return f.decodeVerifiableDecryptReply(ctx, m.VerifiableDecryptReply)

case m.ReencryptRequest != nil:
return f.decodeReencryptRequest(ctx, m.ReencryptRequest)

case m.ReencryptReply != nil:
return f.decodeReencryptReply(ctx, m.ReencryptReply)
}

return nil, xerrors.New("message is empty")
Expand Down Expand Up @@ -538,6 +564,62 @@ func (f msgFormat) decodeDecryptRequest(ctx serde.Context, msg *DecryptRequest)
return req, nil
}

func encodeReencryptRequest(msg types.ReencryptRequest) (Message, error) {
u, err := msg.U.MarshalBinary()
if err != nil {
return Message{}, xerrors.Errorf("couldn't marshal U: %v", err)
}

pubk, err := msg.PubK.MarshalBinary()
if err != nil {
return Message{}, xerrors.Errorf("couldn't marshal PubK: %v", err)
}

v, err := msg.VerificationData.MarshalBinary()
if err != nil {
return Message{}, xerrors.Errorf("couldn't marshal PubK: %v", err)
}

req := ReencryptRequest{
U: u,
PubK: pubk,
VerificationData: v,
}

return Message{ReencryptRequest: &req}, nil
}

func encodeReencryptReply(msg types.ReencryptReply) (Message, error) {
pubk, err := msg.PubK.MarshalBinary()
if err != nil {
return Message{}, xerrors.Errorf("couldn't marshal PubK: %v", err)
}

ui, err := msg.Ui.MarshalBinary()
if err != nil {
return Message{}, xerrors.Errorf("couldn't marshal Ui: %v", err)
}

ei, err := msg.Ei.MarshalBinary()
if err != nil {
return Message{}, xerrors.Errorf("couldn't marshal Ei: %v", err)
}

fi, err := msg.Fi.MarshalBinary()
if err != nil {
return Message{}, xerrors.Errorf("couldn't marshal Fi: %v", err)
}

rep := ReencryptReply{
PubK: pubk,
Ui: ui,
Ei: ei,
Fi: fi,
}

return Message{ReencryptReply: &rep}, nil
}

func encodeVerifiableDecryptRequest(msg types.VerifiableDecryptRequest) (Message, error) {
ciphertexts := msg.GetCiphertexts()
var encodedCiphertexts []Ciphertext
Expand Down Expand Up @@ -778,3 +860,68 @@ func (f msgFormat) decodeVerifiableDecryptReply(ctx serde.Context,

return resp, nil
}

// TODO implement
func (f msgFormat) decodeReencryptRequest(ctx serde.Context, request *ReencryptRequest) (serde.Message, error) {
u := f.suite.Point()
err := u.UnmarshalBinary(request.U)
if err != nil {
return nil, xerrors.Errorf("couldn't unmarshal U: %v", err)
}

pubk := f.suite.Point()
err = pubk.UnmarshalBinary(request.PubK)
if err != nil {
return nil, xerrors.Errorf("couldn't unmarshal PubK: %v", err)
}

v := f.suite.Scalar()
err = v.UnmarshalBinary(request.VerificationData)
if err != nil {
return nil, xerrors.Errorf("couldn't marshal verification data: %v", err)
}

resp := types.ReencryptRequest{
U: u,
PubK: pubk,
VerificationData: v,
}

return resp, nil
}

// TODO implement
func (f msgFormat) decodeReencryptReply(ctx serde.Context, reply *ReencryptReply) (serde.Message, error) {
pubk := f.suite.Point()
err := pubk.UnmarshalBinary(reply.PubK)
if err != nil {
return nil, xerrors.Errorf("couldn't unmarshal PubK: %v", err)
}

ui := f.suite.Point()
err = ui.UnmarshalBinary(reply.Ui)
if err != nil {
return nil, xerrors.Errorf("couldn't unmarshal Ui: %v", err)
}

ei := f.suite.Scalar()
err = ei.UnmarshalBinary(reply.Ei)
if err != nil {
return nil, xerrors.Errorf("couldn't unmarshal Ei: %v", err)
}

fi := f.suite.Scalar()
err = fi.UnmarshalBinary(reply.Fi)
if err != nil {
return nil, xerrors.Errorf("couldn't unmarshal Fi: %v", err)
}

resp := types.ReencryptReply{
PubK: pubk,
Ui: ui,
Ei: ei,
Fi: fi,
}

return resp, nil
}
Loading

0 comments on commit 7375dab

Please sign in to comment.