Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signature.move add secp256k1_verify function #86

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 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 6 tasks

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

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



//# run --signers creator

// test secp256k1_verify
script {
use StarcoinFramework::Signature;
// use StarcoinFramework::EVMAddress::{Self, EVMAddress};
// use StarcoinFramework::Option::{Self, Option};

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);

//test empty data failed
let empty_signature = x"";
let empty_msg_hash = x"";
assert!(!Signature::secp256k1_verify(empty_signature, copy address_bytes, empty_msg_hash), 1011);

//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), 1013);

// //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), 1014);
jolestar marked this conversation as resolved.
Show resolved Hide resolved

}
}
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