Skip to content

Commit

Permalink
Looking for missing setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ineiti committed Feb 27, 2024
1 parent ad067e6 commit 8621008
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 41 deletions.
100 changes: 73 additions & 27 deletions dkg/pedersen/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ func (s *instance) getState() *state {
}

// handleMessage implements dkgInstance. It handles the DKG messages.
func (s *instance) handleMessage(ctx context.Context, msg serde.Message, from mino.Address, out mino.Sender) error {
func (s *instance) handleMessage(
ctx context.Context,
msg serde.Message,
from mino.Address,
out mino.Sender,
) error {
// We expect a Start message or a decrypt request at first, but we might
// receive other messages in the meantime, like a Deal.
switch msg := msg.(type) {
Expand Down Expand Up @@ -194,8 +199,10 @@ func (s *instance) handleMessage(ctx context.Context, msg serde.Message, from mi
// start is called when the node has received its start message. Note that we
// might have already received some deals from other nodes in the meantime. The
// function handles the DKG creation protocol.
func (s *instance) start(ctx context.Context, start types.Start, deals channel.Timed[types.Deal],
resps channel.Timed[types.Response], from mino.Address, out mino.Sender) error {
func (s *instance) start(
ctx context.Context, start types.Start, deals channel.Timed[types.Deal],
resps channel.Timed[types.Response], from mino.Address, out mino.Sender,
) error {

err := s.startRes.switchState(sharing)
if err != nil {
Expand Down Expand Up @@ -227,8 +234,10 @@ func (s *instance) start(ctx context.Context, start types.Start, deals channel.T
}

// doDKG calls the subsequent DKG steps
func (s *instance) doDKG(ctx context.Context, deals channel.Timed[types.Deal],
resps channel.Timed[types.Response], out mino.Sender, from mino.Address) error {
func (s *instance) doDKG(
ctx context.Context, deals channel.Timed[types.Deal],
resps channel.Timed[types.Response], out mino.Sender, from mino.Address,
) error {

defer func() {
s.Lock()
Expand Down Expand Up @@ -315,7 +324,11 @@ func (s *instance) deal(ctx context.Context, out mino.Sender) error {
return nil
}

func (s *instance) respond(ctx context.Context, deals channel.Timed[types.Deal], out mino.Sender) error {
func (s *instance) respond(
ctx context.Context,
deals channel.Timed[types.Deal],
out mino.Sender,
) error {
numReceivedDeals := 0

participants := s.startRes.getParticipants()
Expand Down Expand Up @@ -346,7 +359,11 @@ func (s *instance) respond(ctx context.Context, deals channel.Timed[types.Deal],
// - Resharing with leaving or joining node: (n_common + (n_new - 1)) * n_old,
// nodes that are doing a resharing will broadcast their own deals
// - Resharing with staying node: (n_common + n_new) * n_old
func (s *instance) certify(ctx context.Context, resps channel.Timed[types.Response], expected int) error {
func (s *instance) certify(
ctx context.Context,
resps channel.Timed[types.Response],
expected int,
) error {

responsesReceived := 0

Expand Down Expand Up @@ -414,8 +431,10 @@ func (s *instance) finalize(ctx context.Context, from mino.Address, out mino.Sen
}

// handleDeal process the Deal and send the responses to the other nodes.
func (s *instance) handleDeal(ctx context.Context, msg types.Deal,
out mino.Sender, to []mino.Address) error {
func (s *instance) handleDeal(
ctx context.Context, msg types.Deal,
out mino.Sender, to []mino.Address,
) error {

deal := &pedersen.Deal{
Index: msg.GetIndex(),
Expand Down Expand Up @@ -466,7 +485,12 @@ func (s *instance) handleDeal(ctx context.Context, msg types.Deal,
return nil
}

func (s *instance) finalizeReshare(ctx context.Context, nt nodeType, out mino.Sender, from mino.Address) error {
func (s *instance) finalizeReshare(
ctx context.Context,
nt nodeType,
out mino.Sender,
from mino.Address,
) error {
// Send back the public DKG key
publicKey := s.startRes.getDistKey()

Expand Down Expand Up @@ -508,8 +532,14 @@ func (s *instance) finalizeReshare(ctx context.Context, nt nodeType, out mino.Se

// reshare handles the resharing request. Acts differently for the new
// and old and common nodes
func (s *instance) reshare(ctx context.Context, out mino.Sender,
from mino.Address, msg types.StartResharing, reshares channel.Timed[types.Reshare], resps channel.Timed[types.Response]) error {
func (s *instance) reshare(
ctx context.Context,
out mino.Sender,
from mino.Address,
msg types.StartResharing,
reshares channel.Timed[types.Reshare],
resps channel.Timed[types.Response],
) error {

err := s.startRes.switchState(resharing)
if err != nil {
Expand All @@ -534,8 +564,14 @@ func (s *instance) reshare(ctx context.Context, out mino.Sender,
// doReshare is called when the node has received its reshare message. Note that
// we might have already received some deals from other nodes in the meantime.
// The function handles the DKG resharing protocol.
func (s *instance) doReshare(ctx context.Context, start types.StartResharing,
from mino.Address, out mino.Sender, reshares channel.Timed[types.Reshare], resps channel.Timed[types.Response]) error {
func (s *instance) doReshare(
ctx context.Context,
start types.StartResharing,
from mino.Address,
out mino.Sender,
reshares channel.Timed[types.Reshare],
resps channel.Timed[types.Response],
) error {

s.log.Info().Msgf("resharing with %v", start.GetAddrsNew())

Expand Down Expand Up @@ -668,8 +704,10 @@ func (s *instance) doReshare(ctx context.Context, start types.StartResharing,
// sendDealsResharing is similar to sendDeals except that it creates
// dealResharing which has more data than Deal. Only the old nodes call this
// function.
func (s *instance) sendDealsResharing(ctx context.Context, out mino.Sender,
participants []mino.Address, publicCoeff []kyber.Point) error {
func (s *instance) sendDealsResharing(
ctx context.Context, out mino.Sender,
participants []mino.Address, publicCoeff []kyber.Point,
) error {

s.log.Trace().Msgf("%v is generating its deals", s.me)

Expand All @@ -692,7 +730,7 @@ func (s *instance) sendDealsResharing(ctx context.Context, out mino.Sender,
),
)

//dealResharing contains the public coefficients as well
// dealResharing contains the public coefficients as well
dealResharingMsg := types.NewReshare(dealMsg, publicCoeff)

s.log.Trace().Msgf("%s sent dealResharing %d", s.me, i)
Expand All @@ -714,8 +752,10 @@ func (s *instance) sendDealsResharing(ctx context.Context, out mino.Sender,

// receiveDealsResharing is similar to receiveDeals except that it receives the
// dealResharing. Only the new or common nodes call this function
func (s *instance) receiveDealsResharing(ctx context.Context, nt nodeType,
resharingRequest types.StartResharing, out mino.Sender, reshares channel.Timed[types.Reshare]) error {
func (s *instance) receiveDealsResharing(
ctx context.Context, nt nodeType,
resharingRequest types.StartResharing, out mino.Sender, reshares channel.Timed[types.Reshare],
) error {

s.log.Trace().Msgf("%v is handling deals from other nodes", s.me)

Expand Down Expand Up @@ -780,11 +820,13 @@ func (s *instance) receiveDealsResharing(ctx context.Context, nt nodeType,
return nil
}

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

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

S := suite.Point().Mul(s.privShare.V, msg.K)
Expand All @@ -801,11 +843,13 @@ func (s *instance) handleDecrypt(out mino.Sender, msg types.DecryptRequest,
return nil
}

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

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

ui := s.getUI(msg.K, msg.PubK)
Expand Down Expand Up @@ -841,8 +885,10 @@ func (s *instance) getUI(K, pubk kyber.Point) *share.PubShare {
}
}

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

type job struct {
index int // index where to put the response
Expand Down
27 changes: 15 additions & 12 deletions dkg/pedersen/pedersen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// initDkgFirst message helping the developer to verify whether setup did occur
const initDkgFirst = "you must first initialize DKG. Did you call setup() first?"
const initDkgFirst = "you must first initialize the DKG. Did you call setup() first? #"

// failedStreamCreation message indicating a stream creation failure
const failedStreamCreation = "failed to create stream: %v"
Expand All @@ -47,7 +47,7 @@ var (
// associated with the `dkg-decrypt` protocol.
protocolNameDecrypt = "dkg-decrypt"
// protocolNameReencrypt denotes the value of the protocol span tag
//// associated with the `dkg-reencrypt` protocol.
// // associated with the `dkg-reencrypt` protocol.
protocolNameReencrypt = "dkg-reencrypt"
// ProtocolNameResharing denotes the value of the protocol span tag
// associated with the `dkg-resharing` protocol.
Expand Down Expand Up @@ -214,7 +214,7 @@ func (a *Actor) GetPublicKey() (kyber.Point, error) {
func (a *Actor) Encrypt(msg []byte) (kyber.Point, []kyber.Point, error) {

if !a.startRes.Done() {
return nil, nil, xerrors.Errorf(initDkgFirst)
return nil, nil, xerrors.Errorf(initDkgFirst + "3")
}

pubK, err := a.GetPublicKey()
Expand Down Expand Up @@ -258,7 +258,7 @@ func (a *Actor) Encrypt(msg []byte) (kyber.Point, []kyber.Point, error) {
func (a *Actor) Decrypt(K kyber.Point, Cs []kyber.Point) ([]byte, error) {

if !a.startRes.Done() {
return nil, xerrors.Errorf(initDkgFirst)
return nil, xerrors.Errorf(initDkgFirst + "4")
}

ctx, cancel := context.WithTimeout(context.Background(), decryptTimeout)
Expand Down Expand Up @@ -332,12 +332,13 @@ func (a *Actor) Decrypt(K kyber.Point, Cs []kyber.Point) ([]byte, error) {
// this person.
//
// See https://arxiv.org/pdf/2205.08529.pdf / section 5.4 Protocol / step 1
func (a *Actor) VerifiableEncrypt(message []byte, GBar kyber.Point) (types.Ciphertext,
[]byte, error) {
func (a *Actor) VerifiableEncrypt(message []byte, GBar kyber.Point) (
types.Ciphertext,
[]byte, error,
) {

if !a.startRes.Done() {
return types.Ciphertext{}, nil, xerrors.Errorf("you must first initialize " +
"DKG. Did you call setup() first?")
return types.Ciphertext{}, nil, xerrors.Errorf(initDkgFirst + "5")
}

// Embed the message (or as much of it as will fit) into a curve point.
Expand Down Expand Up @@ -391,7 +392,7 @@ func (a *Actor) VerifiableEncrypt(message []byte, GBar kyber.Point) (types.Ciphe
func (a *Actor) VerifiableDecrypt(ciphertexts []types.Ciphertext) ([][]byte, error) {

if !a.startRes.Done() {
return nil, xerrors.Errorf(initDkgFirst)
return nil, xerrors.Errorf(initDkgFirst + "6")
}

players := mino.NewAddresses(a.startRes.getParticipants()...)
Expand Down Expand Up @@ -480,8 +481,10 @@ func (a *Actor) VerifiableDecrypt(ciphertexts []types.Ciphertext) ([][]byte, err
return decryptedMessage, nil
}

func newWorker(numParticipants int, decryptedMessage [][]byte,
responses []types.VerifiableDecryptReply, ciphertexts []types.Ciphertext) worker {
func newWorker(
numParticipants int, decryptedMessage [][]byte,
responses []types.VerifiableDecryptReply, ciphertexts []types.Ciphertext,
) worker {

return worker{
numParticipants: numParticipants,
Expand Down Expand Up @@ -535,7 +538,7 @@ func (w worker) work(jobIndex int) error {
// participants.
func (a *Actor) Reshare(coAuth crypto.CollectiveAuthority, thresholdNew int) error {
if !a.startRes.Done() {
return xerrors.Errorf(initDkgFirst)
return xerrors.Errorf(initDkgFirst + "7")
}

addrsNew := make([]mino.Address, 0, coAuth.Len())
Expand Down
5 changes: 3 additions & 2 deletions dkg/pedersen/reencrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type reencryptStatus struct {
// Reencrypt implements dkg.Actor.
func (a *Actor) Reencrypt(K kyber.Point, pubk kyber.Point) (XhatEnc kyber.Point, err error) {
if !a.startRes.Done() {
return nil, xerrors.Errorf(initDkgFirst)
return nil, xerrors.Errorf(initDkgFirst + "8")
}

ctx, cancel := context.WithTimeout(context.Background(), decryptTimeout)
Expand Down Expand Up @@ -79,7 +79,8 @@ func (a *Actor) Reencrypt(K kyber.Point, pubk kyber.Point) (XhatEnc kyber.Point,
if err == nil {
dela.Logger.Debug().Msgf("Reencryption Uis: %v", status.Uis)

XhatEnc, err := share.RecoverCommit(suites.MustFind("Ed25519"), status.Uis, status.threshold, status.nbnodes)
XhatEnc, err := share.RecoverCommit(suites.MustFind("Ed25519"), status.Uis,
status.threshold, status.nbnodes)
if err != nil {
return nil, xerrors.Errorf("Reencryption failed: %v", err)
}
Expand Down

0 comments on commit 8621008

Please sign in to comment.