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 committed Feb 17, 2021
1 parent 563f7ee commit d4a1894
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 @@ -225,6 +225,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 @@ -334,6 +339,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 @@ -394,6 +401,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 @@ -455,6 +464,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 @@ -563,6 +574,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 @@ -625,6 +638,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 @@ -901,6 +901,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 @@ -172,6 +172,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 @@ -721,6 +721,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 d4a1894

Please sign in to comment.