diff --git a/src/math/src/ed25519.cairo b/src/math/src/ed25519.cairo index 46dade6c..c2539bd1 100644 --- a/src/math/src/ed25519.cairo +++ b/src/math/src/ed25519.cairo @@ -291,13 +291,17 @@ fn check_group_equation( } fn verify_signature(msg: Span, signature: Span, pub_key: u256) -> bool { - let r: u256 = *signature.get(0).unwrap().unbox(); + if (signature.len() != 2) { + return false; + } + + let r: u256 = *signature[0]; let r_point: Option = r.try_into(); if (r_point.is_none()) { return false; } - let s: u256 = *signature.get(1).unwrap().unbox(); + let s: u256 = *signature[1]; let s_span: Span = s.into(); let reversed_s_span = s_span.reverse(); let s: u256 = reversed_s_span.span().into(); diff --git a/src/math/src/tests/ed25519_test.cairo b/src/math/src/tests/ed25519_test.cairo index 7b471b57..1a323753 100644 --- a/src/math/src/tests/ed25519_test.cairo +++ b/src/math/src/tests/ed25519_test.cairo @@ -1,4 +1,4 @@ -use alexandria_math::ed25519::{p, Point, verify_signature}; +use alexandria_math::ed25519::verify_signature; // Public keys and signatures were generated with JS library Noble (https://github.com/paulmillr/noble-ed25519) @@ -71,3 +71,17 @@ fn verify_signature_invalid() { assert!(!verify_signature(msg, signature.span(), pub_key), "Invalid signature"); } + +#[test] +#[available_gas(3200000000)] +fn verify_signature_invalid_2() { + let pub_key: u256 = 0x040369a47bcee3ae0cb373037ec0d2e36cae4a3762e388ff0682962aef49f444; + + let msg: Span = array![0x0].span(); + + let r_sign: u256 = 0xc71970448f7368c295d11cd64bb4fc7bb8899c830d9055832b6686b3f606b76d; + let s_sign: u256 = 0x68e015fa8775659d1f40a01e1f69b8af4409046f4dc8ff02cdb04fdc3585eb0d; + let signature = array![r_sign, s_sign]; + + assert!(!verify_signature(msg, signature.span(), pub_key), "Invalid signature"); +}