From 762b6ec2198fb8da3a4517c8bd412d423bc4ea93 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 4 Aug 2018 20:34:13 +0800 Subject: [PATCH] Aggregate responses and create schnorr multi-sig --- consensus/consensus.go | 4 +++- consensus/consensus_leader.go | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/consensus/consensus.go b/consensus/consensus.go index a62394b43..0cbab2fd3 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -18,7 +18,9 @@ import ( type Consensus struct { state ConsensusState // Commits collected from validators. A map from node Id to its commitment - commitments map[uint16]kyber.Point + commitments map[uint16]kyber.Point + aggregatedCommitment kyber.Point + // Commits collected from validators. bitmap *crypto.Mask // Responses collected from validators diff --git a/consensus/consensus_leader.go b/consensus/consensus_leader.go index deef1b4c5..9062c78f1 100644 --- a/consensus/consensus_leader.go +++ b/consensus/consensus_leader.go @@ -221,6 +221,7 @@ func (consensus *Consensus) constructChallengeMessage() []byte { // 32 byte challenge buffer.Write(getChallenge(aggCommitment, consensus.bitmap.AggregatePublic, buffer.Bytes()[:36])) // message contains consensus id and block hash for now. + consensus.aggregatedCommitment = aggCommitment // 64 byte of signature on previous data signature := consensus.signMessage(buffer.Bytes()) @@ -325,6 +326,23 @@ func (consensus *Consensus) processResponseMessage(payload []byte) { if len(consensus.responses) >= (2*len(consensus.validators))/3+1 && consensus.state != FINISHED { consensus.mutex.Lock() if len(consensus.responses) >= (2*len(consensus.validators))/3+1 && consensus.state != FINISHED { + // Aggregate responses + responses := make([]kyber.Scalar, 0) + for _, val := range consensus.responses { + responses = append(responses, val) + } + aggResponse, err := crypto.AggregateResponses(crypto.Ed25519Curve, responses) + if err != nil { + log.Error("Failed to aggregate responses") + return + } + collectiveSign, err := crypto.Sign(crypto.Ed25519Curve, consensus.aggregatedCommitment, aggResponse, consensus.bitmap) + if err != nil { + log.Error("Failed to create collective signature") + return + } + _ = collectiveSign // TODO: put the collective signature into block and broadcast + consensus.Log.Debug("Consensus reached with signatures.", "numOfSignatures", len(consensus.responses)) // Reset state to FINISHED, and clear other data. consensus.ResetState() @@ -335,7 +353,7 @@ func (consensus *Consensus) processResponseMessage(payload []byte) { // For now, we used the stored whole block already stored in consensus.blockHeader txDecoder := gob.NewDecoder(bytes.NewReader(consensus.blockHeader)) var blockHeaderObj blockchain.Block - err := txDecoder.Decode(&blockHeaderObj) + err = txDecoder.Decode(&blockHeaderObj) if err != nil { consensus.Log.Debug("failed to construct the new block after consensus") }