Skip to content

Commit

Permalink
Update bls12_318 gas cost
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed May 24, 2024
1 parent 501bcc6 commit e3404b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
34 changes: 19 additions & 15 deletions packages/vm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
},
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions packages/vm/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e3404b7

Please sign in to comment.