github.com/annchain/OG@v0.0.9/consensus/dkg/util.go (about)

     1  package dkg
     2  
     3  import (
     4  	"github.com/annchain/OG/common/crypto"
     5  	"github.com/annchain/kyber/v3/pairing/bn256"
     6  	dkg "github.com/annchain/kyber/v3/share/dkg/pedersen"
     7  )
     8  
     9  func GeneratePeers(suite *bn256.Suite, n int) []PartSec {
    10  	signer := crypto.NewSigner(crypto.CryptoTypeSecp256k1)
    11  	var peerInfos []PartSec
    12  	for i := 0; i < n; i++ {
    13  		pubKey, privKey := signer.RandomKeyPair()
    14  		address := pubKey.Address()
    15  		// dkg kyber pub/priv key
    16  		dkgPrivKey, dkgPubKey := GenPartnerPair(suite)
    17  
    18  		peerInfos = append(peerInfos, PartSec{
    19  			PartPub: PartPub{
    20  				Point: dkgPubKey,
    21  				Peer: DkgPeer{
    22  					Id:             i,
    23  					PublicKey:      pubKey,
    24  					Address:        address,
    25  					PublicKeyBytes: nil,
    26  				},
    27  			},
    28  			Scalar:     dkgPrivKey,
    29  			PrivateKey: privKey,
    30  		})
    31  	}
    32  	return peerInfos
    33  }
    34  
    35  // SetupAllDkgers generate all Dkgers locally (for genesis or test purporse).
    36  func SetupAllDkgers(suite *bn256.Suite, numParts int, threshold int) (dkgers []*dkg.DistKeyGenerator, partSecs []PartSec, err error) {
    37  	// generate PeerInfos
    38  	partSecs = GeneratePeers(suite, numParts)
    39  	var partPubs PartPubs = make([]PartPub, numParts)
    40  
    41  	for i, peer := range partSecs {
    42  		partPubs[i] = peer.PartPub
    43  	}
    44  	dkgers = make([]*dkg.DistKeyGenerator, numParts)
    45  	for i := 0; i < numParts; i++ {
    46  		dkger, err := dkg.NewDistKeyGenerator(suite, partSecs[i].Scalar, partPubs.Points(), threshold)
    47  		if err != nil {
    48  			panic(err)
    49  		}
    50  		dkgers[i] = dkger
    51  	}
    52  	return
    53  
    54  	//var peerChans []chan DkgMessage
    55  	//
    56  	//// prepare incoming channels
    57  	//for i := 0; i < numParts; i++ {
    58  	//	peerChans = append(peerChans, make(chan DkgMessage, 5000))
    59  	//}
    60  
    61  	//var partners []*DefaultDkgPartner
    62  
    63  	//for i := 0; i < numParts; i++ {
    64  	//	communicator := NewDummyDkgPeerCommunicator(i, peerChans[i], peerChans)
    65  	//	communicator.Run()
    66  	//	partner, err := NewDefaultDkgPartner(suite, termId, numParts, threshold, partPubs, PartSecs[i],
    67  	//		communicator, communicator)
    68  	//	if err != nil {
    69  	//		panic(err)
    70  	//	}
    71  	//
    72  	//	partners = append(partners, partner)
    73  	//}
    74  	//return partners, PartSecs
    75  }