-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
crypto: add Move contract and tests for ecrecover to address #4543
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
crates/sui-config/tests/snapshots/snapshot_tests__empty_genesis_snapshot_matches.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ module math::ecdsa { | |
use sui::object::{Self, UID}; | ||
use sui::tx_context::TxContext; | ||
use sui::transfer; | ||
|
||
use std::vector; | ||
/// Event on whether the signature is verified | ||
struct VerifiedEvent has copy, drop { | ||
is_verified: bool, | ||
|
@@ -41,6 +41,48 @@ module math::ecdsa { | |
transfer::transfer(pubkey, recipient) | ||
} | ||
|
||
public entry fun ecrecover_to_eth_address(signature: vector<u8>, hashed_msg: vector<u8>, recipient: address, ctx: &mut TxContext) { | ||
// Normalize the last byte of the signature to be 0 or 1. | ||
let v = vector::borrow_mut(&mut signature, 64); | ||
if (*v == 27) { | ||
*v = 0; | ||
} else if (*v == 28) { | ||
*v = 1; | ||
} else if (*v > 35) { | ||
*v = (*v - 1) % 2; | ||
}; | ||
|
||
let pubkey = crypto::ecrecover(signature, hashed_msg); | ||
let uncompressed = crypto::decompress_pubkey(pubkey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! and good observation that in Eth they hash the uncompressed pub key. |
||
|
||
// Take the last 64 bytes of the uncompressed pubkey. | ||
let uncompressed_64 = vector::empty<u8>(); | ||
let i = 1; | ||
while (i < 65) { | ||
let value = vector::borrow(&uncompressed, i); | ||
vector::push_back(&mut uncompressed_64, *value); | ||
i = i + 1; | ||
}; | ||
|
||
// Take the last 20 bytes of the hash of the 64-bytes uncompressed pubkey. | ||
let hashed = crypto::keccak256(uncompressed_64); | ||
let addr = vector::empty<u8>(); | ||
let i = 12; | ||
while (i < 32) { | ||
let value = vector::borrow(&hashed, i); | ||
vector::push_back(&mut addr, *value); | ||
i = i + 1; | ||
}; | ||
|
||
let addr_object = Output { | ||
id: object::new(ctx), | ||
value: addr, | ||
}; | ||
|
||
// Transfer an output data object holding the address to the recipient. | ||
transfer::transfer(addr_object, recipient) | ||
} | ||
|
||
public entry fun secp256k1_verify(signature: vector<u8>, public_key: vector<u8>, hashed_msg: vector<u8>) { | ||
event::emit(VerifiedEvent {is_verified: crypto::secp256k1_verify(signature, public_key, hashed_msg)}); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Not to be addressed in this PR, but some ideas for improving this library)
vector<u8>
. This should make it easier to avoid common mistakes/misusecrypto
library, we could split it into a few smaller ones partitioned by (e.g.) signature scheme. No strong priors on how this should be done, but I think it will be easier to add new schemes in the future if we can do so by adding a fresh module instead of upgrading an existing on-chain moduleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, will follow up a PR: #4567