Skip to content

Commit

Permalink
🍌
Browse files Browse the repository at this point in the history
  • Loading branch information
assafmo committed Jun 7, 2022
1 parent 27efa9f commit 6501089
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 583 deletions.
1 change: 1 addition & 0 deletions cosmwasm/Cargo.lock

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

13 changes: 2 additions & 11 deletions cosmwasm/enclaves/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 cosmwasm/packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ serde = { version = "1.0.117", default-features = false, features = [
"alloc"
] }
thiserror = "1.0"
snafu = { version = "0.6.6" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cosmwasm-crypto = { path = "../crypto", version = "0.16.0" }
Expand Down
4 changes: 0 additions & 4 deletions cosmwasm/packages/std/src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ impl From<&Addr> for HumanAddr {
}
}

#[deprecated(
since = "0.14.0",
note = "HumanAddr is not much more than an alias to String and it does not provide significant safety advantages. With CosmWasm 0.14, we now use String when there was HumanAddr before. There is also the new Addr, which holds a validated immutable human readable address."
)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, JsonSchema, Default)]
pub struct HumanAddr(pub String);

Expand Down
128 changes: 11 additions & 117 deletions cosmwasm/packages/std/src/imports.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::vec::Vec;

use crate::addresses::{CanonicalAddr, HumanAddr};
use crate::encoding::Binary;
use crate::errors::{RecoverPubkeyError, SigningError, StdError, StdResult, VerificationError};
use crate::addresses::{Addr, CanonicalAddr};
use crate::binary::Binary;
use crate::errors::{
RecoverPubkeyError, SigningError, StdError, StdResult, SystemError, VerificationError,
};
#[cfg(feature = "iterator")]
use crate::iterator::{Order, KV, Pair};
use crate::memory::{alloc, build_region, consume_region, encode_sections, Region, get_optional_region_address};
use crate::iterator::{Order, Pair};
use crate::memory::{
alloc, build_region, consume_region, encode_sections, get_optional_region_address, Region,
};
use crate::results::SystemResult;
use crate::sections::decode_sections2;
use crate::serde::from_slice;
use crate::traits::{Api, Querier, QuerierResult, Storage};

Expand Down Expand Up @@ -219,118 +225,6 @@ impl Api for ExternalApi {
Ok(Addr::unchecked(address))
}

fn secp256k1_verify(
&self,
message_hash: &[u8],
signature: &[u8],
public_key: &[u8],
) -> Result<bool, VerificationError> {
let hash_send = build_region(message_hash);
let hash_send_ptr = &*hash_send as *const Region as u32;
let sig_send = build_region(signature);
let sig_send_ptr = &*sig_send as *const Region as u32;
let pubkey_send = build_region(public_key);
let pubkey_send_ptr = &*pubkey_send as *const Region as u32;

let result = unsafe { secp256k1_verify(hash_send_ptr, sig_send_ptr, pubkey_send_ptr) };
match result {
0 => Ok(true),
1 => Ok(false),
2 => panic!("MessageTooLong must not happen. This is a bug in the VM."),
3 => Err(VerificationError::InvalidHashFormat),
4 => Err(VerificationError::InvalidSignatureFormat),
5 => Err(VerificationError::InvalidPubkeyFormat),
10 => Err(VerificationError::GenericErr),
error_code => Err(VerificationError::unknown_err(error_code)),
}
}

fn secp256k1_recover_pubkey(
&self,
message_hash: &[u8],
signature: &[u8],
recover_param: u8,
) -> Result<Vec<u8>, RecoverPubkeyError> {
let hash_send = build_region(message_hash);
let hash_send_ptr = &*hash_send as *const Region as u32;
let sig_send = build_region(signature);
let sig_send_ptr = &*sig_send as *const Region as u32;

let result =
unsafe { secp256k1_recover_pubkey(hash_send_ptr, sig_send_ptr, recover_param.into()) };
let error_code = from_high_half(result);
let pubkey_ptr = from_low_half(result);
match error_code {
0 => {
let pubkey = unsafe { consume_region(pubkey_ptr as *mut Region) };
Ok(pubkey)
}
2 => panic!("MessageTooLong must not happen. This is a bug in the VM."),
3 => Err(RecoverPubkeyError::InvalidHashFormat),
4 => Err(RecoverPubkeyError::InvalidSignatureFormat),
6 => Err(RecoverPubkeyError::InvalidRecoveryParam),
error_code => Err(RecoverPubkeyError::unknown_err(error_code)),
}
}

fn ed25519_verify(
&self,
message: &[u8],
signature: &[u8],
public_key: &[u8],
) -> Result<bool, VerificationError> {
let msg_send = build_region(message);
let msg_send_ptr = &*msg_send as *const Region as u32;
let sig_send = build_region(signature);
let sig_send_ptr = &*sig_send as *const Region as u32;
let pubkey_send = build_region(public_key);
let pubkey_send_ptr = &*pubkey_send as *const Region as u32;

let result = unsafe { ed25519_verify(msg_send_ptr, sig_send_ptr, pubkey_send_ptr) };
match result {
0 => Ok(true),
1 => Ok(false),
2 => panic!("Error code 2 unused since CosmWasm 0.15. This is a bug in the VM."),
3 => panic!("InvalidHashFormat must not happen. This is a bug in the VM."),
4 => Err(VerificationError::InvalidSignatureFormat),
5 => Err(VerificationError::InvalidPubkeyFormat),
10 => Err(VerificationError::GenericErr),
error_code => Err(VerificationError::unknown_err(error_code)),
}
}

fn ed25519_batch_verify(
&self,
messages: &[&[u8]],
signatures: &[&[u8]],
public_keys: &[&[u8]],
) -> Result<bool, VerificationError> {
let msgs_encoded = encode_sections(messages);
let msgs_send = build_region(&msgs_encoded);
let msgs_send_ptr = &*msgs_send as *const Region as u32;

let sigs_encoded = encode_sections(signatures);
let sig_sends = build_region(&sigs_encoded);
let sigs_send_ptr = &*sig_sends as *const Region as u32;

let pubkeys_encoded = encode_sections(public_keys);
let pubkeys_send = build_region(&pubkeys_encoded);
let pubkeys_send_ptr = &*pubkeys_send as *const Region as u32;

let result =
unsafe { ed25519_batch_verify(msgs_send_ptr, sigs_send_ptr, pubkeys_send_ptr) };
match result {
0 => Ok(true),
1 => Ok(false),
2 => panic!("Error code 2 unused since CosmWasm 0.15. This is a bug in the VM."),
3 => panic!("InvalidHashFormat must not happen. This is a bug in the VM."),
4 => Err(VerificationError::InvalidSignatureFormat),
5 => Err(VerificationError::InvalidPubkeyFormat),
10 => Err(VerificationError::GenericErr),
error_code => Err(VerificationError::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
40 changes: 3 additions & 37 deletions cosmwasm/packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::addresses::{Addr, CanonicalAddr};
use crate::binary::Binary;
use crate::coins::Coin;
use crate::deps::OwnedDeps;
use crate::errors::{RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError};
use crate::errors::{
RecoverPubkeyError, SigningError, StdError, StdResult, SystemError, VerificationError,
};
#[cfg(feature = "stargate")]
use crate::ibc::{
IbcAcknowledgement, IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg,
Expand Down Expand Up @@ -193,42 +195,6 @@ impl Api for MockApi {
println!("{}", message);
}

fn secp256k1_verify(
&self,
_message_hash: &[u8],
_signature: &[u8],
_public_key: &[u8],
) -> Result<bool, VerificationError> {
Ok(true)
}

fn secp256k1_recover_pubkey(
&self,
_message_hash: &[u8],
_signature: &[u8],
_recovery_param: u8,
) -> Result<Vec<u8>, RecoverPubkeyError> {
Ok(vec![])
}

fn ed25519_verify(
&self,
_message: &[u8],
_signature: &[u8],
_public_key: &[u8],
) -> Result<bool, VerificationError> {
Ok(true)
}

fn ed25519_batch_verify(
&self,
_messages: &[&[u8]],
_signatures: &[&[u8]],
_public_keys: &[&[u8]],
) -> Result<bool, VerificationError> {
Ok(true)
}

fn secp256k1_sign(
&self,
_message: &[u8],
Expand Down
6 changes: 2 additions & 4 deletions cosmwasm/packages/std/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use serde::{de::DeserializeOwned, Serialize};
use std::ops::Deref;

use crate::addresses::{Addr, CanonicalAddr};
use crate::addresses::{Addr, CanonicalAddr, HumanAddr};
use crate::binary::Binary;
use crate::coins::Coin;
use crate::errors::{
RecoverPubkeyError, SigningError, StdError, StdResult, VerificationError,
};
use crate::errors::{RecoverPubkeyError, SigningError, StdError, StdResult, VerificationError};
#[cfg(feature = "iterator")]
use crate::iterator::{Order, Pair};
use crate::query::{
Expand Down
1 change: 1 addition & 0 deletions go-cosmwasm/Cargo.lock

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

14 changes: 6 additions & 8 deletions go-cosmwasm/api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ typedef struct AnalysisReport {
Buffer required_features;
} AnalysisReport;

typedef struct EnclaveRuntimeConfig {
uint8_t module_cache_size;
} EnclaveRuntimeConfig;

typedef struct cache_t {

} cache_t;

typedef struct EnclaveRuntimeConfig {
uint8_t module_cache_size;
} EnclaveRuntimeConfig;

/**
* An opaque type. `*gas_meter_t` represents a pointer to Go memory holding the gas meter.
*/
Expand Down Expand Up @@ -145,6 +145,8 @@ typedef struct GoQuerier {

Buffer allocate_rust(const uint8_t *ptr, uintptr_t length);

AnalysisReport analyze_code(cache_t *cache, ByteSliceView checksum, Buffer *error_msg);

void configure_enclave_runtime(EnclaveRuntimeConfig config, Buffer *err);

Buffer create(cache_t *cache, Buffer wasm, Buffer *err);
Expand Down Expand Up @@ -203,10 +205,6 @@ Buffer query(cache_t *cache,
uint64_t *gas_used,
Buffer *err);

AnalysisReport analyze_code(cache_t *cache,
ByteSliceView checksum,
Buffer *error_msg);

/**
* frees a cache reference
*
Expand Down
Loading

1 comment on commit 6501089

@assafmo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥝

Please sign in to comment.