From 781165c84f8563493abff3124cce691c09520ac2 Mon Sep 17 00:00:00 2001 From: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com> Date: Wed, 14 Jun 2023 10:59:49 -0400 Subject: [PATCH] [Natives] Fix VDF dependency issue (#4) --- Cargo.lock | 40 +++++++++++++++++++ .../bytecode/src/read_write_set_analysis.rs | 2 +- third_party/move/move-stdlib/Cargo.toml | 2 +- .../move/move-stdlib/src/natives/mod.rs | 22 +++++----- .../move/move-stdlib/src/natives/ol_vdf.rs | 26 +++++++----- 5 files changed, 69 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba66ecbede98c..5a4a886ada536 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5110,6 +5110,16 @@ dependencies = [ "cc", ] +[[package]] +name = "classgroup" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e828210e45744aa4ab0dbbabad0404de03f268728aeeda9a22cec8db836ea13" +dependencies = [ + "libc", + "num-traits 0.2.15", +] + [[package]] name = "cloud-storage" version = "0.11.1" @@ -6435,6 +6445,12 @@ dependencies = [ "rand 0.8.5", ] +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" + [[package]] name = "fallible_collections" version = "0.4.7" @@ -9392,6 +9408,7 @@ dependencies = [ "sha3 0.9.1", "smallvec", "tempfile", + "vdf", "walkdir", ] @@ -12255,6 +12272,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" +[[package]] +name = "sha2" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" +dependencies = [ + "block-buffer 0.7.3", + "digest 0.8.1", + "fake-simd", + "opaque-debug 0.2.3", +] + [[package]] name = "sha2" version = "0.9.9" @@ -13981,6 +14010,17 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "vdf" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273cdec52f00f63a97053f3ce44b806202cf3f54cd82209004b9d08fab97228" +dependencies = [ + "classgroup", + "num-traits 0.2.15", + "sha2 0.8.2", +] + [[package]] name = "vec_map" version = "0.8.2" diff --git a/third_party/move/move-prover/bytecode/src/read_write_set_analysis.rs b/third_party/move/move-prover/bytecode/src/read_write_set_analysis.rs index e146058def03b..7e28576dc38cf 100644 --- a/third_party/move/move-prover/bytecode/src/read_write_set_analysis.rs +++ b/third_party/move/move-prover/bytecode/src/read_write_set_analysis.rs @@ -693,7 +693,7 @@ fn call_native_function( ("hash", "sha3_256") | ("hash", "sha2_256") => (), ("Signature", "ed25519_validate_pubkey") | ("Signature", "ed25519_verify") => (), /////// 0L ///////// // todo v7 - // ("ol_vdf", "verify") | ("ol_vdf", "extract_address_from_challenge") => (), + ("ol_vdf", "verify") | ("ol_vdf", "extract_address_from_challenge") => (), (m, f) => { panic!("Unsupported native function {:?}::{:?}", m, f) }, diff --git a/third_party/move/move-stdlib/Cargo.toml b/third_party/move/move-stdlib/Cargo.toml index d518b6236faa5..90f5d6f15a9c9 100644 --- a/third_party/move/move-stdlib/Cargo.toml +++ b/third_party/move/move-stdlib/Cargo.toml @@ -29,7 +29,7 @@ smallvec = "1.6.1" walkdir = "2.3.1" ######### 0L ######## # todo v7 -# vdf = { git = "https://github.com/simsekgokhan/libra-v7", rev = "81c77dd" } +vdf = "0.1.0" # rust_decimal= { version = "1.10.3", default-features = true, features = ["maths"] } # tiny-keccak = { version = "2.0.2", features = ["keccak"] } # ethers = {version = "0.1.3"} diff --git a/third_party/move/move-stdlib/src/natives/mod.rs b/third_party/move/move-stdlib/src/natives/mod.rs index 4dfae6e0a7b9b..5d79c75f5c565 100644 --- a/third_party/move/move-stdlib/src/natives/mod.rs +++ b/third_party/move/move-stdlib/src/natives/mod.rs @@ -13,7 +13,7 @@ pub mod type_name; pub mod unit_test; pub mod vector; //////// 0L //////// -// pub mod ol_vdf; // todo v7 +pub mod ol_vdf; // todo v7 mod helpers; @@ -29,7 +29,7 @@ pub struct GasParameters { pub type_name: type_name::GasParameters, pub vector: vector::GasParameters, //////// 0L //////// // todo v7 - // pub ol_vdf: ol_vdf::GasParameters, + pub ol_vdf: ol_vdf::GasParameters, #[cfg(feature = "testing")] pub unit_test: unit_test::GasParameters, @@ -103,14 +103,14 @@ impl GasParameters { }, }, //////// 0L //////// // todo v7 - // ol_vdf: ol_vdf::GasParameters { - // verify: ol_vdf::VerifyGasParameters { - // base: 0.into(), - // }, - // extract_address_from_challenge: ol_vdf::ExtractAddressFromChallengeGasParameters { - // base: 0.into(), - // }, - // }, + ol_vdf: ol_vdf::GasParameters { + verify: ol_vdf::VerifyGasParameters { + base: 0.into(), + }, + extract_address_from_challenge: ol_vdf::ExtractAddressFromChallengeGasParameters { + base: 0.into(), + }, + }, } } } @@ -136,7 +136,7 @@ pub fn all_natives( add_natives!("type_name", type_name::make_all(gas_params.type_name)); add_natives!("vector", vector::make_all(gas_params.vector)); //////// 0L //////// // todo v7 - // add_natives!("ol_vdf", ol_vdf::make_all(gas_params.ol_vdf)); + add_natives!("ol_vdf", ol_vdf::make_all(gas_params.ol_vdf)); #[cfg(feature = "testing")] { diff --git a/third_party/move/move-stdlib/src/natives/ol_vdf.rs b/third_party/move/move-stdlib/src/natives/ol_vdf.rs index 2e2aee78d865c..cab2114d3dbbe 100644 --- a/third_party/move/move-stdlib/src/natives/ol_vdf.rs +++ b/third_party/move/move-stdlib/src/natives/ol_vdf.rs @@ -20,7 +20,7 @@ use std::{collections::VecDeque, sync::Arc}; use move_binary_format::errors::{PartialVMError, PartialVMResult}; use smallvec::smallvec; // use crate::natives::ol_counters::{ -// MOVE_VM_NATIVE_VERIFY_VDF_LATENCY, +// MOVE_VM_NATIVE_VERIFY_VDF_LATENCY, // MOVE_VM_NATIVE_VERIFY_VDF_PROOF_COUNT, // MOVE_VM_NATIVE_VERIFY_VDF_PROOF_ERROR_COUNT // }; @@ -37,7 +37,7 @@ pub struct VerifyGasParameters { pub base: InternalGas, } -/// Rust implementation of Move's `native public fun verify(challenge: vector, +/// Rust implementation of Move's `native public fun verify(challenge: vector, /// difficulty: u64, alleged_solution: vector): bool` pub fn native_verify( gas_params: &VerifyGasParameters, @@ -48,8 +48,8 @@ pub fn native_verify( // temporary logging. // let start_time = Instant::now(); // let metric_timer = MOVE_VM_NATIVE_VERIFY_VDF_LATENCY.start_timer(); // 0L todo - - if arguments.len() != 4 { + + if arguments.len() != 5 { let msg = format!( "wrong number of arguments for vdf_verify expected 4 found {}", arguments.len() @@ -60,6 +60,7 @@ pub fn native_verify( // MOVE_VM_NATIVE_VERIFY_VDF_PROOF_COUNT.inc(); // 0L todo // pop the arguments (reverse order). + let wesolowski = pop_arg!(arguments, Reference).read_ref()?.value_as::()?; // will do pietrezak if `false`. let security = pop_arg!(arguments, Reference).read_ref()?.value_as::()?; let difficulty = pop_arg!(arguments, Reference).read_ref()?.value_as::()?; let solution = pop_arg!(arguments, Reference).read_ref()?.value_as::>()?; @@ -75,8 +76,13 @@ pub fn native_verify( ); } - let v = vdf::PietrzakVDFParams(security as u16).new(); - let result = v.verify(&challenge, difficulty, &solution); + let result = if wesolowski { + let v = vdf::PietrzakVDFParams(security as u16).new(); + v.verify(&challenge, difficulty, &solution) + } else { + let v = vdf::WesolowskiVDFParams(security as u16).new(); + v.verify(&challenge, difficulty, &solution) + }; let return_values = smallvec![Value::bool(result.is_ok())]; @@ -110,7 +116,7 @@ pub struct ExtractAddressFromChallengeGasParameters { } // Extracts the first 32 bits of the vdf challenge which is the auth_key -// Auth Keys can be turned into an AccountAddress type, to be serialized to +// Auth Keys can be turned into an AccountAddress type, to be serialized to // a move address type. pub fn native_extract_address_from_challenge( gas_params: &ExtractAddressFromChallengeGasParameters, @@ -120,7 +126,7 @@ pub fn native_extract_address_from_challenge( ) -> PartialVMResult { let challenge_vec = pop_arg!(arguments, Reference).read_ref()?.value_as::>()?; - // We want to use Diem AuthenticationKey::derived_address() here but this creates + // We want to use Diem AuthenticationKey::derived_address() here but this creates // libra (and as a result cyclic) dependency which we definitely do not want const AUTHENTICATION_KEY_LENGTH: usize = 32; let auth_key_vec = &challenge_vec[..AUTHENTICATION_KEY_LENGTH]; @@ -160,9 +166,9 @@ pub struct GasParameters { pub fn make_all(gas_params: GasParameters) -> impl Iterator { let natives = [ ("verify", make_native_verify(gas_params.verify)), - ("extract_address_from_challenge", + ("extract_address_from_challenge", make_native_extract_address_from_challenge(gas_params.extract_address_from_challenge)), ]; make_module_natives(natives) -} \ No newline at end of file +}