Skip to content

Commit

Permalink
fix: fix opchecksig bug (#223)
Browse files Browse the repository at this point in the history
<!-- enter the gh issue after hash -->

- [x] fixes #211 
- [x] fixes #212 
- [x] follows contribution
[guide](https://github.com/keep-starknet-strange/shinigami/blob/main/CONTRIBUTING.md)
- [ ] code change includes tests


![image](https://github.com/user-attachments/assets/f3bc58f5-5b1b-4f12-bebe-27eaed34313c)


![image](https://github.com/user-attachments/assets/3b787881-2986-4140-84df-78b79c737665)



<!-- PR description below -->

---------

Co-authored-by: Brandon Roberts <[email protected]>
  • Loading branch information
dlaciport and b-j-roberts authored Oct 4, 2024
1 parent 13f6cf4 commit 196aa5c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/engine/src/opcodes/crypto.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn opcode_checksig<
let pk_bytes = engine.dstack.pop_byte_array()?;
let full_sig_bytes = engine.dstack.pop_byte_array()?;

if full_sig_bytes.len() < 1 {
if full_sig_bytes.len() < 1 || pk_bytes.len() < 1 {
engine.dstack.push_bool(false);
return Result::Ok(());
}
Expand Down
23 changes: 15 additions & 8 deletions packages/engine/src/signature/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub fn check_pub_key_encoding<T, +Drop<T>>(
//
// @param pk_bytes The byte array representing the public key to be parsed.
// @return A `Secp256k1Point` representing the public key on the secp256k1 elliptic curve.
pub fn parse_pub_key(pk_bytes: @ByteArray) -> Secp256k1Point {
pub fn parse_pub_key(pk_bytes: @ByteArray) -> Result<Secp256k1Point, felt252> {
let mut pk_bytes_uncompressed = pk_bytes.clone();

if is_compressed_pub_key(pk_bytes) {
Expand All @@ -289,17 +289,24 @@ pub fn parse_pub_key(pk_bytes: @ByteArray) -> Secp256k1Point {
if pk_bytes[0] == 0x03 {
parity = true;
}
return Secp256Trait::<Secp256k1Point>::secp256_ec_get_point_from_x_syscall(pub_key, parity)
.unwrap_syscall()
.expect('Secp256k1Point: Invalid point.');
return Result::Ok(
Secp256Trait::<Secp256k1Point>::secp256_ec_get_point_from_x_syscall(pub_key, parity)
.unwrap_syscall()
.expect('Secp256k1Point: Invalid point.')
);
} else {
// Extract X coordinate and determine parity from last byte.
if pk_bytes_uncompressed.len() != 65 {
return Result::Err('Invalid public key length');
}
let pub_key: u256 = u256_from_byte_array_with_offset(@pk_bytes_uncompressed, 1, 32);
let parity = !(pk_bytes_uncompressed[64] & 1 == 0);

return Secp256Trait::<Secp256k1Point>::secp256_ec_get_point_from_x_syscall(pub_key, parity)
.unwrap_syscall()
.expect('Secp256k1Point: Invalid point.');
return Result::Ok(
Secp256Trait::<Secp256k1Point>::secp256_ec_get_point_from_x_syscall(pub_key, parity)
.unwrap_syscall()
.expect('Secp256k1Point: Invalid point.')
);
}
}

Expand Down Expand Up @@ -378,7 +385,7 @@ pub fn parse_base_sig_and_pk<T, +Drop<T>>(
check_signature_encoding(ref vm, sig_bytes)?;
check_pub_key_encoding(ref vm, pk_bytes)?;

let pub_key = parse_pub_key(pk_bytes);
let pub_key = parse_pub_key(pk_bytes)?;
let sig = parse_signature(sig_bytes)?;

Result::Ok((pub_key, sig, hash_type))
Expand Down

0 comments on commit 196aa5c

Please sign in to comment.