WIP: tests

This commit is contained in:
programmer10110 2020-08-14 17:38:58 +03:00
parent ebf2cd19bf
commit 331fc9d2cf
3 changed files with 108 additions and 14 deletions

View File

@ -14,7 +14,10 @@ import (
"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/encrypt/ecies"
"go.dedis.ch/kyber/v3/pairing/bn256"
"go.dedis.ch/kyber/v3/share"
dkgPedersen "go.dedis.ch/kyber/v3/share/dkg/pedersen"
"go.dedis.ch/kyber/v3/sign/bls"
"go.dedis.ch/kyber/v3/sign/tbls"
"log"
"sync"
)
@ -322,14 +325,12 @@ func (am *AirgappedMachine) handleStateDkgMasterKeyAwaitConfirmations(o *client.
return fmt.Errorf("failed to process responses: %w", err)
}
//TODO: THIS BLOCK IS WRONG. @oopcode, what exactly should we broadcast?
///////////////////////////////////////////////////////////////////////////////
masterPubKey, err := dkgInstance.GetMasterPubKey()
masterPubKey, err := dkgInstance.GetDistributedPublicKey()
if err != nil {
return fmt.Errorf("failed to get master pub key: %w", err)
}
masterPubKeyBz, err := json.Marshal(masterPubKey)
masterPubKeyBz, err := masterPubKey.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal master pub key: %w", err)
}
@ -345,15 +346,12 @@ func (am *AirgappedMachine) handleStateDkgMasterKeyAwaitConfirmations(o *client.
if err != nil {
return fmt.Errorf("failed to generate fsm request: %w", err)
}
///////////////////////////////////////////////////////////////////////////////
o.Result = reqBz
o.Event = dkg_proposal_fsm.EventDKGMasterKeyConfirmationReceived
return nil
}
// TODO @oopcode: reconstruct key and sign handlers
func (am *AirgappedMachine) HandleOperation(operation client.Operation) ([]client.Operation, error) {
var (
err error
@ -438,6 +436,57 @@ func (am *AirgappedMachine) HandleQR() ([]string, error) {
return qrPaths, nil
}
func (am *AirgappedMachine) handlePartialSign(msg []byte, dkgIdentifier string) ([]byte, error) {
dkgInstance, ok := am.dkgInstances[dkgIdentifier]
if !ok {
return nil, fmt.Errorf("dkg instance with identifier %s does not exist", dkgIdentifier)
}
distKeyShare, err := dkgInstance.GetDistKeyShare()
if err != nil {
return nil, err
}
return am.makePartialSign(msg, distKeyShare.PriShare())
}
func (am *AirgappedMachine) handleRecoverFullSign(msg []byte, sigShares [][]byte, dkgIdentifier string) ([]byte, error) {
dkgInstance, ok := am.dkgInstances[dkgIdentifier]
if !ok {
return nil, fmt.Errorf("dkg instance with identifier %s does not exist", dkgIdentifier)
}
masterKey, err := dkgInstance.GetMasterPubKey()
if err != nil {
return nil, err
}
return am.recoverFullSignature(msg, masterKey, sigShares, len(sigShares), len(sigShares))
}
func (am *AirgappedMachine) handleVerifySign(msg []byte, fullSignature []byte, dkgIdentifier string) error {
dkgInstance, ok := am.dkgInstances[dkgIdentifier]
if !ok {
return fmt.Errorf("dkg instance with identifier %s does not exist", dkgIdentifier)
}
masterKey, err := dkgInstance.GetMasterPubKey()
if err != nil {
return err
}
return bls.Verify(am.suite, masterKey.Commit(), msg, fullSignature)
}
func (am *AirgappedMachine) makePartialSign(msg []byte, share *share.PriShare) ([]byte, error) {
return tbls.Sign(am.suite, share, msg)
}
func (am *AirgappedMachine) recoverFullSignature(msg []byte, pubPoly *share.PubPoly,
sigShares [][]byte, t, n int) ([]byte, error) {
return tbls.Recover(am.suite, pubPoly, msg, sigShares, t, n)
}
func (am *AirgappedMachine) writeErrorRequestToOperation(o *client.Operation, handlerError error) error {
// each type of request should have a required event even error
// maybe should be global?

View File

@ -1,6 +1,7 @@
package airgapped
import (
"bytes"
"encoding/json"
"fmt"
"github.com/depools/dc4bc/client"
@ -14,6 +15,10 @@ import (
"time"
)
const (
DKGIdentifier = "dkg_identifier"
)
type Node struct {
ParticipantID int
Participant string
@ -21,7 +26,7 @@ type Node struct {
commits []requests.DKGProposalCommitConfirmationRequest
deals []requests.DKGProposalDealConfirmationRequest
responses []requests.DKGProposalResponseConfirmationRequest
masterKey []requests.DKGProposalMasterKeyConfirmationRequest
masterKeys []requests.DKGProposalMasterKeyConfirmationRequest
}
func (n *Node) storeOperation(t *testing.T, o client.Operation) {
@ -49,7 +54,7 @@ func (n *Node) storeOperation(t *testing.T, o client.Operation) {
if err := json.Unmarshal(o.Result, &req); err != nil {
t.Fatalf("failed to unmarshal fsm req: %v", err)
}
n.masterKey = append(n.masterKey, req)
n.masterKeys = append(n.masterKeys, req)
default:
t.Fatalf("invalid event: %s", o.Event)
}
@ -78,14 +83,14 @@ func createOperation(t *testing.T, opType string, to string, req interface{}) cl
Payload: reqBz,
Result: nil,
CreatedAt: time.Now(),
DKGIdentifier: "dkg_identifier",
DKGIdentifier: DKGIdentifier,
To: to,
}
return op
}
func TestAirgappedFullDKG(t *testing.T) {
nodesCount := 4
func TestAirgappedAllSteps(t *testing.T) {
nodesCount := 13
participants := make([]string, nodesCount)
for i := 0; i < nodesCount; i++ {
participants[i] = fmt.Sprintf("Participant#%d", i)
@ -219,9 +224,37 @@ func TestAirgappedFullDKG(t *testing.T) {
}
})
//TODO: check that master pub key is the same for every participant
// check that all master keys are equal
for _, n := range tr.nodes {
for i := 0; i < len(n.masterKeys); i++ {
if !bytes.Equal(n.masterKeys[0].MasterKey, n.masterKeys[i].MasterKey) {
t.Fatalf("master keys is not equal!")
}
}
}
t.Log("DKG succeeded")
msgToSign := []byte("i am a message")
sigShares := make([][]byte, 0)
for _, n := range tr.nodes {
sigShare, err := n.Machine.handlePartialSign(msgToSign, DKGIdentifier)
if err != nil {
t.Fatalf("failed to create sig share: %v", err.Error())
}
sigShares = append(sigShares, sigShare)
}
fullSign, err := tr.nodes[0].Machine.handleRecoverFullSign(msgToSign, sigShares, DKGIdentifier)
if err != nil {
t.Fatalf("failed to recover full sign: %v", err.Error())
}
for _, n := range tr.nodes {
if err = n.Machine.handleVerifySign(msgToSign, fullSign, DKGIdentifier); err != nil {
t.Fatalf("failed to verify signature: %v", err)
}
}
fmt.Println("DKG succeeded, signature recovered and verified")
}
func runStep(transport *Transport, cb func(n *Node, wg *sync.WaitGroup)) {

View File

@ -231,6 +231,18 @@ func (d *DKG) processDealCommits(verifier *vss.Verifier, deal *dkg.Deal) (bool,
return true, nil
}
func (d *DKG) GetDistKeyShare() (*dkg.DistKeyShare, error) {
return d.instance.DistKeyShare()
}
func (d *DKG) GetDistributedPublicKey() (kyber.Point, error) {
distKeyShare, err := d.instance.DistKeyShare()
if err != nil {
return nil, fmt.Errorf("failed to get distKeyShare")
}
return distKeyShare.Public(), nil
}
func (d *DKG) GetMasterPubKey() (*share.PubPoly, error) {
if d.instance == nil || !d.instance.Certified() {
return nil, fmt.Errorf("dkg instance is not ready")