Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sha256 ABI #226

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rand = "0.8"
displaydoc = "0.2"
thiserror = "1.0"
chrono = { version = "0.4", features = ["clock"], default-features = false }
sha2 = "0.10.6"
# for gas_calibration middleware
regex = "1"
more-asserts = "0.3"
Expand Down
15 changes: 15 additions & 0 deletions src/as_execution/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,21 @@ where
Ok(buffer)
}

/// performs a sha256 hash on byte array and returns the hash as byte array
#[named]
pub(crate) fn assembly_script_hash_sha256(
mut ctx: FunctionEnvMut<ASEnv>,
bytes: i32,
) -> ABIResult<i32> {
let env = ctx.data().clone();
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?;
sydhds marked this conversation as resolved.
Show resolved Hide resolved
let memory = get_memory!(env);
let bytes = read_buffer(memory, &ctx, bytes)?;
sydhds marked this conversation as resolved.
Show resolved Hide resolved
let hash = env.get_interface().hash_sha256(&bytes)?.to_vec();
let ptr = pointer_from_bytearray(&env, &mut ctx, &hash)?.offset();
Ok(ptr as i32)
}

#[cfg(test)]
mod tests {
use crate::as_execution::abi::ser_bytearray_vec;
Expand Down
1 change: 1 addition & 0 deletions src/as_execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ 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
10 changes: 9 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{GasCosts, RuntimeModule};

use anyhow::{bail, Result};
use parking_lot::Mutex;

use sha2::{Digest, Sha256};
use std::sync::Arc;
use std::{collections::BTreeMap, str::FromStr};

Expand Down Expand Up @@ -230,6 +230,14 @@ impl Interface for TestInterface {
.try_into()
.unwrap())
}

// Sha256 hash data
fn hash_sha256(&self, bytes: &[u8]) -> Result<[u8; 32]> {
let mut hasher = Sha256::new();
hasher.update(bytes);
let hash = hasher.finalize().into();
Ok(hash)
}
}

#[cfg(feature = "gas_calibration")]
Expand Down
6 changes: 6 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl Default for GasCosts {
abi_costs.insert(String::from("assembly_script_date_now"), 11);
abi_costs.insert(String::from("assembly_script_console_log"), 36); // same cost as for generate_event
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: 10000,
Expand Down Expand Up @@ -381,6 +382,11 @@ pub trait Interface: Send + Sync + InterfaceClone {
) -> Result<()> {
unimplemented!("send_message")
}

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

impl dyn Interface {
Expand Down