Skip to content

Commit

Permalink
fix: remove the sha1-calculate API
Browse files Browse the repository at this point in the history
  • Loading branch information
loloicci committed May 29, 2024
1 parent a9bb800 commit c2594e3
Show file tree
Hide file tree
Showing 14 changed files with 10 additions and 406 deletions.
30 changes: 0 additions & 30 deletions packages/crypto/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use sha2::Sha256;

use cosmwasm_crypto::{
ed25519_batch_verify, ed25519_verify, secp256k1_recover_pubkey, secp256k1_verify,
sha1_calculate,
};
use std::cmp::min;

Expand Down Expand Up @@ -75,8 +74,6 @@ fn read_decode_cosmos_sigs() -> (Vec<Vec<u8>>, Vec<Vec<u8>>, Vec<Vec<u8>>) {
}

fn bench_crypto(c: &mut Criterion) {
// same as vm::imports::MAX_LENGTH_SHA1_MESSAGE (=80)
const MAX_LENGTH_SHA1_MESSAGE: usize = 80;
let mut group = c.benchmark_group("Crypto");

group.bench_function("secp256k1_verify", |b| {
Expand Down Expand Up @@ -110,33 +107,6 @@ fn bench_crypto(c: &mut Criterion) {
});
});

group.bench_function("sha1_calculate_one", |b| {
let inputs: Vec<&[u8]> = vec![&[0; MAX_LENGTH_SHA1_MESSAGE]];
b.iter(|| {
let hash = sha1_calculate(&inputs).unwrap();
assert_eq!(hash.len(), 20);
});
});
group.bench_function("sha1_calculate_two", |b| {
let inputs: Vec<&[u8]> = vec![&[0; MAX_LENGTH_SHA1_MESSAGE], &[1; MAX_LENGTH_SHA1_MESSAGE]];
b.iter(|| {
let hash = sha1_calculate(&inputs).unwrap();
assert_eq!(hash.len(), 20);
});
});
group.bench_function("sha1_calculate_four", |b| {
let inputs: Vec<&[u8]> = vec![
&[0; MAX_LENGTH_SHA1_MESSAGE],
&[1; MAX_LENGTH_SHA1_MESSAGE],
&[2; MAX_LENGTH_SHA1_MESSAGE],
&[3; MAX_LENGTH_SHA1_MESSAGE],
];
b.iter(|| {
let hash = sha1_calculate(&inputs).unwrap();
assert_eq!(hash.len(), 20);
});
});

group.bench_function("ed25519_verify", |b| {
let message = hex::decode(COSMOS_ED25519_MSG_HEX).unwrap();
let signature = hex::decode(COSMOS_ED25519_SIGNATURE_HEX).unwrap();
Expand Down
3 changes: 0 additions & 3 deletions packages/crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod ed25519;
mod errors;
mod identity_digest;
mod secp256k1;
mod sha1;

#[doc(hidden)]
pub use crate::ed25519::EDDSA_PUBKEY_LEN;
Expand All @@ -20,5 +19,3 @@ pub use crate::errors::{CryptoError, CryptoResult};
pub use crate::secp256k1::{secp256k1_recover_pubkey, secp256k1_verify};
#[doc(hidden)]
pub use crate::secp256k1::{ECDSA_PUBKEY_MAX_LEN, ECDSA_SIGNATURE_LEN, MESSAGE_HASH_MAX_LEN};
#[doc(hidden)]
pub use crate::sha1::sha1_calculate;
26 changes: 0 additions & 26 deletions packages/crypto/src/sha1.rs

This file was deleted.

86 changes: 0 additions & 86 deletions packages/std/src/errors/hash_calculation_error.rs

This file was deleted.

2 changes: 0 additions & 2 deletions packages/std/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod hash_calculation_error;
mod recover_pubkey_error;
mod std_error;
mod system_error;
mod verification_error;

pub use hash_calculation_error::HashCalculationError;
pub use recover_pubkey_error::RecoverPubkeyError;
pub use std_error::{
CheckedFromRatioError, CheckedMultiplyRatioError, ConversionOverflowError, DivideByZeroError,
Expand Down
38 changes: 1 addition & 37 deletions packages/std/src/errors/std_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::backtrace::Backtrace;
use std::fmt;
use thiserror::Error;

use crate::errors::{HashCalculationError, RecoverPubkeyError, VerificationError};
use crate::errors::{RecoverPubkeyError, VerificationError};

/// Structured error type for init, execute and query.
///
Expand Down Expand Up @@ -34,12 +34,6 @@ pub enum StdError {
#[cfg(feature = "backtraces")]
backtrace: Backtrace,
},
#[error("Hash Calculation error: {source}")]
HashCalculationError {
source: HashCalculationError,
#[cfg(feature = "backtraces")]
backtrace: Backtrace,
},
/// Whenever there is no specific error type available
#[error("Generic error: {msg}")]
GenericErr {
Expand Down Expand Up @@ -133,14 +127,6 @@ impl StdError {
}
}

pub fn hash_calculation_err(source: HashCalculationError) -> Self {
StdError::HashCalculationError {
source,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::capture(),
}
}

pub fn generic_err(msg: impl Into<String>) -> Self {
StdError::GenericErr {
msg: msg.into(),
Expand Down Expand Up @@ -261,22 +247,6 @@ impl PartialEq<StdError> for StdError {
false
}
}
StdError::HashCalculationError {
source,
#[cfg(feature = "backtraces")]
backtrace: _,
} => {
if let StdError::HashCalculationError {
source: rhs_source,
#[cfg(feature = "backtraces")]
backtrace: _,
} = rhs
{
source == rhs_source
} else {
false
}
}
StdError::GenericErr {
msg,
#[cfg(feature = "backtraces")]
Expand Down Expand Up @@ -487,12 +457,6 @@ impl From<RecoverPubkeyError> for StdError {
}
}

impl From<HashCalculationError> for StdError {
fn from(source: HashCalculationError) -> Self {
Self::hash_calculation_err(source)
}
}

impl From<OverflowError> for StdError {
fn from(source: OverflowError) -> Self {
Self::overflow(source)
Expand Down
23 changes: 0 additions & 23 deletions packages/std/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ extern "C" {
/// Returns 0 on verification success, 1 on verification failure, and values
/// greater than 1 in case of error.
fn ed25519_batch_verify(messages_ptr: u32, signatures_ptr: u32, public_keys_ptr: u32) -> u32;
fn sha1_calculate(inputs_ptr: u32) -> u64;

/// Writes a debug message (UFT-8 encoded) to the host for debugging purposes.
/// The host is free to log or process this in any way it considers appropriate.
Expand Down Expand Up @@ -360,28 +359,6 @@ impl Api for ExternalApi {
}
}

fn sha1_calculate(&self, inputs: &[&[u8]]) -> Result<[u8; 20], HashCalculationError> {
let inputs_encoded = encode_sections(inputs);
let inputs_send = build_region(&inputs_encoded);
let inputs_send_ptr = &*inputs_send as *const Region as u32;

let result = unsafe { sha1_calculate(inputs_send_ptr) };
let error_code = from_high_half(result);
let hash_ptr = from_low_half(result);
match error_code {
0 => {
let hash = unsafe { consume_region(hash_ptr as *mut Region) };
let hash_array: [u8; 20] = hash.try_into().unwrap_or_else(|v: Vec<u8>| {
panic!("Expected a Vec of length {} but it was {}", 20, v.len())
});
Ok(hash_array)
}
1 => Err(HashCalculationError::InputsTooLarger),
2 => panic!("Error code 2 unused since CosmWasm 0.15. This is a bug in the VM."),
error_code => Err(HashCalculationError::unknown_err(error_code)),
}
}

fn debug(&self, message: &str) {
// keep the boxes in scope, so we free it at the end (don't cast to pointers same line as build_region)
let region = build_region(message.as_bytes());
Expand Down
2 changes: 0 additions & 2 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ mod storage;
mod timestamp;
mod traits;
mod types;
mod uuid;

pub use crate::addresses::{Addr, CanonicalAddr};
pub use crate::binary::Binary;
Expand Down Expand Up @@ -78,7 +77,6 @@ pub use crate::storage::MemoryStorage;
pub use crate::timestamp::Timestamp;
pub use crate::traits::{Api, Querier, QuerierResult, QuerierWrapper, Storage};
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, TransactionInfo};
pub use crate::uuid::{new_uuid, Uuid};

// Exposed in wasm build only

Expand Down
6 changes: 1 addition & 5 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::binary::Binary;
use crate::coin::Coin;
use crate::deps::OwnedDeps;
use crate::errors::{
HashCalculationError, RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError,
RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError,
};
#[cfg(feature = "stargate")]
use crate::ibc::{
Expand Down Expand Up @@ -216,10 +216,6 @@ impl Api for MockApi {
)?)
}

fn sha1_calculate(&self, inputs: &[&[u8]]) -> Result<[u8; 20], HashCalculationError> {
Ok(cosmwasm_crypto::sha1_calculate(inputs)?)
}

fn debug(&self, message: &str) {
println!("{}", message);
}
Expand Down
4 changes: 1 addition & 3 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::addresses::{Addr, CanonicalAddr};
use crate::binary::Binary;
use crate::coin::Coin;
use crate::errors::{
HashCalculationError, RecoverPubkeyError, StdError, StdResult, VerificationError,
RecoverPubkeyError, StdError, StdResult, VerificationError,
};
#[cfg(feature = "iterator")]
use crate::iterator::{Order, Record};
Expand Down Expand Up @@ -138,8 +138,6 @@ pub trait Api {
public_keys: &[&[u8]],
) -> Result<bool, VerificationError>;

fn sha1_calculate(&self, inputs: &[&[u8]]) -> Result<[u8; 20], HashCalculationError>;

/// Emits a debugging message that is handled depending on the environment (typically printed to console or ignored).
/// Those messages are not persisted to chain.
fn debug(&self, message: &str);
Expand Down
Loading

0 comments on commit c2594e3

Please sign in to comment.