diff --git a/cgo/extern.go b/cgo/extern.go index d0f53d0f..2db31537 100644 --- a/cgo/extern.go +++ b/cgo/extern.go @@ -11,13 +11,12 @@ 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() { @@ -33,11 +32,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 @@ -46,8 +45,8 @@ 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() { @@ -63,11 +62,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 diff --git a/cgo/interface.go b/cgo/interface.go index 750a8fc2..d1217fd6 100644 --- a/cgo/interface.go +++ b/cgo/interface.go @@ -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" ) @@ -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) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 16a4f045..be677dd2 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -66,20 +66,11 @@ dependencies = [ "libc", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi 0.3.9", -] - [[package]] name = "anyhow" -version = "1.0.66" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" +checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" dependencies = [ "backtrace", ] @@ -104,9 +95,9 @@ checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" [[package]] name = "async-trait" -version = "0.1.59" +version = "0.1.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6e93155431f3931513b243d371981bb2770112b370c82745a1d19d2f99364" +checksum = "677d1d8ab452a3936018a687b20e6f7cf5363d713b732b8884001317b0e48aa3" dependencies = [ "proc-macro2", "quote", @@ -407,9 +398,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.77" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" +checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" [[package]] name = "cfg-if" @@ -802,9 +793,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.83" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdf07d07d6531bfcdbe9b8b739b104610c6508dcc4d63b410585faf338241daf" +checksum = "5add3fc1717409d029b20c5b6903fc0c0b02fa6741d820054f4a2efa5e5816fd" dependencies = [ "cc", "cxxbridge-flags", @@ -814,9 +805,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.83" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2eb5b96ecdc99f72657332953d4d9c50135af1bac34277801cc3937906ebd39" +checksum = "b4c87959ba14bc6fbc61df77c3fcfe180fc32b93538c4f1031dd802ccb5f2ff0" dependencies = [ "cc", "codespan-reporting", @@ -829,15 +820,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.83" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac040a39517fd1674e0f32177648334b0f4074625b5588a64519804ba0553b12" +checksum = "69a3e162fde4e594ed2b07d0f83c6c67b745e7f28ce58c6df5e6b6bef99dfb59" [[package]] name = "cxxbridge-macro" -version = "1.0.83" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1362b0ddcfc4eb0a1f57b68bd77dd99f0e826958a96abd0ae9bd092e114ffed6" +checksum = "3e7e2adeb6a0d4a282e581096b06e1791532b7d576dcde5ccd9382acf55db8e6" dependencies = [ "proc-macro2", "quote", @@ -1189,6 +1180,7 @@ name = "filcrypto" version = "0.7.5" dependencies = [ "anyhow", + "blake2b_simd", "bls-signatures", "blstrs", "byteorder", @@ -1198,10 +1190,10 @@ dependencies = [ "filepath", "fr32", "fvm 2.1.0", - "fvm 3.0.0-alpha.15", + "fvm 3.0.0-alpha.16", "fvm_ipld_blockstore", "fvm_ipld_encoding 0.2.3", - "fvm_ipld_encoding 0.3.0", + "fvm_ipld_encoding 0.3.2", "fvm_shared 2.0.0", "fvm_shared 3.0.0-alpha.15", "group", @@ -1304,18 +1296,17 @@ dependencies = [ [[package]] name = "flexi_logger" -version = "0.24.1" +version = "0.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99659bcfd52cfece972bd00acb9dba7028094d47e699ea8b193b9aaebd5c362b" +checksum = "ade8e86c48285f138a4d6ca15a2912e39bd6c74d62db42da4f1985f651a0b057" dependencies = [ - "ansi_term", "atty", "chrono", "glob", "lazy_static", "log", + "nu-ansi-term", "regex", - "rustversion", "thiserror", ] @@ -1422,9 +1413,9 @@ dependencies = [ [[package]] name = "fvm" -version = "3.0.0-alpha.15" +version = "3.0.0-alpha.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3bc434d9563f9a9c4555acc442fb2953ed51f301c216c9d357c8d6d52247a9c" +checksum = "ac9f5169c14ce1d132c786390077825d065d04002aace0fdebdab931f6cf28ed" dependencies = [ "anyhow", "blake2b_simd", @@ -1437,7 +1428,7 @@ dependencies = [ "fvm-wasm-instrument 0.4.0", "fvm_ipld_amt 0.5.0", "fvm_ipld_blockstore", - "fvm_ipld_encoding 0.3.0", + "fvm_ipld_encoding 0.3.2", "fvm_ipld_hamt 0.6.1", "fvm_shared 3.0.0-alpha.15", "lazy_static", @@ -1507,7 +1498,7 @@ dependencies = [ "anyhow", "cid", "fvm_ipld_blockstore", - "fvm_ipld_encoding 0.3.0", + "fvm_ipld_encoding 0.3.2", "itertools 0.10.5", "once_cell", "serde", @@ -1545,9 +1536,9 @@ dependencies = [ [[package]] name = "fvm_ipld_encoding" -version = "0.3.0" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf531c59e7c9a4c9044a34d1b44c9d544fab1eb0ba50aaa18121fe698d4fec35" +checksum = "189214585b1dc3c2a92682aa175cce1991c1a09542e5338c400d00c15b706142" dependencies = [ "anyhow", "cid", @@ -1592,7 +1583,7 @@ dependencies = [ "cid", "forest_hash_utils", "fvm_ipld_blockstore", - "fvm_ipld_encoding 0.3.0", + "fvm_ipld_encoding 0.3.2", "libipld-core 0.14.0", "multihash", "once_cell", @@ -1649,7 +1640,7 @@ dependencies = [ "data-encoding-macro", "filecoin-proofs-api", "fvm_ipld_blockstore", - "fvm_ipld_encoding 0.3.0", + "fvm_ipld_encoding 0.3.2", "lazy_static", "libsecp256k1", "log", @@ -1706,9 +1697,9 @@ dependencies = [ [[package]] name = "ghost" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb19fe8de3ea0920d282f7b77dd4227aea6b8b999b42cdf0ca41b2472b14443a" +checksum = "41973d4c45f7a35af8753ba3457cc99d406d863941fd7f52663cff54a5ab99b3" dependencies = [ "proc-macro2", "quote", @@ -1939,9 +1930,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" +checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" [[package]] name = "js-sys" @@ -2069,9 +2060,9 @@ dependencies = [ [[package]] name = "link-cplusplus" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" dependencies = [ "cc", ] @@ -2315,6 +2306,16 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi 0.3.9", +] + [[package]] name = "num" version = "0.1.42" @@ -2456,6 +2457,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "pairing" version = "0.22.0" @@ -2488,9 +2495,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1c2c742266c2f1041c914ba65355a83ae8747b05f208319784083583494b4b" +checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" [[package]] name = "pathdiff" @@ -2581,9 +2588,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro2" -version = "1.0.47" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" dependencies = [ "unicode-ident", ] @@ -2599,9 +2606,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.21" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" dependencies = [ "proc-macro2", ] @@ -2853,15 +2860,15 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8" +checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70" [[package]] name = "ryu" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" [[package]] name = "safer-ffi" @@ -2897,21 +2904,21 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "scratch" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" +checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" [[package]] name = "semver" -version = "1.0.14" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4" +checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a" [[package]] name = "serde" -version = "1.0.150" +version = "1.0.151" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e326c9ec8042f1b5da33252c8a37e9ffbd2c9bef0155215b6e6c80c790e05f91" +checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" dependencies = [ "serde_derive", ] @@ -2927,18 +2934,18 @@ dependencies = [ [[package]] name = "serde_bytes" -version = "0.11.7" +version = "0.11.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfc50e8183eeeb6178dcb167ae34a8051d63535023ae38b5d8d12beae193d37b" +checksum = "718dc5fff5b36f99093fc49b280cfc96ce6fc824317783bff5a1fed0c7a64819" dependencies = [ "serde", ] [[package]] name = "serde_derive" -version = "1.0.150" +version = "1.0.151" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42a3df25b0713732468deadad63ab9da1f1fd75a48a15024b50363f128db627e" +checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8" dependencies = [ "proc-macro2", "quote", @@ -2959,9 +2966,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.89" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" dependencies = [ "itoa", "ryu", @@ -2970,9 +2977,9 @@ dependencies = [ [[package]] name = "serde_repr" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fe39d9fbb0ebf5eb2c7cb7e2a47e4f462fad1379f1166b8ae49ad9eae89a7ca" +checksum = "9a5ec9fa74a20ebbe5d9ac23dac1fc96ba0ecfe9f50f2843b52e537b10fbcb4e" dependencies = [ "proc-macro2", "quote", @@ -3239,9 +3246,9 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "syn" -version = "1.0.105" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" dependencies = [ "proc-macro2", "quote", @@ -3306,18 +3313,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" +checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" +checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" dependencies = [ "proc-macro2", "quote", @@ -3371,9 +3378,9 @@ checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" [[package]] name = "unicode-ident" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" [[package]] name = "unicode-normalization" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 2d651462..ab45ba13 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -47,6 +47,7 @@ lazy_static = "1.4.0" serde = "1.0.117" serde_tuple = "0.5" safer-ffi = { version = "0.0.7", features = ["proc_macros"] } +blake2b_simd = "1.0.0" [dependencies.filecoin-proofs-api] package = "filecoin-proofs-api" diff --git a/rust/src/fvm/cgo/externs.rs b/rust/src/fvm/cgo/externs.rs index a384dbd6..beecab7f 100644 --- a/rust/src/fvm/cgo/externs.rs +++ b/rust/src/fvm/cgo/externs.rs @@ -28,19 +28,13 @@ extern "C" { pub fn cgo_extern_get_chain_randomness( handle: u64, - pers: i64, round: i64, - entropy: *const u8, - entropy_len: i32, randomness: *mut [u8; 32], ) -> i32; pub fn cgo_extern_get_beacon_randomness( handle: u64, - pers: i64, round: i64, - entropy: *const u8, - entropy_len: i32, randomness: *mut [u8; 32], ) -> i32; diff --git a/rust/src/fvm/externs.rs b/rust/src/fvm/externs.rs index 78bd33a2..a116ee68 100644 --- a/rust/src/fvm/externs.rs +++ b/rust/src/fvm/externs.rs @@ -1,5 +1,8 @@ use anyhow::{anyhow, Context}; +use byteorder::{BigEndian, WriteBytesExt}; +use std::io::Write; +use blake2b_simd::Params; use fvm2::externs::{Consensus as Consensus2, Externs as Externs2, Rand as Rand2}; use fvm3::externs::{Chain as Chain3, Consensus as Consensus3, Externs as Externs3, Rand as Rand3}; @@ -42,15 +45,8 @@ impl Rand3 for CgoExterns { ) -> anyhow::Result<[u8; 32]> { unsafe { let mut buf = [0u8; 32]; - match cgo_extern_get_chain_randomness( - self.handle, - pers as i64, - round, - entropy.as_ptr(), - entropy.len() as i32, - &mut buf, - ) { - 0 => Ok(buf), + match cgo_extern_get_chain_randomness(self.handle, round, &mut buf) { + 0 => draw_randomness(&buf, pers, round, entropy), r @ 1.. => panic!("invalid return value from has: {}", r), x if x == FvmError::InvalidHandle as i32 => { panic!("extern {} not registered", self.handle) @@ -71,15 +67,8 @@ impl Rand3 for CgoExterns { ) -> anyhow::Result<[u8; 32]> { unsafe { let mut buf = [0u8; 32]; - match cgo_extern_get_beacon_randomness( - self.handle, - pers as i64, - round, - entropy.as_ptr(), - entropy.len() as i32, - &mut buf, - ) { - 0 => Ok(buf), + match cgo_extern_get_beacon_randomness(self.handle, round, &mut buf) { + 0 => draw_randomness(&buf, pers, round, entropy), r @ 1.. => panic!("invalid return value from has: {}", r), x if x == FvmError::InvalidHandle as i32 => { panic!("extern {} not registered", self.handle) @@ -113,6 +102,22 @@ impl Rand2 for CgoExterns { } } +fn draw_randomness( + rbase: &[u8; 32], + pers: i64, + round: ChainEpoch, + entropy: &[u8], +) -> anyhow::Result<[u8; 32]> { + let mut state = Params::new().hash_length(32).to_state(); + state.write_i64::(pers)?; + state.write_all(rbase)?; + state.write_i64::(round)?; + state.write_all(entropy)?; + let mut ret = [0u8; 32]; + ret.clone_from_slice(state.finalize().as_bytes()); + Ok(ret) +} + impl Consensus3 for CgoExterns { fn verify_consensus_fault( &self,