From e3404b78abbe610533fa7064acaff5fc862fef0a Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Fri, 24 May 2024 12:57:27 +0200 Subject: [PATCH] Update bls12_318 gas cost --- packages/vm/src/environment.rs | 34 +++++++++++++++++++--------------- packages/vm/src/imports.rs | 24 ++++++++++++++---------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/packages/vm/src/environment.rs b/packages/vm/src/environment.rs index 816f35cd69..2e174c6ca4 100644 --- a/packages/vm/src/environment.rs +++ b/packages/vm/src/environment.rs @@ -45,19 +45,16 @@ pub struct GasConfig { pub ed25519_batch_verify_cost: LinearGasCost, /// ed25519 batch signature verification cost (single public key) pub ed25519_batch_verify_one_pubkey_cost: LinearGasCost, - /// bls12-381 aggregate cost per point (g1) - pub bls12_381_aggregate_g1_per_point: u64, - /// bls12-381 aggregate cost per point (g2) - pub bls12_381_aggregate_g2_per_point: u64, + /// bls12-381 aggregate cost (g1) + pub bls12_381_aggregate_g1_cost: LinearGasCost, + /// bls12-381 aggregate cost (g2) + pub bls12_381_aggregate_g2_cost: LinearGasCost, /// bls12-381 hash to g1 cost pub bls12_381_hash_to_g1_cost: u64, /// bls12-381 hash to g2 cost pub bls12_381_hash_to_g2_cost: u64, /// bls12-381 pairing equality check cost - pub bls12_381_pairing_equality_cost: u64, - /// bls12-381 aggregated pairing equality check cost per point - /// (added on top of the base pairing equality check cost) - pub bls12_381_aggregated_pairing_equality_cost_per_pair: u64, + pub bls12_381_pairing_equality_cost: LinearGasCost, } impl Default for GasConfig { @@ -86,13 +83,20 @@ impl Default for GasConfig { per_item: 10 * GAS_PER_US, }, // just assume the production machines have more than 4 cores, so we can half that - bls12_381_aggregate_g1_per_point: 16 * GAS_PER_US / 2, - bls12_381_aggregate_g2_per_point: 33 * GAS_PER_US / 2, - bls12_381_hash_to_g1_cost: 324 * GAS_PER_US, - bls12_381_hash_to_g2_cost: 528 * GAS_PER_US, - // god i wish i was lying - bls12_381_pairing_equality_cost: 1038 * GAS_PER_US, - bls12_381_aggregated_pairing_equality_cost_per_pair: 108 * GAS_PER_US, + bls12_381_aggregate_g1_cost: LinearGasCost { + base: 136 * GAS_PER_US / 2, + per_item: 24 * GAS_PER_US / 2, + }, + bls12_381_aggregate_g2_cost: LinearGasCost { + base: 207 * GAS_PER_US / 2, + per_item: 49 * GAS_PER_US / 2, + }, + bls12_381_hash_to_g1_cost: 563 * GAS_PER_US, + bls12_381_hash_to_g2_cost: 871 * GAS_PER_US, + bls12_381_pairing_equality_cost: LinearGasCost { + base: 2281 * GAS_PER_US, + per_item: 163 * GAS_PER_US, + }, } } } diff --git a/packages/vm/src/imports.rs b/packages/vm/src/imports.rs index fc8307fbc7..506681f175 100644 --- a/packages/vm/src/imports.rs +++ b/packages/vm/src/imports.rs @@ -278,7 +278,9 @@ pub fn do_bls12_381_aggregate_g1< let estimated_point_count = (g1s.len() / BLS12_381_G1_POINT_LEN) as u64; let gas_info = GasInfo::with_cost( - data.gas_config.bls12_381_aggregate_g1_per_point * estimated_point_count, + data.gas_config + .bls12_381_aggregate_g1_cost + .total_cost(estimated_point_count), ); process_gas_info(data, &mut store, gas_info)?; @@ -322,7 +324,9 @@ pub fn do_bls12_381_aggregate_g2< let estimated_point_count = (g2s.len() / BLS12_381_G2_POINT_LEN) as u64; let gas_info = GasInfo::with_cost( - data.gas_config.bls12_381_aggregate_g2_per_point * estimated_point_count, + data.gas_config + .bls12_381_aggregate_g2_cost + .total_cost(estimated_point_count), ); process_gas_info(data, &mut store, gas_info)?; @@ -370,14 +374,14 @@ pub fn do_bls12_381_pairing_equality< let s = read_region(&memory, s_ptr, BLS12_381_G2_POINT_LEN)?; let estimated_point_count = (ps.len() / BLS12_381_G1_POINT_LEN) as u64; - let additional_cost = data - .gas_config - .bls12_381_aggregated_pairing_equality_cost_per_pair - // Add one since we do not include any pairs in the base benchmark, and we always need to add one for the `r` and `s` pair. - * (estimated_point_count + 1); - - let gas_info = - GasInfo::with_cost(data.gas_config.bls12_381_pairing_equality_cost + additional_cost); + + let gas_info = GasInfo::with_cost( + // Add one to the `estimated_point_count` since we do not include any pairs in the base + // benchmark, and we always need to add one for the `r` and `s` pair. + data.gas_config + .bls12_381_pairing_equality_cost + .total_cost(estimated_point_count + 1), + ); process_gas_info(data, &mut store, gas_info)?; let code = match bls12_381_pairing_equality(&ps, &qs, &r, &s) {