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

feat: use optimal sha256 for different envs #16

Merged
merged 2 commits into from
Jul 8, 2024
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
1 change: 1 addition & 0 deletions contract/Cargo.lock

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

5 changes: 5 additions & 0 deletions merkle-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ edition = "2021"

[dependencies]
hex = "0.4.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
sha2 = "0.10.8"

[target.'cfg(target_arch = "wasm32")'.dependencies]
near-sdk = "5.1.0"
3 changes: 3 additions & 0 deletions merkle-tools/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "1.78.0"
components = ["clippy", "rustfmt"]
34 changes: 24 additions & 10 deletions merkle-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use hex::{decode, encode};
use sha2::{Digest, Sha256};

pub fn merkle_proof_calculator(tx_hashes: Vec<String>, transaction_position: usize) -> Vec<String> {
let mut transaction_position = transaction_position;
Expand Down Expand Up @@ -53,6 +52,29 @@ pub fn compute_root_from_merkle_proof(
current_hash
}

#[inline]
fn double_sha256(input: &[u8]) -> Vec<u8> {
#[cfg(target_arch = "wasm32")]
{
use near_sdk::env::sha256;

sha256(&sha256(input))
}

#[cfg(not(target_arch = "wasm32"))]
{
use sha2::{Digest, Sha256};

let mut hasher = Sha256::new();
hasher.update(input);
let first_hash_inputs = hasher.finalize();

let mut hasher = Sha256::new();
hasher.update(&first_hash_inputs);
hasher.finalize().to_vec()
}
}

fn compute_hash(first_tx_hash: &str, second_tx_hash: &str) -> String {
// Reverse inputs before and after hashing
// due to big-endian
Expand All @@ -65,16 +87,8 @@ fn compute_hash(first_tx_hash: &str, second_tx_hash: &str) -> String {
concat_inputs.extend(unhex_reverse_first);
concat_inputs.extend(unhex_reverse_second);

let mut hasher = Sha256::new();
hasher.update(&concat_inputs);
let first_hash_inputs = hasher.finalize();

let mut hasher = Sha256::new();
hasher.update(&first_hash_inputs);
let final_hash_inputs = hasher.finalize();

// Reverse final hash and hex result
let mut final_hash_bytes = final_hash_inputs.to_vec();
let mut final_hash_bytes = double_sha256(&concat_inputs);
final_hash_bytes.reverse();
encode(final_hash_bytes)
}
Expand Down
134 changes: 125 additions & 9 deletions relayer/Cargo.lock

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

Loading