-
Notifications
You must be signed in to change notification settings - Fork 122
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
Add uncompressed public key recover #2003
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -910,6 +910,29 @@ fn secp256k1_ecdsa_verify_and_key_recover( | |
.map(|buffer| buffer.0) | ||
} | ||
|
||
fn secp256k1_ecdsa_verify_and_key_recover_uncompressed( | ||
mut caller: Caller<'_, HostState>, | ||
message_ptr: u32, | ||
message_len: u32, | ||
signature_ptr: u32, | ||
signature_len: u32, | ||
) -> Result<u64, InvokeError<WasmRuntimeError>> { | ||
let runtime = grab_runtime!(caller); | ||
let memory = grab_memory!(caller); | ||
|
||
let message = read_memory(caller.as_context_mut(), memory, message_ptr, message_len)?; | ||
let signature = read_memory( | ||
caller.as_context_mut(), | ||
memory, | ||
signature_ptr, | ||
signature_len, | ||
)?; | ||
|
||
runtime | ||
.crypto_utils_secp256k1_ecdsa_verify_and_key_recover_uncompressed(message, signature) | ||
.map(|buffer| buffer.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this method do? returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can not pass the array raw to wasm as a function return, and have to wrap it into a buffer (which will be read by wasm later). The wasmi linker requires a u32/u64 return type, so we had to unwrap the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the Engine side of the Engine <=> WASM boundary. It effectively returns a pointer to the buffer in WASM's memory space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the caller of this function reads out the 65 bytes how? via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrote simultaneously... thx makes sense |
||
} | ||
|
||
#[cfg(feature = "radix_engine_tests")] | ||
fn test_host_read_memory( | ||
mut caller: Caller<'_, HostState>, | ||
|
@@ -1623,6 +1646,24 @@ impl WasmiModule { | |
.map_err(|e| e.into()) | ||
}, | ||
); | ||
let host_secp2561k1_ecdsa_verify_and_key_recover_uncompressed = Func::wrap( | ||
store.as_context_mut(), | ||
|caller: Caller<'_, HostState>, | ||
message_ptr: u32, | ||
message_len: u32, | ||
signature_ptr: u32, | ||
signature_len: u32| | ||
-> Result<u64, Trap> { | ||
secp256k1_ecdsa_verify_and_key_recover_uncompressed( | ||
caller, | ||
message_ptr, | ||
message_len, | ||
signature_ptr, | ||
signature_len, | ||
) | ||
.map_err(|e| e.into()) | ||
}, | ||
); | ||
|
||
let mut linker = <Linker<HostState>>::new(); | ||
|
||
|
@@ -1826,6 +1867,11 @@ impl WasmiModule { | |
CRYPTO_UTILS_SECP256K1_ECDSA_VERIFY_AND_KEY_RECOVER_FUNCTION_NAME, | ||
host_secp2561k1_ecdsa_verify_and_key_recover | ||
); | ||
linker_define!( | ||
linker, | ||
CRYPTO_UTILS_SECP256K1_ECDSA_VERIFY_AND_KEY_RECOVER_UNCOMPRESSED_FUNCTION_NAME, | ||
host_secp2561k1_ecdsa_verify_and_key_recover_uncompressed | ||
); | ||
|
||
#[cfg(feature = "radix_engine_tests")] | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -794,4 +794,25 @@ impl<'y, Y: SystemApi<RuntimeError>> WasmRuntime for ScryptoRuntime<'y, Y> { | |
|
||
self.allocate_buffer(key.to_vec()) | ||
} | ||
|
||
/// This method is only available to packages uploaded after "Cuttlefish" | ||
/// protocol update due to checks in [`ScryptoV1WasmValidator::validate`]. | ||
#[trace_resources] | ||
fn crypto_utils_secp256k1_ecdsa_verify_and_key_recover_uncompressed( | ||
&mut self, | ||
message: Vec<u8>, | ||
signature: Vec<u8>, | ||
) -> Result<Buffer, InvokeError<WasmRuntimeError>> { | ||
let hash = Hash::try_from(message.as_slice()).map_err(WasmRuntimeError::InvalidHash)?; | ||
let signature = Secp256k1Signature::try_from(signature.as_ref()) | ||
.map_err(WasmRuntimeError::InvalidSecp256k1Signature)?; | ||
|
||
self.api | ||
.consume_cost_units(ClientCostingEntry::Secp256k1EcdsaKeyRecover)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this too. The uncompressed version should be cheaper, but didn't bother changing it for now. |
||
|
||
let key = verify_and_recover_secp256k1_uncompressed(&hash, &signature) | ||
.ok_or(WasmRuntimeError::Secp256k1KeyRecoveryError)?; | ||
|
||
self.allocate_buffer(key.to_vec()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth us adding an explicit new type for this, e.g.
ExpandedSecp256k1PublicKey
- which we can use here and in scrypto? (And can be a semantic transparent wrapper around apub [u8; 65]
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And maybe adding to the Rust doc for
Secp256k1PublicKey
that it's the compressed public key, which is the default format used in the Radix stack?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add the wrapper.