Skip to content

Commit

Permalink
Merge pull request #412 from filecoin-project/asr/randomness
Browse files Browse the repository at this point in the history
FVM: Simplify Randomness externs, perform hashing on the Rust side
  • Loading branch information
arajasek authored Aug 21, 2023
2 parents ba86847 + caab733 commit 8c79a5f
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 224 deletions.
15 changes: 6 additions & 9 deletions cgo/extern.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import (
"github.com/filecoin-project/go-address"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
)

//export cgo_extern_get_chain_randomness
func cgo_extern_get_chain_randomness(
handle C.uint64_t, pers C.int64_t, round C.int64_t,
entropy C.buf_t, entropyLen C.int32_t,
handle C.uint64_t, round C.int64_t,
output C.buf_t,
) (res C.int32_t) {
defer func() {
Expand All @@ -33,11 +31,11 @@ func cgo_extern_get_chain_randomness(
return ErrInvalidHandle
}

rand, err := externs.GetChainRandomness(ctx, crypto.DomainSeparationTag(pers), abi.ChainEpoch(round), C.GoBytes(unsafe.Pointer(entropy), entropyLen))
rand, err := externs.GetChainRandomness(ctx, abi.ChainEpoch(round))

switch err {
case nil:
copy(out[:], rand)
copy(out[:], rand[:])
return 0
default:
return ErrIO
Expand All @@ -46,8 +44,7 @@ func cgo_extern_get_chain_randomness(

//export cgo_extern_get_beacon_randomness
func cgo_extern_get_beacon_randomness(
handle C.uint64_t, pers C.int64_t, round C.int64_t,
entropy C.buf_t, entropyLen C.int32_t,
handle C.uint64_t, round C.int64_t,
output C.buf_t,
) (res C.int32_t) {
defer func() {
Expand All @@ -63,11 +60,11 @@ func cgo_extern_get_beacon_randomness(
return ErrInvalidHandle
}

rand, err := externs.GetBeaconRandomness(ctx, crypto.DomainSeparationTag(pers), abi.ChainEpoch(round), C.GoBytes(unsafe.Pointer(entropy), entropyLen))
rand, err := externs.GetBeaconRandomness(ctx, abi.ChainEpoch(round))

switch err {
case nil:
copy(out[:], rand)
copy(out[:], rand[:])
return 0
default:
return ErrIO
Expand Down
5 changes: 2 additions & 3 deletions cgo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
)
Expand All @@ -29,8 +28,8 @@ const (
)

type Externs interface {
GetChainRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, epoch abi.ChainEpoch, entropy []byte) ([]byte, error)
GetBeaconRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, epoch abi.ChainEpoch, entropy []byte) ([]byte, error)
GetChainRandomness(ctx context.Context, epoch abi.ChainEpoch) ([32]byte, error)
GetBeaconRandomness(ctx context.Context, epoch abi.ChainEpoch) ([32]byte, error)
VerifyConsensusFault(ctx context.Context, h1, h2, extra []byte) (*ConsensusFault, int64)
TipsetCid(ctx context.Context, epoch abi.ChainEpoch) (cid.Cid, error)

Expand Down
Loading

0 comments on commit 8c79a5f

Please sign in to comment.