Skip to content

Commit

Permalink
Keccak256 and EVM verify
Browse files Browse the repository at this point in the history
  • Loading branch information
Eitu33 committed Jul 4, 2023
1 parent afed9fa commit bd67934
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/as_execution/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@ pub(crate) fn assembly_script_hash(mut ctx: FunctionEnvMut<ASEnv>, value: i32) -
Ok(ptr as i32)
}

/// performs a hash on a bytearray and returns the hash
#[named]
pub(crate) fn assembly_script_keccak256_hash(
mut ctx: FunctionEnvMut<ASEnv>,
value: i32,
) -> ABIResult<i32> {
let env = get_env(&ctx)?;
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?;
let memory = get_memory!(env);
let bytes = read_buffer(memory, &ctx, value)?;
let hash = env.get_interface().hash_keccak256(&bytes)?.to_vec();
let ptr = pointer_from_bytearray(&env, &mut ctx, &hash)?.offset();
Ok(ptr as i32)
}

/// Get keys (aka entries) in the datastore
#[named]
pub(crate) fn assembly_script_get_keys(
Expand Down Expand Up @@ -629,6 +644,26 @@ pub(crate) fn assembly_script_signature_verify(
.signature_verify(data.as_bytes(), &signature, &public_key)? as i32)
}

/// Verify an EVM signature.
/// Returns Ok(1) if correctly verified, Ok(0) otherwise.
#[named]
pub(crate) fn assembly_script_evm_signature_verify(
mut ctx: FunctionEnvMut<ASEnv>,
data: i32,
signature: i32,
public_key: i32,
) -> ABIResult<i32> {
let env = get_env(&ctx)?;
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?;
let memory = get_memory!(env);
let data = read_buffer(memory, &ctx, data)?;
let signature = read_buffer(memory, &ctx, signature)?;
let public_key = read_buffer(memory, &ctx, public_key)?;
Ok(env
.get_interface()
.verify_evm_signature(&data, &signature, &public_key)? as i32)
}

/// converts a public key to an address
#[named]
pub(crate) fn assembly_script_address_from_public_key(
Expand Down
4 changes: 3 additions & 1 deletion src/as_execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ impl ASContext {
"assembly_script_get_balance" => Function::new_typed_with_env(store, &fenv, assembly_script_get_balance),
"assembly_script_get_balance_for" => Function::new_typed_with_env(store, &fenv, assembly_script_get_balance_for),
"assembly_script_hash" => Function::new_typed_with_env(store, &fenv, assembly_script_hash),
"assembly_script_hash_sha256" => Function::new_typed_with_env(store, &fenv, assembly_script_hash_sha256),
"assembly_script_keccak256_hash" => Function::new_typed_with_env(store, &fenv, assembly_script_keccak256_hash),
"assembly_script_signature_verify" => Function::new_typed_with_env(store, &fenv, assembly_script_signature_verify),
"assembly_script_evm_signature_verify" => Function::new_typed_with_env(store, &fenv, assembly_script_evm_signature_verify),
"assembly_script_address_from_public_key" => Function::new_typed_with_env(store, &fenv, assembly_script_address_from_public_key),
"assembly_script_validate_address" => Function::new_typed_with_env(store, &fenv, assembly_script_validate_address),
"assembly_script_unsafe_random" => Function::new_typed_with_env(store, &fenv, assembly_script_unsafe_random),
Expand All @@ -265,7 +268,6 @@ impl ASContext {
"assembly_script_local_execution" => Function::new_typed_with_env(store, &fenv, assembly_script_local_execution),
"assembly_script_caller_has_write_access" => Function::new_typed_with_env(store, &fenv, assembly_script_caller_has_write_access),
"assembly_script_function_exists" => Function::new_typed_with_env(store, &fenv, assembly_script_function_exists),
"assembly_script_hash_sha256" => Function::new_typed_with_env(store, &fenv, assembly_script_hash_sha256),
},
};

Expand Down
19 changes: 18 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ impl Default for GasCosts {
abi_costs.insert(String::from("assembly_script_has_data_for"), 115);
abi_costs.insert(String::from("assembly_script_has_op_key"), 78);
abi_costs.insert(String::from("assembly_script_hash"), 83);
abi_costs.insert(String::from("assembly_script_hash_sha256"), 83);
abi_costs.insert(String::from("assembly_script_keccak256_hash"), 83);
abi_costs.insert(String::from("assembly_script_print"), 35);
abi_costs.insert(String::from("assembly_script_send_message"), 316);
abi_costs.insert(String::from("assembly_script_set_bytecode"), 74);
abi_costs.insert(String::from("assembly_script_set_bytecode_for"), 129);
abi_costs.insert(String::from("assembly_script_set_data"), 158);
abi_costs.insert(String::from("assembly_script_set_data_for"), 165);
abi_costs.insert(String::from("assembly_script_signature_verify"), 98);
abi_costs.insert(String::from("assembly_script_evm_signature_verify"), 264);
abi_costs.insert(String::from("assembly_script_transfer_coins"), 62);
abi_costs.insert(String::from("assembly_script_transfer_coins_for"), 102);
abi_costs.insert(String::from("assembly_script_unsafe_random"), 11);
Expand All @@ -127,7 +130,6 @@ impl Default for GasCosts {
abi_costs.insert(String::from("assembly_script_console_warn"), 36);
abi_costs.insert(String::from("assembly_script_console_error"), 36);
abi_costs.insert(String::from("assembly_script_trace"), 36);
abi_costs.insert(String::from("assembly_script_hash_sha256"), 83);
Self {
operator_cost: 1,
launch_cost: 10_000,
Expand Down Expand Up @@ -307,6 +309,16 @@ pub trait Interface: Send + Sync + InterfaceClone {
unimplemented!("signature_verify")
}

// Verify EVM signature
fn verify_evm_signature(
&self,
message: &[u8],
signature: &[u8],
public_key: &[u8],
) -> Result<bool> {
unimplemented!("verify_evm_signature")
}

// Convert a public key to an address
fn address_from_public_key(&self, public_key: &str) -> Result<String> {
unimplemented!("address_from_public_key")
Expand Down Expand Up @@ -404,6 +416,11 @@ pub trait Interface: Send + Sync + InterfaceClone {
fn hash_sha256(&self, bytes: &[u8]) -> Result<[u8; 32]> {
unimplemented!("hash_sha256")
}

// Keccak256 hash bytes
fn hash_keccak256(&self, bytes: &[u8]) -> Result<[u8; 32]> {
unimplemented!("hash_keccak256")
}
}

impl dyn Interface {
Expand Down

0 comments on commit bd67934

Please sign in to comment.