From cf0a3af1f8c56909d73ee3467aab3a2bc0841c31 Mon Sep 17 00:00:00 2001 From: Henri S Date: Tue, 25 Feb 2020 21:47:42 -0800 Subject: [PATCH 1/2] 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 dd21612df..1b326a5a5 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 { From 43bcae17b333d4114d89fae5f9c952ddf770f446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 28 Feb 2020 18:21:22 +0100 Subject: [PATCH 2/2] types: Simplify Ticket.Equals --- chain/types/blockheader.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/chain/types/blockheader.go b/chain/types/blockheader.go index 1b326a5a5..dd21612df 100644 --- a/chain/types/blockheader.go +++ b/chain/types/blockheader.go @@ -9,7 +9,6 @@ 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" @@ -215,7 +214,5 @@ func ElectionPostChallengeCount(sectors uint64, faults uint64) uint64 { } func (t *Ticket) Equals(ot *Ticket) bool { - tDigest := blake2b.Sum256(t.VRFProof) - otDigest := blake2b.Sum256(ot.VRFProof) - return bytes.Equal(tDigest[:], otDigest[:]) + return bytes.Equal(t.VRFProof, ot.VRFProof) }