github.com/annchain/OG@v0.0.9/poc/dkg/dkg.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package dkg 15 16 import ( 17 "errors" 18 "fmt" 19 "github.com/annchain/kyber/v3" 20 "github.com/annchain/kyber/v3/pairing/bn256" 21 "github.com/annchain/kyber/v3/share" 22 "github.com/annchain/kyber/v3/share/dkg/pedersen" 23 "github.com/annchain/kyber/v3/sign/bls" 24 "github.com/annchain/kyber/v3/sign/tbls" 25 ) 26 27 type Partner struct { 28 ID int 29 PartPubs []kyber.Point 30 MyPartSec kyber.Scalar 31 SecretKeyContribution []kyber.Scalar 32 Suite *bn256.Suite 33 Dkger *dkg.DistKeyGenerator 34 Resps []*dkg.Response 35 Threshold int 36 NbParticipants int 37 SigShares [][]byte 38 } 39 40 func (p *Partner) GenerateDKGer() *dkg.DistKeyGenerator { 41 // use all partPubs and my partSec to generate a dkg 42 dkger, err := dkg.NewDistKeyGenerator(p.Suite, p.MyPartSec, p.PartPubs, p.Threshold) 43 if err != nil { 44 panic(err) 45 } 46 p.Dkger = dkger 47 return dkger 48 } 49 50 func (p *Partner) VerifyByPubPoly(msg []byte, sig []byte) (err error) { 51 dks, err := p.Dkger.DistKeyShare() 52 if err != nil { 53 return 54 } 55 pubPoly := share.NewPubPoly(p.Suite, p.Suite.Point().Base(), dks.Commitments()) 56 if pubPoly.Commit() != dks.Public() { 57 err = errors.New("PubPoly not aligned to dksPublic") 58 return 59 } 60 61 err = bls.Verify(p.Suite, pubPoly.Commit(), msg, sig) 62 fmt.Printf("ID [%d] pubPolyCommit [%s] dksPublic [%s] dksCommitments [%s]\n", 63 p.ID, pubPoly.Commit(), dks.Public(), dks.Commitments()) 64 return 65 } 66 67 func (p *Partner) VerifyByDksPublic(msg []byte, sig []byte) (err error) { 68 dks, err := p.Dkger.DistKeyShare() 69 if err != nil { 70 return 71 } 72 err = bls.Verify(p.Suite, dks.Public(), msg, sig) 73 return 74 } 75 76 func (p *Partner) RecoverSig(msg []byte) (jointSig []byte, err error) { 77 dks, err := p.Dkger.DistKeyShare() 78 pubPoly := share.NewPubPoly(p.Suite, p.Suite.Point().Base(), dks.Commitments()) 79 jointSig, err = tbls.Recover(p.Suite, pubPoly, msg, p.SigShares, p.Threshold, p.NbParticipants) 80 return 81 } 82 83 func (p *Partner) RecoverPub() (jointPubKey kyber.Point, err error) { 84 dks, err := p.Dkger.DistKeyShare() 85 if err != nil { 86 return 87 } 88 pubPoly := share.NewPubPoly(p.Suite, p.Suite.Point().Base(), dks.Commitments()) 89 jointPubKey = pubPoly.Commit() 90 return 91 } 92 93 func (p *Partner) Sig(msg []byte) (partSig []byte, err error) { 94 dks, err := p.Dkger.DistKeyShare() 95 if err != nil { 96 return 97 } 98 partSig, err = tbls.Sign(p.Suite, dks.PriShare(), msg) 99 return 100 }