Skip to content

Commit

Permalink
[Natives] Fix VDF dependency issue (0LNetworkCommunity#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Aug 19, 2023
1 parent 82b9e0b commit 781165c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
2 changes: 1 addition & 1 deletion third_party/move/move-stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
22 changes: 11 additions & 11 deletions third_party/move/move-stdlib/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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(),
},
},
}
}
}
Expand All @@ -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")]
{
Expand Down
26 changes: 16 additions & 10 deletions third_party/move/move-stdlib/src/natives/ol_vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// };
Expand All @@ -37,7 +37,7 @@ pub struct VerifyGasParameters {
pub base: InternalGas,
}

/// Rust implementation of Move's `native public fun verify(challenge: vector<u8>,
/// Rust implementation of Move's `native public fun verify(challenge: vector<u8>,
/// difficulty: u64, alleged_solution: vector<u8>): bool`
pub fn native_verify(
gas_params: &VerifyGasParameters,
Expand All @@ -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()
Expand All @@ -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::<bool>()?; // will do pietrezak if `false`.
let security = pop_arg!(arguments, Reference).read_ref()?.value_as::<u64>()?;
let difficulty = pop_arg!(arguments, Reference).read_ref()?.value_as::<u64>()?;
let solution = pop_arg!(arguments, Reference).read_ref()?.value_as::<Vec<u8>>()?;
Expand All @@ -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())];

Expand Down Expand Up @@ -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,
Expand All @@ -120,7 +126,7 @@ pub fn native_extract_address_from_challenge(
) -> PartialVMResult<NativeResult> {
let challenge_vec = pop_arg!(arguments, Reference).read_ref()?.value_as::<Vec<u8>>()?;

// 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];
Expand Down Expand Up @@ -160,9 +166,9 @@ pub struct GasParameters {
pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
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)
}
}

0 comments on commit 781165c

Please sign in to comment.