From 96555aa74b6a209b59adad29742306333703ff52 Mon Sep 17 00:00:00 2001 From: Henri S Date: Tue, 25 Feb 2020 21:47:42 -0800 Subject: [PATCH] update to use VRF digest appropriately and simplify drawing rand past genesis --- chain/store/store.go | 23 +++++------------------ chain/types/blockheader.go | 5 ++++- chain/types/tipset.go | 5 ++++- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/chain/store/store.go b/chain/store/store.go index ff40b6da1..130d9c7c3 100644 --- a/chain/store/store.go +++ b/chain/store/store.go @@ -890,12 +890,12 @@ func (cs *ChainStore) TryFillTipSet(ts *types.TipSet) (*FullTipSet, error) { } func drawRandomness(t *types.Ticket, pers crypto.DomainSeparationTag, round int64, entropy []byte) ([]byte, error) { - // TODO: Make this spec compliant h := blake2b.New256() if err := binary.Write(h, binary.BigEndian, int64(pers)); err != nil { return nil, xerrors.Errorf("deriving randomness: %w", err) } - h.Write(t.VRFProof) + VRFDigest := blake2b.Sum256(t.VRFProof) + h.Write(VRFDigest[:]) if err := binary.Write(h, binary.BigEndian, round); err != nil { return nil, xerrors.Errorf("deriving randomness: %w", err) } @@ -921,25 +921,12 @@ func (cs *ChainStore) GetRandomness(ctx context.Context, blks []cid.Cid, pers cr mtb := nts.MinTicketBlock() - if int64(nts.Height()) <= round { + // if at (or just past -- for null epochs) appropriate epoch + // or at genesis (works for negative epochs) + if int64(nts.Height()) <= round || mtb.Height == 0 { return drawRandomness(nts.MinTicketBlock().Ticket, pers, round, entropy) } - // special case for lookback behind genesis block - // TODO(spec): this is not in the spec, need to sync that - if mtb.Height == 0 { - - // round is negative - thash, err := drawRandomness(mtb.Ticket, pers, round*-1, entropy) - if err != nil { - return nil, err - } - - // for negative lookbacks, just use the hash of the positive tickethash value - h := blake2b.Sum256(thash) - return h[:], nil - } - blks = mtb.Parents } } diff --git a/chain/types/blockheader.go b/chain/types/blockheader.go index 7d579a261..2494a9bf5 100644 --- a/chain/types/blockheader.go +++ b/chain/types/blockheader.go @@ -9,6 +9,7 @@ import ( block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" + "github.com/minio/blake2b-simd" "github.com/minio/sha256-simd" "github.com/multiformats/go-multihash" xerrors "golang.org/x/xerrors" @@ -214,5 +215,7 @@ func ElectionPostChallengeCount(sectors uint64, faults uint64) uint64 { } func (t *Ticket) Equals(ot *Ticket) bool { - return bytes.Equal(t.VRFProof, ot.VRFProof) + tDigest := blake2b.Sum256(t.VRFProof) + otDigest := blake2b.Sum256(ot.VRFProof) + return bytes.Equal(tDigest[:], otDigest[:]) } diff --git a/chain/types/tipset.go b/chain/types/tipset.go index a92b27fcf..86e82032c 100644 --- a/chain/types/tipset.go +++ b/chain/types/tipset.go @@ -10,6 +10,7 @@ import ( "github.com/filecoin-project/specs-actors/actors/abi" "github.com/ipfs/go-cid" logging "github.com/ipfs/go-log/v2" + "github.com/minio/blake2b-simd" cbg "github.com/whyrusleeping/cbor-gen" "golang.org/x/xerrors" ) @@ -170,7 +171,9 @@ func (ts *TipSet) Equals(ots *TipSet) bool { } func (t *Ticket) Less(o *Ticket) bool { - return bytes.Compare(t.VRFProof, o.VRFProof) < 0 + tDigest := blake2b.Sum256(t.VRFProof) + oDigest := blake2b.Sum256(o.VRFProof) + return bytes.Compare(tDigest[:], oDigest[:]) < 0 } func (ts *TipSet) MinTicket() *Ticket {