Skip to content

Commit

Permalink
signature.move add secp256k1_verify function (#86)
Browse files Browse the repository at this point in the history
* signature.move add secp256k1_verify function

* optimze secp256k1_verify integration-tests case

Co-authored-by: tiankonglan <>
  • Loading branch information
tiankonglan authored and nkysg committed Mar 3, 2023
1 parent 5ea36b6 commit 2b64264
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
46 changes: 45 additions & 1 deletion integration-tests/natives/signature.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
processed 5 tasks
processed 8 tasks

task 2 'run'. lines 5-25:
{
Expand All @@ -23,3 +23,47 @@ task 4 'run'. lines 52-87:
"Keep": "Executed"
}
}

task 5 'run'. lines 91-104:
{
"gas_used": 65216,
"status": {
"Keep": "Executed"
}
}

task 6 'run'. lines 106-119:
{
"gas_used": 42272,
"status": {
"Keep": {
"MoveAbort": [
{
"Module": {
"address": "0x00000000000000000000000000000001",
"name": "Option"
}
},
263
]
}
}
}

task 7 'run'. lines 121-141:
{
"gas_used": 103702,
"status": {
"Keep": {
"MoveAbort": [
{
"Module": {
"address": "0x00000000000000000000000000000001",
"name": "Option"
}
},
263
]
}
}
}
54 changes: 54 additions & 0 deletions integration-tests/natives/signature.move
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,57 @@ script {
assert!(Option::is_none<EVMAddress>(&receover_address_opt), 1005);
}
}



//# run --signers creator

// test secp256k1_verify success case
script {
use StarcoinFramework::Signature;

fun main() {
//test success
let signature = x"90a938f7457df6e8f741264c32697fc52f9a8f867c52dd70713d9d2d472f2e415d9c94148991bbe1f4a1818d1dff09165782749c877f5cf1eff4ef126e55714d1c";
let msg_hash = x"b453bd4e271eed985cbab8231da609c4ce0a9cf1f763b6c1594e76315510e0f1";
let address_bytes = x"29c76e6ad8f28bb1004902578fb108c507be341b";
assert!(Signature::secp256k1_verify(copy signature, copy address_bytes, copy msg_hash), 1010);
}
}

//# run --signers creator

// test secp256k1_verify empty signature and empty msg case
script {
use StarcoinFramework::Signature;

fun main() {
//test empty data failed
let empty_signature = x"";
let empty_msg_hash = x"";
let address_bytes = x"29c76e6ad8f28bb1004902578fb108c507be341b";
assert!(!Signature::secp256k1_verify(empty_signature, copy address_bytes, empty_msg_hash), 1020);
}
}

//# run --signers creator

// test secp256k1_verify invalid hash or invalid signature case
script {
use StarcoinFramework::Signature;

fun main() {
//test success
let signature = x"90a938f7457df6e8f741264c32697fc52f9a8f867c52dd70713d9d2d472f2e415d9c94148991bbe1f4a1818d1dff09165782749c877f5cf1eff4ef126e55714d1c";
let msg_hash = x"b453bd4e271eed985cbab8231da609c4ce0a9cf1f763b6c1594e76315510e0f1";
let address_bytes = x"29c76e6ad8f28bb1004902578fb108c507be341b";

//test invalid hash, change the last char from 1 to 0
let invalid_msg_hash = x"b453bd4e271eed985cbab8231da609c4ce0a9cf1f763b6c1594e76315510e0f0";
assert!(!Signature::secp256k1_verify(signature, copy address_bytes, invalid_msg_hash), 1030);

// //test invalid signature, change the last char from 1 to 0
let invalid_signature = x"90a938f7457df6e8f741264c32697fc52f9a8f867c52dd70713d9d2d472f2e415d9c94148991bbe1f4a1818d1dff09165782749c877f5cf1eff4ef126e55714d10";
assert!(!Signature::secp256k1_verify(invalid_signature, address_bytes, msg_hash), 1031);
}
}
7 changes: 7 additions & 0 deletions sources/Signature.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ module Signature {
}
}

// verify eth secp256k1 sign and compare addr, if add equal return true
public fun secp256k1_verify(signature: vector<u8>, addr: vector<u8>, message: vector<u8>) : bool{
let receover_address_opt:Option<EVMAddress> = ecrecover(message, signature);
let expect_address = EVMAddress::new(addr);
&Option::destroy_some<EVMAddress>(receover_address_opt) == &expect_address
}

spec module {
pragma intrinsic = true;
}
Expand Down

0 comments on commit 2b64264

Please sign in to comment.