Skip to content

Commit 92df8f7

Browse files
committed
Add supported pubkey formats early check
1 parent 933f671 commit 92df8f7

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/vm/src/imports.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ const LENGTH_SHA256_HASH: usize = 32;
4040
/// Max length of a serialized signature
4141
const MAX_LENGTH_SIGNATURE: usize = 64;
4242

43-
/// Max length of a serialized public key
44-
const MAX_LENGTH_PUBKEY: usize = 33;
43+
/// Max length of a compressed serialized public key
44+
const MAX_LENGTH_COMPRESSED_PUBKEY: usize = 33;
45+
/// Max length of an uncompressed serialized public key
46+
const MAX_LENGTH_UNCOMPRESSED_PUBKEY: usize = 65;
4547

4648
/// Max length for a debug message
4749
const MAX_LENGTH_DEBUG: usize = 2 * MI;
@@ -265,7 +267,12 @@ fn do_secp256k1_verify<A: Api, S: Storage, Q: Querier>(
265267

266268
let signature = read_region(&env.memory(), signature_ptr, MAX_LENGTH_SIGNATURE)?;
267269

268-
let pubkey = read_region(&env.memory(), pubkey_ptr, MAX_LENGTH_PUBKEY)?;
270+
let pubkey_prefix = read_region(&env.memory(), pubkey_ptr, 1)?;
271+
let pubkey = match pubkey_prefix[0] {
272+
0x02 | 0x03 => read_region(&env.memory(), pubkey_ptr, MAX_LENGTH_COMPRESSED_PUBKEY)?,
273+
0x04 => read_region(&env.memory(), pubkey_ptr, MAX_LENGTH_UNCOMPRESSED_PUBKEY)?,
274+
p => return Err(VmError::crypto_err(format!("unsupported pubkey prefix: {}", p))),
275+
};
269276

270277
let result = secp256k1_verify(&hash, &signature, &pubkey);
271278
let gas_info = GasInfo::with_cost(GAS_COST_VERIFY_SECP256K1_SIGNATURE);

0 commit comments

Comments
 (0)