Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse move-stdlib sha2 gas parameters in aptos-stdlib #7568

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions aptos-move/aptos-gas/src/aptos_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ crate::natives::define_gas_parameters_for_natives!(GasParameters, "aptos_framewo
[.algebra.ark_h2c_bls12381g1_xmd_sha256_sswu_per_msg_byte, { 8.. => "algebra.ark_h2c_bls12381g1_xmd_sha256_sswu_per_msg_byte" }, 48 * MUL],
[.algebra.ark_h2c_bls12381g2_xmd_sha256_sswu_base, { 8.. => "algebra.ark_h2c_bls12381g2_xmd_sha256_sswu_base" }, 6_773_002 * MUL],
[.algebra.ark_h2c_bls12381g2_xmd_sha256_sswu_per_msg_byte, { 8.. => "algebra.ark_h2c_bls12381g2_xmd_sha256_sswu_per_msg_byte" }, 48 * MUL],
[.algebra.sha2_v0_10_6_sha256_base, { 8.. => "algebra.sha2_v0_10_6_sha256_base" }, 51 * MUL],
[.algebra.sha2_v0_10_6_sha256_per_byte, { 8.. => "algebra.sha2_v0_10_6_sha256_per_byte" }, 3_577 * MUL],
// Algebra gas parameters end.

[.bls12381.base, "bls12381.base", 150 * MUL],
Expand Down
3 changes: 3 additions & 0 deletions aptos-move/aptos-vm/src/natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ pub fn aptos_natives(
timed_features: TimedFeatures,
features: Arc<Features>,
) -> NativeFunctionTable {
let sha256_gas_params: move_stdlib::natives::hash::Sha2_256GasParameters =
gas_params.move_stdlib.hash.sha2_256.clone();
move_stdlib::natives::all_natives(CORE_CODE_ADDRESS, gas_params.move_stdlib)
.into_iter()
.filter(|(_, name, _, _)| name.as_str() != "vector")
.chain(aptos_framework::natives::all_natives(
CORE_CODE_ADDRESS,
sha256_gas_params,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could've passed all the Move gas parameters here because it's likely that, down the road, we will need those other parameters too.

gas_params.aptos_framework,
timed_features,
features,
Expand Down
1 change: 1 addition & 0 deletions aptos-move/framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ move-package ={ workspace = true }
move-prover ={ workspace = true }
move-prover-boogie-backend ={ workspace = true }
move-stackless-bytecode ={ workspace = true }
move-stdlib = { workspace = true }
move-table-extension ={ workspace = true }
move-vm-runtime ={ workspace = true }
move-vm-types ={ workspace = true }
Expand Down
7 changes: 5 additions & 2 deletions aptos-move/framework/src/natives/cryptography/algebra/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub struct GasParameters {
pub ark_h2c_bls12381g1_xmd_sha256_sswu_per_msg_byte: InternalGasPerArg,
pub ark_h2c_bls12381g2_xmd_sha256_sswu_base: InternalGasPerArg,
pub ark_h2c_bls12381g2_xmd_sha256_sswu_per_msg_byte: InternalGasPerArg,
pub sha2_v0_10_6_sha256_base: InternalGasPerArg,
pub sha2_v0_10_6_sha256_per_byte: InternalGasPerArg,
}

pub struct HashToGasParameters {
pub algebra: GasParameters,
pub sha2: move_stdlib::natives::hash::Sha2_256GasParameters,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
abort_unless_feature_flag_enabled,
natives::{
cryptography::algebra::{
gas::GasParameters, AlgebraContext, HashToStructureSuite, Structure,
gas::HashToGasParameters, AlgebraContext, HashToStructureSuite, Structure,
MOVE_ABORT_CODE_NOT_IMPLEMENTED,
},
helpers::{SafeNativeContext, SafeNativeError, SafeNativeResult},
Expand All @@ -13,7 +13,9 @@ use crate::{
};
use aptos_types::on_chain_config::FeatureFlag;
use ark_ec::hashing::HashToCurve;
use move_core_types::gas_algebra::{InternalGas, InternalGasPerArg, NumArgs};
use move_core_types::gas_algebra::{
InternalGas, InternalGasPerArg, InternalGasPerByte, NumArgs, NumBytes,
};
use move_vm_types::{
loaded_data::runtime_types::Type,
values::{Value, VectorRef},
Expand Down Expand Up @@ -55,17 +57,16 @@ macro_rules! suite_from_ty_arg {
fn hash_to_bls12381gx_cost(
dst_len: usize,
msg_len: usize,
dst_shortening_base: InternalGasPerArg,
dst_shortening_per_byte: InternalGasPerArg,
dst_shortening_base: InternalGas,
dst_shortening_per_byte: InternalGasPerByte,
mapping_base: InternalGasPerArg,
mapping_per_byte: InternalGasPerArg,
) -> InternalGas {
// DST shortening as defined in https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-16.html#name-using-dsts-longer-than-255-.
let dst_shortening_cost = if dst_len <= 255 {
InternalGas::zero()
} else {
dst_shortening_base * NumArgs::one()
+ dst_shortening_per_byte * NumArgs::from((17 + dst_len) as u64)
dst_shortening_base + dst_shortening_per_byte * NumBytes::from((17 + dst_len) as u64)
};

// Mapping cost. The gas formula is simplified by assuming the DST length is fixed at 256.
Expand All @@ -76,7 +77,7 @@ fn hash_to_bls12381gx_cost(
}

pub fn hash_to_internal(
gas_params: &GasParameters,
gas_params: &HashToGasParameters,
context: &mut SafeNativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
Expand All @@ -99,10 +100,12 @@ pub fn hash_to_internal(
context.charge(hash_to_bls12381gx_cost(
dst.len(),
msg.len(),
gas_params.sha2_v0_10_6_sha256_base,
gas_params.sha2_v0_10_6_sha256_per_byte,
gas_params.ark_h2c_bls12381g1_xmd_sha256_sswu_base,
gas_params.ark_h2c_bls12381g1_xmd_sha256_sswu_per_msg_byte,
gas_params.sha2.base,
gas_params.sha2.per_byte,
gas_params.algebra.ark_h2c_bls12381g1_xmd_sha256_sswu_base,
gas_params
.algebra
.ark_h2c_bls12381g1_xmd_sha256_sswu_per_msg_byte,
))?;
let mapper = ark_ec::hashing::map_to_curve_hasher::MapToCurveBasedHasher::<
ark_ec::models::short_weierstrass::Projective<ark_bls12_381::g1::Config>,
Expand All @@ -121,10 +124,12 @@ pub fn hash_to_internal(
context.charge(hash_to_bls12381gx_cost(
dst.len(),
msg.len(),
gas_params.sha2_v0_10_6_sha256_base,
gas_params.sha2_v0_10_6_sha256_per_byte,
gas_params.ark_h2c_bls12381g2_xmd_sha256_sswu_base,
gas_params.ark_h2c_bls12381g2_xmd_sha256_sswu_per_msg_byte,
gas_params.sha2.base,
gas_params.sha2.per_byte,
gas_params.algebra.ark_h2c_bls12381g2_xmd_sha256_sswu_base,
gas_params
.algebra
.ark_h2c_bls12381g2_xmd_sha256_sswu_per_msg_byte,
))?;
let mapper = ark_ec::hashing::map_to_curve_hasher::MapToCurveBasedHasher::<
ark_ec::models::short_weierstrass::Projective<ark_bls12_381::g2::Config>,
Expand Down
10 changes: 7 additions & 3 deletions aptos-move/framework/src/natives/cryptography/algebra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::natives::{
casting::{downcast_internal, upcast_internal},
constants::{one_internal, order_internal, zero_internal},
eq::eq_internal,
gas::GasParameters,
gas::HashToGasParameters,
hash_to_structure::hash_to_internal,
new::from_u64_internal,
pairing::{multi_pairing_internal, pairing_internal},
Expand Down Expand Up @@ -250,7 +250,8 @@ static BLS12381_Q12_LENDIAN: Lazy<Vec<u8>> = Lazy::new(|| {
});

pub fn make_all(
gas_params: GasParameters,
gas_params: crate::natives::cryptography::algebra::gas::GasParameters,
sha256_gas_params: move_stdlib::natives::hash::Sha2_256GasParameters,
timed_features: TimedFeatures,
features: Arc<Features>,
) -> impl Iterator<Item = (String, NativeFunction)> {
Expand Down Expand Up @@ -414,7 +415,10 @@ pub fn make_all(
(
"hash_to_internal",
make_safe_native(
gas_params.clone(),
HashToGasParameters {
algebra: gas_params.clone(),
sha2: sha256_gas_params,
},
timed_features.clone(),
features.clone(),
hash_to_internal,
Expand Down
4 changes: 2 additions & 2 deletions aptos-move/framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ impl GasParameters {
ark_h2c_bls12381g1_xmd_sha256_sswu_per_msg_byte: 0.into(),
ark_h2c_bls12381g2_xmd_sha256_sswu_base: 0.into(),
ark_h2c_bls12381g2_xmd_sha256_sswu_per_msg_byte: 0.into(),
sha2_v0_10_6_sha256_base: 0.into(),
sha2_v0_10_6_sha256_per_byte: 0.into(),
},
ed25519: ed25519::GasParameters {
base: 0.into(),
Expand Down Expand Up @@ -266,6 +264,7 @@ impl GasParameters {

pub fn all_natives(
framework_addr: AccountAddress,
sha256_gas_params: move_stdlib::natives::hash::Sha2_256GasParameters,
gas_params: GasParameters,
timed_features: TimedFeatures,
features: Arc<Features>,
Expand Down Expand Up @@ -309,6 +308,7 @@ pub fn all_natives(
"algebra",
cryptography::algebra::make_all(
gas_params.algebra.clone(),
sha256_gas_params,
timed_features.clone(),
features.clone()
)
Expand Down
4 changes: 0 additions & 4 deletions crates/aptos-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ harness = false
name = "ristretto255"
harness = false

[[bench]]
name = "sha2_v0_10_6"
harness = false

[[bench]]
name = "secp256k1"
harness = false
Expand Down
34 changes: 0 additions & 34 deletions crates/aptos-crypto/benches/sha2_v0_10_6.rs

This file was deleted.

1 change: 0 additions & 1 deletion scripts/algebra-gas/update_algebra_gas_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def get_algebra_lines(gas_per_ns):
_,_,nanoseconds['ark_bls12_381_multi_pairing_per_pair'],nanoseconds['ark_bls12_381_multi_pairing_base'] = get_bench_ns_linear('target/criterion/ark_bls12_381/pairing_product')
_,_,nanoseconds['ark_h2c_bls12381g1_xmd_sha256_sswu_per_msg_byte'],nanoseconds['ark_h2c_bls12381g1_xmd_sha256_sswu_base'] = get_bench_ns_linear('target/criterion/ark_bls12_381/hash_to_g1_proj')
_,_,nanoseconds['ark_h2c_bls12381g2_xmd_sha256_sswu_per_msg_byte'],nanoseconds['ark_h2c_bls12381g2_xmd_sha256_sswu_base'] = get_bench_ns_linear('target/criterion/ark_bls12_381/hash_to_g2_proj')
_,_,nanoseconds['sha2_v0_10_6_sha256_base'],nanoseconds['sha2_v0_10_6_sha256_per_byte'] = get_bench_ns_linear('target/criterion/sha2_v0_10_6/sha256')
gas_units = {k:gas_per_ns*v for k,v in nanoseconds.items()}
lines = [f' [.algebra.{k}, {{ {TARGET_GAS_VERSION}.. => "algebra.{k}" }}, {prettify_number(v)} * MUL],' for k,v in sorted(gas_units.items())]
return lines
Expand Down