Skip to content

Commit

Permalink
use IndexView + check for length of signature + add invalid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edisontim committed Apr 5, 2024
1 parent ac98f8e commit c8d3de1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/math/src/ed25519.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,17 @@ fn check_group_equation(
}

fn verify_signature(msg: Span<u8>, signature: Span<u256>, 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<Point> = 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<u8> = s.into();
let reversed_s_span = s_span.reverse();
let s: u256 = reversed_s_span.span().into();
Expand Down
16 changes: 15 additions & 1 deletion src/math/src/tests/ed25519_test.cairo
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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<u8> = 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");
}

0 comments on commit c8d3de1

Please sign in to comment.