Skip to content

Commit

Permalink
feat: Add Math API stubs for ECRecover. (#3921)
Browse files Browse the repository at this point in the history
  • Loading branch information
artob authored and joshuajbouw committed Mar 26, 2021
1 parent a7d264f commit 9501198
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ pub struct ExtCostsConfig {
/// Cost of getting blake2b per byte
pub blake2b_byte: Gas,

/// Cost of getting ecrecover base
pub ecrecover_base: Gas,
/// Cost of getting ecrecover per byte
pub ecrecover_byte: Gas,

/// Cost for calling logging.
pub log_base: Gas,
/// Cost for logging per byte
Expand Down Expand Up @@ -367,6 +372,8 @@ impl Default for ExtCostsConfig {
ripemd160_byte: SAFETY_MULTIPLIER * 8039117, // TODO
blake2b_base: SAFETY_MULTIPLIER * 1513656750, // TODO
blake2b_byte: SAFETY_MULTIPLIER * 8039117, // TODO
ecrecover_base: SAFETY_MULTIPLIER * 1000000000, // TODO
ecrecover_byte: SAFETY_MULTIPLIER * 1000000000, // TODO
log_base: SAFETY_MULTIPLIER * 1181104350,
log_byte: SAFETY_MULTIPLIER * 4399597,
storage_write_base: SAFETY_MULTIPLIER * 21398912000,
Expand Down Expand Up @@ -441,6 +448,8 @@ impl ExtCostsConfig {
ripemd160_byte: 0,
blake2b_base: 0,
blake2b_byte: 0,
ecrecover_base: 0,
ecrecover_byte: 0,
log_base: 0,
log_byte: 0,
storage_write_base: 0,
Expand Down Expand Up @@ -516,6 +525,8 @@ pub enum ExtCosts {
ripemd160_byte,
blake2b_base,
blake2b_byte,
ecrecover_base,
ecrecover_byte,
log_base,
log_byte,
storage_write_base,
Expand Down Expand Up @@ -638,6 +649,8 @@ impl ExtCosts {
ripemd160_byte => config.ripemd160_byte,
blake2b_base => config.blake2b_base,
blake2b_byte => config.blake2b_byte,
ecrecover_base => config.ecrecover_base,
ecrecover_byte => config.ecrecover_byte,
log_base => config.log_base,
log_byte => config.log_byte,
storage_write_base => config.storage_write_base,
Expand Down Expand Up @@ -714,6 +727,8 @@ impl ExtCosts {
"ripemd160_byte",
"blake2b_base",
"blake2b_byte",
"ecrecover_base",
"ecrecover_byte",
"log_base",
"log_byte",
"storage_write_base",
Expand Down
15 changes: 15 additions & 0 deletions runtime/near-vm-logic/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,21 @@ impl<'a> VMLogic<'a> {
self.internal_write_register(register_id, value_hash.as_slice().to_vec())
}

/// TODO
///
/// # Errors
///
/// TODO
///
/// # Cost
///
/// TODO
pub fn ecrecover(&mut self, hash_ptr: u64, v: u8, r_ptr: u64, s_ptr: u64, register_id: u64) -> Result<()> {
self.gas_counter.pay_base(ecrecover_base)?;

Ok(()) // TODO
}

/// Called by gas metering injected into Wasm. Counts both towards `burnt_gas` and `used_gas`.
///
/// # Errors
Expand Down
5 changes: 5 additions & 0 deletions runtime/near-vm-logic/tests/test_miscs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,11 @@ fn test_blake2b() {
});
}

#[test]
fn test_ecrecover() {
// TODO
}

#[test]
fn test_hash256_register() {
let mut logic_builder = VMLogicBuilder::default();
Expand Down
1 change: 1 addition & 0 deletions runtime/near-vm-runner/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ wrapped_imports! {
keccak512<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
ripemd160<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
blake2b<[value_len: u64, value_ptr: u64, register_id: u64] -> []>,
ecrecover<[hash_ptr: u64, v: u8, r_ptr: u64, s_ptr: u64, register_id: u64] -> []>,
// #####################
// # Miscellaneous API #
// #####################
Expand Down
2 changes: 2 additions & 0 deletions runtime/runtime-params-estimator/src/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ fn get_ext_costs_config(measurement: &Measurements, config: &Config) -> ExtCosts
ripemd160_byte: measured_to_gas(metric, &measured, ripemd160_byte),
blake2b_base: measured_to_gas(metric, &measured, blake2b_base),
blake2b_byte: measured_to_gas(metric, &measured, blake2b_byte),
ecrecover_base: measured_to_gas(metric, &measured, ecrecover_base),
ecrecover_byte: measured_to_gas(metric, &measured, ecrecover_byte),
log_base: measured_to_gas(metric, &measured, log_base),
log_byte: measured_to_gas(metric, &measured, log_byte),
storage_write_base: measured_to_gas(metric, &measured, storage_write_base),
Expand Down

0 comments on commit 9501198

Please sign in to comment.