Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
sr25519 signature verification
  • Loading branch information
kziemianek committed Apr 19, 2023
commit 57c27e3227de146990114a492580c2ccf79cefdd
2 changes: 2 additions & 0 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ define_error_codes! {
LoggingDisabled = 9,
/// ECDSA public key recovery failed. Most probably wrong recovery id or signature.
EcdsaRecoveryFailed = 11,
/// sr25519 signature verification failed.
Sr25519VerifyFailed = 12,
}

/// The raw return code returned by the host side.
Expand Down
34 changes: 34 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,40 @@ pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result<
})
}

/// Verifies a sr25519 signature
///
/// # Example
///
/// ```
/// let signature: [u8; 64] = [
/// 184, 49, 74, 238, 78, 165, 102, 252, 22, 92, 156, 176, 124, 118, 168, 116, 247,
/// 99, 0, 94, 2, 45, 9, 170, 73, 222, 182, 74, 60, 32, 75, 64, 98, 174, 69, 55, 83,
/// 85, 180, 98, 208, 75, 231, 57, 205, 62, 4, 105, 26, 136, 172, 17, 123, 99, 90, 255,
/// 228, 54, 115, 63, 30, 207, 205, 131,
/// ];
/// let message: &[u8; 11] = b"hello world";
/// let pub_key: [u8; 32] = [
/// 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44,
/// 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125,
/// ];
///
/// let result = ink::env::sr25519_verify(&signature, message.as_slice(), &pub_key);
/// assert!(result.is_ok())
/// ```
///
/// # Errors
///
/// - If sr25519 signature cannot be verified.
pub fn sr25519_verify(
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32]
) -> Result<()> {
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.sr25519_verify(signature, message, pub_key)
})
}

/// Checks whether the specified account is a contract.
///
/// # Errors
Expand Down
7 changes: 7 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ pub trait EnvBackend {
output: &mut [u8; 20],
) -> Result<()>;

fn sr25519_verify(
&mut self,
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32]
) -> Result<()>;

/// Low-level interface to call a chain extension method.
///
/// Returns the output of the chain extension of the specified type.
Expand Down
12 changes: 12 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl From<ext::Error> for crate::Error {
ext::Error::NotCallable => Self::NotCallable,
ext::Error::LoggingDisabled => Self::LoggingDisabled,
ext::Error::EcdsaRecoveryFailed => Self::EcdsaRecoveryFailed,
ext::Error::Sr25519VerifyFailed => Self::Sr25519VerifyFailed,
}
}
}
Expand Down Expand Up @@ -333,6 +334,17 @@ impl EnvBackend for EnvInstance {
Ok(())
}


fn sr25519_verify(
&mut self,
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32]
) -> Result<()> {
// todo: impl
Ok(())
}

fn call_chain_extension<I, T, E, ErrorCode, F, D>(
&mut self,
func_id: u32,
Expand Down
15 changes: 15 additions & 0 deletions crates/env/src/engine/on_chain/ext/riscv32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result
ret_code.into()
}

pub fn sr25519_verify(
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32]
) -> Result {
let ret_code = (
Ptr32::from_slice(signature),
Ptr32::from_slice(pub_key),
input.len() as u32,
Ptr32::from_slice(message),
)
.using_encoded(|in_data| sys::call(FUNC_ID, Ptr32::from_slice(in_data)));
ret_code.into()
}

pub fn is_contract(account_id: &[u8]) -> bool {
let ret_val = sys::call(FUNC_ID, Ptr32::from_slice(account_id));
ret_val.into_bool()
Expand Down
24 changes: 24 additions & 0 deletions crates/env/src/engine/on_chain/ext/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ mod sys {
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;

pub fn sr25519_verify(
signature_ptr: Ptr32<[u8]>,
public_key_ptr: Ptr32<[u8]>,
message_len: u32,
message_ptr: Ptr32<[u8]>,
) -> ReturnCode;

pub fn take_storage(
key_ptr: Ptr32<[u8]>,
key_len: u32,
Expand Down Expand Up @@ -597,6 +604,23 @@ pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result
ret_code.into()
}

pub fn sr25519_verify(
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32],
) -> Result {
let ret_code = unsafe {
sys::sr25519_verify(
Ptr32::from_slice(signature),
Ptr32::from_slice(pub_key),
message.len() as u32,
Ptr32::from_slice(message),

)
};
ret_code.into()
}

pub fn is_contract(account_id: &[u8]) -> bool {
let ret_val = unsafe { sys::is_contract(Ptr32::from_slice(account_id)) };
ret_val.into_bool()
Expand Down
9 changes: 9 additions & 0 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ impl EnvBackend for EnvInstance {
ext::ecdsa_to_eth_address(pubkey, output).map_err(Into::into)
}

fn sr25519_verify(
&mut self,
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32]
) -> Result<()> {
ext::sr25519_verify(signature, message, pub_key).map_err(Into::into)
}

fn call_chain_extension<I, T, E, ErrorCode, F, D>(
&mut self,
func_id: u32,
Expand Down
2 changes: 2 additions & 0 deletions crates/env/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub enum Error {
CallRuntimeFailed,
/// ECDSA pubkey recovery failed. Most probably wrong recovery id or signature.
EcdsaRecoveryFailed,
/// sr25519 signature verification failed.
Sr25519VerifyFailed
}

/// A result of environmental operations.
Expand Down
10 changes: 10 additions & 0 deletions crates/ink/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,16 @@ where
.map_err(|_| Error::EcdsaRecoveryFailed)
}

pub fn sr25519_verify(
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment here as well, please?=)

self,
signature: &[u8; 64],
message: &[u8],
pub_key: &[u8; 32],
) -> Result<()> {
ink_env::sr25519_verify(signature, message, pub_key)
.map_err(|_| Error::Sr25519VerifyFailed)
}

/// Checks whether a specified account belongs to a contract.
///
/// # Example
Expand Down