Skip to content

Commit

Permalink
use existing syscall to generate point and minor refactor to verify_sig
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielElp committed Oct 19, 2023
1 parent 6304ca7 commit e1ad258
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 64 deletions.
17 changes: 6 additions & 11 deletions corelib/src/starknet/secp256_trait.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ trait Secp256Trait<Secp256Point> {
fn secp256_ec_get_point_from_x_syscall(
x: u256, y_parity: bool
) -> SyscallResult<Option<Secp256Point>>;
fn secp256_ec_get_point_from_xy_syscall(
x: u256, y: u256
) -> SyscallResult<Option<Secp256Point>>;
}

trait Secp256PointTrait<Secp256Point> {
Expand All @@ -54,19 +51,17 @@ fn is_signature_entry_valid<
value != 0_u256 && value < Secp256Impl::get_curve_size()
}

fn verify_signature<
fn is_valid_signature<
Secp256Point,
+Drop<Secp256Point>,
impl Secp256Impl: Secp256Trait<Secp256Point>,
+Secp256PointTrait<Secp256Point>
>(
msg_hash: u256, r: u256, s: u256, public_key: Secp256Point
) -> Result<bool, felt252> {
if !is_signature_entry_valid::<Secp256Point>(r) {
return Result::Err('Signature out of range');
}
if !is_signature_entry_valid::<Secp256Point>(s) {
return Result::Err('Signature out of range');
) -> bool {
if !is_signature_entry_valid::<Secp256Point>(r)
|| !is_signature_entry_valid::<Secp256Point>(s) {
return false;
}

let n_nz = Secp256Impl::get_curve_size().try_into().unwrap();
Expand All @@ -80,7 +75,7 @@ fn verify_signature<
let sum = point1.add(point2).unwrap_syscall();

let (x, y) = sum.get_coordinates().unwrap_syscall();
return Result::Ok(x == r);
x == r
}

/// Receives a signature and the signed message hash.
Expand Down
11 changes: 0 additions & 11 deletions corelib/src/starknet/secp256k1.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ impl Secp256k1Impl of Secp256Trait<Secp256k1Point> {
) -> SyscallResult<Option<Secp256k1Point>> {
secp256k1_get_point_from_x_syscall(x, y_parity)
}
fn secp256_ec_get_point_from_xy_syscall(
x: u256, y: u256
) -> SyscallResult<Option<Secp256k1Point>> {
let point = secp256k1_get_point_from_x_syscall(x, false).unwrap_syscall().unwrap();
let (_, point_y) = point.get_coordinates().unwrap_syscall();
if (point_y == y) {
return SyscallResult::Ok(Option::Some(point));
} else {
return secp256k1_get_point_from_x_syscall(x, true);
}
}
}

impl Secp256k1PointImpl of Secp256PointTrait<Secp256k1Point> {
Expand Down
11 changes: 0 additions & 11 deletions corelib/src/starknet/secp256r1.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ impl Secp256r1Impl of Secp256Trait<Secp256r1Point> {
) -> SyscallResult<Option<Secp256r1Point>> {
secp256r1_get_point_from_x_syscall(x, y_parity)
}
fn secp256_ec_get_point_from_xy_syscall(
x: u256, y: u256
) -> SyscallResult<Option<Secp256r1Point>> {
let point = secp256r1_get_point_from_x_syscall(x, false).unwrap_syscall().unwrap();
let (_, point_y) = point.get_coordinates().unwrap_syscall();
if (point_y == y) {
return SyscallResult::Ok(Option::Some(point));
} else {
return secp256r1_get_point_from_x_syscall(x, true);
}
}
}

impl Secp256r1PointImpl of Secp256PointTrait<Secp256r1Point> {
Expand Down
20 changes: 9 additions & 11 deletions corelib/src/test/secp256k1_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use starknet::{
eth_address::U256IntoEthAddress, EthAddress, secp256k1::Secp256k1Impl, SyscallResultTrait
};
use starknet::secp256_trait::{
Signature, recover_public_key, Secp256PointTrait, signature_from_vrs, verify_signature
Signature, recover_public_key, Secp256PointTrait, signature_from_vrs, is_valid_signature
};
use starknet::secp256k1::{Secp256k1Point, Secp256k1PointImpl, verify_eth_signature};

Expand Down Expand Up @@ -112,14 +112,13 @@ fn test_verify_eth_signature_overflowing_signature_s() {
fn test_verify_signature() {
let (msg_hash, signature, public_key_x, public_key_y, _) = get_message_and_signature(false);

let public_key = Secp256k1Impl::secp256_ec_get_point_from_xy_syscall(public_key_x, public_key_y)
let public_key = Secp256k1Impl::secp256_ec_new_syscall(public_key_x, public_key_y)
.unwrap_syscall()
.unwrap();

let is_valid = verify_signature::<Secp256k1Point>(
msg_hash, signature.r, signature.s, public_key
)
.unwrap();
let is_valid = is_valid_signature::<
Secp256k1Point
>(msg_hash, signature.r, signature.s, public_key);
assert(is_valid, 'Signature should be valid');
}

Expand All @@ -128,13 +127,12 @@ fn test_verify_signature() {
fn test_verify_signature_invalid_signature() {
let (msg_hash, signature, public_key_x, public_key_y, _) = get_message_and_signature(false);

let public_key = Secp256k1Impl::secp256_ec_get_point_from_xy_syscall(public_key_x, public_key_y)
let public_key = Secp256k1Impl::secp256_ec_new_syscall(public_key_x, public_key_y)
.unwrap_syscall()
.unwrap();

let is_valid = verify_signature::<Secp256k1Point>(
msg_hash, signature.r + 1, signature.s, public_key
)
.unwrap();
let is_valid = is_valid_signature::<
Secp256k1Point
>(msg_hash, signature.r + 1, signature.s, public_key);
assert(!is_valid, 'Signature should be invalid');
}
32 changes: 12 additions & 20 deletions corelib/src/test/secp256r1_test.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet::{secp256r1::Secp256r1Impl, SyscallResultTrait};
use starknet::secp256_trait::{recover_public_key, Secp256PointTrait, Signature, verify_signature};
use starknet::secp256_trait::{recover_public_key, Secp256PointTrait, Signature, is_valid_signature};
use starknet::secp256r1::{Secp256r1Point, Secp256r1PointImpl};
use test::test_utils::assert_eq;

Expand Down Expand Up @@ -28,7 +28,7 @@ fn get_message_and_signature() -> (u256, Signature, u256, u256, Secp256r1Point)
0x0087d9315798aaa3a5ba01775787ced05eaaf7b4e09fc81d6d1aa546e8365d525d
);

let public_key = Secp256r1Impl::secp256_ec_get_point_from_xy_syscall(public_key_x, public_key_y)
let public_key = Secp256r1Impl::secp256_ec_new_syscall(public_key_x, public_key_y)
.unwrap_syscall()
.unwrap();

Expand All @@ -39,48 +39,40 @@ fn get_message_and_signature() -> (u256, Signature, u256, u256, Secp256r1Point)
#[available_gas(100000000)]
fn test_verify_signature() {
let (msg_hash, signature, _, _, public_key) = get_message_and_signature();
let is_valid = verify_signature::<
let is_valid = is_valid_signature::<
Secp256r1Point
>(msg_hash, signature.r, signature.s, public_key)
.unwrap();
>(msg_hash, signature.r, signature.s, public_key);
assert(is_valid, 'Signature should be valid');
}

#[test]
#[available_gas(100000000)]
fn test_verify_signature_invalid_signature() {
let (msg_hash, signature, _, _, public_key) = get_message_and_signature();
let is_valid = verify_signature::<
let is_valid = is_valid_signature::<
Secp256r1Point
>(msg_hash, signature.r + 1, signature.s, public_key)
.unwrap();
>(msg_hash, signature.r + 1, signature.s, public_key);
assert(!is_valid, 'Signature should be invalid');
}

#[test]
#[should_panic(expected: ('Signature out of range',))]
#[available_gas(100000000)]
fn test_verify_signature_overflowing_signature_r() {
let (msg_hash, mut signature, _, _, public_key) = get_message_and_signature();
match verify_signature::<
let is_valid = is_valid_signature::<
Secp256r1Point
>(msg_hash, Secp256r1Impl::get_curve_size() + 1, signature.s, public_key) {
Result::Ok(_) => {},
Result::Err(err) => panic_with_felt252(err)
}
>(msg_hash, Secp256r1Impl::get_curve_size() + 1, signature.s, public_key);
assert(!is_valid, 'Signature out of range');
}

#[test]
#[should_panic(expected: ('Signature out of range',))]
#[available_gas(100000000)]
fn test_verify_signature_overflowing_signature_s() {
let (msg_hash, mut signature, _, _, public_key) = get_message_and_signature();
match verify_signature::<
let is_valid = is_valid_signature::<
Secp256r1Point
>(msg_hash, signature.r, Secp256r1Impl::get_curve_size() + 1, public_key) {
Result::Ok(_) => {},
Result::Err(err) => panic_with_felt252(err)
}
>(msg_hash, signature.r, Secp256r1Impl::get_curve_size() + 1, public_key);
assert(!is_valid, 'Signature out of range');
}


Expand Down

0 comments on commit e1ad258

Please sign in to comment.