Skip to content

Commit

Permalink
Add wrapper type
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyulong committed Nov 15, 2024
1 parent 6257cac commit fdb2ad3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
14 changes: 13 additions & 1 deletion radix-common/src/crypto/secp256k1/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ use crate::internal_prelude::*;
#[cfg(feature = "fuzzing")]
use arbitrary::Arbitrary;

/// Represents an ECDSA Secp256k1 public key.
/// Represents an uncompressed ECDSA Secp256k1 public key.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Sbor)]
#[sbor(transparent)]
pub struct Secp256k1UncompressedPublicKey(
#[cfg_attr(feature = "serde", serde(with = "hex::serde"))] pub [u8; Self::LENGTH],
);

impl Secp256k1UncompressedPublicKey {
pub const LENGTH: usize = 65;
}

/// Represents a compressed ECDSA Secp256k1 public key, which is the default format used in the Radix stack.
#[cfg_attr(feature = "fuzzing", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Expand Down
4 changes: 2 additions & 2 deletions radix-common/src/crypto/signature_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn verify_and_recover_secp256k1(
pub fn verify_and_recover_secp256k1_uncompressed(
signed_hash: &Hash,
signature: &Secp256k1Signature,
) -> Option<[u8; secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]> {
) -> Option<Secp256k1UncompressedPublicKey> {
let recovery_id = signature.0[0];
let signature_data = &signature.0[1..];
if let Ok(id) = ::secp256k1::ecdsa::RecoveryId::from_i32(recovery_id.into()) {
Expand All @@ -37,7 +37,7 @@ pub fn verify_and_recover_secp256k1_uncompressed(

// The recover method also verifies the signature as part of the recovery process
if let Ok(pk) = SECP256K1_CTX.recover_ecdsa(&msg, &sig) {
return Some(pk.serialize_uncompressed());
return Some(Secp256k1UncompressedPublicKey(pk.serialize_uncompressed()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod component_module {
pub fn secp256k1_ecdsa_verify_and_key_recover_uncompressed(
hash: Hash,
signature: Secp256k1Signature,
) -> [u8; 65] {
) -> Secp256k1UncompressedPublicKey {
CryptoUtils::secp256k1_ecdsa_verify_and_key_recover_uncompressed(&hash, &signature)
}
}
Expand Down
5 changes: 2 additions & 3 deletions radix-engine-tests/tests/system/crypto_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ fn test_crypto_scrypto_key_recover_secp256k1_ecdsa() {
hash1_signature,
true
));
let pk_recovered2: Vec<u8> =
let pk_recovered2: [u8; 65] =
get_output!(crypto_scrypto_secp256k1_ecdsa_verify_and_key_recover(
&mut ledger,
package_address,
Expand All @@ -715,8 +715,7 @@ fn test_crypto_scrypto_key_recover_secp256k1_ecdsa() {
assert_eq!(
secp256k1::PublicKey::from_slice(pk.as_ref())
.unwrap()
.serialize_uncompressed()
.to_vec(),
.serialize_uncompressed(),
pk_recovered2
);

Expand Down
2 changes: 1 addition & 1 deletion radix-engine/src/vm/wasm_runtime/scrypto_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,6 @@ impl<'y, Y: SystemApi<RuntimeError>> WasmRuntime for ScryptoRuntime<'y, Y> {
let key = verify_and_recover_secp256k1_uncompressed(&hash, &signature)
.ok_or(WasmRuntimeError::Secp256k1KeyRecoveryError)?;

self.allocate_buffer(key.to_vec())
self.allocate_buffer(key.0.to_vec())
}
}
9 changes: 6 additions & 3 deletions scrypto/src/crypto_utils/crypto_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::engine::wasm_api::{copy_buffer, crypto_utils};
use radix_common::{
crypto::{Ed25519PublicKey, Ed25519Signature, Secp256k1PublicKey, Secp256k1Signature},
crypto::{
Ed25519PublicKey, Ed25519Signature, Secp256k1PublicKey, Secp256k1Signature,
Secp256k1UncompressedPublicKey,
},
prelude::{scrypto_decode, scrypto_encode, Bls12381G1PublicKey, Bls12381G2Signature, Hash},
};
use sbor::prelude::Vec;
Expand Down Expand Up @@ -160,7 +163,7 @@ impl CryptoUtils {
pub fn secp256k1_ecdsa_verify_and_key_recover_uncompressed(
message_hash: impl AsRef<Hash>,
signature: impl AsRef<Secp256k1Signature>,
) -> [u8; 65] {
) -> Secp256k1UncompressedPublicKey {
let key = copy_buffer(unsafe {
crypto_utils::crypto_utils_secp256k1_ecdsa_verify_and_key_recover_uncompressed(
message_hash.as_ref().0.as_ptr(),
Expand All @@ -169,6 +172,6 @@ impl CryptoUtils {
signature.as_ref().0.len(),
)
});
key.try_into().unwrap()
Secp256k1UncompressedPublicKey(key.try_into().unwrap())
}
}

0 comments on commit fdb2ad3

Please sign in to comment.