-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ec_recover funcs return Results (#1171)
* feat: return Reslut from er_recover funcs * test: update tests to handle returned results * style: forc fmt * fix: improve check for empty pubkey * refactor: remove asm from ec_recover_address * style: forc fmt * cleanup: remove Option import * refactor: move cast to Address type to implicit return * fix: update ec_recover functions to handle failures correctly * test: add tests for failure to recover * fix: remove unused error variant * cleanup: remove unused import of constants * test: clean up test with is_err()
- Loading branch information
Showing
3 changed files
with
68 additions
and
33 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,31 +1,42 @@ | ||
library ecr; | ||
|
||
use ::b512::B512; | ||
use ::address::Address; | ||
use ::b512::B512; | ||
use ::context::registers::error; | ||
use ::hash::{HashMethod, hash_pair}; | ||
use ::result::*; | ||
|
||
/// Recover the public key derived from the private key used to sign a message | ||
pub fn ec_recover(signature: B512, msg_hash: b256) -> B512 { | ||
let public_key = ~B512::new(); | ||
pub enum EcRecoverError { | ||
UnrecoverablePublicKey: (), | ||
} | ||
|
||
asm(buffer: public_key.bytes, sig: signature.bytes, hash: msg_hash) { | ||
/// Recover the public key derived from the private key used to sign a message. | ||
/// Returns a `Result` to let the caller choose an error handling strategy. | ||
pub fn ec_recover(signature: B512, msg_hash: b256) -> Result<B512, EcRecoverError> { | ||
let public_key = ~B512::new(); | ||
let was_error = asm(buffer: public_key.bytes, sig: signature.bytes, hash: msg_hash) { | ||
ecr buffer sig hash; | ||
err | ||
}; | ||
|
||
public_key | ||
// check the $err register to see if the `ecr` opcode succeeded | ||
if was_error == 1 { | ||
Result::Err(EcRecoverError::UnrecoverablePublicKey) | ||
} else { | ||
Result::Ok(public_key) | ||
} | ||
} | ||
|
||
/// Recover the address derived from the private key used to sign a message | ||
pub fn ec_recover_address(signature: B512, msg_hash: b256) -> Address { | ||
let address = asm(sig: signature.bytes, hash: msg_hash, addr_buffer, pub_key_buffer, hash_len: 64) { | ||
move addr_buffer sp; // Buffer for address. | ||
cfei i32; | ||
move pub_key_buffer sp; // Temporary buffer for recovered key. | ||
cfei i64; | ||
ecr pub_key_buffer sig hash; // Recover public_key from sig & hash. | ||
s256 addr_buffer pub_key_buffer hash_len; // Hash 64 bytes to the addr_buffer. | ||
cfsi i64; // Free temporary key buffer. | ||
addr_buffer: b256 | ||
}; | ||
/// Recover the address derived from the private key used to sign a message. | ||
/// Returns a `Result` to let the caller choose an error handling strategy. | ||
pub fn ec_recover_address(signature: B512, msg_hash: b256) -> Result<Address, EcRecoverError> { | ||
let pub_key_result = ec_recover(signature, msg_hash); | ||
|
||
~Address::from(address) | ||
if let Result::Err(e) = pub_key_result { | ||
// propagate the error if it exists | ||
Result::Err(e) | ||
} else { | ||
let pub_key = pub_key_result.unwrap(); | ||
let address = hash_pair((pub_key.bytes)[0], (pub_key.bytes)[1], HashMethod::Sha256); | ||
Result::Ok(~Address::from(address)) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
test/src/e2e_vm_tests/test_programs/should_pass/stdlib/ec_recover_test/Forc.toml
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "ec_recover_test" | ||
entry = "main.sw" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
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