github.com/annchain/OG@v0.0.9/consensus/vrf/vrf.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 vrf
    15  
    16  import (
    17  	"crypto/rand"
    18  	"fmt"
    19  	"github.com/annchain/OG/arefactor/og/types"
    20  	"github.com/annchain/OG/poc/vrf"
    21  	"github.com/sirupsen/logrus"
    22  )
    23  
    24  //go:generate msgp
    25  
    26  type Vrf struct {
    27  }
    28  
    29  func (as *Vrf) GenerateVrf() *VrfInfo {
    30  	sk, err := vrf.GenerateKey(rand.Reader)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	pk, _ := sk.Public()
    35  	//TODO: recover this. currently just comment out for compiler
    36  	//_, data := as.GetProofData(as.Idag.LatestSequencer().Height)
    37  	var data []byte
    38  
    39  	Vrf := sk.Compute(data)
    40  	ok := as.VrfCondition(Vrf)
    41  	if !ok {
    42  		return nil
    43  	}
    44  	VRFFromProof, proof := sk.Prove(data)
    45  	_ = VRFFromProof ///todo ???
    46  	var VrfInfo VrfInfo
    47  	VrfInfo.Vrf = Vrf
    48  	VrfInfo.PublicKey = pk
    49  	VrfInfo.Proof = proof
    50  	VrfInfo.Message = data
    51  	return &VrfInfo
    52  }
    53  
    54  //msgp:tuple VrfData
    55  type VrfData struct {
    56  	SeqHash types.Hash
    57  	Height  uint64
    58  	TxNum   int
    59  }
    60  
    61  //GetProofData get data
    62  //func (as *Vrf) GetProofData(height uint64) (*VrfData, []byte) {
    63  //	var sq *Sequencer
    64  //	if height == 0 {
    65  //		sq = as.Idag.LatestSequencer()
    66  //	} else {
    67  //		sq = as.Idag.GetSequencerByHeight(height)
    68  //	}
    69  //	if sq == nil {
    70  //		logrus.WithField("height ", height).Warn("we don't have this sequencer yet")
    71  //		return nil, nil
    72  //	}
    73  //	txs := as.Idag.GetTxisByHeight(sq.Height)
    74  //	vd := &VrfData{}
    75  //	vd.SeqHash = sq.Hash
    76  //	vd.Height = sq.Height
    77  //	vd.TxNum = len(txs)
    78  //	data, _ := vd.MarshalMsg(nil)
    79  //	return vd, data
    80  //}
    81  
    82  func (as *Vrf) VrfCondition(Vrf []byte) bool {
    83  	if len(Vrf) != vrf.Size {
    84  		logrus.WithField("len", len(Vrf)).Warn("vrf length error")
    85  		return false
    86  	}
    87  	//for test  return true , we need more node
    88  	//todo remove this later
    89  	return true
    90  	if Vrf[0] < 0x80 {
    91  		return false
    92  	}
    93  	return true
    94  
    95  }
    96  
    97  func (as *Vrf) VerifyVrfData(data []byte) error {
    98  	var vd VrfData
    99  	_, err := vd.UnmarshalMsg(data)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	//todo need more condition
   104  
   105  	//shouldVD, _ := as.GetProofData(vd.Height)
   106  	//
   107  	//if shouldVD == nil || *shouldVD != vd {
   108  	//	logrus.WithField("vrf data ", vd).WithField("want ", shouldVD).Debug("vrf data mismatch")
   109  	//	return fmt.Errorf("vfr data mismatch")
   110  	//}
   111  	return nil
   112  }
   113  
   114  func (as *Vrf) VrfVerify(Vrf []byte, pk []byte, data []byte, proof []byte) (err error) {
   115  	if !as.VrfCondition(Vrf) {
   116  		return fmt.Errorf("not your turn ; vrf condition mismatch")
   117  	}
   118  	if len(pk) != vrf.PublicKeySize {
   119  		return fmt.Errorf("publik ley size error %d", len(pk))
   120  	}
   121  	err = as.VerifyVrfData(data)
   122  	if err != nil {
   123  		return err
   124  	}
   125  	pubKey := vrf.PublicKey(pk)
   126  	if !pubKey.Verify(data, Vrf, proof) {
   127  		err = fmt.Errorf("vrf verifr error")
   128  		return err
   129  	}
   130  	return nil
   131  }